11fd7a697STejun Heo /* 21fd7a697STejun Heo * sata_inic162x.c - Driver for Initio 162x SATA controllers 31fd7a697STejun Heo * 41fd7a697STejun Heo * Copyright 2006 SUSE Linux Products GmbH 51fd7a697STejun Heo * Copyright 2006 Tejun Heo <teheo@novell.com> 61fd7a697STejun Heo * 71fd7a697STejun Heo * This file is released under GPL v2. 81fd7a697STejun Heo * 91fd7a697STejun Heo * This controller is eccentric and easily locks up if something isn't 101fd7a697STejun Heo * right. Documentation is available at initio's website but it only 111fd7a697STejun Heo * documents registers (not programming model). 121fd7a697STejun Heo * 1322bfc6d5STejun Heo * This driver has interesting history. The first version was written 1422bfc6d5STejun Heo * from the documentation and a 2.4 IDE driver posted on a Taiwan 1522bfc6d5STejun Heo * company, which didn't use any IDMA features and couldn't handle 1622bfc6d5STejun Heo * LBA48. The resulting driver couldn't handle LBA48 devices either 1722bfc6d5STejun Heo * making it pretty useless. 1822bfc6d5STejun Heo * 1922bfc6d5STejun Heo * After a while, initio picked the driver up, renamed it to 2022bfc6d5STejun Heo * sata_initio162x, updated it to use IDMA for ATA DMA commands and 2122bfc6d5STejun Heo * posted it on their website. It only used ATA_PROT_DMA for IDMA and 2222bfc6d5STejun Heo * attaching both devices and issuing IDMA and !IDMA commands 2322bfc6d5STejun Heo * simultaneously broke it due to PIRQ masking interaction but it did 2422bfc6d5STejun Heo * show how to use the IDMA (ADMA + some initio specific twists) 2522bfc6d5STejun Heo * engine. 2622bfc6d5STejun Heo * 2722bfc6d5STejun Heo * Then, I picked up their changes again and here's the usable driver 2822bfc6d5STejun Heo * which uses IDMA for everything. Everything works now including 2922bfc6d5STejun Heo * LBA48, CD/DVD burning, suspend/resume and hotplug. There are some 3022bfc6d5STejun Heo * issues tho. Result Tf is not resported properly, NCQ isn't 3122bfc6d5STejun Heo * supported yet and CD/DVD writing works with DMA assisted PIO 3222bfc6d5STejun Heo * protocol (which, for native SATA devices, shouldn't cause any 3322bfc6d5STejun Heo * noticeable difference). 3422bfc6d5STejun Heo * 3522bfc6d5STejun Heo * Anyways, so, here's finally a working driver for inic162x. Enjoy! 3622bfc6d5STejun Heo * 3722bfc6d5STejun Heo * initio: If you guys wanna improve the driver regarding result TF 3822bfc6d5STejun Heo * access and other stuff, please feel free to contact me. I'll be 3922bfc6d5STejun Heo * happy to assist. 401fd7a697STejun Heo */ 411fd7a697STejun Heo 425a0e3ad6STejun Heo #include <linux/gfp.h> 431fd7a697STejun Heo #include <linux/kernel.h> 441fd7a697STejun Heo #include <linux/module.h> 451fd7a697STejun Heo #include <linux/pci.h> 461fd7a697STejun Heo #include <scsi/scsi_host.h> 471fd7a697STejun Heo #include <linux/libata.h> 481fd7a697STejun Heo #include <linux/blkdev.h> 491fd7a697STejun Heo #include <scsi/scsi_device.h> 501fd7a697STejun Heo 511fd7a697STejun Heo #define DRV_NAME "sata_inic162x" 5222bfc6d5STejun Heo #define DRV_VERSION "0.4" 531fd7a697STejun Heo 541fd7a697STejun Heo enum { 55ba66b242STejun Heo MMIO_BAR_PCI = 5, 56ba66b242STejun Heo MMIO_BAR_CARDBUS = 1, 571fd7a697STejun Heo 581fd7a697STejun Heo NR_PORTS = 2, 591fd7a697STejun Heo 603ad400a9STejun Heo IDMA_CPB_TBL_SIZE = 4 * 32, 613ad400a9STejun Heo 623ad400a9STejun Heo INIC_DMA_BOUNDARY = 0xffffff, 633ad400a9STejun Heo 64b0dd9b8eSTejun Heo HOST_ACTRL = 0x08, 651fd7a697STejun Heo HOST_CTL = 0x7c, 661fd7a697STejun Heo HOST_STAT = 0x7e, 671fd7a697STejun Heo HOST_IRQ_STAT = 0xbc, 681fd7a697STejun Heo HOST_IRQ_MASK = 0xbe, 691fd7a697STejun Heo 701fd7a697STejun Heo PORT_SIZE = 0x40, 711fd7a697STejun Heo 721fd7a697STejun Heo /* registers for ATA TF operation */ 73b0dd9b8eSTejun Heo PORT_TF_DATA = 0x00, 74b0dd9b8eSTejun Heo PORT_TF_FEATURE = 0x01, 75b0dd9b8eSTejun Heo PORT_TF_NSECT = 0x02, 76b0dd9b8eSTejun Heo PORT_TF_LBAL = 0x03, 77b0dd9b8eSTejun Heo PORT_TF_LBAM = 0x04, 78b0dd9b8eSTejun Heo PORT_TF_LBAH = 0x05, 79b0dd9b8eSTejun Heo PORT_TF_DEVICE = 0x06, 80b0dd9b8eSTejun Heo PORT_TF_COMMAND = 0x07, 81b0dd9b8eSTejun Heo PORT_TF_ALT_STAT = 0x08, 821fd7a697STejun Heo PORT_IRQ_STAT = 0x09, 831fd7a697STejun Heo PORT_IRQ_MASK = 0x0a, 841fd7a697STejun Heo PORT_PRD_CTL = 0x0b, 851fd7a697STejun Heo PORT_PRD_ADDR = 0x0c, 861fd7a697STejun Heo PORT_PRD_XFERLEN = 0x10, 87b0dd9b8eSTejun Heo PORT_CPB_CPBLAR = 0x18, 88b0dd9b8eSTejun Heo PORT_CPB_PTQFIFO = 0x1c, 891fd7a697STejun Heo 901fd7a697STejun Heo /* IDMA register */ 911fd7a697STejun Heo PORT_IDMA_CTL = 0x14, 92b0dd9b8eSTejun Heo PORT_IDMA_STAT = 0x16, 93b0dd9b8eSTejun Heo 94b0dd9b8eSTejun Heo PORT_RPQ_FIFO = 0x1e, 95b0dd9b8eSTejun Heo PORT_RPQ_CNT = 0x1f, 961fd7a697STejun Heo 971fd7a697STejun Heo PORT_SCR = 0x20, 981fd7a697STejun Heo 991fd7a697STejun Heo /* HOST_CTL bits */ 10099580664SBob Stewart HCTL_LEDEN = (1 << 3), /* enable LED operation */ 1011fd7a697STejun Heo HCTL_IRQOFF = (1 << 8), /* global IRQ off */ 102b0dd9b8eSTejun Heo HCTL_FTHD0 = (1 << 10), /* fifo threshold 0 */ 103b0dd9b8eSTejun Heo HCTL_FTHD1 = (1 << 11), /* fifo threshold 1*/ 104b0dd9b8eSTejun Heo HCTL_PWRDWN = (1 << 12), /* power down PHYs */ 1051fd7a697STejun Heo HCTL_SOFTRST = (1 << 13), /* global reset (no phy reset) */ 1061fd7a697STejun Heo HCTL_RPGSEL = (1 << 15), /* register page select */ 1071fd7a697STejun Heo 1081fd7a697STejun Heo HCTL_KNOWN_BITS = HCTL_IRQOFF | HCTL_PWRDWN | HCTL_SOFTRST | 1091fd7a697STejun Heo HCTL_RPGSEL, 1101fd7a697STejun Heo 1111fd7a697STejun Heo /* HOST_IRQ_(STAT|MASK) bits */ 1121fd7a697STejun Heo HIRQ_PORT0 = (1 << 0), 1131fd7a697STejun Heo HIRQ_PORT1 = (1 << 1), 1141fd7a697STejun Heo HIRQ_SOFT = (1 << 14), 1151fd7a697STejun Heo HIRQ_GLOBAL = (1 << 15), /* STAT only */ 1161fd7a697STejun Heo 1171fd7a697STejun Heo /* PORT_IRQ_(STAT|MASK) bits */ 1181fd7a697STejun Heo PIRQ_OFFLINE = (1 << 0), /* device unplugged */ 1191fd7a697STejun Heo PIRQ_ONLINE = (1 << 1), /* device plugged */ 1201fd7a697STejun Heo PIRQ_COMPLETE = (1 << 2), /* completion interrupt */ 1211fd7a697STejun Heo PIRQ_FATAL = (1 << 3), /* fatal error */ 1221fd7a697STejun Heo PIRQ_ATA = (1 << 4), /* ATA interrupt */ 1231fd7a697STejun Heo PIRQ_REPLY = (1 << 5), /* reply FIFO not empty */ 1241fd7a697STejun Heo PIRQ_PENDING = (1 << 7), /* port IRQ pending (STAT only) */ 1251fd7a697STejun Heo 1261fd7a697STejun Heo PIRQ_ERR = PIRQ_OFFLINE | PIRQ_ONLINE | PIRQ_FATAL, 127f8b0685aSTejun Heo PIRQ_MASK_DEFAULT = PIRQ_REPLY | PIRQ_ATA, 1281fd7a697STejun Heo PIRQ_MASK_FREEZE = 0xff, 1291fd7a697STejun Heo 1301fd7a697STejun Heo /* PORT_PRD_CTL bits */ 1311fd7a697STejun Heo PRD_CTL_START = (1 << 0), 1321fd7a697STejun Heo PRD_CTL_WR = (1 << 3), 1331fd7a697STejun Heo PRD_CTL_DMAEN = (1 << 7), /* DMA enable */ 1341fd7a697STejun Heo 1351fd7a697STejun Heo /* PORT_IDMA_CTL bits */ 1361fd7a697STejun Heo IDMA_CTL_RST_ATA = (1 << 2), /* hardreset ATA bus */ 1371fd7a697STejun Heo IDMA_CTL_RST_IDMA = (1 << 5), /* reset IDMA machinary */ 1381fd7a697STejun Heo IDMA_CTL_GO = (1 << 7), /* IDMA mode go */ 1391fd7a697STejun Heo IDMA_CTL_ATA_NIEN = (1 << 8), /* ATA IRQ disable */ 140b0dd9b8eSTejun Heo 141b0dd9b8eSTejun Heo /* PORT_IDMA_STAT bits */ 142b0dd9b8eSTejun Heo IDMA_STAT_PERR = (1 << 0), /* PCI ERROR MODE */ 143b0dd9b8eSTejun Heo IDMA_STAT_CPBERR = (1 << 1), /* ADMA CPB error */ 144b0dd9b8eSTejun Heo IDMA_STAT_LGCY = (1 << 3), /* ADMA legacy */ 145b0dd9b8eSTejun Heo IDMA_STAT_UIRQ = (1 << 4), /* ADMA unsolicited irq */ 146b0dd9b8eSTejun Heo IDMA_STAT_STPD = (1 << 5), /* ADMA stopped */ 147b0dd9b8eSTejun Heo IDMA_STAT_PSD = (1 << 6), /* ADMA pause */ 148b0dd9b8eSTejun Heo IDMA_STAT_DONE = (1 << 7), /* ADMA done */ 149b0dd9b8eSTejun Heo 150b0dd9b8eSTejun Heo IDMA_STAT_ERR = IDMA_STAT_PERR | IDMA_STAT_CPBERR, 151b0dd9b8eSTejun Heo 152b0dd9b8eSTejun Heo /* CPB Control Flags*/ 153b0dd9b8eSTejun Heo CPB_CTL_VALID = (1 << 0), /* CPB valid */ 154b0dd9b8eSTejun Heo CPB_CTL_QUEUED = (1 << 1), /* queued command */ 155b0dd9b8eSTejun Heo CPB_CTL_DATA = (1 << 2), /* data, rsvd in datasheet */ 156b0dd9b8eSTejun Heo CPB_CTL_IEN = (1 << 3), /* PCI interrupt enable */ 157b0dd9b8eSTejun Heo CPB_CTL_DEVDIR = (1 << 4), /* device direction control */ 158b0dd9b8eSTejun Heo 159b0dd9b8eSTejun Heo /* CPB Response Flags */ 160b0dd9b8eSTejun Heo CPB_RESP_DONE = (1 << 0), /* ATA command complete */ 161b0dd9b8eSTejun Heo CPB_RESP_REL = (1 << 1), /* ATA release */ 162b0dd9b8eSTejun Heo CPB_RESP_IGNORED = (1 << 2), /* CPB ignored */ 163b0dd9b8eSTejun Heo CPB_RESP_ATA_ERR = (1 << 3), /* ATA command error */ 164b0dd9b8eSTejun Heo CPB_RESP_SPURIOUS = (1 << 4), /* ATA spurious interrupt error */ 165b0dd9b8eSTejun Heo CPB_RESP_UNDERFLOW = (1 << 5), /* APRD deficiency length error */ 166b0dd9b8eSTejun Heo CPB_RESP_OVERFLOW = (1 << 6), /* APRD exccess length error */ 167b0dd9b8eSTejun Heo CPB_RESP_CPB_ERR = (1 << 7), /* CPB error flag */ 168b0dd9b8eSTejun Heo 169b0dd9b8eSTejun Heo /* PRD Control Flags */ 170b0dd9b8eSTejun Heo PRD_DRAIN = (1 << 1), /* ignore data excess */ 171b0dd9b8eSTejun Heo PRD_CDB = (1 << 2), /* atapi packet command pointer */ 172b0dd9b8eSTejun Heo PRD_DIRECT_INTR = (1 << 3), /* direct interrupt */ 173b0dd9b8eSTejun Heo PRD_DMA = (1 << 4), /* data transfer method */ 174b0dd9b8eSTejun Heo PRD_WRITE = (1 << 5), /* data dir, rsvd in datasheet */ 175b0dd9b8eSTejun Heo PRD_IOM = (1 << 6), /* io/memory transfer */ 176b0dd9b8eSTejun Heo PRD_END = (1 << 7), /* APRD chain end */ 1771fd7a697STejun Heo }; 1781fd7a697STejun Heo 1793ad400a9STejun Heo /* Comman Parameter Block */ 1803ad400a9STejun Heo struct inic_cpb { 1813ad400a9STejun Heo u8 resp_flags; /* Response Flags */ 1823ad400a9STejun Heo u8 error; /* ATA Error */ 1833ad400a9STejun Heo u8 status; /* ATA Status */ 1843ad400a9STejun Heo u8 ctl_flags; /* Control Flags */ 1853ad400a9STejun Heo __le32 len; /* Total Transfer Length */ 1863ad400a9STejun Heo __le32 prd; /* First PRD pointer */ 1873ad400a9STejun Heo u8 rsvd[4]; 1883ad400a9STejun Heo /* 16 bytes */ 1893ad400a9STejun Heo u8 feature; /* ATA Feature */ 1903ad400a9STejun Heo u8 hob_feature; /* ATA Ex. Feature */ 1913ad400a9STejun Heo u8 device; /* ATA Device/Head */ 1923ad400a9STejun Heo u8 mirctl; /* Mirror Control */ 1933ad400a9STejun Heo u8 nsect; /* ATA Sector Count */ 1943ad400a9STejun Heo u8 hob_nsect; /* ATA Ex. Sector Count */ 1953ad400a9STejun Heo u8 lbal; /* ATA Sector Number */ 1963ad400a9STejun Heo u8 hob_lbal; /* ATA Ex. Sector Number */ 1973ad400a9STejun Heo u8 lbam; /* ATA Cylinder Low */ 1983ad400a9STejun Heo u8 hob_lbam; /* ATA Ex. Cylinder Low */ 1993ad400a9STejun Heo u8 lbah; /* ATA Cylinder High */ 2003ad400a9STejun Heo u8 hob_lbah; /* ATA Ex. Cylinder High */ 2013ad400a9STejun Heo u8 command; /* ATA Command */ 2023ad400a9STejun Heo u8 ctl; /* ATA Control */ 2033ad400a9STejun Heo u8 slave_error; /* Slave ATA Error */ 2043ad400a9STejun Heo u8 slave_status; /* Slave ATA Status */ 2053ad400a9STejun Heo /* 32 bytes */ 2063ad400a9STejun Heo } __packed; 2073ad400a9STejun Heo 2083ad400a9STejun Heo /* Physical Region Descriptor */ 2093ad400a9STejun Heo struct inic_prd { 2103ad400a9STejun Heo __le32 mad; /* Physical Memory Address */ 2113ad400a9STejun Heo __le16 len; /* Transfer Length */ 2123ad400a9STejun Heo u8 rsvd; 2133ad400a9STejun Heo u8 flags; /* Control Flags */ 2143ad400a9STejun Heo } __packed; 2153ad400a9STejun Heo 2163ad400a9STejun Heo struct inic_pkt { 2173ad400a9STejun Heo struct inic_cpb cpb; 218b3f677e5STejun Heo struct inic_prd prd[LIBATA_MAX_PRD + 1]; /* + 1 for cdb */ 219b3f677e5STejun Heo u8 cdb[ATAPI_CDB_LEN]; 2203ad400a9STejun Heo } __packed; 2213ad400a9STejun Heo 2221fd7a697STejun Heo struct inic_host_priv { 223ba66b242STejun Heo void __iomem *mmio_base; 2241fd7a697STejun Heo u16 cached_hctl; 2251fd7a697STejun Heo }; 2261fd7a697STejun Heo 2271fd7a697STejun Heo struct inic_port_priv { 2283ad400a9STejun Heo struct inic_pkt *pkt; 2293ad400a9STejun Heo dma_addr_t pkt_dma; 2303ad400a9STejun Heo u32 *cpb_tbl; 2313ad400a9STejun Heo dma_addr_t cpb_tbl_dma; 2321fd7a697STejun Heo }; 2331fd7a697STejun Heo 2341fd7a697STejun Heo static struct scsi_host_template inic_sht = { 235ab5b0235STejun Heo ATA_BASE_SHT(DRV_NAME), 236ab5b0235STejun Heo .sg_tablesize = LIBATA_MAX_PRD, /* maybe it can be larger? */ 2373ad400a9STejun Heo .dma_boundary = INIC_DMA_BOUNDARY, 2381fd7a697STejun Heo }; 2391fd7a697STejun Heo 2401fd7a697STejun Heo static const int scr_map[] = { 2411fd7a697STejun Heo [SCR_STATUS] = 0, 2421fd7a697STejun Heo [SCR_ERROR] = 1, 2431fd7a697STejun Heo [SCR_CONTROL] = 2, 2441fd7a697STejun Heo }; 2451fd7a697STejun Heo 2461fd7a697STejun Heo static void __iomem *inic_port_base(struct ata_port *ap) 2471fd7a697STejun Heo { 248ba66b242STejun Heo struct inic_host_priv *hpriv = ap->host->private_data; 249ba66b242STejun Heo 250ba66b242STejun Heo return hpriv->mmio_base + ap->port_no * PORT_SIZE; 2511fd7a697STejun Heo } 2521fd7a697STejun Heo 2531fd7a697STejun Heo static void inic_reset_port(void __iomem *port_base) 2541fd7a697STejun Heo { 2551fd7a697STejun Heo void __iomem *idma_ctl = port_base + PORT_IDMA_CTL; 2561fd7a697STejun Heo 257f8b0685aSTejun Heo /* stop IDMA engine */ 258f8b0685aSTejun Heo readw(idma_ctl); /* flush */ 259f8b0685aSTejun Heo msleep(1); 2601fd7a697STejun Heo 2611fd7a697STejun Heo /* mask IRQ and assert reset */ 262f8b0685aSTejun Heo writew(IDMA_CTL_RST_IDMA, idma_ctl); 2631fd7a697STejun Heo readw(idma_ctl); /* flush */ 2641fd7a697STejun Heo msleep(1); 2651fd7a697STejun Heo 2661fd7a697STejun Heo /* release reset */ 267f8b0685aSTejun Heo writew(0, idma_ctl); 2681fd7a697STejun Heo 2691fd7a697STejun Heo /* clear irq */ 2701fd7a697STejun Heo writeb(0xff, port_base + PORT_IRQ_STAT); 2711fd7a697STejun Heo } 2721fd7a697STejun Heo 27382ef04fbSTejun Heo static int inic_scr_read(struct ata_link *link, unsigned sc_reg, u32 *val) 2741fd7a697STejun Heo { 27582ef04fbSTejun Heo void __iomem *scr_addr = inic_port_base(link->ap) + PORT_SCR; 2761fd7a697STejun Heo void __iomem *addr; 2771fd7a697STejun Heo 2781fd7a697STejun Heo if (unlikely(sc_reg >= ARRAY_SIZE(scr_map))) 279da3dbb17STejun Heo return -EINVAL; 2801fd7a697STejun Heo 2811fd7a697STejun Heo addr = scr_addr + scr_map[sc_reg] * 4; 282da3dbb17STejun Heo *val = readl(scr_addr + scr_map[sc_reg] * 4); 2831fd7a697STejun Heo 2841fd7a697STejun Heo /* this controller has stuck DIAG.N, ignore it */ 2851fd7a697STejun Heo if (sc_reg == SCR_ERROR) 286da3dbb17STejun Heo *val &= ~SERR_PHYRDY_CHG; 287da3dbb17STejun Heo return 0; 2881fd7a697STejun Heo } 2891fd7a697STejun Heo 29082ef04fbSTejun Heo static int inic_scr_write(struct ata_link *link, unsigned sc_reg, u32 val) 2911fd7a697STejun Heo { 29282ef04fbSTejun Heo void __iomem *scr_addr = inic_port_base(link->ap) + PORT_SCR; 2931fd7a697STejun Heo 2941fd7a697STejun Heo if (unlikely(sc_reg >= ARRAY_SIZE(scr_map))) 295da3dbb17STejun Heo return -EINVAL; 2961fd7a697STejun Heo 2971fd7a697STejun Heo writel(val, scr_addr + scr_map[sc_reg] * 4); 298da3dbb17STejun Heo return 0; 2991fd7a697STejun Heo } 3001fd7a697STejun Heo 3013ad400a9STejun Heo static void inic_stop_idma(struct ata_port *ap) 3023ad400a9STejun Heo { 3033ad400a9STejun Heo void __iomem *port_base = inic_port_base(ap); 3043ad400a9STejun Heo 3053ad400a9STejun Heo readb(port_base + PORT_RPQ_FIFO); 3063ad400a9STejun Heo readb(port_base + PORT_RPQ_CNT); 3073ad400a9STejun Heo writew(0, port_base + PORT_IDMA_CTL); 3083ad400a9STejun Heo } 3093ad400a9STejun Heo 3103ad400a9STejun Heo static void inic_host_err_intr(struct ata_port *ap, u8 irq_stat, u16 idma_stat) 3113ad400a9STejun Heo { 3123ad400a9STejun Heo struct ata_eh_info *ehi = &ap->link.eh_info; 3133ad400a9STejun Heo struct inic_port_priv *pp = ap->private_data; 3143ad400a9STejun Heo struct inic_cpb *cpb = &pp->pkt->cpb; 3153ad400a9STejun Heo bool freeze = false; 3163ad400a9STejun Heo 3173ad400a9STejun Heo ata_ehi_clear_desc(ehi); 3183ad400a9STejun Heo ata_ehi_push_desc(ehi, "irq_stat=0x%x idma_stat=0x%x", 3193ad400a9STejun Heo irq_stat, idma_stat); 3203ad400a9STejun Heo 3213ad400a9STejun Heo inic_stop_idma(ap); 3223ad400a9STejun Heo 3233ad400a9STejun Heo if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) { 3243ad400a9STejun Heo ata_ehi_push_desc(ehi, "hotplug"); 3253ad400a9STejun Heo ata_ehi_hotplugged(ehi); 3263ad400a9STejun Heo freeze = true; 3273ad400a9STejun Heo } 3283ad400a9STejun Heo 3293ad400a9STejun Heo if (idma_stat & IDMA_STAT_PERR) { 3303ad400a9STejun Heo ata_ehi_push_desc(ehi, "PCI error"); 3313ad400a9STejun Heo freeze = true; 3323ad400a9STejun Heo } 3333ad400a9STejun Heo 3343ad400a9STejun Heo if (idma_stat & IDMA_STAT_CPBERR) { 3353ad400a9STejun Heo ata_ehi_push_desc(ehi, "CPB error"); 3363ad400a9STejun Heo 3373ad400a9STejun Heo if (cpb->resp_flags & CPB_RESP_IGNORED) { 3383ad400a9STejun Heo __ata_ehi_push_desc(ehi, " ignored"); 3393ad400a9STejun Heo ehi->err_mask |= AC_ERR_INVALID; 3403ad400a9STejun Heo freeze = true; 3413ad400a9STejun Heo } 3423ad400a9STejun Heo 3433ad400a9STejun Heo if (cpb->resp_flags & CPB_RESP_ATA_ERR) 3443ad400a9STejun Heo ehi->err_mask |= AC_ERR_DEV; 3453ad400a9STejun Heo 3463ad400a9STejun Heo if (cpb->resp_flags & CPB_RESP_SPURIOUS) { 3473ad400a9STejun Heo __ata_ehi_push_desc(ehi, " spurious-intr"); 3483ad400a9STejun Heo ehi->err_mask |= AC_ERR_HSM; 3493ad400a9STejun Heo freeze = true; 3503ad400a9STejun Heo } 3513ad400a9STejun Heo 3523ad400a9STejun Heo if (cpb->resp_flags & 3533ad400a9STejun Heo (CPB_RESP_UNDERFLOW | CPB_RESP_OVERFLOW)) { 3543ad400a9STejun Heo __ata_ehi_push_desc(ehi, " data-over/underflow"); 3553ad400a9STejun Heo ehi->err_mask |= AC_ERR_HSM; 3563ad400a9STejun Heo freeze = true; 3573ad400a9STejun Heo } 3583ad400a9STejun Heo } 3593ad400a9STejun Heo 3603ad400a9STejun Heo if (freeze) 3613ad400a9STejun Heo ata_port_freeze(ap); 3623ad400a9STejun Heo else 3633ad400a9STejun Heo ata_port_abort(ap); 3643ad400a9STejun Heo } 3653ad400a9STejun Heo 3661fd7a697STejun Heo static void inic_host_intr(struct ata_port *ap) 3671fd7a697STejun Heo { 3681fd7a697STejun Heo void __iomem *port_base = inic_port_base(ap); 3693ad400a9STejun Heo struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag); 3701fd7a697STejun Heo u8 irq_stat; 3713ad400a9STejun Heo u16 idma_stat; 3721fd7a697STejun Heo 3733ad400a9STejun Heo /* read and clear IRQ status */ 3741fd7a697STejun Heo irq_stat = readb(port_base + PORT_IRQ_STAT); 3751fd7a697STejun Heo writeb(irq_stat, port_base + PORT_IRQ_STAT); 3763ad400a9STejun Heo idma_stat = readw(port_base + PORT_IDMA_STAT); 3771fd7a697STejun Heo 3783ad400a9STejun Heo if (unlikely((irq_stat & PIRQ_ERR) || (idma_stat & IDMA_STAT_ERR))) 3793ad400a9STejun Heo inic_host_err_intr(ap, irq_stat, idma_stat); 3801fd7a697STejun Heo 381f8b0685aSTejun Heo if (unlikely(!qc)) 3823ad400a9STejun Heo goto spurious; 3831fd7a697STejun Heo 3843ad400a9STejun Heo if (likely(idma_stat & IDMA_STAT_DONE)) { 3853ad400a9STejun Heo inic_stop_idma(ap); 3863ad400a9STejun Heo 3873ad400a9STejun Heo /* Depending on circumstances, device error 3883ad400a9STejun Heo * isn't reported by IDMA, check it explicitly. 3893ad400a9STejun Heo */ 3903ad400a9STejun Heo if (unlikely(readb(port_base + PORT_TF_COMMAND) & 3913ad400a9STejun Heo (ATA_DF | ATA_ERR))) 3923ad400a9STejun Heo qc->err_mask |= AC_ERR_DEV; 3933ad400a9STejun Heo 3943ad400a9STejun Heo ata_qc_complete(qc); 3953ad400a9STejun Heo return; 3963ad400a9STejun Heo } 3971fd7a697STejun Heo 3983ad400a9STejun Heo spurious: 399f8b0685aSTejun Heo ata_port_printk(ap, KERN_WARNING, "unhandled interrupt: " 400f8b0685aSTejun Heo "cmd=0x%x irq_stat=0x%x idma_stat=0x%x\n", 401f8b0685aSTejun Heo qc ? qc->tf.command : 0xff, irq_stat, idma_stat); 4021fd7a697STejun Heo } 4031fd7a697STejun Heo 4041fd7a697STejun Heo static irqreturn_t inic_interrupt(int irq, void *dev_instance) 4051fd7a697STejun Heo { 4061fd7a697STejun Heo struct ata_host *host = dev_instance; 407ba66b242STejun Heo struct inic_host_priv *hpriv = host->private_data; 4081fd7a697STejun Heo u16 host_irq_stat; 40987c8b22bSJoe Perches int i, handled = 0; 4101fd7a697STejun Heo 411ba66b242STejun Heo host_irq_stat = readw(hpriv->mmio_base + HOST_IRQ_STAT); 4121fd7a697STejun Heo 4131fd7a697STejun Heo if (unlikely(!(host_irq_stat & HIRQ_GLOBAL))) 4141fd7a697STejun Heo goto out; 4151fd7a697STejun Heo 4161fd7a697STejun Heo spin_lock(&host->lock); 4171fd7a697STejun Heo 4183e4ec344STejun Heo for (i = 0; i < NR_PORTS; i++) 4193e4ec344STejun Heo if (host_irq_stat & (HIRQ_PORT0 << i)) { 4203e4ec344STejun Heo inic_host_intr(host->ports[i]); 4211fd7a697STejun Heo handled++; 4221fd7a697STejun Heo } 4231fd7a697STejun Heo 4241fd7a697STejun Heo spin_unlock(&host->lock); 4251fd7a697STejun Heo 4261fd7a697STejun Heo out: 4271fd7a697STejun Heo return IRQ_RETVAL(handled); 4281fd7a697STejun Heo } 4291fd7a697STejun Heo 430b3f677e5STejun Heo static int inic_check_atapi_dma(struct ata_queued_cmd *qc) 431b3f677e5STejun Heo { 432b3f677e5STejun Heo /* For some reason ATAPI_PROT_DMA doesn't work for some 433b3f677e5STejun Heo * commands including writes and other misc ops. Use PIO 434b3f677e5STejun Heo * protocol instead, which BTW is driven by the DMA engine 435b3f677e5STejun Heo * anyway, so it shouldn't make much difference for native 436b3f677e5STejun Heo * SATA devices. 437b3f677e5STejun Heo */ 438b3f677e5STejun Heo if (atapi_cmd_type(qc->cdb[0]) == READ) 439b3f677e5STejun Heo return 0; 440b3f677e5STejun Heo return 1; 441b3f677e5STejun Heo } 442b3f677e5STejun Heo 4433ad400a9STejun Heo static void inic_fill_sg(struct inic_prd *prd, struct ata_queued_cmd *qc) 4443ad400a9STejun Heo { 4453ad400a9STejun Heo struct scatterlist *sg; 4463ad400a9STejun Heo unsigned int si; 447049e8e04STejun Heo u8 flags = 0; 4483ad400a9STejun Heo 4493ad400a9STejun Heo if (qc->tf.flags & ATA_TFLAG_WRITE) 4503ad400a9STejun Heo flags |= PRD_WRITE; 4513ad400a9STejun Heo 452049e8e04STejun Heo if (ata_is_dma(qc->tf.protocol)) 453049e8e04STejun Heo flags |= PRD_DMA; 454049e8e04STejun Heo 4553ad400a9STejun Heo for_each_sg(qc->sg, sg, qc->n_elem, si) { 4563ad400a9STejun Heo prd->mad = cpu_to_le32(sg_dma_address(sg)); 4573ad400a9STejun Heo prd->len = cpu_to_le16(sg_dma_len(sg)); 4583ad400a9STejun Heo prd->flags = flags; 4593ad400a9STejun Heo prd++; 4603ad400a9STejun Heo } 4613ad400a9STejun Heo 4623ad400a9STejun Heo WARN_ON(!si); 4633ad400a9STejun Heo prd[-1].flags |= PRD_END; 4643ad400a9STejun Heo } 4653ad400a9STejun Heo 4663ad400a9STejun Heo static void inic_qc_prep(struct ata_queued_cmd *qc) 4673ad400a9STejun Heo { 4683ad400a9STejun Heo struct inic_port_priv *pp = qc->ap->private_data; 4693ad400a9STejun Heo struct inic_pkt *pkt = pp->pkt; 4703ad400a9STejun Heo struct inic_cpb *cpb = &pkt->cpb; 4713ad400a9STejun Heo struct inic_prd *prd = pkt->prd; 472049e8e04STejun Heo bool is_atapi = ata_is_atapi(qc->tf.protocol); 473049e8e04STejun Heo bool is_data = ata_is_data(qc->tf.protocol); 474b3f677e5STejun Heo unsigned int cdb_len = 0; 4753ad400a9STejun Heo 4763ad400a9STejun Heo VPRINTK("ENTER\n"); 4773ad400a9STejun Heo 478049e8e04STejun Heo if (is_atapi) 479b3f677e5STejun Heo cdb_len = qc->dev->cdb_len; 4803ad400a9STejun Heo 4813ad400a9STejun Heo /* prepare packet, based on initio driver */ 4823ad400a9STejun Heo memset(pkt, 0, sizeof(struct inic_pkt)); 4833ad400a9STejun Heo 484049e8e04STejun Heo cpb->ctl_flags = CPB_CTL_VALID | CPB_CTL_IEN; 485b3f677e5STejun Heo if (is_atapi || is_data) 486049e8e04STejun Heo cpb->ctl_flags |= CPB_CTL_DATA; 4873ad400a9STejun Heo 488b3f677e5STejun Heo cpb->len = cpu_to_le32(qc->nbytes + cdb_len); 4893ad400a9STejun Heo cpb->prd = cpu_to_le32(pp->pkt_dma + offsetof(struct inic_pkt, prd)); 4903ad400a9STejun Heo 4913ad400a9STejun Heo cpb->device = qc->tf.device; 4923ad400a9STejun Heo cpb->feature = qc->tf.feature; 4933ad400a9STejun Heo cpb->nsect = qc->tf.nsect; 4943ad400a9STejun Heo cpb->lbal = qc->tf.lbal; 4953ad400a9STejun Heo cpb->lbam = qc->tf.lbam; 4963ad400a9STejun Heo cpb->lbah = qc->tf.lbah; 4973ad400a9STejun Heo 4983ad400a9STejun Heo if (qc->tf.flags & ATA_TFLAG_LBA48) { 4993ad400a9STejun Heo cpb->hob_feature = qc->tf.hob_feature; 5003ad400a9STejun Heo cpb->hob_nsect = qc->tf.hob_nsect; 5013ad400a9STejun Heo cpb->hob_lbal = qc->tf.hob_lbal; 5023ad400a9STejun Heo cpb->hob_lbam = qc->tf.hob_lbam; 5033ad400a9STejun Heo cpb->hob_lbah = qc->tf.hob_lbah; 5043ad400a9STejun Heo } 5053ad400a9STejun Heo 5063ad400a9STejun Heo cpb->command = qc->tf.command; 5073ad400a9STejun Heo /* don't load ctl - dunno why. it's like that in the initio driver */ 5083ad400a9STejun Heo 509b3f677e5STejun Heo /* setup PRD for CDB */ 510b3f677e5STejun Heo if (is_atapi) { 511b3f677e5STejun Heo memcpy(pkt->cdb, qc->cdb, ATAPI_CDB_LEN); 512b3f677e5STejun Heo prd->mad = cpu_to_le32(pp->pkt_dma + 513b3f677e5STejun Heo offsetof(struct inic_pkt, cdb)); 514b3f677e5STejun Heo prd->len = cpu_to_le16(cdb_len); 515b3f677e5STejun Heo prd->flags = PRD_CDB | PRD_WRITE; 516b3f677e5STejun Heo if (!is_data) 517b3f677e5STejun Heo prd->flags |= PRD_END; 518b3f677e5STejun Heo prd++; 519b3f677e5STejun Heo } 520b3f677e5STejun Heo 5213ad400a9STejun Heo /* setup sg table */ 522049e8e04STejun Heo if (is_data) 5233ad400a9STejun Heo inic_fill_sg(prd, qc); 5243ad400a9STejun Heo 5253ad400a9STejun Heo pp->cpb_tbl[0] = pp->pkt_dma; 5263ad400a9STejun Heo } 5273ad400a9STejun Heo 5281fd7a697STejun Heo static unsigned int inic_qc_issue(struct ata_queued_cmd *qc) 5291fd7a697STejun Heo { 5301fd7a697STejun Heo struct ata_port *ap = qc->ap; 5313ad400a9STejun Heo void __iomem *port_base = inic_port_base(ap); 5321fd7a697STejun Heo 5333ad400a9STejun Heo /* fire up the ADMA engine */ 53499580664SBob Stewart writew(HCTL_FTHD0 | HCTL_LEDEN, port_base + HOST_CTL); 5353ad400a9STejun Heo writew(IDMA_CTL_GO, port_base + PORT_IDMA_CTL); 5363ad400a9STejun Heo writeb(0, port_base + PORT_CPB_PTQFIFO); 5373ad400a9STejun Heo 5383ad400a9STejun Heo return 0; 5393ad400a9STejun Heo } 5401fd7a697STejun Heo 541364fac0eSTejun Heo static void inic_tf_read(struct ata_port *ap, struct ata_taskfile *tf) 542364fac0eSTejun Heo { 543364fac0eSTejun Heo void __iomem *port_base = inic_port_base(ap); 544364fac0eSTejun Heo 545364fac0eSTejun Heo tf->feature = readb(port_base + PORT_TF_FEATURE); 546364fac0eSTejun Heo tf->nsect = readb(port_base + PORT_TF_NSECT); 547364fac0eSTejun Heo tf->lbal = readb(port_base + PORT_TF_LBAL); 548364fac0eSTejun Heo tf->lbam = readb(port_base + PORT_TF_LBAM); 549364fac0eSTejun Heo tf->lbah = readb(port_base + PORT_TF_LBAH); 550364fac0eSTejun Heo tf->device = readb(port_base + PORT_TF_DEVICE); 551364fac0eSTejun Heo tf->command = readb(port_base + PORT_TF_COMMAND); 552364fac0eSTejun Heo } 553364fac0eSTejun Heo 554364fac0eSTejun Heo static bool inic_qc_fill_rtf(struct ata_queued_cmd *qc) 555364fac0eSTejun Heo { 556364fac0eSTejun Heo struct ata_taskfile *rtf = &qc->result_tf; 557364fac0eSTejun Heo struct ata_taskfile tf; 558364fac0eSTejun Heo 559364fac0eSTejun Heo /* FIXME: Except for status and error, result TF access 560364fac0eSTejun Heo * doesn't work. I tried reading from BAR0/2, CPB and BAR5. 561364fac0eSTejun Heo * None works regardless of which command interface is used. 562364fac0eSTejun Heo * For now return true iff status indicates device error. 563364fac0eSTejun Heo * This means that we're reporting bogus sector for RW 564364fac0eSTejun Heo * failures. Eeekk.... 565364fac0eSTejun Heo */ 566364fac0eSTejun Heo inic_tf_read(qc->ap, &tf); 567364fac0eSTejun Heo 568364fac0eSTejun Heo if (!(tf.command & ATA_ERR)) 569364fac0eSTejun Heo return false; 570364fac0eSTejun Heo 571364fac0eSTejun Heo rtf->command = tf.command; 572364fac0eSTejun Heo rtf->feature = tf.feature; 573364fac0eSTejun Heo return true; 574364fac0eSTejun Heo } 575364fac0eSTejun Heo 5761fd7a697STejun Heo static void inic_freeze(struct ata_port *ap) 5771fd7a697STejun Heo { 5781fd7a697STejun Heo void __iomem *port_base = inic_port_base(ap); 5791fd7a697STejun Heo 580ab5b0235STejun Heo writeb(PIRQ_MASK_FREEZE, port_base + PORT_IRQ_MASK); 5811fd7a697STejun Heo writeb(0xff, port_base + PORT_IRQ_STAT); 5821fd7a697STejun Heo } 5831fd7a697STejun Heo 5841fd7a697STejun Heo static void inic_thaw(struct ata_port *ap) 5851fd7a697STejun Heo { 5861fd7a697STejun Heo void __iomem *port_base = inic_port_base(ap); 5871fd7a697STejun Heo 5881fd7a697STejun Heo writeb(0xff, port_base + PORT_IRQ_STAT); 589ab5b0235STejun Heo writeb(PIRQ_MASK_DEFAULT, port_base + PORT_IRQ_MASK); 5901fd7a697STejun Heo } 5911fd7a697STejun Heo 592364fac0eSTejun Heo static int inic_check_ready(struct ata_link *link) 593364fac0eSTejun Heo { 594364fac0eSTejun Heo void __iomem *port_base = inic_port_base(link->ap); 595364fac0eSTejun Heo 596364fac0eSTejun Heo return ata_check_ready(readb(port_base + PORT_TF_COMMAND)); 597364fac0eSTejun Heo } 598364fac0eSTejun Heo 5991fd7a697STejun Heo /* 6001fd7a697STejun Heo * SRST and SControl hardreset don't give valid signature on this 6011fd7a697STejun Heo * controller. Only controller specific hardreset mechanism works. 6021fd7a697STejun Heo */ 603cc0680a5STejun Heo static int inic_hardreset(struct ata_link *link, unsigned int *class, 604d4b2bab4STejun Heo unsigned long deadline) 6051fd7a697STejun Heo { 606cc0680a5STejun Heo struct ata_port *ap = link->ap; 6071fd7a697STejun Heo void __iomem *port_base = inic_port_base(ap); 6081fd7a697STejun Heo void __iomem *idma_ctl = port_base + PORT_IDMA_CTL; 609cc0680a5STejun Heo const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context); 6101fd7a697STejun Heo int rc; 6111fd7a697STejun Heo 6121fd7a697STejun Heo /* hammer it into sane state */ 6131fd7a697STejun Heo inic_reset_port(port_base); 6141fd7a697STejun Heo 615f8b0685aSTejun Heo writew(IDMA_CTL_RST_ATA, idma_ctl); 6161fd7a697STejun Heo readw(idma_ctl); /* flush */ 617*97750cebSTejun Heo ata_msleep(ap, 1); 618f8b0685aSTejun Heo writew(0, idma_ctl); 6191fd7a697STejun Heo 620cc0680a5STejun Heo rc = sata_link_resume(link, timing, deadline); 6211fd7a697STejun Heo if (rc) { 622cc0680a5STejun Heo ata_link_printk(link, KERN_WARNING, "failed to resume " 623fe334602STejun Heo "link after reset (errno=%d)\n", rc); 6241fd7a697STejun Heo return rc; 6251fd7a697STejun Heo } 6261fd7a697STejun Heo 6271fd7a697STejun Heo *class = ATA_DEV_NONE; 628cc0680a5STejun Heo if (ata_link_online(link)) { 6291fd7a697STejun Heo struct ata_taskfile tf; 6301fd7a697STejun Heo 631705e76beSTejun Heo /* wait for link to become ready */ 632364fac0eSTejun Heo rc = ata_wait_after_reset(link, deadline, inic_check_ready); 6339b89391cSTejun Heo /* link occupied, -ENODEV too is an error */ 6349b89391cSTejun Heo if (rc) { 635cc0680a5STejun Heo ata_link_printk(link, KERN_WARNING, "device not ready " 636d4b2bab4STejun Heo "after hardreset (errno=%d)\n", rc); 637d4b2bab4STejun Heo return rc; 6381fd7a697STejun Heo } 6391fd7a697STejun Heo 640364fac0eSTejun Heo inic_tf_read(ap, &tf); 6411fd7a697STejun Heo *class = ata_dev_classify(&tf); 6421fd7a697STejun Heo } 6431fd7a697STejun Heo 6441fd7a697STejun Heo return 0; 6451fd7a697STejun Heo } 6461fd7a697STejun Heo 6471fd7a697STejun Heo static void inic_error_handler(struct ata_port *ap) 6481fd7a697STejun Heo { 6491fd7a697STejun Heo void __iomem *port_base = inic_port_base(ap); 6501fd7a697STejun Heo 6511fd7a697STejun Heo inic_reset_port(port_base); 652a1efdabaSTejun Heo ata_std_error_handler(ap); 6531fd7a697STejun Heo } 6541fd7a697STejun Heo 6551fd7a697STejun Heo static void inic_post_internal_cmd(struct ata_queued_cmd *qc) 6561fd7a697STejun Heo { 6571fd7a697STejun Heo /* make DMA engine forget about the failed command */ 658a51d644aSTejun Heo if (qc->flags & ATA_QCFLAG_FAILED) 6591fd7a697STejun Heo inic_reset_port(inic_port_base(qc->ap)); 6601fd7a697STejun Heo } 6611fd7a697STejun Heo 6621fd7a697STejun Heo static void init_port(struct ata_port *ap) 6631fd7a697STejun Heo { 6641fd7a697STejun Heo void __iomem *port_base = inic_port_base(ap); 6653ad400a9STejun Heo struct inic_port_priv *pp = ap->private_data; 6661fd7a697STejun Heo 6673ad400a9STejun Heo /* clear packet and CPB table */ 6683ad400a9STejun Heo memset(pp->pkt, 0, sizeof(struct inic_pkt)); 6693ad400a9STejun Heo memset(pp->cpb_tbl, 0, IDMA_CPB_TBL_SIZE); 6703ad400a9STejun Heo 6716bc0d390STejun Heo /* setup CPB lookup table addresses */ 6723ad400a9STejun Heo writel(pp->cpb_tbl_dma, port_base + PORT_CPB_CPBLAR); 6731fd7a697STejun Heo } 6741fd7a697STejun Heo 6751fd7a697STejun Heo static int inic_port_resume(struct ata_port *ap) 6761fd7a697STejun Heo { 6771fd7a697STejun Heo init_port(ap); 6781fd7a697STejun Heo return 0; 6791fd7a697STejun Heo } 6801fd7a697STejun Heo 6811fd7a697STejun Heo static int inic_port_start(struct ata_port *ap) 6821fd7a697STejun Heo { 6833ad400a9STejun Heo struct device *dev = ap->host->dev; 6841fd7a697STejun Heo struct inic_port_priv *pp; 6851fd7a697STejun Heo 6861fd7a697STejun Heo /* alloc and initialize private data */ 6873ad400a9STejun Heo pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL); 6881fd7a697STejun Heo if (!pp) 6891fd7a697STejun Heo return -ENOMEM; 6901fd7a697STejun Heo ap->private_data = pp; 6911fd7a697STejun Heo 6921fd7a697STejun Heo /* Alloc resources */ 6933ad400a9STejun Heo pp->pkt = dmam_alloc_coherent(dev, sizeof(struct inic_pkt), 6943ad400a9STejun Heo &pp->pkt_dma, GFP_KERNEL); 6953ad400a9STejun Heo if (!pp->pkt) 6963ad400a9STejun Heo return -ENOMEM; 6973ad400a9STejun Heo 6983ad400a9STejun Heo pp->cpb_tbl = dmam_alloc_coherent(dev, IDMA_CPB_TBL_SIZE, 6993ad400a9STejun Heo &pp->cpb_tbl_dma, GFP_KERNEL); 7003ad400a9STejun Heo if (!pp->cpb_tbl) 7013ad400a9STejun Heo return -ENOMEM; 7023ad400a9STejun Heo 7031fd7a697STejun Heo init_port(ap); 7041fd7a697STejun Heo 7051fd7a697STejun Heo return 0; 7061fd7a697STejun Heo } 7071fd7a697STejun Heo 7081fd7a697STejun Heo static struct ata_port_operations inic_port_ops = { 709f8b0685aSTejun Heo .inherits = &sata_port_ops, 7101fd7a697STejun Heo 711b3f677e5STejun Heo .check_atapi_dma = inic_check_atapi_dma, 7123ad400a9STejun Heo .qc_prep = inic_qc_prep, 7131fd7a697STejun Heo .qc_issue = inic_qc_issue, 714364fac0eSTejun Heo .qc_fill_rtf = inic_qc_fill_rtf, 7151fd7a697STejun Heo 7161fd7a697STejun Heo .freeze = inic_freeze, 7171fd7a697STejun Heo .thaw = inic_thaw, 718a1efdabaSTejun Heo .hardreset = inic_hardreset, 7191fd7a697STejun Heo .error_handler = inic_error_handler, 7201fd7a697STejun Heo .post_internal_cmd = inic_post_internal_cmd, 7211fd7a697STejun Heo 722029cfd6bSTejun Heo .scr_read = inic_scr_read, 723029cfd6bSTejun Heo .scr_write = inic_scr_write, 7241fd7a697STejun Heo 725029cfd6bSTejun Heo .port_resume = inic_port_resume, 7261fd7a697STejun Heo .port_start = inic_port_start, 7271fd7a697STejun Heo }; 7281fd7a697STejun Heo 7291fd7a697STejun Heo static struct ata_port_info inic_port_info = { 7301fd7a697STejun Heo .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA, 73114bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 73214bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 733bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6, 7341fd7a697STejun Heo .port_ops = &inic_port_ops 7351fd7a697STejun Heo }; 7361fd7a697STejun Heo 7371fd7a697STejun Heo static int init_controller(void __iomem *mmio_base, u16 hctl) 7381fd7a697STejun Heo { 7391fd7a697STejun Heo int i; 7401fd7a697STejun Heo u16 val; 7411fd7a697STejun Heo 7421fd7a697STejun Heo hctl &= ~HCTL_KNOWN_BITS; 7431fd7a697STejun Heo 7441fd7a697STejun Heo /* Soft reset whole controller. Spec says reset duration is 3 7451fd7a697STejun Heo * PCI clocks, be generous and give it 10ms. 7461fd7a697STejun Heo */ 7471fd7a697STejun Heo writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL); 7481fd7a697STejun Heo readw(mmio_base + HOST_CTL); /* flush */ 7491fd7a697STejun Heo 7501fd7a697STejun Heo for (i = 0; i < 10; i++) { 7511fd7a697STejun Heo msleep(1); 7521fd7a697STejun Heo val = readw(mmio_base + HOST_CTL); 7531fd7a697STejun Heo if (!(val & HCTL_SOFTRST)) 7541fd7a697STejun Heo break; 7551fd7a697STejun Heo } 7561fd7a697STejun Heo 7571fd7a697STejun Heo if (val & HCTL_SOFTRST) 7581fd7a697STejun Heo return -EIO; 7591fd7a697STejun Heo 7601fd7a697STejun Heo /* mask all interrupts and reset ports */ 7611fd7a697STejun Heo for (i = 0; i < NR_PORTS; i++) { 7621fd7a697STejun Heo void __iomem *port_base = mmio_base + i * PORT_SIZE; 7631fd7a697STejun Heo 7641fd7a697STejun Heo writeb(0xff, port_base + PORT_IRQ_MASK); 7651fd7a697STejun Heo inic_reset_port(port_base); 7661fd7a697STejun Heo } 7671fd7a697STejun Heo 7681fd7a697STejun Heo /* port IRQ is masked now, unmask global IRQ */ 7691fd7a697STejun Heo writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL); 7701fd7a697STejun Heo val = readw(mmio_base + HOST_IRQ_MASK); 7711fd7a697STejun Heo val &= ~(HIRQ_PORT0 | HIRQ_PORT1); 7721fd7a697STejun Heo writew(val, mmio_base + HOST_IRQ_MASK); 7731fd7a697STejun Heo 7741fd7a697STejun Heo return 0; 7751fd7a697STejun Heo } 7761fd7a697STejun Heo 777438ac6d5STejun Heo #ifdef CONFIG_PM 7781fd7a697STejun Heo static int inic_pci_device_resume(struct pci_dev *pdev) 7791fd7a697STejun Heo { 7801fd7a697STejun Heo struct ata_host *host = dev_get_drvdata(&pdev->dev); 7811fd7a697STejun Heo struct inic_host_priv *hpriv = host->private_data; 7821fd7a697STejun Heo int rc; 7831fd7a697STejun Heo 7845aea408dSDmitriy Monakhov rc = ata_pci_device_do_resume(pdev); 7855aea408dSDmitriy Monakhov if (rc) 7865aea408dSDmitriy Monakhov return rc; 7871fd7a697STejun Heo 7881fd7a697STejun Heo if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) { 789ba66b242STejun Heo rc = init_controller(hpriv->mmio_base, hpriv->cached_hctl); 7901fd7a697STejun Heo if (rc) 7911fd7a697STejun Heo return rc; 7921fd7a697STejun Heo } 7931fd7a697STejun Heo 7941fd7a697STejun Heo ata_host_resume(host); 7951fd7a697STejun Heo 7961fd7a697STejun Heo return 0; 7971fd7a697STejun Heo } 798438ac6d5STejun Heo #endif 7991fd7a697STejun Heo 8001fd7a697STejun Heo static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 8011fd7a697STejun Heo { 8021fd7a697STejun Heo static int printed_version; 8034447d351STejun Heo const struct ata_port_info *ppi[] = { &inic_port_info, NULL }; 8044447d351STejun Heo struct ata_host *host; 8051fd7a697STejun Heo struct inic_host_priv *hpriv; 8060d5ff566STejun Heo void __iomem * const *iomap; 807ba66b242STejun Heo int mmio_bar; 8081fd7a697STejun Heo int i, rc; 8091fd7a697STejun Heo 8101fd7a697STejun Heo if (!printed_version++) 8111fd7a697STejun Heo dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n"); 8121fd7a697STejun Heo 8134447d351STejun Heo /* alloc host */ 8144447d351STejun Heo host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS); 8154447d351STejun Heo hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL); 8164447d351STejun Heo if (!host || !hpriv) 8174447d351STejun Heo return -ENOMEM; 8184447d351STejun Heo 8194447d351STejun Heo host->private_data = hpriv; 8204447d351STejun Heo 821ba66b242STejun Heo /* Acquire resources and fill host. Note that PCI and cardbus 822ba66b242STejun Heo * use different BARs. 823ba66b242STejun Heo */ 82424dc5f33STejun Heo rc = pcim_enable_device(pdev); 8251fd7a697STejun Heo if (rc) 8261fd7a697STejun Heo return rc; 8271fd7a697STejun Heo 828ba66b242STejun Heo if (pci_resource_flags(pdev, MMIO_BAR_PCI) & IORESOURCE_MEM) 829ba66b242STejun Heo mmio_bar = MMIO_BAR_PCI; 830ba66b242STejun Heo else 831ba66b242STejun Heo mmio_bar = MMIO_BAR_CARDBUS; 832ba66b242STejun Heo 833ba66b242STejun Heo rc = pcim_iomap_regions(pdev, 1 << mmio_bar, DRV_NAME); 8340d5ff566STejun Heo if (rc) 8350d5ff566STejun Heo return rc; 8364447d351STejun Heo host->iomap = iomap = pcim_iomap_table(pdev); 837ba66b242STejun Heo hpriv->mmio_base = iomap[mmio_bar]; 838ba66b242STejun Heo hpriv->cached_hctl = readw(hpriv->mmio_base + HOST_CTL); 8394447d351STejun Heo 8404447d351STejun Heo for (i = 0; i < NR_PORTS; i++) { 841cbcdd875STejun Heo struct ata_port *ap = host->ports[i]; 842cbcdd875STejun Heo 843ba66b242STejun Heo ata_port_pbar_desc(ap, mmio_bar, -1, "mmio"); 844ba66b242STejun Heo ata_port_pbar_desc(ap, mmio_bar, i * PORT_SIZE, "port"); 8454447d351STejun Heo } 8464447d351STejun Heo 8471fd7a697STejun Heo /* Set dma_mask. This devices doesn't support 64bit addressing. */ 848284901a9SYang Hongyang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 8491fd7a697STejun Heo if (rc) { 8501fd7a697STejun Heo dev_printk(KERN_ERR, &pdev->dev, 8511fd7a697STejun Heo "32-bit DMA enable failed\n"); 85224dc5f33STejun Heo return rc; 8531fd7a697STejun Heo } 8541fd7a697STejun Heo 855284901a9SYang Hongyang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 8561fd7a697STejun Heo if (rc) { 8571fd7a697STejun Heo dev_printk(KERN_ERR, &pdev->dev, 8581fd7a697STejun Heo "32-bit consistent DMA enable failed\n"); 85924dc5f33STejun Heo return rc; 8601fd7a697STejun Heo } 8611fd7a697STejun Heo 862b7d8629fSFUJITA Tomonori /* 863b7d8629fSFUJITA Tomonori * This controller is braindamaged. dma_boundary is 0xffff 864b7d8629fSFUJITA Tomonori * like others but it will lock up the whole machine HARD if 865b7d8629fSFUJITA Tomonori * 65536 byte PRD entry is fed. Reduce maximum segment size. 866b7d8629fSFUJITA Tomonori */ 867b7d8629fSFUJITA Tomonori rc = pci_set_dma_max_seg_size(pdev, 65536 - 512); 868b7d8629fSFUJITA Tomonori if (rc) { 869b7d8629fSFUJITA Tomonori dev_printk(KERN_ERR, &pdev->dev, 870b7d8629fSFUJITA Tomonori "failed to set the maximum segment size.\n"); 871b7d8629fSFUJITA Tomonori return rc; 872b7d8629fSFUJITA Tomonori } 873b7d8629fSFUJITA Tomonori 874ba66b242STejun Heo rc = init_controller(hpriv->mmio_base, hpriv->cached_hctl); 8751fd7a697STejun Heo if (rc) { 8761fd7a697STejun Heo dev_printk(KERN_ERR, &pdev->dev, 8771fd7a697STejun Heo "failed to initialize controller\n"); 87824dc5f33STejun Heo return rc; 8791fd7a697STejun Heo } 8801fd7a697STejun Heo 8811fd7a697STejun Heo pci_set_master(pdev); 8824447d351STejun Heo return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED, 8834447d351STejun Heo &inic_sht); 8841fd7a697STejun Heo } 8851fd7a697STejun Heo 8861fd7a697STejun Heo static const struct pci_device_id inic_pci_tbl[] = { 8871fd7a697STejun Heo { PCI_VDEVICE(INIT, 0x1622), }, 8881fd7a697STejun Heo { }, 8891fd7a697STejun Heo }; 8901fd7a697STejun Heo 8911fd7a697STejun Heo static struct pci_driver inic_pci_driver = { 8921fd7a697STejun Heo .name = DRV_NAME, 8931fd7a697STejun Heo .id_table = inic_pci_tbl, 894438ac6d5STejun Heo #ifdef CONFIG_PM 8951fd7a697STejun Heo .suspend = ata_pci_device_suspend, 8961fd7a697STejun Heo .resume = inic_pci_device_resume, 897438ac6d5STejun Heo #endif 8981fd7a697STejun Heo .probe = inic_init_one, 8991fd7a697STejun Heo .remove = ata_pci_remove_one, 9001fd7a697STejun Heo }; 9011fd7a697STejun Heo 9021fd7a697STejun Heo static int __init inic_init(void) 9031fd7a697STejun Heo { 9041fd7a697STejun Heo return pci_register_driver(&inic_pci_driver); 9051fd7a697STejun Heo } 9061fd7a697STejun Heo 9071fd7a697STejun Heo static void __exit inic_exit(void) 9081fd7a697STejun Heo { 9091fd7a697STejun Heo pci_unregister_driver(&inic_pci_driver); 9101fd7a697STejun Heo } 9111fd7a697STejun Heo 9121fd7a697STejun Heo MODULE_AUTHOR("Tejun Heo"); 9131fd7a697STejun Heo MODULE_DESCRIPTION("low-level driver for Initio 162x SATA"); 9141fd7a697STejun Heo MODULE_LICENSE("GPL v2"); 9151fd7a697STejun Heo MODULE_DEVICE_TABLE(pci, inic_pci_tbl); 9161fd7a697STejun Heo MODULE_VERSION(DRV_VERSION); 9171fd7a697STejun Heo 9181fd7a697STejun Heo module_init(inic_init); 9191fd7a697STejun Heo module_exit(inic_exit); 920