Merge git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[pandora-kernel.git] / drivers / ide / pci / 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  *
5  * CYPRESS CY82C693 chipset IDE controller
6  *
7  * The CY82C693 chipset is used on Digital's PC-Alpha 164SX boards.
8  * Writing the driver was quite simple, since most of the job is
9  * done by the generic pci-ide support.
10  * The hard part was finding the CY82C693's datasheet on Cypress's
11  * web page :-(. But Altavista solved this problem :-).
12  *
13  *
14  * Notes:
15  * - I recently got a 16.8G IBM DTTA, so I was able to test it with
16  *   a large and fast disk - the results look great, so I'd say the
17  *   driver is working fine :-)
18  *   hdparm -t reports 8.17 MB/sec at about 6% CPU usage for the DTTA
19  * - this is my first linux driver, so there's probably a lot  of room
20  *   for optimizations and bug fixing, so feel free to do it.
21  * - if using PIO mode it's a good idea to set the PIO mode and
22  *   32-bit I/O support (if possible), e.g. hdparm -p2 -c1 /dev/hda
23  * - I had some problems with my IBM DHEA with PIO modes < 2
24  *   (lost interrupts) ?????
25  * - first tests with DMA look okay, they seem to work, but there is a
26  *   problem with sound - the BusMaster IDE TimeOut should fixed this
27  *
28  * Ancient History:
29  * AMH@1999-08-24: v0.34 init_cy82c693_chip moved to pci_init_cy82c693
30  * ASK@1999-01-23: v0.33 made a few minor code clean ups
31  *                       removed DMA clock speed setting by default
32  *                       added boot message
33  * ASK@1998-11-01: v0.32 added support to set BusMaster IDE TimeOut
34  *                       added support to set DMA Controller Clock Speed
35  * ASK@1998-10-31: v0.31 fixed problem with setting to high DMA modes
36  *                       on some drives.
37  * ASK@1998-10-29: v0.3 added support to set DMA modes
38  * ASK@1998-10-28: v0.2 added support to set PIO modes
39  * ASK@1998-10-27: v0.1 first version - chipset detection
40  *
41  */
42
43 #include <linux/module.h>
44 #include <linux/types.h>
45 #include <linux/pci.h>
46 #include <linux/ide.h>
47 #include <linux/init.h>
48
49 #include <asm/io.h>
50
51 /* the current version */
52 #define CY82_VERSION    "CY82C693U driver v0.34 99-13-12 Andreas S. Krebs (akrebs@altavista.net)"
53
54 /*
55  *      The following are used to debug the driver.
56  */
57 #define CY82C693_DEBUG_LOGS     0
58 #define CY82C693_DEBUG_INFO     0
59
60 /* define CY82C693_SETDMA_CLOCK to set DMA Controller Clock Speed to ATCLK */
61 #undef CY82C693_SETDMA_CLOCK
62
63 /*
64  *      NOTE: the value for busmaster timeout is tricky and I got it by
65  *      trial and error!  By using a to low value will cause DMA timeouts
66  *      and drop IDE performance, and by using a to high value will cause
67  *      audio playback to scatter.
68  *      If you know a better value or how to calc it, please let me know.
69  */
70
71 /* twice the value written in cy82c693ub datasheet */
72 #define BUSMASTER_TIMEOUT       0x50
73 /*
74  * the value above was tested on my machine and it seems to work okay
75  */
76
77 /* here are the offset definitions for the registers */
78 #define CY82_IDE_CMDREG         0x04
79 #define CY82_IDE_ADDRSETUP      0x48
80 #define CY82_IDE_MASTER_IOR     0x4C
81 #define CY82_IDE_MASTER_IOW     0x4D
82 #define CY82_IDE_SLAVE_IOR      0x4E
83 #define CY82_IDE_SLAVE_IOW      0x4F
84 #define CY82_IDE_MASTER_8BIT    0x50
85 #define CY82_IDE_SLAVE_8BIT     0x51
86
87 #define CY82_INDEX_PORT         0x22
88 #define CY82_DATA_PORT          0x23
89
90 #define CY82_INDEX_CTRLREG1     0x01
91 #define CY82_INDEX_CHANNEL0     0x30
92 #define CY82_INDEX_CHANNEL1     0x31
93 #define CY82_INDEX_TIMEOUT      0x32
94
95 /* the min and max PCI bus speed in MHz - from datasheet */
96 #define CY82C963_MIN_BUS_SPEED  25
97 #define CY82C963_MAX_BUS_SPEED  33
98
99 /* the struct for the PIO mode timings */
100 typedef struct pio_clocks_s {
101         u8      address_time;   /* Address setup (clocks) */
102         u8      time_16r;       /* clocks for 16bit IOR (0xF0=Active/data, 0x0F=Recovery) */
103         u8      time_16w;       /* clocks for 16bit IOW (0xF0=Active/data, 0x0F=Recovery) */
104         u8      time_8;         /* clocks for 8bit (0xF0=Active/data, 0x0F=Recovery) */
105 } pio_clocks_t;
106
107 /*
108  * calc clocks using bus_speed
109  * returns (rounded up) time in bus clocks for time in ns
110  */
111 static int calc_clk(int time, int bus_speed)
112 {
113         int clocks;
114
115         clocks = (time*bus_speed+999)/1000 - 1;
116
117         if (clocks < 0)
118                 clocks = 0;
119
120         if (clocks > 0x0F)
121                 clocks = 0x0F;
122
123         return clocks;
124 }
125
126 /*
127  * compute the values for the clock registers for PIO
128  * mode and pci_clk [MHz] speed
129  *
130  * NOTE: for mode 0,1 and 2 drives 8-bit IDE command control registers are used
131  *       for mode 3 and 4 drives 8 and 16-bit timings are the same
132  *
133  */
134 static void compute_clocks(u8 pio, pio_clocks_t *p_pclk)
135 {
136         struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
137         int clk1, clk2;
138         int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
139
140         /* we don't check against CY82C693's min and max speed,
141          * so you can play with the idebus=xx parameter
142          */
143
144         /* let's calc the address setup time clocks */
145         p_pclk->address_time = (u8)calc_clk(t->setup, bus_speed);
146
147         /* let's calc the active and recovery time clocks */
148         clk1 = calc_clk(t->active, bus_speed);
149
150         /* calc recovery timing */
151         clk2 = t->cycle - t->active - t->setup;
152
153         clk2 = calc_clk(clk2, bus_speed);
154
155         clk1 = (clk1<<4)|clk2;  /* combine active and recovery clocks */
156
157         /* note: we use the same values for 16bit IOR and IOW
158          *      those are all the same, since I don't have other
159          *      timings than those from ide-lib.c
160          */
161
162         p_pclk->time_16r = (u8)clk1;
163         p_pclk->time_16w = (u8)clk1;
164
165         /* what are good values for 8bit ?? */
166         p_pclk->time_8 = (u8)clk1;
167 }
168
169 /*
170  * set DMA mode a specific channel for CY82C693
171  */
172
173 static void cy82c693_set_dma_mode(ide_drive_t *drive, const u8 mode)
174 {
175         ide_hwif_t *hwif = drive->hwif;
176         u8 single = (mode & 0x10) >> 4, index = 0, data = 0;
177
178         index = hwif->channel ? CY82_INDEX_CHANNEL1 : CY82_INDEX_CHANNEL0;
179
180 #if CY82C693_DEBUG_LOGS
181         /* for debug let's show the previous values */
182
183         outb(index, CY82_INDEX_PORT);
184         data = inb(CY82_DATA_PORT);
185
186         printk(KERN_INFO "%s (ch=%d, dev=%d): DMA mode is %d (single=%d)\n",
187                 drive->name, HWIF(drive)->channel, drive->select.b.unit,
188                 (data&0x3), ((data>>2)&1));
189 #endif /* CY82C693_DEBUG_LOGS */
190
191         data = (mode & 3) | (single << 2);
192
193         outb(index, CY82_INDEX_PORT);
194         outb(data, CY82_DATA_PORT);
195
196 #if CY82C693_DEBUG_INFO
197         printk(KERN_INFO "%s (ch=%d, dev=%d): set DMA mode to %d (single=%d)\n",
198                 drive->name, HWIF(drive)->channel, drive->select.b.unit,
199                 mode & 3, single);
200 #endif /* CY82C693_DEBUG_INFO */
201
202         /*
203          * note: below we set the value for Bus Master IDE TimeOut Register
204          * I'm not absolutly sure what this does, but it solved my problem
205          * with IDE DMA and sound, so I now can play sound and work with
206          * my IDE driver at the same time :-)
207          *
208          * If you know the correct (best) value for this register please
209          * let me know - ASK
210          */
211
212         data = BUSMASTER_TIMEOUT;
213         outb(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT);
214         outb(data, CY82_DATA_PORT);
215
216 #if CY82C693_DEBUG_INFO
217         printk(KERN_INFO "%s: Set IDE Bus Master TimeOut Register to 0x%X\n",
218                 drive->name, data);
219 #endif /* CY82C693_DEBUG_INFO */
220 }
221
222 static void cy82c693_set_pio_mode(ide_drive_t *drive, const u8 pio)
223 {
224         ide_hwif_t *hwif = HWIF(drive);
225         struct pci_dev *dev = to_pci_dev(hwif->dev);
226         pio_clocks_t pclk;
227         unsigned int addrCtrl;
228
229         /* select primary or secondary channel */
230         if (hwif->index > 0) {  /* drive is on the secondary channel */
231                 dev = pci_get_slot(dev->bus, dev->devfn+1);
232                 if (!dev) {
233                         printk(KERN_ERR "%s: tune_drive: "
234                                 "Cannot find secondary interface!\n",
235                                 drive->name);
236                         return;
237                 }
238         }
239
240 #if CY82C693_DEBUG_LOGS
241         /* for debug let's show the register values */
242
243         if (drive->select.b.unit == 0) {
244                 /*
245                  * get master drive registers
246                  * address setup control register
247                  * is 32 bit !!!
248                  */
249                 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
250                 addrCtrl &= 0x0F;
251
252                 /* now let's get the remaining registers */
253                 pci_read_config_byte(dev, CY82_IDE_MASTER_IOR, &pclk.time_16r);
254                 pci_read_config_byte(dev, CY82_IDE_MASTER_IOW, &pclk.time_16w);
255                 pci_read_config_byte(dev, CY82_IDE_MASTER_8BIT, &pclk.time_8);
256         } else {
257                 /*
258                  * set slave drive registers
259                  * address setup control register
260                  * is 32 bit !!!
261                  */
262                 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
263
264                 addrCtrl &= 0xF0;
265                 addrCtrl >>= 4;
266
267                 /* now let's get the remaining registers */
268                 pci_read_config_byte(dev, CY82_IDE_SLAVE_IOR, &pclk.time_16r);
269                 pci_read_config_byte(dev, CY82_IDE_SLAVE_IOW, &pclk.time_16w);
270                 pci_read_config_byte(dev, CY82_IDE_SLAVE_8BIT, &pclk.time_8);
271         }
272
273         printk(KERN_INFO "%s (ch=%d, dev=%d): PIO timing is "
274                 "(addr=0x%X, ior=0x%X, iow=0x%X, 8bit=0x%X)\n",
275                 drive->name, hwif->channel, drive->select.b.unit,
276                 addrCtrl, pclk.time_16r, pclk.time_16w, pclk.time_8);
277 #endif /* CY82C693_DEBUG_LOGS */
278
279         /* let's calc the values for this PIO mode */
280         compute_clocks(pio, &pclk);
281
282         /* now let's write  the clocks registers */
283         if (drive->select.b.unit == 0) {
284                 /*
285                  * set master drive
286                  * address setup control register
287                  * is 32 bit !!!
288                  */
289                 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
290
291                 addrCtrl &= (~0xF);
292                 addrCtrl |= (unsigned int)pclk.address_time;
293                 pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
294
295                 /* now let's set the remaining registers */
296                 pci_write_config_byte(dev, CY82_IDE_MASTER_IOR, pclk.time_16r);
297                 pci_write_config_byte(dev, CY82_IDE_MASTER_IOW, pclk.time_16w);
298                 pci_write_config_byte(dev, CY82_IDE_MASTER_8BIT, pclk.time_8);
299
300                 addrCtrl &= 0xF;
301         } else {
302                 /*
303                  * set slave drive
304                  * address setup control register
305                  * is 32 bit !!!
306                  */
307                 pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
308
309                 addrCtrl &= (~0xF0);
310                 addrCtrl |= ((unsigned int)pclk.address_time<<4);
311                 pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
312
313                 /* now let's set the remaining registers */
314                 pci_write_config_byte(dev, CY82_IDE_SLAVE_IOR, pclk.time_16r);
315                 pci_write_config_byte(dev, CY82_IDE_SLAVE_IOW, pclk.time_16w);
316                 pci_write_config_byte(dev, CY82_IDE_SLAVE_8BIT, pclk.time_8);
317
318                 addrCtrl >>= 4;
319                 addrCtrl &= 0xF;
320         }
321
322 #if CY82C693_DEBUG_INFO
323         printk(KERN_INFO "%s (ch=%d, dev=%d): set PIO timing to "
324                 "(addr=0x%X, ior=0x%X, iow=0x%X, 8bit=0x%X)\n",
325                 drive->name, hwif->channel, drive->select.b.unit,
326                 addrCtrl, pclk.time_16r, pclk.time_16w, pclk.time_8);
327 #endif /* CY82C693_DEBUG_INFO */
328 }
329
330 /*
331  * this function is called during init and is used to setup the cy82c693 chip
332  */
333 static unsigned int __devinit init_chipset_cy82c693(struct pci_dev *dev, const char *name)
334 {
335         if (PCI_FUNC(dev->devfn) != 1)
336                 return 0;
337
338 #ifdef CY82C693_SETDMA_CLOCK
339         u8 data = 0;
340 #endif /* CY82C693_SETDMA_CLOCK */
341
342         /* write info about this verion of the driver */
343         printk(KERN_INFO CY82_VERSION "\n");
344
345 #ifdef CY82C693_SETDMA_CLOCK
346        /* okay let's set the DMA clock speed */
347
348         outb(CY82_INDEX_CTRLREG1, CY82_INDEX_PORT);
349         data = inb(CY82_DATA_PORT);
350
351 #if CY82C693_DEBUG_INFO
352         printk(KERN_INFO "%s: Peripheral Configuration Register: 0x%X\n",
353                 name, data);
354 #endif /* CY82C693_DEBUG_INFO */
355
356         /*
357          * for some reason sometimes the DMA controller
358          * speed is set to ATCLK/2 ???? - we fix this here
359          *
360          * note: i don't know what causes this strange behaviour,
361          *       but even changing the dma speed doesn't solve it :-(
362          *       the ide performance is still only half the normal speed
363          *
364          *       if anybody knows what goes wrong with my machine, please
365          *       let me know - ASK
366          */
367
368         data |= 0x03;
369
370         outb(CY82_INDEX_CTRLREG1, CY82_INDEX_PORT);
371         outb(data, CY82_DATA_PORT);
372
373 #if CY82C693_DEBUG_INFO
374         printk(KERN_INFO "%s: New Peripheral Configuration Register: 0x%X\n",
375                 name, data);
376 #endif /* CY82C693_DEBUG_INFO */
377
378 #endif /* CY82C693_SETDMA_CLOCK */
379         return 0;
380 }
381
382 static void __devinit init_iops_cy82c693(ide_hwif_t *hwif)
383 {
384         static ide_hwif_t *primary;
385         struct pci_dev *dev = to_pci_dev(hwif->dev);
386
387         if (PCI_FUNC(dev->devfn) == 1)
388                 primary = hwif;
389         else {
390                 hwif->mate = primary;
391                 hwif->channel = 1;
392         }
393 }
394
395 static const struct ide_port_ops cy82c693_port_ops = {
396         .set_pio_mode           = cy82c693_set_pio_mode,
397         .set_dma_mode           = cy82c693_set_dma_mode,
398 };
399
400 static const struct ide_port_info cy82c693_chipset __devinitdata = {
401         .name           = "CY82C693",
402         .init_chipset   = init_chipset_cy82c693,
403         .init_iops      = init_iops_cy82c693,
404         .port_ops       = &cy82c693_port_ops,
405         .chipset        = ide_cy82c693,
406         .host_flags     = IDE_HFLAG_SINGLE,
407         .pio_mask       = ATA_PIO4,
408         .swdma_mask     = ATA_SWDMA2,
409         .mwdma_mask     = ATA_MWDMA2,
410 };
411
412 static int __devinit cy82c693_init_one(struct pci_dev *dev, const struct pci_device_id *id)
413 {
414         struct pci_dev *dev2;
415         int ret = -ENODEV;
416
417         /* CY82C693 is more than only a IDE controller.
418            Function 1 is primary IDE channel, function 2 - secondary. */
419         if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
420             PCI_FUNC(dev->devfn) == 1) {
421                 dev2 = pci_get_slot(dev->bus, dev->devfn + 1);
422                 ret = ide_setup_pci_devices(dev, dev2, &cy82c693_chipset);
423                 /* We leak pci refs here but thats ok - we can't be unloaded */
424         }
425         return ret;
426 }
427
428 static const struct pci_device_id cy82c693_pci_tbl[] = {
429         { PCI_VDEVICE(CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693), 0 },
430         { 0, },
431 };
432 MODULE_DEVICE_TABLE(pci, cy82c693_pci_tbl);
433
434 static struct pci_driver driver = {
435         .name           = "Cypress_IDE",
436         .id_table       = cy82c693_pci_tbl,
437         .probe          = cy82c693_init_one,
438 };
439
440 static int __init cy82c693_ide_init(void)
441 {
442         return ide_pci_register_driver(&driver);
443 }
444
445 module_init(cy82c693_ide_init);
446
447 MODULE_AUTHOR("Andreas Krebs, Andre Hedrick");
448 MODULE_DESCRIPTION("PCI driver module for the Cypress CY82C693 IDE");
449 MODULE_LICENSE("GPL");