xref: /openbmc/linux/drivers/ata/ahci_xgene.c (revision 018d5ef2048fcab339467bcbebccf588c9bd2531)
181d01bfaSLoc Ho /*
281d01bfaSLoc Ho  * AppliedMicro X-Gene SoC SATA Host Controller Driver
381d01bfaSLoc Ho  *
481d01bfaSLoc Ho  * Copyright (c) 2014, Applied Micro Circuits Corporation
581d01bfaSLoc Ho  * Author: Loc Ho <lho@apm.com>
681d01bfaSLoc Ho  *         Tuan Phan <tphan@apm.com>
781d01bfaSLoc Ho  *         Suman Tripathi <stripathi@apm.com>
881d01bfaSLoc Ho  *
981d01bfaSLoc Ho  * This program is free software; you can redistribute  it and/or modify it
1081d01bfaSLoc Ho  * under  the terms of  the GNU General  Public License as published by the
1181d01bfaSLoc Ho  * Free Software Foundation;  either version 2 of the  License, or (at your
1281d01bfaSLoc Ho  * option) any later version.
1381d01bfaSLoc Ho  *
1481d01bfaSLoc Ho  * This program is distributed in the hope that it will be useful,
1581d01bfaSLoc Ho  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1681d01bfaSLoc Ho  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1781d01bfaSLoc Ho  * GNU General Public License for more details.
1881d01bfaSLoc Ho  *
1981d01bfaSLoc Ho  * You should have received a copy of the GNU General Public License
2081d01bfaSLoc Ho  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2181d01bfaSLoc Ho  *
2281d01bfaSLoc Ho  * NOTE: PM support is not currently available.
2381d01bfaSLoc Ho  *
2481d01bfaSLoc Ho  */
2581d01bfaSLoc Ho #include <linux/module.h>
2681d01bfaSLoc Ho #include <linux/platform_device.h>
2781d01bfaSLoc Ho #include <linux/ahci_platform.h>
2881d01bfaSLoc Ho #include <linux/of_address.h>
2981d01bfaSLoc Ho #include <linux/of_irq.h>
3081d01bfaSLoc Ho #include <linux/phy/phy.h>
3181d01bfaSLoc Ho #include "ahci.h"
3281d01bfaSLoc Ho 
33*018d5ef2SAkinobu Mita #define DRV_NAME "xgene-ahci"
34*018d5ef2SAkinobu Mita 
3581d01bfaSLoc Ho /* Max # of disk per a controller */
3681d01bfaSLoc Ho #define MAX_AHCI_CHN_PERCTR		2
3781d01bfaSLoc Ho 
3881d01bfaSLoc Ho /* MUX CSR */
3981d01bfaSLoc Ho #define SATA_ENET_CONFIG_REG		0x00000000
4081d01bfaSLoc Ho #define  CFG_SATA_ENET_SELECT_MASK	0x00000001
4181d01bfaSLoc Ho 
4281d01bfaSLoc Ho /* SATA core host controller CSR */
4381d01bfaSLoc Ho #define SLVRDERRATTRIBUTES		0x00000000
4481d01bfaSLoc Ho #define SLVWRERRATTRIBUTES		0x00000004
4581d01bfaSLoc Ho #define MSTRDERRATTRIBUTES		0x00000008
4681d01bfaSLoc Ho #define MSTWRERRATTRIBUTES		0x0000000c
4781d01bfaSLoc Ho #define BUSCTLREG			0x00000014
4881d01bfaSLoc Ho #define IOFMSTRWAUX			0x00000018
4981d01bfaSLoc Ho #define INTSTATUSMASK			0x0000002c
5081d01bfaSLoc Ho #define ERRINTSTATUS			0x00000030
5181d01bfaSLoc Ho #define ERRINTSTATUSMASK		0x00000034
5281d01bfaSLoc Ho 
5381d01bfaSLoc Ho /* SATA host AHCI CSR */
5481d01bfaSLoc Ho #define PORTCFG				0x000000a4
5581d01bfaSLoc Ho #define  PORTADDR_SET(dst, src) \
5681d01bfaSLoc Ho 		(((dst) & ~0x0000003f) | (((u32)(src)) & 0x0000003f))
5781d01bfaSLoc Ho #define PORTPHY1CFG		0x000000a8
5881d01bfaSLoc Ho #define PORTPHY1CFG_FRCPHYRDY_SET(dst, src) \
5981d01bfaSLoc Ho 		(((dst) & ~0x00100000) | (((u32)(src) << 0x14) & 0x00100000))
6081d01bfaSLoc Ho #define PORTPHY2CFG			0x000000ac
6181d01bfaSLoc Ho #define PORTPHY3CFG			0x000000b0
6281d01bfaSLoc Ho #define PORTPHY4CFG			0x000000b4
6381d01bfaSLoc Ho #define PORTPHY5CFG			0x000000b8
6481d01bfaSLoc Ho #define SCTL0				0x0000012C
6581d01bfaSLoc Ho #define PORTPHY5CFG_RTCHG_SET(dst, src) \
6681d01bfaSLoc Ho 		(((dst) & ~0xfff00000) | (((u32)(src) << 0x14) & 0xfff00000))
6781d01bfaSLoc Ho #define PORTAXICFG_EN_CONTEXT_SET(dst, src) \
6881d01bfaSLoc Ho 		(((dst) & ~0x01000000) | (((u32)(src) << 0x18) & 0x01000000))
6981d01bfaSLoc Ho #define PORTAXICFG			0x000000bc
7081d01bfaSLoc Ho #define PORTAXICFG_OUTTRANS_SET(dst, src) \
7181d01bfaSLoc Ho 		(((dst) & ~0x00f00000) | (((u32)(src) << 0x14) & 0x00f00000))
72aeae4dcaSSuman Tripathi #define PORTRANSCFG			0x000000c8
73aeae4dcaSSuman Tripathi #define PORTRANSCFG_RXWM_SET(dst, src)		\
74aeae4dcaSSuman Tripathi 		(((dst) & ~0x0000007f) | (((u32)(src)) & 0x0000007f))
7581d01bfaSLoc Ho 
7681d01bfaSLoc Ho /* SATA host controller AXI CSR */
7781d01bfaSLoc Ho #define INT_SLV_TMOMASK			0x00000010
7881d01bfaSLoc Ho 
7981d01bfaSLoc Ho /* SATA diagnostic CSR */
8081d01bfaSLoc Ho #define CFG_MEM_RAM_SHUTDOWN		0x00000070
8181d01bfaSLoc Ho #define BLOCK_MEM_RDY			0x00000074
8281d01bfaSLoc Ho 
830babe614SSuman Tripathi /* Max retry for link down */
840babe614SSuman Tripathi #define MAX_LINK_DOWN_RETRY 3
850babe614SSuman Tripathi 
8681d01bfaSLoc Ho struct xgene_ahci_context {
8781d01bfaSLoc Ho 	struct ahci_host_priv *hpriv;
8881d01bfaSLoc Ho 	struct device *dev;
892a0bdff6SSuman Tripathi 	u8 last_cmd[MAX_AHCI_CHN_PERCTR]; /* tracking the last command issued*/
90a3a84bc7SSuman Tripathi 	u32 class[MAX_AHCI_CHN_PERCTR]; /* tracking the class of device */
9181d01bfaSLoc Ho 	void __iomem *csr_core;		/* Core CSR address of IP */
9281d01bfaSLoc Ho 	void __iomem *csr_diag;		/* Diag CSR address of IP */
9381d01bfaSLoc Ho 	void __iomem *csr_axi;		/* AXI CSR address of IP */
9481d01bfaSLoc Ho 	void __iomem *csr_mux;		/* MUX CSR address of IP */
9581d01bfaSLoc Ho };
9681d01bfaSLoc Ho 
9781d01bfaSLoc Ho static int xgene_ahci_init_memram(struct xgene_ahci_context *ctx)
9881d01bfaSLoc Ho {
9981d01bfaSLoc Ho 	dev_dbg(ctx->dev, "Release memory from shutdown\n");
10081d01bfaSLoc Ho 	writel(0x0, ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN);
10181d01bfaSLoc Ho 	readl(ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN); /* Force a barrier */
10281d01bfaSLoc Ho 	msleep(1);	/* reset may take up to 1ms */
10381d01bfaSLoc Ho 	if (readl(ctx->csr_diag + BLOCK_MEM_RDY) != 0xFFFFFFFF) {
10481d01bfaSLoc Ho 		dev_err(ctx->dev, "failed to release memory from shutdown\n");
10581d01bfaSLoc Ho 		return -ENODEV;
10681d01bfaSLoc Ho 	}
10781d01bfaSLoc Ho 	return 0;
10881d01bfaSLoc Ho }
10981d01bfaSLoc Ho 
11081d01bfaSLoc Ho /**
1111540035dSSuman Tripathi  * xgene_ahci_poll_reg_val- Poll a register on a specific value.
1121540035dSSuman Tripathi  * @ap : ATA port of interest.
1131540035dSSuman Tripathi  * @reg : Register of interest.
1141540035dSSuman Tripathi  * @val : Value to be attained.
1151540035dSSuman Tripathi  * @interval : waiting interval for polling.
1161540035dSSuman Tripathi  * @timeout : timeout for achieving the value.
1171540035dSSuman Tripathi  */
1181540035dSSuman Tripathi static int xgene_ahci_poll_reg_val(struct ata_port *ap,
1191540035dSSuman Tripathi 				   void __iomem *reg, unsigned
1201540035dSSuman Tripathi 				   int val, unsigned long interval,
1211540035dSSuman Tripathi 				   unsigned long timeout)
1221540035dSSuman Tripathi {
1231540035dSSuman Tripathi 	unsigned long deadline;
1241540035dSSuman Tripathi 	unsigned int tmp;
1251540035dSSuman Tripathi 
1261540035dSSuman Tripathi 	tmp = ioread32(reg);
1271540035dSSuman Tripathi 	deadline = ata_deadline(jiffies, timeout);
1281540035dSSuman Tripathi 
1291540035dSSuman Tripathi 	while (tmp != val && time_before(jiffies, deadline)) {
1301540035dSSuman Tripathi 		ata_msleep(ap, interval);
1311540035dSSuman Tripathi 		tmp = ioread32(reg);
1321540035dSSuman Tripathi 	}
1331540035dSSuman Tripathi 
1341540035dSSuman Tripathi 	return tmp;
1351540035dSSuman Tripathi }
1361540035dSSuman Tripathi 
1371540035dSSuman Tripathi /**
1382a0bdff6SSuman Tripathi  * xgene_ahci_restart_engine - Restart the dma engine.
1392a0bdff6SSuman Tripathi  * @ap : ATA port of interest
1402a0bdff6SSuman Tripathi  *
1411540035dSSuman Tripathi  * Waits for completion of multiple commands and restarts
1421540035dSSuman Tripathi  * the DMA engine inside the controller.
1432a0bdff6SSuman Tripathi  */
1442a0bdff6SSuman Tripathi static int xgene_ahci_restart_engine(struct ata_port *ap)
1452a0bdff6SSuman Tripathi {
1462a0bdff6SSuman Tripathi 	struct ahci_host_priv *hpriv = ap->host->private_data;
1471540035dSSuman Tripathi 	struct ahci_port_priv *pp = ap->private_data;
1481540035dSSuman Tripathi 	void __iomem *port_mmio = ahci_port_base(ap);
1491540035dSSuman Tripathi 	u32 fbs;
1501540035dSSuman Tripathi 
1511540035dSSuman Tripathi 	/*
1521540035dSSuman Tripathi 	 * In case of PMP multiple IDENTIFY DEVICE commands can be
1531540035dSSuman Tripathi 	 * issued inside PxCI. So need to poll PxCI for the
1541540035dSSuman Tripathi 	 * completion of outstanding IDENTIFY DEVICE commands before
1551540035dSSuman Tripathi 	 * we restart the DMA engine.
1561540035dSSuman Tripathi 	 */
1571540035dSSuman Tripathi 	if (xgene_ahci_poll_reg_val(ap, port_mmio +
1581540035dSSuman Tripathi 				    PORT_CMD_ISSUE, 0x0, 1, 100))
1591540035dSSuman Tripathi 		  return -EBUSY;
1602a0bdff6SSuman Tripathi 
1612a0bdff6SSuman Tripathi 	ahci_stop_engine(ap);
1622a0bdff6SSuman Tripathi 	ahci_start_fis_rx(ap);
1631540035dSSuman Tripathi 
1641540035dSSuman Tripathi 	/*
1651540035dSSuman Tripathi 	 * Enable the PxFBS.FBS_EN bit as it
1661540035dSSuman Tripathi 	 * gets cleared due to stopping the engine.
1671540035dSSuman Tripathi 	 */
1681540035dSSuman Tripathi 	if (pp->fbs_supported) {
1691540035dSSuman Tripathi 		fbs = readl(port_mmio + PORT_FBS);
1701540035dSSuman Tripathi 		writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
1711540035dSSuman Tripathi 		fbs = readl(port_mmio + PORT_FBS);
1721540035dSSuman Tripathi 	}
1731540035dSSuman Tripathi 
1742a0bdff6SSuman Tripathi 	hpriv->start_engine(ap);
1752a0bdff6SSuman Tripathi 
1762a0bdff6SSuman Tripathi 	return 0;
1772a0bdff6SSuman Tripathi }
1782a0bdff6SSuman Tripathi 
1792a0bdff6SSuman Tripathi /**
1802a0bdff6SSuman Tripathi  * xgene_ahci_qc_issue - Issue commands to the device
1812a0bdff6SSuman Tripathi  * @qc: Command to issue
1822a0bdff6SSuman Tripathi  *
183a3a84bc7SSuman Tripathi  * Due to Hardware errata for IDENTIFY DEVICE command, the controller cannot
184a3a84bc7SSuman Tripathi  * clear the BSY bit after receiving the PIO setup FIS. This results in the dma
185a3a84bc7SSuman Tripathi  * state machine goes into the CMFatalErrorUpdate state and locks up. By
186a3a84bc7SSuman Tripathi  * restarting the dma engine, it removes the controller out of lock up state.
187a3a84bc7SSuman Tripathi  *
188a3a84bc7SSuman Tripathi  * Due to H/W errata, the controller is unable to save the PMP
189a3a84bc7SSuman Tripathi  * field fetched from command header before sending the H2D FIS.
190a3a84bc7SSuman Tripathi  * When the device returns the PMP port field in the D2H FIS, there is
191a3a84bc7SSuman Tripathi  * a mismatch and results in command completion failure. The
192a3a84bc7SSuman Tripathi  * workaround is to write the pmp value to PxFBS.DEV field before issuing
193a3a84bc7SSuman Tripathi  * any command to PMP.
1942a0bdff6SSuman Tripathi  */
1952a0bdff6SSuman Tripathi static unsigned int xgene_ahci_qc_issue(struct ata_queued_cmd *qc)
1962a0bdff6SSuman Tripathi {
1972a0bdff6SSuman Tripathi 	struct ata_port *ap = qc->ap;
1982a0bdff6SSuman Tripathi 	struct ahci_host_priv *hpriv = ap->host->private_data;
1992a0bdff6SSuman Tripathi 	struct xgene_ahci_context *ctx = hpriv->plat_data;
2002a0bdff6SSuman Tripathi 	int rc = 0;
201a3a84bc7SSuman Tripathi 	u32 port_fbs;
202a3a84bc7SSuman Tripathi 	void *port_mmio = ahci_port_base(ap);
203a3a84bc7SSuman Tripathi 
204a3a84bc7SSuman Tripathi 	/*
205a3a84bc7SSuman Tripathi 	 * Write the pmp value to PxFBS.DEV
206a3a84bc7SSuman Tripathi 	 * for case of Port Mulitplier.
207a3a84bc7SSuman Tripathi 	 */
208a3a84bc7SSuman Tripathi 	if (ctx->class[ap->port_no] == ATA_DEV_PMP) {
209a3a84bc7SSuman Tripathi 		port_fbs = readl(port_mmio + PORT_FBS);
210a3a84bc7SSuman Tripathi 		port_fbs &= ~PORT_FBS_DEV_MASK;
211a3a84bc7SSuman Tripathi 		port_fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
212a3a84bc7SSuman Tripathi 		writel(port_fbs, port_mmio + PORT_FBS);
213a3a84bc7SSuman Tripathi 	}
2142a0bdff6SSuman Tripathi 
2151102407bSSuman Tripathi 	if (unlikely((ctx->last_cmd[ap->port_no] == ATA_CMD_ID_ATA) ||
2161102407bSSuman Tripathi 	    (ctx->last_cmd[ap->port_no] == ATA_CMD_PACKET)))
2172a0bdff6SSuman Tripathi 		xgene_ahci_restart_engine(ap);
2182a0bdff6SSuman Tripathi 
2192a0bdff6SSuman Tripathi 	rc = ahci_qc_issue(qc);
2202a0bdff6SSuman Tripathi 
2212a0bdff6SSuman Tripathi 	/* Save the last command issued */
2222a0bdff6SSuman Tripathi 	ctx->last_cmd[ap->port_no] = qc->tf.command;
2232a0bdff6SSuman Tripathi 
2242a0bdff6SSuman Tripathi 	return rc;
2252a0bdff6SSuman Tripathi }
2262a0bdff6SSuman Tripathi 
2270bed13beSSuman Tripathi static bool xgene_ahci_is_memram_inited(struct xgene_ahci_context *ctx)
2280bed13beSSuman Tripathi {
2290bed13beSSuman Tripathi 	void __iomem *diagcsr = ctx->csr_diag;
2300bed13beSSuman Tripathi 
2310bed13beSSuman Tripathi 	return (readl(diagcsr + CFG_MEM_RAM_SHUTDOWN) == 0 &&
2320bed13beSSuman Tripathi 	        readl(diagcsr + BLOCK_MEM_RDY) == 0xFFFFFFFF);
2330bed13beSSuman Tripathi }
2340bed13beSSuman Tripathi 
2352a0bdff6SSuman Tripathi /**
23681d01bfaSLoc Ho  * xgene_ahci_read_id - Read ID data from the specified device
23781d01bfaSLoc Ho  * @dev: device
23881d01bfaSLoc Ho  * @tf: proposed taskfile
23981d01bfaSLoc Ho  * @id: data buffer
24081d01bfaSLoc Ho  *
24181d01bfaSLoc Ho  * This custom read ID function is required due to the fact that the HW
2422a0bdff6SSuman Tripathi  * does not support DEVSLP.
24381d01bfaSLoc Ho  */
24481d01bfaSLoc Ho static unsigned int xgene_ahci_read_id(struct ata_device *dev,
24581d01bfaSLoc Ho 				       struct ata_taskfile *tf, u16 *id)
24681d01bfaSLoc Ho {
24781d01bfaSLoc Ho 	u32 err_mask;
24881d01bfaSLoc Ho 
24981d01bfaSLoc Ho 	err_mask = ata_do_dev_read_id(dev, tf, id);
25081d01bfaSLoc Ho 	if (err_mask)
25181d01bfaSLoc Ho 		return err_mask;
25281d01bfaSLoc Ho 
25381d01bfaSLoc Ho 	/*
25481d01bfaSLoc Ho 	 * Mask reserved area. Word78 spec of Link Power Management
25581d01bfaSLoc Ho 	 * bit15-8: reserved
25681d01bfaSLoc Ho 	 * bit7: NCQ autosence
25781d01bfaSLoc Ho 	 * bit6: Software settings preservation supported
25881d01bfaSLoc Ho 	 * bit5: reserved
25981d01bfaSLoc Ho 	 * bit4: In-order sata delivery supported
26081d01bfaSLoc Ho 	 * bit3: DIPM requests supported
26181d01bfaSLoc Ho 	 * bit2: DMA Setup FIS Auto-Activate optimization supported
26281d01bfaSLoc Ho 	 * bit1: DMA Setup FIX non-Zero buffer offsets supported
26381d01bfaSLoc Ho 	 * bit0: Reserved
26481d01bfaSLoc Ho 	 *
26581d01bfaSLoc Ho 	 * Clear reserved bit 8 (DEVSLP bit) as we don't support DEVSLP
26681d01bfaSLoc Ho 	 */
2675c0b8e0dSSuman Tripathi 	id[ATA_ID_FEATURE_SUPP] &= cpu_to_le16(~(1 << 8));
26881d01bfaSLoc Ho 
26981d01bfaSLoc Ho 	return 0;
27081d01bfaSLoc Ho }
27181d01bfaSLoc Ho 
27281d01bfaSLoc Ho static void xgene_ahci_set_phy_cfg(struct xgene_ahci_context *ctx, int channel)
27381d01bfaSLoc Ho {
27481d01bfaSLoc Ho 	void __iomem *mmio = ctx->hpriv->mmio;
27581d01bfaSLoc Ho 	u32 val;
27681d01bfaSLoc Ho 
27781d01bfaSLoc Ho 	dev_dbg(ctx->dev, "port configure mmio 0x%p channel %d\n",
27881d01bfaSLoc Ho 		mmio, channel);
27981d01bfaSLoc Ho 	val = readl(mmio + PORTCFG);
28081d01bfaSLoc Ho 	val = PORTADDR_SET(val, channel == 0 ? 2 : 3);
28181d01bfaSLoc Ho 	writel(val, mmio + PORTCFG);
28281d01bfaSLoc Ho 	readl(mmio + PORTCFG);  /* Force a barrier */
28381d01bfaSLoc Ho 	/* Disable fix rate */
28481d01bfaSLoc Ho 	writel(0x0001fffe, mmio + PORTPHY1CFG);
28581d01bfaSLoc Ho 	readl(mmio + PORTPHY1CFG); /* Force a barrier */
2860185b1b7SSuman Tripathi 	writel(0x28183219, mmio + PORTPHY2CFG);
28781d01bfaSLoc Ho 	readl(mmio + PORTPHY2CFG); /* Force a barrier */
2880185b1b7SSuman Tripathi 	writel(0x13081008, mmio + PORTPHY3CFG);
28981d01bfaSLoc Ho 	readl(mmio + PORTPHY3CFG); /* Force a barrier */
2900185b1b7SSuman Tripathi 	writel(0x00480815, mmio + PORTPHY4CFG);
29181d01bfaSLoc Ho 	readl(mmio + PORTPHY4CFG); /* Force a barrier */
29281d01bfaSLoc Ho 	/* Set window negotiation */
29381d01bfaSLoc Ho 	val = readl(mmio + PORTPHY5CFG);
29481d01bfaSLoc Ho 	val = PORTPHY5CFG_RTCHG_SET(val, 0x300);
29581d01bfaSLoc Ho 	writel(val, mmio + PORTPHY5CFG);
29681d01bfaSLoc Ho 	readl(mmio + PORTPHY5CFG); /* Force a barrier */
29781d01bfaSLoc Ho 	val = readl(mmio + PORTAXICFG);
29881d01bfaSLoc Ho 	val = PORTAXICFG_EN_CONTEXT_SET(val, 0x1); /* Enable context mgmt */
29981d01bfaSLoc Ho 	val = PORTAXICFG_OUTTRANS_SET(val, 0xe); /* Set outstanding */
30081d01bfaSLoc Ho 	writel(val, mmio + PORTAXICFG);
30181d01bfaSLoc Ho 	readl(mmio + PORTAXICFG); /* Force a barrier */
302aeae4dcaSSuman Tripathi 	/* Set the watermark threshold of the receive FIFO */
303aeae4dcaSSuman Tripathi 	val = readl(mmio + PORTRANSCFG);
304aeae4dcaSSuman Tripathi 	val = PORTRANSCFG_RXWM_SET(val, 0x30);
305aeae4dcaSSuman Tripathi 	writel(val, mmio + PORTRANSCFG);
30681d01bfaSLoc Ho }
30781d01bfaSLoc Ho 
30881d01bfaSLoc Ho /**
30981d01bfaSLoc Ho  * xgene_ahci_do_hardreset - Issue the actual COMRESET
31081d01bfaSLoc Ho  * @link: link to reset
31181d01bfaSLoc Ho  * @deadline: deadline jiffies for the operation
31281d01bfaSLoc Ho  * @online: Return value to indicate if device online
31381d01bfaSLoc Ho  *
31481d01bfaSLoc Ho  * Due to the limitation of the hardware PHY, a difference set of setting is
31581d01bfaSLoc Ho  * required for each supported disk speed - Gen3 (6.0Gbps), Gen2 (3.0Gbps),
31681d01bfaSLoc Ho  * and Gen1 (1.5Gbps). Otherwise during long IO stress test, the PHY will
31781d01bfaSLoc Ho  * report disparity error and etc. In addition, during COMRESET, there can
31881d01bfaSLoc Ho  * be error reported in the register PORT_SCR_ERR. For SERR_DISPARITY and
3190babe614SSuman Tripathi  * SERR_10B_8B_ERR, the PHY receiver line must be reseted. Also during long
3200babe614SSuman Tripathi  * reboot cycle regression, sometimes the PHY reports link down even if the
3210babe614SSuman Tripathi  * device is present because of speed negotiation failure. so need to retry
3220babe614SSuman Tripathi  * the COMRESET to get the link up. The following algorithm is followed to
3230babe614SSuman Tripathi  * proper configure the hardware PHY during COMRESET:
32481d01bfaSLoc Ho  *
32581d01bfaSLoc Ho  * Alg Part 1:
32681d01bfaSLoc Ho  * 1. Start the PHY at Gen3 speed (default setting)
32781d01bfaSLoc Ho  * 2. Issue the COMRESET
32881d01bfaSLoc Ho  * 3. If no link, go to Alg Part 3
32981d01bfaSLoc Ho  * 4. If link up, determine if the negotiated speed matches the PHY
33081d01bfaSLoc Ho  *    configured speed
33181d01bfaSLoc Ho  * 5. If they matched, go to Alg Part 2
33281d01bfaSLoc Ho  * 6. If they do not matched and first time, configure the PHY for the linked
33381d01bfaSLoc Ho  *    up disk speed and repeat step 2
33481d01bfaSLoc Ho  * 7. Go to Alg Part 2
33581d01bfaSLoc Ho  *
33681d01bfaSLoc Ho  * Alg Part 2:
33781d01bfaSLoc Ho  * 1. On link up, if there are any SERR_DISPARITY and SERR_10B_8B_ERR error
33881d01bfaSLoc Ho  *    reported in the register PORT_SCR_ERR, then reset the PHY receiver line
3390babe614SSuman Tripathi  * 2. Go to Alg Part 4
34081d01bfaSLoc Ho  *
34181d01bfaSLoc Ho  * Alg Part 3:
3420babe614SSuman Tripathi  * 1. Check the PORT_SCR_STAT to see whether device presence detected but PHY
3430babe614SSuman Tripathi  *    communication establishment failed and maximum link down attempts are
3440babe614SSuman Tripathi  *    less than Max attempts 3 then goto Alg Part 1.
3450babe614SSuman Tripathi  * 2. Go to Alg Part 4.
3460babe614SSuman Tripathi  *
3470babe614SSuman Tripathi  * Alg Part 4:
34881d01bfaSLoc Ho  * 1. Clear any pending from register PORT_SCR_ERR.
34981d01bfaSLoc Ho  *
35081d01bfaSLoc Ho  * NOTE: For the initial version, we will NOT support Gen1/Gen2. In addition
35181d01bfaSLoc Ho  *       and until the underlying PHY supports an method to reset the receiver
35281d01bfaSLoc Ho  *       line, on detection of SERR_DISPARITY or SERR_10B_8B_ERR errors,
35381d01bfaSLoc Ho  *       an warning message will be printed.
35481d01bfaSLoc Ho  */
35581d01bfaSLoc Ho static int xgene_ahci_do_hardreset(struct ata_link *link,
35681d01bfaSLoc Ho 				   unsigned long deadline, bool *online)
35781d01bfaSLoc Ho {
35881d01bfaSLoc Ho 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
35981d01bfaSLoc Ho 	struct ata_port *ap = link->ap;
36081d01bfaSLoc Ho 	struct ahci_host_priv *hpriv = ap->host->private_data;
36181d01bfaSLoc Ho 	struct xgene_ahci_context *ctx = hpriv->plat_data;
36281d01bfaSLoc Ho 	struct ahci_port_priv *pp = ap->private_data;
36381d01bfaSLoc Ho 	u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
36481d01bfaSLoc Ho 	void __iomem *port_mmio = ahci_port_base(ap);
36581d01bfaSLoc Ho 	struct ata_taskfile tf;
3660babe614SSuman Tripathi 	int link_down_retry = 0;
36781d01bfaSLoc Ho 	int rc;
3680babe614SSuman Tripathi 	u32 val, sstatus;
36981d01bfaSLoc Ho 
3700babe614SSuman Tripathi 	do {
37181d01bfaSLoc Ho 		/* clear D2H reception area to properly wait for D2H FIS */
37281d01bfaSLoc Ho 		ata_tf_init(link->device, &tf);
37381d01bfaSLoc Ho 		tf.command = ATA_BUSY;
37481d01bfaSLoc Ho 		ata_tf_to_fis(&tf, 0, 0, d2h_fis);
37581d01bfaSLoc Ho 		rc = sata_link_hardreset(link, timing, deadline, online,
37681d01bfaSLoc Ho 				 ahci_check_ready);
3770babe614SSuman Tripathi 		if (*online) {
37881d01bfaSLoc Ho 			val = readl(port_mmio + PORT_SCR_ERR);
37981d01bfaSLoc Ho 			if (val & (SERR_DISPARITY | SERR_10B_8B_ERR))
38081d01bfaSLoc Ho 				dev_warn(ctx->dev, "link has error\n");
3810babe614SSuman Tripathi 			break;
3820babe614SSuman Tripathi 		}
3830babe614SSuman Tripathi 
3840babe614SSuman Tripathi 		sata_scr_read(link, SCR_STATUS, &sstatus);
3850babe614SSuman Tripathi 	} while (link_down_retry++ < MAX_LINK_DOWN_RETRY &&
3860babe614SSuman Tripathi 		 (sstatus & 0xff) == 0x1);
38781d01bfaSLoc Ho 
38881d01bfaSLoc Ho 	/* clear all errors if any pending */
38981d01bfaSLoc Ho 	val = readl(port_mmio + PORT_SCR_ERR);
39081d01bfaSLoc Ho 	writel(val, port_mmio + PORT_SCR_ERR);
39181d01bfaSLoc Ho 
39281d01bfaSLoc Ho 	return rc;
39381d01bfaSLoc Ho }
39481d01bfaSLoc Ho 
39581d01bfaSLoc Ho static int xgene_ahci_hardreset(struct ata_link *link, unsigned int *class,
39681d01bfaSLoc Ho 				unsigned long deadline)
39781d01bfaSLoc Ho {
39881d01bfaSLoc Ho 	struct ata_port *ap = link->ap;
39981d01bfaSLoc Ho         struct ahci_host_priv *hpriv = ap->host->private_data;
40081d01bfaSLoc Ho 	void __iomem *port_mmio = ahci_port_base(ap);
40181d01bfaSLoc Ho 	bool online;
40281d01bfaSLoc Ho 	int rc;
40381d01bfaSLoc Ho 	u32 portcmd_saved;
40481d01bfaSLoc Ho 	u32 portclb_saved;
40581d01bfaSLoc Ho 	u32 portclbhi_saved;
40681d01bfaSLoc Ho 	u32 portrxfis_saved;
40781d01bfaSLoc Ho 	u32 portrxfishi_saved;
40881d01bfaSLoc Ho 
40981d01bfaSLoc Ho 	/* As hardreset resets these CSR, save it to restore later */
41081d01bfaSLoc Ho 	portcmd_saved = readl(port_mmio + PORT_CMD);
41181d01bfaSLoc Ho 	portclb_saved = readl(port_mmio + PORT_LST_ADDR);
41281d01bfaSLoc Ho 	portclbhi_saved = readl(port_mmio + PORT_LST_ADDR_HI);
41381d01bfaSLoc Ho 	portrxfis_saved = readl(port_mmio + PORT_FIS_ADDR);
41481d01bfaSLoc Ho 	portrxfishi_saved = readl(port_mmio + PORT_FIS_ADDR_HI);
41581d01bfaSLoc Ho 
41681d01bfaSLoc Ho 	ahci_stop_engine(ap);
41781d01bfaSLoc Ho 
41881d01bfaSLoc Ho 	rc = xgene_ahci_do_hardreset(link, deadline, &online);
41981d01bfaSLoc Ho 
42081d01bfaSLoc Ho 	/* As controller hardreset clears them, restore them */
42181d01bfaSLoc Ho 	writel(portcmd_saved, port_mmio + PORT_CMD);
42281d01bfaSLoc Ho 	writel(portclb_saved, port_mmio + PORT_LST_ADDR);
42381d01bfaSLoc Ho 	writel(portclbhi_saved, port_mmio + PORT_LST_ADDR_HI);
42481d01bfaSLoc Ho 	writel(portrxfis_saved, port_mmio + PORT_FIS_ADDR);
42581d01bfaSLoc Ho 	writel(portrxfishi_saved, port_mmio + PORT_FIS_ADDR_HI);
42681d01bfaSLoc Ho 
42781d01bfaSLoc Ho 	hpriv->start_engine(ap);
42881d01bfaSLoc Ho 
42981d01bfaSLoc Ho 	if (online)
43081d01bfaSLoc Ho 		*class = ahci_dev_classify(ap);
43181d01bfaSLoc Ho 
43281d01bfaSLoc Ho 	return rc;
43381d01bfaSLoc Ho }
43481d01bfaSLoc Ho 
43581d01bfaSLoc Ho static void xgene_ahci_host_stop(struct ata_host *host)
43681d01bfaSLoc Ho {
43781d01bfaSLoc Ho 	struct ahci_host_priv *hpriv = host->private_data;
43881d01bfaSLoc Ho 
43981d01bfaSLoc Ho 	ahci_platform_disable_resources(hpriv);
44081d01bfaSLoc Ho }
44181d01bfaSLoc Ho 
442a3a84bc7SSuman Tripathi /**
443a3a84bc7SSuman Tripathi  * xgene_ahci_pmp_softreset - Issue the softreset to the drives connected
444a3a84bc7SSuman Tripathi  *                            to Port Multiplier.
445a3a84bc7SSuman Tripathi  * @link: link to reset
446a3a84bc7SSuman Tripathi  * @class: Return value to indicate class of device
447a3a84bc7SSuman Tripathi  * @deadline: deadline jiffies for the operation
448a3a84bc7SSuman Tripathi  *
449a3a84bc7SSuman Tripathi  * Due to H/W errata, the controller is unable to save the PMP
450a3a84bc7SSuman Tripathi  * field fetched from command header before sending the H2D FIS.
451a3a84bc7SSuman Tripathi  * When the device returns the PMP port field in the D2H FIS, there is
452a3a84bc7SSuman Tripathi  * a mismatch and results in command completion failure. The workaround
453a3a84bc7SSuman Tripathi  * is to write the pmp value to PxFBS.DEV field before issuing any command
454a3a84bc7SSuman Tripathi  * to PMP.
455a3a84bc7SSuman Tripathi  */
456a3a84bc7SSuman Tripathi static int xgene_ahci_pmp_softreset(struct ata_link *link, unsigned int *class,
457a3a84bc7SSuman Tripathi 			  unsigned long deadline)
458a3a84bc7SSuman Tripathi {
459a3a84bc7SSuman Tripathi 	int pmp = sata_srst_pmp(link);
460a3a84bc7SSuman Tripathi 	struct ata_port *ap = link->ap;
461a3a84bc7SSuman Tripathi 	u32 rc;
462a3a84bc7SSuman Tripathi 	void *port_mmio = ahci_port_base(ap);
463a3a84bc7SSuman Tripathi 	u32 port_fbs;
464a3a84bc7SSuman Tripathi 
465a3a84bc7SSuman Tripathi 	/*
466a3a84bc7SSuman Tripathi 	 * Set PxFBS.DEV field with pmp
467a3a84bc7SSuman Tripathi 	 * value.
468a3a84bc7SSuman Tripathi 	 */
469a3a84bc7SSuman Tripathi 	port_fbs = readl(port_mmio + PORT_FBS);
470a3a84bc7SSuman Tripathi 	port_fbs &= ~PORT_FBS_DEV_MASK;
471a3a84bc7SSuman Tripathi 	port_fbs |= pmp << PORT_FBS_DEV_OFFSET;
472a3a84bc7SSuman Tripathi 	writel(port_fbs, port_mmio + PORT_FBS);
473a3a84bc7SSuman Tripathi 
474a3a84bc7SSuman Tripathi 	rc = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
475a3a84bc7SSuman Tripathi 
476a3a84bc7SSuman Tripathi 	return rc;
477a3a84bc7SSuman Tripathi }
478a3a84bc7SSuman Tripathi 
479a3a84bc7SSuman Tripathi /**
480a3a84bc7SSuman Tripathi  * xgene_ahci_softreset - Issue the softreset to the drive.
481a3a84bc7SSuman Tripathi  * @link: link to reset
482a3a84bc7SSuman Tripathi  * @class: Return value to indicate class of device
483a3a84bc7SSuman Tripathi  * @deadline: deadline jiffies for the operation
484a3a84bc7SSuman Tripathi  *
485a3a84bc7SSuman Tripathi  * Due to H/W errata, the controller is unable to save the PMP
486a3a84bc7SSuman Tripathi  * field fetched from command header before sending the H2D FIS.
487a3a84bc7SSuman Tripathi  * When the device returns the PMP port field in the D2H FIS, there is
488a3a84bc7SSuman Tripathi  * a mismatch and results in command completion failure. The workaround
489a3a84bc7SSuman Tripathi  * is to write the pmp value to PxFBS.DEV field before issuing any command
490a3a84bc7SSuman Tripathi  * to PMP. Here is the algorithm to detect PMP :
491a3a84bc7SSuman Tripathi  *
492a3a84bc7SSuman Tripathi  * 1. Save the PxFBS value
493a3a84bc7SSuman Tripathi  * 2. Program PxFBS.DEV with pmp value send by framework. Framework sends
494a3a84bc7SSuman Tripathi  *    0xF for both PMP/NON-PMP initially
495a3a84bc7SSuman Tripathi  * 3. Issue softreset
496a3a84bc7SSuman Tripathi  * 4. If signature class is PMP goto 6
497a3a84bc7SSuman Tripathi  * 5. restore the original PxFBS and goto 3
498a3a84bc7SSuman Tripathi  * 6. return
499a3a84bc7SSuman Tripathi  */
500a3a84bc7SSuman Tripathi static int xgene_ahci_softreset(struct ata_link *link, unsigned int *class,
501a3a84bc7SSuman Tripathi 			  unsigned long deadline)
502a3a84bc7SSuman Tripathi {
503a3a84bc7SSuman Tripathi 	int pmp = sata_srst_pmp(link);
504a3a84bc7SSuman Tripathi 	struct ata_port *ap = link->ap;
505a3a84bc7SSuman Tripathi 	struct ahci_host_priv *hpriv = ap->host->private_data;
506a3a84bc7SSuman Tripathi 	struct xgene_ahci_context *ctx = hpriv->plat_data;
507a3a84bc7SSuman Tripathi 	void *port_mmio = ahci_port_base(ap);
508a3a84bc7SSuman Tripathi 	u32 port_fbs;
509a3a84bc7SSuman Tripathi 	u32 port_fbs_save;
510a3a84bc7SSuman Tripathi 	u32 retry = 1;
511a3a84bc7SSuman Tripathi 	u32 rc;
512a3a84bc7SSuman Tripathi 
513a3a84bc7SSuman Tripathi 	port_fbs_save = readl(port_mmio + PORT_FBS);
514a3a84bc7SSuman Tripathi 
515a3a84bc7SSuman Tripathi 	/*
516a3a84bc7SSuman Tripathi 	 * Set PxFBS.DEV field with pmp
517a3a84bc7SSuman Tripathi 	 * value.
518a3a84bc7SSuman Tripathi 	 */
519a3a84bc7SSuman Tripathi 	port_fbs = readl(port_mmio + PORT_FBS);
520a3a84bc7SSuman Tripathi 	port_fbs &= ~PORT_FBS_DEV_MASK;
521a3a84bc7SSuman Tripathi 	port_fbs |= pmp << PORT_FBS_DEV_OFFSET;
522a3a84bc7SSuman Tripathi 	writel(port_fbs, port_mmio + PORT_FBS);
523a3a84bc7SSuman Tripathi 
524a3a84bc7SSuman Tripathi softreset_retry:
525a3a84bc7SSuman Tripathi 	rc = ahci_do_softreset(link, class, pmp,
526a3a84bc7SSuman Tripathi 			       deadline, ahci_check_ready);
527a3a84bc7SSuman Tripathi 
528a3a84bc7SSuman Tripathi 	ctx->class[ap->port_no] = *class;
529a3a84bc7SSuman Tripathi 	if (*class != ATA_DEV_PMP) {
530a3a84bc7SSuman Tripathi 		/*
531a3a84bc7SSuman Tripathi 		 * Retry for normal drives without
532a3a84bc7SSuman Tripathi 		 * setting PxFBS.DEV field with pmp value.
533a3a84bc7SSuman Tripathi 		 */
534a3a84bc7SSuman Tripathi 		if (retry--) {
535a3a84bc7SSuman Tripathi 			writel(port_fbs_save, port_mmio + PORT_FBS);
536a3a84bc7SSuman Tripathi 			goto softreset_retry;
537a3a84bc7SSuman Tripathi 		}
538a3a84bc7SSuman Tripathi 	}
539a3a84bc7SSuman Tripathi 
540a3a84bc7SSuman Tripathi 	return rc;
541a3a84bc7SSuman Tripathi }
542a3a84bc7SSuman Tripathi 
54381d01bfaSLoc Ho static struct ata_port_operations xgene_ahci_ops = {
54481d01bfaSLoc Ho 	.inherits = &ahci_ops,
54581d01bfaSLoc Ho 	.host_stop = xgene_ahci_host_stop,
54681d01bfaSLoc Ho 	.hardreset = xgene_ahci_hardreset,
54781d01bfaSLoc Ho 	.read_id = xgene_ahci_read_id,
5482a0bdff6SSuman Tripathi 	.qc_issue = xgene_ahci_qc_issue,
549a3a84bc7SSuman Tripathi 	.softreset = xgene_ahci_softreset,
550a3a84bc7SSuman Tripathi 	.pmp_softreset = xgene_ahci_pmp_softreset
55181d01bfaSLoc Ho };
55281d01bfaSLoc Ho 
55381d01bfaSLoc Ho static const struct ata_port_info xgene_ahci_port_info = {
5541540035dSSuman Tripathi 	.flags = AHCI_FLAG_COMMON | ATA_FLAG_PMP,
55581d01bfaSLoc Ho 	.pio_mask = ATA_PIO4,
55681d01bfaSLoc Ho 	.udma_mask = ATA_UDMA6,
55781d01bfaSLoc Ho 	.port_ops = &xgene_ahci_ops,
55881d01bfaSLoc Ho };
55981d01bfaSLoc Ho 
56081d01bfaSLoc Ho static int xgene_ahci_hw_init(struct ahci_host_priv *hpriv)
56181d01bfaSLoc Ho {
56281d01bfaSLoc Ho 	struct xgene_ahci_context *ctx = hpriv->plat_data;
56381d01bfaSLoc Ho 	int i;
56481d01bfaSLoc Ho 	int rc;
56581d01bfaSLoc Ho 	u32 val;
56681d01bfaSLoc Ho 
56781d01bfaSLoc Ho 	/* Remove IP RAM out of shutdown */
56881d01bfaSLoc Ho 	rc = xgene_ahci_init_memram(ctx);
56981d01bfaSLoc Ho 	if (rc)
57081d01bfaSLoc Ho 		return rc;
57181d01bfaSLoc Ho 
57281d01bfaSLoc Ho 	for (i = 0; i < MAX_AHCI_CHN_PERCTR; i++)
57381d01bfaSLoc Ho 		xgene_ahci_set_phy_cfg(ctx, i);
57481d01bfaSLoc Ho 
57581d01bfaSLoc Ho 	/* AXI disable Mask */
57681d01bfaSLoc Ho 	writel(0xffffffff, hpriv->mmio + HOST_IRQ_STAT);
57781d01bfaSLoc Ho 	readl(hpriv->mmio + HOST_IRQ_STAT); /* Force a barrier */
57881d01bfaSLoc Ho 	writel(0, ctx->csr_core + INTSTATUSMASK);
5796a96918aSLoc Ho 	val = readl(ctx->csr_core + INTSTATUSMASK); /* Force a barrier */
58081d01bfaSLoc Ho 	dev_dbg(ctx->dev, "top level interrupt mask 0x%X value 0x%08X\n",
58181d01bfaSLoc Ho 		INTSTATUSMASK, val);
58281d01bfaSLoc Ho 
58381d01bfaSLoc Ho 	writel(0x0, ctx->csr_core + ERRINTSTATUSMASK);
58481d01bfaSLoc Ho 	readl(ctx->csr_core + ERRINTSTATUSMASK); /* Force a barrier */
58581d01bfaSLoc Ho 	writel(0x0, ctx->csr_axi + INT_SLV_TMOMASK);
58681d01bfaSLoc Ho 	readl(ctx->csr_axi + INT_SLV_TMOMASK);
58781d01bfaSLoc Ho 
58881d01bfaSLoc Ho 	/* Enable AXI Interrupt */
58981d01bfaSLoc Ho 	writel(0xffffffff, ctx->csr_core + SLVRDERRATTRIBUTES);
59081d01bfaSLoc Ho 	writel(0xffffffff, ctx->csr_core + SLVWRERRATTRIBUTES);
59181d01bfaSLoc Ho 	writel(0xffffffff, ctx->csr_core + MSTRDERRATTRIBUTES);
59281d01bfaSLoc Ho 	writel(0xffffffff, ctx->csr_core + MSTWRERRATTRIBUTES);
59381d01bfaSLoc Ho 
59481d01bfaSLoc Ho 	/* Enable coherency */
59581d01bfaSLoc Ho 	val = readl(ctx->csr_core + BUSCTLREG);
59681d01bfaSLoc Ho 	val &= ~0x00000002;     /* Enable write coherency */
59781d01bfaSLoc Ho 	val &= ~0x00000001;     /* Enable read coherency */
59881d01bfaSLoc Ho 	writel(val, ctx->csr_core + BUSCTLREG);
59981d01bfaSLoc Ho 
60081d01bfaSLoc Ho 	val = readl(ctx->csr_core + IOFMSTRWAUX);
60181d01bfaSLoc Ho 	val |= (1 << 3);        /* Enable read coherency */
60281d01bfaSLoc Ho 	val |= (1 << 9);        /* Enable write coherency */
60381d01bfaSLoc Ho 	writel(val, ctx->csr_core + IOFMSTRWAUX);
60481d01bfaSLoc Ho 	val = readl(ctx->csr_core + IOFMSTRWAUX);
60581d01bfaSLoc Ho 	dev_dbg(ctx->dev, "coherency 0x%X value 0x%08X\n",
60681d01bfaSLoc Ho 		IOFMSTRWAUX, val);
60781d01bfaSLoc Ho 
60881d01bfaSLoc Ho 	return rc;
60981d01bfaSLoc Ho }
61081d01bfaSLoc Ho 
61181d01bfaSLoc Ho static int xgene_ahci_mux_select(struct xgene_ahci_context *ctx)
61281d01bfaSLoc Ho {
61381d01bfaSLoc Ho 	u32 val;
61481d01bfaSLoc Ho 
61581d01bfaSLoc Ho 	/* Check for optional MUX resource */
616a77b6ee9SSuman Tripathi 	if (!ctx->csr_mux)
61781d01bfaSLoc Ho 		return 0;
61881d01bfaSLoc Ho 
61981d01bfaSLoc Ho 	val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
62081d01bfaSLoc Ho 	val &= ~CFG_SATA_ENET_SELECT_MASK;
62181d01bfaSLoc Ho 	writel(val, ctx->csr_mux + SATA_ENET_CONFIG_REG);
62281d01bfaSLoc Ho 	val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
62381d01bfaSLoc Ho 	return val & CFG_SATA_ENET_SELECT_MASK ? -1 : 0;
62481d01bfaSLoc Ho }
62581d01bfaSLoc Ho 
626*018d5ef2SAkinobu Mita static struct scsi_host_template ahci_platform_sht = {
627*018d5ef2SAkinobu Mita 	AHCI_SHT(DRV_NAME),
628*018d5ef2SAkinobu Mita };
629*018d5ef2SAkinobu Mita 
63081d01bfaSLoc Ho static int xgene_ahci_probe(struct platform_device *pdev)
63181d01bfaSLoc Ho {
63281d01bfaSLoc Ho 	struct device *dev = &pdev->dev;
63381d01bfaSLoc Ho 	struct ahci_host_priv *hpriv;
63481d01bfaSLoc Ho 	struct xgene_ahci_context *ctx;
63581d01bfaSLoc Ho 	struct resource *res;
63681d01bfaSLoc Ho 	int rc;
63781d01bfaSLoc Ho 
63881d01bfaSLoc Ho 	hpriv = ahci_platform_get_resources(pdev);
63981d01bfaSLoc Ho 	if (IS_ERR(hpriv))
64081d01bfaSLoc Ho 		return PTR_ERR(hpriv);
64181d01bfaSLoc Ho 
64281d01bfaSLoc Ho 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
64381d01bfaSLoc Ho 	if (!ctx)
64481d01bfaSLoc Ho 		return -ENOMEM;
64581d01bfaSLoc Ho 
64681d01bfaSLoc Ho 	hpriv->plat_data = ctx;
64781d01bfaSLoc Ho 	ctx->hpriv = hpriv;
64881d01bfaSLoc Ho 	ctx->dev = dev;
64981d01bfaSLoc Ho 
65081d01bfaSLoc Ho 	/* Retrieve the IP core resource */
65181d01bfaSLoc Ho 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
65281d01bfaSLoc Ho 	ctx->csr_core = devm_ioremap_resource(dev, res);
65381d01bfaSLoc Ho 	if (IS_ERR(ctx->csr_core))
65481d01bfaSLoc Ho 		return PTR_ERR(ctx->csr_core);
65581d01bfaSLoc Ho 
65681d01bfaSLoc Ho 	/* Retrieve the IP diagnostic resource */
65781d01bfaSLoc Ho 	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
65881d01bfaSLoc Ho 	ctx->csr_diag = devm_ioremap_resource(dev, res);
65981d01bfaSLoc Ho 	if (IS_ERR(ctx->csr_diag))
66081d01bfaSLoc Ho 		return PTR_ERR(ctx->csr_diag);
66181d01bfaSLoc Ho 
66281d01bfaSLoc Ho 	/* Retrieve the IP AXI resource */
66381d01bfaSLoc Ho 	res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
66481d01bfaSLoc Ho 	ctx->csr_axi = devm_ioremap_resource(dev, res);
66581d01bfaSLoc Ho 	if (IS_ERR(ctx->csr_axi))
66681d01bfaSLoc Ho 		return PTR_ERR(ctx->csr_axi);
66781d01bfaSLoc Ho 
66881d01bfaSLoc Ho 	/* Retrieve the optional IP mux resource */
66981d01bfaSLoc Ho 	res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
670a77b6ee9SSuman Tripathi 	if (res) {
671a77b6ee9SSuman Tripathi 		void __iomem *csr = devm_ioremap_resource(dev, res);
672a77b6ee9SSuman Tripathi 		if (IS_ERR(csr))
673a77b6ee9SSuman Tripathi 			return PTR_ERR(csr);
674a77b6ee9SSuman Tripathi 
675a77b6ee9SSuman Tripathi 		ctx->csr_mux = csr;
676a77b6ee9SSuman Tripathi 	}
67781d01bfaSLoc Ho 
67881d01bfaSLoc Ho 	dev_dbg(dev, "VAddr 0x%p Mmio VAddr 0x%p\n", ctx->csr_core,
67981d01bfaSLoc Ho 		hpriv->mmio);
68081d01bfaSLoc Ho 
68181d01bfaSLoc Ho 	/* Select ATA */
68281d01bfaSLoc Ho 	if ((rc = xgene_ahci_mux_select(ctx))) {
68381d01bfaSLoc Ho 		dev_err(dev, "SATA mux selection failed error %d\n", rc);
68481d01bfaSLoc Ho 		return -ENODEV;
68581d01bfaSLoc Ho 	}
68681d01bfaSLoc Ho 
6870bed13beSSuman Tripathi 	if (xgene_ahci_is_memram_inited(ctx)) {
6880bed13beSSuman Tripathi 		dev_info(dev, "skip clock and PHY initialization\n");
6890bed13beSSuman Tripathi 		goto skip_clk_phy;
6900bed13beSSuman Tripathi 	}
6910bed13beSSuman Tripathi 
69281d01bfaSLoc Ho 	/* Due to errata, HW requires full toggle transition */
69381d01bfaSLoc Ho 	rc = ahci_platform_enable_clks(hpriv);
69481d01bfaSLoc Ho 	if (rc)
69581d01bfaSLoc Ho 		goto disable_resources;
69681d01bfaSLoc Ho 	ahci_platform_disable_clks(hpriv);
69781d01bfaSLoc Ho 
69881d01bfaSLoc Ho 	rc = ahci_platform_enable_resources(hpriv);
69981d01bfaSLoc Ho 	if (rc)
70081d01bfaSLoc Ho 		goto disable_resources;
70181d01bfaSLoc Ho 
70281d01bfaSLoc Ho 	/* Configure the host controller */
70381d01bfaSLoc Ho 	xgene_ahci_hw_init(hpriv);
7040bed13beSSuman Tripathi skip_clk_phy:
70572f79f9eSSuman Tripathi 	hpriv->flags = AHCI_HFLAG_NO_PMP | AHCI_HFLAG_NO_NCQ;
706f9f36917SKefeng Wang 
707*018d5ef2SAkinobu Mita 	rc = ahci_platform_init_host(pdev, hpriv, &xgene_ahci_port_info,
708*018d5ef2SAkinobu Mita 				     &ahci_platform_sht);
70981d01bfaSLoc Ho 	if (rc)
71081d01bfaSLoc Ho 		goto disable_resources;
71181d01bfaSLoc Ho 
71281d01bfaSLoc Ho 	dev_dbg(dev, "X-Gene SATA host controller initialized\n");
71381d01bfaSLoc Ho 	return 0;
71481d01bfaSLoc Ho 
71581d01bfaSLoc Ho disable_resources:
71681d01bfaSLoc Ho 	ahci_platform_disable_resources(hpriv);
71781d01bfaSLoc Ho 	return rc;
71881d01bfaSLoc Ho }
71981d01bfaSLoc Ho 
72081d01bfaSLoc Ho static const struct of_device_id xgene_ahci_of_match[] = {
72181d01bfaSLoc Ho 	{.compatible = "apm,xgene-ahci"},
72281d01bfaSLoc Ho 	{},
72381d01bfaSLoc Ho };
72481d01bfaSLoc Ho MODULE_DEVICE_TABLE(of, xgene_ahci_of_match);
72581d01bfaSLoc Ho 
72681d01bfaSLoc Ho static struct platform_driver xgene_ahci_driver = {
72781d01bfaSLoc Ho 	.probe = xgene_ahci_probe,
72881d01bfaSLoc Ho 	.remove = ata_platform_remove_one,
72981d01bfaSLoc Ho 	.driver = {
730*018d5ef2SAkinobu Mita 		.name = DRV_NAME,
73181d01bfaSLoc Ho 		.of_match_table = xgene_ahci_of_match,
73281d01bfaSLoc Ho 	},
73381d01bfaSLoc Ho };
73481d01bfaSLoc Ho 
73581d01bfaSLoc Ho module_platform_driver(xgene_ahci_driver);
73681d01bfaSLoc Ho 
73781d01bfaSLoc Ho MODULE_DESCRIPTION("APM X-Gene AHCI SATA driver");
73881d01bfaSLoc Ho MODULE_AUTHOR("Loc Ho <lho@apm.com>");
73981d01bfaSLoc Ho MODULE_LICENSE("GPL");
74081d01bfaSLoc Ho MODULE_VERSION("0.4");
741