xref: /openbmc/linux/drivers/scsi/mvsas/mv_64xx.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dd4969a8SJeff Garzik /*
320b09c29SAndy Yan  * Marvell 88SE64xx hardware specific
420b09c29SAndy Yan  *
520b09c29SAndy Yan  * Copyright 2007 Red Hat, Inc.
620b09c29SAndy Yan  * Copyright 2008 Marvell. <kewei@marvell.com>
70b15fb1fSXiangliang Yu  * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
8dd4969a8SJeff Garzik */
9dd4969a8SJeff Garzik 
10dd4969a8SJeff Garzik #include "mv_sas.h"
11dd4969a8SJeff Garzik #include "mv_64xx.h"
12dd4969a8SJeff Garzik #include "mv_chips.h"
13dd4969a8SJeff Garzik 
mvs_64xx_detect_porttype(struct mvs_info * mvi,int i)1420b09c29SAndy Yan static void mvs_64xx_detect_porttype(struct mvs_info *mvi, int i)
15dd4969a8SJeff Garzik {
16dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
17dd4969a8SJeff Garzik 	u32 reg;
18dd4969a8SJeff Garzik 	struct mvs_phy *phy = &mvi->phy[i];
19dd4969a8SJeff Garzik 
2020b09c29SAndy Yan 	reg = mr32(MVS_GBL_PORT_TYPE);
2120b09c29SAndy Yan 	phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
22dd4969a8SJeff Garzik 	if (reg & MODE_SAS_SATA & (1 << i))
23dd4969a8SJeff Garzik 		phy->phy_type |= PORT_TYPE_SAS;
24dd4969a8SJeff Garzik 	else
25dd4969a8SJeff Garzik 		phy->phy_type |= PORT_TYPE_SATA;
26dd4969a8SJeff Garzik }
27dd4969a8SJeff Garzik 
mvs_64xx_enable_xmt(struct mvs_info * mvi,int phy_id)286f039790SGreg Kroah-Hartman static void mvs_64xx_enable_xmt(struct mvs_info *mvi, int phy_id)
29dd4969a8SJeff Garzik {
30dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
31dd4969a8SJeff Garzik 	u32 tmp;
32dd4969a8SJeff Garzik 
3320b09c29SAndy Yan 	tmp = mr32(MVS_PCS);
34a4632aaeSXiangliang Yu 	if (mvi->chip->n_phy <= MVS_SOC_PORTS)
3520b09c29SAndy Yan 		tmp |= 1 << (phy_id + PCS_EN_PORT_XMT_SHIFT);
36dd4969a8SJeff Garzik 	else
3720b09c29SAndy Yan 		tmp |= 1 << (phy_id + PCS_EN_PORT_XMT_SHIFT2);
3820b09c29SAndy Yan 	mw32(MVS_PCS, tmp);
39dd4969a8SJeff Garzik }
40dd4969a8SJeff Garzik 
mvs_64xx_phy_hacks(struct mvs_info * mvi)416f039790SGreg Kroah-Hartman static void mvs_64xx_phy_hacks(struct mvs_info *mvi)
42dd4969a8SJeff Garzik {
43dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
4484fbd0ceSXiangliang Yu 	int i;
45dd4969a8SJeff Garzik 
4620b09c29SAndy Yan 	mvs_phy_hacks(mvi);
47dd4969a8SJeff Garzik 
4820b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
4984fbd0ceSXiangliang Yu 		for (i = 0; i < MVS_SOC_PORTS; i++) {
5084fbd0ceSXiangliang Yu 			mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE8);
5184fbd0ceSXiangliang Yu 			mvs_write_port_vsr_data(mvi, i, 0x2F0);
5284fbd0ceSXiangliang Yu 		}
5320b09c29SAndy Yan 	} else {
5420b09c29SAndy Yan 		/* disable auto port detection */
5520b09c29SAndy Yan 		mw32(MVS_GBL_PORT_TYPE, 0);
5620b09c29SAndy Yan 		for (i = 0; i < mvi->chip->n_phy; i++) {
5720b09c29SAndy Yan 			mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE7);
5820b09c29SAndy Yan 			mvs_write_port_vsr_data(mvi, i, 0x90000000);
5920b09c29SAndy Yan 			mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE9);
6020b09c29SAndy Yan 			mvs_write_port_vsr_data(mvi, i, 0x50f2);
6120b09c29SAndy Yan 			mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE11);
6220b09c29SAndy Yan 			mvs_write_port_vsr_data(mvi, i, 0x0e);
6320b09c29SAndy Yan 		}
6420b09c29SAndy Yan 	}
65dd4969a8SJeff Garzik }
66dd4969a8SJeff Garzik 
mvs_64xx_stp_reset(struct mvs_info * mvi,u32 phy_id)6720b09c29SAndy Yan static void mvs_64xx_stp_reset(struct mvs_info *mvi, u32 phy_id)
6820b09c29SAndy Yan {
6920b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
7020b09c29SAndy Yan 	u32 reg, tmp;
7120b09c29SAndy Yan 
7220b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
73a4632aaeSXiangliang Yu 		if (phy_id < MVS_SOC_PORTS)
7420b09c29SAndy Yan 			pci_read_config_dword(mvi->pdev, PCR_PHY_CTL, &reg);
7520b09c29SAndy Yan 		else
7620b09c29SAndy Yan 			pci_read_config_dword(mvi->pdev, PCR_PHY_CTL2, &reg);
7720b09c29SAndy Yan 
7820b09c29SAndy Yan 	} else
7920b09c29SAndy Yan 		reg = mr32(MVS_PHY_CTL);
8020b09c29SAndy Yan 
8120b09c29SAndy Yan 	tmp = reg;
82a4632aaeSXiangliang Yu 	if (phy_id < MVS_SOC_PORTS)
8320b09c29SAndy Yan 		tmp |= (1U << phy_id) << PCTL_LINK_OFFS;
8420b09c29SAndy Yan 	else
85a4632aaeSXiangliang Yu 		tmp |= (1U << (phy_id - MVS_SOC_PORTS)) << PCTL_LINK_OFFS;
8620b09c29SAndy Yan 
8720b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
88a4632aaeSXiangliang Yu 		if (phy_id < MVS_SOC_PORTS) {
8920b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL, tmp);
9020b09c29SAndy Yan 			mdelay(10);
9120b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL, reg);
9220b09c29SAndy Yan 		} else {
9320b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL2, tmp);
9420b09c29SAndy Yan 			mdelay(10);
9520b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL2, reg);
9620b09c29SAndy Yan 		}
9720b09c29SAndy Yan 	} else {
9820b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
9920b09c29SAndy Yan 		mdelay(10);
10020b09c29SAndy Yan 		mw32(MVS_PHY_CTL, reg);
10120b09c29SAndy Yan 	}
10220b09c29SAndy Yan }
10320b09c29SAndy Yan 
mvs_64xx_phy_reset(struct mvs_info * mvi,u32 phy_id,int hard)10420b09c29SAndy Yan static void mvs_64xx_phy_reset(struct mvs_info *mvi, u32 phy_id, int hard)
10520b09c29SAndy Yan {
10620b09c29SAndy Yan 	u32 tmp;
10720b09c29SAndy Yan 	tmp = mvs_read_port_irq_stat(mvi, phy_id);
10820b09c29SAndy Yan 	tmp &= ~PHYEV_RDY_CH;
10920b09c29SAndy Yan 	mvs_write_port_irq_stat(mvi, phy_id, tmp);
11020b09c29SAndy Yan 	tmp = mvs_read_phy_ctl(mvi, phy_id);
111a4632aaeSXiangliang Yu 	if (hard == MVS_HARD_RESET)
11220b09c29SAndy Yan 		tmp |= PHY_RST_HARD;
113a4632aaeSXiangliang Yu 	else if (hard == MVS_SOFT_RESET)
11420b09c29SAndy Yan 		tmp |= PHY_RST;
11520b09c29SAndy Yan 	mvs_write_phy_ctl(mvi, phy_id, tmp);
11620b09c29SAndy Yan 	if (hard) {
11720b09c29SAndy Yan 		do {
11820b09c29SAndy Yan 			tmp = mvs_read_phy_ctl(mvi, phy_id);
11920b09c29SAndy Yan 		} while (tmp & PHY_RST_HARD);
12020b09c29SAndy Yan 	}
12120b09c29SAndy Yan }
12220b09c29SAndy Yan 
12314bf41dcSBaoyou Xie static void
mvs_64xx_clear_srs_irq(struct mvs_info * mvi,u8 reg_set,u8 clear_all)12414bf41dcSBaoyou Xie mvs_64xx_clear_srs_irq(struct mvs_info *mvi, u8 reg_set, u8 clear_all)
1259dc9fd94SSrinivas {
1269dc9fd94SSrinivas 	void __iomem *regs = mvi->regs;
1279dc9fd94SSrinivas 	u32 tmp;
1289dc9fd94SSrinivas 	if (clear_all) {
1299dc9fd94SSrinivas 		tmp = mr32(MVS_INT_STAT_SRS_0);
1309dc9fd94SSrinivas 		if (tmp) {
1319dc9fd94SSrinivas 			printk(KERN_DEBUG "check SRS 0 %08X.\n", tmp);
1329dc9fd94SSrinivas 			mw32(MVS_INT_STAT_SRS_0, tmp);
1339dc9fd94SSrinivas 		}
1349dc9fd94SSrinivas 	} else {
1359dc9fd94SSrinivas 		tmp = mr32(MVS_INT_STAT_SRS_0);
1369dc9fd94SSrinivas 		if (tmp &  (1 << (reg_set % 32))) {
1379dc9fd94SSrinivas 			printk(KERN_DEBUG "register set 0x%x was stopped.\n",
1389dc9fd94SSrinivas 			       reg_set);
1399dc9fd94SSrinivas 			mw32(MVS_INT_STAT_SRS_0, 1 << (reg_set % 32));
1409dc9fd94SSrinivas 		}
1419dc9fd94SSrinivas 	}
1429dc9fd94SSrinivas }
1439dc9fd94SSrinivas 
mvs_64xx_chip_reset(struct mvs_info * mvi)1446f039790SGreg Kroah-Hartman static int mvs_64xx_chip_reset(struct mvs_info *mvi)
14520b09c29SAndy Yan {
14620b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
14720b09c29SAndy Yan 	u32 tmp;
14820b09c29SAndy Yan 	int i;
14920b09c29SAndy Yan 
15020b09c29SAndy Yan 	/* make sure interrupts are masked immediately (paranoia) */
15120b09c29SAndy Yan 	mw32(MVS_GBL_CTL, 0);
15220b09c29SAndy Yan 	tmp = mr32(MVS_GBL_CTL);
15320b09c29SAndy Yan 
15420b09c29SAndy Yan 	/* Reset Controller */
15520b09c29SAndy Yan 	if (!(tmp & HBA_RST)) {
15620b09c29SAndy Yan 		if (mvi->flags & MVF_PHY_PWR_FIX) {
15720b09c29SAndy Yan 			pci_read_config_dword(mvi->pdev, PCR_PHY_CTL, &tmp);
15820b09c29SAndy Yan 			tmp &= ~PCTL_PWR_OFF;
15920b09c29SAndy Yan 			tmp |= PCTL_PHY_DSBL;
16020b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL, tmp);
16120b09c29SAndy Yan 
16220b09c29SAndy Yan 			pci_read_config_dword(mvi->pdev, PCR_PHY_CTL2, &tmp);
16320b09c29SAndy Yan 			tmp &= ~PCTL_PWR_OFF;
16420b09c29SAndy Yan 			tmp |= PCTL_PHY_DSBL;
16520b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL2, tmp);
16620b09c29SAndy Yan 		}
16720b09c29SAndy Yan 	}
16820b09c29SAndy Yan 
16920b09c29SAndy Yan 	/* make sure interrupts are masked immediately (paranoia) */
17020b09c29SAndy Yan 	mw32(MVS_GBL_CTL, 0);
17120b09c29SAndy Yan 	tmp = mr32(MVS_GBL_CTL);
17220b09c29SAndy Yan 
17320b09c29SAndy Yan 	/* Reset Controller */
17420b09c29SAndy Yan 	if (!(tmp & HBA_RST)) {
17520b09c29SAndy Yan 		/* global reset, incl. COMRESET/H_RESET_N (self-clearing) */
17620b09c29SAndy Yan 		mw32_f(MVS_GBL_CTL, HBA_RST);
17720b09c29SAndy Yan 	}
17820b09c29SAndy Yan 
17920b09c29SAndy Yan 	/* wait for reset to finish; timeout is just a guess */
18020b09c29SAndy Yan 	i = 1000;
18120b09c29SAndy Yan 	while (i-- > 0) {
18220b09c29SAndy Yan 		msleep(10);
18320b09c29SAndy Yan 
18420b09c29SAndy Yan 		if (!(mr32(MVS_GBL_CTL) & HBA_RST))
18520b09c29SAndy Yan 			break;
18620b09c29SAndy Yan 	}
18720b09c29SAndy Yan 	if (mr32(MVS_GBL_CTL) & HBA_RST) {
18820b09c29SAndy Yan 		dev_printk(KERN_ERR, mvi->dev, "HBA reset failed\n");
18920b09c29SAndy Yan 		return -EBUSY;
19020b09c29SAndy Yan 	}
19120b09c29SAndy Yan 	return 0;
19220b09c29SAndy Yan }
19320b09c29SAndy Yan 
mvs_64xx_phy_disable(struct mvs_info * mvi,u32 phy_id)19420b09c29SAndy Yan static void mvs_64xx_phy_disable(struct mvs_info *mvi, u32 phy_id)
19520b09c29SAndy Yan {
19620b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
19720b09c29SAndy Yan 	u32 tmp;
19820b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
19920b09c29SAndy Yan 		u32 offs;
20020b09c29SAndy Yan 		if (phy_id < 4)
20120b09c29SAndy Yan 			offs = PCR_PHY_CTL;
20220b09c29SAndy Yan 		else {
20320b09c29SAndy Yan 			offs = PCR_PHY_CTL2;
20420b09c29SAndy Yan 			phy_id -= 4;
20520b09c29SAndy Yan 		}
20620b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, offs, &tmp);
20720b09c29SAndy Yan 		tmp |= 1U << (PCTL_PHY_DSBL_OFFS + phy_id);
20820b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, offs, tmp);
20920b09c29SAndy Yan 	} else {
21020b09c29SAndy Yan 		tmp = mr32(MVS_PHY_CTL);
21120b09c29SAndy Yan 		tmp |= 1U << (PCTL_PHY_DSBL_OFFS + phy_id);
21220b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
21320b09c29SAndy Yan 	}
21420b09c29SAndy Yan }
21520b09c29SAndy Yan 
mvs_64xx_phy_enable(struct mvs_info * mvi,u32 phy_id)21620b09c29SAndy Yan static void mvs_64xx_phy_enable(struct mvs_info *mvi, u32 phy_id)
21720b09c29SAndy Yan {
21820b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
21920b09c29SAndy Yan 	u32 tmp;
22020b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
22120b09c29SAndy Yan 		u32 offs;
22220b09c29SAndy Yan 		if (phy_id < 4)
22320b09c29SAndy Yan 			offs = PCR_PHY_CTL;
22420b09c29SAndy Yan 		else {
22520b09c29SAndy Yan 			offs = PCR_PHY_CTL2;
22620b09c29SAndy Yan 			phy_id -= 4;
22720b09c29SAndy Yan 		}
22820b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, offs, &tmp);
22920b09c29SAndy Yan 		tmp &= ~(1U << (PCTL_PHY_DSBL_OFFS + phy_id));
23020b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, offs, tmp);
23120b09c29SAndy Yan 	} else {
23220b09c29SAndy Yan 		tmp = mr32(MVS_PHY_CTL);
23320b09c29SAndy Yan 		tmp &= ~(1U << (PCTL_PHY_DSBL_OFFS + phy_id));
23420b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
23520b09c29SAndy Yan 	}
23620b09c29SAndy Yan }
23720b09c29SAndy Yan 
mvs_64xx_init(struct mvs_info * mvi)2386f039790SGreg Kroah-Hartman static int mvs_64xx_init(struct mvs_info *mvi)
23920b09c29SAndy Yan {
24020b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
24120b09c29SAndy Yan 	int i;
24220b09c29SAndy Yan 	u32 tmp, cctl;
24320b09c29SAndy Yan 
24420b09c29SAndy Yan 	if (mvi->pdev && mvi->pdev->revision == 0)
24520b09c29SAndy Yan 		mvi->flags |= MVF_PHY_PWR_FIX;
24620b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
24720b09c29SAndy Yan 		mvs_show_pcie_usage(mvi);
24820b09c29SAndy Yan 		tmp = mvs_64xx_chip_reset(mvi);
24920b09c29SAndy Yan 		if (tmp)
25020b09c29SAndy Yan 			return tmp;
25120b09c29SAndy Yan 	} else {
25220b09c29SAndy Yan 		tmp = mr32(MVS_PHY_CTL);
25320b09c29SAndy Yan 		tmp &= ~PCTL_PWR_OFF;
25420b09c29SAndy Yan 		tmp |= PCTL_PHY_DSBL;
25520b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
25620b09c29SAndy Yan 	}
25720b09c29SAndy Yan 
25820b09c29SAndy Yan 	/* Init Chip */
25920b09c29SAndy Yan 	/* make sure RST is set; HBA_RST /should/ have done that for us */
26020b09c29SAndy Yan 	cctl = mr32(MVS_CTL) & 0xFFFF;
26120b09c29SAndy Yan 	if (cctl & CCTL_RST)
26220b09c29SAndy Yan 		cctl &= ~CCTL_RST;
26320b09c29SAndy Yan 	else
26420b09c29SAndy Yan 		mw32_f(MVS_CTL, cctl | CCTL_RST);
26520b09c29SAndy Yan 
26620b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
26720b09c29SAndy Yan 		/* write to device control _AND_ device status register */
26820b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, PCR_DEV_CTRL, &tmp);
26920b09c29SAndy Yan 		tmp &= ~PRD_REQ_MASK;
27020b09c29SAndy Yan 		tmp |= PRD_REQ_SIZE;
27120b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, PCR_DEV_CTRL, tmp);
27220b09c29SAndy Yan 
27320b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, PCR_PHY_CTL, &tmp);
27420b09c29SAndy Yan 		tmp &= ~PCTL_PWR_OFF;
27520b09c29SAndy Yan 		tmp &= ~PCTL_PHY_DSBL;
27620b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, PCR_PHY_CTL, tmp);
27720b09c29SAndy Yan 
27820b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, PCR_PHY_CTL2, &tmp);
27920b09c29SAndy Yan 		tmp &= PCTL_PWR_OFF;
28020b09c29SAndy Yan 		tmp &= ~PCTL_PHY_DSBL;
28120b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, PCR_PHY_CTL2, tmp);
28220b09c29SAndy Yan 	} else {
28320b09c29SAndy Yan 		tmp = mr32(MVS_PHY_CTL);
28420b09c29SAndy Yan 		tmp &= ~PCTL_PWR_OFF;
28520b09c29SAndy Yan 		tmp |= PCTL_COM_ON;
28620b09c29SAndy Yan 		tmp &= ~PCTL_PHY_DSBL;
28720b09c29SAndy Yan 		tmp |= PCTL_LINK_RST;
28820b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
28920b09c29SAndy Yan 		msleep(100);
29020b09c29SAndy Yan 		tmp &= ~PCTL_LINK_RST;
29120b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
29220b09c29SAndy Yan 		msleep(100);
29320b09c29SAndy Yan 	}
29420b09c29SAndy Yan 
29520b09c29SAndy Yan 	/* reset control */
29620b09c29SAndy Yan 	mw32(MVS_PCS, 0);		/* MVS_PCS */
29720b09c29SAndy Yan 	/* init phys */
29820b09c29SAndy Yan 	mvs_64xx_phy_hacks(mvi);
29920b09c29SAndy Yan 
30084fbd0ceSXiangliang Yu 	tmp = mvs_cr32(mvi, CMD_PHY_MODE_21);
30184fbd0ceSXiangliang Yu 	tmp &= 0x0000ffff;
30284fbd0ceSXiangliang Yu 	tmp |= 0x00fa0000;
30384fbd0ceSXiangliang Yu 	mvs_cw32(mvi, CMD_PHY_MODE_21, tmp);
30484fbd0ceSXiangliang Yu 
30520b09c29SAndy Yan 	/* enable auto port detection */
30620b09c29SAndy Yan 	mw32(MVS_GBL_PORT_TYPE, MODE_AUTO_DET_EN);
30720b09c29SAndy Yan 
30820b09c29SAndy Yan 	mw32(MVS_CMD_LIST_LO, mvi->slot_dma);
30920b09c29SAndy Yan 	mw32(MVS_CMD_LIST_HI, (mvi->slot_dma >> 16) >> 16);
31020b09c29SAndy Yan 
31120b09c29SAndy Yan 	mw32(MVS_RX_FIS_LO, mvi->rx_fis_dma);
31220b09c29SAndy Yan 	mw32(MVS_RX_FIS_HI, (mvi->rx_fis_dma >> 16) >> 16);
31320b09c29SAndy Yan 
31420b09c29SAndy Yan 	mw32(MVS_TX_CFG, MVS_CHIP_SLOT_SZ);
31520b09c29SAndy Yan 	mw32(MVS_TX_LO, mvi->tx_dma);
31620b09c29SAndy Yan 	mw32(MVS_TX_HI, (mvi->tx_dma >> 16) >> 16);
31720b09c29SAndy Yan 
31820b09c29SAndy Yan 	mw32(MVS_RX_CFG, MVS_RX_RING_SZ);
31920b09c29SAndy Yan 	mw32(MVS_RX_LO, mvi->rx_dma);
32020b09c29SAndy Yan 	mw32(MVS_RX_HI, (mvi->rx_dma >> 16) >> 16);
32120b09c29SAndy Yan 
32220b09c29SAndy Yan 	for (i = 0; i < mvi->chip->n_phy; i++) {
32320b09c29SAndy Yan 		/* set phy local SAS address */
32420b09c29SAndy Yan 		/* should set little endian SAS address to 64xx chip */
32520b09c29SAndy Yan 		mvs_set_sas_addr(mvi, i, PHYR_ADDR_LO, PHYR_ADDR_HI,
32620b09c29SAndy Yan 				cpu_to_be64(mvi->phy[i].dev_sas_addr));
32720b09c29SAndy Yan 
32820b09c29SAndy Yan 		mvs_64xx_enable_xmt(mvi, i);
32920b09c29SAndy Yan 
330a4632aaeSXiangliang Yu 		mvs_64xx_phy_reset(mvi, i, MVS_HARD_RESET);
33120b09c29SAndy Yan 		msleep(500);
33220b09c29SAndy Yan 		mvs_64xx_detect_porttype(mvi, i);
33320b09c29SAndy Yan 	}
33420b09c29SAndy Yan 	if (mvi->flags & MVF_FLAG_SOC) {
33520b09c29SAndy Yan 		/* set select registers */
33620b09c29SAndy Yan 		writel(0x0E008000, regs + 0x000);
33720b09c29SAndy Yan 		writel(0x59000008, regs + 0x004);
33820b09c29SAndy Yan 		writel(0x20, regs + 0x008);
33920b09c29SAndy Yan 		writel(0x20, regs + 0x00c);
34020b09c29SAndy Yan 		writel(0x20, regs + 0x010);
34120b09c29SAndy Yan 		writel(0x20, regs + 0x014);
34220b09c29SAndy Yan 		writel(0x20, regs + 0x018);
34320b09c29SAndy Yan 		writel(0x20, regs + 0x01c);
34420b09c29SAndy Yan 	}
34520b09c29SAndy Yan 	for (i = 0; i < mvi->chip->n_phy; i++) {
34620b09c29SAndy Yan 		/* clear phy int status */
34720b09c29SAndy Yan 		tmp = mvs_read_port_irq_stat(mvi, i);
34820b09c29SAndy Yan 		tmp &= ~PHYEV_SIG_FIS;
34920b09c29SAndy Yan 		mvs_write_port_irq_stat(mvi, i, tmp);
35020b09c29SAndy Yan 
35120b09c29SAndy Yan 		/* set phy int mask */
35220b09c29SAndy Yan 		tmp = PHYEV_RDY_CH | PHYEV_BROAD_CH | PHYEV_UNASSOC_FIS |
35320b09c29SAndy Yan 			PHYEV_ID_DONE | PHYEV_DCDR_ERR | PHYEV_CRC_ERR |
35420b09c29SAndy Yan 			PHYEV_DEC_ERR;
35520b09c29SAndy Yan 		mvs_write_port_irq_mask(mvi, i, tmp);
35620b09c29SAndy Yan 
35720b09c29SAndy Yan 		msleep(100);
35820b09c29SAndy Yan 		mvs_update_phyinfo(mvi, i, 1);
35920b09c29SAndy Yan 	}
36020b09c29SAndy Yan 
36120b09c29SAndy Yan 	/* little endian for open address and command table, etc. */
36220b09c29SAndy Yan 	cctl = mr32(MVS_CTL);
36320b09c29SAndy Yan 	cctl |= CCTL_ENDIAN_CMD;
36420b09c29SAndy Yan 	cctl |= CCTL_ENDIAN_DATA;
36520b09c29SAndy Yan 	cctl &= ~CCTL_ENDIAN_OPEN;
36620b09c29SAndy Yan 	cctl |= CCTL_ENDIAN_RSP;
36720b09c29SAndy Yan 	mw32_f(MVS_CTL, cctl);
36820b09c29SAndy Yan 
36920b09c29SAndy Yan 	/* reset CMD queue */
37020b09c29SAndy Yan 	tmp = mr32(MVS_PCS);
37120b09c29SAndy Yan 	tmp |= PCS_CMD_RST;
37284fbd0ceSXiangliang Yu 	tmp &= ~PCS_SELF_CLEAR;
37320b09c29SAndy Yan 	mw32(MVS_PCS, tmp);
374e144f7efSXiangliang Yu 	/*
375e144f7efSXiangliang Yu 	 * the max count is 0x1ff, while our max slot is 0x200,
37620b09c29SAndy Yan 	 * it will make count 0.
37720b09c29SAndy Yan 	 */
37820b09c29SAndy Yan 	tmp = 0;
37984fbd0ceSXiangliang Yu 	if (MVS_CHIP_SLOT_SZ > 0x1ff)
38084fbd0ceSXiangliang Yu 		mw32(MVS_INT_COAL, 0x1ff | COAL_EN);
38184fbd0ceSXiangliang Yu 	else
38284fbd0ceSXiangliang Yu 		mw32(MVS_INT_COAL, MVS_CHIP_SLOT_SZ | COAL_EN);
38320b09c29SAndy Yan 
38483c7b61cSXiangliang Yu 	tmp = 0x10000 | interrupt_coalescing;
38520b09c29SAndy Yan 	mw32(MVS_INT_COAL_TMOUT, tmp);
38620b09c29SAndy Yan 
38720b09c29SAndy Yan 	/* ladies and gentlemen, start your engines */
38820b09c29SAndy Yan 	mw32(MVS_TX_CFG, 0);
38920b09c29SAndy Yan 	mw32(MVS_TX_CFG, MVS_CHIP_SLOT_SZ | TX_EN);
39020b09c29SAndy Yan 	mw32(MVS_RX_CFG, MVS_RX_RING_SZ | RX_EN);
39120b09c29SAndy Yan 	/* enable CMD/CMPL_Q/RESP mode */
39220b09c29SAndy Yan 	mw32(MVS_PCS, PCS_SATA_RETRY | PCS_FIS_RX_EN |
39320b09c29SAndy Yan 		PCS_CMD_EN | PCS_CMD_STOP_ERR);
39420b09c29SAndy Yan 
39520b09c29SAndy Yan 	/* enable completion queue interrupt */
39620b09c29SAndy Yan 	tmp = (CINT_PORT_MASK | CINT_DONE | CINT_MEM | CINT_SRS | CINT_CI_STOP |
39720b09c29SAndy Yan 		CINT_DMA_PCIE);
39820b09c29SAndy Yan 
39920b09c29SAndy Yan 	mw32(MVS_INT_MASK, tmp);
40020b09c29SAndy Yan 
40120b09c29SAndy Yan 	/* Enable SRS interrupt */
40220b09c29SAndy Yan 	mw32(MVS_INT_MASK_SRS_0, 0xFFFF);
40320b09c29SAndy Yan 
40420b09c29SAndy Yan 	return 0;
40520b09c29SAndy Yan }
40620b09c29SAndy Yan 
mvs_64xx_ioremap(struct mvs_info * mvi)40720b09c29SAndy Yan static int mvs_64xx_ioremap(struct mvs_info *mvi)
40820b09c29SAndy Yan {
40920b09c29SAndy Yan 	if (!mvs_ioremap(mvi, 4, 2))
41020b09c29SAndy Yan 		return 0;
41120b09c29SAndy Yan 	return -1;
41220b09c29SAndy Yan }
41320b09c29SAndy Yan 
mvs_64xx_iounmap(struct mvs_info * mvi)41420b09c29SAndy Yan static void mvs_64xx_iounmap(struct mvs_info *mvi)
41520b09c29SAndy Yan {
41620b09c29SAndy Yan 	mvs_iounmap(mvi->regs);
41720b09c29SAndy Yan 	mvs_iounmap(mvi->regs_ex);
41820b09c29SAndy Yan }
41920b09c29SAndy Yan 
mvs_64xx_interrupt_enable(struct mvs_info * mvi)42020b09c29SAndy Yan static void mvs_64xx_interrupt_enable(struct mvs_info *mvi)
421dd4969a8SJeff Garzik {
422dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
423dd4969a8SJeff Garzik 	u32 tmp;
424dd4969a8SJeff Garzik 
42520b09c29SAndy Yan 	tmp = mr32(MVS_GBL_CTL);
42620b09c29SAndy Yan 	mw32(MVS_GBL_CTL, tmp | INT_EN);
427dd4969a8SJeff Garzik }
428dd4969a8SJeff Garzik 
mvs_64xx_interrupt_disable(struct mvs_info * mvi)42920b09c29SAndy Yan static void mvs_64xx_interrupt_disable(struct mvs_info *mvi)
430dd4969a8SJeff Garzik {
431dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
432dd4969a8SJeff Garzik 	u32 tmp;
433dd4969a8SJeff Garzik 
43420b09c29SAndy Yan 	tmp = mr32(MVS_GBL_CTL);
43520b09c29SAndy Yan 	mw32(MVS_GBL_CTL, tmp & ~INT_EN);
436dd4969a8SJeff Garzik }
437dd4969a8SJeff Garzik 
mvs_64xx_isr_status(struct mvs_info * mvi,int irq)43820b09c29SAndy Yan static u32 mvs_64xx_isr_status(struct mvs_info *mvi, int irq)
43920b09c29SAndy Yan {
44020b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
44120b09c29SAndy Yan 	u32 stat;
44220b09c29SAndy Yan 
44320b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
44420b09c29SAndy Yan 		stat = mr32(MVS_GBL_INT_STAT);
44520b09c29SAndy Yan 
44620b09c29SAndy Yan 		if (stat == 0 || stat == 0xffffffff)
44720b09c29SAndy Yan 			return 0;
44820b09c29SAndy Yan 	} else
44920b09c29SAndy Yan 		stat = 1;
45020b09c29SAndy Yan 	return stat;
45120b09c29SAndy Yan }
45220b09c29SAndy Yan 
mvs_64xx_isr(struct mvs_info * mvi,int irq,u32 stat)45320b09c29SAndy Yan static irqreturn_t mvs_64xx_isr(struct mvs_info *mvi, int irq, u32 stat)
45420b09c29SAndy Yan {
45520b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
45620b09c29SAndy Yan 
45720b09c29SAndy Yan 	/* clear CMD_CMPLT ASAP */
45820b09c29SAndy Yan 	mw32_f(MVS_INT_STAT, CINT_DONE);
4596f8ac161SXiangliang Yu 
46020b09c29SAndy Yan 	spin_lock(&mvi->lock);
46120b09c29SAndy Yan 	mvs_int_full(mvi);
46220b09c29SAndy Yan 	spin_unlock(&mvi->lock);
4636f8ac161SXiangliang Yu 
46420b09c29SAndy Yan 	return IRQ_HANDLED;
46520b09c29SAndy Yan }
46620b09c29SAndy Yan 
mvs_64xx_command_active(struct mvs_info * mvi,u32 slot_idx)46720b09c29SAndy Yan static void mvs_64xx_command_active(struct mvs_info *mvi, u32 slot_idx)
46820b09c29SAndy Yan {
46920b09c29SAndy Yan 	u32 tmp;
47020b09c29SAndy Yan 	mvs_cw32(mvi, 0x40 + (slot_idx >> 3), 1 << (slot_idx % 32));
47120b09c29SAndy Yan 	mvs_cw32(mvi, 0x00 + (slot_idx >> 3), 1 << (slot_idx % 32));
47220b09c29SAndy Yan 	do {
47320b09c29SAndy Yan 		tmp = mvs_cr32(mvi, 0x00 + (slot_idx >> 3));
47420b09c29SAndy Yan 	} while (tmp & 1 << (slot_idx % 32));
47520b09c29SAndy Yan 	do {
47620b09c29SAndy Yan 		tmp = mvs_cr32(mvi, 0x40 + (slot_idx >> 3));
47720b09c29SAndy Yan 	} while (tmp & 1 << (slot_idx % 32));
47820b09c29SAndy Yan }
47920b09c29SAndy Yan 
mvs_64xx_issue_stop(struct mvs_info * mvi,enum mvs_port_type type,u32 tfs)48020b09c29SAndy Yan static void mvs_64xx_issue_stop(struct mvs_info *mvi, enum mvs_port_type type,
48120b09c29SAndy Yan 				u32 tfs)
48220b09c29SAndy Yan {
48320b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
48420b09c29SAndy Yan 	u32 tmp;
48520b09c29SAndy Yan 
48620b09c29SAndy Yan 	if (type == PORT_TYPE_SATA) {
48720b09c29SAndy Yan 		tmp = mr32(MVS_INT_STAT_SRS_0) | (1U << tfs);
48820b09c29SAndy Yan 		mw32(MVS_INT_STAT_SRS_0, tmp);
48920b09c29SAndy Yan 	}
49020b09c29SAndy Yan 	mw32(MVS_INT_STAT, CINT_CI_STOP);
49120b09c29SAndy Yan 	tmp = mr32(MVS_PCS) | 0xFF00;
49220b09c29SAndy Yan 	mw32(MVS_PCS, tmp);
49320b09c29SAndy Yan }
49420b09c29SAndy Yan 
mvs_64xx_free_reg_set(struct mvs_info * mvi,u8 * tfs)49520b09c29SAndy Yan static void mvs_64xx_free_reg_set(struct mvs_info *mvi, u8 *tfs)
496dd4969a8SJeff Garzik {
497dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
498dd4969a8SJeff Garzik 	u32 tmp, offs;
499dd4969a8SJeff Garzik 
500dd4969a8SJeff Garzik 	if (*tfs == MVS_ID_NOT_MAPPED)
501dd4969a8SJeff Garzik 		return;
502dd4969a8SJeff Garzik 
503dd4969a8SJeff Garzik 	offs = 1U << ((*tfs & 0x0f) + PCS_EN_SATA_REG_SHIFT);
504dd4969a8SJeff Garzik 	if (*tfs < 16) {
50520b09c29SAndy Yan 		tmp = mr32(MVS_PCS);
50620b09c29SAndy Yan 		mw32(MVS_PCS, tmp & ~offs);
507dd4969a8SJeff Garzik 	} else {
50820b09c29SAndy Yan 		tmp = mr32(MVS_CTL);
50920b09c29SAndy Yan 		mw32(MVS_CTL, tmp & ~offs);
510dd4969a8SJeff Garzik 	}
511dd4969a8SJeff Garzik 
51220b09c29SAndy Yan 	tmp = mr32(MVS_INT_STAT_SRS_0) & (1U << *tfs);
513dd4969a8SJeff Garzik 	if (tmp)
51420b09c29SAndy Yan 		mw32(MVS_INT_STAT_SRS_0, tmp);
515dd4969a8SJeff Garzik 
516dd4969a8SJeff Garzik 	*tfs = MVS_ID_NOT_MAPPED;
51720b09c29SAndy Yan 	return;
518dd4969a8SJeff Garzik }
519dd4969a8SJeff Garzik 
mvs_64xx_assign_reg_set(struct mvs_info * mvi,u8 * tfs)52020b09c29SAndy Yan static u8 mvs_64xx_assign_reg_set(struct mvs_info *mvi, u8 *tfs)
521dd4969a8SJeff Garzik {
522dd4969a8SJeff Garzik 	int i;
523dd4969a8SJeff Garzik 	u32 tmp, offs;
524dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
525dd4969a8SJeff Garzik 
52620b09c29SAndy Yan 	if (*tfs != MVS_ID_NOT_MAPPED)
527dd4969a8SJeff Garzik 		return 0;
528dd4969a8SJeff Garzik 
52920b09c29SAndy Yan 	tmp = mr32(MVS_PCS);
530dd4969a8SJeff Garzik 
531dd4969a8SJeff Garzik 	for (i = 0; i < mvi->chip->srs_sz; i++) {
532dd4969a8SJeff Garzik 		if (i == 16)
53320b09c29SAndy Yan 			tmp = mr32(MVS_CTL);
534dd4969a8SJeff Garzik 		offs = 1U << ((i & 0x0f) + PCS_EN_SATA_REG_SHIFT);
535dd4969a8SJeff Garzik 		if (!(tmp & offs)) {
53620b09c29SAndy Yan 			*tfs = i;
537dd4969a8SJeff Garzik 
538dd4969a8SJeff Garzik 			if (i < 16)
53920b09c29SAndy Yan 				mw32(MVS_PCS, tmp | offs);
540dd4969a8SJeff Garzik 			else
54120b09c29SAndy Yan 				mw32(MVS_CTL, tmp | offs);
54220b09c29SAndy Yan 			tmp = mr32(MVS_INT_STAT_SRS_0) & (1U << i);
543dd4969a8SJeff Garzik 			if (tmp)
54420b09c29SAndy Yan 				mw32(MVS_INT_STAT_SRS_0, tmp);
545dd4969a8SJeff Garzik 			return 0;
546dd4969a8SJeff Garzik 		}
547dd4969a8SJeff Garzik 	}
548dd4969a8SJeff Garzik 	return MVS_ID_NOT_MAPPED;
549dd4969a8SJeff Garzik }
550dd4969a8SJeff Garzik 
mvs_64xx_make_prd(struct scatterlist * scatter,int nr,void * prd)55114bf41dcSBaoyou Xie static void mvs_64xx_make_prd(struct scatterlist *scatter, int nr, void *prd)
55220b09c29SAndy Yan {
55320b09c29SAndy Yan 	int i;
55420b09c29SAndy Yan 	struct scatterlist *sg;
55520b09c29SAndy Yan 	struct mvs_prd *buf_prd = prd;
55620b09c29SAndy Yan 	for_each_sg(scatter, sg, nr, i) {
55720b09c29SAndy Yan 		buf_prd->addr = cpu_to_le64(sg_dma_address(sg));
55820b09c29SAndy Yan 		buf_prd->len = cpu_to_le32(sg_dma_len(sg));
55920b09c29SAndy Yan 		buf_prd++;
56020b09c29SAndy Yan 	}
56120b09c29SAndy Yan }
56220b09c29SAndy Yan 
mvs_64xx_oob_done(struct mvs_info * mvi,int i)56320b09c29SAndy Yan static int mvs_64xx_oob_done(struct mvs_info *mvi, int i)
56420b09c29SAndy Yan {
56520b09c29SAndy Yan 	u32 phy_st;
56620b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i,
56720b09c29SAndy Yan 			PHYR_PHY_STAT);
56820b09c29SAndy Yan 	phy_st = mvs_read_port_cfg_data(mvi, i);
56920b09c29SAndy Yan 	if (phy_st & PHY_OOB_DTCTD)
57020b09c29SAndy Yan 		return 1;
57120b09c29SAndy Yan 	return 0;
57220b09c29SAndy Yan }
57320b09c29SAndy Yan 
mvs_64xx_fix_phy_info(struct mvs_info * mvi,int i,struct sas_identify_frame * id)57420b09c29SAndy Yan static void mvs_64xx_fix_phy_info(struct mvs_info *mvi, int i,
57520b09c29SAndy Yan 				struct sas_identify_frame *id)
57620b09c29SAndy Yan 
57720b09c29SAndy Yan {
57820b09c29SAndy Yan 	struct mvs_phy *phy = &mvi->phy[i];
57920b09c29SAndy Yan 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
58020b09c29SAndy Yan 
58120b09c29SAndy Yan 	sas_phy->linkrate =
58220b09c29SAndy Yan 		(phy->phy_status & PHY_NEG_SPP_PHYS_LINK_RATE_MASK) >>
58320b09c29SAndy Yan 			PHY_NEG_SPP_PHYS_LINK_RATE_MASK_OFFSET;
58420b09c29SAndy Yan 
58520b09c29SAndy Yan 	phy->minimum_linkrate =
58620b09c29SAndy Yan 		(phy->phy_status &
58720b09c29SAndy Yan 			PHY_MIN_SPP_PHYS_LINK_RATE_MASK) >> 8;
58820b09c29SAndy Yan 	phy->maximum_linkrate =
58920b09c29SAndy Yan 		(phy->phy_status &
59020b09c29SAndy Yan 			PHY_MAX_SPP_PHYS_LINK_RATE_MASK) >> 12;
59120b09c29SAndy Yan 
59220b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i, PHYR_IDENTIFY);
59320b09c29SAndy Yan 	phy->dev_info = mvs_read_port_cfg_data(mvi, i);
59420b09c29SAndy Yan 
59520b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i, PHYR_ATT_DEV_INFO);
59620b09c29SAndy Yan 	phy->att_dev_info = mvs_read_port_cfg_data(mvi, i);
59720b09c29SAndy Yan 
59820b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i, PHYR_ATT_ADDR_HI);
59920b09c29SAndy Yan 	phy->att_dev_sas_addr =
60020b09c29SAndy Yan 	     (u64) mvs_read_port_cfg_data(mvi, i) << 32;
60120b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i, PHYR_ATT_ADDR_LO);
60220b09c29SAndy Yan 	phy->att_dev_sas_addr |= mvs_read_port_cfg_data(mvi, i);
60320b09c29SAndy Yan 	phy->att_dev_sas_addr = SAS_ADDR(&phy->att_dev_sas_addr);
60420b09c29SAndy Yan }
60520b09c29SAndy Yan 
mvs_64xx_phy_work_around(struct mvs_info * mvi,int i)60620b09c29SAndy Yan static void mvs_64xx_phy_work_around(struct mvs_info *mvi, int i)
60720b09c29SAndy Yan {
60820b09c29SAndy Yan 	u32 tmp;
60920b09c29SAndy Yan 	struct mvs_phy *phy = &mvi->phy[i];
61020b09c29SAndy Yan 	mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE6);
61120b09c29SAndy Yan 	tmp = mvs_read_port_vsr_data(mvi, i);
61220b09c29SAndy Yan 	if (((phy->phy_status & PHY_NEG_SPP_PHYS_LINK_RATE_MASK) >>
61320b09c29SAndy Yan 	     PHY_NEG_SPP_PHYS_LINK_RATE_MASK_OFFSET) ==
61420b09c29SAndy Yan 		SAS_LINK_RATE_1_5_GBPS)
61520b09c29SAndy Yan 		tmp &= ~PHY_MODE6_LATECLK;
61620b09c29SAndy Yan 	else
61720b09c29SAndy Yan 		tmp |= PHY_MODE6_LATECLK;
61820b09c29SAndy Yan 	mvs_write_port_vsr_data(mvi, i, tmp);
61920b09c29SAndy Yan }
62020b09c29SAndy Yan 
mvs_64xx_phy_set_link_rate(struct mvs_info * mvi,u32 phy_id,struct sas_phy_linkrates * rates)62114bf41dcSBaoyou Xie static void mvs_64xx_phy_set_link_rate(struct mvs_info *mvi, u32 phy_id,
62220b09c29SAndy Yan 			struct sas_phy_linkrates *rates)
62320b09c29SAndy Yan {
62420b09c29SAndy Yan 	u32 lrmin = 0, lrmax = 0;
62520b09c29SAndy Yan 	u32 tmp;
62620b09c29SAndy Yan 
62720b09c29SAndy Yan 	tmp = mvs_read_phy_ctl(mvi, phy_id);
62820b09c29SAndy Yan 	lrmin = (rates->minimum_linkrate << 8);
62920b09c29SAndy Yan 	lrmax = (rates->maximum_linkrate << 12);
63020b09c29SAndy Yan 
63120b09c29SAndy Yan 	if (lrmin) {
63220b09c29SAndy Yan 		tmp &= ~(0xf << 8);
63320b09c29SAndy Yan 		tmp |= lrmin;
63420b09c29SAndy Yan 	}
63520b09c29SAndy Yan 	if (lrmax) {
63620b09c29SAndy Yan 		tmp &= ~(0xf << 12);
63720b09c29SAndy Yan 		tmp |= lrmax;
63820b09c29SAndy Yan 	}
63920b09c29SAndy Yan 	mvs_write_phy_ctl(mvi, phy_id, tmp);
640a4632aaeSXiangliang Yu 	mvs_64xx_phy_reset(mvi, phy_id, MVS_HARD_RESET);
64120b09c29SAndy Yan }
64220b09c29SAndy Yan 
mvs_64xx_clear_active_cmds(struct mvs_info * mvi)64320b09c29SAndy Yan static void mvs_64xx_clear_active_cmds(struct mvs_info *mvi)
64420b09c29SAndy Yan {
64520b09c29SAndy Yan 	u32 tmp;
64620b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
64720b09c29SAndy Yan 	tmp = mr32(MVS_PCS);
64820b09c29SAndy Yan 	mw32(MVS_PCS, tmp & 0xFFFF);
64920b09c29SAndy Yan 	mw32(MVS_PCS, tmp);
65020b09c29SAndy Yan 	tmp = mr32(MVS_CTL);
65120b09c29SAndy Yan 	mw32(MVS_CTL, tmp & 0xFFFF);
65220b09c29SAndy Yan 	mw32(MVS_CTL, tmp);
65320b09c29SAndy Yan }
65420b09c29SAndy Yan 
65520b09c29SAndy Yan 
mvs_64xx_spi_read_data(struct mvs_info * mvi)65614bf41dcSBaoyou Xie static u32 mvs_64xx_spi_read_data(struct mvs_info *mvi)
65720b09c29SAndy Yan {
65820b09c29SAndy Yan 	void __iomem *regs = mvi->regs_ex;
65920b09c29SAndy Yan 	return ior32(SPI_DATA_REG_64XX);
66020b09c29SAndy Yan }
66120b09c29SAndy Yan 
mvs_64xx_spi_write_data(struct mvs_info * mvi,u32 data)66214bf41dcSBaoyou Xie static void mvs_64xx_spi_write_data(struct mvs_info *mvi, u32 data)
66320b09c29SAndy Yan {
66420b09c29SAndy Yan 	void __iomem *regs = mvi->regs_ex;
665f2c43a62SColin Ian King 
66620b09c29SAndy Yan 	iow32(SPI_DATA_REG_64XX, data);
66720b09c29SAndy Yan }
66820b09c29SAndy Yan 
66920b09c29SAndy Yan 
mvs_64xx_spi_buildcmd(struct mvs_info * mvi,u32 * dwCmd,u8 cmd,u8 read,u8 length,u32 addr)67014bf41dcSBaoyou Xie static int mvs_64xx_spi_buildcmd(struct mvs_info *mvi,
67120b09c29SAndy Yan 			u32      *dwCmd,
67220b09c29SAndy Yan 			u8       cmd,
67320b09c29SAndy Yan 			u8       read,
67420b09c29SAndy Yan 			u8       length,
67520b09c29SAndy Yan 			u32      addr
67620b09c29SAndy Yan 			)
67720b09c29SAndy Yan {
67820b09c29SAndy Yan 	u32  dwTmp;
67920b09c29SAndy Yan 
68020b09c29SAndy Yan 	dwTmp = ((u32)cmd << 24) | ((u32)length << 19);
68120b09c29SAndy Yan 	if (read)
68220b09c29SAndy Yan 		dwTmp |= 1U<<23;
68320b09c29SAndy Yan 
68420b09c29SAndy Yan 	if (addr != MV_MAX_U32) {
68520b09c29SAndy Yan 		dwTmp |= 1U<<22;
68620b09c29SAndy Yan 		dwTmp |= (addr & 0x0003FFFF);
68720b09c29SAndy Yan 	}
68820b09c29SAndy Yan 
68920b09c29SAndy Yan 	*dwCmd = dwTmp;
69020b09c29SAndy Yan 	return 0;
69120b09c29SAndy Yan }
69220b09c29SAndy Yan 
69320b09c29SAndy Yan 
mvs_64xx_spi_issuecmd(struct mvs_info * mvi,u32 cmd)69414bf41dcSBaoyou Xie static int mvs_64xx_spi_issuecmd(struct mvs_info *mvi, u32 cmd)
69520b09c29SAndy Yan {
69620b09c29SAndy Yan 	void __iomem *regs = mvi->regs_ex;
69720b09c29SAndy Yan 	int     retry;
69820b09c29SAndy Yan 
69920b09c29SAndy Yan 	for (retry = 0; retry < 1; retry++) {
70020b09c29SAndy Yan 		iow32(SPI_CTRL_REG_64XX, SPI_CTRL_VENDOR_ENABLE);
70120b09c29SAndy Yan 		iow32(SPI_CMD_REG_64XX, cmd);
70220b09c29SAndy Yan 		iow32(SPI_CTRL_REG_64XX,
70320b09c29SAndy Yan 			SPI_CTRL_VENDOR_ENABLE | SPI_CTRL_SPISTART);
70420b09c29SAndy Yan 	}
70520b09c29SAndy Yan 
70620b09c29SAndy Yan 	return 0;
70720b09c29SAndy Yan }
70820b09c29SAndy Yan 
mvs_64xx_spi_waitdataready(struct mvs_info * mvi,u32 timeout)70914bf41dcSBaoyou Xie static int mvs_64xx_spi_waitdataready(struct mvs_info *mvi, u32 timeout)
71020b09c29SAndy Yan {
71120b09c29SAndy Yan 	void __iomem *regs = mvi->regs_ex;
71220b09c29SAndy Yan 	u32 i, dwTmp;
71320b09c29SAndy Yan 
71420b09c29SAndy Yan 	for (i = 0; i < timeout; i++) {
71520b09c29SAndy Yan 		dwTmp = ior32(SPI_CTRL_REG_64XX);
71620b09c29SAndy Yan 		if (!(dwTmp & SPI_CTRL_SPISTART))
71720b09c29SAndy Yan 			return 0;
71820b09c29SAndy Yan 		msleep(10);
71920b09c29SAndy Yan 	}
72020b09c29SAndy Yan 
72120b09c29SAndy Yan 	return -1;
72220b09c29SAndy Yan }
72320b09c29SAndy Yan 
mvs_64xx_fix_dma(struct mvs_info * mvi,u32 phy_mask,int buf_len,int from,void * prd)72414bf41dcSBaoyou Xie static void mvs_64xx_fix_dma(struct mvs_info *mvi, u32 phy_mask,
7258882f081SXiangliang Yu 				int buf_len, int from, void *prd)
72620b09c29SAndy Yan {
72720b09c29SAndy Yan 	int i;
72820b09c29SAndy Yan 	struct mvs_prd *buf_prd = prd;
7298882f081SXiangliang Yu 	dma_addr_t buf_dma = mvi->bulk_buffer_dma;
7308882f081SXiangliang Yu 
73120b09c29SAndy Yan 	buf_prd	+= from;
73220b09c29SAndy Yan 	for (i = 0; i < MAX_SG_ENTRY - from; i++) {
73320b09c29SAndy Yan 		buf_prd->addr = cpu_to_le64(buf_dma);
73420b09c29SAndy Yan 		buf_prd->len = cpu_to_le32(buf_len);
73520b09c29SAndy Yan 		++buf_prd;
73620b09c29SAndy Yan 	}
73720b09c29SAndy Yan }
73820b09c29SAndy Yan 
mvs_64xx_tune_interrupt(struct mvs_info * mvi,u32 time)73983c7b61cSXiangliang Yu static void mvs_64xx_tune_interrupt(struct mvs_info *mvi, u32 time)
74083c7b61cSXiangliang Yu {
74183c7b61cSXiangliang Yu 	void __iomem *regs = mvi->regs;
74283c7b61cSXiangliang Yu 	u32 tmp = 0;
743e144f7efSXiangliang Yu 	/*
744e144f7efSXiangliang Yu 	 * the max count is 0x1ff, while our max slot is 0x200,
74583c7b61cSXiangliang Yu 	 * it will make count 0.
74683c7b61cSXiangliang Yu 	 */
74783c7b61cSXiangliang Yu 	if (time == 0) {
74883c7b61cSXiangliang Yu 		mw32(MVS_INT_COAL, 0);
74983c7b61cSXiangliang Yu 		mw32(MVS_INT_COAL_TMOUT, 0x10000);
75083c7b61cSXiangliang Yu 	} else {
75183c7b61cSXiangliang Yu 		if (MVS_CHIP_SLOT_SZ > 0x1ff)
75283c7b61cSXiangliang Yu 			mw32(MVS_INT_COAL, 0x1ff|COAL_EN);
75383c7b61cSXiangliang Yu 		else
75483c7b61cSXiangliang Yu 			mw32(MVS_INT_COAL, MVS_CHIP_SLOT_SZ|COAL_EN);
75583c7b61cSXiangliang Yu 
75683c7b61cSXiangliang Yu 		tmp = 0x10000 | time;
75783c7b61cSXiangliang Yu 		mw32(MVS_INT_COAL_TMOUT, tmp);
75883c7b61cSXiangliang Yu 	}
75983c7b61cSXiangliang Yu }
76083c7b61cSXiangliang Yu 
76120b09c29SAndy Yan const struct mvs_dispatch mvs_64xx_dispatch = {
76220b09c29SAndy Yan 	"mv64xx",
76320b09c29SAndy Yan 	mvs_64xx_init,
76420b09c29SAndy Yan 	NULL,
76520b09c29SAndy Yan 	mvs_64xx_ioremap,
76620b09c29SAndy Yan 	mvs_64xx_iounmap,
76720b09c29SAndy Yan 	mvs_64xx_isr,
76820b09c29SAndy Yan 	mvs_64xx_isr_status,
76920b09c29SAndy Yan 	mvs_64xx_interrupt_enable,
77020b09c29SAndy Yan 	mvs_64xx_interrupt_disable,
77120b09c29SAndy Yan 	mvs_read_phy_ctl,
77220b09c29SAndy Yan 	mvs_write_phy_ctl,
77320b09c29SAndy Yan 	mvs_read_port_cfg_data,
77420b09c29SAndy Yan 	mvs_write_port_cfg_data,
77520b09c29SAndy Yan 	mvs_write_port_cfg_addr,
77620b09c29SAndy Yan 	mvs_read_port_vsr_data,
77720b09c29SAndy Yan 	mvs_write_port_vsr_data,
77820b09c29SAndy Yan 	mvs_write_port_vsr_addr,
77920b09c29SAndy Yan 	mvs_read_port_irq_stat,
78020b09c29SAndy Yan 	mvs_write_port_irq_stat,
78120b09c29SAndy Yan 	mvs_read_port_irq_mask,
78220b09c29SAndy Yan 	mvs_write_port_irq_mask,
78320b09c29SAndy Yan 	mvs_64xx_command_active,
7849dc9fd94SSrinivas 	mvs_64xx_clear_srs_irq,
78520b09c29SAndy Yan 	mvs_64xx_issue_stop,
78620b09c29SAndy Yan 	mvs_start_delivery,
78720b09c29SAndy Yan 	mvs_rx_update,
78820b09c29SAndy Yan 	mvs_int_full,
78920b09c29SAndy Yan 	mvs_64xx_assign_reg_set,
79020b09c29SAndy Yan 	mvs_64xx_free_reg_set,
79120b09c29SAndy Yan 	mvs_get_prd_size,
79220b09c29SAndy Yan 	mvs_get_prd_count,
79320b09c29SAndy Yan 	mvs_64xx_make_prd,
79420b09c29SAndy Yan 	mvs_64xx_detect_porttype,
79520b09c29SAndy Yan 	mvs_64xx_oob_done,
79620b09c29SAndy Yan 	mvs_64xx_fix_phy_info,
79720b09c29SAndy Yan 	mvs_64xx_phy_work_around,
79820b09c29SAndy Yan 	mvs_64xx_phy_set_link_rate,
79920b09c29SAndy Yan 	mvs_hw_max_link_rate,
80020b09c29SAndy Yan 	mvs_64xx_phy_disable,
80120b09c29SAndy Yan 	mvs_64xx_phy_enable,
80220b09c29SAndy Yan 	mvs_64xx_phy_reset,
80320b09c29SAndy Yan 	mvs_64xx_stp_reset,
80420b09c29SAndy Yan 	mvs_64xx_clear_active_cmds,
80520b09c29SAndy Yan 	mvs_64xx_spi_read_data,
80620b09c29SAndy Yan 	mvs_64xx_spi_write_data,
80720b09c29SAndy Yan 	mvs_64xx_spi_buildcmd,
80820b09c29SAndy Yan 	mvs_64xx_spi_issuecmd,
80920b09c29SAndy Yan 	mvs_64xx_spi_waitdataready,
81020b09c29SAndy Yan 	mvs_64xx_fix_dma,
81183c7b61cSXiangliang Yu 	mvs_64xx_tune_interrupt,
812534ff101SXiangliang Yu 	NULL,
81320b09c29SAndy Yan };
81420b09c29SAndy Yan 
815