xref: /openbmc/linux/drivers/ata/pata_it821x.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1669a5db4SJeff Garzik /*
2c343a839SAlan Cox  * pata_it821x.c 	- IT821x PATA for new ATA layer
3669a5db4SJeff Garzik  *			  (C) 2005 Red Hat Inc
4ab771630SAlan Cox  *			  Alan Cox <alan@lxorguk.ukuu.org.uk>
5374abf2cSBartlomiej Zolnierkiewicz  *			  (C) 2007 Bartlomiej Zolnierkiewicz
6669a5db4SJeff Garzik  *
7669a5db4SJeff Garzik  * based upon
8669a5db4SJeff Garzik  *
9669a5db4SJeff Garzik  * it821x.c
10669a5db4SJeff Garzik  *
11669a5db4SJeff Garzik  * linux/drivers/ide/pci/it821x.c		Version 0.09	December 2004
12669a5db4SJeff Garzik  *
13ab771630SAlan Cox  * Copyright (C) 2004		Red Hat
14669a5db4SJeff Garzik  *
15669a5db4SJeff Garzik  *  May be copied or modified under the terms of the GNU General Public License
16669a5db4SJeff Garzik  *  Based in part on the ITE vendor provided SCSI driver.
17669a5db4SJeff Garzik  *
18631dd1a8SJustin P. Mattock  *  Documentation available from IT8212F_V04.pdf
19631dd1a8SJustin P. Mattock  * 	http://www.ite.com.tw/EN/products_more.aspx?CategoryID=3&ID=5,91
20669a5db4SJeff Garzik  *  Some other documents are NDA.
21669a5db4SJeff Garzik  *
22669a5db4SJeff Garzik  *  The ITE8212 isn't exactly a standard IDE controller. It has two
23669a5db4SJeff Garzik  *  modes. In pass through mode then it is an IDE controller. In its smart
24669a5db4SJeff Garzik  *  mode its actually quite a capable hardware raid controller disguised
25669a5db4SJeff Garzik  *  as an IDE controller. Smart mode only understands DMA read/write and
26669a5db4SJeff Garzik  *  identify, none of the fancier commands apply. The IT8211 is identical
27669a5db4SJeff Garzik  *  in other respects but lacks the raid mode.
28669a5db4SJeff Garzik  *
29669a5db4SJeff Garzik  *  Errata:
30669a5db4SJeff Garzik  *  o	Rev 0x10 also requires master/slave hold the same DMA timings and
31669a5db4SJeff Garzik  *	cannot do ATAPI MWDMA.
32669a5db4SJeff Garzik  *  o	The identify data for raid volumes lacks CHS info (technically ok)
33669a5db4SJeff Garzik  *	but also fails to set the LBA28 and other bits. We fix these in
34669a5db4SJeff Garzik  *	the IDE probe quirk code.
35669a5db4SJeff Garzik  *  o	If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
36669a5db4SJeff Garzik  *	raid then the controller firmware dies
37669a5db4SJeff Garzik  *  o	Smart mode without RAID doesn't clear all the necessary identify
38669a5db4SJeff Garzik  *	bits to reduce the command set to the one used
39669a5db4SJeff Garzik  *
40669a5db4SJeff Garzik  *  This has a few impacts on the driver
41669a5db4SJeff Garzik  *  - In pass through mode we do all the work you would expect
42669a5db4SJeff Garzik  *  - In smart mode the clocking set up is done by the controller generally
43669a5db4SJeff Garzik  *    but we must watch the other limits and filter.
44669a5db4SJeff Garzik  *  - There are a few extra vendor commands that actually talk to the
45669a5db4SJeff Garzik  *    controller but only work PIO with no IRQ.
46669a5db4SJeff Garzik  *
47669a5db4SJeff Garzik  *  Vendor areas of the identify block in smart mode are used for the
48669a5db4SJeff Garzik  *  timing and policy set up. Each HDD in raid mode also has a serial
49669a5db4SJeff Garzik  *  block on the disk. The hardware extra commands are get/set chip status,
50669a5db4SJeff Garzik  *  rebuild, get rebuild status.
51669a5db4SJeff Garzik  *
52669a5db4SJeff Garzik  *  In Linux the driver supports pass through mode as if the device was
53669a5db4SJeff Garzik  *  just another IDE controller. If the smart mode is running then
54669a5db4SJeff Garzik  *  volumes are managed by the controller firmware and each IDE "disk"
55669a5db4SJeff Garzik  *  is a raid volume. Even more cute - the controller can do automated
56669a5db4SJeff Garzik  *  hotplug and rebuild.
57669a5db4SJeff Garzik  *
58669a5db4SJeff Garzik  *  The pass through controller itself is a little demented. It has a
59669a5db4SJeff Garzik  *  flaw that it has a single set of PIO/MWDMA timings per channel so
60669a5db4SJeff Garzik  *  non UDMA devices restrict each others performance. It also has a
61669a5db4SJeff Garzik  *  single clock source per channel so mixed UDMA100/133 performance
62669a5db4SJeff Garzik  *  isn't perfect and we have to pick a clock. Thankfully none of this
63669a5db4SJeff Garzik  *  matters in smart mode. ATAPI DMA is not currently supported.
64669a5db4SJeff Garzik  *
65669a5db4SJeff Garzik  *  It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
66669a5db4SJeff Garzik  *
67669a5db4SJeff Garzik  *  TODO
68669a5db4SJeff Garzik  *	-	ATAPI and other speed filtering
69669a5db4SJeff Garzik  *	-	RAID configuration ioctls
70669a5db4SJeff Garzik  */
71669a5db4SJeff Garzik 
72669a5db4SJeff Garzik #include <linux/kernel.h>
73669a5db4SJeff Garzik #include <linux/module.h>
74669a5db4SJeff Garzik #include <linux/pci.h>
75669a5db4SJeff Garzik #include <linux/blkdev.h>
76669a5db4SJeff Garzik #include <linux/delay.h>
775a0e3ad6STejun Heo #include <linux/slab.h>
78669a5db4SJeff Garzik #include <scsi/scsi_host.h>
79669a5db4SJeff Garzik #include <linux/libata.h>
80669a5db4SJeff Garzik 
81669a5db4SJeff Garzik 
82669a5db4SJeff Garzik #define DRV_NAME "pata_it821x"
834a99d95fSAlan Cox #define DRV_VERSION "0.4.2"
84669a5db4SJeff Garzik 
85669a5db4SJeff Garzik struct it821x_dev
86669a5db4SJeff Garzik {
87669a5db4SJeff Garzik 	unsigned int smart:1,		/* Are we in smart raid mode */
88669a5db4SJeff Garzik 		timing10:1;		/* Rev 0x10 */
89669a5db4SJeff Garzik 	u8	clock_mode;		/* 0, ATA_50 or ATA_66 */
90669a5db4SJeff Garzik 	u8	want[2][2];		/* Mode/Pri log for master slave */
91669a5db4SJeff Garzik 	/* We need these for switching the clock when DMA goes on/off
92669a5db4SJeff Garzik 	   The high byte is the 66Mhz timing */
93669a5db4SJeff Garzik 	u16	pio[2];			/* Cached PIO values */
94669a5db4SJeff Garzik 	u16	mwdma[2];		/* Cached MWDMA values */
95669a5db4SJeff Garzik 	u16	udma[2];		/* Cached UDMA values (per drive) */
96669a5db4SJeff Garzik 	u16	last_device;		/* Master or slave loaded ? */
97669a5db4SJeff Garzik };
98669a5db4SJeff Garzik 
99669a5db4SJeff Garzik #define ATA_66		0
100669a5db4SJeff Garzik #define ATA_50		1
101669a5db4SJeff Garzik #define ATA_ANY		2
102669a5db4SJeff Garzik 
103669a5db4SJeff Garzik #define UDMA_OFF	0
104669a5db4SJeff Garzik #define MWDMA_OFF	0
105669a5db4SJeff Garzik 
106669a5db4SJeff Garzik /*
107669a5db4SJeff Garzik  *	We allow users to force the card into non raid mode without
1083a4fa0a2SRobert P. J. Day  *	flashing the alternative BIOS. This is also necessary right now
109669a5db4SJeff Garzik  *	for embedded platforms that cannot run a PC BIOS but are using this
110669a5db4SJeff Garzik  *	device.
111669a5db4SJeff Garzik  */
112669a5db4SJeff Garzik 
113669a5db4SJeff Garzik static int it8212_noraid;
114669a5db4SJeff Garzik 
115669a5db4SJeff Garzik /**
116669a5db4SJeff Garzik  *	it821x_program	-	program the PIO/MWDMA registers
117669a5db4SJeff Garzik  *	@ap: ATA port
118669a5db4SJeff Garzik  *	@adev: Device to program
119669a5db4SJeff Garzik  *	@timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
120669a5db4SJeff Garzik  *
121669a5db4SJeff Garzik  *	Program the PIO/MWDMA timing for this channel according to the
122669a5db4SJeff Garzik  *	current clock. These share the same register so are managed by
123669a5db4SJeff Garzik  *	the DMA start/stop sequence as with the old driver.
124669a5db4SJeff Garzik  */
125669a5db4SJeff Garzik 
it821x_program(struct ata_port * ap,struct ata_device * adev,u16 timing)126669a5db4SJeff Garzik static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
127669a5db4SJeff Garzik {
128669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
129669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
130669a5db4SJeff Garzik 	int channel = ap->port_no;
131669a5db4SJeff Garzik 	u8 conf;
132669a5db4SJeff Garzik 
133669a5db4SJeff Garzik 	/* Program PIO/MWDMA timing bits */
134669a5db4SJeff Garzik 	if (itdev->clock_mode == ATA_66)
135669a5db4SJeff Garzik 		conf = timing >> 8;
136669a5db4SJeff Garzik 	else
137669a5db4SJeff Garzik 		conf = timing & 0xFF;
138669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
139669a5db4SJeff Garzik }
140669a5db4SJeff Garzik 
141669a5db4SJeff Garzik 
142669a5db4SJeff Garzik /**
143669a5db4SJeff Garzik  *	it821x_program_udma	-	program the UDMA registers
144669a5db4SJeff Garzik  *	@ap: ATA port
145669a5db4SJeff Garzik  *	@adev: ATA device to update
146669a5db4SJeff Garzik  *	@timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
147669a5db4SJeff Garzik  *
148669a5db4SJeff Garzik  *	Program the UDMA timing for this drive according to the
149669a5db4SJeff Garzik  *	current clock. Handles the dual clocks and also knows about
150669a5db4SJeff Garzik  *	the errata on the 0x10 revision. The UDMA errata is partly handled
151669a5db4SJeff Garzik  *	here and partly in start_dma.
152669a5db4SJeff Garzik  */
153669a5db4SJeff Garzik 
it821x_program_udma(struct ata_port * ap,struct ata_device * adev,u16 timing)154669a5db4SJeff Garzik static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
155669a5db4SJeff Garzik {
156669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
157669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
158669a5db4SJeff Garzik 	int channel = ap->port_no;
159669a5db4SJeff Garzik 	int unit = adev->devno;
160669a5db4SJeff Garzik 	u8 conf;
161669a5db4SJeff Garzik 
162669a5db4SJeff Garzik 	/* Program UDMA timing bits */
163669a5db4SJeff Garzik 	if (itdev->clock_mode == ATA_66)
164669a5db4SJeff Garzik 		conf = timing >> 8;
165669a5db4SJeff Garzik 	else
166669a5db4SJeff Garzik 		conf = timing & 0xFF;
167669a5db4SJeff Garzik 	if (itdev->timing10 == 0)
168669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
169669a5db4SJeff Garzik 	else {
170669a5db4SJeff Garzik 		/* Early revision must be programmed for both together */
171669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
172669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
173669a5db4SJeff Garzik 	}
174669a5db4SJeff Garzik }
175669a5db4SJeff Garzik 
176669a5db4SJeff Garzik /**
177669a5db4SJeff Garzik  *	it821x_clock_strategy
178669a5db4SJeff Garzik  *	@ap: ATA interface
179669a5db4SJeff Garzik  *	@adev: ATA device being updated
180669a5db4SJeff Garzik  *
181669a5db4SJeff Garzik  *	Select between the 50 and 66Mhz base clocks to get the best
182669a5db4SJeff Garzik  *	results for this interface.
183669a5db4SJeff Garzik  */
184669a5db4SJeff Garzik 
it821x_clock_strategy(struct ata_port * ap,struct ata_device * adev)185669a5db4SJeff Garzik static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
186669a5db4SJeff Garzik {
187669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
188669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
189669a5db4SJeff Garzik 	u8 unit = adev->devno;
190669a5db4SJeff Garzik 	struct ata_device *pair = ata_dev_pair(adev);
191669a5db4SJeff Garzik 
192669a5db4SJeff Garzik 	int clock, altclock;
193669a5db4SJeff Garzik 	u8 v;
194669a5db4SJeff Garzik 	int sel = 0;
195669a5db4SJeff Garzik 
196669a5db4SJeff Garzik 	/* Look for the most wanted clocking */
197669a5db4SJeff Garzik 	if (itdev->want[0][0] > itdev->want[1][0]) {
198669a5db4SJeff Garzik 		clock = itdev->want[0][1];
199669a5db4SJeff Garzik 		altclock = itdev->want[1][1];
200669a5db4SJeff Garzik 	} else {
201669a5db4SJeff Garzik 		clock = itdev->want[1][1];
202669a5db4SJeff Garzik 		altclock = itdev->want[0][1];
203669a5db4SJeff Garzik 	}
204669a5db4SJeff Garzik 
205669a5db4SJeff Garzik 	/* Master doesn't care does the slave ? */
206669a5db4SJeff Garzik 	if (clock == ATA_ANY)
207669a5db4SJeff Garzik 		clock = altclock;
208669a5db4SJeff Garzik 
209669a5db4SJeff Garzik 	/* Nobody cares - keep the same clock */
210669a5db4SJeff Garzik 	if (clock == ATA_ANY)
211669a5db4SJeff Garzik 		return;
212669a5db4SJeff Garzik 	/* No change */
213669a5db4SJeff Garzik 	if (clock == itdev->clock_mode)
214669a5db4SJeff Garzik 		return;
215669a5db4SJeff Garzik 
216669a5db4SJeff Garzik 	/* Load this into the controller */
217669a5db4SJeff Garzik 	if (clock == ATA_66)
218669a5db4SJeff Garzik 		itdev->clock_mode = ATA_66;
219669a5db4SJeff Garzik 	else {
220669a5db4SJeff Garzik 		itdev->clock_mode = ATA_50;
221669a5db4SJeff Garzik 		sel = 1;
222669a5db4SJeff Garzik 	}
223669a5db4SJeff Garzik 	pci_read_config_byte(pdev, 0x50, &v);
224669a5db4SJeff Garzik 	v &= ~(1 << (1 + ap->port_no));
225669a5db4SJeff Garzik 	v |= sel << (1 + ap->port_no);
226669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x50, v);
227669a5db4SJeff Garzik 
228669a5db4SJeff Garzik 	/*
229669a5db4SJeff Garzik 	 *	Reprogram the UDMA/PIO of the pair drive for the switch
230669a5db4SJeff Garzik 	 *	MWDMA will be dealt with by the dma switcher
231669a5db4SJeff Garzik 	 */
232669a5db4SJeff Garzik 	if (pair && itdev->udma[1-unit] != UDMA_OFF) {
233669a5db4SJeff Garzik 		it821x_program_udma(ap, pair, itdev->udma[1-unit]);
234669a5db4SJeff Garzik 		it821x_program(ap, pair, itdev->pio[1-unit]);
235669a5db4SJeff Garzik 	}
236669a5db4SJeff Garzik 	/*
237669a5db4SJeff Garzik 	 *	Reprogram the UDMA/PIO of our drive for the switch.
238669a5db4SJeff Garzik 	 *	MWDMA will be dealt with by the dma switcher
239669a5db4SJeff Garzik 	 */
240669a5db4SJeff Garzik 	if (itdev->udma[unit] != UDMA_OFF) {
241669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
242669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[unit]);
243669a5db4SJeff Garzik 	}
244669a5db4SJeff Garzik }
245669a5db4SJeff Garzik 
246669a5db4SJeff Garzik /**
247669a5db4SJeff Garzik  *	it821x_passthru_set_piomode	-	set PIO mode data
248669a5db4SJeff Garzik  *	@ap: ATA interface
249669a5db4SJeff Garzik  *	@adev: ATA device
250669a5db4SJeff Garzik  *
251669a5db4SJeff Garzik  *	Configure for PIO mode. This is complicated as the register is
252669a5db4SJeff Garzik  *	shared by PIO and MWDMA and for both channels.
253669a5db4SJeff Garzik  */
254669a5db4SJeff Garzik 
it821x_passthru_set_piomode(struct ata_port * ap,struct ata_device * adev)255669a5db4SJeff Garzik static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
256669a5db4SJeff Garzik {
257669a5db4SJeff Garzik 	/* Spec says 89 ref driver uses 88 */
258669a5db4SJeff Garzik 	static const u16 pio[]	= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
259669a5db4SJeff Garzik 	static const u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
260669a5db4SJeff Garzik 
261669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
262669a5db4SJeff Garzik 	int unit = adev->devno;
263669a5db4SJeff Garzik 	int mode_wanted = adev->pio_mode - XFER_PIO_0;
264669a5db4SJeff Garzik 
265669a5db4SJeff Garzik 	/* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
266669a5db4SJeff Garzik 	itdev->want[unit][1] = pio_want[mode_wanted];
267669a5db4SJeff Garzik 	itdev->want[unit][0] = 1;	/* PIO is lowest priority */
268669a5db4SJeff Garzik 	itdev->pio[unit] = pio[mode_wanted];
269669a5db4SJeff Garzik 	it821x_clock_strategy(ap, adev);
270669a5db4SJeff Garzik 	it821x_program(ap, adev, itdev->pio[unit]);
271669a5db4SJeff Garzik }
272669a5db4SJeff Garzik 
273669a5db4SJeff Garzik /**
274669a5db4SJeff Garzik  *	it821x_passthru_set_dmamode	-	set initial DMA mode data
275669a5db4SJeff Garzik  *	@ap: ATA interface
276669a5db4SJeff Garzik  *	@adev: ATA device
277669a5db4SJeff Garzik  *
278669a5db4SJeff Garzik  *	Set up the DMA modes. The actions taken depend heavily on the mode
279669a5db4SJeff Garzik  *	to use. If UDMA is used as is hopefully the usual case then the
280669a5db4SJeff Garzik  *	timing register is private and we need only consider the clock. If
281669a5db4SJeff Garzik  *	we are using MWDMA then we have to manage the setting ourself as
282669a5db4SJeff Garzik  *	we switch devices and mode.
283669a5db4SJeff Garzik  */
284669a5db4SJeff Garzik 
it821x_passthru_set_dmamode(struct ata_port * ap,struct ata_device * adev)285669a5db4SJeff Garzik static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
286669a5db4SJeff Garzik {
287669a5db4SJeff Garzik 	static const u16 dma[]	= 	{ 0x8866, 0x3222, 0x3121 };
288669a5db4SJeff Garzik 	static const u8 mwdma_want[] =  { ATA_ANY, ATA_66, ATA_ANY };
289669a5db4SJeff Garzik 	static const u16 udma[]	= 	{ 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
290669a5db4SJeff Garzik 	static const u8 udma_want[] =   { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
291669a5db4SJeff Garzik 
292669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
293669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
294669a5db4SJeff Garzik 	int channel = ap->port_no;
295669a5db4SJeff Garzik 	int unit = adev->devno;
296669a5db4SJeff Garzik 	u8 conf;
297669a5db4SJeff Garzik 
298669a5db4SJeff Garzik 	if (adev->dma_mode >= XFER_UDMA_0) {
299669a5db4SJeff Garzik 		int mode_wanted = adev->dma_mode - XFER_UDMA_0;
300669a5db4SJeff Garzik 
301669a5db4SJeff Garzik 		itdev->want[unit][1] = udma_want[mode_wanted];
302669a5db4SJeff Garzik 		itdev->want[unit][0] = 3;	/* UDMA is high priority */
303669a5db4SJeff Garzik 		itdev->mwdma[unit] = MWDMA_OFF;
304669a5db4SJeff Garzik 		itdev->udma[unit] = udma[mode_wanted];
305669a5db4SJeff Garzik 		if (mode_wanted >= 5)
306669a5db4SJeff Garzik 			itdev->udma[unit] |= 0x8080;	/* UDMA 5/6 select on */
307669a5db4SJeff Garzik 
308669a5db4SJeff Garzik 		/* UDMA on. Again revision 0x10 must do the pair */
309669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
310669a5db4SJeff Garzik 		if (itdev->timing10)
311669a5db4SJeff Garzik 			conf &= channel ? 0x9F: 0xE7;
312669a5db4SJeff Garzik 		else
313669a5db4SJeff Garzik 			conf &= ~ (1 << (3 + 2 * channel + unit));
314669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x50, conf);
315669a5db4SJeff Garzik 		it821x_clock_strategy(ap, adev);
316669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
317669a5db4SJeff Garzik 	} else {
318669a5db4SJeff Garzik 		int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
319669a5db4SJeff Garzik 
320669a5db4SJeff Garzik 		itdev->want[unit][1] = mwdma_want[mode_wanted];
321669a5db4SJeff Garzik 		itdev->want[unit][0] = 2;	/* MWDMA is low priority */
322669a5db4SJeff Garzik 		itdev->mwdma[unit] = dma[mode_wanted];
323669a5db4SJeff Garzik 		itdev->udma[unit] = UDMA_OFF;
324669a5db4SJeff Garzik 
325669a5db4SJeff Garzik 		/* UDMA bits off - Revision 0x10 do them in pairs */
326669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
327669a5db4SJeff Garzik 		if (itdev->timing10)
328669a5db4SJeff Garzik 			conf |= channel ? 0x60: 0x18;
329669a5db4SJeff Garzik 		else
330669a5db4SJeff Garzik 			conf |= 1 << (3 + 2 * channel + unit);
331669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x50, conf);
332669a5db4SJeff Garzik 		it821x_clock_strategy(ap, adev);
333669a5db4SJeff Garzik 	}
334669a5db4SJeff Garzik }
335669a5db4SJeff Garzik 
336669a5db4SJeff Garzik /**
33739ac4018SLee Jones  *	it821x_passthru_bmdma_start	-	DMA start callback
338669a5db4SJeff Garzik  *	@qc: Command in progress
339669a5db4SJeff Garzik  *
340669a5db4SJeff Garzik  *	Usually drivers set the DMA timing at the point the set_dmamode call
341669a5db4SJeff Garzik  *	is made. IT821x however requires we load new timings on the
342669a5db4SJeff Garzik  *	transitions in some cases.
343669a5db4SJeff Garzik  */
344669a5db4SJeff Garzik 
it821x_passthru_bmdma_start(struct ata_queued_cmd * qc)345669a5db4SJeff Garzik static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
346669a5db4SJeff Garzik {
347669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
348669a5db4SJeff Garzik 	struct ata_device *adev = qc->dev;
349669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
350669a5db4SJeff Garzik 	int unit = adev->devno;
351669a5db4SJeff Garzik 
352669a5db4SJeff Garzik 	if (itdev->mwdma[unit] != MWDMA_OFF)
353669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->mwdma[unit]);
354669a5db4SJeff Garzik 	else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
355669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
356669a5db4SJeff Garzik 	ata_bmdma_start(qc);
357669a5db4SJeff Garzik }
358669a5db4SJeff Garzik 
359669a5db4SJeff Garzik /**
36039ac4018SLee Jones  *	it821x_passthru_bmdma_stop	-	DMA stop callback
361669a5db4SJeff Garzik  *	@qc: ATA command
362669a5db4SJeff Garzik  *
363669a5db4SJeff Garzik  *	We loaded new timings in dma_start, as a result we need to restore
364669a5db4SJeff Garzik  *	the PIO timings in dma_stop so that the next command issue gets the
365669a5db4SJeff Garzik  *	right clock values.
366669a5db4SJeff Garzik  */
367669a5db4SJeff Garzik 
it821x_passthru_bmdma_stop(struct ata_queued_cmd * qc)368669a5db4SJeff Garzik static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
369669a5db4SJeff Garzik {
370669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
371669a5db4SJeff Garzik 	struct ata_device *adev = qc->dev;
372669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
373669a5db4SJeff Garzik 	int unit = adev->devno;
374669a5db4SJeff Garzik 
375669a5db4SJeff Garzik 	ata_bmdma_stop(qc);
376669a5db4SJeff Garzik 	if (itdev->mwdma[unit] != MWDMA_OFF)
377669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[unit]);
378669a5db4SJeff Garzik }
379669a5db4SJeff Garzik 
380669a5db4SJeff Garzik 
381669a5db4SJeff Garzik /**
382669a5db4SJeff Garzik  *	it821x_passthru_dev_select	-	Select master/slave
383669a5db4SJeff Garzik  *	@ap: ATA port
384669a5db4SJeff Garzik  *	@device: Device number (not pointer)
385669a5db4SJeff Garzik  *
3863a4fa0a2SRobert P. J. Day  *	Device selection hook. If necessary perform clock switching
387669a5db4SJeff Garzik  */
388669a5db4SJeff Garzik 
it821x_passthru_dev_select(struct ata_port * ap,unsigned int device)389669a5db4SJeff Garzik static void it821x_passthru_dev_select(struct ata_port *ap,
390669a5db4SJeff Garzik 				       unsigned int device)
391669a5db4SJeff Garzik {
392669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
393669a5db4SJeff Garzik 	if (itdev && device != itdev->last_device) {
3949af5c9c9STejun Heo 		struct ata_device *adev = &ap->link.device[device];
395669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[adev->devno]);
396669a5db4SJeff Garzik 		itdev->last_device = device;
397669a5db4SJeff Garzik 	}
3989363c382STejun Heo 	ata_sff_dev_select(ap, device);
399669a5db4SJeff Garzik }
400669a5db4SJeff Garzik 
401669a5db4SJeff Garzik /**
4029363c382STejun Heo  *	it821x_smart_qc_issue		-	wrap qc issue prot
403669a5db4SJeff Garzik  *	@qc: command
404669a5db4SJeff Garzik  *
405669a5db4SJeff Garzik  *	Wrap the command issue sequence for the IT821x. We need to
406669a5db4SJeff Garzik  *	perform out own device selection timing loads before the
407669a5db4SJeff Garzik  *	usual happenings kick off
408669a5db4SJeff Garzik  */
409669a5db4SJeff Garzik 
it821x_smart_qc_issue(struct ata_queued_cmd * qc)4109363c382STejun Heo static unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc)
411669a5db4SJeff Garzik {
412669a5db4SJeff Garzik 	switch(qc->tf.command)
413669a5db4SJeff Garzik 	{
414669a5db4SJeff Garzik 		/* Commands the firmware supports */
415669a5db4SJeff Garzik 		case ATA_CMD_READ:
416669a5db4SJeff Garzik 		case ATA_CMD_READ_EXT:
417669a5db4SJeff Garzik 		case ATA_CMD_WRITE:
418669a5db4SJeff Garzik 		case ATA_CMD_WRITE_EXT:
419669a5db4SJeff Garzik 		case ATA_CMD_PIO_READ:
420669a5db4SJeff Garzik 		case ATA_CMD_PIO_READ_EXT:
421669a5db4SJeff Garzik 		case ATA_CMD_PIO_WRITE:
422669a5db4SJeff Garzik 		case ATA_CMD_PIO_WRITE_EXT:
423669a5db4SJeff Garzik 		case ATA_CMD_READ_MULTI:
424669a5db4SJeff Garzik 		case ATA_CMD_READ_MULTI_EXT:
425669a5db4SJeff Garzik 		case ATA_CMD_WRITE_MULTI:
426669a5db4SJeff Garzik 		case ATA_CMD_WRITE_MULTI_EXT:
427669a5db4SJeff Garzik 		case ATA_CMD_ID_ATA:
428963e4975SAlan Cox 		case ATA_CMD_INIT_DEV_PARAMS:
429963e4975SAlan Cox 		case 0xFC:	/* Internal 'report rebuild state' */
430669a5db4SJeff Garzik 		/* Arguably should just no-op this one */
431669a5db4SJeff Garzik 		case ATA_CMD_SET_FEATURES:
432360ff783STejun Heo 			return ata_bmdma_qc_issue(qc);
433669a5db4SJeff Garzik 	}
4343697aaafSHannes Reinecke 	ata_dev_dbg(qc->dev, "it821x: can't process command 0x%02X\n",
4353697aaafSHannes Reinecke 		    qc->tf.command);
436c5038fc0SAlan Cox 	return AC_ERR_DEV;
437669a5db4SJeff Garzik }
438669a5db4SJeff Garzik 
439669a5db4SJeff Garzik /**
4409363c382STejun Heo  *	it821x_passthru_qc_issue	-	wrap qc issue prot
441669a5db4SJeff Garzik  *	@qc: command
442669a5db4SJeff Garzik  *
443669a5db4SJeff Garzik  *	Wrap the command issue sequence for the IT821x. We need to
444669a5db4SJeff Garzik  *	perform out own device selection timing loads before the
445669a5db4SJeff Garzik  *	usual happenings kick off
446669a5db4SJeff Garzik  */
447669a5db4SJeff Garzik 
it821x_passthru_qc_issue(struct ata_queued_cmd * qc)4489363c382STejun Heo static unsigned int it821x_passthru_qc_issue(struct ata_queued_cmd *qc)
449669a5db4SJeff Garzik {
450669a5db4SJeff Garzik 	it821x_passthru_dev_select(qc->ap, qc->dev->devno);
451360ff783STejun Heo 	return ata_bmdma_qc_issue(qc);
452669a5db4SJeff Garzik }
453669a5db4SJeff Garzik 
454669a5db4SJeff Garzik /**
455669a5db4SJeff Garzik  *	it821x_smart_set_mode	-	mode setting
4560260731fSTejun Heo  *	@link: interface to set up
457b229a7b0SAlan  *	@unused: device that failed (error only)
458669a5db4SJeff Garzik  *
459669a5db4SJeff Garzik  *	Use a non standard set_mode function. We don't want to be tuned.
460669a5db4SJeff Garzik  *	The BIOS configured everything. Our job is not to fiddle. We
461669a5db4SJeff Garzik  *	read the dma enabled bits from the PCI configuration of the device
462669a5db4SJeff Garzik  *	and respect them.
463669a5db4SJeff Garzik  */
464669a5db4SJeff Garzik 
it821x_smart_set_mode(struct ata_link * link,struct ata_device ** unused)4650260731fSTejun Heo static int it821x_smart_set_mode(struct ata_link *link, struct ata_device **unused)
466669a5db4SJeff Garzik {
467f58229f8STejun Heo 	struct ata_device *dev;
468669a5db4SJeff Garzik 
4691eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
470669a5db4SJeff Garzik 		/* We don't really care */
471669a5db4SJeff Garzik 		dev->pio_mode = XFER_PIO_0;
472669a5db4SJeff Garzik 		dev->dma_mode = XFER_MW_DMA_0;
473669a5db4SJeff Garzik 		/* We do need the right mode information for DMA or PIO
474669a5db4SJeff Garzik 		   and this comes from the current configuration flags */
475374abf2cSBartlomiej Zolnierkiewicz 		if (ata_id_has_dma(dev->id)) {
476a9a79dfeSJoe Perches 			ata_dev_info(dev, "configured for DMA\n");
477669a5db4SJeff Garzik 			dev->xfer_mode = XFER_MW_DMA_0;
478669a5db4SJeff Garzik 			dev->xfer_shift = ATA_SHIFT_MWDMA;
479669a5db4SJeff Garzik 			dev->flags &= ~ATA_DFLAG_PIO;
480669a5db4SJeff Garzik 		} else {
481a9a79dfeSJoe Perches 			ata_dev_info(dev, "configured for PIO\n");
482669a5db4SJeff Garzik 			dev->xfer_mode = XFER_PIO_0;
483669a5db4SJeff Garzik 			dev->xfer_shift = ATA_SHIFT_PIO;
484669a5db4SJeff Garzik 			dev->flags |= ATA_DFLAG_PIO;
485669a5db4SJeff Garzik 		}
486669a5db4SJeff Garzik 	}
487b229a7b0SAlan 	return 0;
488669a5db4SJeff Garzik }
489669a5db4SJeff Garzik 
490669a5db4SJeff Garzik /**
491669a5db4SJeff Garzik  *	it821x_dev_config	-	Called each device identify
492669a5db4SJeff Garzik  *	@adev: Device that has just been identified
493669a5db4SJeff Garzik  *
494669a5db4SJeff Garzik  *	Perform the initial setup needed for each device that is chip
495669a5db4SJeff Garzik  *	special. In our case we need to lock the sector count to avoid
496669a5db4SJeff Garzik  *	blowing the brains out of the firmware with large LBA48 requests
497669a5db4SJeff Garzik  *
498669a5db4SJeff Garzik  */
499669a5db4SJeff Garzik 
it821x_dev_config(struct ata_device * adev)500cd0d3bbcSAlan static void it821x_dev_config(struct ata_device *adev)
501669a5db4SJeff Garzik {
5028bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
503669a5db4SJeff Garzik 
5048bfa79fcSTejun Heo 	ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
505669a5db4SJeff Garzik 
506669a5db4SJeff Garzik 	if (adev->max_sectors > 255)
507669a5db4SJeff Garzik 		adev->max_sectors = 255;
508669a5db4SJeff Garzik 
509669a5db4SJeff Garzik 	if (strstr(model_num, "Integrated Technology Express")) {
510669a5db4SJeff Garzik 		/* RAID mode */
5113697aaafSHannes Reinecke 		if (adev->id[129] == 1)
5123697aaafSHannes Reinecke 			ata_dev_info(adev, "%sRAID%d volume\n",
513669a5db4SJeff Garzik 				     adev->id[147] ? "Bootable " : "",
514669a5db4SJeff Garzik 				     adev->id[129]);
5153697aaafSHannes Reinecke 		else
5163697aaafSHannes Reinecke 			ata_dev_info(adev, "%sRAID%d volume (%dK stripe)\n",
5173697aaafSHannes Reinecke 				     adev->id[147] ? "Bootable " : "",
5183697aaafSHannes Reinecke 				     adev->id[129], adev->id[146]);
519669a5db4SJeff Garzik 	}
520c5038fc0SAlan Cox 	/* This is a controller firmware triggered funny, don't
521c5038fc0SAlan Cox 	   report the drive faulty! */
522c5038fc0SAlan Cox 	adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
523963e4975SAlan Cox 	/* No HPA in 'smart' mode */
524963e4975SAlan Cox 	adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
525c5038fc0SAlan Cox }
526c5038fc0SAlan Cox 
527c5038fc0SAlan Cox /**
528963e4975SAlan Cox  *	it821x_read_id	-	Hack identify data up
529963e4975SAlan Cox  *	@adev: device to read
530963e4975SAlan Cox  *	@tf: proposed taskfile
531963e4975SAlan Cox  *	@id: buffer for returned ident data
532c5038fc0SAlan Cox  *
533963e4975SAlan Cox  *	Query the devices on this firmware driven port and slightly
534c5038fc0SAlan Cox  *	mash the identify data to stop us and common tools trying to
535c5038fc0SAlan Cox  *	use features not firmware supported. The firmware itself does
536c5038fc0SAlan Cox  *	some masking (eg SMART) but not enough.
537c5038fc0SAlan Cox  */
538c5038fc0SAlan Cox 
it821x_read_id(struct ata_device * adev,struct ata_taskfile * tf,__le16 * id)539963e4975SAlan Cox static unsigned int it821x_read_id(struct ata_device *adev,
5400561e514SDamien Le Moal 				   struct ata_taskfile *tf, __le16 *id)
541c5038fc0SAlan Cox {
542963e4975SAlan Cox 	unsigned int err_mask;
543963e4975SAlan Cox 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
544669a5db4SJeff Garzik 
545963e4975SAlan Cox 	err_mask = ata_do_dev_read_id(adev, tf, id);
546963e4975SAlan Cox 	if (err_mask)
547963e4975SAlan Cox 		return err_mask;
5480561e514SDamien Le Moal 	ata_id_c_string((u16 *)id, model_num, ATA_ID_PROD, sizeof(model_num));
549963e4975SAlan Cox 
5500561e514SDamien Le Moal 	id[83] &= cpu_to_le16(~(1 << 12)); /* Cache flush is firmware handled */
5510561e514SDamien Le Moal 	id[84] &= cpu_to_le16(~(1 << 6));  /* No FUA */
5520561e514SDamien Le Moal 	id[85] &= cpu_to_le16(~(1 << 10)); /* No HPA */
553963e4975SAlan Cox 	id[76] = 0;			   /* No NCQ/AN etc */
554963e4975SAlan Cox 
555963e4975SAlan Cox 	if (strstr(model_num, "Integrated Technology Express")) {
556963e4975SAlan Cox 		/* Set feature bits the firmware neglects */
5570561e514SDamien Le Moal 		id[49] |= cpu_to_le16(0x0300);	/* LBA, DMA */
5580561e514SDamien Le Moal 		id[83] &= cpu_to_le16(0x7FFF);
5590561e514SDamien Le Moal 		id[83] |= cpu_to_le16(0x4400);	/* Word 83 is valid and LBA48 */
5600561e514SDamien Le Moal 		id[86] |= cpu_to_le16(0x0400);	/* LBA48 on */
5610561e514SDamien Le Moal 		id[ATA_ID_MAJOR_VER] |= cpu_to_le16(0x1F);
5627ba07d16SOndrej Zary 		/* Clear the serial number because it's different each boot
5637ba07d16SOndrej Zary 		   which breaks validation on resume */
5647ba07d16SOndrej Zary 		memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN);
565963e4975SAlan Cox 	}
566963e4975SAlan Cox 	return err_mask;
567963e4975SAlan Cox }
568669a5db4SJeff Garzik 
569669a5db4SJeff Garzik /**
570669a5db4SJeff Garzik  *	it821x_check_atapi_dma	-	ATAPI DMA handler
571669a5db4SJeff Garzik  *	@qc: Command we are about to issue
572669a5db4SJeff Garzik  *
573669a5db4SJeff Garzik  *	Decide if this ATAPI command can be issued by DMA on this
574669a5db4SJeff Garzik  *	controller. Return 0 if it can be.
575669a5db4SJeff Garzik  */
576669a5db4SJeff Garzik 
it821x_check_atapi_dma(struct ata_queued_cmd * qc)577669a5db4SJeff Garzik static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
578669a5db4SJeff Garzik {
579669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
580669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
581669a5db4SJeff Garzik 
582bce7d5e0SJeff Norden 	/* Only use dma for transfers to/from the media. */
583b63b1331STejun Heo 	if (ata_qc_raw_nbytes(qc) < 2048)
584bce7d5e0SJeff Norden 		return -EOPNOTSUPP;
585bce7d5e0SJeff Norden 
586669a5db4SJeff Garzik 	/* No ATAPI DMA in smart mode */
587669a5db4SJeff Garzik 	if (itdev->smart)
588669a5db4SJeff Garzik 		return -EOPNOTSUPP;
589669a5db4SJeff Garzik 	/* No ATAPI DMA on rev 10 */
590669a5db4SJeff Garzik 	if (itdev->timing10)
591669a5db4SJeff Garzik 		return -EOPNOTSUPP;
592669a5db4SJeff Garzik 	/* Cool */
593669a5db4SJeff Garzik 	return 0;
594669a5db4SJeff Garzik }
595669a5db4SJeff Garzik 
596963e4975SAlan Cox /**
597963e4975SAlan Cox  *	it821x_display_disk	-	display disk setup
5983697aaafSHannes Reinecke  *	@ap: ATA port
599963e4975SAlan Cox  *	@n: Device number
600963e4975SAlan Cox  *	@buf: Buffer block from firmware
601963e4975SAlan Cox  *
602963e4975SAlan Cox  *	Produce a nice informative display of the device setup as provided
603963e4975SAlan Cox  *	by the firmware.
604963e4975SAlan Cox  */
605963e4975SAlan Cox 
it821x_display_disk(struct ata_port * ap,int n,u8 * buf)6063697aaafSHannes Reinecke static void it821x_display_disk(struct ata_port *ap, int n, u8 *buf)
607963e4975SAlan Cox {
608963e4975SAlan Cox 	unsigned char id[41];
609963e4975SAlan Cox 	int mode = 0;
6103a53b3bcSLABBE Corentin 	const char *mtype = "";
611963e4975SAlan Cox 	char mbuf[8];
6123a53b3bcSLABBE Corentin 	const char *cbl = "(40 wire cable)";
613963e4975SAlan Cox 
614963e4975SAlan Cox 	static const char *types[5] = {
6151c30c027SJean Delvare 		"RAID0", "RAID1", "RAID 0+1", "JBOD", "DISK"
616963e4975SAlan Cox 	};
617963e4975SAlan Cox 
618963e4975SAlan Cox 	if (buf[52] > 4)	/* No Disk */
619963e4975SAlan Cox 		return;
620963e4975SAlan Cox 
621963e4975SAlan Cox 	ata_id_c_string((u16 *)buf, id, 0, 41);
622963e4975SAlan Cox 
623963e4975SAlan Cox 	if (buf[51]) {
624963e4975SAlan Cox 		mode = ffs(buf[51]);
625963e4975SAlan Cox 		mtype = "UDMA";
626963e4975SAlan Cox 	} else if (buf[49]) {
627963e4975SAlan Cox 		mode = ffs(buf[49]);
628963e4975SAlan Cox 		mtype = "MWDMA";
629963e4975SAlan Cox 	}
630963e4975SAlan Cox 
631963e4975SAlan Cox 	if (buf[76])
632963e4975SAlan Cox 		cbl = "";
633963e4975SAlan Cox 
634963e4975SAlan Cox 	if (mode)
635963e4975SAlan Cox 		snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
636963e4975SAlan Cox 	else
637963e4975SAlan Cox 		strcpy(mbuf, "PIO");
638963e4975SAlan Cox 	if (buf[52] == 4)
6393697aaafSHannes Reinecke 		ata_port_info(ap, "%d: %-6s %-8s          %s %s\n",
640963e4975SAlan Cox 				n, mbuf, types[buf[52]], id, cbl);
641963e4975SAlan Cox 	else
6423697aaafSHannes Reinecke 		ata_port_info(ap, "%d: %-6s %-8s Volume: %1d %s %s\n",
643963e4975SAlan Cox 				n, mbuf, types[buf[52]], buf[53], id, cbl);
644963e4975SAlan Cox 	if (buf[125] < 100)
6453697aaafSHannes Reinecke 		ata_port_info(ap, "%d: Rebuilding: %d%%\n", n, buf[125]);
646963e4975SAlan Cox }
647963e4975SAlan Cox 
648963e4975SAlan Cox /**
649963e4975SAlan Cox  *	it821x_firmware_command		-	issue firmware command
650963e4975SAlan Cox  *	@ap: IT821x port to interrogate
651963e4975SAlan Cox  *	@cmd: command
652963e4975SAlan Cox  *	@len: length
653963e4975SAlan Cox  *
654963e4975SAlan Cox  *	Issue firmware commands expecting data back from the controller. We
655963e4975SAlan Cox  *	use this to issue commands that do not go via the normal paths. Other
656963e4975SAlan Cox  *	commands such as 0xFC can be issued normally.
657963e4975SAlan Cox  */
658963e4975SAlan Cox 
it821x_firmware_command(struct ata_port * ap,u8 cmd,int len)659963e4975SAlan Cox static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
660963e4975SAlan Cox {
661963e4975SAlan Cox 	u8 status;
662963e4975SAlan Cox 	int n = 0;
663963e4975SAlan Cox 	u16 *buf = kmalloc(len, GFP_KERNEL);
664ef59a62aSMarkus Elfring 
665ef59a62aSMarkus Elfring 	if (!buf)
666963e4975SAlan Cox 		return NULL;
667ef59a62aSMarkus Elfring 
668963e4975SAlan Cox 	/* This isn't quite a normal ATA command as we are talking to the
669963e4975SAlan Cox 	   firmware not the drives */
670963e4975SAlan Cox 	ap->ctl |= ATA_NIEN;
671963e4975SAlan Cox 	iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
672963e4975SAlan Cox 	ata_wait_idle(ap);
673963e4975SAlan Cox 	iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
674963e4975SAlan Cox 	iowrite8(cmd, ap->ioaddr.command_addr);
675963e4975SAlan Cox 	udelay(1);
676963e4975SAlan Cox 	/* This should be almost immediate but a little paranoia goes a long
677963e4975SAlan Cox 	   way. */
678963e4975SAlan Cox 	while(n++ < 10) {
679963e4975SAlan Cox 		status = ioread8(ap->ioaddr.status_addr);
680963e4975SAlan Cox 		if (status & ATA_ERR) {
681963e4975SAlan Cox 			kfree(buf);
6823697aaafSHannes Reinecke 			ata_port_err(ap, "%s: rejected\n", __func__);
683963e4975SAlan Cox 			return NULL;
684963e4975SAlan Cox 		}
685963e4975SAlan Cox 		if (status & ATA_DRQ) {
686963e4975SAlan Cox 			ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
687963e4975SAlan Cox 			return (u8 *)buf;
688963e4975SAlan Cox 		}
689eada8598SJia-Ju Bai 		usleep_range(500, 1000);
690963e4975SAlan Cox 	}
691963e4975SAlan Cox 	kfree(buf);
6923697aaafSHannes Reinecke 	ata_port_err(ap, "%s: timeout\n", __func__);
693963e4975SAlan Cox 	return NULL;
694963e4975SAlan Cox }
695963e4975SAlan Cox 
696963e4975SAlan Cox /**
697963e4975SAlan Cox  *	it821x_probe_firmware	-	firmware reporting/setup
698963e4975SAlan Cox  *	@ap: IT821x port being probed
699963e4975SAlan Cox  *
700963e4975SAlan Cox  *	Probe the firmware of the controller by issuing firmware command
701963e4975SAlan Cox  *	0xFA and analysing the returned data.
702963e4975SAlan Cox  */
703963e4975SAlan Cox 
it821x_probe_firmware(struct ata_port * ap)704963e4975SAlan Cox static void it821x_probe_firmware(struct ata_port *ap)
705963e4975SAlan Cox {
706963e4975SAlan Cox 	u8 *buf;
707963e4975SAlan Cox 	int i;
708963e4975SAlan Cox 
709963e4975SAlan Cox 	/* This is a bit ugly as we can't just issue a task file to a device
710963e4975SAlan Cox 	   as this is controller magic */
711963e4975SAlan Cox 
712963e4975SAlan Cox 	buf = it821x_firmware_command(ap, 0xFA, 512);
713963e4975SAlan Cox 
714963e4975SAlan Cox 	if (buf != NULL) {
7153697aaafSHannes Reinecke 		ata_port_info(ap, "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
716963e4975SAlan Cox 				buf[505],
717963e4975SAlan Cox 				buf[506],
718963e4975SAlan Cox 				buf[507],
719963e4975SAlan Cox 				buf[508]);
720963e4975SAlan Cox 		for (i = 0; i < 4; i++)
7213697aaafSHannes Reinecke 			it821x_display_disk(ap, i, buf + 128 * i);
722963e4975SAlan Cox 		kfree(buf);
723963e4975SAlan Cox 	}
724963e4975SAlan Cox }
725963e4975SAlan Cox 
726963e4975SAlan Cox 
727669a5db4SJeff Garzik 
728669a5db4SJeff Garzik /**
729669a5db4SJeff Garzik  *	it821x_port_start	-	port setup
730669a5db4SJeff Garzik  *	@ap: ATA port being set up
731669a5db4SJeff Garzik  *
732669a5db4SJeff Garzik  *	The it821x needs to maintain private data structures and also to
733669a5db4SJeff Garzik  *	use the standard PCI interface which lacks support for this
734669a5db4SJeff Garzik  *	functionality. We instead set up the private data on the port
735669a5db4SJeff Garzik  *	start hook, and tear it down on port stop
736669a5db4SJeff Garzik  */
737669a5db4SJeff Garzik 
it821x_port_start(struct ata_port * ap)738669a5db4SJeff Garzik static int it821x_port_start(struct ata_port *ap)
739669a5db4SJeff Garzik {
740669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
741669a5db4SJeff Garzik 	struct it821x_dev *itdev;
742669a5db4SJeff Garzik 	u8 conf;
743669a5db4SJeff Garzik 
744c7087652STejun Heo 	int ret = ata_bmdma_port_start(ap);
745669a5db4SJeff Garzik 	if (ret < 0)
746669a5db4SJeff Garzik 		return ret;
747669a5db4SJeff Garzik 
74824dc5f33STejun Heo 	itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
74924dc5f33STejun Heo 	if (itdev == NULL)
750669a5db4SJeff Garzik 		return -ENOMEM;
75124dc5f33STejun Heo 	ap->private_data = itdev;
752669a5db4SJeff Garzik 
753669a5db4SJeff Garzik 	pci_read_config_byte(pdev, 0x50, &conf);
754669a5db4SJeff Garzik 
755669a5db4SJeff Garzik 	if (conf & 1) {
756669a5db4SJeff Garzik 		itdev->smart = 1;
757669a5db4SJeff Garzik 		/* Long I/O's although allowed in LBA48 space cause the
758669a5db4SJeff Garzik 		   onboard firmware to enter the twighlight zone */
759669a5db4SJeff Garzik 		/* No ATAPI DMA in this mode either */
760963e4975SAlan Cox 		if (ap->port_no == 0)
761963e4975SAlan Cox 			it821x_probe_firmware(ap);
762669a5db4SJeff Garzik 	}
763669a5db4SJeff Garzik 	/* Pull the current clocks from 0x50 */
764669a5db4SJeff Garzik 	if (conf & (1 << (1 + ap->port_no)))
765669a5db4SJeff Garzik 		itdev->clock_mode = ATA_50;
766669a5db4SJeff Garzik 	else
767669a5db4SJeff Garzik 		itdev->clock_mode = ATA_66;
768669a5db4SJeff Garzik 
769669a5db4SJeff Garzik 	itdev->want[0][1] = ATA_ANY;
770669a5db4SJeff Garzik 	itdev->want[1][1] = ATA_ANY;
771669a5db4SJeff Garzik 	itdev->last_device = -1;
772669a5db4SJeff Garzik 
773604de6e0SAlan Cox 	if (pdev->revision == 0x10) {
774669a5db4SJeff Garzik 		itdev->timing10 = 1;
775669a5db4SJeff Garzik 		/* Need to disable ATAPI DMA for this case */
776669a5db4SJeff Garzik 		if (!itdev->smart)
7773697aaafSHannes Reinecke 			dev_warn(&pdev->dev,
7783697aaafSHannes Reinecke 				 "Revision 0x10, workarounds activated.\n");
779669a5db4SJeff Garzik 	}
780669a5db4SJeff Garzik 
781669a5db4SJeff Garzik 	return 0;
782669a5db4SJeff Garzik }
783669a5db4SJeff Garzik 
784963e4975SAlan Cox /**
785963e4975SAlan Cox  *	it821x_rdc_cable	-	Cable detect for RDC1010
786963e4975SAlan Cox  *	@ap: port we are checking
787963e4975SAlan Cox  *
788963e4975SAlan Cox  *	Return the RDC1010 cable type. Unlike the IT821x we know how to do
789963e4975SAlan Cox  *	this and can do host side cable detect
790963e4975SAlan Cox  */
791963e4975SAlan Cox 
it821x_rdc_cable(struct ata_port * ap)792963e4975SAlan Cox static int it821x_rdc_cable(struct ata_port *ap)
793963e4975SAlan Cox {
794963e4975SAlan Cox 	u16 r40;
795963e4975SAlan Cox 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
796963e4975SAlan Cox 
797963e4975SAlan Cox 	pci_read_config_word(pdev, 0x40, &r40);
798963e4975SAlan Cox 	if (r40 & (1 << (2 + ap->port_no)))
799963e4975SAlan Cox 		return ATA_CBL_PATA40;
800963e4975SAlan Cox 	return ATA_CBL_PATA80;
801963e4975SAlan Cox }
802963e4975SAlan Cox 
803*25df73d9SBart Van Assche static const struct scsi_host_template it821x_sht = {
80468d1d07bSTejun Heo 	ATA_BMDMA_SHT(DRV_NAME),
805669a5db4SJeff Garzik };
806669a5db4SJeff Garzik 
807669a5db4SJeff Garzik static struct ata_port_operations it821x_smart_port_ops = {
808029cfd6bSTejun Heo 	.inherits	= &ata_bmdma_port_ops,
809669a5db4SJeff Garzik 
810669a5db4SJeff Garzik 	.check_atapi_dma= it821x_check_atapi_dma,
8119363c382STejun Heo 	.qc_issue	= it821x_smart_qc_issue,
812bda30288SJeff Garzik 
813963e4975SAlan Cox 	.cable_detect	= ata_cable_80wire,
814029cfd6bSTejun Heo 	.set_mode	= it821x_smart_set_mode,
815029cfd6bSTejun Heo 	.dev_config	= it821x_dev_config,
816963e4975SAlan Cox 	.read_id	= it821x_read_id,
817669a5db4SJeff Garzik 
818669a5db4SJeff Garzik 	.port_start	= it821x_port_start,
819669a5db4SJeff Garzik };
820669a5db4SJeff Garzik 
821669a5db4SJeff Garzik static struct ata_port_operations it821x_passthru_port_ops = {
822029cfd6bSTejun Heo 	.inherits	= &ata_bmdma_port_ops,
823669a5db4SJeff Garzik 
824669a5db4SJeff Garzik 	.check_atapi_dma= it821x_check_atapi_dma,
8255682ed33STejun Heo 	.sff_dev_select	= it821x_passthru_dev_select,
826669a5db4SJeff Garzik 	.bmdma_start 	= it821x_passthru_bmdma_start,
827669a5db4SJeff Garzik 	.bmdma_stop	= it821x_passthru_bmdma_stop,
8289363c382STejun Heo 	.qc_issue	= it821x_passthru_qc_issue,
829bda30288SJeff Garzik 
830029cfd6bSTejun Heo 	.cable_detect	= ata_cable_unknown,
831029cfd6bSTejun Heo 	.set_piomode	= it821x_passthru_set_piomode,
832029cfd6bSTejun Heo 	.set_dmamode	= it821x_passthru_set_dmamode,
833669a5db4SJeff Garzik 
834669a5db4SJeff Garzik 	.port_start	= it821x_port_start,
835669a5db4SJeff Garzik };
836669a5db4SJeff Garzik 
837963e4975SAlan Cox static struct ata_port_operations it821x_rdc_port_ops = {
838963e4975SAlan Cox 	.inherits	= &ata_bmdma_port_ops,
839963e4975SAlan Cox 
840963e4975SAlan Cox 	.check_atapi_dma= it821x_check_atapi_dma,
841963e4975SAlan Cox 	.sff_dev_select	= it821x_passthru_dev_select,
842963e4975SAlan Cox 	.bmdma_start 	= it821x_passthru_bmdma_start,
843963e4975SAlan Cox 	.bmdma_stop	= it821x_passthru_bmdma_stop,
844963e4975SAlan Cox 	.qc_issue	= it821x_passthru_qc_issue,
845963e4975SAlan Cox 
846963e4975SAlan Cox 	.cable_detect	= it821x_rdc_cable,
847963e4975SAlan Cox 	.set_piomode	= it821x_passthru_set_piomode,
848963e4975SAlan Cox 	.set_dmamode	= it821x_passthru_set_dmamode,
849963e4975SAlan Cox 
850963e4975SAlan Cox 	.port_start	= it821x_port_start,
851963e4975SAlan Cox };
852963e4975SAlan Cox 
it821x_disable_raid(struct pci_dev * pdev)853112cc2b5SRandy Dunlap static void it821x_disable_raid(struct pci_dev *pdev)
854669a5db4SJeff Garzik {
855963e4975SAlan Cox 	/* Neither the RDC nor the IT8211 */
856963e4975SAlan Cox 	if (pdev->vendor != PCI_VENDOR_ID_ITE ||
857963e4975SAlan Cox 			pdev->device != PCI_DEVICE_ID_ITE_8212)
858963e4975SAlan Cox 			return;
859963e4975SAlan Cox 
860669a5db4SJeff Garzik 	/* Reset local CPU, and set BIOS not ready */
861669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x5E, 0x01);
862669a5db4SJeff Garzik 
863669a5db4SJeff Garzik 	/* Set to bypass mode, and reset PCI bus */
864669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x50, 0x00);
865669a5db4SJeff Garzik 	pci_write_config_word(pdev, PCI_COMMAND,
866669a5db4SJeff Garzik 			      PCI_COMMAND_PARITY | PCI_COMMAND_IO |
867669a5db4SJeff Garzik 			      PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
868669a5db4SJeff Garzik 	pci_write_config_word(pdev, 0x40, 0xA0F3);
869669a5db4SJeff Garzik 
870669a5db4SJeff Garzik 	pci_write_config_dword(pdev,0x4C, 0x02040204);
871669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x42, 0x36);
872669a5db4SJeff Garzik 	pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
873669a5db4SJeff Garzik }
874669a5db4SJeff Garzik 
875669a5db4SJeff Garzik 
it821x_init_one(struct pci_dev * pdev,const struct pci_device_id * id)876669a5db4SJeff Garzik static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
877669a5db4SJeff Garzik {
878669a5db4SJeff Garzik 	u8 conf;
879669a5db4SJeff Garzik 
8801626aeb8STejun Heo 	static const struct ata_port_info info_smart = {
8811d2808fdSJeff Garzik 		.flags = ATA_FLAG_SLAVE_POSS,
88214bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
88314bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
884963e4975SAlan Cox 		.udma_mask = ATA_UDMA6,
885669a5db4SJeff Garzik 		.port_ops = &it821x_smart_port_ops
886669a5db4SJeff Garzik 	};
8871626aeb8STejun Heo 	static const struct ata_port_info info_passthru = {
8881d2808fdSJeff Garzik 		.flags = ATA_FLAG_SLAVE_POSS,
88914bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
89014bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
891bf6263a8SJeff Garzik 		.udma_mask = ATA_UDMA6,
892669a5db4SJeff Garzik 		.port_ops = &it821x_passthru_port_ops
893669a5db4SJeff Garzik 	};
894963e4975SAlan Cox 	static const struct ata_port_info info_rdc = {
895963e4975SAlan Cox 		.flags = ATA_FLAG_SLAVE_POSS,
89614bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
89714bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
8984a99d95fSAlan Cox 		.udma_mask = ATA_UDMA6,
8994a99d95fSAlan Cox 		.port_ops = &it821x_rdc_port_ops
9004a99d95fSAlan Cox 	};
9014a99d95fSAlan Cox 	static const struct ata_port_info info_rdc_11 = {
9024a99d95fSAlan Cox 		.flags = ATA_FLAG_SLAVE_POSS,
90314bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
90414bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
905963e4975SAlan Cox 		/* No UDMA */
906963e4975SAlan Cox 		.port_ops = &it821x_rdc_port_ops
907963e4975SAlan Cox 	};
908669a5db4SJeff Garzik 
9091626aeb8STejun Heo 	const struct ata_port_info *ppi[] = { NULL, NULL };
9103a53b3bcSLABBE Corentin 	static const char *mode[2] = { "pass through", "smart" };
911f08048e9STejun Heo 	int rc;
912f08048e9STejun Heo 
913f08048e9STejun Heo 	rc = pcim_enable_device(pdev);
914f08048e9STejun Heo 	if (rc)
915f08048e9STejun Heo 		return rc;
916669a5db4SJeff Garzik 
917963e4975SAlan Cox 	if (pdev->vendor == PCI_VENDOR_ID_RDC) {
9184a99d95fSAlan Cox 		/* Deal with Vortex86SX */
9194a99d95fSAlan Cox 		if (pdev->revision == 0x11)
9204a99d95fSAlan Cox 			ppi[0] = &info_rdc_11;
9214a99d95fSAlan Cox 		else
922963e4975SAlan Cox 			ppi[0] = &info_rdc;
923963e4975SAlan Cox 	} else {
924669a5db4SJeff Garzik 		/* Force the card into bypass mode if so requested */
925669a5db4SJeff Garzik 		if (it8212_noraid) {
9263697aaafSHannes Reinecke 			dev_info(&pdev->dev, "forcing bypass mode.\n");
927669a5db4SJeff Garzik 			it821x_disable_raid(pdev);
928669a5db4SJeff Garzik 		}
929669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
930669a5db4SJeff Garzik 		conf &= 1;
931669a5db4SJeff Garzik 
9323697aaafSHannes Reinecke 		dev_info(&pdev->dev, "controller in %s mode.\n", mode[conf]);
9333697aaafSHannes Reinecke 
934669a5db4SJeff Garzik 		if (conf == 0)
9351626aeb8STejun Heo 			ppi[0] = &info_passthru;
936669a5db4SJeff Garzik 		else
9371626aeb8STejun Heo 			ppi[0] = &info_smart;
938963e4975SAlan Cox 	}
9391c5afdf7STejun Heo 	return ata_pci_bmdma_init_one(pdev, ppi, &it821x_sht, NULL, 0);
940669a5db4SJeff Garzik }
941669a5db4SJeff Garzik 
94258eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
it821x_reinit_one(struct pci_dev * pdev)943f535d53fSAlan static int it821x_reinit_one(struct pci_dev *pdev)
944f535d53fSAlan {
9450a86e1c8SJingoo Han 	struct ata_host *host = pci_get_drvdata(pdev);
946f08048e9STejun Heo 	int rc;
947f08048e9STejun Heo 
948f08048e9STejun Heo 	rc = ata_pci_device_do_resume(pdev);
949f08048e9STejun Heo 	if (rc)
950f08048e9STejun Heo 		return rc;
951f535d53fSAlan 	/* Resume - turn raid back off if need be */
952f535d53fSAlan 	if (it8212_noraid)
953f535d53fSAlan 		it821x_disable_raid(pdev);
954f08048e9STejun Heo 	ata_host_resume(host);
955f08048e9STejun Heo 	return rc;
956f535d53fSAlan }
957438ac6d5STejun Heo #endif
958f535d53fSAlan 
9592d2744fcSJeff Garzik static const struct pci_device_id it821x[] = {
9602d2744fcSJeff Garzik 	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
9612d2744fcSJeff Garzik 	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
9624192be64SOtavio Salvador 	{ PCI_VDEVICE(RDC, PCI_DEVICE_ID_RDC_D1010), },
9632d2744fcSJeff Garzik 
9642d2744fcSJeff Garzik 	{ },
965669a5db4SJeff Garzik };
966669a5db4SJeff Garzik 
967669a5db4SJeff Garzik static struct pci_driver it821x_pci_driver = {
968669a5db4SJeff Garzik 	.name 		= DRV_NAME,
969669a5db4SJeff Garzik 	.id_table	= it821x,
970669a5db4SJeff Garzik 	.probe 		= it821x_init_one,
971f535d53fSAlan 	.remove		= ata_pci_remove_one,
97258eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
973f535d53fSAlan 	.suspend	= ata_pci_device_suspend,
974f535d53fSAlan 	.resume		= it821x_reinit_one,
975438ac6d5STejun Heo #endif
976669a5db4SJeff Garzik };
977669a5db4SJeff Garzik 
9782fc75da0SAxel Lin module_pci_driver(it821x_pci_driver);
979669a5db4SJeff Garzik 
980669a5db4SJeff Garzik MODULE_AUTHOR("Alan Cox");
981669a5db4SJeff Garzik MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
982669a5db4SJeff Garzik MODULE_LICENSE("GPL");
983669a5db4SJeff Garzik MODULE_DEVICE_TABLE(pci, it821x);
984669a5db4SJeff Garzik MODULE_VERSION(DRV_VERSION);
985669a5db4SJeff Garzik 
986669a5db4SJeff Garzik module_param_named(noraid, it8212_noraid, int, S_IRUGO);
9875fe675e2SStas Sergeev MODULE_PARM_DESC(noraid, "Force card into bypass mode");
988