VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / scsi / ata_piix.c
1 /*
2
3     ata_piix.c - Intel PATA/SATA controllers
4
5     Maintained by:  Jeff Garzik <jgarzik@pobox.com>
6                     Please ALWAYS copy linux-ide@vger.kernel.org
7                     on emails.
8
9
10         Copyright 2003-2004 Red Hat Inc
11         Copyright 2003-2004 Jeff Garzik
12
13
14         Copyright header from piix.c:
15
16     Copyright (C) 1998-1999 Andrzej Krzysztofowicz, Author and Maintainer
17     Copyright (C) 1998-2000 Andre Hedrick <andre@linux-ide.org>
18     Copyright (C) 2003 Red Hat Inc <alan@redhat.com>
19
20     May be copied or modified under the terms of the GNU General Public License
21
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/init.h>
28 #include <linux/blkdev.h>
29 #include <linux/delay.h>
30 #include "scsi.h"
31 #include <scsi/scsi_host.h>
32 #include <linux/libata.h>
33
34 #define DRV_NAME        "ata_piix"
35 #define DRV_VERSION     "1.02"
36
37 enum {
38         PIIX_IOCFG              = 0x54, /* IDE I/O configuration register */
39         ICH5_PMR                = 0x90, /* port mapping register */
40         ICH5_PCS                = 0x92, /* port control and status */
41
42         PIIX_FLAG_CHECKINTR     = (1 << 29), /* make sure PCI INTx enabled */
43         PIIX_FLAG_COMBINED      = (1 << 30), /* combined mode possible */
44
45         /* combined mode.  if set, PATA is channel 0.
46          * if clear, PATA is channel 1.
47          */
48         PIIX_COMB_PATA_P0       = (1 << 1),
49         PIIX_COMB               = (1 << 2), /* combined mode enabled? */
50
51         PIIX_PORT_PRESENT       = (1 << 0),
52         PIIX_PORT_ENABLED       = (1 << 4),
53
54         PIIX_80C_PRI            = (1 << 5) | (1 << 4),
55         PIIX_80C_SEC            = (1 << 7) | (1 << 6),
56
57         ich5_pata               = 0,
58         ich5_sata               = 1,
59         piix4_pata              = 2,
60         ich6_sata               = 3,
61 };
62
63 static int piix_init_one (struct pci_dev *pdev,
64                                     const struct pci_device_id *ent);
65
66 static void piix_pata_phy_reset(struct ata_port *ap);
67 static void piix_sata_phy_reset(struct ata_port *ap);
68 static void piix_set_piomode (struct ata_port *ap, struct ata_device *adev,
69                               unsigned int pio);
70 static void piix_set_udmamode (struct ata_port *ap, struct ata_device *adev,
71                                unsigned int udma);
72
73 static unsigned int in_module_init = 1;
74
75 static struct pci_device_id piix_pci_tbl[] = {
76 #ifdef ATA_ENABLE_PATA
77         { 0x8086, 0x7111, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix4_pata },
78         { 0x8086, 0x24db, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_pata },
79         { 0x8086, 0x25a2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_pata },
80 #endif
81
82         /* NOTE: The following PCI ids must be kept in sync with the
83          * list in drivers/pci/quirks.c.
84          */
85
86         { 0x8086, 0x24d1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
87         { 0x8086, 0x24df, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
88         { 0x8086, 0x25a3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
89         { 0x8086, 0x25b0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
90
91         /* ICH6 operates in two modes, "looks-like-ICH5" mode,
92          * and enhanced mode, with queueing and other fancy stuff.
93          * This is distinguished by PCI class code.
94          */
95         { 0x8086, 0x2651, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata },
96         { 0x8086, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata },
97
98         { }     /* terminate list */
99 };
100
101 static struct pci_driver piix_pci_driver = {
102         .name                   = DRV_NAME,
103         .id_table               = piix_pci_tbl,
104         .probe                  = piix_init_one,
105         .remove                 = ata_pci_remove_one,
106 };
107
108 static Scsi_Host_Template piix_sht = {
109         .module                 = THIS_MODULE,
110         .name                   = DRV_NAME,
111         .queuecommand           = ata_scsi_queuecmd,
112         .eh_strategy_handler    = ata_scsi_error,
113         .can_queue              = ATA_DEF_QUEUE,
114         .this_id                = ATA_SHT_THIS_ID,
115         .sg_tablesize           = LIBATA_MAX_PRD,
116         .max_sectors            = ATA_MAX_SECTORS,
117         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
118         .emulated               = ATA_SHT_EMULATED,
119         .use_clustering         = ATA_SHT_USE_CLUSTERING,
120         .proc_name              = DRV_NAME,
121         .dma_boundary           = ATA_DMA_BOUNDARY,
122         .slave_configure        = ata_scsi_slave_config,
123         .bios_param             = ata_std_bios_param,
124 };
125
126 static struct ata_port_operations piix_pata_ops = {
127         .port_disable           = ata_port_disable,
128         .set_piomode            = piix_set_piomode,
129         .set_udmamode           = piix_set_udmamode,
130
131         .tf_load                = ata_tf_load_pio,
132         .tf_read                = ata_tf_read_pio,
133         .check_status           = ata_check_status_pio,
134         .exec_command           = ata_exec_command_pio,
135
136         .phy_reset              = piix_pata_phy_reset,
137
138         .bmdma_setup            = ata_bmdma_setup_pio,
139         .bmdma_start            = ata_bmdma_start_pio,
140         .qc_prep                = ata_qc_prep,
141         .qc_issue               = ata_qc_issue_prot,
142
143         .eng_timeout            = ata_eng_timeout,
144
145         .irq_handler            = ata_interrupt,
146         .irq_clear              = ata_bmdma_irq_clear,
147
148         .port_start             = ata_port_start,
149         .port_stop              = ata_port_stop,
150 };
151
152 static struct ata_port_operations piix_sata_ops = {
153         .port_disable           = ata_port_disable,
154         .set_piomode            = piix_set_piomode,
155         .set_udmamode           = piix_set_udmamode,
156
157         .tf_load                = ata_tf_load_pio,
158         .tf_read                = ata_tf_read_pio,
159         .check_status           = ata_check_status_pio,
160         .exec_command           = ata_exec_command_pio,
161
162         .phy_reset              = piix_sata_phy_reset,
163
164         .bmdma_setup            = ata_bmdma_setup_pio,
165         .bmdma_start            = ata_bmdma_start_pio,
166         .qc_prep                = ata_qc_prep,
167         .qc_issue               = ata_qc_issue_prot,
168
169         .eng_timeout            = ata_eng_timeout,
170
171         .irq_handler            = ata_interrupt,
172         .irq_clear              = ata_bmdma_irq_clear,
173
174         .port_start             = ata_port_start,
175         .port_stop              = ata_port_stop,
176 };
177
178 static struct ata_port_info piix_port_info[] = {
179         /* ich5_pata */
180         {
181                 .sht            = &piix_sht,
182                 .host_flags     = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST |
183                                   PIIX_FLAG_CHECKINTR,
184                 .pio_mask       = 0x03, /* pio3-4 */
185                 .udma_mask      = ATA_UDMA_MASK_40C, /* FIXME: cbl det */
186                 .port_ops       = &piix_pata_ops,
187         },
188
189         /* ich5_sata */
190         {
191                 .sht            = &piix_sht,
192                 .host_flags     = ATA_FLAG_SATA | ATA_FLAG_SRST |
193                                   PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR,
194                 .pio_mask       = 0x03, /* pio3-4 */
195                 .udma_mask      = 0x7f, /* udma0-6 ; FIXME */
196                 .port_ops       = &piix_sata_ops,
197         },
198
199         /* piix4_pata */
200         {
201                 .sht            = &piix_sht,
202                 .host_flags     = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
203                 .pio_mask       = 0x03, /* pio3-4 */
204                 .udma_mask      = ATA_UDMA_MASK_40C, /* FIXME: cbl det */
205                 .port_ops       = &piix_pata_ops,
206         },
207
208         /* ich6_sata */
209         {
210                 .sht            = &piix_sht,
211                 .host_flags     = ATA_FLAG_SATA | ATA_FLAG_SRST |
212                                   PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
213                                   ATA_FLAG_SLAVE_POSS,
214                 .pio_mask       = 0x03, /* pio3-4 */
215                 .udma_mask      = 0x7f, /* udma0-6 ; FIXME */
216                 .port_ops       = &piix_sata_ops,
217         },
218 };
219
220 static struct pci_bits piix_enable_bits[] = {
221         { 0x41U, 1U, 0x80UL, 0x80UL },  /* port 0 */
222         { 0x43U, 1U, 0x80UL, 0x80UL },  /* port 1 */
223 };
224
225 MODULE_AUTHOR("Andre Hedrick, Alan Cox, Andrzej Krzysztofowicz, Jeff Garzik");
226 MODULE_DESCRIPTION("SCSI low-level driver for Intel PIIX/ICH ATA controllers");
227 MODULE_LICENSE("GPL");
228 MODULE_DEVICE_TABLE(pci, piix_pci_tbl);
229
230 /**
231  *      piix_pata_cbl_detect - Probe host controller cable detect info
232  *      @ap: Port for which cable detect info is desired
233  *
234  *      Read 80c cable indicator from SATA PCI device's PCI config
235  *      register.  This register is normally set by firmware (BIOS).
236  *
237  *      LOCKING:
238  *      None (inherited from caller).
239  */
240 static void piix_pata_cbl_detect(struct ata_port *ap)
241 {
242         struct pci_dev *pdev = ap->host_set->pdev;
243         u8 tmp, mask;
244
245         /* no 80c support in host controller? */
246         if ((ap->udma_mask & ~ATA_UDMA_MASK_40C) == 0)
247                 goto cbl40;
248
249         /* check BIOS cable detect results */
250         mask = ap->port_no == 0 ? PIIX_80C_PRI : PIIX_80C_SEC;
251         pci_read_config_byte(pdev, PIIX_IOCFG, &tmp);
252         if ((tmp & mask) == 0)
253                 goto cbl40;
254
255         ap->cbl = ATA_CBL_PATA80;
256         return;
257
258 cbl40:
259         ap->cbl = ATA_CBL_PATA40;
260         ap->udma_mask &= ATA_UDMA_MASK_40C;
261 }
262
263 /**
264  *      piix_pata_phy_reset - Probe specified port on PATA host controller
265  *      @ap: Port to probe
266  *
267  *      Probe PATA phy.
268  *
269  *      LOCKING:
270  *      None (inherited from caller).
271  */
272
273 static void piix_pata_phy_reset(struct ata_port *ap)
274 {
275         if (!pci_test_config_bits(ap->host_set->pdev,
276                                   &piix_enable_bits[ap->port_no])) {
277                 ata_port_disable(ap);
278                 printk(KERN_INFO "ata%u: port disabled. ignoring.\n", ap->id);
279                 return;
280         }
281
282         piix_pata_cbl_detect(ap);
283
284         ata_port_probe(ap);
285
286         ata_bus_reset(ap);
287 }
288
289 /**
290  *      piix_sata_probe - Probe PCI device for present SATA devices
291  *      @ap: Port associated with the PCI device we wish to probe
292  *
293  *      Reads SATA PCI device's PCI config register Port Configuration
294  *      and Status (PCS) to determine port and device availability.
295  *
296  *      LOCKING:
297  *      None (inherited from caller).
298  *
299  *      RETURNS:
300  *      Non-zero if device detected, zero otherwise.
301  */
302 static int piix_sata_probe (struct ata_port *ap)
303 {
304         struct pci_dev *pdev = ap->host_set->pdev;
305         int combined = (ap->flags & ATA_FLAG_SLAVE_POSS);
306         int orig_mask, mask, i;
307         u8 pcs;
308
309         mask = (PIIX_PORT_PRESENT << ap->port_no) |
310                (PIIX_PORT_ENABLED << ap->port_no);
311
312         pci_read_config_byte(pdev, ICH5_PCS, &pcs);
313         orig_mask = (int) pcs & 0xff;
314
315         /* TODO: this is vaguely wrong for ICH6 combined mode,
316          * where only two of the four SATA ports are mapped
317          * onto a single ATA channel.  It is also vaguely inaccurate
318          * for ICH5, which has only two ports.  However, this is ok,
319          * as further device presence detection code will handle
320          * any false positives produced here.
321          */
322
323         for (i = 0; i < 4; i++) {
324                 mask = (PIIX_PORT_PRESENT << i) | (PIIX_PORT_ENABLED << i);
325
326                 if ((orig_mask & mask) == mask)
327                         if (combined || (i == ap->port_no))
328                                 return 1;
329         }
330
331         return 0;
332 }
333
334 /**
335  *      piix_sata_phy_reset - Probe specified port on SATA host controller
336  *      @ap: Port to probe
337  *
338  *      Probe SATA phy.
339  *
340  *      LOCKING:
341  *      None (inherited from caller).
342  */
343
344 static void piix_sata_phy_reset(struct ata_port *ap)
345 {
346         if (!piix_sata_probe(ap)) {
347                 ata_port_disable(ap);
348                 printk(KERN_INFO "ata%u: SATA port has no device.\n", ap->id);
349                 return;
350         }
351
352         ap->cbl = ATA_CBL_SATA;
353
354         ata_port_probe(ap);
355
356         ata_bus_reset(ap);
357 }
358
359 /**
360  *      piix_set_piomode - Initialize host controller PATA PIO timings
361  *      @ap: Port whose timings we are configuring
362  *      @adev: um
363  *      @pio: PIO mode, 0 - 4
364  *
365  *      Set PIO mode for device, in host controller PCI config space.
366  *
367  *      LOCKING:
368  *      None (inherited from caller).
369  */
370
371 static void piix_set_piomode (struct ata_port *ap, struct ata_device *adev,
372                               unsigned int pio)
373 {
374         struct pci_dev *dev     = ap->host_set->pdev;
375         unsigned int is_slave   = (adev->flags & ATA_DFLAG_MASTER) ? 0 : 1;
376         unsigned int master_port= ap->port_no ? 0x42 : 0x40;
377         unsigned int slave_port = 0x44;
378         u16 master_data;
379         u8 slave_data;
380
381         static const     /* ISP  RTC */
382         u8 timings[][2] = { { 0, 0 },
383                             { 0, 0 },
384                             { 1, 0 },
385                             { 2, 1 },
386                             { 2, 3 }, };
387
388         pci_read_config_word(dev, master_port, &master_data);
389         if (is_slave) {
390                 master_data |= 0x4000;
391                 /* enable PPE, IE and TIME */
392                 master_data |= 0x0070;
393                 pci_read_config_byte(dev, slave_port, &slave_data);
394                 slave_data &= (ap->port_no ? 0x0f : 0xf0);
395                 slave_data |=
396                         (timings[pio][0] << 2) |
397                         (timings[pio][1] << (ap->port_no ? 4 : 0));
398         } else {
399                 master_data &= 0xccf8;
400                 /* enable PPE, IE and TIME */
401                 master_data |= 0x0007;
402                 master_data |=
403                         (timings[pio][0] << 12) |
404                         (timings[pio][1] << 8);
405         }
406         pci_write_config_word(dev, master_port, master_data);
407         if (is_slave)
408                 pci_write_config_byte(dev, slave_port, slave_data);
409 }
410
411 /**
412  *      piix_set_udmamode - Initialize host controller PATA PIO timings
413  *      @ap: Port whose timings we are configuring
414  *      @adev: um
415  *      @udma: udma mode, 0 - 6
416  *
417  *      Set UDMA mode for device, in host controller PCI config space.
418  *
419  *      LOCKING:
420  *      None (inherited from caller).
421  */
422
423 static void piix_set_udmamode (struct ata_port *ap, struct ata_device *adev,
424                               unsigned int udma)
425 {
426         struct pci_dev *dev     = ap->host_set->pdev;
427         u8 maslave              = ap->port_no ? 0x42 : 0x40;
428         u8 speed                = udma;
429         unsigned int drive_dn   = (ap->port_no ? 2 : 0) + adev->devno;
430         int a_speed             = 3 << (drive_dn * 4);
431         int u_flag              = 1 << drive_dn;
432         int v_flag              = 0x01 << drive_dn;
433         int w_flag              = 0x10 << drive_dn;
434         int u_speed             = 0;
435         int                     sitre;
436         u16                     reg4042, reg4a;
437         u8                      reg48, reg54, reg55;
438
439         pci_read_config_word(dev, maslave, &reg4042);
440         DPRINTK("reg4042 = 0x%04x\n", reg4042);
441         sitre = (reg4042 & 0x4000) ? 1 : 0;
442         pci_read_config_byte(dev, 0x48, &reg48);
443         pci_read_config_word(dev, 0x4a, &reg4a);
444         pci_read_config_byte(dev, 0x54, &reg54);
445         pci_read_config_byte(dev, 0x55, &reg55);
446
447         switch(speed) {
448                 case XFER_UDMA_4:
449                 case XFER_UDMA_2:       u_speed = 2 << (drive_dn * 4); break;
450                 case XFER_UDMA_6:
451                 case XFER_UDMA_5:
452                 case XFER_UDMA_3:
453                 case XFER_UDMA_1:       u_speed = 1 << (drive_dn * 4); break;
454                 case XFER_UDMA_0:       u_speed = 0 << (drive_dn * 4); break;
455                 default:
456                         BUG();
457                         return;
458         }
459
460         if (!(reg48 & u_flag))
461                 pci_write_config_byte(dev, 0x48, reg48 | u_flag);
462         if (speed == XFER_UDMA_5) {
463                 pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
464         } else {
465                 pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
466         }
467         if ((reg4a & a_speed) != u_speed)
468                 pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
469         if (speed > XFER_UDMA_2) {
470                 if (!(reg54 & v_flag))
471                         pci_write_config_byte(dev, 0x54, reg54 | v_flag);
472         } else
473                 pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
474 }
475
476 /* move to PCI layer, integrate w/ MSI stuff */
477 static void pci_enable_intx(struct pci_dev *pdev)
478 {
479         u16 pci_command;
480
481         pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
482         if (pci_command & PCI_COMMAND_INTX_DISABLE) {
483                 pci_command &= ~PCI_COMMAND_INTX_DISABLE;
484                 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
485         }
486 }
487
488 /**
489  *      piix_init_one - Register PIIX ATA PCI device with kernel services
490  *      @pdev: PCI device to register
491  *      @ent: Entry in piix_pci_tbl matching with @pdev
492  *
493  *      Called from kernel PCI layer.  We probe for combined mode (sigh),
494  *      and then hand over control to libata, for it to do the rest.
495  *
496  *      LOCKING:
497  *      Inherited from PCI layer (may sleep).
498  *
499  *      RETURNS:
500  *      Zero on success, or -ERRNO value.
501  */
502
503 static int piix_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
504 {
505         static int printed_version;
506         struct ata_port_info *port_info[2];
507         unsigned int combined = 0, n_ports = 1;
508         unsigned int pata_chan = 0, sata_chan = 0;
509
510         if (!printed_version++)
511                 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
512
513         /* no hotplugging support (FIXME) */
514         if (!in_module_init)
515                 return -ENODEV;
516
517         port_info[0] = &piix_port_info[ent->driver_data];
518         port_info[1] = NULL;
519
520         if (port_info[0]->host_flags & PIIX_FLAG_COMBINED) {
521                 u8 tmp;
522                 pci_read_config_byte(pdev, ICH5_PMR, &tmp);
523
524                 if (tmp & PIIX_COMB) {
525                         combined = 1;
526                         if (tmp & PIIX_COMB_PATA_P0)
527                                 sata_chan = 1;
528                         else
529                                 pata_chan = 1;
530                 }
531         }
532
533         /* On ICH5, some BIOSen disable the interrupt using the
534          * PCI_COMMAND_INTX_DISABLE bit added in PCI 2.3.
535          * On ICH6, this bit has the same effect, but only when
536          * MSI is disabled (and it is disabled, as we don't use
537          * message-signalled interrupts currently).
538          */
539         if (port_info[0]->host_flags & PIIX_FLAG_CHECKINTR)
540                 pci_enable_intx(pdev);
541
542         if (combined) {
543                 port_info[sata_chan] = &piix_port_info[ent->driver_data];
544                 port_info[sata_chan]->host_flags |= ATA_FLAG_SLAVE_POSS;
545                 port_info[pata_chan] = &piix_port_info[ich5_pata];
546                 n_ports++;
547
548                 printk(KERN_WARNING DRV_NAME ": combined mode detected\n");
549         }
550
551         return ata_pci_init_one(pdev, port_info, n_ports);
552 }
553
554 /**
555  *      piix_init -
556  *
557  *      LOCKING:
558  *
559  *      RETURNS:
560  *
561  */
562
563 static int __init piix_init(void)
564 {
565         int rc;
566
567         DPRINTK("pci_module_init\n");
568         rc = pci_module_init(&piix_pci_driver);
569         if (rc)
570                 return rc;
571
572         in_module_init = 0;
573
574         DPRINTK("done\n");
575         return 0;
576 }
577
578 /**
579  *      piix_exit -
580  *
581  *      LOCKING:
582  *
583  */
584
585 static void __exit piix_exit(void)
586 {
587         pci_unregister_driver(&piix_pci_driver);
588 }
589
590 module_init(piix_init);
591 module_exit(piix_exit);
592