xref: /OK3568_Linux_fs/kernel/drivers/ata/pata_it821x.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * pata_it821x.c 	- IT821x PATA for new ATA layer
3*4882a593Smuzhiyun  *			  (C) 2005 Red Hat Inc
4*4882a593Smuzhiyun  *			  Alan Cox <alan@lxorguk.ukuu.org.uk>
5*4882a593Smuzhiyun  *			  (C) 2007 Bartlomiej Zolnierkiewicz
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * based upon
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * it821x.c
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * linux/drivers/ide/pci/it821x.c		Version 0.09	December 2004
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Copyright (C) 2004		Red Hat
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *  May be copied or modified under the terms of the GNU General Public License
16*4882a593Smuzhiyun  *  Based in part on the ITE vendor provided SCSI driver.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  *  Documentation available from IT8212F_V04.pdf
19*4882a593Smuzhiyun  * 	http://www.ite.com.tw/EN/products_more.aspx?CategoryID=3&ID=5,91
20*4882a593Smuzhiyun  *  Some other documents are NDA.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  *  The ITE8212 isn't exactly a standard IDE controller. It has two
23*4882a593Smuzhiyun  *  modes. In pass through mode then it is an IDE controller. In its smart
24*4882a593Smuzhiyun  *  mode its actually quite a capable hardware raid controller disguised
25*4882a593Smuzhiyun  *  as an IDE controller. Smart mode only understands DMA read/write and
26*4882a593Smuzhiyun  *  identify, none of the fancier commands apply. The IT8211 is identical
27*4882a593Smuzhiyun  *  in other respects but lacks the raid mode.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  *  Errata:
30*4882a593Smuzhiyun  *  o	Rev 0x10 also requires master/slave hold the same DMA timings and
31*4882a593Smuzhiyun  *	cannot do ATAPI MWDMA.
32*4882a593Smuzhiyun  *  o	The identify data for raid volumes lacks CHS info (technically ok)
33*4882a593Smuzhiyun  *	but also fails to set the LBA28 and other bits. We fix these in
34*4882a593Smuzhiyun  *	the IDE probe quirk code.
35*4882a593Smuzhiyun  *  o	If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
36*4882a593Smuzhiyun  *	raid then the controller firmware dies
37*4882a593Smuzhiyun  *  o	Smart mode without RAID doesn't clear all the necessary identify
38*4882a593Smuzhiyun  *	bits to reduce the command set to the one used
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  *  This has a few impacts on the driver
41*4882a593Smuzhiyun  *  - In pass through mode we do all the work you would expect
42*4882a593Smuzhiyun  *  - In smart mode the clocking set up is done by the controller generally
43*4882a593Smuzhiyun  *    but we must watch the other limits and filter.
44*4882a593Smuzhiyun  *  - There are a few extra vendor commands that actually talk to the
45*4882a593Smuzhiyun  *    controller but only work PIO with no IRQ.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  *  Vendor areas of the identify block in smart mode are used for the
48*4882a593Smuzhiyun  *  timing and policy set up. Each HDD in raid mode also has a serial
49*4882a593Smuzhiyun  *  block on the disk. The hardware extra commands are get/set chip status,
50*4882a593Smuzhiyun  *  rebuild, get rebuild status.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  *  In Linux the driver supports pass through mode as if the device was
53*4882a593Smuzhiyun  *  just another IDE controller. If the smart mode is running then
54*4882a593Smuzhiyun  *  volumes are managed by the controller firmware and each IDE "disk"
55*4882a593Smuzhiyun  *  is a raid volume. Even more cute - the controller can do automated
56*4882a593Smuzhiyun  *  hotplug and rebuild.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  *  The pass through controller itself is a little demented. It has a
59*4882a593Smuzhiyun  *  flaw that it has a single set of PIO/MWDMA timings per channel so
60*4882a593Smuzhiyun  *  non UDMA devices restrict each others performance. It also has a
61*4882a593Smuzhiyun  *  single clock source per channel so mixed UDMA100/133 performance
62*4882a593Smuzhiyun  *  isn't perfect and we have to pick a clock. Thankfully none of this
63*4882a593Smuzhiyun  *  matters in smart mode. ATAPI DMA is not currently supported.
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  *  It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  *  TODO
68*4882a593Smuzhiyun  *	-	ATAPI and other speed filtering
69*4882a593Smuzhiyun  *	-	RAID configuration ioctls
70*4882a593Smuzhiyun  */
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #include <linux/kernel.h>
73*4882a593Smuzhiyun #include <linux/module.h>
74*4882a593Smuzhiyun #include <linux/pci.h>
75*4882a593Smuzhiyun #include <linux/blkdev.h>
76*4882a593Smuzhiyun #include <linux/delay.h>
77*4882a593Smuzhiyun #include <linux/slab.h>
78*4882a593Smuzhiyun #include <scsi/scsi_host.h>
79*4882a593Smuzhiyun #include <linux/libata.h>
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #define DRV_NAME "pata_it821x"
83*4882a593Smuzhiyun #define DRV_VERSION "0.4.2"
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun struct it821x_dev
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	unsigned int smart:1,		/* Are we in smart raid mode */
88*4882a593Smuzhiyun 		timing10:1;		/* Rev 0x10 */
89*4882a593Smuzhiyun 	u8	clock_mode;		/* 0, ATA_50 or ATA_66 */
90*4882a593Smuzhiyun 	u8	want[2][2];		/* Mode/Pri log for master slave */
91*4882a593Smuzhiyun 	/* We need these for switching the clock when DMA goes on/off
92*4882a593Smuzhiyun 	   The high byte is the 66Mhz timing */
93*4882a593Smuzhiyun 	u16	pio[2];			/* Cached PIO values */
94*4882a593Smuzhiyun 	u16	mwdma[2];		/* Cached MWDMA values */
95*4882a593Smuzhiyun 	u16	udma[2];		/* Cached UDMA values (per drive) */
96*4882a593Smuzhiyun 	u16	last_device;		/* Master or slave loaded ? */
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun #define ATA_66		0
100*4882a593Smuzhiyun #define ATA_50		1
101*4882a593Smuzhiyun #define ATA_ANY		2
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun #define UDMA_OFF	0
104*4882a593Smuzhiyun #define MWDMA_OFF	0
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /*
107*4882a593Smuzhiyun  *	We allow users to force the card into non raid mode without
108*4882a593Smuzhiyun  *	flashing the alternative BIOS. This is also necessary right now
109*4882a593Smuzhiyun  *	for embedded platforms that cannot run a PC BIOS but are using this
110*4882a593Smuzhiyun  *	device.
111*4882a593Smuzhiyun  */
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun static int it8212_noraid;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  *	it821x_program	-	program the PIO/MWDMA registers
117*4882a593Smuzhiyun  *	@ap: ATA port
118*4882a593Smuzhiyun  *	@adev: Device to program
119*4882a593Smuzhiyun  *	@timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  *	Program the PIO/MWDMA timing for this channel according to the
122*4882a593Smuzhiyun  *	current clock. These share the same register so are managed by
123*4882a593Smuzhiyun  *	the DMA start/stop sequence as with the old driver.
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun 
it821x_program(struct ata_port * ap,struct ata_device * adev,u16 timing)126*4882a593Smuzhiyun static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
129*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
130*4882a593Smuzhiyun 	int channel = ap->port_no;
131*4882a593Smuzhiyun 	u8 conf;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	/* Program PIO/MWDMA timing bits */
134*4882a593Smuzhiyun 	if (itdev->clock_mode == ATA_66)
135*4882a593Smuzhiyun 		conf = timing >> 8;
136*4882a593Smuzhiyun 	else
137*4882a593Smuzhiyun 		conf = timing & 0xFF;
138*4882a593Smuzhiyun 	pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun  *	it821x_program_udma	-	program the UDMA registers
144*4882a593Smuzhiyun  *	@ap: ATA port
145*4882a593Smuzhiyun  *	@adev: ATA device to update
146*4882a593Smuzhiyun  *	@timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  *	Program the UDMA timing for this drive according to the
149*4882a593Smuzhiyun  *	current clock. Handles the dual clocks and also knows about
150*4882a593Smuzhiyun  *	the errata on the 0x10 revision. The UDMA errata is partly handled
151*4882a593Smuzhiyun  *	here and partly in start_dma.
152*4882a593Smuzhiyun  */
153*4882a593Smuzhiyun 
it821x_program_udma(struct ata_port * ap,struct ata_device * adev,u16 timing)154*4882a593Smuzhiyun static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
157*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
158*4882a593Smuzhiyun 	int channel = ap->port_no;
159*4882a593Smuzhiyun 	int unit = adev->devno;
160*4882a593Smuzhiyun 	u8 conf;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	/* Program UDMA timing bits */
163*4882a593Smuzhiyun 	if (itdev->clock_mode == ATA_66)
164*4882a593Smuzhiyun 		conf = timing >> 8;
165*4882a593Smuzhiyun 	else
166*4882a593Smuzhiyun 		conf = timing & 0xFF;
167*4882a593Smuzhiyun 	if (itdev->timing10 == 0)
168*4882a593Smuzhiyun 		pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
169*4882a593Smuzhiyun 	else {
170*4882a593Smuzhiyun 		/* Early revision must be programmed for both together */
171*4882a593Smuzhiyun 		pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
172*4882a593Smuzhiyun 		pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /**
177*4882a593Smuzhiyun  *	it821x_clock_strategy
178*4882a593Smuzhiyun  *	@ap: ATA interface
179*4882a593Smuzhiyun  *	@adev: ATA device being updated
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  *	Select between the 50 and 66Mhz base clocks to get the best
182*4882a593Smuzhiyun  *	results for this interface.
183*4882a593Smuzhiyun  */
184*4882a593Smuzhiyun 
it821x_clock_strategy(struct ata_port * ap,struct ata_device * adev)185*4882a593Smuzhiyun static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
188*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
189*4882a593Smuzhiyun 	u8 unit = adev->devno;
190*4882a593Smuzhiyun 	struct ata_device *pair = ata_dev_pair(adev);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	int clock, altclock;
193*4882a593Smuzhiyun 	u8 v;
194*4882a593Smuzhiyun 	int sel = 0;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	/* Look for the most wanted clocking */
197*4882a593Smuzhiyun 	if (itdev->want[0][0] > itdev->want[1][0]) {
198*4882a593Smuzhiyun 		clock = itdev->want[0][1];
199*4882a593Smuzhiyun 		altclock = itdev->want[1][1];
200*4882a593Smuzhiyun 	} else {
201*4882a593Smuzhiyun 		clock = itdev->want[1][1];
202*4882a593Smuzhiyun 		altclock = itdev->want[0][1];
203*4882a593Smuzhiyun 	}
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/* Master doesn't care does the slave ? */
206*4882a593Smuzhiyun 	if (clock == ATA_ANY)
207*4882a593Smuzhiyun 		clock = altclock;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* Nobody cares - keep the same clock */
210*4882a593Smuzhiyun 	if (clock == ATA_ANY)
211*4882a593Smuzhiyun 		return;
212*4882a593Smuzhiyun 	/* No change */
213*4882a593Smuzhiyun 	if (clock == itdev->clock_mode)
214*4882a593Smuzhiyun 		return;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/* Load this into the controller */
217*4882a593Smuzhiyun 	if (clock == ATA_66)
218*4882a593Smuzhiyun 		itdev->clock_mode = ATA_66;
219*4882a593Smuzhiyun 	else {
220*4882a593Smuzhiyun 		itdev->clock_mode = ATA_50;
221*4882a593Smuzhiyun 		sel = 1;
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 	pci_read_config_byte(pdev, 0x50, &v);
224*4882a593Smuzhiyun 	v &= ~(1 << (1 + ap->port_no));
225*4882a593Smuzhiyun 	v |= sel << (1 + ap->port_no);
226*4882a593Smuzhiyun 	pci_write_config_byte(pdev, 0x50, v);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	/*
229*4882a593Smuzhiyun 	 *	Reprogram the UDMA/PIO of the pair drive for the switch
230*4882a593Smuzhiyun 	 *	MWDMA will be dealt with by the dma switcher
231*4882a593Smuzhiyun 	 */
232*4882a593Smuzhiyun 	if (pair && itdev->udma[1-unit] != UDMA_OFF) {
233*4882a593Smuzhiyun 		it821x_program_udma(ap, pair, itdev->udma[1-unit]);
234*4882a593Smuzhiyun 		it821x_program(ap, pair, itdev->pio[1-unit]);
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 	/*
237*4882a593Smuzhiyun 	 *	Reprogram the UDMA/PIO of our drive for the switch.
238*4882a593Smuzhiyun 	 *	MWDMA will be dealt with by the dma switcher
239*4882a593Smuzhiyun 	 */
240*4882a593Smuzhiyun 	if (itdev->udma[unit] != UDMA_OFF) {
241*4882a593Smuzhiyun 		it821x_program_udma(ap, adev, itdev->udma[unit]);
242*4882a593Smuzhiyun 		it821x_program(ap, adev, itdev->pio[unit]);
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /**
247*4882a593Smuzhiyun  *	it821x_passthru_set_piomode	-	set PIO mode data
248*4882a593Smuzhiyun  *	@ap: ATA interface
249*4882a593Smuzhiyun  *	@adev: ATA device
250*4882a593Smuzhiyun  *
251*4882a593Smuzhiyun  *	Configure for PIO mode. This is complicated as the register is
252*4882a593Smuzhiyun  *	shared by PIO and MWDMA and for both channels.
253*4882a593Smuzhiyun  */
254*4882a593Smuzhiyun 
it821x_passthru_set_piomode(struct ata_port * ap,struct ata_device * adev)255*4882a593Smuzhiyun static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	/* Spec says 89 ref driver uses 88 */
258*4882a593Smuzhiyun 	static const u16 pio[]	= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
259*4882a593Smuzhiyun 	static const u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
262*4882a593Smuzhiyun 	int unit = adev->devno;
263*4882a593Smuzhiyun 	int mode_wanted = adev->pio_mode - XFER_PIO_0;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	/* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
266*4882a593Smuzhiyun 	itdev->want[unit][1] = pio_want[mode_wanted];
267*4882a593Smuzhiyun 	itdev->want[unit][0] = 1;	/* PIO is lowest priority */
268*4882a593Smuzhiyun 	itdev->pio[unit] = pio[mode_wanted];
269*4882a593Smuzhiyun 	it821x_clock_strategy(ap, adev);
270*4882a593Smuzhiyun 	it821x_program(ap, adev, itdev->pio[unit]);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun /**
274*4882a593Smuzhiyun  *	it821x_passthru_set_dmamode	-	set initial DMA mode data
275*4882a593Smuzhiyun  *	@ap: ATA interface
276*4882a593Smuzhiyun  *	@adev: ATA device
277*4882a593Smuzhiyun  *
278*4882a593Smuzhiyun  *	Set up the DMA modes. The actions taken depend heavily on the mode
279*4882a593Smuzhiyun  *	to use. If UDMA is used as is hopefully the usual case then the
280*4882a593Smuzhiyun  *	timing register is private and we need only consider the clock. If
281*4882a593Smuzhiyun  *	we are using MWDMA then we have to manage the setting ourself as
282*4882a593Smuzhiyun  *	we switch devices and mode.
283*4882a593Smuzhiyun  */
284*4882a593Smuzhiyun 
it821x_passthru_set_dmamode(struct ata_port * ap,struct ata_device * adev)285*4882a593Smuzhiyun static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	static const u16 dma[]	= 	{ 0x8866, 0x3222, 0x3121 };
288*4882a593Smuzhiyun 	static const u8 mwdma_want[] =  { ATA_ANY, ATA_66, ATA_ANY };
289*4882a593Smuzhiyun 	static const u16 udma[]	= 	{ 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
290*4882a593Smuzhiyun 	static const u8 udma_want[] =   { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
293*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
294*4882a593Smuzhiyun 	int channel = ap->port_no;
295*4882a593Smuzhiyun 	int unit = adev->devno;
296*4882a593Smuzhiyun 	u8 conf;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (adev->dma_mode >= XFER_UDMA_0) {
299*4882a593Smuzhiyun 		int mode_wanted = adev->dma_mode - XFER_UDMA_0;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 		itdev->want[unit][1] = udma_want[mode_wanted];
302*4882a593Smuzhiyun 		itdev->want[unit][0] = 3;	/* UDMA is high priority */
303*4882a593Smuzhiyun 		itdev->mwdma[unit] = MWDMA_OFF;
304*4882a593Smuzhiyun 		itdev->udma[unit] = udma[mode_wanted];
305*4882a593Smuzhiyun 		if (mode_wanted >= 5)
306*4882a593Smuzhiyun 			itdev->udma[unit] |= 0x8080;	/* UDMA 5/6 select on */
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		/* UDMA on. Again revision 0x10 must do the pair */
309*4882a593Smuzhiyun 		pci_read_config_byte(pdev, 0x50, &conf);
310*4882a593Smuzhiyun 		if (itdev->timing10)
311*4882a593Smuzhiyun 			conf &= channel ? 0x9F: 0xE7;
312*4882a593Smuzhiyun 		else
313*4882a593Smuzhiyun 			conf &= ~ (1 << (3 + 2 * channel + unit));
314*4882a593Smuzhiyun 		pci_write_config_byte(pdev, 0x50, conf);
315*4882a593Smuzhiyun 		it821x_clock_strategy(ap, adev);
316*4882a593Smuzhiyun 		it821x_program_udma(ap, adev, itdev->udma[unit]);
317*4882a593Smuzhiyun 	} else {
318*4882a593Smuzhiyun 		int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		itdev->want[unit][1] = mwdma_want[mode_wanted];
321*4882a593Smuzhiyun 		itdev->want[unit][0] = 2;	/* MWDMA is low priority */
322*4882a593Smuzhiyun 		itdev->mwdma[unit] = dma[mode_wanted];
323*4882a593Smuzhiyun 		itdev->udma[unit] = UDMA_OFF;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 		/* UDMA bits off - Revision 0x10 do them in pairs */
326*4882a593Smuzhiyun 		pci_read_config_byte(pdev, 0x50, &conf);
327*4882a593Smuzhiyun 		if (itdev->timing10)
328*4882a593Smuzhiyun 			conf |= channel ? 0x60: 0x18;
329*4882a593Smuzhiyun 		else
330*4882a593Smuzhiyun 			conf |= 1 << (3 + 2 * channel + unit);
331*4882a593Smuzhiyun 		pci_write_config_byte(pdev, 0x50, conf);
332*4882a593Smuzhiyun 		it821x_clock_strategy(ap, adev);
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun /**
337*4882a593Smuzhiyun  *	it821x_passthru_dma_start	-	DMA start callback
338*4882a593Smuzhiyun  *	@qc: Command in progress
339*4882a593Smuzhiyun  *
340*4882a593Smuzhiyun  *	Usually drivers set the DMA timing at the point the set_dmamode call
341*4882a593Smuzhiyun  *	is made. IT821x however requires we load new timings on the
342*4882a593Smuzhiyun  *	transitions in some cases.
343*4882a593Smuzhiyun  */
344*4882a593Smuzhiyun 
it821x_passthru_bmdma_start(struct ata_queued_cmd * qc)345*4882a593Smuzhiyun static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	struct ata_port *ap = qc->ap;
348*4882a593Smuzhiyun 	struct ata_device *adev = qc->dev;
349*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
350*4882a593Smuzhiyun 	int unit = adev->devno;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (itdev->mwdma[unit] != MWDMA_OFF)
353*4882a593Smuzhiyun 		it821x_program(ap, adev, itdev->mwdma[unit]);
354*4882a593Smuzhiyun 	else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
355*4882a593Smuzhiyun 		it821x_program_udma(ap, adev, itdev->udma[unit]);
356*4882a593Smuzhiyun 	ata_bmdma_start(qc);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun /**
360*4882a593Smuzhiyun  *	it821x_passthru_dma_stop	-	DMA stop callback
361*4882a593Smuzhiyun  *	@qc: ATA command
362*4882a593Smuzhiyun  *
363*4882a593Smuzhiyun  *	We loaded new timings in dma_start, as a result we need to restore
364*4882a593Smuzhiyun  *	the PIO timings in dma_stop so that the next command issue gets the
365*4882a593Smuzhiyun  *	right clock values.
366*4882a593Smuzhiyun  */
367*4882a593Smuzhiyun 
it821x_passthru_bmdma_stop(struct ata_queued_cmd * qc)368*4882a593Smuzhiyun static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	struct ata_port *ap = qc->ap;
371*4882a593Smuzhiyun 	struct ata_device *adev = qc->dev;
372*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
373*4882a593Smuzhiyun 	int unit = adev->devno;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	ata_bmdma_stop(qc);
376*4882a593Smuzhiyun 	if (itdev->mwdma[unit] != MWDMA_OFF)
377*4882a593Smuzhiyun 		it821x_program(ap, adev, itdev->pio[unit]);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun /**
382*4882a593Smuzhiyun  *	it821x_passthru_dev_select	-	Select master/slave
383*4882a593Smuzhiyun  *	@ap: ATA port
384*4882a593Smuzhiyun  *	@device: Device number (not pointer)
385*4882a593Smuzhiyun  *
386*4882a593Smuzhiyun  *	Device selection hook. If necessary perform clock switching
387*4882a593Smuzhiyun  */
388*4882a593Smuzhiyun 
it821x_passthru_dev_select(struct ata_port * ap,unsigned int device)389*4882a593Smuzhiyun static void it821x_passthru_dev_select(struct ata_port *ap,
390*4882a593Smuzhiyun 				       unsigned int device)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
393*4882a593Smuzhiyun 	if (itdev && device != itdev->last_device) {
394*4882a593Smuzhiyun 		struct ata_device *adev = &ap->link.device[device];
395*4882a593Smuzhiyun 		it821x_program(ap, adev, itdev->pio[adev->devno]);
396*4882a593Smuzhiyun 		itdev->last_device = device;
397*4882a593Smuzhiyun 	}
398*4882a593Smuzhiyun 	ata_sff_dev_select(ap, device);
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun /**
402*4882a593Smuzhiyun  *	it821x_smart_qc_issue		-	wrap qc issue prot
403*4882a593Smuzhiyun  *	@qc: command
404*4882a593Smuzhiyun  *
405*4882a593Smuzhiyun  *	Wrap the command issue sequence for the IT821x. We need to
406*4882a593Smuzhiyun  *	perform out own device selection timing loads before the
407*4882a593Smuzhiyun  *	usual happenings kick off
408*4882a593Smuzhiyun  */
409*4882a593Smuzhiyun 
it821x_smart_qc_issue(struct ata_queued_cmd * qc)410*4882a593Smuzhiyun static unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	switch(qc->tf.command)
413*4882a593Smuzhiyun 	{
414*4882a593Smuzhiyun 		/* Commands the firmware supports */
415*4882a593Smuzhiyun 		case ATA_CMD_READ:
416*4882a593Smuzhiyun 		case ATA_CMD_READ_EXT:
417*4882a593Smuzhiyun 		case ATA_CMD_WRITE:
418*4882a593Smuzhiyun 		case ATA_CMD_WRITE_EXT:
419*4882a593Smuzhiyun 		case ATA_CMD_PIO_READ:
420*4882a593Smuzhiyun 		case ATA_CMD_PIO_READ_EXT:
421*4882a593Smuzhiyun 		case ATA_CMD_PIO_WRITE:
422*4882a593Smuzhiyun 		case ATA_CMD_PIO_WRITE_EXT:
423*4882a593Smuzhiyun 		case ATA_CMD_READ_MULTI:
424*4882a593Smuzhiyun 		case ATA_CMD_READ_MULTI_EXT:
425*4882a593Smuzhiyun 		case ATA_CMD_WRITE_MULTI:
426*4882a593Smuzhiyun 		case ATA_CMD_WRITE_MULTI_EXT:
427*4882a593Smuzhiyun 		case ATA_CMD_ID_ATA:
428*4882a593Smuzhiyun 		case ATA_CMD_INIT_DEV_PARAMS:
429*4882a593Smuzhiyun 		case 0xFC:	/* Internal 'report rebuild state' */
430*4882a593Smuzhiyun 		/* Arguably should just no-op this one */
431*4882a593Smuzhiyun 		case ATA_CMD_SET_FEATURES:
432*4882a593Smuzhiyun 			return ata_bmdma_qc_issue(qc);
433*4882a593Smuzhiyun 	}
434*4882a593Smuzhiyun 	printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command);
435*4882a593Smuzhiyun 	return AC_ERR_DEV;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun /**
439*4882a593Smuzhiyun  *	it821x_passthru_qc_issue	-	wrap qc issue prot
440*4882a593Smuzhiyun  *	@qc: command
441*4882a593Smuzhiyun  *
442*4882a593Smuzhiyun  *	Wrap the command issue sequence for the IT821x. We need to
443*4882a593Smuzhiyun  *	perform out own device selection timing loads before the
444*4882a593Smuzhiyun  *	usual happenings kick off
445*4882a593Smuzhiyun  */
446*4882a593Smuzhiyun 
it821x_passthru_qc_issue(struct ata_queued_cmd * qc)447*4882a593Smuzhiyun static unsigned int it821x_passthru_qc_issue(struct ata_queued_cmd *qc)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun 	it821x_passthru_dev_select(qc->ap, qc->dev->devno);
450*4882a593Smuzhiyun 	return ata_bmdma_qc_issue(qc);
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun /**
454*4882a593Smuzhiyun  *	it821x_smart_set_mode	-	mode setting
455*4882a593Smuzhiyun  *	@link: interface to set up
456*4882a593Smuzhiyun  *	@unused: device that failed (error only)
457*4882a593Smuzhiyun  *
458*4882a593Smuzhiyun  *	Use a non standard set_mode function. We don't want to be tuned.
459*4882a593Smuzhiyun  *	The BIOS configured everything. Our job is not to fiddle. We
460*4882a593Smuzhiyun  *	read the dma enabled bits from the PCI configuration of the device
461*4882a593Smuzhiyun  *	and respect them.
462*4882a593Smuzhiyun  */
463*4882a593Smuzhiyun 
it821x_smart_set_mode(struct ata_link * link,struct ata_device ** unused)464*4882a593Smuzhiyun static int it821x_smart_set_mode(struct ata_link *link, struct ata_device **unused)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun 	struct ata_device *dev;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	ata_for_each_dev(dev, link, ENABLED) {
469*4882a593Smuzhiyun 		/* We don't really care */
470*4882a593Smuzhiyun 		dev->pio_mode = XFER_PIO_0;
471*4882a593Smuzhiyun 		dev->dma_mode = XFER_MW_DMA_0;
472*4882a593Smuzhiyun 		/* We do need the right mode information for DMA or PIO
473*4882a593Smuzhiyun 		   and this comes from the current configuration flags */
474*4882a593Smuzhiyun 		if (ata_id_has_dma(dev->id)) {
475*4882a593Smuzhiyun 			ata_dev_info(dev, "configured for DMA\n");
476*4882a593Smuzhiyun 			dev->xfer_mode = XFER_MW_DMA_0;
477*4882a593Smuzhiyun 			dev->xfer_shift = ATA_SHIFT_MWDMA;
478*4882a593Smuzhiyun 			dev->flags &= ~ATA_DFLAG_PIO;
479*4882a593Smuzhiyun 		} else {
480*4882a593Smuzhiyun 			ata_dev_info(dev, "configured for PIO\n");
481*4882a593Smuzhiyun 			dev->xfer_mode = XFER_PIO_0;
482*4882a593Smuzhiyun 			dev->xfer_shift = ATA_SHIFT_PIO;
483*4882a593Smuzhiyun 			dev->flags |= ATA_DFLAG_PIO;
484*4882a593Smuzhiyun 		}
485*4882a593Smuzhiyun 	}
486*4882a593Smuzhiyun 	return 0;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun /**
490*4882a593Smuzhiyun  *	it821x_dev_config	-	Called each device identify
491*4882a593Smuzhiyun  *	@adev: Device that has just been identified
492*4882a593Smuzhiyun  *
493*4882a593Smuzhiyun  *	Perform the initial setup needed for each device that is chip
494*4882a593Smuzhiyun  *	special. In our case we need to lock the sector count to avoid
495*4882a593Smuzhiyun  *	blowing the brains out of the firmware with large LBA48 requests
496*4882a593Smuzhiyun  *
497*4882a593Smuzhiyun  */
498*4882a593Smuzhiyun 
it821x_dev_config(struct ata_device * adev)499*4882a593Smuzhiyun static void it821x_dev_config(struct ata_device *adev)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	if (adev->max_sectors > 255)
506*4882a593Smuzhiyun 		adev->max_sectors = 255;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	if (strstr(model_num, "Integrated Technology Express")) {
509*4882a593Smuzhiyun 		/* RAID mode */
510*4882a593Smuzhiyun 		ata_dev_info(adev, "%sRAID%d volume",
511*4882a593Smuzhiyun 			     adev->id[147] ? "Bootable " : "",
512*4882a593Smuzhiyun 			     adev->id[129]);
513*4882a593Smuzhiyun 		if (adev->id[129] != 1)
514*4882a593Smuzhiyun 			pr_cont("(%dK stripe)", adev->id[146]);
515*4882a593Smuzhiyun 		pr_cont("\n");
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 	/* This is a controller firmware triggered funny, don't
518*4882a593Smuzhiyun 	   report the drive faulty! */
519*4882a593Smuzhiyun 	adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
520*4882a593Smuzhiyun 	/* No HPA in 'smart' mode */
521*4882a593Smuzhiyun 	adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun /**
525*4882a593Smuzhiyun  *	it821x_read_id	-	Hack identify data up
526*4882a593Smuzhiyun  *	@adev: device to read
527*4882a593Smuzhiyun  *	@tf: proposed taskfile
528*4882a593Smuzhiyun  *	@id: buffer for returned ident data
529*4882a593Smuzhiyun  *
530*4882a593Smuzhiyun  *	Query the devices on this firmware driven port and slightly
531*4882a593Smuzhiyun  *	mash the identify data to stop us and common tools trying to
532*4882a593Smuzhiyun  *	use features not firmware supported. The firmware itself does
533*4882a593Smuzhiyun  *	some masking (eg SMART) but not enough.
534*4882a593Smuzhiyun  */
535*4882a593Smuzhiyun 
it821x_read_id(struct ata_device * adev,struct ata_taskfile * tf,u16 * id)536*4882a593Smuzhiyun static unsigned int it821x_read_id(struct ata_device *adev,
537*4882a593Smuzhiyun 					struct ata_taskfile *tf, u16 *id)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	unsigned int err_mask;
540*4882a593Smuzhiyun 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	err_mask = ata_do_dev_read_id(adev, tf, id);
543*4882a593Smuzhiyun 	if (err_mask)
544*4882a593Smuzhiyun 		return err_mask;
545*4882a593Smuzhiyun 	ata_id_c_string(id, model_num, ATA_ID_PROD, sizeof(model_num));
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	id[83] &= ~(1 << 12);	/* Cache flush is firmware handled */
548*4882a593Smuzhiyun 	id[83] &= ~(1 << 13);	/* Ditto for LBA48 flushes */
549*4882a593Smuzhiyun 	id[84] &= ~(1 << 6);	/* No FUA */
550*4882a593Smuzhiyun 	id[85] &= ~(1 << 10);	/* No HPA */
551*4882a593Smuzhiyun 	id[76] = 0;		/* No NCQ/AN etc */
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	if (strstr(model_num, "Integrated Technology Express")) {
554*4882a593Smuzhiyun 		/* Set feature bits the firmware neglects */
555*4882a593Smuzhiyun 		id[49] |= 0x0300;	/* LBA, DMA */
556*4882a593Smuzhiyun 		id[83] &= 0x7FFF;
557*4882a593Smuzhiyun 		id[83] |= 0x4400;	/* Word 83 is valid and LBA48 */
558*4882a593Smuzhiyun 		id[86] |= 0x0400;	/* LBA48 on */
559*4882a593Smuzhiyun 		id[ATA_ID_MAJOR_VER] |= 0x1F;
560*4882a593Smuzhiyun 		/* Clear the serial number because it's different each boot
561*4882a593Smuzhiyun 		   which breaks validation on resume */
562*4882a593Smuzhiyun 		memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN);
563*4882a593Smuzhiyun 	}
564*4882a593Smuzhiyun 	return err_mask;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun /**
568*4882a593Smuzhiyun  *	it821x_check_atapi_dma	-	ATAPI DMA handler
569*4882a593Smuzhiyun  *	@qc: Command we are about to issue
570*4882a593Smuzhiyun  *
571*4882a593Smuzhiyun  *	Decide if this ATAPI command can be issued by DMA on this
572*4882a593Smuzhiyun  *	controller. Return 0 if it can be.
573*4882a593Smuzhiyun  */
574*4882a593Smuzhiyun 
it821x_check_atapi_dma(struct ata_queued_cmd * qc)575*4882a593Smuzhiyun static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun 	struct ata_port *ap = qc->ap;
578*4882a593Smuzhiyun 	struct it821x_dev *itdev = ap->private_data;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	/* Only use dma for transfers to/from the media. */
581*4882a593Smuzhiyun 	if (ata_qc_raw_nbytes(qc) < 2048)
582*4882a593Smuzhiyun 		return -EOPNOTSUPP;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* No ATAPI DMA in smart mode */
585*4882a593Smuzhiyun 	if (itdev->smart)
586*4882a593Smuzhiyun 		return -EOPNOTSUPP;
587*4882a593Smuzhiyun 	/* No ATAPI DMA on rev 10 */
588*4882a593Smuzhiyun 	if (itdev->timing10)
589*4882a593Smuzhiyun 		return -EOPNOTSUPP;
590*4882a593Smuzhiyun 	/* Cool */
591*4882a593Smuzhiyun 	return 0;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /**
595*4882a593Smuzhiyun  *	it821x_display_disk	-	display disk setup
596*4882a593Smuzhiyun  *	@n: Device number
597*4882a593Smuzhiyun  *	@buf: Buffer block from firmware
598*4882a593Smuzhiyun  *
599*4882a593Smuzhiyun  *	Produce a nice informative display of the device setup as provided
600*4882a593Smuzhiyun  *	by the firmware.
601*4882a593Smuzhiyun  */
602*4882a593Smuzhiyun 
it821x_display_disk(int n,u8 * buf)603*4882a593Smuzhiyun static void it821x_display_disk(int n, u8 *buf)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	unsigned char id[41];
606*4882a593Smuzhiyun 	int mode = 0;
607*4882a593Smuzhiyun 	const char *mtype = "";
608*4882a593Smuzhiyun 	char mbuf[8];
609*4882a593Smuzhiyun 	const char *cbl = "(40 wire cable)";
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	static const char *types[5] = {
612*4882a593Smuzhiyun 		"RAID0", "RAID1", "RAID 0+1", "JBOD", "DISK"
613*4882a593Smuzhiyun 	};
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (buf[52] > 4)	/* No Disk */
616*4882a593Smuzhiyun 		return;
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	ata_id_c_string((u16 *)buf, id, 0, 41);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	if (buf[51]) {
621*4882a593Smuzhiyun 		mode = ffs(buf[51]);
622*4882a593Smuzhiyun 		mtype = "UDMA";
623*4882a593Smuzhiyun 	} else if (buf[49]) {
624*4882a593Smuzhiyun 		mode = ffs(buf[49]);
625*4882a593Smuzhiyun 		mtype = "MWDMA";
626*4882a593Smuzhiyun 	}
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	if (buf[76])
629*4882a593Smuzhiyun 		cbl = "";
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	if (mode)
632*4882a593Smuzhiyun 		snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
633*4882a593Smuzhiyun 	else
634*4882a593Smuzhiyun 		strcpy(mbuf, "PIO");
635*4882a593Smuzhiyun 	if (buf[52] == 4)
636*4882a593Smuzhiyun 		printk(KERN_INFO "%d: %-6s %-8s          %s %s\n",
637*4882a593Smuzhiyun 				n, mbuf, types[buf[52]], id, cbl);
638*4882a593Smuzhiyun 	else
639*4882a593Smuzhiyun 		printk(KERN_INFO "%d: %-6s %-8s Volume: %1d %s %s\n",
640*4882a593Smuzhiyun 				n, mbuf, types[buf[52]], buf[53], id, cbl);
641*4882a593Smuzhiyun 	if (buf[125] < 100)
642*4882a593Smuzhiyun 		printk(KERN_INFO "%d: Rebuilding: %d%%\n", n, buf[125]);
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun /**
646*4882a593Smuzhiyun  *	it821x_firmware_command		-	issue firmware command
647*4882a593Smuzhiyun  *	@ap: IT821x port to interrogate
648*4882a593Smuzhiyun  *	@cmd: command
649*4882a593Smuzhiyun  *	@len: length
650*4882a593Smuzhiyun  *
651*4882a593Smuzhiyun  *	Issue firmware commands expecting data back from the controller. We
652*4882a593Smuzhiyun  *	use this to issue commands that do not go via the normal paths. Other
653*4882a593Smuzhiyun  *	commands such as 0xFC can be issued normally.
654*4882a593Smuzhiyun  */
655*4882a593Smuzhiyun 
it821x_firmware_command(struct ata_port * ap,u8 cmd,int len)656*4882a593Smuzhiyun static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
657*4882a593Smuzhiyun {
658*4882a593Smuzhiyun 	u8 status;
659*4882a593Smuzhiyun 	int n = 0;
660*4882a593Smuzhiyun 	u16 *buf = kmalloc(len, GFP_KERNEL);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	if (!buf)
663*4882a593Smuzhiyun 		return NULL;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	/* This isn't quite a normal ATA command as we are talking to the
666*4882a593Smuzhiyun 	   firmware not the drives */
667*4882a593Smuzhiyun 	ap->ctl |= ATA_NIEN;
668*4882a593Smuzhiyun 	iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
669*4882a593Smuzhiyun 	ata_wait_idle(ap);
670*4882a593Smuzhiyun 	iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
671*4882a593Smuzhiyun 	iowrite8(cmd, ap->ioaddr.command_addr);
672*4882a593Smuzhiyun 	udelay(1);
673*4882a593Smuzhiyun 	/* This should be almost immediate but a little paranoia goes a long
674*4882a593Smuzhiyun 	   way. */
675*4882a593Smuzhiyun 	while(n++ < 10) {
676*4882a593Smuzhiyun 		status = ioread8(ap->ioaddr.status_addr);
677*4882a593Smuzhiyun 		if (status & ATA_ERR) {
678*4882a593Smuzhiyun 			kfree(buf);
679*4882a593Smuzhiyun 			printk(KERN_ERR "it821x_firmware_command: rejected\n");
680*4882a593Smuzhiyun 			return NULL;
681*4882a593Smuzhiyun 		}
682*4882a593Smuzhiyun 		if (status & ATA_DRQ) {
683*4882a593Smuzhiyun 			ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
684*4882a593Smuzhiyun 			return (u8 *)buf;
685*4882a593Smuzhiyun 		}
686*4882a593Smuzhiyun 		usleep_range(500, 1000);
687*4882a593Smuzhiyun 	}
688*4882a593Smuzhiyun 	kfree(buf);
689*4882a593Smuzhiyun 	printk(KERN_ERR "it821x_firmware_command: timeout\n");
690*4882a593Smuzhiyun 	return NULL;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun /**
694*4882a593Smuzhiyun  *	it821x_probe_firmware	-	firmware reporting/setup
695*4882a593Smuzhiyun  *	@ap: IT821x port being probed
696*4882a593Smuzhiyun  *
697*4882a593Smuzhiyun  *	Probe the firmware of the controller by issuing firmware command
698*4882a593Smuzhiyun  *	0xFA and analysing the returned data.
699*4882a593Smuzhiyun  */
700*4882a593Smuzhiyun 
it821x_probe_firmware(struct ata_port * ap)701*4882a593Smuzhiyun static void it821x_probe_firmware(struct ata_port *ap)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	u8 *buf;
704*4882a593Smuzhiyun 	int i;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	/* This is a bit ugly as we can't just issue a task file to a device
707*4882a593Smuzhiyun 	   as this is controller magic */
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	buf = it821x_firmware_command(ap, 0xFA, 512);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	if (buf != NULL) {
712*4882a593Smuzhiyun 		printk(KERN_INFO "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
713*4882a593Smuzhiyun 				buf[505],
714*4882a593Smuzhiyun 				buf[506],
715*4882a593Smuzhiyun 				buf[507],
716*4882a593Smuzhiyun 				buf[508]);
717*4882a593Smuzhiyun 		for (i = 0; i < 4; i++)
718*4882a593Smuzhiyun  			it821x_display_disk(i, buf + 128 * i);
719*4882a593Smuzhiyun 		kfree(buf);
720*4882a593Smuzhiyun 	}
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun /**
726*4882a593Smuzhiyun  *	it821x_port_start	-	port setup
727*4882a593Smuzhiyun  *	@ap: ATA port being set up
728*4882a593Smuzhiyun  *
729*4882a593Smuzhiyun  *	The it821x needs to maintain private data structures and also to
730*4882a593Smuzhiyun  *	use the standard PCI interface which lacks support for this
731*4882a593Smuzhiyun  *	functionality. We instead set up the private data on the port
732*4882a593Smuzhiyun  *	start hook, and tear it down on port stop
733*4882a593Smuzhiyun  */
734*4882a593Smuzhiyun 
it821x_port_start(struct ata_port * ap)735*4882a593Smuzhiyun static int it821x_port_start(struct ata_port *ap)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
738*4882a593Smuzhiyun 	struct it821x_dev *itdev;
739*4882a593Smuzhiyun 	u8 conf;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	int ret = ata_bmdma_port_start(ap);
742*4882a593Smuzhiyun 	if (ret < 0)
743*4882a593Smuzhiyun 		return ret;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
746*4882a593Smuzhiyun 	if (itdev == NULL)
747*4882a593Smuzhiyun 		return -ENOMEM;
748*4882a593Smuzhiyun 	ap->private_data = itdev;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	pci_read_config_byte(pdev, 0x50, &conf);
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	if (conf & 1) {
753*4882a593Smuzhiyun 		itdev->smart = 1;
754*4882a593Smuzhiyun 		/* Long I/O's although allowed in LBA48 space cause the
755*4882a593Smuzhiyun 		   onboard firmware to enter the twighlight zone */
756*4882a593Smuzhiyun 		/* No ATAPI DMA in this mode either */
757*4882a593Smuzhiyun 		if (ap->port_no == 0)
758*4882a593Smuzhiyun 			it821x_probe_firmware(ap);
759*4882a593Smuzhiyun 	}
760*4882a593Smuzhiyun 	/* Pull the current clocks from 0x50 */
761*4882a593Smuzhiyun 	if (conf & (1 << (1 + ap->port_no)))
762*4882a593Smuzhiyun 		itdev->clock_mode = ATA_50;
763*4882a593Smuzhiyun 	else
764*4882a593Smuzhiyun 		itdev->clock_mode = ATA_66;
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	itdev->want[0][1] = ATA_ANY;
767*4882a593Smuzhiyun 	itdev->want[1][1] = ATA_ANY;
768*4882a593Smuzhiyun 	itdev->last_device = -1;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	if (pdev->revision == 0x10) {
771*4882a593Smuzhiyun 		itdev->timing10 = 1;
772*4882a593Smuzhiyun 		/* Need to disable ATAPI DMA for this case */
773*4882a593Smuzhiyun 		if (!itdev->smart)
774*4882a593Smuzhiyun 			printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
775*4882a593Smuzhiyun 	}
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	return 0;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun /**
781*4882a593Smuzhiyun  *	it821x_rdc_cable	-	Cable detect for RDC1010
782*4882a593Smuzhiyun  *	@ap: port we are checking
783*4882a593Smuzhiyun  *
784*4882a593Smuzhiyun  *	Return the RDC1010 cable type. Unlike the IT821x we know how to do
785*4882a593Smuzhiyun  *	this and can do host side cable detect
786*4882a593Smuzhiyun  */
787*4882a593Smuzhiyun 
it821x_rdc_cable(struct ata_port * ap)788*4882a593Smuzhiyun static int it821x_rdc_cable(struct ata_port *ap)
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun 	u16 r40;
791*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	pci_read_config_word(pdev, 0x40, &r40);
794*4882a593Smuzhiyun 	if (r40 & (1 << (2 + ap->port_no)))
795*4882a593Smuzhiyun 		return ATA_CBL_PATA40;
796*4882a593Smuzhiyun 	return ATA_CBL_PATA80;
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun static struct scsi_host_template it821x_sht = {
800*4882a593Smuzhiyun 	ATA_BMDMA_SHT(DRV_NAME),
801*4882a593Smuzhiyun };
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun static struct ata_port_operations it821x_smart_port_ops = {
804*4882a593Smuzhiyun 	.inherits	= &ata_bmdma_port_ops,
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	.check_atapi_dma= it821x_check_atapi_dma,
807*4882a593Smuzhiyun 	.qc_issue	= it821x_smart_qc_issue,
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	.cable_detect	= ata_cable_80wire,
810*4882a593Smuzhiyun 	.set_mode	= it821x_smart_set_mode,
811*4882a593Smuzhiyun 	.dev_config	= it821x_dev_config,
812*4882a593Smuzhiyun 	.read_id	= it821x_read_id,
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	.port_start	= it821x_port_start,
815*4882a593Smuzhiyun };
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun static struct ata_port_operations it821x_passthru_port_ops = {
818*4882a593Smuzhiyun 	.inherits	= &ata_bmdma_port_ops,
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	.check_atapi_dma= it821x_check_atapi_dma,
821*4882a593Smuzhiyun 	.sff_dev_select	= it821x_passthru_dev_select,
822*4882a593Smuzhiyun 	.bmdma_start 	= it821x_passthru_bmdma_start,
823*4882a593Smuzhiyun 	.bmdma_stop	= it821x_passthru_bmdma_stop,
824*4882a593Smuzhiyun 	.qc_issue	= it821x_passthru_qc_issue,
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	.cable_detect	= ata_cable_unknown,
827*4882a593Smuzhiyun 	.set_piomode	= it821x_passthru_set_piomode,
828*4882a593Smuzhiyun 	.set_dmamode	= it821x_passthru_set_dmamode,
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	.port_start	= it821x_port_start,
831*4882a593Smuzhiyun };
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun static struct ata_port_operations it821x_rdc_port_ops = {
834*4882a593Smuzhiyun 	.inherits	= &ata_bmdma_port_ops,
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	.check_atapi_dma= it821x_check_atapi_dma,
837*4882a593Smuzhiyun 	.sff_dev_select	= it821x_passthru_dev_select,
838*4882a593Smuzhiyun 	.bmdma_start 	= it821x_passthru_bmdma_start,
839*4882a593Smuzhiyun 	.bmdma_stop	= it821x_passthru_bmdma_stop,
840*4882a593Smuzhiyun 	.qc_issue	= it821x_passthru_qc_issue,
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	.cable_detect	= it821x_rdc_cable,
843*4882a593Smuzhiyun 	.set_piomode	= it821x_passthru_set_piomode,
844*4882a593Smuzhiyun 	.set_dmamode	= it821x_passthru_set_dmamode,
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	.port_start	= it821x_port_start,
847*4882a593Smuzhiyun };
848*4882a593Smuzhiyun 
it821x_disable_raid(struct pci_dev * pdev)849*4882a593Smuzhiyun static void it821x_disable_raid(struct pci_dev *pdev)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun 	/* Neither the RDC nor the IT8211 */
852*4882a593Smuzhiyun 	if (pdev->vendor != PCI_VENDOR_ID_ITE ||
853*4882a593Smuzhiyun 			pdev->device != PCI_DEVICE_ID_ITE_8212)
854*4882a593Smuzhiyun 			return;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	/* Reset local CPU, and set BIOS not ready */
857*4882a593Smuzhiyun 	pci_write_config_byte(pdev, 0x5E, 0x01);
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	/* Set to bypass mode, and reset PCI bus */
860*4882a593Smuzhiyun 	pci_write_config_byte(pdev, 0x50, 0x00);
861*4882a593Smuzhiyun 	pci_write_config_word(pdev, PCI_COMMAND,
862*4882a593Smuzhiyun 			      PCI_COMMAND_PARITY | PCI_COMMAND_IO |
863*4882a593Smuzhiyun 			      PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
864*4882a593Smuzhiyun 	pci_write_config_word(pdev, 0x40, 0xA0F3);
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	pci_write_config_dword(pdev,0x4C, 0x02040204);
867*4882a593Smuzhiyun 	pci_write_config_byte(pdev, 0x42, 0x36);
868*4882a593Smuzhiyun 	pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 
it821x_init_one(struct pci_dev * pdev,const struct pci_device_id * id)872*4882a593Smuzhiyun static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun 	u8 conf;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	static const struct ata_port_info info_smart = {
877*4882a593Smuzhiyun 		.flags = ATA_FLAG_SLAVE_POSS,
878*4882a593Smuzhiyun 		.pio_mask = ATA_PIO4,
879*4882a593Smuzhiyun 		.mwdma_mask = ATA_MWDMA2,
880*4882a593Smuzhiyun 		.udma_mask = ATA_UDMA6,
881*4882a593Smuzhiyun 		.port_ops = &it821x_smart_port_ops
882*4882a593Smuzhiyun 	};
883*4882a593Smuzhiyun 	static const struct ata_port_info info_passthru = {
884*4882a593Smuzhiyun 		.flags = ATA_FLAG_SLAVE_POSS,
885*4882a593Smuzhiyun 		.pio_mask = ATA_PIO4,
886*4882a593Smuzhiyun 		.mwdma_mask = ATA_MWDMA2,
887*4882a593Smuzhiyun 		.udma_mask = ATA_UDMA6,
888*4882a593Smuzhiyun 		.port_ops = &it821x_passthru_port_ops
889*4882a593Smuzhiyun 	};
890*4882a593Smuzhiyun 	static const struct ata_port_info info_rdc = {
891*4882a593Smuzhiyun 		.flags = ATA_FLAG_SLAVE_POSS,
892*4882a593Smuzhiyun 		.pio_mask = ATA_PIO4,
893*4882a593Smuzhiyun 		.mwdma_mask = ATA_MWDMA2,
894*4882a593Smuzhiyun 		.udma_mask = ATA_UDMA6,
895*4882a593Smuzhiyun 		.port_ops = &it821x_rdc_port_ops
896*4882a593Smuzhiyun 	};
897*4882a593Smuzhiyun 	static const struct ata_port_info info_rdc_11 = {
898*4882a593Smuzhiyun 		.flags = ATA_FLAG_SLAVE_POSS,
899*4882a593Smuzhiyun 		.pio_mask = ATA_PIO4,
900*4882a593Smuzhiyun 		.mwdma_mask = ATA_MWDMA2,
901*4882a593Smuzhiyun 		/* No UDMA */
902*4882a593Smuzhiyun 		.port_ops = &it821x_rdc_port_ops
903*4882a593Smuzhiyun 	};
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	const struct ata_port_info *ppi[] = { NULL, NULL };
906*4882a593Smuzhiyun 	static const char *mode[2] = { "pass through", "smart" };
907*4882a593Smuzhiyun 	int rc;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	rc = pcim_enable_device(pdev);
910*4882a593Smuzhiyun 	if (rc)
911*4882a593Smuzhiyun 		return rc;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	if (pdev->vendor == PCI_VENDOR_ID_RDC) {
914*4882a593Smuzhiyun 		/* Deal with Vortex86SX */
915*4882a593Smuzhiyun 		if (pdev->revision == 0x11)
916*4882a593Smuzhiyun 			ppi[0] = &info_rdc_11;
917*4882a593Smuzhiyun 		else
918*4882a593Smuzhiyun 			ppi[0] = &info_rdc;
919*4882a593Smuzhiyun 	} else {
920*4882a593Smuzhiyun 		/* Force the card into bypass mode if so requested */
921*4882a593Smuzhiyun 		if (it8212_noraid) {
922*4882a593Smuzhiyun 			printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
923*4882a593Smuzhiyun 			it821x_disable_raid(pdev);
924*4882a593Smuzhiyun 		}
925*4882a593Smuzhiyun 		pci_read_config_byte(pdev, 0x50, &conf);
926*4882a593Smuzhiyun 		conf &= 1;
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 		printk(KERN_INFO DRV_NAME": controller in %s mode.\n",
929*4882a593Smuzhiyun 								mode[conf]);
930*4882a593Smuzhiyun 		if (conf == 0)
931*4882a593Smuzhiyun 			ppi[0] = &info_passthru;
932*4882a593Smuzhiyun 		else
933*4882a593Smuzhiyun 			ppi[0] = &info_smart;
934*4882a593Smuzhiyun 	}
935*4882a593Smuzhiyun 	return ata_pci_bmdma_init_one(pdev, ppi, &it821x_sht, NULL, 0);
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
it821x_reinit_one(struct pci_dev * pdev)939*4882a593Smuzhiyun static int it821x_reinit_one(struct pci_dev *pdev)
940*4882a593Smuzhiyun {
941*4882a593Smuzhiyun 	struct ata_host *host = pci_get_drvdata(pdev);
942*4882a593Smuzhiyun 	int rc;
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	rc = ata_pci_device_do_resume(pdev);
945*4882a593Smuzhiyun 	if (rc)
946*4882a593Smuzhiyun 		return rc;
947*4882a593Smuzhiyun 	/* Resume - turn raid back off if need be */
948*4882a593Smuzhiyun 	if (it8212_noraid)
949*4882a593Smuzhiyun 		it821x_disable_raid(pdev);
950*4882a593Smuzhiyun 	ata_host_resume(host);
951*4882a593Smuzhiyun 	return rc;
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun #endif
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun static const struct pci_device_id it821x[] = {
956*4882a593Smuzhiyun 	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
957*4882a593Smuzhiyun 	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
958*4882a593Smuzhiyun 	{ PCI_VDEVICE(RDC, PCI_DEVICE_ID_RDC_D1010), },
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	{ },
961*4882a593Smuzhiyun };
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun static struct pci_driver it821x_pci_driver = {
964*4882a593Smuzhiyun 	.name 		= DRV_NAME,
965*4882a593Smuzhiyun 	.id_table	= it821x,
966*4882a593Smuzhiyun 	.probe 		= it821x_init_one,
967*4882a593Smuzhiyun 	.remove		= ata_pci_remove_one,
968*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
969*4882a593Smuzhiyun 	.suspend	= ata_pci_device_suspend,
970*4882a593Smuzhiyun 	.resume		= it821x_reinit_one,
971*4882a593Smuzhiyun #endif
972*4882a593Smuzhiyun };
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun module_pci_driver(it821x_pci_driver);
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun MODULE_AUTHOR("Alan Cox");
977*4882a593Smuzhiyun MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
978*4882a593Smuzhiyun MODULE_LICENSE("GPL");
979*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, it821x);
980*4882a593Smuzhiyun MODULE_VERSION(DRV_VERSION);
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun module_param_named(noraid, it8212_noraid, int, S_IRUGO);
983*4882a593Smuzhiyun MODULE_PARM_DESC(noraid, "Force card into bypass mode");
984