blob: 8c715b7a85dbd1382832df0f3c0ef4476d6dfdd2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +020048 * master for PIO 0. Second problem is that opti621_set_pio_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * 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
Bartlomiej Zolnierkiewicz73f1ad82008-04-27 15:38:29 +020056 * register, but driver is written for use at any frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Version 0.1, Nov 8, 1996
Paolo Ciarrocchi4eb68a22008-04-26 17:36:41 +020059 * by Jaromir Koutek, for 2.1.8.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * Initial version of driver.
Paolo Ciarrocchi4eb68a22008-04-26 17:36:41 +020061 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * Version 0.2
63 * Number 0.2 skipped.
64 *
65 * Version 0.3, Nov 29, 1997
66 * by Mark Lord (probably), for 2.1.68
67 * Updates for use with new IDE block driver.
68 *
69 * Version 0.4, Dec 14, 1997
70 * by Jan Harkes
71 * Fixed some errors and cleaned the code.
72 *
73 * Version 0.5, Jan 2, 1998
74 * by Jaromir Koutek
75 * Updates for use with (again) new IDE block driver.
76 * Update of documentation.
Paolo Ciarrocchi4eb68a22008-04-26 17:36:41 +020077 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * Version 0.6, Jan 2, 1999
79 * by Jaromir Koutek
80 * Reversed to version 0.3 of the driver, because
81 * 0.5 doesn't work.
82 */
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/types.h>
85#include <linux/module.h>
86#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/pci.h>
88#include <linux/hdreg.h>
89#include <linux/ide.h>
90
91#include <asm/io.h>
92
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +020093//#define OPTI621_MAX_PIO 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* In fact, I do not have any PIO 4 drive
95 * (address: 25 ns, data: 70 ns, recovery: 35 ns),
96 * but OPTi 82C621 is programmable and it can do (minimal values):
97 * on 40MHz PCI bus (pulse 25 ns):
98 * address: 25 ns, data: 25 ns, recovery: 50 ns;
99 * on 20MHz PCI bus (pulse 50 ns):
100 * address: 50 ns, data: 50 ns, recovery: 100 ns.
101 */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#define READ_REG 0 /* index of Read cycle timing register */
104#define WRITE_REG 1 /* index of Write cycle timing register */
105#define CNTRL_REG 3 /* index of Control register */
106#define STRAP_REG 5 /* index of Strap register */
107#define MISC_REG 6 /* index of Miscellaneous register */
108
109static int reg_base;
110
Bartlomiej Zolnierkiewicze65dde72007-10-20 00:32:35 +0200111static DEFINE_SPINLOCK(opti621_lock);
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* Write value to register reg, base of register
114 * is at reg_base (0x1f0 primary, 0x170 secondary,
115 * if not changed by PCI configuration).
116 * This is from setupvic.exe program.
117 */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100118static void write_reg(u8 value, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100120 inw(reg_base + 1);
121 inw(reg_base + 1);
122 outb(3, reg_base + 2);
123 outb(value, reg_base + reg);
124 outb(0x83, reg_base + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* Read value from register reg, base of register
128 * is at reg_base (0x1f0 primary, 0x170 secondary,
129 * if not changed by PCI configuration).
130 * This is from setupvic.exe program.
131 */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100132static u8 read_reg(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 u8 ret = 0;
135
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100136 inw(reg_base + 1);
137 inw(reg_base + 1);
138 outb(3, reg_base + 2);
139 ret = inb(reg_base + reg);
140 outb(0x83, reg_base + 2);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return ret;
143}
144
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200145static void opti621_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Bartlomiej Zolnierkiewicz6c987182008-06-15 21:00:22 +0200147 ide_hwif_t *hwif = drive->hwif;
148 ide_drive_t *pair = ide_get_paired_drive(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 unsigned long flags;
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +0200150 u8 tim, misc, addr_pio = pio, clk;
151
152 /* DRDY is default 2 (by OPTi Databook) */
153 static const u8 addr_timings[2][4] = {
154 { 0x20, 0x10, 0x00, 0x00 }, /* 33 MHz */
155 { 0x10, 0x10, 0x00, 0x00 }, /* 25 MHz */
156 };
157 static const u8 data_rec_timings[2][4] = {
158 { 0x5b, 0x45, 0x32, 0x21 }, /* 33 MHz */
159 { 0x48, 0x34, 0x21, 0x10 } /* 25 MHz */
160 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Bartlomiej Zolnierkiewicz6c987182008-06-15 21:00:22 +0200162 drive->drive_data = XFER_PIO_0 + pio;
163
164 if (pair->present) {
165 if (pair->drive_data && pair->drive_data < drive->drive_data)
166 addr_pio = pair->drive_data - XFER_PIO_0;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Bartlomiej Zolnierkiewicz21bd33a2008-06-15 21:00:22 +0200169 spin_lock_irqsave(&opti621_lock, flags);
170
171 reg_base = hwif->io_ports.data_addr;
172
173 /* allow Register-B */
174 outb(0xc0, reg_base + CNTRL_REG);
175 /* hmm, setupvic.exe does this ;-) */
176 outb(0xff, reg_base + 5);
177 /* if reads 0xff, adapter not exist? */
178 (void)inb(reg_base + CNTRL_REG);
179 /* if reads 0xc0, no interface exist? */
180 read_reg(CNTRL_REG);
181
182 /* check CLK speed */
183 clk = read_reg(STRAP_REG) & 1;
184
185 printk(KERN_INFO "%s: CLK = %d MHz\n", hwif->name, clk ? 25 : 33);
186
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +0200187 tim = data_rec_timings[clk][pio];
188 misc = addr_timings[clk][addr_pio];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Bartlomiej Zolnierkiewicz6c987182008-06-15 21:00:22 +0200190 /* select Index-0/1 for Register-A/B */
191 write_reg(drive->select.b.unit, MISC_REG);
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100192 /* set read cycle timings */
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +0200193 write_reg(tim, READ_REG);
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100194 /* set write cycle timings */
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +0200195 write_reg(tim, WRITE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* use Register-A for drive 0 */
198 /* use Register-B for drive 1 */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100199 write_reg(0x85, CNTRL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* set address setup, DRDY timings, */
202 /* and read prefetch for both drives */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100203 write_reg(misc, MISC_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Bartlomiej Zolnierkiewicze65dde72007-10-20 00:32:35 +0200205 spin_unlock_irqrestore(&opti621_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200208static const struct ide_port_ops opti621_port_ops = {
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200209 .set_pio_mode = opti621_set_pio_mode,
210};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200212static const struct ide_port_info opti621_chipsets[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 { /* 0 */
214 .name = "OPTI621",
Paolo Ciarrocchi4eb68a22008-04-26 17:36:41 +0200215 .enablebits = { {0x45, 0x80, 0x00}, {0x40, 0x08, 0x00} },
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200216 .port_ops = &opti621_port_ops,
Bartlomiej Zolnierkiewiczf3610372008-06-15 21:00:21 +0200217 .host_flags = IDE_HFLAG_NO_DMA,
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200218 .pio_mask = ATA_PIO3,
Paolo Ciarrocchi4eb68a22008-04-26 17:36:41 +0200219 }, { /* 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .name = "OPTI621X",
Paolo Ciarrocchi4eb68a22008-04-26 17:36:41 +0200221 .enablebits = { {0x45, 0x80, 0x00}, {0x40, 0x08, 0x00} },
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200222 .port_ops = &opti621_port_ops,
Bartlomiej Zolnierkiewiczf3610372008-06-15 21:00:21 +0200223 .host_flags = IDE_HFLAG_NO_DMA,
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200224 .pio_mask = ATA_PIO3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226};
227
228static int __devinit opti621_init_one(struct pci_dev *dev, const struct pci_device_id *id)
229{
230 return ide_setup_pci_device(dev, &opti621_chipsets[id->driver_data]);
231}
232
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200233static const struct pci_device_id opti621_pci_tbl[] = {
234 { PCI_VDEVICE(OPTI, PCI_DEVICE_ID_OPTI_82C621), 0 },
235 { PCI_VDEVICE(OPTI, PCI_DEVICE_ID_OPTI_82C825), 1 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 { 0, },
237};
238MODULE_DEVICE_TABLE(pci, opti621_pci_tbl);
239
240static struct pci_driver driver = {
241 .name = "Opti621_IDE",
242 .id_table = opti621_pci_tbl,
243 .probe = opti621_init_one,
244};
245
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +0100246static int __init opti621_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 return ide_pci_register_driver(&driver);
249}
250
251module_init(opti621_ide_init);
252
253MODULE_AUTHOR("Jaromir Koutek, Jan Harkes, Mark Lord");
254MODULE_DESCRIPTION("PCI driver module for Opti621 IDE");
255MODULE_LICENSE("GPL");