1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66#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,
79 timing10:1;
80 u8 clock_mode;
81 u8 want[2][2];
82
83
84 u16 pio[2];
85 u16 mwdma[2];
86 u16 udma[2];
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
98
99
100
101
102
103static int it8212_noraid;
104
105
106
107
108
109
110
111
112
113
114static void it821x_program(ide_drive_t *drive, u16 timing)
115{
116 ide_hwif_t *hwif = drive->hwif;
117 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
118 int channel = hwif->channel;
119 u8 conf;
120
121
122 if(itdev->clock_mode == ATA_66)
123 conf = timing >> 8;
124 else
125 conf = timing & 0xFF;
126 pci_write_config_byte(hwif->pci_dev, 0x54 + 4 * channel, conf);
127}
128
129
130
131
132
133
134
135
136
137
138static void it821x_program_udma(ide_drive_t *drive, u16 timing)
139{
140 ide_hwif_t *hwif = drive->hwif;
141 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
142 int channel = hwif->channel;
143 int unit = drive->select.b.unit;
144 u8 conf;
145
146
147 if(itdev->clock_mode == ATA_66)
148 conf = timing >> 8;
149 else
150 conf = timing & 0xFF;
151 if(itdev->timing10 == 0)
152 pci_write_config_byte(hwif->pci_dev, 0x56 + 4 * channel + unit, conf);
153 else {
154 pci_write_config_byte(hwif->pci_dev, 0x56 + 4 * channel, conf);
155 pci_write_config_byte(hwif->pci_dev, 0x56 + 4 * channel + 1, conf);
156 }
157}
158
159
160
161
162
163
164
165
166
167static void it821x_clock_strategy(ide_drive_t *drive)
168{
169 ide_hwif_t *hwif = drive->hwif;
170 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
171
172 u8 unit = drive->select.b.unit;
173 ide_drive_t *pair = &hwif->drives[1-unit];
174
175 int clock, altclock;
176 u8 v;
177 int sel = 0;
178
179 if(itdev->want[0][0] > itdev->want[1][0]) {
180 clock = itdev->want[0][1];
181 altclock = itdev->want[1][1];
182 } else {
183 clock = itdev->want[1][1];
184 altclock = itdev->want[0][1];
185 }
186
187
188
189
190
191 if (clock == ATA_ANY)
192 clock = altclock;
193
194
195 if(clock == ATA_ANY)
196 return;
197
198 if(clock == itdev->clock_mode)
199 return;
200
201
202 if(clock == ATA_66)
203 itdev->clock_mode = ATA_66;
204 else {
205 itdev->clock_mode = ATA_50;
206 sel = 1;
207 }
208 pci_read_config_byte(hwif->pci_dev, 0x50, &v);
209 v &= ~(1 << (1 + hwif->channel));
210 v |= sel << (1 + hwif->channel);
211 pci_write_config_byte(hwif->pci_dev, 0x50, v);
212
213
214
215
216
217 if(pair && itdev->udma[1-unit] != UDMA_OFF) {
218 it821x_program_udma(pair, itdev->udma[1-unit]);
219 it821x_program(pair, itdev->pio[1-unit]);
220 }
221
222
223
224
225 if(itdev->udma[unit] != UDMA_OFF) {
226 it821x_program_udma(drive, itdev->udma[unit]);
227 it821x_program(drive, itdev->pio[unit]);
228 }
229}
230
231
232
233
234
235
236
237
238
239
240static void it821x_set_pio_mode(ide_drive_t *drive, const u8 pio)
241{
242 ide_hwif_t *hwif = drive->hwif;
243 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
244 int unit = drive->select.b.unit;
245 ide_drive_t *pair = &hwif->drives[1 - unit];
246 u8 set_pio = pio;
247
248
249 static u16 pio_timings[]= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
250 static u8 pio_want[] = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
251
252
253
254
255
256
257 if (pair) {
258 u8 pair_pio = ide_get_best_pio_mode(pair, 255, 4);
259
260 if (pair_pio < set_pio)
261 set_pio = pair_pio;
262 }
263
264
265 itdev->want[unit][1] = pio_want[set_pio];
266 itdev->want[unit][0] = 1;
267 itdev->pio[unit] = pio_timings[set_pio];
268 it821x_clock_strategy(drive);
269 it821x_program(drive, itdev->pio[unit]);
270}
271
272
273
274
275
276
277
278
279
280
281
282
283static void it821x_tune_mwdma (ide_drive_t *drive, byte mode_wanted)
284{
285 ide_hwif_t *hwif = drive->hwif;
286 struct it821x_dev *itdev = (void *)ide_get_hwifdata(hwif);
287 int unit = drive->select.b.unit;
288 int channel = hwif->channel;
289 u8 conf;
290
291 static u16 dma[] = { 0x8866, 0x3222, 0x3121 };
292 static u8 mwdma_want[] = { ATA_ANY, ATA_66, ATA_ANY };
293
294 itdev->want[unit][1] = mwdma_want[mode_wanted];
295 itdev->want[unit][0] = 2;
296 itdev->mwdma[unit] = dma[mode_wanted];
297 itdev->udma[unit] = UDMA_OFF;
298
299
300 pci_read_config_byte(hwif->pci_dev, 0x50, &conf);
301 if(itdev->timing10)
302 conf |= channel ? 0x60: 0x18;
303 else
304 conf |= 1 << (3 + 2 * channel + unit);
305 pci_write_config_byte(hwif->pci_dev, 0x50, conf);
306
307 it821x_clock_strategy(drive);
308
309
310}
311
312
313
314
315
316
317
318
319
320
321static void it821x_tune_udma (ide_drive_t *drive, byte mode_wanted)
322{
323 ide_hwif_t *hwif = drive->hwif;
324 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
325 int unit = drive->select.b.unit;
326 int channel = hwif->channel;
327 u8 conf;
328
329 static u16 udma[] = { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
330 static u8 udma_want[] = { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
331
332 itdev->want[unit][1] = udma_want[mode_wanted];
333 itdev->want[unit][0] = 3;
334 itdev->mwdma[unit] = MWDMA_OFF;
335 itdev->udma[unit] = udma[mode_wanted];
336 if(mode_wanted >= 5)
337 itdev->udma[unit] |= 0x8080;
338
339
340 pci_read_config_byte(hwif->pci_dev, 0x50, &conf);
341 if(itdev->timing10)
342 conf &= channel ? 0x9F: 0xE7;
343 else
344 conf &= ~ (1 << (3 + 2 * channel + unit));
345 pci_write_config_byte(hwif->pci_dev, 0x50, conf);
346
347 it821x_clock_strategy(drive);
348 it821x_program_udma(drive, itdev->udma[unit]);
349
350}
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365static void it821x_dma_start(ide_drive_t *drive)
366{
367 ide_hwif_t *hwif = drive->hwif;
368 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
369 int unit = drive->select.b.unit;
370 if(itdev->mwdma[unit] != MWDMA_OFF)
371 it821x_program(drive, itdev->mwdma[unit]);
372 else if(itdev->udma[unit] != UDMA_OFF && itdev->timing10)
373 it821x_program_udma(drive, itdev->udma[unit]);
374 ide_dma_start(drive);
375}
376
377
378
379
380
381
382
383
384
385
386static int it821x_dma_end(ide_drive_t *drive)
387{
388 ide_hwif_t *hwif = drive->hwif;
389 int unit = drive->select.b.unit;
390 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
391 int ret = __ide_dma_end(drive);
392 if(itdev->mwdma[unit] != MWDMA_OFF)
393 it821x_program(drive, itdev->pio[unit]);
394 return ret;
395}
396
397
398
399
400
401
402
403
404
405static void it821x_set_dma_mode(ide_drive_t *drive, const u8 speed)
406{
407
408
409
410
411
412 if (speed >= XFER_UDMA_0 && speed <= XFER_UDMA_6)
413 it821x_tune_udma(drive, speed - XFER_UDMA_0);
414 else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
415 it821x_tune_mwdma(drive, speed - XFER_MW_DMA_0);
416}
417
418
419
420
421
422
423
424
425
426
427static u8 __devinit ata66_it821x(ide_hwif_t *hwif)
428{
429
430 return ATA_CBL_PATA80;
431}
432
433
434
435
436
437
438
439
440
441
442static void __devinit it821x_fixups(ide_hwif_t *hwif)
443{
444 struct it821x_dev *itdev = ide_get_hwifdata(hwif);
445 int i;
446
447 if(!itdev->smart) {
448
449
450
451
452
453
454 for (i = 0; i < 2; i++) {
455 ide_drive_t *drive = &hwif->drives[i];
456 if(drive->present)
457 drive->unmask = 1;
458 }
459 return;
460 }
461
462
463
464
465
466
467
468 for(i = 0; i < 2; i++) {
469 ide_drive_t *drive = &hwif->drives[i];
470 struct hd_driveid *id;
471 u16 *idbits;
472
473 if(!drive->present)
474 continue;
475 id = drive->id;
476 idbits = (u16 *)drive->id;
477
478
479 if(strstr(id->model, "Integrated Technology Express")) {
480
481
482
483 id->capability |= 3;
484 id->command_set_2 |= 0x0400;
485 id->cfs_enable_2 |= 0x0400;
486
487 printk(KERN_INFO "%s: IT8212 %sRAID %d volume",
488 drive->name,
489 idbits[147] ? "Bootable ":"",
490 idbits[129]);
491 if(idbits[129] != 1)
492 printk("(%dK stripe)", idbits[146]);
493 printk(".\n");
494 } else {
495
496
497 id->field_valid &= 3;
498 id->queue_depth = 0;
499 id->command_set_1 = 0;
500 id->command_set_2 &= 0xC400;
501 id->cfsse &= 0xC000;
502 id->cfs_enable_1 = 0;
503 id->cfs_enable_2 &= 0xC400;
504 id->csf_default &= 0xC000;
505 id->word127 = 0;
506 id->dlf = 0;
507 id->csfo = 0;
508 id->cfa_power = 0;
509 printk(KERN_INFO "%s: Performing identify fixups.\n",
510 drive->name);
511 }
512
513
514
515
516
517
518 if (id->capability & 1) {
519 id->dma_mword |= 0x0101;
520 drive->current_speed = XFER_MW_DMA_0;
521 }
522 }
523
524}
525
526
527
528
529
530
531
532
533
534
535static void __devinit init_hwif_it821x(ide_hwif_t *hwif)
536{
537 struct it821x_dev *idev = kzalloc(sizeof(struct it821x_dev), GFP_KERNEL);
538 u8 conf;
539
540 if (idev == NULL) {
541 printk(KERN_ERR "it821x: out of memory, falling back to legacy behaviour.\n");
542 return;
543 }
544
545 ide_set_hwifdata(hwif, idev);
546
547 pci_read_config_byte(hwif->pci_dev, 0x50, &conf);
548 if (conf & 1) {
549 idev->smart = 1;
550 hwif->host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
551
552
553 hwif->rqsize = 256;
554 }
555
556
557 if (conf & (1 << (1 + hwif->channel)))
558 idev->clock_mode = ATA_50;
559 else
560 idev->clock_mode = ATA_66;
561
562 idev->want[0][1] = ATA_ANY;
563 idev->want[1][1] = ATA_ANY;
564
565
566
567
568
569
570 pci_read_config_byte(hwif->pci_dev, 0x08, &conf);
571 if (conf == 0x10) {
572 idev->timing10 = 1;
573 hwif->host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
574 if (idev->smart == 0)
575 printk(KERN_WARNING "it821x: Revision 0x10, workarounds activated.\n");
576 }
577
578 if (idev->smart == 0) {
579 hwif->set_pio_mode = &it821x_set_pio_mode;
580 hwif->set_dma_mode = &it821x_set_dma_mode;
581
582
583 hwif->dma_start = &it821x_dma_start;
584 hwif->ide_dma_end = &it821x_dma_end;
585 } else
586 hwif->host_flags |= IDE_HFLAG_NO_SET_MODE;
587
588 if (hwif->dma_base == 0)
589 return;
590
591 hwif->ultra_mask = ATA_UDMA6;
592 hwif->mwdma_mask = ATA_MWDMA2;
593
594 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
595 hwif->cbl = ata66_it821x(hwif);
596}
597
598static void __devinit it8212_disable_raid(struct pci_dev *dev)
599{
600
601 pci_write_config_byte(dev, 0x5E, 0x01);
602
603
604 pci_write_config_byte(dev, 0x50, 0x00);
605 pci_write_config_word(dev, PCI_COMMAND,
606 PCI_COMMAND_PARITY | PCI_COMMAND_IO |
607 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
608 pci_write_config_word(dev, 0x40, 0xA0F3);
609
610 pci_write_config_dword(dev,0x4C, 0x02040204);
611 pci_write_config_byte(dev, 0x42, 0x36);
612 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x20);
613}
614
615static unsigned int __devinit init_chipset_it821x(struct pci_dev *dev, const char *name)
616{
617 u8 conf;
618 static char *mode[2] = { "pass through", "smart" };
619
620
621 if (it8212_noraid) {
622 printk(KERN_INFO "it8212: forcing bypass mode.\n");
623 it8212_disable_raid(dev);
624 }
625 pci_read_config_byte(dev, 0x50, &conf);
626 printk(KERN_INFO "it821x: controller in %s mode.\n", mode[conf & 1]);
627 return 0;
628}
629
630
631#define DECLARE_ITE_DEV(name_str) \
632 { \
633 .name = name_str, \
634 .init_chipset = init_chipset_it821x, \
635 .init_hwif = init_hwif_it821x, \
636 .fixup = it821x_fixups, \
637 .host_flags = IDE_HFLAG_BOOTABLE, \
638 .pio_mask = ATA_PIO4, \
639 }
640
641static const struct ide_port_info it821x_chipsets[] __devinitdata = {
642 DECLARE_ITE_DEV("IT8212"),
643};
644
645
646
647
648
649
650
651
652
653
654static int __devinit it821x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
655{
656 return ide_setup_pci_device(dev, &it821x_chipsets[id->driver_data]);
657}
658
659static const struct pci_device_id it821x_pci_tbl[] = {
660 { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), 0 },
661 { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), 0 },
662 { 0, },
663};
664
665MODULE_DEVICE_TABLE(pci, it821x_pci_tbl);
666
667static struct pci_driver driver = {
668 .name = "ITE821x IDE",
669 .id_table = it821x_pci_tbl,
670 .probe = it821x_init_one,
671};
672
673static int __init it821x_ide_init(void)
674{
675 return ide_pci_register_driver(&driver);
676}
677
678module_init(it821x_ide_init);
679
680module_param_named(noraid, it8212_noraid, int, S_IRUGO);
681MODULE_PARM_DESC(it8212_noraid, "Force card into bypass mode");
682
683MODULE_AUTHOR("Alan Cox");
684MODULE_DESCRIPTION("PCI driver module for the ITE 821x");
685MODULE_LICENSE("GPL");
686