patch-2_6_7-vs1_9_1_12
[linux-2.6.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 #define DISPLAY_CS5520_TIMINGS
55
56 #if defined(DISPLAY_CS5520_TIMINGS) && defined(CONFIG_PROC_FS)
57 #include <linux/stat.h>
58 #include <linux/proc_fs.h>
59
60 static u8 cs5520_proc = 0;
61 static struct pci_dev *bmide_dev;
62
63 static int cs5520_get_info(char *buffer, char **addr, off_t offset, int count)
64 {
65         char *p = buffer;
66         unsigned long bmiba = pci_resource_start(bmide_dev, 2);
67         int len;
68         u8 c0 = 0, c1 = 0;
69         u16 reg16;
70         u32 reg32;
71
72         /*
73          * at that point bibma+0x2 et bibma+0xa are byte registers
74          * to investigate:
75          */
76         c0 = inb(bmiba + 0x02);
77         c1 = inb(bmiba + 0x0a);
78         
79         p += sprintf(p, "\nCyrix CS55x0 IDE\n");
80         p += sprintf(p, "--------------- Primary Channel "
81                         "---------------- Secondary Channel "
82                         "-------------\n");
83         p += sprintf(p, "                %sabled "
84                         "                        %sabled\n",
85                         (c0&0x80) ? "dis" : " en",
86                         (c1&0x80) ? "dis" : " en");
87                         
88         p += sprintf(p, "\n\nTimings: \n");
89         
90         pci_read_config_word(bmide_dev, 0x62, &reg16);
91         p += sprintf(p, "8bit CAT/CRT   : %04x\n", reg16);
92         pci_read_config_dword(bmide_dev, 0x64, &reg32);
93         p += sprintf(p, "16bit Primary  : %08x\n", reg32);
94         pci_read_config_dword(bmide_dev, 0x68, &reg32);
95         p += sprintf(p, "16bit Secondary: %08x\n", reg32);
96         
97         len = (p - buffer) - offset;
98         *addr = buffer + offset;
99         
100         return len > count ? count : len;
101 }
102
103 #endif
104
105 struct pio_clocks
106 {
107         int address;
108         int assert;
109         int recovery;
110 };
111
112 struct pio_clocks cs5520_pio_clocks[]={
113         {3, 6, 11},
114         {2, 5, 6},
115         {1, 4, 3},
116         {1, 3, 2},
117         {1, 2, 1}
118 };
119
120 static int cs5520_tune_chipset(ide_drive_t *drive, u8 xferspeed)
121 {
122         ide_hwif_t *hwif = HWIF(drive);
123         struct pci_dev *pdev = hwif->pci_dev;
124         u8 speed = min((u8)XFER_PIO_4, xferspeed);
125         int pio = speed;
126         u8 reg;
127         int controller = drive->dn > 1 ? 1 : 0;
128         int error;
129         
130         switch(speed)
131         {
132                 case XFER_PIO_4:
133                 case XFER_PIO_3:
134                 case XFER_PIO_2:
135                 case XFER_PIO_1:
136                 case XFER_PIO_0:
137                         pio -= XFER_PIO_0;
138                         break;
139                 default:
140                         pio = 0;
141                         printk(KERN_ERR "cs55x0: bad ide timing.\n");
142         }
143         
144         printk("PIO clocking = %d\n", pio);
145         
146         /* FIXME: if DMA = 1 do we need to set the DMA bit here ? */
147         
148         /* 8bit command timing for channel */
149         pci_write_config_byte(pdev, 0x62 + controller, 
150                 (cs5520_pio_clocks[pio].recovery << 4) |
151                 (cs5520_pio_clocks[pio].assert));
152                 
153         /* FIXME: should these use address ? */
154         /* Data read timing */
155         pci_write_config_byte(pdev, 0x64 + 4*controller + (drive->dn&1),
156                 (cs5520_pio_clocks[pio].recovery << 4) |
157                 (cs5520_pio_clocks[pio].assert));
158         /* Write command timing */
159         pci_write_config_byte(pdev, 0x66 + 4*controller + (drive->dn&1),
160                 (cs5520_pio_clocks[pio].recovery << 4) |
161                 (cs5520_pio_clocks[pio].assert));
162                 
163         /* Set the DMA enable/disable flag */
164         reg = inb(hwif->dma_base + 0x02 + 8*controller);
165         reg |= 1<<((drive->dn&1)+5);
166         outb(reg, hwif->dma_base + 0x02 + 8*controller);
167                 
168         error = ide_config_drive_speed(drive, speed);
169         /* ATAPI is harder so leave it for now */
170         if(!error && drive->media == ide_disk)
171                 error = hwif->ide_dma_on(drive);
172
173         return error;
174 }       
175         
176 static void cs5520_tune_drive(ide_drive_t *drive, u8 pio)
177 {
178         pio = ide_get_best_pio_mode(drive, pio, 4, NULL);
179         cs5520_tune_chipset(drive, (XFER_PIO_0 + pio));
180 }
181
182 static int cs5520_config_drive_xfer_rate(ide_drive_t *drive)
183 {
184         ide_hwif_t *hwif = HWIF(drive);
185
186         /* Tune the drive for PIO modes up to PIO 4 */  
187         cs5520_tune_drive(drive, 4);
188         /* Then tell the core to use DMA operations */
189         return hwif->ide_dma_on(drive);
190 }
191         
192         
193 static unsigned int __devinit init_chipset_cs5520(struct pci_dev *dev, const char *name)
194 {
195 #if defined(DISPLAY_CS5520_TIMINGS) && defined(CONFIG_PROC_FS)
196         if (!cs5520_proc) {
197                 cs5520_proc = 1;
198                 bmide_dev = dev;
199                 ide_pci_create_host_proc("cs5520", cs5520_get_info);
200         }
201 #endif /* DISPLAY_CS5520_TIMINGS && CONFIG_PROC_FS */
202         return 0;
203 }
204
205 /*
206  *      We provide a callback for our nonstandard DMA location
207  */
208
209 static void __devinit cs5520_init_setup_dma(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *hwif)
210 {
211         unsigned long bmide = pci_resource_start(dev, 2);       /* Not the usual 4 */
212         if(hwif->mate && hwif->mate->dma_base)  /* Second channel at primary + 8 */
213                 bmide += 8;
214         ide_setup_dma(hwif, bmide, 8);
215 }
216
217 /*
218  *      We wrap the DMA activate to set the vdma flag. This is needed
219  *      so that the IDE DMA layer issues PIO not DMA commands over the
220  *      DMA channel
221  */
222  
223 static int cs5520_dma_on(ide_drive_t *drive)
224 {
225         drive->vdma = 1;
226         return 0;
227 }
228
229 static void __devinit init_hwif_cs5520(ide_hwif_t *hwif)
230 {
231         hwif->tuneproc = &cs5520_tune_drive;
232         hwif->speedproc = &cs5520_tune_chipset;
233         hwif->ide_dma_check = &cs5520_config_drive_xfer_rate;
234         hwif->ide_dma_on = &cs5520_dma_on;
235
236         if(!noautodma)
237                 hwif->autodma = 1;
238         
239         if(!hwif->dma_base)
240         {
241                 hwif->drives[0].autotune = 1;
242                 hwif->drives[1].autotune = 1;
243                 return;
244         }
245         
246         hwif->atapi_dma = 0;
247         hwif->ultra_mask = 0;
248         hwif->swdma_mask = 0;
249         hwif->mwdma_mask = 0;
250         
251         hwif->drives[0].autodma = hwif->autodma;
252         hwif->drives[1].autodma = hwif->autodma;
253 }
254
255 #define DECLARE_CS_DEV(name_str)                                \
256         {                                                       \
257                 .name           = name_str,                     \
258                 .init_chipset   = init_chipset_cs5520,          \
259                 .init_setup_dma = cs5520_init_setup_dma,        \
260                 .init_hwif      = init_hwif_cs5520,             \
261                 .channels       = 2,                            \
262                 .autodma        = AUTODMA,                      \
263                 .bootable       = ON_BOARD,                     \
264                 .flags          = IDEPCI_FLAG_ISA_PORTS,        \
265         }
266
267 static ide_pci_device_t cyrix_chipsets[] __devinitdata = {
268         /* 0 */ DECLARE_CS_DEV("Cyrix 5510"),
269         /* 1 */ DECLARE_CS_DEV("Cyrix 5520")
270 };
271
272 /*
273  *      The 5510/5520 are a bit weird. They don't quite set up the way
274  *      the PCI helper layer expects so we must do much of the set up 
275  *      work longhand.
276  */
277  
278 static int __devinit cs5520_init_one(struct pci_dev *dev, const struct pci_device_id *id)
279 {
280         ata_index_t index;
281         ide_pci_device_t *d = &cyrix_chipsets[id->driver_data];
282
283         ide_setup_pci_noise(dev, d);
284
285         /* We must not grab the entire device, it has 'ISA' space in its
286            BARS too and we will freak out other bits of the kernel */
287         if(pci_enable_device_bars(dev, 1<<2))
288         {
289                 printk(KERN_WARNING "%s: Unable to enable 55x0.\n", d->name);
290                 return 1;
291         }
292         pci_set_master(dev);
293         pci_set_dma_mask(dev, 0xFFFFFFFF);
294         init_chipset_cs5520(dev, d->name);
295
296         index.all = 0xf0f0;
297
298         /*
299          *      Now the chipset is configured we can let the core
300          *      do all the device setup for us
301          */
302
303         ide_pci_setup_ports(dev, d, 1, 14, &index);
304
305         printk("Index.b %d %d\n", index.b.low, index.b.high);
306         mdelay(2000);
307         if((index.b.low & 0xf0) != 0xf0)
308                 probe_hwif_init(&ide_hwifs[index.b.low]);
309         if((index.b.high & 0xf0) != 0xf0)
310                 probe_hwif_init(&ide_hwifs[index.b.high]);
311         return 0;
312 }
313
314 static struct pci_device_id cs5520_pci_tbl[] = {
315         { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5510, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
316         { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
317         { 0, },
318 };
319 MODULE_DEVICE_TABLE(pci, cs5520_pci_tbl);
320
321 static struct pci_driver driver = {
322         .name           = "CyrixIDE",
323         .id_table       = cs5520_pci_tbl,
324         .probe          = cs5520_init_one,
325 };
326
327 static int cs5520_ide_init(void)
328 {
329         return ide_pci_register_driver(&driver);
330 }
331
332 module_init(cs5520_ide_init);
333
334 MODULE_AUTHOR("Alan Cox");
335 MODULE_DESCRIPTION("PCI driver module for Cyrix 5510/5520 IDE");
336 MODULE_LICENSE("GPL");