blob: 26a45007e535093a16545a2a43cb4e839a4dff68 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/module.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/ide.h>
18
19#include <asm/io.h>
20
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +020021#define DRV_NAME "opti621"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define READ_REG 0 /* index of Read cycle timing register */
24#define WRITE_REG 1 /* index of Write cycle timing register */
25#define CNTRL_REG 3 /* index of Control register */
26#define STRAP_REG 5 /* index of Strap register */
27#define MISC_REG 6 /* index of Miscellaneous register */
28
29static int reg_base;
30
Bartlomiej Zolnierkiewicze65dde72007-10-20 00:32:35 +020031static DEFINE_SPINLOCK(opti621_lock);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* Write value to register reg, base of register
34 * is at reg_base (0x1f0 primary, 0x170 secondary,
35 * if not changed by PCI configuration).
36 * This is from setupvic.exe program.
37 */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +010038static void write_reg(u8 value, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +010040 inw(reg_base + 1);
41 inw(reg_base + 1);
42 outb(3, reg_base + 2);
43 outb(value, reg_base + reg);
44 outb(0x83, reg_base + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* Read value from register reg, base of register
48 * is at reg_base (0x1f0 primary, 0x170 secondary,
49 * if not changed by PCI configuration).
50 * This is from setupvic.exe program.
51 */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +010052static u8 read_reg(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 u8 ret = 0;
55
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +010056 inw(reg_base + 1);
57 inw(reg_base + 1);
58 outb(3, reg_base + 2);
59 ret = inb(reg_base + reg);
60 outb(0x83, reg_base + 2);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return ret;
63}
64
Bartlomiej Zolnierkiewicze085b3c2010-01-19 01:44:41 -080065static void opti621_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Bartlomiej Zolnierkiewicz7e59ea22008-10-10 22:39:26 +020067 ide_drive_t *pair = ide_get_pair_dev(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned long flags;
Bartlomiej Zolnierkiewicze085b3c2010-01-19 01:44:41 -080069 unsigned long mode = drive->pio_mode, pair_mode;
70 const u8 pio = mode - XFER_PIO_0;
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +020071 u8 tim, misc, addr_pio = pio, clk;
72
73 /* DRDY is default 2 (by OPTi Databook) */
Bartlomiej Zolnierkiewicz80a65fc2008-06-15 21:00:22 +020074 static const u8 addr_timings[2][5] = {
75 { 0x20, 0x10, 0x00, 0x00, 0x00 }, /* 33 MHz */
76 { 0x10, 0x10, 0x00, 0x00, 0x00 }, /* 25 MHz */
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +020077 };
Bartlomiej Zolnierkiewicz80a65fc2008-06-15 21:00:22 +020078 static const u8 data_rec_timings[2][5] = {
79 { 0x5b, 0x45, 0x32, 0x21, 0x20 }, /* 33 MHz */
80 { 0x48, 0x34, 0x21, 0x10, 0x10 } /* 25 MHz */
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +020081 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Joao Ramos5bfb151f2009-06-15 22:13:44 +020083 ide_set_drivedata(drive, (void *)mode);
Bartlomiej Zolnierkiewicz6c987182008-06-15 21:00:22 +020084
Bartlomiej Zolnierkiewicz7e59ea22008-10-10 22:39:26 +020085 if (pair) {
Joao Ramos5bfb151f2009-06-15 22:13:44 +020086 pair_mode = (unsigned long)ide_get_drivedata(pair);
87 if (pair_mode && pair_mode < mode)
88 addr_pio = pair_mode - XFER_PIO_0;
Bartlomiej Zolnierkiewicz6c987182008-06-15 21:00:22 +020089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Bartlomiej Zolnierkiewicz21bd33a2008-06-15 21:00:22 +020091 spin_lock_irqsave(&opti621_lock, flags);
92
93 reg_base = hwif->io_ports.data_addr;
94
95 /* allow Register-B */
96 outb(0xc0, reg_base + CNTRL_REG);
97 /* hmm, setupvic.exe does this ;-) */
98 outb(0xff, reg_base + 5);
99 /* if reads 0xff, adapter not exist? */
100 (void)inb(reg_base + CNTRL_REG);
101 /* if reads 0xc0, no interface exist? */
102 read_reg(CNTRL_REG);
103
104 /* check CLK speed */
105 clk = read_reg(STRAP_REG) & 1;
106
107 printk(KERN_INFO "%s: CLK = %d MHz\n", hwif->name, clk ? 25 : 33);
108
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +0200109 tim = data_rec_timings[clk][pio];
110 misc = addr_timings[clk][addr_pio];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Bartlomiej Zolnierkiewicz6c987182008-06-15 21:00:22 +0200112 /* select Index-0/1 for Register-A/B */
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200113 write_reg(drive->dn & 1, MISC_REG);
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100114 /* set read cycle timings */
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +0200115 write_reg(tim, READ_REG);
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100116 /* set write cycle timings */
Bartlomiej Zolnierkiewicz810253d2008-06-15 21:00:22 +0200117 write_reg(tim, WRITE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /* use Register-A for drive 0 */
120 /* use Register-B for drive 1 */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100121 write_reg(0x85, CNTRL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* set address setup, DRDY timings, */
124 /* and read prefetch for both drives */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100125 write_reg(misc, MISC_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Bartlomiej Zolnierkiewicze65dde72007-10-20 00:32:35 +0200127 spin_unlock_irqrestore(&opti621_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200130static const struct ide_port_ops opti621_port_ops = {
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200131 .set_pio_mode = opti621_set_pio_mode,
132};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800134static const struct ide_port_info opti621_chipset = {
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +0200135 .name = DRV_NAME,
Bartlomiej Zolnierkiewicz80a65fc2008-06-15 21:00:22 +0200136 .enablebits = { {0x45, 0x80, 0x00}, {0x40, 0x08, 0x00} },
137 .port_ops = &opti621_port_ops,
138 .host_flags = IDE_HFLAG_NO_DMA,
139 .pio_mask = ATA_PIO4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800142static int opti621_init_one(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +0200144 return ide_pci_init_one(dev, &opti621_chipset, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200147static const struct pci_device_id opti621_pci_tbl[] = {
148 { PCI_VDEVICE(OPTI, PCI_DEVICE_ID_OPTI_82C621), 0 },
Bartlomiej Zolnierkiewicz80a65fc2008-06-15 21:00:22 +0200149 { PCI_VDEVICE(OPTI, PCI_DEVICE_ID_OPTI_82C825), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 { 0, },
151};
152MODULE_DEVICE_TABLE(pci, opti621_pci_tbl);
153
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200154static struct pci_driver opti621_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .name = "Opti621_IDE",
156 .id_table = opti621_pci_tbl,
157 .probe = opti621_init_one,
Bartlomiej Zolnierkiewiczadc7f852008-07-24 22:53:23 +0200158 .remove = ide_pci_remove,
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +0200159 .suspend = ide_pci_suspend,
160 .resume = ide_pci_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +0100163static int __init opti621_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200165 return ide_pci_register_driver(&opti621_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Bartlomiej Zolnierkiewiczadc7f852008-07-24 22:53:23 +0200168static void __exit opti621_ide_exit(void)
169{
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200170 pci_unregister_driver(&opti621_pci_driver);
Bartlomiej Zolnierkiewiczadc7f852008-07-24 22:53:23 +0200171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173module_init(opti621_ide_init);
Bartlomiej Zolnierkiewiczadc7f852008-07-24 22:53:23 +0200174module_exit(opti621_ide_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176MODULE_AUTHOR("Jaromir Koutek, Jan Harkes, Mark Lord");
177MODULE_DESCRIPTION("PCI driver module for Opti621 IDE");
178MODULE_LICENSE("GPL");