1c6fd2807SJeff Garzik /* 2c6fd2807SJeff Garzik * sata_mv.c - Marvell SATA support 3c6fd2807SJeff Garzik * 440f21b11SMark Lord * Copyright 2008-2009: Marvell Corporation, all rights reserved. 5c6fd2807SJeff Garzik * Copyright 2005: EMC Corporation, all rights reserved. 6c6fd2807SJeff Garzik * Copyright 2005 Red Hat, Inc. All rights reserved. 7c6fd2807SJeff Garzik * 840f21b11SMark Lord * Originally written by Brett Russ. 940f21b11SMark Lord * Extensive overhaul and enhancement by Mark Lord <mlord@pobox.com>. 1040f21b11SMark Lord * 11c6fd2807SJeff Garzik * Please ALWAYS copy linux-ide@vger.kernel.org on emails. 12c6fd2807SJeff Garzik * 13c6fd2807SJeff Garzik * This program is free software; you can redistribute it and/or modify 14c6fd2807SJeff Garzik * it under the terms of the GNU General Public License as published by 15c6fd2807SJeff Garzik * the Free Software Foundation; version 2 of the License. 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; if not, write to the Free Software 24c6fd2807SJeff Garzik * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25c6fd2807SJeff Garzik * 26c6fd2807SJeff Garzik */ 27c6fd2807SJeff Garzik 284a05e209SJeff Garzik /* 2985afb934SMark Lord * sata_mv TODO list: 3085afb934SMark Lord * 3185afb934SMark Lord * --> Develop a low-power-consumption strategy, and implement it. 3285afb934SMark Lord * 332b748a0aSMark Lord * --> Add sysfs attributes for per-chip / per-HC IRQ coalescing thresholds. 3485afb934SMark Lord * 3585afb934SMark Lord * --> [Experiment, Marvell value added] Is it possible to use target 3685afb934SMark Lord * mode to cross-connect two Linux boxes with Marvell cards? If so, 3785afb934SMark Lord * creating LibATA target mode support would be very interesting. 3885afb934SMark Lord * 3985afb934SMark Lord * Target mode, for those without docs, is the ability to directly 4085afb934SMark Lord * connect two SATA ports. 414a05e209SJeff Garzik */ 424a05e209SJeff Garzik 4365ad7fefSMark Lord /* 4465ad7fefSMark Lord * 80x1-B2 errata PCI#11: 4565ad7fefSMark Lord * 4665ad7fefSMark Lord * Users of the 6041/6081 Rev.B2 chips (current is C0) 4765ad7fefSMark Lord * should be careful to insert those cards only onto PCI-X bus #0, 4865ad7fefSMark Lord * and only in device slots 0..7, not higher. The chips may not 4965ad7fefSMark Lord * work correctly otherwise (note: this is a pretty rare condition). 5065ad7fefSMark Lord */ 5165ad7fefSMark Lord 52c6fd2807SJeff Garzik #include <linux/kernel.h> 53c6fd2807SJeff Garzik #include <linux/module.h> 54c6fd2807SJeff Garzik #include <linux/pci.h> 55c6fd2807SJeff Garzik #include <linux/init.h> 56c6fd2807SJeff Garzik #include <linux/blkdev.h> 57c6fd2807SJeff Garzik #include <linux/delay.h> 58c6fd2807SJeff Garzik #include <linux/interrupt.h> 598d8b6004SAndrew Morton #include <linux/dmapool.h> 60c6fd2807SJeff Garzik #include <linux/dma-mapping.h> 61c6fd2807SJeff Garzik #include <linux/device.h> 62c77a2f4eSSaeed Bishara #include <linux/clk.h> 63f351b2d6SSaeed Bishara #include <linux/platform_device.h> 64f351b2d6SSaeed Bishara #include <linux/ata_platform.h> 6515a32632SLennert Buytenhek #include <linux/mbus.h> 66c46938ccSMark Lord #include <linux/bitops.h> 675a0e3ad6STejun Heo #include <linux/gfp.h> 68c6fd2807SJeff Garzik #include <scsi/scsi_host.h> 69c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h> 706c08772eSJeff Garzik #include <scsi/scsi_device.h> 71c6fd2807SJeff Garzik #include <linux/libata.h> 72c6fd2807SJeff Garzik 73c6fd2807SJeff Garzik #define DRV_NAME "sata_mv" 74cae5a29dSMark Lord #define DRV_VERSION "1.28" 75c6fd2807SJeff Garzik 7640f21b11SMark Lord /* 7740f21b11SMark Lord * module options 7840f21b11SMark Lord */ 7940f21b11SMark Lord 8040f21b11SMark Lord static int msi; 8140f21b11SMark Lord #ifdef CONFIG_PCI 8240f21b11SMark Lord module_param(msi, int, S_IRUGO); 8340f21b11SMark Lord MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)"); 8440f21b11SMark Lord #endif 8540f21b11SMark Lord 862b748a0aSMark Lord static int irq_coalescing_io_count; 872b748a0aSMark Lord module_param(irq_coalescing_io_count, int, S_IRUGO); 882b748a0aSMark Lord MODULE_PARM_DESC(irq_coalescing_io_count, 892b748a0aSMark Lord "IRQ coalescing I/O count threshold (0..255)"); 902b748a0aSMark Lord 912b748a0aSMark Lord static int irq_coalescing_usecs; 922b748a0aSMark Lord module_param(irq_coalescing_usecs, int, S_IRUGO); 932b748a0aSMark Lord MODULE_PARM_DESC(irq_coalescing_usecs, 942b748a0aSMark Lord "IRQ coalescing time threshold in usecs"); 952b748a0aSMark Lord 96c6fd2807SJeff Garzik enum { 97c6fd2807SJeff Garzik /* BAR's are enumerated in terms of pci_resource_start() terms */ 98c6fd2807SJeff Garzik MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */ 99c6fd2807SJeff Garzik MV_IO_BAR = 2, /* offset 0x18: IO space */ 100c6fd2807SJeff Garzik MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */ 101c6fd2807SJeff Garzik 102c6fd2807SJeff Garzik MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */ 103c6fd2807SJeff Garzik MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */ 104c6fd2807SJeff Garzik 1052b748a0aSMark Lord /* For use with both IRQ coalescing methods ("all ports" or "per-HC" */ 1062b748a0aSMark Lord COAL_CLOCKS_PER_USEC = 150, /* for calculating COAL_TIMEs */ 1072b748a0aSMark Lord MAX_COAL_TIME_THRESHOLD = ((1 << 24) - 1), /* internal clocks count */ 1082b748a0aSMark Lord MAX_COAL_IO_COUNT = 255, /* completed I/O count */ 1092b748a0aSMark Lord 110c6fd2807SJeff Garzik MV_PCI_REG_BASE = 0, 111c6fd2807SJeff Garzik 1122b748a0aSMark Lord /* 1132b748a0aSMark Lord * Per-chip ("all ports") interrupt coalescing feature. 1142b748a0aSMark Lord * This is only for GEN_II / GEN_IIE hardware. 1152b748a0aSMark Lord * 1162b748a0aSMark Lord * Coalescing defers the interrupt until either the IO_THRESHOLD 1172b748a0aSMark Lord * (count of completed I/Os) is met, or the TIME_THRESHOLD is met. 1182b748a0aSMark Lord */ 119cae5a29dSMark Lord COAL_REG_BASE = 0x18000, 120cae5a29dSMark Lord IRQ_COAL_CAUSE = (COAL_REG_BASE + 0x08), 1212b748a0aSMark Lord ALL_PORTS_COAL_IRQ = (1 << 4), /* all ports irq event */ 1222b748a0aSMark Lord 123cae5a29dSMark Lord IRQ_COAL_IO_THRESHOLD = (COAL_REG_BASE + 0xcc), 124cae5a29dSMark Lord IRQ_COAL_TIME_THRESHOLD = (COAL_REG_BASE + 0xd0), 1252b748a0aSMark Lord 1262b748a0aSMark Lord /* 1272b748a0aSMark Lord * Registers for the (unused here) transaction coalescing feature: 1282b748a0aSMark Lord */ 129cae5a29dSMark Lord TRAN_COAL_CAUSE_LO = (COAL_REG_BASE + 0x88), 130cae5a29dSMark Lord TRAN_COAL_CAUSE_HI = (COAL_REG_BASE + 0x8c), 1312b748a0aSMark Lord 132cae5a29dSMark Lord SATAHC0_REG_BASE = 0x20000, 133cae5a29dSMark Lord FLASH_CTL = 0x1046c, 134cae5a29dSMark Lord GPIO_PORT_CTL = 0x104f0, 135cae5a29dSMark Lord RESET_CFG = 0x180d8, 136c6fd2807SJeff Garzik 137c6fd2807SJeff Garzik MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ, 138c6fd2807SJeff Garzik MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ, 139c6fd2807SJeff Garzik MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */ 140c6fd2807SJeff Garzik MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ, 141c6fd2807SJeff Garzik 142c6fd2807SJeff Garzik MV_MAX_Q_DEPTH = 32, 143c6fd2807SJeff Garzik MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1, 144c6fd2807SJeff Garzik 145c6fd2807SJeff Garzik /* CRQB needs alignment on a 1KB boundary. Size == 1KB 146c6fd2807SJeff Garzik * CRPB needs alignment on a 256B boundary. Size == 256B 147c6fd2807SJeff Garzik * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B 148c6fd2807SJeff Garzik */ 149c6fd2807SJeff Garzik MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH), 150c6fd2807SJeff Garzik MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH), 151da2fa9baSMark Lord MV_MAX_SG_CT = 256, 152c6fd2807SJeff Garzik MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT), 153c6fd2807SJeff Garzik 154352fab70SMark Lord /* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */ 155c6fd2807SJeff Garzik MV_PORT_HC_SHIFT = 2, 156352fab70SMark Lord MV_PORTS_PER_HC = (1 << MV_PORT_HC_SHIFT), /* 4 */ 157352fab70SMark Lord /* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */ 158352fab70SMark Lord MV_PORT_MASK = (MV_PORTS_PER_HC - 1), /* 3 */ 159c6fd2807SJeff Garzik 160c6fd2807SJeff Garzik /* Host Flags */ 161c6fd2807SJeff Garzik MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */ 1627bb3c529SSaeed Bishara 1639cbe056fSSergei Shtylyov MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_PIO_POLLING, 164ad3aef51SMark Lord 16591b1a84cSMark Lord MV_GEN_I_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI, 166c6fd2807SJeff Garzik 16740f21b11SMark Lord MV_GEN_II_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NCQ | 16840f21b11SMark Lord ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA, 16991b1a84cSMark Lord 17091b1a84cSMark Lord MV_GEN_IIE_FLAGS = MV_GEN_II_FLAGS | ATA_FLAG_AN, 171ad3aef51SMark Lord 172c6fd2807SJeff Garzik CRQB_FLAG_READ = (1 << 0), 173c6fd2807SJeff Garzik CRQB_TAG_SHIFT = 1, 174c5d3e45aSJeff Garzik CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */ 175e12bef50SMark Lord CRQB_PMP_SHIFT = 12, /* CRQB Gen-II/IIE PMP shift */ 176c5d3e45aSJeff Garzik CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */ 177c6fd2807SJeff Garzik CRQB_CMD_ADDR_SHIFT = 8, 178c6fd2807SJeff Garzik CRQB_CMD_CS = (0x2 << 11), 179c6fd2807SJeff Garzik CRQB_CMD_LAST = (1 << 15), 180c6fd2807SJeff Garzik 181c6fd2807SJeff Garzik CRPB_FLAG_STATUS_SHIFT = 8, 182c5d3e45aSJeff Garzik CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */ 183c5d3e45aSJeff Garzik CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */ 184c6fd2807SJeff Garzik 185c6fd2807SJeff Garzik EPRD_FLAG_END_OF_TBL = (1 << 31), 186c6fd2807SJeff Garzik 187c6fd2807SJeff Garzik /* PCI interface registers */ 188c6fd2807SJeff Garzik 189cae5a29dSMark Lord MV_PCI_COMMAND = 0xc00, 190cae5a29dSMark Lord MV_PCI_COMMAND_MWRCOM = (1 << 4), /* PCI Master Write Combining */ 191cae5a29dSMark Lord MV_PCI_COMMAND_MRDTRIG = (1 << 7), /* PCI Master Read Trigger */ 192c6fd2807SJeff Garzik 193cae5a29dSMark Lord PCI_MAIN_CMD_STS = 0xd30, 194c6fd2807SJeff Garzik STOP_PCI_MASTER = (1 << 2), 195c6fd2807SJeff Garzik PCI_MASTER_EMPTY = (1 << 3), 196c6fd2807SJeff Garzik GLOB_SFT_RST = (1 << 4), 197c6fd2807SJeff Garzik 198cae5a29dSMark Lord MV_PCI_MODE = 0xd00, 1998e7decdbSMark Lord MV_PCI_MODE_MASK = 0x30, 2008e7decdbSMark Lord 201c6fd2807SJeff Garzik MV_PCI_EXP_ROM_BAR_CTL = 0xd2c, 202c6fd2807SJeff Garzik MV_PCI_DISC_TIMER = 0xd04, 203c6fd2807SJeff Garzik MV_PCI_MSI_TRIGGER = 0xc38, 204c6fd2807SJeff Garzik MV_PCI_SERR_MASK = 0xc28, 205cae5a29dSMark Lord MV_PCI_XBAR_TMOUT = 0x1d04, 206c6fd2807SJeff Garzik MV_PCI_ERR_LOW_ADDRESS = 0x1d40, 207c6fd2807SJeff Garzik MV_PCI_ERR_HIGH_ADDRESS = 0x1d44, 208c6fd2807SJeff Garzik MV_PCI_ERR_ATTRIBUTE = 0x1d48, 209c6fd2807SJeff Garzik MV_PCI_ERR_COMMAND = 0x1d50, 210c6fd2807SJeff Garzik 211cae5a29dSMark Lord PCI_IRQ_CAUSE = 0x1d58, 212cae5a29dSMark Lord PCI_IRQ_MASK = 0x1d5c, 213c6fd2807SJeff Garzik PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */ 214c6fd2807SJeff Garzik 215cae5a29dSMark Lord PCIE_IRQ_CAUSE = 0x1900, 216cae5a29dSMark Lord PCIE_IRQ_MASK = 0x1910, 217646a4da5SMark Lord PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */ 21802a121daSMark Lord 2197368f919SMark Lord /* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */ 220cae5a29dSMark Lord PCI_HC_MAIN_IRQ_CAUSE = 0x1d60, 221cae5a29dSMark Lord PCI_HC_MAIN_IRQ_MASK = 0x1d64, 222cae5a29dSMark Lord SOC_HC_MAIN_IRQ_CAUSE = 0x20020, 223cae5a29dSMark Lord SOC_HC_MAIN_IRQ_MASK = 0x20024, 22440f21b11SMark Lord ERR_IRQ = (1 << 0), /* shift by (2 * port #) */ 22540f21b11SMark Lord DONE_IRQ = (1 << 1), /* shift by (2 * port #) */ 226c6fd2807SJeff Garzik HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */ 227c6fd2807SJeff Garzik HC_SHIFT = 9, /* bits 9-17 = HC1's ports */ 2282b748a0aSMark Lord DONE_IRQ_0_3 = 0x000000aa, /* DONE_IRQ ports 0,1,2,3 */ 2292b748a0aSMark Lord DONE_IRQ_4_7 = (DONE_IRQ_0_3 << HC_SHIFT), /* 4,5,6,7 */ 230c6fd2807SJeff Garzik PCI_ERR = (1 << 18), 23140f21b11SMark Lord TRAN_COAL_LO_DONE = (1 << 19), /* transaction coalescing */ 23240f21b11SMark Lord TRAN_COAL_HI_DONE = (1 << 20), /* transaction coalescing */ 23340f21b11SMark Lord PORTS_0_3_COAL_DONE = (1 << 8), /* HC0 IRQ coalescing */ 23440f21b11SMark Lord PORTS_4_7_COAL_DONE = (1 << 17), /* HC1 IRQ coalescing */ 23540f21b11SMark Lord ALL_PORTS_COAL_DONE = (1 << 21), /* GEN_II(E) IRQ coalescing */ 236c6fd2807SJeff Garzik GPIO_INT = (1 << 22), 237c6fd2807SJeff Garzik SELF_INT = (1 << 23), 238c6fd2807SJeff Garzik TWSI_INT = (1 << 24), 239c6fd2807SJeff Garzik HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */ 240fb621e2fSJeff Garzik HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */ 241f351b2d6SSaeed Bishara HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */ 242c6fd2807SJeff Garzik 243c6fd2807SJeff Garzik /* SATAHC registers */ 244cae5a29dSMark Lord HC_CFG = 0x00, 245c6fd2807SJeff Garzik 246cae5a29dSMark Lord HC_IRQ_CAUSE = 0x14, 247352fab70SMark Lord DMA_IRQ = (1 << 0), /* shift by port # */ 248352fab70SMark Lord HC_COAL_IRQ = (1 << 4), /* IRQ coalescing */ 249c6fd2807SJeff Garzik DEV_IRQ = (1 << 8), /* shift by port # */ 250c6fd2807SJeff Garzik 2512b748a0aSMark Lord /* 2522b748a0aSMark Lord * Per-HC (Host-Controller) interrupt coalescing feature. 2532b748a0aSMark Lord * This is present on all chip generations. 2542b748a0aSMark Lord * 2552b748a0aSMark Lord * Coalescing defers the interrupt until either the IO_THRESHOLD 2562b748a0aSMark Lord * (count of completed I/Os) is met, or the TIME_THRESHOLD is met. 2572b748a0aSMark Lord */ 258cae5a29dSMark Lord HC_IRQ_COAL_IO_THRESHOLD = 0x000c, 259cae5a29dSMark Lord HC_IRQ_COAL_TIME_THRESHOLD = 0x0010, 2602b748a0aSMark Lord 261cae5a29dSMark Lord SOC_LED_CTRL = 0x2c, 262000b344fSMark Lord SOC_LED_CTRL_BLINK = (1 << 0), /* Active LED blink */ 263000b344fSMark Lord SOC_LED_CTRL_ACT_PRESENCE = (1 << 2), /* Multiplex dev presence */ 264000b344fSMark Lord /* with dev activity LED */ 265000b344fSMark Lord 266c6fd2807SJeff Garzik /* Shadow block registers */ 267cae5a29dSMark Lord SHD_BLK = 0x100, 268cae5a29dSMark Lord SHD_CTL_AST = 0x20, /* ofs from SHD_BLK */ 269c6fd2807SJeff Garzik 270c6fd2807SJeff Garzik /* SATA registers */ 271cae5a29dSMark Lord SATA_STATUS = 0x300, /* ctrl, err regs follow status */ 272cae5a29dSMark Lord SATA_ACTIVE = 0x350, 273cae5a29dSMark Lord FIS_IRQ_CAUSE = 0x364, 274cae5a29dSMark Lord FIS_IRQ_CAUSE_AN = (1 << 9), /* async notification */ 27517c5aab5SMark Lord 276cae5a29dSMark Lord LTMODE = 0x30c, /* requires read-after-write */ 27717c5aab5SMark Lord LTMODE_BIT8 = (1 << 8), /* unknown, but necessary */ 27817c5aab5SMark Lord 279cae5a29dSMark Lord PHY_MODE2 = 0x330, 280c6fd2807SJeff Garzik PHY_MODE3 = 0x310, 281cae5a29dSMark Lord 282cae5a29dSMark Lord PHY_MODE4 = 0x314, /* requires read-after-write */ 283ba069e37SMark Lord PHY_MODE4_CFG_MASK = 0x00000003, /* phy internal config field */ 284ba069e37SMark Lord PHY_MODE4_CFG_VALUE = 0x00000001, /* phy internal config field */ 285ba069e37SMark Lord PHY_MODE4_RSVD_ZEROS = 0x5de3fffa, /* Gen2e always write zeros */ 286ba069e37SMark Lord PHY_MODE4_RSVD_ONES = 0x00000005, /* Gen2e always write ones */ 287ba069e37SMark Lord 288cae5a29dSMark Lord SATA_IFCTL = 0x344, 289cae5a29dSMark Lord SATA_TESTCTL = 0x348, 290cae5a29dSMark Lord SATA_IFSTAT = 0x34c, 291cae5a29dSMark Lord VENDOR_UNIQUE_FIS = 0x35c, 29217c5aab5SMark Lord 293cae5a29dSMark Lord FISCFG = 0x360, 2948e7decdbSMark Lord FISCFG_WAIT_DEV_ERR = (1 << 8), /* wait for host on DevErr */ 2958e7decdbSMark Lord FISCFG_SINGLE_SYNC = (1 << 16), /* SYNC on DMA activation */ 29617c5aab5SMark Lord 29729b7e43cSMartin Michlmayr PHY_MODE9_GEN2 = 0x398, 29829b7e43cSMartin Michlmayr PHY_MODE9_GEN1 = 0x39c, 29929b7e43cSMartin Michlmayr PHYCFG_OFS = 0x3a0, /* only in 65n devices */ 30029b7e43cSMartin Michlmayr 301c6fd2807SJeff Garzik MV5_PHY_MODE = 0x74, 302cae5a29dSMark Lord MV5_LTMODE = 0x30, 303cae5a29dSMark Lord MV5_PHY_CTL = 0x0C, 304cae5a29dSMark Lord SATA_IFCFG = 0x050, 305c6fd2807SJeff Garzik 306c6fd2807SJeff Garzik MV_M2_PREAMP_MASK = 0x7e0, 307c6fd2807SJeff Garzik 308c6fd2807SJeff Garzik /* Port registers */ 309cae5a29dSMark Lord EDMA_CFG = 0, 3100c58912eSMark Lord EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */ 3110c58912eSMark Lord EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */ 312c6fd2807SJeff Garzik EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */ 313c6fd2807SJeff Garzik EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */ 314c6fd2807SJeff Garzik EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */ 315e12bef50SMark Lord EDMA_CFG_EDMA_FBS = (1 << 16), /* EDMA FIS-Based Switching */ 316e12bef50SMark Lord EDMA_CFG_FBS = (1 << 26), /* FIS-Based Switching */ 317c6fd2807SJeff Garzik 318cae5a29dSMark Lord EDMA_ERR_IRQ_CAUSE = 0x8, 319cae5a29dSMark Lord EDMA_ERR_IRQ_MASK = 0xc, 3206c1153e0SJeff Garzik EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */ 3216c1153e0SJeff Garzik EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */ 3226c1153e0SJeff Garzik EDMA_ERR_DEV = (1 << 2), /* device error */ 3236c1153e0SJeff Garzik EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */ 3246c1153e0SJeff Garzik EDMA_ERR_DEV_CON = (1 << 4), /* device connected */ 3256c1153e0SJeff Garzik EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */ 326c5d3e45aSJeff Garzik EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */ 327c5d3e45aSJeff Garzik EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */ 3286c1153e0SJeff Garzik EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */ 329c5d3e45aSJeff Garzik EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */ 3306c1153e0SJeff Garzik EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */ 3316c1153e0SJeff Garzik EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */ 3326c1153e0SJeff Garzik EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */ 3336c1153e0SJeff Garzik EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */ 334646a4da5SMark Lord 3356c1153e0SJeff Garzik EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */ 336646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */ 337646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */ 338646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */ 339646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */ 340646a4da5SMark Lord 3416c1153e0SJeff Garzik EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */ 342646a4da5SMark Lord 3436c1153e0SJeff Garzik EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */ 344646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */ 345646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */ 346646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */ 347646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */ 348646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */ 349646a4da5SMark Lord 3506c1153e0SJeff Garzik EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */ 351646a4da5SMark Lord 3526c1153e0SJeff Garzik EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */ 353c5d3e45aSJeff Garzik EDMA_ERR_OVERRUN_5 = (1 << 5), 354c5d3e45aSJeff Garzik EDMA_ERR_UNDERRUN_5 = (1 << 6), 355646a4da5SMark Lord 356646a4da5SMark Lord EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 | 357646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_1 | 358646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_3 | 35985afb934SMark Lord EDMA_ERR_LNK_CTRL_TX, 360646a4da5SMark Lord 361bdd4dddeSJeff Garzik EDMA_EH_FREEZE = EDMA_ERR_D_PAR | 362bdd4dddeSJeff Garzik EDMA_ERR_PRD_PAR | 363bdd4dddeSJeff Garzik EDMA_ERR_DEV_DCON | 364bdd4dddeSJeff Garzik EDMA_ERR_DEV_CON | 365bdd4dddeSJeff Garzik EDMA_ERR_SERR | 366bdd4dddeSJeff Garzik EDMA_ERR_SELF_DIS | 3676c1153e0SJeff Garzik EDMA_ERR_CRQB_PAR | 368bdd4dddeSJeff Garzik EDMA_ERR_CRPB_PAR | 369bdd4dddeSJeff Garzik EDMA_ERR_INTRL_PAR | 370bdd4dddeSJeff Garzik EDMA_ERR_IORDY | 371bdd4dddeSJeff Garzik EDMA_ERR_LNK_CTRL_RX_2 | 372c6fd2807SJeff Garzik EDMA_ERR_LNK_DATA_RX | 373c6fd2807SJeff Garzik EDMA_ERR_LNK_DATA_TX | 374bdd4dddeSJeff Garzik EDMA_ERR_TRANS_PROTO, 375e12bef50SMark Lord 376bdd4dddeSJeff Garzik EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR | 377bdd4dddeSJeff Garzik EDMA_ERR_PRD_PAR | 378bdd4dddeSJeff Garzik EDMA_ERR_DEV_DCON | 379bdd4dddeSJeff Garzik EDMA_ERR_DEV_CON | 380bdd4dddeSJeff Garzik EDMA_ERR_OVERRUN_5 | 381bdd4dddeSJeff Garzik EDMA_ERR_UNDERRUN_5 | 382bdd4dddeSJeff Garzik EDMA_ERR_SELF_DIS_5 | 3836c1153e0SJeff Garzik EDMA_ERR_CRQB_PAR | 384bdd4dddeSJeff Garzik EDMA_ERR_CRPB_PAR | 385bdd4dddeSJeff Garzik EDMA_ERR_INTRL_PAR | 386bdd4dddeSJeff Garzik EDMA_ERR_IORDY, 387c6fd2807SJeff Garzik 388cae5a29dSMark Lord EDMA_REQ_Q_BASE_HI = 0x10, 389cae5a29dSMark Lord EDMA_REQ_Q_IN_PTR = 0x14, /* also contains BASE_LO */ 390c6fd2807SJeff Garzik 391cae5a29dSMark Lord EDMA_REQ_Q_OUT_PTR = 0x18, 392c6fd2807SJeff Garzik EDMA_REQ_Q_PTR_SHIFT = 5, 393c6fd2807SJeff Garzik 394cae5a29dSMark Lord EDMA_RSP_Q_BASE_HI = 0x1c, 395cae5a29dSMark Lord EDMA_RSP_Q_IN_PTR = 0x20, 396cae5a29dSMark Lord EDMA_RSP_Q_OUT_PTR = 0x24, /* also contains BASE_LO */ 397c6fd2807SJeff Garzik EDMA_RSP_Q_PTR_SHIFT = 3, 398c6fd2807SJeff Garzik 399cae5a29dSMark Lord EDMA_CMD = 0x28, /* EDMA command register */ 4000ea9e179SJeff Garzik EDMA_EN = (1 << 0), /* enable EDMA */ 4010ea9e179SJeff Garzik EDMA_DS = (1 << 1), /* disable EDMA; self-negated */ 4028e7decdbSMark Lord EDMA_RESET = (1 << 2), /* reset eng/trans/link/phy */ 403c6fd2807SJeff Garzik 404cae5a29dSMark Lord EDMA_STATUS = 0x30, /* EDMA engine status */ 4058e7decdbSMark Lord EDMA_STATUS_CACHE_EMPTY = (1 << 6), /* GenIIe command cache empty */ 4068e7decdbSMark Lord EDMA_STATUS_IDLE = (1 << 7), /* GenIIe EDMA enabled/idle */ 4078e7decdbSMark Lord 408cae5a29dSMark Lord EDMA_IORDY_TMOUT = 0x34, 409cae5a29dSMark Lord EDMA_ARB_CFG = 0x38, 4108e7decdbSMark Lord 411cae5a29dSMark Lord EDMA_HALTCOND = 0x60, /* GenIIe halt conditions */ 412cae5a29dSMark Lord EDMA_UNKNOWN_RSVD = 0x6C, /* GenIIe unknown/reserved */ 413da14265eSMark Lord 414cae5a29dSMark Lord BMDMA_CMD = 0x224, /* bmdma command register */ 415cae5a29dSMark Lord BMDMA_STATUS = 0x228, /* bmdma status register */ 416cae5a29dSMark Lord BMDMA_PRD_LOW = 0x22c, /* bmdma PRD addr 31:0 */ 417cae5a29dSMark Lord BMDMA_PRD_HIGH = 0x230, /* bmdma PRD addr 63:32 */ 418da14265eSMark Lord 419c6fd2807SJeff Garzik /* Host private flags (hp_flags) */ 420c6fd2807SJeff Garzik MV_HP_FLAG_MSI = (1 << 0), 421c6fd2807SJeff Garzik MV_HP_ERRATA_50XXB0 = (1 << 1), 422c6fd2807SJeff Garzik MV_HP_ERRATA_50XXB2 = (1 << 2), 423c6fd2807SJeff Garzik MV_HP_ERRATA_60X1B2 = (1 << 3), 424c6fd2807SJeff Garzik MV_HP_ERRATA_60X1C0 = (1 << 4), 4250ea9e179SJeff Garzik MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */ 4260ea9e179SJeff Garzik MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */ 4270ea9e179SJeff Garzik MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */ 42802a121daSMark Lord MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */ 429616d4a98SMark Lord MV_HP_CUT_THROUGH = (1 << 10), /* can use EDMA cut-through */ 4301f398472SMark Lord MV_HP_FLAG_SOC = (1 << 11), /* SystemOnChip, no PCI */ 431000b344fSMark Lord MV_HP_QUIRK_LED_BLINK_EN = (1 << 12), /* is led blinking enabled? */ 432c6fd2807SJeff Garzik 433c6fd2807SJeff Garzik /* Port private flags (pp_flags) */ 4340ea9e179SJeff Garzik MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */ 43572109168SMark Lord MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */ 43600f42eabSMark Lord MV_PP_FLAG_FBS_EN = (1 << 2), /* is EDMA set up for FBS? */ 43729d187bbSMark Lord MV_PP_FLAG_DELAYED_EH = (1 << 3), /* delayed dev err handling */ 438d16ab3f6SMark Lord MV_PP_FLAG_FAKE_ATA_BUSY = (1 << 4), /* ignore initial ATA_DRDY */ 439c6fd2807SJeff Garzik }; 440c6fd2807SJeff Garzik 441ee9ccdf7SJeff Garzik #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I) 442ee9ccdf7SJeff Garzik #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II) 443c6fd2807SJeff Garzik #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE) 4448e7decdbSMark Lord #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE) 4451f398472SMark Lord #define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC) 446c6fd2807SJeff Garzik 44715a32632SLennert Buytenhek #define WINDOW_CTRL(i) (0x20030 + ((i) << 4)) 44815a32632SLennert Buytenhek #define WINDOW_BASE(i) (0x20034 + ((i) << 4)) 44915a32632SLennert Buytenhek 450c6fd2807SJeff Garzik enum { 451baf14aa1SJeff Garzik /* DMA boundary 0xffff is required by the s/g splitting 452baf14aa1SJeff Garzik * we need on /length/ in mv_fill-sg(). 453baf14aa1SJeff Garzik */ 454baf14aa1SJeff Garzik MV_DMA_BOUNDARY = 0xffffU, 455c6fd2807SJeff Garzik 4560ea9e179SJeff Garzik /* mask of register bits containing lower 32 bits 4570ea9e179SJeff Garzik * of EDMA request queue DMA address 4580ea9e179SJeff Garzik */ 459c6fd2807SJeff Garzik EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U, 460c6fd2807SJeff Garzik 4610ea9e179SJeff Garzik /* ditto, for response queue */ 462c6fd2807SJeff Garzik EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U, 463c6fd2807SJeff Garzik }; 464c6fd2807SJeff Garzik 465c6fd2807SJeff Garzik enum chip_type { 466c6fd2807SJeff Garzik chip_504x, 467c6fd2807SJeff Garzik chip_508x, 468c6fd2807SJeff Garzik chip_5080, 469c6fd2807SJeff Garzik chip_604x, 470c6fd2807SJeff Garzik chip_608x, 471c6fd2807SJeff Garzik chip_6042, 472c6fd2807SJeff Garzik chip_7042, 473f351b2d6SSaeed Bishara chip_soc, 474c6fd2807SJeff Garzik }; 475c6fd2807SJeff Garzik 476c6fd2807SJeff Garzik /* Command ReQuest Block: 32B */ 477c6fd2807SJeff Garzik struct mv_crqb { 478c6fd2807SJeff Garzik __le32 sg_addr; 479c6fd2807SJeff Garzik __le32 sg_addr_hi; 480c6fd2807SJeff Garzik __le16 ctrl_flags; 481c6fd2807SJeff Garzik __le16 ata_cmd[11]; 482c6fd2807SJeff Garzik }; 483c6fd2807SJeff Garzik 484c6fd2807SJeff Garzik struct mv_crqb_iie { 485c6fd2807SJeff Garzik __le32 addr; 486c6fd2807SJeff Garzik __le32 addr_hi; 487c6fd2807SJeff Garzik __le32 flags; 488c6fd2807SJeff Garzik __le32 len; 489c6fd2807SJeff Garzik __le32 ata_cmd[4]; 490c6fd2807SJeff Garzik }; 491c6fd2807SJeff Garzik 492c6fd2807SJeff Garzik /* Command ResPonse Block: 8B */ 493c6fd2807SJeff Garzik struct mv_crpb { 494c6fd2807SJeff Garzik __le16 id; 495c6fd2807SJeff Garzik __le16 flags; 496c6fd2807SJeff Garzik __le32 tmstmp; 497c6fd2807SJeff Garzik }; 498c6fd2807SJeff Garzik 499c6fd2807SJeff Garzik /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */ 500c6fd2807SJeff Garzik struct mv_sg { 501c6fd2807SJeff Garzik __le32 addr; 502c6fd2807SJeff Garzik __le32 flags_size; 503c6fd2807SJeff Garzik __le32 addr_hi; 504c6fd2807SJeff Garzik __le32 reserved; 505c6fd2807SJeff Garzik }; 506c6fd2807SJeff Garzik 50708da1759SMark Lord /* 50808da1759SMark Lord * We keep a local cache of a few frequently accessed port 50908da1759SMark Lord * registers here, to avoid having to read them (very slow) 51008da1759SMark Lord * when switching between EDMA and non-EDMA modes. 51108da1759SMark Lord */ 51208da1759SMark Lord struct mv_cached_regs { 51308da1759SMark Lord u32 fiscfg; 51408da1759SMark Lord u32 ltmode; 51508da1759SMark Lord u32 haltcond; 516c01e8a23SMark Lord u32 unknown_rsvd; 51708da1759SMark Lord }; 51808da1759SMark Lord 519c6fd2807SJeff Garzik struct mv_port_priv { 520c6fd2807SJeff Garzik struct mv_crqb *crqb; 521c6fd2807SJeff Garzik dma_addr_t crqb_dma; 522c6fd2807SJeff Garzik struct mv_crpb *crpb; 523c6fd2807SJeff Garzik dma_addr_t crpb_dma; 524eb73d558SMark Lord struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH]; 525eb73d558SMark Lord dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH]; 526bdd4dddeSJeff Garzik 527bdd4dddeSJeff Garzik unsigned int req_idx; 528bdd4dddeSJeff Garzik unsigned int resp_idx; 529bdd4dddeSJeff Garzik 530c6fd2807SJeff Garzik u32 pp_flags; 53108da1759SMark Lord struct mv_cached_regs cached; 53229d187bbSMark Lord unsigned int delayed_eh_pmp_map; 533c6fd2807SJeff Garzik }; 534c6fd2807SJeff Garzik 535c6fd2807SJeff Garzik struct mv_port_signal { 536c6fd2807SJeff Garzik u32 amps; 537c6fd2807SJeff Garzik u32 pre; 538c6fd2807SJeff Garzik }; 539c6fd2807SJeff Garzik 54002a121daSMark Lord struct mv_host_priv { 54102a121daSMark Lord u32 hp_flags; 5421bfeff03SSaeed Bishara unsigned int board_idx; 54396e2c487SMark Lord u32 main_irq_mask; 54402a121daSMark Lord struct mv_port_signal signal[8]; 54502a121daSMark Lord const struct mv_hw_ops *ops; 546f351b2d6SSaeed Bishara int n_ports; 547f351b2d6SSaeed Bishara void __iomem *base; 5487368f919SMark Lord void __iomem *main_irq_cause_addr; 5497368f919SMark Lord void __iomem *main_irq_mask_addr; 550cae5a29dSMark Lord u32 irq_cause_offset; 551cae5a29dSMark Lord u32 irq_mask_offset; 55202a121daSMark Lord u32 unmask_all_irqs; 553c77a2f4eSSaeed Bishara 554c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK) 555c77a2f4eSSaeed Bishara struct clk *clk; 556c77a2f4eSSaeed Bishara #endif 557da2fa9baSMark Lord /* 558da2fa9baSMark Lord * These consistent DMA memory pools give us guaranteed 559da2fa9baSMark Lord * alignment for hardware-accessed data structures, 560da2fa9baSMark Lord * and less memory waste in accomplishing the alignment. 561da2fa9baSMark Lord */ 562da2fa9baSMark Lord struct dma_pool *crqb_pool; 563da2fa9baSMark Lord struct dma_pool *crpb_pool; 564da2fa9baSMark Lord struct dma_pool *sg_tbl_pool; 56502a121daSMark Lord }; 56602a121daSMark Lord 567c6fd2807SJeff Garzik struct mv_hw_ops { 568c6fd2807SJeff Garzik void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio, 569c6fd2807SJeff Garzik unsigned int port); 570c6fd2807SJeff Garzik void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio); 571c6fd2807SJeff Garzik void (*read_preamp)(struct mv_host_priv *hpriv, int idx, 572c6fd2807SJeff Garzik void __iomem *mmio); 573c6fd2807SJeff Garzik int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio, 574c6fd2807SJeff Garzik unsigned int n_hc); 575c6fd2807SJeff Garzik void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio); 5767bb3c529SSaeed Bishara void (*reset_bus)(struct ata_host *host, void __iomem *mmio); 577c6fd2807SJeff Garzik }; 578c6fd2807SJeff Garzik 57982ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val); 58082ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val); 58182ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val); 58282ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val); 583c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap); 584c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap); 5853e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc); 586c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc); 587c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc); 588c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc); 589a1efdabaSTejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class, 590a1efdabaSTejun Heo unsigned long deadline); 591bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap); 592bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap); 593f273827eSMark Lord static void mv6_dev_config(struct ata_device *dev); 594c6fd2807SJeff Garzik 595c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio, 596c6fd2807SJeff Garzik unsigned int port); 597c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio); 598c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx, 599c6fd2807SJeff Garzik void __iomem *mmio); 600c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio, 601c6fd2807SJeff Garzik unsigned int n_hc); 602c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio); 6037bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio); 604c6fd2807SJeff Garzik 605c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio, 606c6fd2807SJeff Garzik unsigned int port); 607c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio); 608c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx, 609c6fd2807SJeff Garzik void __iomem *mmio); 610c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio, 611c6fd2807SJeff Garzik unsigned int n_hc); 612c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio); 613f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv, 614f351b2d6SSaeed Bishara void __iomem *mmio); 615f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx, 616f351b2d6SSaeed Bishara void __iomem *mmio); 617f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv, 618f351b2d6SSaeed Bishara void __iomem *mmio, unsigned int n_hc); 619f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv, 620f351b2d6SSaeed Bishara void __iomem *mmio); 621f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio); 62229b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv, 62329b7e43cSMartin Michlmayr void __iomem *mmio, unsigned int port); 6247bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio); 625e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio, 626c6fd2807SJeff Garzik unsigned int port_no); 627e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap); 628b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio); 62900b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma); 630c6fd2807SJeff Garzik 631e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp); 632e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class, 633e49856d8SMark Lord unsigned long deadline); 634e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class, 635e49856d8SMark Lord unsigned long deadline); 63629d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap); 6374c299ca3SMark Lord static void mv_process_crpb_entries(struct ata_port *ap, 6384c299ca3SMark Lord struct mv_port_priv *pp); 639c6fd2807SJeff Garzik 640da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap); 641da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc); 642da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc); 643da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc); 644da14265eSMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc); 645da14265eSMark Lord static u8 mv_bmdma_status(struct ata_port *ap); 646d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap); 647da14265eSMark Lord 648eb73d558SMark Lord /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below 649eb73d558SMark Lord * because we have to allow room for worst case splitting of 650eb73d558SMark Lord * PRDs for 64K boundaries in mv_fill_sg(). 651eb73d558SMark Lord */ 652c5d3e45aSJeff Garzik static struct scsi_host_template mv5_sht = { 65368d1d07bSTejun Heo ATA_BASE_SHT(DRV_NAME), 654baf14aa1SJeff Garzik .sg_tablesize = MV_MAX_SG_CT / 2, 655c5d3e45aSJeff Garzik .dma_boundary = MV_DMA_BOUNDARY, 656c5d3e45aSJeff Garzik }; 657c5d3e45aSJeff Garzik 658c5d3e45aSJeff Garzik static struct scsi_host_template mv6_sht = { 65968d1d07bSTejun Heo ATA_NCQ_SHT(DRV_NAME), 660138bfdd0SMark Lord .can_queue = MV_MAX_Q_DEPTH - 1, 661baf14aa1SJeff Garzik .sg_tablesize = MV_MAX_SG_CT / 2, 662c6fd2807SJeff Garzik .dma_boundary = MV_DMA_BOUNDARY, 663c6fd2807SJeff Garzik }; 664c6fd2807SJeff Garzik 665029cfd6bSTejun Heo static struct ata_port_operations mv5_ops = { 666029cfd6bSTejun Heo .inherits = &ata_sff_port_ops, 667c6fd2807SJeff Garzik 668c96f1732SAlan Cox .lost_interrupt = ATA_OP_NULL, 669c96f1732SAlan Cox 6703e4a1391SMark Lord .qc_defer = mv_qc_defer, 671c6fd2807SJeff Garzik .qc_prep = mv_qc_prep, 672c6fd2807SJeff Garzik .qc_issue = mv_qc_issue, 673c6fd2807SJeff Garzik 674bdd4dddeSJeff Garzik .freeze = mv_eh_freeze, 675bdd4dddeSJeff Garzik .thaw = mv_eh_thaw, 676a1efdabaSTejun Heo .hardreset = mv_hardreset, 677bdd4dddeSJeff Garzik 678c6fd2807SJeff Garzik .scr_read = mv5_scr_read, 679c6fd2807SJeff Garzik .scr_write = mv5_scr_write, 680c6fd2807SJeff Garzik 681c6fd2807SJeff Garzik .port_start = mv_port_start, 682c6fd2807SJeff Garzik .port_stop = mv_port_stop, 683c6fd2807SJeff Garzik }; 684c6fd2807SJeff Garzik 685029cfd6bSTejun Heo static struct ata_port_operations mv6_ops = { 6868930ff25STejun Heo .inherits = &ata_bmdma_port_ops, 687c6fd2807SJeff Garzik 6888930ff25STejun Heo .lost_interrupt = ATA_OP_NULL, 6898930ff25STejun Heo 6908930ff25STejun Heo .qc_defer = mv_qc_defer, 6918930ff25STejun Heo .qc_prep = mv_qc_prep, 6928930ff25STejun Heo .qc_issue = mv_qc_issue, 6938930ff25STejun Heo 6948930ff25STejun Heo .dev_config = mv6_dev_config, 6958930ff25STejun Heo 6968930ff25STejun Heo .freeze = mv_eh_freeze, 6978930ff25STejun Heo .thaw = mv_eh_thaw, 6988930ff25STejun Heo .hardreset = mv_hardreset, 6998930ff25STejun Heo .softreset = mv_softreset, 700e49856d8SMark Lord .pmp_hardreset = mv_pmp_hardreset, 701e49856d8SMark Lord .pmp_softreset = mv_softreset, 70229d187bbSMark Lord .error_handler = mv_pmp_error_handler, 703da14265eSMark Lord 7048930ff25STejun Heo .scr_read = mv_scr_read, 7058930ff25STejun Heo .scr_write = mv_scr_write, 7068930ff25STejun Heo 707d16ab3f6SMark Lord .sff_check_status = mv_sff_check_status, 708da14265eSMark Lord .sff_irq_clear = mv_sff_irq_clear, 709da14265eSMark Lord .check_atapi_dma = mv_check_atapi_dma, 710da14265eSMark Lord .bmdma_setup = mv_bmdma_setup, 711da14265eSMark Lord .bmdma_start = mv_bmdma_start, 712da14265eSMark Lord .bmdma_stop = mv_bmdma_stop, 713da14265eSMark Lord .bmdma_status = mv_bmdma_status, 7148930ff25STejun Heo 7158930ff25STejun Heo .port_start = mv_port_start, 7168930ff25STejun Heo .port_stop = mv_port_stop, 717c6fd2807SJeff Garzik }; 718c6fd2807SJeff Garzik 719029cfd6bSTejun Heo static struct ata_port_operations mv_iie_ops = { 720029cfd6bSTejun Heo .inherits = &mv6_ops, 721029cfd6bSTejun Heo .dev_config = ATA_OP_NULL, 722c6fd2807SJeff Garzik .qc_prep = mv_qc_prep_iie, 723c6fd2807SJeff Garzik }; 724c6fd2807SJeff Garzik 725c6fd2807SJeff Garzik static const struct ata_port_info mv_port_info[] = { 726c6fd2807SJeff Garzik { /* chip_504x */ 72791b1a84cSMark Lord .flags = MV_GEN_I_FLAGS, 728c361acbcSMark Lord .pio_mask = ATA_PIO4, 729bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 730c6fd2807SJeff Garzik .port_ops = &mv5_ops, 731c6fd2807SJeff Garzik }, 732c6fd2807SJeff Garzik { /* chip_508x */ 73391b1a84cSMark Lord .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC, 734c361acbcSMark Lord .pio_mask = ATA_PIO4, 735bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 736c6fd2807SJeff Garzik .port_ops = &mv5_ops, 737c6fd2807SJeff Garzik }, 738c6fd2807SJeff Garzik { /* chip_5080 */ 73991b1a84cSMark Lord .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC, 740c361acbcSMark Lord .pio_mask = ATA_PIO4, 741bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 742c6fd2807SJeff Garzik .port_ops = &mv5_ops, 743c6fd2807SJeff Garzik }, 744c6fd2807SJeff Garzik { /* chip_604x */ 74591b1a84cSMark Lord .flags = MV_GEN_II_FLAGS, 746c361acbcSMark Lord .pio_mask = ATA_PIO4, 747bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 748c6fd2807SJeff Garzik .port_ops = &mv6_ops, 749c6fd2807SJeff Garzik }, 750c6fd2807SJeff Garzik { /* chip_608x */ 75191b1a84cSMark Lord .flags = MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC, 752c361acbcSMark Lord .pio_mask = ATA_PIO4, 753bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 754c6fd2807SJeff Garzik .port_ops = &mv6_ops, 755c6fd2807SJeff Garzik }, 756c6fd2807SJeff Garzik { /* chip_6042 */ 75791b1a84cSMark Lord .flags = MV_GEN_IIE_FLAGS, 758c361acbcSMark Lord .pio_mask = ATA_PIO4, 759bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 760c6fd2807SJeff Garzik .port_ops = &mv_iie_ops, 761c6fd2807SJeff Garzik }, 762c6fd2807SJeff Garzik { /* chip_7042 */ 76391b1a84cSMark Lord .flags = MV_GEN_IIE_FLAGS, 764c361acbcSMark Lord .pio_mask = ATA_PIO4, 765bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 766c6fd2807SJeff Garzik .port_ops = &mv_iie_ops, 767c6fd2807SJeff Garzik }, 768f351b2d6SSaeed Bishara { /* chip_soc */ 76991b1a84cSMark Lord .flags = MV_GEN_IIE_FLAGS, 770c361acbcSMark Lord .pio_mask = ATA_PIO4, 771f351b2d6SSaeed Bishara .udma_mask = ATA_UDMA6, 772f351b2d6SSaeed Bishara .port_ops = &mv_iie_ops, 773f351b2d6SSaeed Bishara }, 774c6fd2807SJeff Garzik }; 775c6fd2807SJeff Garzik 776c6fd2807SJeff Garzik static const struct pci_device_id mv_pci_tbl[] = { 7772d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x5040), chip_504x }, 7782d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x5041), chip_504x }, 7792d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 }, 7802d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x5081), chip_508x }, 78146c5784cSMark Lord /* RocketRAID 1720/174x have different identifiers */ 78246c5784cSMark Lord { PCI_VDEVICE(TTI, 0x1720), chip_6042 }, 7834462254aSMark Lord { PCI_VDEVICE(TTI, 0x1740), chip_6042 }, 7844462254aSMark Lord { PCI_VDEVICE(TTI, 0x1742), chip_6042 }, 785c6fd2807SJeff Garzik 7862d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x6040), chip_604x }, 7872d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x6041), chip_604x }, 7882d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 }, 7892d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x6080), chip_608x }, 7902d2744fcSJeff Garzik { PCI_VDEVICE(MARVELL, 0x6081), chip_608x }, 791c6fd2807SJeff Garzik 7922d2744fcSJeff Garzik { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x }, 7932d2744fcSJeff Garzik 794d9f9c6bcSFlorian Attenberger /* Adaptec 1430SA */ 795d9f9c6bcSFlorian Attenberger { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 }, 796d9f9c6bcSFlorian Attenberger 79702a121daSMark Lord /* Marvell 7042 support */ 7986a3d586dSMorrison, Tom { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 }, 7996a3d586dSMorrison, Tom 80002a121daSMark Lord /* Highpoint RocketRAID PCIe series */ 80102a121daSMark Lord { PCI_VDEVICE(TTI, 0x2300), chip_7042 }, 80202a121daSMark Lord { PCI_VDEVICE(TTI, 0x2310), chip_7042 }, 80302a121daSMark Lord 804c6fd2807SJeff Garzik { } /* terminate list */ 805c6fd2807SJeff Garzik }; 806c6fd2807SJeff Garzik 807c6fd2807SJeff Garzik static const struct mv_hw_ops mv5xxx_ops = { 808c6fd2807SJeff Garzik .phy_errata = mv5_phy_errata, 809c6fd2807SJeff Garzik .enable_leds = mv5_enable_leds, 810c6fd2807SJeff Garzik .read_preamp = mv5_read_preamp, 811c6fd2807SJeff Garzik .reset_hc = mv5_reset_hc, 812c6fd2807SJeff Garzik .reset_flash = mv5_reset_flash, 813c6fd2807SJeff Garzik .reset_bus = mv5_reset_bus, 814c6fd2807SJeff Garzik }; 815c6fd2807SJeff Garzik 816c6fd2807SJeff Garzik static const struct mv_hw_ops mv6xxx_ops = { 817c6fd2807SJeff Garzik .phy_errata = mv6_phy_errata, 818c6fd2807SJeff Garzik .enable_leds = mv6_enable_leds, 819c6fd2807SJeff Garzik .read_preamp = mv6_read_preamp, 820c6fd2807SJeff Garzik .reset_hc = mv6_reset_hc, 821c6fd2807SJeff Garzik .reset_flash = mv6_reset_flash, 822c6fd2807SJeff Garzik .reset_bus = mv_reset_pci_bus, 823c6fd2807SJeff Garzik }; 824c6fd2807SJeff Garzik 825f351b2d6SSaeed Bishara static const struct mv_hw_ops mv_soc_ops = { 826f351b2d6SSaeed Bishara .phy_errata = mv6_phy_errata, 827f351b2d6SSaeed Bishara .enable_leds = mv_soc_enable_leds, 828f351b2d6SSaeed Bishara .read_preamp = mv_soc_read_preamp, 829f351b2d6SSaeed Bishara .reset_hc = mv_soc_reset_hc, 830f351b2d6SSaeed Bishara .reset_flash = mv_soc_reset_flash, 831f351b2d6SSaeed Bishara .reset_bus = mv_soc_reset_bus, 832f351b2d6SSaeed Bishara }; 833f351b2d6SSaeed Bishara 83429b7e43cSMartin Michlmayr static const struct mv_hw_ops mv_soc_65n_ops = { 83529b7e43cSMartin Michlmayr .phy_errata = mv_soc_65n_phy_errata, 83629b7e43cSMartin Michlmayr .enable_leds = mv_soc_enable_leds, 83729b7e43cSMartin Michlmayr .reset_hc = mv_soc_reset_hc, 83829b7e43cSMartin Michlmayr .reset_flash = mv_soc_reset_flash, 83929b7e43cSMartin Michlmayr .reset_bus = mv_soc_reset_bus, 84029b7e43cSMartin Michlmayr }; 84129b7e43cSMartin Michlmayr 842c6fd2807SJeff Garzik /* 843c6fd2807SJeff Garzik * Functions 844c6fd2807SJeff Garzik */ 845c6fd2807SJeff Garzik 846c6fd2807SJeff Garzik static inline void writelfl(unsigned long data, void __iomem *addr) 847c6fd2807SJeff Garzik { 848c6fd2807SJeff Garzik writel(data, addr); 849c6fd2807SJeff Garzik (void) readl(addr); /* flush to avoid PCI posted write */ 850c6fd2807SJeff Garzik } 851c6fd2807SJeff Garzik 852c6fd2807SJeff Garzik static inline unsigned int mv_hc_from_port(unsigned int port) 853c6fd2807SJeff Garzik { 854c6fd2807SJeff Garzik return port >> MV_PORT_HC_SHIFT; 855c6fd2807SJeff Garzik } 856c6fd2807SJeff Garzik 857c6fd2807SJeff Garzik static inline unsigned int mv_hardport_from_port(unsigned int port) 858c6fd2807SJeff Garzik { 859c6fd2807SJeff Garzik return port & MV_PORT_MASK; 860c6fd2807SJeff Garzik } 861c6fd2807SJeff Garzik 8621cfd19aeSMark Lord /* 8631cfd19aeSMark Lord * Consolidate some rather tricky bit shift calculations. 8641cfd19aeSMark Lord * This is hot-path stuff, so not a function. 8651cfd19aeSMark Lord * Simple code, with two return values, so macro rather than inline. 8661cfd19aeSMark Lord * 8671cfd19aeSMark Lord * port is the sole input, in range 0..7. 8687368f919SMark Lord * shift is one output, for use with main_irq_cause / main_irq_mask registers. 8697368f919SMark Lord * hardport is the other output, in range 0..3. 8701cfd19aeSMark Lord * 8711cfd19aeSMark Lord * Note that port and hardport may be the same variable in some cases. 8721cfd19aeSMark Lord */ 8731cfd19aeSMark Lord #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport) \ 8741cfd19aeSMark Lord { \ 8751cfd19aeSMark Lord shift = mv_hc_from_port(port) * HC_SHIFT; \ 8761cfd19aeSMark Lord hardport = mv_hardport_from_port(port); \ 8771cfd19aeSMark Lord shift += hardport * 2; \ 8781cfd19aeSMark Lord } 8791cfd19aeSMark Lord 880352fab70SMark Lord static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc) 881352fab70SMark Lord { 882cae5a29dSMark Lord return (base + SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ)); 883352fab70SMark Lord } 884352fab70SMark Lord 885c6fd2807SJeff Garzik static inline void __iomem *mv_hc_base_from_port(void __iomem *base, 886c6fd2807SJeff Garzik unsigned int port) 887c6fd2807SJeff Garzik { 888c6fd2807SJeff Garzik return mv_hc_base(base, mv_hc_from_port(port)); 889c6fd2807SJeff Garzik } 890c6fd2807SJeff Garzik 891c6fd2807SJeff Garzik static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port) 892c6fd2807SJeff Garzik { 893c6fd2807SJeff Garzik return mv_hc_base_from_port(base, port) + 894c6fd2807SJeff Garzik MV_SATAHC_ARBTR_REG_SZ + 895c6fd2807SJeff Garzik (mv_hardport_from_port(port) * MV_PORT_REG_SZ); 896c6fd2807SJeff Garzik } 897c6fd2807SJeff Garzik 898e12bef50SMark Lord static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port) 899e12bef50SMark Lord { 900e12bef50SMark Lord void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port); 901e12bef50SMark Lord unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL; 902e12bef50SMark Lord 903e12bef50SMark Lord return hc_mmio + ofs; 904e12bef50SMark Lord } 905e12bef50SMark Lord 906f351b2d6SSaeed Bishara static inline void __iomem *mv_host_base(struct ata_host *host) 907f351b2d6SSaeed Bishara { 908f351b2d6SSaeed Bishara struct mv_host_priv *hpriv = host->private_data; 909f351b2d6SSaeed Bishara return hpriv->base; 910f351b2d6SSaeed Bishara } 911f351b2d6SSaeed Bishara 912c6fd2807SJeff Garzik static inline void __iomem *mv_ap_base(struct ata_port *ap) 913c6fd2807SJeff Garzik { 914f351b2d6SSaeed Bishara return mv_port_base(mv_host_base(ap->host), ap->port_no); 915c6fd2807SJeff Garzik } 916c6fd2807SJeff Garzik 917cca3974eSJeff Garzik static inline int mv_get_hc_count(unsigned long port_flags) 918c6fd2807SJeff Garzik { 919cca3974eSJeff Garzik return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1); 920c6fd2807SJeff Garzik } 921c6fd2807SJeff Garzik 92208da1759SMark Lord /** 92308da1759SMark Lord * mv_save_cached_regs - (re-)initialize cached port registers 92408da1759SMark Lord * @ap: the port whose registers we are caching 92508da1759SMark Lord * 92608da1759SMark Lord * Initialize the local cache of port registers, 92708da1759SMark Lord * so that reading them over and over again can 92808da1759SMark Lord * be avoided on the hotter paths of this driver. 92908da1759SMark Lord * This saves a few microseconds each time we switch 93008da1759SMark Lord * to/from EDMA mode to perform (eg.) a drive cache flush. 93108da1759SMark Lord */ 93208da1759SMark Lord static void mv_save_cached_regs(struct ata_port *ap) 93308da1759SMark Lord { 93408da1759SMark Lord void __iomem *port_mmio = mv_ap_base(ap); 93508da1759SMark Lord struct mv_port_priv *pp = ap->private_data; 93608da1759SMark Lord 937cae5a29dSMark Lord pp->cached.fiscfg = readl(port_mmio + FISCFG); 938cae5a29dSMark Lord pp->cached.ltmode = readl(port_mmio + LTMODE); 939cae5a29dSMark Lord pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND); 940cae5a29dSMark Lord pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD); 94108da1759SMark Lord } 94208da1759SMark Lord 94308da1759SMark Lord /** 94408da1759SMark Lord * mv_write_cached_reg - write to a cached port register 94508da1759SMark Lord * @addr: hardware address of the register 94608da1759SMark Lord * @old: pointer to cached value of the register 94708da1759SMark Lord * @new: new value for the register 94808da1759SMark Lord * 94908da1759SMark Lord * Write a new value to a cached register, 95008da1759SMark Lord * but only if the value is different from before. 95108da1759SMark Lord */ 95208da1759SMark Lord static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new) 95308da1759SMark Lord { 95408da1759SMark Lord if (new != *old) { 95512f3b6d7SMark Lord unsigned long laddr; 95608da1759SMark Lord *old = new; 95712f3b6d7SMark Lord /* 95812f3b6d7SMark Lord * Workaround for 88SX60x1-B2 FEr SATA#13: 95912f3b6d7SMark Lord * Read-after-write is needed to prevent generating 64-bit 96012f3b6d7SMark Lord * write cycles on the PCI bus for SATA interface registers 96112f3b6d7SMark Lord * at offsets ending in 0x4 or 0xc. 96212f3b6d7SMark Lord * 96312f3b6d7SMark Lord * Looks like a lot of fuss, but it avoids an unnecessary 96412f3b6d7SMark Lord * +1 usec read-after-write delay for unaffected registers. 96512f3b6d7SMark Lord */ 96612f3b6d7SMark Lord laddr = (long)addr & 0xffff; 96712f3b6d7SMark Lord if (laddr >= 0x300 && laddr <= 0x33c) { 96812f3b6d7SMark Lord laddr &= 0x000f; 96912f3b6d7SMark Lord if (laddr == 0x4 || laddr == 0xc) { 97012f3b6d7SMark Lord writelfl(new, addr); /* read after write */ 97112f3b6d7SMark Lord return; 97212f3b6d7SMark Lord } 97312f3b6d7SMark Lord } 97412f3b6d7SMark Lord writel(new, addr); /* unaffected by the errata */ 97508da1759SMark Lord } 97608da1759SMark Lord } 97708da1759SMark Lord 978c5d3e45aSJeff Garzik static void mv_set_edma_ptrs(void __iomem *port_mmio, 979c5d3e45aSJeff Garzik struct mv_host_priv *hpriv, 980c5d3e45aSJeff Garzik struct mv_port_priv *pp) 981c5d3e45aSJeff Garzik { 982bdd4dddeSJeff Garzik u32 index; 983bdd4dddeSJeff Garzik 984c5d3e45aSJeff Garzik /* 985c5d3e45aSJeff Garzik * initialize request queue 986c5d3e45aSJeff Garzik */ 987fcfb1f77SMark Lord pp->req_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */ 988fcfb1f77SMark Lord index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT; 989bdd4dddeSJeff Garzik 990c5d3e45aSJeff Garzik WARN_ON(pp->crqb_dma & 0x3ff); 991cae5a29dSMark Lord writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI); 992bdd4dddeSJeff Garzik writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index, 993cae5a29dSMark Lord port_mmio + EDMA_REQ_Q_IN_PTR); 994cae5a29dSMark Lord writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR); 995c5d3e45aSJeff Garzik 996c5d3e45aSJeff Garzik /* 997c5d3e45aSJeff Garzik * initialize response queue 998c5d3e45aSJeff Garzik */ 999fcfb1f77SMark Lord pp->resp_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */ 1000fcfb1f77SMark Lord index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT; 1001bdd4dddeSJeff Garzik 1002c5d3e45aSJeff Garzik WARN_ON(pp->crpb_dma & 0xff); 1003cae5a29dSMark Lord writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI); 1004cae5a29dSMark Lord writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR); 1005bdd4dddeSJeff Garzik writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index, 1006cae5a29dSMark Lord port_mmio + EDMA_RSP_Q_OUT_PTR); 1007c5d3e45aSJeff Garzik } 1008c5d3e45aSJeff Garzik 10092b748a0aSMark Lord static void mv_write_main_irq_mask(u32 mask, struct mv_host_priv *hpriv) 10102b748a0aSMark Lord { 10112b748a0aSMark Lord /* 10122b748a0aSMark Lord * When writing to the main_irq_mask in hardware, 10132b748a0aSMark Lord * we must ensure exclusivity between the interrupt coalescing bits 10142b748a0aSMark Lord * and the corresponding individual port DONE_IRQ bits. 10152b748a0aSMark Lord * 10162b748a0aSMark Lord * Note that this register is really an "IRQ enable" register, 10172b748a0aSMark Lord * not an "IRQ mask" register as Marvell's naming might suggest. 10182b748a0aSMark Lord */ 10192b748a0aSMark Lord if (mask & (ALL_PORTS_COAL_DONE | PORTS_0_3_COAL_DONE)) 10202b748a0aSMark Lord mask &= ~DONE_IRQ_0_3; 10212b748a0aSMark Lord if (mask & (ALL_PORTS_COAL_DONE | PORTS_4_7_COAL_DONE)) 10222b748a0aSMark Lord mask &= ~DONE_IRQ_4_7; 10232b748a0aSMark Lord writelfl(mask, hpriv->main_irq_mask_addr); 10242b748a0aSMark Lord } 10252b748a0aSMark Lord 1026c4de573bSMark Lord static void mv_set_main_irq_mask(struct ata_host *host, 1027c4de573bSMark Lord u32 disable_bits, u32 enable_bits) 1028c4de573bSMark Lord { 1029c4de573bSMark Lord struct mv_host_priv *hpriv = host->private_data; 1030c4de573bSMark Lord u32 old_mask, new_mask; 1031c4de573bSMark Lord 103296e2c487SMark Lord old_mask = hpriv->main_irq_mask; 1033c4de573bSMark Lord new_mask = (old_mask & ~disable_bits) | enable_bits; 103496e2c487SMark Lord if (new_mask != old_mask) { 103596e2c487SMark Lord hpriv->main_irq_mask = new_mask; 10362b748a0aSMark Lord mv_write_main_irq_mask(new_mask, hpriv); 1037c4de573bSMark Lord } 103896e2c487SMark Lord } 1039c4de573bSMark Lord 1040c4de573bSMark Lord static void mv_enable_port_irqs(struct ata_port *ap, 1041c4de573bSMark Lord unsigned int port_bits) 1042c4de573bSMark Lord { 1043c4de573bSMark Lord unsigned int shift, hardport, port = ap->port_no; 1044c4de573bSMark Lord u32 disable_bits, enable_bits; 1045c4de573bSMark Lord 1046c4de573bSMark Lord MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport); 1047c4de573bSMark Lord 1048c4de573bSMark Lord disable_bits = (DONE_IRQ | ERR_IRQ) << shift; 1049c4de573bSMark Lord enable_bits = port_bits << shift; 1050c4de573bSMark Lord mv_set_main_irq_mask(ap->host, disable_bits, enable_bits); 1051c4de573bSMark Lord } 1052c4de573bSMark Lord 105300b81235SMark Lord static void mv_clear_and_enable_port_irqs(struct ata_port *ap, 105400b81235SMark Lord void __iomem *port_mmio, 105500b81235SMark Lord unsigned int port_irqs) 1056c6fd2807SJeff Garzik { 10570c58912eSMark Lord struct mv_host_priv *hpriv = ap->host->private_data; 1058352fab70SMark Lord int hardport = mv_hardport_from_port(ap->port_no); 10590c58912eSMark Lord void __iomem *hc_mmio = mv_hc_base_from_port( 1060b0bccb18SMark Lord mv_host_base(ap->host), ap->port_no); 1061cae6edc3SMark Lord u32 hc_irq_cause; 10620c58912eSMark Lord 1063bdd4dddeSJeff Garzik /* clear EDMA event indicators, if any */ 1064cae5a29dSMark Lord writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE); 1065bdd4dddeSJeff Garzik 1066cae6edc3SMark Lord /* clear pending irq events */ 1067cae6edc3SMark Lord hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport); 1068cae5a29dSMark Lord writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE); 10690c58912eSMark Lord 10700c58912eSMark Lord /* clear FIS IRQ Cause */ 1071e4006077SMark Lord if (IS_GEN_IIE(hpriv)) 1072cae5a29dSMark Lord writelfl(0, port_mmio + FIS_IRQ_CAUSE); 10730c58912eSMark Lord 107400b81235SMark Lord mv_enable_port_irqs(ap, port_irqs); 107500b81235SMark Lord } 107600b81235SMark Lord 10772b748a0aSMark Lord static void mv_set_irq_coalescing(struct ata_host *host, 10782b748a0aSMark Lord unsigned int count, unsigned int usecs) 10792b748a0aSMark Lord { 10802b748a0aSMark Lord struct mv_host_priv *hpriv = host->private_data; 10812b748a0aSMark Lord void __iomem *mmio = hpriv->base, *hc_mmio; 10822b748a0aSMark Lord u32 coal_enable = 0; 10832b748a0aSMark Lord unsigned long flags; 10846abf4678SMark Lord unsigned int clks, is_dual_hc = hpriv->n_ports > MV_PORTS_PER_HC; 10852b748a0aSMark Lord const u32 coal_disable = PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE | 10862b748a0aSMark Lord ALL_PORTS_COAL_DONE; 10872b748a0aSMark Lord 10882b748a0aSMark Lord /* Disable IRQ coalescing if either threshold is zero */ 10892b748a0aSMark Lord if (!usecs || !count) { 10902b748a0aSMark Lord clks = count = 0; 10912b748a0aSMark Lord } else { 10922b748a0aSMark Lord /* Respect maximum limits of the hardware */ 10932b748a0aSMark Lord clks = usecs * COAL_CLOCKS_PER_USEC; 10942b748a0aSMark Lord if (clks > MAX_COAL_TIME_THRESHOLD) 10952b748a0aSMark Lord clks = MAX_COAL_TIME_THRESHOLD; 10962b748a0aSMark Lord if (count > MAX_COAL_IO_COUNT) 10972b748a0aSMark Lord count = MAX_COAL_IO_COUNT; 10982b748a0aSMark Lord } 10992b748a0aSMark Lord 11002b748a0aSMark Lord spin_lock_irqsave(&host->lock, flags); 11016abf4678SMark Lord mv_set_main_irq_mask(host, coal_disable, 0); 11022b748a0aSMark Lord 11036abf4678SMark Lord if (is_dual_hc && !IS_GEN_I(hpriv)) { 11042b748a0aSMark Lord /* 11056abf4678SMark Lord * GEN_II/GEN_IIE with dual host controllers: 11066abf4678SMark Lord * one set of global thresholds for the entire chip. 11072b748a0aSMark Lord */ 1108cae5a29dSMark Lord writel(clks, mmio + IRQ_COAL_TIME_THRESHOLD); 1109cae5a29dSMark Lord writel(count, mmio + IRQ_COAL_IO_THRESHOLD); 11102b748a0aSMark Lord /* clear leftover coal IRQ bit */ 1111cae5a29dSMark Lord writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE); 11126abf4678SMark Lord if (count) 11132b748a0aSMark Lord coal_enable = ALL_PORTS_COAL_DONE; 11146abf4678SMark Lord clks = count = 0; /* force clearing of regular regs below */ 11152b748a0aSMark Lord } 11166abf4678SMark Lord 11172b748a0aSMark Lord /* 11182b748a0aSMark Lord * All chips: independent thresholds for each HC on the chip. 11192b748a0aSMark Lord */ 11202b748a0aSMark Lord hc_mmio = mv_hc_base_from_port(mmio, 0); 1121cae5a29dSMark Lord writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD); 1122cae5a29dSMark Lord writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD); 1123cae5a29dSMark Lord writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE); 11246abf4678SMark Lord if (count) 11252b748a0aSMark Lord coal_enable |= PORTS_0_3_COAL_DONE; 11266abf4678SMark Lord if (is_dual_hc) { 11272b748a0aSMark Lord hc_mmio = mv_hc_base_from_port(mmio, MV_PORTS_PER_HC); 1128cae5a29dSMark Lord writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD); 1129cae5a29dSMark Lord writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD); 1130cae5a29dSMark Lord writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE); 11316abf4678SMark Lord if (count) 11322b748a0aSMark Lord coal_enable |= PORTS_4_7_COAL_DONE; 11332b748a0aSMark Lord } 11342b748a0aSMark Lord 11356abf4678SMark Lord mv_set_main_irq_mask(host, 0, coal_enable); 11362b748a0aSMark Lord spin_unlock_irqrestore(&host->lock, flags); 11372b748a0aSMark Lord } 11382b748a0aSMark Lord 113900b81235SMark Lord /** 114000b81235SMark Lord * mv_start_edma - Enable eDMA engine 114100b81235SMark Lord * @base: port base address 114200b81235SMark Lord * @pp: port private data 114300b81235SMark Lord * 114400b81235SMark Lord * Verify the local cache of the eDMA state is accurate with a 114500b81235SMark Lord * WARN_ON. 114600b81235SMark Lord * 114700b81235SMark Lord * LOCKING: 114800b81235SMark Lord * Inherited from caller. 114900b81235SMark Lord */ 115000b81235SMark Lord static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio, 115100b81235SMark Lord struct mv_port_priv *pp, u8 protocol) 115200b81235SMark Lord { 115300b81235SMark Lord int want_ncq = (protocol == ATA_PROT_NCQ); 115400b81235SMark Lord 115500b81235SMark Lord if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) { 115600b81235SMark Lord int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0); 115700b81235SMark Lord if (want_ncq != using_ncq) 115800b81235SMark Lord mv_stop_edma(ap); 115900b81235SMark Lord } 116000b81235SMark Lord if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) { 116100b81235SMark Lord struct mv_host_priv *hpriv = ap->host->private_data; 116200b81235SMark Lord 116300b81235SMark Lord mv_edma_cfg(ap, want_ncq, 1); 116400b81235SMark Lord 1165f630d562SMark Lord mv_set_edma_ptrs(port_mmio, hpriv, pp); 116600b81235SMark Lord mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ); 1167bdd4dddeSJeff Garzik 1168cae5a29dSMark Lord writelfl(EDMA_EN, port_mmio + EDMA_CMD); 1169c6fd2807SJeff Garzik pp->pp_flags |= MV_PP_FLAG_EDMA_EN; 1170c6fd2807SJeff Garzik } 1171c6fd2807SJeff Garzik } 1172c6fd2807SJeff Garzik 11739b2c4e0bSMark Lord static void mv_wait_for_edma_empty_idle(struct ata_port *ap) 11749b2c4e0bSMark Lord { 11759b2c4e0bSMark Lord void __iomem *port_mmio = mv_ap_base(ap); 11769b2c4e0bSMark Lord const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE); 11779b2c4e0bSMark Lord const int per_loop = 5, timeout = (15 * 1000 / per_loop); 11789b2c4e0bSMark Lord int i; 11799b2c4e0bSMark Lord 11809b2c4e0bSMark Lord /* 11819b2c4e0bSMark Lord * Wait for the EDMA engine to finish transactions in progress. 1182c46938ccSMark Lord * No idea what a good "timeout" value might be, but measurements 1183c46938ccSMark Lord * indicate that it often requires hundreds of microseconds 1184c46938ccSMark Lord * with two drives in-use. So we use the 15msec value above 1185c46938ccSMark Lord * as a rough guess at what even more drives might require. 11869b2c4e0bSMark Lord */ 11879b2c4e0bSMark Lord for (i = 0; i < timeout; ++i) { 1188cae5a29dSMark Lord u32 edma_stat = readl(port_mmio + EDMA_STATUS); 11899b2c4e0bSMark Lord if ((edma_stat & empty_idle) == empty_idle) 11909b2c4e0bSMark Lord break; 11919b2c4e0bSMark Lord udelay(per_loop); 11929b2c4e0bSMark Lord } 1193a9a79dfeSJoe Perches /* ata_port_info(ap, "%s: %u+ usecs\n", __func__, i); */ 11949b2c4e0bSMark Lord } 11959b2c4e0bSMark Lord 1196c6fd2807SJeff Garzik /** 1197e12bef50SMark Lord * mv_stop_edma_engine - Disable eDMA engine 1198b562468cSMark Lord * @port_mmio: io base address 1199c6fd2807SJeff Garzik * 1200c6fd2807SJeff Garzik * LOCKING: 1201c6fd2807SJeff Garzik * Inherited from caller. 1202c6fd2807SJeff Garzik */ 1203b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio) 1204c6fd2807SJeff Garzik { 1205b562468cSMark Lord int i; 1206c6fd2807SJeff Garzik 1207b562468cSMark Lord /* Disable eDMA. The disable bit auto clears. */ 1208cae5a29dSMark Lord writelfl(EDMA_DS, port_mmio + EDMA_CMD); 1209c6fd2807SJeff Garzik 1210b562468cSMark Lord /* Wait for the chip to confirm eDMA is off. */ 1211b562468cSMark Lord for (i = 10000; i > 0; i--) { 1212cae5a29dSMark Lord u32 reg = readl(port_mmio + EDMA_CMD); 12134537deb5SJeff Garzik if (!(reg & EDMA_EN)) 1214b562468cSMark Lord return 0; 1215b562468cSMark Lord udelay(10); 1216c6fd2807SJeff Garzik } 1217b562468cSMark Lord return -EIO; 1218c6fd2807SJeff Garzik } 1219c6fd2807SJeff Garzik 1220e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap) 1221c6fd2807SJeff Garzik { 1222c6fd2807SJeff Garzik void __iomem *port_mmio = mv_ap_base(ap); 1223c6fd2807SJeff Garzik struct mv_port_priv *pp = ap->private_data; 122466e57a2cSMark Lord int err = 0; 1225c6fd2807SJeff Garzik 1226b562468cSMark Lord if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) 1227b562468cSMark Lord return 0; 1228c6fd2807SJeff Garzik pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN; 12299b2c4e0bSMark Lord mv_wait_for_edma_empty_idle(ap); 1230b562468cSMark Lord if (mv_stop_edma_engine(port_mmio)) { 1231a9a79dfeSJoe Perches ata_port_err(ap, "Unable to stop eDMA\n"); 123266e57a2cSMark Lord err = -EIO; 1233c6fd2807SJeff Garzik } 123466e57a2cSMark Lord mv_edma_cfg(ap, 0, 0); 123566e57a2cSMark Lord return err; 12360ea9e179SJeff Garzik } 12370ea9e179SJeff Garzik 1238c6fd2807SJeff Garzik #ifdef ATA_DEBUG 1239c6fd2807SJeff Garzik static void mv_dump_mem(void __iomem *start, unsigned bytes) 1240c6fd2807SJeff Garzik { 1241c6fd2807SJeff Garzik int b, w; 1242c6fd2807SJeff Garzik for (b = 0; b < bytes; ) { 1243c6fd2807SJeff Garzik DPRINTK("%p: ", start + b); 1244c6fd2807SJeff Garzik for (w = 0; b < bytes && w < 4; w++) { 1245c6fd2807SJeff Garzik printk("%08x ", readl(start + b)); 1246c6fd2807SJeff Garzik b += sizeof(u32); 1247c6fd2807SJeff Garzik } 1248c6fd2807SJeff Garzik printk("\n"); 1249c6fd2807SJeff Garzik } 1250c6fd2807SJeff Garzik } 1251c6fd2807SJeff Garzik #endif 1252c6fd2807SJeff Garzik 1253c6fd2807SJeff Garzik static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes) 1254c6fd2807SJeff Garzik { 1255c6fd2807SJeff Garzik #ifdef ATA_DEBUG 1256c6fd2807SJeff Garzik int b, w; 1257c6fd2807SJeff Garzik u32 dw; 1258c6fd2807SJeff Garzik for (b = 0; b < bytes; ) { 1259c6fd2807SJeff Garzik DPRINTK("%02x: ", b); 1260c6fd2807SJeff Garzik for (w = 0; b < bytes && w < 4; w++) { 1261c6fd2807SJeff Garzik (void) pci_read_config_dword(pdev, b, &dw); 1262c6fd2807SJeff Garzik printk("%08x ", dw); 1263c6fd2807SJeff Garzik b += sizeof(u32); 1264c6fd2807SJeff Garzik } 1265c6fd2807SJeff Garzik printk("\n"); 1266c6fd2807SJeff Garzik } 1267c6fd2807SJeff Garzik #endif 1268c6fd2807SJeff Garzik } 1269c6fd2807SJeff Garzik static void mv_dump_all_regs(void __iomem *mmio_base, int port, 1270c6fd2807SJeff Garzik struct pci_dev *pdev) 1271c6fd2807SJeff Garzik { 1272c6fd2807SJeff Garzik #ifdef ATA_DEBUG 1273c6fd2807SJeff Garzik void __iomem *hc_base = mv_hc_base(mmio_base, 1274c6fd2807SJeff Garzik port >> MV_PORT_HC_SHIFT); 1275c6fd2807SJeff Garzik void __iomem *port_base; 1276c6fd2807SJeff Garzik int start_port, num_ports, p, start_hc, num_hcs, hc; 1277c6fd2807SJeff Garzik 1278c6fd2807SJeff Garzik if (0 > port) { 1279c6fd2807SJeff Garzik start_hc = start_port = 0; 1280c6fd2807SJeff Garzik num_ports = 8; /* shld be benign for 4 port devs */ 1281c6fd2807SJeff Garzik num_hcs = 2; 1282c6fd2807SJeff Garzik } else { 1283c6fd2807SJeff Garzik start_hc = port >> MV_PORT_HC_SHIFT; 1284c6fd2807SJeff Garzik start_port = port; 1285c6fd2807SJeff Garzik num_ports = num_hcs = 1; 1286c6fd2807SJeff Garzik } 1287c6fd2807SJeff Garzik DPRINTK("All registers for port(s) %u-%u:\n", start_port, 1288c6fd2807SJeff Garzik num_ports > 1 ? num_ports - 1 : start_port); 1289c6fd2807SJeff Garzik 1290c6fd2807SJeff Garzik if (NULL != pdev) { 1291c6fd2807SJeff Garzik DPRINTK("PCI config space regs:\n"); 1292c6fd2807SJeff Garzik mv_dump_pci_cfg(pdev, 0x68); 1293c6fd2807SJeff Garzik } 1294c6fd2807SJeff Garzik DPRINTK("PCI regs:\n"); 1295c6fd2807SJeff Garzik mv_dump_mem(mmio_base+0xc00, 0x3c); 1296c6fd2807SJeff Garzik mv_dump_mem(mmio_base+0xd00, 0x34); 1297c6fd2807SJeff Garzik mv_dump_mem(mmio_base+0xf00, 0x4); 1298c6fd2807SJeff Garzik mv_dump_mem(mmio_base+0x1d00, 0x6c); 1299c6fd2807SJeff Garzik for (hc = start_hc; hc < start_hc + num_hcs; hc++) { 1300c6fd2807SJeff Garzik hc_base = mv_hc_base(mmio_base, hc); 1301c6fd2807SJeff Garzik DPRINTK("HC regs (HC %i):\n", hc); 1302c6fd2807SJeff Garzik mv_dump_mem(hc_base, 0x1c); 1303c6fd2807SJeff Garzik } 1304c6fd2807SJeff Garzik for (p = start_port; p < start_port + num_ports; p++) { 1305c6fd2807SJeff Garzik port_base = mv_port_base(mmio_base, p); 1306c6fd2807SJeff Garzik DPRINTK("EDMA regs (port %i):\n", p); 1307c6fd2807SJeff Garzik mv_dump_mem(port_base, 0x54); 1308c6fd2807SJeff Garzik DPRINTK("SATA regs (port %i):\n", p); 1309c6fd2807SJeff Garzik mv_dump_mem(port_base+0x300, 0x60); 1310c6fd2807SJeff Garzik } 1311c6fd2807SJeff Garzik #endif 1312c6fd2807SJeff Garzik } 1313c6fd2807SJeff Garzik 1314c6fd2807SJeff Garzik static unsigned int mv_scr_offset(unsigned int sc_reg_in) 1315c6fd2807SJeff Garzik { 1316c6fd2807SJeff Garzik unsigned int ofs; 1317c6fd2807SJeff Garzik 1318c6fd2807SJeff Garzik switch (sc_reg_in) { 1319c6fd2807SJeff Garzik case SCR_STATUS: 1320c6fd2807SJeff Garzik case SCR_CONTROL: 1321c6fd2807SJeff Garzik case SCR_ERROR: 1322cae5a29dSMark Lord ofs = SATA_STATUS + (sc_reg_in * sizeof(u32)); 1323c6fd2807SJeff Garzik break; 1324c6fd2807SJeff Garzik case SCR_ACTIVE: 1325cae5a29dSMark Lord ofs = SATA_ACTIVE; /* active is not with the others */ 1326c6fd2807SJeff Garzik break; 1327c6fd2807SJeff Garzik default: 1328c6fd2807SJeff Garzik ofs = 0xffffffffU; 1329c6fd2807SJeff Garzik break; 1330c6fd2807SJeff Garzik } 1331c6fd2807SJeff Garzik return ofs; 1332c6fd2807SJeff Garzik } 1333c6fd2807SJeff Garzik 133482ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val) 1335c6fd2807SJeff Garzik { 1336c6fd2807SJeff Garzik unsigned int ofs = mv_scr_offset(sc_reg_in); 1337c6fd2807SJeff Garzik 1338da3dbb17STejun Heo if (ofs != 0xffffffffU) { 133982ef04fbSTejun Heo *val = readl(mv_ap_base(link->ap) + ofs); 1340da3dbb17STejun Heo return 0; 1341da3dbb17STejun Heo } else 1342da3dbb17STejun Heo return -EINVAL; 1343c6fd2807SJeff Garzik } 1344c6fd2807SJeff Garzik 134582ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val) 1346c6fd2807SJeff Garzik { 1347c6fd2807SJeff Garzik unsigned int ofs = mv_scr_offset(sc_reg_in); 1348c6fd2807SJeff Garzik 1349da3dbb17STejun Heo if (ofs != 0xffffffffU) { 135020091773SMark Lord void __iomem *addr = mv_ap_base(link->ap) + ofs; 135120091773SMark Lord if (sc_reg_in == SCR_CONTROL) { 135220091773SMark Lord /* 135320091773SMark Lord * Workaround for 88SX60x1 FEr SATA#26: 135420091773SMark Lord * 135525985edcSLucas De Marchi * COMRESETs have to take care not to accidentally 135620091773SMark Lord * put the drive to sleep when writing SCR_CONTROL. 135720091773SMark Lord * Setting bits 12..15 prevents this problem. 135820091773SMark Lord * 135920091773SMark Lord * So if we see an outbound COMMRESET, set those bits. 136020091773SMark Lord * Ditto for the followup write that clears the reset. 136120091773SMark Lord * 136220091773SMark Lord * The proprietary driver does this for 136320091773SMark Lord * all chip versions, and so do we. 136420091773SMark Lord */ 136520091773SMark Lord if ((val & 0xf) == 1 || (readl(addr) & 0xf) == 1) 136620091773SMark Lord val |= 0xf000; 136720091773SMark Lord } 136820091773SMark Lord writelfl(val, addr); 1369da3dbb17STejun Heo return 0; 1370da3dbb17STejun Heo } else 1371da3dbb17STejun Heo return -EINVAL; 1372c6fd2807SJeff Garzik } 1373c6fd2807SJeff Garzik 1374f273827eSMark Lord static void mv6_dev_config(struct ata_device *adev) 1375f273827eSMark Lord { 1376f273827eSMark Lord /* 1377e49856d8SMark Lord * Deal with Gen-II ("mv6") hardware quirks/restrictions: 1378e49856d8SMark Lord * 1379e49856d8SMark Lord * Gen-II does not support NCQ over a port multiplier 1380e49856d8SMark Lord * (no FIS-based switching). 1381f273827eSMark Lord */ 1382e49856d8SMark Lord if (adev->flags & ATA_DFLAG_NCQ) { 1383352fab70SMark Lord if (sata_pmp_attached(adev->link->ap)) { 1384e49856d8SMark Lord adev->flags &= ~ATA_DFLAG_NCQ; 1385a9a79dfeSJoe Perches ata_dev_info(adev, 1386352fab70SMark Lord "NCQ disabled for command-based switching\n"); 1387352fab70SMark Lord } 1388f273827eSMark Lord } 1389e49856d8SMark Lord } 1390f273827eSMark Lord 13913e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc) 13923e4a1391SMark Lord { 13933e4a1391SMark Lord struct ata_link *link = qc->dev->link; 13943e4a1391SMark Lord struct ata_port *ap = link->ap; 13953e4a1391SMark Lord struct mv_port_priv *pp = ap->private_data; 13963e4a1391SMark Lord 13973e4a1391SMark Lord /* 139829d187bbSMark Lord * Don't allow new commands if we're in a delayed EH state 139929d187bbSMark Lord * for NCQ and/or FIS-based switching. 140029d187bbSMark Lord */ 140129d187bbSMark Lord if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) 140229d187bbSMark Lord return ATA_DEFER_PORT; 1403159a7ff7SGwendal Grignou 1404159a7ff7SGwendal Grignou /* PIO commands need exclusive link: no other commands [DMA or PIO] 1405159a7ff7SGwendal Grignou * can run concurrently. 1406159a7ff7SGwendal Grignou * set excl_link when we want to send a PIO command in DMA mode 1407159a7ff7SGwendal Grignou * or a non-NCQ command in NCQ mode. 1408159a7ff7SGwendal Grignou * When we receive a command from that link, and there are no 1409159a7ff7SGwendal Grignou * outstanding commands, mark a flag to clear excl_link and let 1410159a7ff7SGwendal Grignou * the command go through. 1411159a7ff7SGwendal Grignou */ 1412159a7ff7SGwendal Grignou if (unlikely(ap->excl_link)) { 1413159a7ff7SGwendal Grignou if (link == ap->excl_link) { 1414159a7ff7SGwendal Grignou if (ap->nr_active_links) 1415159a7ff7SGwendal Grignou return ATA_DEFER_PORT; 1416159a7ff7SGwendal Grignou qc->flags |= ATA_QCFLAG_CLEAR_EXCL; 1417159a7ff7SGwendal Grignou return 0; 1418159a7ff7SGwendal Grignou } else 1419159a7ff7SGwendal Grignou return ATA_DEFER_PORT; 1420159a7ff7SGwendal Grignou } 1421159a7ff7SGwendal Grignou 142229d187bbSMark Lord /* 14233e4a1391SMark Lord * If the port is completely idle, then allow the new qc. 14243e4a1391SMark Lord */ 14253e4a1391SMark Lord if (ap->nr_active_links == 0) 14263e4a1391SMark Lord return 0; 14273e4a1391SMark Lord 14283e4a1391SMark Lord /* 14294bdee6c5STejun Heo * The port is operating in host queuing mode (EDMA) with NCQ 14304bdee6c5STejun Heo * enabled, allow multiple NCQ commands. EDMA also allows 14314bdee6c5STejun Heo * queueing multiple DMA commands but libata core currently 14324bdee6c5STejun Heo * doesn't allow it. 14333e4a1391SMark Lord */ 14344bdee6c5STejun Heo if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) && 1435159a7ff7SGwendal Grignou (pp->pp_flags & MV_PP_FLAG_NCQ_EN)) { 1436159a7ff7SGwendal Grignou if (ata_is_ncq(qc->tf.protocol)) 14373e4a1391SMark Lord return 0; 1438159a7ff7SGwendal Grignou else { 1439159a7ff7SGwendal Grignou ap->excl_link = link; 1440159a7ff7SGwendal Grignou return ATA_DEFER_PORT; 1441159a7ff7SGwendal Grignou } 1442159a7ff7SGwendal Grignou } 14434bdee6c5STejun Heo 14443e4a1391SMark Lord return ATA_DEFER_PORT; 14453e4a1391SMark Lord } 14463e4a1391SMark Lord 144708da1759SMark Lord static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs) 1448e49856d8SMark Lord { 144908da1759SMark Lord struct mv_port_priv *pp = ap->private_data; 145008da1759SMark Lord void __iomem *port_mmio; 145100f42eabSMark Lord 145208da1759SMark Lord u32 fiscfg, *old_fiscfg = &pp->cached.fiscfg; 145308da1759SMark Lord u32 ltmode, *old_ltmode = &pp->cached.ltmode; 145408da1759SMark Lord u32 haltcond, *old_haltcond = &pp->cached.haltcond; 145500f42eabSMark Lord 145608da1759SMark Lord ltmode = *old_ltmode & ~LTMODE_BIT8; 145708da1759SMark Lord haltcond = *old_haltcond | EDMA_ERR_DEV; 145800f42eabSMark Lord 145900f42eabSMark Lord if (want_fbs) { 146008da1759SMark Lord fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC; 146108da1759SMark Lord ltmode = *old_ltmode | LTMODE_BIT8; 14624c299ca3SMark Lord if (want_ncq) 146308da1759SMark Lord haltcond &= ~EDMA_ERR_DEV; 14644c299ca3SMark Lord else 146508da1759SMark Lord fiscfg |= FISCFG_WAIT_DEV_ERR; 146608da1759SMark Lord } else { 146708da1759SMark Lord fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR); 1468e49856d8SMark Lord } 146900f42eabSMark Lord 147008da1759SMark Lord port_mmio = mv_ap_base(ap); 1471cae5a29dSMark Lord mv_write_cached_reg(port_mmio + FISCFG, old_fiscfg, fiscfg); 1472cae5a29dSMark Lord mv_write_cached_reg(port_mmio + LTMODE, old_ltmode, ltmode); 1473cae5a29dSMark Lord mv_write_cached_reg(port_mmio + EDMA_HALTCOND, old_haltcond, haltcond); 1474e49856d8SMark Lord } 1475c6fd2807SJeff Garzik 1476dd2890f6SMark Lord static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq) 1477dd2890f6SMark Lord { 1478dd2890f6SMark Lord struct mv_host_priv *hpriv = ap->host->private_data; 1479dd2890f6SMark Lord u32 old, new; 1480dd2890f6SMark Lord 1481dd2890f6SMark Lord /* workaround for 88SX60x1 FEr SATA#25 (part 1) */ 1482cae5a29dSMark Lord old = readl(hpriv->base + GPIO_PORT_CTL); 1483dd2890f6SMark Lord if (want_ncq) 1484dd2890f6SMark Lord new = old | (1 << 22); 1485dd2890f6SMark Lord else 1486dd2890f6SMark Lord new = old & ~(1 << 22); 1487dd2890f6SMark Lord if (new != old) 1488cae5a29dSMark Lord writel(new, hpriv->base + GPIO_PORT_CTL); 1489dd2890f6SMark Lord } 1490dd2890f6SMark Lord 1491c01e8a23SMark Lord /** 1492c01e8a23SMark Lord * mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma 1493c01e8a23SMark Lord * @ap: Port being initialized 1494c01e8a23SMark Lord * 1495c01e8a23SMark Lord * There are two DMA modes on these chips: basic DMA, and EDMA. 1496c01e8a23SMark Lord * 1497c01e8a23SMark Lord * Bit-0 of the "EDMA RESERVED" register enables/disables use 1498c01e8a23SMark Lord * of basic DMA on the GEN_IIE versions of the chips. 1499c01e8a23SMark Lord * 1500c01e8a23SMark Lord * This bit survives EDMA resets, and must be set for basic DMA 1501c01e8a23SMark Lord * to function, and should be cleared when EDMA is active. 1502c01e8a23SMark Lord */ 1503c01e8a23SMark Lord static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma) 1504c01e8a23SMark Lord { 1505c01e8a23SMark Lord struct mv_port_priv *pp = ap->private_data; 1506c01e8a23SMark Lord u32 new, *old = &pp->cached.unknown_rsvd; 1507c01e8a23SMark Lord 1508c01e8a23SMark Lord if (enable_bmdma) 1509c01e8a23SMark Lord new = *old | 1; 1510c01e8a23SMark Lord else 1511c01e8a23SMark Lord new = *old & ~1; 1512cae5a29dSMark Lord mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD, old, new); 1513c01e8a23SMark Lord } 1514c01e8a23SMark Lord 1515000b344fSMark Lord /* 1516000b344fSMark Lord * SOC chips have an issue whereby the HDD LEDs don't always blink 1517000b344fSMark Lord * during I/O when NCQ is enabled. Enabling a special "LED blink" mode 1518000b344fSMark Lord * of the SOC takes care of it, generating a steady blink rate when 1519000b344fSMark Lord * any drive on the chip is active. 1520000b344fSMark Lord * 1521000b344fSMark Lord * Unfortunately, the blink mode is a global hardware setting for the SOC, 1522000b344fSMark Lord * so we must use it whenever at least one port on the SOC has NCQ enabled. 1523000b344fSMark Lord * 1524000b344fSMark Lord * We turn "LED blink" off when NCQ is not in use anywhere, because the normal 1525000b344fSMark Lord * LED operation works then, and provides better (more accurate) feedback. 1526000b344fSMark Lord * 1527000b344fSMark Lord * Note that this code assumes that an SOC never has more than one HC onboard. 1528000b344fSMark Lord */ 1529000b344fSMark Lord static void mv_soc_led_blink_enable(struct ata_port *ap) 1530000b344fSMark Lord { 1531000b344fSMark Lord struct ata_host *host = ap->host; 1532000b344fSMark Lord struct mv_host_priv *hpriv = host->private_data; 1533000b344fSMark Lord void __iomem *hc_mmio; 1534000b344fSMark Lord u32 led_ctrl; 1535000b344fSMark Lord 1536000b344fSMark Lord if (hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN) 1537000b344fSMark Lord return; 1538000b344fSMark Lord hpriv->hp_flags |= MV_HP_QUIRK_LED_BLINK_EN; 1539000b344fSMark Lord hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no); 1540cae5a29dSMark Lord led_ctrl = readl(hc_mmio + SOC_LED_CTRL); 1541cae5a29dSMark Lord writel(led_ctrl | SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL); 1542000b344fSMark Lord } 1543000b344fSMark Lord 1544000b344fSMark Lord static void mv_soc_led_blink_disable(struct ata_port *ap) 1545000b344fSMark Lord { 1546000b344fSMark Lord struct ata_host *host = ap->host; 1547000b344fSMark Lord struct mv_host_priv *hpriv = host->private_data; 1548000b344fSMark Lord void __iomem *hc_mmio; 1549000b344fSMark Lord u32 led_ctrl; 1550000b344fSMark Lord unsigned int port; 1551000b344fSMark Lord 1552000b344fSMark Lord if (!(hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN)) 1553000b344fSMark Lord return; 1554000b344fSMark Lord 1555000b344fSMark Lord /* disable led-blink only if no ports are using NCQ */ 1556000b344fSMark Lord for (port = 0; port < hpriv->n_ports; port++) { 1557000b344fSMark Lord struct ata_port *this_ap = host->ports[port]; 1558000b344fSMark Lord struct mv_port_priv *pp = this_ap->private_data; 1559000b344fSMark Lord 1560000b344fSMark Lord if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) 1561000b344fSMark Lord return; 1562000b344fSMark Lord } 1563000b344fSMark Lord 1564000b344fSMark Lord hpriv->hp_flags &= ~MV_HP_QUIRK_LED_BLINK_EN; 1565000b344fSMark Lord hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no); 1566cae5a29dSMark Lord led_ctrl = readl(hc_mmio + SOC_LED_CTRL); 1567cae5a29dSMark Lord writel(led_ctrl & ~SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL); 1568000b344fSMark Lord } 1569000b344fSMark Lord 157000b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma) 1571c6fd2807SJeff Garzik { 1572c6fd2807SJeff Garzik u32 cfg; 1573e12bef50SMark Lord struct mv_port_priv *pp = ap->private_data; 1574e12bef50SMark Lord struct mv_host_priv *hpriv = ap->host->private_data; 1575e12bef50SMark Lord void __iomem *port_mmio = mv_ap_base(ap); 1576c6fd2807SJeff Garzik 1577c6fd2807SJeff Garzik /* set up non-NCQ EDMA configuration */ 1578c6fd2807SJeff Garzik cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */ 1579d16ab3f6SMark Lord pp->pp_flags &= 1580d16ab3f6SMark Lord ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY); 1581c6fd2807SJeff Garzik 1582c6fd2807SJeff Garzik if (IS_GEN_I(hpriv)) 1583c6fd2807SJeff Garzik cfg |= (1 << 8); /* enab config burst size mask */ 1584c6fd2807SJeff Garzik 1585dd2890f6SMark Lord else if (IS_GEN_II(hpriv)) { 1586c6fd2807SJeff Garzik cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN; 1587dd2890f6SMark Lord mv_60x1_errata_sata25(ap, want_ncq); 1588c6fd2807SJeff Garzik 1589dd2890f6SMark Lord } else if (IS_GEN_IIE(hpriv)) { 159000f42eabSMark Lord int want_fbs = sata_pmp_attached(ap); 159100f42eabSMark Lord /* 159200f42eabSMark Lord * Possible future enhancement: 159300f42eabSMark Lord * 159400f42eabSMark Lord * The chip can use FBS with non-NCQ, if we allow it, 159500f42eabSMark Lord * But first we need to have the error handling in place 159600f42eabSMark Lord * for this mode (datasheet section 7.3.15.4.2.3). 159700f42eabSMark Lord * So disallow non-NCQ FBS for now. 159800f42eabSMark Lord */ 159900f42eabSMark Lord want_fbs &= want_ncq; 160000f42eabSMark Lord 160108da1759SMark Lord mv_config_fbs(ap, want_ncq, want_fbs); 160200f42eabSMark Lord 160300f42eabSMark Lord if (want_fbs) { 160400f42eabSMark Lord pp->pp_flags |= MV_PP_FLAG_FBS_EN; 160500f42eabSMark Lord cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */ 160600f42eabSMark Lord } 160700f42eabSMark Lord 1608e728eabeSJeff Garzik cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */ 160900b81235SMark Lord if (want_edma) { 1610e728eabeSJeff Garzik cfg |= (1 << 22); /* enab 4-entry host queue cache */ 16111f398472SMark Lord if (!IS_SOC(hpriv)) 1612c6fd2807SJeff Garzik cfg |= (1 << 18); /* enab early completion */ 161300b81235SMark Lord } 1614616d4a98SMark Lord if (hpriv->hp_flags & MV_HP_CUT_THROUGH) 1615616d4a98SMark Lord cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */ 1616c01e8a23SMark Lord mv_bmdma_enable_iie(ap, !want_edma); 1617000b344fSMark Lord 1618000b344fSMark Lord if (IS_SOC(hpriv)) { 1619000b344fSMark Lord if (want_ncq) 1620000b344fSMark Lord mv_soc_led_blink_enable(ap); 1621000b344fSMark Lord else 1622000b344fSMark Lord mv_soc_led_blink_disable(ap); 1623000b344fSMark Lord } 1624c6fd2807SJeff Garzik } 1625c6fd2807SJeff Garzik 162672109168SMark Lord if (want_ncq) { 162772109168SMark Lord cfg |= EDMA_CFG_NCQ; 162872109168SMark Lord pp->pp_flags |= MV_PP_FLAG_NCQ_EN; 162900b81235SMark Lord } 163072109168SMark Lord 1631cae5a29dSMark Lord writelfl(cfg, port_mmio + EDMA_CFG); 1632c6fd2807SJeff Garzik } 1633c6fd2807SJeff Garzik 1634da2fa9baSMark Lord static void mv_port_free_dma_mem(struct ata_port *ap) 1635da2fa9baSMark Lord { 1636da2fa9baSMark Lord struct mv_host_priv *hpriv = ap->host->private_data; 1637da2fa9baSMark Lord struct mv_port_priv *pp = ap->private_data; 1638eb73d558SMark Lord int tag; 1639da2fa9baSMark Lord 1640da2fa9baSMark Lord if (pp->crqb) { 1641da2fa9baSMark Lord dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma); 1642da2fa9baSMark Lord pp->crqb = NULL; 1643da2fa9baSMark Lord } 1644da2fa9baSMark Lord if (pp->crpb) { 1645da2fa9baSMark Lord dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma); 1646da2fa9baSMark Lord pp->crpb = NULL; 1647da2fa9baSMark Lord } 1648eb73d558SMark Lord /* 1649eb73d558SMark Lord * For GEN_I, there's no NCQ, so we have only a single sg_tbl. 1650eb73d558SMark Lord * For later hardware, we have one unique sg_tbl per NCQ tag. 1651eb73d558SMark Lord */ 1652eb73d558SMark Lord for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) { 1653eb73d558SMark Lord if (pp->sg_tbl[tag]) { 1654eb73d558SMark Lord if (tag == 0 || !IS_GEN_I(hpriv)) 1655eb73d558SMark Lord dma_pool_free(hpriv->sg_tbl_pool, 1656eb73d558SMark Lord pp->sg_tbl[tag], 1657eb73d558SMark Lord pp->sg_tbl_dma[tag]); 1658eb73d558SMark Lord pp->sg_tbl[tag] = NULL; 1659eb73d558SMark Lord } 1660da2fa9baSMark Lord } 1661da2fa9baSMark Lord } 1662da2fa9baSMark Lord 1663c6fd2807SJeff Garzik /** 1664c6fd2807SJeff Garzik * mv_port_start - Port specific init/start routine. 1665c6fd2807SJeff Garzik * @ap: ATA channel to manipulate 1666c6fd2807SJeff Garzik * 1667c6fd2807SJeff Garzik * Allocate and point to DMA memory, init port private memory, 1668c6fd2807SJeff Garzik * zero indices. 1669c6fd2807SJeff Garzik * 1670c6fd2807SJeff Garzik * LOCKING: 1671c6fd2807SJeff Garzik * Inherited from caller. 1672c6fd2807SJeff Garzik */ 1673c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap) 1674c6fd2807SJeff Garzik { 1675cca3974eSJeff Garzik struct device *dev = ap->host->dev; 1676cca3974eSJeff Garzik struct mv_host_priv *hpriv = ap->host->private_data; 1677c6fd2807SJeff Garzik struct mv_port_priv *pp; 1678933cb8e5SMark Lord unsigned long flags; 1679dde20207SJames Bottomley int tag; 1680c6fd2807SJeff Garzik 168124dc5f33STejun Heo pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL); 1682c6fd2807SJeff Garzik if (!pp) 168324dc5f33STejun Heo return -ENOMEM; 1684da2fa9baSMark Lord ap->private_data = pp; 1685c6fd2807SJeff Garzik 1686da2fa9baSMark Lord pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma); 1687da2fa9baSMark Lord if (!pp->crqb) 1688da2fa9baSMark Lord return -ENOMEM; 1689da2fa9baSMark Lord memset(pp->crqb, 0, MV_CRQB_Q_SZ); 1690c6fd2807SJeff Garzik 1691da2fa9baSMark Lord pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma); 1692da2fa9baSMark Lord if (!pp->crpb) 1693da2fa9baSMark Lord goto out_port_free_dma_mem; 1694da2fa9baSMark Lord memset(pp->crpb, 0, MV_CRPB_Q_SZ); 1695c6fd2807SJeff Garzik 16963bd0a70eSMark Lord /* 6041/6081 Rev. "C0" (and newer) are okay with async notify */ 16973bd0a70eSMark Lord if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0) 16983bd0a70eSMark Lord ap->flags |= ATA_FLAG_AN; 1699eb73d558SMark Lord /* 1700eb73d558SMark Lord * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl. 1701eb73d558SMark Lord * For later hardware, we need one unique sg_tbl per NCQ tag. 1702eb73d558SMark Lord */ 1703eb73d558SMark Lord for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) { 1704eb73d558SMark Lord if (tag == 0 || !IS_GEN_I(hpriv)) { 1705eb73d558SMark Lord pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool, 1706eb73d558SMark Lord GFP_KERNEL, &pp->sg_tbl_dma[tag]); 1707eb73d558SMark Lord if (!pp->sg_tbl[tag]) 1708da2fa9baSMark Lord goto out_port_free_dma_mem; 1709eb73d558SMark Lord } else { 1710eb73d558SMark Lord pp->sg_tbl[tag] = pp->sg_tbl[0]; 1711eb73d558SMark Lord pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0]; 1712eb73d558SMark Lord } 1713eb73d558SMark Lord } 1714933cb8e5SMark Lord 1715933cb8e5SMark Lord spin_lock_irqsave(ap->lock, flags); 171608da1759SMark Lord mv_save_cached_regs(ap); 171766e57a2cSMark Lord mv_edma_cfg(ap, 0, 0); 1718933cb8e5SMark Lord spin_unlock_irqrestore(ap->lock, flags); 1719933cb8e5SMark Lord 1720c6fd2807SJeff Garzik return 0; 1721da2fa9baSMark Lord 1722da2fa9baSMark Lord out_port_free_dma_mem: 1723da2fa9baSMark Lord mv_port_free_dma_mem(ap); 1724da2fa9baSMark Lord return -ENOMEM; 1725c6fd2807SJeff Garzik } 1726c6fd2807SJeff Garzik 1727c6fd2807SJeff Garzik /** 1728c6fd2807SJeff Garzik * mv_port_stop - Port specific cleanup/stop routine. 1729c6fd2807SJeff Garzik * @ap: ATA channel to manipulate 1730c6fd2807SJeff Garzik * 1731c6fd2807SJeff Garzik * Stop DMA, cleanup port memory. 1732c6fd2807SJeff Garzik * 1733c6fd2807SJeff Garzik * LOCKING: 1734cca3974eSJeff Garzik * This routine uses the host lock to protect the DMA stop. 1735c6fd2807SJeff Garzik */ 1736c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap) 1737c6fd2807SJeff Garzik { 1738933cb8e5SMark Lord unsigned long flags; 1739933cb8e5SMark Lord 1740933cb8e5SMark Lord spin_lock_irqsave(ap->lock, flags); 1741e12bef50SMark Lord mv_stop_edma(ap); 174288e675e1SMark Lord mv_enable_port_irqs(ap, 0); 1743933cb8e5SMark Lord spin_unlock_irqrestore(ap->lock, flags); 1744da2fa9baSMark Lord mv_port_free_dma_mem(ap); 1745c6fd2807SJeff Garzik } 1746c6fd2807SJeff Garzik 1747c6fd2807SJeff Garzik /** 1748c6fd2807SJeff Garzik * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries 1749c6fd2807SJeff Garzik * @qc: queued command whose SG list to source from 1750c6fd2807SJeff Garzik * 1751c6fd2807SJeff Garzik * Populate the SG list and mark the last entry. 1752c6fd2807SJeff Garzik * 1753c6fd2807SJeff Garzik * LOCKING: 1754c6fd2807SJeff Garzik * Inherited from caller. 1755c6fd2807SJeff Garzik */ 17566c08772eSJeff Garzik static void mv_fill_sg(struct ata_queued_cmd *qc) 1757c6fd2807SJeff Garzik { 1758c6fd2807SJeff Garzik struct mv_port_priv *pp = qc->ap->private_data; 1759c6fd2807SJeff Garzik struct scatterlist *sg; 17603be6cbd7SJeff Garzik struct mv_sg *mv_sg, *last_sg = NULL; 1761ff2aeb1eSTejun Heo unsigned int si; 1762c6fd2807SJeff Garzik 1763eb73d558SMark Lord mv_sg = pp->sg_tbl[qc->tag]; 1764ff2aeb1eSTejun Heo for_each_sg(qc->sg, sg, qc->n_elem, si) { 1765d88184fbSJeff Garzik dma_addr_t addr = sg_dma_address(sg); 1766d88184fbSJeff Garzik u32 sg_len = sg_dma_len(sg); 1767c6fd2807SJeff Garzik 17684007b493SOlof Johansson while (sg_len) { 17694007b493SOlof Johansson u32 offset = addr & 0xffff; 17704007b493SOlof Johansson u32 len = sg_len; 17714007b493SOlof Johansson 177232cd11a6SMark Lord if (offset + len > 0x10000) 17734007b493SOlof Johansson len = 0x10000 - offset; 17744007b493SOlof Johansson 1775d88184fbSJeff Garzik mv_sg->addr = cpu_to_le32(addr & 0xffffffff); 1776d88184fbSJeff Garzik mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16); 17776c08772eSJeff Garzik mv_sg->flags_size = cpu_to_le32(len & 0xffff); 177832cd11a6SMark Lord mv_sg->reserved = 0; 1779c6fd2807SJeff Garzik 17804007b493SOlof Johansson sg_len -= len; 17814007b493SOlof Johansson addr += len; 17824007b493SOlof Johansson 17833be6cbd7SJeff Garzik last_sg = mv_sg; 1784d88184fbSJeff Garzik mv_sg++; 1785c6fd2807SJeff Garzik } 17864007b493SOlof Johansson } 17873be6cbd7SJeff Garzik 17883be6cbd7SJeff Garzik if (likely(last_sg)) 17893be6cbd7SJeff Garzik last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL); 179032cd11a6SMark Lord mb(); /* ensure data structure is visible to the chipset */ 1791c6fd2807SJeff Garzik } 1792c6fd2807SJeff Garzik 17935796d1c4SJeff Garzik static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last) 1794c6fd2807SJeff Garzik { 1795c6fd2807SJeff Garzik u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS | 1796c6fd2807SJeff Garzik (last ? CRQB_CMD_LAST : 0); 1797c6fd2807SJeff Garzik *cmdw = cpu_to_le16(tmp); 1798c6fd2807SJeff Garzik } 1799c6fd2807SJeff Garzik 1800c6fd2807SJeff Garzik /** 1801da14265eSMark Lord * mv_sff_irq_clear - Clear hardware interrupt after DMA. 1802da14265eSMark Lord * @ap: Port associated with this ATA transaction. 1803da14265eSMark Lord * 1804da14265eSMark Lord * We need this only for ATAPI bmdma transactions, 1805da14265eSMark Lord * as otherwise we experience spurious interrupts 1806da14265eSMark Lord * after libata-sff handles the bmdma interrupts. 1807da14265eSMark Lord */ 1808da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap) 1809da14265eSMark Lord { 1810da14265eSMark Lord mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ); 1811da14265eSMark Lord } 1812da14265eSMark Lord 1813da14265eSMark Lord /** 1814da14265eSMark Lord * mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA. 1815da14265eSMark Lord * @qc: queued command to check for chipset/DMA compatibility. 1816da14265eSMark Lord * 1817da14265eSMark Lord * The bmdma engines cannot handle speculative data sizes 1818da14265eSMark Lord * (bytecount under/over flow). So only allow DMA for 1819da14265eSMark Lord * data transfer commands with known data sizes. 1820da14265eSMark Lord * 1821da14265eSMark Lord * LOCKING: 1822da14265eSMark Lord * Inherited from caller. 1823da14265eSMark Lord */ 1824da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc) 1825da14265eSMark Lord { 1826da14265eSMark Lord struct scsi_cmnd *scmd = qc->scsicmd; 1827da14265eSMark Lord 1828da14265eSMark Lord if (scmd) { 1829da14265eSMark Lord switch (scmd->cmnd[0]) { 1830da14265eSMark Lord case READ_6: 1831da14265eSMark Lord case READ_10: 1832da14265eSMark Lord case READ_12: 1833da14265eSMark Lord case WRITE_6: 1834da14265eSMark Lord case WRITE_10: 1835da14265eSMark Lord case WRITE_12: 1836da14265eSMark Lord case GPCMD_READ_CD: 1837da14265eSMark Lord case GPCMD_SEND_DVD_STRUCTURE: 1838da14265eSMark Lord case GPCMD_SEND_CUE_SHEET: 1839da14265eSMark Lord return 0; /* DMA is safe */ 1840da14265eSMark Lord } 1841da14265eSMark Lord } 1842da14265eSMark Lord return -EOPNOTSUPP; /* use PIO instead */ 1843da14265eSMark Lord } 1844da14265eSMark Lord 1845da14265eSMark Lord /** 1846da14265eSMark Lord * mv_bmdma_setup - Set up BMDMA transaction 1847da14265eSMark Lord * @qc: queued command to prepare DMA for. 1848da14265eSMark Lord * 1849da14265eSMark Lord * LOCKING: 1850da14265eSMark Lord * Inherited from caller. 1851da14265eSMark Lord */ 1852da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc) 1853da14265eSMark Lord { 1854da14265eSMark Lord struct ata_port *ap = qc->ap; 1855da14265eSMark Lord void __iomem *port_mmio = mv_ap_base(ap); 1856da14265eSMark Lord struct mv_port_priv *pp = ap->private_data; 1857da14265eSMark Lord 1858da14265eSMark Lord mv_fill_sg(qc); 1859da14265eSMark Lord 1860da14265eSMark Lord /* clear all DMA cmd bits */ 1861cae5a29dSMark Lord writel(0, port_mmio + BMDMA_CMD); 1862da14265eSMark Lord 1863da14265eSMark Lord /* load PRD table addr. */ 1864da14265eSMark Lord writel((pp->sg_tbl_dma[qc->tag] >> 16) >> 16, 1865cae5a29dSMark Lord port_mmio + BMDMA_PRD_HIGH); 1866da14265eSMark Lord writelfl(pp->sg_tbl_dma[qc->tag], 1867cae5a29dSMark Lord port_mmio + BMDMA_PRD_LOW); 1868da14265eSMark Lord 1869da14265eSMark Lord /* issue r/w command */ 1870da14265eSMark Lord ap->ops->sff_exec_command(ap, &qc->tf); 1871da14265eSMark Lord } 1872da14265eSMark Lord 1873da14265eSMark Lord /** 1874da14265eSMark Lord * mv_bmdma_start - Start a BMDMA transaction 1875da14265eSMark Lord * @qc: queued command to start DMA on. 1876da14265eSMark Lord * 1877da14265eSMark Lord * LOCKING: 1878da14265eSMark Lord * Inherited from caller. 1879da14265eSMark Lord */ 1880da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc) 1881da14265eSMark Lord { 1882da14265eSMark Lord struct ata_port *ap = qc->ap; 1883da14265eSMark Lord void __iomem *port_mmio = mv_ap_base(ap); 1884da14265eSMark Lord unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE); 1885da14265eSMark Lord u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START; 1886da14265eSMark Lord 1887da14265eSMark Lord /* start host DMA transaction */ 1888cae5a29dSMark Lord writelfl(cmd, port_mmio + BMDMA_CMD); 1889da14265eSMark Lord } 1890da14265eSMark Lord 1891da14265eSMark Lord /** 1892da14265eSMark Lord * mv_bmdma_stop - Stop BMDMA transfer 1893da14265eSMark Lord * @qc: queued command to stop DMA on. 1894da14265eSMark Lord * 1895da14265eSMark Lord * Clears the ATA_DMA_START flag in the bmdma control register 1896da14265eSMark Lord * 1897da14265eSMark Lord * LOCKING: 1898da14265eSMark Lord * Inherited from caller. 1899da14265eSMark Lord */ 190044b73380SMark Lord static void mv_bmdma_stop_ap(struct ata_port *ap) 1901da14265eSMark Lord { 1902da14265eSMark Lord void __iomem *port_mmio = mv_ap_base(ap); 1903da14265eSMark Lord u32 cmd; 1904da14265eSMark Lord 1905da14265eSMark Lord /* clear start/stop bit */ 1906cae5a29dSMark Lord cmd = readl(port_mmio + BMDMA_CMD); 190744b73380SMark Lord if (cmd & ATA_DMA_START) { 1908da14265eSMark Lord cmd &= ~ATA_DMA_START; 1909cae5a29dSMark Lord writelfl(cmd, port_mmio + BMDMA_CMD); 1910da14265eSMark Lord 1911da14265eSMark Lord /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */ 1912da14265eSMark Lord ata_sff_dma_pause(ap); 1913da14265eSMark Lord } 191444b73380SMark Lord } 191544b73380SMark Lord 191644b73380SMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc) 191744b73380SMark Lord { 191844b73380SMark Lord mv_bmdma_stop_ap(qc->ap); 191944b73380SMark Lord } 1920da14265eSMark Lord 1921da14265eSMark Lord /** 1922da14265eSMark Lord * mv_bmdma_status - Read BMDMA status 1923da14265eSMark Lord * @ap: port for which to retrieve DMA status. 1924da14265eSMark Lord * 1925da14265eSMark Lord * Read and return equivalent of the sff BMDMA status register. 1926da14265eSMark Lord * 1927da14265eSMark Lord * LOCKING: 1928da14265eSMark Lord * Inherited from caller. 1929da14265eSMark Lord */ 1930da14265eSMark Lord static u8 mv_bmdma_status(struct ata_port *ap) 1931da14265eSMark Lord { 1932da14265eSMark Lord void __iomem *port_mmio = mv_ap_base(ap); 1933da14265eSMark Lord u32 reg, status; 1934da14265eSMark Lord 1935da14265eSMark Lord /* 1936da14265eSMark Lord * Other bits are valid only if ATA_DMA_ACTIVE==0, 1937da14265eSMark Lord * and the ATA_DMA_INTR bit doesn't exist. 1938da14265eSMark Lord */ 1939cae5a29dSMark Lord reg = readl(port_mmio + BMDMA_STATUS); 1940da14265eSMark Lord if (reg & ATA_DMA_ACTIVE) 1941da14265eSMark Lord status = ATA_DMA_ACTIVE; 194244b73380SMark Lord else if (reg & ATA_DMA_ERR) 1943da14265eSMark Lord status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR; 194444b73380SMark Lord else { 194544b73380SMark Lord /* 194644b73380SMark Lord * Just because DMA_ACTIVE is 0 (DMA completed), 194744b73380SMark Lord * this does _not_ mean the device is "done". 194844b73380SMark Lord * So we should not yet be signalling ATA_DMA_INTR 194944b73380SMark Lord * in some cases. Eg. DSM/TRIM, and perhaps others. 195044b73380SMark Lord */ 195144b73380SMark Lord mv_bmdma_stop_ap(ap); 195244b73380SMark Lord if (ioread8(ap->ioaddr.altstatus_addr) & ATA_BUSY) 195344b73380SMark Lord status = 0; 195444b73380SMark Lord else 195544b73380SMark Lord status = ATA_DMA_INTR; 195644b73380SMark Lord } 1957da14265eSMark Lord return status; 1958da14265eSMark Lord } 1959da14265eSMark Lord 1960299b3f8dSMark Lord static void mv_rw_multi_errata_sata24(struct ata_queued_cmd *qc) 1961299b3f8dSMark Lord { 1962299b3f8dSMark Lord struct ata_taskfile *tf = &qc->tf; 1963299b3f8dSMark Lord /* 1964299b3f8dSMark Lord * Workaround for 88SX60x1 FEr SATA#24. 1965299b3f8dSMark Lord * 1966299b3f8dSMark Lord * Chip may corrupt WRITEs if multi_count >= 4kB. 1967299b3f8dSMark Lord * Note that READs are unaffected. 1968299b3f8dSMark Lord * 1969299b3f8dSMark Lord * It's not clear if this errata really means "4K bytes", 1970299b3f8dSMark Lord * or if it always happens for multi_count > 7 1971299b3f8dSMark Lord * regardless of device sector_size. 1972299b3f8dSMark Lord * 1973299b3f8dSMark Lord * So, for safety, any write with multi_count > 7 1974299b3f8dSMark Lord * gets converted here into a regular PIO write instead: 1975299b3f8dSMark Lord */ 1976299b3f8dSMark Lord if ((tf->flags & ATA_TFLAG_WRITE) && is_multi_taskfile(tf)) { 1977299b3f8dSMark Lord if (qc->dev->multi_count > 7) { 1978299b3f8dSMark Lord switch (tf->command) { 1979299b3f8dSMark Lord case ATA_CMD_WRITE_MULTI: 1980299b3f8dSMark Lord tf->command = ATA_CMD_PIO_WRITE; 1981299b3f8dSMark Lord break; 1982299b3f8dSMark Lord case ATA_CMD_WRITE_MULTI_FUA_EXT: 1983299b3f8dSMark Lord tf->flags &= ~ATA_TFLAG_FUA; /* ugh */ 1984299b3f8dSMark Lord /* fall through */ 1985299b3f8dSMark Lord case ATA_CMD_WRITE_MULTI_EXT: 1986299b3f8dSMark Lord tf->command = ATA_CMD_PIO_WRITE_EXT; 1987299b3f8dSMark Lord break; 1988299b3f8dSMark Lord } 1989299b3f8dSMark Lord } 1990299b3f8dSMark Lord } 1991299b3f8dSMark Lord } 1992299b3f8dSMark Lord 1993da14265eSMark Lord /** 1994c6fd2807SJeff Garzik * mv_qc_prep - Host specific command preparation. 1995c6fd2807SJeff Garzik * @qc: queued command to prepare 1996c6fd2807SJeff Garzik * 1997c6fd2807SJeff Garzik * This routine simply redirects to the general purpose routine 1998c6fd2807SJeff Garzik * if command is not DMA. Else, it handles prep of the CRQB 1999c6fd2807SJeff Garzik * (command request block), does some sanity checking, and calls 2000c6fd2807SJeff Garzik * the SG load routine. 2001c6fd2807SJeff Garzik * 2002c6fd2807SJeff Garzik * LOCKING: 2003c6fd2807SJeff Garzik * Inherited from caller. 2004c6fd2807SJeff Garzik */ 2005c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc) 2006c6fd2807SJeff Garzik { 2007c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 2008c6fd2807SJeff Garzik struct mv_port_priv *pp = ap->private_data; 2009c6fd2807SJeff Garzik __le16 *cw; 20108d2b450dSMark Lord struct ata_taskfile *tf = &qc->tf; 2011c6fd2807SJeff Garzik u16 flags = 0; 2012c6fd2807SJeff Garzik unsigned in_index; 2013c6fd2807SJeff Garzik 2014299b3f8dSMark Lord switch (tf->protocol) { 2015299b3f8dSMark Lord case ATA_PROT_DMA: 201644b73380SMark Lord if (tf->command == ATA_CMD_DSM) 201744b73380SMark Lord return; 201844b73380SMark Lord /* fall-thru */ 2019299b3f8dSMark Lord case ATA_PROT_NCQ: 2020299b3f8dSMark Lord break; /* continue below */ 2021299b3f8dSMark Lord case ATA_PROT_PIO: 2022299b3f8dSMark Lord mv_rw_multi_errata_sata24(qc); 2023c6fd2807SJeff Garzik return; 2024299b3f8dSMark Lord default: 2025299b3f8dSMark Lord return; 2026299b3f8dSMark Lord } 2027c6fd2807SJeff Garzik 2028c6fd2807SJeff Garzik /* Fill in command request block 2029c6fd2807SJeff Garzik */ 20308d2b450dSMark Lord if (!(tf->flags & ATA_TFLAG_WRITE)) 2031c6fd2807SJeff Garzik flags |= CRQB_FLAG_READ; 2032c6fd2807SJeff Garzik WARN_ON(MV_MAX_Q_DEPTH <= qc->tag); 2033c6fd2807SJeff Garzik flags |= qc->tag << CRQB_TAG_SHIFT; 2034e49856d8SMark Lord flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT; 2035c6fd2807SJeff Garzik 2036bdd4dddeSJeff Garzik /* get current queue index from software */ 2037fcfb1f77SMark Lord in_index = pp->req_idx; 2038c6fd2807SJeff Garzik 2039c6fd2807SJeff Garzik pp->crqb[in_index].sg_addr = 2040eb73d558SMark Lord cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff); 2041c6fd2807SJeff Garzik pp->crqb[in_index].sg_addr_hi = 2042eb73d558SMark Lord cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16); 2043c6fd2807SJeff Garzik pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags); 2044c6fd2807SJeff Garzik 2045c6fd2807SJeff Garzik cw = &pp->crqb[in_index].ata_cmd[0]; 2046c6fd2807SJeff Garzik 204725985edcSLucas De Marchi /* Sadly, the CRQB cannot accommodate all registers--there are 2048c6fd2807SJeff Garzik * only 11 bytes...so we must pick and choose required 2049c6fd2807SJeff Garzik * registers based on the command. So, we drop feature and 2050c6fd2807SJeff Garzik * hob_feature for [RW] DMA commands, but they are needed for 2051cd12e1f7SMark Lord * NCQ. NCQ will drop hob_nsect, which is not needed there 2052cd12e1f7SMark Lord * (nsect is used only for the tag; feat/hob_feat hold true nsect). 2053c6fd2807SJeff Garzik */ 2054c6fd2807SJeff Garzik switch (tf->command) { 2055c6fd2807SJeff Garzik case ATA_CMD_READ: 2056c6fd2807SJeff Garzik case ATA_CMD_READ_EXT: 2057c6fd2807SJeff Garzik case ATA_CMD_WRITE: 2058c6fd2807SJeff Garzik case ATA_CMD_WRITE_EXT: 2059c6fd2807SJeff Garzik case ATA_CMD_WRITE_FUA_EXT: 2060c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0); 2061c6fd2807SJeff Garzik break; 2062c6fd2807SJeff Garzik case ATA_CMD_FPDMA_READ: 2063c6fd2807SJeff Garzik case ATA_CMD_FPDMA_WRITE: 2064c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0); 2065c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0); 2066c6fd2807SJeff Garzik break; 2067c6fd2807SJeff Garzik default: 2068c6fd2807SJeff Garzik /* The only other commands EDMA supports in non-queued and 2069c6fd2807SJeff Garzik * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none 2070c6fd2807SJeff Garzik * of which are defined/used by Linux. If we get here, this 2071c6fd2807SJeff Garzik * driver needs work. 2072c6fd2807SJeff Garzik * 2073c6fd2807SJeff Garzik * FIXME: modify libata to give qc_prep a return value and 2074c6fd2807SJeff Garzik * return error here. 2075c6fd2807SJeff Garzik */ 2076c6fd2807SJeff Garzik BUG_ON(tf->command); 2077c6fd2807SJeff Garzik break; 2078c6fd2807SJeff Garzik } 2079c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0); 2080c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0); 2081c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0); 2082c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0); 2083c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0); 2084c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0); 2085c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0); 2086c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0); 2087c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */ 2088c6fd2807SJeff Garzik 2089c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_DMAMAP)) 2090c6fd2807SJeff Garzik return; 2091c6fd2807SJeff Garzik mv_fill_sg(qc); 2092c6fd2807SJeff Garzik } 2093c6fd2807SJeff Garzik 2094c6fd2807SJeff Garzik /** 2095c6fd2807SJeff Garzik * mv_qc_prep_iie - Host specific command preparation. 2096c6fd2807SJeff Garzik * @qc: queued command to prepare 2097c6fd2807SJeff Garzik * 2098c6fd2807SJeff Garzik * This routine simply redirects to the general purpose routine 2099c6fd2807SJeff Garzik * if command is not DMA. Else, it handles prep of the CRQB 2100c6fd2807SJeff Garzik * (command request block), does some sanity checking, and calls 2101c6fd2807SJeff Garzik * the SG load routine. 2102c6fd2807SJeff Garzik * 2103c6fd2807SJeff Garzik * LOCKING: 2104c6fd2807SJeff Garzik * Inherited from caller. 2105c6fd2807SJeff Garzik */ 2106c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc) 2107c6fd2807SJeff Garzik { 2108c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 2109c6fd2807SJeff Garzik struct mv_port_priv *pp = ap->private_data; 2110c6fd2807SJeff Garzik struct mv_crqb_iie *crqb; 21118d2b450dSMark Lord struct ata_taskfile *tf = &qc->tf; 2112c6fd2807SJeff Garzik unsigned in_index; 2113c6fd2807SJeff Garzik u32 flags = 0; 2114c6fd2807SJeff Garzik 21158d2b450dSMark Lord if ((tf->protocol != ATA_PROT_DMA) && 21168d2b450dSMark Lord (tf->protocol != ATA_PROT_NCQ)) 2117c6fd2807SJeff Garzik return; 211844b73380SMark Lord if (tf->command == ATA_CMD_DSM) 211944b73380SMark Lord return; /* use bmdma for this */ 2120c6fd2807SJeff Garzik 2121e12bef50SMark Lord /* Fill in Gen IIE command request block */ 21228d2b450dSMark Lord if (!(tf->flags & ATA_TFLAG_WRITE)) 2123c6fd2807SJeff Garzik flags |= CRQB_FLAG_READ; 2124c6fd2807SJeff Garzik 2125c6fd2807SJeff Garzik WARN_ON(MV_MAX_Q_DEPTH <= qc->tag); 2126c6fd2807SJeff Garzik flags |= qc->tag << CRQB_TAG_SHIFT; 21278c0aeb4aSMark Lord flags |= qc->tag << CRQB_HOSTQ_SHIFT; 2128e49856d8SMark Lord flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT; 2129c6fd2807SJeff Garzik 2130bdd4dddeSJeff Garzik /* get current queue index from software */ 2131fcfb1f77SMark Lord in_index = pp->req_idx; 2132c6fd2807SJeff Garzik 2133c6fd2807SJeff Garzik crqb = (struct mv_crqb_iie *) &pp->crqb[in_index]; 2134eb73d558SMark Lord crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff); 2135eb73d558SMark Lord crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16); 2136c6fd2807SJeff Garzik crqb->flags = cpu_to_le32(flags); 2137c6fd2807SJeff Garzik 2138c6fd2807SJeff Garzik crqb->ata_cmd[0] = cpu_to_le32( 2139c6fd2807SJeff Garzik (tf->command << 16) | 2140c6fd2807SJeff Garzik (tf->feature << 24) 2141c6fd2807SJeff Garzik ); 2142c6fd2807SJeff Garzik crqb->ata_cmd[1] = cpu_to_le32( 2143c6fd2807SJeff Garzik (tf->lbal << 0) | 2144c6fd2807SJeff Garzik (tf->lbam << 8) | 2145c6fd2807SJeff Garzik (tf->lbah << 16) | 2146c6fd2807SJeff Garzik (tf->device << 24) 2147c6fd2807SJeff Garzik ); 2148c6fd2807SJeff Garzik crqb->ata_cmd[2] = cpu_to_le32( 2149c6fd2807SJeff Garzik (tf->hob_lbal << 0) | 2150c6fd2807SJeff Garzik (tf->hob_lbam << 8) | 2151c6fd2807SJeff Garzik (tf->hob_lbah << 16) | 2152c6fd2807SJeff Garzik (tf->hob_feature << 24) 2153c6fd2807SJeff Garzik ); 2154c6fd2807SJeff Garzik crqb->ata_cmd[3] = cpu_to_le32( 2155c6fd2807SJeff Garzik (tf->nsect << 0) | 2156c6fd2807SJeff Garzik (tf->hob_nsect << 8) 2157c6fd2807SJeff Garzik ); 2158c6fd2807SJeff Garzik 2159c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_DMAMAP)) 2160c6fd2807SJeff Garzik return; 2161c6fd2807SJeff Garzik mv_fill_sg(qc); 2162c6fd2807SJeff Garzik } 2163c6fd2807SJeff Garzik 2164c6fd2807SJeff Garzik /** 2165d16ab3f6SMark Lord * mv_sff_check_status - fetch device status, if valid 2166d16ab3f6SMark Lord * @ap: ATA port to fetch status from 2167d16ab3f6SMark Lord * 2168d16ab3f6SMark Lord * When using command issue via mv_qc_issue_fis(), 2169d16ab3f6SMark Lord * the initial ATA_BUSY state does not show up in the 2170d16ab3f6SMark Lord * ATA status (shadow) register. This can confuse libata! 2171d16ab3f6SMark Lord * 2172d16ab3f6SMark Lord * So we have a hook here to fake ATA_BUSY for that situation, 2173d16ab3f6SMark Lord * until the first time a BUSY, DRQ, or ERR bit is seen. 2174d16ab3f6SMark Lord * 2175d16ab3f6SMark Lord * The rest of the time, it simply returns the ATA status register. 2176d16ab3f6SMark Lord */ 2177d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap) 2178d16ab3f6SMark Lord { 2179d16ab3f6SMark Lord u8 stat = ioread8(ap->ioaddr.status_addr); 2180d16ab3f6SMark Lord struct mv_port_priv *pp = ap->private_data; 2181d16ab3f6SMark Lord 2182d16ab3f6SMark Lord if (pp->pp_flags & MV_PP_FLAG_FAKE_ATA_BUSY) { 2183d16ab3f6SMark Lord if (stat & (ATA_BUSY | ATA_DRQ | ATA_ERR)) 2184d16ab3f6SMark Lord pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; 2185d16ab3f6SMark Lord else 2186d16ab3f6SMark Lord stat = ATA_BUSY; 2187d16ab3f6SMark Lord } 2188d16ab3f6SMark Lord return stat; 2189d16ab3f6SMark Lord } 2190d16ab3f6SMark Lord 2191d16ab3f6SMark Lord /** 219270f8b79cSMark Lord * mv_send_fis - Send a FIS, using the "Vendor-Unique FIS" register 219370f8b79cSMark Lord * @fis: fis to be sent 219470f8b79cSMark Lord * @nwords: number of 32-bit words in the fis 219570f8b79cSMark Lord */ 219670f8b79cSMark Lord static unsigned int mv_send_fis(struct ata_port *ap, u32 *fis, int nwords) 219770f8b79cSMark Lord { 219870f8b79cSMark Lord void __iomem *port_mmio = mv_ap_base(ap); 219970f8b79cSMark Lord u32 ifctl, old_ifctl, ifstat; 220070f8b79cSMark Lord int i, timeout = 200, final_word = nwords - 1; 220170f8b79cSMark Lord 220270f8b79cSMark Lord /* Initiate FIS transmission mode */ 2203cae5a29dSMark Lord old_ifctl = readl(port_mmio + SATA_IFCTL); 220470f8b79cSMark Lord ifctl = 0x100 | (old_ifctl & 0xf); 2205cae5a29dSMark Lord writelfl(ifctl, port_mmio + SATA_IFCTL); 220670f8b79cSMark Lord 220770f8b79cSMark Lord /* Send all words of the FIS except for the final word */ 220870f8b79cSMark Lord for (i = 0; i < final_word; ++i) 2209cae5a29dSMark Lord writel(fis[i], port_mmio + VENDOR_UNIQUE_FIS); 221070f8b79cSMark Lord 221170f8b79cSMark Lord /* Flag end-of-transmission, and then send the final word */ 2212cae5a29dSMark Lord writelfl(ifctl | 0x200, port_mmio + SATA_IFCTL); 2213cae5a29dSMark Lord writelfl(fis[final_word], port_mmio + VENDOR_UNIQUE_FIS); 221470f8b79cSMark Lord 221570f8b79cSMark Lord /* 221670f8b79cSMark Lord * Wait for FIS transmission to complete. 221770f8b79cSMark Lord * This typically takes just a single iteration. 221870f8b79cSMark Lord */ 221970f8b79cSMark Lord do { 2220cae5a29dSMark Lord ifstat = readl(port_mmio + SATA_IFSTAT); 222170f8b79cSMark Lord } while (!(ifstat & 0x1000) && --timeout); 222270f8b79cSMark Lord 222370f8b79cSMark Lord /* Restore original port configuration */ 2224cae5a29dSMark Lord writelfl(old_ifctl, port_mmio + SATA_IFCTL); 222570f8b79cSMark Lord 222670f8b79cSMark Lord /* See if it worked */ 222770f8b79cSMark Lord if ((ifstat & 0x3000) != 0x1000) { 2228a9a79dfeSJoe Perches ata_port_warn(ap, "%s transmission error, ifstat=%08x\n", 222970f8b79cSMark Lord __func__, ifstat); 223070f8b79cSMark Lord return AC_ERR_OTHER; 223170f8b79cSMark Lord } 223270f8b79cSMark Lord return 0; 223370f8b79cSMark Lord } 223470f8b79cSMark Lord 223570f8b79cSMark Lord /** 223670f8b79cSMark Lord * mv_qc_issue_fis - Issue a command directly as a FIS 223770f8b79cSMark Lord * @qc: queued command to start 223870f8b79cSMark Lord * 223970f8b79cSMark Lord * Note that the ATA shadow registers are not updated 224070f8b79cSMark Lord * after command issue, so the device will appear "READY" 224170f8b79cSMark Lord * if polled, even while it is BUSY processing the command. 224270f8b79cSMark Lord * 224370f8b79cSMark Lord * So we use a status hook to fake ATA_BUSY until the drive changes state. 224470f8b79cSMark Lord * 224570f8b79cSMark Lord * Note: we don't get updated shadow regs on *completion* 224670f8b79cSMark Lord * of non-data commands. So avoid sending them via this function, 224770f8b79cSMark Lord * as they will appear to have completed immediately. 224870f8b79cSMark Lord * 224970f8b79cSMark Lord * GEN_IIE has special registers that we could get the result tf from, 225070f8b79cSMark Lord * but earlier chipsets do not. For now, we ignore those registers. 225170f8b79cSMark Lord */ 225270f8b79cSMark Lord static unsigned int mv_qc_issue_fis(struct ata_queued_cmd *qc) 225370f8b79cSMark Lord { 225470f8b79cSMark Lord struct ata_port *ap = qc->ap; 225570f8b79cSMark Lord struct mv_port_priv *pp = ap->private_data; 225670f8b79cSMark Lord struct ata_link *link = qc->dev->link; 225770f8b79cSMark Lord u32 fis[5]; 225870f8b79cSMark Lord int err = 0; 225970f8b79cSMark Lord 226070f8b79cSMark Lord ata_tf_to_fis(&qc->tf, link->pmp, 1, (void *)fis); 22614c4a90fdSThiago Farina err = mv_send_fis(ap, fis, ARRAY_SIZE(fis)); 226270f8b79cSMark Lord if (err) 226370f8b79cSMark Lord return err; 226470f8b79cSMark Lord 226570f8b79cSMark Lord switch (qc->tf.protocol) { 226670f8b79cSMark Lord case ATAPI_PROT_PIO: 226770f8b79cSMark Lord pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY; 226870f8b79cSMark Lord /* fall through */ 226970f8b79cSMark Lord case ATAPI_PROT_NODATA: 227070f8b79cSMark Lord ap->hsm_task_state = HSM_ST_FIRST; 227170f8b79cSMark Lord break; 227270f8b79cSMark Lord case ATA_PROT_PIO: 227370f8b79cSMark Lord pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY; 227470f8b79cSMark Lord if (qc->tf.flags & ATA_TFLAG_WRITE) 227570f8b79cSMark Lord ap->hsm_task_state = HSM_ST_FIRST; 227670f8b79cSMark Lord else 227770f8b79cSMark Lord ap->hsm_task_state = HSM_ST; 227870f8b79cSMark Lord break; 227970f8b79cSMark Lord default: 228070f8b79cSMark Lord ap->hsm_task_state = HSM_ST_LAST; 228170f8b79cSMark Lord break; 228270f8b79cSMark Lord } 228370f8b79cSMark Lord 228470f8b79cSMark Lord if (qc->tf.flags & ATA_TFLAG_POLLING) 2285ea3c6450SGwendal Grignou ata_sff_queue_pio_task(link, 0); 228670f8b79cSMark Lord return 0; 228770f8b79cSMark Lord } 228870f8b79cSMark Lord 228970f8b79cSMark Lord /** 2290c6fd2807SJeff Garzik * mv_qc_issue - Initiate a command to the host 2291c6fd2807SJeff Garzik * @qc: queued command to start 2292c6fd2807SJeff Garzik * 2293c6fd2807SJeff Garzik * This routine simply redirects to the general purpose routine 2294c6fd2807SJeff Garzik * if command is not DMA. Else, it sanity checks our local 2295c6fd2807SJeff Garzik * caches of the request producer/consumer indices then enables 2296c6fd2807SJeff Garzik * DMA and bumps the request producer index. 2297c6fd2807SJeff Garzik * 2298c6fd2807SJeff Garzik * LOCKING: 2299c6fd2807SJeff Garzik * Inherited from caller. 2300c6fd2807SJeff Garzik */ 2301c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc) 2302c6fd2807SJeff Garzik { 2303f48765ccSMark Lord static int limit_warnings = 10; 2304c5d3e45aSJeff Garzik struct ata_port *ap = qc->ap; 2305c5d3e45aSJeff Garzik void __iomem *port_mmio = mv_ap_base(ap); 2306c5d3e45aSJeff Garzik struct mv_port_priv *pp = ap->private_data; 2307bdd4dddeSJeff Garzik u32 in_index; 230842ed893dSMark Lord unsigned int port_irqs; 2309c6fd2807SJeff Garzik 2310d16ab3f6SMark Lord pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; /* paranoia */ 2311d16ab3f6SMark Lord 2312f48765ccSMark Lord switch (qc->tf.protocol) { 2313f48765ccSMark Lord case ATA_PROT_DMA: 231444b73380SMark Lord if (qc->tf.command == ATA_CMD_DSM) { 231544b73380SMark Lord if (!ap->ops->bmdma_setup) /* no bmdma on GEN_I */ 231644b73380SMark Lord return AC_ERR_OTHER; 231744b73380SMark Lord break; /* use bmdma for this */ 231844b73380SMark Lord } 231944b73380SMark Lord /* fall thru */ 2320f48765ccSMark Lord case ATA_PROT_NCQ: 2321f48765ccSMark Lord mv_start_edma(ap, port_mmio, pp, qc->tf.protocol); 2322f48765ccSMark Lord pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK; 2323f48765ccSMark Lord in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT; 2324f48765ccSMark Lord 2325f48765ccSMark Lord /* Write the request in pointer to kick the EDMA to life */ 2326f48765ccSMark Lord writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index, 2327cae5a29dSMark Lord port_mmio + EDMA_REQ_Q_IN_PTR); 2328f48765ccSMark Lord return 0; 2329f48765ccSMark Lord 2330f48765ccSMark Lord case ATA_PROT_PIO: 2331c6112bd8SMark Lord /* 2332c6112bd8SMark Lord * Errata SATA#16, SATA#24: warn if multiple DRQs expected. 2333c6112bd8SMark Lord * 2334c6112bd8SMark Lord * Someday, we might implement special polling workarounds 2335c6112bd8SMark Lord * for these, but it all seems rather unnecessary since we 2336c6112bd8SMark Lord * normally use only DMA for commands which transfer more 2337c6112bd8SMark Lord * than a single block of data. 2338c6112bd8SMark Lord * 2339c6112bd8SMark Lord * Much of the time, this could just work regardless. 2340c6112bd8SMark Lord * So for now, just log the incident, and allow the attempt. 2341c6112bd8SMark Lord */ 2342c7843e8fSMark Lord if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) { 2343c6112bd8SMark Lord --limit_warnings; 2344a9a79dfeSJoe Perches ata_link_warn(qc->dev->link, DRV_NAME 2345c6112bd8SMark Lord ": attempting PIO w/multiple DRQ: " 2346c6112bd8SMark Lord "this may fail due to h/w errata\n"); 2347c6112bd8SMark Lord } 2348f48765ccSMark Lord /* drop through */ 234942ed893dSMark Lord case ATA_PROT_NODATA: 2350f48765ccSMark Lord case ATAPI_PROT_PIO: 235142ed893dSMark Lord case ATAPI_PROT_NODATA: 235242ed893dSMark Lord if (ap->flags & ATA_FLAG_PIO_POLLING) 235342ed893dSMark Lord qc->tf.flags |= ATA_TFLAG_POLLING; 235442ed893dSMark Lord break; 235542ed893dSMark Lord } 235642ed893dSMark Lord 235742ed893dSMark Lord if (qc->tf.flags & ATA_TFLAG_POLLING) 235842ed893dSMark Lord port_irqs = ERR_IRQ; /* mask device interrupt when polling */ 235942ed893dSMark Lord else 236042ed893dSMark Lord port_irqs = ERR_IRQ | DONE_IRQ; /* unmask all interrupts */ 236142ed893dSMark Lord 236217c5aab5SMark Lord /* 236317c5aab5SMark Lord * We're about to send a non-EDMA capable command to the 2364c6fd2807SJeff Garzik * port. Turn off EDMA so there won't be problems accessing 2365c6fd2807SJeff Garzik * shadow block, etc registers. 2366c6fd2807SJeff Garzik */ 2367b562468cSMark Lord mv_stop_edma(ap); 2368f48765ccSMark Lord mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs); 2369e49856d8SMark Lord mv_pmp_select(ap, qc->dev->link->pmp); 237070f8b79cSMark Lord 237170f8b79cSMark Lord if (qc->tf.command == ATA_CMD_READ_LOG_EXT) { 237270f8b79cSMark Lord struct mv_host_priv *hpriv = ap->host->private_data; 237370f8b79cSMark Lord /* 237470f8b79cSMark Lord * Workaround for 88SX60x1 FEr SATA#25 (part 2). 237570f8b79cSMark Lord * 237670f8b79cSMark Lord * After any NCQ error, the READ_LOG_EXT command 237770f8b79cSMark Lord * from libata-eh *must* use mv_qc_issue_fis(). 237870f8b79cSMark Lord * Otherwise it might fail, due to chip errata. 237970f8b79cSMark Lord * 238070f8b79cSMark Lord * Rather than special-case it, we'll just *always* 238170f8b79cSMark Lord * use this method here for READ_LOG_EXT, making for 238270f8b79cSMark Lord * easier testing. 238370f8b79cSMark Lord */ 238470f8b79cSMark Lord if (IS_GEN_II(hpriv)) 238570f8b79cSMark Lord return mv_qc_issue_fis(qc); 238670f8b79cSMark Lord } 2387360ff783STejun Heo return ata_bmdma_qc_issue(qc); 2388c6fd2807SJeff Garzik } 2389c6fd2807SJeff Garzik 23908f767f8aSMark Lord static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap) 23918f767f8aSMark Lord { 23928f767f8aSMark Lord struct mv_port_priv *pp = ap->private_data; 23938f767f8aSMark Lord struct ata_queued_cmd *qc; 23948f767f8aSMark Lord 23958f767f8aSMark Lord if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) 23968f767f8aSMark Lord return NULL; 23978f767f8aSMark Lord qc = ata_qc_from_tag(ap, ap->link.active_tag); 23983e4ec344STejun Heo if (qc && !(qc->tf.flags & ATA_TFLAG_POLLING)) 23998f767f8aSMark Lord return qc; 24003e4ec344STejun Heo return NULL; 24018f767f8aSMark Lord } 24028f767f8aSMark Lord 240329d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap) 240429d187bbSMark Lord { 240529d187bbSMark Lord unsigned int pmp, pmp_map; 240629d187bbSMark Lord struct mv_port_priv *pp = ap->private_data; 240729d187bbSMark Lord 240829d187bbSMark Lord if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) { 240929d187bbSMark Lord /* 241029d187bbSMark Lord * Perform NCQ error analysis on failed PMPs 241129d187bbSMark Lord * before we freeze the port entirely. 241229d187bbSMark Lord * 241329d187bbSMark Lord * The failed PMPs are marked earlier by mv_pmp_eh_prep(). 241429d187bbSMark Lord */ 241529d187bbSMark Lord pmp_map = pp->delayed_eh_pmp_map; 241629d187bbSMark Lord pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH; 241729d187bbSMark Lord for (pmp = 0; pmp_map != 0; pmp++) { 241829d187bbSMark Lord unsigned int this_pmp = (1 << pmp); 241929d187bbSMark Lord if (pmp_map & this_pmp) { 242029d187bbSMark Lord struct ata_link *link = &ap->pmp_link[pmp]; 242129d187bbSMark Lord pmp_map &= ~this_pmp; 242229d187bbSMark Lord ata_eh_analyze_ncq_error(link); 242329d187bbSMark Lord } 242429d187bbSMark Lord } 242529d187bbSMark Lord ata_port_freeze(ap); 242629d187bbSMark Lord } 242729d187bbSMark Lord sata_pmp_error_handler(ap); 242829d187bbSMark Lord } 242929d187bbSMark Lord 24304c299ca3SMark Lord static unsigned int mv_get_err_pmp_map(struct ata_port *ap) 24314c299ca3SMark Lord { 24324c299ca3SMark Lord void __iomem *port_mmio = mv_ap_base(ap); 24334c299ca3SMark Lord 2434cae5a29dSMark Lord return readl(port_mmio + SATA_TESTCTL) >> 16; 24354c299ca3SMark Lord } 24364c299ca3SMark Lord 24374c299ca3SMark Lord static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map) 24384c299ca3SMark Lord { 24394c299ca3SMark Lord struct ata_eh_info *ehi; 24404c299ca3SMark Lord unsigned int pmp; 24414c299ca3SMark Lord 24424c299ca3SMark Lord /* 24434c299ca3SMark Lord * Initialize EH info for PMPs which saw device errors 24444c299ca3SMark Lord */ 24454c299ca3SMark Lord ehi = &ap->link.eh_info; 24464c299ca3SMark Lord for (pmp = 0; pmp_map != 0; pmp++) { 24474c299ca3SMark Lord unsigned int this_pmp = (1 << pmp); 24484c299ca3SMark Lord if (pmp_map & this_pmp) { 24494c299ca3SMark Lord struct ata_link *link = &ap->pmp_link[pmp]; 24504c299ca3SMark Lord 24514c299ca3SMark Lord pmp_map &= ~this_pmp; 24524c299ca3SMark Lord ehi = &link->eh_info; 24534c299ca3SMark Lord ata_ehi_clear_desc(ehi); 24544c299ca3SMark Lord ata_ehi_push_desc(ehi, "dev err"); 24554c299ca3SMark Lord ehi->err_mask |= AC_ERR_DEV; 24564c299ca3SMark Lord ehi->action |= ATA_EH_RESET; 24574c299ca3SMark Lord ata_link_abort(link); 24584c299ca3SMark Lord } 24594c299ca3SMark Lord } 24604c299ca3SMark Lord } 24614c299ca3SMark Lord 246206aaca3fSMark Lord static int mv_req_q_empty(struct ata_port *ap) 246306aaca3fSMark Lord { 246406aaca3fSMark Lord void __iomem *port_mmio = mv_ap_base(ap); 246506aaca3fSMark Lord u32 in_ptr, out_ptr; 246606aaca3fSMark Lord 2467cae5a29dSMark Lord in_ptr = (readl(port_mmio + EDMA_REQ_Q_IN_PTR) 246806aaca3fSMark Lord >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK; 2469cae5a29dSMark Lord out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR) 247006aaca3fSMark Lord >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK; 247106aaca3fSMark Lord return (in_ptr == out_ptr); /* 1 == queue_is_empty */ 247206aaca3fSMark Lord } 247306aaca3fSMark Lord 24744c299ca3SMark Lord static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap) 24754c299ca3SMark Lord { 24764c299ca3SMark Lord struct mv_port_priv *pp = ap->private_data; 24774c299ca3SMark Lord int failed_links; 24784c299ca3SMark Lord unsigned int old_map, new_map; 24794c299ca3SMark Lord 24804c299ca3SMark Lord /* 24814c299ca3SMark Lord * Device error during FBS+NCQ operation: 24824c299ca3SMark Lord * 24834c299ca3SMark Lord * Set a port flag to prevent further I/O being enqueued. 24844c299ca3SMark Lord * Leave the EDMA running to drain outstanding commands from this port. 24854c299ca3SMark Lord * Perform the post-mortem/EH only when all responses are complete. 24864c299ca3SMark Lord * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2). 24874c299ca3SMark Lord */ 24884c299ca3SMark Lord if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) { 24894c299ca3SMark Lord pp->pp_flags |= MV_PP_FLAG_DELAYED_EH; 24904c299ca3SMark Lord pp->delayed_eh_pmp_map = 0; 24914c299ca3SMark Lord } 24924c299ca3SMark Lord old_map = pp->delayed_eh_pmp_map; 24934c299ca3SMark Lord new_map = old_map | mv_get_err_pmp_map(ap); 24944c299ca3SMark Lord 24954c299ca3SMark Lord if (old_map != new_map) { 24964c299ca3SMark Lord pp->delayed_eh_pmp_map = new_map; 24974c299ca3SMark Lord mv_pmp_eh_prep(ap, new_map & ~old_map); 24984c299ca3SMark Lord } 2499c46938ccSMark Lord failed_links = hweight16(new_map); 25004c299ca3SMark Lord 2501a9a79dfeSJoe Perches ata_port_info(ap, 2502a9a79dfeSJoe Perches "%s: pmp_map=%04x qc_map=%04x failed_links=%d nr_active_links=%d\n", 25034c299ca3SMark Lord __func__, pp->delayed_eh_pmp_map, 25044c299ca3SMark Lord ap->qc_active, failed_links, 25054c299ca3SMark Lord ap->nr_active_links); 25064c299ca3SMark Lord 250706aaca3fSMark Lord if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) { 25084c299ca3SMark Lord mv_process_crpb_entries(ap, pp); 25094c299ca3SMark Lord mv_stop_edma(ap); 25104c299ca3SMark Lord mv_eh_freeze(ap); 2511a9a79dfeSJoe Perches ata_port_info(ap, "%s: done\n", __func__); 25124c299ca3SMark Lord return 1; /* handled */ 25134c299ca3SMark Lord } 2514a9a79dfeSJoe Perches ata_port_info(ap, "%s: waiting\n", __func__); 25154c299ca3SMark Lord return 1; /* handled */ 25164c299ca3SMark Lord } 25174c299ca3SMark Lord 25184c299ca3SMark Lord static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap) 25194c299ca3SMark Lord { 25204c299ca3SMark Lord /* 25214c299ca3SMark Lord * Possible future enhancement: 25224c299ca3SMark Lord * 25234c299ca3SMark Lord * FBS+non-NCQ operation is not yet implemented. 25244c299ca3SMark Lord * See related notes in mv_edma_cfg(). 25254c299ca3SMark Lord * 25264c299ca3SMark Lord * Device error during FBS+non-NCQ operation: 25274c299ca3SMark Lord * 25284c299ca3SMark Lord * We need to snapshot the shadow registers for each failed command. 25294c299ca3SMark Lord * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3). 25304c299ca3SMark Lord */ 25314c299ca3SMark Lord return 0; /* not handled */ 25324c299ca3SMark Lord } 25334c299ca3SMark Lord 25344c299ca3SMark Lord static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause) 25354c299ca3SMark Lord { 25364c299ca3SMark Lord struct mv_port_priv *pp = ap->private_data; 25374c299ca3SMark Lord 25384c299ca3SMark Lord if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) 25394c299ca3SMark Lord return 0; /* EDMA was not active: not handled */ 25404c299ca3SMark Lord if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN)) 25414c299ca3SMark Lord return 0; /* FBS was not active: not handled */ 25424c299ca3SMark Lord 25434c299ca3SMark Lord if (!(edma_err_cause & EDMA_ERR_DEV)) 25444c299ca3SMark Lord return 0; /* non DEV error: not handled */ 25454c299ca3SMark Lord edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT; 25464c299ca3SMark Lord if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS)) 25474c299ca3SMark Lord return 0; /* other problems: not handled */ 25484c299ca3SMark Lord 25494c299ca3SMark Lord if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) { 25504c299ca3SMark Lord /* 25514c299ca3SMark Lord * EDMA should NOT have self-disabled for this case. 25524c299ca3SMark Lord * If it did, then something is wrong elsewhere, 25534c299ca3SMark Lord * and we cannot handle it here. 25544c299ca3SMark Lord */ 25554c299ca3SMark Lord if (edma_err_cause & EDMA_ERR_SELF_DIS) { 2556a9a79dfeSJoe Perches ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n", 25574c299ca3SMark Lord __func__, edma_err_cause, pp->pp_flags); 25584c299ca3SMark Lord return 0; /* not handled */ 25594c299ca3SMark Lord } 25604c299ca3SMark Lord return mv_handle_fbs_ncq_dev_err(ap); 25614c299ca3SMark Lord } else { 25624c299ca3SMark Lord /* 25634c299ca3SMark Lord * EDMA should have self-disabled for this case. 25644c299ca3SMark Lord * If it did not, then something is wrong elsewhere, 25654c299ca3SMark Lord * and we cannot handle it here. 25664c299ca3SMark Lord */ 25674c299ca3SMark Lord if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) { 2568a9a79dfeSJoe Perches ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n", 25694c299ca3SMark Lord __func__, edma_err_cause, pp->pp_flags); 25704c299ca3SMark Lord return 0; /* not handled */ 25714c299ca3SMark Lord } 25724c299ca3SMark Lord return mv_handle_fbs_non_ncq_dev_err(ap); 25734c299ca3SMark Lord } 25744c299ca3SMark Lord return 0; /* not handled */ 25754c299ca3SMark Lord } 25764c299ca3SMark Lord 2577a9010329SMark Lord static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled) 25788f767f8aSMark Lord { 25798f767f8aSMark Lord struct ata_eh_info *ehi = &ap->link.eh_info; 2580a9010329SMark Lord char *when = "idle"; 25818f767f8aSMark Lord 25828f767f8aSMark Lord ata_ehi_clear_desc(ehi); 25833e4ec344STejun Heo if (edma_was_enabled) { 2584a9010329SMark Lord when = "EDMA enabled"; 25858f767f8aSMark Lord } else { 25868f767f8aSMark Lord struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag); 25878f767f8aSMark Lord if (qc && (qc->tf.flags & ATA_TFLAG_POLLING)) 2588a9010329SMark Lord when = "polling"; 25898f767f8aSMark Lord } 2590a9010329SMark Lord ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when); 25918f767f8aSMark Lord ehi->err_mask |= AC_ERR_OTHER; 25928f767f8aSMark Lord ehi->action |= ATA_EH_RESET; 25938f767f8aSMark Lord ata_port_freeze(ap); 25948f767f8aSMark Lord } 25958f767f8aSMark Lord 2596c6fd2807SJeff Garzik /** 2597c6fd2807SJeff Garzik * mv_err_intr - Handle error interrupts on the port 2598c6fd2807SJeff Garzik * @ap: ATA channel to manipulate 2599c6fd2807SJeff Garzik * 26008d07379dSMark Lord * Most cases require a full reset of the chip's state machine, 26018d07379dSMark Lord * which also performs a COMRESET. 26028d07379dSMark Lord * Also, if the port disabled DMA, update our cached copy to match. 2603c6fd2807SJeff Garzik * 2604c6fd2807SJeff Garzik * LOCKING: 2605c6fd2807SJeff Garzik * Inherited from caller. 2606c6fd2807SJeff Garzik */ 260737b9046aSMark Lord static void mv_err_intr(struct ata_port *ap) 2608c6fd2807SJeff Garzik { 2609c6fd2807SJeff Garzik void __iomem *port_mmio = mv_ap_base(ap); 2610bdd4dddeSJeff Garzik u32 edma_err_cause, eh_freeze_mask, serr = 0; 2611e4006077SMark Lord u32 fis_cause = 0; 2612bdd4dddeSJeff Garzik struct mv_port_priv *pp = ap->private_data; 2613bdd4dddeSJeff Garzik struct mv_host_priv *hpriv = ap->host->private_data; 2614bdd4dddeSJeff Garzik unsigned int action = 0, err_mask = 0; 26159af5c9c9STejun Heo struct ata_eh_info *ehi = &ap->link.eh_info; 261637b9046aSMark Lord struct ata_queued_cmd *qc; 261737b9046aSMark Lord int abort = 0; 2618c6fd2807SJeff Garzik 26198d07379dSMark Lord /* 262037b9046aSMark Lord * Read and clear the SError and err_cause bits. 2621e4006077SMark Lord * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear 2622e4006077SMark Lord * the FIS_IRQ_CAUSE register before clearing edma_err_cause. 2623bdd4dddeSJeff Garzik */ 262437b9046aSMark Lord sata_scr_read(&ap->link, SCR_ERROR, &serr); 262537b9046aSMark Lord sata_scr_write_flush(&ap->link, SCR_ERROR, serr); 262637b9046aSMark Lord 2627cae5a29dSMark Lord edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE); 2628e4006077SMark Lord if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) { 2629cae5a29dSMark Lord fis_cause = readl(port_mmio + FIS_IRQ_CAUSE); 2630cae5a29dSMark Lord writelfl(~fis_cause, port_mmio + FIS_IRQ_CAUSE); 2631e4006077SMark Lord } 2632cae5a29dSMark Lord writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE); 2633bdd4dddeSJeff Garzik 26344c299ca3SMark Lord if (edma_err_cause & EDMA_ERR_DEV) { 26354c299ca3SMark Lord /* 26364c299ca3SMark Lord * Device errors during FIS-based switching operation 26374c299ca3SMark Lord * require special handling. 26384c299ca3SMark Lord */ 26394c299ca3SMark Lord if (mv_handle_dev_err(ap, edma_err_cause)) 26404c299ca3SMark Lord return; 26414c299ca3SMark Lord } 26424c299ca3SMark Lord 264337b9046aSMark Lord qc = mv_get_active_qc(ap); 264437b9046aSMark Lord ata_ehi_clear_desc(ehi); 264537b9046aSMark Lord ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x", 264637b9046aSMark Lord edma_err_cause, pp->pp_flags); 2647e4006077SMark Lord 2648c443c500SMark Lord if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) { 2649e4006077SMark Lord ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause); 2650cae5a29dSMark Lord if (fis_cause & FIS_IRQ_CAUSE_AN) { 2651c443c500SMark Lord u32 ec = edma_err_cause & 2652c443c500SMark Lord ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT); 2653c443c500SMark Lord sata_async_notification(ap); 2654c443c500SMark Lord if (!ec) 2655c443c500SMark Lord return; /* Just an AN; no need for the nukes */ 2656c443c500SMark Lord ata_ehi_push_desc(ehi, "SDB notify"); 2657c443c500SMark Lord } 2658c443c500SMark Lord } 2659bdd4dddeSJeff Garzik /* 2660352fab70SMark Lord * All generations share these EDMA error cause bits: 2661bdd4dddeSJeff Garzik */ 266237b9046aSMark Lord if (edma_err_cause & EDMA_ERR_DEV) { 2663bdd4dddeSJeff Garzik err_mask |= AC_ERR_DEV; 266437b9046aSMark Lord action |= ATA_EH_RESET; 266537b9046aSMark Lord ata_ehi_push_desc(ehi, "dev error"); 266637b9046aSMark Lord } 2667bdd4dddeSJeff Garzik if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR | 26686c1153e0SJeff Garzik EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR | 2669bdd4dddeSJeff Garzik EDMA_ERR_INTRL_PAR)) { 2670bdd4dddeSJeff Garzik err_mask |= AC_ERR_ATA_BUS; 2671cf480626STejun Heo action |= ATA_EH_RESET; 2672b64bbc39STejun Heo ata_ehi_push_desc(ehi, "parity error"); 2673bdd4dddeSJeff Garzik } 2674bdd4dddeSJeff Garzik if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) { 2675bdd4dddeSJeff Garzik ata_ehi_hotplugged(ehi); 2676bdd4dddeSJeff Garzik ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ? 2677b64bbc39STejun Heo "dev disconnect" : "dev connect"); 2678cf480626STejun Heo action |= ATA_EH_RESET; 2679bdd4dddeSJeff Garzik } 2680bdd4dddeSJeff Garzik 2681352fab70SMark Lord /* 2682352fab70SMark Lord * Gen-I has a different SELF_DIS bit, 2683352fab70SMark Lord * different FREEZE bits, and no SERR bit: 2684352fab70SMark Lord */ 2685ee9ccdf7SJeff Garzik if (IS_GEN_I(hpriv)) { 2686bdd4dddeSJeff Garzik eh_freeze_mask = EDMA_EH_FREEZE_5; 2687bdd4dddeSJeff Garzik if (edma_err_cause & EDMA_ERR_SELF_DIS_5) { 2688c6fd2807SJeff Garzik pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN; 2689b64bbc39STejun Heo ata_ehi_push_desc(ehi, "EDMA self-disable"); 2690c6fd2807SJeff Garzik } 2691bdd4dddeSJeff Garzik } else { 2692bdd4dddeSJeff Garzik eh_freeze_mask = EDMA_EH_FREEZE; 2693bdd4dddeSJeff Garzik if (edma_err_cause & EDMA_ERR_SELF_DIS) { 2694bdd4dddeSJeff Garzik pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN; 2695b64bbc39STejun Heo ata_ehi_push_desc(ehi, "EDMA self-disable"); 2696bdd4dddeSJeff Garzik } 2697bdd4dddeSJeff Garzik if (edma_err_cause & EDMA_ERR_SERR) { 26988d07379dSMark Lord ata_ehi_push_desc(ehi, "SError=%08x", serr); 26998d07379dSMark Lord err_mask |= AC_ERR_ATA_BUS; 2700cf480626STejun Heo action |= ATA_EH_RESET; 2701bdd4dddeSJeff Garzik } 2702bdd4dddeSJeff Garzik } 2703c6fd2807SJeff Garzik 2704bdd4dddeSJeff Garzik if (!err_mask) { 2705bdd4dddeSJeff Garzik err_mask = AC_ERR_OTHER; 2706cf480626STejun Heo action |= ATA_EH_RESET; 2707bdd4dddeSJeff Garzik } 2708bdd4dddeSJeff Garzik 2709bdd4dddeSJeff Garzik ehi->serror |= serr; 2710bdd4dddeSJeff Garzik ehi->action |= action; 2711bdd4dddeSJeff Garzik 2712bdd4dddeSJeff Garzik if (qc) 2713bdd4dddeSJeff Garzik qc->err_mask |= err_mask; 2714bdd4dddeSJeff Garzik else 2715bdd4dddeSJeff Garzik ehi->err_mask |= err_mask; 2716bdd4dddeSJeff Garzik 271737b9046aSMark Lord if (err_mask == AC_ERR_DEV) { 271837b9046aSMark Lord /* 271937b9046aSMark Lord * Cannot do ata_port_freeze() here, 272037b9046aSMark Lord * because it would kill PIO access, 272137b9046aSMark Lord * which is needed for further diagnosis. 272237b9046aSMark Lord */ 272337b9046aSMark Lord mv_eh_freeze(ap); 272437b9046aSMark Lord abort = 1; 272537b9046aSMark Lord } else if (edma_err_cause & eh_freeze_mask) { 272637b9046aSMark Lord /* 272737b9046aSMark Lord * Note to self: ata_port_freeze() calls ata_port_abort() 272837b9046aSMark Lord */ 2729bdd4dddeSJeff Garzik ata_port_freeze(ap); 273037b9046aSMark Lord } else { 273137b9046aSMark Lord abort = 1; 273237b9046aSMark Lord } 273337b9046aSMark Lord 273437b9046aSMark Lord if (abort) { 273537b9046aSMark Lord if (qc) 273637b9046aSMark Lord ata_link_abort(qc->dev->link); 2737bdd4dddeSJeff Garzik else 2738bdd4dddeSJeff Garzik ata_port_abort(ap); 2739bdd4dddeSJeff Garzik } 274037b9046aSMark Lord } 2741bdd4dddeSJeff Garzik 27421aadf5c3STejun Heo static bool mv_process_crpb_response(struct ata_port *ap, 2743fcfb1f77SMark Lord struct mv_crpb *response, unsigned int tag, int ncq_enabled) 2744fcfb1f77SMark Lord { 2745fcfb1f77SMark Lord u8 ata_status; 2746fcfb1f77SMark Lord u16 edma_status = le16_to_cpu(response->flags); 2747752e386cSTejun Heo 2748fcfb1f77SMark Lord /* 2749fcfb1f77SMark Lord * edma_status from a response queue entry: 2750cae5a29dSMark Lord * LSB is from EDMA_ERR_IRQ_CAUSE (non-NCQ only). 2751fcfb1f77SMark Lord * MSB is saved ATA status from command completion. 2752fcfb1f77SMark Lord */ 2753fcfb1f77SMark Lord if (!ncq_enabled) { 2754fcfb1f77SMark Lord u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV; 2755fcfb1f77SMark Lord if (err_cause) { 2756fcfb1f77SMark Lord /* 2757752e386cSTejun Heo * Error will be seen/handled by 2758752e386cSTejun Heo * mv_err_intr(). So do nothing at all here. 2759fcfb1f77SMark Lord */ 27601aadf5c3STejun Heo return false; 2761fcfb1f77SMark Lord } 2762fcfb1f77SMark Lord } 2763fcfb1f77SMark Lord ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT; 276437b9046aSMark Lord if (!ac_err_mask(ata_status)) 27651aadf5c3STejun Heo return true; 276637b9046aSMark Lord /* else: leave it for mv_err_intr() */ 27671aadf5c3STejun Heo return false; 2768fcfb1f77SMark Lord } 2769fcfb1f77SMark Lord 2770fcfb1f77SMark Lord static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp) 2771bdd4dddeSJeff Garzik { 2772bdd4dddeSJeff Garzik void __iomem *port_mmio = mv_ap_base(ap); 2773bdd4dddeSJeff Garzik struct mv_host_priv *hpriv = ap->host->private_data; 2774fcfb1f77SMark Lord u32 in_index; 2775bdd4dddeSJeff Garzik bool work_done = false; 27761aadf5c3STejun Heo u32 done_mask = 0; 2777fcfb1f77SMark Lord int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN); 2778bdd4dddeSJeff Garzik 2779fcfb1f77SMark Lord /* Get the hardware queue position index */ 2780cae5a29dSMark Lord in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR) 2781bdd4dddeSJeff Garzik >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK; 2782bdd4dddeSJeff Garzik 2783fcfb1f77SMark Lord /* Process new responses from since the last time we looked */ 2784fcfb1f77SMark Lord while (in_index != pp->resp_idx) { 27856c1153e0SJeff Garzik unsigned int tag; 2786fcfb1f77SMark Lord struct mv_crpb *response = &pp->crpb[pp->resp_idx]; 2787bdd4dddeSJeff Garzik 2788fcfb1f77SMark Lord pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK; 2789bdd4dddeSJeff Garzik 2790fcfb1f77SMark Lord if (IS_GEN_I(hpriv)) { 2791fcfb1f77SMark Lord /* 50xx: no NCQ, only one command active at a time */ 27929af5c9c9STejun Heo tag = ap->link.active_tag; 2793fcfb1f77SMark Lord } else { 2794fcfb1f77SMark Lord /* Gen II/IIE: get command tag from CRPB entry */ 2795fcfb1f77SMark Lord tag = le16_to_cpu(response->id) & 0x1f; 2796bdd4dddeSJeff Garzik } 27971aadf5c3STejun Heo if (mv_process_crpb_response(ap, response, tag, ncq_enabled)) 27981aadf5c3STejun Heo done_mask |= 1 << tag; 2799bdd4dddeSJeff Garzik work_done = true; 2800bdd4dddeSJeff Garzik } 2801bdd4dddeSJeff Garzik 28021aadf5c3STejun Heo if (work_done) { 28031aadf5c3STejun Heo ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask); 28041aadf5c3STejun Heo 2805352fab70SMark Lord /* Update the software queue position index in hardware */ 2806bdd4dddeSJeff Garzik writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | 2807fcfb1f77SMark Lord (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT), 2808cae5a29dSMark Lord port_mmio + EDMA_RSP_Q_OUT_PTR); 2809c6fd2807SJeff Garzik } 28101aadf5c3STejun Heo } 2811c6fd2807SJeff Garzik 2812a9010329SMark Lord static void mv_port_intr(struct ata_port *ap, u32 port_cause) 2813a9010329SMark Lord { 2814a9010329SMark Lord struct mv_port_priv *pp; 2815a9010329SMark Lord int edma_was_enabled; 2816a9010329SMark Lord 2817a9010329SMark Lord /* 2818a9010329SMark Lord * Grab a snapshot of the EDMA_EN flag setting, 2819a9010329SMark Lord * so that we have a consistent view for this port, 2820a9010329SMark Lord * even if something we call of our routines changes it. 2821a9010329SMark Lord */ 2822a9010329SMark Lord pp = ap->private_data; 2823a9010329SMark Lord edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN); 2824a9010329SMark Lord /* 2825a9010329SMark Lord * Process completed CRPB response(s) before other events. 2826a9010329SMark Lord */ 2827a9010329SMark Lord if (edma_was_enabled && (port_cause & DONE_IRQ)) { 2828a9010329SMark Lord mv_process_crpb_entries(ap, pp); 28294c299ca3SMark Lord if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) 28304c299ca3SMark Lord mv_handle_fbs_ncq_dev_err(ap); 2831a9010329SMark Lord } 2832a9010329SMark Lord /* 2833a9010329SMark Lord * Handle chip-reported errors, or continue on to handle PIO. 2834a9010329SMark Lord */ 2835a9010329SMark Lord if (unlikely(port_cause & ERR_IRQ)) { 2836a9010329SMark Lord mv_err_intr(ap); 2837a9010329SMark Lord } else if (!edma_was_enabled) { 2838a9010329SMark Lord struct ata_queued_cmd *qc = mv_get_active_qc(ap); 2839a9010329SMark Lord if (qc) 2840c3b28894STejun Heo ata_bmdma_port_intr(ap, qc); 2841a9010329SMark Lord else 2842a9010329SMark Lord mv_unexpected_intr(ap, edma_was_enabled); 2843a9010329SMark Lord } 2844a9010329SMark Lord } 2845a9010329SMark Lord 2846c6fd2807SJeff Garzik /** 2847c6fd2807SJeff Garzik * mv_host_intr - Handle all interrupts on the given host controller 2848cca3974eSJeff Garzik * @host: host specific structure 28497368f919SMark Lord * @main_irq_cause: Main interrupt cause register for the chip. 2850c6fd2807SJeff Garzik * 2851c6fd2807SJeff Garzik * LOCKING: 2852c6fd2807SJeff Garzik * Inherited from caller. 2853c6fd2807SJeff Garzik */ 28547368f919SMark Lord static int mv_host_intr(struct ata_host *host, u32 main_irq_cause) 2855c6fd2807SJeff Garzik { 2856f351b2d6SSaeed Bishara struct mv_host_priv *hpriv = host->private_data; 2857eabd5eb1SMark Lord void __iomem *mmio = hpriv->base, *hc_mmio; 2858a3718c1fSMark Lord unsigned int handled = 0, port; 2859c6fd2807SJeff Garzik 28602b748a0aSMark Lord /* If asserted, clear the "all ports" IRQ coalescing bit */ 28612b748a0aSMark Lord if (main_irq_cause & ALL_PORTS_COAL_DONE) 2862cae5a29dSMark Lord writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE); 28632b748a0aSMark Lord 2864a3718c1fSMark Lord for (port = 0; port < hpriv->n_ports; port++) { 2865cca3974eSJeff Garzik struct ata_port *ap = host->ports[port]; 2866eabd5eb1SMark Lord unsigned int p, shift, hardport, port_cause; 2867eabd5eb1SMark Lord 2868a3718c1fSMark Lord MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport); 2869a3718c1fSMark Lord /* 2870eabd5eb1SMark Lord * Each hc within the host has its own hc_irq_cause register, 2871eabd5eb1SMark Lord * where the interrupting ports bits get ack'd. 2872a3718c1fSMark Lord */ 2873eabd5eb1SMark Lord if (hardport == 0) { /* first port on this hc ? */ 2874eabd5eb1SMark Lord u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND; 2875eabd5eb1SMark Lord u32 port_mask, ack_irqs; 2876eabd5eb1SMark Lord /* 2877eabd5eb1SMark Lord * Skip this entire hc if nothing pending for any ports 2878eabd5eb1SMark Lord */ 2879eabd5eb1SMark Lord if (!hc_cause) { 2880eabd5eb1SMark Lord port += MV_PORTS_PER_HC - 1; 2881eabd5eb1SMark Lord continue; 2882eabd5eb1SMark Lord } 2883eabd5eb1SMark Lord /* 2884eabd5eb1SMark Lord * We don't need/want to read the hc_irq_cause register, 2885eabd5eb1SMark Lord * because doing so hurts performance, and 2886eabd5eb1SMark Lord * main_irq_cause already gives us everything we need. 2887eabd5eb1SMark Lord * 2888eabd5eb1SMark Lord * But we do have to *write* to the hc_irq_cause to ack 2889eabd5eb1SMark Lord * the ports that we are handling this time through. 2890eabd5eb1SMark Lord * 2891eabd5eb1SMark Lord * This requires that we create a bitmap for those 2892eabd5eb1SMark Lord * ports which interrupted us, and use that bitmap 2893eabd5eb1SMark Lord * to ack (only) those ports via hc_irq_cause. 2894eabd5eb1SMark Lord */ 2895eabd5eb1SMark Lord ack_irqs = 0; 28962b748a0aSMark Lord if (hc_cause & PORTS_0_3_COAL_DONE) 28972b748a0aSMark Lord ack_irqs = HC_COAL_IRQ; 2898eabd5eb1SMark Lord for (p = 0; p < MV_PORTS_PER_HC; ++p) { 2899eabd5eb1SMark Lord if ((port + p) >= hpriv->n_ports) 2900eabd5eb1SMark Lord break; 2901eabd5eb1SMark Lord port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2); 2902eabd5eb1SMark Lord if (hc_cause & port_mask) 2903eabd5eb1SMark Lord ack_irqs |= (DMA_IRQ | DEV_IRQ) << p; 2904eabd5eb1SMark Lord } 2905a3718c1fSMark Lord hc_mmio = mv_hc_base_from_port(mmio, port); 2906cae5a29dSMark Lord writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE); 2907a3718c1fSMark Lord handled = 1; 2908a3718c1fSMark Lord } 2909a9010329SMark Lord /* 2910a9010329SMark Lord * Handle interrupts signalled for this port: 2911a9010329SMark Lord */ 2912eabd5eb1SMark Lord port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ); 2913a9010329SMark Lord if (port_cause) 2914a9010329SMark Lord mv_port_intr(ap, port_cause); 2915eabd5eb1SMark Lord } 2916a3718c1fSMark Lord return handled; 2917c6fd2807SJeff Garzik } 2918c6fd2807SJeff Garzik 2919a3718c1fSMark Lord static int mv_pci_error(struct ata_host *host, void __iomem *mmio) 2920bdd4dddeSJeff Garzik { 292102a121daSMark Lord struct mv_host_priv *hpriv = host->private_data; 2922bdd4dddeSJeff Garzik struct ata_port *ap; 2923bdd4dddeSJeff Garzik struct ata_queued_cmd *qc; 2924bdd4dddeSJeff Garzik struct ata_eh_info *ehi; 2925bdd4dddeSJeff Garzik unsigned int i, err_mask, printed = 0; 2926bdd4dddeSJeff Garzik u32 err_cause; 2927bdd4dddeSJeff Garzik 2928cae5a29dSMark Lord err_cause = readl(mmio + hpriv->irq_cause_offset); 2929bdd4dddeSJeff Garzik 2930a44fec1fSJoe Perches dev_err(host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n", err_cause); 2931bdd4dddeSJeff Garzik 2932bdd4dddeSJeff Garzik DPRINTK("All regs @ PCI error\n"); 2933bdd4dddeSJeff Garzik mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev)); 2934bdd4dddeSJeff Garzik 2935cae5a29dSMark Lord writelfl(0, mmio + hpriv->irq_cause_offset); 2936bdd4dddeSJeff Garzik 2937bdd4dddeSJeff Garzik for (i = 0; i < host->n_ports; i++) { 2938bdd4dddeSJeff Garzik ap = host->ports[i]; 2939936fd732STejun Heo if (!ata_link_offline(&ap->link)) { 29409af5c9c9STejun Heo ehi = &ap->link.eh_info; 2941bdd4dddeSJeff Garzik ata_ehi_clear_desc(ehi); 2942bdd4dddeSJeff Garzik if (!printed++) 2943bdd4dddeSJeff Garzik ata_ehi_push_desc(ehi, 2944bdd4dddeSJeff Garzik "PCI err cause 0x%08x", err_cause); 2945bdd4dddeSJeff Garzik err_mask = AC_ERR_HOST_BUS; 2946cf480626STejun Heo ehi->action = ATA_EH_RESET; 29479af5c9c9STejun Heo qc = ata_qc_from_tag(ap, ap->link.active_tag); 2948bdd4dddeSJeff Garzik if (qc) 2949bdd4dddeSJeff Garzik qc->err_mask |= err_mask; 2950bdd4dddeSJeff Garzik else 2951bdd4dddeSJeff Garzik ehi->err_mask |= err_mask; 2952bdd4dddeSJeff Garzik 2953bdd4dddeSJeff Garzik ata_port_freeze(ap); 2954bdd4dddeSJeff Garzik } 2955bdd4dddeSJeff Garzik } 2956a3718c1fSMark Lord return 1; /* handled */ 2957bdd4dddeSJeff Garzik } 2958bdd4dddeSJeff Garzik 2959c6fd2807SJeff Garzik /** 2960c5d3e45aSJeff Garzik * mv_interrupt - Main interrupt event handler 2961c6fd2807SJeff Garzik * @irq: unused 2962c6fd2807SJeff Garzik * @dev_instance: private data; in this case the host structure 2963c6fd2807SJeff Garzik * 2964c6fd2807SJeff Garzik * Read the read only register to determine if any host 2965c6fd2807SJeff Garzik * controllers have pending interrupts. If so, call lower level 2966c6fd2807SJeff Garzik * routine to handle. Also check for PCI errors which are only 2967c6fd2807SJeff Garzik * reported here. 2968c6fd2807SJeff Garzik * 2969c6fd2807SJeff Garzik * LOCKING: 2970cca3974eSJeff Garzik * This routine holds the host lock while processing pending 2971c6fd2807SJeff Garzik * interrupts. 2972c6fd2807SJeff Garzik */ 29737d12e780SDavid Howells static irqreturn_t mv_interrupt(int irq, void *dev_instance) 2974c6fd2807SJeff Garzik { 2975cca3974eSJeff Garzik struct ata_host *host = dev_instance; 2976f351b2d6SSaeed Bishara struct mv_host_priv *hpriv = host->private_data; 2977a3718c1fSMark Lord unsigned int handled = 0; 29786d3c30efSMark Lord int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI; 297996e2c487SMark Lord u32 main_irq_cause, pending_irqs; 2980c6fd2807SJeff Garzik 2981646a4da5SMark Lord spin_lock(&host->lock); 29826d3c30efSMark Lord 29836d3c30efSMark Lord /* for MSI: block new interrupts while in here */ 29846d3c30efSMark Lord if (using_msi) 29852b748a0aSMark Lord mv_write_main_irq_mask(0, hpriv); 29866d3c30efSMark Lord 29877368f919SMark Lord main_irq_cause = readl(hpriv->main_irq_cause_addr); 298896e2c487SMark Lord pending_irqs = main_irq_cause & hpriv->main_irq_mask; 2989352fab70SMark Lord /* 2990352fab70SMark Lord * Deal with cases where we either have nothing pending, or have read 2991352fab70SMark Lord * a bogus register value which can indicate HW removal or PCI fault. 2992c6fd2807SJeff Garzik */ 2993a44253d2SMark Lord if (pending_irqs && main_irq_cause != 0xffffffffU) { 29941f398472SMark Lord if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv))) 2995a3718c1fSMark Lord handled = mv_pci_error(host, hpriv->base); 2996a3718c1fSMark Lord else 2997a44253d2SMark Lord handled = mv_host_intr(host, pending_irqs); 2998bdd4dddeSJeff Garzik } 29996d3c30efSMark Lord 30006d3c30efSMark Lord /* for MSI: unmask; interrupt cause bits will retrigger now */ 30016d3c30efSMark Lord if (using_msi) 30022b748a0aSMark Lord mv_write_main_irq_mask(hpriv->main_irq_mask, hpriv); 30036d3c30efSMark Lord 30049d51af7bSMark Lord spin_unlock(&host->lock); 30059d51af7bSMark Lord 3006c6fd2807SJeff Garzik return IRQ_RETVAL(handled); 3007c6fd2807SJeff Garzik } 3008c6fd2807SJeff Garzik 3009c6fd2807SJeff Garzik static unsigned int mv5_scr_offset(unsigned int sc_reg_in) 3010c6fd2807SJeff Garzik { 3011c6fd2807SJeff Garzik unsigned int ofs; 3012c6fd2807SJeff Garzik 3013c6fd2807SJeff Garzik switch (sc_reg_in) { 3014c6fd2807SJeff Garzik case SCR_STATUS: 3015c6fd2807SJeff Garzik case SCR_ERROR: 3016c6fd2807SJeff Garzik case SCR_CONTROL: 3017c6fd2807SJeff Garzik ofs = sc_reg_in * sizeof(u32); 3018c6fd2807SJeff Garzik break; 3019c6fd2807SJeff Garzik default: 3020c6fd2807SJeff Garzik ofs = 0xffffffffU; 3021c6fd2807SJeff Garzik break; 3022c6fd2807SJeff Garzik } 3023c6fd2807SJeff Garzik return ofs; 3024c6fd2807SJeff Garzik } 3025c6fd2807SJeff Garzik 302682ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val) 3027c6fd2807SJeff Garzik { 302882ef04fbSTejun Heo struct mv_host_priv *hpriv = link->ap->host->private_data; 3029f351b2d6SSaeed Bishara void __iomem *mmio = hpriv->base; 303082ef04fbSTejun Heo void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no); 3031c6fd2807SJeff Garzik unsigned int ofs = mv5_scr_offset(sc_reg_in); 3032c6fd2807SJeff Garzik 3033da3dbb17STejun Heo if (ofs != 0xffffffffU) { 3034da3dbb17STejun Heo *val = readl(addr + ofs); 3035da3dbb17STejun Heo return 0; 3036da3dbb17STejun Heo } else 3037da3dbb17STejun Heo return -EINVAL; 3038c6fd2807SJeff Garzik } 3039c6fd2807SJeff Garzik 304082ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val) 3041c6fd2807SJeff Garzik { 304282ef04fbSTejun Heo struct mv_host_priv *hpriv = link->ap->host->private_data; 3043f351b2d6SSaeed Bishara void __iomem *mmio = hpriv->base; 304482ef04fbSTejun Heo void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no); 3045c6fd2807SJeff Garzik unsigned int ofs = mv5_scr_offset(sc_reg_in); 3046c6fd2807SJeff Garzik 3047da3dbb17STejun Heo if (ofs != 0xffffffffU) { 30480d5ff566STejun Heo writelfl(val, addr + ofs); 3049da3dbb17STejun Heo return 0; 3050da3dbb17STejun Heo } else 3051da3dbb17STejun Heo return -EINVAL; 3052c6fd2807SJeff Garzik } 3053c6fd2807SJeff Garzik 30547bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio) 3055c6fd2807SJeff Garzik { 30567bb3c529SSaeed Bishara struct pci_dev *pdev = to_pci_dev(host->dev); 3057c6fd2807SJeff Garzik int early_5080; 3058c6fd2807SJeff Garzik 305944c10138SAuke Kok early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0); 3060c6fd2807SJeff Garzik 3061c6fd2807SJeff Garzik if (!early_5080) { 3062c6fd2807SJeff Garzik u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL); 3063c6fd2807SJeff Garzik tmp |= (1 << 0); 3064c6fd2807SJeff Garzik writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL); 3065c6fd2807SJeff Garzik } 3066c6fd2807SJeff Garzik 30677bb3c529SSaeed Bishara mv_reset_pci_bus(host, mmio); 3068c6fd2807SJeff Garzik } 3069c6fd2807SJeff Garzik 3070c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio) 3071c6fd2807SJeff Garzik { 3072cae5a29dSMark Lord writel(0x0fcfffff, mmio + FLASH_CTL); 3073c6fd2807SJeff Garzik } 3074c6fd2807SJeff Garzik 3075c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx, 3076c6fd2807SJeff Garzik void __iomem *mmio) 3077c6fd2807SJeff Garzik { 3078c6fd2807SJeff Garzik void __iomem *phy_mmio = mv5_phy_base(mmio, idx); 3079c6fd2807SJeff Garzik u32 tmp; 3080c6fd2807SJeff Garzik 3081c6fd2807SJeff Garzik tmp = readl(phy_mmio + MV5_PHY_MODE); 3082c6fd2807SJeff Garzik 3083c6fd2807SJeff Garzik hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */ 3084c6fd2807SJeff Garzik hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */ 3085c6fd2807SJeff Garzik } 3086c6fd2807SJeff Garzik 3087c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio) 3088c6fd2807SJeff Garzik { 3089c6fd2807SJeff Garzik u32 tmp; 3090c6fd2807SJeff Garzik 3091cae5a29dSMark Lord writel(0, mmio + GPIO_PORT_CTL); 3092c6fd2807SJeff Garzik 3093c6fd2807SJeff Garzik /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */ 3094c6fd2807SJeff Garzik 3095c6fd2807SJeff Garzik tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL); 3096c6fd2807SJeff Garzik tmp |= ~(1 << 0); 3097c6fd2807SJeff Garzik writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL); 3098c6fd2807SJeff Garzik } 3099c6fd2807SJeff Garzik 3100c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio, 3101c6fd2807SJeff Garzik unsigned int port) 3102c6fd2807SJeff Garzik { 3103c6fd2807SJeff Garzik void __iomem *phy_mmio = mv5_phy_base(mmio, port); 3104c6fd2807SJeff Garzik const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5); 3105c6fd2807SJeff Garzik u32 tmp; 3106c6fd2807SJeff Garzik int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0); 3107c6fd2807SJeff Garzik 3108c6fd2807SJeff Garzik if (fix_apm_sq) { 3109cae5a29dSMark Lord tmp = readl(phy_mmio + MV5_LTMODE); 3110c6fd2807SJeff Garzik tmp |= (1 << 19); 3111cae5a29dSMark Lord writel(tmp, phy_mmio + MV5_LTMODE); 3112c6fd2807SJeff Garzik 3113cae5a29dSMark Lord tmp = readl(phy_mmio + MV5_PHY_CTL); 3114c6fd2807SJeff Garzik tmp &= ~0x3; 3115c6fd2807SJeff Garzik tmp |= 0x1; 3116cae5a29dSMark Lord writel(tmp, phy_mmio + MV5_PHY_CTL); 3117c6fd2807SJeff Garzik } 3118c6fd2807SJeff Garzik 3119c6fd2807SJeff Garzik tmp = readl(phy_mmio + MV5_PHY_MODE); 3120c6fd2807SJeff Garzik tmp &= ~mask; 3121c6fd2807SJeff Garzik tmp |= hpriv->signal[port].pre; 3122c6fd2807SJeff Garzik tmp |= hpriv->signal[port].amps; 3123c6fd2807SJeff Garzik writel(tmp, phy_mmio + MV5_PHY_MODE); 3124c6fd2807SJeff Garzik } 3125c6fd2807SJeff Garzik 3126c6fd2807SJeff Garzik 3127c6fd2807SJeff Garzik #undef ZERO 3128c6fd2807SJeff Garzik #define ZERO(reg) writel(0, port_mmio + (reg)) 3129c6fd2807SJeff Garzik static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio, 3130c6fd2807SJeff Garzik unsigned int port) 3131c6fd2807SJeff Garzik { 3132c6fd2807SJeff Garzik void __iomem *port_mmio = mv_port_base(mmio, port); 3133c6fd2807SJeff Garzik 3134e12bef50SMark Lord mv_reset_channel(hpriv, mmio, port); 3135c6fd2807SJeff Garzik 3136c6fd2807SJeff Garzik ZERO(0x028); /* command */ 3137cae5a29dSMark Lord writel(0x11f, port_mmio + EDMA_CFG); 3138c6fd2807SJeff Garzik ZERO(0x004); /* timer */ 3139c6fd2807SJeff Garzik ZERO(0x008); /* irq err cause */ 3140c6fd2807SJeff Garzik ZERO(0x00c); /* irq err mask */ 3141c6fd2807SJeff Garzik ZERO(0x010); /* rq bah */ 3142c6fd2807SJeff Garzik ZERO(0x014); /* rq inp */ 3143c6fd2807SJeff Garzik ZERO(0x018); /* rq outp */ 3144c6fd2807SJeff Garzik ZERO(0x01c); /* respq bah */ 3145c6fd2807SJeff Garzik ZERO(0x024); /* respq outp */ 3146c6fd2807SJeff Garzik ZERO(0x020); /* respq inp */ 3147c6fd2807SJeff Garzik ZERO(0x02c); /* test control */ 3148cae5a29dSMark Lord writel(0xbc, port_mmio + EDMA_IORDY_TMOUT); 3149c6fd2807SJeff Garzik } 3150c6fd2807SJeff Garzik #undef ZERO 3151c6fd2807SJeff Garzik 3152c6fd2807SJeff Garzik #define ZERO(reg) writel(0, hc_mmio + (reg)) 3153c6fd2807SJeff Garzik static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio, 3154c6fd2807SJeff Garzik unsigned int hc) 3155c6fd2807SJeff Garzik { 3156c6fd2807SJeff Garzik void __iomem *hc_mmio = mv_hc_base(mmio, hc); 3157c6fd2807SJeff Garzik u32 tmp; 3158c6fd2807SJeff Garzik 3159c6fd2807SJeff Garzik ZERO(0x00c); 3160c6fd2807SJeff Garzik ZERO(0x010); 3161c6fd2807SJeff Garzik ZERO(0x014); 3162c6fd2807SJeff Garzik ZERO(0x018); 3163c6fd2807SJeff Garzik 3164c6fd2807SJeff Garzik tmp = readl(hc_mmio + 0x20); 3165c6fd2807SJeff Garzik tmp &= 0x1c1c1c1c; 3166c6fd2807SJeff Garzik tmp |= 0x03030303; 3167c6fd2807SJeff Garzik writel(tmp, hc_mmio + 0x20); 3168c6fd2807SJeff Garzik } 3169c6fd2807SJeff Garzik #undef ZERO 3170c6fd2807SJeff Garzik 3171c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio, 3172c6fd2807SJeff Garzik unsigned int n_hc) 3173c6fd2807SJeff Garzik { 3174c6fd2807SJeff Garzik unsigned int hc, port; 3175c6fd2807SJeff Garzik 3176c6fd2807SJeff Garzik for (hc = 0; hc < n_hc; hc++) { 3177c6fd2807SJeff Garzik for (port = 0; port < MV_PORTS_PER_HC; port++) 3178c6fd2807SJeff Garzik mv5_reset_hc_port(hpriv, mmio, 3179c6fd2807SJeff Garzik (hc * MV_PORTS_PER_HC) + port); 3180c6fd2807SJeff Garzik 3181c6fd2807SJeff Garzik mv5_reset_one_hc(hpriv, mmio, hc); 3182c6fd2807SJeff Garzik } 3183c6fd2807SJeff Garzik 3184c6fd2807SJeff Garzik return 0; 3185c6fd2807SJeff Garzik } 3186c6fd2807SJeff Garzik 3187c6fd2807SJeff Garzik #undef ZERO 3188c6fd2807SJeff Garzik #define ZERO(reg) writel(0, mmio + (reg)) 31897bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio) 3190c6fd2807SJeff Garzik { 319102a121daSMark Lord struct mv_host_priv *hpriv = host->private_data; 3192c6fd2807SJeff Garzik u32 tmp; 3193c6fd2807SJeff Garzik 3194cae5a29dSMark Lord tmp = readl(mmio + MV_PCI_MODE); 3195c6fd2807SJeff Garzik tmp &= 0xff00ffff; 3196cae5a29dSMark Lord writel(tmp, mmio + MV_PCI_MODE); 3197c6fd2807SJeff Garzik 3198c6fd2807SJeff Garzik ZERO(MV_PCI_DISC_TIMER); 3199c6fd2807SJeff Garzik ZERO(MV_PCI_MSI_TRIGGER); 3200cae5a29dSMark Lord writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT); 3201c6fd2807SJeff Garzik ZERO(MV_PCI_SERR_MASK); 3202cae5a29dSMark Lord ZERO(hpriv->irq_cause_offset); 3203cae5a29dSMark Lord ZERO(hpriv->irq_mask_offset); 3204c6fd2807SJeff Garzik ZERO(MV_PCI_ERR_LOW_ADDRESS); 3205c6fd2807SJeff Garzik ZERO(MV_PCI_ERR_HIGH_ADDRESS); 3206c6fd2807SJeff Garzik ZERO(MV_PCI_ERR_ATTRIBUTE); 3207c6fd2807SJeff Garzik ZERO(MV_PCI_ERR_COMMAND); 3208c6fd2807SJeff Garzik } 3209c6fd2807SJeff Garzik #undef ZERO 3210c6fd2807SJeff Garzik 3211c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio) 3212c6fd2807SJeff Garzik { 3213c6fd2807SJeff Garzik u32 tmp; 3214c6fd2807SJeff Garzik 3215c6fd2807SJeff Garzik mv5_reset_flash(hpriv, mmio); 3216c6fd2807SJeff Garzik 3217cae5a29dSMark Lord tmp = readl(mmio + GPIO_PORT_CTL); 3218c6fd2807SJeff Garzik tmp &= 0x3; 3219c6fd2807SJeff Garzik tmp |= (1 << 5) | (1 << 6); 3220cae5a29dSMark Lord writel(tmp, mmio + GPIO_PORT_CTL); 3221c6fd2807SJeff Garzik } 3222c6fd2807SJeff Garzik 3223c6fd2807SJeff Garzik /** 3224c6fd2807SJeff Garzik * mv6_reset_hc - Perform the 6xxx global soft reset 3225c6fd2807SJeff Garzik * @mmio: base address of the HBA 3226c6fd2807SJeff Garzik * 3227c6fd2807SJeff Garzik * This routine only applies to 6xxx parts. 3228c6fd2807SJeff Garzik * 3229c6fd2807SJeff Garzik * LOCKING: 3230c6fd2807SJeff Garzik * Inherited from caller. 3231c6fd2807SJeff Garzik */ 3232c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio, 3233c6fd2807SJeff Garzik unsigned int n_hc) 3234c6fd2807SJeff Garzik { 3235cae5a29dSMark Lord void __iomem *reg = mmio + PCI_MAIN_CMD_STS; 3236c6fd2807SJeff Garzik int i, rc = 0; 3237c6fd2807SJeff Garzik u32 t; 3238c6fd2807SJeff Garzik 3239c6fd2807SJeff Garzik /* Following procedure defined in PCI "main command and status 3240c6fd2807SJeff Garzik * register" table. 3241c6fd2807SJeff Garzik */ 3242c6fd2807SJeff Garzik t = readl(reg); 3243c6fd2807SJeff Garzik writel(t | STOP_PCI_MASTER, reg); 3244c6fd2807SJeff Garzik 3245c6fd2807SJeff Garzik for (i = 0; i < 1000; i++) { 3246c6fd2807SJeff Garzik udelay(1); 3247c6fd2807SJeff Garzik t = readl(reg); 32482dcb407eSJeff Garzik if (PCI_MASTER_EMPTY & t) 3249c6fd2807SJeff Garzik break; 3250c6fd2807SJeff Garzik } 3251c6fd2807SJeff Garzik if (!(PCI_MASTER_EMPTY & t)) { 3252c6fd2807SJeff Garzik printk(KERN_ERR DRV_NAME ": PCI master won't flush\n"); 3253c6fd2807SJeff Garzik rc = 1; 3254c6fd2807SJeff Garzik goto done; 3255c6fd2807SJeff Garzik } 3256c6fd2807SJeff Garzik 3257c6fd2807SJeff Garzik /* set reset */ 3258c6fd2807SJeff Garzik i = 5; 3259c6fd2807SJeff Garzik do { 3260c6fd2807SJeff Garzik writel(t | GLOB_SFT_RST, reg); 3261c6fd2807SJeff Garzik t = readl(reg); 3262c6fd2807SJeff Garzik udelay(1); 3263c6fd2807SJeff Garzik } while (!(GLOB_SFT_RST & t) && (i-- > 0)); 3264c6fd2807SJeff Garzik 3265c6fd2807SJeff Garzik if (!(GLOB_SFT_RST & t)) { 3266c6fd2807SJeff Garzik printk(KERN_ERR DRV_NAME ": can't set global reset\n"); 3267c6fd2807SJeff Garzik rc = 1; 3268c6fd2807SJeff Garzik goto done; 3269c6fd2807SJeff Garzik } 3270c6fd2807SJeff Garzik 3271c6fd2807SJeff Garzik /* clear reset and *reenable the PCI master* (not mentioned in spec) */ 3272c6fd2807SJeff Garzik i = 5; 3273c6fd2807SJeff Garzik do { 3274c6fd2807SJeff Garzik writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg); 3275c6fd2807SJeff Garzik t = readl(reg); 3276c6fd2807SJeff Garzik udelay(1); 3277c6fd2807SJeff Garzik } while ((GLOB_SFT_RST & t) && (i-- > 0)); 3278c6fd2807SJeff Garzik 3279c6fd2807SJeff Garzik if (GLOB_SFT_RST & t) { 3280c6fd2807SJeff Garzik printk(KERN_ERR DRV_NAME ": can't clear global reset\n"); 3281c6fd2807SJeff Garzik rc = 1; 3282c6fd2807SJeff Garzik } 3283c6fd2807SJeff Garzik done: 3284c6fd2807SJeff Garzik return rc; 3285c6fd2807SJeff Garzik } 3286c6fd2807SJeff Garzik 3287c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx, 3288c6fd2807SJeff Garzik void __iomem *mmio) 3289c6fd2807SJeff Garzik { 3290c6fd2807SJeff Garzik void __iomem *port_mmio; 3291c6fd2807SJeff Garzik u32 tmp; 3292c6fd2807SJeff Garzik 3293cae5a29dSMark Lord tmp = readl(mmio + RESET_CFG); 3294c6fd2807SJeff Garzik if ((tmp & (1 << 0)) == 0) { 3295c6fd2807SJeff Garzik hpriv->signal[idx].amps = 0x7 << 8; 3296c6fd2807SJeff Garzik hpriv->signal[idx].pre = 0x1 << 5; 3297c6fd2807SJeff Garzik return; 3298c6fd2807SJeff Garzik } 3299c6fd2807SJeff Garzik 3300c6fd2807SJeff Garzik port_mmio = mv_port_base(mmio, idx); 3301c6fd2807SJeff Garzik tmp = readl(port_mmio + PHY_MODE2); 3302c6fd2807SJeff Garzik 3303c6fd2807SJeff Garzik hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */ 3304c6fd2807SJeff Garzik hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */ 3305c6fd2807SJeff Garzik } 3306c6fd2807SJeff Garzik 3307c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio) 3308c6fd2807SJeff Garzik { 3309cae5a29dSMark Lord writel(0x00000060, mmio + GPIO_PORT_CTL); 3310c6fd2807SJeff Garzik } 3311c6fd2807SJeff Garzik 3312c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio, 3313c6fd2807SJeff Garzik unsigned int port) 3314c6fd2807SJeff Garzik { 3315c6fd2807SJeff Garzik void __iomem *port_mmio = mv_port_base(mmio, port); 3316c6fd2807SJeff Garzik 3317c6fd2807SJeff Garzik u32 hp_flags = hpriv->hp_flags; 3318c6fd2807SJeff Garzik int fix_phy_mode2 = 3319c6fd2807SJeff Garzik hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0); 3320c6fd2807SJeff Garzik int fix_phy_mode4 = 3321c6fd2807SJeff Garzik hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0); 33228c30a8b9SMark Lord u32 m2, m3; 3323c6fd2807SJeff Garzik 3324c6fd2807SJeff Garzik if (fix_phy_mode2) { 3325c6fd2807SJeff Garzik m2 = readl(port_mmio + PHY_MODE2); 3326c6fd2807SJeff Garzik m2 &= ~(1 << 16); 3327c6fd2807SJeff Garzik m2 |= (1 << 31); 3328c6fd2807SJeff Garzik writel(m2, port_mmio + PHY_MODE2); 3329c6fd2807SJeff Garzik 3330c6fd2807SJeff Garzik udelay(200); 3331c6fd2807SJeff Garzik 3332c6fd2807SJeff Garzik m2 = readl(port_mmio + PHY_MODE2); 3333c6fd2807SJeff Garzik m2 &= ~((1 << 16) | (1 << 31)); 3334c6fd2807SJeff Garzik writel(m2, port_mmio + PHY_MODE2); 3335c6fd2807SJeff Garzik 3336c6fd2807SJeff Garzik udelay(200); 3337c6fd2807SJeff Garzik } 3338c6fd2807SJeff Garzik 33398c30a8b9SMark Lord /* 33408c30a8b9SMark Lord * Gen-II/IIe PHY_MODE3 errata RM#2: 33418c30a8b9SMark Lord * Achieves better receiver noise performance than the h/w default: 33428c30a8b9SMark Lord */ 33438c30a8b9SMark Lord m3 = readl(port_mmio + PHY_MODE3); 33448c30a8b9SMark Lord m3 = (m3 & 0x1f) | (0x5555601 << 5); 3345c6fd2807SJeff Garzik 33460388a8c0SMark Lord /* Guideline 88F5182 (GL# SATA-S11) */ 33470388a8c0SMark Lord if (IS_SOC(hpriv)) 33480388a8c0SMark Lord m3 &= ~0x1c; 33490388a8c0SMark Lord 3350c6fd2807SJeff Garzik if (fix_phy_mode4) { 3351ba069e37SMark Lord u32 m4 = readl(port_mmio + PHY_MODE4); 3352ba069e37SMark Lord /* 3353ba069e37SMark Lord * Enforce reserved-bit restrictions on GenIIe devices only. 3354ba069e37SMark Lord * For earlier chipsets, force only the internal config field 3355ba069e37SMark Lord * (workaround for errata FEr SATA#10 part 1). 3356ba069e37SMark Lord */ 33578c30a8b9SMark Lord if (IS_GEN_IIE(hpriv)) 3358ba069e37SMark Lord m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES; 3359ba069e37SMark Lord else 3360ba069e37SMark Lord m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE; 33618c30a8b9SMark Lord writel(m4, port_mmio + PHY_MODE4); 3362c6fd2807SJeff Garzik } 3363b406c7a6SMark Lord /* 3364b406c7a6SMark Lord * Workaround for 60x1-B2 errata SATA#13: 3365b406c7a6SMark Lord * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3, 3366b406c7a6SMark Lord * so we must always rewrite PHY_MODE3 after PHY_MODE4. 3367ba68460bSMark Lord * Or ensure we use writelfl() when writing PHY_MODE4. 3368b406c7a6SMark Lord */ 3369b406c7a6SMark Lord writel(m3, port_mmio + PHY_MODE3); 3370c6fd2807SJeff Garzik 3371c6fd2807SJeff Garzik /* Revert values of pre-emphasis and signal amps to the saved ones */ 3372c6fd2807SJeff Garzik m2 = readl(port_mmio + PHY_MODE2); 3373c6fd2807SJeff Garzik 3374c6fd2807SJeff Garzik m2 &= ~MV_M2_PREAMP_MASK; 3375c6fd2807SJeff Garzik m2 |= hpriv->signal[port].amps; 3376c6fd2807SJeff Garzik m2 |= hpriv->signal[port].pre; 3377c6fd2807SJeff Garzik m2 &= ~(1 << 16); 3378c6fd2807SJeff Garzik 3379c6fd2807SJeff Garzik /* according to mvSata 3.6.1, some IIE values are fixed */ 3380c6fd2807SJeff Garzik if (IS_GEN_IIE(hpriv)) { 3381c6fd2807SJeff Garzik m2 &= ~0xC30FF01F; 3382c6fd2807SJeff Garzik m2 |= 0x0000900F; 3383c6fd2807SJeff Garzik } 3384c6fd2807SJeff Garzik 3385c6fd2807SJeff Garzik writel(m2, port_mmio + PHY_MODE2); 3386c6fd2807SJeff Garzik } 3387c6fd2807SJeff Garzik 3388f351b2d6SSaeed Bishara /* TODO: use the generic LED interface to configure the SATA Presence */ 3389f351b2d6SSaeed Bishara /* & Acitivy LEDs on the board */ 3390f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv, 3391f351b2d6SSaeed Bishara void __iomem *mmio) 3392f351b2d6SSaeed Bishara { 3393f351b2d6SSaeed Bishara return; 3394f351b2d6SSaeed Bishara } 3395f351b2d6SSaeed Bishara 3396f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx, 3397f351b2d6SSaeed Bishara void __iomem *mmio) 3398f351b2d6SSaeed Bishara { 3399f351b2d6SSaeed Bishara void __iomem *port_mmio; 3400f351b2d6SSaeed Bishara u32 tmp; 3401f351b2d6SSaeed Bishara 3402f351b2d6SSaeed Bishara port_mmio = mv_port_base(mmio, idx); 3403f351b2d6SSaeed Bishara tmp = readl(port_mmio + PHY_MODE2); 3404f351b2d6SSaeed Bishara 3405f351b2d6SSaeed Bishara hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */ 3406f351b2d6SSaeed Bishara hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */ 3407f351b2d6SSaeed Bishara } 3408f351b2d6SSaeed Bishara 3409f351b2d6SSaeed Bishara #undef ZERO 3410f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, port_mmio + (reg)) 3411f351b2d6SSaeed Bishara static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv, 3412f351b2d6SSaeed Bishara void __iomem *mmio, unsigned int port) 3413f351b2d6SSaeed Bishara { 3414f351b2d6SSaeed Bishara void __iomem *port_mmio = mv_port_base(mmio, port); 3415f351b2d6SSaeed Bishara 3416e12bef50SMark Lord mv_reset_channel(hpriv, mmio, port); 3417f351b2d6SSaeed Bishara 3418f351b2d6SSaeed Bishara ZERO(0x028); /* command */ 3419cae5a29dSMark Lord writel(0x101f, port_mmio + EDMA_CFG); 3420f351b2d6SSaeed Bishara ZERO(0x004); /* timer */ 3421f351b2d6SSaeed Bishara ZERO(0x008); /* irq err cause */ 3422f351b2d6SSaeed Bishara ZERO(0x00c); /* irq err mask */ 3423f351b2d6SSaeed Bishara ZERO(0x010); /* rq bah */ 3424f351b2d6SSaeed Bishara ZERO(0x014); /* rq inp */ 3425f351b2d6SSaeed Bishara ZERO(0x018); /* rq outp */ 3426f351b2d6SSaeed Bishara ZERO(0x01c); /* respq bah */ 3427f351b2d6SSaeed Bishara ZERO(0x024); /* respq outp */ 3428f351b2d6SSaeed Bishara ZERO(0x020); /* respq inp */ 3429f351b2d6SSaeed Bishara ZERO(0x02c); /* test control */ 3430d7b0c143SSaeed Bishara writel(0x800, port_mmio + EDMA_IORDY_TMOUT); 3431f351b2d6SSaeed Bishara } 3432f351b2d6SSaeed Bishara 3433f351b2d6SSaeed Bishara #undef ZERO 3434f351b2d6SSaeed Bishara 3435f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, hc_mmio + (reg)) 3436f351b2d6SSaeed Bishara static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv, 3437f351b2d6SSaeed Bishara void __iomem *mmio) 3438f351b2d6SSaeed Bishara { 3439f351b2d6SSaeed Bishara void __iomem *hc_mmio = mv_hc_base(mmio, 0); 3440f351b2d6SSaeed Bishara 3441f351b2d6SSaeed Bishara ZERO(0x00c); 3442f351b2d6SSaeed Bishara ZERO(0x010); 3443f351b2d6SSaeed Bishara ZERO(0x014); 3444f351b2d6SSaeed Bishara 3445f351b2d6SSaeed Bishara } 3446f351b2d6SSaeed Bishara 3447f351b2d6SSaeed Bishara #undef ZERO 3448f351b2d6SSaeed Bishara 3449f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv, 3450f351b2d6SSaeed Bishara void __iomem *mmio, unsigned int n_hc) 3451f351b2d6SSaeed Bishara { 3452f351b2d6SSaeed Bishara unsigned int port; 3453f351b2d6SSaeed Bishara 3454f351b2d6SSaeed Bishara for (port = 0; port < hpriv->n_ports; port++) 3455f351b2d6SSaeed Bishara mv_soc_reset_hc_port(hpriv, mmio, port); 3456f351b2d6SSaeed Bishara 3457f351b2d6SSaeed Bishara mv_soc_reset_one_hc(hpriv, mmio); 3458f351b2d6SSaeed Bishara 3459f351b2d6SSaeed Bishara return 0; 3460f351b2d6SSaeed Bishara } 3461f351b2d6SSaeed Bishara 3462f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv, 3463f351b2d6SSaeed Bishara void __iomem *mmio) 3464f351b2d6SSaeed Bishara { 3465f351b2d6SSaeed Bishara return; 3466f351b2d6SSaeed Bishara } 3467f351b2d6SSaeed Bishara 3468f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio) 3469f351b2d6SSaeed Bishara { 3470f351b2d6SSaeed Bishara return; 3471f351b2d6SSaeed Bishara } 3472f351b2d6SSaeed Bishara 347329b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv, 347429b7e43cSMartin Michlmayr void __iomem *mmio, unsigned int port) 347529b7e43cSMartin Michlmayr { 347629b7e43cSMartin Michlmayr void __iomem *port_mmio = mv_port_base(mmio, port); 347729b7e43cSMartin Michlmayr u32 reg; 347829b7e43cSMartin Michlmayr 347929b7e43cSMartin Michlmayr reg = readl(port_mmio + PHY_MODE3); 348029b7e43cSMartin Michlmayr reg &= ~(0x3 << 27); /* SELMUPF (bits 28:27) to 1 */ 348129b7e43cSMartin Michlmayr reg |= (0x1 << 27); 348229b7e43cSMartin Michlmayr reg &= ~(0x3 << 29); /* SELMUPI (bits 30:29) to 1 */ 348329b7e43cSMartin Michlmayr reg |= (0x1 << 29); 348429b7e43cSMartin Michlmayr writel(reg, port_mmio + PHY_MODE3); 348529b7e43cSMartin Michlmayr 348629b7e43cSMartin Michlmayr reg = readl(port_mmio + PHY_MODE4); 348729b7e43cSMartin Michlmayr reg &= ~0x1; /* SATU_OD8 (bit 0) to 0, reserved bit 16 must be set */ 348829b7e43cSMartin Michlmayr reg |= (0x1 << 16); 348929b7e43cSMartin Michlmayr writel(reg, port_mmio + PHY_MODE4); 349029b7e43cSMartin Michlmayr 349129b7e43cSMartin Michlmayr reg = readl(port_mmio + PHY_MODE9_GEN2); 349229b7e43cSMartin Michlmayr reg &= ~0xf; /* TXAMP[3:0] (bits 3:0) to 8 */ 349329b7e43cSMartin Michlmayr reg |= 0x8; 349429b7e43cSMartin Michlmayr reg &= ~(0x1 << 14); /* TXAMP[4] (bit 14) to 0 */ 349529b7e43cSMartin Michlmayr writel(reg, port_mmio + PHY_MODE9_GEN2); 349629b7e43cSMartin Michlmayr 349729b7e43cSMartin Michlmayr reg = readl(port_mmio + PHY_MODE9_GEN1); 349829b7e43cSMartin Michlmayr reg &= ~0xf; /* TXAMP[3:0] (bits 3:0) to 8 */ 349929b7e43cSMartin Michlmayr reg |= 0x8; 350029b7e43cSMartin Michlmayr reg &= ~(0x1 << 14); /* TXAMP[4] (bit 14) to 0 */ 350129b7e43cSMartin Michlmayr writel(reg, port_mmio + PHY_MODE9_GEN1); 350229b7e43cSMartin Michlmayr } 350329b7e43cSMartin Michlmayr 350429b7e43cSMartin Michlmayr /** 350529b7e43cSMartin Michlmayr * soc_is_65 - check if the soc is 65 nano device 350629b7e43cSMartin Michlmayr * 350729b7e43cSMartin Michlmayr * Detect the type of the SoC, this is done by reading the PHYCFG_OFS 350829b7e43cSMartin Michlmayr * register, this register should contain non-zero value and it exists only 350929b7e43cSMartin Michlmayr * in the 65 nano devices, when reading it from older devices we get 0. 351029b7e43cSMartin Michlmayr */ 351129b7e43cSMartin Michlmayr static bool soc_is_65n(struct mv_host_priv *hpriv) 351229b7e43cSMartin Michlmayr { 351329b7e43cSMartin Michlmayr void __iomem *port0_mmio = mv_port_base(hpriv->base, 0); 351429b7e43cSMartin Michlmayr 351529b7e43cSMartin Michlmayr if (readl(port0_mmio + PHYCFG_OFS)) 351629b7e43cSMartin Michlmayr return true; 351729b7e43cSMartin Michlmayr return false; 351829b7e43cSMartin Michlmayr } 351929b7e43cSMartin Michlmayr 35208e7decdbSMark Lord static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i) 3521b67a1064SMark Lord { 3522cae5a29dSMark Lord u32 ifcfg = readl(port_mmio + SATA_IFCFG); 3523b67a1064SMark Lord 35248e7decdbSMark Lord ifcfg = (ifcfg & 0xf7f) | 0x9b1000; /* from chip spec */ 3525b67a1064SMark Lord if (want_gen2i) 35268e7decdbSMark Lord ifcfg |= (1 << 7); /* enable gen2i speed */ 3527cae5a29dSMark Lord writelfl(ifcfg, port_mmio + SATA_IFCFG); 3528b67a1064SMark Lord } 3529b67a1064SMark Lord 3530e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio, 3531c6fd2807SJeff Garzik unsigned int port_no) 3532c6fd2807SJeff Garzik { 3533c6fd2807SJeff Garzik void __iomem *port_mmio = mv_port_base(mmio, port_no); 3534c6fd2807SJeff Garzik 35358e7decdbSMark Lord /* 35368e7decdbSMark Lord * The datasheet warns against setting EDMA_RESET when EDMA is active 35378e7decdbSMark Lord * (but doesn't say what the problem might be). So we first try 35388e7decdbSMark Lord * to disable the EDMA engine before doing the EDMA_RESET operation. 35398e7decdbSMark Lord */ 35400d8be5cbSMark Lord mv_stop_edma_engine(port_mmio); 3541cae5a29dSMark Lord writelfl(EDMA_RESET, port_mmio + EDMA_CMD); 3542c6fd2807SJeff Garzik 3543b67a1064SMark Lord if (!IS_GEN_I(hpriv)) { 35448e7decdbSMark Lord /* Enable 3.0gb/s link speed: this survives EDMA_RESET */ 35458e7decdbSMark Lord mv_setup_ifcfg(port_mmio, 1); 3546c6fd2807SJeff Garzik } 3547b67a1064SMark Lord /* 35488e7decdbSMark Lord * Strobing EDMA_RESET here causes a hard reset of the SATA transport, 3549b67a1064SMark Lord * link, and physical layers. It resets all SATA interface registers 3550cae5a29dSMark Lord * (except for SATA_IFCFG), and issues a COMRESET to the dev. 3551c6fd2807SJeff Garzik */ 3552cae5a29dSMark Lord writelfl(EDMA_RESET, port_mmio + EDMA_CMD); 3553b67a1064SMark Lord udelay(25); /* allow reset propagation */ 3554cae5a29dSMark Lord writelfl(0, port_mmio + EDMA_CMD); 3555c6fd2807SJeff Garzik 3556c6fd2807SJeff Garzik hpriv->ops->phy_errata(hpriv, mmio, port_no); 3557c6fd2807SJeff Garzik 3558ee9ccdf7SJeff Garzik if (IS_GEN_I(hpriv)) 3559c6fd2807SJeff Garzik mdelay(1); 3560c6fd2807SJeff Garzik } 3561c6fd2807SJeff Garzik 3562e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp) 3563e49856d8SMark Lord { 3564e49856d8SMark Lord if (sata_pmp_supported(ap)) { 3565e49856d8SMark Lord void __iomem *port_mmio = mv_ap_base(ap); 3566cae5a29dSMark Lord u32 reg = readl(port_mmio + SATA_IFCTL); 3567e49856d8SMark Lord int old = reg & 0xf; 3568e49856d8SMark Lord 3569e49856d8SMark Lord if (old != pmp) { 3570e49856d8SMark Lord reg = (reg & ~0xf) | pmp; 3571cae5a29dSMark Lord writelfl(reg, port_mmio + SATA_IFCTL); 3572e49856d8SMark Lord } 3573e49856d8SMark Lord } 3574e49856d8SMark Lord } 3575e49856d8SMark Lord 3576e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class, 3577bdd4dddeSJeff Garzik unsigned long deadline) 3578c6fd2807SJeff Garzik { 3579e49856d8SMark Lord mv_pmp_select(link->ap, sata_srst_pmp(link)); 3580e49856d8SMark Lord return sata_std_hardreset(link, class, deadline); 3581e49856d8SMark Lord } 3582c6fd2807SJeff Garzik 3583e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class, 3584e49856d8SMark Lord unsigned long deadline) 3585da3dbb17STejun Heo { 3586e49856d8SMark Lord mv_pmp_select(link->ap, sata_srst_pmp(link)); 3587e49856d8SMark Lord return ata_sff_softreset(link, class, deadline); 3588bdd4dddeSJeff Garzik } 3589bdd4dddeSJeff Garzik 3590cc0680a5STejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class, 3591bdd4dddeSJeff Garzik unsigned long deadline) 3592bdd4dddeSJeff Garzik { 3593cc0680a5STejun Heo struct ata_port *ap = link->ap; 3594bdd4dddeSJeff Garzik struct mv_host_priv *hpriv = ap->host->private_data; 3595b562468cSMark Lord struct mv_port_priv *pp = ap->private_data; 3596f351b2d6SSaeed Bishara void __iomem *mmio = hpriv->base; 35970d8be5cbSMark Lord int rc, attempts = 0, extra = 0; 35980d8be5cbSMark Lord u32 sstatus; 35990d8be5cbSMark Lord bool online; 3600bdd4dddeSJeff Garzik 3601e12bef50SMark Lord mv_reset_channel(hpriv, mmio, ap->port_no); 3602b562468cSMark Lord pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN; 3603d16ab3f6SMark Lord pp->pp_flags &= 3604d16ab3f6SMark Lord ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY); 3605bdd4dddeSJeff Garzik 36060d8be5cbSMark Lord /* Workaround for errata FEr SATA#10 (part 2) */ 36070d8be5cbSMark Lord do { 360817c5aab5SMark Lord const unsigned long *timing = 360917c5aab5SMark Lord sata_ehc_deb_timing(&link->eh_context); 3610bdd4dddeSJeff Garzik 361117c5aab5SMark Lord rc = sata_link_hardreset(link, timing, deadline + extra, 361217c5aab5SMark Lord &online, NULL); 36139dcffd99SMark Lord rc = online ? -EAGAIN : rc; 361417c5aab5SMark Lord if (rc) 36150d8be5cbSMark Lord return rc; 36160d8be5cbSMark Lord sata_scr_read(link, SCR_STATUS, &sstatus); 36170d8be5cbSMark Lord if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) { 36180d8be5cbSMark Lord /* Force 1.5gb/s link speed and try again */ 36198e7decdbSMark Lord mv_setup_ifcfg(mv_ap_base(ap), 0); 36200d8be5cbSMark Lord if (time_after(jiffies + HZ, deadline)) 36210d8be5cbSMark Lord extra = HZ; /* only extend it once, max */ 3622bdd4dddeSJeff Garzik } 36230d8be5cbSMark Lord } while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123); 362408da1759SMark Lord mv_save_cached_regs(ap); 362566e57a2cSMark Lord mv_edma_cfg(ap, 0, 0); 3626bdd4dddeSJeff Garzik 362717c5aab5SMark Lord return rc; 3628bdd4dddeSJeff Garzik } 3629bdd4dddeSJeff Garzik 3630bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap) 3631c6fd2807SJeff Garzik { 36321cfd19aeSMark Lord mv_stop_edma(ap); 3633c4de573bSMark Lord mv_enable_port_irqs(ap, 0); 3634c6fd2807SJeff Garzik } 3635bdd4dddeSJeff Garzik 3636bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap) 3637bdd4dddeSJeff Garzik { 3638f351b2d6SSaeed Bishara struct mv_host_priv *hpriv = ap->host->private_data; 3639c4de573bSMark Lord unsigned int port = ap->port_no; 3640c4de573bSMark Lord unsigned int hardport = mv_hardport_from_port(port); 36411cfd19aeSMark Lord void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port); 3642bdd4dddeSJeff Garzik void __iomem *port_mmio = mv_ap_base(ap); 3643c4de573bSMark Lord u32 hc_irq_cause; 3644bdd4dddeSJeff Garzik 3645bdd4dddeSJeff Garzik /* clear EDMA errors on this port */ 3646cae5a29dSMark Lord writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE); 3647bdd4dddeSJeff Garzik 3648bdd4dddeSJeff Garzik /* clear pending irq events */ 3649cae6edc3SMark Lord hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport); 3650cae5a29dSMark Lord writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE); 3651bdd4dddeSJeff Garzik 365288e675e1SMark Lord mv_enable_port_irqs(ap, ERR_IRQ); 3653c6fd2807SJeff Garzik } 3654c6fd2807SJeff Garzik 3655c6fd2807SJeff Garzik /** 3656c6fd2807SJeff Garzik * mv_port_init - Perform some early initialization on a single port. 3657c6fd2807SJeff Garzik * @port: libata data structure storing shadow register addresses 3658c6fd2807SJeff Garzik * @port_mmio: base address of the port 3659c6fd2807SJeff Garzik * 3660c6fd2807SJeff Garzik * Initialize shadow register mmio addresses, clear outstanding 3661c6fd2807SJeff Garzik * interrupts on the port, and unmask interrupts for the future 3662c6fd2807SJeff Garzik * start of the port. 3663c6fd2807SJeff Garzik * 3664c6fd2807SJeff Garzik * LOCKING: 3665c6fd2807SJeff Garzik * Inherited from caller. 3666c6fd2807SJeff Garzik */ 3667c6fd2807SJeff Garzik static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio) 3668c6fd2807SJeff Garzik { 3669cae5a29dSMark Lord void __iomem *serr, *shd_base = port_mmio + SHD_BLK; 3670c6fd2807SJeff Garzik 3671c6fd2807SJeff Garzik /* PIO related setup 3672c6fd2807SJeff Garzik */ 3673c6fd2807SJeff Garzik port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA); 3674c6fd2807SJeff Garzik port->error_addr = 3675c6fd2807SJeff Garzik port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR); 3676c6fd2807SJeff Garzik port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT); 3677c6fd2807SJeff Garzik port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL); 3678c6fd2807SJeff Garzik port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM); 3679c6fd2807SJeff Garzik port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH); 3680c6fd2807SJeff Garzik port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE); 3681c6fd2807SJeff Garzik port->status_addr = 3682c6fd2807SJeff Garzik port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS); 3683c6fd2807SJeff Garzik /* special case: control/altstatus doesn't have ATA_REG_ address */ 3684cae5a29dSMark Lord port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST; 3685c6fd2807SJeff Garzik 3686c6fd2807SJeff Garzik /* Clear any currently outstanding port interrupt conditions */ 3687cae5a29dSMark Lord serr = port_mmio + mv_scr_offset(SCR_ERROR); 3688cae5a29dSMark Lord writelfl(readl(serr), serr); 3689cae5a29dSMark Lord writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE); 3690c6fd2807SJeff Garzik 3691646a4da5SMark Lord /* unmask all non-transient EDMA error interrupts */ 3692cae5a29dSMark Lord writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK); 3693c6fd2807SJeff Garzik 3694c6fd2807SJeff Garzik VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n", 3695cae5a29dSMark Lord readl(port_mmio + EDMA_CFG), 3696cae5a29dSMark Lord readl(port_mmio + EDMA_ERR_IRQ_CAUSE), 3697cae5a29dSMark Lord readl(port_mmio + EDMA_ERR_IRQ_MASK)); 3698c6fd2807SJeff Garzik } 3699c6fd2807SJeff Garzik 3700616d4a98SMark Lord static unsigned int mv_in_pcix_mode(struct ata_host *host) 3701616d4a98SMark Lord { 3702616d4a98SMark Lord struct mv_host_priv *hpriv = host->private_data; 3703616d4a98SMark Lord void __iomem *mmio = hpriv->base; 3704616d4a98SMark Lord u32 reg; 3705616d4a98SMark Lord 37061f398472SMark Lord if (IS_SOC(hpriv) || !IS_PCIE(hpriv)) 3707616d4a98SMark Lord return 0; /* not PCI-X capable */ 3708cae5a29dSMark Lord reg = readl(mmio + MV_PCI_MODE); 3709616d4a98SMark Lord if ((reg & MV_PCI_MODE_MASK) == 0) 3710616d4a98SMark Lord return 0; /* conventional PCI mode */ 3711616d4a98SMark Lord return 1; /* chip is in PCI-X mode */ 3712616d4a98SMark Lord } 3713616d4a98SMark Lord 3714616d4a98SMark Lord static int mv_pci_cut_through_okay(struct ata_host *host) 3715616d4a98SMark Lord { 3716616d4a98SMark Lord struct mv_host_priv *hpriv = host->private_data; 3717616d4a98SMark Lord void __iomem *mmio = hpriv->base; 3718616d4a98SMark Lord u32 reg; 3719616d4a98SMark Lord 3720616d4a98SMark Lord if (!mv_in_pcix_mode(host)) { 3721cae5a29dSMark Lord reg = readl(mmio + MV_PCI_COMMAND); 3722cae5a29dSMark Lord if (reg & MV_PCI_COMMAND_MRDTRIG) 3723616d4a98SMark Lord return 0; /* not okay */ 3724616d4a98SMark Lord } 3725616d4a98SMark Lord return 1; /* okay */ 3726616d4a98SMark Lord } 3727616d4a98SMark Lord 372865ad7fefSMark Lord static void mv_60x1b2_errata_pci7(struct ata_host *host) 372965ad7fefSMark Lord { 373065ad7fefSMark Lord struct mv_host_priv *hpriv = host->private_data; 373165ad7fefSMark Lord void __iomem *mmio = hpriv->base; 373265ad7fefSMark Lord 373365ad7fefSMark Lord /* workaround for 60x1-B2 errata PCI#7 */ 373465ad7fefSMark Lord if (mv_in_pcix_mode(host)) { 3735cae5a29dSMark Lord u32 reg = readl(mmio + MV_PCI_COMMAND); 3736cae5a29dSMark Lord writelfl(reg & ~MV_PCI_COMMAND_MWRCOM, mmio + MV_PCI_COMMAND); 373765ad7fefSMark Lord } 373865ad7fefSMark Lord } 373965ad7fefSMark Lord 37404447d351STejun Heo static int mv_chip_id(struct ata_host *host, unsigned int board_idx) 3741c6fd2807SJeff Garzik { 37424447d351STejun Heo struct pci_dev *pdev = to_pci_dev(host->dev); 37434447d351STejun Heo struct mv_host_priv *hpriv = host->private_data; 3744c6fd2807SJeff Garzik u32 hp_flags = hpriv->hp_flags; 3745c6fd2807SJeff Garzik 3746c6fd2807SJeff Garzik switch (board_idx) { 3747c6fd2807SJeff Garzik case chip_5080: 3748c6fd2807SJeff Garzik hpriv->ops = &mv5xxx_ops; 3749ee9ccdf7SJeff Garzik hp_flags |= MV_HP_GEN_I; 3750c6fd2807SJeff Garzik 375144c10138SAuke Kok switch (pdev->revision) { 3752c6fd2807SJeff Garzik case 0x1: 3753c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB0; 3754c6fd2807SJeff Garzik break; 3755c6fd2807SJeff Garzik case 0x3: 3756c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB2; 3757c6fd2807SJeff Garzik break; 3758c6fd2807SJeff Garzik default: 3759a44fec1fSJoe Perches dev_warn(&pdev->dev, 3760c6fd2807SJeff Garzik "Applying 50XXB2 workarounds to unknown rev\n"); 3761c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB2; 3762c6fd2807SJeff Garzik break; 3763c6fd2807SJeff Garzik } 3764c6fd2807SJeff Garzik break; 3765c6fd2807SJeff Garzik 3766c6fd2807SJeff Garzik case chip_504x: 3767c6fd2807SJeff Garzik case chip_508x: 3768c6fd2807SJeff Garzik hpriv->ops = &mv5xxx_ops; 3769ee9ccdf7SJeff Garzik hp_flags |= MV_HP_GEN_I; 3770c6fd2807SJeff Garzik 377144c10138SAuke Kok switch (pdev->revision) { 3772c6fd2807SJeff Garzik case 0x0: 3773c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB0; 3774c6fd2807SJeff Garzik break; 3775c6fd2807SJeff Garzik case 0x3: 3776c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB2; 3777c6fd2807SJeff Garzik break; 3778c6fd2807SJeff Garzik default: 3779a44fec1fSJoe Perches dev_warn(&pdev->dev, 3780c6fd2807SJeff Garzik "Applying B2 workarounds to unknown rev\n"); 3781c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB2; 3782c6fd2807SJeff Garzik break; 3783c6fd2807SJeff Garzik } 3784c6fd2807SJeff Garzik break; 3785c6fd2807SJeff Garzik 3786c6fd2807SJeff Garzik case chip_604x: 3787c6fd2807SJeff Garzik case chip_608x: 3788c6fd2807SJeff Garzik hpriv->ops = &mv6xxx_ops; 3789ee9ccdf7SJeff Garzik hp_flags |= MV_HP_GEN_II; 3790c6fd2807SJeff Garzik 379144c10138SAuke Kok switch (pdev->revision) { 3792c6fd2807SJeff Garzik case 0x7: 379365ad7fefSMark Lord mv_60x1b2_errata_pci7(host); 3794c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1B2; 3795c6fd2807SJeff Garzik break; 3796c6fd2807SJeff Garzik case 0x9: 3797c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1C0; 3798c6fd2807SJeff Garzik break; 3799c6fd2807SJeff Garzik default: 3800a44fec1fSJoe Perches dev_warn(&pdev->dev, 3801c6fd2807SJeff Garzik "Applying B2 workarounds to unknown rev\n"); 3802c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1B2; 3803c6fd2807SJeff Garzik break; 3804c6fd2807SJeff Garzik } 3805c6fd2807SJeff Garzik break; 3806c6fd2807SJeff Garzik 3807c6fd2807SJeff Garzik case chip_7042: 3808616d4a98SMark Lord hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH; 3809306b30f7SMark Lord if (pdev->vendor == PCI_VENDOR_ID_TTI && 3810306b30f7SMark Lord (pdev->device == 0x2300 || pdev->device == 0x2310)) 3811306b30f7SMark Lord { 38124e520033SMark Lord /* 38134e520033SMark Lord * Highpoint RocketRAID PCIe 23xx series cards: 38144e520033SMark Lord * 38154e520033SMark Lord * Unconfigured drives are treated as "Legacy" 38164e520033SMark Lord * by the BIOS, and it overwrites sector 8 with 38174e520033SMark Lord * a "Lgcy" metadata block prior to Linux boot. 38184e520033SMark Lord * 38194e520033SMark Lord * Configured drives (RAID or JBOD) leave sector 8 38204e520033SMark Lord * alone, but instead overwrite a high numbered 38214e520033SMark Lord * sector for the RAID metadata. This sector can 38224e520033SMark Lord * be determined exactly, by truncating the physical 38234e520033SMark Lord * drive capacity to a nice even GB value. 38244e520033SMark Lord * 38254e520033SMark Lord * RAID metadata is at: (dev->n_sectors & ~0xfffff) 38264e520033SMark Lord * 38274e520033SMark Lord * Warn the user, lest they think we're just buggy. 38284e520033SMark Lord */ 38294e520033SMark Lord printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID" 38304e520033SMark Lord " BIOS CORRUPTS DATA on all attached drives," 38314e520033SMark Lord " regardless of if/how they are configured." 38324e520033SMark Lord " BEWARE!\n"); 38334e520033SMark Lord printk(KERN_WARNING DRV_NAME ": For data safety, do not" 38344e520033SMark Lord " use sectors 8-9 on \"Legacy\" drives," 38354e520033SMark Lord " and avoid the final two gigabytes on" 38364e520033SMark Lord " all RocketRAID BIOS initialized drives.\n"); 3837306b30f7SMark Lord } 38388e7decdbSMark Lord /* drop through */ 3839c6fd2807SJeff Garzik case chip_6042: 3840c6fd2807SJeff Garzik hpriv->ops = &mv6xxx_ops; 3841c6fd2807SJeff Garzik hp_flags |= MV_HP_GEN_IIE; 3842616d4a98SMark Lord if (board_idx == chip_6042 && mv_pci_cut_through_okay(host)) 3843616d4a98SMark Lord hp_flags |= MV_HP_CUT_THROUGH; 3844c6fd2807SJeff Garzik 384544c10138SAuke Kok switch (pdev->revision) { 38465cf73bfbSMark Lord case 0x2: /* Rev.B0: the first/only public release */ 3847c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1C0; 3848c6fd2807SJeff Garzik break; 3849c6fd2807SJeff Garzik default: 3850a44fec1fSJoe Perches dev_warn(&pdev->dev, 3851c6fd2807SJeff Garzik "Applying 60X1C0 workarounds to unknown rev\n"); 3852c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1C0; 3853c6fd2807SJeff Garzik break; 3854c6fd2807SJeff Garzik } 3855c6fd2807SJeff Garzik break; 3856f351b2d6SSaeed Bishara case chip_soc: 385729b7e43cSMartin Michlmayr if (soc_is_65n(hpriv)) 385829b7e43cSMartin Michlmayr hpriv->ops = &mv_soc_65n_ops; 385929b7e43cSMartin Michlmayr else 3860f351b2d6SSaeed Bishara hpriv->ops = &mv_soc_ops; 3861eb3a55a9SSaeed Bishara hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE | 3862eb3a55a9SSaeed Bishara MV_HP_ERRATA_60X1C0; 3863f351b2d6SSaeed Bishara break; 3864c6fd2807SJeff Garzik 3865c6fd2807SJeff Garzik default: 3866a44fec1fSJoe Perches dev_err(host->dev, "BUG: invalid board index %u\n", board_idx); 3867c6fd2807SJeff Garzik return 1; 3868c6fd2807SJeff Garzik } 3869c6fd2807SJeff Garzik 3870c6fd2807SJeff Garzik hpriv->hp_flags = hp_flags; 387102a121daSMark Lord if (hp_flags & MV_HP_PCIE) { 3872cae5a29dSMark Lord hpriv->irq_cause_offset = PCIE_IRQ_CAUSE; 3873cae5a29dSMark Lord hpriv->irq_mask_offset = PCIE_IRQ_MASK; 387402a121daSMark Lord hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS; 387502a121daSMark Lord } else { 3876cae5a29dSMark Lord hpriv->irq_cause_offset = PCI_IRQ_CAUSE; 3877cae5a29dSMark Lord hpriv->irq_mask_offset = PCI_IRQ_MASK; 387802a121daSMark Lord hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS; 387902a121daSMark Lord } 3880c6fd2807SJeff Garzik 3881c6fd2807SJeff Garzik return 0; 3882c6fd2807SJeff Garzik } 3883c6fd2807SJeff Garzik 3884c6fd2807SJeff Garzik /** 3885c6fd2807SJeff Garzik * mv_init_host - Perform some early initialization of the host. 38864447d351STejun Heo * @host: ATA host to initialize 3887c6fd2807SJeff Garzik * 3888c6fd2807SJeff Garzik * If possible, do an early global reset of the host. Then do 3889c6fd2807SJeff Garzik * our port init and clear/unmask all/relevant host interrupts. 3890c6fd2807SJeff Garzik * 3891c6fd2807SJeff Garzik * LOCKING: 3892c6fd2807SJeff Garzik * Inherited from caller. 3893c6fd2807SJeff Garzik */ 38941bfeff03SSaeed Bishara static int mv_init_host(struct ata_host *host) 3895c6fd2807SJeff Garzik { 3896c6fd2807SJeff Garzik int rc = 0, n_hc, port, hc; 38974447d351STejun Heo struct mv_host_priv *hpriv = host->private_data; 3898f351b2d6SSaeed Bishara void __iomem *mmio = hpriv->base; 3899c6fd2807SJeff Garzik 39001bfeff03SSaeed Bishara rc = mv_chip_id(host, hpriv->board_idx); 3901c6fd2807SJeff Garzik if (rc) 3902c6fd2807SJeff Garzik goto done; 3903c6fd2807SJeff Garzik 39041f398472SMark Lord if (IS_SOC(hpriv)) { 3905cae5a29dSMark Lord hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE; 3906cae5a29dSMark Lord hpriv->main_irq_mask_addr = mmio + SOC_HC_MAIN_IRQ_MASK; 39071f398472SMark Lord } else { 3908cae5a29dSMark Lord hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE; 3909cae5a29dSMark Lord hpriv->main_irq_mask_addr = mmio + PCI_HC_MAIN_IRQ_MASK; 3910f351b2d6SSaeed Bishara } 3911352fab70SMark Lord 39125d0fb2e7SThomas Reitmayr /* initialize shadow irq mask with register's value */ 39135d0fb2e7SThomas Reitmayr hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr); 39145d0fb2e7SThomas Reitmayr 3915352fab70SMark Lord /* global interrupt mask: 0 == mask everything */ 3916c4de573bSMark Lord mv_set_main_irq_mask(host, ~0, 0); 3917f351b2d6SSaeed Bishara 39184447d351STejun Heo n_hc = mv_get_hc_count(host->ports[0]->flags); 3919c6fd2807SJeff Garzik 39204447d351STejun Heo for (port = 0; port < host->n_ports; port++) 392129b7e43cSMartin Michlmayr if (hpriv->ops->read_preamp) 3922c6fd2807SJeff Garzik hpriv->ops->read_preamp(hpriv, port, mmio); 3923c6fd2807SJeff Garzik 3924c6fd2807SJeff Garzik rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc); 3925c6fd2807SJeff Garzik if (rc) 3926c6fd2807SJeff Garzik goto done; 3927c6fd2807SJeff Garzik 3928c6fd2807SJeff Garzik hpriv->ops->reset_flash(hpriv, mmio); 39297bb3c529SSaeed Bishara hpriv->ops->reset_bus(host, mmio); 3930c6fd2807SJeff Garzik hpriv->ops->enable_leds(hpriv, mmio); 3931c6fd2807SJeff Garzik 39324447d351STejun Heo for (port = 0; port < host->n_ports; port++) { 3933cbcdd875STejun Heo struct ata_port *ap = host->ports[port]; 3934c6fd2807SJeff Garzik void __iomem *port_mmio = mv_port_base(mmio, port); 3935cbcdd875STejun Heo 3936cbcdd875STejun Heo mv_port_init(&ap->ioaddr, port_mmio); 3937c6fd2807SJeff Garzik } 3938c6fd2807SJeff Garzik 3939c6fd2807SJeff Garzik for (hc = 0; hc < n_hc; hc++) { 3940c6fd2807SJeff Garzik void __iomem *hc_mmio = mv_hc_base(mmio, hc); 3941c6fd2807SJeff Garzik 3942c6fd2807SJeff Garzik VPRINTK("HC%i: HC config=0x%08x HC IRQ cause " 3943c6fd2807SJeff Garzik "(before clear)=0x%08x\n", hc, 3944cae5a29dSMark Lord readl(hc_mmio + HC_CFG), 3945cae5a29dSMark Lord readl(hc_mmio + HC_IRQ_CAUSE)); 3946c6fd2807SJeff Garzik 3947c6fd2807SJeff Garzik /* Clear any currently outstanding hc interrupt conditions */ 3948cae5a29dSMark Lord writelfl(0, hc_mmio + HC_IRQ_CAUSE); 3949c6fd2807SJeff Garzik } 3950c6fd2807SJeff Garzik 395144c65d16SMark Lord if (!IS_SOC(hpriv)) { 3952c6fd2807SJeff Garzik /* Clear any currently outstanding host interrupt conditions */ 3953cae5a29dSMark Lord writelfl(0, mmio + hpriv->irq_cause_offset); 3954c6fd2807SJeff Garzik 3955c6fd2807SJeff Garzik /* and unmask interrupt generation for host regs */ 3956cae5a29dSMark Lord writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_offset); 395744c65d16SMark Lord } 3958c6fd2807SJeff Garzik 395951de32d2SMark Lord /* 396051de32d2SMark Lord * enable only global host interrupts for now. 396151de32d2SMark Lord * The per-port interrupts get done later as ports are set up. 396251de32d2SMark Lord */ 3963c4de573bSMark Lord mv_set_main_irq_mask(host, 0, PCI_ERR); 39642b748a0aSMark Lord mv_set_irq_coalescing(host, irq_coalescing_io_count, 39652b748a0aSMark Lord irq_coalescing_usecs); 3966c6fd2807SJeff Garzik done: 3967c6fd2807SJeff Garzik return rc; 3968c6fd2807SJeff Garzik } 3969c6fd2807SJeff Garzik 3970fbf14e2fSByron Bradley static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev) 3971fbf14e2fSByron Bradley { 3972fbf14e2fSByron Bradley hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ, 3973fbf14e2fSByron Bradley MV_CRQB_Q_SZ, 0); 3974fbf14e2fSByron Bradley if (!hpriv->crqb_pool) 3975fbf14e2fSByron Bradley return -ENOMEM; 3976fbf14e2fSByron Bradley 3977fbf14e2fSByron Bradley hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ, 3978fbf14e2fSByron Bradley MV_CRPB_Q_SZ, 0); 3979fbf14e2fSByron Bradley if (!hpriv->crpb_pool) 3980fbf14e2fSByron Bradley return -ENOMEM; 3981fbf14e2fSByron Bradley 3982fbf14e2fSByron Bradley hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ, 3983fbf14e2fSByron Bradley MV_SG_TBL_SZ, 0); 3984fbf14e2fSByron Bradley if (!hpriv->sg_tbl_pool) 3985fbf14e2fSByron Bradley return -ENOMEM; 3986fbf14e2fSByron Bradley 3987fbf14e2fSByron Bradley return 0; 3988fbf14e2fSByron Bradley } 3989fbf14e2fSByron Bradley 399015a32632SLennert Buytenhek static void mv_conf_mbus_windows(struct mv_host_priv *hpriv, 399115a32632SLennert Buytenhek struct mbus_dram_target_info *dram) 399215a32632SLennert Buytenhek { 399315a32632SLennert Buytenhek int i; 399415a32632SLennert Buytenhek 399515a32632SLennert Buytenhek for (i = 0; i < 4; i++) { 399615a32632SLennert Buytenhek writel(0, hpriv->base + WINDOW_CTRL(i)); 399715a32632SLennert Buytenhek writel(0, hpriv->base + WINDOW_BASE(i)); 399815a32632SLennert Buytenhek } 399915a32632SLennert Buytenhek 400015a32632SLennert Buytenhek for (i = 0; i < dram->num_cs; i++) { 400115a32632SLennert Buytenhek struct mbus_dram_window *cs = dram->cs + i; 400215a32632SLennert Buytenhek 400315a32632SLennert Buytenhek writel(((cs->size - 1) & 0xffff0000) | 400415a32632SLennert Buytenhek (cs->mbus_attr << 8) | 400515a32632SLennert Buytenhek (dram->mbus_dram_target_id << 4) | 1, 400615a32632SLennert Buytenhek hpriv->base + WINDOW_CTRL(i)); 400715a32632SLennert Buytenhek writel(cs->base, hpriv->base + WINDOW_BASE(i)); 400815a32632SLennert Buytenhek } 400915a32632SLennert Buytenhek } 401015a32632SLennert Buytenhek 4011f351b2d6SSaeed Bishara /** 4012f351b2d6SSaeed Bishara * mv_platform_probe - handle a positive probe of an soc Marvell 4013f351b2d6SSaeed Bishara * host 4014f351b2d6SSaeed Bishara * @pdev: platform device found 4015f351b2d6SSaeed Bishara * 4016f351b2d6SSaeed Bishara * LOCKING: 4017f351b2d6SSaeed Bishara * Inherited from caller. 4018f351b2d6SSaeed Bishara */ 4019f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev) 4020f351b2d6SSaeed Bishara { 4021f351b2d6SSaeed Bishara const struct mv_sata_platform_data *mv_platform_data; 4022f351b2d6SSaeed Bishara const struct ata_port_info *ppi[] = 4023f351b2d6SSaeed Bishara { &mv_port_info[chip_soc], NULL }; 4024f351b2d6SSaeed Bishara struct ata_host *host; 4025f351b2d6SSaeed Bishara struct mv_host_priv *hpriv; 4026f351b2d6SSaeed Bishara struct resource *res; 4027f351b2d6SSaeed Bishara int n_ports, rc; 4028f351b2d6SSaeed Bishara 402906296a1eSJoe Perches ata_print_version_once(&pdev->dev, DRV_VERSION); 4030f351b2d6SSaeed Bishara 4031f351b2d6SSaeed Bishara /* 4032f351b2d6SSaeed Bishara * Simple resource validation .. 4033f351b2d6SSaeed Bishara */ 4034f351b2d6SSaeed Bishara if (unlikely(pdev->num_resources != 2)) { 4035f351b2d6SSaeed Bishara dev_err(&pdev->dev, "invalid number of resources\n"); 4036f351b2d6SSaeed Bishara return -EINVAL; 4037f351b2d6SSaeed Bishara } 4038f351b2d6SSaeed Bishara 4039f351b2d6SSaeed Bishara /* 4040f351b2d6SSaeed Bishara * Get the register base first 4041f351b2d6SSaeed Bishara */ 4042f351b2d6SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 4043f351b2d6SSaeed Bishara if (res == NULL) 4044f351b2d6SSaeed Bishara return -EINVAL; 4045f351b2d6SSaeed Bishara 4046f351b2d6SSaeed Bishara /* allocate host */ 4047f351b2d6SSaeed Bishara mv_platform_data = pdev->dev.platform_data; 4048f351b2d6SSaeed Bishara n_ports = mv_platform_data->n_ports; 4049f351b2d6SSaeed Bishara 4050f351b2d6SSaeed Bishara host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports); 4051f351b2d6SSaeed Bishara hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL); 4052f351b2d6SSaeed Bishara 4053f351b2d6SSaeed Bishara if (!host || !hpriv) 4054f351b2d6SSaeed Bishara return -ENOMEM; 4055f351b2d6SSaeed Bishara host->private_data = hpriv; 4056f351b2d6SSaeed Bishara hpriv->n_ports = n_ports; 40571bfeff03SSaeed Bishara hpriv->board_idx = chip_soc; 4058f351b2d6SSaeed Bishara 4059f351b2d6SSaeed Bishara host->iomap = NULL; 4060f1cb0ea1SSaeed Bishara hpriv->base = devm_ioremap(&pdev->dev, res->start, 4061041b5eacSJulia Lawall resource_size(res)); 4062cae5a29dSMark Lord hpriv->base -= SATAHC0_REG_BASE; 4063f351b2d6SSaeed Bishara 4064c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK) 4065c77a2f4eSSaeed Bishara hpriv->clk = clk_get(&pdev->dev, NULL); 4066c77a2f4eSSaeed Bishara if (IS_ERR(hpriv->clk)) 4067c77a2f4eSSaeed Bishara dev_notice(&pdev->dev, "cannot get clkdev\n"); 4068c77a2f4eSSaeed Bishara else 4069c77a2f4eSSaeed Bishara clk_enable(hpriv->clk); 4070c77a2f4eSSaeed Bishara #endif 4071c77a2f4eSSaeed Bishara 407215a32632SLennert Buytenhek /* 407315a32632SLennert Buytenhek * (Re-)program MBUS remapping windows if we are asked to. 407415a32632SLennert Buytenhek */ 407515a32632SLennert Buytenhek if (mv_platform_data->dram != NULL) 407615a32632SLennert Buytenhek mv_conf_mbus_windows(hpriv, mv_platform_data->dram); 407715a32632SLennert Buytenhek 4078fbf14e2fSByron Bradley rc = mv_create_dma_pools(hpriv, &pdev->dev); 4079fbf14e2fSByron Bradley if (rc) 4080c77a2f4eSSaeed Bishara goto err; 4081fbf14e2fSByron Bradley 4082f351b2d6SSaeed Bishara /* initialize adapter */ 40831bfeff03SSaeed Bishara rc = mv_init_host(host); 4084f351b2d6SSaeed Bishara if (rc) 4085c77a2f4eSSaeed Bishara goto err; 4086f351b2d6SSaeed Bishara 4087a44fec1fSJoe Perches dev_info(&pdev->dev, "slots %u ports %d\n", 4088a44fec1fSJoe Perches (unsigned)MV_MAX_Q_DEPTH, host->n_ports); 4089f351b2d6SSaeed Bishara 4090c00a4c9dSSergei Shtylyov rc = ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt, 4091f351b2d6SSaeed Bishara IRQF_SHARED, &mv6_sht); 4092c00a4c9dSSergei Shtylyov if (!rc) 4093c00a4c9dSSergei Shtylyov return 0; 4094c00a4c9dSSergei Shtylyov 4095c77a2f4eSSaeed Bishara err: 4096c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK) 4097c77a2f4eSSaeed Bishara if (!IS_ERR(hpriv->clk)) { 4098c77a2f4eSSaeed Bishara clk_disable(hpriv->clk); 4099c77a2f4eSSaeed Bishara clk_put(hpriv->clk); 4100c77a2f4eSSaeed Bishara } 4101c77a2f4eSSaeed Bishara #endif 4102c77a2f4eSSaeed Bishara 4103c77a2f4eSSaeed Bishara return rc; 4104f351b2d6SSaeed Bishara } 4105f351b2d6SSaeed Bishara 4106f351b2d6SSaeed Bishara /* 4107f351b2d6SSaeed Bishara * 4108f351b2d6SSaeed Bishara * mv_platform_remove - unplug a platform interface 4109f351b2d6SSaeed Bishara * @pdev: platform device 4110f351b2d6SSaeed Bishara * 4111f351b2d6SSaeed Bishara * A platform bus SATA device has been unplugged. Perform the needed 4112f351b2d6SSaeed Bishara * cleanup. Also called on module unload for any active devices. 4113f351b2d6SSaeed Bishara */ 4114f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev) 4115f351b2d6SSaeed Bishara { 4116*d8661921SSergei Shtylyov struct ata_host *host = platform_get_drvdata(pdev); 4117c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK) 4118c77a2f4eSSaeed Bishara struct mv_host_priv *hpriv = host->private_data; 4119c77a2f4eSSaeed Bishara #endif 4120f351b2d6SSaeed Bishara ata_host_detach(host); 4121c77a2f4eSSaeed Bishara 4122c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK) 4123c77a2f4eSSaeed Bishara if (!IS_ERR(hpriv->clk)) { 4124c77a2f4eSSaeed Bishara clk_disable(hpriv->clk); 4125c77a2f4eSSaeed Bishara clk_put(hpriv->clk); 4126c77a2f4eSSaeed Bishara } 4127c77a2f4eSSaeed Bishara #endif 4128f351b2d6SSaeed Bishara return 0; 4129f351b2d6SSaeed Bishara } 4130f351b2d6SSaeed Bishara 41316481f2b5SSaeed Bishara #ifdef CONFIG_PM 41326481f2b5SSaeed Bishara static int mv_platform_suspend(struct platform_device *pdev, pm_message_t state) 41336481f2b5SSaeed Bishara { 4134*d8661921SSergei Shtylyov struct ata_host *host = platform_get_drvdata(pdev); 41356481f2b5SSaeed Bishara if (host) 41366481f2b5SSaeed Bishara return ata_host_suspend(host, state); 41376481f2b5SSaeed Bishara else 41386481f2b5SSaeed Bishara return 0; 41396481f2b5SSaeed Bishara } 41406481f2b5SSaeed Bishara 41416481f2b5SSaeed Bishara static int mv_platform_resume(struct platform_device *pdev) 41426481f2b5SSaeed Bishara { 4143*d8661921SSergei Shtylyov struct ata_host *host = platform_get_drvdata(pdev); 41446481f2b5SSaeed Bishara int ret; 41456481f2b5SSaeed Bishara 41466481f2b5SSaeed Bishara if (host) { 41476481f2b5SSaeed Bishara struct mv_host_priv *hpriv = host->private_data; 41486481f2b5SSaeed Bishara const struct mv_sata_platform_data *mv_platform_data = \ 41496481f2b5SSaeed Bishara pdev->dev.platform_data; 41506481f2b5SSaeed Bishara /* 41516481f2b5SSaeed Bishara * (Re-)program MBUS remapping windows if we are asked to. 41526481f2b5SSaeed Bishara */ 41536481f2b5SSaeed Bishara if (mv_platform_data->dram != NULL) 41546481f2b5SSaeed Bishara mv_conf_mbus_windows(hpriv, mv_platform_data->dram); 41556481f2b5SSaeed Bishara 41566481f2b5SSaeed Bishara /* initialize adapter */ 41571bfeff03SSaeed Bishara ret = mv_init_host(host); 41586481f2b5SSaeed Bishara if (ret) { 41596481f2b5SSaeed Bishara printk(KERN_ERR DRV_NAME ": Error during HW init\n"); 41606481f2b5SSaeed Bishara return ret; 41616481f2b5SSaeed Bishara } 41626481f2b5SSaeed Bishara ata_host_resume(host); 41636481f2b5SSaeed Bishara } 41646481f2b5SSaeed Bishara 41656481f2b5SSaeed Bishara return 0; 41666481f2b5SSaeed Bishara } 41676481f2b5SSaeed Bishara #else 41686481f2b5SSaeed Bishara #define mv_platform_suspend NULL 41696481f2b5SSaeed Bishara #define mv_platform_resume NULL 41706481f2b5SSaeed Bishara #endif 41716481f2b5SSaeed Bishara 4172f351b2d6SSaeed Bishara static struct platform_driver mv_platform_driver = { 4173f351b2d6SSaeed Bishara .probe = mv_platform_probe, 4174f351b2d6SSaeed Bishara .remove = __devexit_p(mv_platform_remove), 41756481f2b5SSaeed Bishara .suspend = mv_platform_suspend, 41766481f2b5SSaeed Bishara .resume = mv_platform_resume, 4177f351b2d6SSaeed Bishara .driver = { 4178f351b2d6SSaeed Bishara .name = DRV_NAME, 4179f351b2d6SSaeed Bishara .owner = THIS_MODULE, 4180f351b2d6SSaeed Bishara }, 4181f351b2d6SSaeed Bishara }; 4182f351b2d6SSaeed Bishara 4183f351b2d6SSaeed Bishara 41847bb3c529SSaeed Bishara #ifdef CONFIG_PCI 4185f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev, 4186f351b2d6SSaeed Bishara const struct pci_device_id *ent); 4187b2dec48cSSaeed Bishara #ifdef CONFIG_PM 4188b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev); 4189b2dec48cSSaeed Bishara #endif 4190f351b2d6SSaeed Bishara 41917bb3c529SSaeed Bishara 41927bb3c529SSaeed Bishara static struct pci_driver mv_pci_driver = { 41937bb3c529SSaeed Bishara .name = DRV_NAME, 41947bb3c529SSaeed Bishara .id_table = mv_pci_tbl, 4195f351b2d6SSaeed Bishara .probe = mv_pci_init_one, 41967bb3c529SSaeed Bishara .remove = ata_pci_remove_one, 4197b2dec48cSSaeed Bishara #ifdef CONFIG_PM 4198b2dec48cSSaeed Bishara .suspend = ata_pci_device_suspend, 4199b2dec48cSSaeed Bishara .resume = mv_pci_device_resume, 4200b2dec48cSSaeed Bishara #endif 4201b2dec48cSSaeed Bishara 42027bb3c529SSaeed Bishara }; 42037bb3c529SSaeed Bishara 42047bb3c529SSaeed Bishara /* move to PCI layer or libata core? */ 42057bb3c529SSaeed Bishara static int pci_go_64(struct pci_dev *pdev) 42067bb3c529SSaeed Bishara { 42077bb3c529SSaeed Bishara int rc; 42087bb3c529SSaeed Bishara 42096a35528aSYang Hongyang if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) { 42106a35528aSYang Hongyang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 42117bb3c529SSaeed Bishara if (rc) { 4212284901a9SYang Hongyang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 42137bb3c529SSaeed Bishara if (rc) { 4214a44fec1fSJoe Perches dev_err(&pdev->dev, 42157bb3c529SSaeed Bishara "64-bit DMA enable failed\n"); 42167bb3c529SSaeed Bishara return rc; 42177bb3c529SSaeed Bishara } 42187bb3c529SSaeed Bishara } 42197bb3c529SSaeed Bishara } else { 4220284901a9SYang Hongyang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 42217bb3c529SSaeed Bishara if (rc) { 4222a44fec1fSJoe Perches dev_err(&pdev->dev, "32-bit DMA enable failed\n"); 42237bb3c529SSaeed Bishara return rc; 42247bb3c529SSaeed Bishara } 4225284901a9SYang Hongyang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 42267bb3c529SSaeed Bishara if (rc) { 4227a44fec1fSJoe Perches dev_err(&pdev->dev, 42287bb3c529SSaeed Bishara "32-bit consistent DMA enable failed\n"); 42297bb3c529SSaeed Bishara return rc; 42307bb3c529SSaeed Bishara } 42317bb3c529SSaeed Bishara } 42327bb3c529SSaeed Bishara 42337bb3c529SSaeed Bishara return rc; 42347bb3c529SSaeed Bishara } 42357bb3c529SSaeed Bishara 4236c6fd2807SJeff Garzik /** 4237c6fd2807SJeff Garzik * mv_print_info - Dump key info to kernel log for perusal. 42384447d351STejun Heo * @host: ATA host to print info about 4239c6fd2807SJeff Garzik * 4240c6fd2807SJeff Garzik * FIXME: complete this. 4241c6fd2807SJeff Garzik * 4242c6fd2807SJeff Garzik * LOCKING: 4243c6fd2807SJeff Garzik * Inherited from caller. 4244c6fd2807SJeff Garzik */ 42454447d351STejun Heo static void mv_print_info(struct ata_host *host) 4246c6fd2807SJeff Garzik { 42474447d351STejun Heo struct pci_dev *pdev = to_pci_dev(host->dev); 42484447d351STejun Heo struct mv_host_priv *hpriv = host->private_data; 424944c10138SAuke Kok u8 scc; 4250c1e4fe71SJeff Garzik const char *scc_s, *gen; 4251c6fd2807SJeff Garzik 4252c6fd2807SJeff Garzik /* Use this to determine the HW stepping of the chip so we know 4253c6fd2807SJeff Garzik * what errata to workaround 4254c6fd2807SJeff Garzik */ 4255c6fd2807SJeff Garzik pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc); 4256c6fd2807SJeff Garzik if (scc == 0) 4257c6fd2807SJeff Garzik scc_s = "SCSI"; 4258c6fd2807SJeff Garzik else if (scc == 0x01) 4259c6fd2807SJeff Garzik scc_s = "RAID"; 4260c6fd2807SJeff Garzik else 4261c1e4fe71SJeff Garzik scc_s = "?"; 4262c1e4fe71SJeff Garzik 4263c1e4fe71SJeff Garzik if (IS_GEN_I(hpriv)) 4264c1e4fe71SJeff Garzik gen = "I"; 4265c1e4fe71SJeff Garzik else if (IS_GEN_II(hpriv)) 4266c1e4fe71SJeff Garzik gen = "II"; 4267c1e4fe71SJeff Garzik else if (IS_GEN_IIE(hpriv)) 4268c1e4fe71SJeff Garzik gen = "IIE"; 4269c1e4fe71SJeff Garzik else 4270c1e4fe71SJeff Garzik gen = "?"; 4271c6fd2807SJeff Garzik 4272a44fec1fSJoe Perches dev_info(&pdev->dev, "Gen-%s %u slots %u ports %s mode IRQ via %s\n", 4273c1e4fe71SJeff Garzik gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports, 4274c6fd2807SJeff Garzik scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx"); 4275c6fd2807SJeff Garzik } 4276c6fd2807SJeff Garzik 4277c6fd2807SJeff Garzik /** 4278f351b2d6SSaeed Bishara * mv_pci_init_one - handle a positive probe of a PCI Marvell host 4279c6fd2807SJeff Garzik * @pdev: PCI device found 4280c6fd2807SJeff Garzik * @ent: PCI device ID entry for the matched host 4281c6fd2807SJeff Garzik * 4282c6fd2807SJeff Garzik * LOCKING: 4283c6fd2807SJeff Garzik * Inherited from caller. 4284c6fd2807SJeff Garzik */ 4285f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev, 4286f351b2d6SSaeed Bishara const struct pci_device_id *ent) 4287c6fd2807SJeff Garzik { 4288c6fd2807SJeff Garzik unsigned int board_idx = (unsigned int)ent->driver_data; 42894447d351STejun Heo const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL }; 42904447d351STejun Heo struct ata_host *host; 42914447d351STejun Heo struct mv_host_priv *hpriv; 4292c4bc7d73SSaeed Bishara int n_ports, port, rc; 4293c6fd2807SJeff Garzik 429406296a1eSJoe Perches ata_print_version_once(&pdev->dev, DRV_VERSION); 4295c6fd2807SJeff Garzik 42964447d351STejun Heo /* allocate host */ 42974447d351STejun Heo n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC; 42984447d351STejun Heo 42994447d351STejun Heo host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports); 43004447d351STejun Heo hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL); 43014447d351STejun Heo if (!host || !hpriv) 43024447d351STejun Heo return -ENOMEM; 43034447d351STejun Heo host->private_data = hpriv; 4304f351b2d6SSaeed Bishara hpriv->n_ports = n_ports; 43051bfeff03SSaeed Bishara hpriv->board_idx = board_idx; 43064447d351STejun Heo 43074447d351STejun Heo /* acquire resources */ 430824dc5f33STejun Heo rc = pcim_enable_device(pdev); 430924dc5f33STejun Heo if (rc) 4310c6fd2807SJeff Garzik return rc; 4311c6fd2807SJeff Garzik 43120d5ff566STejun Heo rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME); 43130d5ff566STejun Heo if (rc == -EBUSY) 431424dc5f33STejun Heo pcim_pin_device(pdev); 43150d5ff566STejun Heo if (rc) 431624dc5f33STejun Heo return rc; 43174447d351STejun Heo host->iomap = pcim_iomap_table(pdev); 4318f351b2d6SSaeed Bishara hpriv->base = host->iomap[MV_PRIMARY_BAR]; 4319c6fd2807SJeff Garzik 4320d88184fbSJeff Garzik rc = pci_go_64(pdev); 4321d88184fbSJeff Garzik if (rc) 4322d88184fbSJeff Garzik return rc; 4323d88184fbSJeff Garzik 4324da2fa9baSMark Lord rc = mv_create_dma_pools(hpriv, &pdev->dev); 4325da2fa9baSMark Lord if (rc) 4326da2fa9baSMark Lord return rc; 4327da2fa9baSMark Lord 4328c4bc7d73SSaeed Bishara for (port = 0; port < host->n_ports; port++) { 4329c4bc7d73SSaeed Bishara struct ata_port *ap = host->ports[port]; 4330c4bc7d73SSaeed Bishara void __iomem *port_mmio = mv_port_base(hpriv->base, port); 4331c4bc7d73SSaeed Bishara unsigned int offset = port_mmio - hpriv->base; 4332c4bc7d73SSaeed Bishara 4333c4bc7d73SSaeed Bishara ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio"); 4334c4bc7d73SSaeed Bishara ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port"); 4335c4bc7d73SSaeed Bishara } 4336c4bc7d73SSaeed Bishara 4337c6fd2807SJeff Garzik /* initialize adapter */ 43381bfeff03SSaeed Bishara rc = mv_init_host(host); 433924dc5f33STejun Heo if (rc) 434024dc5f33STejun Heo return rc; 4341c6fd2807SJeff Garzik 43426d3c30efSMark Lord /* Enable message-switched interrupts, if requested */ 43436d3c30efSMark Lord if (msi && pci_enable_msi(pdev) == 0) 43446d3c30efSMark Lord hpriv->hp_flags |= MV_HP_FLAG_MSI; 4345c6fd2807SJeff Garzik 4346c6fd2807SJeff Garzik mv_dump_pci_cfg(pdev, 0x68); 43474447d351STejun Heo mv_print_info(host); 4348c6fd2807SJeff Garzik 43494447d351STejun Heo pci_set_master(pdev); 4350ea8b4db9SJeff Garzik pci_try_set_mwi(pdev); 43514447d351STejun Heo return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED, 4352c5d3e45aSJeff Garzik IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht); 4353c6fd2807SJeff Garzik } 4354b2dec48cSSaeed Bishara 4355b2dec48cSSaeed Bishara #ifdef CONFIG_PM 4356b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev) 4357b2dec48cSSaeed Bishara { 4358*d8661921SSergei Shtylyov struct ata_host *host = pci_get_drvdata(pdev); 4359b2dec48cSSaeed Bishara int rc; 4360b2dec48cSSaeed Bishara 4361b2dec48cSSaeed Bishara rc = ata_pci_device_do_resume(pdev); 4362b2dec48cSSaeed Bishara if (rc) 4363b2dec48cSSaeed Bishara return rc; 4364b2dec48cSSaeed Bishara 4365b2dec48cSSaeed Bishara /* initialize adapter */ 4366b2dec48cSSaeed Bishara rc = mv_init_host(host); 4367b2dec48cSSaeed Bishara if (rc) 4368b2dec48cSSaeed Bishara return rc; 4369b2dec48cSSaeed Bishara 4370b2dec48cSSaeed Bishara ata_host_resume(host); 4371b2dec48cSSaeed Bishara 4372b2dec48cSSaeed Bishara return 0; 4373b2dec48cSSaeed Bishara } 4374b2dec48cSSaeed Bishara #endif 43757bb3c529SSaeed Bishara #endif 4376c6fd2807SJeff Garzik 4377f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev); 4378f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev); 4379f351b2d6SSaeed Bishara 4380c6fd2807SJeff Garzik static int __init mv_init(void) 4381c6fd2807SJeff Garzik { 43827bb3c529SSaeed Bishara int rc = -ENODEV; 43837bb3c529SSaeed Bishara #ifdef CONFIG_PCI 43847bb3c529SSaeed Bishara rc = pci_register_driver(&mv_pci_driver); 4385f351b2d6SSaeed Bishara if (rc < 0) 4386f351b2d6SSaeed Bishara return rc; 4387f351b2d6SSaeed Bishara #endif 4388f351b2d6SSaeed Bishara rc = platform_driver_register(&mv_platform_driver); 4389f351b2d6SSaeed Bishara 4390f351b2d6SSaeed Bishara #ifdef CONFIG_PCI 4391f351b2d6SSaeed Bishara if (rc < 0) 4392f351b2d6SSaeed Bishara pci_unregister_driver(&mv_pci_driver); 43937bb3c529SSaeed Bishara #endif 43947bb3c529SSaeed Bishara return rc; 4395c6fd2807SJeff Garzik } 4396c6fd2807SJeff Garzik 4397c6fd2807SJeff Garzik static void __exit mv_exit(void) 4398c6fd2807SJeff Garzik { 43997bb3c529SSaeed Bishara #ifdef CONFIG_PCI 4400c6fd2807SJeff Garzik pci_unregister_driver(&mv_pci_driver); 44017bb3c529SSaeed Bishara #endif 4402f351b2d6SSaeed Bishara platform_driver_unregister(&mv_platform_driver); 4403c6fd2807SJeff Garzik } 4404c6fd2807SJeff Garzik 4405c6fd2807SJeff Garzik MODULE_AUTHOR("Brett Russ"); 4406c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers"); 4407c6fd2807SJeff Garzik MODULE_LICENSE("GPL"); 4408c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, mv_pci_tbl); 4409c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION); 441017c5aab5SMark Lord MODULE_ALIAS("platform:" DRV_NAME); 4411c6fd2807SJeff Garzik 4412c6fd2807SJeff Garzik module_init(mv_init); 4413c6fd2807SJeff Garzik module_exit(mv_exit); 4414