Merge ../linux-2.6
[pandora-kernel.git] / drivers / ide / pci / it821x.c
1
2 /*
3  * linux/drivers/ide/pci/it821x.c               Version 0.09    December 2004
4  *
5  * Copyright (C) 2004           Red Hat <alan@redhat.com>
6  *
7  *  May be copied or modified under the terms of the GNU General Public License
8  *  Based in part on the ITE vendor provided SCSI driver.
9  *
10  *  Documentation available from
11  *      http://www.ite.com.tw/pc/IT8212F_V04.pdf
12  *  Some other documents are NDA.
13  *
14  *  The ITE8212 isn't exactly a standard IDE controller. It has two
15  *  modes. In pass through mode then it is an IDE controller. In its smart
16  *  mode its actually quite a capable hardware raid controller disguised
17  *  as an IDE controller. Smart mode only understands DMA read/write and
18  *  identify, none of the fancier commands apply. The IT8211 is identical
19  *  in other respects but lacks the raid mode.
20  *
21  *  Errata:
22  *  o   Rev 0x10 also requires master/slave hold the same DMA timings and
23  *      cannot do ATAPI MWDMA.
24  *  o   The identify data for raid volumes lacks CHS info (technically ok)
25  *      but also fails to set the LBA28 and other bits. We fix these in
26  *      the IDE probe quirk code.
27  *  o   If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
28  *      raid then the controller firmware dies
29  *  o   Smart mode without RAID doesn't clear all the necessary identify
30  *      bits to reduce the command set to the one used
31  *
32  *  This has a few impacts on the driver
33  *  - In pass through mode we do all the work you would expect
34  *  - In smart mode the clocking set up is done by the controller generally
35  *    but we must watch the other limits and filter.
36  *  - There are a few extra vendor commands that actually talk to the
37  *    controller but only work PIO with no IRQ.
38  *
39  *  Vendor areas of the identify block in smart mode are used for the
40  *  timing and policy set up. Each HDD in raid mode also has a serial
41  *  block on the disk. The hardware extra commands are get/set chip status,
42  *  rebuild, get rebuild status.
43  *
44  *  In Linux the driver supports pass through mode as if the device was
45  *  just another IDE controller. If the smart mode is running then
46  *  volumes are managed by the controller firmware and each IDE "disk"
47  *  is a raid volume. Even more cute - the controller can do automated
48  *  hotplug and rebuild.
49  *
50  *  The pass through controller itself is a little demented. It has a
51  *  flaw that it has a single set of PIO/MWDMA timings per channel so
52  *  non UDMA devices restrict each others performance. It also has a
53  *  single clock source per channel so mixed UDMA100/133 performance
54  *  isn't perfect and we have to pick a clock. Thankfully none of this
55  *  matters in smart mode. ATAPI DMA is not currently supported.
56  *
57  *  It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
58  *
59  *  TODO
60  *      -       ATAPI UDMA is ok but not MWDMA it seems
61  *      -       RAID configuration ioctls
62  *      -       Move to libata once it grows up
63  */
64
65 #include <linux/types.h>
66 #include <linux/module.h>
67 #include <linux/pci.h>
68 #include <linux/delay.h>
69 #include <linux/hdreg.h>
70 #include <linux/ide.h>
71 #include <linux/init.h>
72
73 #include <asm/io.h>
74
75 struct it821x_dev
76 {
77         unsigned int smart:1,           /* Are we in smart raid mode */
78                 timing10:1;             /* Rev 0x10 */
79         u8      clock_mode;             /* 0, ATA_50 or ATA_66 */
80         u8      want[2][2];             /* Mode/Pri log for master slave */
81         /* We need these for switching the clock when DMA goes on/off
82            The high byte is the 66Mhz timing */
83         u16     pio[2];                 /* Cached PIO values */
84         u16     mwdma[2];               /* Cached MWDMA values */
85         u16     udma[2];                /* Cached UDMA values (per drive) */
86 };
87
88 #define ATA_66          0
89 #define ATA_50          1
90 #define ATA_ANY         2
91
92 #define UDMA_OFF        0
93 #define MWDMA_OFF       0
94
95 /*
96  *      We allow users to force the card into non raid mode without
97  *      flashing the alternative BIOS. This is also neccessary right now
98  *      for embedded platforms that cannot run a PC BIOS but are using this
99  *      device.
100  */
101
102 static int it8212_noraid;
103
104 /**
105  *      it821x_program  -       program the PIO/MWDMA registers
106  *      @drive: drive to tune
107  *
108  *      Program the PIO/MWDMA timing for this channel according to the
109  *      current clock.
110  */
111
112 static void it821x_program(ide_drive_t *drive, u16 timing)
113 {
114         ide_hwif_t *hwif        = drive->hwif;
115         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
116         int channel = hwif->channel;
117         u8 conf;
118
119         /* Program PIO/MWDMA timing bits */
120         if(itdev->clock_mode == ATA_66)
121                 conf = timing >> 8;
122         else
123                 conf = timing & 0xFF;
124         pci_write_config_byte(hwif->pci_dev, 0x54 + 4 * channel, conf);
125 }
126
127 /**
128  *      it821x_program_udma     -       program the UDMA registers
129  *      @drive: drive to tune
130  *
131  *      Program the UDMA timing for this drive according to the
132  *      current clock.
133  */
134
135 static void it821x_program_udma(ide_drive_t *drive, u16 timing)
136 {
137         ide_hwif_t *hwif        = drive->hwif;
138         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
139         int channel = hwif->channel;
140         int unit = drive->select.b.unit;
141         u8 conf;
142
143         /* Program UDMA timing bits */
144         if(itdev->clock_mode == ATA_66)
145                 conf = timing >> 8;
146         else
147                 conf = timing & 0xFF;
148         if(itdev->timing10 == 0)
149                 pci_write_config_byte(hwif->pci_dev, 0x56 + 4 * channel + unit, conf);
150         else {
151                 pci_write_config_byte(hwif->pci_dev, 0x56 + 4 * channel, conf);
152                 pci_write_config_byte(hwif->pci_dev, 0x56 + 4 * channel + 1, conf);
153         }
154 }
155
156
157 /**
158  *      it821x_clock_strategy
159  *      @hwif: hardware interface
160  *
161  *      Select between the 50 and 66Mhz base clocks to get the best
162  *      results for this interface.
163  */
164
165 static void it821x_clock_strategy(ide_drive_t *drive)
166 {
167         ide_hwif_t *hwif = drive->hwif;
168         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
169
170         u8 unit = drive->select.b.unit;
171         ide_drive_t *pair = &hwif->drives[1-unit];
172
173         int clock, altclock;
174         u8 v;
175         int sel = 0;
176
177         if(itdev->want[0][0] > itdev->want[1][0]) {
178                 clock = itdev->want[0][1];
179                 altclock = itdev->want[1][1];
180         } else {
181                 clock = itdev->want[1][1];
182                 altclock = itdev->want[0][1];
183         }
184
185         /* Master doesn't care does the slave ? */
186         if(clock == ATA_ANY)
187                 clock = altclock;
188
189         /* Nobody cares - keep the same clock */
190         if(clock == ATA_ANY)
191                 return;
192         /* No change */
193         if(clock == itdev->clock_mode)
194                 return;
195
196         /* Load this into the controller ? */
197         if(clock == ATA_66)
198                 itdev->clock_mode = ATA_66;
199         else {
200                 itdev->clock_mode = ATA_50;
201                 sel = 1;
202         }
203         pci_read_config_byte(hwif->pci_dev, 0x50, &v);
204         v &= ~(1 << (1 + hwif->channel));
205         v |= sel << (1 + hwif->channel);
206         pci_write_config_byte(hwif->pci_dev, 0x50, v);
207
208         /*
209          *      Reprogram the UDMA/PIO of the pair drive for the switch
210          *      MWDMA will be dealt with by the dma switcher
211          */
212         if(pair && itdev->udma[1-unit] != UDMA_OFF) {
213                 it821x_program_udma(pair, itdev->udma[1-unit]);
214                 it821x_program(pair, itdev->pio[1-unit]);
215         }
216         /*
217          *      Reprogram the UDMA/PIO of our drive for the switch.
218          *      MWDMA will be dealt with by the dma switcher
219          */
220         if(itdev->udma[unit] != UDMA_OFF) {
221                 it821x_program_udma(drive, itdev->udma[unit]);
222                 it821x_program(drive, itdev->pio[unit]);
223         }
224 }
225
226 /**
227  *      it821x_ratemask -       Compute available modes
228  *      @drive: IDE drive
229  *
230  *      Compute the available speeds for the devices on the interface. This
231  *      is all modes to ATA133 clipped by drive cable setup.
232  */
233
234 static u8 it821x_ratemask (ide_drive_t *drive)
235 {
236         u8 mode = 4;
237         if (!eighty_ninty_three(drive))
238                 mode = min(mode, (u8)1);
239         return mode;
240 }
241
242 /**
243  *      it821x_tuneproc -       tune a drive
244  *      @drive: drive to tune
245  *      @mode_wanted: the target operating mode
246  *
247  *      Load the timing settings for this device mode into the
248  *      controller. By the time we are called the mode has been
249  *      modified as neccessary to handle the absence of seperate
250  *      master/slave timers for MWDMA/PIO.
251  *
252  *      This code is only used in pass through mode.
253  */
254
255 static void it821x_tuneproc (ide_drive_t *drive, byte mode_wanted)
256 {
257         ide_hwif_t *hwif        = drive->hwif;
258         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
259         int unit = drive->select.b.unit;
260
261         /* Spec says 89 ref driver uses 88 */
262         static u16 pio[]        = { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
263         static u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
264
265         if(itdev->smart)
266                 return;
267
268         /* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
269         itdev->want[unit][1] = pio_want[mode_wanted];
270         itdev->want[unit][0] = 1;       /* PIO is lowest priority */
271         itdev->pio[unit] = pio[mode_wanted];
272         it821x_clock_strategy(drive);
273         it821x_program(drive, itdev->pio[unit]);
274 }
275
276 /**
277  *      it821x_tune_mwdma       -       tune a channel for MWDMA
278  *      @drive: drive to set up
279  *      @mode_wanted: the target operating mode
280  *
281  *      Load the timing settings for this device mode into the
282  *      controller when doing MWDMA in pass through mode. The caller
283  *      must manage the whole lack of per device MWDMA/PIO timings and
284  *      the shared MWDMA/PIO timing register.
285  */
286
287 static void it821x_tune_mwdma (ide_drive_t *drive, byte mode_wanted)
288 {
289         ide_hwif_t *hwif        = drive->hwif;
290         struct it821x_dev *itdev = (void *)ide_get_hwifdata(hwif);
291         int unit = drive->select.b.unit;
292         int channel = hwif->channel;
293         u8 conf;
294
295         static u16 dma[]        = { 0x8866, 0x3222, 0x3121 };
296         static u8 mwdma_want[]  = { ATA_ANY, ATA_66, ATA_ANY };
297
298         itdev->want[unit][1] = mwdma_want[mode_wanted];
299         itdev->want[unit][0] = 2;       /* MWDMA is low priority */
300         itdev->mwdma[unit] = dma[mode_wanted];
301         itdev->udma[unit] = UDMA_OFF;
302
303         /* UDMA bits off - Revision 0x10 do them in pairs */
304         pci_read_config_byte(hwif->pci_dev, 0x50, &conf);
305         if(itdev->timing10)
306                 conf |= channel ? 0x60: 0x18;
307         else
308                 conf |= 1 << (3 + 2 * channel + unit);
309         pci_write_config_byte(hwif->pci_dev, 0x50, conf);
310
311         it821x_clock_strategy(drive);
312         /* FIXME: do we need to program this ? */
313         /* it821x_program(drive, itdev->mwdma[unit]); */
314 }
315
316 /**
317  *      it821x_tune_udma        -       tune a channel for UDMA
318  *      @drive: drive to set up
319  *      @mode_wanted: the target operating mode
320  *
321  *      Load the timing settings for this device mode into the
322  *      controller when doing UDMA modes in pass through.
323  */
324
325 static void it821x_tune_udma (ide_drive_t *drive, byte mode_wanted)
326 {
327         ide_hwif_t *hwif        = drive->hwif;
328         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
329         int unit = drive->select.b.unit;
330         int channel = hwif->channel;
331         u8 conf;
332
333         static u16 udma[]       = { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
334         static u8 udma_want[]   = { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
335
336         itdev->want[unit][1] = udma_want[mode_wanted];
337         itdev->want[unit][0] = 3;       /* UDMA is high priority */
338         itdev->mwdma[unit] = MWDMA_OFF;
339         itdev->udma[unit] = udma[mode_wanted];
340         if(mode_wanted >= 5)
341                 itdev->udma[unit] |= 0x8080;    /* UDMA 5/6 select on */
342
343         /* UDMA on. Again revision 0x10 must do the pair */
344         pci_read_config_byte(hwif->pci_dev, 0x50, &conf);
345         if(itdev->timing10)
346                 conf &= channel ? 0x9F: 0xE7;
347         else
348                 conf &= ~ (1 << (3 + 2 * channel + unit));
349         pci_write_config_byte(hwif->pci_dev, 0x50, conf);
350
351         it821x_clock_strategy(drive);
352         it821x_program_udma(drive, itdev->udma[unit]);
353
354 }
355
356 /**
357  *      config_it821x_chipset_for_pio   -       set drive timings
358  *      @drive: drive to tune
359  *      @speed we want
360  *
361  *      Compute the best pio mode we can for a given device. We must
362  *      pick a speed that does not cause problems with the other device
363  *      on the cable.
364  */
365
366 static void config_it821x_chipset_for_pio (ide_drive_t *drive, byte set_speed)
367 {
368         u8 unit = drive->select.b.unit;
369         ide_hwif_t *hwif = drive->hwif;
370         ide_drive_t *pair = &hwif->drives[1-unit];
371         u8 speed = 0, set_pio   = ide_get_best_pio_mode(drive, 255, 5, NULL);
372         u8 pair_pio;
373
374         /* We have to deal with this mess in pairs */
375         if(pair != NULL) {
376                 pair_pio = ide_get_best_pio_mode(pair, 255, 5, NULL);
377                 /* Trim PIO to the slowest of the master/slave */
378                 if(pair_pio < set_pio)
379                         set_pio = pair_pio;
380         }
381         it821x_tuneproc(drive, set_pio);
382         speed = XFER_PIO_0 + set_pio;
383         /* XXX - We trim to the lowest of the pair so the other drive
384            will always be fine at this point until we do hotplug passthru */
385
386         if (set_speed)
387                 (void) ide_config_drive_speed(drive, speed);
388 }
389
390 /**
391  *      it821x_dma_read -       DMA hook
392  *      @drive: drive for DMA
393  *
394  *      The IT821x has a single timing register for MWDMA and for PIO
395  *      operations. As we flip back and forth we have to reload the
396  *      clock. In addition the rev 0x10 device only works if the same
397  *      timing value is loaded into the master and slave UDMA clock
398  *      so we must also reload that.
399  *
400  *      FIXME: we could figure out in advance if we need to do reloads
401  */
402
403 static void it821x_dma_start(ide_drive_t *drive)
404 {
405         ide_hwif_t *hwif = drive->hwif;
406         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
407         int unit = drive->select.b.unit;
408         if(itdev->mwdma[unit] != MWDMA_OFF)
409                 it821x_program(drive, itdev->mwdma[unit]);
410         else if(itdev->udma[unit] != UDMA_OFF && itdev->timing10)
411                 it821x_program_udma(drive, itdev->udma[unit]);
412         ide_dma_start(drive);
413 }
414
415 /**
416  *      it821x_dma_write        -       DMA hook
417  *      @drive: drive for DMA stop
418  *
419  *      The IT821x has a single timing register for MWDMA and for PIO
420  *      operations. As we flip back and forth we have to reload the
421  *      clock.
422  */
423
424 static int it821x_dma_end(ide_drive_t *drive)
425 {
426         ide_hwif_t *hwif = drive->hwif;
427         int unit = drive->select.b.unit;
428         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
429         int ret = __ide_dma_end(drive);
430         if(itdev->mwdma[unit] != MWDMA_OFF)
431                 it821x_program(drive, itdev->pio[unit]);
432         return ret;
433 }
434
435
436 /**
437  *      it821x_tune_chipset     -       set controller timings
438  *      @drive: Drive to set up
439  *      @xferspeed: speed we want to achieve
440  *
441  *      Tune the ITE chipset for the desired mode. If we can't achieve
442  *      the desired mode then tune for a lower one, but ultimately
443  *      make the thing work.
444  */
445
446 static int it821x_tune_chipset (ide_drive_t *drive, byte xferspeed)
447 {
448
449         ide_hwif_t *hwif        = drive->hwif;
450         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
451         u8 speed                = ide_rate_filter(it821x_ratemask(drive), xferspeed);
452
453         if(!itdev->smart) {
454                 switch(speed) {
455                         case XFER_PIO_4:
456                         case XFER_PIO_3:
457                         case XFER_PIO_2:
458                         case XFER_PIO_1:
459                         case XFER_PIO_0:
460                                 it821x_tuneproc(drive, (speed - XFER_PIO_0));
461                                 break;
462                         /* MWDMA tuning is really hard because our MWDMA and PIO
463                            timings are kept in the same place. We can switch in the
464                            host dma on/off callbacks */
465                         case XFER_MW_DMA_2:
466                         case XFER_MW_DMA_1:
467                         case XFER_MW_DMA_0:
468                                 it821x_tune_mwdma(drive, (speed - XFER_MW_DMA_0));
469                                 break;
470                         case XFER_UDMA_6:
471                         case XFER_UDMA_5:
472                         case XFER_UDMA_4:
473                         case XFER_UDMA_3:
474                         case XFER_UDMA_2:
475                         case XFER_UDMA_1:
476                         case XFER_UDMA_0:
477                                 it821x_tune_udma(drive, (speed - XFER_UDMA_0));
478                                 break;
479                         default:
480                                 return 1;
481                 }
482         }
483         /*
484          *      In smart mode the clocking is done by the host controller
485          *      snooping the mode we picked. The rest of it is not our problem
486          */
487         return ide_config_drive_speed(drive, speed);
488 }
489
490 /**
491  *      config_chipset_for_dma  -       configure for DMA
492  *      @drive: drive to configure
493  *
494  *      Called by the IDE layer when it wants the timings set up.
495  */
496
497 static int config_chipset_for_dma (ide_drive_t *drive)
498 {
499         u8 speed        = ide_dma_speed(drive, it821x_ratemask(drive));
500
501         if (speed) {
502                 config_it821x_chipset_for_pio(drive, 0);
503                 it821x_tune_chipset(drive, speed);
504
505                 return ide_dma_enable(drive);
506         }
507
508         return 0;
509 }
510
511 /**
512  *      it821x_configure_drive_for_dma  -       set up for DMA transfers
513  *      @drive: drive we are going to set up
514  *
515  *      Set up the drive for DMA, tune the controller and drive as
516  *      required. If the drive isn't suitable for DMA or we hit
517  *      other problems then we will drop down to PIO and set up
518  *      PIO appropriately
519  */
520
521 static int it821x_config_drive_for_dma (ide_drive_t *drive)
522 {
523         ide_hwif_t *hwif        = drive->hwif;
524
525         if (ide_use_dma(drive)) {
526                 if (config_chipset_for_dma(drive))
527                         return hwif->ide_dma_on(drive);
528         }
529         config_it821x_chipset_for_pio(drive, 1);
530         return hwif->ide_dma_off_quietly(drive);
531 }
532
533 /**
534  *      ata66_it821x    -       check for 80 pin cable
535  *      @hwif: interface to check
536  *
537  *      Check for the presence of an ATA66 capable cable on the
538  *      interface. Problematic as it seems some cards don't have
539  *      the needed logic onboard.
540  */
541
542 static unsigned int __devinit ata66_it821x(ide_hwif_t *hwif)
543 {
544         /* The reference driver also only does disk side */
545         return 1;
546 }
547
548 /**
549  *      it821x_fixup    -       post init callback
550  *      @hwif: interface
551  *
552  *      This callback is run after the drives have been probed but
553  *      before anything gets attached. It allows drivers to do any
554  *      final tuning that is needed, or fixups to work around bugs.
555  */
556
557 static void __devinit it821x_fixups(ide_hwif_t *hwif)
558 {
559         struct it821x_dev *itdev = ide_get_hwifdata(hwif);
560         int i;
561
562         if(!itdev->smart) {
563                 /*
564                  *      If we are in pass through mode then not much
565                  *      needs to be done, but we do bother to clear the
566                  *      IRQ mask as we may well be in PIO (eg rev 0x10)
567                  *      for now and we know unmasking is safe on this chipset.
568                  */
569                 for (i = 0; i < 2; i++) {
570                         ide_drive_t *drive = &hwif->drives[i];
571                         if(drive->present)
572                                 drive->unmask = 1;
573                 }
574                 return;
575         }
576         /*
577          *      Perform fixups on smart mode. We need to "lose" some
578          *      capabilities the firmware lacks but does not filter, and
579          *      also patch up some capability bits that it forgets to set
580          *      in RAID mode.
581          */
582
583         for(i = 0; i < 2; i++) {
584                 ide_drive_t *drive = &hwif->drives[i];
585                 struct hd_driveid *id;
586                 u16 *idbits;
587
588                 if(!drive->present)
589                         continue;
590                 id = drive->id;
591                 idbits = (u16 *)drive->id;
592
593                 /* Check for RAID v native */
594                 if(strstr(id->model, "Integrated Technology Express")) {
595                         /* In raid mode the ident block is slightly buggy
596                            We need to set the bits so that the IDE layer knows
597                            LBA28. LBA48 and DMA ar valid */
598                         id->capability |= 3;            /* LBA28, DMA */
599                         id->command_set_2 |= 0x0400;    /* LBA48 valid */
600                         id->cfs_enable_2 |= 0x0400;     /* LBA48 on */
601                         /* Reporting logic */
602                         printk(KERN_INFO "%s: IT8212 %sRAID %d volume",
603                                 drive->name,
604                                 idbits[147] ? "Bootable ":"",
605                                 idbits[129]);
606                                 if(idbits[129] != 1)
607                                         printk("(%dK stripe)", idbits[146]);
608                                 printk(".\n");
609                         /* Now the core code will have wrongly decided no DMA
610                            so we need to fix this */
611                         hwif->ide_dma_off_quietly(drive);
612 #ifdef CONFIG_IDEDMA_ONLYDISK
613                         if (drive->media == ide_disk)
614 #endif
615                                 hwif->ide_dma_check(drive);
616                 } else {
617                         /* Non RAID volume. Fixups to stop the core code
618                            doing unsupported things */
619                         id->field_valid &= 1;
620                         id->queue_depth = 0;
621                         id->command_set_1 = 0;
622                         id->command_set_2 &= 0xC400;
623                         id->cfsse &= 0xC000;
624                         id->cfs_enable_1 = 0;
625                         id->cfs_enable_2 &= 0xC400;
626                         id->csf_default &= 0xC000;
627                         id->word127 = 0;
628                         id->dlf = 0;
629                         id->csfo = 0;
630                         id->cfa_power = 0;
631                         printk(KERN_INFO "%s: Performing identify fixups.\n",
632                                 drive->name);
633                 }
634         }
635
636 }
637
638 /**
639  *      init_hwif_it821x        -       set up hwif structs
640  *      @hwif: interface to set up
641  *
642  *      We do the basic set up of the interface structure. The IT8212
643  *      requires several custom handlers so we override the default
644  *      ide DMA handlers appropriately
645  */
646
647 static void __devinit init_hwif_it821x(ide_hwif_t *hwif)
648 {
649         struct it821x_dev *idev = kzalloc(sizeof(struct it821x_dev), GFP_KERNEL);
650         u8 conf;
651
652         if(idev == NULL) {
653                 printk(KERN_ERR "it821x: out of memory, falling back to legacy behaviour.\n");
654                 goto fallback;
655         }
656         ide_set_hwifdata(hwif, idev);
657
658         hwif->atapi_dma = 1;
659
660         pci_read_config_byte(hwif->pci_dev, 0x50, &conf);
661         if(conf & 1) {
662                 idev->smart = 1;
663                 hwif->atapi_dma = 0;
664                 /* Long I/O's although allowed in LBA48 space cause the
665                    onboard firmware to enter the twighlight zone */
666                 hwif->rqsize = 256;
667         }
668
669         /* Pull the current clocks from 0x50 also */
670         if (conf & (1 << (1 + hwif->channel)))
671                 idev->clock_mode = ATA_50;
672         else
673                 idev->clock_mode = ATA_66;
674
675         idev->want[0][1] = ATA_ANY;
676         idev->want[1][1] = ATA_ANY;
677
678         /*
679          *      Not in the docs but according to the reference driver
680          *      this is neccessary.
681          */
682
683         pci_read_config_byte(hwif->pci_dev, 0x08, &conf);
684         if(conf == 0x10) {
685                 idev->timing10 = 1;
686                 hwif->atapi_dma = 0;
687                 if(!idev->smart)
688                         printk(KERN_WARNING "it821x: Revision 0x10, workarounds activated.\n");
689         }
690
691         hwif->speedproc = &it821x_tune_chipset;
692         hwif->tuneproc  = &it821x_tuneproc;
693
694         /* MWDMA/PIO clock switching for pass through mode */
695         if(!idev->smart) {
696                 hwif->dma_start = &it821x_dma_start;
697                 hwif->ide_dma_end = &it821x_dma_end;
698         }
699
700         hwif->drives[0].autotune = 1;
701         hwif->drives[1].autotune = 1;
702
703         if (!hwif->dma_base)
704                 goto fallback;
705
706         hwif->ultra_mask = 0x7f;
707         hwif->mwdma_mask = 0x07;
708         hwif->swdma_mask = 0x07;
709
710         hwif->ide_dma_check = &it821x_config_drive_for_dma;
711         if (!(hwif->udma_four))
712                 hwif->udma_four = ata66_it821x(hwif);
713
714         /*
715          *      The BIOS often doesn't set up DMA on this controller
716          *      so we always do it.
717          */
718
719         hwif->autodma = 1;
720         hwif->drives[0].autodma = hwif->autodma;
721         hwif->drives[1].autodma = hwif->autodma;
722         return;
723 fallback:
724         hwif->autodma = 0;
725         return;
726 }
727
728 static void __devinit it8212_disable_raid(struct pci_dev *dev)
729 {
730         /* Reset local CPU, and set BIOS not ready */
731         pci_write_config_byte(dev, 0x5E, 0x01);
732
733         /* Set to bypass mode, and reset PCI bus */
734         pci_write_config_byte(dev, 0x50, 0x00);
735         pci_write_config_word(dev, PCI_COMMAND,
736                               PCI_COMMAND_PARITY | PCI_COMMAND_IO |
737                               PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
738         pci_write_config_word(dev, 0x40, 0xA0F3);
739
740         pci_write_config_dword(dev,0x4C, 0x02040204);
741         pci_write_config_byte(dev, 0x42, 0x36);
742         pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x20);
743 }
744
745 static unsigned int __devinit init_chipset_it821x(struct pci_dev *dev, const char *name)
746 {
747         u8 conf;
748         static char *mode[2] = { "pass through", "smart" };
749
750         /* Force the card into bypass mode if so requested */
751         if (it8212_noraid) {
752                 printk(KERN_INFO "it8212: forcing bypass mode.\n");
753                 it8212_disable_raid(dev);
754         }
755         pci_read_config_byte(dev, 0x50, &conf);
756         printk(KERN_INFO "it821x: controller in %s mode.\n", mode[conf & 1]);
757         return 0;
758 }
759
760
761 #define DECLARE_ITE_DEV(name_str)                       \
762         {                                               \
763                 .name           = name_str,             \
764                 .init_chipset   = init_chipset_it821x,  \
765                 .init_hwif      = init_hwif_it821x,     \
766                 .channels       = 2,                    \
767                 .autodma        = AUTODMA,              \
768                 .bootable       = ON_BOARD,             \
769                 .fixup          = it821x_fixups         \
770         }
771
772 static ide_pci_device_t it821x_chipsets[] __devinitdata = {
773         /* 0 */ DECLARE_ITE_DEV("IT8212"),
774 };
775
776 /**
777  *      it821x_init_one -       pci layer discovery entry
778  *      @dev: PCI device
779  *      @id: ident table entry
780  *
781  *      Called by the PCI code when it finds an ITE821x controller.
782  *      We then use the IDE PCI generic helper to do most of the work.
783  */
784
785 static int __devinit it821x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
786 {
787         ide_setup_pci_device(dev, &it821x_chipsets[id->driver_data]);
788         return 0;
789 }
790
791 static struct pci_device_id it821x_pci_tbl[] = {
792         { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_8211,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
793         { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_8212,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
794         { 0, },
795 };
796
797 MODULE_DEVICE_TABLE(pci, it821x_pci_tbl);
798
799 static struct pci_driver driver = {
800         .name           = "ITE821x IDE",
801         .id_table       = it821x_pci_tbl,
802         .probe          = it821x_init_one,
803 };
804
805 static int __init it821x_ide_init(void)
806 {
807         return ide_pci_register_driver(&driver);
808 }
809
810 module_init(it821x_ide_init);
811
812 module_param_named(noraid, it8212_noraid, int, S_IRUGO);
813 MODULE_PARM_DESC(it8212_noraid, "Force card into bypass mode");
814
815 MODULE_AUTHOR("Alan Cox");
816 MODULE_DESCRIPTION("PCI driver module for the ITE 821x");
817 MODULE_LICENSE("GPL");