1ffc664e8SDave Liu /* 224b44844SDave Liu * Copyright 2003-2004 Red Hat, Inc. All rights reserved. 324b44844SDave Liu * Copyright 2003-2004 Jeff Garzik 4ffc664e8SDave Liu * Copyright (C) 2008 Freescale Semiconductor, Inc. 5ffc664e8SDave Liu * Dave Liu <daveliu@freescale.com> 6ffc664e8SDave Liu * port from libata of linux kernel 7ffc664e8SDave Liu * 8*1a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 9ffc664e8SDave Liu */ 10ffc664e8SDave Liu 11ffc664e8SDave Liu #ifndef __LIBATA_H__ 12ffc664e8SDave Liu #define __LIBATA_H__ 13ffc664e8SDave Liu 14ffc664e8SDave Liu #include <common.h> 15ffc664e8SDave Liu 1624b44844SDave Liu enum { 1724b44844SDave Liu /* various global constants */ 1824b44844SDave Liu ATA_MAX_DEVICES = 2, /* per bus/port */ 1924b44844SDave Liu ATA_MAX_PRD = 256, /* we could make these 256/256 */ 2024b44844SDave Liu ATA_SECT_SIZE = 512, 2124b44844SDave Liu ATA_MAX_SECTORS_128 = 128, 2224b44844SDave Liu ATA_MAX_SECTORS = 256, 2324b44844SDave Liu ATA_MAX_SECTORS_LBA48 = 65535, 2424b44844SDave Liu ATA_MAX_SECTORS_TAPE = 65535, 2524b44844SDave Liu 2624b44844SDave Liu ATA_ID_WORDS = 256, 2724b44844SDave Liu ATA_ID_SERNO = 10, 2824b44844SDave Liu ATA_ID_FW_REV = 23, 2924b44844SDave Liu ATA_ID_PROD = 27, 3024b44844SDave Liu ATA_ID_OLD_PIO_MODES = 51, 3124b44844SDave Liu ATA_ID_FIELD_VALID = 53, 3224b44844SDave Liu ATA_ID_LBA_SECTORS = 60, 3324b44844SDave Liu ATA_ID_MWDMA_MODES = 63, 3424b44844SDave Liu ATA_ID_PIO_MODES = 64, 3524b44844SDave Liu ATA_ID_EIDE_DMA_MIN = 65, 3624b44844SDave Liu ATA_ID_EIDE_PIO = 67, 3724b44844SDave Liu ATA_ID_EIDE_PIO_IORDY = 68, 3824b44844SDave Liu ATA_ID_PIO4 = (1 << 1), 3924b44844SDave Liu ATA_ID_QUEUE_DEPTH = 75, 4024b44844SDave Liu ATA_ID_SATA_CAP = 76, 4124b44844SDave Liu ATA_ID_SATA_FEATURES = 78, 4224b44844SDave Liu ATA_ID_SATA_FEATURES_EN = 79, 4324b44844SDave Liu ATA_ID_MAJOR_VER = 80, 4424b44844SDave Liu ATA_ID_MINOR_VER = 81, 4524b44844SDave Liu ATA_ID_UDMA_MODES = 88, 4624b44844SDave Liu ATA_ID_LBA48_SECTORS = 100, 4724b44844SDave Liu 4824b44844SDave Liu ATA_ID_SERNO_LEN = 20, 4924b44844SDave Liu ATA_ID_FW_REV_LEN = 8, 5024b44844SDave Liu ATA_ID_PROD_LEN = 40, 5124b44844SDave Liu 5224b44844SDave Liu ATA_PCI_CTL_OFS = 2, 5324b44844SDave Liu 5424b44844SDave Liu ATA_PIO0 = (1 << 0), 5524b44844SDave Liu ATA_PIO1 = ATA_PIO0 | (1 << 1), 5624b44844SDave Liu ATA_PIO2 = ATA_PIO1 | (1 << 2), 5724b44844SDave Liu ATA_PIO3 = ATA_PIO2 | (1 << 3), 5824b44844SDave Liu ATA_PIO4 = ATA_PIO3 | (1 << 4), 5924b44844SDave Liu ATA_PIO5 = ATA_PIO4 | (1 << 5), 6024b44844SDave Liu ATA_PIO6 = ATA_PIO5 | (1 << 6), 6124b44844SDave Liu 6224b44844SDave Liu ATA_SWDMA0 = (1 << 0), 6324b44844SDave Liu ATA_SWDMA1 = ATA_SWDMA0 | (1 << 1), 6424b44844SDave Liu ATA_SWDMA2 = ATA_SWDMA1 | (1 << 2), 6524b44844SDave Liu 6624b44844SDave Liu ATA_SWDMA2_ONLY = (1 << 2), 6724b44844SDave Liu 6824b44844SDave Liu ATA_MWDMA0 = (1 << 0), 6924b44844SDave Liu ATA_MWDMA1 = ATA_MWDMA0 | (1 << 1), 7024b44844SDave Liu ATA_MWDMA2 = ATA_MWDMA1 | (1 << 2), 7124b44844SDave Liu 7224b44844SDave Liu ATA_MWDMA12_ONLY = (1 << 1) | (1 << 2), 7324b44844SDave Liu ATA_MWDMA2_ONLY = (1 << 2), 7424b44844SDave Liu 7524b44844SDave Liu ATA_UDMA0 = (1 << 0), 7624b44844SDave Liu ATA_UDMA1 = ATA_UDMA0 | (1 << 1), 7724b44844SDave Liu ATA_UDMA2 = ATA_UDMA1 | (1 << 2), 7824b44844SDave Liu ATA_UDMA3 = ATA_UDMA2 | (1 << 3), 7924b44844SDave Liu ATA_UDMA4 = ATA_UDMA3 | (1 << 4), 8024b44844SDave Liu ATA_UDMA5 = ATA_UDMA4 | (1 << 5), 8124b44844SDave Liu ATA_UDMA6 = ATA_UDMA5 | (1 << 6), 8224b44844SDave Liu ATA_UDMA7 = ATA_UDMA6 | (1 << 7), 8324b44844SDave Liu /* ATA_UDMA7 is just for completeness... doesn't exist (yet?). */ 8424b44844SDave Liu 8524b44844SDave Liu ATA_UDMA_MASK_40C = ATA_UDMA2, /* udma0-2 */ 8624b44844SDave Liu 8724b44844SDave Liu /* DMA-related */ 8824b44844SDave Liu ATA_PRD_SZ = 8, 8924b44844SDave Liu ATA_PRD_TBL_SZ = (ATA_MAX_PRD * ATA_PRD_SZ), 9024b44844SDave Liu ATA_PRD_EOT = (1 << 31), /* end-of-table flag */ 9124b44844SDave Liu 9224b44844SDave Liu ATA_DMA_TABLE_OFS = 4, 9324b44844SDave Liu ATA_DMA_STATUS = 2, 9424b44844SDave Liu ATA_DMA_CMD = 0, 9524b44844SDave Liu ATA_DMA_WR = (1 << 3), 9624b44844SDave Liu ATA_DMA_START = (1 << 0), 9724b44844SDave Liu ATA_DMA_INTR = (1 << 2), 9824b44844SDave Liu ATA_DMA_ERR = (1 << 1), 9924b44844SDave Liu ATA_DMA_ACTIVE = (1 << 0), 10024b44844SDave Liu 10124b44844SDave Liu /* bits in ATA command block registers */ 10224b44844SDave Liu ATA_HOB = (1 << 7), /* LBA48 selector */ 10324b44844SDave Liu ATA_NIEN = (1 << 1), /* disable-irq flag */ 10424b44844SDave Liu ATA_LBA = (1 << 6), /* LBA28 selector */ 10524b44844SDave Liu ATA_DEV1 = (1 << 4), /* Select Device 1 (slave) */ 10624b44844SDave Liu ATA_DEVICE_OBS = (1 << 7) | (1 << 5), /* obs bits in dev reg */ 10724b44844SDave Liu ATA_DEVCTL_OBS = (1 << 3), /* obsolete bit in devctl reg */ 10824b44844SDave Liu ATA_BUSY = (1 << 7), /* BSY status bit */ 10924b44844SDave Liu ATA_DRDY = (1 << 6), /* device ready */ 11024b44844SDave Liu ATA_DF = (1 << 5), /* device fault */ 11124b44844SDave Liu ATA_DRQ = (1 << 3), /* data request i/o */ 11224b44844SDave Liu ATA_ERR = (1 << 0), /* have an error */ 11324b44844SDave Liu ATA_SRST = (1 << 2), /* software reset */ 11424b44844SDave Liu ATA_ICRC = (1 << 7), /* interface CRC error */ 11524b44844SDave Liu ATA_UNC = (1 << 6), /* uncorrectable media error */ 11624b44844SDave Liu ATA_IDNF = (1 << 4), /* ID not found */ 11724b44844SDave Liu ATA_ABORTED = (1 << 2), /* command aborted */ 11824b44844SDave Liu 11924b44844SDave Liu /* ATA command block registers */ 12024b44844SDave Liu ATA_REG_DATA = 0x00, 12124b44844SDave Liu ATA_REG_ERR = 0x01, 12224b44844SDave Liu ATA_REG_NSECT = 0x02, 12324b44844SDave Liu ATA_REG_LBAL = 0x03, 12424b44844SDave Liu ATA_REG_LBAM = 0x04, 12524b44844SDave Liu ATA_REG_LBAH = 0x05, 12624b44844SDave Liu ATA_REG_DEVICE = 0x06, 12724b44844SDave Liu ATA_REG_STATUS = 0x07, 12824b44844SDave Liu 12924b44844SDave Liu ATA_REG_FEATURE = ATA_REG_ERR, /* and their aliases */ 13024b44844SDave Liu ATA_REG_CMD = ATA_REG_STATUS, 13124b44844SDave Liu ATA_REG_BYTEL = ATA_REG_LBAM, 13224b44844SDave Liu ATA_REG_BYTEH = ATA_REG_LBAH, 13324b44844SDave Liu ATA_REG_DEVSEL = ATA_REG_DEVICE, 13424b44844SDave Liu ATA_REG_IRQ = ATA_REG_NSECT, 13524b44844SDave Liu 13624b44844SDave Liu /* ATA device commands */ 137ffc664e8SDave Liu ATA_CMD_DEV_RESET = 0x08, /* ATAPI device reset */ 13824b44844SDave Liu ATA_CMD_CHK_POWER = 0xE5, /* check power mode */ 13924b44844SDave Liu ATA_CMD_STANDBY = 0xE2, /* place in standby power mode */ 14024b44844SDave Liu ATA_CMD_IDLE = 0xE3, /* place in idle power mode */ 14124b44844SDave Liu ATA_CMD_EDD = 0x90, /* execute device diagnostic */ 14224b44844SDave Liu ATA_CMD_FLUSH = 0xE7, 14324b44844SDave Liu ATA_CMD_FLUSH_EXT = 0xEA, 144ffc664e8SDave Liu ATA_CMD_ID_ATA = 0xEC, 145ffc664e8SDave Liu ATA_CMD_ID_ATAPI = 0xA1, 14624b44844SDave Liu ATA_CMD_READ = 0xC8, 14724b44844SDave Liu ATA_CMD_READ_EXT = 0x25, 14824b44844SDave Liu ATA_CMD_WRITE = 0xCA, 14924b44844SDave Liu ATA_CMD_WRITE_EXT = 0x35, 15024b44844SDave Liu ATA_CMD_WRITE_FUA_EXT = 0x3D, 15124b44844SDave Liu ATA_CMD_FPDMA_READ = 0x60, 15224b44844SDave Liu ATA_CMD_FPDMA_WRITE = 0x61, 153ffc664e8SDave Liu ATA_CMD_PIO_READ = 0x20, 154ffc664e8SDave Liu ATA_CMD_PIO_READ_EXT = 0x24, 155ffc664e8SDave Liu ATA_CMD_PIO_WRITE = 0x30, 156ffc664e8SDave Liu ATA_CMD_PIO_WRITE_EXT = 0x34, 15724b44844SDave Liu ATA_CMD_READ_MULTI = 0xC4, 15824b44844SDave Liu ATA_CMD_READ_MULTI_EXT = 0x29, 15924b44844SDave Liu ATA_CMD_WRITE_MULTI = 0xC5, 16024b44844SDave Liu ATA_CMD_WRITE_MULTI_EXT = 0x39, 16124b44844SDave Liu ATA_CMD_WRITE_MULTI_FUA_EXT = 0xCE, 162ffc664e8SDave Liu ATA_CMD_SET_FEATURES = 0xEF, 16324b44844SDave Liu ATA_CMD_SET_MULTI = 0xC6, 16424b44844SDave Liu ATA_CMD_PACKET = 0xA0, 16524b44844SDave Liu ATA_CMD_VERIFY = 0x40, 16624b44844SDave Liu ATA_CMD_VERIFY_EXT = 0x42, 16724b44844SDave Liu ATA_CMD_STANDBYNOW1 = 0xE0, 16824b44844SDave Liu ATA_CMD_IDLEIMMEDIATE = 0xE1, 16924b44844SDave Liu ATA_CMD_SLEEP = 0xE6, 17024b44844SDave Liu ATA_CMD_INIT_DEV_PARAMS = 0x91, 17124b44844SDave Liu ATA_CMD_READ_NATIVE_MAX = 0xF8, 17224b44844SDave Liu ATA_CMD_READ_NATIVE_MAX_EXT = 0x27, 17324b44844SDave Liu ATA_CMD_SET_MAX = 0xF9, 17424b44844SDave Liu ATA_CMD_SET_MAX_EXT = 0x37, 17524b44844SDave Liu ATA_CMD_READ_LOG_EXT = 0x2f, 17624b44844SDave Liu ATA_CMD_PMP_READ = 0xE4, 17724b44844SDave Liu ATA_CMD_PMP_WRITE = 0xE8, 17824b44844SDave Liu ATA_CMD_CONF_OVERLAY = 0xB1, 17924b44844SDave Liu ATA_CMD_SEC_FREEZE_LOCK = 0xF5, 180ffc664e8SDave Liu 18124b44844SDave Liu /* READ_LOG_EXT pages */ 18224b44844SDave Liu ATA_LOG_SATA_NCQ = 0x10, 183ffc664e8SDave Liu 18424b44844SDave Liu /* READ/WRITE LONG (obsolete) */ 18524b44844SDave Liu ATA_CMD_READ_LONG = 0x22, 18624b44844SDave Liu ATA_CMD_READ_LONG_ONCE = 0x23, 18724b44844SDave Liu ATA_CMD_WRITE_LONG = 0x32, 18824b44844SDave Liu ATA_CMD_WRITE_LONG_ONCE = 0x33, 18924b44844SDave Liu 190ffc664e8SDave Liu /* SETFEATURES stuff */ 191ffc664e8SDave Liu SETFEATURES_XFER = 0x03, 192ffc664e8SDave Liu XFER_UDMA_7 = 0x47, 193ffc664e8SDave Liu XFER_UDMA_6 = 0x46, 194ffc664e8SDave Liu XFER_UDMA_5 = 0x45, 195ffc664e8SDave Liu XFER_UDMA_4 = 0x44, 196ffc664e8SDave Liu XFER_UDMA_3 = 0x43, 197ffc664e8SDave Liu XFER_UDMA_2 = 0x42, 198ffc664e8SDave Liu XFER_UDMA_1 = 0x41, 199ffc664e8SDave Liu XFER_UDMA_0 = 0x40, 200ffc664e8SDave Liu XFER_MW_DMA_4 = 0x24, /* CFA only */ 201ffc664e8SDave Liu XFER_MW_DMA_3 = 0x23, /* CFA only */ 202ffc664e8SDave Liu XFER_MW_DMA_2 = 0x22, 203ffc664e8SDave Liu XFER_MW_DMA_1 = 0x21, 204ffc664e8SDave Liu XFER_MW_DMA_0 = 0x20, 20524b44844SDave Liu XFER_SW_DMA_2 = 0x12, 20624b44844SDave Liu XFER_SW_DMA_1 = 0x11, 20724b44844SDave Liu XFER_SW_DMA_0 = 0x10, 208ffc664e8SDave Liu XFER_PIO_6 = 0x0E, /* CFA only */ 209ffc664e8SDave Liu XFER_PIO_5 = 0x0D, /* CFA only */ 210ffc664e8SDave Liu XFER_PIO_4 = 0x0C, 211ffc664e8SDave Liu XFER_PIO_3 = 0x0B, 212ffc664e8SDave Liu XFER_PIO_2 = 0x0A, 213ffc664e8SDave Liu XFER_PIO_1 = 0x09, 214ffc664e8SDave Liu XFER_PIO_0 = 0x08, 215ffc664e8SDave Liu XFER_PIO_SLOW = 0x00, 216ffc664e8SDave Liu 217ffc664e8SDave Liu SETFEATURES_WC_ON = 0x02, /* Enable write cache */ 218ffc664e8SDave Liu SETFEATURES_WC_OFF = 0x82, /* Disable write cache */ 219ffc664e8SDave Liu 220ffc664e8SDave Liu SETFEATURES_SPINUP = 0x07, /* Spin-up drive */ 22124b44844SDave Liu 22224b44844SDave Liu SETFEATURES_SATA_ENABLE = 0x10, /* Enable use of SATA feature */ 22324b44844SDave Liu SETFEATURES_SATA_DISABLE = 0x90, /* Disable use of SATA feature */ 22424b44844SDave Liu 22524b44844SDave Liu /* SETFEATURE Sector counts for SATA features */ 22624b44844SDave Liu SATA_AN = 0x05, /* Asynchronous Notification */ 22724b44844SDave Liu SATA_DIPM = 0x03, /* Device Initiated Power Management */ 22824b44844SDave Liu 22924b44844SDave Liu /* feature values for SET_MAX */ 23024b44844SDave Liu ATA_SET_MAX_ADDR = 0x00, 23124b44844SDave Liu ATA_SET_MAX_PASSWD = 0x01, 23224b44844SDave Liu ATA_SET_MAX_LOCK = 0x02, 23324b44844SDave Liu ATA_SET_MAX_UNLOCK = 0x03, 23424b44844SDave Liu ATA_SET_MAX_FREEZE_LOCK = 0x04, 23524b44844SDave Liu 23624b44844SDave Liu /* feature values for DEVICE CONFIGURATION OVERLAY */ 23724b44844SDave Liu ATA_DCO_RESTORE = 0xC0, 23824b44844SDave Liu ATA_DCO_FREEZE_LOCK = 0xC1, 23924b44844SDave Liu ATA_DCO_IDENTIFY = 0xC2, 24024b44844SDave Liu ATA_DCO_SET = 0xC3, 24124b44844SDave Liu 24224b44844SDave Liu /* ATAPI stuff */ 24324b44844SDave Liu ATAPI_PKT_DMA = (1 << 0), 24424b44844SDave Liu ATAPI_DMADIR = (1 << 2), /* ATAPI data dir: 24524b44844SDave Liu 0=to device, 1=to host */ 24624b44844SDave Liu ATAPI_CDB_LEN = 16, 24724b44844SDave Liu 24824b44844SDave Liu /* PMP stuff */ 24924b44844SDave Liu SATA_PMP_MAX_PORTS = 15, 25024b44844SDave Liu SATA_PMP_CTRL_PORT = 15, 25124b44844SDave Liu 25224b44844SDave Liu SATA_PMP_GSCR_DWORDS = 128, 25324b44844SDave Liu SATA_PMP_GSCR_PROD_ID = 0, 25424b44844SDave Liu SATA_PMP_GSCR_REV = 1, 25524b44844SDave Liu SATA_PMP_GSCR_PORT_INFO = 2, 25624b44844SDave Liu SATA_PMP_GSCR_ERROR = 32, 25724b44844SDave Liu SATA_PMP_GSCR_ERROR_EN = 33, 25824b44844SDave Liu SATA_PMP_GSCR_FEAT = 64, 25924b44844SDave Liu SATA_PMP_GSCR_FEAT_EN = 96, 26024b44844SDave Liu 26124b44844SDave Liu SATA_PMP_PSCR_STATUS = 0, 26224b44844SDave Liu SATA_PMP_PSCR_ERROR = 1, 26324b44844SDave Liu SATA_PMP_PSCR_CONTROL = 2, 26424b44844SDave Liu 26524b44844SDave Liu SATA_PMP_FEAT_BIST = (1 << 0), 26624b44844SDave Liu SATA_PMP_FEAT_PMREQ = (1 << 1), 26724b44844SDave Liu SATA_PMP_FEAT_DYNSSC = (1 << 2), 26824b44844SDave Liu SATA_PMP_FEAT_NOTIFY = (1 << 3), 26924b44844SDave Liu 27024b44844SDave Liu /* cable types */ 27124b44844SDave Liu ATA_CBL_NONE = 0, 27224b44844SDave Liu ATA_CBL_PATA40 = 1, 27324b44844SDave Liu ATA_CBL_PATA80 = 2, 27424b44844SDave Liu ATA_CBL_PATA40_SHORT = 3, /* 40 wire cable to high UDMA spec */ 27524b44844SDave Liu ATA_CBL_PATA_UNK = 4, /* don't know, maybe 80c? */ 27624b44844SDave Liu ATA_CBL_PATA_IGN = 5, /* don't know, ignore cable handling */ 27724b44844SDave Liu ATA_CBL_SATA = 6, 27824b44844SDave Liu 27924b44844SDave Liu /* SATA Status and Control Registers */ 28024b44844SDave Liu SCR_STATUS = 0, 28124b44844SDave Liu SCR_ERROR = 1, 28224b44844SDave Liu SCR_CONTROL = 2, 28324b44844SDave Liu SCR_ACTIVE = 3, 28424b44844SDave Liu SCR_NOTIFICATION = 4, 28524b44844SDave Liu 28624b44844SDave Liu /* SError bits */ 28724b44844SDave Liu SERR_DATA_RECOVERED = (1 << 0), /* recovered data error */ 28824b44844SDave Liu SERR_COMM_RECOVERED = (1 << 1), /* recovered comm failure */ 28924b44844SDave Liu SERR_DATA = (1 << 8), /* unrecovered data error */ 29024b44844SDave Liu SERR_PERSISTENT = (1 << 9), /* persistent data/comm error */ 29124b44844SDave Liu SERR_PROTOCOL = (1 << 10), /* protocol violation */ 29224b44844SDave Liu SERR_INTERNAL = (1 << 11), /* host internal error */ 29324b44844SDave Liu SERR_PHYRDY_CHG = (1 << 16), /* PHY RDY changed */ 29424b44844SDave Liu SERR_PHY_INT_ERR = (1 << 17), /* PHY internal error */ 29524b44844SDave Liu SERR_COMM_WAKE = (1 << 18), /* Comm wake */ 29624b44844SDave Liu SERR_10B_8B_ERR = (1 << 19), /* 10b to 8b decode error */ 29724b44844SDave Liu SERR_DISPARITY = (1 << 20), /* Disparity */ 29824b44844SDave Liu SERR_CRC = (1 << 21), /* CRC error */ 29924b44844SDave Liu SERR_HANDSHAKE = (1 << 22), /* Handshake error */ 30024b44844SDave Liu SERR_LINK_SEQ_ERR = (1 << 23), /* Link sequence error */ 30124b44844SDave Liu SERR_TRANS_ST_ERROR = (1 << 24), /* Transport state trans. error */ 30224b44844SDave Liu SERR_UNRECOG_FIS = (1 << 25), /* Unrecognized FIS */ 30324b44844SDave Liu SERR_DEV_XCHG = (1 << 26), /* device exchanged */ 30424b44844SDave Liu 30524b44844SDave Liu /* struct ata_taskfile flags */ 30624b44844SDave Liu ATA_TFLAG_LBA48 = (1 << 0), /* enable 48-bit LBA and "HOB" */ 30724b44844SDave Liu ATA_TFLAG_ISADDR = (1 << 1), /* enable r/w to nsect/lba regs */ 30824b44844SDave Liu ATA_TFLAG_DEVICE = (1 << 2), /* enable r/w to device reg */ 30924b44844SDave Liu ATA_TFLAG_WRITE = (1 << 3), /* data dir: host->dev==1 (write) */ 31024b44844SDave Liu ATA_TFLAG_LBA = (1 << 4), /* enable LBA */ 31124b44844SDave Liu ATA_TFLAG_FUA = (1 << 5), /* enable FUA */ 31224b44844SDave Liu ATA_TFLAG_POLLING = (1 << 6), /* set nIEN to 1 and use polling */ 31324b44844SDave Liu 31424b44844SDave Liu /* protocol flags */ 31524b44844SDave Liu ATA_PROT_FLAG_PIO = (1 << 0), /* is PIO */ 31624b44844SDave Liu ATA_PROT_FLAG_DMA = (1 << 1), /* is DMA */ 31724b44844SDave Liu ATA_PROT_FLAG_DATA = ATA_PROT_FLAG_PIO | ATA_PROT_FLAG_DMA, 31824b44844SDave Liu ATA_PROT_FLAG_NCQ = (1 << 2), /* is NCQ */ 31924b44844SDave Liu ATA_PROT_FLAG_ATAPI = (1 << 3), /* is ATAPI */ 320ffc664e8SDave Liu }; 321ffc664e8SDave Liu 32224b44844SDave Liu enum ata_tf_protocols { 32324b44844SDave Liu /* ATA taskfile protocols */ 32424b44844SDave Liu ATA_PROT_UNKNOWN, /* unknown/invalid */ 325ffc664e8SDave Liu ATA_PROT_NODATA, /* no data */ 326ffc664e8SDave Liu ATA_PROT_PIO, /* PIO data xfer */ 327ffc664e8SDave Liu ATA_PROT_DMA, /* DMA */ 328ffc664e8SDave Liu ATA_PROT_NCQ, /* NCQ */ 32924b44844SDave Liu ATAPI_PROT_NODATA, /* packet command, no data */ 33024b44844SDave Liu ATAPI_PROT_PIO, /* packet command, PIO data xfer*/ 33124b44844SDave Liu ATAPI_PROT_DMA, /* packet command with special DMA sauce */ 33224b44844SDave Liu }; 33324b44844SDave Liu 33424b44844SDave Liu enum ata_ioctls { 33524b44844SDave Liu ATA_IOC_GET_IO32 = 0x309, 33624b44844SDave Liu ATA_IOC_SET_IO32 = 0x324, 337ffc664e8SDave Liu }; 338ffc664e8SDave Liu 339ffc664e8SDave Liu enum ata_dev_typed { 340ffc664e8SDave Liu ATA_DEV_ATA, /* ATA device */ 341ffc664e8SDave Liu ATA_DEV_ATAPI, /* ATAPI device */ 342ffc664e8SDave Liu ATA_DEV_PMP, /* Port Multiplier Port */ 343ffc664e8SDave Liu ATA_DEV_UNKNOWN, /* unknown */ 344ffc664e8SDave Liu }; 345ffc664e8SDave Liu 34624b44844SDave Liu struct ata_taskfile { 34724b44844SDave Liu unsigned long flags; /* ATA_TFLAG_xxx */ 34824b44844SDave Liu u8 protocol; /* ATA_PROT_xxx */ 349ffc664e8SDave Liu 35024b44844SDave Liu u8 ctl; /* control reg */ 351ffc664e8SDave Liu 35224b44844SDave Liu u8 hob_feature; /* additional data */ 35324b44844SDave Liu u8 hob_nsect; /* to support LBA48 */ 35424b44844SDave Liu u8 hob_lbal; 35524b44844SDave Liu u8 hob_lbam; 35624b44844SDave Liu u8 hob_lbah; 357ffc664e8SDave Liu 35824b44844SDave Liu u8 feature; 35924b44844SDave Liu u8 nsect; 36024b44844SDave Liu u8 lbal; 36124b44844SDave Liu u8 lbam; 36224b44844SDave Liu u8 lbah; 363ffc664e8SDave Liu 36424b44844SDave Liu u8 device; 365ffc664e8SDave Liu 36624b44844SDave Liu u8 command; /* IO operation */ 367ffc664e8SDave Liu }; 368ffc664e8SDave Liu 36924b44844SDave Liu /* 37024b44844SDave Liu * protocol tests 37124b44844SDave Liu */ 37224b44844SDave Liu static inline unsigned int ata_prot_flags(u8 prot) 37324b44844SDave Liu { 37424b44844SDave Liu switch (prot) { 37524b44844SDave Liu case ATA_PROT_NODATA: 37624b44844SDave Liu return 0; 37724b44844SDave Liu case ATA_PROT_PIO: 37824b44844SDave Liu return ATA_PROT_FLAG_PIO; 37924b44844SDave Liu case ATA_PROT_DMA: 38024b44844SDave Liu return ATA_PROT_FLAG_DMA; 38124b44844SDave Liu case ATA_PROT_NCQ: 38224b44844SDave Liu return ATA_PROT_FLAG_DMA | ATA_PROT_FLAG_NCQ; 38324b44844SDave Liu case ATAPI_PROT_NODATA: 38424b44844SDave Liu return ATA_PROT_FLAG_ATAPI; 38524b44844SDave Liu case ATAPI_PROT_PIO: 38624b44844SDave Liu return ATA_PROT_FLAG_ATAPI | ATA_PROT_FLAG_PIO; 38724b44844SDave Liu case ATAPI_PROT_DMA: 38824b44844SDave Liu return ATA_PROT_FLAG_ATAPI | ATA_PROT_FLAG_DMA; 38924b44844SDave Liu } 39024b44844SDave Liu return 0; 39124b44844SDave Liu } 39224b44844SDave Liu 39324b44844SDave Liu static inline int ata_is_atapi(u8 prot) 39424b44844SDave Liu { 39524b44844SDave Liu return ata_prot_flags(prot) & ATA_PROT_FLAG_ATAPI; 39624b44844SDave Liu } 39724b44844SDave Liu 39824b44844SDave Liu static inline int ata_is_nodata(u8 prot) 39924b44844SDave Liu { 40024b44844SDave Liu return !(ata_prot_flags(prot) & ATA_PROT_FLAG_DATA); 40124b44844SDave Liu } 40224b44844SDave Liu 40324b44844SDave Liu static inline int ata_is_pio(u8 prot) 40424b44844SDave Liu { 40524b44844SDave Liu return ata_prot_flags(prot) & ATA_PROT_FLAG_PIO; 40624b44844SDave Liu } 40724b44844SDave Liu 40824b44844SDave Liu static inline int ata_is_dma(u8 prot) 40924b44844SDave Liu { 41024b44844SDave Liu return ata_prot_flags(prot) & ATA_PROT_FLAG_DMA; 41124b44844SDave Liu } 41224b44844SDave Liu 41324b44844SDave Liu static inline int ata_is_ncq(u8 prot) 41424b44844SDave Liu { 41524b44844SDave Liu return ata_prot_flags(prot) & ATA_PROT_FLAG_NCQ; 41624b44844SDave Liu } 41724b44844SDave Liu 41824b44844SDave Liu static inline int ata_is_data(u8 prot) 41924b44844SDave Liu { 42024b44844SDave Liu return ata_prot_flags(prot) & ATA_PROT_FLAG_DATA; 42124b44844SDave Liu } 42224b44844SDave Liu 42324b44844SDave Liu /* 42424b44844SDave Liu * id tests 42524b44844SDave Liu */ 426ffc664e8SDave Liu #define ata_id_is_ata(id) (((id)[0] & (1 << 15)) == 0) 427ffc664e8SDave Liu #define ata_id_has_lba(id) ((id)[49] & (1 << 9)) 428ffc664e8SDave Liu #define ata_id_has_dma(id) ((id)[49] & (1 << 8)) 429ffc664e8SDave Liu #define ata_id_has_ncq(id) ((id)[76] & (1 << 8)) 430ffc664e8SDave Liu #define ata_id_queue_depth(id) (((id)[75] & 0x1f) + 1) 43124b44844SDave Liu #define ata_id_removeable(id) ((id)[0] & (1 << 7)) 43224b44844SDave Liu #define ata_id_iordy_disable(id) ((id)[49] & (1 << 10)) 43324b44844SDave Liu #define ata_id_has_iordy(id) ((id)[49] & (1 << 11)) 434ffc664e8SDave Liu 435ffc664e8SDave Liu #define ata_id_u32(id,n) \ 436ffc664e8SDave Liu (((u32) (id)[(n) + 1] << 16) | ((u32) (id)[(n)])) 437ffc664e8SDave Liu #define ata_id_u64(id,n) \ 438ffc664e8SDave Liu ( ((u64) (id)[(n) + 3] << 48) | \ 439ffc664e8SDave Liu ((u64) (id)[(n) + 2] << 32) | \ 440ffc664e8SDave Liu ((u64) (id)[(n) + 1] << 16) | \ 441ffc664e8SDave Liu ((u64) (id)[(n) + 0]) ) 442ffc664e8SDave Liu 44324b44844SDave Liu #define ata_id_cdb_intr(id) (((id)[0] & 0x60) == 0x20) 44424b44844SDave Liu 44524b44844SDave Liu static inline int ata_id_has_fua(const u16 *id) 44624b44844SDave Liu { 44724b44844SDave Liu if ((id[84] & 0xC000) != 0x4000) 44824b44844SDave Liu return 0; 44924b44844SDave Liu return id[84] & (1 << 6); 45024b44844SDave Liu } 45124b44844SDave Liu 45224b44844SDave Liu static inline int ata_id_has_flush(const u16 *id) 45324b44844SDave Liu { 45424b44844SDave Liu if ((id[83] & 0xC000) != 0x4000) 45524b44844SDave Liu return 0; 45624b44844SDave Liu return id[83] & (1 << 12); 45724b44844SDave Liu } 45824b44844SDave Liu 45924b44844SDave Liu static inline int ata_id_has_flush_ext(const u16 *id) 46024b44844SDave Liu { 46124b44844SDave Liu if ((id[83] & 0xC000) != 0x4000) 46224b44844SDave Liu return 0; 46324b44844SDave Liu return id[83] & (1 << 13); 46424b44844SDave Liu } 46524b44844SDave Liu 46624b44844SDave Liu static inline int ata_id_has_lba48(const u16 *id) 46724b44844SDave Liu { 46824b44844SDave Liu if ((id[83] & 0xC000) != 0x4000) 46924b44844SDave Liu return 0; 47024b44844SDave Liu if (!ata_id_u64(id, 100)) 47124b44844SDave Liu return 0; 47224b44844SDave Liu return id[83] & (1 << 10); 47324b44844SDave Liu } 47424b44844SDave Liu 47524b44844SDave Liu static inline int ata_id_hpa_enabled(const u16 *id) 47624b44844SDave Liu { 47724b44844SDave Liu /* Yes children, word 83 valid bits cover word 82 data */ 47824b44844SDave Liu if ((id[83] & 0xC000) != 0x4000) 47924b44844SDave Liu return 0; 48024b44844SDave Liu /* And 87 covers 85-87 */ 48124b44844SDave Liu if ((id[87] & 0xC000) != 0x4000) 48224b44844SDave Liu return 0; 48324b44844SDave Liu /* Check command sets enabled as well as supported */ 48424b44844SDave Liu if ((id[85] & ( 1 << 10)) == 0) 48524b44844SDave Liu return 0; 48624b44844SDave Liu return id[82] & (1 << 10); 48724b44844SDave Liu } 48824b44844SDave Liu 48924b44844SDave Liu static inline int ata_id_has_wcache(const u16 *id) 49024b44844SDave Liu { 49124b44844SDave Liu /* Yes children, word 83 valid bits cover word 82 data */ 49224b44844SDave Liu if ((id[83] & 0xC000) != 0x4000) 49324b44844SDave Liu return 0; 49424b44844SDave Liu return id[82] & (1 << 5); 49524b44844SDave Liu } 49624b44844SDave Liu 49724b44844SDave Liu static inline int ata_id_has_pm(const u16 *id) 49824b44844SDave Liu { 49924b44844SDave Liu if ((id[83] & 0xC000) != 0x4000) 50024b44844SDave Liu return 0; 50124b44844SDave Liu return id[82] & (1 << 3); 50224b44844SDave Liu } 50324b44844SDave Liu 50424b44844SDave Liu static inline int ata_id_rahead_enabled(const u16 *id) 50524b44844SDave Liu { 50624b44844SDave Liu if ((id[87] & 0xC000) != 0x4000) 50724b44844SDave Liu return 0; 50824b44844SDave Liu return id[85] & (1 << 6); 50924b44844SDave Liu } 51024b44844SDave Liu 51124b44844SDave Liu static inline int ata_id_wcache_enabled(const u16 *id) 51224b44844SDave Liu { 51324b44844SDave Liu if ((id[87] & 0xC000) != 0x4000) 51424b44844SDave Liu return 0; 51524b44844SDave Liu return id[85] & (1 << 5); 51624b44844SDave Liu } 517ffc664e8SDave Liu 518ffc664e8SDave Liu static inline unsigned int ata_id_major_version(const u16 *id) 519ffc664e8SDave Liu { 520ffc664e8SDave Liu unsigned int mver; 521ffc664e8SDave Liu 522ffc664e8SDave Liu if (id[ATA_ID_MAJOR_VER] == 0xFFFF) 523ffc664e8SDave Liu return 0; 524ffc664e8SDave Liu 525ffc664e8SDave Liu for (mver = 14; mver >= 1; mver--) 526ffc664e8SDave Liu if (id[ATA_ID_MAJOR_VER] & (1 << mver)) 527ffc664e8SDave Liu break; 528ffc664e8SDave Liu return mver; 529ffc664e8SDave Liu } 530ffc664e8SDave Liu 531ffc664e8SDave Liu static inline int ata_id_is_sata(const u16 *id) 532ffc664e8SDave Liu { 533ffc664e8SDave Liu return ata_id_major_version(id) >= 5 && id[93] == 0; 534ffc664e8SDave Liu } 535ffc664e8SDave Liu 53624b44844SDave Liu static inline int ata_id_has_tpm(const u16 *id) 53724b44844SDave Liu { 53824b44844SDave Liu /* The TPM bits are only valid on ATA8 */ 53924b44844SDave Liu if (ata_id_major_version(id) < 8) 54024b44844SDave Liu return 0; 54124b44844SDave Liu if ((id[48] & 0xC000) != 0x4000) 54224b44844SDave Liu return 0; 54324b44844SDave Liu return id[48] & (1 << 0); 54424b44844SDave Liu } 54524b44844SDave Liu 54624b44844SDave Liu static inline int ata_id_has_dword_io(const u16 *id) 54724b44844SDave Liu { 54824b44844SDave Liu /* ATA 8 reuses this flag for "trusted" computing */ 54924b44844SDave Liu if (ata_id_major_version(id) > 7) 55024b44844SDave Liu return 0; 55124b44844SDave Liu if (id[48] & (1 << 0)) 55224b44844SDave Liu return 1; 55324b44844SDave Liu return 0; 55424b44844SDave Liu } 55524b44844SDave Liu 55624b44844SDave Liu static inline int ata_id_current_chs_valid(const u16 *id) 55724b44844SDave Liu { 55824b44844SDave Liu /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command 55924b44844SDave Liu has not been issued to the device then the values of 56024b44844SDave Liu id[54] to id[56] are vendor specific. */ 56124b44844SDave Liu return (id[53] & 0x01) && /* Current translation valid */ 56224b44844SDave Liu id[54] && /* cylinders in current translation */ 56324b44844SDave Liu id[55] && /* heads in current translation */ 56424b44844SDave Liu id[55] <= 16 && 56524b44844SDave Liu id[56]; /* sectors in current translation */ 56624b44844SDave Liu } 56724b44844SDave Liu 56824b44844SDave Liu static inline int ata_id_is_cfa(const u16 *id) 56924b44844SDave Liu { 57024b44844SDave Liu u16 v = id[0]; 57124b44844SDave Liu if (v == 0x848A) /* Standard CF */ 57224b44844SDave Liu return 1; 57324b44844SDave Liu /* Could be CF hiding as standard ATA */ 57424b44844SDave Liu if (ata_id_major_version(id) >= 3 && id[82] != 0xFFFF && 57524b44844SDave Liu (id[82] & ( 1 << 2))) 57624b44844SDave Liu return 1; 57724b44844SDave Liu return 0; 57824b44844SDave Liu } 57924b44844SDave Liu 58024b44844SDave Liu static inline int ata_drive_40wire(const u16 *dev_id) 58124b44844SDave Liu { 58224b44844SDave Liu if (ata_id_is_sata(dev_id)) 58324b44844SDave Liu return 0; /* SATA */ 58424b44844SDave Liu if ((dev_id[93] & 0xE000) == 0x6000) 58524b44844SDave Liu return 0; /* 80 wire */ 58624b44844SDave Liu return 1; 58724b44844SDave Liu } 58824b44844SDave Liu 58924b44844SDave Liu static inline int ata_drive_40wire_relaxed(const u16 *dev_id) 59024b44844SDave Liu { 59124b44844SDave Liu if ((dev_id[93] & 0x2000) == 0x2000) 59224b44844SDave Liu return 0; /* 80 wire */ 59324b44844SDave Liu return 1; 59424b44844SDave Liu } 59524b44844SDave Liu 59624b44844SDave Liu static inline int atapi_cdb_len(const u16 *dev_id) 59724b44844SDave Liu { 59824b44844SDave Liu u16 tmp = dev_id[0] & 0x3; 59924b44844SDave Liu switch (tmp) { 60024b44844SDave Liu case 0: return 12; 60124b44844SDave Liu case 1: return 16; 60224b44844SDave Liu default: return -1; 60324b44844SDave Liu } 60424b44844SDave Liu } 60524b44844SDave Liu 60624b44844SDave Liu static inline int atapi_command_packet_set(const u16 *dev_id) 60724b44844SDave Liu { 60824b44844SDave Liu return (dev_id[0] >> 8) & 0x1f; 60924b44844SDave Liu } 61024b44844SDave Liu 61124b44844SDave Liu static inline int atapi_id_dmadir(const u16 *dev_id) 61224b44844SDave Liu { 61324b44844SDave Liu return ata_id_major_version(dev_id) >= 7 && (dev_id[62] & 0x8000); 61424b44844SDave Liu } 61524b44844SDave Liu 61624b44844SDave Liu static inline int is_multi_taskfile(struct ata_taskfile *tf) 61724b44844SDave Liu { 61824b44844SDave Liu return (tf->command == ATA_CMD_READ_MULTI) || 61924b44844SDave Liu (tf->command == ATA_CMD_WRITE_MULTI) || 62024b44844SDave Liu (tf->command == ATA_CMD_READ_MULTI_EXT) || 62124b44844SDave Liu (tf->command == ATA_CMD_WRITE_MULTI_EXT) || 62224b44844SDave Liu (tf->command == ATA_CMD_WRITE_MULTI_FUA_EXT); 62324b44844SDave Liu } 62424b44844SDave Liu 62524b44844SDave Liu static inline int ata_ok(u8 status) 62624b44844SDave Liu { 62724b44844SDave Liu return ((status & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ | ATA_ERR)) 62824b44844SDave Liu == ATA_DRDY); 62924b44844SDave Liu } 63024b44844SDave Liu 63124b44844SDave Liu static inline int lba_28_ok(u64 block, u32 n_block) 63224b44844SDave Liu { 63324b44844SDave Liu /* check the ending block number */ 63424b44844SDave Liu return ((block + n_block - 1) < ((u64)1 << 28)) && (n_block <= 256); 63524b44844SDave Liu } 63624b44844SDave Liu 63724b44844SDave Liu static inline int lba_48_ok(u64 block, u32 n_block) 63824b44844SDave Liu { 63924b44844SDave Liu /* check the ending block number */ 64024b44844SDave Liu return ((block + n_block - 1) < ((u64)1 << 48)) && (n_block <= 65536); 64124b44844SDave Liu } 64224b44844SDave Liu 64324b44844SDave Liu #define sata_pmp_gscr_vendor(gscr) ((gscr)[SATA_PMP_GSCR_PROD_ID] & 0xffff) 64424b44844SDave Liu #define sata_pmp_gscr_devid(gscr) ((gscr)[SATA_PMP_GSCR_PROD_ID] >> 16) 64524b44844SDave Liu #define sata_pmp_gscr_rev(gscr) (((gscr)[SATA_PMP_GSCR_REV] >> 8) & 0xff) 64624b44844SDave Liu #define sata_pmp_gscr_ports(gscr) ((gscr)[SATA_PMP_GSCR_PORT_INFO] & 0xf) 64724b44844SDave Liu 648ffc664e8SDave Liu u64 ata_id_n_sectors(u16 *id); 649ffc664e8SDave Liu u32 ata_dev_classify(u32 sig); 650ffc664e8SDave Liu void ata_id_c_string(const u16 *id, unsigned char *s, 651ffc664e8SDave Liu unsigned int ofs, unsigned int len); 652ffc664e8SDave Liu void ata_dump_id(u16 *id); 653ffc664e8SDave Liu void ata_swap_buf_le16(u16 *buf, unsigned int buf_words); 654ffc664e8SDave Liu 655ffc664e8SDave Liu #endif /* __LIBATA_H__ */ 656