blob: 49dfb8d40dcf79b8195fa60c5c1c60020f94e82d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1998-2000 Andreas S. Krebs (akrebs@altavista.net), Maintainer
3 * Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>, Integrator
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +00004 * Copyright (C) 2007-2010 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * CYPRESS CY82C693 chipset IDE controller
7 *
8 * The CY82C693 chipset is used on Digital's PC-Alpha 164SX boards.
9 * Writing the driver was quite simple, since most of the job is
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +020010 * done by the generic pci-ide support.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * The hard part was finding the CY82C693's datasheet on Cypress's
12 * web page :-(. But Altavista solved this problem :-).
13 *
14 *
15 * Notes:
16 * - I recently got a 16.8G IBM DTTA, so I was able to test it with
17 * a large and fast disk - the results look great, so I'd say the
18 * driver is working fine :-)
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +020019 * hdparm -t reports 8.17 MB/sec at about 6% CPU usage for the DTTA
20 * - this is my first linux driver, so there's probably a lot of room
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * for optimizations and bug fixing, so feel free to do it.
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +020022 * - if using PIO mode it's a good idea to set the PIO mode and
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * 32-bit I/O support (if possible), e.g. hdparm -p2 -c1 /dev/hda
24 * - I had some problems with my IBM DHEA with PIO modes < 2
25 * (lost interrupts) ?????
26 * - first tests with DMA look okay, they seem to work, but there is a
27 * problem with sound - the BusMaster IDE TimeOut should fixed this
28 *
29 * Ancient History:
30 * AMH@1999-08-24: v0.34 init_cy82c693_chip moved to pci_init_cy82c693
31 * ASK@1999-01-23: v0.33 made a few minor code clean ups
32 * removed DMA clock speed setting by default
33 * added boot message
34 * ASK@1998-11-01: v0.32 added support to set BusMaster IDE TimeOut
35 * added support to set DMA Controller Clock Speed
36 * ASK@1998-10-31: v0.31 fixed problem with setting to high DMA modes
37 * on some drives.
38 * ASK@1998-10-29: v0.3 added support to set DMA modes
39 * ASK@1998-10-28: v0.2 added support to set PIO modes
40 * ASK@1998-10-27: v0.1 first version - chipset detection
41 *
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/types.h>
46#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/ide.h>
48#include <linux/init.h>
49
50#include <asm/io.h>
51
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +020052#define DRV_NAME "cy82c693"
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * NOTE: the value for busmaster timeout is tricky and I got it by
56 * trial and error! By using a to low value will cause DMA timeouts
57 * and drop IDE performance, and by using a to high value will cause
58 * audio playback to scatter.
59 * If you know a better value or how to calc it, please let me know.
60 */
61
62/* twice the value written in cy82c693ub datasheet */
63#define BUSMASTER_TIMEOUT 0x50
64/*
65 * the value above was tested on my machine and it seems to work okay
66 */
67
68/* here are the offset definitions for the registers */
69#define CY82_IDE_CMDREG 0x04
70#define CY82_IDE_ADDRSETUP 0x48
71#define CY82_IDE_MASTER_IOR 0x4C
72#define CY82_IDE_MASTER_IOW 0x4D
73#define CY82_IDE_SLAVE_IOR 0x4E
74#define CY82_IDE_SLAVE_IOW 0x4F
75#define CY82_IDE_MASTER_8BIT 0x50
76#define CY82_IDE_SLAVE_8BIT 0x51
77
78#define CY82_INDEX_PORT 0x22
79#define CY82_DATA_PORT 0x23
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define CY82_INDEX_CHANNEL0 0x30
82#define CY82_INDEX_CHANNEL1 0x31
83#define CY82_INDEX_TIMEOUT 0x32
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
86 * set DMA mode a specific channel for CY82C693
87 */
88
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +010089static void cy82c693_set_dma_mode(ide_drive_t *drive, const u8 mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +010091 ide_hwif_t *hwif = drive->hwif;
92 u8 single = (mode & 0x10) >> 4, index = 0, data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +010094 index = hwif->channel ? CY82_INDEX_CHANNEL1 : CY82_INDEX_CHANNEL0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +010096 data = (mode & 3) | (single << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +010098 outb(index, CY82_INDEX_PORT);
99 outb(data, CY82_DATA_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200101 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * note: below we set the value for Bus Master IDE TimeOut Register
103 * I'm not absolutly sure what this does, but it solved my problem
104 * with IDE DMA and sound, so I now can play sound and work with
105 * my IDE driver at the same time :-)
106 *
107 * If you know the correct (best) value for this register please
108 * let me know - ASK
109 */
110
111 data = BUSMASTER_TIMEOUT;
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100112 outb(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT);
113 outb(data, CY82_DATA_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200116static void cy82c693_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100118 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100119 struct pci_dev *dev = to_pci_dev(hwif->dev);
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000120 int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
121 const unsigned long T = 1000000 / bus_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned int addrCtrl;
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000123 struct ide_timing t;
124 u8 time_16, time_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /* select primary or secondary channel */
127 if (hwif->index > 0) { /* drive is on the secondary channel */
Alan Cox652aa162006-10-03 01:14:35 -0700128 dev = pci_get_slot(dev->bus, dev->devfn+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (!dev) {
130 printk(KERN_ERR "%s: tune_drive: "
131 "Cannot find secondary interface!\n",
132 drive->name);
133 return;
134 }
135 }
136
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000137 ide_timing_compute(drive, XFER_PIO_0 + pio, &t, T, 1);
138
139 time_16 = clamp_val(t.recover - 1, 0, 15) |
140 (clamp_val(t.active - 1, 0, 15) << 4);
141 time_8 = clamp_val(t.act8b - 1, 0, 15) |
142 (clamp_val(t.rec8b - 1, 0, 15) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* now let's write the clocks registers */
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200145 if ((drive->dn & 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /*
147 * set master drive
148 * address setup control register
149 * is 32 bit !!!
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 addrCtrl &= (~0xF);
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000154 addrCtrl |= clamp_val(t.setup - 1, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
156
157 /* now let's set the remaining registers */
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000158 pci_write_config_byte(dev, CY82_IDE_MASTER_IOR, time_16);
159 pci_write_config_byte(dev, CY82_IDE_MASTER_IOW, time_16);
160 pci_write_config_byte(dev, CY82_IDE_MASTER_8BIT, time_8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 } else {
162 /*
163 * set slave drive
164 * address setup control register
165 * is 32 bit !!!
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
168
169 addrCtrl &= (~0xF0);
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000170 addrCtrl |= (clamp_val(t.setup - 1, 0, 15) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
172
173 /* now let's set the remaining registers */
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000174 pci_write_config_byte(dev, CY82_IDE_SLAVE_IOR, time_16);
175 pci_write_config_byte(dev, CY82_IDE_SLAVE_IOW, time_16);
176 pci_write_config_byte(dev, CY82_IDE_SLAVE_8BIT, time_8);
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Adrian Bunke851b622005-11-09 23:07:56 +0100180static void __devinit init_iops_cy82c693(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Bartlomiej Zolnierkiewiczf32d26a2007-10-26 20:31:15 +0200182 static ide_hwif_t *primary;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100183 struct pci_dev *dev = to_pci_dev(hwif->dev);
Bartlomiej Zolnierkiewiczf32d26a2007-10-26 20:31:15 +0200184
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100185 if (PCI_FUNC(dev->devfn) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 primary = hwif;
187 else {
188 hwif->mate = primary;
189 hwif->channel = 1;
190 }
191}
192
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200193static const struct ide_port_ops cy82c693_port_ops = {
194 .set_pio_mode = cy82c693_set_pio_mode,
195 .set_dma_mode = cy82c693_set_dma_mode,
196};
197
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200198static const struct ide_port_info cy82c693_chipset __devinitdata = {
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +0200199 .name = DRV_NAME,
Bartlomiej Zolnierkiewicz7b77d862007-02-17 02:40:24 +0100200 .init_iops = init_iops_cy82c693,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200201 .port_ops = &cy82c693_port_ops,
Bartlomiej Zolnierkiewicz951784b2008-04-26 17:36:38 +0200202 .host_flags = IDE_HFLAG_SINGLE,
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200203 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100204 .swdma_mask = ATA_SWDMA2,
205 .mwdma_mask = ATA_MWDMA2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
208static int __devinit cy82c693_init_one(struct pci_dev *dev, const struct pci_device_id *id)
209{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct pci_dev *dev2;
211 int ret = -ENODEV;
212
213 /* CY82C693 is more than only a IDE controller.
214 Function 1 is primary IDE channel, function 2 - secondary. */
Paolo Ciarrocchi175f3542008-04-26 17:36:42 +0200215 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 PCI_FUNC(dev->devfn) == 1) {
Alan Cox652aa162006-10-03 01:14:35 -0700217 dev2 = pci_get_slot(dev->bus, dev->devfn + 1);
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +0200218 ret = ide_pci_init_two(dev, dev2, &cy82c693_chipset, NULL);
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200219 if (ret)
220 pci_dev_put(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 return ret;
223}
224
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200225static void __devexit cy82c693_remove(struct pci_dev *dev)
226{
227 struct ide_host *host = pci_get_drvdata(dev);
228 struct pci_dev *dev2 = host->dev[1] ? to_pci_dev(host->dev[1]) : NULL;
229
230 ide_pci_remove(dev);
231 pci_dev_put(dev2);
232}
233
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200234static const struct pci_device_id cy82c693_pci_tbl[] = {
235 { PCI_VDEVICE(CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 { 0, },
237};
238MODULE_DEVICE_TABLE(pci, cy82c693_pci_tbl);
239
Bartlomiej Zolnierkiewicza9ab09e2008-10-13 21:39:41 +0200240static struct pci_driver cy82c693_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .name = "Cypress_IDE",
242 .id_table = cy82c693_pci_tbl,
243 .probe = cy82c693_init_one,
Adrian Bunka69999e2008-08-18 21:40:03 +0200244 .remove = __devexit_p(cy82c693_remove),
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +0200245 .suspend = ide_pci_suspend,
246 .resume = ide_pci_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +0100249static int __init cy82c693_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Bartlomiej Zolnierkiewicza9ab09e2008-10-13 21:39:41 +0200251 return ide_pci_register_driver(&cy82c693_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200254static void __exit cy82c693_ide_exit(void)
255{
Bartlomiej Zolnierkiewicza9ab09e2008-10-13 21:39:41 +0200256 pci_unregister_driver(&cy82c693_pci_driver);
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259module_init(cy82c693_ide_init);
Bartlomiej Zolnierkiewiczcd688412008-07-24 22:53:21 +0200260module_exit(cy82c693_ide_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Bartlomiej Zolnierkiewicz4d6b3282010-01-18 07:18:47 +0000262MODULE_AUTHOR("Andreas Krebs, Andre Hedrick, Bartlomiej Zolnierkiewicz");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263MODULE_DESCRIPTION("PCI driver module for the Cypress CY82C693 IDE");
264MODULE_LICENSE("GPL");