]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/ide/cy82c693.c
via82cxxx: add support for vt8261 and future chips
[mv-sheeva.git] / drivers / ide / cy82c693.c
1 /*
2  *  Copyright (C) 1998-2000 Andreas S. Krebs (akrebs@altavista.net), Maintainer
3  *  Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>, Integrator
4  *  Copyright (C) 2007-2010 Bartlomiej Zolnierkiewicz
5  *
6  * CYPRESS CY82C693 chipset IDE controller
7  *
8  * The CY82C693 chipset is used on Digital's PC-Alpha 164SX boards.
9  */
10
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/pci.h>
14 #include <linux/ide.h>
15 #include <linux/init.h>
16
17 #include <asm/io.h>
18
19 #define DRV_NAME "cy82c693"
20
21 /*
22  *      NOTE: the value for busmaster timeout is tricky and I got it by
23  *      trial and error!  By using a to low value will cause DMA timeouts
24  *      and drop IDE performance, and by using a to high value will cause
25  *      audio playback to scatter.
26  *      If you know a better value or how to calc it, please let me know.
27  */
28
29 /* twice the value written in cy82c693ub datasheet */
30 #define BUSMASTER_TIMEOUT       0x50
31 /*
32  * the value above was tested on my machine and it seems to work okay
33  */
34
35 /* here are the offset definitions for the registers */
36 #define CY82_IDE_CMDREG         0x04
37 #define CY82_IDE_ADDRSETUP      0x48
38 #define CY82_IDE_MASTER_IOR     0x4C
39 #define CY82_IDE_MASTER_IOW     0x4D
40 #define CY82_IDE_SLAVE_IOR      0x4E
41 #define CY82_IDE_SLAVE_IOW      0x4F
42 #define CY82_IDE_MASTER_8BIT    0x50
43 #define CY82_IDE_SLAVE_8BIT     0x51
44
45 #define CY82_INDEX_PORT         0x22
46 #define CY82_DATA_PORT          0x23
47
48 #define CY82_INDEX_CHANNEL0     0x30
49 #define CY82_INDEX_CHANNEL1     0x31
50 #define CY82_INDEX_TIMEOUT      0x32
51
52 /*
53  * set DMA mode a specific channel for CY82C693
54  */
55
56 static void cy82c693_set_dma_mode(ide_drive_t *drive, const u8 mode)
57 {
58         ide_hwif_t *hwif = drive->hwif;
59         u8 single = (mode & 0x10) >> 4, index = 0, data = 0;
60
61         index = hwif->channel ? CY82_INDEX_CHANNEL1 : CY82_INDEX_CHANNEL0;
62
63         data = (mode & 3) | (single << 2);
64
65         outb(index, CY82_INDEX_PORT);
66         outb(data, CY82_DATA_PORT);
67
68         /*
69          * note: below we set the value for Bus Master IDE TimeOut Register
70          * I'm not absolutly sure what this does, but it solved my problem
71          * with IDE DMA and sound, so I now can play sound and work with
72          * my IDE driver at the same time :-)
73          *
74          * If you know the correct (best) value for this register please
75          * let me know - ASK
76          */
77
78         data = BUSMASTER_TIMEOUT;
79         outb(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT);
80         outb(data, CY82_DATA_PORT);
81 }
82
83 static void cy82c693_set_pio_mode(ide_drive_t *drive, const u8 pio)
84 {
85         ide_hwif_t *hwif = drive->hwif;
86         struct pci_dev *dev = to_pci_dev(hwif->dev);
87         int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
88         const unsigned long T = 1000000 / bus_speed;
89         unsigned int addrCtrl;
90         struct ide_timing t;
91         u8 time_16, time_8;
92
93         /* select primary or secondary channel */
94         if (hwif->index > 0) {  /* drive is on the secondary channel */
95                 dev = pci_get_slot(dev->bus, dev->devfn+1);
96                 if (!dev) {
97                         printk(KERN_ERR "%s: tune_drive: "
98                                 "Cannot find secondary interface!\n",
99                                 drive->name);
100                         return;
101                 }
102         }
103
104         ide_timing_compute(drive, XFER_PIO_0 + pio, &t, T, 1);
105
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);
110
111         /* now let's write  the clocks registers */
112         if ((drive->dn & 1) == 0) {
113                 /*
114                  * set master drive
115                  * address setup control register
116                  * is 32 bit !!!
117                  */
118                 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
119
120                 addrCtrl &= (~0xF);
121                 addrCtrl |= clamp_val(t.setup - 1, 0, 15);
122                 pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
123
124                 /* now let's set the remaining registers */
125                 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);
128         } else {
129                 /*
130                  * set slave drive
131                  * address setup control register
132                  * is 32 bit !!!
133                  */
134                 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
135
136                 addrCtrl &= (~0xF0);
137                 addrCtrl |= (clamp_val(t.setup - 1, 0, 15) << 4);
138                 pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
139
140                 /* now let's set the remaining registers */
141                 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);
144         }
145 }
146
147 static void __devinit init_iops_cy82c693(ide_hwif_t *hwif)
148 {
149         static ide_hwif_t *primary;
150         struct pci_dev *dev = to_pci_dev(hwif->dev);
151
152         if (PCI_FUNC(dev->devfn) == 1)
153                 primary = hwif;
154         else {
155                 hwif->mate = primary;
156                 hwif->channel = 1;
157         }
158 }
159
160 static const struct ide_port_ops cy82c693_port_ops = {
161         .set_pio_mode           = cy82c693_set_pio_mode,
162         .set_dma_mode           = cy82c693_set_dma_mode,
163 };
164
165 static const struct ide_port_info cy82c693_chipset __devinitdata = {
166         .name           = DRV_NAME,
167         .init_iops      = init_iops_cy82c693,
168         .port_ops       = &cy82c693_port_ops,
169         .host_flags     = IDE_HFLAG_SINGLE,
170         .pio_mask       = ATA_PIO4,
171         .swdma_mask     = ATA_SWDMA2,
172         .mwdma_mask     = ATA_MWDMA2,
173 };
174
175 static int __devinit cy82c693_init_one(struct pci_dev *dev, const struct pci_device_id *id)
176 {
177         struct pci_dev *dev2;
178         int ret = -ENODEV;
179
180         /* CY82C693 is more than only a IDE controller.
181            Function 1 is primary IDE channel, function 2 - secondary. */
182         if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
183             PCI_FUNC(dev->devfn) == 1) {
184                 dev2 = pci_get_slot(dev->bus, dev->devfn + 1);
185                 ret = ide_pci_init_two(dev, dev2, &cy82c693_chipset, NULL);
186                 if (ret)
187                         pci_dev_put(dev2);
188         }
189         return ret;
190 }
191
192 static void __devexit cy82c693_remove(struct pci_dev *dev)
193 {
194         struct ide_host *host = pci_get_drvdata(dev);
195         struct pci_dev *dev2 = host->dev[1] ? to_pci_dev(host->dev[1]) : NULL;
196
197         ide_pci_remove(dev);
198         pci_dev_put(dev2);
199 }
200
201 static const struct pci_device_id cy82c693_pci_tbl[] = {
202         { PCI_VDEVICE(CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693), 0 },
203         { 0, },
204 };
205 MODULE_DEVICE_TABLE(pci, cy82c693_pci_tbl);
206
207 static struct pci_driver cy82c693_pci_driver = {
208         .name           = "Cypress_IDE",
209         .id_table       = cy82c693_pci_tbl,
210         .probe          = cy82c693_init_one,
211         .remove         = __devexit_p(cy82c693_remove),
212         .suspend        = ide_pci_suspend,
213         .resume         = ide_pci_resume,
214 };
215
216 static int __init cy82c693_ide_init(void)
217 {
218         return ide_pci_register_driver(&cy82c693_pci_driver);
219 }
220
221 static void __exit cy82c693_ide_exit(void)
222 {
223         pci_unregister_driver(&cy82c693_pci_driver);
224 }
225
226 module_init(cy82c693_ide_init);
227 module_exit(cy82c693_ide_exit);
228
229 MODULE_AUTHOR("Andreas Krebs, Andre Hedrick, Bartlomiej Zolnierkiewicz");
230 MODULE_DESCRIPTION("PCI driver module for the Cypress CY82C693 IDE");
231 MODULE_LICENSE("GPL");