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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99#define CMD640_PREFETCH_MASKS 1
100
101
102
103#include <linux/types.h>
104#include <linux/kernel.h>
105#include <linux/delay.h>
106#include <linux/ide.h>
107#include <linux/init.h>
108#include <linux/module.h>
109
110#include <asm/io.h>
111
112#define DRV_NAME "cmd640"
113
114static bool cmd640_vlb;
115
116
117
118
119
120#define VID 0x00
121#define DID 0x02
122#define PCMD 0x04
123#define PCMD_ENA 0x01
124#define PSTTS 0x06
125#define REVID 0x08
126#define PROGIF 0x09
127#define SUBCL 0x0a
128#define BASCL 0x0b
129#define BaseA0 0x10
130#define BaseA1 0x14
131#define BaseA2 0x18
132#define BaseA3 0x1c
133#define INTLINE 0x3c
134#define INPINE 0x3d
135
136#define CFR 0x50
137#define CFR_DEVREV 0x03
138#define CFR_IDE01INTR 0x04
139#define CFR_DEVID 0x18
140#define CFR_AT_VESA_078h 0x20
141#define CFR_DSA1 0x40
142#define CFR_DSA0 0x80
143
144#define CNTRL 0x51
145#define CNTRL_DIS_RA0 0x40
146#define CNTRL_DIS_RA1 0x80
147#define CNTRL_ENA_2ND 0x08
148
149#define CMDTIM 0x52
150#define ARTTIM0 0x53
151#define DRWTIM0 0x54
152#define ARTTIM1 0x55
153#define DRWTIM1 0x56
154#define ARTTIM23 0x57
155#define ARTTIM23_DIS_RA2 0x04
156#define ARTTIM23_DIS_RA3 0x08
157#define ARTTIM23_IDE23INTR 0x10
158#define DRWTIM23 0x58
159#define BRST 0x59
160
161
162
163
164static u8 prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
165static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
166
167#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
168
169static u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
170static u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23};
171
172
173
174
175
176static u8 setup_counts[4] = {4, 4, 4, 4};
177static u8 active_counts[4] = {16, 16, 16, 16};
178static u8 recovery_counts[4] = {16, 16, 16, 16};
179
180#endif
181
182static DEFINE_SPINLOCK(cmd640_lock);
183
184
185
186
187static unsigned int cmd640_key;
188static void (*__put_cmd640_reg)(u16 reg, u8 val);
189static u8 (*__get_cmd640_reg)(u16 reg);
190
191
192
193
194static unsigned int cmd640_chip_version;
195
196
197
198
199
200
201
202
203
204static void put_cmd640_reg_pci1(u16 reg, u8 val)
205{
206 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
207 outb_p(val, (reg & 3) | 0xcfc);
208}
209
210static u8 get_cmd640_reg_pci1(u16 reg)
211{
212 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
213 return inb_p((reg & 3) | 0xcfc);
214}
215
216
217
218static void put_cmd640_reg_pci2(u16 reg, u8 val)
219{
220 outb_p(0x10, 0xcf8);
221 outb_p(val, cmd640_key + reg);
222 outb_p(0, 0xcf8);
223}
224
225static u8 get_cmd640_reg_pci2(u16 reg)
226{
227 u8 b;
228
229 outb_p(0x10, 0xcf8);
230 b = inb_p(cmd640_key + reg);
231 outb_p(0, 0xcf8);
232 return b;
233}
234
235
236
237static void put_cmd640_reg_vlb(u16 reg, u8 val)
238{
239 outb_p(reg, cmd640_key);
240 outb_p(val, cmd640_key + 4);
241}
242
243static u8 get_cmd640_reg_vlb(u16 reg)
244{
245 outb_p(reg, cmd640_key);
246 return inb_p(cmd640_key + 4);
247}
248
249static u8 get_cmd640_reg(u16 reg)
250{
251 unsigned long flags;
252 u8 b;
253
254 spin_lock_irqsave(&cmd640_lock, flags);
255 b = __get_cmd640_reg(reg);
256 spin_unlock_irqrestore(&cmd640_lock, flags);
257 return b;
258}
259
260static void put_cmd640_reg(u16 reg, u8 val)
261{
262 unsigned long flags;
263
264 spin_lock_irqsave(&cmd640_lock, flags);
265 __put_cmd640_reg(reg, val);
266 spin_unlock_irqrestore(&cmd640_lock, flags);
267}
268
269static int __init match_pci_cmd640_device(void)
270{
271 const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
272 unsigned int i;
273 for (i = 0; i < 4; i++) {
274 if (get_cmd640_reg(i) != ven_dev[i])
275 return 0;
276 }
277#ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT
278 if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) {
279 printk("ide: cmd640 on PCI disabled by BIOS\n");
280 return 0;
281 }
282#endif
283 return 1;
284}
285
286
287
288
289static int __init probe_for_cmd640_pci1(void)
290{
291 __get_cmd640_reg = get_cmd640_reg_pci1;
292 __put_cmd640_reg = put_cmd640_reg_pci1;
293 for (cmd640_key = 0x80000000;
294 cmd640_key <= 0x8000f800;
295 cmd640_key += 0x800) {
296 if (match_pci_cmd640_device())
297 return 1;
298 }
299 return 0;
300}
301
302
303
304
305static int __init probe_for_cmd640_pci2(void)
306{
307 __get_cmd640_reg = get_cmd640_reg_pci2;
308 __put_cmd640_reg = put_cmd640_reg_pci2;
309 for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) {
310 if (match_pci_cmd640_device())
311 return 1;
312 }
313 return 0;
314}
315
316
317
318
319static int __init probe_for_cmd640_vlb(void)
320{
321 u8 b;
322
323 __get_cmd640_reg = get_cmd640_reg_vlb;
324 __put_cmd640_reg = put_cmd640_reg_vlb;
325 cmd640_key = 0x178;
326 b = get_cmd640_reg(CFR);
327 if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) {
328 cmd640_key = 0x78;
329 b = get_cmd640_reg(CFR);
330 if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h))
331 return 0;
332 }
333 return 1;
334}
335
336
337
338
339
340static int __init secondary_port_responding(void)
341{
342 unsigned long flags;
343
344 spin_lock_irqsave(&cmd640_lock, flags);
345
346 outb_p(0x0a, 0x176);
347 udelay(100);
348 if ((inb_p(0x176) & 0x1f) != 0x0a) {
349 outb_p(0x1a, 0x176);
350 udelay(100);
351 if ((inb_p(0x176) & 0x1f) != 0x1a) {
352 spin_unlock_irqrestore(&cmd640_lock, flags);
353 return 0;
354 }
355 }
356 spin_unlock_irqrestore(&cmd640_lock, flags);
357 return 1;
358}
359
360#ifdef CMD640_DUMP_REGS
361
362
363
364static void cmd640_dump_regs(void)
365{
366 unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
367
368
369 printk("ide: cmd640 internal register dump:");
370 for (; reg <= 0x59; reg++) {
371 if (!(reg & 0x0f))
372 printk("\n%04x:", reg);
373 printk(" %02x", get_cmd640_reg(reg));
374 }
375 printk("\n");
376}
377#endif
378
379static void __set_prefetch_mode(ide_drive_t *drive, int mode)
380{
381 if (mode) {
382#if CMD640_PREFETCH_MASKS
383 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
384 drive->dev_flags &= ~IDE_DFLAG_UNMASK;
385#endif
386 drive->dev_flags &= ~IDE_DFLAG_NO_IO_32BIT;
387 } else {
388 drive->dev_flags &= ~IDE_DFLAG_NO_UNMASK;
389 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
390 drive->io_32bit = 0;
391 }
392}
393
394#ifndef CONFIG_BLK_DEV_CMD640_ENHANCED
395
396
397
398
399static void __init check_prefetch(ide_drive_t *drive, unsigned int index)
400{
401 u8 b = get_cmd640_reg(prefetch_regs[index]);
402
403 __set_prefetch_mode(drive, (b & prefetch_masks[index]) ? 0 : 1);
404}
405#else
406
407
408
409
410static void set_prefetch_mode(ide_drive_t *drive, unsigned int index, int mode)
411{
412 unsigned long flags;
413 int reg = prefetch_regs[index];
414 u8 b;
415
416 spin_lock_irqsave(&cmd640_lock, flags);
417 b = __get_cmd640_reg(reg);
418 __set_prefetch_mode(drive, mode);
419 if (mode)
420 b &= ~prefetch_masks[index];
421 else
422 b |= prefetch_masks[index];
423 __put_cmd640_reg(reg, b);
424 spin_unlock_irqrestore(&cmd640_lock, flags);
425}
426
427
428
429
430static void display_clocks(unsigned int index)
431{
432 u8 active_count, recovery_count;
433
434 active_count = active_counts[index];
435 if (active_count == 1)
436 ++active_count;
437 recovery_count = recovery_counts[index];
438 if (active_count > 3 && recovery_count == 1)
439 ++recovery_count;
440 if (cmd640_chip_version > 1)
441 recovery_count += 1;
442 printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count);
443}
444
445
446
447
448
449static inline u8 pack_nibbles(u8 upper, u8 lower)
450{
451 return ((upper & 0x0f) << 4) | (lower & 0x0f);
452}
453
454
455
456
457
458static void program_drive_counts(ide_drive_t *drive, unsigned int index)
459{
460 unsigned long flags;
461 u8 setup_count = setup_counts[index];
462 u8 active_count = active_counts[index];
463 u8 recovery_count = recovery_counts[index];
464
465
466
467
468
469
470
471 if (index > 1) {
472 ide_drive_t *peer = ide_get_pair_dev(drive);
473 unsigned int mate = index ^ 1;
474
475 if (peer) {
476 if (setup_count < setup_counts[mate])
477 setup_count = setup_counts[mate];
478 if (active_count < active_counts[mate])
479 active_count = active_counts[mate];
480 if (recovery_count < recovery_counts[mate])
481 recovery_count = recovery_counts[mate];
482 }
483 }
484
485
486
487
488 switch (setup_count) {
489 case 4: setup_count = 0x00; break;
490 case 3: setup_count = 0x80; break;
491 case 1:
492 case 2: setup_count = 0x40; break;
493 default: setup_count = 0xc0;
494 }
495
496
497
498
499 spin_lock_irqsave(&cmd640_lock, flags);
500
501
502
503
504
505 setup_count |= __get_cmd640_reg(arttim_regs[index]) & 0x3f;
506 __put_cmd640_reg(arttim_regs[index], setup_count);
507 __put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count));
508 spin_unlock_irqrestore(&cmd640_lock, flags);
509}
510
511
512
513
514static void cmd640_set_mode(ide_drive_t *drive, unsigned int index,
515 u8 pio_mode, unsigned int cycle_time)
516{
517 struct ide_timing *t;
518 int setup_time, active_time, recovery_time, clock_time;
519 u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
520 int bus_speed;
521
522 if (cmd640_vlb)
523 bus_speed = ide_vlb_clk ? ide_vlb_clk : 50;
524 else
525 bus_speed = ide_pci_clk ? ide_pci_clk : 33;
526
527 if (pio_mode > 5)
528 pio_mode = 5;
529
530 t = ide_timing_find_mode(XFER_PIO_0 + pio_mode);
531 setup_time = t->setup;
532 active_time = t->active;
533
534 recovery_time = cycle_time - (setup_time + active_time);
535 clock_time = 1000 / bus_speed;
536 cycle_count = DIV_ROUND_UP(cycle_time, clock_time);
537
538 setup_count = DIV_ROUND_UP(setup_time, clock_time);
539
540 active_count = DIV_ROUND_UP(active_time, clock_time);
541 if (active_count < 2)
542 active_count = 2;
543
544 recovery_count = DIV_ROUND_UP(recovery_time, clock_time);
545 recovery_count2 = cycle_count - (setup_count + active_count);
546 if (recovery_count2 > recovery_count)
547 recovery_count = recovery_count2;
548 if (recovery_count < 2)
549 recovery_count = 2;
550 if (recovery_count > 17) {
551 active_count += recovery_count - 17;
552 recovery_count = 17;
553 }
554 if (active_count > 16)
555 active_count = 16;
556 if (cmd640_chip_version > 1)
557 recovery_count -= 1;
558 if (recovery_count > 16)
559 recovery_count = 16;
560
561 setup_counts[index] = setup_count;
562 active_counts[index] = active_count;
563 recovery_counts[index] = recovery_count;
564
565
566
567
568
569
570
571
572
573 program_drive_counts(drive, index);
574}
575
576static void cmd640_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
577{
578 unsigned int index = 0, cycle_time;
579 const u8 pio = drive->pio_mode - XFER_PIO_0;
580 u8 b;
581
582 switch (pio) {
583 case 6:
584 case 7:
585 b = get_cmd640_reg(CNTRL) & ~0x27;
586 if (pio & 1)
587 b |= 0x27;
588 put_cmd640_reg(CNTRL, b);
589 printk("%s: %sabled cmd640 fast host timing (devsel)\n",
590 drive->name, (pio & 1) ? "en" : "dis");
591 return;
592 case 8:
593 case 9:
594 set_prefetch_mode(drive, index, pio & 1);
595 printk("%s: %sabled cmd640 prefetch\n",
596 drive->name, (pio & 1) ? "en" : "dis");
597 return;
598 }
599
600 cycle_time = ide_pio_cycle_time(drive, pio);
601 cmd640_set_mode(drive, index, pio, cycle_time);
602
603 printk("%s: selected cmd640 PIO mode%d (%dns)",
604 drive->name, pio, cycle_time);
605
606 display_clocks(index);
607}
608#endif
609
610static void __init cmd640_init_dev(ide_drive_t *drive)
611{
612 unsigned int i = drive->hwif->channel * 2 + (drive->dn & 1);
613
614#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
615
616
617
618
619 setup_counts[i] = 4;
620 active_counts[i] = 16;
621 recovery_counts[i] = 16;
622 program_drive_counts(drive, i);
623 set_prefetch_mode(drive, i, 0);
624 printk(KERN_INFO DRV_NAME ": drive%d timings/prefetch cleared\n", i);
625#else
626
627
628
629 check_prefetch(drive, i);
630 printk(KERN_INFO DRV_NAME ": drive%d timings/prefetch(%s) preserved\n",
631 i, (drive->dev_flags & IDE_DFLAG_NO_IO_32BIT) ? "off" : "on");
632#endif
633}
634
635static int cmd640_test_irq(ide_hwif_t *hwif)
636{
637 int irq_reg = hwif->channel ? ARTTIM23 : CFR;
638 u8 irq_mask = hwif->channel ? ARTTIM23_IDE23INTR :
639 CFR_IDE01INTR;
640 u8 irq_stat = get_cmd640_reg(irq_reg);
641
642 return (irq_stat & irq_mask) ? 1 : 0;
643}
644
645static const struct ide_port_ops cmd640_port_ops = {
646 .init_dev = cmd640_init_dev,
647#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
648 .set_pio_mode = cmd640_set_pio_mode,
649#endif
650 .test_irq = cmd640_test_irq,
651};
652
653static int pci_conf1(void)
654{
655 unsigned long flags;
656 u32 tmp;
657
658 spin_lock_irqsave(&cmd640_lock, flags);
659 outb(0x01, 0xCFB);
660 tmp = inl(0xCF8);
661 outl(0x80000000, 0xCF8);
662 if (inl(0xCF8) == 0x80000000) {
663 outl(tmp, 0xCF8);
664 spin_unlock_irqrestore(&cmd640_lock, flags);
665 return 1;
666 }
667 outl(tmp, 0xCF8);
668 spin_unlock_irqrestore(&cmd640_lock, flags);
669 return 0;
670}
671
672static int pci_conf2(void)
673{
674 unsigned long flags;
675
676 spin_lock_irqsave(&cmd640_lock, flags);
677 outb(0x00, 0xCFB);
678 outb(0x00, 0xCF8);
679 outb(0x00, 0xCFA);
680 if (inb(0xCF8) == 0x00 && inb(0xCF8) == 0x00) {
681 spin_unlock_irqrestore(&cmd640_lock, flags);
682 return 1;
683 }
684 spin_unlock_irqrestore(&cmd640_lock, flags);
685 return 0;
686}
687
688static const struct ide_port_info cmd640_port_info __initconst = {
689 .chipset = ide_cmd640,
690 .host_flags = IDE_HFLAG_SERIALIZE |
691 IDE_HFLAG_NO_DMA |
692 IDE_HFLAG_ABUSE_PREFETCH |
693 IDE_HFLAG_ABUSE_FAST_DEVSEL,
694 .port_ops = &cmd640_port_ops,
695 .pio_mask = ATA_PIO5,
696};
697
698static int __init cmd640x_init_one(unsigned long base, unsigned long ctl)
699{
700 if (!request_region(base, 8, DRV_NAME)) {
701 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
702 DRV_NAME, base, base + 7);
703 return -EBUSY;
704 }
705
706 if (!request_region(ctl, 1, DRV_NAME)) {
707 printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
708 DRV_NAME, ctl);
709 release_region(base, 8);
710 return -EBUSY;
711 }
712
713 return 0;
714}
715
716
717
718
719static int __init cmd640x_init(void)
720{
721 int second_port_cmd640 = 0, rc;
722 const char *bus_type, *port2;
723 u8 b, cfr;
724 struct ide_hw hw[2], *hws[2];
725
726 if (cmd640_vlb && probe_for_cmd640_vlb()) {
727 bus_type = "VLB";
728 } else {
729 cmd640_vlb = 0;
730
731
732 if (pci_conf1() && probe_for_cmd640_pci1())
733 bus_type = "PCI (type1)";
734 else if (pci_conf2() && probe_for_cmd640_pci2())
735 bus_type = "PCI (type2)";
736 else
737 return 0;
738 }
739
740
741
742 put_cmd640_reg(0x5b, 0xbd);
743 if (get_cmd640_reg(0x5b) != 0xbd) {
744 printk(KERN_ERR "ide: cmd640 init failed: wrong value in reg 0x5b\n");
745 return 0;
746 }
747 put_cmd640_reg(0x5b, 0);
748
749#ifdef CMD640_DUMP_REGS
750 cmd640_dump_regs();
751#endif
752
753
754
755
756 cfr = get_cmd640_reg(CFR);
757 cmd640_chip_version = cfr & CFR_DEVREV;
758 if (cmd640_chip_version == 0) {
759 printk("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
760 return 0;
761 }
762
763 rc = cmd640x_init_one(0x1f0, 0x3f6);
764 if (rc)
765 return rc;
766
767 rc = cmd640x_init_one(0x170, 0x376);
768 if (rc) {
769 release_region(0x3f6, 1);
770 release_region(0x1f0, 8);
771 return rc;
772 }
773
774 memset(&hw, 0, sizeof(hw));
775
776 ide_std_init_ports(&hw[0], 0x1f0, 0x3f6);
777 hw[0].irq = 14;
778
779 ide_std_init_ports(&hw[1], 0x170, 0x376);
780 hw[1].irq = 15;
781
782 printk(KERN_INFO "cmd640: buggy cmd640%c interface on %s, config=0x%02x"
783 "\n", 'a' + cmd640_chip_version - 1, bus_type, cfr);
784
785
786
787
788 hws[0] = &hw[0];
789
790
791
792
793
794
795
796
797 put_cmd640_reg(CMDTIM, 0);
798 put_cmd640_reg(BRST, 0x40);
799
800 b = get_cmd640_reg(CNTRL);
801
802
803
804
805 if (secondary_port_responding()) {
806 if ((b & CNTRL_ENA_2ND)) {
807 second_port_cmd640 = 1;
808 port2 = "okay";
809 } else if (cmd640_vlb) {
810 second_port_cmd640 = 1;
811 port2 = "alive";
812 } else
813 port2 = "not cmd640";
814 } else {
815 put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND);
816 if (secondary_port_responding()) {
817 second_port_cmd640 = 1;
818 port2 = "enabled";
819 } else {
820 put_cmd640_reg(CNTRL, b);
821 port2 = "not responding";
822 }
823 }
824
825
826
827
828 if (second_port_cmd640)
829 hws[1] = &hw[1];
830
831 printk(KERN_INFO "cmd640: %sserialized, secondary interface %s\n",
832 second_port_cmd640 ? "" : "not ", port2);
833
834#ifdef CMD640_DUMP_REGS
835 cmd640_dump_regs();
836#endif
837
838 return ide_host_add(&cmd640_port_info, hws, second_port_cmd640 ? 2 : 1,
839 NULL);
840}
841
842module_param_named(probe_vlb, cmd640_vlb, bool, 0);
843MODULE_PARM_DESC(probe_vlb, "probe for VLB version of CMD640 chipset");
844
845module_init(cmd640x_init);
846
847MODULE_LICENSE("GPL");
848