Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Copyright (C) 1996-1998 Linus Torvalds & authors (see below) |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Authors: |
| 8 | * Jaromir Koutek <miri@punknet.cz>, |
| 9 | * Jan Harkes <jaharkes@cwi.nl>, |
| 10 | * Mark Lord <mlord@pobox.com> |
| 11 | * Some parts of code are from ali14xx.c and from rz1000.c. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/ide.h> |
| 19 | |
| 20 | #include <asm/io.h> |
| 21 | |
Bartlomiej Zolnierkiewicz | ced3ec8 | 2008-07-24 22:53:32 +0200 | [diff] [blame] | 22 | #define DRV_NAME "opti621" |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #define READ_REG 0 /* index of Read cycle timing register */ |
| 25 | #define WRITE_REG 1 /* index of Write cycle timing register */ |
| 26 | #define CNTRL_REG 3 /* index of Control register */ |
| 27 | #define STRAP_REG 5 /* index of Strap register */ |
| 28 | #define MISC_REG 6 /* index of Miscellaneous register */ |
| 29 | |
| 30 | static int reg_base; |
| 31 | |
Bartlomiej Zolnierkiewicz | e65dde7 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 32 | static DEFINE_SPINLOCK(opti621_lock); |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | /* Write value to register reg, base of register |
| 35 | * is at reg_base (0x1f0 primary, 0x170 secondary, |
| 36 | * if not changed by PCI configuration). |
| 37 | * This is from setupvic.exe program. |
| 38 | */ |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 39 | static void write_reg(u8 value, int reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 41 | inw(reg_base + 1); |
| 42 | inw(reg_base + 1); |
| 43 | outb(3, reg_base + 2); |
| 44 | outb(value, reg_base + reg); |
| 45 | outb(0x83, reg_base + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* Read value from register reg, base of register |
| 49 | * is at reg_base (0x1f0 primary, 0x170 secondary, |
| 50 | * if not changed by PCI configuration). |
| 51 | * This is from setupvic.exe program. |
| 52 | */ |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 53 | static u8 read_reg(int reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | u8 ret = 0; |
| 56 | |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 57 | inw(reg_base + 1); |
| 58 | inw(reg_base + 1); |
| 59 | outb(3, reg_base + 2); |
| 60 | ret = inb(reg_base + reg); |
| 61 | outb(0x83, reg_base + 2); |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return ret; |
| 64 | } |
| 65 | |
Bartlomiej Zolnierkiewicz | e085b3c | 2010-01-19 01:44:41 -0800 | [diff] [blame] | 66 | static void opti621_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Bartlomiej Zolnierkiewicz | 7e59ea2 | 2008-10-10 22:39:26 +0200 | [diff] [blame] | 68 | ide_drive_t *pair = ide_get_pair_dev(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | unsigned long flags; |
Bartlomiej Zolnierkiewicz | e085b3c | 2010-01-19 01:44:41 -0800 | [diff] [blame] | 70 | unsigned long mode = drive->pio_mode, pair_mode; |
| 71 | const u8 pio = mode - XFER_PIO_0; |
Bartlomiej Zolnierkiewicz | 810253d | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 72 | u8 tim, misc, addr_pio = pio, clk; |
| 73 | |
| 74 | /* DRDY is default 2 (by OPTi Databook) */ |
Bartlomiej Zolnierkiewicz | 80a65fc | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 75 | static const u8 addr_timings[2][5] = { |
| 76 | { 0x20, 0x10, 0x00, 0x00, 0x00 }, /* 33 MHz */ |
| 77 | { 0x10, 0x10, 0x00, 0x00, 0x00 }, /* 25 MHz */ |
Bartlomiej Zolnierkiewicz | 810253d | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 78 | }; |
Bartlomiej Zolnierkiewicz | 80a65fc | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 79 | static const u8 data_rec_timings[2][5] = { |
| 80 | { 0x5b, 0x45, 0x32, 0x21, 0x20 }, /* 33 MHz */ |
| 81 | { 0x48, 0x34, 0x21, 0x10, 0x10 } /* 25 MHz */ |
Bartlomiej Zolnierkiewicz | 810253d | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 82 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Joao Ramos | 5bfb151f | 2009-06-15 22:13:44 +0200 | [diff] [blame] | 84 | ide_set_drivedata(drive, (void *)mode); |
Bartlomiej Zolnierkiewicz | 6c98718 | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 85 | |
Bartlomiej Zolnierkiewicz | 7e59ea2 | 2008-10-10 22:39:26 +0200 | [diff] [blame] | 86 | if (pair) { |
Joao Ramos | 5bfb151f | 2009-06-15 22:13:44 +0200 | [diff] [blame] | 87 | pair_mode = (unsigned long)ide_get_drivedata(pair); |
| 88 | if (pair_mode && pair_mode < mode) |
| 89 | addr_pio = pair_mode - XFER_PIO_0; |
Bartlomiej Zolnierkiewicz | 6c98718 | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Bartlomiej Zolnierkiewicz | 21bd33a | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 92 | spin_lock_irqsave(&opti621_lock, flags); |
| 93 | |
| 94 | reg_base = hwif->io_ports.data_addr; |
| 95 | |
| 96 | /* allow Register-B */ |
| 97 | outb(0xc0, reg_base + CNTRL_REG); |
| 98 | /* hmm, setupvic.exe does this ;-) */ |
| 99 | outb(0xff, reg_base + 5); |
| 100 | /* if reads 0xff, adapter not exist? */ |
| 101 | (void)inb(reg_base + CNTRL_REG); |
| 102 | /* if reads 0xc0, no interface exist? */ |
| 103 | read_reg(CNTRL_REG); |
| 104 | |
| 105 | /* check CLK speed */ |
| 106 | clk = read_reg(STRAP_REG) & 1; |
| 107 | |
| 108 | printk(KERN_INFO "%s: CLK = %d MHz\n", hwif->name, clk ? 25 : 33); |
| 109 | |
Bartlomiej Zolnierkiewicz | 810253d | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 110 | tim = data_rec_timings[clk][pio]; |
| 111 | misc = addr_timings[clk][addr_pio]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Bartlomiej Zolnierkiewicz | 6c98718 | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 113 | /* select Index-0/1 for Register-A/B */ |
Bartlomiej Zolnierkiewicz | 123995b | 2008-10-13 21:39:40 +0200 | [diff] [blame] | 114 | write_reg(drive->dn & 1, MISC_REG); |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 115 | /* set read cycle timings */ |
Bartlomiej Zolnierkiewicz | 810253d | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 116 | write_reg(tim, READ_REG); |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 117 | /* set write cycle timings */ |
Bartlomiej Zolnierkiewicz | 810253d | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 118 | write_reg(tim, WRITE_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* use Register-A for drive 0 */ |
| 121 | /* use Register-B for drive 1 */ |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 122 | write_reg(0x85, CNTRL_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | /* set address setup, DRDY timings, */ |
| 125 | /* and read prefetch for both drives */ |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 126 | write_reg(misc, MISC_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Bartlomiej Zolnierkiewicz | e65dde7 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 128 | spin_unlock_irqrestore(&opti621_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 131 | static const struct ide_port_ops opti621_port_ops = { |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 132 | .set_pio_mode = opti621_set_pio_mode, |
| 133 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Greg Kroah-Hartman | fe31edc | 2012-12-21 13:21:03 -0800 | [diff] [blame] | 135 | static const struct ide_port_info opti621_chipset = { |
Bartlomiej Zolnierkiewicz | ced3ec8 | 2008-07-24 22:53:32 +0200 | [diff] [blame] | 136 | .name = DRV_NAME, |
Bartlomiej Zolnierkiewicz | 80a65fc | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 137 | .enablebits = { {0x45, 0x80, 0x00}, {0x40, 0x08, 0x00} }, |
| 138 | .port_ops = &opti621_port_ops, |
| 139 | .host_flags = IDE_HFLAG_NO_DMA, |
| 140 | .pio_mask = ATA_PIO4, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Greg Kroah-Hartman | fe31edc | 2012-12-21 13:21:03 -0800 | [diff] [blame] | 143 | static int opti621_init_one(struct pci_dev *dev, const struct pci_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Bartlomiej Zolnierkiewicz | 6cdf6eb | 2008-07-24 22:53:14 +0200 | [diff] [blame] | 145 | return ide_pci_init_one(dev, &opti621_chipset, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Bartlomiej Zolnierkiewicz | 9cbcc5e | 2007-10-16 22:29:56 +0200 | [diff] [blame] | 148 | static const struct pci_device_id opti621_pci_tbl[] = { |
| 149 | { PCI_VDEVICE(OPTI, PCI_DEVICE_ID_OPTI_82C621), 0 }, |
Bartlomiej Zolnierkiewicz | 80a65fc | 2008-06-15 21:00:22 +0200 | [diff] [blame] | 150 | { PCI_VDEVICE(OPTI, PCI_DEVICE_ID_OPTI_82C825), 0 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { 0, }, |
| 152 | }; |
| 153 | MODULE_DEVICE_TABLE(pci, opti621_pci_tbl); |
| 154 | |
Bartlomiej Zolnierkiewicz | a9ab09e2 | 2008-10-13 21:39:41 +0200 | [diff] [blame] | 155 | static struct pci_driver opti621_pci_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | .name = "Opti621_IDE", |
| 157 | .id_table = opti621_pci_tbl, |
| 158 | .probe = opti621_init_one, |
Bartlomiej Zolnierkiewicz | adc7f85 | 2008-07-24 22:53:23 +0200 | [diff] [blame] | 159 | .remove = ide_pci_remove, |
Bartlomiej Zolnierkiewicz | feb22b7 | 2008-10-10 22:39:32 +0200 | [diff] [blame] | 160 | .suspend = ide_pci_suspend, |
| 161 | .resume = ide_pci_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Bartlomiej Zolnierkiewicz | 82ab1ee | 2007-01-27 13:46:56 +0100 | [diff] [blame] | 164 | static int __init opti621_ide_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
Bartlomiej Zolnierkiewicz | a9ab09e2 | 2008-10-13 21:39:41 +0200 | [diff] [blame] | 166 | return ide_pci_register_driver(&opti621_pci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Bartlomiej Zolnierkiewicz | adc7f85 | 2008-07-24 22:53:23 +0200 | [diff] [blame] | 169 | static void __exit opti621_ide_exit(void) |
| 170 | { |
Bartlomiej Zolnierkiewicz | a9ab09e2 | 2008-10-13 21:39:41 +0200 | [diff] [blame] | 171 | pci_unregister_driver(&opti621_pci_driver); |
Bartlomiej Zolnierkiewicz | adc7f85 | 2008-07-24 22:53:23 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | module_init(opti621_ide_init); |
Bartlomiej Zolnierkiewicz | adc7f85 | 2008-07-24 22:53:23 +0200 | [diff] [blame] | 175 | module_exit(opti621_ide_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | MODULE_AUTHOR("Jaromir Koutek, Jan Harkes, Mark Lord"); |
| 178 | MODULE_DESCRIPTION("PCI driver module for Opti621 IDE"); |
| 179 | MODULE_LICENSE("GPL"); |