Merge ../linux-2.6-watchdog-mm
[pandora-kernel.git] / drivers / ide / pci / atiixp.c
1 /*
2  *  linux/drivers/ide/pci/atiixp.c      Version 0.01-bart2      Feb. 26, 2004
3  *
4  *  Copyright (C) 2003 ATI Inc. <hyu@ati.com>
5  *  Copyright (C) 2004 Bartlomiej Zolnierkiewicz
6  *
7  */
8
9 #include <linux/types.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/ioport.h>
13 #include <linux/pci.h>
14 #include <linux/hdreg.h>
15 #include <linux/ide.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18
19 #include <asm/io.h>
20
21 #define ATIIXP_IDE_PIO_TIMING           0x40
22 #define ATIIXP_IDE_MDMA_TIMING          0x44
23 #define ATIIXP_IDE_PIO_CONTROL          0x48
24 #define ATIIXP_IDE_PIO_MODE             0x4a
25 #define ATIIXP_IDE_UDMA_CONTROL         0x54
26 #define ATIIXP_IDE_UDMA_MODE            0x56
27
28 typedef struct {
29         u8 command_width;
30         u8 recover_width;
31 } atiixp_ide_timing;
32
33 static atiixp_ide_timing pio_timing[] = {
34         { 0x05, 0x0d },
35         { 0x04, 0x07 },
36         { 0x03, 0x04 },
37         { 0x02, 0x02 },
38         { 0x02, 0x00 },
39 };
40
41 static atiixp_ide_timing mdma_timing[] = {
42         { 0x07, 0x07 },
43         { 0x02, 0x01 },
44         { 0x02, 0x00 },
45 };
46
47 static int save_mdma_mode[4];
48
49 static DEFINE_SPINLOCK(atiixp_lock);
50
51 /**
52  *      atiixp_ratemask         -       compute rate mask for ATIIXP IDE
53  *      @drive: IDE drive to compute for
54  *
55  *      Returns the available modes for the ATIIXP IDE controller.
56  */
57
58 static u8 atiixp_ratemask(ide_drive_t *drive)
59 {
60         u8 mode = 3;
61
62         if (!eighty_ninty_three(drive))
63                 mode = min(mode, (u8)1);
64         return mode;
65 }
66
67 /**
68  *      atiixp_dma_2_pio                -       return the PIO mode matching DMA
69  *      @xfer_rate: transfer speed
70  *
71  *      Returns the nearest equivalent PIO timing for the PIO or DMA
72  *      mode requested by the controller.
73  */
74
75 static u8 atiixp_dma_2_pio(u8 xfer_rate) {
76         switch(xfer_rate) {
77                 case XFER_UDMA_6:
78                 case XFER_UDMA_5:
79                 case XFER_UDMA_4:
80                 case XFER_UDMA_3:
81                 case XFER_UDMA_2:
82                 case XFER_UDMA_1:
83                 case XFER_UDMA_0:
84                 case XFER_MW_DMA_2:
85                 case XFER_PIO_4:
86                         return 4;
87                 case XFER_MW_DMA_1:
88                 case XFER_PIO_3:
89                         return 3;
90                 case XFER_SW_DMA_2:
91                 case XFER_PIO_2:
92                         return 2;
93                 case XFER_MW_DMA_0:
94                 case XFER_SW_DMA_1:
95                 case XFER_SW_DMA_0:
96                 case XFER_PIO_1:
97                 case XFER_PIO_0:
98                 case XFER_PIO_SLOW:
99                 default:
100                         return 0;
101         }
102 }
103
104 static int atiixp_ide_dma_host_on(ide_drive_t *drive)
105 {
106         struct pci_dev *dev = drive->hwif->pci_dev;
107         unsigned long flags;
108         u16 tmp16;
109
110         spin_lock_irqsave(&atiixp_lock, flags);
111
112         pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &tmp16);
113         if (save_mdma_mode[drive->dn])
114                 tmp16 &= ~(1 << drive->dn);
115         else
116                 tmp16 |= (1 << drive->dn);
117         pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, tmp16);
118
119         spin_unlock_irqrestore(&atiixp_lock, flags);
120
121         return __ide_dma_host_on(drive);
122 }
123
124 static int atiixp_ide_dma_host_off(ide_drive_t *drive)
125 {
126         struct pci_dev *dev = drive->hwif->pci_dev;
127         unsigned long flags;
128         u16 tmp16;
129
130         spin_lock_irqsave(&atiixp_lock, flags);
131
132         pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &tmp16);
133         tmp16 &= ~(1 << drive->dn);
134         pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, tmp16);
135
136         spin_unlock_irqrestore(&atiixp_lock, flags);
137
138         return __ide_dma_host_off(drive);
139 }
140
141 /**
142  *      atiixp_tune_drive               -       tune a drive attached to a ATIIXP
143  *      @drive: drive to tune
144  *      @pio: desired PIO mode
145  *
146  *      Set the interface PIO mode.
147  */
148
149 static void atiixp_tuneproc(ide_drive_t *drive, u8 pio)
150 {
151         struct pci_dev *dev = drive->hwif->pci_dev;
152         unsigned long flags;
153         int timing_shift = (drive->dn & 2) ? 16 : 0 + (drive->dn & 1) ? 0 : 8;
154         u32 pio_timing_data;
155         u16 pio_mode_data;
156
157         spin_lock_irqsave(&atiixp_lock, flags);
158
159         pci_read_config_word(dev, ATIIXP_IDE_PIO_MODE, &pio_mode_data);
160         pio_mode_data &= ~(0x07 << (drive->dn * 4));
161         pio_mode_data |= (pio << (drive->dn * 4));
162         pci_write_config_word(dev, ATIIXP_IDE_PIO_MODE, pio_mode_data);
163
164         pci_read_config_dword(dev, ATIIXP_IDE_PIO_TIMING, &pio_timing_data);
165         pio_timing_data &= ~(0xff << timing_shift);
166         pio_timing_data |= (pio_timing[pio].recover_width << timing_shift) |
167                  (pio_timing[pio].command_width << (timing_shift + 4));
168         pci_write_config_dword(dev, ATIIXP_IDE_PIO_TIMING, pio_timing_data);
169
170         spin_unlock_irqrestore(&atiixp_lock, flags);
171 }
172
173 /**
174  *      atiixp_tune_chipset     -       tune a ATIIXP interface
175  *      @drive: IDE drive to tune
176  *      @xferspeed: speed to configure
177  *
178  *      Set a ATIIXP interface channel to the desired speeds. This involves
179  *      requires the right timing data into the ATIIXP configuration space
180  *      then setting the drive parameters appropriately
181  */
182
183 static int atiixp_speedproc(ide_drive_t *drive, u8 xferspeed)
184 {
185         struct pci_dev *dev = drive->hwif->pci_dev;
186         unsigned long flags;
187         int timing_shift = (drive->dn & 2) ? 16 : 0 + (drive->dn & 1) ? 0 : 8;
188         u32 tmp32;
189         u16 tmp16;
190         u8 speed, pio;
191
192         speed = ide_rate_filter(atiixp_ratemask(drive), xferspeed);
193
194         spin_lock_irqsave(&atiixp_lock, flags);
195
196         save_mdma_mode[drive->dn] = 0;
197         if (speed >= XFER_UDMA_0) {
198                 pci_read_config_word(dev, ATIIXP_IDE_UDMA_MODE, &tmp16);
199                 tmp16 &= ~(0x07 << (drive->dn * 4));
200                 tmp16 |= ((speed & 0x07) << (drive->dn * 4));
201                 pci_write_config_word(dev, ATIIXP_IDE_UDMA_MODE, tmp16);
202         } else {
203                 if ((speed >= XFER_MW_DMA_0) && (speed <= XFER_MW_DMA_2)) {
204                         save_mdma_mode[drive->dn] = speed;
205                         pci_read_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, &tmp32);
206                         tmp32 &= ~(0xff << timing_shift);
207                         tmp32 |= (mdma_timing[speed & 0x03].recover_width << timing_shift) |
208                                 (mdma_timing[speed & 0x03].command_width << (timing_shift + 4));
209                         pci_write_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, tmp32);
210                 }
211         }
212
213         spin_unlock_irqrestore(&atiixp_lock, flags);
214
215         if (speed >= XFER_SW_DMA_0)
216                 pio = atiixp_dma_2_pio(speed);
217         else
218                 pio = speed - XFER_PIO_0;
219
220         atiixp_tuneproc(drive, pio);
221
222         return ide_config_drive_speed(drive, speed);
223 }
224
225 /**
226  *      atiixp_config_drive_for_dma     -       configure drive for DMA
227  *      @drive: IDE drive to configure
228  *
229  *      Set up a ATIIXP interface channel for the best available speed.
230  *      We prefer UDMA if it is available and then MWDMA. If DMA is
231  *      not available we switch to PIO and return 0.
232  */
233
234 static int atiixp_config_drive_for_dma(ide_drive_t *drive)
235 {
236         u8 speed = ide_dma_speed(drive, atiixp_ratemask(drive));
237
238         /* If no DMA speed was available then disable DMA and use PIO. */
239         if (!speed) {
240                 u8 tspeed = ide_get_best_pio_mode(drive, 255, 5, NULL);
241                 speed = atiixp_dma_2_pio(XFER_PIO_0 + tspeed) + XFER_PIO_0;
242         }
243
244         (void) atiixp_speedproc(drive, speed);
245         return ide_dma_enable(drive);
246 }
247
248 /**
249  *      atiixp_dma_check        -       set up an IDE device
250  *      @drive: IDE drive to configure
251  *
252  *      Set up the ATIIXP interface for the best available speed on this
253  *      interface, preferring DMA to PIO.
254  */
255
256 static int atiixp_dma_check(ide_drive_t *drive)
257 {
258         ide_hwif_t *hwif        = HWIF(drive);
259         struct hd_driveid *id   = drive->id;
260         u8 tspeed, speed;
261
262         drive->init_speed = 0;
263
264         if ((id->capability & 1) && drive->autodma) {
265
266                 if (ide_use_dma(drive)) {
267                         if (atiixp_config_drive_for_dma(drive))
268                                 return hwif->ide_dma_on(drive);
269                 }
270
271                 goto fast_ata_pio;
272
273         } else if ((id->capability & 8) || (id->field_valid & 2)) {
274 fast_ata_pio:
275                 tspeed = ide_get_best_pio_mode(drive, 255, 5, NULL);
276                 speed = atiixp_dma_2_pio(XFER_PIO_0 + tspeed) + XFER_PIO_0;
277                 hwif->speedproc(drive, speed);
278                 return hwif->ide_dma_off_quietly(drive);
279         }
280         /* IORDY not supported */
281         return 0;
282 }
283
284 /**
285  *      init_hwif_atiixp                -       fill in the hwif for the ATIIXP
286  *      @hwif: IDE interface
287  *
288  *      Set up the ide_hwif_t for the ATIIXP interface according to the
289  *      capabilities of the hardware.
290  */
291
292 static void __devinit init_hwif_atiixp(ide_hwif_t *hwif)
293 {
294         u8 udma_mode = 0;
295         u8 ch = hwif->channel;
296         struct pci_dev *pdev = hwif->pci_dev;
297
298         if (!hwif->irq)
299                 hwif->irq = ch ? 15 : 14;
300
301         hwif->autodma = 0;
302         hwif->tuneproc = &atiixp_tuneproc;
303         hwif->speedproc = &atiixp_speedproc;
304         hwif->drives[0].autotune = 1;
305         hwif->drives[1].autotune = 1;
306
307         if (!hwif->dma_base)
308                 return;
309
310         hwif->atapi_dma = 1;
311         hwif->ultra_mask = 0x3f;
312         hwif->mwdma_mask = 0x06;
313         hwif->swdma_mask = 0x04;
314
315         pci_read_config_byte(pdev, ATIIXP_IDE_UDMA_MODE + ch, &udma_mode);
316         if ((udma_mode & 0x07) >= 0x04 || (udma_mode & 0x70) >= 0x40)
317                 hwif->udma_four = 1;
318         else
319                 hwif->udma_four = 0;
320
321         hwif->ide_dma_host_on = &atiixp_ide_dma_host_on;
322         hwif->ide_dma_host_off = &atiixp_ide_dma_host_off;
323         hwif->ide_dma_check = &atiixp_dma_check;
324         if (!noautodma)
325                 hwif->autodma = 1;
326
327         hwif->drives[1].autodma = hwif->autodma;
328         hwif->drives[0].autodma = hwif->autodma;
329 }
330
331
332 static ide_pci_device_t atiixp_pci_info[] __devinitdata = {
333         {       /* 0 */
334                 .name           = "ATIIXP",
335                 .init_hwif      = init_hwif_atiixp,
336                 .channels       = 2,
337                 .autodma        = AUTODMA,
338                 .enablebits     = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
339                 .bootable       = ON_BOARD,
340         },{     /* 1 */
341                 .name           = "SB600_PATA",
342                 .init_hwif      = init_hwif_atiixp,
343                 .channels       = 1,
344                 .autodma        = AUTODMA,
345                 .enablebits     = {{0x48,0x01,0x00}, {0x00,0x00,0x00}},
346                 .bootable       = ON_BOARD,
347         },
348 };
349
350 /**
351  *      atiixp_init_one -       called when a ATIIXP is found
352  *      @dev: the atiixp device
353  *      @id: the matching pci id
354  *
355  *      Called when the PCI registration layer (or the IDE initialization)
356  *      finds a device matching our IDE device tables.
357  */
358
359 static int __devinit atiixp_init_one(struct pci_dev *dev, const struct pci_device_id *id)
360 {
361         return ide_setup_pci_device(dev, &atiixp_pci_info[id->driver_data]);
362 }
363
364 static struct pci_device_id atiixp_pci_tbl[] = {
365         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
366         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
367         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
368         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
369         { 0, },
370 };
371 MODULE_DEVICE_TABLE(pci, atiixp_pci_tbl);
372
373 static struct pci_driver driver = {
374         .name           = "ATIIXP_IDE",
375         .id_table       = atiixp_pci_tbl,
376         .probe          = atiixp_init_one,
377 };
378
379 static int __init atiixp_ide_init(void)
380 {
381         return ide_pci_register_driver(&driver);
382 }
383
384 module_init(atiixp_ide_init);
385
386 MODULE_AUTHOR("HUI YU");
387 MODULE_DESCRIPTION("PCI driver module for ATI IXP IDE");
388 MODULE_LICENSE("GPL");