1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 1995-1996 Linus Torvalds & author (see below)
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /*
7*4882a593Smuzhiyun * Principal Author/Maintainer: PODIEN@hml2.atlas.de (Wolfram Podien)
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This file provides support for the advanced features
10*4882a593Smuzhiyun * of the UMC 8672 IDE interface.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Version 0.01 Initial version, hacked out of ide.c,
13*4882a593Smuzhiyun * and #include'd rather than compiled separately.
14*4882a593Smuzhiyun * This will get cleaned up in a subsequent release.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * Version 0.02 now configs/compiles separate from ide.c -ml
17*4882a593Smuzhiyun * Version 0.03 enhanced auto-tune, fix display bug
18*4882a593Smuzhiyun * Version 0.05 replace sti() with restore_flags() -ml
19*4882a593Smuzhiyun * add detection of possible race condition -ml
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * VLB Controller Support from
24*4882a593Smuzhiyun * Wolfram Podien
25*4882a593Smuzhiyun * Rohoefe 3
26*4882a593Smuzhiyun * D28832 Achim
27*4882a593Smuzhiyun * Germany
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * To enable UMC8672 support there must a lilo line like
30*4882a593Smuzhiyun * append="ide0=umc8672"...
31*4882a593Smuzhiyun * To set the speed according to the abilities of the hardware there must be a
32*4882a593Smuzhiyun * line like
33*4882a593Smuzhiyun * #define UMC_DRIVE0 11
34*4882a593Smuzhiyun * in the beginning of the driver, which sets the speed of drive 0 to 11 (there
35*4882a593Smuzhiyun * are some lines present). 0 - 11 are allowed speed values. These values are
36*4882a593Smuzhiyun * the results from the DOS speed test program supplied from UMC. 11 is the
37*4882a593Smuzhiyun * highest speed (about PIO mode 3)
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun #define REALLY_SLOW_IO /* some systems can safely undef this */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #include <linux/module.h>
42*4882a593Smuzhiyun #include <linux/types.h>
43*4882a593Smuzhiyun #include <linux/kernel.h>
44*4882a593Smuzhiyun #include <linux/delay.h>
45*4882a593Smuzhiyun #include <linux/timer.h>
46*4882a593Smuzhiyun #include <linux/mm.h>
47*4882a593Smuzhiyun #include <linux/ioport.h>
48*4882a593Smuzhiyun #include <linux/blkdev.h>
49*4882a593Smuzhiyun #include <linux/ide.h>
50*4882a593Smuzhiyun #include <linux/init.h>
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #include <asm/io.h>
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #define DRV_NAME "umc8672"
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * Default speeds. These can be changed with "auto-tune" and/or hdparm.
58*4882a593Smuzhiyun */
59*4882a593Smuzhiyun #define UMC_DRIVE0 1 /* DOS measured drive speeds */
60*4882a593Smuzhiyun #define UMC_DRIVE1 1 /* 0 to 11 allowed */
61*4882a593Smuzhiyun #define UMC_DRIVE2 1 /* 11 = Fastest Speed */
62*4882a593Smuzhiyun #define UMC_DRIVE3 1 /* In case of crash reduce speed */
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun static u8 current_speeds[4] = {UMC_DRIVE0, UMC_DRIVE1, UMC_DRIVE2, UMC_DRIVE3};
65*4882a593Smuzhiyun static const u8 pio_to_umc [5] = {0, 3, 7, 10, 11}; /* rough guesses */
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /* 0 1 2 3 4 5 6 7 8 9 10 11 */
68*4882a593Smuzhiyun static const u8 speedtab [3][12] = {
69*4882a593Smuzhiyun {0x0f, 0x0b, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1},
70*4882a593Smuzhiyun {0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1},
71*4882a593Smuzhiyun {0xff, 0xcb, 0xc0, 0x58, 0x36, 0x33, 0x23, 0x22, 0x21, 0x11, 0x10, 0x0}
72*4882a593Smuzhiyun };
73*4882a593Smuzhiyun
out_umc(char port,char wert)74*4882a593Smuzhiyun static void out_umc(char port, char wert)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun outb_p(port, 0x108);
77*4882a593Smuzhiyun outb_p(wert, 0x109);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
in_umc(char port)80*4882a593Smuzhiyun static inline u8 in_umc(char port)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun outb_p(port, 0x108);
83*4882a593Smuzhiyun return inb_p(0x109);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
umc_set_speeds(u8 speeds[])86*4882a593Smuzhiyun static void umc_set_speeds(u8 speeds[])
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun int i, tmp;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun outb_p(0x5A, 0x108); /* enable umc */
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun out_umc(0xd7, (speedtab[0][speeds[2]] | (speedtab[0][speeds[3]]<<4)));
93*4882a593Smuzhiyun out_umc(0xd6, (speedtab[0][speeds[0]] | (speedtab[0][speeds[1]]<<4)));
94*4882a593Smuzhiyun tmp = 0;
95*4882a593Smuzhiyun for (i = 3; i >= 0; i--)
96*4882a593Smuzhiyun tmp = (tmp << 2) | speedtab[1][speeds[i]];
97*4882a593Smuzhiyun out_umc(0xdc, tmp);
98*4882a593Smuzhiyun for (i = 0; i < 4; i++) {
99*4882a593Smuzhiyun out_umc(0xd0 + i, speedtab[2][speeds[i]]);
100*4882a593Smuzhiyun out_umc(0xd8 + i, speedtab[2][speeds[i]]);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun outb_p(0xa5, 0x108); /* disable umc */
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun printk("umc8672: drive speeds [0 to 11]: %d %d %d %d\n",
105*4882a593Smuzhiyun speeds[0], speeds[1], speeds[2], speeds[3]);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
umc_set_pio_mode(ide_hwif_t * hwif,ide_drive_t * drive)108*4882a593Smuzhiyun static void umc_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun ide_hwif_t *mate = hwif->mate;
111*4882a593Smuzhiyun unsigned long flags;
112*4882a593Smuzhiyun const u8 pio = drive->pio_mode - XFER_PIO_0;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun printk("%s: setting umc8672 to PIO mode%d (speed %d)\n",
115*4882a593Smuzhiyun drive->name, pio, pio_to_umc[pio]);
116*4882a593Smuzhiyun if (mate)
117*4882a593Smuzhiyun spin_lock_irqsave(&mate->lock, flags);
118*4882a593Smuzhiyun if (mate && mate->handler) {
119*4882a593Smuzhiyun printk(KERN_ERR "umc8672: other interface is busy: exiting tune_umc()\n");
120*4882a593Smuzhiyun } else {
121*4882a593Smuzhiyun current_speeds[drive->name[2] - 'a'] = pio_to_umc[pio];
122*4882a593Smuzhiyun umc_set_speeds(current_speeds);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun if (mate)
125*4882a593Smuzhiyun spin_unlock_irqrestore(&mate->lock, flags);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun static const struct ide_port_ops umc8672_port_ops = {
129*4882a593Smuzhiyun .set_pio_mode = umc_set_pio_mode,
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun static const struct ide_port_info umc8672_port_info __initconst = {
133*4882a593Smuzhiyun .name = DRV_NAME,
134*4882a593Smuzhiyun .chipset = ide_umc8672,
135*4882a593Smuzhiyun .port_ops = &umc8672_port_ops,
136*4882a593Smuzhiyun .host_flags = IDE_HFLAG_NO_DMA,
137*4882a593Smuzhiyun .pio_mask = ATA_PIO4,
138*4882a593Smuzhiyun };
139*4882a593Smuzhiyun
umc8672_probe(void)140*4882a593Smuzhiyun static int __init umc8672_probe(void)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun unsigned long flags;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun if (!request_region(0x108, 2, "umc8672")) {
145*4882a593Smuzhiyun printk(KERN_ERR "umc8672: ports 0x108-0x109 already in use.\n");
146*4882a593Smuzhiyun return 1;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun local_irq_save(flags);
149*4882a593Smuzhiyun outb_p(0x5A, 0x108); /* enable umc */
150*4882a593Smuzhiyun if (in_umc (0xd5) != 0xa0) {
151*4882a593Smuzhiyun local_irq_restore(flags);
152*4882a593Smuzhiyun printk(KERN_ERR "umc8672: not found\n");
153*4882a593Smuzhiyun release_region(0x108, 2);
154*4882a593Smuzhiyun return 1;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun outb_p(0xa5, 0x108); /* disable umc */
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun umc_set_speeds(current_speeds);
159*4882a593Smuzhiyun local_irq_restore(flags);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun return ide_legacy_device_add(&umc8672_port_info, 0);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun static bool probe_umc8672;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun module_param_named(probe, probe_umc8672, bool, 0);
167*4882a593Smuzhiyun MODULE_PARM_DESC(probe, "probe for UMC8672 chipset");
168*4882a593Smuzhiyun
umc8672_init(void)169*4882a593Smuzhiyun static int __init umc8672_init(void)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun if (probe_umc8672 == 0)
172*4882a593Smuzhiyun goto out;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun if (umc8672_probe() == 0)
175*4882a593Smuzhiyun return 0;
176*4882a593Smuzhiyun out:
177*4882a593Smuzhiyun return -ENODEV;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun module_init(umc8672_init);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun MODULE_AUTHOR("Wolfram Podien");
183*4882a593Smuzhiyun MODULE_DESCRIPTION("Support for UMC 8672 IDE chipset");
184*4882a593Smuzhiyun MODULE_LICENSE("GPL");
185