pata_ali: force initialise a few bits
[safe/jmp/linux-2.6] / drivers / ata / pata_ali.c
1 /*
2  * pata_ali.c   - ALI 15x3 PATA for new ATA layer
3  *                        (C) 2005 Red Hat Inc
4  *
5  * based in part upon
6  * linux/drivers/ide/pci/alim15x3.c             Version 0.17    2003/01/02
7  *
8  *  Copyright (C) 1998-2000 Michel Aubry, Maintainer
9  *  Copyright (C) 1998-2000 Andrzej Krzysztofowicz, Maintainer
10  *  Copyright (C) 1999-2000 CJ, cjtsai@ali.com.tw, Maintainer
11  *
12  *  Copyright (C) 1998-2000 Andre Hedrick (andre@linux-ide.org)
13  *  May be copied or modified under the terms of the GNU General Public License
14  *  Copyright (C) 2002 Alan Cox <alan@redhat.com>
15  *  ALi (now ULi M5228) support by Clear Zhang <Clear.Zhang@ali.com.tw>
16  *
17  *  Documentation
18  *      Chipset documentation available under NDA only
19  *
20  *  TODO/CHECK
21  *      Cannot have ATAPI on both master & slave for rev < c2 (???) but
22  *      otherwise should do atapi DMA (For now for old we do PIO only for
23  *      ATAPI)
24  *      Review Sunblade workaround.
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/init.h>
31 #include <linux/blkdev.h>
32 #include <linux/delay.h>
33 #include <scsi/scsi_host.h>
34 #include <linux/libata.h>
35 #include <linux/dmi.h>
36
37 #define DRV_NAME "pata_ali"
38 #define DRV_VERSION "0.7.7"
39
40 static int ali_atapi_dma = 0;
41 module_param_named(atapi_dma, ali_atapi_dma, int, 0644);
42 MODULE_PARM_DESC(atapi_dma, "Enable ATAPI DMA (0=disable, 1=enable)");
43
44 static struct pci_dev *isa_bridge;
45
46 /*
47  *      Cable special cases
48  */
49
50 static const struct dmi_system_id cable_dmi_table[] = {
51         {
52                 .ident = "HP Pavilion N5430",
53                 .matches = {
54                         DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
55                         DMI_MATCH(DMI_BOARD_VERSION, "OmniBook N32N-736"),
56                 },
57         },
58         {
59                 .ident = "Toshiba Satelite S1800-814",
60                 .matches = {
61                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
62                         DMI_MATCH(DMI_PRODUCT_NAME, "S1800-814"),
63                 },
64         },
65         { }
66 };
67
68 static int ali_cable_override(struct pci_dev *pdev)
69 {
70         /* Fujitsu P2000 */
71         if (pdev->subsystem_vendor == 0x10CF && pdev->subsystem_device == 0x10AF)
72                 return 1;
73         /* Mitac 8317 (Winbook-A) and relatives */
74         if (pdev->subsystem_vendor == 0x1071 && pdev->subsystem_device == 0x8317)
75                 return 1;
76         /* Systems by DMI */
77         if (dmi_check_system(cable_dmi_table))
78                 return 1;
79         return 0;
80 }
81
82 /**
83  *      ali_c2_cable_detect     -       cable detection
84  *      @ap: ATA port
85  *
86  *      Perform cable detection for C2 and later revisions
87  */
88
89 static int ali_c2_cable_detect(struct ata_port *ap)
90 {
91         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
92         u8 ata66;
93
94         /* Certain laptops use short but suitable cables and don't
95            implement the detect logic */
96
97         if (ali_cable_override(pdev))
98                 return ATA_CBL_PATA40_SHORT;
99
100         /* Host view cable detect 0x4A bit 0 primary bit 1 secondary
101            Bit set for 40 pin */
102         pci_read_config_byte(pdev, 0x4A, &ata66);
103         if (ata66 & (1 << ap->port_no))
104                 return ATA_CBL_PATA40;
105         else
106                 return ATA_CBL_PATA80;
107 }
108
109 /**
110  *      ali_20_filter           -       filter for earlier ALI DMA
111  *      @ap: ALi ATA port
112  *      @adev: attached device
113  *
114  *      Ensure that we do not do DMA on CD devices. We may be able to
115  *      fix that later on. Also ensure we do not do UDMA on WDC drives
116  */
117
118 static unsigned long ali_20_filter(struct ata_device *adev, unsigned long mask)
119 {
120         char model_num[ATA_ID_PROD_LEN + 1];
121         /* No DMA on anything but a disk for now */
122         if (adev->class != ATA_DEV_ATA)
123                 mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
124         ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
125         if (strstr(model_num, "WDC"))
126                 return mask &= ~ATA_MASK_UDMA;
127         return ata_bmdma_mode_filter(adev, mask);
128 }
129
130 /**
131  *      ali_fifo_control        -       FIFO manager
132  *      @ap: ALi channel to control
133  *      @adev: device for FIFO control
134  *      @on: 0 for off 1 for on
135  *
136  *      Enable or disable the FIFO on a given device. Because of the way the
137  *      ALi FIFO works it provides a boost on ATA disk but can be confused by
138  *      ATAPI and we must therefore manage it.
139  */
140
141 static void ali_fifo_control(struct ata_port *ap, struct ata_device *adev, int on)
142 {
143         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
144         int pio_fifo = 0x54 + ap->port_no;
145         u8 fifo;
146         int shift = 4 * adev->devno;
147
148         /* ATA - FIFO on set nibble to 0x05, ATAPI - FIFO off, set nibble to
149            0x00. Not all the docs agree but the behaviour we now use is the
150            one stated in the BIOS Programming Guide */
151
152         pci_read_config_byte(pdev, pio_fifo, &fifo);
153         fifo &= ~(0x0F << shift);
154         if (on)
155                 fifo |= (on << shift);
156         pci_write_config_byte(pdev, pio_fifo, fifo);
157 }
158
159 /**
160  *      ali_program_modes       -       load mode registers
161  *      @ap: ALi channel to load
162  *      @adev: Device the timing is for
163  *      @cmd: Command timing
164  *      @data: Data timing
165  *      @ultra: UDMA timing or zero for off
166  *
167  *      Loads the timing registers for cmd/data and disable UDMA if
168  *      ultra is zero. If ultra is set then load and enable the UDMA
169  *      timing but do not touch the command/data timing.
170  */
171
172 static void ali_program_modes(struct ata_port *ap, struct ata_device *adev, struct ata_timing *t, u8 ultra)
173 {
174         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
175         int cas = 0x58 + 4 * ap->port_no;       /* Command timing */
176         int cbt = 0x59 + 4 * ap->port_no;       /* Command timing */
177         int drwt = 0x5A + 4 * ap->port_no + adev->devno; /* R/W timing */
178         int udmat = 0x56 + ap->port_no; /* UDMA timing */
179         int shift = 4 * adev->devno;
180         u8 udma;
181
182         if (t != NULL) {
183                 t->setup = clamp_val(t->setup, 1, 8) & 7;
184                 t->act8b = clamp_val(t->act8b, 1, 8) & 7;
185                 t->rec8b = clamp_val(t->rec8b, 1, 16) & 15;
186                 t->active = clamp_val(t->active, 1, 8) & 7;
187                 t->recover = clamp_val(t->recover, 1, 16) & 15;
188
189                 pci_write_config_byte(pdev, cas, t->setup);
190                 pci_write_config_byte(pdev, cbt, (t->act8b << 4) | t->rec8b);
191                 pci_write_config_byte(pdev, drwt, (t->active << 4) | t->recover);
192         }
193
194         /* Set up the UDMA enable */
195         pci_read_config_byte(pdev, udmat, &udma);
196         udma &= ~(0x0F << shift);
197         udma |= ultra << shift;
198         pci_write_config_byte(pdev, udmat, udma);
199 }
200
201 /**
202  *      ali_set_piomode -       set initial PIO mode data
203  *      @ap: ATA interface
204  *      @adev: ATA device
205  *
206  *      Program the ALi registers for PIO mode. FIXME: add timings for
207  *      PIO5.
208  */
209
210 static void ali_set_piomode(struct ata_port *ap, struct ata_device *adev)
211 {
212         struct ata_device *pair = ata_dev_pair(adev);
213         struct ata_timing t;
214         unsigned long T =  1000000000 / 33333;  /* PCI clock based */
215
216         ata_timing_compute(adev, adev->pio_mode, &t, T, 1);
217         if (pair) {
218                 struct ata_timing p;
219                 ata_timing_compute(pair, pair->pio_mode, &p, T, 1);
220                 ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP|ATA_TIMING_8BIT);
221                 if (pair->dma_mode) {
222                         ata_timing_compute(pair, pair->dma_mode, &p, T, 1);
223                         ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP|ATA_TIMING_8BIT);
224                 }
225         }
226
227         /* PIO FIFO is only permitted on ATA disk */
228         if (adev->class != ATA_DEV_ATA)
229                 ali_fifo_control(ap, adev, 0x00);
230         ali_program_modes(ap, adev, &t, 0);
231         if (adev->class == ATA_DEV_ATA)
232                 ali_fifo_control(ap, adev, 0x05);
233
234 }
235
236 /**
237  *      ali_set_dmamode -       set initial DMA mode data
238  *      @ap: ATA interface
239  *      @adev: ATA device
240  *
241  *      FIXME: MWDMA timings
242  */
243
244 static void ali_set_dmamode(struct ata_port *ap, struct ata_device *adev)
245 {
246         static u8 udma_timing[7] = { 0xC, 0xB, 0xA, 0x9, 0x8, 0xF, 0xD };
247         struct ata_device *pair = ata_dev_pair(adev);
248         struct ata_timing t;
249         unsigned long T =  1000000000 / 33333;  /* PCI clock based */
250         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
251
252
253         if (adev->class == ATA_DEV_ATA)
254                 ali_fifo_control(ap, adev, 0x08);
255
256         if (adev->dma_mode >= XFER_UDMA_0) {
257                 ali_program_modes(ap, adev, NULL, udma_timing[adev->dma_mode - XFER_UDMA_0]);
258                 if (adev->dma_mode >= XFER_UDMA_3) {
259                         u8 reg4b;
260                         pci_read_config_byte(pdev, 0x4B, &reg4b);
261                         reg4b |= 1;
262                         pci_write_config_byte(pdev, 0x4B, reg4b);
263                 }
264         } else {
265                 ata_timing_compute(adev, adev->dma_mode, &t, T, 1);
266                 if (pair) {
267                         struct ata_timing p;
268                         ata_timing_compute(pair, pair->pio_mode, &p, T, 1);
269                         ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP|ATA_TIMING_8BIT);
270                         if (pair->dma_mode) {
271                                 ata_timing_compute(pair, pair->dma_mode, &p, T, 1);
272                                 ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP|ATA_TIMING_8BIT);
273                         }
274                 }
275                 ali_program_modes(ap, adev, &t, 0);
276         }
277 }
278
279 /**
280  *      ali_warn_atapi_dma      -       Warn about ATAPI DMA disablement
281  *      @adev: Device
282  *
283  *      Whine about ATAPI DMA disablement if @adev is an ATAPI device.
284  *      Can be used as ->dev_config.
285  */
286
287 static void ali_warn_atapi_dma(struct ata_device *adev)
288 {
289         struct ata_eh_context *ehc = &adev->link->eh_context;
290         int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
291
292         if (print_info && adev->class == ATA_DEV_ATAPI && !ali_atapi_dma) {
293                 ata_dev_printk(adev, KERN_WARNING,
294                                "WARNING: ATAPI DMA disabled for reliablity issues.  It can be enabled\n");
295                 ata_dev_printk(adev, KERN_WARNING,
296                                "WARNING: via pata_ali.atapi_dma modparam or corresponding sysfs node.\n");
297         }
298 }
299
300 /**
301  *      ali_lock_sectors        -       Keep older devices to 255 sector mode
302  *      @adev: Device
303  *
304  *      Called during the bus probe for each device that is found. We use
305  *      this call to lock the sector count of the device to 255 or less on
306  *      older ALi controllers. If we didn't do this then large I/O's would
307  *      require LBA48 commands which the older ALi requires are issued by
308  *      slower PIO methods
309  */
310
311 static void ali_lock_sectors(struct ata_device *adev)
312 {
313         adev->max_sectors = 255;
314         ali_warn_atapi_dma(adev);
315 }
316
317 /**
318  *      ali_check_atapi_dma     -       DMA check for most ALi controllers
319  *      @adev: Device
320  *
321  *      Called to decide whether commands should be sent by DMA or PIO
322  */
323
324 static int ali_check_atapi_dma(struct ata_queued_cmd *qc)
325 {
326         if (!ali_atapi_dma) {
327                 /* FIXME: pata_ali can't do ATAPI DMA reliably but the
328                  * IDE alim15x3 driver can.  I tried lots of things
329                  * but couldn't find what the actual difference was.
330                  * If you got an idea, please write it to
331                  * linux-ide@vger.kernel.org and cc htejun@gmail.com.
332                  *
333                  * Disable ATAPI DMA for now.
334                  */
335                 return -EOPNOTSUPP;
336         }
337
338         /* If its not a media command, its not worth it */
339         if (atapi_cmd_type(qc->cdb[0]) == ATAPI_MISC)
340                 return -EOPNOTSUPP;
341         return 0;
342 }
343
344 static struct scsi_host_template ali_sht = {
345         ATA_BMDMA_SHT(DRV_NAME),
346 };
347
348 /*
349  *      Port operations for PIO only ALi
350  */
351
352 static struct ata_port_operations ali_early_port_ops = {
353         .inherits       = &ata_sff_port_ops,
354         .cable_detect   = ata_cable_40wire,
355         .set_piomode    = ali_set_piomode,
356 };
357
358 static const struct ata_port_operations ali_dma_base_ops = {
359         .inherits       = &ata_bmdma_port_ops,
360         .set_piomode    = ali_set_piomode,
361         .set_dmamode    = ali_set_dmamode,
362 };
363
364 /*
365  *      Port operations for DMA capable ALi without cable
366  *      detect
367  */
368 static struct ata_port_operations ali_20_port_ops = {
369         .inherits       = &ali_dma_base_ops,
370         .cable_detect   = ata_cable_40wire,
371         .mode_filter    = ali_20_filter,
372         .check_atapi_dma = ali_check_atapi_dma,
373         .dev_config     = ali_lock_sectors,
374 };
375
376 /*
377  *      Port operations for DMA capable ALi with cable detect
378  */
379 static struct ata_port_operations ali_c2_port_ops = {
380         .inherits       = &ali_dma_base_ops,
381         .check_atapi_dma = ali_check_atapi_dma,
382         .cable_detect   = ali_c2_cable_detect,
383         .dev_config     = ali_lock_sectors,
384 };
385
386 /*
387  *      Port operations for DMA capable ALi with cable detect and LBA48
388  */
389 static struct ata_port_operations ali_c5_port_ops = {
390         .inherits       = &ali_dma_base_ops,
391         .check_atapi_dma = ali_check_atapi_dma,
392         .dev_config     = ali_warn_atapi_dma,
393         .cable_detect   = ali_c2_cable_detect,
394 };
395
396
397 /**
398  *      ali_init_chipset        -       chip setup function
399  *      @pdev: PCI device of ATA controller
400  *
401  *      Perform the setup on the device that must be done both at boot
402  *      and at resume time.
403  */
404
405 static void ali_init_chipset(struct pci_dev *pdev)
406 {
407         u8 tmp;
408         struct pci_dev *north;
409
410         /*
411          * The chipset revision selects the driver operations and
412          * mode data.
413          */
414
415         if (pdev->revision <= 0x20) {
416                 pci_read_config_byte(pdev, 0x53, &tmp);
417                 tmp |= 0x03;
418                 pci_write_config_byte(pdev, 0x53, tmp);
419         } else {
420                 pci_read_config_byte(pdev, 0x4a, &tmp);
421                 pci_write_config_byte(pdev, 0x4a, tmp | 0x20);
422                 pci_read_config_byte(pdev, 0x4B, &tmp);
423                 if (pdev->revision < 0xC2)
424                         /* 1543-E/F, 1543C-C, 1543C-D, 1543C-E */
425                         /* Clear CD-ROM DMA write bit */
426                         tmp &= 0x7F;
427                 /* Cable and UDMA */
428                 pci_write_config_byte(pdev, 0x4B, tmp | 0x09);
429                 /*
430                  * CD_ROM DMA on (0x53 bit 0). Enable this even if we want
431                  * to use PIO. 0x53 bit 1 (rev 20 only) - enable FIFO control
432                  * via 0x54/55.
433                  */
434                 pci_read_config_byte(pdev, 0x53, &tmp);
435                 if (pdev->revision >= 0xc7)
436                         tmp |= 0x03;
437                 else
438                         tmp |= 0x01;    /* CD_ROM enable for DMA */
439                 pci_write_config_byte(pdev, 0x53, tmp);
440         }
441         north = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
442         if (north && north->vendor == PCI_VENDOR_ID_AL && isa_bridge) {
443                 /* Configure the ALi bridge logic. For non ALi rely on BIOS.
444                    Set the south bridge enable bit */
445                 pci_read_config_byte(isa_bridge, 0x79, &tmp);
446                 if (pdev->revision == 0xC2)
447                         pci_write_config_byte(isa_bridge, 0x79, tmp | 0x04);
448                 else if (pdev->revision > 0xC2 && pdev->revision < 0xC5)
449                         pci_write_config_byte(isa_bridge, 0x79, tmp | 0x02);
450         }
451         pci_dev_put(north);
452         ata_pci_bmdma_clear_simplex(pdev);
453 }
454 /**
455  *      ali_init_one            -       discovery callback
456  *      @pdev: PCI device ID
457  *      @id: PCI table info
458  *
459  *      An ALi IDE interface has been discovered. Figure out what revision
460  *      and perform configuration work before handing it to the ATA layer
461  */
462
463 static int ali_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
464 {
465         static const struct ata_port_info info_early = {
466                 .flags = ATA_FLAG_SLAVE_POSS,
467                 .pio_mask = 0x1f,
468                 .port_ops = &ali_early_port_ops
469         };
470         /* Revision 0x20 added DMA */
471         static const struct ata_port_info info_20 = {
472                 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_LBA48,
473                 .pio_mask = 0x1f,
474                 .mwdma_mask = 0x07,
475                 .port_ops = &ali_20_port_ops
476         };
477         /* Revision 0x20 with support logic added UDMA */
478         static const struct ata_port_info info_20_udma = {
479                 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_LBA48,
480                 .pio_mask = 0x1f,
481                 .mwdma_mask = 0x07,
482                 .udma_mask = 0x07,      /* UDMA33 */
483                 .port_ops = &ali_20_port_ops
484         };
485         /* Revision 0xC2 adds UDMA66 */
486         static const struct ata_port_info info_c2 = {
487                 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_LBA48,
488                 .pio_mask = 0x1f,
489                 .mwdma_mask = 0x07,
490                 .udma_mask = ATA_UDMA4,
491                 .port_ops = &ali_c2_port_ops
492         };
493         /* Revision 0xC3 is UDMA66 for now */
494         static const struct ata_port_info info_c3 = {
495                 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_LBA48,
496                 .pio_mask = 0x1f,
497                 .mwdma_mask = 0x07,
498                 .udma_mask = ATA_UDMA4,
499                 .port_ops = &ali_c2_port_ops
500         };
501         /* Revision 0xC4 is UDMA100 */
502         static const struct ata_port_info info_c4 = {
503                 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_LBA48,
504                 .pio_mask = 0x1f,
505                 .mwdma_mask = 0x07,
506                 .udma_mask = ATA_UDMA5,
507                 .port_ops = &ali_c2_port_ops
508         };
509         /* Revision 0xC5 is UDMA133 with LBA48 DMA */
510         static const struct ata_port_info info_c5 = {
511                 .flags = ATA_FLAG_SLAVE_POSS,
512                 .pio_mask = 0x1f,
513                 .mwdma_mask = 0x07,
514                 .udma_mask = ATA_UDMA6,
515                 .port_ops = &ali_c5_port_ops
516         };
517
518         const struct ata_port_info *ppi[] = { NULL, NULL };
519         u8 tmp;
520         int rc;
521
522         rc = pcim_enable_device(pdev);
523         if (rc)
524                 return rc;
525
526         /*
527          * The chipset revision selects the driver operations and
528          * mode data.
529          */
530
531         if (pdev->revision < 0x20) {
532                 ppi[0] = &info_early;
533         } else if (pdev->revision < 0xC2) {
534                 ppi[0] = &info_20;
535         } else if (pdev->revision == 0xC2) {
536                 ppi[0] = &info_c2;
537         } else if (pdev->revision == 0xC3) {
538                 ppi[0] = &info_c3;
539         } else if (pdev->revision == 0xC4) {
540                 ppi[0] = &info_c4;
541         } else
542                 ppi[0] = &info_c5;
543
544         ali_init_chipset(pdev);
545
546         if (isa_bridge && pdev->revision >= 0x20 && pdev->revision < 0xC2) {
547                 /* Are we paired with a UDMA capable chip */
548                 pci_read_config_byte(isa_bridge, 0x5E, &tmp);
549                 if ((tmp & 0x1E) == 0x12)
550                         ppi[0] = &info_20_udma;
551         }
552
553         return ata_pci_sff_init_one(pdev, ppi, &ali_sht, NULL);
554 }
555
556 #ifdef CONFIG_PM
557 static int ali_reinit_one(struct pci_dev *pdev)
558 {
559         struct ata_host *host = dev_get_drvdata(&pdev->dev);
560         int rc;
561
562         rc = ata_pci_device_do_resume(pdev);
563         if (rc)
564                 return rc;
565         ali_init_chipset(pdev);
566         ata_host_resume(host);
567         return 0;
568 }
569 #endif
570
571 static const struct pci_device_id ali[] = {
572         { PCI_VDEVICE(AL, PCI_DEVICE_ID_AL_M5228), },
573         { PCI_VDEVICE(AL, PCI_DEVICE_ID_AL_M5229), },
574
575         { },
576 };
577
578 static struct pci_driver ali_pci_driver = {
579         .name           = DRV_NAME,
580         .id_table       = ali,
581         .probe          = ali_init_one,
582         .remove         = ata_pci_remove_one,
583 #ifdef CONFIG_PM
584         .suspend        = ata_pci_device_suspend,
585         .resume         = ali_reinit_one,
586 #endif
587 };
588
589 static int __init ali_init(void)
590 {
591         int ret;
592         isa_bridge = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
593
594         ret = pci_register_driver(&ali_pci_driver);
595         if (ret < 0)
596                 pci_dev_put(isa_bridge);
597         return ret;
598 }
599
600
601 static void __exit ali_exit(void)
602 {
603         pci_unregister_driver(&ali_pci_driver);
604         pci_dev_put(isa_bridge);
605 }
606
607
608 MODULE_AUTHOR("Alan Cox");
609 MODULE_DESCRIPTION("low-level driver for ALi PATA");
610 MODULE_LICENSE("GPL");
611 MODULE_DEVICE_TABLE(pci, ali);
612 MODULE_VERSION(DRV_VERSION);
613
614 module_init(ali_init);
615 module_exit(ali_exit);