blob: c0797b82ce78180fd5dc75f24c13f52cfbe02bc5 [file] [log] [blame]
Alan Coxda9091e2005-06-27 15:24:30 -07001
2/*
Bartlomiej Zolnierkiewicz52374f82007-07-03 22:28:35 +02003 * linux/drivers/ide/pci/it821x.c Version 0.16 Jul 3 2007
Alan Coxda9091e2005-06-27 15:24:30 -07004 *
5 * Copyright (C) 2004 Red Hat <alan@redhat.com>
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +02006 * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
Alan Coxda9091e2005-06-27 15:24:30 -07007 *
8 * May be copied or modified under the terms of the GNU General Public License
9 * Based in part on the ITE vendor provided SCSI driver.
10 *
11 * Documentation available from
12 * http://www.ite.com.tw/pc/IT8212F_V04.pdf
13 * Some other documents are NDA.
14 *
15 * The ITE8212 isn't exactly a standard IDE controller. It has two
16 * modes. In pass through mode then it is an IDE controller. In its smart
17 * mode its actually quite a capable hardware raid controller disguised
18 * as an IDE controller. Smart mode only understands DMA read/write and
19 * identify, none of the fancier commands apply. The IT8211 is identical
20 * in other respects but lacks the raid mode.
21 *
22 * Errata:
23 * o Rev 0x10 also requires master/slave hold the same DMA timings and
24 * cannot do ATAPI MWDMA.
25 * o The identify data for raid volumes lacks CHS info (technically ok)
26 * but also fails to set the LBA28 and other bits. We fix these in
27 * the IDE probe quirk code.
28 * o If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
29 * raid then the controller firmware dies
30 * o Smart mode without RAID doesn't clear all the necessary identify
31 * bits to reduce the command set to the one used
32 *
33 * This has a few impacts on the driver
34 * - In pass through mode we do all the work you would expect
35 * - In smart mode the clocking set up is done by the controller generally
36 * but we must watch the other limits and filter.
37 * - There are a few extra vendor commands that actually talk to the
38 * controller but only work PIO with no IRQ.
39 *
40 * Vendor areas of the identify block in smart mode are used for the
41 * timing and policy set up. Each HDD in raid mode also has a serial
42 * block on the disk. The hardware extra commands are get/set chip status,
43 * rebuild, get rebuild status.
44 *
45 * In Linux the driver supports pass through mode as if the device was
46 * just another IDE controller. If the smart mode is running then
47 * volumes are managed by the controller firmware and each IDE "disk"
48 * is a raid volume. Even more cute - the controller can do automated
49 * hotplug and rebuild.
50 *
51 * The pass through controller itself is a little demented. It has a
52 * flaw that it has a single set of PIO/MWDMA timings per channel so
53 * non UDMA devices restrict each others performance. It also has a
54 * single clock source per channel so mixed UDMA100/133 performance
55 * isn't perfect and we have to pick a clock. Thankfully none of this
56 * matters in smart mode. ATAPI DMA is not currently supported.
57 *
58 * It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
59 *
60 * TODO
61 * - ATAPI UDMA is ok but not MWDMA it seems
62 * - RAID configuration ioctls
63 * - Move to libata once it grows up
64 */
65
Alan Coxda9091e2005-06-27 15:24:30 -070066#include <linux/types.h>
67#include <linux/module.h>
68#include <linux/pci.h>
69#include <linux/delay.h>
70#include <linux/hdreg.h>
71#include <linux/ide.h>
72#include <linux/init.h>
73
74#include <asm/io.h>
75
76struct it821x_dev
77{
78 unsigned int smart:1, /* Are we in smart raid mode */
79 timing10:1; /* Rev 0x10 */
80 u8 clock_mode; /* 0, ATA_50 or ATA_66 */
81 u8 want[2][2]; /* Mode/Pri log for master slave */
82 /* We need these for switching the clock when DMA goes on/off
83 The high byte is the 66Mhz timing */
84 u16 pio[2]; /* Cached PIO values */
85 u16 mwdma[2]; /* Cached MWDMA values */
86 u16 udma[2]; /* Cached UDMA values (per drive) */
87};
88
89#define ATA_66 0
90#define ATA_50 1
91#define ATA_ANY 2
92
93#define UDMA_OFF 0
94#define MWDMA_OFF 0
95
96/*
97 * We allow users to force the card into non raid mode without
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020098 * flashing the alternative BIOS. This is also necessary right now
Alan Coxda9091e2005-06-27 15:24:30 -070099 * for embedded platforms that cannot run a PC BIOS but are using this
100 * device.
101 */
102
103static int it8212_noraid;
104
105/**
106 * it821x_program - program the PIO/MWDMA registers
107 * @drive: drive to tune
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +0200108 * @timing: timing info
Alan Coxda9091e2005-06-27 15:24:30 -0700109 *
110 * Program the PIO/MWDMA timing for this channel according to the
111 * current clock.
112 */
113
114static void it821x_program(ide_drive_t *drive, u16 timing)
115{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100116 ide_hwif_t *hwif = drive->hwif;
117 struct pci_dev *dev = to_pci_dev(hwif->dev);
Alan Coxda9091e2005-06-27 15:24:30 -0700118 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
119 int channel = hwif->channel;
120 u8 conf;
121
122 /* Program PIO/MWDMA timing bits */
123 if(itdev->clock_mode == ATA_66)
124 conf = timing >> 8;
125 else
126 conf = timing & 0xFF;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100127
128 pci_write_config_byte(dev, 0x54 + 4 * channel, conf);
Alan Coxda9091e2005-06-27 15:24:30 -0700129}
130
131/**
132 * it821x_program_udma - program the UDMA registers
133 * @drive: drive to tune
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +0200134 * @timing: timing info
Alan Coxda9091e2005-06-27 15:24:30 -0700135 *
136 * Program the UDMA timing for this drive according to the
137 * current clock.
138 */
139
140static void it821x_program_udma(ide_drive_t *drive, u16 timing)
141{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100142 ide_hwif_t *hwif = drive->hwif;
143 struct pci_dev *dev = to_pci_dev(hwif->dev);
Alan Coxda9091e2005-06-27 15:24:30 -0700144 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
145 int channel = hwif->channel;
146 int unit = drive->select.b.unit;
147 u8 conf;
148
149 /* Program UDMA timing bits */
150 if(itdev->clock_mode == ATA_66)
151 conf = timing >> 8;
152 else
153 conf = timing & 0xFF;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100154
155 if (itdev->timing10 == 0)
156 pci_write_config_byte(dev, 0x56 + 4 * channel + unit, conf);
Alan Coxda9091e2005-06-27 15:24:30 -0700157 else {
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100158 pci_write_config_byte(dev, 0x56 + 4 * channel, conf);
159 pci_write_config_byte(dev, 0x56 + 4 * channel + 1, conf);
Alan Coxda9091e2005-06-27 15:24:30 -0700160 }
161}
162
Alan Coxda9091e2005-06-27 15:24:30 -0700163/**
164 * it821x_clock_strategy
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +0200165 * @drive: drive to set up
Alan Coxda9091e2005-06-27 15:24:30 -0700166 *
167 * Select between the 50 and 66Mhz base clocks to get the best
168 * results for this interface.
169 */
170
171static void it821x_clock_strategy(ide_drive_t *drive)
172{
173 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100174 struct pci_dev *dev = to_pci_dev(hwif->dev);
Alan Coxda9091e2005-06-27 15:24:30 -0700175 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
176
177 u8 unit = drive->select.b.unit;
178 ide_drive_t *pair = &hwif->drives[1-unit];
179
180 int clock, altclock;
181 u8 v;
182 int sel = 0;
183
184 if(itdev->want[0][0] > itdev->want[1][0]) {
185 clock = itdev->want[0][1];
186 altclock = itdev->want[1][1];
187 } else {
188 clock = itdev->want[1][1];
189 altclock = itdev->want[0][1];
190 }
191
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +0200192 /*
193 * if both clocks can be used for the mode with the higher priority
194 * use the clock needed by the mode with the lower priority
195 */
196 if (clock == ATA_ANY)
Alan Coxda9091e2005-06-27 15:24:30 -0700197 clock = altclock;
198
199 /* Nobody cares - keep the same clock */
200 if(clock == ATA_ANY)
201 return;
202 /* No change */
203 if(clock == itdev->clock_mode)
204 return;
205
206 /* Load this into the controller ? */
207 if(clock == ATA_66)
208 itdev->clock_mode = ATA_66;
209 else {
210 itdev->clock_mode = ATA_50;
211 sel = 1;
212 }
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100213
214 pci_read_config_byte(dev, 0x50, &v);
Alan Coxda9091e2005-06-27 15:24:30 -0700215 v &= ~(1 << (1 + hwif->channel));
216 v |= sel << (1 + hwif->channel);
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100217 pci_write_config_byte(dev, 0x50, v);
Alan Coxda9091e2005-06-27 15:24:30 -0700218
219 /*
220 * Reprogram the UDMA/PIO of the pair drive for the switch
221 * MWDMA will be dealt with by the dma switcher
222 */
223 if(pair && itdev->udma[1-unit] != UDMA_OFF) {
224 it821x_program_udma(pair, itdev->udma[1-unit]);
225 it821x_program(pair, itdev->pio[1-unit]);
226 }
227 /*
228 * Reprogram the UDMA/PIO of our drive for the switch.
229 * MWDMA will be dealt with by the dma switcher
230 */
231 if(itdev->udma[unit] != UDMA_OFF) {
232 it821x_program_udma(drive, itdev->udma[unit]);
233 it821x_program(drive, itdev->pio[unit]);
234 }
235}
236
237/**
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200238 * it821x_set_pio_mode - set host controller for PIO mode
239 * @drive: drive
240 * @pio: PIO mode number
Alan Coxda9091e2005-06-27 15:24:30 -0700241 *
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200242 * Tune the host to the desired PIO mode taking into the consideration
243 * the maximum PIO mode supported by the other device on the cable.
Alan Coxda9091e2005-06-27 15:24:30 -0700244 */
245
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200246static void it821x_set_pio_mode(ide_drive_t *drive, const u8 pio)
Alan Coxda9091e2005-06-27 15:24:30 -0700247{
248 ide_hwif_t *hwif = drive->hwif;
249 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
250 int unit = drive->select.b.unit;
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +0200251 ide_drive_t *pair = &hwif->drives[1 - unit];
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200252 u8 set_pio = pio;
Alan Coxda9091e2005-06-27 15:24:30 -0700253
254 /* Spec says 89 ref driver uses 88 */
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200255 static u16 pio_timings[]= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
Alan Coxda9091e2005-06-27 15:24:30 -0700256 static u8 pio_want[] = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
257
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +0200258 /*
259 * Compute the best PIO mode we can for a given device. We must
260 * pick a speed that does not cause problems with the other device
261 * on the cable.
262 */
263 if (pair) {
Bartlomiej Zolnierkiewicz21347582007-07-20 01:11:58 +0200264 u8 pair_pio = ide_get_best_pio_mode(pair, 255, 4);
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +0200265 /* trim PIO to the slowest of the master/slave */
266 if (pair_pio < set_pio)
267 set_pio = pair_pio;
268 }
269
Alan Coxda9091e2005-06-27 15:24:30 -0700270 /* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
Bartlomiej Zolnierkiewicz0e9b4e52007-05-05 22:03:50 +0200271 itdev->want[unit][1] = pio_want[set_pio];
Alan Coxda9091e2005-06-27 15:24:30 -0700272 itdev->want[unit][0] = 1; /* PIO is lowest priority */
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200273 itdev->pio[unit] = pio_timings[set_pio];
Alan Coxda9091e2005-06-27 15:24:30 -0700274 it821x_clock_strategy(drive);
275 it821x_program(drive, itdev->pio[unit]);
276}
277
278/**
279 * it821x_tune_mwdma - tune a channel for MWDMA
280 * @drive: drive to set up
281 * @mode_wanted: the target operating mode
282 *
283 * Load the timing settings for this device mode into the
284 * controller when doing MWDMA in pass through mode. The caller
285 * must manage the whole lack of per device MWDMA/PIO timings and
286 * the shared MWDMA/PIO timing register.
287 */
288
289static void it821x_tune_mwdma (ide_drive_t *drive, byte mode_wanted)
290{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100291 ide_hwif_t *hwif = drive->hwif;
292 struct pci_dev *dev = to_pci_dev(hwif->dev);
Alan Coxda9091e2005-06-27 15:24:30 -0700293 struct it821x_dev *itdev = (void *)ide_get_hwifdata(hwif);
294 int unit = drive->select.b.unit;
295 int channel = hwif->channel;
296 u8 conf;
297
298 static u16 dma[] = { 0x8866, 0x3222, 0x3121 };
299 static u8 mwdma_want[] = { ATA_ANY, ATA_66, ATA_ANY };
300
301 itdev->want[unit][1] = mwdma_want[mode_wanted];
302 itdev->want[unit][0] = 2; /* MWDMA is low priority */
303 itdev->mwdma[unit] = dma[mode_wanted];
304 itdev->udma[unit] = UDMA_OFF;
305
306 /* UDMA bits off - Revision 0x10 do them in pairs */
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100307 pci_read_config_byte(dev, 0x50, &conf);
308 if (itdev->timing10)
Alan Coxda9091e2005-06-27 15:24:30 -0700309 conf |= channel ? 0x60: 0x18;
310 else
311 conf |= 1 << (3 + 2 * channel + unit);
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100312 pci_write_config_byte(dev, 0x50, conf);
Alan Coxda9091e2005-06-27 15:24:30 -0700313
314 it821x_clock_strategy(drive);
315 /* FIXME: do we need to program this ? */
316 /* it821x_program(drive, itdev->mwdma[unit]); */
317}
318
319/**
320 * it821x_tune_udma - tune a channel for UDMA
321 * @drive: drive to set up
322 * @mode_wanted: the target operating mode
323 *
324 * Load the timing settings for this device mode into the
325 * controller when doing UDMA modes in pass through.
326 */
327
328static void it821x_tune_udma (ide_drive_t *drive, byte mode_wanted)
329{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100330 ide_hwif_t *hwif = drive->hwif;
331 struct pci_dev *dev = to_pci_dev(hwif->dev);
Alan Coxda9091e2005-06-27 15:24:30 -0700332 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
333 int unit = drive->select.b.unit;
334 int channel = hwif->channel;
335 u8 conf;
336
337 static u16 udma[] = { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
338 static u8 udma_want[] = { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
339
340 itdev->want[unit][1] = udma_want[mode_wanted];
341 itdev->want[unit][0] = 3; /* UDMA is high priority */
342 itdev->mwdma[unit] = MWDMA_OFF;
343 itdev->udma[unit] = udma[mode_wanted];
344 if(mode_wanted >= 5)
345 itdev->udma[unit] |= 0x8080; /* UDMA 5/6 select on */
346
347 /* UDMA on. Again revision 0x10 must do the pair */
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100348 pci_read_config_byte(dev, 0x50, &conf);
349 if (itdev->timing10)
Alan Coxda9091e2005-06-27 15:24:30 -0700350 conf &= channel ? 0x9F: 0xE7;
351 else
352 conf &= ~ (1 << (3 + 2 * channel + unit));
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100353 pci_write_config_byte(dev, 0x50, conf);
Alan Coxda9091e2005-06-27 15:24:30 -0700354
355 it821x_clock_strategy(drive);
356 it821x_program_udma(drive, itdev->udma[unit]);
357
358}
359
360/**
Alan Coxda9091e2005-06-27 15:24:30 -0700361 * it821x_dma_read - DMA hook
362 * @drive: drive for DMA
363 *
364 * The IT821x has a single timing register for MWDMA and for PIO
365 * operations. As we flip back and forth we have to reload the
366 * clock. In addition the rev 0x10 device only works if the same
367 * timing value is loaded into the master and slave UDMA clock
368 * so we must also reload that.
369 *
370 * FIXME: we could figure out in advance if we need to do reloads
371 */
372
373static void it821x_dma_start(ide_drive_t *drive)
374{
375 ide_hwif_t *hwif = drive->hwif;
376 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
377 int unit = drive->select.b.unit;
378 if(itdev->mwdma[unit] != MWDMA_OFF)
379 it821x_program(drive, itdev->mwdma[unit]);
380 else if(itdev->udma[unit] != UDMA_OFF && itdev->timing10)
381 it821x_program_udma(drive, itdev->udma[unit]);
382 ide_dma_start(drive);
383}
384
385/**
386 * it821x_dma_write - DMA hook
387 * @drive: drive for DMA stop
388 *
389 * The IT821x has a single timing register for MWDMA and for PIO
390 * operations. As we flip back and forth we have to reload the
391 * clock.
392 */
393
394static int it821x_dma_end(ide_drive_t *drive)
395{
396 ide_hwif_t *hwif = drive->hwif;
397 int unit = drive->select.b.unit;
398 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
399 int ret = __ide_dma_end(drive);
400 if(itdev->mwdma[unit] != MWDMA_OFF)
401 it821x_program(drive, itdev->pio[unit]);
402 return ret;
403}
404
Alan Coxda9091e2005-06-27 15:24:30 -0700405/**
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200406 * it821x_set_dma_mode - set host controller for DMA mode
407 * @drive: drive
408 * @speed: DMA mode
Alan Coxda9091e2005-06-27 15:24:30 -0700409 *
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200410 * Tune the ITE chipset for the desired DMA mode.
Alan Coxda9091e2005-06-27 15:24:30 -0700411 */
412
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200413static void it821x_set_dma_mode(ide_drive_t *drive, const u8 speed)
Alan Coxda9091e2005-06-27 15:24:30 -0700414{
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200415 /*
416 * MWDMA tuning is really hard because our MWDMA and PIO
417 * timings are kept in the same place. We can switch in the
418 * host dma on/off callbacks.
419 */
420 if (speed >= XFER_UDMA_0 && speed <= XFER_UDMA_6)
421 it821x_tune_udma(drive, speed - XFER_UDMA_0);
422 else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
423 it821x_tune_mwdma(drive, speed - XFER_MW_DMA_0);
Alan Coxda9091e2005-06-27 15:24:30 -0700424}
425
426/**
Alan Coxda9091e2005-06-27 15:24:30 -0700427 * ata66_it821x - check for 80 pin cable
428 * @hwif: interface to check
429 *
430 * Check for the presence of an ATA66 capable cable on the
431 * interface. Problematic as it seems some cards don't have
432 * the needed logic onboard.
433 */
434
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200435static u8 __devinit ata66_it821x(ide_hwif_t *hwif)
Alan Coxda9091e2005-06-27 15:24:30 -0700436{
437 /* The reference driver also only does disk side */
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200438 return ATA_CBL_PATA80;
Alan Coxda9091e2005-06-27 15:24:30 -0700439}
440
441/**
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100442 * it821x_quirkproc - post init callback
443 * @drive: drive
Alan Coxda9091e2005-06-27 15:24:30 -0700444 *
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100445 * This callback is run after the drive has been probed but
Alan Coxda9091e2005-06-27 15:24:30 -0700446 * before anything gets attached. It allows drivers to do any
447 * final tuning that is needed, or fixups to work around bugs.
448 */
449
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100450static void __devinit it821x_quirkproc(ide_drive_t *drive)
Alan Coxda9091e2005-06-27 15:24:30 -0700451{
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100452 struct it821x_dev *itdev = ide_get_hwifdata(drive->hwif);
453 struct hd_driveid *id = drive->id;
454 u16 *idbits = (u16 *)drive->id;
Alan Coxda9091e2005-06-27 15:24:30 -0700455
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100456 if (!itdev->smart) {
Alan Coxda9091e2005-06-27 15:24:30 -0700457 /*
458 * If we are in pass through mode then not much
459 * needs to be done, but we do bother to clear the
460 * IRQ mask as we may well be in PIO (eg rev 0x10)
461 * for now and we know unmasking is safe on this chipset.
462 */
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100463 drive->unmask = 1;
464 } else {
Alan Coxda9091e2005-06-27 15:24:30 -0700465 /*
466 * Perform fixups on smart mode. We need to "lose" some
467 * capabilities the firmware lacks but does not filter, and
468 * also patch up some capability bits that it forgets to set
469 * in RAID mode.
470 */
471
Alan Coxda9091e2005-06-27 15:24:30 -0700472 /* Check for RAID v native */
473 if(strstr(id->model, "Integrated Technology Express")) {
474 /* In raid mode the ident block is slightly buggy
475 We need to set the bits so that the IDE layer knows
476 LBA28. LBA48 and DMA ar valid */
477 id->capability |= 3; /* LBA28, DMA */
478 id->command_set_2 |= 0x0400; /* LBA48 valid */
479 id->cfs_enable_2 |= 0x0400; /* LBA48 on */
480 /* Reporting logic */
481 printk(KERN_INFO "%s: IT8212 %sRAID %d volume",
482 drive->name,
483 idbits[147] ? "Bootable ":"",
484 idbits[129]);
485 if(idbits[129] != 1)
486 printk("(%dK stripe)", idbits[146]);
487 printk(".\n");
Alan Coxda9091e2005-06-27 15:24:30 -0700488 } else {
489 /* Non RAID volume. Fixups to stop the core code
490 doing unsupported things */
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200491 id->field_valid &= 3;
Alan Coxda9091e2005-06-27 15:24:30 -0700492 id->queue_depth = 0;
493 id->command_set_1 = 0;
494 id->command_set_2 &= 0xC400;
495 id->cfsse &= 0xC000;
496 id->cfs_enable_1 = 0;
497 id->cfs_enable_2 &= 0xC400;
498 id->csf_default &= 0xC000;
499 id->word127 = 0;
500 id->dlf = 0;
501 id->csfo = 0;
502 id->cfa_power = 0;
503 printk(KERN_INFO "%s: Performing identify fixups.\n",
504 drive->name);
505 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200506
507 /*
508 * Set MWDMA0 mode as enabled/support - just to tell
509 * IDE core that DMA is supported (it821x hardware
510 * takes care of DMA mode programming).
511 */
512 if (id->capability & 1) {
513 id->dma_mword |= 0x0101;
514 drive->current_speed = XFER_MW_DMA_0;
515 }
Alan Coxda9091e2005-06-27 15:24:30 -0700516 }
517
518}
519
520/**
521 * init_hwif_it821x - set up hwif structs
522 * @hwif: interface to set up
523 *
524 * We do the basic set up of the interface structure. The IT8212
525 * requires several custom handlers so we override the default
526 * ide DMA handlers appropriately
527 */
528
529static void __devinit init_hwif_it821x(ide_hwif_t *hwif)
530{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100531 struct pci_dev *dev = to_pci_dev(hwif->dev);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800532 struct it821x_dev *idev = kzalloc(sizeof(struct it821x_dev), GFP_KERNEL);
Alan Coxda9091e2005-06-27 15:24:30 -0700533 u8 conf;
534
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100535 hwif->quirkproc = &it821x_quirkproc;
536
Bartlomiej Zolnierkiewicz9ff6f722007-10-16 22:29:58 +0200537 if (idev == NULL) {
Alan Coxda9091e2005-06-27 15:24:30 -0700538 printk(KERN_ERR "it821x: out of memory, falling back to legacy behaviour.\n");
Bartlomiej Zolnierkiewicz9ff6f722007-10-16 22:29:58 +0200539 return;
Alan Coxda9091e2005-06-27 15:24:30 -0700540 }
Bartlomiej Zolnierkiewicz9ff6f722007-10-16 22:29:58 +0200541
Alan Coxda9091e2005-06-27 15:24:30 -0700542 ide_set_hwifdata(hwif, idev);
543
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100544 pci_read_config_byte(dev, 0x50, &conf);
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200545 if (conf & 1) {
Alan Coxda9091e2005-06-27 15:24:30 -0700546 idev->smart = 1;
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200547 hwif->host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
Alan Coxda9091e2005-06-27 15:24:30 -0700548 /* Long I/O's although allowed in LBA48 space cause the
549 onboard firmware to enter the twighlight zone */
550 hwif->rqsize = 256;
551 }
552
553 /* Pull the current clocks from 0x50 also */
554 if (conf & (1 << (1 + hwif->channel)))
555 idev->clock_mode = ATA_50;
556 else
557 idev->clock_mode = ATA_66;
558
559 idev->want[0][1] = ATA_ANY;
560 idev->want[1][1] = ATA_ANY;
561
562 /*
563 * Not in the docs but according to the reference driver
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200564 * this is necessary.
Alan Coxda9091e2005-06-27 15:24:30 -0700565 */
566
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100567 pci_read_config_byte(dev, 0x08, &conf);
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200568 if (conf == 0x10) {
Alan Coxda9091e2005-06-27 15:24:30 -0700569 idev->timing10 = 1;
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200570 hwif->host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
571 if (idev->smart == 0)
Alan Coxda9091e2005-06-27 15:24:30 -0700572 printk(KERN_WARNING "it821x: Revision 0x10, workarounds activated.\n");
573 }
574
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200575 if (idev->smart == 0) {
576 hwif->set_pio_mode = &it821x_set_pio_mode;
577 hwif->set_dma_mode = &it821x_set_dma_mode;
Alan Coxda9091e2005-06-27 15:24:30 -0700578
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200579 /* MWDMA/PIO clock switching for pass through mode */
Alan Coxda9091e2005-06-27 15:24:30 -0700580 hwif->dma_start = &it821x_dma_start;
581 hwif->ide_dma_end = &it821x_dma_end;
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200582 } else
583 hwif->host_flags |= IDE_HFLAG_NO_SET_MODE;
Alan Coxda9091e2005-06-27 15:24:30 -0700584
Bartlomiej Zolnierkiewicz9ff6f722007-10-16 22:29:58 +0200585 if (hwif->dma_base == 0)
586 return;
Alan Coxda9091e2005-06-27 15:24:30 -0700587
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200588 hwif->ultra_mask = ATA_UDMA6;
589 hwif->mwdma_mask = ATA_MWDMA2;
Alan Coxda9091e2005-06-27 15:24:30 -0700590
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200591 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
592 hwif->cbl = ata66_it821x(hwif);
Alan Coxda9091e2005-06-27 15:24:30 -0700593}
594
595static void __devinit it8212_disable_raid(struct pci_dev *dev)
596{
597 /* Reset local CPU, and set BIOS not ready */
598 pci_write_config_byte(dev, 0x5E, 0x01);
599
600 /* Set to bypass mode, and reset PCI bus */
601 pci_write_config_byte(dev, 0x50, 0x00);
602 pci_write_config_word(dev, PCI_COMMAND,
603 PCI_COMMAND_PARITY | PCI_COMMAND_IO |
604 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
605 pci_write_config_word(dev, 0x40, 0xA0F3);
606
607 pci_write_config_dword(dev,0x4C, 0x02040204);
608 pci_write_config_byte(dev, 0x42, 0x36);
Alan Cox0c866b52006-02-03 03:04:58 -0800609 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x20);
Alan Coxda9091e2005-06-27 15:24:30 -0700610}
611
612static unsigned int __devinit init_chipset_it821x(struct pci_dev *dev, const char *name)
613{
614 u8 conf;
615 static char *mode[2] = { "pass through", "smart" };
616
617 /* Force the card into bypass mode if so requested */
618 if (it8212_noraid) {
619 printk(KERN_INFO "it8212: forcing bypass mode.\n");
620 it8212_disable_raid(dev);
621 }
622 pci_read_config_byte(dev, 0x50, &conf);
623 printk(KERN_INFO "it821x: controller in %s mode.\n", mode[conf & 1]);
624 return 0;
625}
626
627
628#define DECLARE_ITE_DEV(name_str) \
629 { \
630 .name = name_str, \
631 .init_chipset = init_chipset_it821x, \
632 .init_hwif = init_hwif_it821x, \
Bartlomiej Zolnierkiewicz7cab14a2007-10-19 00:30:06 +0200633 .host_flags = IDE_HFLAG_BOOTABLE, \
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200634 .pio_mask = ATA_PIO4, \
Alan Coxda9091e2005-06-27 15:24:30 -0700635 }
636
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200637static const struct ide_port_info it821x_chipsets[] __devinitdata = {
Alan Coxda9091e2005-06-27 15:24:30 -0700638 /* 0 */ DECLARE_ITE_DEV("IT8212"),
639};
640
641/**
642 * it821x_init_one - pci layer discovery entry
643 * @dev: PCI device
644 * @id: ident table entry
645 *
646 * Called by the PCI code when it finds an ITE821x controller.
647 * We then use the IDE PCI generic helper to do most of the work.
648 */
649
650static int __devinit it821x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
651{
Bartlomiej Zolnierkiewicz24ffbd62007-11-13 22:09:16 +0100652 return ide_setup_pci_device(dev, &it821x_chipsets[id->driver_data]);
Alan Coxda9091e2005-06-27 15:24:30 -0700653}
654
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200655static const struct pci_device_id it821x_pci_tbl[] = {
656 { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), 0 },
657 { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), 0 },
Alan Coxda9091e2005-06-27 15:24:30 -0700658 { 0, },
659};
660
661MODULE_DEVICE_TABLE(pci, it821x_pci_tbl);
662
663static struct pci_driver driver = {
664 .name = "ITE821x IDE",
665 .id_table = it821x_pci_tbl,
666 .probe = it821x_init_one,
667};
668
669static int __init it821x_ide_init(void)
670{
671 return ide_pci_register_driver(&driver);
672}
673
674module_init(it821x_ide_init);
675
676module_param_named(noraid, it8212_noraid, int, S_IRUGO);
677MODULE_PARM_DESC(it8212_noraid, "Force card into bypass mode");
678
679MODULE_AUTHOR("Alan Cox");
680MODULE_DESCRIPTION("PCI driver module for the ITE 821x");
681MODULE_LICENSE("GPL");