xref: /openbmc/linux/drivers/scsi/mvsas/mv_64xx.c (revision 14bf41dcef651c13911a1715e83220732a3a4071)
1dd4969a8SJeff Garzik /*
220b09c29SAndy Yan  * Marvell 88SE64xx hardware specific
320b09c29SAndy Yan  *
420b09c29SAndy Yan  * Copyright 2007 Red Hat, Inc.
520b09c29SAndy Yan  * Copyright 2008 Marvell. <kewei@marvell.com>
60b15fb1fSXiangliang Yu  * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
720b09c29SAndy Yan  *
820b09c29SAndy Yan  * This file is licensed under GPLv2.
920b09c29SAndy Yan  *
1020b09c29SAndy Yan  * This program is free software; you can redistribute it and/or
1120b09c29SAndy Yan  * modify it under the terms of the GNU General Public License as
1220b09c29SAndy Yan  * published by the Free Software Foundation; version 2 of the
1320b09c29SAndy Yan  * License.
1420b09c29SAndy Yan  *
1520b09c29SAndy Yan  * This program is distributed in the hope that it will be useful,
1620b09c29SAndy Yan  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1720b09c29SAndy Yan  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1820b09c29SAndy Yan  * General Public License for more details.
1920b09c29SAndy Yan  *
2020b09c29SAndy Yan  * You should have received a copy of the GNU General Public License
2120b09c29SAndy Yan  * along with this program; if not, write to the Free Software
2220b09c29SAndy Yan  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
2320b09c29SAndy Yan  * USA
24dd4969a8SJeff Garzik */
25dd4969a8SJeff Garzik 
26dd4969a8SJeff Garzik #include "mv_sas.h"
27dd4969a8SJeff Garzik #include "mv_64xx.h"
28dd4969a8SJeff Garzik #include "mv_chips.h"
29dd4969a8SJeff Garzik 
3020b09c29SAndy Yan static void mvs_64xx_detect_porttype(struct mvs_info *mvi, int i)
31dd4969a8SJeff Garzik {
32dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
33dd4969a8SJeff Garzik 	u32 reg;
34dd4969a8SJeff Garzik 	struct mvs_phy *phy = &mvi->phy[i];
35dd4969a8SJeff Garzik 
3620b09c29SAndy Yan 	reg = mr32(MVS_GBL_PORT_TYPE);
3720b09c29SAndy Yan 	phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
38dd4969a8SJeff Garzik 	if (reg & MODE_SAS_SATA & (1 << i))
39dd4969a8SJeff Garzik 		phy->phy_type |= PORT_TYPE_SAS;
40dd4969a8SJeff Garzik 	else
41dd4969a8SJeff Garzik 		phy->phy_type |= PORT_TYPE_SATA;
42dd4969a8SJeff Garzik }
43dd4969a8SJeff Garzik 
446f039790SGreg Kroah-Hartman static void mvs_64xx_enable_xmt(struct mvs_info *mvi, int phy_id)
45dd4969a8SJeff Garzik {
46dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
47dd4969a8SJeff Garzik 	u32 tmp;
48dd4969a8SJeff Garzik 
4920b09c29SAndy Yan 	tmp = mr32(MVS_PCS);
50a4632aaeSXiangliang Yu 	if (mvi->chip->n_phy <= MVS_SOC_PORTS)
5120b09c29SAndy Yan 		tmp |= 1 << (phy_id + PCS_EN_PORT_XMT_SHIFT);
52dd4969a8SJeff Garzik 	else
5320b09c29SAndy Yan 		tmp |= 1 << (phy_id + PCS_EN_PORT_XMT_SHIFT2);
5420b09c29SAndy Yan 	mw32(MVS_PCS, tmp);
55dd4969a8SJeff Garzik }
56dd4969a8SJeff Garzik 
576f039790SGreg Kroah-Hartman static void mvs_64xx_phy_hacks(struct mvs_info *mvi)
58dd4969a8SJeff Garzik {
59dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
6084fbd0ceSXiangliang Yu 	int i;
61dd4969a8SJeff Garzik 
6220b09c29SAndy Yan 	mvs_phy_hacks(mvi);
63dd4969a8SJeff Garzik 
6420b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
6584fbd0ceSXiangliang Yu 		for (i = 0; i < MVS_SOC_PORTS; i++) {
6684fbd0ceSXiangliang Yu 			mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE8);
6784fbd0ceSXiangliang Yu 			mvs_write_port_vsr_data(mvi, i, 0x2F0);
6884fbd0ceSXiangliang Yu 		}
6920b09c29SAndy Yan 	} else {
7020b09c29SAndy Yan 		/* disable auto port detection */
7120b09c29SAndy Yan 		mw32(MVS_GBL_PORT_TYPE, 0);
7220b09c29SAndy Yan 		for (i = 0; i < mvi->chip->n_phy; i++) {
7320b09c29SAndy Yan 			mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE7);
7420b09c29SAndy Yan 			mvs_write_port_vsr_data(mvi, i, 0x90000000);
7520b09c29SAndy Yan 			mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE9);
7620b09c29SAndy Yan 			mvs_write_port_vsr_data(mvi, i, 0x50f2);
7720b09c29SAndy Yan 			mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE11);
7820b09c29SAndy Yan 			mvs_write_port_vsr_data(mvi, i, 0x0e);
7920b09c29SAndy Yan 		}
8020b09c29SAndy Yan 	}
81dd4969a8SJeff Garzik }
82dd4969a8SJeff Garzik 
8320b09c29SAndy Yan static void mvs_64xx_stp_reset(struct mvs_info *mvi, u32 phy_id)
8420b09c29SAndy Yan {
8520b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
8620b09c29SAndy Yan 	u32 reg, tmp;
8720b09c29SAndy Yan 
8820b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
89a4632aaeSXiangliang Yu 		if (phy_id < MVS_SOC_PORTS)
9020b09c29SAndy Yan 			pci_read_config_dword(mvi->pdev, PCR_PHY_CTL, &reg);
9120b09c29SAndy Yan 		else
9220b09c29SAndy Yan 			pci_read_config_dword(mvi->pdev, PCR_PHY_CTL2, &reg);
9320b09c29SAndy Yan 
9420b09c29SAndy Yan 	} else
9520b09c29SAndy Yan 		reg = mr32(MVS_PHY_CTL);
9620b09c29SAndy Yan 
9720b09c29SAndy Yan 	tmp = reg;
98a4632aaeSXiangliang Yu 	if (phy_id < MVS_SOC_PORTS)
9920b09c29SAndy Yan 		tmp |= (1U << phy_id) << PCTL_LINK_OFFS;
10020b09c29SAndy Yan 	else
101a4632aaeSXiangliang Yu 		tmp |= (1U << (phy_id - MVS_SOC_PORTS)) << PCTL_LINK_OFFS;
10220b09c29SAndy Yan 
10320b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
104a4632aaeSXiangliang Yu 		if (phy_id < MVS_SOC_PORTS) {
10520b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL, tmp);
10620b09c29SAndy Yan 			mdelay(10);
10720b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL, reg);
10820b09c29SAndy Yan 		} else {
10920b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL2, tmp);
11020b09c29SAndy Yan 			mdelay(10);
11120b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL2, reg);
11220b09c29SAndy Yan 		}
11320b09c29SAndy Yan 	} else {
11420b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
11520b09c29SAndy Yan 		mdelay(10);
11620b09c29SAndy Yan 		mw32(MVS_PHY_CTL, reg);
11720b09c29SAndy Yan 	}
11820b09c29SAndy Yan }
11920b09c29SAndy Yan 
12020b09c29SAndy Yan static void mvs_64xx_phy_reset(struct mvs_info *mvi, u32 phy_id, int hard)
12120b09c29SAndy Yan {
12220b09c29SAndy Yan 	u32 tmp;
12320b09c29SAndy Yan 	tmp = mvs_read_port_irq_stat(mvi, phy_id);
12420b09c29SAndy Yan 	tmp &= ~PHYEV_RDY_CH;
12520b09c29SAndy Yan 	mvs_write_port_irq_stat(mvi, phy_id, tmp);
12620b09c29SAndy Yan 	tmp = mvs_read_phy_ctl(mvi, phy_id);
127a4632aaeSXiangliang Yu 	if (hard == MVS_HARD_RESET)
12820b09c29SAndy Yan 		tmp |= PHY_RST_HARD;
129a4632aaeSXiangliang Yu 	else if (hard == MVS_SOFT_RESET)
13020b09c29SAndy Yan 		tmp |= PHY_RST;
13120b09c29SAndy Yan 	mvs_write_phy_ctl(mvi, phy_id, tmp);
13220b09c29SAndy Yan 	if (hard) {
13320b09c29SAndy Yan 		do {
13420b09c29SAndy Yan 			tmp = mvs_read_phy_ctl(mvi, phy_id);
13520b09c29SAndy Yan 		} while (tmp & PHY_RST_HARD);
13620b09c29SAndy Yan 	}
13720b09c29SAndy Yan }
13820b09c29SAndy Yan 
139*14bf41dcSBaoyou Xie static void
140*14bf41dcSBaoyou Xie mvs_64xx_clear_srs_irq(struct mvs_info *mvi, u8 reg_set, u8 clear_all)
1419dc9fd94SSrinivas {
1429dc9fd94SSrinivas 	void __iomem *regs = mvi->regs;
1439dc9fd94SSrinivas 	u32 tmp;
1449dc9fd94SSrinivas 	if (clear_all) {
1459dc9fd94SSrinivas 		tmp = mr32(MVS_INT_STAT_SRS_0);
1469dc9fd94SSrinivas 		if (tmp) {
1479dc9fd94SSrinivas 			printk(KERN_DEBUG "check SRS 0 %08X.\n", tmp);
1489dc9fd94SSrinivas 			mw32(MVS_INT_STAT_SRS_0, tmp);
1499dc9fd94SSrinivas 		}
1509dc9fd94SSrinivas 	} else {
1519dc9fd94SSrinivas 		tmp = mr32(MVS_INT_STAT_SRS_0);
1529dc9fd94SSrinivas 		if (tmp &  (1 << (reg_set % 32))) {
1539dc9fd94SSrinivas 			printk(KERN_DEBUG "register set 0x%x was stopped.\n",
1549dc9fd94SSrinivas 			       reg_set);
1559dc9fd94SSrinivas 			mw32(MVS_INT_STAT_SRS_0, 1 << (reg_set % 32));
1569dc9fd94SSrinivas 		}
1579dc9fd94SSrinivas 	}
1589dc9fd94SSrinivas }
1599dc9fd94SSrinivas 
1606f039790SGreg Kroah-Hartman static int mvs_64xx_chip_reset(struct mvs_info *mvi)
16120b09c29SAndy Yan {
16220b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
16320b09c29SAndy Yan 	u32 tmp;
16420b09c29SAndy Yan 	int i;
16520b09c29SAndy Yan 
16620b09c29SAndy Yan 	/* make sure interrupts are masked immediately (paranoia) */
16720b09c29SAndy Yan 	mw32(MVS_GBL_CTL, 0);
16820b09c29SAndy Yan 	tmp = mr32(MVS_GBL_CTL);
16920b09c29SAndy Yan 
17020b09c29SAndy Yan 	/* Reset Controller */
17120b09c29SAndy Yan 	if (!(tmp & HBA_RST)) {
17220b09c29SAndy Yan 		if (mvi->flags & MVF_PHY_PWR_FIX) {
17320b09c29SAndy Yan 			pci_read_config_dword(mvi->pdev, PCR_PHY_CTL, &tmp);
17420b09c29SAndy Yan 			tmp &= ~PCTL_PWR_OFF;
17520b09c29SAndy Yan 			tmp |= PCTL_PHY_DSBL;
17620b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL, tmp);
17720b09c29SAndy Yan 
17820b09c29SAndy Yan 			pci_read_config_dword(mvi->pdev, PCR_PHY_CTL2, &tmp);
17920b09c29SAndy Yan 			tmp &= ~PCTL_PWR_OFF;
18020b09c29SAndy Yan 			tmp |= PCTL_PHY_DSBL;
18120b09c29SAndy Yan 			pci_write_config_dword(mvi->pdev, PCR_PHY_CTL2, tmp);
18220b09c29SAndy Yan 		}
18320b09c29SAndy Yan 	}
18420b09c29SAndy Yan 
18520b09c29SAndy Yan 	/* make sure interrupts are masked immediately (paranoia) */
18620b09c29SAndy Yan 	mw32(MVS_GBL_CTL, 0);
18720b09c29SAndy Yan 	tmp = mr32(MVS_GBL_CTL);
18820b09c29SAndy Yan 
18920b09c29SAndy Yan 	/* Reset Controller */
19020b09c29SAndy Yan 	if (!(tmp & HBA_RST)) {
19120b09c29SAndy Yan 		/* global reset, incl. COMRESET/H_RESET_N (self-clearing) */
19220b09c29SAndy Yan 		mw32_f(MVS_GBL_CTL, HBA_RST);
19320b09c29SAndy Yan 	}
19420b09c29SAndy Yan 
19520b09c29SAndy Yan 	/* wait for reset to finish; timeout is just a guess */
19620b09c29SAndy Yan 	i = 1000;
19720b09c29SAndy Yan 	while (i-- > 0) {
19820b09c29SAndy Yan 		msleep(10);
19920b09c29SAndy Yan 
20020b09c29SAndy Yan 		if (!(mr32(MVS_GBL_CTL) & HBA_RST))
20120b09c29SAndy Yan 			break;
20220b09c29SAndy Yan 	}
20320b09c29SAndy Yan 	if (mr32(MVS_GBL_CTL) & HBA_RST) {
20420b09c29SAndy Yan 		dev_printk(KERN_ERR, mvi->dev, "HBA reset failed\n");
20520b09c29SAndy Yan 		return -EBUSY;
20620b09c29SAndy Yan 	}
20720b09c29SAndy Yan 	return 0;
20820b09c29SAndy Yan }
20920b09c29SAndy Yan 
21020b09c29SAndy Yan static void mvs_64xx_phy_disable(struct mvs_info *mvi, u32 phy_id)
21120b09c29SAndy Yan {
21220b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
21320b09c29SAndy Yan 	u32 tmp;
21420b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
21520b09c29SAndy Yan 		u32 offs;
21620b09c29SAndy Yan 		if (phy_id < 4)
21720b09c29SAndy Yan 			offs = PCR_PHY_CTL;
21820b09c29SAndy Yan 		else {
21920b09c29SAndy Yan 			offs = PCR_PHY_CTL2;
22020b09c29SAndy Yan 			phy_id -= 4;
22120b09c29SAndy Yan 		}
22220b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, offs, &tmp);
22320b09c29SAndy Yan 		tmp |= 1U << (PCTL_PHY_DSBL_OFFS + phy_id);
22420b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, offs, tmp);
22520b09c29SAndy Yan 	} else {
22620b09c29SAndy Yan 		tmp = mr32(MVS_PHY_CTL);
22720b09c29SAndy Yan 		tmp |= 1U << (PCTL_PHY_DSBL_OFFS + phy_id);
22820b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
22920b09c29SAndy Yan 	}
23020b09c29SAndy Yan }
23120b09c29SAndy Yan 
23220b09c29SAndy Yan static void mvs_64xx_phy_enable(struct mvs_info *mvi, u32 phy_id)
23320b09c29SAndy Yan {
23420b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
23520b09c29SAndy Yan 	u32 tmp;
23620b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
23720b09c29SAndy Yan 		u32 offs;
23820b09c29SAndy Yan 		if (phy_id < 4)
23920b09c29SAndy Yan 			offs = PCR_PHY_CTL;
24020b09c29SAndy Yan 		else {
24120b09c29SAndy Yan 			offs = PCR_PHY_CTL2;
24220b09c29SAndy Yan 			phy_id -= 4;
24320b09c29SAndy Yan 		}
24420b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, offs, &tmp);
24520b09c29SAndy Yan 		tmp &= ~(1U << (PCTL_PHY_DSBL_OFFS + phy_id));
24620b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, offs, tmp);
24720b09c29SAndy Yan 	} else {
24820b09c29SAndy Yan 		tmp = mr32(MVS_PHY_CTL);
24920b09c29SAndy Yan 		tmp &= ~(1U << (PCTL_PHY_DSBL_OFFS + phy_id));
25020b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
25120b09c29SAndy Yan 	}
25220b09c29SAndy Yan }
25320b09c29SAndy Yan 
2546f039790SGreg Kroah-Hartman static int mvs_64xx_init(struct mvs_info *mvi)
25520b09c29SAndy Yan {
25620b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
25720b09c29SAndy Yan 	int i;
25820b09c29SAndy Yan 	u32 tmp, cctl;
25920b09c29SAndy Yan 
26020b09c29SAndy Yan 	if (mvi->pdev && mvi->pdev->revision == 0)
26120b09c29SAndy Yan 		mvi->flags |= MVF_PHY_PWR_FIX;
26220b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
26320b09c29SAndy Yan 		mvs_show_pcie_usage(mvi);
26420b09c29SAndy Yan 		tmp = mvs_64xx_chip_reset(mvi);
26520b09c29SAndy Yan 		if (tmp)
26620b09c29SAndy Yan 			return tmp;
26720b09c29SAndy Yan 	} else {
26820b09c29SAndy Yan 		tmp = mr32(MVS_PHY_CTL);
26920b09c29SAndy Yan 		tmp &= ~PCTL_PWR_OFF;
27020b09c29SAndy Yan 		tmp |= PCTL_PHY_DSBL;
27120b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
27220b09c29SAndy Yan 	}
27320b09c29SAndy Yan 
27420b09c29SAndy Yan 	/* Init Chip */
27520b09c29SAndy Yan 	/* make sure RST is set; HBA_RST /should/ have done that for us */
27620b09c29SAndy Yan 	cctl = mr32(MVS_CTL) & 0xFFFF;
27720b09c29SAndy Yan 	if (cctl & CCTL_RST)
27820b09c29SAndy Yan 		cctl &= ~CCTL_RST;
27920b09c29SAndy Yan 	else
28020b09c29SAndy Yan 		mw32_f(MVS_CTL, cctl | CCTL_RST);
28120b09c29SAndy Yan 
28220b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
28320b09c29SAndy Yan 		/* write to device control _AND_ device status register */
28420b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, PCR_DEV_CTRL, &tmp);
28520b09c29SAndy Yan 		tmp &= ~PRD_REQ_MASK;
28620b09c29SAndy Yan 		tmp |= PRD_REQ_SIZE;
28720b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, PCR_DEV_CTRL, tmp);
28820b09c29SAndy Yan 
28920b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, PCR_PHY_CTL, &tmp);
29020b09c29SAndy Yan 		tmp &= ~PCTL_PWR_OFF;
29120b09c29SAndy Yan 		tmp &= ~PCTL_PHY_DSBL;
29220b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, PCR_PHY_CTL, tmp);
29320b09c29SAndy Yan 
29420b09c29SAndy Yan 		pci_read_config_dword(mvi->pdev, PCR_PHY_CTL2, &tmp);
29520b09c29SAndy Yan 		tmp &= PCTL_PWR_OFF;
29620b09c29SAndy Yan 		tmp &= ~PCTL_PHY_DSBL;
29720b09c29SAndy Yan 		pci_write_config_dword(mvi->pdev, PCR_PHY_CTL2, tmp);
29820b09c29SAndy Yan 	} else {
29920b09c29SAndy Yan 		tmp = mr32(MVS_PHY_CTL);
30020b09c29SAndy Yan 		tmp &= ~PCTL_PWR_OFF;
30120b09c29SAndy Yan 		tmp |= PCTL_COM_ON;
30220b09c29SAndy Yan 		tmp &= ~PCTL_PHY_DSBL;
30320b09c29SAndy Yan 		tmp |= PCTL_LINK_RST;
30420b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
30520b09c29SAndy Yan 		msleep(100);
30620b09c29SAndy Yan 		tmp &= ~PCTL_LINK_RST;
30720b09c29SAndy Yan 		mw32(MVS_PHY_CTL, tmp);
30820b09c29SAndy Yan 		msleep(100);
30920b09c29SAndy Yan 	}
31020b09c29SAndy Yan 
31120b09c29SAndy Yan 	/* reset control */
31220b09c29SAndy Yan 	mw32(MVS_PCS, 0);		/* MVS_PCS */
31320b09c29SAndy Yan 	/* init phys */
31420b09c29SAndy Yan 	mvs_64xx_phy_hacks(mvi);
31520b09c29SAndy Yan 
31684fbd0ceSXiangliang Yu 	tmp = mvs_cr32(mvi, CMD_PHY_MODE_21);
31784fbd0ceSXiangliang Yu 	tmp &= 0x0000ffff;
31884fbd0ceSXiangliang Yu 	tmp |= 0x00fa0000;
31984fbd0ceSXiangliang Yu 	mvs_cw32(mvi, CMD_PHY_MODE_21, tmp);
32084fbd0ceSXiangliang Yu 
32120b09c29SAndy Yan 	/* enable auto port detection */
32220b09c29SAndy Yan 	mw32(MVS_GBL_PORT_TYPE, MODE_AUTO_DET_EN);
32320b09c29SAndy Yan 
32420b09c29SAndy Yan 	mw32(MVS_CMD_LIST_LO, mvi->slot_dma);
32520b09c29SAndy Yan 	mw32(MVS_CMD_LIST_HI, (mvi->slot_dma >> 16) >> 16);
32620b09c29SAndy Yan 
32720b09c29SAndy Yan 	mw32(MVS_RX_FIS_LO, mvi->rx_fis_dma);
32820b09c29SAndy Yan 	mw32(MVS_RX_FIS_HI, (mvi->rx_fis_dma >> 16) >> 16);
32920b09c29SAndy Yan 
33020b09c29SAndy Yan 	mw32(MVS_TX_CFG, MVS_CHIP_SLOT_SZ);
33120b09c29SAndy Yan 	mw32(MVS_TX_LO, mvi->tx_dma);
33220b09c29SAndy Yan 	mw32(MVS_TX_HI, (mvi->tx_dma >> 16) >> 16);
33320b09c29SAndy Yan 
33420b09c29SAndy Yan 	mw32(MVS_RX_CFG, MVS_RX_RING_SZ);
33520b09c29SAndy Yan 	mw32(MVS_RX_LO, mvi->rx_dma);
33620b09c29SAndy Yan 	mw32(MVS_RX_HI, (mvi->rx_dma >> 16) >> 16);
33720b09c29SAndy Yan 
33820b09c29SAndy Yan 	for (i = 0; i < mvi->chip->n_phy; i++) {
33920b09c29SAndy Yan 		/* set phy local SAS address */
34020b09c29SAndy Yan 		/* should set little endian SAS address to 64xx chip */
34120b09c29SAndy Yan 		mvs_set_sas_addr(mvi, i, PHYR_ADDR_LO, PHYR_ADDR_HI,
34220b09c29SAndy Yan 				cpu_to_be64(mvi->phy[i].dev_sas_addr));
34320b09c29SAndy Yan 
34420b09c29SAndy Yan 		mvs_64xx_enable_xmt(mvi, i);
34520b09c29SAndy Yan 
346a4632aaeSXiangliang Yu 		mvs_64xx_phy_reset(mvi, i, MVS_HARD_RESET);
34720b09c29SAndy Yan 		msleep(500);
34820b09c29SAndy Yan 		mvs_64xx_detect_porttype(mvi, i);
34920b09c29SAndy Yan 	}
35020b09c29SAndy Yan 	if (mvi->flags & MVF_FLAG_SOC) {
35120b09c29SAndy Yan 		/* set select registers */
35220b09c29SAndy Yan 		writel(0x0E008000, regs + 0x000);
35320b09c29SAndy Yan 		writel(0x59000008, regs + 0x004);
35420b09c29SAndy Yan 		writel(0x20, regs + 0x008);
35520b09c29SAndy Yan 		writel(0x20, regs + 0x00c);
35620b09c29SAndy Yan 		writel(0x20, regs + 0x010);
35720b09c29SAndy Yan 		writel(0x20, regs + 0x014);
35820b09c29SAndy Yan 		writel(0x20, regs + 0x018);
35920b09c29SAndy Yan 		writel(0x20, regs + 0x01c);
36020b09c29SAndy Yan 	}
36120b09c29SAndy Yan 	for (i = 0; i < mvi->chip->n_phy; i++) {
36220b09c29SAndy Yan 		/* clear phy int status */
36320b09c29SAndy Yan 		tmp = mvs_read_port_irq_stat(mvi, i);
36420b09c29SAndy Yan 		tmp &= ~PHYEV_SIG_FIS;
36520b09c29SAndy Yan 		mvs_write_port_irq_stat(mvi, i, tmp);
36620b09c29SAndy Yan 
36720b09c29SAndy Yan 		/* set phy int mask */
36820b09c29SAndy Yan 		tmp = PHYEV_RDY_CH | PHYEV_BROAD_CH | PHYEV_UNASSOC_FIS |
36920b09c29SAndy Yan 			PHYEV_ID_DONE | PHYEV_DCDR_ERR | PHYEV_CRC_ERR |
37020b09c29SAndy Yan 			PHYEV_DEC_ERR;
37120b09c29SAndy Yan 		mvs_write_port_irq_mask(mvi, i, tmp);
37220b09c29SAndy Yan 
37320b09c29SAndy Yan 		msleep(100);
37420b09c29SAndy Yan 		mvs_update_phyinfo(mvi, i, 1);
37520b09c29SAndy Yan 	}
37620b09c29SAndy Yan 
37720b09c29SAndy Yan 	/* little endian for open address and command table, etc. */
37820b09c29SAndy Yan 	cctl = mr32(MVS_CTL);
37920b09c29SAndy Yan 	cctl |= CCTL_ENDIAN_CMD;
38020b09c29SAndy Yan 	cctl |= CCTL_ENDIAN_DATA;
38120b09c29SAndy Yan 	cctl &= ~CCTL_ENDIAN_OPEN;
38220b09c29SAndy Yan 	cctl |= CCTL_ENDIAN_RSP;
38320b09c29SAndy Yan 	mw32_f(MVS_CTL, cctl);
38420b09c29SAndy Yan 
38520b09c29SAndy Yan 	/* reset CMD queue */
38620b09c29SAndy Yan 	tmp = mr32(MVS_PCS);
38720b09c29SAndy Yan 	tmp |= PCS_CMD_RST;
38884fbd0ceSXiangliang Yu 	tmp &= ~PCS_SELF_CLEAR;
38920b09c29SAndy Yan 	mw32(MVS_PCS, tmp);
390e144f7efSXiangliang Yu 	/*
391e144f7efSXiangliang Yu 	 * the max count is 0x1ff, while our max slot is 0x200,
39220b09c29SAndy Yan 	 * it will make count 0.
39320b09c29SAndy Yan 	 */
39420b09c29SAndy Yan 	tmp = 0;
39584fbd0ceSXiangliang Yu 	if (MVS_CHIP_SLOT_SZ > 0x1ff)
39684fbd0ceSXiangliang Yu 		mw32(MVS_INT_COAL, 0x1ff | COAL_EN);
39784fbd0ceSXiangliang Yu 	else
39884fbd0ceSXiangliang Yu 		mw32(MVS_INT_COAL, MVS_CHIP_SLOT_SZ | COAL_EN);
39920b09c29SAndy Yan 
40083c7b61cSXiangliang Yu 	tmp = 0x10000 | interrupt_coalescing;
40120b09c29SAndy Yan 	mw32(MVS_INT_COAL_TMOUT, tmp);
40220b09c29SAndy Yan 
40320b09c29SAndy Yan 	/* ladies and gentlemen, start your engines */
40420b09c29SAndy Yan 	mw32(MVS_TX_CFG, 0);
40520b09c29SAndy Yan 	mw32(MVS_TX_CFG, MVS_CHIP_SLOT_SZ | TX_EN);
40620b09c29SAndy Yan 	mw32(MVS_RX_CFG, MVS_RX_RING_SZ | RX_EN);
40720b09c29SAndy Yan 	/* enable CMD/CMPL_Q/RESP mode */
40820b09c29SAndy Yan 	mw32(MVS_PCS, PCS_SATA_RETRY | PCS_FIS_RX_EN |
40920b09c29SAndy Yan 		PCS_CMD_EN | PCS_CMD_STOP_ERR);
41020b09c29SAndy Yan 
41120b09c29SAndy Yan 	/* enable completion queue interrupt */
41220b09c29SAndy Yan 	tmp = (CINT_PORT_MASK | CINT_DONE | CINT_MEM | CINT_SRS | CINT_CI_STOP |
41320b09c29SAndy Yan 		CINT_DMA_PCIE);
41420b09c29SAndy Yan 
41520b09c29SAndy Yan 	mw32(MVS_INT_MASK, tmp);
41620b09c29SAndy Yan 
41720b09c29SAndy Yan 	/* Enable SRS interrupt */
41820b09c29SAndy Yan 	mw32(MVS_INT_MASK_SRS_0, 0xFFFF);
41920b09c29SAndy Yan 
42020b09c29SAndy Yan 	return 0;
42120b09c29SAndy Yan }
42220b09c29SAndy Yan 
42320b09c29SAndy Yan static int mvs_64xx_ioremap(struct mvs_info *mvi)
42420b09c29SAndy Yan {
42520b09c29SAndy Yan 	if (!mvs_ioremap(mvi, 4, 2))
42620b09c29SAndy Yan 		return 0;
42720b09c29SAndy Yan 	return -1;
42820b09c29SAndy Yan }
42920b09c29SAndy Yan 
43020b09c29SAndy Yan static void mvs_64xx_iounmap(struct mvs_info *mvi)
43120b09c29SAndy Yan {
43220b09c29SAndy Yan 	mvs_iounmap(mvi->regs);
43320b09c29SAndy Yan 	mvs_iounmap(mvi->regs_ex);
43420b09c29SAndy Yan }
43520b09c29SAndy Yan 
43620b09c29SAndy Yan static void mvs_64xx_interrupt_enable(struct mvs_info *mvi)
437dd4969a8SJeff Garzik {
438dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
439dd4969a8SJeff Garzik 	u32 tmp;
440dd4969a8SJeff Garzik 
44120b09c29SAndy Yan 	tmp = mr32(MVS_GBL_CTL);
44220b09c29SAndy Yan 	mw32(MVS_GBL_CTL, tmp | INT_EN);
443dd4969a8SJeff Garzik }
444dd4969a8SJeff Garzik 
44520b09c29SAndy Yan static void mvs_64xx_interrupt_disable(struct mvs_info *mvi)
446dd4969a8SJeff Garzik {
447dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
448dd4969a8SJeff Garzik 	u32 tmp;
449dd4969a8SJeff Garzik 
45020b09c29SAndy Yan 	tmp = mr32(MVS_GBL_CTL);
45120b09c29SAndy Yan 	mw32(MVS_GBL_CTL, tmp & ~INT_EN);
452dd4969a8SJeff Garzik }
453dd4969a8SJeff Garzik 
45420b09c29SAndy Yan static u32 mvs_64xx_isr_status(struct mvs_info *mvi, int irq)
45520b09c29SAndy Yan {
45620b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
45720b09c29SAndy Yan 	u32 stat;
45820b09c29SAndy Yan 
45920b09c29SAndy Yan 	if (!(mvi->flags & MVF_FLAG_SOC)) {
46020b09c29SAndy Yan 		stat = mr32(MVS_GBL_INT_STAT);
46120b09c29SAndy Yan 
46220b09c29SAndy Yan 		if (stat == 0 || stat == 0xffffffff)
46320b09c29SAndy Yan 			return 0;
46420b09c29SAndy Yan 	} else
46520b09c29SAndy Yan 		stat = 1;
46620b09c29SAndy Yan 	return stat;
46720b09c29SAndy Yan }
46820b09c29SAndy Yan 
46920b09c29SAndy Yan static irqreturn_t mvs_64xx_isr(struct mvs_info *mvi, int irq, u32 stat)
47020b09c29SAndy Yan {
47120b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
47220b09c29SAndy Yan 
47320b09c29SAndy Yan 	/* clear CMD_CMPLT ASAP */
47420b09c29SAndy Yan 	mw32_f(MVS_INT_STAT, CINT_DONE);
4756f8ac161SXiangliang Yu 
47620b09c29SAndy Yan 	spin_lock(&mvi->lock);
47720b09c29SAndy Yan 	mvs_int_full(mvi);
47820b09c29SAndy Yan 	spin_unlock(&mvi->lock);
4796f8ac161SXiangliang Yu 
48020b09c29SAndy Yan 	return IRQ_HANDLED;
48120b09c29SAndy Yan }
48220b09c29SAndy Yan 
48320b09c29SAndy Yan static void mvs_64xx_command_active(struct mvs_info *mvi, u32 slot_idx)
48420b09c29SAndy Yan {
48520b09c29SAndy Yan 	u32 tmp;
48620b09c29SAndy Yan 	mvs_cw32(mvi, 0x40 + (slot_idx >> 3), 1 << (slot_idx % 32));
48720b09c29SAndy Yan 	mvs_cw32(mvi, 0x00 + (slot_idx >> 3), 1 << (slot_idx % 32));
48820b09c29SAndy Yan 	do {
48920b09c29SAndy Yan 		tmp = mvs_cr32(mvi, 0x00 + (slot_idx >> 3));
49020b09c29SAndy Yan 	} while (tmp & 1 << (slot_idx % 32));
49120b09c29SAndy Yan 	do {
49220b09c29SAndy Yan 		tmp = mvs_cr32(mvi, 0x40 + (slot_idx >> 3));
49320b09c29SAndy Yan 	} while (tmp & 1 << (slot_idx % 32));
49420b09c29SAndy Yan }
49520b09c29SAndy Yan 
49620b09c29SAndy Yan static void mvs_64xx_issue_stop(struct mvs_info *mvi, enum mvs_port_type type,
49720b09c29SAndy Yan 				u32 tfs)
49820b09c29SAndy Yan {
49920b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
50020b09c29SAndy Yan 	u32 tmp;
50120b09c29SAndy Yan 
50220b09c29SAndy Yan 	if (type == PORT_TYPE_SATA) {
50320b09c29SAndy Yan 		tmp = mr32(MVS_INT_STAT_SRS_0) | (1U << tfs);
50420b09c29SAndy Yan 		mw32(MVS_INT_STAT_SRS_0, tmp);
50520b09c29SAndy Yan 	}
50620b09c29SAndy Yan 	mw32(MVS_INT_STAT, CINT_CI_STOP);
50720b09c29SAndy Yan 	tmp = mr32(MVS_PCS) | 0xFF00;
50820b09c29SAndy Yan 	mw32(MVS_PCS, tmp);
50920b09c29SAndy Yan }
51020b09c29SAndy Yan 
51120b09c29SAndy Yan static void mvs_64xx_free_reg_set(struct mvs_info *mvi, u8 *tfs)
512dd4969a8SJeff Garzik {
513dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
514dd4969a8SJeff Garzik 	u32 tmp, offs;
515dd4969a8SJeff Garzik 
516dd4969a8SJeff Garzik 	if (*tfs == MVS_ID_NOT_MAPPED)
517dd4969a8SJeff Garzik 		return;
518dd4969a8SJeff Garzik 
519dd4969a8SJeff Garzik 	offs = 1U << ((*tfs & 0x0f) + PCS_EN_SATA_REG_SHIFT);
520dd4969a8SJeff Garzik 	if (*tfs < 16) {
52120b09c29SAndy Yan 		tmp = mr32(MVS_PCS);
52220b09c29SAndy Yan 		mw32(MVS_PCS, tmp & ~offs);
523dd4969a8SJeff Garzik 	} else {
52420b09c29SAndy Yan 		tmp = mr32(MVS_CTL);
52520b09c29SAndy Yan 		mw32(MVS_CTL, tmp & ~offs);
526dd4969a8SJeff Garzik 	}
527dd4969a8SJeff Garzik 
52820b09c29SAndy Yan 	tmp = mr32(MVS_INT_STAT_SRS_0) & (1U << *tfs);
529dd4969a8SJeff Garzik 	if (tmp)
53020b09c29SAndy Yan 		mw32(MVS_INT_STAT_SRS_0, tmp);
531dd4969a8SJeff Garzik 
532dd4969a8SJeff Garzik 	*tfs = MVS_ID_NOT_MAPPED;
53320b09c29SAndy Yan 	return;
534dd4969a8SJeff Garzik }
535dd4969a8SJeff Garzik 
53620b09c29SAndy Yan static u8 mvs_64xx_assign_reg_set(struct mvs_info *mvi, u8 *tfs)
537dd4969a8SJeff Garzik {
538dd4969a8SJeff Garzik 	int i;
539dd4969a8SJeff Garzik 	u32 tmp, offs;
540dd4969a8SJeff Garzik 	void __iomem *regs = mvi->regs;
541dd4969a8SJeff Garzik 
54220b09c29SAndy Yan 	if (*tfs != MVS_ID_NOT_MAPPED)
543dd4969a8SJeff Garzik 		return 0;
544dd4969a8SJeff Garzik 
54520b09c29SAndy Yan 	tmp = mr32(MVS_PCS);
546dd4969a8SJeff Garzik 
547dd4969a8SJeff Garzik 	for (i = 0; i < mvi->chip->srs_sz; i++) {
548dd4969a8SJeff Garzik 		if (i == 16)
54920b09c29SAndy Yan 			tmp = mr32(MVS_CTL);
550dd4969a8SJeff Garzik 		offs = 1U << ((i & 0x0f) + PCS_EN_SATA_REG_SHIFT);
551dd4969a8SJeff Garzik 		if (!(tmp & offs)) {
55220b09c29SAndy Yan 			*tfs = i;
553dd4969a8SJeff Garzik 
554dd4969a8SJeff Garzik 			if (i < 16)
55520b09c29SAndy Yan 				mw32(MVS_PCS, tmp | offs);
556dd4969a8SJeff Garzik 			else
55720b09c29SAndy Yan 				mw32(MVS_CTL, tmp | offs);
55820b09c29SAndy Yan 			tmp = mr32(MVS_INT_STAT_SRS_0) & (1U << i);
559dd4969a8SJeff Garzik 			if (tmp)
56020b09c29SAndy Yan 				mw32(MVS_INT_STAT_SRS_0, tmp);
561dd4969a8SJeff Garzik 			return 0;
562dd4969a8SJeff Garzik 		}
563dd4969a8SJeff Garzik 	}
564dd4969a8SJeff Garzik 	return MVS_ID_NOT_MAPPED;
565dd4969a8SJeff Garzik }
566dd4969a8SJeff Garzik 
567*14bf41dcSBaoyou Xie static void mvs_64xx_make_prd(struct scatterlist *scatter, int nr, void *prd)
56820b09c29SAndy Yan {
56920b09c29SAndy Yan 	int i;
57020b09c29SAndy Yan 	struct scatterlist *sg;
57120b09c29SAndy Yan 	struct mvs_prd *buf_prd = prd;
57220b09c29SAndy Yan 	for_each_sg(scatter, sg, nr, i) {
57320b09c29SAndy Yan 		buf_prd->addr = cpu_to_le64(sg_dma_address(sg));
57420b09c29SAndy Yan 		buf_prd->len = cpu_to_le32(sg_dma_len(sg));
57520b09c29SAndy Yan 		buf_prd++;
57620b09c29SAndy Yan 	}
57720b09c29SAndy Yan }
57820b09c29SAndy Yan 
57920b09c29SAndy Yan static int mvs_64xx_oob_done(struct mvs_info *mvi, int i)
58020b09c29SAndy Yan {
58120b09c29SAndy Yan 	u32 phy_st;
58220b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i,
58320b09c29SAndy Yan 			PHYR_PHY_STAT);
58420b09c29SAndy Yan 	phy_st = mvs_read_port_cfg_data(mvi, i);
58520b09c29SAndy Yan 	if (phy_st & PHY_OOB_DTCTD)
58620b09c29SAndy Yan 		return 1;
58720b09c29SAndy Yan 	return 0;
58820b09c29SAndy Yan }
58920b09c29SAndy Yan 
59020b09c29SAndy Yan static void mvs_64xx_fix_phy_info(struct mvs_info *mvi, int i,
59120b09c29SAndy Yan 				struct sas_identify_frame *id)
59220b09c29SAndy Yan 
59320b09c29SAndy Yan {
59420b09c29SAndy Yan 	struct mvs_phy *phy = &mvi->phy[i];
59520b09c29SAndy Yan 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
59620b09c29SAndy Yan 
59720b09c29SAndy Yan 	sas_phy->linkrate =
59820b09c29SAndy Yan 		(phy->phy_status & PHY_NEG_SPP_PHYS_LINK_RATE_MASK) >>
59920b09c29SAndy Yan 			PHY_NEG_SPP_PHYS_LINK_RATE_MASK_OFFSET;
60020b09c29SAndy Yan 
60120b09c29SAndy Yan 	phy->minimum_linkrate =
60220b09c29SAndy Yan 		(phy->phy_status &
60320b09c29SAndy Yan 			PHY_MIN_SPP_PHYS_LINK_RATE_MASK) >> 8;
60420b09c29SAndy Yan 	phy->maximum_linkrate =
60520b09c29SAndy Yan 		(phy->phy_status &
60620b09c29SAndy Yan 			PHY_MAX_SPP_PHYS_LINK_RATE_MASK) >> 12;
60720b09c29SAndy Yan 
60820b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i, PHYR_IDENTIFY);
60920b09c29SAndy Yan 	phy->dev_info = mvs_read_port_cfg_data(mvi, i);
61020b09c29SAndy Yan 
61120b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i, PHYR_ATT_DEV_INFO);
61220b09c29SAndy Yan 	phy->att_dev_info = mvs_read_port_cfg_data(mvi, i);
61320b09c29SAndy Yan 
61420b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i, PHYR_ATT_ADDR_HI);
61520b09c29SAndy Yan 	phy->att_dev_sas_addr =
61620b09c29SAndy Yan 	     (u64) mvs_read_port_cfg_data(mvi, i) << 32;
61720b09c29SAndy Yan 	mvs_write_port_cfg_addr(mvi, i, PHYR_ATT_ADDR_LO);
61820b09c29SAndy Yan 	phy->att_dev_sas_addr |= mvs_read_port_cfg_data(mvi, i);
61920b09c29SAndy Yan 	phy->att_dev_sas_addr = SAS_ADDR(&phy->att_dev_sas_addr);
62020b09c29SAndy Yan }
62120b09c29SAndy Yan 
62220b09c29SAndy Yan static void mvs_64xx_phy_work_around(struct mvs_info *mvi, int i)
62320b09c29SAndy Yan {
62420b09c29SAndy Yan 	u32 tmp;
62520b09c29SAndy Yan 	struct mvs_phy *phy = &mvi->phy[i];
62620b09c29SAndy Yan 	mvs_write_port_vsr_addr(mvi, i, VSR_PHY_MODE6);
62720b09c29SAndy Yan 	tmp = mvs_read_port_vsr_data(mvi, i);
62820b09c29SAndy Yan 	if (((phy->phy_status & PHY_NEG_SPP_PHYS_LINK_RATE_MASK) >>
62920b09c29SAndy Yan 	     PHY_NEG_SPP_PHYS_LINK_RATE_MASK_OFFSET) ==
63020b09c29SAndy Yan 		SAS_LINK_RATE_1_5_GBPS)
63120b09c29SAndy Yan 		tmp &= ~PHY_MODE6_LATECLK;
63220b09c29SAndy Yan 	else
63320b09c29SAndy Yan 		tmp |= PHY_MODE6_LATECLK;
63420b09c29SAndy Yan 	mvs_write_port_vsr_data(mvi, i, tmp);
63520b09c29SAndy Yan }
63620b09c29SAndy Yan 
637*14bf41dcSBaoyou Xie static void mvs_64xx_phy_set_link_rate(struct mvs_info *mvi, u32 phy_id,
63820b09c29SAndy Yan 			struct sas_phy_linkrates *rates)
63920b09c29SAndy Yan {
64020b09c29SAndy Yan 	u32 lrmin = 0, lrmax = 0;
64120b09c29SAndy Yan 	u32 tmp;
64220b09c29SAndy Yan 
64320b09c29SAndy Yan 	tmp = mvs_read_phy_ctl(mvi, phy_id);
64420b09c29SAndy Yan 	lrmin = (rates->minimum_linkrate << 8);
64520b09c29SAndy Yan 	lrmax = (rates->maximum_linkrate << 12);
64620b09c29SAndy Yan 
64720b09c29SAndy Yan 	if (lrmin) {
64820b09c29SAndy Yan 		tmp &= ~(0xf << 8);
64920b09c29SAndy Yan 		tmp |= lrmin;
65020b09c29SAndy Yan 	}
65120b09c29SAndy Yan 	if (lrmax) {
65220b09c29SAndy Yan 		tmp &= ~(0xf << 12);
65320b09c29SAndy Yan 		tmp |= lrmax;
65420b09c29SAndy Yan 	}
65520b09c29SAndy Yan 	mvs_write_phy_ctl(mvi, phy_id, tmp);
656a4632aaeSXiangliang Yu 	mvs_64xx_phy_reset(mvi, phy_id, MVS_HARD_RESET);
65720b09c29SAndy Yan }
65820b09c29SAndy Yan 
65920b09c29SAndy Yan static void mvs_64xx_clear_active_cmds(struct mvs_info *mvi)
66020b09c29SAndy Yan {
66120b09c29SAndy Yan 	u32 tmp;
66220b09c29SAndy Yan 	void __iomem *regs = mvi->regs;
66320b09c29SAndy Yan 	tmp = mr32(MVS_PCS);
66420b09c29SAndy Yan 	mw32(MVS_PCS, tmp & 0xFFFF);
66520b09c29SAndy Yan 	mw32(MVS_PCS, tmp);
66620b09c29SAndy Yan 	tmp = mr32(MVS_CTL);
66720b09c29SAndy Yan 	mw32(MVS_CTL, tmp & 0xFFFF);
66820b09c29SAndy Yan 	mw32(MVS_CTL, tmp);
66920b09c29SAndy Yan }
67020b09c29SAndy Yan 
67120b09c29SAndy Yan 
672*14bf41dcSBaoyou Xie static u32 mvs_64xx_spi_read_data(struct mvs_info *mvi)
67320b09c29SAndy Yan {
67420b09c29SAndy Yan 	void __iomem *regs = mvi->regs_ex;
67520b09c29SAndy Yan 	return ior32(SPI_DATA_REG_64XX);
67620b09c29SAndy Yan }
67720b09c29SAndy Yan 
678*14bf41dcSBaoyou Xie static void mvs_64xx_spi_write_data(struct mvs_info *mvi, u32 data)
67920b09c29SAndy Yan {
68020b09c29SAndy Yan 	void __iomem *regs = mvi->regs_ex;
68120b09c29SAndy Yan 	 iow32(SPI_DATA_REG_64XX, data);
68220b09c29SAndy Yan }
68320b09c29SAndy Yan 
68420b09c29SAndy Yan 
685*14bf41dcSBaoyou Xie static int mvs_64xx_spi_buildcmd(struct mvs_info *mvi,
68620b09c29SAndy Yan 			u32      *dwCmd,
68720b09c29SAndy Yan 			u8       cmd,
68820b09c29SAndy Yan 			u8       read,
68920b09c29SAndy Yan 			u8       length,
69020b09c29SAndy Yan 			u32      addr
69120b09c29SAndy Yan 			)
69220b09c29SAndy Yan {
69320b09c29SAndy Yan 	u32  dwTmp;
69420b09c29SAndy Yan 
69520b09c29SAndy Yan 	dwTmp = ((u32)cmd << 24) | ((u32)length << 19);
69620b09c29SAndy Yan 	if (read)
69720b09c29SAndy Yan 		dwTmp |= 1U<<23;
69820b09c29SAndy Yan 
69920b09c29SAndy Yan 	if (addr != MV_MAX_U32) {
70020b09c29SAndy Yan 		dwTmp |= 1U<<22;
70120b09c29SAndy Yan 		dwTmp |= (addr & 0x0003FFFF);
70220b09c29SAndy Yan 	}
70320b09c29SAndy Yan 
70420b09c29SAndy Yan 	*dwCmd = dwTmp;
70520b09c29SAndy Yan 	return 0;
70620b09c29SAndy Yan }
70720b09c29SAndy Yan 
70820b09c29SAndy Yan 
709*14bf41dcSBaoyou Xie static int mvs_64xx_spi_issuecmd(struct mvs_info *mvi, u32 cmd)
71020b09c29SAndy Yan {
71120b09c29SAndy Yan 	void __iomem *regs = mvi->regs_ex;
71220b09c29SAndy Yan 	int     retry;
71320b09c29SAndy Yan 
71420b09c29SAndy Yan 	for (retry = 0; retry < 1; retry++) {
71520b09c29SAndy Yan 		iow32(SPI_CTRL_REG_64XX, SPI_CTRL_VENDOR_ENABLE);
71620b09c29SAndy Yan 		iow32(SPI_CMD_REG_64XX, cmd);
71720b09c29SAndy Yan 		iow32(SPI_CTRL_REG_64XX,
71820b09c29SAndy Yan 			SPI_CTRL_VENDOR_ENABLE | SPI_CTRL_SPISTART);
71920b09c29SAndy Yan 	}
72020b09c29SAndy Yan 
72120b09c29SAndy Yan 	return 0;
72220b09c29SAndy Yan }
72320b09c29SAndy Yan 
724*14bf41dcSBaoyou Xie static int mvs_64xx_spi_waitdataready(struct mvs_info *mvi, u32 timeout)
72520b09c29SAndy Yan {
72620b09c29SAndy Yan 	void __iomem *regs = mvi->regs_ex;
72720b09c29SAndy Yan 	u32 i, dwTmp;
72820b09c29SAndy Yan 
72920b09c29SAndy Yan 	for (i = 0; i < timeout; i++) {
73020b09c29SAndy Yan 		dwTmp = ior32(SPI_CTRL_REG_64XX);
73120b09c29SAndy Yan 		if (!(dwTmp & SPI_CTRL_SPISTART))
73220b09c29SAndy Yan 			return 0;
73320b09c29SAndy Yan 		msleep(10);
73420b09c29SAndy Yan 	}
73520b09c29SAndy Yan 
73620b09c29SAndy Yan 	return -1;
73720b09c29SAndy Yan }
73820b09c29SAndy Yan 
739*14bf41dcSBaoyou Xie static void mvs_64xx_fix_dma(struct mvs_info *mvi, u32 phy_mask,
7408882f081SXiangliang Yu 				int buf_len, int from, void *prd)
74120b09c29SAndy Yan {
74220b09c29SAndy Yan 	int i;
74320b09c29SAndy Yan 	struct mvs_prd *buf_prd = prd;
7448882f081SXiangliang Yu 	dma_addr_t buf_dma = mvi->bulk_buffer_dma;
7458882f081SXiangliang Yu 
74620b09c29SAndy Yan 	buf_prd	+= from;
74720b09c29SAndy Yan 	for (i = 0; i < MAX_SG_ENTRY - from; i++) {
74820b09c29SAndy Yan 		buf_prd->addr = cpu_to_le64(buf_dma);
74920b09c29SAndy Yan 		buf_prd->len = cpu_to_le32(buf_len);
75020b09c29SAndy Yan 		++buf_prd;
75120b09c29SAndy Yan 	}
75220b09c29SAndy Yan }
75320b09c29SAndy Yan 
75483c7b61cSXiangliang Yu static void mvs_64xx_tune_interrupt(struct mvs_info *mvi, u32 time)
75583c7b61cSXiangliang Yu {
75683c7b61cSXiangliang Yu 	void __iomem *regs = mvi->regs;
75783c7b61cSXiangliang Yu 	u32 tmp = 0;
758e144f7efSXiangliang Yu 	/*
759e144f7efSXiangliang Yu 	 * the max count is 0x1ff, while our max slot is 0x200,
76083c7b61cSXiangliang Yu 	 * it will make count 0.
76183c7b61cSXiangliang Yu 	 */
76283c7b61cSXiangliang Yu 	if (time == 0) {
76383c7b61cSXiangliang Yu 		mw32(MVS_INT_COAL, 0);
76483c7b61cSXiangliang Yu 		mw32(MVS_INT_COAL_TMOUT, 0x10000);
76583c7b61cSXiangliang Yu 	} else {
76683c7b61cSXiangliang Yu 		if (MVS_CHIP_SLOT_SZ > 0x1ff)
76783c7b61cSXiangliang Yu 			mw32(MVS_INT_COAL, 0x1ff|COAL_EN);
76883c7b61cSXiangliang Yu 		else
76983c7b61cSXiangliang Yu 			mw32(MVS_INT_COAL, MVS_CHIP_SLOT_SZ|COAL_EN);
77083c7b61cSXiangliang Yu 
77183c7b61cSXiangliang Yu 		tmp = 0x10000 | time;
77283c7b61cSXiangliang Yu 		mw32(MVS_INT_COAL_TMOUT, tmp);
77383c7b61cSXiangliang Yu 	}
77483c7b61cSXiangliang Yu }
77583c7b61cSXiangliang Yu 
77620b09c29SAndy Yan const struct mvs_dispatch mvs_64xx_dispatch = {
77720b09c29SAndy Yan 	"mv64xx",
77820b09c29SAndy Yan 	mvs_64xx_init,
77920b09c29SAndy Yan 	NULL,
78020b09c29SAndy Yan 	mvs_64xx_ioremap,
78120b09c29SAndy Yan 	mvs_64xx_iounmap,
78220b09c29SAndy Yan 	mvs_64xx_isr,
78320b09c29SAndy Yan 	mvs_64xx_isr_status,
78420b09c29SAndy Yan 	mvs_64xx_interrupt_enable,
78520b09c29SAndy Yan 	mvs_64xx_interrupt_disable,
78620b09c29SAndy Yan 	mvs_read_phy_ctl,
78720b09c29SAndy Yan 	mvs_write_phy_ctl,
78820b09c29SAndy Yan 	mvs_read_port_cfg_data,
78920b09c29SAndy Yan 	mvs_write_port_cfg_data,
79020b09c29SAndy Yan 	mvs_write_port_cfg_addr,
79120b09c29SAndy Yan 	mvs_read_port_vsr_data,
79220b09c29SAndy Yan 	mvs_write_port_vsr_data,
79320b09c29SAndy Yan 	mvs_write_port_vsr_addr,
79420b09c29SAndy Yan 	mvs_read_port_irq_stat,
79520b09c29SAndy Yan 	mvs_write_port_irq_stat,
79620b09c29SAndy Yan 	mvs_read_port_irq_mask,
79720b09c29SAndy Yan 	mvs_write_port_irq_mask,
79820b09c29SAndy Yan 	mvs_64xx_command_active,
7999dc9fd94SSrinivas 	mvs_64xx_clear_srs_irq,
80020b09c29SAndy Yan 	mvs_64xx_issue_stop,
80120b09c29SAndy Yan 	mvs_start_delivery,
80220b09c29SAndy Yan 	mvs_rx_update,
80320b09c29SAndy Yan 	mvs_int_full,
80420b09c29SAndy Yan 	mvs_64xx_assign_reg_set,
80520b09c29SAndy Yan 	mvs_64xx_free_reg_set,
80620b09c29SAndy Yan 	mvs_get_prd_size,
80720b09c29SAndy Yan 	mvs_get_prd_count,
80820b09c29SAndy Yan 	mvs_64xx_make_prd,
80920b09c29SAndy Yan 	mvs_64xx_detect_porttype,
81020b09c29SAndy Yan 	mvs_64xx_oob_done,
81120b09c29SAndy Yan 	mvs_64xx_fix_phy_info,
81220b09c29SAndy Yan 	mvs_64xx_phy_work_around,
81320b09c29SAndy Yan 	mvs_64xx_phy_set_link_rate,
81420b09c29SAndy Yan 	mvs_hw_max_link_rate,
81520b09c29SAndy Yan 	mvs_64xx_phy_disable,
81620b09c29SAndy Yan 	mvs_64xx_phy_enable,
81720b09c29SAndy Yan 	mvs_64xx_phy_reset,
81820b09c29SAndy Yan 	mvs_64xx_stp_reset,
81920b09c29SAndy Yan 	mvs_64xx_clear_active_cmds,
82020b09c29SAndy Yan 	mvs_64xx_spi_read_data,
82120b09c29SAndy Yan 	mvs_64xx_spi_write_data,
82220b09c29SAndy Yan 	mvs_64xx_spi_buildcmd,
82320b09c29SAndy Yan 	mvs_64xx_spi_issuecmd,
82420b09c29SAndy Yan 	mvs_64xx_spi_waitdataready,
82520b09c29SAndy Yan 	mvs_64xx_fix_dma,
82683c7b61cSXiangliang Yu 	mvs_64xx_tune_interrupt,
827534ff101SXiangliang Yu 	NULL,
82820b09c29SAndy Yan };
82920b09c29SAndy Yan 
830