1669a5db4SJeff Garzik /* 2c343a839SAlan Cox * pata_it821x.c - IT821x PATA for new ATA layer 3669a5db4SJeff Garzik * (C) 2005 Red Hat Inc 4ab771630SAlan Cox * Alan Cox <alan@lxorguk.ukuu.org.uk> 5374abf2cSBartlomiej Zolnierkiewicz * (C) 2007 Bartlomiej Zolnierkiewicz 6669a5db4SJeff Garzik * 7669a5db4SJeff Garzik * based upon 8669a5db4SJeff Garzik * 9669a5db4SJeff Garzik * it821x.c 10669a5db4SJeff Garzik * 11669a5db4SJeff Garzik * linux/drivers/ide/pci/it821x.c Version 0.09 December 2004 12669a5db4SJeff Garzik * 13ab771630SAlan Cox * Copyright (C) 2004 Red Hat 14669a5db4SJeff Garzik * 15669a5db4SJeff Garzik * May be copied or modified under the terms of the GNU General Public License 16669a5db4SJeff Garzik * Based in part on the ITE vendor provided SCSI driver. 17669a5db4SJeff Garzik * 18631dd1a8SJustin P. Mattock * Documentation available from IT8212F_V04.pdf 19631dd1a8SJustin P. Mattock * http://www.ite.com.tw/EN/products_more.aspx?CategoryID=3&ID=5,91 20669a5db4SJeff Garzik * Some other documents are NDA. 21669a5db4SJeff Garzik * 22669a5db4SJeff Garzik * The ITE8212 isn't exactly a standard IDE controller. It has two 23669a5db4SJeff Garzik * modes. In pass through mode then it is an IDE controller. In its smart 24669a5db4SJeff Garzik * mode its actually quite a capable hardware raid controller disguised 25669a5db4SJeff Garzik * as an IDE controller. Smart mode only understands DMA read/write and 26669a5db4SJeff Garzik * identify, none of the fancier commands apply. The IT8211 is identical 27669a5db4SJeff Garzik * in other respects but lacks the raid mode. 28669a5db4SJeff Garzik * 29669a5db4SJeff Garzik * Errata: 30669a5db4SJeff Garzik * o Rev 0x10 also requires master/slave hold the same DMA timings and 31669a5db4SJeff Garzik * cannot do ATAPI MWDMA. 32669a5db4SJeff Garzik * o The identify data for raid volumes lacks CHS info (technically ok) 33669a5db4SJeff Garzik * but also fails to set the LBA28 and other bits. We fix these in 34669a5db4SJeff Garzik * the IDE probe quirk code. 35669a5db4SJeff Garzik * o If you write LBA48 sized I/O's (ie > 256 sector) in smart mode 36669a5db4SJeff Garzik * raid then the controller firmware dies 37669a5db4SJeff Garzik * o Smart mode without RAID doesn't clear all the necessary identify 38669a5db4SJeff Garzik * bits to reduce the command set to the one used 39669a5db4SJeff Garzik * 40669a5db4SJeff Garzik * This has a few impacts on the driver 41669a5db4SJeff Garzik * - In pass through mode we do all the work you would expect 42669a5db4SJeff Garzik * - In smart mode the clocking set up is done by the controller generally 43669a5db4SJeff Garzik * but we must watch the other limits and filter. 44669a5db4SJeff Garzik * - There are a few extra vendor commands that actually talk to the 45669a5db4SJeff Garzik * controller but only work PIO with no IRQ. 46669a5db4SJeff Garzik * 47669a5db4SJeff Garzik * Vendor areas of the identify block in smart mode are used for the 48669a5db4SJeff Garzik * timing and policy set up. Each HDD in raid mode also has a serial 49669a5db4SJeff Garzik * block on the disk. The hardware extra commands are get/set chip status, 50669a5db4SJeff Garzik * rebuild, get rebuild status. 51669a5db4SJeff Garzik * 52669a5db4SJeff Garzik * In Linux the driver supports pass through mode as if the device was 53669a5db4SJeff Garzik * just another IDE controller. If the smart mode is running then 54669a5db4SJeff Garzik * volumes are managed by the controller firmware and each IDE "disk" 55669a5db4SJeff Garzik * is a raid volume. Even more cute - the controller can do automated 56669a5db4SJeff Garzik * hotplug and rebuild. 57669a5db4SJeff Garzik * 58669a5db4SJeff Garzik * The pass through controller itself is a little demented. It has a 59669a5db4SJeff Garzik * flaw that it has a single set of PIO/MWDMA timings per channel so 60669a5db4SJeff Garzik * non UDMA devices restrict each others performance. It also has a 61669a5db4SJeff Garzik * single clock source per channel so mixed UDMA100/133 performance 62669a5db4SJeff Garzik * isn't perfect and we have to pick a clock. Thankfully none of this 63669a5db4SJeff Garzik * matters in smart mode. ATAPI DMA is not currently supported. 64669a5db4SJeff Garzik * 65669a5db4SJeff Garzik * It seems the smart mode is a win for RAID1/RAID10 but otherwise not. 66669a5db4SJeff Garzik * 67669a5db4SJeff Garzik * TODO 68669a5db4SJeff Garzik * - ATAPI and other speed filtering 69669a5db4SJeff Garzik * - RAID configuration ioctls 70669a5db4SJeff Garzik */ 71669a5db4SJeff Garzik 72669a5db4SJeff Garzik #include <linux/kernel.h> 73669a5db4SJeff Garzik #include <linux/module.h> 74669a5db4SJeff Garzik #include <linux/pci.h> 75669a5db4SJeff Garzik #include <linux/blkdev.h> 76669a5db4SJeff Garzik #include <linux/delay.h> 775a0e3ad6STejun Heo #include <linux/slab.h> 78669a5db4SJeff Garzik #include <scsi/scsi_host.h> 79669a5db4SJeff Garzik #include <linux/libata.h> 80669a5db4SJeff Garzik 81669a5db4SJeff Garzik 82669a5db4SJeff Garzik #define DRV_NAME "pata_it821x" 834a99d95fSAlan Cox #define DRV_VERSION "0.4.2" 84669a5db4SJeff Garzik 85669a5db4SJeff Garzik struct it821x_dev 86669a5db4SJeff Garzik { 87669a5db4SJeff Garzik unsigned int smart:1, /* Are we in smart raid mode */ 88669a5db4SJeff Garzik timing10:1; /* Rev 0x10 */ 89669a5db4SJeff Garzik u8 clock_mode; /* 0, ATA_50 or ATA_66 */ 90669a5db4SJeff Garzik u8 want[2][2]; /* Mode/Pri log for master slave */ 91669a5db4SJeff Garzik /* We need these for switching the clock when DMA goes on/off 92669a5db4SJeff Garzik The high byte is the 66Mhz timing */ 93669a5db4SJeff Garzik u16 pio[2]; /* Cached PIO values */ 94669a5db4SJeff Garzik u16 mwdma[2]; /* Cached MWDMA values */ 95669a5db4SJeff Garzik u16 udma[2]; /* Cached UDMA values (per drive) */ 96669a5db4SJeff Garzik u16 last_device; /* Master or slave loaded ? */ 97669a5db4SJeff Garzik }; 98669a5db4SJeff Garzik 99669a5db4SJeff Garzik #define ATA_66 0 100669a5db4SJeff Garzik #define ATA_50 1 101669a5db4SJeff Garzik #define ATA_ANY 2 102669a5db4SJeff Garzik 103669a5db4SJeff Garzik #define UDMA_OFF 0 104669a5db4SJeff Garzik #define MWDMA_OFF 0 105669a5db4SJeff Garzik 106669a5db4SJeff Garzik /* 107669a5db4SJeff Garzik * We allow users to force the card into non raid mode without 1083a4fa0a2SRobert P. J. Day * flashing the alternative BIOS. This is also necessary right now 109669a5db4SJeff Garzik * for embedded platforms that cannot run a PC BIOS but are using this 110669a5db4SJeff Garzik * device. 111669a5db4SJeff Garzik */ 112669a5db4SJeff Garzik 113669a5db4SJeff Garzik static int it8212_noraid; 114669a5db4SJeff Garzik 115669a5db4SJeff Garzik /** 116669a5db4SJeff Garzik * it821x_program - program the PIO/MWDMA registers 117669a5db4SJeff Garzik * @ap: ATA port 118669a5db4SJeff Garzik * @adev: Device to program 119669a5db4SJeff Garzik * @timing: Timing value (66Mhz in top 8bits, 50 in the low 8) 120669a5db4SJeff Garzik * 121669a5db4SJeff Garzik * Program the PIO/MWDMA timing for this channel according to the 122669a5db4SJeff Garzik * current clock. These share the same register so are managed by 123669a5db4SJeff Garzik * the DMA start/stop sequence as with the old driver. 124669a5db4SJeff Garzik */ 125669a5db4SJeff Garzik 126669a5db4SJeff Garzik static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing) 127669a5db4SJeff Garzik { 128669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev); 129669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 130669a5db4SJeff Garzik int channel = ap->port_no; 131669a5db4SJeff Garzik u8 conf; 132669a5db4SJeff Garzik 133669a5db4SJeff Garzik /* Program PIO/MWDMA timing bits */ 134669a5db4SJeff Garzik if (itdev->clock_mode == ATA_66) 135669a5db4SJeff Garzik conf = timing >> 8; 136669a5db4SJeff Garzik else 137669a5db4SJeff Garzik conf = timing & 0xFF; 138669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x54 + 4 * channel, conf); 139669a5db4SJeff Garzik } 140669a5db4SJeff Garzik 141669a5db4SJeff Garzik 142669a5db4SJeff Garzik /** 143669a5db4SJeff Garzik * it821x_program_udma - program the UDMA registers 144669a5db4SJeff Garzik * @ap: ATA port 145669a5db4SJeff Garzik * @adev: ATA device to update 146669a5db4SJeff Garzik * @timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz 147669a5db4SJeff Garzik * 148669a5db4SJeff Garzik * Program the UDMA timing for this drive according to the 149669a5db4SJeff Garzik * current clock. Handles the dual clocks and also knows about 150669a5db4SJeff Garzik * the errata on the 0x10 revision. The UDMA errata is partly handled 151669a5db4SJeff Garzik * here and partly in start_dma. 152669a5db4SJeff Garzik */ 153669a5db4SJeff Garzik 154669a5db4SJeff Garzik static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing) 155669a5db4SJeff Garzik { 156669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 157669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev); 158669a5db4SJeff Garzik int channel = ap->port_no; 159669a5db4SJeff Garzik int unit = adev->devno; 160669a5db4SJeff Garzik u8 conf; 161669a5db4SJeff Garzik 162669a5db4SJeff Garzik /* Program UDMA timing bits */ 163669a5db4SJeff Garzik if (itdev->clock_mode == ATA_66) 164669a5db4SJeff Garzik conf = timing >> 8; 165669a5db4SJeff Garzik else 166669a5db4SJeff Garzik conf = timing & 0xFF; 167669a5db4SJeff Garzik if (itdev->timing10 == 0) 168669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf); 169669a5db4SJeff Garzik else { 170669a5db4SJeff Garzik /* Early revision must be programmed for both together */ 171669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x56 + 4 * channel, conf); 172669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf); 173669a5db4SJeff Garzik } 174669a5db4SJeff Garzik } 175669a5db4SJeff Garzik 176669a5db4SJeff Garzik /** 177669a5db4SJeff Garzik * it821x_clock_strategy 178669a5db4SJeff Garzik * @ap: ATA interface 179669a5db4SJeff Garzik * @adev: ATA device being updated 180669a5db4SJeff Garzik * 181669a5db4SJeff Garzik * Select between the 50 and 66Mhz base clocks to get the best 182669a5db4SJeff Garzik * results for this interface. 183669a5db4SJeff Garzik */ 184669a5db4SJeff Garzik 185669a5db4SJeff Garzik static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev) 186669a5db4SJeff Garzik { 187669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev); 188669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 189669a5db4SJeff Garzik u8 unit = adev->devno; 190669a5db4SJeff Garzik struct ata_device *pair = ata_dev_pair(adev); 191669a5db4SJeff Garzik 192669a5db4SJeff Garzik int clock, altclock; 193669a5db4SJeff Garzik u8 v; 194669a5db4SJeff Garzik int sel = 0; 195669a5db4SJeff Garzik 196669a5db4SJeff Garzik /* Look for the most wanted clocking */ 197669a5db4SJeff Garzik if (itdev->want[0][0] > itdev->want[1][0]) { 198669a5db4SJeff Garzik clock = itdev->want[0][1]; 199669a5db4SJeff Garzik altclock = itdev->want[1][1]; 200669a5db4SJeff Garzik } else { 201669a5db4SJeff Garzik clock = itdev->want[1][1]; 202669a5db4SJeff Garzik altclock = itdev->want[0][1]; 203669a5db4SJeff Garzik } 204669a5db4SJeff Garzik 205669a5db4SJeff Garzik /* Master doesn't care does the slave ? */ 206669a5db4SJeff Garzik if (clock == ATA_ANY) 207669a5db4SJeff Garzik clock = altclock; 208669a5db4SJeff Garzik 209669a5db4SJeff Garzik /* Nobody cares - keep the same clock */ 210669a5db4SJeff Garzik if (clock == ATA_ANY) 211669a5db4SJeff Garzik return; 212669a5db4SJeff Garzik /* No change */ 213669a5db4SJeff Garzik if (clock == itdev->clock_mode) 214669a5db4SJeff Garzik return; 215669a5db4SJeff Garzik 216669a5db4SJeff Garzik /* Load this into the controller */ 217669a5db4SJeff Garzik if (clock == ATA_66) 218669a5db4SJeff Garzik itdev->clock_mode = ATA_66; 219669a5db4SJeff Garzik else { 220669a5db4SJeff Garzik itdev->clock_mode = ATA_50; 221669a5db4SJeff Garzik sel = 1; 222669a5db4SJeff Garzik } 223669a5db4SJeff Garzik pci_read_config_byte(pdev, 0x50, &v); 224669a5db4SJeff Garzik v &= ~(1 << (1 + ap->port_no)); 225669a5db4SJeff Garzik v |= sel << (1 + ap->port_no); 226669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x50, v); 227669a5db4SJeff Garzik 228669a5db4SJeff Garzik /* 229669a5db4SJeff Garzik * Reprogram the UDMA/PIO of the pair drive for the switch 230669a5db4SJeff Garzik * MWDMA will be dealt with by the dma switcher 231669a5db4SJeff Garzik */ 232669a5db4SJeff Garzik if (pair && itdev->udma[1-unit] != UDMA_OFF) { 233669a5db4SJeff Garzik it821x_program_udma(ap, pair, itdev->udma[1-unit]); 234669a5db4SJeff Garzik it821x_program(ap, pair, itdev->pio[1-unit]); 235669a5db4SJeff Garzik } 236669a5db4SJeff Garzik /* 237669a5db4SJeff Garzik * Reprogram the UDMA/PIO of our drive for the switch. 238669a5db4SJeff Garzik * MWDMA will be dealt with by the dma switcher 239669a5db4SJeff Garzik */ 240669a5db4SJeff Garzik if (itdev->udma[unit] != UDMA_OFF) { 241669a5db4SJeff Garzik it821x_program_udma(ap, adev, itdev->udma[unit]); 242669a5db4SJeff Garzik it821x_program(ap, adev, itdev->pio[unit]); 243669a5db4SJeff Garzik } 244669a5db4SJeff Garzik } 245669a5db4SJeff Garzik 246669a5db4SJeff Garzik /** 247669a5db4SJeff Garzik * it821x_passthru_set_piomode - set PIO mode data 248669a5db4SJeff Garzik * @ap: ATA interface 249669a5db4SJeff Garzik * @adev: ATA device 250669a5db4SJeff Garzik * 251669a5db4SJeff Garzik * Configure for PIO mode. This is complicated as the register is 252669a5db4SJeff Garzik * shared by PIO and MWDMA and for both channels. 253669a5db4SJeff Garzik */ 254669a5db4SJeff Garzik 255669a5db4SJeff Garzik static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev) 256669a5db4SJeff Garzik { 257669a5db4SJeff Garzik /* Spec says 89 ref driver uses 88 */ 258669a5db4SJeff Garzik static const u16 pio[] = { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 }; 259669a5db4SJeff Garzik static const u8 pio_want[] = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY }; 260669a5db4SJeff Garzik 261669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 262669a5db4SJeff Garzik int unit = adev->devno; 263669a5db4SJeff Garzik int mode_wanted = adev->pio_mode - XFER_PIO_0; 264669a5db4SJeff Garzik 265669a5db4SJeff Garzik /* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */ 266669a5db4SJeff Garzik itdev->want[unit][1] = pio_want[mode_wanted]; 267669a5db4SJeff Garzik itdev->want[unit][0] = 1; /* PIO is lowest priority */ 268669a5db4SJeff Garzik itdev->pio[unit] = pio[mode_wanted]; 269669a5db4SJeff Garzik it821x_clock_strategy(ap, adev); 270669a5db4SJeff Garzik it821x_program(ap, adev, itdev->pio[unit]); 271669a5db4SJeff Garzik } 272669a5db4SJeff Garzik 273669a5db4SJeff Garzik /** 274669a5db4SJeff Garzik * it821x_passthru_set_dmamode - set initial DMA mode data 275669a5db4SJeff Garzik * @ap: ATA interface 276669a5db4SJeff Garzik * @adev: ATA device 277669a5db4SJeff Garzik * 278669a5db4SJeff Garzik * Set up the DMA modes. The actions taken depend heavily on the mode 279669a5db4SJeff Garzik * to use. If UDMA is used as is hopefully the usual case then the 280669a5db4SJeff Garzik * timing register is private and we need only consider the clock. If 281669a5db4SJeff Garzik * we are using MWDMA then we have to manage the setting ourself as 282669a5db4SJeff Garzik * we switch devices and mode. 283669a5db4SJeff Garzik */ 284669a5db4SJeff Garzik 285669a5db4SJeff Garzik static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev) 286669a5db4SJeff Garzik { 287669a5db4SJeff Garzik static const u16 dma[] = { 0x8866, 0x3222, 0x3121 }; 288669a5db4SJeff Garzik static const u8 mwdma_want[] = { ATA_ANY, ATA_66, ATA_ANY }; 289669a5db4SJeff Garzik static const u16 udma[] = { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 }; 290669a5db4SJeff Garzik static const u8 udma_want[] = { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 }; 291669a5db4SJeff Garzik 292669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev); 293669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 294669a5db4SJeff Garzik int channel = ap->port_no; 295669a5db4SJeff Garzik int unit = adev->devno; 296669a5db4SJeff Garzik u8 conf; 297669a5db4SJeff Garzik 298669a5db4SJeff Garzik if (adev->dma_mode >= XFER_UDMA_0) { 299669a5db4SJeff Garzik int mode_wanted = adev->dma_mode - XFER_UDMA_0; 300669a5db4SJeff Garzik 301669a5db4SJeff Garzik itdev->want[unit][1] = udma_want[mode_wanted]; 302669a5db4SJeff Garzik itdev->want[unit][0] = 3; /* UDMA is high priority */ 303669a5db4SJeff Garzik itdev->mwdma[unit] = MWDMA_OFF; 304669a5db4SJeff Garzik itdev->udma[unit] = udma[mode_wanted]; 305669a5db4SJeff Garzik if (mode_wanted >= 5) 306669a5db4SJeff Garzik itdev->udma[unit] |= 0x8080; /* UDMA 5/6 select on */ 307669a5db4SJeff Garzik 308669a5db4SJeff Garzik /* UDMA on. Again revision 0x10 must do the pair */ 309669a5db4SJeff Garzik pci_read_config_byte(pdev, 0x50, &conf); 310669a5db4SJeff Garzik if (itdev->timing10) 311669a5db4SJeff Garzik conf &= channel ? 0x9F: 0xE7; 312669a5db4SJeff Garzik else 313669a5db4SJeff Garzik conf &= ~ (1 << (3 + 2 * channel + unit)); 314669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x50, conf); 315669a5db4SJeff Garzik it821x_clock_strategy(ap, adev); 316669a5db4SJeff Garzik it821x_program_udma(ap, adev, itdev->udma[unit]); 317669a5db4SJeff Garzik } else { 318669a5db4SJeff Garzik int mode_wanted = adev->dma_mode - XFER_MW_DMA_0; 319669a5db4SJeff Garzik 320669a5db4SJeff Garzik itdev->want[unit][1] = mwdma_want[mode_wanted]; 321669a5db4SJeff Garzik itdev->want[unit][0] = 2; /* MWDMA is low priority */ 322669a5db4SJeff Garzik itdev->mwdma[unit] = dma[mode_wanted]; 323669a5db4SJeff Garzik itdev->udma[unit] = UDMA_OFF; 324669a5db4SJeff Garzik 325669a5db4SJeff Garzik /* UDMA bits off - Revision 0x10 do them in pairs */ 326669a5db4SJeff Garzik pci_read_config_byte(pdev, 0x50, &conf); 327669a5db4SJeff Garzik if (itdev->timing10) 328669a5db4SJeff Garzik conf |= channel ? 0x60: 0x18; 329669a5db4SJeff Garzik else 330669a5db4SJeff Garzik conf |= 1 << (3 + 2 * channel + unit); 331669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x50, conf); 332669a5db4SJeff Garzik it821x_clock_strategy(ap, adev); 333669a5db4SJeff Garzik } 334669a5db4SJeff Garzik } 335669a5db4SJeff Garzik 336669a5db4SJeff Garzik /** 337669a5db4SJeff Garzik * it821x_passthru_dma_start - DMA start callback 338669a5db4SJeff Garzik * @qc: Command in progress 339669a5db4SJeff Garzik * 340669a5db4SJeff Garzik * Usually drivers set the DMA timing at the point the set_dmamode call 341669a5db4SJeff Garzik * is made. IT821x however requires we load new timings on the 342669a5db4SJeff Garzik * transitions in some cases. 343669a5db4SJeff Garzik */ 344669a5db4SJeff Garzik 345669a5db4SJeff Garzik static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc) 346669a5db4SJeff Garzik { 347669a5db4SJeff Garzik struct ata_port *ap = qc->ap; 348669a5db4SJeff Garzik struct ata_device *adev = qc->dev; 349669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 350669a5db4SJeff Garzik int unit = adev->devno; 351669a5db4SJeff Garzik 352669a5db4SJeff Garzik if (itdev->mwdma[unit] != MWDMA_OFF) 353669a5db4SJeff Garzik it821x_program(ap, adev, itdev->mwdma[unit]); 354669a5db4SJeff Garzik else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10) 355669a5db4SJeff Garzik it821x_program_udma(ap, adev, itdev->udma[unit]); 356669a5db4SJeff Garzik ata_bmdma_start(qc); 357669a5db4SJeff Garzik } 358669a5db4SJeff Garzik 359669a5db4SJeff Garzik /** 360669a5db4SJeff Garzik * it821x_passthru_dma_stop - DMA stop callback 361669a5db4SJeff Garzik * @qc: ATA command 362669a5db4SJeff Garzik * 363669a5db4SJeff Garzik * We loaded new timings in dma_start, as a result we need to restore 364669a5db4SJeff Garzik * the PIO timings in dma_stop so that the next command issue gets the 365669a5db4SJeff Garzik * right clock values. 366669a5db4SJeff Garzik */ 367669a5db4SJeff Garzik 368669a5db4SJeff Garzik static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc) 369669a5db4SJeff Garzik { 370669a5db4SJeff Garzik struct ata_port *ap = qc->ap; 371669a5db4SJeff Garzik struct ata_device *adev = qc->dev; 372669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 373669a5db4SJeff Garzik int unit = adev->devno; 374669a5db4SJeff Garzik 375669a5db4SJeff Garzik ata_bmdma_stop(qc); 376669a5db4SJeff Garzik if (itdev->mwdma[unit] != MWDMA_OFF) 377669a5db4SJeff Garzik it821x_program(ap, adev, itdev->pio[unit]); 378669a5db4SJeff Garzik } 379669a5db4SJeff Garzik 380669a5db4SJeff Garzik 381669a5db4SJeff Garzik /** 382669a5db4SJeff Garzik * it821x_passthru_dev_select - Select master/slave 383669a5db4SJeff Garzik * @ap: ATA port 384669a5db4SJeff Garzik * @device: Device number (not pointer) 385669a5db4SJeff Garzik * 3863a4fa0a2SRobert P. J. Day * Device selection hook. If necessary perform clock switching 387669a5db4SJeff Garzik */ 388669a5db4SJeff Garzik 389669a5db4SJeff Garzik static void it821x_passthru_dev_select(struct ata_port *ap, 390669a5db4SJeff Garzik unsigned int device) 391669a5db4SJeff Garzik { 392669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 393669a5db4SJeff Garzik if (itdev && device != itdev->last_device) { 3949af5c9c9STejun Heo struct ata_device *adev = &ap->link.device[device]; 395669a5db4SJeff Garzik it821x_program(ap, adev, itdev->pio[adev->devno]); 396669a5db4SJeff Garzik itdev->last_device = device; 397669a5db4SJeff Garzik } 3989363c382STejun Heo ata_sff_dev_select(ap, device); 399669a5db4SJeff Garzik } 400669a5db4SJeff Garzik 401669a5db4SJeff Garzik /** 4029363c382STejun Heo * it821x_smart_qc_issue - wrap qc issue prot 403669a5db4SJeff Garzik * @qc: command 404669a5db4SJeff Garzik * 405669a5db4SJeff Garzik * Wrap the command issue sequence for the IT821x. We need to 406669a5db4SJeff Garzik * perform out own device selection timing loads before the 407669a5db4SJeff Garzik * usual happenings kick off 408669a5db4SJeff Garzik */ 409669a5db4SJeff Garzik 4109363c382STejun Heo static unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc) 411669a5db4SJeff Garzik { 412669a5db4SJeff Garzik switch(qc->tf.command) 413669a5db4SJeff Garzik { 414669a5db4SJeff Garzik /* Commands the firmware supports */ 415669a5db4SJeff Garzik case ATA_CMD_READ: 416669a5db4SJeff Garzik case ATA_CMD_READ_EXT: 417669a5db4SJeff Garzik case ATA_CMD_WRITE: 418669a5db4SJeff Garzik case ATA_CMD_WRITE_EXT: 419669a5db4SJeff Garzik case ATA_CMD_PIO_READ: 420669a5db4SJeff Garzik case ATA_CMD_PIO_READ_EXT: 421669a5db4SJeff Garzik case ATA_CMD_PIO_WRITE: 422669a5db4SJeff Garzik case ATA_CMD_PIO_WRITE_EXT: 423669a5db4SJeff Garzik case ATA_CMD_READ_MULTI: 424669a5db4SJeff Garzik case ATA_CMD_READ_MULTI_EXT: 425669a5db4SJeff Garzik case ATA_CMD_WRITE_MULTI: 426669a5db4SJeff Garzik case ATA_CMD_WRITE_MULTI_EXT: 427669a5db4SJeff Garzik case ATA_CMD_ID_ATA: 428963e4975SAlan Cox case ATA_CMD_INIT_DEV_PARAMS: 429963e4975SAlan Cox case 0xFC: /* Internal 'report rebuild state' */ 430669a5db4SJeff Garzik /* Arguably should just no-op this one */ 431669a5db4SJeff Garzik case ATA_CMD_SET_FEATURES: 432360ff783STejun Heo return ata_bmdma_qc_issue(qc); 433669a5db4SJeff Garzik } 434669a5db4SJeff Garzik printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command); 435c5038fc0SAlan Cox return AC_ERR_DEV; 436669a5db4SJeff Garzik } 437669a5db4SJeff Garzik 438669a5db4SJeff Garzik /** 4399363c382STejun Heo * it821x_passthru_qc_issue - wrap qc issue prot 440669a5db4SJeff Garzik * @qc: command 441669a5db4SJeff Garzik * 442669a5db4SJeff Garzik * Wrap the command issue sequence for the IT821x. We need to 443669a5db4SJeff Garzik * perform out own device selection timing loads before the 444669a5db4SJeff Garzik * usual happenings kick off 445669a5db4SJeff Garzik */ 446669a5db4SJeff Garzik 4479363c382STejun Heo static unsigned int it821x_passthru_qc_issue(struct ata_queued_cmd *qc) 448669a5db4SJeff Garzik { 449669a5db4SJeff Garzik it821x_passthru_dev_select(qc->ap, qc->dev->devno); 450360ff783STejun Heo return ata_bmdma_qc_issue(qc); 451669a5db4SJeff Garzik } 452669a5db4SJeff Garzik 453669a5db4SJeff Garzik /** 454669a5db4SJeff Garzik * it821x_smart_set_mode - mode setting 4550260731fSTejun Heo * @link: interface to set up 456b229a7b0SAlan * @unused: device that failed (error only) 457669a5db4SJeff Garzik * 458669a5db4SJeff Garzik * Use a non standard set_mode function. We don't want to be tuned. 459669a5db4SJeff Garzik * The BIOS configured everything. Our job is not to fiddle. We 460669a5db4SJeff Garzik * read the dma enabled bits from the PCI configuration of the device 461669a5db4SJeff Garzik * and respect them. 462669a5db4SJeff Garzik */ 463669a5db4SJeff Garzik 4640260731fSTejun Heo static int it821x_smart_set_mode(struct ata_link *link, struct ata_device **unused) 465669a5db4SJeff Garzik { 466f58229f8STejun Heo struct ata_device *dev; 467669a5db4SJeff Garzik 4681eca4365STejun Heo ata_for_each_dev(dev, link, ENABLED) { 469669a5db4SJeff Garzik /* We don't really care */ 470669a5db4SJeff Garzik dev->pio_mode = XFER_PIO_0; 471669a5db4SJeff Garzik dev->dma_mode = XFER_MW_DMA_0; 472669a5db4SJeff Garzik /* We do need the right mode information for DMA or PIO 473669a5db4SJeff Garzik and this comes from the current configuration flags */ 474374abf2cSBartlomiej Zolnierkiewicz if (ata_id_has_dma(dev->id)) { 475a9a79dfeSJoe Perches ata_dev_info(dev, "configured for DMA\n"); 476669a5db4SJeff Garzik dev->xfer_mode = XFER_MW_DMA_0; 477669a5db4SJeff Garzik dev->xfer_shift = ATA_SHIFT_MWDMA; 478669a5db4SJeff Garzik dev->flags &= ~ATA_DFLAG_PIO; 479669a5db4SJeff Garzik } else { 480a9a79dfeSJoe Perches ata_dev_info(dev, "configured for PIO\n"); 481669a5db4SJeff Garzik dev->xfer_mode = XFER_PIO_0; 482669a5db4SJeff Garzik dev->xfer_shift = ATA_SHIFT_PIO; 483669a5db4SJeff Garzik dev->flags |= ATA_DFLAG_PIO; 484669a5db4SJeff Garzik } 485669a5db4SJeff Garzik } 486b229a7b0SAlan return 0; 487669a5db4SJeff Garzik } 488669a5db4SJeff Garzik 489669a5db4SJeff Garzik /** 490669a5db4SJeff Garzik * it821x_dev_config - Called each device identify 491669a5db4SJeff Garzik * @adev: Device that has just been identified 492669a5db4SJeff Garzik * 493669a5db4SJeff Garzik * Perform the initial setup needed for each device that is chip 494669a5db4SJeff Garzik * special. In our case we need to lock the sector count to avoid 495669a5db4SJeff Garzik * blowing the brains out of the firmware with large LBA48 requests 496669a5db4SJeff Garzik * 497669a5db4SJeff Garzik */ 498669a5db4SJeff Garzik 499cd0d3bbcSAlan static void it821x_dev_config(struct ata_device *adev) 500669a5db4SJeff Garzik { 5018bfa79fcSTejun Heo unsigned char model_num[ATA_ID_PROD_LEN + 1]; 502669a5db4SJeff Garzik 5038bfa79fcSTejun Heo ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num)); 504669a5db4SJeff Garzik 505669a5db4SJeff Garzik if (adev->max_sectors > 255) 506669a5db4SJeff Garzik adev->max_sectors = 255; 507669a5db4SJeff Garzik 508669a5db4SJeff Garzik if (strstr(model_num, "Integrated Technology Express")) { 509669a5db4SJeff Garzik /* RAID mode */ 510a9a79dfeSJoe Perches ata_dev_info(adev, "%sRAID%d volume", 511669a5db4SJeff Garzik adev->id[147] ? "Bootable " : "", 512669a5db4SJeff Garzik adev->id[129]); 513669a5db4SJeff Garzik if (adev->id[129] != 1) 514a9a79dfeSJoe Perches pr_cont("(%dK stripe)", adev->id[146]); 515a9a79dfeSJoe Perches pr_cont("\n"); 516669a5db4SJeff Garzik } 517c5038fc0SAlan Cox /* This is a controller firmware triggered funny, don't 518c5038fc0SAlan Cox report the drive faulty! */ 519c5038fc0SAlan Cox adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC; 520963e4975SAlan Cox /* No HPA in 'smart' mode */ 521963e4975SAlan Cox adev->horkage |= ATA_HORKAGE_BROKEN_HPA; 522c5038fc0SAlan Cox } 523c5038fc0SAlan Cox 524c5038fc0SAlan Cox /** 525963e4975SAlan Cox * it821x_read_id - Hack identify data up 526963e4975SAlan Cox * @adev: device to read 527963e4975SAlan Cox * @tf: proposed taskfile 528963e4975SAlan Cox * @id: buffer for returned ident data 529c5038fc0SAlan Cox * 530963e4975SAlan Cox * Query the devices on this firmware driven port and slightly 531c5038fc0SAlan Cox * mash the identify data to stop us and common tools trying to 532c5038fc0SAlan Cox * use features not firmware supported. The firmware itself does 533c5038fc0SAlan Cox * some masking (eg SMART) but not enough. 534c5038fc0SAlan Cox */ 535c5038fc0SAlan Cox 536963e4975SAlan Cox static unsigned int it821x_read_id(struct ata_device *adev, 537963e4975SAlan Cox struct ata_taskfile *tf, u16 *id) 538c5038fc0SAlan Cox { 539963e4975SAlan Cox unsigned int err_mask; 540963e4975SAlan Cox unsigned char model_num[ATA_ID_PROD_LEN + 1]; 541669a5db4SJeff Garzik 542963e4975SAlan Cox err_mask = ata_do_dev_read_id(adev, tf, id); 543963e4975SAlan Cox if (err_mask) 544963e4975SAlan Cox return err_mask; 545963e4975SAlan Cox ata_id_c_string(id, model_num, ATA_ID_PROD, sizeof(model_num)); 546963e4975SAlan Cox 547963e4975SAlan Cox id[83] &= ~(1 << 12); /* Cache flush is firmware handled */ 548963e4975SAlan Cox id[83] &= ~(1 << 13); /* Ditto for LBA48 flushes */ 549963e4975SAlan Cox id[84] &= ~(1 << 6); /* No FUA */ 550963e4975SAlan Cox id[85] &= ~(1 << 10); /* No HPA */ 551963e4975SAlan Cox id[76] = 0; /* No NCQ/AN etc */ 552963e4975SAlan Cox 553963e4975SAlan Cox if (strstr(model_num, "Integrated Technology Express")) { 554963e4975SAlan Cox /* Set feature bits the firmware neglects */ 555963e4975SAlan Cox id[49] |= 0x0300; /* LBA, DMA */ 556963e4975SAlan Cox id[83] &= 0x7FFF; 557054e5f61SOndrej Zary id[83] |= 0x4400; /* Word 83 is valid and LBA48 */ 558963e4975SAlan Cox id[86] |= 0x0400; /* LBA48 on */ 559963e4975SAlan Cox id[ATA_ID_MAJOR_VER] |= 0x1F; 5607ba07d16SOndrej Zary /* Clear the serial number because it's different each boot 5617ba07d16SOndrej Zary which breaks validation on resume */ 5627ba07d16SOndrej Zary memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN); 563963e4975SAlan Cox } 564963e4975SAlan Cox return err_mask; 565963e4975SAlan Cox } 566669a5db4SJeff Garzik 567669a5db4SJeff Garzik /** 568669a5db4SJeff Garzik * it821x_check_atapi_dma - ATAPI DMA handler 569669a5db4SJeff Garzik * @qc: Command we are about to issue 570669a5db4SJeff Garzik * 571669a5db4SJeff Garzik * Decide if this ATAPI command can be issued by DMA on this 572669a5db4SJeff Garzik * controller. Return 0 if it can be. 573669a5db4SJeff Garzik */ 574669a5db4SJeff Garzik 575669a5db4SJeff Garzik static int it821x_check_atapi_dma(struct ata_queued_cmd *qc) 576669a5db4SJeff Garzik { 577669a5db4SJeff Garzik struct ata_port *ap = qc->ap; 578669a5db4SJeff Garzik struct it821x_dev *itdev = ap->private_data; 579669a5db4SJeff Garzik 580bce7d5e0SJeff Norden /* Only use dma for transfers to/from the media. */ 581b63b1331STejun Heo if (ata_qc_raw_nbytes(qc) < 2048) 582bce7d5e0SJeff Norden return -EOPNOTSUPP; 583bce7d5e0SJeff Norden 584669a5db4SJeff Garzik /* No ATAPI DMA in smart mode */ 585669a5db4SJeff Garzik if (itdev->smart) 586669a5db4SJeff Garzik return -EOPNOTSUPP; 587669a5db4SJeff Garzik /* No ATAPI DMA on rev 10 */ 588669a5db4SJeff Garzik if (itdev->timing10) 589669a5db4SJeff Garzik return -EOPNOTSUPP; 590669a5db4SJeff Garzik /* Cool */ 591669a5db4SJeff Garzik return 0; 592669a5db4SJeff Garzik } 593669a5db4SJeff Garzik 594963e4975SAlan Cox /** 595963e4975SAlan Cox * it821x_display_disk - display disk setup 596963e4975SAlan Cox * @n: Device number 597963e4975SAlan Cox * @buf: Buffer block from firmware 598963e4975SAlan Cox * 599963e4975SAlan Cox * Produce a nice informative display of the device setup as provided 600963e4975SAlan Cox * by the firmware. 601963e4975SAlan Cox */ 602963e4975SAlan Cox 603963e4975SAlan Cox static void it821x_display_disk(int n, u8 *buf) 604963e4975SAlan Cox { 605963e4975SAlan Cox unsigned char id[41]; 606963e4975SAlan Cox int mode = 0; 6073a53b3bcSLABBE Corentin const char *mtype = ""; 608963e4975SAlan Cox char mbuf[8]; 6093a53b3bcSLABBE Corentin const char *cbl = "(40 wire cable)"; 610963e4975SAlan Cox 611963e4975SAlan Cox static const char *types[5] = { 6121c30c027SJean Delvare "RAID0", "RAID1", "RAID 0+1", "JBOD", "DISK" 613963e4975SAlan Cox }; 614963e4975SAlan Cox 615963e4975SAlan Cox if (buf[52] > 4) /* No Disk */ 616963e4975SAlan Cox return; 617963e4975SAlan Cox 618963e4975SAlan Cox ata_id_c_string((u16 *)buf, id, 0, 41); 619963e4975SAlan Cox 620963e4975SAlan Cox if (buf[51]) { 621963e4975SAlan Cox mode = ffs(buf[51]); 622963e4975SAlan Cox mtype = "UDMA"; 623963e4975SAlan Cox } else if (buf[49]) { 624963e4975SAlan Cox mode = ffs(buf[49]); 625963e4975SAlan Cox mtype = "MWDMA"; 626963e4975SAlan Cox } 627963e4975SAlan Cox 628963e4975SAlan Cox if (buf[76]) 629963e4975SAlan Cox cbl = ""; 630963e4975SAlan Cox 631963e4975SAlan Cox if (mode) 632963e4975SAlan Cox snprintf(mbuf, 8, "%5s%d", mtype, mode - 1); 633963e4975SAlan Cox else 634963e4975SAlan Cox strcpy(mbuf, "PIO"); 635963e4975SAlan Cox if (buf[52] == 4) 636963e4975SAlan Cox printk(KERN_INFO "%d: %-6s %-8s %s %s\n", 637963e4975SAlan Cox n, mbuf, types[buf[52]], id, cbl); 638963e4975SAlan Cox else 639963e4975SAlan Cox printk(KERN_INFO "%d: %-6s %-8s Volume: %1d %s %s\n", 640963e4975SAlan Cox n, mbuf, types[buf[52]], buf[53], id, cbl); 641963e4975SAlan Cox if (buf[125] < 100) 642963e4975SAlan Cox printk(KERN_INFO "%d: Rebuilding: %d%%\n", n, buf[125]); 643963e4975SAlan Cox } 644963e4975SAlan Cox 645963e4975SAlan Cox /** 646963e4975SAlan Cox * it821x_firmware_command - issue firmware command 647963e4975SAlan Cox * @ap: IT821x port to interrogate 648963e4975SAlan Cox * @cmd: command 649963e4975SAlan Cox * @len: length 650963e4975SAlan Cox * 651963e4975SAlan Cox * Issue firmware commands expecting data back from the controller. We 652963e4975SAlan Cox * use this to issue commands that do not go via the normal paths. Other 653963e4975SAlan Cox * commands such as 0xFC can be issued normally. 654963e4975SAlan Cox */ 655963e4975SAlan Cox 656963e4975SAlan Cox static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len) 657963e4975SAlan Cox { 658963e4975SAlan Cox u8 status; 659963e4975SAlan Cox int n = 0; 660963e4975SAlan Cox u16 *buf = kmalloc(len, GFP_KERNEL); 661963e4975SAlan Cox if (buf == NULL) { 662963e4975SAlan Cox printk(KERN_ERR "it821x_firmware_command: Out of memory\n"); 663963e4975SAlan Cox return NULL; 664963e4975SAlan Cox } 665963e4975SAlan Cox /* This isn't quite a normal ATA command as we are talking to the 666963e4975SAlan Cox firmware not the drives */ 667963e4975SAlan Cox ap->ctl |= ATA_NIEN; 668963e4975SAlan Cox iowrite8(ap->ctl, ap->ioaddr.ctl_addr); 669963e4975SAlan Cox ata_wait_idle(ap); 670963e4975SAlan Cox iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr); 671963e4975SAlan Cox iowrite8(cmd, ap->ioaddr.command_addr); 672963e4975SAlan Cox udelay(1); 673963e4975SAlan Cox /* This should be almost immediate but a little paranoia goes a long 674963e4975SAlan Cox way. */ 675963e4975SAlan Cox while(n++ < 10) { 676963e4975SAlan Cox status = ioread8(ap->ioaddr.status_addr); 677963e4975SAlan Cox if (status & ATA_ERR) { 678963e4975SAlan Cox kfree(buf); 679963e4975SAlan Cox printk(KERN_ERR "it821x_firmware_command: rejected\n"); 680963e4975SAlan Cox return NULL; 681963e4975SAlan Cox } 682963e4975SAlan Cox if (status & ATA_DRQ) { 683963e4975SAlan Cox ioread16_rep(ap->ioaddr.data_addr, buf, len/2); 684963e4975SAlan Cox return (u8 *)buf; 685963e4975SAlan Cox } 686*eada8598SJia-Ju Bai usleep_range(500, 1000); 687963e4975SAlan Cox } 688963e4975SAlan Cox kfree(buf); 689963e4975SAlan Cox printk(KERN_ERR "it821x_firmware_command: timeout\n"); 690963e4975SAlan Cox return NULL; 691963e4975SAlan Cox } 692963e4975SAlan Cox 693963e4975SAlan Cox /** 694963e4975SAlan Cox * it821x_probe_firmware - firmware reporting/setup 695963e4975SAlan Cox * @ap: IT821x port being probed 696963e4975SAlan Cox * 697963e4975SAlan Cox * Probe the firmware of the controller by issuing firmware command 698963e4975SAlan Cox * 0xFA and analysing the returned data. 699963e4975SAlan Cox */ 700963e4975SAlan Cox 701963e4975SAlan Cox static void it821x_probe_firmware(struct ata_port *ap) 702963e4975SAlan Cox { 703963e4975SAlan Cox u8 *buf; 704963e4975SAlan Cox int i; 705963e4975SAlan Cox 706963e4975SAlan Cox /* This is a bit ugly as we can't just issue a task file to a device 707963e4975SAlan Cox as this is controller magic */ 708963e4975SAlan Cox 709963e4975SAlan Cox buf = it821x_firmware_command(ap, 0xFA, 512); 710963e4975SAlan Cox 711963e4975SAlan Cox if (buf != NULL) { 712963e4975SAlan Cox printk(KERN_INFO "pata_it821x: Firmware %02X/%02X/%02X%02X\n", 713963e4975SAlan Cox buf[505], 714963e4975SAlan Cox buf[506], 715963e4975SAlan Cox buf[507], 716963e4975SAlan Cox buf[508]); 717963e4975SAlan Cox for (i = 0; i < 4; i++) 718963e4975SAlan Cox it821x_display_disk(i, buf + 128 * i); 719963e4975SAlan Cox kfree(buf); 720963e4975SAlan Cox } 721963e4975SAlan Cox } 722963e4975SAlan Cox 723963e4975SAlan Cox 724669a5db4SJeff Garzik 725669a5db4SJeff Garzik /** 726669a5db4SJeff Garzik * it821x_port_start - port setup 727669a5db4SJeff Garzik * @ap: ATA port being set up 728669a5db4SJeff Garzik * 729669a5db4SJeff Garzik * The it821x needs to maintain private data structures and also to 730669a5db4SJeff Garzik * use the standard PCI interface which lacks support for this 731669a5db4SJeff Garzik * functionality. We instead set up the private data on the port 732669a5db4SJeff Garzik * start hook, and tear it down on port stop 733669a5db4SJeff Garzik */ 734669a5db4SJeff Garzik 735669a5db4SJeff Garzik static int it821x_port_start(struct ata_port *ap) 736669a5db4SJeff Garzik { 737669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev); 738669a5db4SJeff Garzik struct it821x_dev *itdev; 739669a5db4SJeff Garzik u8 conf; 740669a5db4SJeff Garzik 741c7087652STejun Heo int ret = ata_bmdma_port_start(ap); 742669a5db4SJeff Garzik if (ret < 0) 743669a5db4SJeff Garzik return ret; 744669a5db4SJeff Garzik 74524dc5f33STejun Heo itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL); 74624dc5f33STejun Heo if (itdev == NULL) 747669a5db4SJeff Garzik return -ENOMEM; 74824dc5f33STejun Heo ap->private_data = itdev; 749669a5db4SJeff Garzik 750669a5db4SJeff Garzik pci_read_config_byte(pdev, 0x50, &conf); 751669a5db4SJeff Garzik 752669a5db4SJeff Garzik if (conf & 1) { 753669a5db4SJeff Garzik itdev->smart = 1; 754669a5db4SJeff Garzik /* Long I/O's although allowed in LBA48 space cause the 755669a5db4SJeff Garzik onboard firmware to enter the twighlight zone */ 756669a5db4SJeff Garzik /* No ATAPI DMA in this mode either */ 757963e4975SAlan Cox if (ap->port_no == 0) 758963e4975SAlan Cox it821x_probe_firmware(ap); 759669a5db4SJeff Garzik } 760669a5db4SJeff Garzik /* Pull the current clocks from 0x50 */ 761669a5db4SJeff Garzik if (conf & (1 << (1 + ap->port_no))) 762669a5db4SJeff Garzik itdev->clock_mode = ATA_50; 763669a5db4SJeff Garzik else 764669a5db4SJeff Garzik itdev->clock_mode = ATA_66; 765669a5db4SJeff Garzik 766669a5db4SJeff Garzik itdev->want[0][1] = ATA_ANY; 767669a5db4SJeff Garzik itdev->want[1][1] = ATA_ANY; 768669a5db4SJeff Garzik itdev->last_device = -1; 769669a5db4SJeff Garzik 770604de6e0SAlan Cox if (pdev->revision == 0x10) { 771669a5db4SJeff Garzik itdev->timing10 = 1; 772669a5db4SJeff Garzik /* Need to disable ATAPI DMA for this case */ 773669a5db4SJeff Garzik if (!itdev->smart) 774669a5db4SJeff Garzik printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n"); 775669a5db4SJeff Garzik } 776669a5db4SJeff Garzik 777669a5db4SJeff Garzik return 0; 778669a5db4SJeff Garzik } 779669a5db4SJeff Garzik 780963e4975SAlan Cox /** 781963e4975SAlan Cox * it821x_rdc_cable - Cable detect for RDC1010 782963e4975SAlan Cox * @ap: port we are checking 783963e4975SAlan Cox * 784963e4975SAlan Cox * Return the RDC1010 cable type. Unlike the IT821x we know how to do 785963e4975SAlan Cox * this and can do host side cable detect 786963e4975SAlan Cox */ 787963e4975SAlan Cox 788963e4975SAlan Cox static int it821x_rdc_cable(struct ata_port *ap) 789963e4975SAlan Cox { 790963e4975SAlan Cox u16 r40; 791963e4975SAlan Cox struct pci_dev *pdev = to_pci_dev(ap->host->dev); 792963e4975SAlan Cox 793963e4975SAlan Cox pci_read_config_word(pdev, 0x40, &r40); 794963e4975SAlan Cox if (r40 & (1 << (2 + ap->port_no))) 795963e4975SAlan Cox return ATA_CBL_PATA40; 796963e4975SAlan Cox return ATA_CBL_PATA80; 797963e4975SAlan Cox } 798963e4975SAlan Cox 799669a5db4SJeff Garzik static struct scsi_host_template it821x_sht = { 80068d1d07bSTejun Heo ATA_BMDMA_SHT(DRV_NAME), 801669a5db4SJeff Garzik }; 802669a5db4SJeff Garzik 803669a5db4SJeff Garzik static struct ata_port_operations it821x_smart_port_ops = { 804029cfd6bSTejun Heo .inherits = &ata_bmdma_port_ops, 805669a5db4SJeff Garzik 806669a5db4SJeff Garzik .check_atapi_dma= it821x_check_atapi_dma, 8079363c382STejun Heo .qc_issue = it821x_smart_qc_issue, 808bda30288SJeff Garzik 809963e4975SAlan Cox .cable_detect = ata_cable_80wire, 810029cfd6bSTejun Heo .set_mode = it821x_smart_set_mode, 811029cfd6bSTejun Heo .dev_config = it821x_dev_config, 812963e4975SAlan Cox .read_id = it821x_read_id, 813669a5db4SJeff Garzik 814669a5db4SJeff Garzik .port_start = it821x_port_start, 815669a5db4SJeff Garzik }; 816669a5db4SJeff Garzik 817669a5db4SJeff Garzik static struct ata_port_operations it821x_passthru_port_ops = { 818029cfd6bSTejun Heo .inherits = &ata_bmdma_port_ops, 819669a5db4SJeff Garzik 820669a5db4SJeff Garzik .check_atapi_dma= it821x_check_atapi_dma, 8215682ed33STejun Heo .sff_dev_select = it821x_passthru_dev_select, 822669a5db4SJeff Garzik .bmdma_start = it821x_passthru_bmdma_start, 823669a5db4SJeff Garzik .bmdma_stop = it821x_passthru_bmdma_stop, 8249363c382STejun Heo .qc_issue = it821x_passthru_qc_issue, 825bda30288SJeff Garzik 826029cfd6bSTejun Heo .cable_detect = ata_cable_unknown, 827029cfd6bSTejun Heo .set_piomode = it821x_passthru_set_piomode, 828029cfd6bSTejun Heo .set_dmamode = it821x_passthru_set_dmamode, 829669a5db4SJeff Garzik 830669a5db4SJeff Garzik .port_start = it821x_port_start, 831669a5db4SJeff Garzik }; 832669a5db4SJeff Garzik 833963e4975SAlan Cox static struct ata_port_operations it821x_rdc_port_ops = { 834963e4975SAlan Cox .inherits = &ata_bmdma_port_ops, 835963e4975SAlan Cox 836963e4975SAlan Cox .check_atapi_dma= it821x_check_atapi_dma, 837963e4975SAlan Cox .sff_dev_select = it821x_passthru_dev_select, 838963e4975SAlan Cox .bmdma_start = it821x_passthru_bmdma_start, 839963e4975SAlan Cox .bmdma_stop = it821x_passthru_bmdma_stop, 840963e4975SAlan Cox .qc_issue = it821x_passthru_qc_issue, 841963e4975SAlan Cox 842963e4975SAlan Cox .cable_detect = it821x_rdc_cable, 843963e4975SAlan Cox .set_piomode = it821x_passthru_set_piomode, 844963e4975SAlan Cox .set_dmamode = it821x_passthru_set_dmamode, 845963e4975SAlan Cox 846963e4975SAlan Cox .port_start = it821x_port_start, 847963e4975SAlan Cox }; 848963e4975SAlan Cox 849112cc2b5SRandy Dunlap static void it821x_disable_raid(struct pci_dev *pdev) 850669a5db4SJeff Garzik { 851963e4975SAlan Cox /* Neither the RDC nor the IT8211 */ 852963e4975SAlan Cox if (pdev->vendor != PCI_VENDOR_ID_ITE || 853963e4975SAlan Cox pdev->device != PCI_DEVICE_ID_ITE_8212) 854963e4975SAlan Cox return; 855963e4975SAlan Cox 856669a5db4SJeff Garzik /* Reset local CPU, and set BIOS not ready */ 857669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x5E, 0x01); 858669a5db4SJeff Garzik 859669a5db4SJeff Garzik /* Set to bypass mode, and reset PCI bus */ 860669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x50, 0x00); 861669a5db4SJeff Garzik pci_write_config_word(pdev, PCI_COMMAND, 862669a5db4SJeff Garzik PCI_COMMAND_PARITY | PCI_COMMAND_IO | 863669a5db4SJeff Garzik PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); 864669a5db4SJeff Garzik pci_write_config_word(pdev, 0x40, 0xA0F3); 865669a5db4SJeff Garzik 866669a5db4SJeff Garzik pci_write_config_dword(pdev,0x4C, 0x02040204); 867669a5db4SJeff Garzik pci_write_config_byte(pdev, 0x42, 0x36); 868669a5db4SJeff Garzik pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20); 869669a5db4SJeff Garzik } 870669a5db4SJeff Garzik 871669a5db4SJeff Garzik 872669a5db4SJeff Garzik static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id) 873669a5db4SJeff Garzik { 874669a5db4SJeff Garzik u8 conf; 875669a5db4SJeff Garzik 8761626aeb8STejun Heo static const struct ata_port_info info_smart = { 8771d2808fdSJeff Garzik .flags = ATA_FLAG_SLAVE_POSS, 87814bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 87914bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 880963e4975SAlan Cox .udma_mask = ATA_UDMA6, 881669a5db4SJeff Garzik .port_ops = &it821x_smart_port_ops 882669a5db4SJeff Garzik }; 8831626aeb8STejun Heo static const struct ata_port_info info_passthru = { 8841d2808fdSJeff Garzik .flags = ATA_FLAG_SLAVE_POSS, 88514bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 88614bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 887bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 888669a5db4SJeff Garzik .port_ops = &it821x_passthru_port_ops 889669a5db4SJeff Garzik }; 890963e4975SAlan Cox static const struct ata_port_info info_rdc = { 891963e4975SAlan Cox .flags = ATA_FLAG_SLAVE_POSS, 89214bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 89314bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 8944a99d95fSAlan Cox .udma_mask = ATA_UDMA6, 8954a99d95fSAlan Cox .port_ops = &it821x_rdc_port_ops 8964a99d95fSAlan Cox }; 8974a99d95fSAlan Cox static const struct ata_port_info info_rdc_11 = { 8984a99d95fSAlan Cox .flags = ATA_FLAG_SLAVE_POSS, 89914bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 90014bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 901963e4975SAlan Cox /* No UDMA */ 902963e4975SAlan Cox .port_ops = &it821x_rdc_port_ops 903963e4975SAlan Cox }; 904669a5db4SJeff Garzik 9051626aeb8STejun Heo const struct ata_port_info *ppi[] = { NULL, NULL }; 9063a53b3bcSLABBE Corentin static const char *mode[2] = { "pass through", "smart" }; 907f08048e9STejun Heo int rc; 908f08048e9STejun Heo 909f08048e9STejun Heo rc = pcim_enable_device(pdev); 910f08048e9STejun Heo if (rc) 911f08048e9STejun Heo return rc; 912669a5db4SJeff Garzik 913963e4975SAlan Cox if (pdev->vendor == PCI_VENDOR_ID_RDC) { 9144a99d95fSAlan Cox /* Deal with Vortex86SX */ 9154a99d95fSAlan Cox if (pdev->revision == 0x11) 9164a99d95fSAlan Cox ppi[0] = &info_rdc_11; 9174a99d95fSAlan Cox else 918963e4975SAlan Cox ppi[0] = &info_rdc; 919963e4975SAlan Cox } else { 920669a5db4SJeff Garzik /* Force the card into bypass mode if so requested */ 921669a5db4SJeff Garzik if (it8212_noraid) { 922669a5db4SJeff Garzik printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n"); 923669a5db4SJeff Garzik it821x_disable_raid(pdev); 924669a5db4SJeff Garzik } 925669a5db4SJeff Garzik pci_read_config_byte(pdev, 0x50, &conf); 926669a5db4SJeff Garzik conf &= 1; 927669a5db4SJeff Garzik 928963e4975SAlan Cox printk(KERN_INFO DRV_NAME": controller in %s mode.\n", 929963e4975SAlan Cox mode[conf]); 930669a5db4SJeff Garzik if (conf == 0) 9311626aeb8STejun Heo ppi[0] = &info_passthru; 932669a5db4SJeff Garzik else 9331626aeb8STejun Heo ppi[0] = &info_smart; 934963e4975SAlan Cox } 9351c5afdf7STejun Heo return ata_pci_bmdma_init_one(pdev, ppi, &it821x_sht, NULL, 0); 936669a5db4SJeff Garzik } 937669a5db4SJeff Garzik 93858eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP 939f535d53fSAlan static int it821x_reinit_one(struct pci_dev *pdev) 940f535d53fSAlan { 9410a86e1c8SJingoo Han struct ata_host *host = pci_get_drvdata(pdev); 942f08048e9STejun Heo int rc; 943f08048e9STejun Heo 944f08048e9STejun Heo rc = ata_pci_device_do_resume(pdev); 945f08048e9STejun Heo if (rc) 946f08048e9STejun Heo return rc; 947f535d53fSAlan /* Resume - turn raid back off if need be */ 948f535d53fSAlan if (it8212_noraid) 949f535d53fSAlan it821x_disable_raid(pdev); 950f08048e9STejun Heo ata_host_resume(host); 951f08048e9STejun Heo return rc; 952f535d53fSAlan } 953438ac6d5STejun Heo #endif 954f535d53fSAlan 9552d2744fcSJeff Garzik static const struct pci_device_id it821x[] = { 9562d2744fcSJeff Garzik { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), }, 9572d2744fcSJeff Garzik { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), }, 9584192be64SOtavio Salvador { PCI_VDEVICE(RDC, PCI_DEVICE_ID_RDC_D1010), }, 9592d2744fcSJeff Garzik 9602d2744fcSJeff Garzik { }, 961669a5db4SJeff Garzik }; 962669a5db4SJeff Garzik 963669a5db4SJeff Garzik static struct pci_driver it821x_pci_driver = { 964669a5db4SJeff Garzik .name = DRV_NAME, 965669a5db4SJeff Garzik .id_table = it821x, 966669a5db4SJeff Garzik .probe = it821x_init_one, 967f535d53fSAlan .remove = ata_pci_remove_one, 96858eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP 969f535d53fSAlan .suspend = ata_pci_device_suspend, 970f535d53fSAlan .resume = it821x_reinit_one, 971438ac6d5STejun Heo #endif 972669a5db4SJeff Garzik }; 973669a5db4SJeff Garzik 9742fc75da0SAxel Lin module_pci_driver(it821x_pci_driver); 975669a5db4SJeff Garzik 976669a5db4SJeff Garzik MODULE_AUTHOR("Alan Cox"); 977669a5db4SJeff Garzik MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller"); 978669a5db4SJeff Garzik MODULE_LICENSE("GPL"); 979669a5db4SJeff Garzik MODULE_DEVICE_TABLE(pci, it821x); 980669a5db4SJeff Garzik MODULE_VERSION(DRV_VERSION); 981669a5db4SJeff Garzik 982669a5db4SJeff Garzik module_param_named(noraid, it8212_noraid, int, S_IRUGO); 9835fe675e2SStas Sergeev MODULE_PARM_DESC(noraid, "Force card into bypass mode"); 984