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> 43c6fd2807SJeff Garzik #include <scsi/scsi_host.h> 44c6fd2807SJeff Garzik #include <linux/libata.h> 45c6fd2807SJeff Garzik 46c6fd2807SJeff Garzik #define DRV_NAME "sata_via" 47*02d1d616SBart Hartgers #define DRV_VERSION "2.5" 48c6fd2807SJeff Garzik 49b9d5b89bSTejun Heo /* 50b9d5b89bSTejun Heo * vt8251 is different from other sata controllers of VIA. It has two 51b9d5b89bSTejun Heo * channels, each channel has both Master and Slave slot. 52b9d5b89bSTejun Heo */ 53c6fd2807SJeff Garzik enum board_ids_enum { 54c6fd2807SJeff Garzik vt6420, 55c6fd2807SJeff Garzik vt6421, 56b9d5b89bSTejun Heo vt8251, 57c6fd2807SJeff Garzik }; 58c6fd2807SJeff Garzik 59c6fd2807SJeff Garzik enum { 60c6fd2807SJeff Garzik SATA_CHAN_ENAB = 0x40, /* SATA channel enable */ 61c6fd2807SJeff Garzik SATA_INT_GATE = 0x41, /* SATA interrupt gating */ 62c6fd2807SJeff Garzik SATA_NATIVE_MODE = 0x42, /* Native mode enable */ 63d73f30e1SAlan PATA_UDMA_TIMING = 0xB3, /* PATA timing for DMA/ cable detect */ 64d73f30e1SAlan PATA_PIO_TIMING = 0xAB, /* PATA timing register */ 65c6fd2807SJeff Garzik 66c6fd2807SJeff Garzik PORT0 = (1 << 1), 67c6fd2807SJeff Garzik PORT1 = (1 << 0), 68c6fd2807SJeff Garzik ALL_PORTS = PORT0 | PORT1, 69c6fd2807SJeff Garzik 70c6fd2807SJeff Garzik NATIVE_MODE_ALL = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4), 71c6fd2807SJeff Garzik 72c6fd2807SJeff Garzik SATA_EXT_PHY = (1 << 6), /* 0==use PATA, 1==ext phy */ 73c6fd2807SJeff Garzik }; 74c6fd2807SJeff Garzik 75c6fd2807SJeff Garzik static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent); 7682ef04fbSTejun Heo static int svia_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val); 7782ef04fbSTejun Heo static int svia_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val); 78b9d5b89bSTejun Heo static int vt8251_scr_read(struct ata_link *link, unsigned int scr, u32 *val); 79b9d5b89bSTejun Heo static int vt8251_scr_write(struct ata_link *link, unsigned int scr, u32 val); 80b78152e9STejun Heo static void svia_tf_load(struct ata_port *ap, const struct ata_taskfile *tf); 8117234246STejun Heo static void svia_noop_freeze(struct ata_port *ap); 82a1efdabaSTejun Heo static int vt6420_prereset(struct ata_link *link, unsigned long deadline); 83a0fcdc02SJeff Garzik static int vt6421_pata_cable_detect(struct ata_port *ap); 84d73f30e1SAlan static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev); 85d73f30e1SAlan static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev); 86c6fd2807SJeff Garzik 87c6fd2807SJeff Garzik static const struct pci_device_id svia_pci_tbl[] = { 8896bc103fSLuca Pedrielli { PCI_VDEVICE(VIA, 0x5337), vt6420 }, 89b9d5b89bSTejun Heo { PCI_VDEVICE(VIA, 0x0591), vt6420 }, /* 2 sata chnls (Master) */ 90b9d5b89bSTejun Heo { PCI_VDEVICE(VIA, 0x3149), vt6420 }, /* 2 sata chnls (Master) */ 91b9d5b89bSTejun Heo { PCI_VDEVICE(VIA, 0x3249), vt6421 }, /* 2 sata chnls, 1 pata chnl */ 9252df0ee0SJeff Garzik { PCI_VDEVICE(VIA, 0x5372), vt6420 }, 9352df0ee0SJeff Garzik { PCI_VDEVICE(VIA, 0x7372), vt6420 }, 94b9d5b89bSTejun Heo { PCI_VDEVICE(VIA, 0x5287), vt8251 }, /* 2 sata chnls (Master/Slave) */ 9568139520SJosephChan@via.com.tw { PCI_VDEVICE(VIA, 0x9000), vt8251 }, 96c6fd2807SJeff Garzik 97c6fd2807SJeff Garzik { } /* terminate list */ 98c6fd2807SJeff Garzik }; 99c6fd2807SJeff Garzik 100c6fd2807SJeff Garzik static struct pci_driver svia_pci_driver = { 101c6fd2807SJeff Garzik .name = DRV_NAME, 102c6fd2807SJeff Garzik .id_table = svia_pci_tbl, 103c6fd2807SJeff Garzik .probe = svia_init_one, 104e1e143cfSTejun Heo #ifdef CONFIG_PM 105e1e143cfSTejun Heo .suspend = ata_pci_device_suspend, 106e1e143cfSTejun Heo .resume = ata_pci_device_resume, 107e1e143cfSTejun Heo #endif 108c6fd2807SJeff Garzik .remove = ata_pci_remove_one, 109c6fd2807SJeff Garzik }; 110c6fd2807SJeff Garzik 111c6fd2807SJeff Garzik static struct scsi_host_template svia_sht = { 11268d1d07bSTejun Heo ATA_BMDMA_SHT(DRV_NAME), 113c6fd2807SJeff Garzik }; 114c6fd2807SJeff Garzik 115b78152e9STejun Heo static struct ata_port_operations svia_base_ops = { 116029cfd6bSTejun Heo .inherits = &ata_bmdma_port_ops, 117b78152e9STejun Heo .sff_tf_load = svia_tf_load, 118b78152e9STejun Heo }; 119b78152e9STejun Heo 120b78152e9STejun Heo static struct ata_port_operations vt6420_sata_ops = { 121b78152e9STejun Heo .inherits = &svia_base_ops, 12217234246STejun Heo .freeze = svia_noop_freeze, 123a1efdabaSTejun Heo .prereset = vt6420_prereset, 12454a86bfcSJeff Garzik }; 12554a86bfcSJeff Garzik 126029cfd6bSTejun Heo static struct ata_port_operations vt6421_pata_ops = { 127b78152e9STejun Heo .inherits = &svia_base_ops, 128029cfd6bSTejun Heo .cable_detect = vt6421_pata_cable_detect, 129d73f30e1SAlan .set_piomode = vt6421_set_pio_mode, 130d73f30e1SAlan .set_dmamode = vt6421_set_dma_mode, 131d73f30e1SAlan }; 132d73f30e1SAlan 133029cfd6bSTejun Heo static struct ata_port_operations vt6421_sata_ops = { 134b78152e9STejun Heo .inherits = &svia_base_ops, 135c6fd2807SJeff Garzik .scr_read = svia_scr_read, 136c6fd2807SJeff Garzik .scr_write = svia_scr_write, 137c6fd2807SJeff Garzik }; 138c6fd2807SJeff Garzik 139b9d5b89bSTejun Heo static struct ata_port_operations vt8251_ops = { 140b9d5b89bSTejun Heo .inherits = &svia_base_ops, 141b9d5b89bSTejun Heo .hardreset = sata_std_hardreset, 142b9d5b89bSTejun Heo .scr_read = vt8251_scr_read, 143b9d5b89bSTejun Heo .scr_write = vt8251_scr_write, 144b9d5b89bSTejun Heo }; 145b9d5b89bSTejun Heo 146eca25dcaSTejun Heo static const struct ata_port_info vt6420_port_info = { 147cca3974eSJeff Garzik .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY, 14814bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 14914bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 150bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 15154a86bfcSJeff Garzik .port_ops = &vt6420_sata_ops, 152c6fd2807SJeff Garzik }; 153c6fd2807SJeff Garzik 154eca25dcaSTejun Heo static struct ata_port_info vt6421_sport_info = { 155eca25dcaSTejun Heo .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY, 15614bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 15714bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 158bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 159eca25dcaSTejun Heo .port_ops = &vt6421_sata_ops, 160eca25dcaSTejun Heo }; 161eca25dcaSTejun Heo 162eca25dcaSTejun Heo static struct ata_port_info vt6421_pport_info = { 163eca25dcaSTejun Heo .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_LEGACY, 16414bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 16514bdef98SErik Inge Bolsø /* No MWDMA */ 166bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 167eca25dcaSTejun Heo .port_ops = &vt6421_pata_ops, 168eca25dcaSTejun Heo }; 169eca25dcaSTejun Heo 170b9d5b89bSTejun Heo static struct ata_port_info vt8251_port_info = { 171b9d5b89bSTejun Heo .flags = ATA_FLAG_SATA | ATA_FLAG_SLAVE_POSS | 172b9d5b89bSTejun Heo ATA_FLAG_NO_LEGACY, 17314bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 17414bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 175b9d5b89bSTejun Heo .udma_mask = ATA_UDMA6, 176b9d5b89bSTejun Heo .port_ops = &vt8251_ops, 177b9d5b89bSTejun Heo }; 178b9d5b89bSTejun Heo 179c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik"); 180c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for VIA SATA controllers"); 181c6fd2807SJeff Garzik MODULE_LICENSE("GPL"); 182c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, svia_pci_tbl); 183c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION); 184c6fd2807SJeff Garzik 18582ef04fbSTejun Heo static int svia_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val) 186c6fd2807SJeff Garzik { 187c6fd2807SJeff Garzik if (sc_reg > SCR_CONTROL) 188da3dbb17STejun Heo return -EINVAL; 18982ef04fbSTejun Heo *val = ioread32(link->ap->ioaddr.scr_addr + (4 * sc_reg)); 190da3dbb17STejun Heo return 0; 191c6fd2807SJeff Garzik } 192c6fd2807SJeff Garzik 19382ef04fbSTejun Heo static int svia_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val) 194c6fd2807SJeff Garzik { 195c6fd2807SJeff Garzik if (sc_reg > SCR_CONTROL) 196da3dbb17STejun Heo return -EINVAL; 19782ef04fbSTejun Heo iowrite32(val, link->ap->ioaddr.scr_addr + (4 * sc_reg)); 198da3dbb17STejun Heo return 0; 199c6fd2807SJeff Garzik } 200c6fd2807SJeff Garzik 201b9d5b89bSTejun Heo static int vt8251_scr_read(struct ata_link *link, unsigned int scr, u32 *val) 202b9d5b89bSTejun Heo { 203b9d5b89bSTejun Heo static const u8 ipm_tbl[] = { 1, 2, 6, 0 }; 204b9d5b89bSTejun Heo struct pci_dev *pdev = to_pci_dev(link->ap->host->dev); 205b9d5b89bSTejun Heo int slot = 2 * link->ap->port_no + link->pmp; 206b9d5b89bSTejun Heo u32 v = 0; 207b9d5b89bSTejun Heo u8 raw; 208b9d5b89bSTejun Heo 209b9d5b89bSTejun Heo switch (scr) { 210b9d5b89bSTejun Heo case SCR_STATUS: 211b9d5b89bSTejun Heo pci_read_config_byte(pdev, 0xA0 + slot, &raw); 212b9d5b89bSTejun Heo 213b9d5b89bSTejun Heo /* read the DET field, bit0 and 1 of the config byte */ 214b9d5b89bSTejun Heo v |= raw & 0x03; 215b9d5b89bSTejun Heo 216b9d5b89bSTejun Heo /* read the SPD field, bit4 of the configure byte */ 217b9d5b89bSTejun Heo if (raw & (1 << 4)) 218b9d5b89bSTejun Heo v |= 0x02 << 4; 219b9d5b89bSTejun Heo else 220b9d5b89bSTejun Heo v |= 0x01 << 4; 221b9d5b89bSTejun Heo 222b9d5b89bSTejun Heo /* read the IPM field, bit2 and 3 of the config byte */ 223b9d5b89bSTejun Heo v |= ipm_tbl[(raw >> 2) & 0x3]; 224b9d5b89bSTejun Heo break; 225b9d5b89bSTejun Heo 226b9d5b89bSTejun Heo case SCR_ERROR: 227b9d5b89bSTejun Heo /* devices other than 5287 uses 0xA8 as base */ 228b9d5b89bSTejun Heo WARN_ON(pdev->device != 0x5287); 229b9d5b89bSTejun Heo pci_read_config_dword(pdev, 0xB0 + slot * 4, &v); 230b9d5b89bSTejun Heo break; 231b9d5b89bSTejun Heo 232b9d5b89bSTejun Heo case SCR_CONTROL: 233b9d5b89bSTejun Heo pci_read_config_byte(pdev, 0xA4 + slot, &raw); 234b9d5b89bSTejun Heo 235b9d5b89bSTejun Heo /* read the DET field, bit0 and bit1 */ 236b9d5b89bSTejun Heo v |= ((raw & 0x02) << 1) | (raw & 0x01); 237b9d5b89bSTejun Heo 238b9d5b89bSTejun Heo /* read the IPM field, bit2 and bit3 */ 239b9d5b89bSTejun Heo v |= ((raw >> 2) & 0x03) << 8; 240b9d5b89bSTejun Heo break; 241b9d5b89bSTejun Heo 242b9d5b89bSTejun Heo default: 243b9d5b89bSTejun Heo return -EINVAL; 244b9d5b89bSTejun Heo } 245b9d5b89bSTejun Heo 246b9d5b89bSTejun Heo *val = v; 247b9d5b89bSTejun Heo return 0; 248b9d5b89bSTejun Heo } 249b9d5b89bSTejun Heo 250b9d5b89bSTejun Heo static int vt8251_scr_write(struct ata_link *link, unsigned int scr, u32 val) 251b9d5b89bSTejun Heo { 252b9d5b89bSTejun Heo struct pci_dev *pdev = to_pci_dev(link->ap->host->dev); 253b9d5b89bSTejun Heo int slot = 2 * link->ap->port_no + link->pmp; 254b9d5b89bSTejun Heo u32 v = 0; 255b9d5b89bSTejun Heo 256b9d5b89bSTejun Heo switch (scr) { 257b9d5b89bSTejun Heo case SCR_ERROR: 258b9d5b89bSTejun Heo /* devices other than 5287 uses 0xA8 as base */ 259b9d5b89bSTejun Heo WARN_ON(pdev->device != 0x5287); 260b9d5b89bSTejun Heo pci_write_config_dword(pdev, 0xB0 + slot * 4, val); 261b9d5b89bSTejun Heo return 0; 262b9d5b89bSTejun Heo 263b9d5b89bSTejun Heo case SCR_CONTROL: 264b9d5b89bSTejun Heo /* set the DET field */ 265b9d5b89bSTejun Heo v |= ((val & 0x4) >> 1) | (val & 0x1); 266b9d5b89bSTejun Heo 267b9d5b89bSTejun Heo /* set the IPM field */ 268b9d5b89bSTejun Heo v |= ((val >> 8) & 0x3) << 2; 269b9d5b89bSTejun Heo 270b9d5b89bSTejun Heo pci_write_config_byte(pdev, 0xA4 + slot, v); 271b9d5b89bSTejun Heo return 0; 272b9d5b89bSTejun Heo 273b9d5b89bSTejun Heo default: 274b9d5b89bSTejun Heo return -EINVAL; 275b9d5b89bSTejun Heo } 276b9d5b89bSTejun Heo } 277b9d5b89bSTejun Heo 278b78152e9STejun Heo /** 279b78152e9STejun Heo * svia_tf_load - send taskfile registers to host controller 280b78152e9STejun Heo * @ap: Port to which output is sent 281b78152e9STejun Heo * @tf: ATA taskfile register set 282b78152e9STejun Heo * 283b78152e9STejun Heo * Outputs ATA taskfile to standard ATA host controller. 284b78152e9STejun Heo * 285b78152e9STejun Heo * This is to fix the internal bug of via chipsets, which will 286b78152e9STejun Heo * reset the device register after changing the IEN bit on ctl 287b78152e9STejun Heo * register. 288b78152e9STejun Heo */ 289b78152e9STejun Heo static void svia_tf_load(struct ata_port *ap, const struct ata_taskfile *tf) 290b78152e9STejun Heo { 291b78152e9STejun Heo struct ata_taskfile ttf; 292b78152e9STejun Heo 293b78152e9STejun Heo if (tf->ctl != ap->last_ctl) { 294b78152e9STejun Heo ttf = *tf; 295b78152e9STejun Heo ttf.flags |= ATA_TFLAG_DEVICE; 296b78152e9STejun Heo tf = &ttf; 297b78152e9STejun Heo } 298b78152e9STejun Heo ata_sff_tf_load(ap, tf); 299b78152e9STejun Heo } 300b78152e9STejun Heo 30117234246STejun Heo static void svia_noop_freeze(struct ata_port *ap) 30217234246STejun Heo { 30317234246STejun Heo /* Some VIA controllers choke if ATA_NIEN is manipulated in 30417234246STejun Heo * certain way. Leave it alone and just clear pending IRQ. 30517234246STejun Heo */ 3065682ed33STejun Heo ap->ops->sff_check_status(ap); 3079363c382STejun Heo ata_sff_irq_clear(ap); 30817234246STejun Heo } 30917234246STejun Heo 31054a86bfcSJeff Garzik /** 31154a86bfcSJeff Garzik * vt6420_prereset - prereset for vt6420 312cc0680a5STejun Heo * @link: target ATA link 313d4b2bab4STejun Heo * @deadline: deadline jiffies for the operation 31454a86bfcSJeff Garzik * 31554a86bfcSJeff Garzik * SCR registers on vt6420 are pieces of shit and may hang the 31654a86bfcSJeff Garzik * whole machine completely if accessed with the wrong timing. 31754a86bfcSJeff Garzik * To avoid such catastrophe, vt6420 doesn't provide generic SCR 31854a86bfcSJeff Garzik * access operations, but uses SStatus and SControl only during 31954a86bfcSJeff Garzik * boot probing in controlled way. 32054a86bfcSJeff Garzik * 32154a86bfcSJeff Garzik * As the old (pre EH update) probing code is proven to work, we 32254a86bfcSJeff Garzik * strictly follow the access pattern. 32354a86bfcSJeff Garzik * 32454a86bfcSJeff Garzik * LOCKING: 32554a86bfcSJeff Garzik * Kernel thread context (may sleep) 32654a86bfcSJeff Garzik * 32754a86bfcSJeff Garzik * RETURNS: 32854a86bfcSJeff Garzik * 0 on success, -errno otherwise. 32954a86bfcSJeff Garzik */ 330cc0680a5STejun Heo static int vt6420_prereset(struct ata_link *link, unsigned long deadline) 33154a86bfcSJeff Garzik { 332cc0680a5STejun Heo struct ata_port *ap = link->ap; 3339af5c9c9STejun Heo struct ata_eh_context *ehc = &ap->link.eh_context; 33454a86bfcSJeff Garzik unsigned long timeout = jiffies + (HZ * 5); 33554a86bfcSJeff Garzik u32 sstatus, scontrol; 33654a86bfcSJeff Garzik int online; 33754a86bfcSJeff Garzik 33854a86bfcSJeff Garzik /* don't do any SCR stuff if we're not loading */ 33968ff6e8eSJeff Garzik if (!(ap->pflags & ATA_PFLAG_LOADING)) 34054a86bfcSJeff Garzik goto skip_scr; 34154a86bfcSJeff Garzik 342a09060ffSJeff Garzik /* Resume phy. This is the old SATA resume sequence */ 34382ef04fbSTejun Heo svia_scr_write(link, SCR_CONTROL, 0x300); 34482ef04fbSTejun Heo svia_scr_read(link, SCR_CONTROL, &scontrol); /* flush */ 34554a86bfcSJeff Garzik 34654a86bfcSJeff Garzik /* wait for phy to become ready, if necessary */ 34754a86bfcSJeff Garzik do { 34854a86bfcSJeff Garzik msleep(200); 34982ef04fbSTejun Heo svia_scr_read(link, SCR_STATUS, &sstatus); 350da3dbb17STejun Heo if ((sstatus & 0xf) != 1) 35154a86bfcSJeff Garzik break; 35254a86bfcSJeff Garzik } while (time_before(jiffies, timeout)); 35354a86bfcSJeff Garzik 35454a86bfcSJeff Garzik /* open code sata_print_link_status() */ 35582ef04fbSTejun Heo svia_scr_read(link, SCR_STATUS, &sstatus); 35682ef04fbSTejun Heo svia_scr_read(link, SCR_CONTROL, &scontrol); 35754a86bfcSJeff Garzik 35854a86bfcSJeff Garzik online = (sstatus & 0xf) == 0x3; 35954a86bfcSJeff Garzik 36054a86bfcSJeff Garzik ata_port_printk(ap, KERN_INFO, 36154a86bfcSJeff Garzik "SATA link %s 1.5 Gbps (SStatus %X SControl %X)\n", 36254a86bfcSJeff Garzik online ? "up" : "down", sstatus, scontrol); 36354a86bfcSJeff Garzik 36454a86bfcSJeff Garzik /* SStatus is read one more time */ 36582ef04fbSTejun Heo svia_scr_read(link, SCR_STATUS, &sstatus); 36654a86bfcSJeff Garzik 36754a86bfcSJeff Garzik if (!online) { 36854a86bfcSJeff Garzik /* tell EH to bail */ 369cf480626STejun Heo ehc->i.action &= ~ATA_EH_RESET; 37054a86bfcSJeff Garzik return 0; 37154a86bfcSJeff Garzik } 37254a86bfcSJeff Garzik 37354a86bfcSJeff Garzik skip_scr: 37454a86bfcSJeff Garzik /* wait for !BSY */ 375705e76beSTejun Heo ata_sff_wait_ready(link, deadline); 37654a86bfcSJeff Garzik 37754a86bfcSJeff Garzik return 0; 37854a86bfcSJeff Garzik } 37954a86bfcSJeff Garzik 380a0fcdc02SJeff Garzik static int vt6421_pata_cable_detect(struct ata_port *ap) 381d73f30e1SAlan { 382d73f30e1SAlan struct pci_dev *pdev = to_pci_dev(ap->host->dev); 383d73f30e1SAlan u8 tmp; 384d73f30e1SAlan 385d73f30e1SAlan pci_read_config_byte(pdev, PATA_UDMA_TIMING, &tmp); 386d73f30e1SAlan if (tmp & 0x10) 387a0fcdc02SJeff Garzik return ATA_CBL_PATA40; 388a0fcdc02SJeff Garzik return ATA_CBL_PATA80; 389d73f30e1SAlan } 390d73f30e1SAlan 391d73f30e1SAlan static void vt6421_set_pio_mode(struct ata_port *ap, struct ata_device *adev) 392d73f30e1SAlan { 393d73f30e1SAlan struct pci_dev *pdev = to_pci_dev(ap->host->dev); 394d73f30e1SAlan static const u8 pio_bits[] = { 0xA8, 0x65, 0x65, 0x31, 0x20 }; 395*02d1d616SBart Hartgers pci_write_config_byte(pdev, PATA_PIO_TIMING - adev->devno, 396*02d1d616SBart Hartgers pio_bits[adev->pio_mode - XFER_PIO_0]); 397d73f30e1SAlan } 398d73f30e1SAlan 399d73f30e1SAlan static void vt6421_set_dma_mode(struct ata_port *ap, struct ata_device *adev) 400d73f30e1SAlan { 401d73f30e1SAlan struct pci_dev *pdev = to_pci_dev(ap->host->dev); 402d73f30e1SAlan static const u8 udma_bits[] = { 0xEE, 0xE8, 0xE6, 0xE4, 0xE2, 0xE1, 0xE0, 0xE0 }; 403*02d1d616SBart Hartgers pci_write_config_byte(pdev, PATA_UDMA_TIMING - adev->devno, 404*02d1d616SBart Hartgers udma_bits[adev->dma_mode - XFER_UDMA_0]); 405d73f30e1SAlan } 406d73f30e1SAlan 407c6fd2807SJeff Garzik static const unsigned int svia_bar_sizes[] = { 408c6fd2807SJeff Garzik 8, 4, 8, 4, 16, 256 409c6fd2807SJeff Garzik }; 410c6fd2807SJeff Garzik 411c6fd2807SJeff Garzik static const unsigned int vt6421_bar_sizes[] = { 412c6fd2807SJeff Garzik 16, 16, 16, 16, 32, 128 413c6fd2807SJeff Garzik }; 414c6fd2807SJeff Garzik 4150d5ff566STejun Heo static void __iomem *svia_scr_addr(void __iomem *addr, unsigned int port) 416c6fd2807SJeff Garzik { 417c6fd2807SJeff Garzik return addr + (port * 128); 418c6fd2807SJeff Garzik } 419c6fd2807SJeff Garzik 4200d5ff566STejun Heo static void __iomem *vt6421_scr_addr(void __iomem *addr, unsigned int port) 421c6fd2807SJeff Garzik { 422c6fd2807SJeff Garzik return addr + (port * 64); 423c6fd2807SJeff Garzik } 424c6fd2807SJeff Garzik 425eca25dcaSTejun Heo static void vt6421_init_addrs(struct ata_port *ap) 426c6fd2807SJeff Garzik { 427eca25dcaSTejun Heo void __iomem * const * iomap = ap->host->iomap; 428eca25dcaSTejun Heo void __iomem *reg_addr = iomap[ap->port_no]; 429eca25dcaSTejun Heo void __iomem *bmdma_addr = iomap[4] + (ap->port_no * 8); 430eca25dcaSTejun Heo struct ata_ioports *ioaddr = &ap->ioaddr; 431c6fd2807SJeff Garzik 432eca25dcaSTejun Heo ioaddr->cmd_addr = reg_addr; 433eca25dcaSTejun Heo ioaddr->altstatus_addr = 434eca25dcaSTejun Heo ioaddr->ctl_addr = (void __iomem *) 4350d5ff566STejun Heo ((unsigned long)(reg_addr + 8) | ATA_PCI_CTL_OFS); 436eca25dcaSTejun Heo ioaddr->bmdma_addr = bmdma_addr; 437eca25dcaSTejun Heo ioaddr->scr_addr = vt6421_scr_addr(iomap[5], ap->port_no); 438c6fd2807SJeff Garzik 4399363c382STejun Heo ata_sff_std_ports(ioaddr); 440cbcdd875STejun Heo 441cbcdd875STejun Heo ata_port_pbar_desc(ap, ap->port_no, -1, "port"); 442cbcdd875STejun Heo ata_port_pbar_desc(ap, 4, ap->port_no * 8, "bmdma"); 443c6fd2807SJeff Garzik } 444c6fd2807SJeff Garzik 445eca25dcaSTejun Heo static int vt6420_prepare_host(struct pci_dev *pdev, struct ata_host **r_host) 446c6fd2807SJeff Garzik { 447eca25dcaSTejun Heo const struct ata_port_info *ppi[] = { &vt6420_port_info, NULL }; 448eca25dcaSTejun Heo struct ata_host *host; 449eca25dcaSTejun Heo int rc; 450c6fd2807SJeff Garzik 4519363c382STejun Heo rc = ata_pci_sff_prepare_host(pdev, ppi, &host); 452eca25dcaSTejun Heo if (rc) 453eca25dcaSTejun Heo return rc; 454eca25dcaSTejun Heo *r_host = host; 455c6fd2807SJeff Garzik 456eca25dcaSTejun Heo rc = pcim_iomap_regions(pdev, 1 << 5, DRV_NAME); 457eca25dcaSTejun Heo if (rc) { 458e1be5d73STejun Heo dev_printk(KERN_ERR, &pdev->dev, "failed to iomap PCI BAR 5\n"); 459eca25dcaSTejun Heo return rc; 460e1be5d73STejun Heo } 461e1be5d73STejun Heo 462eca25dcaSTejun Heo host->ports[0]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 0); 463eca25dcaSTejun Heo host->ports[1]->ioaddr.scr_addr = svia_scr_addr(host->iomap[5], 1); 464c6fd2807SJeff Garzik 465eca25dcaSTejun Heo return 0; 466c6fd2807SJeff Garzik } 467c6fd2807SJeff Garzik 468eca25dcaSTejun Heo static int vt6421_prepare_host(struct pci_dev *pdev, struct ata_host **r_host) 469c6fd2807SJeff Garzik { 470eca25dcaSTejun Heo const struct ata_port_info *ppi[] = 471eca25dcaSTejun Heo { &vt6421_sport_info, &vt6421_sport_info, &vt6421_pport_info }; 472eca25dcaSTejun Heo struct ata_host *host; 473eca25dcaSTejun Heo int i, rc; 474c6fd2807SJeff Garzik 475eca25dcaSTejun Heo *r_host = host = ata_host_alloc_pinfo(&pdev->dev, ppi, ARRAY_SIZE(ppi)); 476eca25dcaSTejun Heo if (!host) { 477eca25dcaSTejun Heo dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n"); 478eca25dcaSTejun Heo return -ENOMEM; 479e1be5d73STejun Heo } 480e1be5d73STejun Heo 4818fd7d1b1STejun Heo rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME); 482eca25dcaSTejun Heo if (rc) { 483eca25dcaSTejun Heo dev_printk(KERN_ERR, &pdev->dev, "failed to request/iomap " 484eca25dcaSTejun Heo "PCI BARs (errno=%d)\n", rc); 485eca25dcaSTejun Heo return rc; 486eca25dcaSTejun Heo } 487eca25dcaSTejun Heo host->iomap = pcim_iomap_table(pdev); 488c6fd2807SJeff Garzik 489eca25dcaSTejun Heo for (i = 0; i < host->n_ports; i++) 490eca25dcaSTejun Heo vt6421_init_addrs(host->ports[i]); 491eca25dcaSTejun Heo 492eca25dcaSTejun Heo rc = pci_set_dma_mask(pdev, ATA_DMA_MASK); 493eca25dcaSTejun Heo if (rc) 494eca25dcaSTejun Heo return rc; 495eca25dcaSTejun Heo rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK); 496eca25dcaSTejun Heo if (rc) 497eca25dcaSTejun Heo return rc; 498eca25dcaSTejun Heo 499eca25dcaSTejun Heo return 0; 500c6fd2807SJeff Garzik } 501c6fd2807SJeff Garzik 502b9d5b89bSTejun Heo static int vt8251_prepare_host(struct pci_dev *pdev, struct ata_host **r_host) 503b9d5b89bSTejun Heo { 504b9d5b89bSTejun Heo const struct ata_port_info *ppi[] = { &vt8251_port_info, NULL }; 505b9d5b89bSTejun Heo struct ata_host *host; 506b9d5b89bSTejun Heo int i, rc; 507b9d5b89bSTejun Heo 508b9d5b89bSTejun Heo rc = ata_pci_sff_prepare_host(pdev, ppi, &host); 509b9d5b89bSTejun Heo if (rc) 510b9d5b89bSTejun Heo return rc; 511b9d5b89bSTejun Heo *r_host = host; 512b9d5b89bSTejun Heo 513b9d5b89bSTejun Heo rc = pcim_iomap_regions(pdev, 1 << 5, DRV_NAME); 514b9d5b89bSTejun Heo if (rc) { 515b9d5b89bSTejun Heo dev_printk(KERN_ERR, &pdev->dev, "failed to iomap PCI BAR 5\n"); 516b9d5b89bSTejun Heo return rc; 517b9d5b89bSTejun Heo } 518b9d5b89bSTejun Heo 519b9d5b89bSTejun Heo /* 8251 hosts four sata ports as M/S of the two channels */ 520b9d5b89bSTejun Heo for (i = 0; i < host->n_ports; i++) 521b9d5b89bSTejun Heo ata_slave_link_init(host->ports[i]); 522b9d5b89bSTejun Heo 523b9d5b89bSTejun Heo return 0; 524b9d5b89bSTejun Heo } 525b9d5b89bSTejun Heo 526c6fd2807SJeff Garzik static void svia_configure(struct pci_dev *pdev) 527c6fd2807SJeff Garzik { 528c6fd2807SJeff Garzik u8 tmp8; 529c6fd2807SJeff Garzik 530c6fd2807SJeff Garzik pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, &tmp8); 531c6fd2807SJeff Garzik dev_printk(KERN_INFO, &pdev->dev, "routed to hard irq line %d\n", 532c6fd2807SJeff Garzik (int) (tmp8 & 0xf0) == 0xf0 ? 0 : tmp8 & 0x0f); 533c6fd2807SJeff Garzik 534c6fd2807SJeff Garzik /* make sure SATA channels are enabled */ 535c6fd2807SJeff Garzik pci_read_config_byte(pdev, SATA_CHAN_ENAB, &tmp8); 536c6fd2807SJeff Garzik if ((tmp8 & ALL_PORTS) != ALL_PORTS) { 537c6fd2807SJeff Garzik dev_printk(KERN_DEBUG, &pdev->dev, 538c6fd2807SJeff Garzik "enabling SATA channels (0x%x)\n", 539c6fd2807SJeff Garzik (int) tmp8); 540c6fd2807SJeff Garzik tmp8 |= ALL_PORTS; 541c6fd2807SJeff Garzik pci_write_config_byte(pdev, SATA_CHAN_ENAB, tmp8); 542c6fd2807SJeff Garzik } 543c6fd2807SJeff Garzik 544c6fd2807SJeff Garzik /* make sure interrupts for each channel sent to us */ 545c6fd2807SJeff Garzik pci_read_config_byte(pdev, SATA_INT_GATE, &tmp8); 546c6fd2807SJeff Garzik if ((tmp8 & ALL_PORTS) != ALL_PORTS) { 547c6fd2807SJeff Garzik dev_printk(KERN_DEBUG, &pdev->dev, 548c6fd2807SJeff Garzik "enabling SATA channel interrupts (0x%x)\n", 549c6fd2807SJeff Garzik (int) tmp8); 550c6fd2807SJeff Garzik tmp8 |= ALL_PORTS; 551c6fd2807SJeff Garzik pci_write_config_byte(pdev, SATA_INT_GATE, tmp8); 552c6fd2807SJeff Garzik } 553c6fd2807SJeff Garzik 554c6fd2807SJeff Garzik /* make sure native mode is enabled */ 555c6fd2807SJeff Garzik pci_read_config_byte(pdev, SATA_NATIVE_MODE, &tmp8); 556c6fd2807SJeff Garzik if ((tmp8 & NATIVE_MODE_ALL) != NATIVE_MODE_ALL) { 557c6fd2807SJeff Garzik dev_printk(KERN_DEBUG, &pdev->dev, 558c6fd2807SJeff Garzik "enabling SATA channel native mode (0x%x)\n", 559c6fd2807SJeff Garzik (int) tmp8); 560c6fd2807SJeff Garzik tmp8 |= NATIVE_MODE_ALL; 561c6fd2807SJeff Garzik pci_write_config_byte(pdev, SATA_NATIVE_MODE, tmp8); 562c6fd2807SJeff Garzik } 563c6fd2807SJeff Garzik } 564c6fd2807SJeff Garzik 565c6fd2807SJeff Garzik static int svia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 566c6fd2807SJeff Garzik { 567c6fd2807SJeff Garzik static int printed_version; 568c6fd2807SJeff Garzik unsigned int i; 569c6fd2807SJeff Garzik int rc; 570f1c22943SJeff Garzik struct ata_host *host = NULL; 571c6fd2807SJeff Garzik int board_id = (int) ent->driver_data; 572b4482a4bSAl Viro const unsigned *bar_sizes; 573c6fd2807SJeff Garzik 574c6fd2807SJeff Garzik if (!printed_version++) 575c6fd2807SJeff Garzik dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n"); 576c6fd2807SJeff Garzik 57724dc5f33STejun Heo rc = pcim_enable_device(pdev); 578c6fd2807SJeff Garzik if (rc) 579c6fd2807SJeff Garzik return rc; 580c6fd2807SJeff Garzik 581b9d5b89bSTejun Heo if (board_id == vt6421) 582c6fd2807SJeff Garzik bar_sizes = &vt6421_bar_sizes[0]; 583b9d5b89bSTejun Heo else 584b9d5b89bSTejun Heo bar_sizes = &svia_bar_sizes[0]; 585c6fd2807SJeff Garzik 586c6fd2807SJeff Garzik for (i = 0; i < ARRAY_SIZE(svia_bar_sizes); i++) 587c6fd2807SJeff Garzik if ((pci_resource_start(pdev, i) == 0) || 588c6fd2807SJeff Garzik (pci_resource_len(pdev, i) < bar_sizes[i])) { 589c6fd2807SJeff Garzik dev_printk(KERN_ERR, &pdev->dev, 590c6fd2807SJeff Garzik "invalid PCI BAR %u (sz 0x%llx, val 0x%llx)\n", 591c6fd2807SJeff Garzik i, 592c6fd2807SJeff Garzik (unsigned long long)pci_resource_start(pdev, i), 593c6fd2807SJeff Garzik (unsigned long long)pci_resource_len(pdev, i)); 59424dc5f33STejun Heo return -ENODEV; 595c6fd2807SJeff Garzik } 596c6fd2807SJeff Garzik 597b9d5b89bSTejun Heo switch (board_id) { 598b9d5b89bSTejun Heo case vt6420: 599eca25dcaSTejun Heo rc = vt6420_prepare_host(pdev, &host); 600b9d5b89bSTejun Heo break; 601b9d5b89bSTejun Heo case vt6421: 602eca25dcaSTejun Heo rc = vt6421_prepare_host(pdev, &host); 603b9d5b89bSTejun Heo break; 604b9d5b89bSTejun Heo case vt8251: 605b9d5b89bSTejun Heo rc = vt8251_prepare_host(pdev, &host); 606b9d5b89bSTejun Heo break; 607b9d5b89bSTejun Heo default: 608554d491dSMarcin Slusarz rc = -EINVAL; 609b9d5b89bSTejun Heo } 610554d491dSMarcin Slusarz if (rc) 611554d491dSMarcin Slusarz return rc; 612c6fd2807SJeff Garzik 613c6fd2807SJeff Garzik svia_configure(pdev); 614c6fd2807SJeff Garzik 615c6fd2807SJeff Garzik pci_set_master(pdev); 6169363c382STejun Heo return ata_host_activate(host, pdev->irq, ata_sff_interrupt, 6179363c382STejun Heo IRQF_SHARED, &svia_sht); 618c6fd2807SJeff Garzik } 619c6fd2807SJeff Garzik 620c6fd2807SJeff Garzik static int __init svia_init(void) 621c6fd2807SJeff Garzik { 622c6fd2807SJeff Garzik return pci_register_driver(&svia_pci_driver); 623c6fd2807SJeff Garzik } 624c6fd2807SJeff Garzik 625c6fd2807SJeff Garzik static void __exit svia_exit(void) 626c6fd2807SJeff Garzik { 627c6fd2807SJeff Garzik pci_unregister_driver(&svia_pci_driver); 628c6fd2807SJeff Garzik } 629c6fd2807SJeff Garzik 630c6fd2807SJeff Garzik module_init(svia_init); 631c6fd2807SJeff Garzik module_exit(svia_exit); 632