1669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/*
2c343a8391496de83c66e771ead986aab0d6f777fAlan Cox * pata_it821x.c 	- IT821x PATA for new ATA layer
3669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *			  (C) 2005 Red Hat Inc
4ab77163008c596aad9624ceab190d840c0143fa8Alan Cox *			  Alan Cox <alan@lxorguk.ukuu.org.uk>
5374abf2cb16a9df8be96675c606996458872e8b3Bartlomiej Zolnierkiewicz *			  (C) 2007 Bartlomiej Zolnierkiewicz
6669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
7669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik * based upon
8669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
9669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik * it821x.c
1085cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik *
11669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik * linux/drivers/ide/pci/it821x.c		Version 0.09	December 2004
12669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
13ab77163008c596aad9624ceab190d840c0143fa8Alan Cox * Copyright (C) 2004		Red Hat
14669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
15669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  May be copied or modified under the terms of the GNU General Public License
16669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  Based in part on the ITE vendor provided SCSI driver.
17669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
18631dd1a885b6d7e9f6f51b4e5b311c2bb04c323cJustin P. Mattock *  Documentation available from IT8212F_V04.pdf
19631dd1a885b6d7e9f6f51b4e5b311c2bb04c323cJustin P. Mattock * 	http://www.ite.com.tw/EN/products_more.aspx?CategoryID=3&ID=5,91
20669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  Some other documents are NDA.
21669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
22669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  The ITE8212 isn't exactly a standard IDE controller. It has two
23669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  modes. In pass through mode then it is an IDE controller. In its smart
24669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  mode its actually quite a capable hardware raid controller disguised
25669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  as an IDE controller. Smart mode only understands DMA read/write and
26669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  identify, none of the fancier commands apply. The IT8211 is identical
27669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  in other respects but lacks the raid mode.
28669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
29669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  Errata:
30669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  o	Rev 0x10 also requires master/slave hold the same DMA timings and
31669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	cannot do ATAPI MWDMA.
32669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  o	The identify data for raid volumes lacks CHS info (technically ok)
33669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	but also fails to set the LBA28 and other bits. We fix these in
34669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	the IDE probe quirk code.
35669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  o	If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
36669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	raid then the controller firmware dies
37669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  o	Smart mode without RAID doesn't clear all the necessary identify
38669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	bits to reduce the command set to the one used
39669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
40669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  This has a few impacts on the driver
41669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  - In pass through mode we do all the work you would expect
42669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  - In smart mode the clocking set up is done by the controller generally
43669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *    but we must watch the other limits and filter.
44669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  - There are a few extra vendor commands that actually talk to the
45669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *    controller but only work PIO with no IRQ.
46669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
47669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  Vendor areas of the identify block in smart mode are used for the
48669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  timing and policy set up. Each HDD in raid mode also has a serial
49669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  block on the disk. The hardware extra commands are get/set chip status,
50669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  rebuild, get rebuild status.
51669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
52669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  In Linux the driver supports pass through mode as if the device was
53669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  just another IDE controller. If the smart mode is running then
54669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  volumes are managed by the controller firmware and each IDE "disk"
55669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  is a raid volume. Even more cute - the controller can do automated
56669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  hotplug and rebuild.
57669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
58669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  The pass through controller itself is a little demented. It has a
59669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  flaw that it has a single set of PIO/MWDMA timings per channel so
60669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  non UDMA devices restrict each others performance. It also has a
61669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  single clock source per channel so mixed UDMA100/133 performance
62669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  isn't perfect and we have to pick a clock. Thankfully none of this
63669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  matters in smart mode. ATAPI DMA is not currently supported.
64669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
65669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
66669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
67669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *  TODO
68669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	-	ATAPI and other speed filtering
69669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	-	RAID configuration ioctls
70669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
71669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
72669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#include <linux/kernel.h>
73669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#include <linux/module.h>
74669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#include <linux/pci.h>
75669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#include <linux/init.h>
76669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#include <linux/blkdev.h>
77669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#include <linux/delay.h>
785a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
79669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#include <scsi/scsi_host.h>
80669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#include <linux/libata.h>
81669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
82669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
83669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#define DRV_NAME "pata_it821x"
844a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox#define DRV_VERSION "0.4.2"
85669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
86669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstruct it821x_dev
87669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
88669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	unsigned int smart:1,		/* Are we in smart raid mode */
89669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		timing10:1;		/* Rev 0x10 */
90669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8	clock_mode;		/* 0, ATA_50 or ATA_66 */
91669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8	want[2][2];		/* Mode/Pri log for master slave */
92669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* We need these for switching the clock when DMA goes on/off
93669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	   The high byte is the 66Mhz timing */
94669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u16	pio[2];			/* Cached PIO values */
95669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u16	mwdma[2];		/* Cached MWDMA values */
96669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u16	udma[2];		/* Cached UDMA values (per drive) */
97669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u16	last_device;		/* Master or slave loaded ? */
98669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik};
99669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
100669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#define ATA_66		0
101669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#define ATA_50		1
102669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#define ATA_ANY		2
103669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
104669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#define UDMA_OFF	0
105669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik#define MWDMA_OFF	0
106669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
107669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/*
108669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	We allow users to force the card into non raid mode without
1093a4fa0a25da81600ea0bcd75692ae8ca6050d165Robert P. J. Day *	flashing the alternative BIOS. This is also necessary right now
110669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	for embedded platforms that cannot run a PC BIOS but are using this
111669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	device.
112669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
113669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
114669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic int it8212_noraid;
115669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
116669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
117669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_program	-	program the PIO/MWDMA registers
118669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@ap: ATA port
119669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@adev: Device to program
120669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
121669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
122669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Program the PIO/MWDMA timing for this channel according to the
123669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	current clock. These share the same register so are managed by
124669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	the DMA start/stop sequence as with the old driver.
125669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
126669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
127669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
128669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
129669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
130669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
131669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int channel = ap->port_no;
132669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8 conf;
133669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
134669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Program PIO/MWDMA timing bits */
135669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->clock_mode == ATA_66)
136669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		conf = timing >> 8;
137669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	else
138669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		conf = timing & 0xFF;
139669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
140669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
141669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
142669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
143669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
144669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_program_udma	-	program the UDMA registers
145669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@ap: ATA port
146669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@adev: ATA device to update
147669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
148669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
149669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Program the UDMA timing for this drive according to the
150669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	current clock. Handles the dual clocks and also knows about
151669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	the errata on the 0x10 revision. The UDMA errata is partly handled
152669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	here and partly in start_dma.
153669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
154669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
155669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
156669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
157669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
158669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
159669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int channel = ap->port_no;
160669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int unit = adev->devno;
161669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8 conf;
162669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
163669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Program UDMA timing bits */
164669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->clock_mode == ATA_66)
165669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		conf = timing >> 8;
166669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	else
167669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		conf = timing & 0xFF;
168669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->timing10 == 0)
169669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
170669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	else {
171669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* Early revision must be programmed for both together */
172669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
173669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
174669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
175669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
176669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
177669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
178669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_clock_strategy
179669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@ap: ATA interface
180669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@adev: ATA device being updated
181669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
182669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Select between the 50 and 66Mhz base clocks to get the best
183669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	results for this interface.
184669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
185669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
186669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
187669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
188669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
189669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
190669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8 unit = adev->devno;
191669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct ata_device *pair = ata_dev_pair(adev);
192669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
193669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int clock, altclock;
194669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8 v;
195669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int sel = 0;
196669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
197669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Look for the most wanted clocking */
198669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->want[0][0] > itdev->want[1][0]) {
199669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		clock = itdev->want[0][1];
200669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		altclock = itdev->want[1][1];
201669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	} else {
202669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		clock = itdev->want[1][1];
203669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		altclock = itdev->want[0][1];
204669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
205669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
206669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Master doesn't care does the slave ? */
207669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (clock == ATA_ANY)
208669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		clock = altclock;
209669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
210669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Nobody cares - keep the same clock */
211669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (clock == ATA_ANY)
212669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		return;
213669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* No change */
214669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (clock == itdev->clock_mode)
215669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		return;
216669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
217669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Load this into the controller */
218669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (clock == ATA_66)
219669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->clock_mode = ATA_66;
220669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	else {
221669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->clock_mode = ATA_50;
222669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		sel = 1;
223669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
224669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_read_config_byte(pdev, 0x50, &v);
225669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	v &= ~(1 << (1 + ap->port_no));
226669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	v |= sel << (1 + ap->port_no);
227669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_byte(pdev, 0x50, v);
228669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
229669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/*
230669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	 *	Reprogram the UDMA/PIO of the pair drive for the switch
231669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	 *	MWDMA will be dealt with by the dma switcher
232669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	 */
233669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (pair && itdev->udma[1-unit] != UDMA_OFF) {
234669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program_udma(ap, pair, itdev->udma[1-unit]);
235669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program(ap, pair, itdev->pio[1-unit]);
236669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
237669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/*
238669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	 *	Reprogram the UDMA/PIO of our drive for the switch.
239669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	 *	MWDMA will be dealt with by the dma switcher
240669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	 */
241669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->udma[unit] != UDMA_OFF) {
242669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program_udma(ap, adev, itdev->udma[unit]);
243669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program(ap, adev, itdev->pio[unit]);
244669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
245669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
246669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
247669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
248669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_passthru_set_piomode	-	set PIO mode data
249669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@ap: ATA interface
250669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@adev: ATA device
251669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
252669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Configure for PIO mode. This is complicated as the register is
253669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	shared by PIO and MWDMA and for both channels.
254669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
255669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
256669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
257669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
258669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Spec says 89 ref driver uses 88 */
259669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	static const u16 pio[]	= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
260669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	static const u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
261669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
262669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
263669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int unit = adev->devno;
264669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int mode_wanted = adev->pio_mode - XFER_PIO_0;
26585cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
266669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
267669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	itdev->want[unit][1] = pio_want[mode_wanted];
268669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	itdev->want[unit][0] = 1;	/* PIO is lowest priority */
269669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	itdev->pio[unit] = pio[mode_wanted];
270669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	it821x_clock_strategy(ap, adev);
271669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	it821x_program(ap, adev, itdev->pio[unit]);
272669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
273669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
274669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
275669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_passthru_set_dmamode	-	set initial DMA mode data
276669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@ap: ATA interface
277669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@adev: ATA device
278669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
279669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Set up the DMA modes. The actions taken depend heavily on the mode
28085cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik *	to use. If UDMA is used as is hopefully the usual case then the
281669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	timing register is private and we need only consider the clock. If
282669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	we are using MWDMA then we have to manage the setting ourself as
283669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	we switch devices and mode.
284669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
285669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
286669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
287669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
288669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	static const u16 dma[]	= 	{ 0x8866, 0x3222, 0x3121 };
289669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	static const u8 mwdma_want[] =  { ATA_ANY, ATA_66, ATA_ANY };
290669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	static const u16 udma[]	= 	{ 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
291669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	static const u8 udma_want[] =   { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
292669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
293669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
294669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
295669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int channel = ap->port_no;
296669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int unit = adev->devno;
297669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8 conf;
298669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
299669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (adev->dma_mode >= XFER_UDMA_0) {
300669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		int mode_wanted = adev->dma_mode - XFER_UDMA_0;
30185cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
302669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->want[unit][1] = udma_want[mode_wanted];
303669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->want[unit][0] = 3;	/* UDMA is high priority */
304669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->mwdma[unit] = MWDMA_OFF;
305669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->udma[unit] = udma[mode_wanted];
306669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		if (mode_wanted >= 5)
307669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik			itdev->udma[unit] |= 0x8080;	/* UDMA 5/6 select on */
308669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
309669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* UDMA on. Again revision 0x10 must do the pair */
310669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		pci_read_config_byte(pdev, 0x50, &conf);
311669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		if (itdev->timing10)
312669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik			conf &= channel ? 0x9F: 0xE7;
313669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		else
314669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik			conf &= ~ (1 << (3 + 2 * channel + unit));
315669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		pci_write_config_byte(pdev, 0x50, conf);
316669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_clock_strategy(ap, adev);
317669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program_udma(ap, adev, itdev->udma[unit]);
318669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	} else {
319669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
32085cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
321669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->want[unit][1] = mwdma_want[mode_wanted];
322669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->want[unit][0] = 2;	/* MWDMA is low priority */
323669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->mwdma[unit] = dma[mode_wanted];
324669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->udma[unit] = UDMA_OFF;
325669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
326669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* UDMA bits off - Revision 0x10 do them in pairs */
327669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		pci_read_config_byte(pdev, 0x50, &conf);
328669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		if (itdev->timing10)
329669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik			conf |= channel ? 0x60: 0x18;
330669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		else
331669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik			conf |= 1 << (3 + 2 * channel + unit);
332669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		pci_write_config_byte(pdev, 0x50, conf);
333669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_clock_strategy(ap, adev);
334669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
335669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
336669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
337669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
338669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_passthru_dma_start	-	DMA start callback
339669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@qc: Command in progress
340669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
341669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Usually drivers set the DMA timing at the point the set_dmamode call
34285cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik *	is made. IT821x however requires we load new timings on the
343669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	transitions in some cases.
344669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
345669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
346669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
347669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
348669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct ata_port *ap = qc->ap;
349669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct ata_device *adev = qc->dev;
350669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
351669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int unit = adev->devno;
352669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
353669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->mwdma[unit] != MWDMA_OFF)
354669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program(ap, adev, itdev->mwdma[unit]);
355669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
356669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program_udma(ap, adev, itdev->udma[unit]);
357669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	ata_bmdma_start(qc);
358669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
359669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
360669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
361669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_passthru_dma_stop	-	DMA stop callback
362669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@qc: ATA command
363669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
364669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	We loaded new timings in dma_start, as a result we need to restore
365669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	the PIO timings in dma_stop so that the next command issue gets the
366669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	right clock values.
367669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
368669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
369669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
370669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
371669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct ata_port *ap = qc->ap;
372669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct ata_device *adev = qc->dev;
373669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
374669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	int unit = adev->devno;
375669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
376669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	ata_bmdma_stop(qc);
377669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->mwdma[unit] != MWDMA_OFF)
378669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program(ap, adev, itdev->pio[unit]);
379669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
380669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
381669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
382669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
383669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_passthru_dev_select	-	Select master/slave
384669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@ap: ATA port
385669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@device: Device number (not pointer)
386669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
3873a4fa0a25da81600ea0bcd75692ae8ca6050d165Robert P. J. Day *	Device selection hook. If necessary perform clock switching
388669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
38985cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
390669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void it821x_passthru_dev_select(struct ata_port *ap,
391669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik				       unsigned int device)
392669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
393669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
394669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev && device != itdev->last_device) {
3959af5c9c97dc9d599281778864c72b385f0c63341Tejun Heo		struct ata_device *adev = &ap->link.device[device];
396669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		it821x_program(ap, adev, itdev->pio[adev->devno]);
397669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->last_device = device;
398669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
3999363c3825ea9ad76561eb48a395349dd29211ed6Tejun Heo	ata_sff_dev_select(ap, device);
400669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
401669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
402669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
4039363c3825ea9ad76561eb48a395349dd29211ed6Tejun Heo *	it821x_smart_qc_issue		-	wrap qc issue prot
404669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@qc: command
405669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
406669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Wrap the command issue sequence for the IT821x. We need to
407669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	perform out own device selection timing loads before the
408669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	usual happenings kick off
409669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
41085cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
4119363c3825ea9ad76561eb48a395349dd29211ed6Tejun Heostatic unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc)
412669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
413669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	switch(qc->tf.command)
414669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	{
415669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* Commands the firmware supports */
416669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_READ:
417669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_READ_EXT:
418669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_WRITE:
419669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_WRITE_EXT:
420669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_PIO_READ:
421669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_PIO_READ_EXT:
422669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_PIO_WRITE:
423669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_PIO_WRITE_EXT:
424669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_READ_MULTI:
425669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_READ_MULTI_EXT:
426669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_WRITE_MULTI:
427669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_WRITE_MULTI_EXT:
428669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_ID_ATA:
429963e4975c6f93c148ca809d986d412201df9af89Alan Cox		case ATA_CMD_INIT_DEV_PARAMS:
430963e4975c6f93c148ca809d986d412201df9af89Alan Cox		case 0xFC:	/* Internal 'report rebuild state' */
431669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* Arguably should just no-op this one */
432669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		case ATA_CMD_SET_FEATURES:
433360ff7833098e944e5003618b03894251e937802Tejun Heo			return ata_bmdma_qc_issue(qc);
434669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
435669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command);
436c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox	return AC_ERR_DEV;
437669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
438669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
439669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
4409363c3825ea9ad76561eb48a395349dd29211ed6Tejun Heo *	it821x_passthru_qc_issue	-	wrap qc issue prot
441669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@qc: command
442669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
443669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Wrap the command issue sequence for the IT821x. We need to
444669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	perform out own device selection timing loads before the
445669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	usual happenings kick off
446669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
44785cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
4489363c3825ea9ad76561eb48a395349dd29211ed6Tejun Heostatic unsigned int it821x_passthru_qc_issue(struct ata_queued_cmd *qc)
449669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
450669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	it821x_passthru_dev_select(qc->ap, qc->dev->devno);
451360ff7833098e944e5003618b03894251e937802Tejun Heo	return ata_bmdma_qc_issue(qc);
452669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
453669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
454669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
455669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_smart_set_mode	-	mode setting
4560260731f0187840e272bfa10d3ba0f3e417976f5Tejun Heo *	@link: interface to set up
457b229a7b0aed808f2ef6a5e9dbf78b0f17cefb4d0Alan Cox *	@unused: device that failed (error only)
458669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
459669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Use a non standard set_mode function. We don't want to be tuned.
460669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	The BIOS configured everything. Our job is not to fiddle. We
461669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	read the dma enabled bits from the PCI configuration of the device
46285cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik *	and respect them.
463669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
46485cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
4650260731f0187840e272bfa10d3ba0f3e417976f5Tejun Heostatic int it821x_smart_set_mode(struct ata_link *link, struct ata_device **unused)
466669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
467f58229f8060055b08b34008ea08f31de1e2f003cTejun Heo	struct ata_device *dev;
468669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
4691eca4365be25c540650693e941bc06a66cf38f94Tejun Heo	ata_for_each_dev(dev, link, ENABLED) {
4701eca4365be25c540650693e941bc06a66cf38f94Tejun Heo		/* We don't really care */
4711eca4365be25c540650693e941bc06a66cf38f94Tejun Heo		dev->pio_mode = XFER_PIO_0;
4721eca4365be25c540650693e941bc06a66cf38f94Tejun Heo		dev->dma_mode = XFER_MW_DMA_0;
4731eca4365be25c540650693e941bc06a66cf38f94Tejun Heo		/* We do need the right mode information for DMA or PIO
4741eca4365be25c540650693e941bc06a66cf38f94Tejun Heo		   and this comes from the current configuration flags */
4751eca4365be25c540650693e941bc06a66cf38f94Tejun Heo		if (ata_id_has_dma(dev->id)) {
476a9a79dfec239568bdbf778242f8fcd10bcc5b9e2Joe Perches			ata_dev_info(dev, "configured for DMA\n");
4771eca4365be25c540650693e941bc06a66cf38f94Tejun Heo			dev->xfer_mode = XFER_MW_DMA_0;
4781eca4365be25c540650693e941bc06a66cf38f94Tejun Heo			dev->xfer_shift = ATA_SHIFT_MWDMA;
4791eca4365be25c540650693e941bc06a66cf38f94Tejun Heo			dev->flags &= ~ATA_DFLAG_PIO;
4801eca4365be25c540650693e941bc06a66cf38f94Tejun Heo		} else {
481a9a79dfec239568bdbf778242f8fcd10bcc5b9e2Joe Perches			ata_dev_info(dev, "configured for PIO\n");
4821eca4365be25c540650693e941bc06a66cf38f94Tejun Heo			dev->xfer_mode = XFER_PIO_0;
4831eca4365be25c540650693e941bc06a66cf38f94Tejun Heo			dev->xfer_shift = ATA_SHIFT_PIO;
4841eca4365be25c540650693e941bc06a66cf38f94Tejun Heo			dev->flags |= ATA_DFLAG_PIO;
485669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		}
486669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
487b229a7b0aed808f2ef6a5e9dbf78b0f17cefb4d0Alan Cox	return 0;
488669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
489669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
490669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
491669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_dev_config	-	Called each device identify
492669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@adev: Device that has just been identified
493669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
494669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Perform the initial setup needed for each device that is chip
495669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	special. In our case we need to lock the sector count to avoid
496669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	blowing the brains out of the firmware with large LBA48 requests
497669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
498669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
49985cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
500cd0d3bbcdd650651b7ccfaf55d107e3fc237d95aAlan Coxstatic void it821x_dev_config(struct ata_device *adev)
501669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
5028bfa79fcb81d2bdb043f60ab4171704467808b55Tejun Heo	unsigned char model_num[ATA_ID_PROD_LEN + 1];
503669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
5048bfa79fcb81d2bdb043f60ab4171704467808b55Tejun Heo	ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
505669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
506669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (adev->max_sectors > 255)
507669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		adev->max_sectors = 255;
50885cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
509669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (strstr(model_num, "Integrated Technology Express")) {
510669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* RAID mode */
511a9a79dfec239568bdbf778242f8fcd10bcc5b9e2Joe Perches		ata_dev_info(adev, "%sRAID%d volume",
512a9a79dfec239568bdbf778242f8fcd10bcc5b9e2Joe Perches			     adev->id[147] ? "Bootable " : "",
513a9a79dfec239568bdbf778242f8fcd10bcc5b9e2Joe Perches			     adev->id[129]);
514669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		if (adev->id[129] != 1)
515a9a79dfec239568bdbf778242f8fcd10bcc5b9e2Joe Perches			pr_cont("(%dK stripe)", adev->id[146]);
516a9a79dfec239568bdbf778242f8fcd10bcc5b9e2Joe Perches		pr_cont("\n");
517669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
518c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox	/* This is a controller firmware triggered funny, don't
519c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox	   report the drive faulty! */
520c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox	adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
521963e4975c6f93c148ca809d986d412201df9af89Alan Cox	/* No HPA in 'smart' mode */
522963e4975c6f93c148ca809d986d412201df9af89Alan Cox	adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
523c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox}
524c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox
525c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox/**
526963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	it821x_read_id	-	Hack identify data up
527963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@adev: device to read
528963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@tf: proposed taskfile
529963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@id: buffer for returned ident data
530c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox *
531963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	Query the devices on this firmware driven port and slightly
532c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox *	mash the identify data to stop us and common tools trying to
533c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox *	use features not firmware supported. The firmware itself does
534c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox *	some masking (eg SMART) but not enough.
535c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox */
536c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox
537963e4975c6f93c148ca809d986d412201df9af89Alan Coxstatic unsigned int it821x_read_id(struct ata_device *adev,
538963e4975c6f93c148ca809d986d412201df9af89Alan Cox					struct ata_taskfile *tf, u16 *id)
539c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox{
540963e4975c6f93c148ca809d986d412201df9af89Alan Cox	unsigned int err_mask;
541963e4975c6f93c148ca809d986d412201df9af89Alan Cox	unsigned char model_num[ATA_ID_PROD_LEN + 1];
542963e4975c6f93c148ca809d986d412201df9af89Alan Cox
543963e4975c6f93c148ca809d986d412201df9af89Alan Cox	err_mask = ata_do_dev_read_id(adev, tf, id);
544963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (err_mask)
545963e4975c6f93c148ca809d986d412201df9af89Alan Cox		return err_mask;
546963e4975c6f93c148ca809d986d412201df9af89Alan Cox	ata_id_c_string(id, model_num, ATA_ID_PROD, sizeof(model_num));
547963e4975c6f93c148ca809d986d412201df9af89Alan Cox
548963e4975c6f93c148ca809d986d412201df9af89Alan Cox	id[83] &= ~(1 << 12);	/* Cache flush is firmware handled */
549963e4975c6f93c148ca809d986d412201df9af89Alan Cox	id[83] &= ~(1 << 13);	/* Ditto for LBA48 flushes */
550963e4975c6f93c148ca809d986d412201df9af89Alan Cox	id[84] &= ~(1 << 6);	/* No FUA */
551963e4975c6f93c148ca809d986d412201df9af89Alan Cox	id[85] &= ~(1 << 10);	/* No HPA */
552963e4975c6f93c148ca809d986d412201df9af89Alan Cox	id[76] = 0;		/* No NCQ/AN etc */
553963e4975c6f93c148ca809d986d412201df9af89Alan Cox
554963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (strstr(model_num, "Integrated Technology Express")) {
555963e4975c6f93c148ca809d986d412201df9af89Alan Cox		/* Set feature bits the firmware neglects */
556963e4975c6f93c148ca809d986d412201df9af89Alan Cox		id[49] |= 0x0300;	/* LBA, DMA */
557963e4975c6f93c148ca809d986d412201df9af89Alan Cox		id[83] &= 0x7FFF;
558054e5f616b5becdc096b793407dc33fe379749acOndrej Zary		id[83] |= 0x4400;	/* Word 83 is valid and LBA48 */
559963e4975c6f93c148ca809d986d412201df9af89Alan Cox		id[86] |= 0x0400;	/* LBA48 on */
560963e4975c6f93c148ca809d986d412201df9af89Alan Cox		id[ATA_ID_MAJOR_VER] |= 0x1F;
5617ba07d16bd62f931efec1fc8e63bf1aeebfe42a9Ondrej Zary		/* Clear the serial number because it's different each boot
5627ba07d16bd62f931efec1fc8e63bf1aeebfe42a9Ondrej Zary		   which breaks validation on resume */
5637ba07d16bd62f931efec1fc8e63bf1aeebfe42a9Ondrej Zary		memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN);
564c5038fc05d4aa4ae0671776199459690e4c973cbAlan Cox	}
565963e4975c6f93c148ca809d986d412201df9af89Alan Cox	return err_mask;
566669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
567669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
568669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
569669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_check_atapi_dma	-	ATAPI DMA handler
570669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@qc: Command we are about to issue
571669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
572669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	Decide if this ATAPI command can be issued by DMA on this
573669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	controller. Return 0 if it can be.
574669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
57585cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
576669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
577669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
578669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct ata_port *ap = qc->ap;
579669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev = ap->private_data;
58085cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
581bce7d5e0e1fc0c1f1251b7f21a19cb48207408b6Jeff Norden	/* Only use dma for transfers to/from the media. */
582b63b133165b876838e8685350ef469620f4abd99Tejun Heo	if (ata_qc_raw_nbytes(qc) < 2048)
583bce7d5e0e1fc0c1f1251b7f21a19cb48207408b6Jeff Norden		return -EOPNOTSUPP;
584bce7d5e0e1fc0c1f1251b7f21a19cb48207408b6Jeff Norden
585669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* No ATAPI DMA in smart mode */
586669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->smart)
587669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		return -EOPNOTSUPP;
588669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* No ATAPI DMA on rev 10 */
589669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (itdev->timing10)
590669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		return -EOPNOTSUPP;
591669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Cool */
592669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	return 0;
593669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
59485cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
595963e4975c6f93c148ca809d986d412201df9af89Alan Cox/**
596963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	it821x_display_disk	-	display disk setup
597963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@n: Device number
598963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@buf: Buffer block from firmware
599963e4975c6f93c148ca809d986d412201df9af89Alan Cox *
600963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	Produce a nice informative display of the device setup as provided
601963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	by the firmware.
602963e4975c6f93c148ca809d986d412201df9af89Alan Cox */
603963e4975c6f93c148ca809d986d412201df9af89Alan Cox
604963e4975c6f93c148ca809d986d412201df9af89Alan Coxstatic void it821x_display_disk(int n, u8 *buf)
605963e4975c6f93c148ca809d986d412201df9af89Alan Cox{
606963e4975c6f93c148ca809d986d412201df9af89Alan Cox	unsigned char id[41];
607963e4975c6f93c148ca809d986d412201df9af89Alan Cox	int mode = 0;
6084ef281853474389fea952e9e59650e599ecf2d62Jeff Garzik	char *mtype = "";
609963e4975c6f93c148ca809d986d412201df9af89Alan Cox	char mbuf[8];
610963e4975c6f93c148ca809d986d412201df9af89Alan Cox	char *cbl = "(40 wire cable)";
611963e4975c6f93c148ca809d986d412201df9af89Alan Cox
612963e4975c6f93c148ca809d986d412201df9af89Alan Cox	static const char *types[5] = {
6131c30c02757027ed2da1b0e26609ac8b9b1c2bb1fJean Delvare		"RAID0", "RAID1", "RAID 0+1", "JBOD", "DISK"
614963e4975c6f93c148ca809d986d412201df9af89Alan Cox	};
615963e4975c6f93c148ca809d986d412201df9af89Alan Cox
616963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (buf[52] > 4)	/* No Disk */
617963e4975c6f93c148ca809d986d412201df9af89Alan Cox		return;
618963e4975c6f93c148ca809d986d412201df9af89Alan Cox
6194fca377f7488095ab04035e2bfe5c59873c22382Jeff Garzik	ata_id_c_string((u16 *)buf, id, 0, 41);
620963e4975c6f93c148ca809d986d412201df9af89Alan Cox
621963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (buf[51]) {
622963e4975c6f93c148ca809d986d412201df9af89Alan Cox		mode = ffs(buf[51]);
623963e4975c6f93c148ca809d986d412201df9af89Alan Cox		mtype = "UDMA";
624963e4975c6f93c148ca809d986d412201df9af89Alan Cox	} else if (buf[49]) {
625963e4975c6f93c148ca809d986d412201df9af89Alan Cox		mode = ffs(buf[49]);
626963e4975c6f93c148ca809d986d412201df9af89Alan Cox		mtype = "MWDMA";
627963e4975c6f93c148ca809d986d412201df9af89Alan Cox	}
628963e4975c6f93c148ca809d986d412201df9af89Alan Cox
629963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (buf[76])
630963e4975c6f93c148ca809d986d412201df9af89Alan Cox		cbl = "";
631963e4975c6f93c148ca809d986d412201df9af89Alan Cox
632963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (mode)
633963e4975c6f93c148ca809d986d412201df9af89Alan Cox		snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
634963e4975c6f93c148ca809d986d412201df9af89Alan Cox	else
635963e4975c6f93c148ca809d986d412201df9af89Alan Cox		strcpy(mbuf, "PIO");
636963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (buf[52] == 4)
637963e4975c6f93c148ca809d986d412201df9af89Alan Cox		printk(KERN_INFO "%d: %-6s %-8s          %s %s\n",
638963e4975c6f93c148ca809d986d412201df9af89Alan Cox				n, mbuf, types[buf[52]], id, cbl);
639963e4975c6f93c148ca809d986d412201df9af89Alan Cox	else
640963e4975c6f93c148ca809d986d412201df9af89Alan Cox		printk(KERN_INFO "%d: %-6s %-8s Volume: %1d %s %s\n",
641963e4975c6f93c148ca809d986d412201df9af89Alan Cox				n, mbuf, types[buf[52]], buf[53], id, cbl);
642963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (buf[125] < 100)
643963e4975c6f93c148ca809d986d412201df9af89Alan Cox		printk(KERN_INFO "%d: Rebuilding: %d%%\n", n, buf[125]);
644963e4975c6f93c148ca809d986d412201df9af89Alan Cox}
645963e4975c6f93c148ca809d986d412201df9af89Alan Cox
646963e4975c6f93c148ca809d986d412201df9af89Alan Cox/**
647963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	it821x_firmware_command		-	issue firmware command
648963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@ap: IT821x port to interrogate
649963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@cmd: command
650963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@len: length
651963e4975c6f93c148ca809d986d412201df9af89Alan Cox *
652963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	Issue firmware commands expecting data back from the controller. We
653963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	use this to issue commands that do not go via the normal paths. Other
654963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	commands such as 0xFC can be issued normally.
655963e4975c6f93c148ca809d986d412201df9af89Alan Cox */
656963e4975c6f93c148ca809d986d412201df9af89Alan Cox
657963e4975c6f93c148ca809d986d412201df9af89Alan Coxstatic u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
658963e4975c6f93c148ca809d986d412201df9af89Alan Cox{
659963e4975c6f93c148ca809d986d412201df9af89Alan Cox	u8 status;
660963e4975c6f93c148ca809d986d412201df9af89Alan Cox	int n = 0;
661963e4975c6f93c148ca809d986d412201df9af89Alan Cox	u16 *buf = kmalloc(len, GFP_KERNEL);
662963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (buf == NULL) {
663963e4975c6f93c148ca809d986d412201df9af89Alan Cox		printk(KERN_ERR "it821x_firmware_command: Out of memory\n");
664963e4975c6f93c148ca809d986d412201df9af89Alan Cox		return NULL;
665963e4975c6f93c148ca809d986d412201df9af89Alan Cox	}
666963e4975c6f93c148ca809d986d412201df9af89Alan Cox	/* This isn't quite a normal ATA command as we are talking to the
667963e4975c6f93c148ca809d986d412201df9af89Alan Cox	   firmware not the drives */
668963e4975c6f93c148ca809d986d412201df9af89Alan Cox	ap->ctl |= ATA_NIEN;
669963e4975c6f93c148ca809d986d412201df9af89Alan Cox	iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
670963e4975c6f93c148ca809d986d412201df9af89Alan Cox	ata_wait_idle(ap);
671963e4975c6f93c148ca809d986d412201df9af89Alan Cox	iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
672963e4975c6f93c148ca809d986d412201df9af89Alan Cox	iowrite8(cmd, ap->ioaddr.command_addr);
673963e4975c6f93c148ca809d986d412201df9af89Alan Cox	udelay(1);
674963e4975c6f93c148ca809d986d412201df9af89Alan Cox	/* This should be almost immediate but a little paranoia goes a long
675963e4975c6f93c148ca809d986d412201df9af89Alan Cox	   way. */
676963e4975c6f93c148ca809d986d412201df9af89Alan Cox	while(n++ < 10) {
677963e4975c6f93c148ca809d986d412201df9af89Alan Cox		status = ioread8(ap->ioaddr.status_addr);
678963e4975c6f93c148ca809d986d412201df9af89Alan Cox		if (status & ATA_ERR) {
679963e4975c6f93c148ca809d986d412201df9af89Alan Cox			kfree(buf);
680963e4975c6f93c148ca809d986d412201df9af89Alan Cox			printk(KERN_ERR "it821x_firmware_command: rejected\n");
681963e4975c6f93c148ca809d986d412201df9af89Alan Cox			return NULL;
682963e4975c6f93c148ca809d986d412201df9af89Alan Cox		}
683963e4975c6f93c148ca809d986d412201df9af89Alan Cox		if (status & ATA_DRQ) {
684963e4975c6f93c148ca809d986d412201df9af89Alan Cox			ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
685963e4975c6f93c148ca809d986d412201df9af89Alan Cox			return (u8 *)buf;
686963e4975c6f93c148ca809d986d412201df9af89Alan Cox		}
687963e4975c6f93c148ca809d986d412201df9af89Alan Cox		mdelay(1);
688963e4975c6f93c148ca809d986d412201df9af89Alan Cox	}
689963e4975c6f93c148ca809d986d412201df9af89Alan Cox	kfree(buf);
690963e4975c6f93c148ca809d986d412201df9af89Alan Cox	printk(KERN_ERR "it821x_firmware_command: timeout\n");
691963e4975c6f93c148ca809d986d412201df9af89Alan Cox	return NULL;
692963e4975c6f93c148ca809d986d412201df9af89Alan Cox}
693963e4975c6f93c148ca809d986d412201df9af89Alan Cox
694963e4975c6f93c148ca809d986d412201df9af89Alan Cox/**
695963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	it821x_probe_firmware	-	firmware reporting/setup
696963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@ap: IT821x port being probed
697963e4975c6f93c148ca809d986d412201df9af89Alan Cox *
698963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	Probe the firmware of the controller by issuing firmware command
699963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	0xFA and analysing the returned data.
700963e4975c6f93c148ca809d986d412201df9af89Alan Cox */
701963e4975c6f93c148ca809d986d412201df9af89Alan Cox
702963e4975c6f93c148ca809d986d412201df9af89Alan Coxstatic void it821x_probe_firmware(struct ata_port *ap)
703963e4975c6f93c148ca809d986d412201df9af89Alan Cox{
704963e4975c6f93c148ca809d986d412201df9af89Alan Cox	u8 *buf;
705963e4975c6f93c148ca809d986d412201df9af89Alan Cox	int i;
706963e4975c6f93c148ca809d986d412201df9af89Alan Cox
707963e4975c6f93c148ca809d986d412201df9af89Alan Cox	/* This is a bit ugly as we can't just issue a task file to a device
708963e4975c6f93c148ca809d986d412201df9af89Alan Cox	   as this is controller magic */
709963e4975c6f93c148ca809d986d412201df9af89Alan Cox
710963e4975c6f93c148ca809d986d412201df9af89Alan Cox	buf = it821x_firmware_command(ap, 0xFA, 512);
711963e4975c6f93c148ca809d986d412201df9af89Alan Cox
712963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (buf != NULL) {
713963e4975c6f93c148ca809d986d412201df9af89Alan Cox		printk(KERN_INFO "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
714963e4975c6f93c148ca809d986d412201df9af89Alan Cox				buf[505],
715963e4975c6f93c148ca809d986d412201df9af89Alan Cox				buf[506],
716963e4975c6f93c148ca809d986d412201df9af89Alan Cox				buf[507],
717963e4975c6f93c148ca809d986d412201df9af89Alan Cox				buf[508]);
718963e4975c6f93c148ca809d986d412201df9af89Alan Cox		for (i = 0; i < 4; i++)
719963e4975c6f93c148ca809d986d412201df9af89Alan Cox 			it821x_display_disk(i, buf + 128 * i);
720963e4975c6f93c148ca809d986d412201df9af89Alan Cox		kfree(buf);
721963e4975c6f93c148ca809d986d412201df9af89Alan Cox	}
722963e4975c6f93c148ca809d986d412201df9af89Alan Cox}
723963e4975c6f93c148ca809d986d412201df9af89Alan Cox
724963e4975c6f93c148ca809d986d412201df9af89Alan Cox
725669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
726669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik/**
727669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	it821x_port_start	-	port setup
728669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	@ap: ATA port being set up
729669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *
730669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	The it821x needs to maintain private data structures and also to
731669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	use the standard PCI interface which lacks support for this
73285cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik *	functionality. We instead set up the private data on the port
733669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik *	start hook, and tear it down on port stop
734669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik */
73585cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
736669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic int it821x_port_start(struct ata_port *ap)
737669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
738669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
739669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	struct it821x_dev *itdev;
740669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8 conf;
741669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
742c7087652e1890a3feef35b30ee1d4be68e1932cdTejun Heo	int ret = ata_bmdma_port_start(ap);
743669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (ret < 0)
744669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		return ret;
74585cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
74624dc5f33ea4b504cfbd23fa159a4cacba8e4d800Tejun Heo	itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
74724dc5f33ea4b504cfbd23fa159a4cacba8e4d800Tejun Heo	if (itdev == NULL)
748669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		return -ENOMEM;
74924dc5f33ea4b504cfbd23fa159a4cacba8e4d800Tejun Heo	ap->private_data = itdev;
750669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
751669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_read_config_byte(pdev, 0x50, &conf);
752669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
753669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (conf & 1) {
754669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->smart = 1;
755669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* Long I/O's although allowed in LBA48 space cause the
756669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		   onboard firmware to enter the twighlight zone */
757669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* No ATAPI DMA in this mode either */
758963e4975c6f93c148ca809d986d412201df9af89Alan Cox		if (ap->port_no == 0)
759963e4975c6f93c148ca809d986d412201df9af89Alan Cox			it821x_probe_firmware(ap);
760669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
761669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Pull the current clocks from 0x50 */
762669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	if (conf & (1 << (1 + ap->port_no)))
763669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->clock_mode = ATA_50;
764669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	else
765669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->clock_mode = ATA_66;
766669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
767669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	itdev->want[0][1] = ATA_ANY;
768669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	itdev->want[1][1] = ATA_ANY;
769669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	itdev->last_device = -1;
770669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
771604de6e0ee3517d7f66427c8fc782761e057f025Alan Cox	if (pdev->revision == 0x10) {
772669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		itdev->timing10 = 1;
773669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		/* Need to disable ATAPI DMA for this case */
774669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		if (!itdev->smart)
775669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik			printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
776669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
777669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
778669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	return 0;
779669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
780669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
781963e4975c6f93c148ca809d986d412201df9af89Alan Cox/**
782963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	it821x_rdc_cable	-	Cable detect for RDC1010
783963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	@ap: port we are checking
784963e4975c6f93c148ca809d986d412201df9af89Alan Cox *
785963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	Return the RDC1010 cable type. Unlike the IT821x we know how to do
786963e4975c6f93c148ca809d986d412201df9af89Alan Cox *	this and can do host side cable detect
787963e4975c6f93c148ca809d986d412201df9af89Alan Cox */
788963e4975c6f93c148ca809d986d412201df9af89Alan Cox
789963e4975c6f93c148ca809d986d412201df9af89Alan Coxstatic int it821x_rdc_cable(struct ata_port *ap)
790963e4975c6f93c148ca809d986d412201df9af89Alan Cox{
791963e4975c6f93c148ca809d986d412201df9af89Alan Cox	u16 r40;
792963e4975c6f93c148ca809d986d412201df9af89Alan Cox	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
793963e4975c6f93c148ca809d986d412201df9af89Alan Cox
794963e4975c6f93c148ca809d986d412201df9af89Alan Cox	pci_read_config_word(pdev, 0x40, &r40);
795963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (r40 & (1 << (2 + ap->port_no)))
796963e4975c6f93c148ca809d986d412201df9af89Alan Cox		return ATA_CBL_PATA40;
797963e4975c6f93c148ca809d986d412201df9af89Alan Cox	return ATA_CBL_PATA80;
798963e4975c6f93c148ca809d986d412201df9af89Alan Cox}
799963e4975c6f93c148ca809d986d412201df9af89Alan Cox
800669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic struct scsi_host_template it821x_sht = {
80168d1d07b510bb57a504588adc2bd2758adea0965Tejun Heo	ATA_BMDMA_SHT(DRV_NAME),
802669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik};
803669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
804669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic struct ata_port_operations it821x_smart_port_ops = {
805029cfd6b74fc5c517865fad78cf4a3ea8d9b664aTejun Heo	.inherits	= &ata_bmdma_port_ops,
80685cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
807669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	.check_atapi_dma= it821x_check_atapi_dma,
8089363c3825ea9ad76561eb48a395349dd29211ed6Tejun Heo	.qc_issue	= it821x_smart_qc_issue,
809bda3028813bd07f34f30288a492fbf6f7b8712ddJeff Garzik
810963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.cable_detect	= ata_cable_80wire,
811029cfd6b74fc5c517865fad78cf4a3ea8d9b664aTejun Heo	.set_mode	= it821x_smart_set_mode,
812029cfd6b74fc5c517865fad78cf4a3ea8d9b664aTejun Heo	.dev_config	= it821x_dev_config,
813963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.read_id	= it821x_read_id,
814669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
815669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	.port_start	= it821x_port_start,
81685cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik};
817669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
818669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic struct ata_port_operations it821x_passthru_port_ops = {
819029cfd6b74fc5c517865fad78cf4a3ea8d9b664aTejun Heo	.inherits	= &ata_bmdma_port_ops,
82085cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
821669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	.check_atapi_dma= it821x_check_atapi_dma,
8225682ed33aae05d10a25c95633ef9d9c062825888Tejun Heo	.sff_dev_select	= it821x_passthru_dev_select,
823669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	.bmdma_start 	= it821x_passthru_bmdma_start,
824669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	.bmdma_stop	= it821x_passthru_bmdma_stop,
8259363c3825ea9ad76561eb48a395349dd29211ed6Tejun Heo	.qc_issue	= it821x_passthru_qc_issue,
826bda3028813bd07f34f30288a492fbf6f7b8712ddJeff Garzik
827029cfd6b74fc5c517865fad78cf4a3ea8d9b664aTejun Heo	.cable_detect	= ata_cable_unknown,
828029cfd6b74fc5c517865fad78cf4a3ea8d9b664aTejun Heo	.set_piomode	= it821x_passthru_set_piomode,
829029cfd6b74fc5c517865fad78cf4a3ea8d9b664aTejun Heo	.set_dmamode	= it821x_passthru_set_dmamode,
830669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
831669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	.port_start	= it821x_port_start,
83285cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik};
833669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
834963e4975c6f93c148ca809d986d412201df9af89Alan Coxstatic struct ata_port_operations it821x_rdc_port_ops = {
835963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.inherits	= &ata_bmdma_port_ops,
836963e4975c6f93c148ca809d986d412201df9af89Alan Cox
837963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.check_atapi_dma= it821x_check_atapi_dma,
838963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.sff_dev_select	= it821x_passthru_dev_select,
839963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.bmdma_start 	= it821x_passthru_bmdma_start,
840963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.bmdma_stop	= it821x_passthru_bmdma_stop,
841963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.qc_issue	= it821x_passthru_qc_issue,
842963e4975c6f93c148ca809d986d412201df9af89Alan Cox
843963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.cable_detect	= it821x_rdc_cable,
844963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.set_piomode	= it821x_passthru_set_piomode,
845963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.set_dmamode	= it821x_passthru_set_dmamode,
846963e4975c6f93c148ca809d986d412201df9af89Alan Cox
847963e4975c6f93c148ca809d986d412201df9af89Alan Cox	.port_start	= it821x_port_start,
848963e4975c6f93c148ca809d986d412201df9af89Alan Cox};
849963e4975c6f93c148ca809d986d412201df9af89Alan Cox
850112cc2b510156494918abdf877111dfd56e5643bRandy Dunlapstatic void it821x_disable_raid(struct pci_dev *pdev)
851669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
852963e4975c6f93c148ca809d986d412201df9af89Alan Cox	/* Neither the RDC nor the IT8211 */
853963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (pdev->vendor != PCI_VENDOR_ID_ITE ||
854963e4975c6f93c148ca809d986d412201df9af89Alan Cox			pdev->device != PCI_DEVICE_ID_ITE_8212)
855963e4975c6f93c148ca809d986d412201df9af89Alan Cox			return;
856963e4975c6f93c148ca809d986d412201df9af89Alan Cox
857669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Reset local CPU, and set BIOS not ready */
858669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_byte(pdev, 0x5E, 0x01);
859669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
860669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	/* Set to bypass mode, and reset PCI bus */
861669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_byte(pdev, 0x50, 0x00);
862669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_word(pdev, PCI_COMMAND,
863669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik			      PCI_COMMAND_PARITY | PCI_COMMAND_IO |
864669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik			      PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
865669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_word(pdev, 0x40, 0xA0F3);
866669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
867669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_dword(pdev,0x4C, 0x02040204);
868669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_byte(pdev, 0x42, 0x36);
869669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
870669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
871669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
87285cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
873669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
874669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
875669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	u8 conf;
87685cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
8771626aeb881236c8cb022b5e4ca594146a951d669Tejun Heo	static const struct ata_port_info info_smart = {
8781d2808fd3d2d5d2c0483796a0f443d1cb3f11367Jeff Garzik		.flags = ATA_FLAG_SLAVE_POSS,
87914bdef982caeda19afe34010482867c18217c641Erik Inge Bolsø		.pio_mask = ATA_PIO4,
88014bdef982caeda19afe34010482867c18217c641Erik Inge Bolsø		.mwdma_mask = ATA_MWDMA2,
881963e4975c6f93c148ca809d986d412201df9af89Alan Cox		.udma_mask = ATA_UDMA6,
882669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		.port_ops = &it821x_smart_port_ops
883669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	};
8841626aeb881236c8cb022b5e4ca594146a951d669Tejun Heo	static const struct ata_port_info info_passthru = {
8851d2808fd3d2d5d2c0483796a0f443d1cb3f11367Jeff Garzik		.flags = ATA_FLAG_SLAVE_POSS,
88614bdef982caeda19afe34010482867c18217c641Erik Inge Bolsø		.pio_mask = ATA_PIO4,
88714bdef982caeda19afe34010482867c18217c641Erik Inge Bolsø		.mwdma_mask = ATA_MWDMA2,
888bf6263a853c9c143bf03f0a6fdcc68ab714fb5f5Jeff Garzik		.udma_mask = ATA_UDMA6,
889669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik		.port_ops = &it821x_passthru_port_ops
890669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	};
891963e4975c6f93c148ca809d986d412201df9af89Alan Cox	static const struct ata_port_info info_rdc = {
892963e4975c6f93c148ca809d986d412201df9af89Alan Cox		.flags = ATA_FLAG_SLAVE_POSS,
89314bdef982caeda19afe34010482867c18217c641Erik Inge Bolsø		.pio_mask = ATA_PIO4,
89414bdef982caeda19afe34010482867c18217c641Erik Inge Bolsø		.mwdma_mask = ATA_MWDMA2,
8954a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox		.udma_mask = ATA_UDMA6,
8964a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox		.port_ops = &it821x_rdc_port_ops
8974a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox	};
8984a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox	static const struct ata_port_info info_rdc_11 = {
8994a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox		.flags = ATA_FLAG_SLAVE_POSS,
90014bdef982caeda19afe34010482867c18217c641Erik Inge Bolsø		.pio_mask = ATA_PIO4,
90114bdef982caeda19afe34010482867c18217c641Erik Inge Bolsø		.mwdma_mask = ATA_MWDMA2,
902963e4975c6f93c148ca809d986d412201df9af89Alan Cox		/* No UDMA */
903963e4975c6f93c148ca809d986d412201df9af89Alan Cox		.port_ops = &it821x_rdc_port_ops
904963e4975c6f93c148ca809d986d412201df9af89Alan Cox	};
90585cd7251b9112e3dabeac9fd3b175601ca607241Jeff Garzik
9061626aeb881236c8cb022b5e4ca594146a951d669Tejun Heo	const struct ata_port_info *ppi[] = { NULL, NULL };
907669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	static char *mode[2] = { "pass through", "smart" };
908f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	int rc;
909f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo
910f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	rc = pcim_enable_device(pdev);
911f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	if (rc)
912f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo		return rc;
9134fca377f7488095ab04035e2bfe5c59873c22382Jeff Garzik
914963e4975c6f93c148ca809d986d412201df9af89Alan Cox	if (pdev->vendor == PCI_VENDOR_ID_RDC) {
9154a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox		/* Deal with Vortex86SX */
9164a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox		if (pdev->revision == 0x11)
9174a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox			ppi[0] = &info_rdc_11;
9184a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox		else
9194a99d95fa836e254d17910e52663ba2be7b880e7Alan Cox			ppi[0] = &info_rdc;
920963e4975c6f93c148ca809d986d412201df9af89Alan Cox	} else {
921963e4975c6f93c148ca809d986d412201df9af89Alan Cox		/* Force the card into bypass mode if so requested */
922963e4975c6f93c148ca809d986d412201df9af89Alan Cox		if (it8212_noraid) {
923963e4975c6f93c148ca809d986d412201df9af89Alan Cox			printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
924963e4975c6f93c148ca809d986d412201df9af89Alan Cox			it821x_disable_raid(pdev);
925963e4975c6f93c148ca809d986d412201df9af89Alan Cox		}
926963e4975c6f93c148ca809d986d412201df9af89Alan Cox		pci_read_config_byte(pdev, 0x50, &conf);
927963e4975c6f93c148ca809d986d412201df9af89Alan Cox		conf &= 1;
928669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
929963e4975c6f93c148ca809d986d412201df9af89Alan Cox		printk(KERN_INFO DRV_NAME": controller in %s mode.\n",
930963e4975c6f93c148ca809d986d412201df9af89Alan Cox								mode[conf]);
931963e4975c6f93c148ca809d986d412201df9af89Alan Cox		if (conf == 0)
932963e4975c6f93c148ca809d986d412201df9af89Alan Cox			ppi[0] = &info_passthru;
933963e4975c6f93c148ca809d986d412201df9af89Alan Cox		else
934963e4975c6f93c148ca809d986d412201df9af89Alan Cox			ppi[0] = &info_smart;
935669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	}
9361c5afdf7a629d2e77de8dd043b97a33dcd7e6dfaTejun Heo	return ata_pci_bmdma_init_one(pdev, ppi, &it821x_sht, NULL, 0);
937669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
938669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
939438ac6d5e3f8106a6bd1a5682c508d660294a85dTejun Heo#ifdef CONFIG_PM
940f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Coxstatic int it821x_reinit_one(struct pci_dev *pdev)
941f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox{
942f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	struct ata_host *host = dev_get_drvdata(&pdev->dev);
943f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	int rc;
944f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo
945f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	rc = ata_pci_device_do_resume(pdev);
946f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	if (rc)
947f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo		return rc;
948f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox	/* Resume - turn raid back off if need be */
949f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox	if (it8212_noraid)
950f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox		it821x_disable_raid(pdev);
951f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	ata_host_resume(host);
952f08048e94564d009b19038cfbdd800aa83e79c7fTejun Heo	return rc;
953f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox}
954438ac6d5e3f8106a6bd1a5682c508d660294a85dTejun Heo#endif
955f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox
9562d2744fc8be620a2dc469cf48349e3e704119f1bJeff Garzikstatic const struct pci_device_id it821x[] = {
9572d2744fc8be620a2dc469cf48349e3e704119f1bJeff Garzik	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
9582d2744fc8be620a2dc469cf48349e3e704119f1bJeff Garzik	{ PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
9594192be64026dd8bfe94970846e4a135e531bd116Otavio Salvador	{ PCI_VDEVICE(RDC, PCI_DEVICE_ID_RDC_D1010), },
9602d2744fc8be620a2dc469cf48349e3e704119f1bJeff Garzik
9612d2744fc8be620a2dc469cf48349e3e704119f1bJeff Garzik	{ },
962669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik};
963669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
964669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic struct pci_driver it821x_pci_driver = {
9652d2744fc8be620a2dc469cf48349e3e704119f1bJeff Garzik	.name 		= DRV_NAME,
966669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	.id_table	= it821x,
967669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	.probe 		= it821x_init_one,
968f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox	.remove		= ata_pci_remove_one,
969438ac6d5e3f8106a6bd1a5682c508d660294a85dTejun Heo#ifdef CONFIG_PM
970f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox	.suspend	= ata_pci_device_suspend,
971f535d53f3f8bc8243ad2167d7127f6efba9b46cbAlan Cox	.resume		= it821x_reinit_one,
972438ac6d5e3f8106a6bd1a5682c508d660294a85dTejun Heo#endif
973669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik};
974669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
975669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic int __init it821x_init(void)
976669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
977669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	return pci_register_driver(&it821x_pci_driver);
978669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
979669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
980669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikstatic void __exit it821x_exit(void)
981669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik{
982669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik	pci_unregister_driver(&it821x_pci_driver);
983669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik}
984669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
985669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff GarzikMODULE_AUTHOR("Alan Cox");
986669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff GarzikMODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
987669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff GarzikMODULE_LICENSE("GPL");
988669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff GarzikMODULE_DEVICE_TABLE(pci, it821x);
989669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff GarzikMODULE_VERSION(DRV_VERSION);
990669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
991669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
992669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikmodule_param_named(noraid, it8212_noraid, int, S_IRUGO);
9935fe675e2b3e41349deb0c81667065b9cb229ea1dStas SergeevMODULE_PARM_DESC(noraid, "Force card into bypass mode");
994669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzik
995669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikmodule_init(it821x_init);
996669a5db411d85a14f86cd92bc16bf7ab5b8aa235Jeff Garzikmodule_exit(it821x_exit);
997