11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
281d01bfaSLoc Ho /*
381d01bfaSLoc Ho * AppliedMicro X-Gene SoC SATA Host Controller Driver
481d01bfaSLoc Ho *
581d01bfaSLoc Ho * Copyright (c) 2014, Applied Micro Circuits Corporation
681d01bfaSLoc Ho * Author: Loc Ho <lho@apm.com>
781d01bfaSLoc Ho * Tuan Phan <tphan@apm.com>
881d01bfaSLoc Ho * Suman Tripathi <stripathi@apm.com>
981d01bfaSLoc Ho *
1081d01bfaSLoc Ho * NOTE: PM support is not currently available.
1181d01bfaSLoc Ho */
1292b5bf98SFeng Kan #include <linux/acpi.h>
1381d01bfaSLoc Ho #include <linux/module.h>
1481d01bfaSLoc Ho #include <linux/platform_device.h>
1581d01bfaSLoc Ho #include <linux/ahci_platform.h>
1681d01bfaSLoc Ho #include <linux/of_address.h>
17c9802a4bSSuman Tripathi #include <linux/of_device.h>
1881d01bfaSLoc Ho #include <linux/of_irq.h>
1981d01bfaSLoc Ho #include <linux/phy/phy.h>
2081d01bfaSLoc Ho #include "ahci.h"
2181d01bfaSLoc Ho
22018d5ef2SAkinobu Mita #define DRV_NAME "xgene-ahci"
23018d5ef2SAkinobu Mita
2481d01bfaSLoc Ho /* Max # of disk per a controller */
2581d01bfaSLoc Ho #define MAX_AHCI_CHN_PERCTR 2
2681d01bfaSLoc Ho
2781d01bfaSLoc Ho /* MUX CSR */
2881d01bfaSLoc Ho #define SATA_ENET_CONFIG_REG 0x00000000
2981d01bfaSLoc Ho #define CFG_SATA_ENET_SELECT_MASK 0x00000001
3081d01bfaSLoc Ho
3181d01bfaSLoc Ho /* SATA core host controller CSR */
3281d01bfaSLoc Ho #define SLVRDERRATTRIBUTES 0x00000000
3381d01bfaSLoc Ho #define SLVWRERRATTRIBUTES 0x00000004
3481d01bfaSLoc Ho #define MSTRDERRATTRIBUTES 0x00000008
3581d01bfaSLoc Ho #define MSTWRERRATTRIBUTES 0x0000000c
3681d01bfaSLoc Ho #define BUSCTLREG 0x00000014
3781d01bfaSLoc Ho #define IOFMSTRWAUX 0x00000018
3881d01bfaSLoc Ho #define INTSTATUSMASK 0x0000002c
3981d01bfaSLoc Ho #define ERRINTSTATUS 0x00000030
4081d01bfaSLoc Ho #define ERRINTSTATUSMASK 0x00000034
4181d01bfaSLoc Ho
4281d01bfaSLoc Ho /* SATA host AHCI CSR */
4381d01bfaSLoc Ho #define PORTCFG 0x000000a4
4481d01bfaSLoc Ho #define PORTADDR_SET(dst, src) \
4581d01bfaSLoc Ho (((dst) & ~0x0000003f) | (((u32)(src)) & 0x0000003f))
4681d01bfaSLoc Ho #define PORTPHY1CFG 0x000000a8
4781d01bfaSLoc Ho #define PORTPHY1CFG_FRCPHYRDY_SET(dst, src) \
4881d01bfaSLoc Ho (((dst) & ~0x00100000) | (((u32)(src) << 0x14) & 0x00100000))
4981d01bfaSLoc Ho #define PORTPHY2CFG 0x000000ac
5081d01bfaSLoc Ho #define PORTPHY3CFG 0x000000b0
5181d01bfaSLoc Ho #define PORTPHY4CFG 0x000000b4
5281d01bfaSLoc Ho #define PORTPHY5CFG 0x000000b8
5381d01bfaSLoc Ho #define SCTL0 0x0000012C
5481d01bfaSLoc Ho #define PORTPHY5CFG_RTCHG_SET(dst, src) \
5581d01bfaSLoc Ho (((dst) & ~0xfff00000) | (((u32)(src) << 0x14) & 0xfff00000))
5681d01bfaSLoc Ho #define PORTAXICFG_EN_CONTEXT_SET(dst, src) \
5781d01bfaSLoc Ho (((dst) & ~0x01000000) | (((u32)(src) << 0x18) & 0x01000000))
5881d01bfaSLoc Ho #define PORTAXICFG 0x000000bc
5981d01bfaSLoc Ho #define PORTAXICFG_OUTTRANS_SET(dst, src) \
6081d01bfaSLoc Ho (((dst) & ~0x00f00000) | (((u32)(src) << 0x14) & 0x00f00000))
61aeae4dcaSSuman Tripathi #define PORTRANSCFG 0x000000c8
62aeae4dcaSSuman Tripathi #define PORTRANSCFG_RXWM_SET(dst, src) \
63aeae4dcaSSuman Tripathi (((dst) & ~0x0000007f) | (((u32)(src)) & 0x0000007f))
6481d01bfaSLoc Ho
6581d01bfaSLoc Ho /* SATA host controller AXI CSR */
6681d01bfaSLoc Ho #define INT_SLV_TMOMASK 0x00000010
6781d01bfaSLoc Ho
6881d01bfaSLoc Ho /* SATA diagnostic CSR */
6981d01bfaSLoc Ho #define CFG_MEM_RAM_SHUTDOWN 0x00000070
7081d01bfaSLoc Ho #define BLOCK_MEM_RDY 0x00000074
7181d01bfaSLoc Ho
720babe614SSuman Tripathi /* Max retry for link down */
730babe614SSuman Tripathi #define MAX_LINK_DOWN_RETRY 3
740babe614SSuman Tripathi
75c9802a4bSSuman Tripathi enum xgene_ahci_version {
76c9802a4bSSuman Tripathi XGENE_AHCI_V1 = 1,
77c9802a4bSSuman Tripathi XGENE_AHCI_V2,
78c9802a4bSSuman Tripathi };
79c9802a4bSSuman Tripathi
8081d01bfaSLoc Ho struct xgene_ahci_context {
8181d01bfaSLoc Ho struct ahci_host_priv *hpriv;
8281d01bfaSLoc Ho struct device *dev;
832a0bdff6SSuman Tripathi u8 last_cmd[MAX_AHCI_CHN_PERCTR]; /* tracking the last command issued*/
84a3a84bc7SSuman Tripathi u32 class[MAX_AHCI_CHN_PERCTR]; /* tracking the class of device */
8581d01bfaSLoc Ho void __iomem *csr_core; /* Core CSR address of IP */
8681d01bfaSLoc Ho void __iomem *csr_diag; /* Diag CSR address of IP */
8781d01bfaSLoc Ho void __iomem *csr_axi; /* AXI CSR address of IP */
8881d01bfaSLoc Ho void __iomem *csr_mux; /* MUX CSR address of IP */
8981d01bfaSLoc Ho };
9081d01bfaSLoc Ho
xgene_ahci_init_memram(struct xgene_ahci_context * ctx)9181d01bfaSLoc Ho static int xgene_ahci_init_memram(struct xgene_ahci_context *ctx)
9281d01bfaSLoc Ho {
9381d01bfaSLoc Ho dev_dbg(ctx->dev, "Release memory from shutdown\n");
9481d01bfaSLoc Ho writel(0x0, ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN);
9581d01bfaSLoc Ho readl(ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN); /* Force a barrier */
9681d01bfaSLoc Ho msleep(1); /* reset may take up to 1ms */
9781d01bfaSLoc Ho if (readl(ctx->csr_diag + BLOCK_MEM_RDY) != 0xFFFFFFFF) {
9881d01bfaSLoc Ho dev_err(ctx->dev, "failed to release memory from shutdown\n");
9981d01bfaSLoc Ho return -ENODEV;
10081d01bfaSLoc Ho }
10181d01bfaSLoc Ho return 0;
10281d01bfaSLoc Ho }
10381d01bfaSLoc Ho
10481d01bfaSLoc Ho /**
1051540035dSSuman Tripathi * xgene_ahci_poll_reg_val- Poll a register on a specific value.
1061540035dSSuman Tripathi * @ap : ATA port of interest.
1071540035dSSuman Tripathi * @reg : Register of interest.
1081540035dSSuman Tripathi * @val : Value to be attained.
1091540035dSSuman Tripathi * @interval : waiting interval for polling.
1101540035dSSuman Tripathi * @timeout : timeout for achieving the value.
1111540035dSSuman Tripathi */
xgene_ahci_poll_reg_val(struct ata_port * ap,void __iomem * reg,unsigned int val,unsigned int interval,unsigned int timeout)1121540035dSSuman Tripathi static int xgene_ahci_poll_reg_val(struct ata_port *ap,
113*6da99acbSSergey Shtylyov void __iomem *reg, unsigned int val,
114*6da99acbSSergey Shtylyov unsigned int interval, unsigned int timeout)
1151540035dSSuman Tripathi {
1161540035dSSuman Tripathi unsigned long deadline;
1171540035dSSuman Tripathi unsigned int tmp;
1181540035dSSuman Tripathi
1191540035dSSuman Tripathi tmp = ioread32(reg);
1201540035dSSuman Tripathi deadline = ata_deadline(jiffies, timeout);
1211540035dSSuman Tripathi
1221540035dSSuman Tripathi while (tmp != val && time_before(jiffies, deadline)) {
1231540035dSSuman Tripathi ata_msleep(ap, interval);
1241540035dSSuman Tripathi tmp = ioread32(reg);
1251540035dSSuman Tripathi }
1261540035dSSuman Tripathi
1271540035dSSuman Tripathi return tmp;
1281540035dSSuman Tripathi }
1291540035dSSuman Tripathi
1301540035dSSuman Tripathi /**
1312a0bdff6SSuman Tripathi * xgene_ahci_restart_engine - Restart the dma engine.
1322a0bdff6SSuman Tripathi * @ap : ATA port of interest
1332a0bdff6SSuman Tripathi *
1341540035dSSuman Tripathi * Waits for completion of multiple commands and restarts
1351540035dSSuman Tripathi * the DMA engine inside the controller.
1362a0bdff6SSuman Tripathi */
xgene_ahci_restart_engine(struct ata_port * ap)1372a0bdff6SSuman Tripathi static int xgene_ahci_restart_engine(struct ata_port *ap)
1382a0bdff6SSuman Tripathi {
1392a0bdff6SSuman Tripathi struct ahci_host_priv *hpriv = ap->host->private_data;
1401540035dSSuman Tripathi struct ahci_port_priv *pp = ap->private_data;
1411540035dSSuman Tripathi void __iomem *port_mmio = ahci_port_base(ap);
1421540035dSSuman Tripathi u32 fbs;
1431540035dSSuman Tripathi
1441540035dSSuman Tripathi /*
1451540035dSSuman Tripathi * In case of PMP multiple IDENTIFY DEVICE commands can be
1461540035dSSuman Tripathi * issued inside PxCI. So need to poll PxCI for the
1471540035dSSuman Tripathi * completion of outstanding IDENTIFY DEVICE commands before
1481540035dSSuman Tripathi * we restart the DMA engine.
1491540035dSSuman Tripathi */
1501540035dSSuman Tripathi if (xgene_ahci_poll_reg_val(ap, port_mmio +
1511540035dSSuman Tripathi PORT_CMD_ISSUE, 0x0, 1, 100))
1521540035dSSuman Tripathi return -EBUSY;
1532a0bdff6SSuman Tripathi
154fa89f53bSEvan Wang hpriv->stop_engine(ap);
1552a0bdff6SSuman Tripathi ahci_start_fis_rx(ap);
1561540035dSSuman Tripathi
1571540035dSSuman Tripathi /*
1581540035dSSuman Tripathi * Enable the PxFBS.FBS_EN bit as it
1591540035dSSuman Tripathi * gets cleared due to stopping the engine.
1601540035dSSuman Tripathi */
1611540035dSSuman Tripathi if (pp->fbs_supported) {
1621540035dSSuman Tripathi fbs = readl(port_mmio + PORT_FBS);
1631540035dSSuman Tripathi writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
1641540035dSSuman Tripathi fbs = readl(port_mmio + PORT_FBS);
1651540035dSSuman Tripathi }
1661540035dSSuman Tripathi
1672a0bdff6SSuman Tripathi hpriv->start_engine(ap);
1682a0bdff6SSuman Tripathi
1692a0bdff6SSuman Tripathi return 0;
1702a0bdff6SSuman Tripathi }
1712a0bdff6SSuman Tripathi
1722a0bdff6SSuman Tripathi /**
1732a0bdff6SSuman Tripathi * xgene_ahci_qc_issue - Issue commands to the device
1742a0bdff6SSuman Tripathi * @qc: Command to issue
1752a0bdff6SSuman Tripathi *
176a3a84bc7SSuman Tripathi * Due to Hardware errata for IDENTIFY DEVICE command, the controller cannot
177a3a84bc7SSuman Tripathi * clear the BSY bit after receiving the PIO setup FIS. This results in the dma
178a3a84bc7SSuman Tripathi * state machine goes into the CMFatalErrorUpdate state and locks up. By
179a3a84bc7SSuman Tripathi * restarting the dma engine, it removes the controller out of lock up state.
180a3a84bc7SSuman Tripathi *
181a3a84bc7SSuman Tripathi * Due to H/W errata, the controller is unable to save the PMP
182a3a84bc7SSuman Tripathi * field fetched from command header before sending the H2D FIS.
183a3a84bc7SSuman Tripathi * When the device returns the PMP port field in the D2H FIS, there is
184a3a84bc7SSuman Tripathi * a mismatch and results in command completion failure. The
185a3a84bc7SSuman Tripathi * workaround is to write the pmp value to PxFBS.DEV field before issuing
186a3a84bc7SSuman Tripathi * any command to PMP.
1872a0bdff6SSuman Tripathi */
xgene_ahci_qc_issue(struct ata_queued_cmd * qc)1882a0bdff6SSuman Tripathi static unsigned int xgene_ahci_qc_issue(struct ata_queued_cmd *qc)
1892a0bdff6SSuman Tripathi {
1902a0bdff6SSuman Tripathi struct ata_port *ap = qc->ap;
1912a0bdff6SSuman Tripathi struct ahci_host_priv *hpriv = ap->host->private_data;
1922a0bdff6SSuman Tripathi struct xgene_ahci_context *ctx = hpriv->plat_data;
1932a0bdff6SSuman Tripathi int rc = 0;
194a3a84bc7SSuman Tripathi u32 port_fbs;
1952bce6907SDamien Le Moal void __iomem *port_mmio = ahci_port_base(ap);
196a3a84bc7SSuman Tripathi
197a3a84bc7SSuman Tripathi /*
198a3a84bc7SSuman Tripathi * Write the pmp value to PxFBS.DEV
199a3a84bc7SSuman Tripathi * for case of Port Mulitplier.
200a3a84bc7SSuman Tripathi */
201a3a84bc7SSuman Tripathi if (ctx->class[ap->port_no] == ATA_DEV_PMP) {
202a3a84bc7SSuman Tripathi port_fbs = readl(port_mmio + PORT_FBS);
203a3a84bc7SSuman Tripathi port_fbs &= ~PORT_FBS_DEV_MASK;
204a3a84bc7SSuman Tripathi port_fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
205a3a84bc7SSuman Tripathi writel(port_fbs, port_mmio + PORT_FBS);
206a3a84bc7SSuman Tripathi }
2072a0bdff6SSuman Tripathi
2081102407bSSuman Tripathi if (unlikely((ctx->last_cmd[ap->port_no] == ATA_CMD_ID_ATA) ||
20909c32aaaSSuman Tripathi (ctx->last_cmd[ap->port_no] == ATA_CMD_PACKET) ||
21009c32aaaSSuman Tripathi (ctx->last_cmd[ap->port_no] == ATA_CMD_SMART)))
2112a0bdff6SSuman Tripathi xgene_ahci_restart_engine(ap);
2122a0bdff6SSuman Tripathi
2132a0bdff6SSuman Tripathi rc = ahci_qc_issue(qc);
2142a0bdff6SSuman Tripathi
2152a0bdff6SSuman Tripathi /* Save the last command issued */
2162a0bdff6SSuman Tripathi ctx->last_cmd[ap->port_no] = qc->tf.command;
2172a0bdff6SSuman Tripathi
2182a0bdff6SSuman Tripathi return rc;
2192a0bdff6SSuman Tripathi }
2202a0bdff6SSuman Tripathi
xgene_ahci_is_memram_inited(struct xgene_ahci_context * ctx)2210bed13beSSuman Tripathi static bool xgene_ahci_is_memram_inited(struct xgene_ahci_context *ctx)
2220bed13beSSuman Tripathi {
2230bed13beSSuman Tripathi void __iomem *diagcsr = ctx->csr_diag;
2240bed13beSSuman Tripathi
2250bed13beSSuman Tripathi return (readl(diagcsr + CFG_MEM_RAM_SHUTDOWN) == 0 &&
2260bed13beSSuman Tripathi readl(diagcsr + BLOCK_MEM_RDY) == 0xFFFFFFFF);
2270bed13beSSuman Tripathi }
2280bed13beSSuman Tripathi
2292a0bdff6SSuman Tripathi /**
23081d01bfaSLoc Ho * xgene_ahci_read_id - Read ID data from the specified device
23181d01bfaSLoc Ho * @dev: device
23281d01bfaSLoc Ho * @tf: proposed taskfile
23381d01bfaSLoc Ho * @id: data buffer
23481d01bfaSLoc Ho *
23581d01bfaSLoc Ho * This custom read ID function is required due to the fact that the HW
2362a0bdff6SSuman Tripathi * does not support DEVSLP.
23781d01bfaSLoc Ho */
xgene_ahci_read_id(struct ata_device * dev,struct ata_taskfile * tf,__le16 * id)23881d01bfaSLoc Ho static unsigned int xgene_ahci_read_id(struct ata_device *dev,
2390561e514SDamien Le Moal struct ata_taskfile *tf, __le16 *id)
24081d01bfaSLoc Ho {
24181d01bfaSLoc Ho u32 err_mask;
24281d01bfaSLoc Ho
24381d01bfaSLoc Ho err_mask = ata_do_dev_read_id(dev, tf, id);
24481d01bfaSLoc Ho if (err_mask)
24581d01bfaSLoc Ho return err_mask;
24681d01bfaSLoc Ho
24781d01bfaSLoc Ho /*
24881d01bfaSLoc Ho * Mask reserved area. Word78 spec of Link Power Management
24981d01bfaSLoc Ho * bit15-8: reserved
25081d01bfaSLoc Ho * bit7: NCQ autosence
25181d01bfaSLoc Ho * bit6: Software settings preservation supported
25281d01bfaSLoc Ho * bit5: reserved
25381d01bfaSLoc Ho * bit4: In-order sata delivery supported
25481d01bfaSLoc Ho * bit3: DIPM requests supported
25581d01bfaSLoc Ho * bit2: DMA Setup FIS Auto-Activate optimization supported
25681d01bfaSLoc Ho * bit1: DMA Setup FIX non-Zero buffer offsets supported
25781d01bfaSLoc Ho * bit0: Reserved
25881d01bfaSLoc Ho *
25981d01bfaSLoc Ho * Clear reserved bit 8 (DEVSLP bit) as we don't support DEVSLP
26081d01bfaSLoc Ho */
2615c0b8e0dSSuman Tripathi id[ATA_ID_FEATURE_SUPP] &= cpu_to_le16(~(1 << 8));
26281d01bfaSLoc Ho
26381d01bfaSLoc Ho return 0;
26481d01bfaSLoc Ho }
26581d01bfaSLoc Ho
xgene_ahci_set_phy_cfg(struct xgene_ahci_context * ctx,int channel)26681d01bfaSLoc Ho static void xgene_ahci_set_phy_cfg(struct xgene_ahci_context *ctx, int channel)
26781d01bfaSLoc Ho {
26881d01bfaSLoc Ho void __iomem *mmio = ctx->hpriv->mmio;
26981d01bfaSLoc Ho u32 val;
27081d01bfaSLoc Ho
27181d01bfaSLoc Ho dev_dbg(ctx->dev, "port configure mmio 0x%p channel %d\n",
27281d01bfaSLoc Ho mmio, channel);
27381d01bfaSLoc Ho val = readl(mmio + PORTCFG);
27481d01bfaSLoc Ho val = PORTADDR_SET(val, channel == 0 ? 2 : 3);
27581d01bfaSLoc Ho writel(val, mmio + PORTCFG);
27681d01bfaSLoc Ho readl(mmio + PORTCFG); /* Force a barrier */
27781d01bfaSLoc Ho /* Disable fix rate */
27881d01bfaSLoc Ho writel(0x0001fffe, mmio + PORTPHY1CFG);
27981d01bfaSLoc Ho readl(mmio + PORTPHY1CFG); /* Force a barrier */
2800185b1b7SSuman Tripathi writel(0x28183219, mmio + PORTPHY2CFG);
28181d01bfaSLoc Ho readl(mmio + PORTPHY2CFG); /* Force a barrier */
2820185b1b7SSuman Tripathi writel(0x13081008, mmio + PORTPHY3CFG);
28381d01bfaSLoc Ho readl(mmio + PORTPHY3CFG); /* Force a barrier */
2840185b1b7SSuman Tripathi writel(0x00480815, mmio + PORTPHY4CFG);
28581d01bfaSLoc Ho readl(mmio + PORTPHY4CFG); /* Force a barrier */
28681d01bfaSLoc Ho /* Set window negotiation */
28781d01bfaSLoc Ho val = readl(mmio + PORTPHY5CFG);
28881d01bfaSLoc Ho val = PORTPHY5CFG_RTCHG_SET(val, 0x300);
28981d01bfaSLoc Ho writel(val, mmio + PORTPHY5CFG);
29081d01bfaSLoc Ho readl(mmio + PORTPHY5CFG); /* Force a barrier */
29181d01bfaSLoc Ho val = readl(mmio + PORTAXICFG);
29281d01bfaSLoc Ho val = PORTAXICFG_EN_CONTEXT_SET(val, 0x1); /* Enable context mgmt */
29381d01bfaSLoc Ho val = PORTAXICFG_OUTTRANS_SET(val, 0xe); /* Set outstanding */
29481d01bfaSLoc Ho writel(val, mmio + PORTAXICFG);
29581d01bfaSLoc Ho readl(mmio + PORTAXICFG); /* Force a barrier */
296aeae4dcaSSuman Tripathi /* Set the watermark threshold of the receive FIFO */
297aeae4dcaSSuman Tripathi val = readl(mmio + PORTRANSCFG);
298aeae4dcaSSuman Tripathi val = PORTRANSCFG_RXWM_SET(val, 0x30);
299aeae4dcaSSuman Tripathi writel(val, mmio + PORTRANSCFG);
30081d01bfaSLoc Ho }
30181d01bfaSLoc Ho
30281d01bfaSLoc Ho /**
30381d01bfaSLoc Ho * xgene_ahci_do_hardreset - Issue the actual COMRESET
30481d01bfaSLoc Ho * @link: link to reset
30581d01bfaSLoc Ho * @deadline: deadline jiffies for the operation
30681d01bfaSLoc Ho * @online: Return value to indicate if device online
30781d01bfaSLoc Ho *
30881d01bfaSLoc Ho * Due to the limitation of the hardware PHY, a difference set of setting is
30981d01bfaSLoc Ho * required for each supported disk speed - Gen3 (6.0Gbps), Gen2 (3.0Gbps),
31081d01bfaSLoc Ho * and Gen1 (1.5Gbps). Otherwise during long IO stress test, the PHY will
31181d01bfaSLoc Ho * report disparity error and etc. In addition, during COMRESET, there can
31281d01bfaSLoc Ho * be error reported in the register PORT_SCR_ERR. For SERR_DISPARITY and
3130babe614SSuman Tripathi * SERR_10B_8B_ERR, the PHY receiver line must be reseted. Also during long
3140babe614SSuman Tripathi * reboot cycle regression, sometimes the PHY reports link down even if the
3150babe614SSuman Tripathi * device is present because of speed negotiation failure. so need to retry
3160babe614SSuman Tripathi * the COMRESET to get the link up. The following algorithm is followed to
3170babe614SSuman Tripathi * proper configure the hardware PHY during COMRESET:
31881d01bfaSLoc Ho *
31981d01bfaSLoc Ho * Alg Part 1:
32081d01bfaSLoc Ho * 1. Start the PHY at Gen3 speed (default setting)
32181d01bfaSLoc Ho * 2. Issue the COMRESET
32281d01bfaSLoc Ho * 3. If no link, go to Alg Part 3
32381d01bfaSLoc Ho * 4. If link up, determine if the negotiated speed matches the PHY
32481d01bfaSLoc Ho * configured speed
32581d01bfaSLoc Ho * 5. If they matched, go to Alg Part 2
32681d01bfaSLoc Ho * 6. If they do not matched and first time, configure the PHY for the linked
32781d01bfaSLoc Ho * up disk speed and repeat step 2
32881d01bfaSLoc Ho * 7. Go to Alg Part 2
32981d01bfaSLoc Ho *
33081d01bfaSLoc Ho * Alg Part 2:
33181d01bfaSLoc Ho * 1. On link up, if there are any SERR_DISPARITY and SERR_10B_8B_ERR error
33281d01bfaSLoc Ho * reported in the register PORT_SCR_ERR, then reset the PHY receiver line
3330babe614SSuman Tripathi * 2. Go to Alg Part 4
33481d01bfaSLoc Ho *
33581d01bfaSLoc Ho * Alg Part 3:
3360babe614SSuman Tripathi * 1. Check the PORT_SCR_STAT to see whether device presence detected but PHY
3370babe614SSuman Tripathi * communication establishment failed and maximum link down attempts are
3380babe614SSuman Tripathi * less than Max attempts 3 then goto Alg Part 1.
3390babe614SSuman Tripathi * 2. Go to Alg Part 4.
3400babe614SSuman Tripathi *
3410babe614SSuman Tripathi * Alg Part 4:
34281d01bfaSLoc Ho * 1. Clear any pending from register PORT_SCR_ERR.
34381d01bfaSLoc Ho *
34481d01bfaSLoc Ho * NOTE: For the initial version, we will NOT support Gen1/Gen2. In addition
34581d01bfaSLoc Ho * and until the underlying PHY supports an method to reset the receiver
34681d01bfaSLoc Ho * line, on detection of SERR_DISPARITY or SERR_10B_8B_ERR errors,
34781d01bfaSLoc Ho * an warning message will be printed.
34881d01bfaSLoc Ho */
xgene_ahci_do_hardreset(struct ata_link * link,unsigned long deadline,bool * online)34981d01bfaSLoc Ho static int xgene_ahci_do_hardreset(struct ata_link *link,
35081d01bfaSLoc Ho unsigned long deadline, bool *online)
35181d01bfaSLoc Ho {
352d14d41ccSSergey Shtylyov const unsigned int *timing = sata_ehc_deb_timing(&link->eh_context);
35381d01bfaSLoc Ho struct ata_port *ap = link->ap;
35481d01bfaSLoc Ho struct ahci_host_priv *hpriv = ap->host->private_data;
35581d01bfaSLoc Ho struct xgene_ahci_context *ctx = hpriv->plat_data;
35681d01bfaSLoc Ho struct ahci_port_priv *pp = ap->private_data;
35781d01bfaSLoc Ho u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
35881d01bfaSLoc Ho void __iomem *port_mmio = ahci_port_base(ap);
35981d01bfaSLoc Ho struct ata_taskfile tf;
3600babe614SSuman Tripathi int link_down_retry = 0;
36181d01bfaSLoc Ho int rc;
3620babe614SSuman Tripathi u32 val, sstatus;
36381d01bfaSLoc Ho
3640babe614SSuman Tripathi do {
36581d01bfaSLoc Ho /* clear D2H reception area to properly wait for D2H FIS */
36681d01bfaSLoc Ho ata_tf_init(link->device, &tf);
367efcef265SSergey Shtylyov tf.status = ATA_BUSY;
36881d01bfaSLoc Ho ata_tf_to_fis(&tf, 0, 0, d2h_fis);
36981d01bfaSLoc Ho rc = sata_link_hardreset(link, timing, deadline, online,
37081d01bfaSLoc Ho ahci_check_ready);
3710babe614SSuman Tripathi if (*online) {
37281d01bfaSLoc Ho val = readl(port_mmio + PORT_SCR_ERR);
37381d01bfaSLoc Ho if (val & (SERR_DISPARITY | SERR_10B_8B_ERR))
37481d01bfaSLoc Ho dev_warn(ctx->dev, "link has error\n");
3750babe614SSuman Tripathi break;
3760babe614SSuman Tripathi }
3770babe614SSuman Tripathi
3780babe614SSuman Tripathi sata_scr_read(link, SCR_STATUS, &sstatus);
3790babe614SSuman Tripathi } while (link_down_retry++ < MAX_LINK_DOWN_RETRY &&
3800babe614SSuman Tripathi (sstatus & 0xff) == 0x1);
38181d01bfaSLoc Ho
38281d01bfaSLoc Ho /* clear all errors if any pending */
38381d01bfaSLoc Ho val = readl(port_mmio + PORT_SCR_ERR);
38481d01bfaSLoc Ho writel(val, port_mmio + PORT_SCR_ERR);
38581d01bfaSLoc Ho
38681d01bfaSLoc Ho return rc;
38781d01bfaSLoc Ho }
38881d01bfaSLoc Ho
xgene_ahci_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)38981d01bfaSLoc Ho static int xgene_ahci_hardreset(struct ata_link *link, unsigned int *class,
39081d01bfaSLoc Ho unsigned long deadline)
39181d01bfaSLoc Ho {
39281d01bfaSLoc Ho struct ata_port *ap = link->ap;
39381d01bfaSLoc Ho struct ahci_host_priv *hpriv = ap->host->private_data;
39481d01bfaSLoc Ho void __iomem *port_mmio = ahci_port_base(ap);
39581d01bfaSLoc Ho bool online;
39681d01bfaSLoc Ho int rc;
39781d01bfaSLoc Ho u32 portcmd_saved;
39881d01bfaSLoc Ho u32 portclb_saved;
39981d01bfaSLoc Ho u32 portclbhi_saved;
40081d01bfaSLoc Ho u32 portrxfis_saved;
40181d01bfaSLoc Ho u32 portrxfishi_saved;
40281d01bfaSLoc Ho
40381d01bfaSLoc Ho /* As hardreset resets these CSR, save it to restore later */
40481d01bfaSLoc Ho portcmd_saved = readl(port_mmio + PORT_CMD);
40581d01bfaSLoc Ho portclb_saved = readl(port_mmio + PORT_LST_ADDR);
40681d01bfaSLoc Ho portclbhi_saved = readl(port_mmio + PORT_LST_ADDR_HI);
40781d01bfaSLoc Ho portrxfis_saved = readl(port_mmio + PORT_FIS_ADDR);
40881d01bfaSLoc Ho portrxfishi_saved = readl(port_mmio + PORT_FIS_ADDR_HI);
40981d01bfaSLoc Ho
410fa89f53bSEvan Wang hpriv->stop_engine(ap);
41181d01bfaSLoc Ho
41281d01bfaSLoc Ho rc = xgene_ahci_do_hardreset(link, deadline, &online);
41381d01bfaSLoc Ho
41481d01bfaSLoc Ho /* As controller hardreset clears them, restore them */
41581d01bfaSLoc Ho writel(portcmd_saved, port_mmio + PORT_CMD);
41681d01bfaSLoc Ho writel(portclb_saved, port_mmio + PORT_LST_ADDR);
41781d01bfaSLoc Ho writel(portclbhi_saved, port_mmio + PORT_LST_ADDR_HI);
41881d01bfaSLoc Ho writel(portrxfis_saved, port_mmio + PORT_FIS_ADDR);
41981d01bfaSLoc Ho writel(portrxfishi_saved, port_mmio + PORT_FIS_ADDR_HI);
42081d01bfaSLoc Ho
42181d01bfaSLoc Ho hpriv->start_engine(ap);
42281d01bfaSLoc Ho
42381d01bfaSLoc Ho if (online)
42481d01bfaSLoc Ho *class = ahci_dev_classify(ap);
42581d01bfaSLoc Ho
42681d01bfaSLoc Ho return rc;
42781d01bfaSLoc Ho }
42881d01bfaSLoc Ho
xgene_ahci_host_stop(struct ata_host * host)42981d01bfaSLoc Ho static void xgene_ahci_host_stop(struct ata_host *host)
43081d01bfaSLoc Ho {
43181d01bfaSLoc Ho struct ahci_host_priv *hpriv = host->private_data;
43281d01bfaSLoc Ho
43381d01bfaSLoc Ho ahci_platform_disable_resources(hpriv);
43481d01bfaSLoc Ho }
43581d01bfaSLoc Ho
436a3a84bc7SSuman Tripathi /**
437a3a84bc7SSuman Tripathi * xgene_ahci_pmp_softreset - Issue the softreset to the drives connected
438a3a84bc7SSuman Tripathi * to Port Multiplier.
439a3a84bc7SSuman Tripathi * @link: link to reset
440a3a84bc7SSuman Tripathi * @class: Return value to indicate class of device
441a3a84bc7SSuman Tripathi * @deadline: deadline jiffies for the operation
442a3a84bc7SSuman Tripathi *
443a3a84bc7SSuman Tripathi * Due to H/W errata, the controller is unable to save the PMP
444a3a84bc7SSuman Tripathi * field fetched from command header before sending the H2D FIS.
445a3a84bc7SSuman Tripathi * When the device returns the PMP port field in the D2H FIS, there is
446a3a84bc7SSuman Tripathi * a mismatch and results in command completion failure. The workaround
447a3a84bc7SSuman Tripathi * is to write the pmp value to PxFBS.DEV field before issuing any command
448a3a84bc7SSuman Tripathi * to PMP.
449a3a84bc7SSuman Tripathi */
xgene_ahci_pmp_softreset(struct ata_link * link,unsigned int * class,unsigned long deadline)450a3a84bc7SSuman Tripathi static int xgene_ahci_pmp_softreset(struct ata_link *link, unsigned int *class,
451a3a84bc7SSuman Tripathi unsigned long deadline)
452a3a84bc7SSuman Tripathi {
453a3a84bc7SSuman Tripathi int pmp = sata_srst_pmp(link);
454a3a84bc7SSuman Tripathi struct ata_port *ap = link->ap;
455a3a84bc7SSuman Tripathi u32 rc;
4562bce6907SDamien Le Moal void __iomem *port_mmio = ahci_port_base(ap);
457a3a84bc7SSuman Tripathi u32 port_fbs;
458a3a84bc7SSuman Tripathi
459a3a84bc7SSuman Tripathi /*
460a3a84bc7SSuman Tripathi * Set PxFBS.DEV field with pmp
461a3a84bc7SSuman Tripathi * value.
462a3a84bc7SSuman Tripathi */
463a3a84bc7SSuman Tripathi port_fbs = readl(port_mmio + PORT_FBS);
464a3a84bc7SSuman Tripathi port_fbs &= ~PORT_FBS_DEV_MASK;
465a3a84bc7SSuman Tripathi port_fbs |= pmp << PORT_FBS_DEV_OFFSET;
466a3a84bc7SSuman Tripathi writel(port_fbs, port_mmio + PORT_FBS);
467a3a84bc7SSuman Tripathi
468a3a84bc7SSuman Tripathi rc = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
469a3a84bc7SSuman Tripathi
470a3a84bc7SSuman Tripathi return rc;
471a3a84bc7SSuman Tripathi }
472a3a84bc7SSuman Tripathi
473a3a84bc7SSuman Tripathi /**
474a3a84bc7SSuman Tripathi * xgene_ahci_softreset - Issue the softreset to the drive.
475a3a84bc7SSuman Tripathi * @link: link to reset
476a3a84bc7SSuman Tripathi * @class: Return value to indicate class of device
477a3a84bc7SSuman Tripathi * @deadline: deadline jiffies for the operation
478a3a84bc7SSuman Tripathi *
479a3a84bc7SSuman Tripathi * Due to H/W errata, the controller is unable to save the PMP
480a3a84bc7SSuman Tripathi * field fetched from command header before sending the H2D FIS.
481a3a84bc7SSuman Tripathi * When the device returns the PMP port field in the D2H FIS, there is
482a3a84bc7SSuman Tripathi * a mismatch and results in command completion failure. The workaround
483a3a84bc7SSuman Tripathi * is to write the pmp value to PxFBS.DEV field before issuing any command
484a3a84bc7SSuman Tripathi * to PMP. Here is the algorithm to detect PMP :
485a3a84bc7SSuman Tripathi *
486a3a84bc7SSuman Tripathi * 1. Save the PxFBS value
487a3a84bc7SSuman Tripathi * 2. Program PxFBS.DEV with pmp value send by framework. Framework sends
488a3a84bc7SSuman Tripathi * 0xF for both PMP/NON-PMP initially
489a3a84bc7SSuman Tripathi * 3. Issue softreset
490a3a84bc7SSuman Tripathi * 4. If signature class is PMP goto 6
491a3a84bc7SSuman Tripathi * 5. restore the original PxFBS and goto 3
492a3a84bc7SSuman Tripathi * 6. return
493a3a84bc7SSuman Tripathi */
xgene_ahci_softreset(struct ata_link * link,unsigned int * class,unsigned long deadline)494a3a84bc7SSuman Tripathi static int xgene_ahci_softreset(struct ata_link *link, unsigned int *class,
495a3a84bc7SSuman Tripathi unsigned long deadline)
496a3a84bc7SSuman Tripathi {
497a3a84bc7SSuman Tripathi int pmp = sata_srst_pmp(link);
498a3a84bc7SSuman Tripathi struct ata_port *ap = link->ap;
499a3a84bc7SSuman Tripathi struct ahci_host_priv *hpriv = ap->host->private_data;
500a3a84bc7SSuman Tripathi struct xgene_ahci_context *ctx = hpriv->plat_data;
5012bce6907SDamien Le Moal void __iomem *port_mmio = ahci_port_base(ap);
502a3a84bc7SSuman Tripathi u32 port_fbs;
503a3a84bc7SSuman Tripathi u32 port_fbs_save;
504a3a84bc7SSuman Tripathi u32 retry = 1;
505a3a84bc7SSuman Tripathi u32 rc;
506a3a84bc7SSuman Tripathi
507a3a84bc7SSuman Tripathi port_fbs_save = readl(port_mmio + PORT_FBS);
508a3a84bc7SSuman Tripathi
509a3a84bc7SSuman Tripathi /*
510a3a84bc7SSuman Tripathi * Set PxFBS.DEV field with pmp
511a3a84bc7SSuman Tripathi * value.
512a3a84bc7SSuman Tripathi */
513a3a84bc7SSuman Tripathi port_fbs = readl(port_mmio + PORT_FBS);
514a3a84bc7SSuman Tripathi port_fbs &= ~PORT_FBS_DEV_MASK;
515a3a84bc7SSuman Tripathi port_fbs |= pmp << PORT_FBS_DEV_OFFSET;
516a3a84bc7SSuman Tripathi writel(port_fbs, port_mmio + PORT_FBS);
517a3a84bc7SSuman Tripathi
518a3a84bc7SSuman Tripathi softreset_retry:
519a3a84bc7SSuman Tripathi rc = ahci_do_softreset(link, class, pmp,
520a3a84bc7SSuman Tripathi deadline, ahci_check_ready);
521a3a84bc7SSuman Tripathi
522a3a84bc7SSuman Tripathi ctx->class[ap->port_no] = *class;
523a3a84bc7SSuman Tripathi if (*class != ATA_DEV_PMP) {
524a3a84bc7SSuman Tripathi /*
525a3a84bc7SSuman Tripathi * Retry for normal drives without
526a3a84bc7SSuman Tripathi * setting PxFBS.DEV field with pmp value.
527a3a84bc7SSuman Tripathi */
528a3a84bc7SSuman Tripathi if (retry--) {
529a3a84bc7SSuman Tripathi writel(port_fbs_save, port_mmio + PORT_FBS);
530a3a84bc7SSuman Tripathi goto softreset_retry;
531a3a84bc7SSuman Tripathi }
532a3a84bc7SSuman Tripathi }
533a3a84bc7SSuman Tripathi
534a3a84bc7SSuman Tripathi return rc;
535a3a84bc7SSuman Tripathi }
536a3a84bc7SSuman Tripathi
53732aea268SSuman Tripathi /**
53832aea268SSuman Tripathi * xgene_ahci_handle_broken_edge_irq - Handle the broken irq.
5396864e781SLee Jones * @host: Host that recieved the irq
54032aea268SSuman Tripathi * @irq_masked: HOST_IRQ_STAT value
54132aea268SSuman Tripathi *
54232aea268SSuman Tripathi * For hardware with broken edge trigger latch
54332aea268SSuman Tripathi * the HOST_IRQ_STAT register misses the edge interrupt
54432aea268SSuman Tripathi * when clearing of HOST_IRQ_STAT register and hardware
54532aea268SSuman Tripathi * reporting the PORT_IRQ_STAT register at the
54632aea268SSuman Tripathi * same clock cycle.
54732aea268SSuman Tripathi * As such, the algorithm below outlines the workaround.
54832aea268SSuman Tripathi *
54932aea268SSuman Tripathi * 1. Read HOST_IRQ_STAT register and save the state.
55032aea268SSuman Tripathi * 2. Clear the HOST_IRQ_STAT register.
55132aea268SSuman Tripathi * 3. Read back the HOST_IRQ_STAT register.
55232aea268SSuman Tripathi * 4. If HOST_IRQ_STAT register equals to zero, then
55332aea268SSuman Tripathi * traverse the rest of port's PORT_IRQ_STAT register
55432aea268SSuman Tripathi * to check if an interrupt is triggered at that point else
55532aea268SSuman Tripathi * go to step 6.
55632aea268SSuman Tripathi * 5. If PORT_IRQ_STAT register of rest ports is not equal to zero
55732aea268SSuman Tripathi * then update the state of HOST_IRQ_STAT saved in step 1.
55832aea268SSuman Tripathi * 6. Handle port interrupts.
55932aea268SSuman Tripathi * 7. Exit
56032aea268SSuman Tripathi */
xgene_ahci_handle_broken_edge_irq(struct ata_host * host,u32 irq_masked)56132aea268SSuman Tripathi static int xgene_ahci_handle_broken_edge_irq(struct ata_host *host,
56232aea268SSuman Tripathi u32 irq_masked)
56332aea268SSuman Tripathi {
56432aea268SSuman Tripathi struct ahci_host_priv *hpriv = host->private_data;
56532aea268SSuman Tripathi void __iomem *port_mmio;
56632aea268SSuman Tripathi int i;
56732aea268SSuman Tripathi
56832aea268SSuman Tripathi if (!readl(hpriv->mmio + HOST_IRQ_STAT)) {
56932aea268SSuman Tripathi for (i = 0; i < host->n_ports; i++) {
57032aea268SSuman Tripathi if (irq_masked & (1 << i))
57132aea268SSuman Tripathi continue;
57232aea268SSuman Tripathi
57332aea268SSuman Tripathi port_mmio = ahci_port_base(host->ports[i]);
57432aea268SSuman Tripathi if (readl(port_mmio + PORT_IRQ_STAT))
57532aea268SSuman Tripathi irq_masked |= (1 << i);
57632aea268SSuman Tripathi }
57732aea268SSuman Tripathi }
57832aea268SSuman Tripathi
57932aea268SSuman Tripathi return ahci_handle_port_intr(host, irq_masked);
58032aea268SSuman Tripathi }
58132aea268SSuman Tripathi
xgene_ahci_irq_intr(int irq,void * dev_instance)582d867b95fSSuman Tripathi static irqreturn_t xgene_ahci_irq_intr(int irq, void *dev_instance)
583d867b95fSSuman Tripathi {
584d867b95fSSuman Tripathi struct ata_host *host = dev_instance;
585d867b95fSSuman Tripathi struct ahci_host_priv *hpriv;
586d867b95fSSuman Tripathi unsigned int rc = 0;
587d867b95fSSuman Tripathi void __iomem *mmio;
588d867b95fSSuman Tripathi u32 irq_stat, irq_masked;
589d867b95fSSuman Tripathi
590d867b95fSSuman Tripathi hpriv = host->private_data;
591d867b95fSSuman Tripathi mmio = hpriv->mmio;
592d867b95fSSuman Tripathi
593d867b95fSSuman Tripathi /* sigh. 0xffffffff is a valid return from h/w */
594d867b95fSSuman Tripathi irq_stat = readl(mmio + HOST_IRQ_STAT);
595d867b95fSSuman Tripathi if (!irq_stat)
596d867b95fSSuman Tripathi return IRQ_NONE;
597d867b95fSSuman Tripathi
598d867b95fSSuman Tripathi irq_masked = irq_stat & hpriv->port_map;
599d867b95fSSuman Tripathi
600d867b95fSSuman Tripathi spin_lock(&host->lock);
601d867b95fSSuman Tripathi
602d867b95fSSuman Tripathi /*
603d867b95fSSuman Tripathi * HOST_IRQ_STAT behaves as edge triggered latch meaning that
604d867b95fSSuman Tripathi * it should be cleared before all the port events are cleared.
605d867b95fSSuman Tripathi */
606d867b95fSSuman Tripathi writel(irq_stat, mmio + HOST_IRQ_STAT);
607d867b95fSSuman Tripathi
60832aea268SSuman Tripathi rc = xgene_ahci_handle_broken_edge_irq(host, irq_masked);
609d867b95fSSuman Tripathi
610d867b95fSSuman Tripathi spin_unlock(&host->lock);
611d867b95fSSuman Tripathi
612d867b95fSSuman Tripathi return IRQ_RETVAL(rc);
613d867b95fSSuman Tripathi }
614d867b95fSSuman Tripathi
615c9802a4bSSuman Tripathi static struct ata_port_operations xgene_ahci_v1_ops = {
61681d01bfaSLoc Ho .inherits = &ahci_ops,
61781d01bfaSLoc Ho .host_stop = xgene_ahci_host_stop,
61881d01bfaSLoc Ho .hardreset = xgene_ahci_hardreset,
61981d01bfaSLoc Ho .read_id = xgene_ahci_read_id,
6202a0bdff6SSuman Tripathi .qc_issue = xgene_ahci_qc_issue,
621a3a84bc7SSuman Tripathi .softreset = xgene_ahci_softreset,
622a3a84bc7SSuman Tripathi .pmp_softreset = xgene_ahci_pmp_softreset
62381d01bfaSLoc Ho };
62481d01bfaSLoc Ho
625c9802a4bSSuman Tripathi static const struct ata_port_info xgene_ahci_v1_port_info = {
6261540035dSSuman Tripathi .flags = AHCI_FLAG_COMMON | ATA_FLAG_PMP,
62781d01bfaSLoc Ho .pio_mask = ATA_PIO4,
62881d01bfaSLoc Ho .udma_mask = ATA_UDMA6,
629c9802a4bSSuman Tripathi .port_ops = &xgene_ahci_v1_ops,
630c9802a4bSSuman Tripathi };
631c9802a4bSSuman Tripathi
632c9802a4bSSuman Tripathi static struct ata_port_operations xgene_ahci_v2_ops = {
633c9802a4bSSuman Tripathi .inherits = &ahci_ops,
634c9802a4bSSuman Tripathi .host_stop = xgene_ahci_host_stop,
635c9802a4bSSuman Tripathi .hardreset = xgene_ahci_hardreset,
636c9802a4bSSuman Tripathi .read_id = xgene_ahci_read_id,
637c9802a4bSSuman Tripathi };
638c9802a4bSSuman Tripathi
639c9802a4bSSuman Tripathi static const struct ata_port_info xgene_ahci_v2_port_info = {
640c9802a4bSSuman Tripathi .flags = AHCI_FLAG_COMMON | ATA_FLAG_PMP,
641c9802a4bSSuman Tripathi .pio_mask = ATA_PIO4,
642c9802a4bSSuman Tripathi .udma_mask = ATA_UDMA6,
643c9802a4bSSuman Tripathi .port_ops = &xgene_ahci_v2_ops,
64481d01bfaSLoc Ho };
64581d01bfaSLoc Ho
xgene_ahci_hw_init(struct ahci_host_priv * hpriv)64681d01bfaSLoc Ho static int xgene_ahci_hw_init(struct ahci_host_priv *hpriv)
64781d01bfaSLoc Ho {
64881d01bfaSLoc Ho struct xgene_ahci_context *ctx = hpriv->plat_data;
64981d01bfaSLoc Ho int i;
65081d01bfaSLoc Ho int rc;
65181d01bfaSLoc Ho u32 val;
65281d01bfaSLoc Ho
65381d01bfaSLoc Ho /* Remove IP RAM out of shutdown */
65481d01bfaSLoc Ho rc = xgene_ahci_init_memram(ctx);
65581d01bfaSLoc Ho if (rc)
65681d01bfaSLoc Ho return rc;
65781d01bfaSLoc Ho
65881d01bfaSLoc Ho for (i = 0; i < MAX_AHCI_CHN_PERCTR; i++)
65981d01bfaSLoc Ho xgene_ahci_set_phy_cfg(ctx, i);
66081d01bfaSLoc Ho
66181d01bfaSLoc Ho /* AXI disable Mask */
66281d01bfaSLoc Ho writel(0xffffffff, hpriv->mmio + HOST_IRQ_STAT);
66381d01bfaSLoc Ho readl(hpriv->mmio + HOST_IRQ_STAT); /* Force a barrier */
66481d01bfaSLoc Ho writel(0, ctx->csr_core + INTSTATUSMASK);
6656a96918aSLoc Ho val = readl(ctx->csr_core + INTSTATUSMASK); /* Force a barrier */
66681d01bfaSLoc Ho dev_dbg(ctx->dev, "top level interrupt mask 0x%X value 0x%08X\n",
66781d01bfaSLoc Ho INTSTATUSMASK, val);
66881d01bfaSLoc Ho
66981d01bfaSLoc Ho writel(0x0, ctx->csr_core + ERRINTSTATUSMASK);
67081d01bfaSLoc Ho readl(ctx->csr_core + ERRINTSTATUSMASK); /* Force a barrier */
67181d01bfaSLoc Ho writel(0x0, ctx->csr_axi + INT_SLV_TMOMASK);
67281d01bfaSLoc Ho readl(ctx->csr_axi + INT_SLV_TMOMASK);
67381d01bfaSLoc Ho
67481d01bfaSLoc Ho /* Enable AXI Interrupt */
67581d01bfaSLoc Ho writel(0xffffffff, ctx->csr_core + SLVRDERRATTRIBUTES);
67681d01bfaSLoc Ho writel(0xffffffff, ctx->csr_core + SLVWRERRATTRIBUTES);
67781d01bfaSLoc Ho writel(0xffffffff, ctx->csr_core + MSTRDERRATTRIBUTES);
67881d01bfaSLoc Ho writel(0xffffffff, ctx->csr_core + MSTWRERRATTRIBUTES);
67981d01bfaSLoc Ho
68081d01bfaSLoc Ho /* Enable coherency */
68181d01bfaSLoc Ho val = readl(ctx->csr_core + BUSCTLREG);
68281d01bfaSLoc Ho val &= ~0x00000002; /* Enable write coherency */
68381d01bfaSLoc Ho val &= ~0x00000001; /* Enable read coherency */
68481d01bfaSLoc Ho writel(val, ctx->csr_core + BUSCTLREG);
68581d01bfaSLoc Ho
68681d01bfaSLoc Ho val = readl(ctx->csr_core + IOFMSTRWAUX);
68781d01bfaSLoc Ho val |= (1 << 3); /* Enable read coherency */
68881d01bfaSLoc Ho val |= (1 << 9); /* Enable write coherency */
68981d01bfaSLoc Ho writel(val, ctx->csr_core + IOFMSTRWAUX);
69081d01bfaSLoc Ho val = readl(ctx->csr_core + IOFMSTRWAUX);
69181d01bfaSLoc Ho dev_dbg(ctx->dev, "coherency 0x%X value 0x%08X\n",
69281d01bfaSLoc Ho IOFMSTRWAUX, val);
69381d01bfaSLoc Ho
69481d01bfaSLoc Ho return rc;
69581d01bfaSLoc Ho }
69681d01bfaSLoc Ho
xgene_ahci_mux_select(struct xgene_ahci_context * ctx)69781d01bfaSLoc Ho static int xgene_ahci_mux_select(struct xgene_ahci_context *ctx)
69881d01bfaSLoc Ho {
69981d01bfaSLoc Ho u32 val;
70081d01bfaSLoc Ho
70181d01bfaSLoc Ho /* Check for optional MUX resource */
702a77b6ee9SSuman Tripathi if (!ctx->csr_mux)
70381d01bfaSLoc Ho return 0;
70481d01bfaSLoc Ho
70581d01bfaSLoc Ho val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
70681d01bfaSLoc Ho val &= ~CFG_SATA_ENET_SELECT_MASK;
70781d01bfaSLoc Ho writel(val, ctx->csr_mux + SATA_ENET_CONFIG_REG);
70881d01bfaSLoc Ho val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
70981d01bfaSLoc Ho return val & CFG_SATA_ENET_SELECT_MASK ? -1 : 0;
71081d01bfaSLoc Ho }
71181d01bfaSLoc Ho
71225df73d9SBart Van Assche static const struct scsi_host_template ahci_platform_sht = {
713018d5ef2SAkinobu Mita AHCI_SHT(DRV_NAME),
714018d5ef2SAkinobu Mita };
715018d5ef2SAkinobu Mita
716c9802a4bSSuman Tripathi #ifdef CONFIG_ACPI
717c9802a4bSSuman Tripathi static const struct acpi_device_id xgene_ahci_acpi_match[] = {
718c9802a4bSSuman Tripathi { "APMC0D0D", XGENE_AHCI_V1},
719c9802a4bSSuman Tripathi { "APMC0D32", XGENE_AHCI_V2},
720c9802a4bSSuman Tripathi {},
721c9802a4bSSuman Tripathi };
722c9802a4bSSuman Tripathi MODULE_DEVICE_TABLE(acpi, xgene_ahci_acpi_match);
723c9802a4bSSuman Tripathi #endif
724c9802a4bSSuman Tripathi
725c9802a4bSSuman Tripathi static const struct of_device_id xgene_ahci_of_match[] = {
726c9802a4bSSuman Tripathi {.compatible = "apm,xgene-ahci", .data = (void *) XGENE_AHCI_V1},
727c9802a4bSSuman Tripathi {.compatible = "apm,xgene-ahci-v2", .data = (void *) XGENE_AHCI_V2},
7285e776d7bSGeert Uytterhoeven { /* sentinel */ }
729c9802a4bSSuman Tripathi };
730c9802a4bSSuman Tripathi MODULE_DEVICE_TABLE(of, xgene_ahci_of_match);
731c9802a4bSSuman Tripathi
xgene_ahci_probe(struct platform_device * pdev)73281d01bfaSLoc Ho static int xgene_ahci_probe(struct platform_device *pdev)
73381d01bfaSLoc Ho {
73481d01bfaSLoc Ho struct device *dev = &pdev->dev;
73581d01bfaSLoc Ho struct ahci_host_priv *hpriv;
73681d01bfaSLoc Ho struct xgene_ahci_context *ctx;
73781d01bfaSLoc Ho struct resource *res;
738c9802a4bSSuman Tripathi const struct of_device_id *of_devid;
739c9802a4bSSuman Tripathi enum xgene_ahci_version version = XGENE_AHCI_V1;
740c9802a4bSSuman Tripathi const struct ata_port_info *ppi[] = { &xgene_ahci_v1_port_info,
741c9802a4bSSuman Tripathi &xgene_ahci_v2_port_info };
74281d01bfaSLoc Ho int rc;
74381d01bfaSLoc Ho
74416af2d65SKunihiko Hayashi hpriv = ahci_platform_get_resources(pdev, 0);
74581d01bfaSLoc Ho if (IS_ERR(hpriv))
74681d01bfaSLoc Ho return PTR_ERR(hpriv);
74781d01bfaSLoc Ho
74881d01bfaSLoc Ho ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
74981d01bfaSLoc Ho if (!ctx)
75081d01bfaSLoc Ho return -ENOMEM;
75181d01bfaSLoc Ho
75281d01bfaSLoc Ho hpriv->plat_data = ctx;
75381d01bfaSLoc Ho ctx->hpriv = hpriv;
75481d01bfaSLoc Ho ctx->dev = dev;
75581d01bfaSLoc Ho
75681d01bfaSLoc Ho /* Retrieve the IP core resource */
7576dd7830cSYangtao Li ctx->csr_core = devm_platform_ioremap_resource(pdev, 1);
75881d01bfaSLoc Ho if (IS_ERR(ctx->csr_core))
75981d01bfaSLoc Ho return PTR_ERR(ctx->csr_core);
76081d01bfaSLoc Ho
76181d01bfaSLoc Ho /* Retrieve the IP diagnostic resource */
7626dd7830cSYangtao Li ctx->csr_diag = devm_platform_ioremap_resource(pdev, 2);
76381d01bfaSLoc Ho if (IS_ERR(ctx->csr_diag))
76481d01bfaSLoc Ho return PTR_ERR(ctx->csr_diag);
76581d01bfaSLoc Ho
76681d01bfaSLoc Ho /* Retrieve the IP AXI resource */
7676dd7830cSYangtao Li ctx->csr_axi = devm_platform_ioremap_resource(pdev, 3);
76881d01bfaSLoc Ho if (IS_ERR(ctx->csr_axi))
76981d01bfaSLoc Ho return PTR_ERR(ctx->csr_axi);
77081d01bfaSLoc Ho
77181d01bfaSLoc Ho /* Retrieve the optional IP mux resource */
77281d01bfaSLoc Ho res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
773a77b6ee9SSuman Tripathi if (res) {
774a77b6ee9SSuman Tripathi void __iomem *csr = devm_ioremap_resource(dev, res);
775a77b6ee9SSuman Tripathi if (IS_ERR(csr))
776a77b6ee9SSuman Tripathi return PTR_ERR(csr);
777a77b6ee9SSuman Tripathi
778a77b6ee9SSuman Tripathi ctx->csr_mux = csr;
779a77b6ee9SSuman Tripathi }
78081d01bfaSLoc Ho
781c9802a4bSSuman Tripathi of_devid = of_match_device(xgene_ahci_of_match, dev);
782c9802a4bSSuman Tripathi if (of_devid) {
783c9802a4bSSuman Tripathi if (of_devid->data)
784e8fbdf18SDamien Le Moal version = (unsigned long) of_devid->data;
785c9802a4bSSuman Tripathi }
786c9802a4bSSuman Tripathi #ifdef CONFIG_ACPI
787c9802a4bSSuman Tripathi else {
788c9802a4bSSuman Tripathi const struct acpi_device_id *acpi_id;
789c9802a4bSSuman Tripathi struct acpi_device_info *info;
790c9802a4bSSuman Tripathi acpi_status status;
791c9802a4bSSuman Tripathi
792c9802a4bSSuman Tripathi acpi_id = acpi_match_device(xgene_ahci_acpi_match, &pdev->dev);
793c9802a4bSSuman Tripathi if (!acpi_id) {
794c9802a4bSSuman Tripathi dev_warn(&pdev->dev, "No node entry in ACPI table. Assume version1\n");
795c9802a4bSSuman Tripathi version = XGENE_AHCI_V1;
7962d32d101SDan Carpenter } else if (acpi_id->driver_data) {
797c9802a4bSSuman Tripathi version = (enum xgene_ahci_version) acpi_id->driver_data;
798c9802a4bSSuman Tripathi status = acpi_get_object_info(ACPI_HANDLE(&pdev->dev), &info);
799c9802a4bSSuman Tripathi if (ACPI_FAILURE(status)) {
800c9802a4bSSuman Tripathi dev_warn(&pdev->dev, "%s: Error reading device info. Assume version1\n",
801c9802a4bSSuman Tripathi __func__);
802c9802a4bSSuman Tripathi version = XGENE_AHCI_V1;
803de251820SMichał Kępień } else {
804de251820SMichał Kępień if (info->valid & ACPI_VALID_CID)
805c9802a4bSSuman Tripathi version = XGENE_AHCI_V2;
806de251820SMichał Kępień kfree(info);
807c9802a4bSSuman Tripathi }
808c9802a4bSSuman Tripathi }
8098134233eSDan Carpenter }
810c9802a4bSSuman Tripathi #endif
811c9802a4bSSuman Tripathi
81281d01bfaSLoc Ho dev_dbg(dev, "VAddr 0x%p Mmio VAddr 0x%p\n", ctx->csr_core,
81381d01bfaSLoc Ho hpriv->mmio);
81481d01bfaSLoc Ho
81581d01bfaSLoc Ho /* Select ATA */
81681d01bfaSLoc Ho if ((rc = xgene_ahci_mux_select(ctx))) {
81781d01bfaSLoc Ho dev_err(dev, "SATA mux selection failed error %d\n", rc);
81881d01bfaSLoc Ho return -ENODEV;
81981d01bfaSLoc Ho }
82081d01bfaSLoc Ho
8210bed13beSSuman Tripathi if (xgene_ahci_is_memram_inited(ctx)) {
8220bed13beSSuman Tripathi dev_info(dev, "skip clock and PHY initialization\n");
8230bed13beSSuman Tripathi goto skip_clk_phy;
8240bed13beSSuman Tripathi }
8250bed13beSSuman Tripathi
82681d01bfaSLoc Ho /* Due to errata, HW requires full toggle transition */
82781d01bfaSLoc Ho rc = ahci_platform_enable_clks(hpriv);
82881d01bfaSLoc Ho if (rc)
82981d01bfaSLoc Ho goto disable_resources;
83081d01bfaSLoc Ho ahci_platform_disable_clks(hpriv);
83181d01bfaSLoc Ho
83281d01bfaSLoc Ho rc = ahci_platform_enable_resources(hpriv);
83381d01bfaSLoc Ho if (rc)
83481d01bfaSLoc Ho goto disable_resources;
83581d01bfaSLoc Ho
83681d01bfaSLoc Ho /* Configure the host controller */
83781d01bfaSLoc Ho xgene_ahci_hw_init(hpriv);
8380bed13beSSuman Tripathi skip_clk_phy:
839f9f36917SKefeng Wang
840c9802a4bSSuman Tripathi switch (version) {
841c9802a4bSSuman Tripathi case XGENE_AHCI_V1:
842c9802a4bSSuman Tripathi hpriv->flags = AHCI_HFLAG_NO_NCQ;
843c9802a4bSSuman Tripathi break;
844c9802a4bSSuman Tripathi case XGENE_AHCI_V2:
845d867b95fSSuman Tripathi hpriv->flags |= AHCI_HFLAG_YES_FBS;
846d867b95fSSuman Tripathi hpriv->irq_handler = xgene_ahci_irq_intr;
847c9802a4bSSuman Tripathi break;
848c9802a4bSSuman Tripathi default:
849c9802a4bSSuman Tripathi break;
850c9802a4bSSuman Tripathi }
851c9802a4bSSuman Tripathi
852c9802a4bSSuman Tripathi rc = ahci_platform_init_host(pdev, hpriv, ppi[version - 1],
853018d5ef2SAkinobu Mita &ahci_platform_sht);
85481d01bfaSLoc Ho if (rc)
85581d01bfaSLoc Ho goto disable_resources;
85681d01bfaSLoc Ho
85781d01bfaSLoc Ho dev_dbg(dev, "X-Gene SATA host controller initialized\n");
85881d01bfaSLoc Ho return 0;
85981d01bfaSLoc Ho
86081d01bfaSLoc Ho disable_resources:
86181d01bfaSLoc Ho ahci_platform_disable_resources(hpriv);
86281d01bfaSLoc Ho return rc;
86381d01bfaSLoc Ho }
86481d01bfaSLoc Ho
86581d01bfaSLoc Ho static struct platform_driver xgene_ahci_driver = {
86681d01bfaSLoc Ho .probe = xgene_ahci_probe,
867a7eb54d4SUwe Kleine-König .remove_new = ata_platform_remove_one,
86881d01bfaSLoc Ho .driver = {
869018d5ef2SAkinobu Mita .name = DRV_NAME,
87081d01bfaSLoc Ho .of_match_table = xgene_ahci_of_match,
87192b5bf98SFeng Kan .acpi_match_table = ACPI_PTR(xgene_ahci_acpi_match),
87281d01bfaSLoc Ho },
87381d01bfaSLoc Ho };
87481d01bfaSLoc Ho
87581d01bfaSLoc Ho module_platform_driver(xgene_ahci_driver);
87681d01bfaSLoc Ho
87781d01bfaSLoc Ho MODULE_DESCRIPTION("APM X-Gene AHCI SATA driver");
87881d01bfaSLoc Ho MODULE_AUTHOR("Loc Ho <lho@apm.com>");
87981d01bfaSLoc Ho MODULE_LICENSE("GPL");
88081d01bfaSLoc Ho MODULE_VERSION("0.4");
881