xref: /openbmc/linux/drivers/ata/pata_it821x.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
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  *
18669a5db4SJeff Garzik  *  Documentation available from
19669a5db4SJeff Garzik  * 	http://www.ite.com.tw/pc/IT8212F_V04.pdf
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/init.h>
76669a5db4SJeff Garzik #include <linux/blkdev.h>
77669a5db4SJeff Garzik #include <linux/delay.h>
78*5a0e3ad6STejun Heo #include <linux/slab.h>
79669a5db4SJeff Garzik #include <scsi/scsi_host.h>
80669a5db4SJeff Garzik #include <linux/libata.h>
81669a5db4SJeff Garzik 
82669a5db4SJeff Garzik 
83669a5db4SJeff Garzik #define DRV_NAME "pata_it821x"
844a99d95fSAlan Cox #define DRV_VERSION "0.4.2"
85669a5db4SJeff Garzik 
86669a5db4SJeff Garzik struct it821x_dev
87669a5db4SJeff Garzik {
88669a5db4SJeff Garzik 	unsigned int smart:1,		/* Are we in smart raid mode */
89669a5db4SJeff Garzik 		timing10:1;		/* Rev 0x10 */
90669a5db4SJeff Garzik 	u8	clock_mode;		/* 0, ATA_50 or ATA_66 */
91669a5db4SJeff Garzik 	u8	want[2][2];		/* Mode/Pri log for master slave */
92669a5db4SJeff Garzik 	/* We need these for switching the clock when DMA goes on/off
93669a5db4SJeff Garzik 	   The high byte is the 66Mhz timing */
94669a5db4SJeff Garzik 	u16	pio[2];			/* Cached PIO values */
95669a5db4SJeff Garzik 	u16	mwdma[2];		/* Cached MWDMA values */
96669a5db4SJeff Garzik 	u16	udma[2];		/* Cached UDMA values (per drive) */
97669a5db4SJeff Garzik 	u16	last_device;		/* Master or slave loaded ? */
98669a5db4SJeff Garzik };
99669a5db4SJeff Garzik 
100669a5db4SJeff Garzik #define ATA_66		0
101669a5db4SJeff Garzik #define ATA_50		1
102669a5db4SJeff Garzik #define ATA_ANY		2
103669a5db4SJeff Garzik 
104669a5db4SJeff Garzik #define UDMA_OFF	0
105669a5db4SJeff Garzik #define MWDMA_OFF	0
106669a5db4SJeff Garzik 
107669a5db4SJeff Garzik /*
108669a5db4SJeff Garzik  *	We allow users to force the card into non raid mode without
1093a4fa0a2SRobert P. J. Day  *	flashing the alternative BIOS. This is also necessary right now
110669a5db4SJeff Garzik  *	for embedded platforms that cannot run a PC BIOS but are using this
111669a5db4SJeff Garzik  *	device.
112669a5db4SJeff Garzik  */
113669a5db4SJeff Garzik 
114669a5db4SJeff Garzik static int it8212_noraid;
115669a5db4SJeff Garzik 
116669a5db4SJeff Garzik /**
117669a5db4SJeff Garzik  *	it821x_program	-	program the PIO/MWDMA registers
118669a5db4SJeff Garzik  *	@ap: ATA port
119669a5db4SJeff Garzik  *	@adev: Device to program
120669a5db4SJeff Garzik  *	@timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
121669a5db4SJeff Garzik  *
122669a5db4SJeff Garzik  *	Program the PIO/MWDMA timing for this channel according to the
123669a5db4SJeff Garzik  *	current clock. These share the same register so are managed by
124669a5db4SJeff Garzik  *	the DMA start/stop sequence as with the old driver.
125669a5db4SJeff Garzik  */
126669a5db4SJeff Garzik 
127669a5db4SJeff Garzik static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
128669a5db4SJeff Garzik {
129669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
130669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
131669a5db4SJeff Garzik 	int channel = ap->port_no;
132669a5db4SJeff Garzik 	u8 conf;
133669a5db4SJeff Garzik 
134669a5db4SJeff Garzik 	/* Program PIO/MWDMA timing bits */
135669a5db4SJeff Garzik 	if (itdev->clock_mode == ATA_66)
136669a5db4SJeff Garzik 		conf = timing >> 8;
137669a5db4SJeff Garzik 	else
138669a5db4SJeff Garzik 		conf = timing & 0xFF;
139669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
140669a5db4SJeff Garzik }
141669a5db4SJeff Garzik 
142669a5db4SJeff Garzik 
143669a5db4SJeff Garzik /**
144669a5db4SJeff Garzik  *	it821x_program_udma	-	program the UDMA registers
145669a5db4SJeff Garzik  *	@ap: ATA port
146669a5db4SJeff Garzik  *	@adev: ATA device to update
147669a5db4SJeff Garzik  *	@timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
148669a5db4SJeff Garzik  *
149669a5db4SJeff Garzik  *	Program the UDMA timing for this drive according to the
150669a5db4SJeff Garzik  *	current clock. Handles the dual clocks and also knows about
151669a5db4SJeff Garzik  *	the errata on the 0x10 revision. The UDMA errata is partly handled
152669a5db4SJeff Garzik  *	here and partly in start_dma.
153669a5db4SJeff Garzik  */
154669a5db4SJeff Garzik 
155669a5db4SJeff Garzik static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
156669a5db4SJeff Garzik {
157669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
158669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
159669a5db4SJeff Garzik 	int channel = ap->port_no;
160669a5db4SJeff Garzik 	int unit = adev->devno;
161669a5db4SJeff Garzik 	u8 conf;
162669a5db4SJeff Garzik 
163669a5db4SJeff Garzik 	/* Program UDMA timing bits */
164669a5db4SJeff Garzik 	if (itdev->clock_mode == ATA_66)
165669a5db4SJeff Garzik 		conf = timing >> 8;
166669a5db4SJeff Garzik 	else
167669a5db4SJeff Garzik 		conf = timing & 0xFF;
168669a5db4SJeff Garzik 	if (itdev->timing10 == 0)
169669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
170669a5db4SJeff Garzik 	else {
171669a5db4SJeff Garzik 		/* Early revision must be programmed for both together */
172669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
173669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
174669a5db4SJeff Garzik 	}
175669a5db4SJeff Garzik }
176669a5db4SJeff Garzik 
177669a5db4SJeff Garzik /**
178669a5db4SJeff Garzik  *	it821x_clock_strategy
179669a5db4SJeff Garzik  *	@ap: ATA interface
180669a5db4SJeff Garzik  *	@adev: ATA device being updated
181669a5db4SJeff Garzik  *
182669a5db4SJeff Garzik  *	Select between the 50 and 66Mhz base clocks to get the best
183669a5db4SJeff Garzik  *	results for this interface.
184669a5db4SJeff Garzik  */
185669a5db4SJeff Garzik 
186669a5db4SJeff Garzik static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
187669a5db4SJeff Garzik {
188669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
189669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
190669a5db4SJeff Garzik 	u8 unit = adev->devno;
191669a5db4SJeff Garzik 	struct ata_device *pair = ata_dev_pair(adev);
192669a5db4SJeff Garzik 
193669a5db4SJeff Garzik 	int clock, altclock;
194669a5db4SJeff Garzik 	u8 v;
195669a5db4SJeff Garzik 	int sel = 0;
196669a5db4SJeff Garzik 
197669a5db4SJeff Garzik 	/* Look for the most wanted clocking */
198669a5db4SJeff Garzik 	if (itdev->want[0][0] > itdev->want[1][0]) {
199669a5db4SJeff Garzik 		clock = itdev->want[0][1];
200669a5db4SJeff Garzik 		altclock = itdev->want[1][1];
201669a5db4SJeff Garzik 	} else {
202669a5db4SJeff Garzik 		clock = itdev->want[1][1];
203669a5db4SJeff Garzik 		altclock = itdev->want[0][1];
204669a5db4SJeff Garzik 	}
205669a5db4SJeff Garzik 
206669a5db4SJeff Garzik 	/* Master doesn't care does the slave ? */
207669a5db4SJeff Garzik 	if (clock == ATA_ANY)
208669a5db4SJeff Garzik 		clock = altclock;
209669a5db4SJeff Garzik 
210669a5db4SJeff Garzik 	/* Nobody cares - keep the same clock */
211669a5db4SJeff Garzik 	if (clock == ATA_ANY)
212669a5db4SJeff Garzik 		return;
213669a5db4SJeff Garzik 	/* No change */
214669a5db4SJeff Garzik 	if (clock == itdev->clock_mode)
215669a5db4SJeff Garzik 		return;
216669a5db4SJeff Garzik 
217669a5db4SJeff Garzik 	/* Load this into the controller */
218669a5db4SJeff Garzik 	if (clock == ATA_66)
219669a5db4SJeff Garzik 		itdev->clock_mode = ATA_66;
220669a5db4SJeff Garzik 	else {
221669a5db4SJeff Garzik 		itdev->clock_mode = ATA_50;
222669a5db4SJeff Garzik 		sel = 1;
223669a5db4SJeff Garzik 	}
224669a5db4SJeff Garzik 	pci_read_config_byte(pdev, 0x50, &v);
225669a5db4SJeff Garzik 	v &= ~(1 << (1 + ap->port_no));
226669a5db4SJeff Garzik 	v |= sel << (1 + ap->port_no);
227669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x50, v);
228669a5db4SJeff Garzik 
229669a5db4SJeff Garzik 	/*
230669a5db4SJeff Garzik 	 *	Reprogram the UDMA/PIO of the pair drive for the switch
231669a5db4SJeff Garzik 	 *	MWDMA will be dealt with by the dma switcher
232669a5db4SJeff Garzik 	 */
233669a5db4SJeff Garzik 	if (pair && itdev->udma[1-unit] != UDMA_OFF) {
234669a5db4SJeff Garzik 		it821x_program_udma(ap, pair, itdev->udma[1-unit]);
235669a5db4SJeff Garzik 		it821x_program(ap, pair, itdev->pio[1-unit]);
236669a5db4SJeff Garzik 	}
237669a5db4SJeff Garzik 	/*
238669a5db4SJeff Garzik 	 *	Reprogram the UDMA/PIO of our drive for the switch.
239669a5db4SJeff Garzik 	 *	MWDMA will be dealt with by the dma switcher
240669a5db4SJeff Garzik 	 */
241669a5db4SJeff Garzik 	if (itdev->udma[unit] != UDMA_OFF) {
242669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
243669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[unit]);
244669a5db4SJeff Garzik 	}
245669a5db4SJeff Garzik }
246669a5db4SJeff Garzik 
247669a5db4SJeff Garzik /**
248669a5db4SJeff Garzik  *	it821x_passthru_set_piomode	-	set PIO mode data
249669a5db4SJeff Garzik  *	@ap: ATA interface
250669a5db4SJeff Garzik  *	@adev: ATA device
251669a5db4SJeff Garzik  *
252669a5db4SJeff Garzik  *	Configure for PIO mode. This is complicated as the register is
253669a5db4SJeff Garzik  *	shared by PIO and MWDMA and for both channels.
254669a5db4SJeff Garzik  */
255669a5db4SJeff Garzik 
256669a5db4SJeff Garzik static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
257669a5db4SJeff Garzik {
258669a5db4SJeff Garzik 	/* Spec says 89 ref driver uses 88 */
259669a5db4SJeff Garzik 	static const u16 pio[]	= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
260669a5db4SJeff Garzik 	static const u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
261669a5db4SJeff Garzik 
262669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
263669a5db4SJeff Garzik 	int unit = adev->devno;
264669a5db4SJeff Garzik 	int mode_wanted = adev->pio_mode - XFER_PIO_0;
265669a5db4SJeff Garzik 
266669a5db4SJeff Garzik 	/* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
267669a5db4SJeff Garzik 	itdev->want[unit][1] = pio_want[mode_wanted];
268669a5db4SJeff Garzik 	itdev->want[unit][0] = 1;	/* PIO is lowest priority */
269669a5db4SJeff Garzik 	itdev->pio[unit] = pio[mode_wanted];
270669a5db4SJeff Garzik 	it821x_clock_strategy(ap, adev);
271669a5db4SJeff Garzik 	it821x_program(ap, adev, itdev->pio[unit]);
272669a5db4SJeff Garzik }
273669a5db4SJeff Garzik 
274669a5db4SJeff Garzik /**
275669a5db4SJeff Garzik  *	it821x_passthru_set_dmamode	-	set initial DMA mode data
276669a5db4SJeff Garzik  *	@ap: ATA interface
277669a5db4SJeff Garzik  *	@adev: ATA device
278669a5db4SJeff Garzik  *
279669a5db4SJeff Garzik  *	Set up the DMA modes. The actions taken depend heavily on the mode
280669a5db4SJeff Garzik  *	to use. If UDMA is used as is hopefully the usual case then the
281669a5db4SJeff Garzik  *	timing register is private and we need only consider the clock. If
282669a5db4SJeff Garzik  *	we are using MWDMA then we have to manage the setting ourself as
283669a5db4SJeff Garzik  *	we switch devices and mode.
284669a5db4SJeff Garzik  */
285669a5db4SJeff Garzik 
286669a5db4SJeff Garzik static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
287669a5db4SJeff Garzik {
288669a5db4SJeff Garzik 	static const u16 dma[]	= 	{ 0x8866, 0x3222, 0x3121 };
289669a5db4SJeff Garzik 	static const u8 mwdma_want[] =  { ATA_ANY, ATA_66, ATA_ANY };
290669a5db4SJeff Garzik 	static const u16 udma[]	= 	{ 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
291669a5db4SJeff Garzik 	static const u8 udma_want[] =   { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
292669a5db4SJeff Garzik 
293669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
294669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
295669a5db4SJeff Garzik 	int channel = ap->port_no;
296669a5db4SJeff Garzik 	int unit = adev->devno;
297669a5db4SJeff Garzik 	u8 conf;
298669a5db4SJeff Garzik 
299669a5db4SJeff Garzik 	if (adev->dma_mode >= XFER_UDMA_0) {
300669a5db4SJeff Garzik 		int mode_wanted = adev->dma_mode - XFER_UDMA_0;
301669a5db4SJeff Garzik 
302669a5db4SJeff Garzik 		itdev->want[unit][1] = udma_want[mode_wanted];
303669a5db4SJeff Garzik 		itdev->want[unit][0] = 3;	/* UDMA is high priority */
304669a5db4SJeff Garzik 		itdev->mwdma[unit] = MWDMA_OFF;
305669a5db4SJeff Garzik 		itdev->udma[unit] = udma[mode_wanted];
306669a5db4SJeff Garzik 		if (mode_wanted >= 5)
307669a5db4SJeff Garzik 			itdev->udma[unit] |= 0x8080;	/* UDMA 5/6 select on */
308669a5db4SJeff Garzik 
309669a5db4SJeff Garzik 		/* UDMA on. Again revision 0x10 must do the pair */
310669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
311669a5db4SJeff Garzik 		if (itdev->timing10)
312669a5db4SJeff Garzik 			conf &= channel ? 0x9F: 0xE7;
313669a5db4SJeff Garzik 		else
314669a5db4SJeff Garzik 			conf &= ~ (1 << (3 + 2 * channel + unit));
315669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x50, conf);
316669a5db4SJeff Garzik 		it821x_clock_strategy(ap, adev);
317669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
318669a5db4SJeff Garzik 	} else {
319669a5db4SJeff Garzik 		int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
320669a5db4SJeff Garzik 
321669a5db4SJeff Garzik 		itdev->want[unit][1] = mwdma_want[mode_wanted];
322669a5db4SJeff Garzik 		itdev->want[unit][0] = 2;	/* MWDMA is low priority */
323669a5db4SJeff Garzik 		itdev->mwdma[unit] = dma[mode_wanted];
324669a5db4SJeff Garzik 		itdev->udma[unit] = UDMA_OFF;
325669a5db4SJeff Garzik 
326669a5db4SJeff Garzik 		/* UDMA bits off - Revision 0x10 do them in pairs */
327669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
328669a5db4SJeff Garzik 		if (itdev->timing10)
329669a5db4SJeff Garzik 			conf |= channel ? 0x60: 0x18;
330669a5db4SJeff Garzik 		else
331669a5db4SJeff Garzik 			conf |= 1 << (3 + 2 * channel + unit);
332669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x50, conf);
333669a5db4SJeff Garzik 		it821x_clock_strategy(ap, adev);
334669a5db4SJeff Garzik 	}
335669a5db4SJeff Garzik }
336669a5db4SJeff Garzik 
337669a5db4SJeff Garzik /**
338669a5db4SJeff Garzik  *	it821x_passthru_dma_start	-	DMA start callback
339669a5db4SJeff Garzik  *	@qc: Command in progress
340669a5db4SJeff Garzik  *
341669a5db4SJeff Garzik  *	Usually drivers set the DMA timing at the point the set_dmamode call
342669a5db4SJeff Garzik  *	is made. IT821x however requires we load new timings on the
343669a5db4SJeff Garzik  *	transitions in some cases.
344669a5db4SJeff Garzik  */
345669a5db4SJeff Garzik 
346669a5db4SJeff Garzik static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
347669a5db4SJeff Garzik {
348669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
349669a5db4SJeff Garzik 	struct ata_device *adev = qc->dev;
350669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
351669a5db4SJeff Garzik 	int unit = adev->devno;
352669a5db4SJeff Garzik 
353669a5db4SJeff Garzik 	if (itdev->mwdma[unit] != MWDMA_OFF)
354669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->mwdma[unit]);
355669a5db4SJeff Garzik 	else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
356669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
357669a5db4SJeff Garzik 	ata_bmdma_start(qc);
358669a5db4SJeff Garzik }
359669a5db4SJeff Garzik 
360669a5db4SJeff Garzik /**
361669a5db4SJeff Garzik  *	it821x_passthru_dma_stop	-	DMA stop callback
362669a5db4SJeff Garzik  *	@qc: ATA command
363669a5db4SJeff Garzik  *
364669a5db4SJeff Garzik  *	We loaded new timings in dma_start, as a result we need to restore
365669a5db4SJeff Garzik  *	the PIO timings in dma_stop so that the next command issue gets the
366669a5db4SJeff Garzik  *	right clock values.
367669a5db4SJeff Garzik  */
368669a5db4SJeff Garzik 
369669a5db4SJeff Garzik static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
370669a5db4SJeff Garzik {
371669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
372669a5db4SJeff Garzik 	struct ata_device *adev = qc->dev;
373669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
374669a5db4SJeff Garzik 	int unit = adev->devno;
375669a5db4SJeff Garzik 
376669a5db4SJeff Garzik 	ata_bmdma_stop(qc);
377669a5db4SJeff Garzik 	if (itdev->mwdma[unit] != MWDMA_OFF)
378669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[unit]);
379669a5db4SJeff Garzik }
380669a5db4SJeff Garzik 
381669a5db4SJeff Garzik 
382669a5db4SJeff Garzik /**
383669a5db4SJeff Garzik  *	it821x_passthru_dev_select	-	Select master/slave
384669a5db4SJeff Garzik  *	@ap: ATA port
385669a5db4SJeff Garzik  *	@device: Device number (not pointer)
386669a5db4SJeff Garzik  *
3873a4fa0a2SRobert P. J. Day  *	Device selection hook. If necessary perform clock switching
388669a5db4SJeff Garzik  */
389669a5db4SJeff Garzik 
390669a5db4SJeff Garzik static void it821x_passthru_dev_select(struct ata_port *ap,
391669a5db4SJeff Garzik 				       unsigned int device)
392669a5db4SJeff Garzik {
393669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
394669a5db4SJeff Garzik 	if (itdev && device != itdev->last_device) {
3959af5c9c9STejun Heo 		struct ata_device *adev = &ap->link.device[device];
396669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[adev->devno]);
397669a5db4SJeff Garzik 		itdev->last_device = device;
398669a5db4SJeff Garzik 	}
3999363c382STejun Heo 	ata_sff_dev_select(ap, device);
400669a5db4SJeff Garzik }
401669a5db4SJeff Garzik 
402669a5db4SJeff Garzik /**
4039363c382STejun Heo  *	it821x_smart_qc_issue		-	wrap qc issue prot
404669a5db4SJeff Garzik  *	@qc: command
405669a5db4SJeff Garzik  *
406669a5db4SJeff Garzik  *	Wrap the command issue sequence for the IT821x. We need to
407669a5db4SJeff Garzik  *	perform out own device selection timing loads before the
408669a5db4SJeff Garzik  *	usual happenings kick off
409669a5db4SJeff Garzik  */
410669a5db4SJeff Garzik 
4119363c382STejun Heo static unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc)
412669a5db4SJeff Garzik {
413669a5db4SJeff Garzik 	switch(qc->tf.command)
414669a5db4SJeff Garzik 	{
415669a5db4SJeff Garzik 		/* Commands the firmware supports */
416669a5db4SJeff Garzik 		case ATA_CMD_READ:
417669a5db4SJeff Garzik 		case ATA_CMD_READ_EXT:
418669a5db4SJeff Garzik 		case ATA_CMD_WRITE:
419669a5db4SJeff Garzik 		case ATA_CMD_WRITE_EXT:
420669a5db4SJeff Garzik 		case ATA_CMD_PIO_READ:
421669a5db4SJeff Garzik 		case ATA_CMD_PIO_READ_EXT:
422669a5db4SJeff Garzik 		case ATA_CMD_PIO_WRITE:
423669a5db4SJeff Garzik 		case ATA_CMD_PIO_WRITE_EXT:
424669a5db4SJeff Garzik 		case ATA_CMD_READ_MULTI:
425669a5db4SJeff Garzik 		case ATA_CMD_READ_MULTI_EXT:
426669a5db4SJeff Garzik 		case ATA_CMD_WRITE_MULTI:
427669a5db4SJeff Garzik 		case ATA_CMD_WRITE_MULTI_EXT:
428669a5db4SJeff Garzik 		case ATA_CMD_ID_ATA:
429963e4975SAlan Cox 		case ATA_CMD_INIT_DEV_PARAMS:
430963e4975SAlan Cox 		case 0xFC:	/* Internal 'report rebuild state' */
431669a5db4SJeff Garzik 		/* Arguably should just no-op this one */
432669a5db4SJeff Garzik 		case ATA_CMD_SET_FEATURES:
4339363c382STejun Heo 			return ata_sff_qc_issue(qc);
434669a5db4SJeff Garzik 	}
435669a5db4SJeff Garzik 	printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", 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 
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);
4519363c382STejun Heo 	return ata_sff_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 
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)) {
476616ece2eSAlan 			ata_dev_printk(dev, KERN_INFO, "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 {
481616ece2eSAlan 			ata_dev_printk(dev, KERN_INFO, "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 
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 */
511963e4975SAlan Cox 		ata_dev_printk(adev, KERN_INFO, "%sRAID%d volume",
512669a5db4SJeff Garzik 			adev->id[147]?"Bootable ":"",
513669a5db4SJeff Garzik 			adev->id[129]);
514669a5db4SJeff Garzik 		if (adev->id[129] != 1)
515669a5db4SJeff Garzik 			printk("(%dK stripe)", adev->id[146]);
516669a5db4SJeff Garzik 		printk(".\n");
517669a5db4SJeff Garzik 	}
518c5038fc0SAlan Cox 	/* This is a controller firmware triggered funny, don't
519c5038fc0SAlan Cox 	   report the drive faulty! */
520c5038fc0SAlan Cox 	adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
521963e4975SAlan Cox 	/* No HPA in 'smart' mode */
522963e4975SAlan Cox 	adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
523c5038fc0SAlan Cox }
524c5038fc0SAlan Cox 
525c5038fc0SAlan Cox /**
526963e4975SAlan Cox  *	it821x_read_id	-	Hack identify data up
527963e4975SAlan Cox  *	@adev: device to read
528963e4975SAlan Cox  *	@tf: proposed taskfile
529963e4975SAlan Cox  *	@id: buffer for returned ident data
530c5038fc0SAlan Cox  *
531963e4975SAlan Cox  *	Query the devices on this firmware driven port and slightly
532c5038fc0SAlan Cox  *	mash the identify data to stop us and common tools trying to
533c5038fc0SAlan Cox  *	use features not firmware supported. The firmware itself does
534c5038fc0SAlan Cox  *	some masking (eg SMART) but not enough.
535c5038fc0SAlan Cox  */
536c5038fc0SAlan Cox 
537963e4975SAlan Cox static unsigned int it821x_read_id(struct ata_device *adev,
538963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
539c5038fc0SAlan Cox {
540963e4975SAlan Cox 	unsigned int err_mask;
541963e4975SAlan Cox 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
542669a5db4SJeff Garzik 
543963e4975SAlan Cox 	err_mask = ata_do_dev_read_id(adev, tf, id);
544963e4975SAlan Cox 	if (err_mask)
545963e4975SAlan Cox 		return err_mask;
546963e4975SAlan Cox 	ata_id_c_string(id, model_num, ATA_ID_PROD, sizeof(model_num));
547963e4975SAlan Cox 
548963e4975SAlan Cox 	id[83] &= ~(1 << 12);	/* Cache flush is firmware handled */
549963e4975SAlan Cox 	id[83] &= ~(1 << 13);	/* Ditto for LBA48 flushes */
550963e4975SAlan Cox 	id[84] &= ~(1 << 6);	/* No FUA */
551963e4975SAlan Cox 	id[85] &= ~(1 << 10);	/* No HPA */
552963e4975SAlan Cox 	id[76] = 0;		/* No NCQ/AN etc */
553963e4975SAlan Cox 
554963e4975SAlan Cox 	if (strstr(model_num, "Integrated Technology Express")) {
555963e4975SAlan Cox 		/* Set feature bits the firmware neglects */
556963e4975SAlan Cox 		id[49] |= 0x0300;	/* LBA, DMA */
557963e4975SAlan Cox 		id[83] &= 0x7FFF;
558054e5f61SOndrej Zary 		id[83] |= 0x4400;	/* Word 83 is valid and LBA48 */
559963e4975SAlan Cox 		id[86] |= 0x0400;	/* LBA48 on */
560963e4975SAlan Cox 		id[ATA_ID_MAJOR_VER] |= 0x1F;
5617ba07d16SOndrej Zary 		/* Clear the serial number because it's different each boot
5627ba07d16SOndrej Zary 		   which breaks validation on resume */
5637ba07d16SOndrej Zary 		memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN);
564963e4975SAlan Cox 	}
565963e4975SAlan Cox 	return err_mask;
566963e4975SAlan Cox }
567669a5db4SJeff Garzik 
568669a5db4SJeff Garzik /**
569669a5db4SJeff Garzik  *	it821x_check_atapi_dma	-	ATAPI DMA handler
570669a5db4SJeff Garzik  *	@qc: Command we are about to issue
571669a5db4SJeff Garzik  *
572669a5db4SJeff Garzik  *	Decide if this ATAPI command can be issued by DMA on this
573669a5db4SJeff Garzik  *	controller. Return 0 if it can be.
574669a5db4SJeff Garzik  */
575669a5db4SJeff Garzik 
576669a5db4SJeff Garzik static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
577669a5db4SJeff Garzik {
578669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
579669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
580669a5db4SJeff Garzik 
581bce7d5e0SJeff Norden 	/* Only use dma for transfers to/from the media. */
582b63b1331STejun Heo 	if (ata_qc_raw_nbytes(qc) < 2048)
583bce7d5e0SJeff Norden 		return -EOPNOTSUPP;
584bce7d5e0SJeff Norden 
585669a5db4SJeff Garzik 	/* No ATAPI DMA in smart mode */
586669a5db4SJeff Garzik 	if (itdev->smart)
587669a5db4SJeff Garzik 		return -EOPNOTSUPP;
588669a5db4SJeff Garzik 	/* No ATAPI DMA on rev 10 */
589669a5db4SJeff Garzik 	if (itdev->timing10)
590669a5db4SJeff Garzik 		return -EOPNOTSUPP;
591669a5db4SJeff Garzik 	/* Cool */
592669a5db4SJeff Garzik 	return 0;
593669a5db4SJeff Garzik }
594669a5db4SJeff Garzik 
595963e4975SAlan Cox /**
596963e4975SAlan Cox  *	it821x_display_disk	-	display disk setup
597963e4975SAlan Cox  *	@n: Device number
598963e4975SAlan Cox  *	@buf: Buffer block from firmware
599963e4975SAlan Cox  *
600963e4975SAlan Cox  *	Produce a nice informative display of the device setup as provided
601963e4975SAlan Cox  *	by the firmware.
602963e4975SAlan Cox  */
603963e4975SAlan Cox 
604963e4975SAlan Cox static void it821x_display_disk(int n, u8 *buf)
605963e4975SAlan Cox {
606963e4975SAlan Cox 	unsigned char id[41];
607963e4975SAlan Cox 	int mode = 0;
6084ef28185SJeff Garzik 	char *mtype = "";
609963e4975SAlan Cox 	char mbuf[8];
610963e4975SAlan Cox 	char *cbl = "(40 wire cable)";
611963e4975SAlan Cox 
612963e4975SAlan Cox 	static const char *types[5] = {
613963e4975SAlan Cox 		"RAID0", "RAID1" "RAID 0+1", "JBOD", "DISK"
614963e4975SAlan Cox 	};
615963e4975SAlan Cox 
616963e4975SAlan Cox 	if (buf[52] > 4)	/* No Disk */
617963e4975SAlan Cox 		return;
618963e4975SAlan Cox 
619963e4975SAlan Cox 	ata_id_c_string((u16 *)buf, id, 0, 41);
620963e4975SAlan Cox 
621963e4975SAlan Cox 	if (buf[51]) {
622963e4975SAlan Cox 		mode = ffs(buf[51]);
623963e4975SAlan Cox 		mtype = "UDMA";
624963e4975SAlan Cox 	} else if (buf[49]) {
625963e4975SAlan Cox 		mode = ffs(buf[49]);
626963e4975SAlan Cox 		mtype = "MWDMA";
627963e4975SAlan Cox 	}
628963e4975SAlan Cox 
629963e4975SAlan Cox 	if (buf[76])
630963e4975SAlan Cox 		cbl = "";
631963e4975SAlan Cox 
632963e4975SAlan Cox 	if (mode)
633963e4975SAlan Cox 		snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
634963e4975SAlan Cox 	else
635963e4975SAlan Cox 		strcpy(mbuf, "PIO");
636963e4975SAlan Cox 	if (buf[52] == 4)
637963e4975SAlan Cox 		printk(KERN_INFO "%d: %-6s %-8s          %s %s\n",
638963e4975SAlan Cox 				n, mbuf, types[buf[52]], id, cbl);
639963e4975SAlan Cox 	else
640963e4975SAlan Cox 		printk(KERN_INFO "%d: %-6s %-8s Volume: %1d %s %s\n",
641963e4975SAlan Cox 				n, mbuf, types[buf[52]], buf[53], id, cbl);
642963e4975SAlan Cox 	if (buf[125] < 100)
643963e4975SAlan Cox 		printk(KERN_INFO "%d: Rebuilding: %d%%\n", n, buf[125]);
644963e4975SAlan Cox }
645963e4975SAlan Cox 
646963e4975SAlan Cox /**
647963e4975SAlan Cox  *	it821x_firmware_command		-	issue firmware command
648963e4975SAlan Cox  *	@ap: IT821x port to interrogate
649963e4975SAlan Cox  *	@cmd: command
650963e4975SAlan Cox  *	@len: length
651963e4975SAlan Cox  *
652963e4975SAlan Cox  *	Issue firmware commands expecting data back from the controller. We
653963e4975SAlan Cox  *	use this to issue commands that do not go via the normal paths. Other
654963e4975SAlan Cox  *	commands such as 0xFC can be issued normally.
655963e4975SAlan Cox  */
656963e4975SAlan Cox 
657963e4975SAlan Cox static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
658963e4975SAlan Cox {
659963e4975SAlan Cox 	u8 status;
660963e4975SAlan Cox 	int n = 0;
661963e4975SAlan Cox 	u16 *buf = kmalloc(len, GFP_KERNEL);
662963e4975SAlan Cox 	if (buf == NULL) {
663963e4975SAlan Cox 		printk(KERN_ERR "it821x_firmware_command: Out of memory\n");
664963e4975SAlan Cox 		return NULL;
665963e4975SAlan Cox 	}
666963e4975SAlan Cox 	/* This isn't quite a normal ATA command as we are talking to the
667963e4975SAlan Cox 	   firmware not the drives */
668963e4975SAlan Cox 	ap->ctl |= ATA_NIEN;
669963e4975SAlan Cox 	iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
670963e4975SAlan Cox 	ata_wait_idle(ap);
671963e4975SAlan Cox 	iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
672963e4975SAlan Cox 	iowrite8(cmd, ap->ioaddr.command_addr);
673963e4975SAlan Cox 	udelay(1);
674963e4975SAlan Cox 	/* This should be almost immediate but a little paranoia goes a long
675963e4975SAlan Cox 	   way. */
676963e4975SAlan Cox 	while(n++ < 10) {
677963e4975SAlan Cox 		status = ioread8(ap->ioaddr.status_addr);
678963e4975SAlan Cox 		if (status & ATA_ERR) {
679963e4975SAlan Cox 			kfree(buf);
680963e4975SAlan Cox 			printk(KERN_ERR "it821x_firmware_command: rejected\n");
681963e4975SAlan Cox 			return NULL;
682963e4975SAlan Cox 		}
683963e4975SAlan Cox 		if (status & ATA_DRQ) {
684963e4975SAlan Cox 			ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
685963e4975SAlan Cox 			return (u8 *)buf;
686963e4975SAlan Cox 		}
687963e4975SAlan Cox 		mdelay(1);
688963e4975SAlan Cox 	}
689963e4975SAlan Cox 	kfree(buf);
690963e4975SAlan Cox 	printk(KERN_ERR "it821x_firmware_command: timeout\n");
691963e4975SAlan Cox 	return NULL;
692963e4975SAlan Cox }
693963e4975SAlan Cox 
694963e4975SAlan Cox /**
695963e4975SAlan Cox  *	it821x_probe_firmware	-	firmware reporting/setup
696963e4975SAlan Cox  *	@ap: IT821x port being probed
697963e4975SAlan Cox  *
698963e4975SAlan Cox  *	Probe the firmware of the controller by issuing firmware command
699963e4975SAlan Cox  *	0xFA and analysing the returned data.
700963e4975SAlan Cox  */
701963e4975SAlan Cox 
702963e4975SAlan Cox static void it821x_probe_firmware(struct ata_port *ap)
703963e4975SAlan Cox {
704963e4975SAlan Cox 	u8 *buf;
705963e4975SAlan Cox 	int i;
706963e4975SAlan Cox 
707963e4975SAlan Cox 	/* This is a bit ugly as we can't just issue a task file to a device
708963e4975SAlan Cox 	   as this is controller magic */
709963e4975SAlan Cox 
710963e4975SAlan Cox 	buf = it821x_firmware_command(ap, 0xFA, 512);
711963e4975SAlan Cox 
712963e4975SAlan Cox 	if (buf != NULL) {
713963e4975SAlan Cox 		printk(KERN_INFO "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
714963e4975SAlan Cox 				buf[505],
715963e4975SAlan Cox 				buf[506],
716963e4975SAlan Cox 				buf[507],
717963e4975SAlan Cox 				buf[508]);
718963e4975SAlan Cox 		for (i = 0; i < 4; i++)
719963e4975SAlan Cox  			it821x_display_disk(i, buf + 128 * i);
720963e4975SAlan Cox 		kfree(buf);
721963e4975SAlan Cox 	}
722963e4975SAlan Cox }
723963e4975SAlan Cox 
724963e4975SAlan Cox 
725669a5db4SJeff Garzik 
726669a5db4SJeff Garzik /**
727669a5db4SJeff Garzik  *	it821x_port_start	-	port setup
728669a5db4SJeff Garzik  *	@ap: ATA port being set up
729669a5db4SJeff Garzik  *
730669a5db4SJeff Garzik  *	The it821x needs to maintain private data structures and also to
731669a5db4SJeff Garzik  *	use the standard PCI interface which lacks support for this
732669a5db4SJeff Garzik  *	functionality. We instead set up the private data on the port
733669a5db4SJeff Garzik  *	start hook, and tear it down on port stop
734669a5db4SJeff Garzik  */
735669a5db4SJeff Garzik 
736669a5db4SJeff Garzik static int it821x_port_start(struct ata_port *ap)
737669a5db4SJeff Garzik {
738669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
739669a5db4SJeff Garzik 	struct it821x_dev *itdev;
740669a5db4SJeff Garzik 	u8 conf;
741669a5db4SJeff Garzik 
74281ad1837SAlan Cox 	int ret = ata_sff_port_start(ap);
743669a5db4SJeff Garzik 	if (ret < 0)
744669a5db4SJeff Garzik 		return ret;
745669a5db4SJeff Garzik 
74624dc5f33STejun Heo 	itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
74724dc5f33STejun Heo 	if (itdev == NULL)
748669a5db4SJeff Garzik 		return -ENOMEM;
74924dc5f33STejun Heo 	ap->private_data = itdev;
750669a5db4SJeff Garzik 
751669a5db4SJeff Garzik 	pci_read_config_byte(pdev, 0x50, &conf);
752669a5db4SJeff Garzik 
753669a5db4SJeff Garzik 	if (conf & 1) {
754669a5db4SJeff Garzik 		itdev->smart = 1;
755669a5db4SJeff Garzik 		/* Long I/O's although allowed in LBA48 space cause the
756669a5db4SJeff Garzik 		   onboard firmware to enter the twighlight zone */
757669a5db4SJeff Garzik 		/* No ATAPI DMA in this mode either */
758963e4975SAlan Cox 		if (ap->port_no == 0)
759963e4975SAlan Cox 			it821x_probe_firmware(ap);
760669a5db4SJeff Garzik 	}
761669a5db4SJeff Garzik 	/* Pull the current clocks from 0x50 */
762669a5db4SJeff Garzik 	if (conf & (1 << (1 + ap->port_no)))
763669a5db4SJeff Garzik 		itdev->clock_mode = ATA_50;
764669a5db4SJeff Garzik 	else
765669a5db4SJeff Garzik 		itdev->clock_mode = ATA_66;
766669a5db4SJeff Garzik 
767669a5db4SJeff Garzik 	itdev->want[0][1] = ATA_ANY;
768669a5db4SJeff Garzik 	itdev->want[1][1] = ATA_ANY;
769669a5db4SJeff Garzik 	itdev->last_device = -1;
770669a5db4SJeff Garzik 
771604de6e0SAlan Cox 	if (pdev->revision == 0x10) {
772669a5db4SJeff Garzik 		itdev->timing10 = 1;
773669a5db4SJeff Garzik 		/* Need to disable ATAPI DMA for this case */
774669a5db4SJeff Garzik 		if (!itdev->smart)
775669a5db4SJeff Garzik 			printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
776669a5db4SJeff Garzik 	}
777669a5db4SJeff Garzik 
778669a5db4SJeff Garzik 	return 0;
779669a5db4SJeff Garzik }
780669a5db4SJeff Garzik 
781963e4975SAlan Cox /**
782963e4975SAlan Cox  *	it821x_rdc_cable	-	Cable detect for RDC1010
783963e4975SAlan Cox  *	@ap: port we are checking
784963e4975SAlan Cox  *
785963e4975SAlan Cox  *	Return the RDC1010 cable type. Unlike the IT821x we know how to do
786963e4975SAlan Cox  *	this and can do host side cable detect
787963e4975SAlan Cox  */
788963e4975SAlan Cox 
789963e4975SAlan Cox static int it821x_rdc_cable(struct ata_port *ap)
790963e4975SAlan Cox {
791963e4975SAlan Cox 	u16 r40;
792963e4975SAlan Cox 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
793963e4975SAlan Cox 
794963e4975SAlan Cox 	pci_read_config_word(pdev, 0x40, &r40);
795963e4975SAlan Cox 	if (r40 & (1 << (2 + ap->port_no)))
796963e4975SAlan Cox 		return ATA_CBL_PATA40;
797963e4975SAlan Cox 	return ATA_CBL_PATA80;
798963e4975SAlan Cox }
799963e4975SAlan Cox 
800669a5db4SJeff Garzik static struct scsi_host_template it821x_sht = {
80168d1d07bSTejun Heo 	ATA_BMDMA_SHT(DRV_NAME),
802669a5db4SJeff Garzik };
803669a5db4SJeff Garzik 
804669a5db4SJeff Garzik static struct ata_port_operations it821x_smart_port_ops = {
805029cfd6bSTejun Heo 	.inherits	= &ata_bmdma_port_ops,
806669a5db4SJeff Garzik 
807669a5db4SJeff Garzik 	.check_atapi_dma= it821x_check_atapi_dma,
8089363c382STejun Heo 	.qc_issue	= it821x_smart_qc_issue,
809bda30288SJeff Garzik 
810963e4975SAlan Cox 	.cable_detect	= ata_cable_80wire,
811029cfd6bSTejun Heo 	.set_mode	= it821x_smart_set_mode,
812029cfd6bSTejun Heo 	.dev_config	= it821x_dev_config,
813963e4975SAlan Cox 	.read_id	= it821x_read_id,
814669a5db4SJeff Garzik 
815669a5db4SJeff Garzik 	.port_start	= it821x_port_start,
816669a5db4SJeff Garzik };
817669a5db4SJeff Garzik 
818669a5db4SJeff Garzik static struct ata_port_operations it821x_passthru_port_ops = {
819029cfd6bSTejun Heo 	.inherits	= &ata_bmdma_port_ops,
820669a5db4SJeff Garzik 
821669a5db4SJeff Garzik 	.check_atapi_dma= it821x_check_atapi_dma,
8225682ed33STejun Heo 	.sff_dev_select	= it821x_passthru_dev_select,
823669a5db4SJeff Garzik 	.bmdma_start 	= it821x_passthru_bmdma_start,
824669a5db4SJeff Garzik 	.bmdma_stop	= it821x_passthru_bmdma_stop,
8259363c382STejun Heo 	.qc_issue	= it821x_passthru_qc_issue,
826bda30288SJeff Garzik 
827029cfd6bSTejun Heo 	.cable_detect	= ata_cable_unknown,
828029cfd6bSTejun Heo 	.set_piomode	= it821x_passthru_set_piomode,
829029cfd6bSTejun Heo 	.set_dmamode	= it821x_passthru_set_dmamode,
830669a5db4SJeff Garzik 
831669a5db4SJeff Garzik 	.port_start	= it821x_port_start,
832669a5db4SJeff Garzik };
833669a5db4SJeff Garzik 
834963e4975SAlan Cox static struct ata_port_operations it821x_rdc_port_ops = {
835963e4975SAlan Cox 	.inherits	= &ata_bmdma_port_ops,
836963e4975SAlan Cox 
837963e4975SAlan Cox 	.check_atapi_dma= it821x_check_atapi_dma,
838963e4975SAlan Cox 	.sff_dev_select	= it821x_passthru_dev_select,
839963e4975SAlan Cox 	.bmdma_start 	= it821x_passthru_bmdma_start,
840963e4975SAlan Cox 	.bmdma_stop	= it821x_passthru_bmdma_stop,
841963e4975SAlan Cox 	.qc_issue	= it821x_passthru_qc_issue,
842963e4975SAlan Cox 
843963e4975SAlan Cox 	.cable_detect	= it821x_rdc_cable,
844963e4975SAlan Cox 	.set_piomode	= it821x_passthru_set_piomode,
845963e4975SAlan Cox 	.set_dmamode	= it821x_passthru_set_dmamode,
846963e4975SAlan Cox 
847963e4975SAlan Cox 	.port_start	= it821x_port_start,
848963e4975SAlan Cox };
849963e4975SAlan Cox 
850112cc2b5SRandy Dunlap static void it821x_disable_raid(struct pci_dev *pdev)
851669a5db4SJeff Garzik {
852963e4975SAlan Cox 	/* Neither the RDC nor the IT8211 */
853963e4975SAlan Cox 	if (pdev->vendor != PCI_VENDOR_ID_ITE ||
854963e4975SAlan Cox 			pdev->device != PCI_DEVICE_ID_ITE_8212)
855963e4975SAlan Cox 			return;
856963e4975SAlan Cox 
857669a5db4SJeff Garzik 	/* Reset local CPU, and set BIOS not ready */
858669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x5E, 0x01);
859669a5db4SJeff Garzik 
860669a5db4SJeff Garzik 	/* Set to bypass mode, and reset PCI bus */
861669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x50, 0x00);
862669a5db4SJeff Garzik 	pci_write_config_word(pdev, PCI_COMMAND,
863669a5db4SJeff Garzik 			      PCI_COMMAND_PARITY | PCI_COMMAND_IO |
864669a5db4SJeff Garzik 			      PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
865669a5db4SJeff Garzik 	pci_write_config_word(pdev, 0x40, 0xA0F3);
866669a5db4SJeff Garzik 
867669a5db4SJeff Garzik 	pci_write_config_dword(pdev,0x4C, 0x02040204);
868669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x42, 0x36);
869669a5db4SJeff Garzik 	pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
870669a5db4SJeff Garzik }
871669a5db4SJeff Garzik 
872669a5db4SJeff Garzik 
873669a5db4SJeff Garzik static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
874669a5db4SJeff Garzik {
875669a5db4SJeff Garzik 	u8 conf;
876669a5db4SJeff Garzik 
8771626aeb8STejun Heo 	static const struct ata_port_info info_smart = {
8781d2808fdSJeff Garzik 		.flags = ATA_FLAG_SLAVE_POSS,
87914bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
88014bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
881963e4975SAlan Cox 		.udma_mask = ATA_UDMA6,
882669a5db4SJeff Garzik 		.port_ops = &it821x_smart_port_ops
883669a5db4SJeff Garzik 	};
8841626aeb8STejun Heo 	static const struct ata_port_info info_passthru = {
8851d2808fdSJeff Garzik 		.flags = ATA_FLAG_SLAVE_POSS,
88614bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
88714bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
888bf6263a8SJeff Garzik 		.udma_mask = ATA_UDMA6,
889669a5db4SJeff Garzik 		.port_ops = &it821x_passthru_port_ops
890669a5db4SJeff Garzik 	};
891963e4975SAlan Cox 	static const struct ata_port_info info_rdc = {
892963e4975SAlan Cox 		.flags = ATA_FLAG_SLAVE_POSS,
89314bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
89414bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
8954a99d95fSAlan Cox 		.udma_mask = ATA_UDMA6,
8964a99d95fSAlan Cox 		.port_ops = &it821x_rdc_port_ops
8974a99d95fSAlan Cox 	};
8984a99d95fSAlan Cox 	static const struct ata_port_info info_rdc_11 = {
8994a99d95fSAlan Cox 		.flags = ATA_FLAG_SLAVE_POSS,
90014bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
90114bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
902963e4975SAlan Cox 		/* No UDMA */
903963e4975SAlan Cox 		.port_ops = &it821x_rdc_port_ops
904963e4975SAlan Cox 	};
905669a5db4SJeff Garzik 
9061626aeb8STejun Heo 	const struct ata_port_info *ppi[] = { NULL, NULL };
907669a5db4SJeff Garzik 	static char *mode[2] = { "pass through", "smart" };
908f08048e9STejun Heo 	int rc;
909f08048e9STejun Heo 
910f08048e9STejun Heo 	rc = pcim_enable_device(pdev);
911f08048e9STejun Heo 	if (rc)
912f08048e9STejun Heo 		return rc;
913669a5db4SJeff Garzik 
914963e4975SAlan Cox 	if (pdev->vendor == PCI_VENDOR_ID_RDC) {
9154a99d95fSAlan Cox 		/* Deal with Vortex86SX */
9164a99d95fSAlan Cox 		if (pdev->revision == 0x11)
9174a99d95fSAlan Cox 			ppi[0] = &info_rdc_11;
9184a99d95fSAlan Cox 		else
919963e4975SAlan Cox 			ppi[0] = &info_rdc;
920963e4975SAlan Cox 	} else {
921669a5db4SJeff Garzik 		/* Force the card into bypass mode if so requested */
922669a5db4SJeff Garzik 		if (it8212_noraid) {
923669a5db4SJeff Garzik 			printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
924669a5db4SJeff Garzik 			it821x_disable_raid(pdev);
925669a5db4SJeff Garzik 		}
926669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
927669a5db4SJeff Garzik 		conf &= 1;
928669a5db4SJeff Garzik 
929963e4975SAlan Cox 		printk(KERN_INFO DRV_NAME": controller in %s mode.\n",
930963e4975SAlan Cox 								mode[conf]);
931669a5db4SJeff Garzik 		if (conf == 0)
9321626aeb8STejun Heo 			ppi[0] = &info_passthru;
933669a5db4SJeff Garzik 		else
9341626aeb8STejun Heo 			ppi[0] = &info_smart;
935963e4975SAlan Cox 	}
93616ea0fc9SAlan Cox 	return ata_pci_sff_init_one(pdev, ppi, &it821x_sht, NULL, 0);
937669a5db4SJeff Garzik }
938669a5db4SJeff Garzik 
939438ac6d5STejun Heo #ifdef CONFIG_PM
940f535d53fSAlan static int it821x_reinit_one(struct pci_dev *pdev)
941f535d53fSAlan {
942f08048e9STejun Heo 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
943f08048e9STejun Heo 	int rc;
944f08048e9STejun Heo 
945f08048e9STejun Heo 	rc = ata_pci_device_do_resume(pdev);
946f08048e9STejun Heo 	if (rc)
947f08048e9STejun Heo 		return rc;
948f535d53fSAlan 	/* Resume - turn raid back off if need be */
949f535d53fSAlan 	if (it8212_noraid)
950f535d53fSAlan 		it821x_disable_raid(pdev);
951f08048e9STejun Heo 	ata_host_resume(host);
952f08048e9STejun Heo 	return rc;
953f535d53fSAlan }
954438ac6d5STejun Heo #endif
955f535d53fSAlan 
9562d2744fcSJeff Garzik static const struct pci_device_id it821x[] = {
9572d2744fcSJeff Garzik 	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
9582d2744fcSJeff Garzik 	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
9594192be64SOtavio Salvador 	{ PCI_VDEVICE(RDC, PCI_DEVICE_ID_RDC_D1010), },
9602d2744fcSJeff Garzik 
9612d2744fcSJeff Garzik 	{ },
962669a5db4SJeff Garzik };
963669a5db4SJeff Garzik 
964669a5db4SJeff Garzik static struct pci_driver it821x_pci_driver = {
965669a5db4SJeff Garzik 	.name 		= DRV_NAME,
966669a5db4SJeff Garzik 	.id_table	= it821x,
967669a5db4SJeff Garzik 	.probe 		= it821x_init_one,
968f535d53fSAlan 	.remove		= ata_pci_remove_one,
969438ac6d5STejun Heo #ifdef CONFIG_PM
970f535d53fSAlan 	.suspend	= ata_pci_device_suspend,
971f535d53fSAlan 	.resume		= it821x_reinit_one,
972438ac6d5STejun Heo #endif
973669a5db4SJeff Garzik };
974669a5db4SJeff Garzik 
975669a5db4SJeff Garzik static int __init it821x_init(void)
976669a5db4SJeff Garzik {
977669a5db4SJeff Garzik 	return pci_register_driver(&it821x_pci_driver);
978669a5db4SJeff Garzik }
979669a5db4SJeff Garzik 
980669a5db4SJeff Garzik static void __exit it821x_exit(void)
981669a5db4SJeff Garzik {
982669a5db4SJeff Garzik 	pci_unregister_driver(&it821x_pci_driver);
983669a5db4SJeff Garzik }
984669a5db4SJeff Garzik 
985669a5db4SJeff Garzik MODULE_AUTHOR("Alan Cox");
986669a5db4SJeff Garzik MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
987669a5db4SJeff Garzik MODULE_LICENSE("GPL");
988669a5db4SJeff Garzik MODULE_DEVICE_TABLE(pci, it821x);
989669a5db4SJeff Garzik MODULE_VERSION(DRV_VERSION);
990669a5db4SJeff Garzik 
991669a5db4SJeff Garzik 
992669a5db4SJeff Garzik module_param_named(noraid, it8212_noraid, int, S_IRUGO);
9935fe675e2SStas Sergeev MODULE_PARM_DESC(noraid, "Force card into bypass mode");
994669a5db4SJeff Garzik 
995669a5db4SJeff Garzik module_init(it821x_init);
996669a5db4SJeff Garzik module_exit(it821x_exit);
997