xref: /openbmc/linux/drivers/ata/ahci_xgene.c (revision 0185b1b787962db38fdf74e1cab9b7a2619490a1)
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 
3381d01bfaSLoc Ho /* Max # of disk per a controller */
3481d01bfaSLoc Ho #define MAX_AHCI_CHN_PERCTR		2
3581d01bfaSLoc Ho 
3681d01bfaSLoc Ho /* MUX CSR */
3781d01bfaSLoc Ho #define SATA_ENET_CONFIG_REG		0x00000000
3881d01bfaSLoc Ho #define  CFG_SATA_ENET_SELECT_MASK	0x00000001
3981d01bfaSLoc Ho 
4081d01bfaSLoc Ho /* SATA core host controller CSR */
4181d01bfaSLoc Ho #define SLVRDERRATTRIBUTES		0x00000000
4281d01bfaSLoc Ho #define SLVWRERRATTRIBUTES		0x00000004
4381d01bfaSLoc Ho #define MSTRDERRATTRIBUTES		0x00000008
4481d01bfaSLoc Ho #define MSTWRERRATTRIBUTES		0x0000000c
4581d01bfaSLoc Ho #define BUSCTLREG			0x00000014
4681d01bfaSLoc Ho #define IOFMSTRWAUX			0x00000018
4781d01bfaSLoc Ho #define INTSTATUSMASK			0x0000002c
4881d01bfaSLoc Ho #define ERRINTSTATUS			0x00000030
4981d01bfaSLoc Ho #define ERRINTSTATUSMASK		0x00000034
5081d01bfaSLoc Ho 
5181d01bfaSLoc Ho /* SATA host AHCI CSR */
5281d01bfaSLoc Ho #define PORTCFG				0x000000a4
5381d01bfaSLoc Ho #define  PORTADDR_SET(dst, src) \
5481d01bfaSLoc Ho 		(((dst) & ~0x0000003f) | (((u32)(src)) & 0x0000003f))
5581d01bfaSLoc Ho #define PORTPHY1CFG		0x000000a8
5681d01bfaSLoc Ho #define PORTPHY1CFG_FRCPHYRDY_SET(dst, src) \
5781d01bfaSLoc Ho 		(((dst) & ~0x00100000) | (((u32)(src) << 0x14) & 0x00100000))
5881d01bfaSLoc Ho #define PORTPHY2CFG			0x000000ac
5981d01bfaSLoc Ho #define PORTPHY3CFG			0x000000b0
6081d01bfaSLoc Ho #define PORTPHY4CFG			0x000000b4
6181d01bfaSLoc Ho #define PORTPHY5CFG			0x000000b8
6281d01bfaSLoc Ho #define SCTL0				0x0000012C
6381d01bfaSLoc Ho #define PORTPHY5CFG_RTCHG_SET(dst, src) \
6481d01bfaSLoc Ho 		(((dst) & ~0xfff00000) | (((u32)(src) << 0x14) & 0xfff00000))
6581d01bfaSLoc Ho #define PORTAXICFG_EN_CONTEXT_SET(dst, src) \
6681d01bfaSLoc Ho 		(((dst) & ~0x01000000) | (((u32)(src) << 0x18) & 0x01000000))
6781d01bfaSLoc Ho #define PORTAXICFG			0x000000bc
6881d01bfaSLoc Ho #define PORTAXICFG_OUTTRANS_SET(dst, src) \
6981d01bfaSLoc Ho 		(((dst) & ~0x00f00000) | (((u32)(src) << 0x14) & 0x00f00000))
70aeae4dcaSSuman Tripathi #define PORTRANSCFG			0x000000c8
71aeae4dcaSSuman Tripathi #define PORTRANSCFG_RXWM_SET(dst, src)		\
72aeae4dcaSSuman Tripathi 		(((dst) & ~0x0000007f) | (((u32)(src)) & 0x0000007f))
7381d01bfaSLoc Ho 
7481d01bfaSLoc Ho /* SATA host controller AXI CSR */
7581d01bfaSLoc Ho #define INT_SLV_TMOMASK			0x00000010
7681d01bfaSLoc Ho 
7781d01bfaSLoc Ho /* SATA diagnostic CSR */
7881d01bfaSLoc Ho #define CFG_MEM_RAM_SHUTDOWN		0x00000070
7981d01bfaSLoc Ho #define BLOCK_MEM_RDY			0x00000074
8081d01bfaSLoc Ho 
8181d01bfaSLoc Ho struct xgene_ahci_context {
8281d01bfaSLoc Ho 	struct ahci_host_priv *hpriv;
8381d01bfaSLoc Ho 	struct device *dev;
8481d01bfaSLoc Ho 	void __iomem *csr_core;		/* Core CSR address of IP */
8581d01bfaSLoc Ho 	void __iomem *csr_diag;		/* Diag CSR address of IP */
8681d01bfaSLoc Ho 	void __iomem *csr_axi;		/* AXI CSR address of IP */
8781d01bfaSLoc Ho 	void __iomem *csr_mux;		/* MUX CSR address of IP */
8881d01bfaSLoc Ho };
8981d01bfaSLoc Ho 
9081d01bfaSLoc Ho static int xgene_ahci_init_memram(struct xgene_ahci_context *ctx)
9181d01bfaSLoc Ho {
9281d01bfaSLoc Ho 	dev_dbg(ctx->dev, "Release memory from shutdown\n");
9381d01bfaSLoc Ho 	writel(0x0, ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN);
9481d01bfaSLoc Ho 	readl(ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN); /* Force a barrier */
9581d01bfaSLoc Ho 	msleep(1);	/* reset may take up to 1ms */
9681d01bfaSLoc Ho 	if (readl(ctx->csr_diag + BLOCK_MEM_RDY) != 0xFFFFFFFF) {
9781d01bfaSLoc Ho 		dev_err(ctx->dev, "failed to release memory from shutdown\n");
9881d01bfaSLoc Ho 		return -ENODEV;
9981d01bfaSLoc Ho 	}
10081d01bfaSLoc Ho 	return 0;
10181d01bfaSLoc Ho }
10281d01bfaSLoc Ho 
10381d01bfaSLoc Ho /**
10481d01bfaSLoc Ho  * xgene_ahci_read_id - Read ID data from the specified device
10581d01bfaSLoc Ho  * @dev: device
10681d01bfaSLoc Ho  * @tf: proposed taskfile
10781d01bfaSLoc Ho  * @id: data buffer
10881d01bfaSLoc Ho  *
10981d01bfaSLoc Ho  * This custom read ID function is required due to the fact that the HW
11081d01bfaSLoc Ho  * does not support DEVSLP and the controller state machine may get stuck
11181d01bfaSLoc Ho  * after processing the ID query command.
11281d01bfaSLoc Ho  */
11381d01bfaSLoc Ho static unsigned int xgene_ahci_read_id(struct ata_device *dev,
11481d01bfaSLoc Ho 				       struct ata_taskfile *tf, u16 *id)
11581d01bfaSLoc Ho {
11681d01bfaSLoc Ho 	u32 err_mask;
11781d01bfaSLoc Ho 	void __iomem *port_mmio = ahci_port_base(dev->link->ap);
11881d01bfaSLoc Ho 
11981d01bfaSLoc Ho 	err_mask = ata_do_dev_read_id(dev, tf, id);
12081d01bfaSLoc Ho 	if (err_mask)
12181d01bfaSLoc Ho 		return err_mask;
12281d01bfaSLoc Ho 
12381d01bfaSLoc Ho 	/*
12481d01bfaSLoc Ho 	 * Mask reserved area. Word78 spec of Link Power Management
12581d01bfaSLoc Ho 	 * bit15-8: reserved
12681d01bfaSLoc Ho 	 * bit7: NCQ autosence
12781d01bfaSLoc Ho 	 * bit6: Software settings preservation supported
12881d01bfaSLoc Ho 	 * bit5: reserved
12981d01bfaSLoc Ho 	 * bit4: In-order sata delivery supported
13081d01bfaSLoc Ho 	 * bit3: DIPM requests supported
13181d01bfaSLoc Ho 	 * bit2: DMA Setup FIS Auto-Activate optimization supported
13281d01bfaSLoc Ho 	 * bit1: DMA Setup FIX non-Zero buffer offsets supported
13381d01bfaSLoc Ho 	 * bit0: Reserved
13481d01bfaSLoc Ho 	 *
13581d01bfaSLoc Ho 	 * Clear reserved bit 8 (DEVSLP bit) as we don't support DEVSLP
13681d01bfaSLoc Ho 	 */
13781d01bfaSLoc Ho 	id[ATA_ID_FEATURE_SUPP] &= ~(1 << 8);
13881d01bfaSLoc Ho 
13981d01bfaSLoc Ho 	/*
14081d01bfaSLoc Ho 	 * Due to HW errata, restart the port if no other command active.
14181d01bfaSLoc Ho 	 * Otherwise the controller may get stuck.
14281d01bfaSLoc Ho 	 */
14381d01bfaSLoc Ho 	if (!readl(port_mmio + PORT_CMD_ISSUE)) {
14481d01bfaSLoc Ho 		writel(PORT_CMD_FIS_RX, port_mmio + PORT_CMD);
14581d01bfaSLoc Ho 		readl(port_mmio + PORT_CMD);	/* Force a barrier */
14681d01bfaSLoc Ho 		writel(PORT_CMD_FIS_RX | PORT_CMD_START, port_mmio + PORT_CMD);
14781d01bfaSLoc Ho 		readl(port_mmio + PORT_CMD);	/* Force a barrier */
14881d01bfaSLoc Ho 	}
14981d01bfaSLoc Ho 	return 0;
15081d01bfaSLoc Ho }
15181d01bfaSLoc Ho 
15281d01bfaSLoc Ho static void xgene_ahci_set_phy_cfg(struct xgene_ahci_context *ctx, int channel)
15381d01bfaSLoc Ho {
15481d01bfaSLoc Ho 	void __iomem *mmio = ctx->hpriv->mmio;
15581d01bfaSLoc Ho 	u32 val;
15681d01bfaSLoc Ho 
15781d01bfaSLoc Ho 	dev_dbg(ctx->dev, "port configure mmio 0x%p channel %d\n",
15881d01bfaSLoc Ho 		mmio, channel);
15981d01bfaSLoc Ho 	val = readl(mmio + PORTCFG);
16081d01bfaSLoc Ho 	val = PORTADDR_SET(val, channel == 0 ? 2 : 3);
16181d01bfaSLoc Ho 	writel(val, mmio + PORTCFG);
16281d01bfaSLoc Ho 	readl(mmio + PORTCFG);  /* Force a barrier */
16381d01bfaSLoc Ho 	/* Disable fix rate */
16481d01bfaSLoc Ho 	writel(0x0001fffe, mmio + PORTPHY1CFG);
16581d01bfaSLoc Ho 	readl(mmio + PORTPHY1CFG); /* Force a barrier */
166*0185b1b7SSuman Tripathi 	writel(0x28183219, mmio + PORTPHY2CFG);
16781d01bfaSLoc Ho 	readl(mmio + PORTPHY2CFG); /* Force a barrier */
168*0185b1b7SSuman Tripathi 	writel(0x13081008, mmio + PORTPHY3CFG);
16981d01bfaSLoc Ho 	readl(mmio + PORTPHY3CFG); /* Force a barrier */
170*0185b1b7SSuman Tripathi 	writel(0x00480815, mmio + PORTPHY4CFG);
17181d01bfaSLoc Ho 	readl(mmio + PORTPHY4CFG); /* Force a barrier */
17281d01bfaSLoc Ho 	/* Set window negotiation */
17381d01bfaSLoc Ho 	val = readl(mmio + PORTPHY5CFG);
17481d01bfaSLoc Ho 	val = PORTPHY5CFG_RTCHG_SET(val, 0x300);
17581d01bfaSLoc Ho 	writel(val, mmio + PORTPHY5CFG);
17681d01bfaSLoc Ho 	readl(mmio + PORTPHY5CFG); /* Force a barrier */
17781d01bfaSLoc Ho 	val = readl(mmio + PORTAXICFG);
17881d01bfaSLoc Ho 	val = PORTAXICFG_EN_CONTEXT_SET(val, 0x1); /* Enable context mgmt */
17981d01bfaSLoc Ho 	val = PORTAXICFG_OUTTRANS_SET(val, 0xe); /* Set outstanding */
18081d01bfaSLoc Ho 	writel(val, mmio + PORTAXICFG);
18181d01bfaSLoc Ho 	readl(mmio + PORTAXICFG); /* Force a barrier */
182aeae4dcaSSuman Tripathi 	/* Set the watermark threshold of the receive FIFO */
183aeae4dcaSSuman Tripathi 	val = readl(mmio + PORTRANSCFG);
184aeae4dcaSSuman Tripathi 	val = PORTRANSCFG_RXWM_SET(val, 0x30);
185aeae4dcaSSuman Tripathi 	writel(val, mmio + PORTRANSCFG);
18681d01bfaSLoc Ho }
18781d01bfaSLoc Ho 
18881d01bfaSLoc Ho /**
18981d01bfaSLoc Ho  * xgene_ahci_do_hardreset - Issue the actual COMRESET
19081d01bfaSLoc Ho  * @link: link to reset
19181d01bfaSLoc Ho  * @deadline: deadline jiffies for the operation
19281d01bfaSLoc Ho  * @online: Return value to indicate if device online
19381d01bfaSLoc Ho  *
19481d01bfaSLoc Ho  * Due to the limitation of the hardware PHY, a difference set of setting is
19581d01bfaSLoc Ho  * required for each supported disk speed - Gen3 (6.0Gbps), Gen2 (3.0Gbps),
19681d01bfaSLoc Ho  * and Gen1 (1.5Gbps). Otherwise during long IO stress test, the PHY will
19781d01bfaSLoc Ho  * report disparity error and etc. In addition, during COMRESET, there can
19881d01bfaSLoc Ho  * be error reported in the register PORT_SCR_ERR. For SERR_DISPARITY and
19981d01bfaSLoc Ho  * SERR_10B_8B_ERR, the PHY receiver line must be reseted. The following
20081d01bfaSLoc Ho  * algorithm is followed to proper configure the hardware PHY during COMRESET:
20181d01bfaSLoc Ho  *
20281d01bfaSLoc Ho  * Alg Part 1:
20381d01bfaSLoc Ho  * 1. Start the PHY at Gen3 speed (default setting)
20481d01bfaSLoc Ho  * 2. Issue the COMRESET
20581d01bfaSLoc Ho  * 3. If no link, go to Alg Part 3
20681d01bfaSLoc Ho  * 4. If link up, determine if the negotiated speed matches the PHY
20781d01bfaSLoc Ho  *    configured speed
20881d01bfaSLoc Ho  * 5. If they matched, go to Alg Part 2
20981d01bfaSLoc Ho  * 6. If they do not matched and first time, configure the PHY for the linked
21081d01bfaSLoc Ho  *    up disk speed and repeat step 2
21181d01bfaSLoc Ho  * 7. Go to Alg Part 2
21281d01bfaSLoc Ho  *
21381d01bfaSLoc Ho  * Alg Part 2:
21481d01bfaSLoc Ho  * 1. On link up, if there are any SERR_DISPARITY and SERR_10B_8B_ERR error
21581d01bfaSLoc Ho  *    reported in the register PORT_SCR_ERR, then reset the PHY receiver line
21681d01bfaSLoc Ho  * 2. Go to Alg Part 3
21781d01bfaSLoc Ho  *
21881d01bfaSLoc Ho  * Alg Part 3:
21981d01bfaSLoc Ho  * 1. Clear any pending from register PORT_SCR_ERR.
22081d01bfaSLoc Ho  *
22181d01bfaSLoc Ho  * NOTE: For the initial version, we will NOT support Gen1/Gen2. In addition
22281d01bfaSLoc Ho  *       and until the underlying PHY supports an method to reset the receiver
22381d01bfaSLoc Ho  *       line, on detection of SERR_DISPARITY or SERR_10B_8B_ERR errors,
22481d01bfaSLoc Ho  *       an warning message will be printed.
22581d01bfaSLoc Ho  */
22681d01bfaSLoc Ho static int xgene_ahci_do_hardreset(struct ata_link *link,
22781d01bfaSLoc Ho 				   unsigned long deadline, bool *online)
22881d01bfaSLoc Ho {
22981d01bfaSLoc Ho 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
23081d01bfaSLoc Ho 	struct ata_port *ap = link->ap;
23181d01bfaSLoc Ho 	struct ahci_host_priv *hpriv = ap->host->private_data;
23281d01bfaSLoc Ho 	struct xgene_ahci_context *ctx = hpriv->plat_data;
23381d01bfaSLoc Ho 	struct ahci_port_priv *pp = ap->private_data;
23481d01bfaSLoc Ho 	u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
23581d01bfaSLoc Ho 	void __iomem *port_mmio = ahci_port_base(ap);
23681d01bfaSLoc Ho 	struct ata_taskfile tf;
23781d01bfaSLoc Ho 	int rc;
23881d01bfaSLoc Ho 	u32 val;
23981d01bfaSLoc Ho 
24081d01bfaSLoc Ho 	/* clear D2H reception area to properly wait for D2H FIS */
24181d01bfaSLoc Ho 	ata_tf_init(link->device, &tf);
24281d01bfaSLoc Ho 	tf.command = ATA_BUSY;
24381d01bfaSLoc Ho 	ata_tf_to_fis(&tf, 0, 0, d2h_fis);
24481d01bfaSLoc Ho 	rc = sata_link_hardreset(link, timing, deadline, online,
24581d01bfaSLoc Ho 				 ahci_check_ready);
24681d01bfaSLoc Ho 
24781d01bfaSLoc Ho 	val = readl(port_mmio + PORT_SCR_ERR);
24881d01bfaSLoc Ho 	if (val & (SERR_DISPARITY | SERR_10B_8B_ERR))
24981d01bfaSLoc Ho 		dev_warn(ctx->dev, "link has error\n");
25081d01bfaSLoc Ho 
25181d01bfaSLoc Ho 	/* clear all errors if any pending */
25281d01bfaSLoc Ho 	val = readl(port_mmio + PORT_SCR_ERR);
25381d01bfaSLoc Ho 	writel(val, port_mmio + PORT_SCR_ERR);
25481d01bfaSLoc Ho 
25581d01bfaSLoc Ho 	return rc;
25681d01bfaSLoc Ho }
25781d01bfaSLoc Ho 
25881d01bfaSLoc Ho static int xgene_ahci_hardreset(struct ata_link *link, unsigned int *class,
25981d01bfaSLoc Ho 				unsigned long deadline)
26081d01bfaSLoc Ho {
26181d01bfaSLoc Ho 	struct ata_port *ap = link->ap;
26281d01bfaSLoc Ho         struct ahci_host_priv *hpriv = ap->host->private_data;
26381d01bfaSLoc Ho 	void __iomem *port_mmio = ahci_port_base(ap);
26481d01bfaSLoc Ho 	bool online;
26581d01bfaSLoc Ho 	int rc;
26681d01bfaSLoc Ho 	u32 portcmd_saved;
26781d01bfaSLoc Ho 	u32 portclb_saved;
26881d01bfaSLoc Ho 	u32 portclbhi_saved;
26981d01bfaSLoc Ho 	u32 portrxfis_saved;
27081d01bfaSLoc Ho 	u32 portrxfishi_saved;
27181d01bfaSLoc Ho 
27281d01bfaSLoc Ho 	/* As hardreset resets these CSR, save it to restore later */
27381d01bfaSLoc Ho 	portcmd_saved = readl(port_mmio + PORT_CMD);
27481d01bfaSLoc Ho 	portclb_saved = readl(port_mmio + PORT_LST_ADDR);
27581d01bfaSLoc Ho 	portclbhi_saved = readl(port_mmio + PORT_LST_ADDR_HI);
27681d01bfaSLoc Ho 	portrxfis_saved = readl(port_mmio + PORT_FIS_ADDR);
27781d01bfaSLoc Ho 	portrxfishi_saved = readl(port_mmio + PORT_FIS_ADDR_HI);
27881d01bfaSLoc Ho 
27981d01bfaSLoc Ho 	ahci_stop_engine(ap);
28081d01bfaSLoc Ho 
28181d01bfaSLoc Ho 	rc = xgene_ahci_do_hardreset(link, deadline, &online);
28281d01bfaSLoc Ho 
28381d01bfaSLoc Ho 	/* As controller hardreset clears them, restore them */
28481d01bfaSLoc Ho 	writel(portcmd_saved, port_mmio + PORT_CMD);
28581d01bfaSLoc Ho 	writel(portclb_saved, port_mmio + PORT_LST_ADDR);
28681d01bfaSLoc Ho 	writel(portclbhi_saved, port_mmio + PORT_LST_ADDR_HI);
28781d01bfaSLoc Ho 	writel(portrxfis_saved, port_mmio + PORT_FIS_ADDR);
28881d01bfaSLoc Ho 	writel(portrxfishi_saved, port_mmio + PORT_FIS_ADDR_HI);
28981d01bfaSLoc Ho 
29081d01bfaSLoc Ho 	hpriv->start_engine(ap);
29181d01bfaSLoc Ho 
29281d01bfaSLoc Ho 	if (online)
29381d01bfaSLoc Ho 		*class = ahci_dev_classify(ap);
29481d01bfaSLoc Ho 
29581d01bfaSLoc Ho 	return rc;
29681d01bfaSLoc Ho }
29781d01bfaSLoc Ho 
29881d01bfaSLoc Ho static void xgene_ahci_host_stop(struct ata_host *host)
29981d01bfaSLoc Ho {
30081d01bfaSLoc Ho 	struct ahci_host_priv *hpriv = host->private_data;
30181d01bfaSLoc Ho 
30281d01bfaSLoc Ho 	ahci_platform_disable_resources(hpriv);
30381d01bfaSLoc Ho }
30481d01bfaSLoc Ho 
30581d01bfaSLoc Ho static struct ata_port_operations xgene_ahci_ops = {
30681d01bfaSLoc Ho 	.inherits = &ahci_ops,
30781d01bfaSLoc Ho 	.host_stop = xgene_ahci_host_stop,
30881d01bfaSLoc Ho 	.hardreset = xgene_ahci_hardreset,
30981d01bfaSLoc Ho 	.read_id = xgene_ahci_read_id,
31081d01bfaSLoc Ho };
31181d01bfaSLoc Ho 
31281d01bfaSLoc Ho static const struct ata_port_info xgene_ahci_port_info = {
31381d01bfaSLoc Ho 	.flags = AHCI_FLAG_COMMON | ATA_FLAG_NCQ,
31481d01bfaSLoc Ho 	.pio_mask = ATA_PIO4,
31581d01bfaSLoc Ho 	.udma_mask = ATA_UDMA6,
31681d01bfaSLoc Ho 	.port_ops = &xgene_ahci_ops,
31781d01bfaSLoc Ho };
31881d01bfaSLoc Ho 
31981d01bfaSLoc Ho static int xgene_ahci_hw_init(struct ahci_host_priv *hpriv)
32081d01bfaSLoc Ho {
32181d01bfaSLoc Ho 	struct xgene_ahci_context *ctx = hpriv->plat_data;
32281d01bfaSLoc Ho 	int i;
32381d01bfaSLoc Ho 	int rc;
32481d01bfaSLoc Ho 	u32 val;
32581d01bfaSLoc Ho 
32681d01bfaSLoc Ho 	/* Remove IP RAM out of shutdown */
32781d01bfaSLoc Ho 	rc = xgene_ahci_init_memram(ctx);
32881d01bfaSLoc Ho 	if (rc)
32981d01bfaSLoc Ho 		return rc;
33081d01bfaSLoc Ho 
33181d01bfaSLoc Ho 	for (i = 0; i < MAX_AHCI_CHN_PERCTR; i++)
33281d01bfaSLoc Ho 		xgene_ahci_set_phy_cfg(ctx, i);
33381d01bfaSLoc Ho 
33481d01bfaSLoc Ho 	/* AXI disable Mask */
33581d01bfaSLoc Ho 	writel(0xffffffff, hpriv->mmio + HOST_IRQ_STAT);
33681d01bfaSLoc Ho 	readl(hpriv->mmio + HOST_IRQ_STAT); /* Force a barrier */
33781d01bfaSLoc Ho 	writel(0, ctx->csr_core + INTSTATUSMASK);
3386a96918aSLoc Ho 	val = readl(ctx->csr_core + INTSTATUSMASK); /* Force a barrier */
33981d01bfaSLoc Ho 	dev_dbg(ctx->dev, "top level interrupt mask 0x%X value 0x%08X\n",
34081d01bfaSLoc Ho 		INTSTATUSMASK, val);
34181d01bfaSLoc Ho 
34281d01bfaSLoc Ho 	writel(0x0, ctx->csr_core + ERRINTSTATUSMASK);
34381d01bfaSLoc Ho 	readl(ctx->csr_core + ERRINTSTATUSMASK); /* Force a barrier */
34481d01bfaSLoc Ho 	writel(0x0, ctx->csr_axi + INT_SLV_TMOMASK);
34581d01bfaSLoc Ho 	readl(ctx->csr_axi + INT_SLV_TMOMASK);
34681d01bfaSLoc Ho 
34781d01bfaSLoc Ho 	/* Enable AXI Interrupt */
34881d01bfaSLoc Ho 	writel(0xffffffff, ctx->csr_core + SLVRDERRATTRIBUTES);
34981d01bfaSLoc Ho 	writel(0xffffffff, ctx->csr_core + SLVWRERRATTRIBUTES);
35081d01bfaSLoc Ho 	writel(0xffffffff, ctx->csr_core + MSTRDERRATTRIBUTES);
35181d01bfaSLoc Ho 	writel(0xffffffff, ctx->csr_core + MSTWRERRATTRIBUTES);
35281d01bfaSLoc Ho 
35381d01bfaSLoc Ho 	/* Enable coherency */
35481d01bfaSLoc Ho 	val = readl(ctx->csr_core + BUSCTLREG);
35581d01bfaSLoc Ho 	val &= ~0x00000002;     /* Enable write coherency */
35681d01bfaSLoc Ho 	val &= ~0x00000001;     /* Enable read coherency */
35781d01bfaSLoc Ho 	writel(val, ctx->csr_core + BUSCTLREG);
35881d01bfaSLoc Ho 
35981d01bfaSLoc Ho 	val = readl(ctx->csr_core + IOFMSTRWAUX);
36081d01bfaSLoc Ho 	val |= (1 << 3);        /* Enable read coherency */
36181d01bfaSLoc Ho 	val |= (1 << 9);        /* Enable write coherency */
36281d01bfaSLoc Ho 	writel(val, ctx->csr_core + IOFMSTRWAUX);
36381d01bfaSLoc Ho 	val = readl(ctx->csr_core + IOFMSTRWAUX);
36481d01bfaSLoc Ho 	dev_dbg(ctx->dev, "coherency 0x%X value 0x%08X\n",
36581d01bfaSLoc Ho 		IOFMSTRWAUX, val);
36681d01bfaSLoc Ho 
36781d01bfaSLoc Ho 	return rc;
36881d01bfaSLoc Ho }
36981d01bfaSLoc Ho 
37081d01bfaSLoc Ho static int xgene_ahci_mux_select(struct xgene_ahci_context *ctx)
37181d01bfaSLoc Ho {
37281d01bfaSLoc Ho 	u32 val;
37381d01bfaSLoc Ho 
37481d01bfaSLoc Ho 	/* Check for optional MUX resource */
37581d01bfaSLoc Ho 	if (IS_ERR(ctx->csr_mux))
37681d01bfaSLoc Ho 		return 0;
37781d01bfaSLoc Ho 
37881d01bfaSLoc Ho 	val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
37981d01bfaSLoc Ho 	val &= ~CFG_SATA_ENET_SELECT_MASK;
38081d01bfaSLoc Ho 	writel(val, ctx->csr_mux + SATA_ENET_CONFIG_REG);
38181d01bfaSLoc Ho 	val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
38281d01bfaSLoc Ho 	return val & CFG_SATA_ENET_SELECT_MASK ? -1 : 0;
38381d01bfaSLoc Ho }
38481d01bfaSLoc Ho 
38581d01bfaSLoc Ho static int xgene_ahci_probe(struct platform_device *pdev)
38681d01bfaSLoc Ho {
38781d01bfaSLoc Ho 	struct device *dev = &pdev->dev;
38881d01bfaSLoc Ho 	struct ahci_host_priv *hpriv;
38981d01bfaSLoc Ho 	struct xgene_ahci_context *ctx;
39081d01bfaSLoc Ho 	struct resource *res;
391f9f36917SKefeng Wang 	unsigned long hflags;
39281d01bfaSLoc Ho 	int rc;
39381d01bfaSLoc Ho 
39481d01bfaSLoc Ho 	hpriv = ahci_platform_get_resources(pdev);
39581d01bfaSLoc Ho 	if (IS_ERR(hpriv))
39681d01bfaSLoc Ho 		return PTR_ERR(hpriv);
39781d01bfaSLoc Ho 
39881d01bfaSLoc Ho 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
39981d01bfaSLoc Ho 	if (!ctx)
40081d01bfaSLoc Ho 		return -ENOMEM;
40181d01bfaSLoc Ho 
40281d01bfaSLoc Ho 	hpriv->plat_data = ctx;
40381d01bfaSLoc Ho 	ctx->hpriv = hpriv;
40481d01bfaSLoc Ho 	ctx->dev = dev;
40581d01bfaSLoc Ho 
40681d01bfaSLoc Ho 	/* Retrieve the IP core resource */
40781d01bfaSLoc Ho 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
40881d01bfaSLoc Ho 	ctx->csr_core = devm_ioremap_resource(dev, res);
40981d01bfaSLoc Ho 	if (IS_ERR(ctx->csr_core))
41081d01bfaSLoc Ho 		return PTR_ERR(ctx->csr_core);
41181d01bfaSLoc Ho 
41281d01bfaSLoc Ho 	/* Retrieve the IP diagnostic resource */
41381d01bfaSLoc Ho 	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
41481d01bfaSLoc Ho 	ctx->csr_diag = devm_ioremap_resource(dev, res);
41581d01bfaSLoc Ho 	if (IS_ERR(ctx->csr_diag))
41681d01bfaSLoc Ho 		return PTR_ERR(ctx->csr_diag);
41781d01bfaSLoc Ho 
41881d01bfaSLoc Ho 	/* Retrieve the IP AXI resource */
41981d01bfaSLoc Ho 	res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
42081d01bfaSLoc Ho 	ctx->csr_axi = devm_ioremap_resource(dev, res);
42181d01bfaSLoc Ho 	if (IS_ERR(ctx->csr_axi))
42281d01bfaSLoc Ho 		return PTR_ERR(ctx->csr_axi);
42381d01bfaSLoc Ho 
42481d01bfaSLoc Ho 	/* Retrieve the optional IP mux resource */
42581d01bfaSLoc Ho 	res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
42681d01bfaSLoc Ho 	ctx->csr_mux = devm_ioremap_resource(dev, res);
42781d01bfaSLoc Ho 
42881d01bfaSLoc Ho 	dev_dbg(dev, "VAddr 0x%p Mmio VAddr 0x%p\n", ctx->csr_core,
42981d01bfaSLoc Ho 		hpriv->mmio);
43081d01bfaSLoc Ho 
43181d01bfaSLoc Ho 	/* Select ATA */
43281d01bfaSLoc Ho 	if ((rc = xgene_ahci_mux_select(ctx))) {
43381d01bfaSLoc Ho 		dev_err(dev, "SATA mux selection failed error %d\n", rc);
43481d01bfaSLoc Ho 		return -ENODEV;
43581d01bfaSLoc Ho 	}
43681d01bfaSLoc Ho 
43781d01bfaSLoc Ho 	/* Due to errata, HW requires full toggle transition */
43881d01bfaSLoc Ho 	rc = ahci_platform_enable_clks(hpriv);
43981d01bfaSLoc Ho 	if (rc)
44081d01bfaSLoc Ho 		goto disable_resources;
44181d01bfaSLoc Ho 	ahci_platform_disable_clks(hpriv);
44281d01bfaSLoc Ho 
44381d01bfaSLoc Ho 	rc = ahci_platform_enable_resources(hpriv);
44481d01bfaSLoc Ho 	if (rc)
44581d01bfaSLoc Ho 		goto disable_resources;
44681d01bfaSLoc Ho 
44781d01bfaSLoc Ho 	/* Configure the host controller */
44881d01bfaSLoc Ho 	xgene_ahci_hw_init(hpriv);
44981d01bfaSLoc Ho 
450f9f36917SKefeng Wang 	hflags = AHCI_HFLAG_NO_PMP | AHCI_HFLAG_YES_NCQ;
451f9f36917SKefeng Wang 
452f9f36917SKefeng Wang 	rc = ahci_platform_init_host(pdev, hpriv, &xgene_ahci_port_info,
453f9f36917SKefeng Wang 				     hflags, 0, 0);
45481d01bfaSLoc Ho 	if (rc)
45581d01bfaSLoc Ho 		goto disable_resources;
45681d01bfaSLoc Ho 
45781d01bfaSLoc Ho 	dev_dbg(dev, "X-Gene SATA host controller initialized\n");
45881d01bfaSLoc Ho 	return 0;
45981d01bfaSLoc Ho 
46081d01bfaSLoc Ho disable_resources:
46181d01bfaSLoc Ho 	ahci_platform_disable_resources(hpriv);
46281d01bfaSLoc Ho 	return rc;
46381d01bfaSLoc Ho }
46481d01bfaSLoc Ho 
46581d01bfaSLoc Ho static const struct of_device_id xgene_ahci_of_match[] = {
46681d01bfaSLoc Ho 	{.compatible = "apm,xgene-ahci"},
46781d01bfaSLoc Ho 	{},
46881d01bfaSLoc Ho };
46981d01bfaSLoc Ho MODULE_DEVICE_TABLE(of, xgene_ahci_of_match);
47081d01bfaSLoc Ho 
47181d01bfaSLoc Ho static struct platform_driver xgene_ahci_driver = {
47281d01bfaSLoc Ho 	.probe = xgene_ahci_probe,
47381d01bfaSLoc Ho 	.remove = ata_platform_remove_one,
47481d01bfaSLoc Ho 	.driver = {
47581d01bfaSLoc Ho 		.name = "xgene-ahci",
47681d01bfaSLoc Ho 		.owner = THIS_MODULE,
47781d01bfaSLoc Ho 		.of_match_table = xgene_ahci_of_match,
47881d01bfaSLoc Ho 	},
47981d01bfaSLoc Ho };
48081d01bfaSLoc Ho 
48181d01bfaSLoc Ho module_platform_driver(xgene_ahci_driver);
48281d01bfaSLoc Ho 
48381d01bfaSLoc Ho MODULE_DESCRIPTION("APM X-Gene AHCI SATA driver");
48481d01bfaSLoc Ho MODULE_AUTHOR("Loc Ho <lho@apm.com>");
48581d01bfaSLoc Ho MODULE_LICENSE("GPL");
48681d01bfaSLoc Ho MODULE_VERSION("0.4");
487