d1780db45e1c1d263160a3178f3e51306eec7f51
[linux-flexiantxendom0-3.2.10.git] / drivers / ide / pci / cs5520.c
1 /*
2  *      IDE tuning and bus mastering support for the CS5510/CS5520
3  *      chipsets
4  *
5  *      The CS5510/CS5520 are slightly unusual devices. Unlike the 
6  *      typical IDE controllers they do bus mastering with the drive in
7  *      PIO mode and smarter silicon.
8  *
9  *      The practical upshot of this is that we must always tune the
10  *      drive for the right PIO mode. We must also ignore all the blacklists
11  *      and the drive bus mastering DMA information.
12  *
13  *      *** This driver is strictly experimental ***
14  *
15  *      (c) Copyright Red Hat Inc 2002
16  * 
17  * This program is free software; you can redistribute it and/or modify it
18  * under the terms of the GNU General Public License as published by the
19  * Free Software Foundation; either version 2, or (at your option) any
20  * later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  * General Public License for more details.
26  *
27  * For the avoidance of doubt the "preferred form" of this code is one which
28  * is in an open non patent encumbered format. Where cryptographic key signing
29  * forms part of the process of creating an executable the information
30  * including keys needed to generate an equivalently functional executable
31  * are deemed to be part of the source code.
32  *
33  */
34  
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <linux/delay.h>
40 #include <linux/timer.h>
41 #include <linux/mm.h>
42 #include <linux/ioport.h>
43 #include <linux/blkdev.h>
44 #include <linux/hdreg.h>
45
46 #include <linux/interrupt.h>
47 #include <linux/init.h>
48 #include <linux/pci.h>
49 #include <linux/ide.h>
50
51 #include <asm/io.h>
52 #include <asm/irq.h>
53
54 #include "ide_modes.h"
55 #include "cs5520.h"
56
57 #if defined(DISPLAY_CS5520_TIMINGS) && defined(CONFIG_PROC_FS)
58 #include <linux/stat.h>
59 #include <linux/proc_fs.h>
60
61 static u8 cs5520_proc = 0;
62 static struct pci_dev *bmide_dev;
63
64 static int cs5520_get_info(char *buffer, char **addr, off_t offset, int count)
65 {
66         char *p = buffer;
67         unsigned long bmiba = pci_resource_start(bmide_dev, 2);
68         int len;
69         u8 c0 = 0, c1 = 0;
70         u16 reg16;
71         u32 reg32;
72
73         /*
74          * at that point bibma+0x2 et bibma+0xa are byte registers
75          * to investigate:
76          */
77         c0 = inb(bmiba + 0x02);
78         c1 = inb(bmiba + 0x0a);
79         
80         p += sprintf(p, "\nCyrix CS55x0 IDE\n");
81         p += sprintf(p, "--------------- Primary Channel "
82                         "---------------- Secondary Channel "
83                         "-------------\n");
84         p += sprintf(p, "                %sabled "
85                         "                        %sabled\n",
86                         (c0&0x80) ? "dis" : " en",
87                         (c1&0x80) ? "dis" : " en");
88                         
89         p += sprintf(p, "\n\nTimings: \n");
90         
91         pci_read_config_word(bmide_dev, 0x62, &reg16);
92         p += sprintf(p, "8bit CAT/CRT   : %04x\n", reg16);
93         pci_read_config_dword(bmide_dev, 0x64, &reg32);
94         p += sprintf(p, "16bit Primary  : %08x\n", reg32);
95         pci_read_config_dword(bmide_dev, 0x68, &reg32);
96         p += sprintf(p, "16bit Secondary: %08x\n", reg32);
97         
98         len = (p - buffer) - offset;
99         *addr = buffer + offset;
100         
101         return len > count ? count : len;
102 }
103
104 #endif
105
106 struct pio_clocks
107 {
108         int address;
109         int assert;
110         int recovery;
111 };
112
113 struct pio_clocks cs5520_pio_clocks[]={
114         {3, 6, 11},
115         {2, 5, 6},
116         {1, 4, 3},
117         {1, 3, 2},
118         {1, 2, 1}
119 };
120
121 static int cs5520_tune_chipset(ide_drive_t *drive, u8 xferspeed)
122 {
123         ide_hwif_t *hwif = HWIF(drive);
124         struct pci_dev *pdev = hwif->pci_dev;
125         u8 speed = min((u8)XFER_PIO_4, xferspeed);
126         int pio = speed;
127         u8 reg;
128         int controller = drive->dn > 1 ? 1 : 0;
129         int error;
130         
131         switch(speed)
132         {
133                 case XFER_PIO_4:
134                 case XFER_PIO_3:
135                 case XFER_PIO_2:
136                 case XFER_PIO_1:
137                 case XFER_PIO_0:
138                         pio -= XFER_PIO_0;
139                         break;
140                 default:
141                         pio = 0;
142                         printk(KERN_ERR "cs55x0: bad ide timing.\n");
143         }
144         
145         printk("PIO clocking = %d\n", pio);
146         
147         /* FIXME: if DMA = 1 do we need to set the DMA bit here ? */
148         
149         /* 8bit command timing for channel */
150         pci_write_config_byte(pdev, 0x62 + controller, 
151                 (cs5520_pio_clocks[pio].recovery << 4) |
152                 (cs5520_pio_clocks[pio].assert));
153                 
154         /* FIXME: should these use address ? */
155         /* Data read timing */
156         pci_write_config_byte(pdev, 0x64 + 4*controller + (drive->dn&1),
157                 (cs5520_pio_clocks[pio].recovery << 4) |
158                 (cs5520_pio_clocks[pio].assert));
159         /* Write command timing */
160         pci_write_config_byte(pdev, 0x66 + 4*controller + (drive->dn&1),
161                 (cs5520_pio_clocks[pio].recovery << 4) |
162                 (cs5520_pio_clocks[pio].assert));
163                 
164         /* Set the DMA enable/disable flag */
165         reg = inb(hwif->dma_base + 0x02 + 8*controller);
166         reg |= 1<<((drive->dn&1)+5);
167         outb(reg, hwif->dma_base + 0x02 + 8*controller);
168                 
169         error = ide_config_drive_speed(drive, speed);
170         /* ATAPI is harder so leave it for now */
171         if(!error && drive->media == ide_disk)
172                 error = hwif->ide_dma_on(drive);
173
174         return error;
175 }       
176         
177 static void cs5520_tune_drive(ide_drive_t *drive, u8 pio)
178 {
179         pio = ide_get_best_pio_mode(drive, pio, 4, NULL);
180         cs5520_tune_chipset(drive, (XFER_PIO_0 + pio));
181 }
182
183 static int cs5520_config_drive_xfer_rate(ide_drive_t *drive)
184 {
185         ide_hwif_t *hwif = HWIF(drive);
186
187         /* Tune the drive for PIO modes up to PIO 4 */  
188         cs5520_tune_drive(drive, 4);
189         /* Then tell the core to use DMA operations */
190         return hwif->ide_dma_on(drive);
191 }
192         
193         
194 static unsigned int __devinit init_chipset_cs5520(struct pci_dev *dev, const char *name)
195 {
196 #if defined(DISPLAY_CS5520_TIMINGS) && defined(CONFIG_PROC_FS)
197         if (!cs5520_proc) {
198                 cs5520_proc = 1;
199                 bmide_dev = dev;
200                 ide_pci_register_host_proc(&cs5520_procs[0]);
201         }
202 #endif /* DISPLAY_CS5520_TIMINGS && CONFIG_PROC_FS */
203         return 0;
204 }
205
206 /*
207  *      We provide a callback for our nonstandard DMA location
208  */
209
210 static void __devinit cs5520_init_setup_dma(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *hwif)
211 {
212         unsigned long bmide = pci_resource_start(dev, 2);       /* Not the usual 4 */
213         if(hwif->mate && hwif->mate->dma_base)  /* Second channel at primary + 8 */
214                 bmide += 8;
215         ide_setup_dma(hwif, bmide, 8);
216 }
217
218 /*
219  *      We wrap the DMA activate to set the vdma flag. This is needed
220  *      so that the IDE DMA layer issues PIO not DMA commands over the
221  *      DMA channel
222  */
223  
224 static int cs5520_dma_on(ide_drive_t *drive)
225 {
226         drive->vdma = 1;
227         return 0;
228 }
229
230 static void __devinit init_hwif_cs5520(ide_hwif_t *hwif)
231 {
232         hwif->tuneproc = &cs5520_tune_drive;
233         hwif->speedproc = &cs5520_tune_chipset;
234         hwif->ide_dma_check = &cs5520_config_drive_xfer_rate;
235         hwif->ide_dma_on = &cs5520_dma_on;
236
237         if(!noautodma)
238                 hwif->autodma = 1;
239         
240         if(!hwif->dma_base)
241         {
242                 hwif->drives[0].autotune = 1;
243                 hwif->drives[1].autotune = 1;
244                 return;
245         }
246         
247         hwif->atapi_dma = 0;
248         hwif->ultra_mask = 0;
249         hwif->swdma_mask = 0;
250         hwif->mwdma_mask = 0;
251         
252         hwif->drives[0].autodma = hwif->autodma;
253         hwif->drives[1].autodma = hwif->autodma;
254 }
255                 
256 /*
257  *      The 5510/5520 are a bit weird. They don't quite set up the way
258  *      the PCI helper layer expects so we must do much of the set up 
259  *      work longhand.
260  */
261  
262 static int __devinit cs5520_init_one(struct pci_dev *dev, const struct pci_device_id *id)
263 {
264         ata_index_t index;
265         ide_pci_device_t *d = &cyrix_chipsets[id->driver_data];
266
267         ide_setup_pci_noise(dev, d);
268
269         /* We must not grab the entire device, it has 'ISA' space in its
270            BARS too and we will freak out other bits of the kernel */
271         if(pci_enable_device_bars(dev, 1<<2))
272         {
273                 printk(KERN_WARNING "%s: Unable to enable 55x0.\n", d->name);
274                 return 1;
275         }
276         pci_set_master(dev);
277         pci_set_dma_mask(dev, 0xFFFFFFFF);
278         init_chipset_cs5520(dev, d->name);
279
280         index.all = 0xf0f0;
281
282         /*
283          *      Now the chipset is configured we can let the core
284          *      do all the device setup for us
285          */
286
287         ide_pci_setup_ports(dev, d, 1, 14, &index);
288
289         printk("Index.b %d %d\n", index.b.low, index.b.high);
290         mdelay(2000);
291         if((index.b.low & 0xf0) != 0xf0)
292                 probe_hwif_init(&ide_hwifs[index.b.low]);
293         if((index.b.high & 0xf0) != 0xf0)
294                 probe_hwif_init(&ide_hwifs[index.b.high]);
295         MOD_INC_USE_COUNT;
296         return 0;
297 }
298
299 static struct pci_device_id cs5520_pci_tbl[] __devinitdata = {
300         { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5510, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
301         { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
302         { 0, },
303 };
304
305 static struct pci_driver driver = {
306         .name           = "CyrixIDE",
307         .id_table       = cs5520_pci_tbl,
308         .probe          = cs5520_init_one,
309 };
310
311 static int cs5520_ide_init(void)
312 {
313         return ide_pci_register_driver(&driver);
314 }
315
316 static void cs5520_ide_exit(void)
317 {
318         return ide_pci_unregister_driver(&driver);
319 }
320
321 module_init(cs5520_ide_init);
322 module_exit(cs5520_ide_exit);
323
324 MODULE_AUTHOR("Alan Cox");
325 MODULE_DESCRIPTION("PCI driver module for Cyrix 5510/5520 IDE");
326 MODULE_LICENSE("GPL");