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