blob: bc01660ee8fd383c9e651759d2d21c02c3a10d03 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1998-2000 Andreas S. Krebs (akrebs@altavista.net), Maintainer
4 * Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>, Integrator
Bartlomiej Zolnierkiewicz0ab3d8b2011-10-13 00:28:54 +00005 * Copyright (C) 2007-2011 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * CYPRESS CY82C693 chipset IDE controller
8 *
9 * The CY82C693 chipset is used on Digital's PC-Alpha 164SX boards.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/ide.h>
16#include <linux/init.h>
17
18#include <asm/io.h>
19
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +020020#define DRV_NAME "cy82c693"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * NOTE: the value for busmaster timeout is tricky and I got it by
24 * trial and error! By using a to low value will cause DMA timeouts
25 * and drop IDE performance, and by using a to high value will cause
26 * audio playback to scatter.
27 * If you know a better value or how to calc it, please let me know.
28 */
29
30/* twice the value written in cy82c693ub datasheet */
31#define BUSMASTER_TIMEOUT 0x50
32/*
33 * the value above was tested on my machine and it seems to work okay
34 */
35
36/* here are the offset definitions for the registers */
37#define CY82_IDE_CMDREG 0x04
38#define CY82_IDE_ADDRSETUP 0x48
39#define CY82_IDE_MASTER_IOR 0x4C
40#define CY82_IDE_MASTER_IOW 0x4D
41#define CY82_IDE_SLAVE_IOR 0x4E
42#define CY82_IDE_SLAVE_IOW 0x4F
43#define CY82_IDE_MASTER_8BIT 0x50
44#define CY82_IDE_SLAVE_8BIT 0x51
45
46#define CY82_INDEX_PORT 0x22
47#define CY82_DATA_PORT 0x23
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define CY82_INDEX_CHANNEL0 0x30
50#define CY82_INDEX_CHANNEL1 0x31
51#define CY82_INDEX_TIMEOUT 0x32
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * set DMA mode a specific channel for CY82C693
55 */
56
Bartlomiej Zolnierkiewicz87761682010-01-19 01:45:29 -080057static void cy82c693_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Bartlomiej Zolnierkiewicz87761682010-01-19 01:45:29 -080059 const u8 mode = drive->dma_mode;
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +010060 u8 single = (mode & 0x10) >> 4, index = 0, data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +010062 index = hwif->channel ? CY82_INDEX_CHANNEL1 : CY82_INDEX_CHANNEL0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +010064 data = (mode & 3) | (single << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +010066 outb(index, CY82_INDEX_PORT);
67 outb(data, CY82_DATA_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +020069 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * note: below we set the value for Bus Master IDE TimeOut Register
Lucas De Marchi25985ed2011-03-30 22:57:33 -030071 * I'm not absolutely sure what this does, but it solved my problem
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * with IDE DMA and sound, so I now can play sound and work with
73 * my IDE driver at the same time :-)
74 *
75 * If you know the correct (best) value for this register please
76 * let me know - ASK
77 */
78
79 data = BUSMASTER_TIMEOUT;
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +010080 outb(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT);
81 outb(data, CY82_DATA_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Bartlomiej Zolnierkiewicze085b3c2010-01-19 01:44:41 -080084static void cy82c693_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +010086 struct pci_dev *dev = to_pci_dev(hwif->dev);
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +000087 int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
88 const unsigned long T = 1000000 / bus_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned int addrCtrl;
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +000090 struct ide_timing t;
91 u8 time_16, time_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /* select primary or secondary channel */
Bartlomiej Zolnierkiewicz0ab3d8b2011-10-13 00:28:54 +000094 if (drive->dn > 1) { /* drive is on the secondary channel */
Alan Cox652aa162006-10-03 01:14:35 -070095 dev = pci_get_slot(dev->bus, dev->devfn+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (!dev) {
97 printk(KERN_ERR "%s: tune_drive: "
98 "Cannot find secondary interface!\n",
99 drive->name);
100 return;
101 }
102 }
103
Bartlomiej Zolnierkiewicze085b3c2010-01-19 01:44:41 -0800104 ide_timing_compute(drive, drive->pio_mode, &t, T, 1);
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000105
106 time_16 = clamp_val(t.recover - 1, 0, 15) |
107 (clamp_val(t.active - 1, 0, 15) << 4);
108 time_8 = clamp_val(t.act8b - 1, 0, 15) |
109 (clamp_val(t.rec8b - 1, 0, 15) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* now let's write the clocks registers */
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200112 if ((drive->dn & 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /*
114 * set master drive
115 * address setup control register
116 * is 32 bit !!!
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 addrCtrl &= (~0xF);
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000121 addrCtrl |= clamp_val(t.setup - 1, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
123
124 /* now let's set the remaining registers */
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000125 pci_write_config_byte(dev, CY82_IDE_MASTER_IOR, time_16);
126 pci_write_config_byte(dev, CY82_IDE_MASTER_IOW, time_16);
127 pci_write_config_byte(dev, CY82_IDE_MASTER_8BIT, time_8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 } else {
129 /*
130 * set slave drive
131 * address setup control register
132 * is 32 bit !!!
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
135
136 addrCtrl &= (~0xF0);
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000137 addrCtrl |= (clamp_val(t.setup - 1, 0, 15) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
139
140 /* now let's set the remaining registers */
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000141 pci_write_config_byte(dev, CY82_IDE_SLAVE_IOR, time_16);
142 pci_write_config_byte(dev, CY82_IDE_SLAVE_IOW, time_16);
143 pci_write_config_byte(dev, CY82_IDE_SLAVE_8BIT, time_8);
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200144 }
Bartlomiej Zolnierkiewicz0ab3d8b2011-10-13 00:28:54 +0000145 if (drive->dn > 1)
Julia Lawall03028992011-08-04 01:30:34 -0700146 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800149static void init_iops_cy82c693(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Bartlomiej Zolnierkiewiczf32d26a2007-10-26 20:31:15 +0200151 static ide_hwif_t *primary;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100152 struct pci_dev *dev = to_pci_dev(hwif->dev);
Bartlomiej Zolnierkiewiczf32d26a2007-10-26 20:31:15 +0200153
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100154 if (PCI_FUNC(dev->devfn) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 primary = hwif;
156 else {
157 hwif->mate = primary;
158 hwif->channel = 1;
159 }
160}
161
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200162static const struct ide_port_ops cy82c693_port_ops = {
163 .set_pio_mode = cy82c693_set_pio_mode,
164 .set_dma_mode = cy82c693_set_dma_mode,
165};
166
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800167static const struct ide_port_info cy82c693_chipset = {
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +0200168 .name = DRV_NAME,
Bartlomiej Zolnierkiewicz7b77d862007-02-17 02:40:24 +0100169 .init_iops = init_iops_cy82c693,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200170 .port_ops = &cy82c693_port_ops,
Bartlomiej Zolnierkiewicz951784b2008-04-26 17:36:38 +0200171 .host_flags = IDE_HFLAG_SINGLE,
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200172 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100173 .swdma_mask = ATA_SWDMA2,
174 .mwdma_mask = ATA_MWDMA2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800177static int cy82c693_init_one(struct pci_dev *dev,
178 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct pci_dev *dev2;
181 int ret = -ENODEV;
182
183 /* CY82C693 is more than only a IDE controller.
184 Function 1 is primary IDE channel, function 2 - secondary. */
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200185 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 PCI_FUNC(dev->devfn) == 1) {
Alan Cox652aa162006-10-03 01:14:35 -0700187 dev2 = pci_get_slot(dev->bus, dev->devfn + 1);
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +0200188 ret = ide_pci_init_two(dev, dev2, &cy82c693_chipset, NULL);
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200189 if (ret)
190 pci_dev_put(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192 return ret;
193}
194
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800195static void cy82c693_remove(struct pci_dev *dev)
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200196{
197 struct ide_host *host = pci_get_drvdata(dev);
198 struct pci_dev *dev2 = host->dev[1] ? to_pci_dev(host->dev[1]) : NULL;
199
200 ide_pci_remove(dev);
201 pci_dev_put(dev2);
202}
203
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200204static const struct pci_device_id cy82c693_pci_tbl[] = {
205 { PCI_VDEVICE(CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 { 0, },
207};
208MODULE_DEVICE_TABLE(pci, cy82c693_pci_tbl);
209
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200210static struct pci_driver cy82c693_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .name = "Cypress_IDE",
212 .id_table = cy82c693_pci_tbl,
213 .probe = cy82c693_init_one,
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800214 .remove = cy82c693_remove,
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +0200215 .suspend = ide_pci_suspend,
216 .resume = ide_pci_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +0100219static int __init cy82c693_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200221 return ide_pci_register_driver(&cy82c693_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200224static void __exit cy82c693_ide_exit(void)
225{
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200226 pci_unregister_driver(&cy82c693_pci_driver);
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229module_init(cy82c693_ide_init);
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200230module_exit(cy82c693_ide_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000232MODULE_AUTHOR("Andreas Krebs, Andre Hedrick, Bartlomiej Zolnierkiewicz");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233MODULE_DESCRIPTION("PCI driver module for the Cypress CY82C693 IDE");
234MODULE_LICENSE("GPL");