Import 2.3.9pre5
[davej-history.git] / drivers / block / ali14xx.c
blob46a275305122c0f239b4a24d68ced5954905de4a
1 /*
2 * linux/drivers/block/ali14xx.c Version 0.03 Feb 09, 1996
4 * Copyright (C) 1996 Linus Torvalds & author (see below)
5 */
7 /*
8 * ALI M14xx chipset EIDE controller
10 * Works for ALI M1439/1443/1445/1487/1489 chipsets.
12 * Adapted from code developed by derekn@vw.ece.cmu.edu. -ml
13 * Derek's notes follow:
15 * I think the code should be pretty understandable,
16 * but I'll be happy to (try to) answer questions.
18 * The critical part is in the setupDrive function. The initRegisters
19 * function doesn't seem to be necessary, but the DOS driver does it, so
20 * I threw it in.
22 * I've only tested this on my system, which only has one disk. I posted
23 * it to comp.sys.linux.hardware, so maybe some other people will try it
24 * out.
26 * Derek Noonburg (derekn@ece.cmu.edu)
27 * 95-sep-26
29 * Update 96-jul-13:
31 * I've since upgraded to two disks and a CD-ROM, with no trouble, and
32 * I've also heard from several others who have used it successfully.
33 * This driver appears to work with both the 1443/1445 and the 1487/1489
34 * chipsets. I've added support for PIO mode 4 for the 1487. This
35 * seems to work just fine on the 1443 also, although I'm not sure it's
36 * advertised as supporting mode 4. (I've been running a WDC AC21200 in
37 * mode 4 for a while now with no trouble.) -Derek
40 #undef REALLY_SLOW_IO /* most systems can safely undef this */
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/delay.h>
45 #include <linux/timer.h>
46 #include <linux/mm.h>
47 #include <linux/ioport.h>
48 #include <linux/blkdev.h>
49 #include <linux/hdreg.h>
50 #include <linux/ide.h>
52 #include <asm/io.h>
54 #include "ide_modes.h"
56 /* port addresses for auto-detection */
57 #define ALI_NUM_PORTS 4
58 static int ports[ALI_NUM_PORTS] = {0x074, 0x0f4, 0x034, 0x0e4};
60 /* register initialization data */
61 typedef struct { byte reg, data; } RegInitializer;
63 static RegInitializer initData[] = {
64 {0x01, 0x0f}, {0x02, 0x00}, {0x03, 0x00}, {0x04, 0x00},
65 {0x05, 0x00}, {0x06, 0x00}, {0x07, 0x2b}, {0x0a, 0x0f},
66 {0x25, 0x00}, {0x26, 0x00}, {0x27, 0x00}, {0x28, 0x00},
67 {0x29, 0x00}, {0x2a, 0x00}, {0x2f, 0x00}, {0x2b, 0x00},
68 {0x2c, 0x00}, {0x2d, 0x00}, {0x2e, 0x00}, {0x30, 0x00},
69 {0x31, 0x00}, {0x32, 0x00}, {0x33, 0x00}, {0x34, 0xff},
70 {0x35, 0x03}, {0x00, 0x00}
73 #define ALI_MAX_PIO 4
75 /* timing parameter registers for each drive */
76 static struct { byte reg1, reg2, reg3, reg4; } regTab[4] = {
77 {0x03, 0x26, 0x04, 0x27}, /* drive 0 */
78 {0x05, 0x28, 0x06, 0x29}, /* drive 1 */
79 {0x2b, 0x30, 0x2c, 0x31}, /* drive 2 */
80 {0x2d, 0x32, 0x2e, 0x33}, /* drive 3 */
83 static int basePort = 0; /* base port address */
84 static int regPort = 0; /* port for register number */
85 static int dataPort = 0; /* port for register data */
86 static byte regOn; /* output to base port to access registers */
87 static byte regOff; /* output to base port to close registers */
89 /*------------------------------------------------------------------------*/
92 * Read a controller register.
94 static inline byte inReg (byte reg)
96 outb_p(reg, regPort);
97 return inb(dataPort);
101 * Write a controller register.
103 static void outReg (byte data, byte reg)
105 outb_p(reg, regPort);
106 outb_p(data, dataPort);
110 * Set PIO mode for the specified drive.
111 * This function computes timing parameters
112 * and sets controller registers accordingly.
114 static void ali14xx_tune_drive (ide_drive_t *drive, byte pio)
116 int driveNum;
117 int time1, time2;
118 byte param1, param2, param3, param4;
119 unsigned long flags;
120 ide_pio_data_t d;
121 int bus_speed = ide_system_bus_speed();
123 pio = ide_get_best_pio_mode(drive, pio, ALI_MAX_PIO, &d);
125 /* calculate timing, according to PIO mode */
126 time1 = d.cycle_time;
127 time2 = ide_pio_timings[pio].active_time;
128 param3 = param1 = (time2 * bus_speed + 999) / 1000;
129 param4 = param2 = (time1 * bus_speed + 999) / 1000 - param1;
130 if (pio < 3) {
131 param3 += 8;
132 param4 += 8;
134 printk("%s: PIO mode%d, t1=%dns, t2=%dns, cycles = %d+%d, %d+%d\n",
135 drive->name, pio, time1, time2, param1, param2, param3, param4);
137 /* stuff timing parameters into controller registers */
138 driveNum = (HWIF(drive)->index << 1) + drive->select.b.unit;
139 save_flags(flags); /* all CPUs */
140 cli(); /* all CPUs */
141 outb_p(regOn, basePort);
142 outReg(param1, regTab[driveNum].reg1);
143 outReg(param2, regTab[driveNum].reg2);
144 outReg(param3, regTab[driveNum].reg3);
145 outReg(param4, regTab[driveNum].reg4);
146 outb_p(regOff, basePort);
147 restore_flags(flags); /* all CPUs */
151 * Auto-detect the IDE controller port.
153 static int findPort (void)
155 int i;
156 byte t;
157 unsigned long flags;
159 __save_flags(flags); /* local CPU only */
160 __cli(); /* local CPU only */
161 for (i = 0; i < ALI_NUM_PORTS; ++i) {
162 basePort = ports[i];
163 regOff = inb(basePort);
164 for (regOn = 0x30; regOn <= 0x33; ++regOn) {
165 outb_p(regOn, basePort);
166 if (inb(basePort) == regOn) {
167 regPort = basePort + 4;
168 dataPort = basePort + 8;
169 t = inReg(0) & 0xf0;
170 outb_p(regOff, basePort);
171 __restore_flags(flags); /* local CPU only */
172 if (t != 0x50)
173 return 0;
174 return 1; /* success */
177 outb_p(regOff, basePort);
179 __restore_flags(flags); /* local CPU only */
180 return 0;
184 * Initialize controller registers with default values.
186 static int initRegisters (void) {
187 RegInitializer *p;
188 byte t;
189 unsigned long flags;
191 __save_flags(flags); /* local CPU only */
192 __cli(); /* local CPU only */
193 outb_p(regOn, basePort);
194 for (p = initData; p->reg != 0; ++p)
195 outReg(p->data, p->reg);
196 outb_p(0x01, regPort);
197 t = inb(regPort) & 0x01;
198 outb_p(regOff, basePort);
199 __restore_flags(flags); /* local CPU only */
200 return t;
203 void init_ali14xx (void)
205 /* auto-detect IDE controller port */
206 if (!findPort()) {
207 printk("ali14xx: not found\n");
208 return;
211 printk("ali14xx: base= 0x%03x, regOn = 0x%02x\n", basePort, regOn);
212 ide_hwifs[0].chipset = ide_ali14xx;
213 ide_hwifs[1].chipset = ide_ali14xx;
214 ide_hwifs[0].tuneproc = &ali14xx_tune_drive;
215 ide_hwifs[1].tuneproc = &ali14xx_tune_drive;
216 ide_hwifs[0].mate = &ide_hwifs[1];
217 ide_hwifs[1].mate = &ide_hwifs[0];
218 ide_hwifs[1].channel = 1;
220 /* initialize controller registers */
221 if (!initRegisters()) {
222 printk("ali14xx: Chip initialization failed\n");
223 return;