0ce92d323036c5061a2e31f8db02e362ca7547ef
[pandora-kernel.git] / drivers / ide / pci / opti621.c
1 /*
2  *  Copyright (C) 1996-1998  Linus Torvalds & authors (see below)
3  */
4
5 /*
6  * Authors:
7  * Jaromir Koutek <miri@punknet.cz>,
8  * Jan Harkes <jaharkes@cwi.nl>,
9  * Mark Lord <mlord@pobox.com>
10  * Some parts of code are from ali14xx.c and from rz1000.c.
11  *
12  * OPTi is trademark of OPTi, Octek is trademark of Octek.
13  *
14  * I used docs from OPTi databook, from ftp.opti.com, file 9123-0002.ps
15  * and disassembled/traced setupvic.exe (DOS program).
16  * It increases kernel code about 2 kB.
17  * I don't have this card no more, but I hope I can get some in case
18  * of needed development.
19  * My card is Octek PIDE 1.01 (on card) or OPTiViC (program).
20  * It has a place for a secondary connector in circuit, but nothing
21  * is there. Also BIOS says no address for
22  * secondary controller (see bellow in ide_init_opti621).
23  * I've only tested this on my system, which only has one disk.
24  * It's Western Digital WDAC2850, with PIO mode 3. The PCI bus
25  * is at 20 MHz (I have DX2/80, I tried PCI at 40, but I got random
26  * lockups). I tried the OCTEK double speed CD-ROM and
27  * it does not work! But I can't boot DOS also, so it's probably
28  * hardware fault. I have connected Conner 80MB, the Seagate 850MB (no
29  * problems) and Seagate 1GB (as slave, WD as master). My experiences
30  * with the third, 1GB drive: I got 3MB/s (hdparm), but sometimes
31  * it slows to about 100kB/s! I don't know why and I have
32  * not this drive now, so I can't try it again.
33  * I write this driver because I lost the paper ("manual") with
34  * settings of jumpers on the card and I have to boot Linux with
35  * Loadlin except LILO, cause I have to run the setupvic.exe program
36  * already or I get disk errors (my test: rpm -Vf
37  * /usr/X11R6/bin/XF86_SVGA - or any big file).
38  * Some numbers from hdparm -t /dev/hda:
39  * Timing buffer-cache reads:   32 MB in  3.02 seconds =10.60 MB/sec
40  * Timing buffered disk reads:  16 MB in  5.52 seconds = 2.90 MB/sec
41  * I have 4 Megs/s before, but I don't know why (maybe changes
42  * in hdparm test).
43  * After release of 0.1, I got some successful reports, so it might work.
44  *
45  * The main problem with OPTi is that some timings for master
46  * and slave must be the same. For example, if you have master
47  * PIO 3 and slave PIO 0, driver have to set some timings of
48  * master for PIO 0. Second problem is that opti621_set_pio_mode
49  * got only one drive to set, but have to set both drives.
50  * This is solved in compute_pios. If you don't set
51  * the second drive, compute_pios use ide_get_best_pio_mode
52  * for autoselect mode (you can change it to PIO 0, if you want).
53  * If you then set the second drive to another PIO, the old value
54  * (automatically selected) will be overrided by yours.
55  * There is a 25/33MHz switch in configuration
56  * register, but driver is written for use at any frequency which get
57  * (use idebus=xx to select PCI bus speed).
58  *
59  * Version 0.1, Nov 8, 1996
60  * by Jaromir Koutek, for 2.1.8. 
61  * Initial version of driver.
62  * 
63  * Version 0.2
64  * Number 0.2 skipped.
65  *
66  * Version 0.3, Nov 29, 1997
67  * by Mark Lord (probably), for 2.1.68
68  * Updates for use with new IDE block driver.
69  *
70  * Version 0.4, Dec 14, 1997
71  * by Jan Harkes
72  * Fixed some errors and cleaned the code.
73  *
74  * Version 0.5, Jan 2, 1998
75  * by Jaromir Koutek
76  * Updates for use with (again) new IDE block driver.
77  * Update of documentation.
78  * 
79  * Version 0.6, Jan 2, 1999
80  * by Jaromir Koutek
81  * Reversed to version 0.3 of the driver, because
82  * 0.5 doesn't work.
83  */
84
85 #define OPTI621_DEBUG           /* define for debug messages */
86
87 #include <linux/types.h>
88 #include <linux/module.h>
89 #include <linux/kernel.h>
90 #include <linux/delay.h>
91 #include <linux/timer.h>
92 #include <linux/mm.h>
93 #include <linux/ioport.h>
94 #include <linux/blkdev.h>
95 #include <linux/pci.h>
96 #include <linux/hdreg.h>
97 #include <linux/ide.h>
98
99 #include <asm/io.h>
100
101 //#define OPTI621_MAX_PIO 3
102 /* In fact, I do not have any PIO 4 drive
103  * (address: 25 ns, data: 70 ns, recovery: 35 ns),
104  * but OPTi 82C621 is programmable and it can do (minimal values):
105  * on 40MHz PCI bus (pulse 25 ns):
106  *  address: 25 ns, data: 25 ns, recovery: 50 ns;
107  * on 20MHz PCI bus (pulse 50 ns):
108  *  address: 50 ns, data: 50 ns, recovery: 100 ns.
109  */
110
111 /* #define READ_PREFETCH 0 */
112 /* Uncomment for disable read prefetch.
113  * There is some readprefetch capatibility in hdparm,
114  * but when I type hdparm -P 1 /dev/hda, I got errors
115  * and till reset drive is inaccessible.
116  * This (hw) read prefetch is safe on my drive.
117  */
118
119 #ifndef READ_PREFETCH
120 #define READ_PREFETCH 0x40 /* read prefetch is enabled */
121 #endif /* else read prefetch is disabled */
122
123 #define READ_REG 0      /* index of Read cycle timing register */
124 #define WRITE_REG 1     /* index of Write cycle timing register */
125 #define CNTRL_REG 3     /* index of Control register */
126 #define STRAP_REG 5     /* index of Strap register */
127 #define MISC_REG 6      /* index of Miscellaneous register */
128
129 static int reg_base;
130
131 #define PIO_NOT_EXIST 254
132 #define PIO_DONT_KNOW 255
133
134 static DEFINE_SPINLOCK(opti621_lock);
135
136 /* there are stored pio numbers from other calls of opti621_set_pio_mode */
137 static void compute_pios(ide_drive_t *drive, const u8 pio)
138 /* Store values into drive->drive_data
139  *      second_contr - 0 for primary controller, 1 for secondary
140  *      slave_drive - 0 -> pio is for master, 1 -> pio is for slave
141  *      pio - PIO mode for selected drive (for other we don't know)
142  */
143 {
144         int d;
145         ide_hwif_t *hwif = HWIF(drive);
146
147         drive->drive_data = pio;
148
149         for (d = 0; d < 2; ++d) {
150                 drive = &hwif->drives[d];
151                 if (drive->present) {
152                         if (drive->drive_data == PIO_DONT_KNOW)
153                                 drive->drive_data = ide_get_best_pio_mode(drive, 255, 3);
154 #ifdef OPTI621_DEBUG
155                         printk("%s: Selected PIO mode %d\n",
156                                 drive->name, drive->drive_data);
157 #endif
158                 } else {
159                         drive->drive_data = PIO_NOT_EXIST;
160                 }
161         }
162 }
163
164 static int cmpt_clk(int time, int bus_speed)
165 /* Returns (rounded up) time in clocks for time in ns,
166  * with bus_speed in MHz.
167  * Example: bus_speed = 40 MHz, time = 80 ns
168  * 1000/40 = 25 ns (clk value),
169  * 80/25 = 3.2, rounded up to 4 (I hope ;-)).
170  * Use idebus=xx to select right frequency.
171  */
172 {
173         return ((time*bus_speed+999)/1000);
174 }
175
176 /* Write value to register reg, base of register
177  * is at reg_base (0x1f0 primary, 0x170 secondary,
178  * if not changed by PCI configuration).
179  * This is from setupvic.exe program.
180  */
181 static void write_reg(u8 value, int reg)
182 {
183         inw(reg_base + 1);
184         inw(reg_base + 1);
185         outb(3, reg_base + 2);
186         outb(value, reg_base + reg);
187         outb(0x83, reg_base + 2);
188 }
189
190 /* Read value from register reg, base of register
191  * is at reg_base (0x1f0 primary, 0x170 secondary,
192  * if not changed by PCI configuration).
193  * This is from setupvic.exe program.
194  */
195 static u8 read_reg(int reg)
196 {
197         u8 ret = 0;
198
199         inw(reg_base + 1);
200         inw(reg_base + 1);
201         outb(3, reg_base + 2);
202         ret = inb(reg_base + reg);
203         outb(0x83, reg_base + 2);
204
205         return ret;
206 }
207
208 typedef struct pio_clocks_s {
209         int     address_time;   /* Address setup (clocks) */
210         int     data_time;      /* Active/data pulse (clocks) */
211         int     recovery_time;  /* Recovery time (clocks) */
212 } pio_clocks_t;
213
214 static void compute_clocks(int pio, pio_clocks_t *clks)
215 {
216         if (pio != PIO_NOT_EXIST) {
217                 int adr_setup, data_pls;
218                 int bus_speed = system_bus_clock();
219
220                 adr_setup = ide_pio_timings[pio].setup_time;
221                 data_pls = ide_pio_timings[pio].active_time;
222                 clks->address_time = cmpt_clk(adr_setup, bus_speed);
223                 clks->data_time = cmpt_clk(data_pls, bus_speed);
224                 clks->recovery_time = cmpt_clk(ide_pio_timings[pio].cycle_time
225                         - adr_setup-data_pls, bus_speed);
226                 if (clks->address_time<1) clks->address_time = 1;
227                 if (clks->address_time>4) clks->address_time = 4;
228                 if (clks->data_time<1) clks->data_time = 1;
229                 if (clks->data_time>16) clks->data_time = 16;
230                 if (clks->recovery_time<2) clks->recovery_time = 2;
231                 if (clks->recovery_time>17) clks->recovery_time = 17;
232         } else {
233                 clks->address_time = 1;
234                 clks->data_time = 1;
235                 clks->recovery_time = 2;
236                 /* minimal values */
237         }
238  
239 }
240
241 static void opti621_set_pio_mode(ide_drive_t *drive, const u8 pio)
242 {
243         /* primary and secondary drives share some registers,
244          * so we have to program both drives
245          */
246         unsigned long flags;
247         u8 pio1 = 0, pio2 = 0;
248         pio_clocks_t first, second;
249         int ax, drdy;
250         u8 cycle1, cycle2, misc;
251         ide_hwif_t *hwif = HWIF(drive);
252
253         /* sets drive->drive_data for both drives */
254         compute_pios(drive, pio);
255         pio1 = hwif->drives[0].drive_data;
256         pio2 = hwif->drives[1].drive_data;
257
258         compute_clocks(pio1, &first);
259         compute_clocks(pio2, &second);
260
261         /* ax = max(a1,a2) */
262         ax = (first.address_time < second.address_time) ? second.address_time : first.address_time;
263
264         drdy = 2; /* DRDY is default 2 (by OPTi Databook) */
265
266         cycle1 = ((first.data_time-1)<<4)  | (first.recovery_time-2);
267         cycle2 = ((second.data_time-1)<<4) | (second.recovery_time-2);
268         misc = READ_PREFETCH | ((ax-1)<<4) | ((drdy-2)<<1);
269
270 #ifdef OPTI621_DEBUG
271         printk("%s: master: address: %d, data: %d, "
272                 "recovery: %d, drdy: %d [clk]\n",
273                 hwif->name, ax, first.data_time,
274                 first.recovery_time, drdy);
275         printk("%s: slave:  address: %d, data: %d, "
276                 "recovery: %d, drdy: %d [clk]\n",
277                 hwif->name, ax, second.data_time,
278                 second.recovery_time, drdy);
279 #endif
280
281         spin_lock_irqsave(&opti621_lock, flags);
282
283         reg_base = hwif->io_ports[IDE_DATA_OFFSET];
284
285         /* allow Register-B */
286         outb(0xc0, reg_base + CNTRL_REG);
287         /* hmm, setupvic.exe does this ;-) */
288         outb(0xff, reg_base + 5);
289         /* if reads 0xff, adapter not exist? */
290         (void)inb(reg_base + CNTRL_REG);
291         /* if reads 0xc0, no interface exist? */
292         read_reg(CNTRL_REG);
293         /* read version, probably 0 */
294         read_reg(STRAP_REG);
295
296         /* program primary drive */
297         /* select Index-0 for Register-A */
298         write_reg(0, MISC_REG);
299         /* set read cycle timings */
300         write_reg(cycle1, READ_REG);
301         /* set write cycle timings */
302         write_reg(cycle1, WRITE_REG);
303
304         /* program secondary drive */
305         /* select Index-1 for Register-B */
306         write_reg(1, MISC_REG);
307         /* set read cycle timings */
308         write_reg(cycle2, READ_REG);
309         /* set write cycle timings */
310         write_reg(cycle2, WRITE_REG);
311
312         /* use Register-A for drive 0 */
313         /* use Register-B for drive 1 */
314         write_reg(0x85, CNTRL_REG);
315
316         /* set address setup, DRDY timings,   */
317         /*  and read prefetch for both drives */
318         write_reg(misc, MISC_REG);
319
320         spin_unlock_irqrestore(&opti621_lock, flags);
321 }
322
323 /*
324  * init_hwif_opti621() is called once for each hwif found at boot.
325  */
326 static void __devinit init_hwif_opti621 (ide_hwif_t *hwif)
327 {
328         hwif->drives[0].drive_data = PIO_DONT_KNOW;
329         hwif->drives[1].drive_data = PIO_DONT_KNOW;
330
331         hwif->set_pio_mode = &opti621_set_pio_mode;
332 }
333
334 static const struct ide_port_info opti621_chipsets[] __devinitdata = {
335         {       /* 0 */
336                 .name           = "OPTI621",
337                 .init_hwif      = init_hwif_opti621,
338                 .enablebits     = {{0x45,0x80,0x00}, {0x40,0x08,0x00}},
339                 .host_flags     = IDE_HFLAG_TRUST_BIOS_FOR_DMA |
340                                   IDE_HFLAG_BOOTABLE,
341                 .pio_mask       = ATA_PIO3,
342                 .swdma_mask     = ATA_SWDMA2,
343                 .mwdma_mask     = ATA_MWDMA2,
344         },{     /* 1 */
345                 .name           = "OPTI621X",
346                 .init_hwif      = init_hwif_opti621,
347                 .enablebits     = {{0x45,0x80,0x00}, {0x40,0x08,0x00}},
348                 .host_flags     = IDE_HFLAG_TRUST_BIOS_FOR_DMA |
349                                   IDE_HFLAG_BOOTABLE,
350                 .pio_mask       = ATA_PIO3,
351                 .swdma_mask     = ATA_SWDMA2,
352                 .mwdma_mask     = ATA_MWDMA2,
353         }
354 };
355
356 static int __devinit opti621_init_one(struct pci_dev *dev, const struct pci_device_id *id)
357 {
358         return ide_setup_pci_device(dev, &opti621_chipsets[id->driver_data]);
359 }
360
361 static const struct pci_device_id opti621_pci_tbl[] = {
362         { PCI_VDEVICE(OPTI, PCI_DEVICE_ID_OPTI_82C621), 0 },
363         { PCI_VDEVICE(OPTI, PCI_DEVICE_ID_OPTI_82C825), 1 },
364         { 0, },
365 };
366 MODULE_DEVICE_TABLE(pci, opti621_pci_tbl);
367
368 static struct pci_driver driver = {
369         .name           = "Opti621_IDE",
370         .id_table       = opti621_pci_tbl,
371         .probe          = opti621_init_one,
372 };
373
374 static int __init opti621_ide_init(void)
375 {
376         return ide_pci_register_driver(&driver);
377 }
378
379 module_init(opti621_ide_init);
380
381 MODULE_AUTHOR("Jaromir Koutek, Jan Harkes, Mark Lord");
382 MODULE_DESCRIPTION("PCI driver module for Opti621 IDE");
383 MODULE_LICENSE("GPL");