xref: /openbmc/linux/drivers/ata/pata_it821x.c (revision 963e4975c6f93c148ca809d986d412201df9af89)
1669a5db4SJeff Garzik /*
2c343a839SAlan Cox  * pata_it821x.c 	- IT821x PATA for new ATA layer
3669a5db4SJeff Garzik  *			  (C) 2005 Red Hat Inc
4669a5db4SJeff Garzik  *			  Alan Cox <alan@redhat.com>
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  *
13669a5db4SJeff Garzik  * Copyright (C) 2004		Red Hat <alan@redhat.com>
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>
78669a5db4SJeff Garzik #include <scsi/scsi_host.h>
79669a5db4SJeff Garzik #include <linux/libata.h>
80669a5db4SJeff Garzik 
81669a5db4SJeff Garzik 
82669a5db4SJeff Garzik #define DRV_NAME "pata_it821x"
83*963e4975SAlan Cox #define DRV_VERSION "0.4.0"
84669a5db4SJeff Garzik 
85669a5db4SJeff Garzik struct it821x_dev
86669a5db4SJeff Garzik {
87669a5db4SJeff Garzik 	unsigned int smart:1,		/* Are we in smart raid mode */
88669a5db4SJeff Garzik 		timing10:1;		/* Rev 0x10 */
89669a5db4SJeff Garzik 	u8	clock_mode;		/* 0, ATA_50 or ATA_66 */
90669a5db4SJeff Garzik 	u8	want[2][2];		/* Mode/Pri log for master slave */
91669a5db4SJeff Garzik 	/* We need these for switching the clock when DMA goes on/off
92669a5db4SJeff Garzik 	   The high byte is the 66Mhz timing */
93669a5db4SJeff Garzik 	u16	pio[2];			/* Cached PIO values */
94669a5db4SJeff Garzik 	u16	mwdma[2];		/* Cached MWDMA values */
95669a5db4SJeff Garzik 	u16	udma[2];		/* Cached UDMA values (per drive) */
96669a5db4SJeff Garzik 	u16	last_device;		/* Master or slave loaded ? */
97669a5db4SJeff Garzik };
98669a5db4SJeff Garzik 
99669a5db4SJeff Garzik #define ATA_66		0
100669a5db4SJeff Garzik #define ATA_50		1
101669a5db4SJeff Garzik #define ATA_ANY		2
102669a5db4SJeff Garzik 
103669a5db4SJeff Garzik #define UDMA_OFF	0
104669a5db4SJeff Garzik #define MWDMA_OFF	0
105669a5db4SJeff Garzik 
106669a5db4SJeff Garzik /*
107669a5db4SJeff Garzik  *	We allow users to force the card into non raid mode without
1083a4fa0a2SRobert P. J. Day  *	flashing the alternative BIOS. This is also necessary right now
109669a5db4SJeff Garzik  *	for embedded platforms that cannot run a PC BIOS but are using this
110669a5db4SJeff Garzik  *	device.
111669a5db4SJeff Garzik  */
112669a5db4SJeff Garzik 
113669a5db4SJeff Garzik static int it8212_noraid;
114669a5db4SJeff Garzik 
115669a5db4SJeff Garzik /**
116669a5db4SJeff Garzik  *	it821x_program	-	program the PIO/MWDMA registers
117669a5db4SJeff Garzik  *	@ap: ATA port
118669a5db4SJeff Garzik  *	@adev: Device to program
119669a5db4SJeff Garzik  *	@timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
120669a5db4SJeff Garzik  *
121669a5db4SJeff Garzik  *	Program the PIO/MWDMA timing for this channel according to the
122669a5db4SJeff Garzik  *	current clock. These share the same register so are managed by
123669a5db4SJeff Garzik  *	the DMA start/stop sequence as with the old driver.
124669a5db4SJeff Garzik  */
125669a5db4SJeff Garzik 
126669a5db4SJeff Garzik static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
127669a5db4SJeff Garzik {
128669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
129669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
130669a5db4SJeff Garzik 	int channel = ap->port_no;
131669a5db4SJeff Garzik 	u8 conf;
132669a5db4SJeff Garzik 
133669a5db4SJeff Garzik 	/* Program PIO/MWDMA timing bits */
134669a5db4SJeff Garzik 	if (itdev->clock_mode == ATA_66)
135669a5db4SJeff Garzik 		conf = timing >> 8;
136669a5db4SJeff Garzik 	else
137669a5db4SJeff Garzik 		conf = timing & 0xFF;
138669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
139669a5db4SJeff Garzik }
140669a5db4SJeff Garzik 
141669a5db4SJeff Garzik 
142669a5db4SJeff Garzik /**
143669a5db4SJeff Garzik  *	it821x_program_udma	-	program the UDMA registers
144669a5db4SJeff Garzik  *	@ap: ATA port
145669a5db4SJeff Garzik  *	@adev: ATA device to update
146669a5db4SJeff Garzik  *	@timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
147669a5db4SJeff Garzik  *
148669a5db4SJeff Garzik  *	Program the UDMA timing for this drive according to the
149669a5db4SJeff Garzik  *	current clock. Handles the dual clocks and also knows about
150669a5db4SJeff Garzik  *	the errata on the 0x10 revision. The UDMA errata is partly handled
151669a5db4SJeff Garzik  *	here and partly in start_dma.
152669a5db4SJeff Garzik  */
153669a5db4SJeff Garzik 
154669a5db4SJeff Garzik static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
155669a5db4SJeff Garzik {
156669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
157669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
158669a5db4SJeff Garzik 	int channel = ap->port_no;
159669a5db4SJeff Garzik 	int unit = adev->devno;
160669a5db4SJeff Garzik 	u8 conf;
161669a5db4SJeff Garzik 
162669a5db4SJeff Garzik 	/* Program UDMA timing bits */
163669a5db4SJeff Garzik 	if (itdev->clock_mode == ATA_66)
164669a5db4SJeff Garzik 		conf = timing >> 8;
165669a5db4SJeff Garzik 	else
166669a5db4SJeff Garzik 		conf = timing & 0xFF;
167669a5db4SJeff Garzik 	if (itdev->timing10 == 0)
168669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
169669a5db4SJeff Garzik 	else {
170669a5db4SJeff Garzik 		/* Early revision must be programmed for both together */
171669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
172669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
173669a5db4SJeff Garzik 	}
174669a5db4SJeff Garzik }
175669a5db4SJeff Garzik 
176669a5db4SJeff Garzik /**
177669a5db4SJeff Garzik  *	it821x_clock_strategy
178669a5db4SJeff Garzik  *	@ap: ATA interface
179669a5db4SJeff Garzik  *	@adev: ATA device being updated
180669a5db4SJeff Garzik  *
181669a5db4SJeff Garzik  *	Select between the 50 and 66Mhz base clocks to get the best
182669a5db4SJeff Garzik  *	results for this interface.
183669a5db4SJeff Garzik  */
184669a5db4SJeff Garzik 
185669a5db4SJeff Garzik static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
186669a5db4SJeff Garzik {
187669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
188669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
189669a5db4SJeff Garzik 	u8 unit = adev->devno;
190669a5db4SJeff Garzik 	struct ata_device *pair = ata_dev_pair(adev);
191669a5db4SJeff Garzik 
192669a5db4SJeff Garzik 	int clock, altclock;
193669a5db4SJeff Garzik 	u8 v;
194669a5db4SJeff Garzik 	int sel = 0;
195669a5db4SJeff Garzik 
196669a5db4SJeff Garzik 	/* Look for the most wanted clocking */
197669a5db4SJeff Garzik 	if (itdev->want[0][0] > itdev->want[1][0]) {
198669a5db4SJeff Garzik 		clock = itdev->want[0][1];
199669a5db4SJeff Garzik 		altclock = itdev->want[1][1];
200669a5db4SJeff Garzik 	} else {
201669a5db4SJeff Garzik 		clock = itdev->want[1][1];
202669a5db4SJeff Garzik 		altclock = itdev->want[0][1];
203669a5db4SJeff Garzik 	}
204669a5db4SJeff Garzik 
205669a5db4SJeff Garzik 	/* Master doesn't care does the slave ? */
206669a5db4SJeff Garzik 	if (clock == ATA_ANY)
207669a5db4SJeff Garzik 		clock = altclock;
208669a5db4SJeff Garzik 
209669a5db4SJeff Garzik 	/* Nobody cares - keep the same clock */
210669a5db4SJeff Garzik 	if (clock == ATA_ANY)
211669a5db4SJeff Garzik 		return;
212669a5db4SJeff Garzik 	/* No change */
213669a5db4SJeff Garzik 	if (clock == itdev->clock_mode)
214669a5db4SJeff Garzik 		return;
215669a5db4SJeff Garzik 
216669a5db4SJeff Garzik 	/* Load this into the controller */
217669a5db4SJeff Garzik 	if (clock == ATA_66)
218669a5db4SJeff Garzik 		itdev->clock_mode = ATA_66;
219669a5db4SJeff Garzik 	else {
220669a5db4SJeff Garzik 		itdev->clock_mode = ATA_50;
221669a5db4SJeff Garzik 		sel = 1;
222669a5db4SJeff Garzik 	}
223669a5db4SJeff Garzik 	pci_read_config_byte(pdev, 0x50, &v);
224669a5db4SJeff Garzik 	v &= ~(1 << (1 + ap->port_no));
225669a5db4SJeff Garzik 	v |= sel << (1 + ap->port_no);
226669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x50, v);
227669a5db4SJeff Garzik 
228669a5db4SJeff Garzik 	/*
229669a5db4SJeff Garzik 	 *	Reprogram the UDMA/PIO of the pair drive for the switch
230669a5db4SJeff Garzik 	 *	MWDMA will be dealt with by the dma switcher
231669a5db4SJeff Garzik 	 */
232669a5db4SJeff Garzik 	if (pair && itdev->udma[1-unit] != UDMA_OFF) {
233669a5db4SJeff Garzik 		it821x_program_udma(ap, pair, itdev->udma[1-unit]);
234669a5db4SJeff Garzik 		it821x_program(ap, pair, itdev->pio[1-unit]);
235669a5db4SJeff Garzik 	}
236669a5db4SJeff Garzik 	/*
237669a5db4SJeff Garzik 	 *	Reprogram the UDMA/PIO of our drive for the switch.
238669a5db4SJeff Garzik 	 *	MWDMA will be dealt with by the dma switcher
239669a5db4SJeff Garzik 	 */
240669a5db4SJeff Garzik 	if (itdev->udma[unit] != UDMA_OFF) {
241669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
242669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[unit]);
243669a5db4SJeff Garzik 	}
244669a5db4SJeff Garzik }
245669a5db4SJeff Garzik 
246669a5db4SJeff Garzik /**
247669a5db4SJeff Garzik  *	it821x_passthru_set_piomode	-	set PIO mode data
248669a5db4SJeff Garzik  *	@ap: ATA interface
249669a5db4SJeff Garzik  *	@adev: ATA device
250669a5db4SJeff Garzik  *
251669a5db4SJeff Garzik  *	Configure for PIO mode. This is complicated as the register is
252669a5db4SJeff Garzik  *	shared by PIO and MWDMA and for both channels.
253669a5db4SJeff Garzik  */
254669a5db4SJeff Garzik 
255669a5db4SJeff Garzik static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
256669a5db4SJeff Garzik {
257669a5db4SJeff Garzik 	/* Spec says 89 ref driver uses 88 */
258669a5db4SJeff Garzik 	static const u16 pio[]	= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
259669a5db4SJeff Garzik 	static const u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
260669a5db4SJeff Garzik 
261669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
262669a5db4SJeff Garzik 	int unit = adev->devno;
263669a5db4SJeff Garzik 	int mode_wanted = adev->pio_mode - XFER_PIO_0;
264669a5db4SJeff Garzik 
265669a5db4SJeff Garzik 	/* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
266669a5db4SJeff Garzik 	itdev->want[unit][1] = pio_want[mode_wanted];
267669a5db4SJeff Garzik 	itdev->want[unit][0] = 1;	/* PIO is lowest priority */
268669a5db4SJeff Garzik 	itdev->pio[unit] = pio[mode_wanted];
269669a5db4SJeff Garzik 	it821x_clock_strategy(ap, adev);
270669a5db4SJeff Garzik 	it821x_program(ap, adev, itdev->pio[unit]);
271669a5db4SJeff Garzik }
272669a5db4SJeff Garzik 
273669a5db4SJeff Garzik /**
274669a5db4SJeff Garzik  *	it821x_passthru_set_dmamode	-	set initial DMA mode data
275669a5db4SJeff Garzik  *	@ap: ATA interface
276669a5db4SJeff Garzik  *	@adev: ATA device
277669a5db4SJeff Garzik  *
278669a5db4SJeff Garzik  *	Set up the DMA modes. The actions taken depend heavily on the mode
279669a5db4SJeff Garzik  *	to use. If UDMA is used as is hopefully the usual case then the
280669a5db4SJeff Garzik  *	timing register is private and we need only consider the clock. If
281669a5db4SJeff Garzik  *	we are using MWDMA then we have to manage the setting ourself as
282669a5db4SJeff Garzik  *	we switch devices and mode.
283669a5db4SJeff Garzik  */
284669a5db4SJeff Garzik 
285669a5db4SJeff Garzik static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
286669a5db4SJeff Garzik {
287669a5db4SJeff Garzik 	static const u16 dma[]	= 	{ 0x8866, 0x3222, 0x3121 };
288669a5db4SJeff Garzik 	static const u8 mwdma_want[] =  { ATA_ANY, ATA_66, ATA_ANY };
289669a5db4SJeff Garzik 	static const u16 udma[]	= 	{ 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
290669a5db4SJeff Garzik 	static const u8 udma_want[] =   { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
291669a5db4SJeff Garzik 
292669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
293669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
294669a5db4SJeff Garzik 	int channel = ap->port_no;
295669a5db4SJeff Garzik 	int unit = adev->devno;
296669a5db4SJeff Garzik 	u8 conf;
297669a5db4SJeff Garzik 
298669a5db4SJeff Garzik 	if (adev->dma_mode >= XFER_UDMA_0) {
299669a5db4SJeff Garzik 		int mode_wanted = adev->dma_mode - XFER_UDMA_0;
300669a5db4SJeff Garzik 
301669a5db4SJeff Garzik 		itdev->want[unit][1] = udma_want[mode_wanted];
302669a5db4SJeff Garzik 		itdev->want[unit][0] = 3;	/* UDMA is high priority */
303669a5db4SJeff Garzik 		itdev->mwdma[unit] = MWDMA_OFF;
304669a5db4SJeff Garzik 		itdev->udma[unit] = udma[mode_wanted];
305669a5db4SJeff Garzik 		if (mode_wanted >= 5)
306669a5db4SJeff Garzik 			itdev->udma[unit] |= 0x8080;	/* UDMA 5/6 select on */
307669a5db4SJeff Garzik 
308669a5db4SJeff Garzik 		/* UDMA on. Again revision 0x10 must do the pair */
309669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
310669a5db4SJeff Garzik 		if (itdev->timing10)
311669a5db4SJeff Garzik 			conf &= channel ? 0x9F: 0xE7;
312669a5db4SJeff Garzik 		else
313669a5db4SJeff Garzik 			conf &= ~ (1 << (3 + 2 * channel + unit));
314669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x50, conf);
315669a5db4SJeff Garzik 		it821x_clock_strategy(ap, adev);
316669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
317669a5db4SJeff Garzik 	} else {
318669a5db4SJeff Garzik 		int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
319669a5db4SJeff Garzik 
320669a5db4SJeff Garzik 		itdev->want[unit][1] = mwdma_want[mode_wanted];
321669a5db4SJeff Garzik 		itdev->want[unit][0] = 2;	/* MWDMA is low priority */
322669a5db4SJeff Garzik 		itdev->mwdma[unit] = dma[mode_wanted];
323669a5db4SJeff Garzik 		itdev->udma[unit] = UDMA_OFF;
324669a5db4SJeff Garzik 
325669a5db4SJeff Garzik 		/* UDMA bits off - Revision 0x10 do them in pairs */
326669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
327669a5db4SJeff Garzik 		if (itdev->timing10)
328669a5db4SJeff Garzik 			conf |= channel ? 0x60: 0x18;
329669a5db4SJeff Garzik 		else
330669a5db4SJeff Garzik 			conf |= 1 << (3 + 2 * channel + unit);
331669a5db4SJeff Garzik 		pci_write_config_byte(pdev, 0x50, conf);
332669a5db4SJeff Garzik 		it821x_clock_strategy(ap, adev);
333669a5db4SJeff Garzik 	}
334669a5db4SJeff Garzik }
335669a5db4SJeff Garzik 
336669a5db4SJeff Garzik /**
337669a5db4SJeff Garzik  *	it821x_passthru_dma_start	-	DMA start callback
338669a5db4SJeff Garzik  *	@qc: Command in progress
339669a5db4SJeff Garzik  *
340669a5db4SJeff Garzik  *	Usually drivers set the DMA timing at the point the set_dmamode call
341669a5db4SJeff Garzik  *	is made. IT821x however requires we load new timings on the
342669a5db4SJeff Garzik  *	transitions in some cases.
343669a5db4SJeff Garzik  */
344669a5db4SJeff Garzik 
345669a5db4SJeff Garzik static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
346669a5db4SJeff Garzik {
347669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
348669a5db4SJeff Garzik 	struct ata_device *adev = qc->dev;
349669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
350669a5db4SJeff Garzik 	int unit = adev->devno;
351669a5db4SJeff Garzik 
352669a5db4SJeff Garzik 	if (itdev->mwdma[unit] != MWDMA_OFF)
353669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->mwdma[unit]);
354669a5db4SJeff Garzik 	else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
355669a5db4SJeff Garzik 		it821x_program_udma(ap, adev, itdev->udma[unit]);
356669a5db4SJeff Garzik 	ata_bmdma_start(qc);
357669a5db4SJeff Garzik }
358669a5db4SJeff Garzik 
359669a5db4SJeff Garzik /**
360669a5db4SJeff Garzik  *	it821x_passthru_dma_stop	-	DMA stop callback
361669a5db4SJeff Garzik  *	@qc: ATA command
362669a5db4SJeff Garzik  *
363669a5db4SJeff Garzik  *	We loaded new timings in dma_start, as a result we need to restore
364669a5db4SJeff Garzik  *	the PIO timings in dma_stop so that the next command issue gets the
365669a5db4SJeff Garzik  *	right clock values.
366669a5db4SJeff Garzik  */
367669a5db4SJeff Garzik 
368669a5db4SJeff Garzik static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
369669a5db4SJeff Garzik {
370669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
371669a5db4SJeff Garzik 	struct ata_device *adev = qc->dev;
372669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
373669a5db4SJeff Garzik 	int unit = adev->devno;
374669a5db4SJeff Garzik 
375669a5db4SJeff Garzik 	ata_bmdma_stop(qc);
376669a5db4SJeff Garzik 	if (itdev->mwdma[unit] != MWDMA_OFF)
377669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[unit]);
378669a5db4SJeff Garzik }
379669a5db4SJeff Garzik 
380669a5db4SJeff Garzik 
381669a5db4SJeff Garzik /**
382669a5db4SJeff Garzik  *	it821x_passthru_dev_select	-	Select master/slave
383669a5db4SJeff Garzik  *	@ap: ATA port
384669a5db4SJeff Garzik  *	@device: Device number (not pointer)
385669a5db4SJeff Garzik  *
3863a4fa0a2SRobert P. J. Day  *	Device selection hook. If necessary perform clock switching
387669a5db4SJeff Garzik  */
388669a5db4SJeff Garzik 
389669a5db4SJeff Garzik static void it821x_passthru_dev_select(struct ata_port *ap,
390669a5db4SJeff Garzik 				       unsigned int device)
391669a5db4SJeff Garzik {
392669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
393669a5db4SJeff Garzik 	if (itdev && device != itdev->last_device) {
3949af5c9c9STejun Heo 		struct ata_device *adev = &ap->link.device[device];
395669a5db4SJeff Garzik 		it821x_program(ap, adev, itdev->pio[adev->devno]);
396669a5db4SJeff Garzik 		itdev->last_device = device;
397669a5db4SJeff Garzik 	}
3989363c382STejun Heo 	ata_sff_dev_select(ap, device);
399669a5db4SJeff Garzik }
400669a5db4SJeff Garzik 
401669a5db4SJeff Garzik /**
4029363c382STejun Heo  *	it821x_smart_qc_issue		-	wrap qc issue prot
403669a5db4SJeff Garzik  *	@qc: command
404669a5db4SJeff Garzik  *
405669a5db4SJeff Garzik  *	Wrap the command issue sequence for the IT821x. We need to
406669a5db4SJeff Garzik  *	perform out own device selection timing loads before the
407669a5db4SJeff Garzik  *	usual happenings kick off
408669a5db4SJeff Garzik  */
409669a5db4SJeff Garzik 
4109363c382STejun Heo static unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc)
411669a5db4SJeff Garzik {
412669a5db4SJeff Garzik 	switch(qc->tf.command)
413669a5db4SJeff Garzik 	{
414669a5db4SJeff Garzik 		/* Commands the firmware supports */
415669a5db4SJeff Garzik 		case ATA_CMD_READ:
416669a5db4SJeff Garzik 		case ATA_CMD_READ_EXT:
417669a5db4SJeff Garzik 		case ATA_CMD_WRITE:
418669a5db4SJeff Garzik 		case ATA_CMD_WRITE_EXT:
419669a5db4SJeff Garzik 		case ATA_CMD_PIO_READ:
420669a5db4SJeff Garzik 		case ATA_CMD_PIO_READ_EXT:
421669a5db4SJeff Garzik 		case ATA_CMD_PIO_WRITE:
422669a5db4SJeff Garzik 		case ATA_CMD_PIO_WRITE_EXT:
423669a5db4SJeff Garzik 		case ATA_CMD_READ_MULTI:
424669a5db4SJeff Garzik 		case ATA_CMD_READ_MULTI_EXT:
425669a5db4SJeff Garzik 		case ATA_CMD_WRITE_MULTI:
426669a5db4SJeff Garzik 		case ATA_CMD_WRITE_MULTI_EXT:
427669a5db4SJeff Garzik 		case ATA_CMD_ID_ATA:
428*963e4975SAlan Cox 		case ATA_CMD_INIT_DEV_PARAMS:
429*963e4975SAlan Cox 		case 0xFC:	/* Internal 'report rebuild state' */
430669a5db4SJeff Garzik 		/* Arguably should just no-op this one */
431669a5db4SJeff Garzik 		case ATA_CMD_SET_FEATURES:
4329363c382STejun Heo 			return ata_sff_qc_issue(qc);
433669a5db4SJeff Garzik 	}
434669a5db4SJeff Garzik 	printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command);
435c5038fc0SAlan Cox 	return AC_ERR_DEV;
436669a5db4SJeff Garzik }
437669a5db4SJeff Garzik 
438669a5db4SJeff Garzik /**
4399363c382STejun Heo  *	it821x_passthru_qc_issue	-	wrap qc issue prot
440669a5db4SJeff Garzik  *	@qc: command
441669a5db4SJeff Garzik  *
442669a5db4SJeff Garzik  *	Wrap the command issue sequence for the IT821x. We need to
443669a5db4SJeff Garzik  *	perform out own device selection timing loads before the
444669a5db4SJeff Garzik  *	usual happenings kick off
445669a5db4SJeff Garzik  */
446669a5db4SJeff Garzik 
4479363c382STejun Heo static unsigned int it821x_passthru_qc_issue(struct ata_queued_cmd *qc)
448669a5db4SJeff Garzik {
449669a5db4SJeff Garzik 	it821x_passthru_dev_select(qc->ap, qc->dev->devno);
4509363c382STejun Heo 	return ata_sff_qc_issue(qc);
451669a5db4SJeff Garzik }
452669a5db4SJeff Garzik 
453669a5db4SJeff Garzik /**
454669a5db4SJeff Garzik  *	it821x_smart_set_mode	-	mode setting
4550260731fSTejun Heo  *	@link: interface to set up
456b229a7b0SAlan  *	@unused: device that failed (error only)
457669a5db4SJeff Garzik  *
458669a5db4SJeff Garzik  *	Use a non standard set_mode function. We don't want to be tuned.
459669a5db4SJeff Garzik  *	The BIOS configured everything. Our job is not to fiddle. We
460669a5db4SJeff Garzik  *	read the dma enabled bits from the PCI configuration of the device
461669a5db4SJeff Garzik  *	and respect them.
462669a5db4SJeff Garzik  */
463669a5db4SJeff Garzik 
4640260731fSTejun Heo static int it821x_smart_set_mode(struct ata_link *link, struct ata_device **unused)
465669a5db4SJeff Garzik {
466f58229f8STejun Heo 	struct ata_device *dev;
467669a5db4SJeff Garzik 
4680260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
469669a5db4SJeff Garzik 		if (ata_dev_enabled(dev)) {
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 		}
487669a5db4SJeff Garzik 	}
488b229a7b0SAlan 	return 0;
489669a5db4SJeff Garzik }
490669a5db4SJeff Garzik 
491669a5db4SJeff Garzik /**
492669a5db4SJeff Garzik  *	it821x_dev_config	-	Called each device identify
493669a5db4SJeff Garzik  *	@adev: Device that has just been identified
494669a5db4SJeff Garzik  *
495669a5db4SJeff Garzik  *	Perform the initial setup needed for each device that is chip
496669a5db4SJeff Garzik  *	special. In our case we need to lock the sector count to avoid
497669a5db4SJeff Garzik  *	blowing the brains out of the firmware with large LBA48 requests
498669a5db4SJeff Garzik  *
499669a5db4SJeff Garzik  *	FIXME: When FUA appears we need to block FUA too. And SMART and
500669a5db4SJeff Garzik  *	basically we need to filter commands for this chip.
501669a5db4SJeff Garzik  */
502669a5db4SJeff Garzik 
503cd0d3bbcSAlan static void it821x_dev_config(struct ata_device *adev)
504669a5db4SJeff Garzik {
5058bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
506669a5db4SJeff Garzik 
5078bfa79fcSTejun Heo 	ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
508669a5db4SJeff Garzik 
509669a5db4SJeff Garzik 	if (adev->max_sectors > 255)
510669a5db4SJeff Garzik 		adev->max_sectors = 255;
511669a5db4SJeff Garzik 
512669a5db4SJeff Garzik 	if (strstr(model_num, "Integrated Technology Express")) {
513669a5db4SJeff Garzik 		/* RAID mode */
514*963e4975SAlan Cox 		ata_dev_printk(adev, KERN_INFO, "%sRAID%d volume",
515669a5db4SJeff Garzik 			adev->id[147]?"Bootable ":"",
516669a5db4SJeff Garzik 			adev->id[129]);
517669a5db4SJeff Garzik 		if (adev->id[129] != 1)
518669a5db4SJeff Garzik 			printk("(%dK stripe)", adev->id[146]);
519669a5db4SJeff Garzik 		printk(".\n");
520669a5db4SJeff Garzik 	}
521c5038fc0SAlan Cox 	/* This is a controller firmware triggered funny, don't
522c5038fc0SAlan Cox 	   report the drive faulty! */
523c5038fc0SAlan Cox 	adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
524*963e4975SAlan Cox 	/* No HPA in 'smart' mode */
525*963e4975SAlan Cox 	adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
526c5038fc0SAlan Cox }
527c5038fc0SAlan Cox 
528c5038fc0SAlan Cox /**
529*963e4975SAlan Cox  *	it821x_read_id	-	Hack identify data up
530*963e4975SAlan Cox  *	@adev: device to read
531*963e4975SAlan Cox  *	@tf: proposed taskfile
532*963e4975SAlan Cox  *	@id: buffer for returned ident data
533c5038fc0SAlan Cox  *
534*963e4975SAlan Cox  *	Query the devices on this firmware driven port and slightly
535c5038fc0SAlan Cox  *	mash the identify data to stop us and common tools trying to
536c5038fc0SAlan Cox  *	use features not firmware supported. The firmware itself does
537c5038fc0SAlan Cox  *	some masking (eg SMART) but not enough.
538c5038fc0SAlan Cox  */
539c5038fc0SAlan Cox 
540*963e4975SAlan Cox static unsigned int it821x_read_id(struct ata_device *adev,
541*963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
542c5038fc0SAlan Cox {
543*963e4975SAlan Cox 	unsigned int err_mask;
544*963e4975SAlan Cox 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
545669a5db4SJeff Garzik 
546*963e4975SAlan Cox 	err_mask = ata_do_dev_read_id(adev, tf, id);
547*963e4975SAlan Cox 	if (err_mask)
548*963e4975SAlan Cox 		return err_mask;
549*963e4975SAlan Cox 	ata_id_c_string(id, model_num, ATA_ID_PROD, sizeof(model_num));
550*963e4975SAlan Cox 
551*963e4975SAlan Cox 	id[83] &= ~(1 << 12);	/* Cache flush is firmware handled */
552*963e4975SAlan Cox 	id[83] &= ~(1 << 13);	/* Ditto for LBA48 flushes */
553*963e4975SAlan Cox 	id[84] &= ~(1 << 6);	/* No FUA */
554*963e4975SAlan Cox 	id[85] &= ~(1 << 10);	/* No HPA */
555*963e4975SAlan Cox 	id[76] = 0;		/* No NCQ/AN etc */
556*963e4975SAlan Cox 
557*963e4975SAlan Cox 	if (strstr(model_num, "Integrated Technology Express")) {
558*963e4975SAlan Cox 		/* Set feature bits the firmware neglects */
559*963e4975SAlan Cox 		id[49] |= 0x0300;	/* LBA, DMA */
560*963e4975SAlan Cox 		id[82] |= 0x0400;	/* LBA48 */
561*963e4975SAlan Cox 		id[83] &= 0x7FFF;
562*963e4975SAlan Cox 		id[83] |= 0x4000;	/* Word 83 is valid */
563*963e4975SAlan Cox 		id[86] |= 0x0400;	/* LBA48 on */
564*963e4975SAlan Cox 		id[ATA_ID_MAJOR_VER] |= 0x1F;
565*963e4975SAlan Cox 	}
566*963e4975SAlan Cox 	return err_mask;
567*963e4975SAlan Cox }
568669a5db4SJeff Garzik 
569669a5db4SJeff Garzik /**
570669a5db4SJeff Garzik  *	it821x_check_atapi_dma	-	ATAPI DMA handler
571669a5db4SJeff Garzik  *	@qc: Command we are about to issue
572669a5db4SJeff Garzik  *
573669a5db4SJeff Garzik  *	Decide if this ATAPI command can be issued by DMA on this
574669a5db4SJeff Garzik  *	controller. Return 0 if it can be.
575669a5db4SJeff Garzik  */
576669a5db4SJeff Garzik 
577669a5db4SJeff Garzik static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
578669a5db4SJeff Garzik {
579669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
580669a5db4SJeff Garzik 	struct it821x_dev *itdev = ap->private_data;
581669a5db4SJeff Garzik 
582bce7d5e0SJeff Norden 	/* Only use dma for transfers to/from the media. */
583b63b1331STejun Heo 	if (ata_qc_raw_nbytes(qc) < 2048)
584bce7d5e0SJeff Norden 		return -EOPNOTSUPP;
585bce7d5e0SJeff Norden 
586669a5db4SJeff Garzik 	/* No ATAPI DMA in smart mode */
587669a5db4SJeff Garzik 	if (itdev->smart)
588669a5db4SJeff Garzik 		return -EOPNOTSUPP;
589669a5db4SJeff Garzik 	/* No ATAPI DMA on rev 10 */
590669a5db4SJeff Garzik 	if (itdev->timing10)
591669a5db4SJeff Garzik 		return -EOPNOTSUPP;
592669a5db4SJeff Garzik 	/* Cool */
593669a5db4SJeff Garzik 	return 0;
594669a5db4SJeff Garzik }
595669a5db4SJeff Garzik 
596*963e4975SAlan Cox /**
597*963e4975SAlan Cox  *	it821x_display_disk	-	display disk setup
598*963e4975SAlan Cox  *	@n: Device number
599*963e4975SAlan Cox  *	@buf: Buffer block from firmware
600*963e4975SAlan Cox  *
601*963e4975SAlan Cox  *	Produce a nice informative display of the device setup as provided
602*963e4975SAlan Cox  *	by the firmware.
603*963e4975SAlan Cox  */
604*963e4975SAlan Cox 
605*963e4975SAlan Cox static void it821x_display_disk(int n, u8 *buf)
606*963e4975SAlan Cox {
607*963e4975SAlan Cox 	unsigned char id[41];
608*963e4975SAlan Cox 	int mode = 0;
609*963e4975SAlan Cox 	char *mtype;
610*963e4975SAlan Cox 	char mbuf[8];
611*963e4975SAlan Cox 	char *cbl = "(40 wire cable)";
612*963e4975SAlan Cox 
613*963e4975SAlan Cox 	static const char *types[5] = {
614*963e4975SAlan Cox 		"RAID0", "RAID1" "RAID 0+1", "JBOD", "DISK"
615*963e4975SAlan Cox 	};
616*963e4975SAlan Cox 
617*963e4975SAlan Cox 	if (buf[52] > 4)	/* No Disk */
618*963e4975SAlan Cox 		return;
619*963e4975SAlan Cox 
620*963e4975SAlan Cox 	ata_id_c_string((u16 *)buf, id, 0, 41);
621*963e4975SAlan Cox 
622*963e4975SAlan Cox 	if (buf[51]) {
623*963e4975SAlan Cox 		mode = ffs(buf[51]);
624*963e4975SAlan Cox 		mtype = "UDMA";
625*963e4975SAlan Cox 	} else if (buf[49]) {
626*963e4975SAlan Cox 		mode = ffs(buf[49]);
627*963e4975SAlan Cox 		mtype = "MWDMA";
628*963e4975SAlan Cox 	}
629*963e4975SAlan Cox 
630*963e4975SAlan Cox 	if (buf[76])
631*963e4975SAlan Cox 		cbl = "";
632*963e4975SAlan Cox 
633*963e4975SAlan Cox 	if (mode)
634*963e4975SAlan Cox 		snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
635*963e4975SAlan Cox 	else
636*963e4975SAlan Cox 		strcpy(mbuf, "PIO");
637*963e4975SAlan Cox 	if (buf[52] == 4)
638*963e4975SAlan Cox 		printk(KERN_INFO "%d: %-6s %-8s          %s %s\n",
639*963e4975SAlan Cox 				n, mbuf, types[buf[52]], id, cbl);
640*963e4975SAlan Cox 	else
641*963e4975SAlan Cox 		printk(KERN_INFO "%d: %-6s %-8s Volume: %1d %s %s\n",
642*963e4975SAlan Cox 				n, mbuf, types[buf[52]], buf[53], id, cbl);
643*963e4975SAlan Cox 	if (buf[125] < 100)
644*963e4975SAlan Cox 		printk(KERN_INFO "%d: Rebuilding: %d%%\n", n, buf[125]);
645*963e4975SAlan Cox }
646*963e4975SAlan Cox 
647*963e4975SAlan Cox /**
648*963e4975SAlan Cox  *	it821x_firmware_command		-	issue firmware command
649*963e4975SAlan Cox  *	@ap: IT821x port to interrogate
650*963e4975SAlan Cox  *	@cmd: command
651*963e4975SAlan Cox  *	@len: length
652*963e4975SAlan Cox  *
653*963e4975SAlan Cox  *	Issue firmware commands expecting data back from the controller. We
654*963e4975SAlan Cox  *	use this to issue commands that do not go via the normal paths. Other
655*963e4975SAlan Cox  *	commands such as 0xFC can be issued normally.
656*963e4975SAlan Cox  */
657*963e4975SAlan Cox 
658*963e4975SAlan Cox static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
659*963e4975SAlan Cox {
660*963e4975SAlan Cox 	u8 status;
661*963e4975SAlan Cox 	int n = 0;
662*963e4975SAlan Cox 	u16 *buf = kmalloc(len, GFP_KERNEL);
663*963e4975SAlan Cox 	if (buf == NULL) {
664*963e4975SAlan Cox 		printk(KERN_ERR "it821x_firmware_command: Out of memory\n");
665*963e4975SAlan Cox 		return NULL;
666*963e4975SAlan Cox 	}
667*963e4975SAlan Cox 	/* This isn't quite a normal ATA command as we are talking to the
668*963e4975SAlan Cox 	   firmware not the drives */
669*963e4975SAlan Cox 	ap->ctl |= ATA_NIEN;
670*963e4975SAlan Cox 	iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
671*963e4975SAlan Cox 	ata_wait_idle(ap);
672*963e4975SAlan Cox 	iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
673*963e4975SAlan Cox 	iowrite8(cmd, ap->ioaddr.command_addr);
674*963e4975SAlan Cox 	udelay(1);
675*963e4975SAlan Cox 	/* This should be almost immediate but a little paranoia goes a long
676*963e4975SAlan Cox 	   way. */
677*963e4975SAlan Cox 	while(n++ < 10) {
678*963e4975SAlan Cox 		status = ioread8(ap->ioaddr.status_addr);
679*963e4975SAlan Cox 		if (status & ATA_ERR) {
680*963e4975SAlan Cox 			kfree(buf);
681*963e4975SAlan Cox 			printk(KERN_ERR "it821x_firmware_command: rejected\n");
682*963e4975SAlan Cox 			return NULL;
683*963e4975SAlan Cox 		}
684*963e4975SAlan Cox 		if (status & ATA_DRQ) {
685*963e4975SAlan Cox 			ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
686*963e4975SAlan Cox 			return (u8 *)buf;
687*963e4975SAlan Cox 		}
688*963e4975SAlan Cox 		mdelay(1);
689*963e4975SAlan Cox 	}
690*963e4975SAlan Cox 	kfree(buf);
691*963e4975SAlan Cox 	printk(KERN_ERR "it821x_firmware_command: timeout\n");
692*963e4975SAlan Cox 	return NULL;
693*963e4975SAlan Cox }
694*963e4975SAlan Cox 
695*963e4975SAlan Cox /**
696*963e4975SAlan Cox  *	it821x_probe_firmware	-	firmware reporting/setup
697*963e4975SAlan Cox  *	@ap: IT821x port being probed
698*963e4975SAlan Cox  *
699*963e4975SAlan Cox  *	Probe the firmware of the controller by issuing firmware command
700*963e4975SAlan Cox  *	0xFA and analysing the returned data.
701*963e4975SAlan Cox  */
702*963e4975SAlan Cox 
703*963e4975SAlan Cox static void it821x_probe_firmware(struct ata_port *ap)
704*963e4975SAlan Cox {
705*963e4975SAlan Cox 	u8 *buf;
706*963e4975SAlan Cox 	int i;
707*963e4975SAlan Cox 
708*963e4975SAlan Cox 	/* This is a bit ugly as we can't just issue a task file to a device
709*963e4975SAlan Cox 	   as this is controller magic */
710*963e4975SAlan Cox 
711*963e4975SAlan Cox 	buf = it821x_firmware_command(ap, 0xFA, 512);
712*963e4975SAlan Cox 
713*963e4975SAlan Cox 	if (buf != NULL) {
714*963e4975SAlan Cox 		printk(KERN_INFO "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
715*963e4975SAlan Cox 				buf[505],
716*963e4975SAlan Cox 				buf[506],
717*963e4975SAlan Cox 				buf[507],
718*963e4975SAlan Cox 				buf[508]);
719*963e4975SAlan Cox 		for (i = 0; i < 4; i++)
720*963e4975SAlan Cox  			it821x_display_disk(i, buf + 128 * i);
721*963e4975SAlan Cox 		kfree(buf);
722*963e4975SAlan Cox 	}
723*963e4975SAlan Cox }
724*963e4975SAlan Cox 
725*963e4975SAlan Cox 
726669a5db4SJeff Garzik 
727669a5db4SJeff Garzik /**
728669a5db4SJeff Garzik  *	it821x_port_start	-	port setup
729669a5db4SJeff Garzik  *	@ap: ATA port being set up
730669a5db4SJeff Garzik  *
731669a5db4SJeff Garzik  *	The it821x needs to maintain private data structures and also to
732669a5db4SJeff Garzik  *	use the standard PCI interface which lacks support for this
733669a5db4SJeff Garzik  *	functionality. We instead set up the private data on the port
734669a5db4SJeff Garzik  *	start hook, and tear it down on port stop
735669a5db4SJeff Garzik  */
736669a5db4SJeff Garzik 
737669a5db4SJeff Garzik static int it821x_port_start(struct ata_port *ap)
738669a5db4SJeff Garzik {
739669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
740669a5db4SJeff Garzik 	struct it821x_dev *itdev;
741669a5db4SJeff Garzik 	u8 conf;
742669a5db4SJeff Garzik 
74381ad1837SAlan Cox 	int ret = ata_sff_port_start(ap);
744669a5db4SJeff Garzik 	if (ret < 0)
745669a5db4SJeff Garzik 		return ret;
746669a5db4SJeff Garzik 
74724dc5f33STejun Heo 	itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
74824dc5f33STejun Heo 	if (itdev == NULL)
749669a5db4SJeff Garzik 		return -ENOMEM;
75024dc5f33STejun Heo 	ap->private_data = itdev;
751669a5db4SJeff Garzik 
752669a5db4SJeff Garzik 	pci_read_config_byte(pdev, 0x50, &conf);
753669a5db4SJeff Garzik 
754669a5db4SJeff Garzik 	if (conf & 1) {
755669a5db4SJeff Garzik 		itdev->smart = 1;
756669a5db4SJeff Garzik 		/* Long I/O's although allowed in LBA48 space cause the
757669a5db4SJeff Garzik 		   onboard firmware to enter the twighlight zone */
758669a5db4SJeff Garzik 		/* No ATAPI DMA in this mode either */
759*963e4975SAlan Cox 		if (ap->port_no == 0)
760*963e4975SAlan Cox 			it821x_probe_firmware(ap);
761669a5db4SJeff Garzik 	}
762669a5db4SJeff Garzik 	/* Pull the current clocks from 0x50 */
763669a5db4SJeff Garzik 	if (conf & (1 << (1 + ap->port_no)))
764669a5db4SJeff Garzik 		itdev->clock_mode = ATA_50;
765669a5db4SJeff Garzik 	else
766669a5db4SJeff Garzik 		itdev->clock_mode = ATA_66;
767669a5db4SJeff Garzik 
768669a5db4SJeff Garzik 	itdev->want[0][1] = ATA_ANY;
769669a5db4SJeff Garzik 	itdev->want[1][1] = ATA_ANY;
770669a5db4SJeff Garzik 	itdev->last_device = -1;
771669a5db4SJeff Garzik 
772604de6e0SAlan Cox 	if (pdev->revision == 0x10) {
773669a5db4SJeff Garzik 		itdev->timing10 = 1;
774669a5db4SJeff Garzik 		/* Need to disable ATAPI DMA for this case */
775669a5db4SJeff Garzik 		if (!itdev->smart)
776669a5db4SJeff Garzik 			printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
777669a5db4SJeff Garzik 	}
778669a5db4SJeff Garzik 
779669a5db4SJeff Garzik 	return 0;
780669a5db4SJeff Garzik }
781669a5db4SJeff Garzik 
782*963e4975SAlan Cox /**
783*963e4975SAlan Cox  *	it821x_rdc_cable	-	Cable detect for RDC1010
784*963e4975SAlan Cox  *	@ap: port we are checking
785*963e4975SAlan Cox  *
786*963e4975SAlan Cox  *	Return the RDC1010 cable type. Unlike the IT821x we know how to do
787*963e4975SAlan Cox  *	this and can do host side cable detect
788*963e4975SAlan Cox  */
789*963e4975SAlan Cox 
790*963e4975SAlan Cox static int it821x_rdc_cable(struct ata_port *ap)
791*963e4975SAlan Cox {
792*963e4975SAlan Cox 	u16 r40;
793*963e4975SAlan Cox 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
794*963e4975SAlan Cox 
795*963e4975SAlan Cox 	pci_read_config_word(pdev, 0x40, &r40);
796*963e4975SAlan Cox 	if (r40 & (1 << (2 + ap->port_no)))
797*963e4975SAlan Cox 		return ATA_CBL_PATA40;
798*963e4975SAlan Cox 	return ATA_CBL_PATA80;
799*963e4975SAlan Cox }
800*963e4975SAlan Cox 
801669a5db4SJeff Garzik static struct scsi_host_template it821x_sht = {
80268d1d07bSTejun Heo 	ATA_BMDMA_SHT(DRV_NAME),
803669a5db4SJeff Garzik };
804669a5db4SJeff Garzik 
805669a5db4SJeff Garzik static struct ata_port_operations it821x_smart_port_ops = {
806029cfd6bSTejun Heo 	.inherits	= &ata_bmdma_port_ops,
807669a5db4SJeff Garzik 
808669a5db4SJeff Garzik 	.check_atapi_dma= it821x_check_atapi_dma,
8099363c382STejun Heo 	.qc_issue	= it821x_smart_qc_issue,
810bda30288SJeff Garzik 
811*963e4975SAlan Cox 	.cable_detect	= ata_cable_80wire,
812029cfd6bSTejun Heo 	.set_mode	= it821x_smart_set_mode,
813029cfd6bSTejun Heo 	.dev_config	= it821x_dev_config,
814*963e4975SAlan Cox 	.read_id	= it821x_read_id,
815669a5db4SJeff Garzik 
816669a5db4SJeff Garzik 	.port_start	= it821x_port_start,
817669a5db4SJeff Garzik };
818669a5db4SJeff Garzik 
819669a5db4SJeff Garzik static struct ata_port_operations it821x_passthru_port_ops = {
820029cfd6bSTejun Heo 	.inherits	= &ata_bmdma_port_ops,
821669a5db4SJeff Garzik 
822669a5db4SJeff Garzik 	.check_atapi_dma= it821x_check_atapi_dma,
8235682ed33STejun Heo 	.sff_dev_select	= it821x_passthru_dev_select,
824669a5db4SJeff Garzik 	.bmdma_start 	= it821x_passthru_bmdma_start,
825669a5db4SJeff Garzik 	.bmdma_stop	= it821x_passthru_bmdma_stop,
8269363c382STejun Heo 	.qc_issue	= it821x_passthru_qc_issue,
827bda30288SJeff Garzik 
828029cfd6bSTejun Heo 	.cable_detect	= ata_cable_unknown,
829029cfd6bSTejun Heo 	.set_piomode	= it821x_passthru_set_piomode,
830029cfd6bSTejun Heo 	.set_dmamode	= it821x_passthru_set_dmamode,
831669a5db4SJeff Garzik 
832669a5db4SJeff Garzik 	.port_start	= it821x_port_start,
833669a5db4SJeff Garzik };
834669a5db4SJeff Garzik 
835*963e4975SAlan Cox static struct ata_port_operations it821x_rdc_port_ops = {
836*963e4975SAlan Cox 	.inherits	= &ata_bmdma_port_ops,
837*963e4975SAlan Cox 
838*963e4975SAlan Cox 	.check_atapi_dma= it821x_check_atapi_dma,
839*963e4975SAlan Cox 	.sff_dev_select	= it821x_passthru_dev_select,
840*963e4975SAlan Cox 	.bmdma_start 	= it821x_passthru_bmdma_start,
841*963e4975SAlan Cox 	.bmdma_stop	= it821x_passthru_bmdma_stop,
842*963e4975SAlan Cox 	.qc_issue	= it821x_passthru_qc_issue,
843*963e4975SAlan Cox 
844*963e4975SAlan Cox 	.cable_detect	= it821x_rdc_cable,
845*963e4975SAlan Cox 	.set_piomode	= it821x_passthru_set_piomode,
846*963e4975SAlan Cox 	.set_dmamode	= it821x_passthru_set_dmamode,
847*963e4975SAlan Cox 
848*963e4975SAlan Cox 	.port_start	= it821x_port_start,
849*963e4975SAlan Cox };
850*963e4975SAlan Cox 
851112cc2b5SRandy Dunlap static void it821x_disable_raid(struct pci_dev *pdev)
852669a5db4SJeff Garzik {
853*963e4975SAlan Cox 	/* Neither the RDC nor the IT8211 */
854*963e4975SAlan Cox 	if (pdev->vendor != PCI_VENDOR_ID_ITE ||
855*963e4975SAlan Cox 			pdev->device != PCI_DEVICE_ID_ITE_8212)
856*963e4975SAlan Cox 			return;
857*963e4975SAlan Cox 
858669a5db4SJeff Garzik 	/* Reset local CPU, and set BIOS not ready */
859669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x5E, 0x01);
860669a5db4SJeff Garzik 
861669a5db4SJeff Garzik 	/* Set to bypass mode, and reset PCI bus */
862669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x50, 0x00);
863669a5db4SJeff Garzik 	pci_write_config_word(pdev, PCI_COMMAND,
864669a5db4SJeff Garzik 			      PCI_COMMAND_PARITY | PCI_COMMAND_IO |
865669a5db4SJeff Garzik 			      PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
866669a5db4SJeff Garzik 	pci_write_config_word(pdev, 0x40, 0xA0F3);
867669a5db4SJeff Garzik 
868669a5db4SJeff Garzik 	pci_write_config_dword(pdev,0x4C, 0x02040204);
869669a5db4SJeff Garzik 	pci_write_config_byte(pdev, 0x42, 0x36);
870669a5db4SJeff Garzik 	pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
871669a5db4SJeff Garzik }
872669a5db4SJeff Garzik 
873669a5db4SJeff Garzik 
874669a5db4SJeff Garzik static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
875669a5db4SJeff Garzik {
876669a5db4SJeff Garzik 	u8 conf;
877669a5db4SJeff Garzik 
8781626aeb8STejun Heo 	static const struct ata_port_info info_smart = {
8791d2808fdSJeff Garzik 		.flags = ATA_FLAG_SLAVE_POSS,
880669a5db4SJeff Garzik 		.pio_mask = 0x1f,
881669a5db4SJeff Garzik 		.mwdma_mask = 0x07,
882*963e4975SAlan Cox 		.udma_mask = ATA_UDMA6,
883669a5db4SJeff Garzik 		.port_ops = &it821x_smart_port_ops
884669a5db4SJeff Garzik 	};
8851626aeb8STejun Heo 	static const struct ata_port_info info_passthru = {
8861d2808fdSJeff Garzik 		.flags = ATA_FLAG_SLAVE_POSS,
887669a5db4SJeff Garzik 		.pio_mask = 0x1f,
888669a5db4SJeff Garzik 		.mwdma_mask = 0x07,
889bf6263a8SJeff Garzik 		.udma_mask = ATA_UDMA6,
890669a5db4SJeff Garzik 		.port_ops = &it821x_passthru_port_ops
891669a5db4SJeff Garzik 	};
892*963e4975SAlan Cox 	static const struct ata_port_info info_rdc = {
893*963e4975SAlan Cox 		.flags = ATA_FLAG_SLAVE_POSS,
894*963e4975SAlan Cox 		.pio_mask = 0x1f,
895*963e4975SAlan Cox 		.mwdma_mask = 0x07,
896*963e4975SAlan Cox 		/* No UDMA */
897*963e4975SAlan Cox 		.port_ops = &it821x_rdc_port_ops
898*963e4975SAlan Cox 	};
899669a5db4SJeff Garzik 
9001626aeb8STejun Heo 	const struct ata_port_info *ppi[] = { NULL, NULL };
901669a5db4SJeff Garzik 	static char *mode[2] = { "pass through", "smart" };
902f08048e9STejun Heo 	int rc;
903f08048e9STejun Heo 
904f08048e9STejun Heo 	rc = pcim_enable_device(pdev);
905f08048e9STejun Heo 	if (rc)
906f08048e9STejun Heo 		return rc;
907669a5db4SJeff Garzik 
908*963e4975SAlan Cox 	if (pdev->vendor == PCI_VENDOR_ID_RDC) {
909*963e4975SAlan Cox 		ppi[0] = &info_rdc;
910*963e4975SAlan Cox 	} else {
911669a5db4SJeff Garzik 		/* Force the card into bypass mode if so requested */
912669a5db4SJeff Garzik 		if (it8212_noraid) {
913669a5db4SJeff Garzik 			printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
914669a5db4SJeff Garzik 			it821x_disable_raid(pdev);
915669a5db4SJeff Garzik 		}
916669a5db4SJeff Garzik 		pci_read_config_byte(pdev, 0x50, &conf);
917669a5db4SJeff Garzik 		conf &= 1;
918669a5db4SJeff Garzik 
919*963e4975SAlan Cox 		printk(KERN_INFO DRV_NAME": controller in %s mode.\n",
920*963e4975SAlan Cox 								mode[conf]);
921669a5db4SJeff Garzik 		if (conf == 0)
9221626aeb8STejun Heo 			ppi[0] = &info_passthru;
923669a5db4SJeff Garzik 		else
9241626aeb8STejun Heo 			ppi[0] = &info_smart;
925*963e4975SAlan Cox 	}
9269363c382STejun Heo 	return ata_pci_sff_init_one(pdev, ppi, &it821x_sht, NULL);
927669a5db4SJeff Garzik }
928669a5db4SJeff Garzik 
929438ac6d5STejun Heo #ifdef CONFIG_PM
930f535d53fSAlan static int it821x_reinit_one(struct pci_dev *pdev)
931f535d53fSAlan {
932f08048e9STejun Heo 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
933f08048e9STejun Heo 	int rc;
934f08048e9STejun Heo 
935f08048e9STejun Heo 	rc = ata_pci_device_do_resume(pdev);
936f08048e9STejun Heo 	if (rc)
937f08048e9STejun Heo 		return rc;
938f535d53fSAlan 	/* Resume - turn raid back off if need be */
939f535d53fSAlan 	if (it8212_noraid)
940f535d53fSAlan 		it821x_disable_raid(pdev);
941f08048e9STejun Heo 	ata_host_resume(host);
942f08048e9STejun Heo 	return rc;
943f535d53fSAlan }
944438ac6d5STejun Heo #endif
945f535d53fSAlan 
9462d2744fcSJeff Garzik static const struct pci_device_id it821x[] = {
9472d2744fcSJeff Garzik 	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
9482d2744fcSJeff Garzik 	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
949*963e4975SAlan Cox 	{ PCI_VDEVICE(RDC, 0x1010), },
9502d2744fcSJeff Garzik 
9512d2744fcSJeff Garzik 	{ },
952669a5db4SJeff Garzik };
953669a5db4SJeff Garzik 
954669a5db4SJeff Garzik static struct pci_driver it821x_pci_driver = {
955669a5db4SJeff Garzik 	.name 		= DRV_NAME,
956669a5db4SJeff Garzik 	.id_table	= it821x,
957669a5db4SJeff Garzik 	.probe 		= it821x_init_one,
958f535d53fSAlan 	.remove		= ata_pci_remove_one,
959438ac6d5STejun Heo #ifdef CONFIG_PM
960f535d53fSAlan 	.suspend	= ata_pci_device_suspend,
961f535d53fSAlan 	.resume		= it821x_reinit_one,
962438ac6d5STejun Heo #endif
963669a5db4SJeff Garzik };
964669a5db4SJeff Garzik 
965669a5db4SJeff Garzik static int __init it821x_init(void)
966669a5db4SJeff Garzik {
967669a5db4SJeff Garzik 	return pci_register_driver(&it821x_pci_driver);
968669a5db4SJeff Garzik }
969669a5db4SJeff Garzik 
970669a5db4SJeff Garzik static void __exit it821x_exit(void)
971669a5db4SJeff Garzik {
972669a5db4SJeff Garzik 	pci_unregister_driver(&it821x_pci_driver);
973669a5db4SJeff Garzik }
974669a5db4SJeff Garzik 
975669a5db4SJeff Garzik MODULE_AUTHOR("Alan Cox");
976669a5db4SJeff Garzik MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
977669a5db4SJeff Garzik MODULE_LICENSE("GPL");
978669a5db4SJeff Garzik MODULE_DEVICE_TABLE(pci, it821x);
979669a5db4SJeff Garzik MODULE_VERSION(DRV_VERSION);
980669a5db4SJeff Garzik 
981669a5db4SJeff Garzik 
982669a5db4SJeff Garzik module_param_named(noraid, it8212_noraid, int, S_IRUGO);
9835fe675e2SStas Sergeev MODULE_PARM_DESC(noraid, "Force card into bypass mode");
984669a5db4SJeff Garzik 
985669a5db4SJeff Garzik module_init(it821x_init);
986669a5db4SJeff Garzik module_exit(it821x_exit);
987