1c6fd2807SJeff Garzik /* 2c6fd2807SJeff Garzik * sata_via.c - VIA Serial ATA controllers 3c6fd2807SJeff Garzik * 4c6fd2807SJeff Garzik * Maintained by: Jeff Garzik <jgarzik@pobox.com> 5c6fd2807SJeff Garzik * Please ALWAYS copy linux-ide@vger.kernel.org 65796d1c4SJeff Garzik * on emails. 7c6fd2807SJeff Garzik * 8c6fd2807SJeff Garzik * Copyright 2003-2004 Red Hat, Inc. All rights reserved. 9c6fd2807SJeff Garzik * Copyright 2003-2004 Jeff Garzik 10c6fd2807SJeff Garzik * 11c6fd2807SJeff Garzik * 12c6fd2807SJeff Garzik * This program is free software; you can redistribute it and/or modify 13c6fd2807SJeff Garzik * it under the terms of the GNU General Public License as published by 14c6fd2807SJeff Garzik * the Free Software Foundation; either version 2, or (at your option) 15c6fd2807SJeff Garzik * any later version. 16c6fd2807SJeff Garzik * 17c6fd2807SJeff Garzik * This program is distributed in the hope that it will be useful, 18c6fd2807SJeff Garzik * but WITHOUT ANY WARRANTY; without even the implied warranty of 19c6fd2807SJeff Garzik * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20c6fd2807SJeff Garzik * GNU General Public License for more details. 21c6fd2807SJeff Garzik * 22c6fd2807SJeff Garzik * You should have received a copy of the GNU General Public License 23c6fd2807SJeff Garzik * along with this program; see the file COPYING. If not, write to 24c6fd2807SJeff Garzik * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 25c6fd2807SJeff Garzik * 26c6fd2807SJeff Garzik * 27c6fd2807SJeff Garzik * libata documentation is available via 'make {ps|pdf}docs', 28c6fd2807SJeff Garzik * as Documentation/DocBook/libata.* 29c6fd2807SJeff Garzik * 30c6fd2807SJeff Garzik * Hardware documentation available under NDA. 31c6fd2807SJeff Garzik * 32c6fd2807SJeff Garzik * 33c6fd2807SJeff Garzik * 34c6fd2807SJeff Garzik */ 35c6fd2807SJeff Garzik 36c6fd2807SJeff Garzik #include <linux/kernel.h> 37c6fd2807SJeff Garzik #include <linux/module.h> 38c6fd2807SJeff Garzik #include <linux/pci.h> 39c6fd2807SJeff Garzik #include <linux/init.h> 40c6fd2807SJeff Garzik #include <linux/blkdev.h> 41c6fd2807SJeff Garzik #include <linux/delay.h> 42c6fd2807SJeff Garzik #include <linux/device.h> 43a55ab496SBart Hartgers #include <scsi/scsi.h> 44a55ab496SBart Hartgers #include <scsi/scsi_cmnd.h> 45c6fd2807SJeff Garzik #include <scsi/scsi_host.h> 46c6fd2807SJeff Garzik #include <linux/libata.h> 47c6fd2807SJeff Garzik 48c6fd2807SJeff Garzik #define DRV_NAME "sata_via" 49a55ab496SBart Hartgers #define DRV_VERSION "2.6" 50c6fd2807SJeff Garzik 51b9d5b89bSTejun Heo /* 52b9d5b89bSTejun Heo * vt8251 is different from other sata controllers of VIA. It has two 53b9d5b89bSTejun Heo * channels, each channel has both Master and Slave slot. 54b9d5b89bSTejun Heo */ 55c6fd2807SJeff Garzik enum board_ids_enum { 56c6fd2807SJeff Garzik vt6420, 57c6fd2807SJeff Garzik vt6421, 58b9d5b89bSTejun Heo vt8251, 59c6fd2807SJeff Garzik }; 60c6fd2807SJeff Garzik 61c6fd2807SJeff Garzik enum { 62c6fd2807SJeff Garzik SATA_CHAN_ENAB = 0x40, /* SATA channel enable */ 63c6fd2807SJeff Garzik SATA_INT_GATE = 0x41, /* SATA interrupt gating */ 64c6fd2807SJeff Garzik SATA_NATIVE_MODE = 0x42, /* Native mode enable */ 65d73f30e1SAlan PATA_UDMA_TIMING = 0xB3, /* PATA timing for DMA/ cable detect */ 66d73f30e1SAlan PATA_PIO_TIMING = 0xAB, /* PATA timing register */ 67c6fd2807SJeff Garzik 68c6fd2807SJeff Garzik PORT0 = (1 << 1), 69c6fd2807SJeff Garzik PORT1 = (1 << 0), 70c6fd2807SJeff Garzik ALL_PORTS = PORT0 | PORT1, 71c6fd2807SJeff Garzik 72c6fd2807SJeff Garzik NATIVE_MODE_ALL = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4), 73c6fd2807SJeff Garzik 74c6fd2807SJeff Garzik SATA_EXT_PHY = (1 << 6), /* 0==use PATA, 1==ext phy */ 75c6fd2807SJeff Garzik }; 76c6fd2807SJeff Garzik 77c6fd2807SJeff Garzik static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent); 7882ef04fbSTejun Heo static int svia_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val); 7982ef04fbSTejun Heo static int svia_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val); 80b9d5b89bSTejun Heo static int vt8251_scr_read(struct ata_link *link, unsigned int scr, u32 *val); 81b9d5b89bSTejun Heo static int vt8251_scr_write(struct ata_link *link, unsigned int scr, u32 val); 82b78152e9STejun Heo static void svia_tf_load(struct ata_port *ap, const struct ata_taskfile *tf); 8317234246STejun Heo static void svia_noop_freeze(struct ata_port *ap); 84a1efdabaSTejun Heo static int vt6420_prereset(struct ata_link *link, unsigned long deadline); 85a55ab496SBart Hartgers static void vt6420_bmdma_start(struct ata_queued_cmd *qc); 86a0fcdc02SJeff Garzik static int vt6421_pata_cable_detect(struct ata_port *ap); 87d73f30e1SAlan static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev); 88d73f30e1SAlan static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev); 89c6fd2807SJeff Garzik 90c6fd2807SJeff Garzik static const struct pci_device_id svia_pci_tbl[] = { 9196bc103fSLuca Pedrielli { PCI_VDEVICE(VIA, 0x5337), vt6420 }, 92b9d5b89bSTejun Heo { PCI_VDEVICE(VIA, 0x0591), vt6420 }, /* 2 sata chnls (Master) */ 93b9d5b89bSTejun Heo { PCI_VDEVICE(VIA, 0x3149), vt6420 }, /* 2 sata chnls (Master) */ 94b9d5b89bSTejun Heo { PCI_VDEVICE(VIA, 0x3249), vt6421 }, /* 2 sata chnls, 1 pata chnl */ 9552df0ee0SJeff Garzik { PCI_VDEVICE(VIA, 0x5372), vt6420 }, 9652df0ee0SJeff Garzik { PCI_VDEVICE(VIA, 0x7372), vt6420 }, 97b9d5b89bSTejun Heo { PCI_VDEVICE(VIA, 0x5287), vt8251 }, /* 2 sata chnls (Master/Slave) */ 9868139520SJosephChan@via.com.tw { PCI_VDEVICE(VIA, 0x9000), vt8251 }, 99c6fd2807SJeff Garzik 100c6fd2807SJeff Garzik { } /* terminate list */ 101c6fd2807SJeff Garzik }; 102c6fd2807SJeff Garzik 103c6fd2807SJeff Garzik static struct pci_driver svia_pci_driver = { 104c6fd2807SJeff Garzik .name = DRV_NAME, 105c6fd2807SJeff Garzik .id_table = svia_pci_tbl, 106c6fd2807SJeff Garzik .probe = svia_init_one, 107e1e143cfSTejun Heo #ifdef CONFIG_PM 108e1e143cfSTejun Heo .suspend = ata_pci_device_suspend, 109e1e143cfSTejun Heo .resume = ata_pci_device_resume, 110e1e143cfSTejun Heo #endif 111c6fd2807SJeff Garzik .remove = ata_pci_remove_one, 112c6fd2807SJeff Garzik }; 113c6fd2807SJeff Garzik 114c6fd2807SJeff Garzik static struct scsi_host_template svia_sht = { 11568d1d07bSTejun Heo ATA_BMDMA_SHT(DRV_NAME), 116c6fd2807SJeff Garzik }; 117c6fd2807SJeff Garzik 118b78152e9STejun Heo static struct ata_port_operations svia_base_ops = { 119029cfd6bSTejun Heo .inherits = &ata_bmdma_port_ops, 120b78152e9STejun Heo .sff_tf_load = svia_tf_load, 121b78152e9STejun Heo }; 122b78152e9STejun Heo 123b78152e9STejun Heo static struct ata_port_operations vt6420_sata_ops = { 124b78152e9STejun Heo .inherits = &svia_base_ops, 12517234246STejun Heo .freeze = svia_noop_freeze, 126a1efdabaSTejun Heo .prereset = vt6420_prereset, 127a55ab496SBart Hartgers .bmdma_start = vt6420_bmdma_start, 12854a86bfcSJeff Garzik }; 12954a86bfcSJeff Garzik 130029cfd6bSTejun Heo static struct ata_port_operations vt6421_pata_ops = { 131b78152e9STejun Heo .inherits = &svia_base_ops, 132029cfd6bSTejun Heo .cable_detect = vt6421_pata_cable_detect, 133d73f30e1SAlan .set_piomode = vt6421_set_pio_mode, 134d73f30e1SAlan .set_dmamode = vt6421_set_dma_mode, 135d73f30e1SAlan }; 136d73f30e1SAlan 137029cfd6bSTejun Heo static struct ata_port_operations vt6421_sata_ops = { 138b78152e9STejun Heo .inherits = &svia_base_ops, 139c6fd2807SJeff Garzik .scr_read = svia_scr_read, 140c6fd2807SJeff Garzik .scr_write = svia_scr_write, 141c6fd2807SJeff Garzik }; 142c6fd2807SJeff Garzik 143b9d5b89bSTejun Heo static struct ata_port_operations vt8251_ops = { 144b9d5b89bSTejun Heo .inherits = &svia_base_ops, 145b9d5b89bSTejun Heo .hardreset = sata_std_hardreset, 146b9d5b89bSTejun Heo .scr_read = vt8251_scr_read, 147b9d5b89bSTejun Heo .scr_write = vt8251_scr_write, 148b9d5b89bSTejun Heo }; 149b9d5b89bSTejun Heo 150eca25dcaSTejun Heo static const struct ata_port_info vt6420_port_info = { 151cca3974eSJeff Garzik .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY, 15214bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 15314bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 154bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 15554a86bfcSJeff Garzik .port_ops = &vt6420_sata_ops, 156c6fd2807SJeff Garzik }; 157c6fd2807SJeff Garzik 158eca25dcaSTejun Heo static struct ata_port_info vt6421_sport_info = { 159eca25dcaSTejun Heo .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY, 16014bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 16114bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 162bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 163eca25dcaSTejun Heo .port_ops = &vt6421_sata_ops, 164eca25dcaSTejun Heo }; 165eca25dcaSTejun Heo 166eca25dcaSTejun Heo static struct ata_port_info vt6421_pport_info = { 167eca25dcaSTejun Heo .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_LEGACY, 16814bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 16914bdef98SErik Inge Bolsø /* No MWDMA */ 170bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 171eca25dcaSTejun Heo .port_ops = &vt6421_pata_ops, 172eca25dcaSTejun Heo }; 173eca25dcaSTejun Heo 174b9d5b89bSTejun Heo static struct ata_port_info vt8251_port_info = { 175b9d5b89bSTejun Heo .flags = ATA_FLAG_SATA | ATA_FLAG_SLAVE_POSS | 176b9d5b89bSTejun Heo ATA_FLAG_NO_LEGACY, 17714bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 17814bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 179b9d5b89bSTejun Heo .udma_mask = ATA_UDMA6, 180b9d5b89bSTejun Heo .port_ops = &vt8251_ops, 181b9d5b89bSTejun Heo }; 182b9d5b89bSTejun Heo 183c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik"); 184c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for VIA SATA controllers"); 185c6fd2807SJeff Garzik MODULE_LICENSE("GPL"); 186c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, svia_pci_tbl); 187c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION); 188c6fd2807SJeff Garzik 18982ef04fbSTejun Heo static int svia_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val) 190c6fd2807SJeff Garzik { 191c6fd2807SJeff Garzik if (sc_reg > SCR_CONTROL) 192da3dbb17STejun Heo return -EINVAL; 19382ef04fbSTejun Heo *val = ioread32(link->ap->ioaddr.scr_addr + (4 * sc_reg)); 194da3dbb17STejun Heo return 0; 195c6fd2807SJeff Garzik } 196c6fd2807SJeff Garzik 19782ef04fbSTejun Heo static int svia_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val) 198c6fd2807SJeff Garzik { 199c6fd2807SJeff Garzik if (sc_reg > SCR_CONTROL) 200da3dbb17STejun Heo return -EINVAL; 20182ef04fbSTejun Heo iowrite32(val, link->ap->ioaddr.scr_addr + (4 * sc_reg)); 202da3dbb17STejun Heo return 0; 203c6fd2807SJeff Garzik } 204c6fd2807SJeff Garzik 205b9d5b89bSTejun Heo static int vt8251_scr_read(struct ata_link *link, unsigned int scr, u32 *val) 206b9d5b89bSTejun Heo { 207b9d5b89bSTejun Heo static const u8 ipm_tbl[] = { 1, 2, 6, 0 }; 208b9d5b89bSTejun Heo struct pci_dev *pdev = to_pci_dev(link->ap->host->dev); 209b9d5b89bSTejun Heo int slot = 2 * link->ap->port_no + link->pmp; 210b9d5b89bSTejun Heo u32 v = 0; 211b9d5b89bSTejun Heo u8 raw; 212b9d5b89bSTejun Heo 213b9d5b89bSTejun Heo switch (scr) { 214b9d5b89bSTejun Heo case SCR_STATUS: 215b9d5b89bSTejun Heo pci_read_config_byte(pdev, 0xA0 + slot, &raw); 216b9d5b89bSTejun Heo 217b9d5b89bSTejun Heo /* read the DET field, bit0 and 1 of the config byte */ 218b9d5b89bSTejun Heo v |= raw & 0x03; 219b9d5b89bSTejun Heo 220b9d5b89bSTejun Heo /* read the SPD field, bit4 of the configure byte */ 221b9d5b89bSTejun Heo if (raw & (1 << 4)) 222b9d5b89bSTejun Heo v |= 0x02 << 4; 223b9d5b89bSTejun Heo else 224b9d5b89bSTejun Heo v |= 0x01 << 4; 225b9d5b89bSTejun Heo 226b9d5b89bSTejun Heo /* read the IPM field, bit2 and 3 of the config byte */ 227b9d5b89bSTejun Heo v |= ipm_tbl[(raw >> 2) & 0x3]; 228b9d5b89bSTejun Heo break; 229b9d5b89bSTejun Heo 230b9d5b89bSTejun Heo case SCR_ERROR: 231b9d5b89bSTejun Heo /* devices other than 5287 uses 0xA8 as base */ 232b9d5b89bSTejun Heo WARN_ON(pdev->device != 0x5287); 233b9d5b89bSTejun Heo pci_read_config_dword(pdev, 0xB0 + slot * 4, &v); 234b9d5b89bSTejun Heo break; 235b9d5b89bSTejun Heo 236b9d5b89bSTejun Heo case SCR_CONTROL: 237b9d5b89bSTejun Heo pci_read_config_byte(pdev, 0xA4 + slot, &raw); 238b9d5b89bSTejun Heo 239b9d5b89bSTejun Heo /* read the DET field, bit0 and bit1 */ 240b9d5b89bSTejun Heo v |= ((raw & 0x02) << 1) | (raw & 0x01); 241b9d5b89bSTejun Heo 242b9d5b89bSTejun Heo /* read the IPM field, bit2 and bit3 */ 243b9d5b89bSTejun Heo v |= ((raw >> 2) & 0x03) << 8; 244b9d5b89bSTejun Heo break; 245b9d5b89bSTejun Heo 246b9d5b89bSTejun Heo default: 247b9d5b89bSTejun Heo return -EINVAL; 248b9d5b89bSTejun Heo } 249b9d5b89bSTejun Heo 250b9d5b89bSTejun Heo *val = v; 251b9d5b89bSTejun Heo return 0; 252b9d5b89bSTejun Heo } 253b9d5b89bSTejun Heo 254b9d5b89bSTejun Heo static int vt8251_scr_write(struct ata_link *link, unsigned int scr, u32 val) 255b9d5b89bSTejun Heo { 256b9d5b89bSTejun Heo struct pci_dev *pdev = to_pci_dev(link->ap->host->dev); 257b9d5b89bSTejun Heo int slot = 2 * link->ap->port_no + link->pmp; 258b9d5b89bSTejun Heo u32 v = 0; 259b9d5b89bSTejun Heo 260b9d5b89bSTejun Heo switch (scr) { 261b9d5b89bSTejun Heo case SCR_ERROR: 262b9d5b89bSTejun Heo /* devices other than 5287 uses 0xA8 as base */ 263b9d5b89bSTejun Heo WARN_ON(pdev->device != 0x5287); 264b9d5b89bSTejun Heo pci_write_config_dword(pdev, 0xB0 + slot * 4, val); 265b9d5b89bSTejun Heo return 0; 266b9d5b89bSTejun Heo 267b9d5b89bSTejun Heo case SCR_CONTROL: 268b9d5b89bSTejun Heo /* set the DET field */ 269b9d5b89bSTejun Heo v |= ((val & 0x4) >> 1) | (val & 0x1); 270b9d5b89bSTejun Heo 271b9d5b89bSTejun Heo /* set the IPM field */ 272b9d5b89bSTejun Heo v |= ((val >> 8) & 0x3) << 2; 273b9d5b89bSTejun Heo 274b9d5b89bSTejun Heo pci_write_config_byte(pdev, 0xA4 + slot, v); 275b9d5b89bSTejun Heo return 0; 276b9d5b89bSTejun Heo 277b9d5b89bSTejun Heo default: 278b9d5b89bSTejun Heo return -EINVAL; 279b9d5b89bSTejun Heo } 280b9d5b89bSTejun Heo } 281b9d5b89bSTejun Heo 282b78152e9STejun Heo /** 283b78152e9STejun Heo * svia_tf_load - send taskfile registers to host controller 284b78152e9STejun Heo * @ap: Port to which output is sent 285b78152e9STejun Heo * @tf: ATA taskfile register set 286b78152e9STejun Heo * 287b78152e9STejun Heo * Outputs ATA taskfile to standard ATA host controller. 288b78152e9STejun Heo * 289b78152e9STejun Heo * This is to fix the internal bug of via chipsets, which will 290b78152e9STejun Heo * reset the device register after changing the IEN bit on ctl 291b78152e9STejun Heo * register. 292b78152e9STejun Heo */ 293b78152e9STejun Heo static void svia_tf_load(struct ata_port *ap, const struct ata_taskfile *tf) 294b78152e9STejun Heo { 295b78152e9STejun Heo struct ata_taskfile ttf; 296b78152e9STejun Heo 297b78152e9STejun Heo if (tf->ctl != ap->last_ctl) { 298b78152e9STejun Heo ttf = *tf; 299b78152e9STejun Heo ttf.flags |= ATA_TFLAG_DEVICE; 300b78152e9STejun Heo tf = &ttf; 301b78152e9STejun Heo } 302b78152e9STejun Heo ata_sff_tf_load(ap, tf); 303b78152e9STejun Heo } 304b78152e9STejun Heo 30517234246STejun Heo static void svia_noop_freeze(struct ata_port *ap) 30617234246STejun Heo { 30717234246STejun Heo /* Some VIA controllers choke if ATA_NIEN is manipulated in 30817234246STejun Heo * certain way. Leave it alone and just clear pending IRQ. 30917234246STejun Heo */ 3105682ed33STejun Heo ap->ops->sff_check_status(ap); 31137f65b8bSTejun Heo ata_bmdma_irq_clear(ap); 31217234246STejun Heo } 31317234246STejun Heo 31454a86bfcSJeff Garzik /** 31554a86bfcSJeff Garzik * vt6420_prereset - prereset for vt6420 316cc0680a5STejun Heo * @link: target ATA link 317d4b2bab4STejun Heo * @deadline: deadline jiffies for the operation 31854a86bfcSJeff Garzik * 31954a86bfcSJeff Garzik * SCR registers on vt6420 are pieces of shit and may hang the 32054a86bfcSJeff Garzik * whole machine completely if accessed with the wrong timing. 32154a86bfcSJeff Garzik * To avoid such catastrophe, vt6420 doesn't provide generic SCR 32254a86bfcSJeff Garzik * access operations, but uses SStatus and SControl only during 32354a86bfcSJeff Garzik * boot probing in controlled way. 32454a86bfcSJeff Garzik * 32554a86bfcSJeff Garzik * As the old (pre EH update) probing code is proven to work, we 32654a86bfcSJeff Garzik * strictly follow the access pattern. 32754a86bfcSJeff Garzik * 32854a86bfcSJeff Garzik * LOCKING: 32954a86bfcSJeff Garzik * Kernel thread context (may sleep) 33054a86bfcSJeff Garzik * 33154a86bfcSJeff Garzik * RETURNS: 33254a86bfcSJeff Garzik * 0 on success, -errno otherwise. 33354a86bfcSJeff Garzik */ 334cc0680a5STejun Heo static int vt6420_prereset(struct ata_link *link, unsigned long deadline) 33554a86bfcSJeff Garzik { 336cc0680a5STejun Heo struct ata_port *ap = link->ap; 3379af5c9c9STejun Heo struct ata_eh_context *ehc = &ap->link.eh_context; 33854a86bfcSJeff Garzik unsigned long timeout = jiffies + (HZ * 5); 33954a86bfcSJeff Garzik u32 sstatus, scontrol; 34054a86bfcSJeff Garzik int online; 34154a86bfcSJeff Garzik 34254a86bfcSJeff Garzik /* don't do any SCR stuff if we're not loading */ 34368ff6e8eSJeff Garzik if (!(ap->pflags & ATA_PFLAG_LOADING)) 34454a86bfcSJeff Garzik goto skip_scr; 34554a86bfcSJeff Garzik 346a09060ffSJeff Garzik /* Resume phy. This is the old SATA resume sequence */ 34782ef04fbSTejun Heo svia_scr_write(link, SCR_CONTROL, 0x300); 34882ef04fbSTejun Heo svia_scr_read(link, SCR_CONTROL, &scontrol); /* flush */ 34954a86bfcSJeff Garzik 35054a86bfcSJeff Garzik /* wait for phy to become ready, if necessary */ 35154a86bfcSJeff Garzik do { 35297750cebSTejun Heo ata_msleep(link->ap, 200); 35382ef04fbSTejun Heo svia_scr_read(link, SCR_STATUS, &sstatus); 354da3dbb17STejun Heo if ((sstatus & 0xf) != 1) 35554a86bfcSJeff Garzik break; 35654a86bfcSJeff Garzik } while (time_before(jiffies, timeout)); 35754a86bfcSJeff Garzik 35854a86bfcSJeff Garzik /* open code sata_print_link_status() */ 35982ef04fbSTejun Heo svia_scr_read(link, SCR_STATUS, &sstatus); 36082ef04fbSTejun Heo svia_scr_read(link, SCR_CONTROL, &scontrol); 36154a86bfcSJeff Garzik 36254a86bfcSJeff Garzik online = (sstatus & 0xf) == 0x3; 36354a86bfcSJeff Garzik 36454a86bfcSJeff Garzik ata_port_printk(ap, KERN_INFO, 36554a86bfcSJeff Garzik "SATA link %s 1.5 Gbps (SStatus %X SControl %X)\n", 36654a86bfcSJeff Garzik online ? "up" : "down", sstatus, scontrol); 36754a86bfcSJeff Garzik 36854a86bfcSJeff Garzik /* SStatus is read one more time */ 36982ef04fbSTejun Heo svia_scr_read(link, SCR_STATUS, &sstatus); 37054a86bfcSJeff Garzik 37154a86bfcSJeff Garzik if (!online) { 37254a86bfcSJeff Garzik /* tell EH to bail */ 373cf480626STejun Heo ehc->i.action &= ~ATA_EH_RESET; 37454a86bfcSJeff Garzik return 0; 37554a86bfcSJeff Garzik } 37654a86bfcSJeff Garzik 37754a86bfcSJeff Garzik skip_scr: 37854a86bfcSJeff Garzik /* wait for !BSY */ 379705e76beSTejun Heo ata_sff_wait_ready(link, deadline); 38054a86bfcSJeff Garzik 38154a86bfcSJeff Garzik return 0; 38254a86bfcSJeff Garzik } 38354a86bfcSJeff Garzik 384a55ab496SBart Hartgers static void vt6420_bmdma_start(struct ata_queued_cmd *qc) 385a55ab496SBart Hartgers { 386a55ab496SBart Hartgers struct ata_port *ap = qc->ap; 387a55ab496SBart Hartgers if ((qc->tf.command == ATA_CMD_PACKET) && 388a55ab496SBart Hartgers (qc->scsicmd->sc_data_direction == DMA_TO_DEVICE)) { 389a55ab496SBart Hartgers /* Prevents corruption on some ATAPI burners */ 390a55ab496SBart Hartgers ata_sff_pause(ap); 391a55ab496SBart Hartgers } 392a55ab496SBart Hartgers ata_bmdma_start(qc); 393a55ab496SBart Hartgers } 394a55ab496SBart Hartgers 395a0fcdc02SJeff Garzik static int vt6421_pata_cable_detect(struct ata_port *ap) 396d73f30e1SAlan { 397d73f30e1SAlan struct pci_dev *pdev = to_pci_dev(ap->host->dev); 398d73f30e1SAlan u8 tmp; 399d73f30e1SAlan 400d73f30e1SAlan pci_read_config_byte(pdev, PATA_UDMA_TIMING, &tmp); 401d73f30e1SAlan if (tmp & 0x10) 402a0fcdc02SJeff Garzik return ATA_CBL_PATA40; 403a0fcdc02SJeff Garzik return ATA_CBL_PATA80; 404d73f30e1SAlan } 405d73f30e1SAlan 406d73f30e1SAlan static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev) 407d73f30e1SAlan { 408d73f30e1SAlan struct pci_dev *pdev = to_pci_dev(ap->host->dev); 409d73f30e1SAlan static const u8 pio_bits[] = { 0xA8, 0x65, 0x65, 0x31, 0x20 }; 41002d1d616SBart Hartgers pci_write_config_byte(pdev, PATA_PIO_TIMING - adev->devno, 41102d1d616SBart Hartgers pio_bits[adev->pio_mode - XFER_PIO_0]); 412d73f30e1SAlan } 413d73f30e1SAlan 414d73f30e1SAlan static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev) 415d73f30e1SAlan { 416d73f30e1SAlan struct pci_dev *pdev = to_pci_dev(ap->host->dev); 417d73f30e1SAlan static const u8 udma_bits[] = { 0xEE, 0xE8, 0xE6, 0xE4, 0xE2, 0xE1, 0xE0, 0xE0 }; 41802d1d616SBart Hartgers pci_write_config_byte(pdev, PATA_UDMA_TIMING - adev->devno, 41902d1d616SBart Hartgers udma_bits[adev->dma_mode - XFER_UDMA_0]); 420d73f30e1SAlan } 421d73f30e1SAlan 422c6fd2807SJeff Garzik static const unsigned int svia_bar_sizes[] = { 423c6fd2807SJeff Garzik 8, 4, 8, 4, 16, 256 424c6fd2807SJeff Garzik }; 425c6fd2807SJeff Garzik 426c6fd2807SJeff Garzik static const unsigned int vt6421_bar_sizes[] = { 427c6fd2807SJeff Garzik 16, 16, 16, 16, 32, 128 428c6fd2807SJeff Garzik }; 429c6fd2807SJeff Garzik 4300d5ff566STejun Heo static void __iomem *svia_scr_addr(void __iomem *addr, unsigned int port) 431c6fd2807SJeff Garzik { 432c6fd2807SJeff Garzik return addr + (port * 128); 433c6fd2807SJeff Garzik } 434c6fd2807SJeff Garzik 4350d5ff566STejun Heo static void __iomem *vt6421_scr_addr(void __iomem *addr, unsigned int port) 436c6fd2807SJeff Garzik { 437c6fd2807SJeff Garzik return addr + (port * 64); 438c6fd2807SJeff Garzik } 439c6fd2807SJeff Garzik 440eca25dcaSTejun Heo static void vt6421_init_addrs(struct ata_port *ap) 441c6fd2807SJeff Garzik { 442eca25dcaSTejun Heo void __iomem * const * iomap = ap->host->iomap; 443eca25dcaSTejun Heo void __iomem *reg_addr = iomap[ap->port_no]; 444eca25dcaSTejun Heo void __iomem *bmdma_addr = iomap[4] + (ap->port_no * 8); 445eca25dcaSTejun Heo struct ata_ioports *ioaddr = &ap->ioaddr; 446c6fd2807SJeff Garzik 447eca25dcaSTejun Heo ioaddr->cmd_addr = reg_addr; 448eca25dcaSTejun Heo ioaddr->altstatus_addr = 449eca25dcaSTejun Heo ioaddr->ctl_addr = (void __iomem *) 4500d5ff566STejun Heo ((unsigned long)(reg_addr + 8) | ATA_PCI_CTL_OFS); 451eca25dcaSTejun Heo ioaddr->bmdma_addr = bmdma_addr; 452eca25dcaSTejun Heo ioaddr->scr_addr = vt6421_scr_addr(iomap[5], ap->port_no); 453c6fd2807SJeff Garzik 4549363c382STejun Heo ata_sff_std_ports(ioaddr); 455cbcdd875STejun Heo 456cbcdd875STejun Heo ata_port_pbar_desc(ap, ap->port_no, -1, "port"); 457cbcdd875STejun Heo ata_port_pbar_desc(ap, 4, ap->port_no * 8, "bmdma"); 458c6fd2807SJeff Garzik } 459c6fd2807SJeff Garzik 460eca25dcaSTejun Heo static int vt6420_prepare_host(struct pci_dev *pdev, struct ata_host **r_host) 461c6fd2807SJeff Garzik { 462eca25dcaSTejun Heo const struct ata_port_info *ppi[] = { &vt6420_port_info, NULL }; 463eca25dcaSTejun Heo struct ata_host *host; 464eca25dcaSTejun Heo int rc; 465c6fd2807SJeff Garzik 4661c5afdf7STejun Heo rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host); 467eca25dcaSTejun Heo if (rc) 468eca25dcaSTejun Heo return rc; 469eca25dcaSTejun Heo *r_host = host; 470c6fd2807SJeff Garzik 471eca25dcaSTejun Heo rc = pcim_iomap_regions(pdev, 1 << 5, DRV_NAME); 472eca25dcaSTejun Heo if (rc) { 473e1be5d73STejun Heo dev_printk(KERN_ERR, &pdev->dev, "failed to iomap PCI BAR 5\n"); 474eca25dcaSTejun Heo return rc; 475e1be5d73STejun Heo } 476e1be5d73STejun Heo 477eca25dcaSTejun Heo host->ports[0]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 0); 478eca25dcaSTejun Heo host->ports[1]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 1); 479c6fd2807SJeff Garzik 480eca25dcaSTejun Heo return 0; 481c6fd2807SJeff Garzik } 482c6fd2807SJeff Garzik 483eca25dcaSTejun Heo static int vt6421_prepare_host(struct pci_dev *pdev, struct ata_host **r_host) 484c6fd2807SJeff Garzik { 485eca25dcaSTejun Heo const struct ata_port_info *ppi[] = 486eca25dcaSTejun Heo { &vt6421_sport_info, &vt6421_sport_info, &vt6421_pport_info }; 487eca25dcaSTejun Heo struct ata_host *host; 488eca25dcaSTejun Heo int i, rc; 489c6fd2807SJeff Garzik 490eca25dcaSTejun Heo *r_host = host = ata_host_alloc_pinfo(&pdev->dev, ppi, ARRAY_SIZE(ppi)); 491eca25dcaSTejun Heo if (!host) { 492eca25dcaSTejun Heo dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n"); 493eca25dcaSTejun Heo return -ENOMEM; 494e1be5d73STejun Heo } 495e1be5d73STejun Heo 4968fd7d1b1STejun Heo rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME); 497eca25dcaSTejun Heo if (rc) { 498eca25dcaSTejun Heo dev_printk(KERN_ERR, &pdev->dev, "failed to request/iomap " 499eca25dcaSTejun Heo "PCI BARs (errno=%d)\n", rc); 500eca25dcaSTejun Heo return rc; 501eca25dcaSTejun Heo } 502eca25dcaSTejun Heo host->iomap = pcim_iomap_table(pdev); 503c6fd2807SJeff Garzik 504eca25dcaSTejun Heo for (i = 0; i < host->n_ports; i++) 505eca25dcaSTejun Heo vt6421_init_addrs(host->ports[i]); 506eca25dcaSTejun Heo 507eca25dcaSTejun Heo rc = pci_set_dma_mask(pdev, ATA_DMA_MASK); 508eca25dcaSTejun Heo if (rc) 509eca25dcaSTejun Heo return rc; 510eca25dcaSTejun Heo rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK); 511eca25dcaSTejun Heo if (rc) 512eca25dcaSTejun Heo return rc; 513eca25dcaSTejun Heo 514eca25dcaSTejun Heo return 0; 515c6fd2807SJeff Garzik } 516c6fd2807SJeff Garzik 517b9d5b89bSTejun Heo static int vt8251_prepare_host(struct pci_dev *pdev, struct ata_host **r_host) 518b9d5b89bSTejun Heo { 519b9d5b89bSTejun Heo const struct ata_port_info *ppi[] = { &vt8251_port_info, NULL }; 520b9d5b89bSTejun Heo struct ata_host *host; 521b9d5b89bSTejun Heo int i, rc; 522b9d5b89bSTejun Heo 5231c5afdf7STejun Heo rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host); 524b9d5b89bSTejun Heo if (rc) 525b9d5b89bSTejun Heo return rc; 526b9d5b89bSTejun Heo *r_host = host; 527b9d5b89bSTejun Heo 528b9d5b89bSTejun Heo rc = pcim_iomap_regions(pdev, 1 << 5, DRV_NAME); 529b9d5b89bSTejun Heo if (rc) { 530b9d5b89bSTejun Heo dev_printk(KERN_ERR, &pdev->dev, "failed to iomap PCI BAR 5\n"); 531b9d5b89bSTejun Heo return rc; 532b9d5b89bSTejun Heo } 533b9d5b89bSTejun Heo 534b9d5b89bSTejun Heo /* 8251 hosts four sata ports as M/S of the two channels */ 535b9d5b89bSTejun Heo for (i = 0; i < host->n_ports; i++) 536b9d5b89bSTejun Heo ata_slave_link_init(host->ports[i]); 537b9d5b89bSTejun Heo 538b9d5b89bSTejun Heo return 0; 539b9d5b89bSTejun Heo } 540b9d5b89bSTejun Heo 541*b1353e4fSTejun Heo static void svia_configure(struct pci_dev *pdev, int board_id) 542c6fd2807SJeff Garzik { 543c6fd2807SJeff Garzik u8 tmp8; 544c6fd2807SJeff Garzik 545c6fd2807SJeff Garzik pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, &tmp8); 546c6fd2807SJeff Garzik dev_printk(KERN_INFO, &pdev->dev, "routed to hard irq line %d\n", 547c6fd2807SJeff Garzik (int) (tmp8 & 0xf0) == 0xf0 ? 0 : tmp8 & 0x0f); 548c6fd2807SJeff Garzik 549c6fd2807SJeff Garzik /* make sure SATA channels are enabled */ 550c6fd2807SJeff Garzik pci_read_config_byte(pdev, SATA_CHAN_ENAB, &tmp8); 551c6fd2807SJeff Garzik if ((tmp8 & ALL_PORTS) != ALL_PORTS) { 552c6fd2807SJeff Garzik dev_printk(KERN_DEBUG, &pdev->dev, 553c6fd2807SJeff Garzik "enabling SATA channels (0x%x)\n", 554c6fd2807SJeff Garzik (int) tmp8); 555c6fd2807SJeff Garzik tmp8 |= ALL_PORTS; 556c6fd2807SJeff Garzik pci_write_config_byte(pdev, SATA_CHAN_ENAB, tmp8); 557c6fd2807SJeff Garzik } 558c6fd2807SJeff Garzik 559c6fd2807SJeff Garzik /* make sure interrupts for each channel sent to us */ 560c6fd2807SJeff Garzik pci_read_config_byte(pdev, SATA_INT_GATE, &tmp8); 561c6fd2807SJeff Garzik if ((tmp8 & ALL_PORTS) != ALL_PORTS) { 562c6fd2807SJeff Garzik dev_printk(KERN_DEBUG, &pdev->dev, 563c6fd2807SJeff Garzik "enabling SATA channel interrupts (0x%x)\n", 564c6fd2807SJeff Garzik (int) tmp8); 565c6fd2807SJeff Garzik tmp8 |= ALL_PORTS; 566c6fd2807SJeff Garzik pci_write_config_byte(pdev, SATA_INT_GATE, tmp8); 567c6fd2807SJeff Garzik } 568c6fd2807SJeff Garzik 569c6fd2807SJeff Garzik /* make sure native mode is enabled */ 570c6fd2807SJeff Garzik pci_read_config_byte(pdev, SATA_NATIVE_MODE, &tmp8); 571c6fd2807SJeff Garzik if ((tmp8 & NATIVE_MODE_ALL) != NATIVE_MODE_ALL) { 572c6fd2807SJeff Garzik dev_printk(KERN_DEBUG, &pdev->dev, 573c6fd2807SJeff Garzik "enabling SATA channel native mode (0x%x)\n", 574c6fd2807SJeff Garzik (int) tmp8); 575c6fd2807SJeff Garzik tmp8 |= NATIVE_MODE_ALL; 576c6fd2807SJeff Garzik pci_write_config_byte(pdev, SATA_NATIVE_MODE, tmp8); 577c6fd2807SJeff Garzik } 5788b27ff4cSTejun Heo 5798b27ff4cSTejun Heo /* 580*b1353e4fSTejun Heo * vt6420/1 has problems talking to some drives. The following 581b475a3b8STejun Heo * is the fix from Joseph Chan <JosephChan@via.com.tw>. 582b475a3b8STejun Heo * 583b475a3b8STejun Heo * When host issues HOLD, device may send up to 20DW of data 584b475a3b8STejun Heo * before acknowledging it with HOLDA and the host should be 585b475a3b8STejun Heo * able to buffer them in FIFO. Unfortunately, some WD drives 586b475a3b8STejun Heo * send upto 40DW before acknowledging HOLD and, in the 587b475a3b8STejun Heo * default configuration, this ends up overflowing vt6421's 588b475a3b8STejun Heo * FIFO, making the controller abort the transaction with 589b475a3b8STejun Heo * R_ERR. 590b475a3b8STejun Heo * 591b475a3b8STejun Heo * Rx52[2] is the internal 128DW FIFO Flow control watermark 592b475a3b8STejun Heo * adjusting mechanism enable bit and the default value 0 593b475a3b8STejun Heo * means host will issue HOLD to device when the left FIFO 594b475a3b8STejun Heo * size goes below 32DW. Setting it to 1 makes the watermark 595b475a3b8STejun Heo * 64DW. 5968b27ff4cSTejun Heo * 5978b27ff4cSTejun Heo * https://bugzilla.kernel.org/show_bug.cgi?id=15173 598b475a3b8STejun Heo * http://article.gmane.org/gmane.linux.ide/46352 599*b1353e4fSTejun Heo * http://thread.gmane.org/gmane.linux.kernel/1062139 6008b27ff4cSTejun Heo */ 601*b1353e4fSTejun Heo if (board_id == vt6420 || board_id == vt6421) { 6028b27ff4cSTejun Heo pci_read_config_byte(pdev, 0x52, &tmp8); 6038b27ff4cSTejun Heo tmp8 |= 1 << 2; 6048b27ff4cSTejun Heo pci_write_config_byte(pdev, 0x52, tmp8); 6058b27ff4cSTejun Heo } 606c6fd2807SJeff Garzik } 607c6fd2807SJeff Garzik 608c6fd2807SJeff Garzik static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 609c6fd2807SJeff Garzik { 610c6fd2807SJeff Garzik static int printed_version; 611c6fd2807SJeff Garzik unsigned int i; 612c6fd2807SJeff Garzik int rc; 613f1c22943SJeff Garzik struct ata_host *host = NULL; 614c6fd2807SJeff Garzik int board_id = (int) ent->driver_data; 615b4482a4bSAl Viro const unsigned *bar_sizes; 616c6fd2807SJeff Garzik 617c6fd2807SJeff Garzik if (!printed_version++) 618c6fd2807SJeff Garzik dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n"); 619c6fd2807SJeff Garzik 62024dc5f33STejun Heo rc = pcim_enable_device(pdev); 621c6fd2807SJeff Garzik if (rc) 622c6fd2807SJeff Garzik return rc; 623c6fd2807SJeff Garzik 624b9d5b89bSTejun Heo if (board_id == vt6421) 625c6fd2807SJeff Garzik bar_sizes = &vt6421_bar_sizes[0]; 626b9d5b89bSTejun Heo else 627b9d5b89bSTejun Heo bar_sizes = &svia_bar_sizes[0]; 628c6fd2807SJeff Garzik 629c6fd2807SJeff Garzik for (i = 0; i < ARRAY_SIZE(svia_bar_sizes); i++) 630c6fd2807SJeff Garzik if ((pci_resource_start(pdev, i) == 0) || 631c6fd2807SJeff Garzik (pci_resource_len(pdev, i) < bar_sizes[i])) { 632c6fd2807SJeff Garzik dev_printk(KERN_ERR, &pdev->dev, 633c6fd2807SJeff Garzik "invalid PCI BAR %u (sz 0x%llx, val 0x%llx)\n", 634c6fd2807SJeff Garzik i, 635c6fd2807SJeff Garzik (unsigned long long)pci_resource_start(pdev, i), 636c6fd2807SJeff Garzik (unsigned long long)pci_resource_len(pdev, i)); 63724dc5f33STejun Heo return -ENODEV; 638c6fd2807SJeff Garzik } 639c6fd2807SJeff Garzik 640b9d5b89bSTejun Heo switch (board_id) { 641b9d5b89bSTejun Heo case vt6420: 642eca25dcaSTejun Heo rc = vt6420_prepare_host(pdev, &host); 643b9d5b89bSTejun Heo break; 644b9d5b89bSTejun Heo case vt6421: 645eca25dcaSTejun Heo rc = vt6421_prepare_host(pdev, &host); 646b9d5b89bSTejun Heo break; 647b9d5b89bSTejun Heo case vt8251: 648b9d5b89bSTejun Heo rc = vt8251_prepare_host(pdev, &host); 649b9d5b89bSTejun Heo break; 650b9d5b89bSTejun Heo default: 651554d491dSMarcin Slusarz rc = -EINVAL; 652b9d5b89bSTejun Heo } 653554d491dSMarcin Slusarz if (rc) 654554d491dSMarcin Slusarz return rc; 655c6fd2807SJeff Garzik 656*b1353e4fSTejun Heo svia_configure(pdev, board_id); 657c6fd2807SJeff Garzik 658c6fd2807SJeff Garzik pci_set_master(pdev); 659c3b28894STejun Heo return ata_host_activate(host, pdev->irq, ata_bmdma_interrupt, 6609363c382STejun Heo IRQF_SHARED, &svia_sht); 661c6fd2807SJeff Garzik } 662c6fd2807SJeff Garzik 663c6fd2807SJeff Garzik static int __init svia_init(void) 664c6fd2807SJeff Garzik { 665c6fd2807SJeff Garzik return pci_register_driver(&svia_pci_driver); 666c6fd2807SJeff Garzik } 667c6fd2807SJeff Garzik 668c6fd2807SJeff Garzik static void __exit svia_exit(void) 669c6fd2807SJeff Garzik { 670c6fd2807SJeff Garzik pci_unregister_driver(&svia_pci_driver); 671c6fd2807SJeff Garzik } 672c6fd2807SJeff Garzik 673c6fd2807SJeff Garzik module_init(svia_init); 674c6fd2807SJeff Garzik module_exit(svia_exit); 675