xref: /rk3399_ARM-atf/drivers/marvell/comphy/phy-comphy-cp110.c (revision 5a9e46e69c154ee8f77dbec5e036774622b66ad3)
10ade8cd8SKonstantin Porotchkin /*
20ade8cd8SKonstantin Porotchkin  * Copyright (C) 2018 Marvell International Ltd.
30ade8cd8SKonstantin Porotchkin  *
40ade8cd8SKonstantin Porotchkin  * SPDX-License-Identifier:     BSD-3-Clause
50ade8cd8SKonstantin Porotchkin  * https://spdx.org/licenses
60ade8cd8SKonstantin Porotchkin  */
70ade8cd8SKonstantin Porotchkin 
80ade8cd8SKonstantin Porotchkin /* Marvell CP110 SoC COMPHY unit driver */
90ade8cd8SKonstantin Porotchkin 
100ade8cd8SKonstantin Porotchkin #include <errno.h>
1109d40e0eSAntonio Nino Diaz 
1209d40e0eSAntonio Nino Diaz #include <common/debug.h>
1309d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
14*5a9e46e6SGrzegorz Jaszczyk #include <mg_conf_cm3/mg_conf_cm3.h>
1509d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1609d40e0eSAntonio Nino Diaz #include <lib/spinlock.h>
1709d40e0eSAntonio Nino Diaz 
180ade8cd8SKonstantin Porotchkin #include <mvebu_def.h>
190ade8cd8SKonstantin Porotchkin #include "mvebu.h"
200ade8cd8SKonstantin Porotchkin #include "comphy-cp110.h"
2142a29337SGrzegorz Jaszczyk #include "phy-comphy-cp110.h"
2242a29337SGrzegorz Jaszczyk #include "phy-comphy-common.h"
230ade8cd8SKonstantin Porotchkin 
2442a29337SGrzegorz Jaszczyk #if __has_include("phy-porting-layer.h")
2542a29337SGrzegorz Jaszczyk #include "phy-porting-layer.h"
260ade8cd8SKonstantin Porotchkin #else
2742a29337SGrzegorz Jaszczyk #include "phy-default-porting-layer.h"
280ade8cd8SKonstantin Porotchkin #endif
290ade8cd8SKonstantin Porotchkin 
300ade8cd8SKonstantin Porotchkin /* COMPHY speed macro */
310ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_1_25G		0 /* SGMII 1G */
320ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_2_5G		1
330ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_3_125G		2 /* SGMII 2.5G */
340ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_5G			3
350ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_5_15625G		4 /* XFI 5G */
360ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_6G			5
370ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_10_3125G		6 /* XFI 10G */
380ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_MAX		0x3F
390ade8cd8SKonstantin Porotchkin /* The  default speed for IO with fixed known speed */
400ade8cd8SKonstantin Porotchkin #define COMPHY_SPEED_DEFAULT		COMPHY_SPEED_MAX
410ade8cd8SKonstantin Porotchkin 
420ade8cd8SKonstantin Porotchkin /* Commands for comphy driver */
430ade8cd8SKonstantin Porotchkin #define COMPHY_COMMAND_DIGITAL_PWR_OFF		0x00000001
440ade8cd8SKonstantin Porotchkin #define COMPHY_COMMAND_DIGITAL_PWR_ON		0x00000002
450ade8cd8SKonstantin Porotchkin 
460ade8cd8SKonstantin Porotchkin #define COMPHY_PIPE_FROM_COMPHY_ADDR(x)	((x & ~0xffffff) + 0x120000)
470ade8cd8SKonstantin Porotchkin 
480ade8cd8SKonstantin Porotchkin /* System controller registers */
490ade8cd8SKonstantin Porotchkin #define PCIE_MAC_RESET_MASK_PORT0	BIT(13)
500ade8cd8SKonstantin Porotchkin #define PCIE_MAC_RESET_MASK_PORT1	BIT(11)
510ade8cd8SKonstantin Porotchkin #define PCIE_MAC_RESET_MASK_PORT2	BIT(12)
520ade8cd8SKonstantin Porotchkin #define SYS_CTRL_UINIT_SOFT_RESET_REG	0x268
530ade8cd8SKonstantin Porotchkin #define SYS_CTRL_FROM_COMPHY_ADDR(x)	((x & ~0xffffff) + 0x440000)
540ade8cd8SKonstantin Porotchkin 
550ade8cd8SKonstantin Porotchkin /* DFX register spaces */
560ade8cd8SKonstantin Porotchkin #define SAR_RST_PCIE0_CLOCK_CONFIG_CP1_OFFSET	(0)
570ade8cd8SKonstantin Porotchkin #define SAR_RST_PCIE0_CLOCK_CONFIG_CP1_MASK	(0x1 << \
580ade8cd8SKonstantin Porotchkin 					SAR_RST_PCIE0_CLOCK_CONFIG_CP1_OFFSET)
590ade8cd8SKonstantin Porotchkin #define SAR_RST_PCIE1_CLOCK_CONFIG_CP1_OFFSET	(1)
600ade8cd8SKonstantin Porotchkin #define SAR_RST_PCIE1_CLOCK_CONFIG_CP1_MASK	(0x1 << \
610ade8cd8SKonstantin Porotchkin 					SAR_RST_PCIE1_CLOCK_CONFIG_CP1_OFFSET)
620ade8cd8SKonstantin Porotchkin #define SAR_STATUS_0_REG			200
630ade8cd8SKonstantin Porotchkin #define DFX_FROM_COMPHY_ADDR(x)			((x & ~0xffffff) + DFX_BASE)
640ade8cd8SKonstantin Porotchkin 
650ade8cd8SKonstantin Porotchkin /* The same Units Soft Reset Config register are accessed in all PCIe ports
660ade8cd8SKonstantin Porotchkin  * initialization, so a spin lock is defined in case when more than 1 CPUs
670ade8cd8SKonstantin Porotchkin  * resets PCIe MAC and need to access the register in the same time. The spin
680ade8cd8SKonstantin Porotchkin  * lock is shared by all CP110 units.
690ade8cd8SKonstantin Porotchkin  */
700ade8cd8SKonstantin Porotchkin spinlock_t cp110_mac_reset_lock;
710ade8cd8SKonstantin Porotchkin 
720ade8cd8SKonstantin Porotchkin /* These values come from the PCI Express Spec */
730ade8cd8SKonstantin Porotchkin enum pcie_link_width {
740ade8cd8SKonstantin Porotchkin 	PCIE_LNK_WIDTH_RESRV	= 0x00,
750ade8cd8SKonstantin Porotchkin 	PCIE_LNK_X1		= 0x01,
760ade8cd8SKonstantin Porotchkin 	PCIE_LNK_X2		= 0x02,
770ade8cd8SKonstantin Porotchkin 	PCIE_LNK_X4		= 0x04,
780ade8cd8SKonstantin Porotchkin 	PCIE_LNK_X8		= 0x08,
790ade8cd8SKonstantin Porotchkin 	PCIE_LNK_X12		= 0x0C,
800ade8cd8SKonstantin Porotchkin 	PCIE_LNK_X16		= 0x10,
810ade8cd8SKonstantin Porotchkin 	PCIE_LNK_X32		= 0x20,
820ade8cd8SKonstantin Porotchkin 	PCIE_LNK_WIDTH_UNKNOWN  = 0xFF,
830ade8cd8SKonstantin Porotchkin };
840ade8cd8SKonstantin Porotchkin 
8542a29337SGrzegorz Jaszczyk _Bool rx_trainng_done[AP_NUM][CP_NUM][MAX_LANE_NR] = {0};
8642a29337SGrzegorz Jaszczyk 
8742a29337SGrzegorz Jaszczyk static void mvebu_cp110_get_ap_and_cp_nr(uint8_t *ap_nr, uint8_t *cp_nr,
8842a29337SGrzegorz Jaszczyk 					 uint64_t comphy_base)
890ade8cd8SKonstantin Porotchkin {
9042a29337SGrzegorz Jaszczyk #if (AP_NUM == 1)
9142a29337SGrzegorz Jaszczyk 	*ap_nr = 0;
9242a29337SGrzegorz Jaszczyk #else
9342a29337SGrzegorz Jaszczyk 	*ap_nr = (((comphy_base & ~0xffffff) - MVEBU_AP_IO_BASE(0)) /
9442a29337SGrzegorz Jaszczyk 			 AP_IO_OFFSET);
9542a29337SGrzegorz Jaszczyk #endif
960ade8cd8SKonstantin Porotchkin 
9742a29337SGrzegorz Jaszczyk 	*cp_nr = (((comphy_base & ~0xffffff) - MVEBU_AP_IO_BASE(*ap_nr)) /
9842a29337SGrzegorz Jaszczyk 		 MVEBU_CP_OFFSET);
990ade8cd8SKonstantin Porotchkin 
10042a29337SGrzegorz Jaszczyk 	debug("cp_base 0x%llx, ap_io_base 0x%lx, cp_offset 0x%lx\n",
10142a29337SGrzegorz Jaszczyk 	       comphy_base, (unsigned long)MVEBU_AP_IO_BASE(*ap_nr),
10242a29337SGrzegorz Jaszczyk 	       (unsigned long)MVEBU_CP_OFFSET);
1030ade8cd8SKonstantin Porotchkin }
1040ade8cd8SKonstantin Porotchkin 
1050ade8cd8SKonstantin Porotchkin /* Clear PIPE selector - avoid collision with previous configuration */
1060ade8cd8SKonstantin Porotchkin static void mvebu_cp110_comphy_clr_pipe_selector(uint64_t comphy_base,
1070ade8cd8SKonstantin Porotchkin 						 uint8_t comphy_index)
1080ade8cd8SKonstantin Porotchkin {
1090ade8cd8SKonstantin Porotchkin 	uint32_t reg, mask, field;
1100ade8cd8SKonstantin Porotchkin 	uint32_t comphy_offset =
1110ade8cd8SKonstantin Porotchkin 			COMMON_SELECTOR_COMPHYN_FIELD_WIDTH * comphy_index;
1120ade8cd8SKonstantin Porotchkin 
1130ade8cd8SKonstantin Porotchkin 	mask = COMMON_SELECTOR_COMPHY_MASK << comphy_offset;
1140ade8cd8SKonstantin Porotchkin 	reg = mmio_read_32(comphy_base + COMMON_SELECTOR_PIPE_REG_OFFSET);
1150ade8cd8SKonstantin Porotchkin 	field = reg & mask;
1160ade8cd8SKonstantin Porotchkin 
1170ade8cd8SKonstantin Porotchkin 	if (field) {
1180ade8cd8SKonstantin Porotchkin 		reg &= ~mask;
1190ade8cd8SKonstantin Porotchkin 		mmio_write_32(comphy_base + COMMON_SELECTOR_PIPE_REG_OFFSET,
1200ade8cd8SKonstantin Porotchkin 			     reg);
1210ade8cd8SKonstantin Porotchkin 	}
1220ade8cd8SKonstantin Porotchkin }
1230ade8cd8SKonstantin Porotchkin 
1240ade8cd8SKonstantin Porotchkin /* Clear PHY selector - avoid collision with previous configuration */
1250ade8cd8SKonstantin Porotchkin static void mvebu_cp110_comphy_clr_phy_selector(uint64_t comphy_base,
1260ade8cd8SKonstantin Porotchkin 						uint8_t comphy_index)
1270ade8cd8SKonstantin Porotchkin {
1280ade8cd8SKonstantin Porotchkin 	uint32_t reg, mask, field;
1290ade8cd8SKonstantin Porotchkin 	uint32_t comphy_offset =
1300ade8cd8SKonstantin Porotchkin 			COMMON_SELECTOR_COMPHYN_FIELD_WIDTH * comphy_index;
1310ade8cd8SKonstantin Porotchkin 
1320ade8cd8SKonstantin Porotchkin 	mask = COMMON_SELECTOR_COMPHY_MASK << comphy_offset;
1330ade8cd8SKonstantin Porotchkin 	reg = mmio_read_32(comphy_base + COMMON_SELECTOR_PHY_REG_OFFSET);
1340ade8cd8SKonstantin Porotchkin 	field = reg & mask;
1350ade8cd8SKonstantin Porotchkin 
1360ade8cd8SKonstantin Porotchkin 	/* Clear comphy selector - if it was already configured.
1370ade8cd8SKonstantin Porotchkin 	 * (might be that this comphy was configured as PCIe/USB,
1380ade8cd8SKonstantin Porotchkin 	 * in such case, no need to clear comphy selector because PCIe/USB
1390ade8cd8SKonstantin Porotchkin 	 * are controlled by hpipe selector).
1400ade8cd8SKonstantin Porotchkin 	 */
1410ade8cd8SKonstantin Porotchkin 	if (field) {
1420ade8cd8SKonstantin Porotchkin 		reg &= ~mask;
1430ade8cd8SKonstantin Porotchkin 		mmio_write_32(comphy_base + COMMON_SELECTOR_PHY_REG_OFFSET,
1440ade8cd8SKonstantin Porotchkin 			      reg);
1450ade8cd8SKonstantin Porotchkin 	}
1460ade8cd8SKonstantin Porotchkin }
1470ade8cd8SKonstantin Porotchkin 
1480ade8cd8SKonstantin Porotchkin /* PHY selector configures SATA and Network modes */
1490ade8cd8SKonstantin Porotchkin static void mvebu_cp110_comphy_set_phy_selector(uint64_t comphy_base,
1500ade8cd8SKonstantin Porotchkin 				     uint8_t comphy_index, uint32_t comphy_mode)
1510ade8cd8SKonstantin Porotchkin {
1520ade8cd8SKonstantin Porotchkin 	uint32_t reg, mask;
1530ade8cd8SKonstantin Porotchkin 	uint32_t comphy_offset =
1540ade8cd8SKonstantin Porotchkin 			COMMON_SELECTOR_COMPHYN_FIELD_WIDTH * comphy_index;
1550ade8cd8SKonstantin Porotchkin 	int mode;
1560ade8cd8SKonstantin Porotchkin 
1570ade8cd8SKonstantin Porotchkin 	/* If phy selector is used the pipe selector should be marked as
1580ade8cd8SKonstantin Porotchkin 	 * unconnected.
1590ade8cd8SKonstantin Porotchkin 	 */
1600ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_clr_pipe_selector(comphy_base, comphy_index);
1610ade8cd8SKonstantin Porotchkin 
1620ade8cd8SKonstantin Porotchkin 	/* Comphy mode (compound of the IO mode and id). Here, only the IO mode
1630ade8cd8SKonstantin Porotchkin 	 * is required to distinguish between SATA and network modes.
1640ade8cd8SKonstantin Porotchkin 	 */
1650ade8cd8SKonstantin Porotchkin 	mode = COMPHY_GET_MODE(comphy_mode);
1660ade8cd8SKonstantin Porotchkin 
1670ade8cd8SKonstantin Porotchkin 	mask = COMMON_SELECTOR_COMPHY_MASK << comphy_offset;
1680ade8cd8SKonstantin Porotchkin 	reg = mmio_read_32(comphy_base + COMMON_SELECTOR_PHY_REG_OFFSET);
1690ade8cd8SKonstantin Porotchkin 	reg &= ~mask;
1700ade8cd8SKonstantin Porotchkin 
1710ade8cd8SKonstantin Porotchkin 	/* SATA port 0/1 require the same configuration */
1720ade8cd8SKonstantin Porotchkin 	if (mode == COMPHY_SATA_MODE) {
1730ade8cd8SKonstantin Porotchkin 		/* SATA selector values is always 4 */
1740ade8cd8SKonstantin Porotchkin 		reg |= COMMON_SELECTOR_COMPHYN_SATA << comphy_offset;
1750ade8cd8SKonstantin Porotchkin 	} else {
1760ade8cd8SKonstantin Porotchkin 		switch (comphy_index) {
1770ade8cd8SKonstantin Porotchkin 		case(0):
1780ade8cd8SKonstantin Porotchkin 		case(1):
1790ade8cd8SKonstantin Porotchkin 		case(2):
1800ade8cd8SKonstantin Porotchkin 			/* For comphy 0,1, and 2:
1810ade8cd8SKonstantin Porotchkin 			 * Network selector value is always 1.
1820ade8cd8SKonstantin Porotchkin 			 */
1830ade8cd8SKonstantin Porotchkin 			reg |= COMMON_SELECTOR_COMPHY0_1_2_NETWORK <<
1840ade8cd8SKonstantin Porotchkin 				comphy_offset;
1850ade8cd8SKonstantin Porotchkin 			break;
1860ade8cd8SKonstantin Porotchkin 		case(3):
1870ade8cd8SKonstantin Porotchkin 			/* For comphy 3:
1880ade8cd8SKonstantin Porotchkin 			 * 0x1 = RXAUI_Lane1
1890ade8cd8SKonstantin Porotchkin 			 * 0x2 = SGMII/HS-SGMII Port1
1900ade8cd8SKonstantin Porotchkin 			 */
1910ade8cd8SKonstantin Porotchkin 			if (mode == COMPHY_RXAUI_MODE)
1920ade8cd8SKonstantin Porotchkin 				reg |= COMMON_SELECTOR_COMPHY3_RXAUI <<
1930ade8cd8SKonstantin Porotchkin 					comphy_offset;
1940ade8cd8SKonstantin Porotchkin 			else
1950ade8cd8SKonstantin Porotchkin 				reg |= COMMON_SELECTOR_COMPHY3_SGMII <<
1960ade8cd8SKonstantin Porotchkin 					comphy_offset;
1970ade8cd8SKonstantin Porotchkin 			break;
1980ade8cd8SKonstantin Porotchkin 		case(4):
1990ade8cd8SKonstantin Porotchkin 			 /* For comphy 4:
2000ade8cd8SKonstantin Porotchkin 			  * 0x1 = SGMII/HS-SGMII Port1, XFI1/SFI1
2010ade8cd8SKonstantin Porotchkin 			  * 0x2 = SGMII/HS-SGMII Port0: XFI0/SFI0, RXAUI_Lane0
2020ade8cd8SKonstantin Porotchkin 			  *
2030ade8cd8SKonstantin Porotchkin 			  * We want to check if SGMII1/HS_SGMII1 is the
2040ade8cd8SKonstantin Porotchkin 			  * requested mode in order to determine which value
2050ade8cd8SKonstantin Porotchkin 			  * should be set (all other modes use the same value)
2060ade8cd8SKonstantin Porotchkin 			  * so we need to strip the mode, and check the ID
2070ade8cd8SKonstantin Porotchkin 			  * because we might handle SGMII0/HS_SGMII0 too.
2080ade8cd8SKonstantin Porotchkin 			  */
2090ade8cd8SKonstantin Porotchkin 			  /* TODO: need to distinguish between CP110 and CP115
2100ade8cd8SKonstantin Porotchkin 			   * as SFI1/XFI1 available only for CP115.
2110ade8cd8SKonstantin Porotchkin 			   */
2120ade8cd8SKonstantin Porotchkin 			if ((mode == COMPHY_SGMII_MODE ||
2130ade8cd8SKonstantin Porotchkin 			     mode == COMPHY_HS_SGMII_MODE ||
214050eb19cSGrzegorz Jaszczyk 			     mode == COMPHY_SFI_MODE ||
215050eb19cSGrzegorz Jaszczyk 			     mode == COMPHY_XFI_MODE ||
216050eb19cSGrzegorz Jaszczyk 			     mode == COMPHY_AP_MODE)
2170529106cSGrzegorz Jaszczyk 			    && COMPHY_GET_ID(comphy_mode) == 1)
2180ade8cd8SKonstantin Porotchkin 				reg |= COMMON_SELECTOR_COMPHY4_PORT1 <<
2190ade8cd8SKonstantin Porotchkin 					comphy_offset;
2200ade8cd8SKonstantin Porotchkin 			else
2210ade8cd8SKonstantin Porotchkin 				reg |= COMMON_SELECTOR_COMPHY4_ALL_OTHERS <<
2220ade8cd8SKonstantin Porotchkin 					comphy_offset;
2230ade8cd8SKonstantin Porotchkin 			break;
2240ade8cd8SKonstantin Porotchkin 		case(5):
2250ade8cd8SKonstantin Porotchkin 			/* For comphy 5:
2260ade8cd8SKonstantin Porotchkin 			 * 0x1 = SGMII/HS-SGMII Port2
2270ade8cd8SKonstantin Porotchkin 			 * 0x2 = RXAUI Lane1
2280ade8cd8SKonstantin Porotchkin 			 */
2290ade8cd8SKonstantin Porotchkin 			if (mode == COMPHY_RXAUI_MODE)
2300ade8cd8SKonstantin Porotchkin 				reg |= COMMON_SELECTOR_COMPHY5_RXAUI <<
2310ade8cd8SKonstantin Porotchkin 					comphy_offset;
2320ade8cd8SKonstantin Porotchkin 			else
2330ade8cd8SKonstantin Porotchkin 				reg |= COMMON_SELECTOR_COMPHY5_SGMII <<
2340ade8cd8SKonstantin Porotchkin 					comphy_offset;
2350ade8cd8SKonstantin Porotchkin 			break;
2360ade8cd8SKonstantin Porotchkin 		}
2370ade8cd8SKonstantin Porotchkin 	}
2380ade8cd8SKonstantin Porotchkin 
2390ade8cd8SKonstantin Porotchkin 	mmio_write_32(comphy_base + COMMON_SELECTOR_PHY_REG_OFFSET, reg);
2400ade8cd8SKonstantin Porotchkin }
2410ade8cd8SKonstantin Porotchkin 
2420ade8cd8SKonstantin Porotchkin /* PIPE selector configures for PCIe, USB 3.0 Host, and USB 3.0 Device mode */
2430ade8cd8SKonstantin Porotchkin static void mvebu_cp110_comphy_set_pipe_selector(uint64_t comphy_base,
2440ade8cd8SKonstantin Porotchkin 				     uint8_t comphy_index, uint32_t comphy_mode)
2450ade8cd8SKonstantin Porotchkin {
2460ade8cd8SKonstantin Porotchkin 	uint32_t reg;
2470ade8cd8SKonstantin Porotchkin 	uint32_t shift = COMMON_SELECTOR_COMPHYN_FIELD_WIDTH * comphy_index;
2480ade8cd8SKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
2490ade8cd8SKonstantin Porotchkin 	uint32_t mask = COMMON_SELECTOR_COMPHY_MASK << shift;
2500ade8cd8SKonstantin Porotchkin 	uint32_t pipe_sel = 0x0;
2510ade8cd8SKonstantin Porotchkin 
2520ade8cd8SKonstantin Porotchkin 	/* If pipe selector is used the phy selector should be marked as
2530ade8cd8SKonstantin Porotchkin 	 * unconnected.
2540ade8cd8SKonstantin Porotchkin 	 */
2550ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_clr_phy_selector(comphy_base, comphy_index);
2560ade8cd8SKonstantin Porotchkin 
2570ade8cd8SKonstantin Porotchkin 	reg = mmio_read_32(comphy_base + COMMON_SELECTOR_PIPE_REG_OFFSET);
2580ade8cd8SKonstantin Porotchkin 	reg &= ~mask;
2590ade8cd8SKonstantin Porotchkin 
2600ade8cd8SKonstantin Porotchkin 	switch (mode) {
2610ade8cd8SKonstantin Porotchkin 	case (COMPHY_PCIE_MODE):
2620ade8cd8SKonstantin Porotchkin 		/* For lanes support PCIE, selector value are all same */
2630ade8cd8SKonstantin Porotchkin 		pipe_sel = COMMON_SELECTOR_PIPE_COMPHY_PCIE;
2640ade8cd8SKonstantin Porotchkin 		break;
2650ade8cd8SKonstantin Porotchkin 
2660ade8cd8SKonstantin Porotchkin 	case (COMPHY_USB3H_MODE):
2670ade8cd8SKonstantin Porotchkin 		/* Only lane 1-4 support USB host, selector value is same */
2680ade8cd8SKonstantin Porotchkin 		if (comphy_index == COMPHY_LANE0 ||
2690ade8cd8SKonstantin Porotchkin 		    comphy_index == COMPHY_LANE5)
2700ade8cd8SKonstantin Porotchkin 			ERROR("COMPHY[%d] mode[%d] is invalid\n",
2710ade8cd8SKonstantin Porotchkin 			      comphy_index, mode);
2720ade8cd8SKonstantin Porotchkin 		else
2730ade8cd8SKonstantin Porotchkin 			pipe_sel = COMMON_SELECTOR_PIPE_COMPHY_USBH;
2740ade8cd8SKonstantin Porotchkin 		break;
2750ade8cd8SKonstantin Porotchkin 
2760ade8cd8SKonstantin Porotchkin 	case (COMPHY_USB3D_MODE):
2770ade8cd8SKonstantin Porotchkin 		/* Lane 1 and 4 support USB device, selector value is same */
2780ade8cd8SKonstantin Porotchkin 		if (comphy_index == COMPHY_LANE1 ||
2790ade8cd8SKonstantin Porotchkin 		    comphy_index == COMPHY_LANE4)
2800ade8cd8SKonstantin Porotchkin 			pipe_sel = COMMON_SELECTOR_PIPE_COMPHY_USBD;
2810ade8cd8SKonstantin Porotchkin 		else
2820ade8cd8SKonstantin Porotchkin 			ERROR("COMPHY[%d] mode[%d] is invalid\n", comphy_index,
2830ade8cd8SKonstantin Porotchkin 			      mode);
2840ade8cd8SKonstantin Porotchkin 		break;
2850ade8cd8SKonstantin Porotchkin 
2860ade8cd8SKonstantin Porotchkin 	default:
2870ade8cd8SKonstantin Porotchkin 		ERROR("COMPHY[%d] mode[%d] is invalid\n", comphy_index, mode);
2880ade8cd8SKonstantin Porotchkin 		break;
2890ade8cd8SKonstantin Porotchkin 	}
2900ade8cd8SKonstantin Porotchkin 
2910ade8cd8SKonstantin Porotchkin 	mmio_write_32(comphy_base + COMMON_SELECTOR_PIPE_REG_OFFSET, reg |
2920ade8cd8SKonstantin Porotchkin 		      (pipe_sel << shift));
2930ade8cd8SKonstantin Porotchkin }
2940ade8cd8SKonstantin Porotchkin 
2950ade8cd8SKonstantin Porotchkin int mvebu_cp110_comphy_is_pll_locked(uint64_t comphy_base, uint8_t comphy_index)
2960ade8cd8SKonstantin Porotchkin {
2970ade8cd8SKonstantin Porotchkin 	uintptr_t sd_ip_addr, addr;
2980ade8cd8SKonstantin Porotchkin 	uint32_t mask, data;
2990ade8cd8SKonstantin Porotchkin 	int ret = 0;
3000ade8cd8SKonstantin Porotchkin 
3010ade8cd8SKonstantin Porotchkin 	debug_enter();
3020ade8cd8SKonstantin Porotchkin 
3030ade8cd8SKonstantin Porotchkin 	sd_ip_addr = SD_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
3040ade8cd8SKonstantin Porotchkin 			     comphy_index);
3050ade8cd8SKonstantin Porotchkin 
3060ade8cd8SKonstantin Porotchkin 	addr = sd_ip_addr + SD_EXTERNAL_STATUS0_REG;
3070ade8cd8SKonstantin Porotchkin 	data = SD_EXTERNAL_STATUS0_PLL_TX_MASK &
3080ade8cd8SKonstantin Porotchkin 		SD_EXTERNAL_STATUS0_PLL_RX_MASK;
3090ade8cd8SKonstantin Porotchkin 	mask = data;
3100ade8cd8SKonstantin Porotchkin 	data = polling_with_timeout(addr, data, mask,
3110ade8cd8SKonstantin Porotchkin 				    PLL_LOCK_TIMEOUT, REG_32BIT);
3120ade8cd8SKonstantin Porotchkin 	if (data != 0) {
3130ade8cd8SKonstantin Porotchkin 		if (data & SD_EXTERNAL_STATUS0_PLL_RX_MASK)
3140ade8cd8SKonstantin Porotchkin 			ERROR("RX PLL is not locked\n");
3150ade8cd8SKonstantin Porotchkin 		if (data & SD_EXTERNAL_STATUS0_PLL_TX_MASK)
3160ade8cd8SKonstantin Porotchkin 			ERROR("TX PLL is not locked\n");
3170ade8cd8SKonstantin Porotchkin 
3180ade8cd8SKonstantin Porotchkin 		ret = -ETIMEDOUT;
3190ade8cd8SKonstantin Porotchkin 	}
3200ade8cd8SKonstantin Porotchkin 
3210ade8cd8SKonstantin Porotchkin 	debug_exit();
3220ade8cd8SKonstantin Porotchkin 
3230ade8cd8SKonstantin Porotchkin 	return ret;
3240ade8cd8SKonstantin Porotchkin }
3250ade8cd8SKonstantin Porotchkin 
3260ade8cd8SKonstantin Porotchkin static int mvebu_cp110_comphy_sata_power_on(uint64_t comphy_base,
3270ade8cd8SKonstantin Porotchkin 				     uint8_t comphy_index, uint32_t comphy_mode)
3280ade8cd8SKonstantin Porotchkin {
3290ade8cd8SKonstantin Porotchkin 	uintptr_t hpipe_addr, sd_ip_addr, comphy_addr;
3300ade8cd8SKonstantin Porotchkin 	uint32_t mask, data;
33142a29337SGrzegorz Jaszczyk 	uint8_t ap_nr, cp_nr;
3320ade8cd8SKonstantin Porotchkin 	int ret = 0;
3330ade8cd8SKonstantin Porotchkin 
3340ade8cd8SKonstantin Porotchkin 	debug_enter();
3350ade8cd8SKonstantin Porotchkin 
33642a29337SGrzegorz Jaszczyk 	mvebu_cp110_get_ap_and_cp_nr(&ap_nr, &cp_nr, comphy_base);
33742a29337SGrzegorz Jaszczyk 
33842a29337SGrzegorz Jaszczyk 	const struct sata_params *sata_static_values =
33942a29337SGrzegorz Jaszczyk 			&sata_static_values_tab[ap_nr][cp_nr][comphy_index];
34042a29337SGrzegorz Jaszczyk 
34142a29337SGrzegorz Jaszczyk 
3420ade8cd8SKonstantin Porotchkin 	/* configure phy selector for SATA */
3430ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_set_phy_selector(comphy_base,
3440ade8cd8SKonstantin Porotchkin 					    comphy_index, comphy_mode);
3450ade8cd8SKonstantin Porotchkin 
3460ade8cd8SKonstantin Porotchkin 	hpipe_addr = HPIPE_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
3470ade8cd8SKonstantin Porotchkin 				comphy_index);
3480ade8cd8SKonstantin Porotchkin 	sd_ip_addr = SD_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
3490ade8cd8SKonstantin Porotchkin 			     comphy_index);
3500ade8cd8SKonstantin Porotchkin 	comphy_addr = COMPHY_ADDR(comphy_base, comphy_index);
3510ade8cd8SKonstantin Porotchkin 
3520ade8cd8SKonstantin Porotchkin 	debug(" add hpipe 0x%lx, sd 0x%lx, comphy 0x%lx\n",
3530ade8cd8SKonstantin Porotchkin 					   hpipe_addr, sd_ip_addr, comphy_addr);
3540ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations - hard reset comphy\n");
3550ade8cd8SKonstantin Porotchkin 	/* RFU configurations - hard reset comphy */
3560ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_UP_MASK;
3570ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_UP_OFFSET;
3580ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PIPE_SELECT_MASK;
3590ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_PIPE_SELECT_OFFSET;
3600ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PWR_ON_RESET_MASK;
3610ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_PWR_ON_RESET_OFFSET;
3620ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_CORE_RSTN_MASK;
3630ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_CORE_RSTN_OFFSET;
3640ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
3650ade8cd8SKonstantin Porotchkin 
3660ade8cd8SKonstantin Porotchkin 	/* Set select data  width 40Bit - SATA mode only */
3670ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG6_REG,
3680ade8cd8SKonstantin Porotchkin 		0x1 << COMMON_PHY_CFG6_IF_40_SEL_OFFSET,
3690ade8cd8SKonstantin Porotchkin 		COMMON_PHY_CFG6_IF_40_SEL_MASK);
3700ade8cd8SKonstantin Porotchkin 
3710ade8cd8SKonstantin Porotchkin 	/* release from hard reset in SD external */
3720ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RESET_IN_MASK;
3730ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG1_RESET_IN_OFFSET;
3740ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RESET_CORE_MASK;
3750ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG1_RESET_CORE_OFFSET;
3760ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
3770ade8cd8SKonstantin Porotchkin 
3780ade8cd8SKonstantin Porotchkin 	/* Wait 1ms - until band gap and ref clock ready */
3790ade8cd8SKonstantin Porotchkin 	mdelay(1);
3800ade8cd8SKonstantin Porotchkin 
3810ade8cd8SKonstantin Porotchkin 	debug("stage: Comphy configuration\n");
3820ade8cd8SKonstantin Porotchkin 	/* Start comphy Configuration */
3830ade8cd8SKonstantin Porotchkin 	/* Set reference clock to comes from group 1 - choose 25Mhz */
3840ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_MISC_REG,
3850ade8cd8SKonstantin Porotchkin 		0x0 << HPIPE_MISC_REFCLK_SEL_OFFSET,
3860ade8cd8SKonstantin Porotchkin 		HPIPE_MISC_REFCLK_SEL_MASK);
3870ade8cd8SKonstantin Porotchkin 	/* Reference frequency select set 1 (for SATA = 25Mhz) */
3880ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_PLL_REF_FREQ_MASK;
3890ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_PWR_PLL_REF_FREQ_OFFSET;
3900ade8cd8SKonstantin Porotchkin 	/* PHY mode select (set SATA = 0x0 */
3910ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_PLL_PHY_MODE_MASK;
3920ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_PWR_PLL_PHY_MODE_OFFSET;
3930ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_PLL_REG, data, mask);
3940ade8cd8SKonstantin Porotchkin 	/* Set max PHY generation setting - 6Gbps */
3950ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_INTERFACE_REG,
3960ade8cd8SKonstantin Porotchkin 		0x2 << HPIPE_INTERFACE_GEN_MAX_OFFSET,
3970ade8cd8SKonstantin Porotchkin 		HPIPE_INTERFACE_GEN_MAX_MASK);
3980ade8cd8SKonstantin Porotchkin 	/* Set select data  width 40Bit (SEL_BITS[2:0]) */
3990ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LOOPBACK_REG,
4000ade8cd8SKonstantin Porotchkin 		0x2 << HPIPE_LOOPBACK_SEL_OFFSET, HPIPE_LOOPBACK_SEL_MASK);
4010ade8cd8SKonstantin Porotchkin 
4020ade8cd8SKonstantin Porotchkin 	debug("stage: Analog parameters from ETP(HW)\n");
4030ade8cd8SKonstantin Porotchkin 	/* G1 settings */
4040ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SET_1_G1_RX_SELMUPI_MASK;
40542a29337SGrzegorz Jaszczyk 	data = sata_static_values->g1_rx_selmupi <<
40642a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_1_G1_RX_SELMUPI_OFFSET;
4073c0024ccSGrzegorz Jaszczyk 	mask |= HPIPE_G1_SET_1_G1_RX_SELMUPF_MASK;
40842a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g1_rx_selmupf <<
40942a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_1_G1_RX_SELMUPF_OFFSET;
4100ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SET_1_G1_RX_SELMUFI_MASK;
41142a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g1_rx_selmufi <<
41242a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_1_G1_RX_SELMUFI_OFFSET;
4130ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SET_1_G1_RX_SELMUFF_MASK;
41442a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g1_rx_selmuff <<
41542a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_1_G1_RX_SELMUFF_OFFSET;
4160ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SET_1_G1_RX_DIGCK_DIV_MASK;
4170ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G1_SET_1_G1_RX_DIGCK_DIV_OFFSET;
4180ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SET_1_REG, data, mask);
4190ade8cd8SKonstantin Porotchkin 
4200ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SETTINGS_3_G1_FFE_CAP_SEL_MASK;
4210ade8cd8SKonstantin Porotchkin 	data = 0xf << HPIPE_G1_SETTINGS_3_G1_FFE_CAP_SEL_OFFSET;
4220ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SETTINGS_3_G1_FFE_RES_SEL_MASK;
4230ade8cd8SKonstantin Porotchkin 	data |= 0x2 << HPIPE_G1_SETTINGS_3_G1_FFE_RES_SEL_OFFSET;
4240ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_MASK;
4250ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_OFFSET;
4260ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SETTINGS_3_G1_FFE_DEG_RES_LEVEL_MASK;
4270ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G1_SETTINGS_3_G1_FFE_DEG_RES_LEVEL_OFFSET;
4280ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SETTINGS_3_G1_FFE_LOAD_RES_LEVEL_MASK;
4290ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G1_SETTINGS_3_G1_FFE_LOAD_RES_LEVEL_OFFSET;
4300ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SETTINGS_3_REG, data, mask);
4310ade8cd8SKonstantin Porotchkin 
4320ade8cd8SKonstantin Porotchkin 	/* G2 settings */
4330ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G2_SET_1_G2_RX_SELMUPI_MASK;
43442a29337SGrzegorz Jaszczyk 	data = sata_static_values->g2_rx_selmupi <<
43542a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_1_G2_RX_SELMUPI_OFFSET;
4363c0024ccSGrzegorz Jaszczyk 	mask |= HPIPE_G2_SET_1_G2_RX_SELMUPF_MASK;
43742a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g2_rx_selmupf <<
43842a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_1_G2_RX_SELMUPF_OFFSET;
4390ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G2_SET_1_G2_RX_SELMUFI_MASK;
44042a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g2_rx_selmufi <<
44142a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_1_G2_RX_SELMUFI_OFFSET;
4420ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G2_SET_1_G2_RX_SELMUFF_MASK;
44342a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g2_rx_selmuff <<
44442a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_1_G2_RX_SELMUFF_OFFSET;
4450ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G2_SET_1_G2_RX_DIGCK_DIV_MASK;
4460ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G2_SET_1_G2_RX_DIGCK_DIV_OFFSET;
4470ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G2_SET_1_REG, data, mask);
4480ade8cd8SKonstantin Porotchkin 
4490ade8cd8SKonstantin Porotchkin 	/* G3 settings */
4500ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G3_SET_1_G3_RX_SELMUPI_MASK;
45142a29337SGrzegorz Jaszczyk 	data = sata_static_values->g3_rx_selmupi <<
45242a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_1_G3_RX_SELMUPI_OFFSET;
4530ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_1_G3_RX_SELMUPF_MASK;
45442a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g3_rx_selmupf <<
45542a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_1_G3_RX_SELMUPF_OFFSET;
4560ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_1_G3_RX_SELMUFI_MASK;
45742a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g3_rx_selmufi <<
45842a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_1_G3_RX_SELMUFI_OFFSET;
4590ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_1_G3_RX_SELMUFF_MASK;
46042a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g3_rx_selmuff <<
46142a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_1_G3_RX_SELMUFF_OFFSET;
4620ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_1_G3_RX_DFE_EN_MASK;
4630ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G3_SET_1_G3_RX_DFE_EN_OFFSET;
4640ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_1_G3_RX_DIGCK_DIV_MASK;
4650ade8cd8SKonstantin Porotchkin 	data |= 0x2 << HPIPE_G3_SET_1_G3_RX_DIGCK_DIV_OFFSET;
4660ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_1_G3_SAMPLER_INPAIRX2_EN_MASK;
4670ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_G3_SET_1_G3_SAMPLER_INPAIRX2_EN_OFFSET;
4680ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G3_SET_1_REG, data, mask);
4690ade8cd8SKonstantin Porotchkin 
4700ade8cd8SKonstantin Porotchkin 	/* DTL Control */
4710ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_CTR_DTL_SQ_DET_EN_MASK;
4720ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_PWR_CTR_DTL_SQ_DET_EN_OFFSET;
4730ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_CTR_DTL_SQ_PLOOP_EN_MASK;
4740ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_PWR_CTR_DTL_SQ_PLOOP_EN_OFFSET;
4750ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_CTR_DTL_FLOOP_EN_MASK;
4760ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_PWR_CTR_DTL_FLOOP_EN_OFFSET;
4770ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_CTR_DTL_CLAMPING_SEL_MASK;
4780ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_PWR_CTR_DTL_CLAMPING_SEL_OFFSET;
4790ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_CTR_DTL_INTPCLK_DIV_FORCE_MASK;
4800ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_PWR_CTR_DTL_INTPCLK_DIV_FORCE_OFFSET;
4810ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_CTR_DTL_CLK_MODE_MASK;
4820ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_PWR_CTR_DTL_CLK_MODE_OFFSET;
4830ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_CTR_DTL_CLK_MODE_FORCE_MASK;
4840ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_PWR_CTR_DTL_CLK_MODE_FORCE_OFFSET;
4850ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_DTL_REG, data, mask);
4860ade8cd8SKonstantin Porotchkin 
4870ade8cd8SKonstantin Porotchkin 	/* Trigger sampler enable pulse */
4880ade8cd8SKonstantin Porotchkin 	mask = HPIPE_SMAPLER_MASK;
4890ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_SMAPLER_OFFSET;
4900ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_SAMPLER_N_PROC_CALIB_CTRL_REG, data, mask);
4910ade8cd8SKonstantin Porotchkin 	mask = HPIPE_SMAPLER_MASK;
4920ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_SMAPLER_OFFSET;
4930ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_SAMPLER_N_PROC_CALIB_CTRL_REG, data, mask);
4940ade8cd8SKonstantin Porotchkin 
4950ade8cd8SKonstantin Porotchkin 	/* VDD Calibration Control 3 */
4960ade8cd8SKonstantin Porotchkin 	mask = HPIPE_EXT_SELLV_RXSAMPL_MASK;
4970ade8cd8SKonstantin Porotchkin 	data = 0x10 << HPIPE_EXT_SELLV_RXSAMPL_OFFSET;
4980ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_VDD_CAL_CTRL_REG, data, mask);
4990ade8cd8SKonstantin Porotchkin 
5000ade8cd8SKonstantin Porotchkin 	/* DFE Resolution Control */
5010ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_RES_FORCE_MASK;
5020ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_DFE_RES_FORCE_OFFSET;
5030ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_REG0, data, mask);
5040ade8cd8SKonstantin Porotchkin 
5050ade8cd8SKonstantin Porotchkin 	/* DFE F3-F5 Coefficient Control */
5060ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_F3_F5_DFE_EN_MASK;
5070ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_DFE_F3_F5_DFE_EN_OFFSET;
5080ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_DFE_F3_F5_DFE_CTRL_MASK;
5090ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_DFE_F3_F5_DFE_CTRL_OFFSET;
5100ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_F3_F5_REG, data, mask);
5110ade8cd8SKonstantin Porotchkin 
5120ade8cd8SKonstantin Porotchkin 	/* G3 Setting 3 */
5130ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G3_FFE_CAP_SEL_MASK;
51442a29337SGrzegorz Jaszczyk 	data = sata_static_values->g3_ffe_cap_sel <<
51542a29337SGrzegorz Jaszczyk 			HPIPE_G3_FFE_CAP_SEL_OFFSET;
5160ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_FFE_RES_SEL_MASK;
51742a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g3_ffe_res_sel <<
51842a29337SGrzegorz Jaszczyk 			HPIPE_G3_FFE_RES_SEL_OFFSET;
5190ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_FFE_SETTING_FORCE_MASK;
5200ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G3_FFE_SETTING_FORCE_OFFSET;
5210ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_FFE_DEG_RES_LEVEL_MASK;
5220ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G3_FFE_DEG_RES_LEVEL_OFFSET;
5230ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_FFE_LOAD_RES_LEVEL_MASK;
5240ade8cd8SKonstantin Porotchkin 	data |= 0x3 << HPIPE_G3_FFE_LOAD_RES_LEVEL_OFFSET;
5250ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G3_SETTING_3_REG, data, mask);
5260ade8cd8SKonstantin Porotchkin 
5270ade8cd8SKonstantin Porotchkin 	/* G3 Setting 4 */
5280ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G3_DFE_RES_MASK;
52942a29337SGrzegorz Jaszczyk 	data = sata_static_values->g3_dfe_res << HPIPE_G3_DFE_RES_OFFSET;
5300ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G3_SETTING_4_REG, data, mask);
5310ade8cd8SKonstantin Porotchkin 
5320ade8cd8SKonstantin Porotchkin 	/* Offset Phase Control */
5330ade8cd8SKonstantin Porotchkin 	mask = HPIPE_OS_PH_OFFSET_MASK;
53442a29337SGrzegorz Jaszczyk 	data = sata_static_values->align90 << HPIPE_OS_PH_OFFSET_OFFSET;
5350ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_OS_PH_OFFSET_FORCE_MASK;
5360ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_OS_PH_OFFSET_FORCE_OFFSET;
5370ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_OS_PH_VALID_MASK;
5380ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_OS_PH_VALID_OFFSET;
5390ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PHASE_CONTROL_REG, data, mask);
5400ade8cd8SKonstantin Porotchkin 	mask = HPIPE_OS_PH_VALID_MASK;
5410ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_OS_PH_VALID_OFFSET;
5420ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PHASE_CONTROL_REG, data, mask);
5430ade8cd8SKonstantin Porotchkin 	mask = HPIPE_OS_PH_VALID_MASK;
5440ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_OS_PH_VALID_OFFSET;
5450ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PHASE_CONTROL_REG, data, mask);
5460ade8cd8SKonstantin Porotchkin 
5470ade8cd8SKonstantin Porotchkin 	/* Set G1 TX amplitude and TX post emphasis value */
5480ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SET_0_G1_TX_AMP_MASK;
54942a29337SGrzegorz Jaszczyk 	data = sata_static_values->g1_amp << HPIPE_G1_SET_0_G1_TX_AMP_OFFSET;
5500ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SET_0_G1_TX_AMP_ADJ_MASK;
55142a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g1_tx_amp_adj <<
55242a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_0_G1_TX_AMP_ADJ_OFFSET;
5530ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SET_0_G1_TX_EMPH1_MASK;
55442a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g1_emph <<
55542a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_0_G1_TX_EMPH1_OFFSET;
5560ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SET_0_G1_TX_EMPH1_EN_MASK;
55742a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g1_emph_en <<
55842a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_0_G1_TX_EMPH1_EN_OFFSET;
5590ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SET_0_REG, data, mask);
5600ade8cd8SKonstantin Porotchkin 
56142a29337SGrzegorz Jaszczyk 	/* Set G1 emph */
56242a29337SGrzegorz Jaszczyk 	mask = HPIPE_G1_SET_2_G1_TX_EMPH0_EN_MASK;
56342a29337SGrzegorz Jaszczyk 	data = sata_static_values->g1_tx_emph_en <<
56442a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_2_G1_TX_EMPH0_EN_OFFSET;
56542a29337SGrzegorz Jaszczyk 	mask |= HPIPE_G1_SET_2_G1_TX_EMPH0_MASK;
56642a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g1_tx_emph <<
56742a29337SGrzegorz Jaszczyk 			HPIPE_G1_SET_2_G1_TX_EMPH0_OFFSET;
56842a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_G1_SET_2_REG, data, mask);
56942a29337SGrzegorz Jaszczyk 
5700ade8cd8SKonstantin Porotchkin 	/* Set G2 TX amplitude and TX post emphasis value */
5710ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G2_SET_0_G2_TX_AMP_MASK;
57242a29337SGrzegorz Jaszczyk 	data = sata_static_values->g2_amp << HPIPE_G2_SET_0_G2_TX_AMP_OFFSET;
5730ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G2_SET_0_G2_TX_AMP_ADJ_MASK;
57442a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g2_tx_amp_adj <<
57542a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_0_G2_TX_AMP_ADJ_OFFSET;
5760ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G2_SET_0_G2_TX_EMPH1_MASK;
57742a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g2_emph <<
57842a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_0_G2_TX_EMPH1_OFFSET;
5790ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G2_SET_0_G2_TX_EMPH1_EN_MASK;
58042a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g2_emph_en <<
58142a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_0_G2_TX_EMPH1_EN_OFFSET;
5820ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G2_SET_0_REG, data, mask);
5830ade8cd8SKonstantin Porotchkin 
58442a29337SGrzegorz Jaszczyk 	/* Set G2 emph */
58542a29337SGrzegorz Jaszczyk 	mask = HPIPE_G2_SET_2_G2_TX_EMPH0_EN_MASK;
58642a29337SGrzegorz Jaszczyk 	data = sata_static_values->g2_tx_emph_en <<
58742a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_2_G2_TX_EMPH0_EN_OFFSET;
58842a29337SGrzegorz Jaszczyk 	mask |= HPIPE_G2_SET_2_G2_TX_EMPH0_MASK;
58942a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g2_tx_emph <<
59042a29337SGrzegorz Jaszczyk 			HPIPE_G2_SET_2_G2_TX_EMPH0_OFFSET;
59142a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_G2_SET_2_REG, data, mask);
59242a29337SGrzegorz Jaszczyk 
5930ade8cd8SKonstantin Porotchkin 	/* Set G3 TX amplitude and TX post emphasis value */
5940ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G3_SET_0_G3_TX_AMP_MASK;
59542a29337SGrzegorz Jaszczyk 	data = sata_static_values->g3_amp << HPIPE_G3_SET_0_G3_TX_AMP_OFFSET;
5960ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_0_G3_TX_AMP_ADJ_MASK;
59742a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g3_tx_amp_adj <<
59842a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_0_G3_TX_AMP_ADJ_OFFSET;
5990ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_0_G3_TX_EMPH1_MASK;
60042a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g3_emph <<
60142a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_0_G3_TX_EMPH1_OFFSET;
6020ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_0_G3_TX_EMPH1_EN_MASK;
60342a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g3_emph_en <<
60442a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_0_G3_TX_EMPH1_EN_OFFSET;
6050ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_0_G3_TX_SLEW_RATE_SEL_MASK;
6060ade8cd8SKonstantin Porotchkin 	data |= 0x4 << HPIPE_G3_SET_0_G3_TX_SLEW_RATE_SEL_OFFSET;
6070ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_0_G3_TX_SLEW_CTRL_EN_MASK;
6080ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_G3_SET_0_G3_TX_SLEW_CTRL_EN_OFFSET;
6090ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G3_SET_0_REG, data, mask);
6100ade8cd8SKonstantin Porotchkin 
61142a29337SGrzegorz Jaszczyk 	/* Set G3 emph */
61242a29337SGrzegorz Jaszczyk 	mask = HPIPE_G3_SET_2_G3_TX_EMPH0_EN_MASK;
61342a29337SGrzegorz Jaszczyk 	data = sata_static_values->g3_tx_emph_en <<
61442a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_2_G3_TX_EMPH0_EN_OFFSET;
61542a29337SGrzegorz Jaszczyk 	mask |= HPIPE_G3_SET_2_G3_TX_EMPH0_MASK;
61642a29337SGrzegorz Jaszczyk 	data |= sata_static_values->g3_tx_emph <<
61742a29337SGrzegorz Jaszczyk 			HPIPE_G3_SET_2_G3_TX_EMPH0_OFFSET;
61842a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_G3_SET_2_REG, data, mask);
61942a29337SGrzegorz Jaszczyk 
6200ade8cd8SKonstantin Porotchkin 	/* SERDES External Configuration 2 register */
6210ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG2_SSC_ENABLE_MASK;
6220ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG2_SSC_ENABLE_OFFSET;
6230ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG2_REG, data, mask);
6240ade8cd8SKonstantin Porotchkin 
6250ade8cd8SKonstantin Porotchkin 	/* DFE reset sequence */
6260ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_REG,
6270ade8cd8SKonstantin Porotchkin 		0x1 << HPIPE_PWR_CTR_RST_DFE_OFFSET,
6280ade8cd8SKonstantin Porotchkin 		HPIPE_PWR_CTR_RST_DFE_MASK);
6290ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_REG,
6300ade8cd8SKonstantin Porotchkin 		0x0 << HPIPE_PWR_CTR_RST_DFE_OFFSET,
6310ade8cd8SKonstantin Porotchkin 		HPIPE_PWR_CTR_RST_DFE_MASK);
6320ade8cd8SKonstantin Porotchkin 	/* SW reset for interrupt logic */
6330ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_REG,
6340ade8cd8SKonstantin Porotchkin 		0x1 << HPIPE_PWR_CTR_SFT_RST_OFFSET,
6350ade8cd8SKonstantin Porotchkin 		HPIPE_PWR_CTR_SFT_RST_MASK);
6360ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_REG,
6370ade8cd8SKonstantin Porotchkin 		0x0 << HPIPE_PWR_CTR_SFT_RST_OFFSET,
6380ade8cd8SKonstantin Porotchkin 		HPIPE_PWR_CTR_SFT_RST_MASK);
6390ade8cd8SKonstantin Porotchkin 
6400ade8cd8SKonstantin Porotchkin 	debug_exit();
6410ade8cd8SKonstantin Porotchkin 
6420ade8cd8SKonstantin Porotchkin 	return ret;
6430ade8cd8SKonstantin Porotchkin }
6440ade8cd8SKonstantin Porotchkin 
6450ade8cd8SKonstantin Porotchkin static int mvebu_cp110_comphy_sgmii_power_on(uint64_t comphy_base,
6460ade8cd8SKonstantin Porotchkin 				     uint8_t comphy_index, uint32_t comphy_mode)
6470ade8cd8SKonstantin Porotchkin {
6480ade8cd8SKonstantin Porotchkin 	uintptr_t hpipe_addr, sd_ip_addr, comphy_addr, addr;
6490ade8cd8SKonstantin Porotchkin 	uint32_t mask, data, sgmii_speed = COMPHY_GET_SPEED(comphy_mode);
6500ade8cd8SKonstantin Porotchkin 	int ret = 0;
6510ade8cd8SKonstantin Porotchkin 
6520ade8cd8SKonstantin Porotchkin 	debug_enter();
6530ade8cd8SKonstantin Porotchkin 
6540ade8cd8SKonstantin Porotchkin 	hpipe_addr = HPIPE_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
6550ade8cd8SKonstantin Porotchkin 				comphy_index);
6560ade8cd8SKonstantin Porotchkin 	sd_ip_addr = SD_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
6570ade8cd8SKonstantin Porotchkin 			     comphy_index);
6580ade8cd8SKonstantin Porotchkin 	comphy_addr = COMPHY_ADDR(comphy_base, comphy_index);
6590ade8cd8SKonstantin Porotchkin 
6600ade8cd8SKonstantin Porotchkin 	/* configure phy selector for SGMII */
6610ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_set_phy_selector(comphy_base, comphy_index,
6620ade8cd8SKonstantin Porotchkin 					    comphy_mode);
6630ade8cd8SKonstantin Porotchkin 
6640ade8cd8SKonstantin Porotchkin 	/* Confiugre the lane */
6650ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations - hard reset comphy\n");
6660ade8cd8SKonstantin Porotchkin 	/* RFU configurations - hard reset comphy */
6670ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_UP_MASK;
6680ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_UP_OFFSET;
6690ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PIPE_SELECT_MASK;
6700ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_PIPE_SELECT_OFFSET;
6710ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
6720ade8cd8SKonstantin Porotchkin 
6730ade8cd8SKonstantin Porotchkin 	/* Select Baud Rate of Comphy And PD_PLL/Tx/Rx */
6740ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG0_SD_PU_PLL_MASK;
6750ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG0_SD_PU_PLL_OFFSET;
6760ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PHY_GEN_RX_MASK;
6770ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PHY_GEN_TX_MASK;
6780ade8cd8SKonstantin Porotchkin 
6790ade8cd8SKonstantin Porotchkin 	if (sgmii_speed == COMPHY_SPEED_1_25G) {
6800ade8cd8SKonstantin Porotchkin 		/* SGMII 1G, SerDes speed 1.25G */
6810ade8cd8SKonstantin Porotchkin 		data |= 0x6 << SD_EXTERNAL_CONFIG0_SD_PHY_GEN_RX_OFFSET;
6820ade8cd8SKonstantin Porotchkin 		data |= 0x6 << SD_EXTERNAL_CONFIG0_SD_PHY_GEN_TX_OFFSET;
6830ade8cd8SKonstantin Porotchkin 	} else if (sgmii_speed == COMPHY_SPEED_3_125G) {
6840ade8cd8SKonstantin Porotchkin 		/* HS SGMII (2.5G), SerDes speed 3.125G */
6850ade8cd8SKonstantin Porotchkin 		data |= 0x8 << SD_EXTERNAL_CONFIG0_SD_PHY_GEN_RX_OFFSET;
6860ade8cd8SKonstantin Porotchkin 		data |= 0x8 << SD_EXTERNAL_CONFIG0_SD_PHY_GEN_TX_OFFSET;
6870ade8cd8SKonstantin Porotchkin 	} else {
6880ade8cd8SKonstantin Porotchkin 		/* Other rates are not supported */
6890ade8cd8SKonstantin Porotchkin 		ERROR("unsupported SGMII speed on comphy%d\n", comphy_index);
6900ade8cd8SKonstantin Porotchkin 		return -EINVAL;
6910ade8cd8SKonstantin Porotchkin 	}
6920ade8cd8SKonstantin Porotchkin 
6930ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_RX_MASK;
6940ade8cd8SKonstantin Porotchkin 	data |= 0 << SD_EXTERNAL_CONFIG0_SD_PU_RX_OFFSET;
6950ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_TX_MASK;
6960ade8cd8SKonstantin Porotchkin 	data |= 0 << SD_EXTERNAL_CONFIG0_SD_PU_TX_OFFSET;
6970ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_HALF_BUS_MODE_MASK;
6980ade8cd8SKonstantin Porotchkin 	data |= 1 << SD_EXTERNAL_CONFIG0_HALF_BUS_MODE_OFFSET;
6990ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG0_REG, data, mask);
7000ade8cd8SKonstantin Porotchkin 
7010ade8cd8SKonstantin Porotchkin 	/* Set hard reset */
7020ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RESET_IN_MASK;
7030ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG1_RESET_IN_OFFSET;
7040ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RESET_CORE_MASK;
7050ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG1_RESET_CORE_OFFSET;
7060ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RF_RESET_IN_MASK;
7070ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG1_RF_RESET_IN_OFFSET;
7080ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
7090ade8cd8SKonstantin Porotchkin 
7100ade8cd8SKonstantin Porotchkin 	/* Release hard reset */
7110ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RESET_IN_MASK;
7120ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG1_RESET_IN_OFFSET;
7130ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RESET_CORE_MASK;
7140ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG1_RESET_CORE_OFFSET;
7150ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
7160ade8cd8SKonstantin Porotchkin 
7170ade8cd8SKonstantin Porotchkin 	/* Wait 1ms - until band gap and ref clock ready */
7180ade8cd8SKonstantin Porotchkin 	mdelay(1);
7190ade8cd8SKonstantin Porotchkin 
7200ade8cd8SKonstantin Porotchkin 	/* Make sure that 40 data bits is disabled
7210ade8cd8SKonstantin Porotchkin 	 * This bit is not cleared by reset
7220ade8cd8SKonstantin Porotchkin 	 */
7230ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG6_IF_40_SEL_MASK;
7240ade8cd8SKonstantin Porotchkin 	data = 0 << COMMON_PHY_CFG6_IF_40_SEL_OFFSET;
7250ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG6_REG, data, mask);
7260ade8cd8SKonstantin Porotchkin 
7270ade8cd8SKonstantin Porotchkin 	/* Start comphy Configuration */
7280ade8cd8SKonstantin Porotchkin 	debug("stage: Comphy configuration\n");
7290ade8cd8SKonstantin Porotchkin 	/* set reference clock */
7300ade8cd8SKonstantin Porotchkin 	mask = HPIPE_MISC_REFCLK_SEL_MASK;
7310ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_MISC_REFCLK_SEL_OFFSET;
7320ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_MISC_REG, data, mask);
7330ade8cd8SKonstantin Porotchkin 	/* Power and PLL Control */
7340ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_PLL_REF_FREQ_MASK;
7350ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_PWR_PLL_REF_FREQ_OFFSET;
7360ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_PLL_PHY_MODE_MASK;
7370ade8cd8SKonstantin Porotchkin 	data |= 0x4 << HPIPE_PWR_PLL_PHY_MODE_OFFSET;
7380ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_PLL_REG, data, mask);
7390ade8cd8SKonstantin Porotchkin 	/* Loopback register */
7400ade8cd8SKonstantin Porotchkin 	mask = HPIPE_LOOPBACK_SEL_MASK;
7410ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_LOOPBACK_SEL_OFFSET;
7420ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LOOPBACK_REG, data, mask);
7430ade8cd8SKonstantin Porotchkin 	/* rx control 1 */
7440ade8cd8SKonstantin Porotchkin 	mask = HPIPE_RX_CONTROL_1_RXCLK2X_SEL_MASK;
7450ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_RX_CONTROL_1_RXCLK2X_SEL_OFFSET;
7460ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RX_CONTROL_1_CLK8T_EN_MASK;
7470ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_RX_CONTROL_1_CLK8T_EN_OFFSET;
7480ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_RX_CONTROL_1_REG, data, mask);
7490ade8cd8SKonstantin Porotchkin 	/* DTL Control */
7500ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_CTR_DTL_FLOOP_EN_MASK;
7510ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_PWR_CTR_DTL_FLOOP_EN_OFFSET;
7520ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_DTL_REG, data, mask);
7530ade8cd8SKonstantin Porotchkin 
75442a29337SGrzegorz Jaszczyk 	/* Set analog parameters from ETP(HW) - for now use the default data */
7550ade8cd8SKonstantin Porotchkin 	debug("stage: Analog parameters from ETP(HW)\n");
7560ade8cd8SKonstantin Porotchkin 
7570ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SET_0_REG,
7580ade8cd8SKonstantin Porotchkin 		0x1 << HPIPE_G1_SET_0_G1_TX_EMPH1_OFFSET,
7590ade8cd8SKonstantin Porotchkin 		HPIPE_G1_SET_0_G1_TX_EMPH1_MASK);
7600ade8cd8SKonstantin Porotchkin 
7610ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations- Power Up PLL,Tx,Rx\n");
7620ade8cd8SKonstantin Porotchkin 	/* SERDES External Configuration */
7630ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG0_SD_PU_PLL_MASK;
7640ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_PLL_OFFSET;
7650ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_RX_MASK;
7660ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_RX_OFFSET;
7670ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_TX_MASK;
7680ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_TX_OFFSET;
7690ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG0_REG, data, mask);
7700ade8cd8SKonstantin Porotchkin 
7710ade8cd8SKonstantin Porotchkin 	ret = mvebu_cp110_comphy_is_pll_locked(comphy_base, comphy_index);
7720ade8cd8SKonstantin Porotchkin 	if (ret)
7730ade8cd8SKonstantin Porotchkin 		return ret;
7740ade8cd8SKonstantin Porotchkin 
7750ade8cd8SKonstantin Porotchkin 	/* RX init */
7760ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RX_INIT_MASK;
7770ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG1_RX_INIT_OFFSET;
7780ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
7790ade8cd8SKonstantin Porotchkin 
7800ade8cd8SKonstantin Porotchkin 	/* check that RX init done */
7810ade8cd8SKonstantin Porotchkin 	addr = sd_ip_addr + SD_EXTERNAL_STATUS0_REG;
7820ade8cd8SKonstantin Porotchkin 	data = SD_EXTERNAL_STATUS0_RX_INIT_MASK;
7830ade8cd8SKonstantin Porotchkin 	mask = data;
7840ade8cd8SKonstantin Porotchkin 	data = polling_with_timeout(addr, data, mask, 100, REG_32BIT);
7850ade8cd8SKonstantin Porotchkin 	if (data != 0) {
7860ade8cd8SKonstantin Porotchkin 		ERROR("RX init failed\n");
7870ade8cd8SKonstantin Porotchkin 		ret = -ETIMEDOUT;
7880ade8cd8SKonstantin Porotchkin 	}
7890ade8cd8SKonstantin Porotchkin 
7900ade8cd8SKonstantin Porotchkin 	debug("stage: RF Reset\n");
7910ade8cd8SKonstantin Porotchkin 	/* RF Reset */
7920ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RX_INIT_MASK;
7930ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG1_RX_INIT_OFFSET;
7940ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RF_RESET_IN_MASK;
7950ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG1_RF_RESET_IN_OFFSET;
7960ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
7970ade8cd8SKonstantin Porotchkin 
7980ade8cd8SKonstantin Porotchkin 	debug_exit();
7990ade8cd8SKonstantin Porotchkin 
8000ade8cd8SKonstantin Porotchkin 	return ret;
8010ade8cd8SKonstantin Porotchkin }
8020ade8cd8SKonstantin Porotchkin 
8030ade8cd8SKonstantin Porotchkin static int mvebu_cp110_comphy_xfi_power_on(uint64_t comphy_base,
8040ade8cd8SKonstantin Porotchkin 					   uint8_t comphy_index,
8050ade8cd8SKonstantin Porotchkin 					   uint32_t comphy_mode)
8060ade8cd8SKonstantin Porotchkin {
8070ade8cd8SKonstantin Porotchkin 	uintptr_t hpipe_addr, sd_ip_addr, comphy_addr, addr;
8080ade8cd8SKonstantin Porotchkin 	uint32_t mask, data, speed = COMPHY_GET_SPEED(comphy_mode);
8090ade8cd8SKonstantin Porotchkin 	int ret = 0;
81042a29337SGrzegorz Jaszczyk 	uint8_t ap_nr, cp_nr;
8110ade8cd8SKonstantin Porotchkin 
8120ade8cd8SKonstantin Porotchkin 	debug_enter();
8130ade8cd8SKonstantin Porotchkin 
81442a29337SGrzegorz Jaszczyk 	mvebu_cp110_get_ap_and_cp_nr(&ap_nr, &cp_nr, comphy_base);
81542a29337SGrzegorz Jaszczyk 
81642a29337SGrzegorz Jaszczyk 	if (rx_trainng_done[ap_nr][cp_nr][comphy_index]) {
81742a29337SGrzegorz Jaszczyk 		debug("Skip %s for comphy[%d][%d][%d], due to rx training\n",
81842a29337SGrzegorz Jaszczyk 		       __func__, ap_nr, cp_nr, comphy_index);
81942a29337SGrzegorz Jaszczyk 		return 0;
82042a29337SGrzegorz Jaszczyk 	}
82142a29337SGrzegorz Jaszczyk 
82242a29337SGrzegorz Jaszczyk 	const struct xfi_params *xfi_static_values =
82342a29337SGrzegorz Jaszczyk 			     &xfi_static_values_tab[ap_nr][cp_nr][comphy_index];
82442a29337SGrzegorz Jaszczyk 
82542a29337SGrzegorz Jaszczyk 	debug("%s: the ap_nr = %d, cp_nr = %d, comphy_index %d\n",
82642a29337SGrzegorz Jaszczyk 	      __func__, ap_nr, cp_nr, comphy_index);
82742a29337SGrzegorz Jaszczyk 
82842a29337SGrzegorz Jaszczyk 	debug("g1_ffe_cap_sel= 0x%x, g1_ffe_res_sel= 0x%x, g1_dfe_res= 0x%x\n",
82942a29337SGrzegorz Jaszczyk 	      xfi_static_values->g1_ffe_cap_sel,
83042a29337SGrzegorz Jaszczyk 	      xfi_static_values->g1_ffe_res_sel,
83142a29337SGrzegorz Jaszczyk 	      xfi_static_values->g1_dfe_res);
83242a29337SGrzegorz Jaszczyk 
83342a29337SGrzegorz Jaszczyk 	if (!xfi_static_values->valid) {
83442a29337SGrzegorz Jaszczyk 		ERROR("[ap%d][cp[%d][comphy:%d]: Has no valid static params\n",
83542a29337SGrzegorz Jaszczyk 		      ap_nr, cp_nr, comphy_index);
83642a29337SGrzegorz Jaszczyk 		ERROR("[ap%d][cp[%d][comphy:%d]: porting layer needs update\n",
83742a29337SGrzegorz Jaszczyk 		      ap_nr, cp_nr, comphy_index);
83842a29337SGrzegorz Jaszczyk 		return -EINVAL;
83942a29337SGrzegorz Jaszczyk 	}
84042a29337SGrzegorz Jaszczyk 
8410ade8cd8SKonstantin Porotchkin 	if ((speed != COMPHY_SPEED_5_15625G) &&
8420ade8cd8SKonstantin Porotchkin 	     (speed != COMPHY_SPEED_10_3125G) &&
8430ade8cd8SKonstantin Porotchkin 	     (speed != COMPHY_SPEED_DEFAULT)) {
8440ade8cd8SKonstantin Porotchkin 		ERROR("comphy:%d: unsupported sfi/xfi speed\n", comphy_index);
8450ade8cd8SKonstantin Porotchkin 		return -EINVAL;
8460ade8cd8SKonstantin Porotchkin 	}
8470ade8cd8SKonstantin Porotchkin 
8480ade8cd8SKonstantin Porotchkin 	hpipe_addr = HPIPE_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
8490ade8cd8SKonstantin Porotchkin 				comphy_index);
8500ade8cd8SKonstantin Porotchkin 	sd_ip_addr = SD_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
8510ade8cd8SKonstantin Porotchkin 			     comphy_index);
8520ade8cd8SKonstantin Porotchkin 	comphy_addr = COMPHY_ADDR(comphy_base, comphy_index);
8530ade8cd8SKonstantin Porotchkin 
8540ade8cd8SKonstantin Porotchkin 	/* configure phy selector for XFI/SFI */
8550ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_set_phy_selector(comphy_base, comphy_index,
8560ade8cd8SKonstantin Porotchkin 					    comphy_mode);
8570ade8cd8SKonstantin Porotchkin 
8580ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations - hard reset comphy\n");
8590ade8cd8SKonstantin Porotchkin 	/* RFU configurations - hard reset comphy */
8600ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_UP_MASK;
8610ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_UP_OFFSET;
8620ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PIPE_SELECT_MASK;
8630ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_PIPE_SELECT_OFFSET;
8640ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
8650ade8cd8SKonstantin Porotchkin 
8660ade8cd8SKonstantin Porotchkin 	/* Make sure that 40 data bits is disabled
8670ade8cd8SKonstantin Porotchkin 	 * This bit is not cleared by reset
8680ade8cd8SKonstantin Porotchkin 	 */
8690ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG6_IF_40_SEL_MASK;
8700ade8cd8SKonstantin Porotchkin 	data = 0 << COMMON_PHY_CFG6_IF_40_SEL_OFFSET;
8710ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG6_REG, data, mask);
8720ade8cd8SKonstantin Porotchkin 
8730ade8cd8SKonstantin Porotchkin 	/* Select Baud Rate of Comphy And PD_PLL/Tx/Rx */
8740ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG0_SD_PU_PLL_MASK;
8750ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG0_SD_PU_PLL_OFFSET;
8760ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PHY_GEN_RX_MASK;
8770ade8cd8SKonstantin Porotchkin 	data |= 0xE << SD_EXTERNAL_CONFIG0_SD_PHY_GEN_RX_OFFSET;
8780ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PHY_GEN_TX_MASK;
8790ade8cd8SKonstantin Porotchkin 	data |= 0xE << SD_EXTERNAL_CONFIG0_SD_PHY_GEN_TX_OFFSET;
8800ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_RX_MASK;
8810ade8cd8SKonstantin Porotchkin 	data |= 0 << SD_EXTERNAL_CONFIG0_SD_PU_RX_OFFSET;
8820ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_TX_MASK;
8830ade8cd8SKonstantin Porotchkin 	data |= 0 << SD_EXTERNAL_CONFIG0_SD_PU_TX_OFFSET;
8840ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_HALF_BUS_MODE_MASK;
8850ade8cd8SKonstantin Porotchkin 	data |= 0 << SD_EXTERNAL_CONFIG0_HALF_BUS_MODE_OFFSET;
8860ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG0_REG, data, mask);
8870ade8cd8SKonstantin Porotchkin 
8880ade8cd8SKonstantin Porotchkin 	/* release from hard reset */
8890ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RESET_IN_MASK;
8900ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG1_RESET_IN_OFFSET;
8910ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RESET_CORE_MASK;
8920ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG1_RESET_CORE_OFFSET;
8930ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RF_RESET_IN_MASK;
8940ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG1_RF_RESET_IN_OFFSET;
8950ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
8960ade8cd8SKonstantin Porotchkin 
8970ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RESET_IN_MASK;
8980ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG1_RESET_IN_OFFSET;
8990ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RESET_CORE_MASK;
9000ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG1_RESET_CORE_OFFSET;
9010ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
9020ade8cd8SKonstantin Porotchkin 
9030ade8cd8SKonstantin Porotchkin 	/* Wait 1ms - until band gap and ref clock ready */
9040ade8cd8SKonstantin Porotchkin 	mdelay(1);
9050ade8cd8SKonstantin Porotchkin 
9060ade8cd8SKonstantin Porotchkin 	/* Start comphy Configuration */
9070ade8cd8SKonstantin Porotchkin 	debug("stage: Comphy configuration\n");
9080ade8cd8SKonstantin Porotchkin 	/* set reference clock */
9090ade8cd8SKonstantin Porotchkin 	mask = HPIPE_MISC_ICP_FORCE_MASK;
9100ade8cd8SKonstantin Porotchkin 	data = (speed == COMPHY_SPEED_5_15625G) ?
9110ade8cd8SKonstantin Porotchkin 		(0x0 << HPIPE_MISC_ICP_FORCE_OFFSET) :
9120ade8cd8SKonstantin Porotchkin 		(0x1 << HPIPE_MISC_ICP_FORCE_OFFSET);
9130ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_MISC_REFCLK_SEL_MASK;
9140ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_MISC_REFCLK_SEL_OFFSET;
9150ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_MISC_REG, data, mask);
9160ade8cd8SKonstantin Porotchkin 	/* Power and PLL Control */
9170ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_PLL_REF_FREQ_MASK;
9180ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_PWR_PLL_REF_FREQ_OFFSET;
9190ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_PLL_PHY_MODE_MASK;
9200ade8cd8SKonstantin Porotchkin 	data |= 0x4 << HPIPE_PWR_PLL_PHY_MODE_OFFSET;
9210ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_PLL_REG, data, mask);
9220ade8cd8SKonstantin Porotchkin 	/* Loopback register */
9230ade8cd8SKonstantin Porotchkin 	mask = HPIPE_LOOPBACK_SEL_MASK;
9240ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_LOOPBACK_SEL_OFFSET;
9250ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LOOPBACK_REG, data, mask);
9260ade8cd8SKonstantin Porotchkin 	/* rx control 1 */
9270ade8cd8SKonstantin Porotchkin 	mask = HPIPE_RX_CONTROL_1_RXCLK2X_SEL_MASK;
9280ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_RX_CONTROL_1_RXCLK2X_SEL_OFFSET;
9290ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RX_CONTROL_1_CLK8T_EN_MASK;
9300ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_RX_CONTROL_1_CLK8T_EN_OFFSET;
9310ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_RX_CONTROL_1_REG, data, mask);
9320ade8cd8SKonstantin Porotchkin 	/* DTL Control */
9330ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_CTR_DTL_FLOOP_EN_MASK;
9340ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_PWR_CTR_DTL_FLOOP_EN_OFFSET;
9350ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_DTL_REG, data, mask);
9360ade8cd8SKonstantin Porotchkin 
9370ade8cd8SKonstantin Porotchkin 	/* Transmitter/Receiver Speed Divider Force */
9380ade8cd8SKonstantin Porotchkin 	if (speed == COMPHY_SPEED_5_15625G) {
9390ade8cd8SKonstantin Porotchkin 		mask = HPIPE_SPD_DIV_FORCE_RX_SPD_DIV_MASK;
9400ade8cd8SKonstantin Porotchkin 		data = 1 << HPIPE_SPD_DIV_FORCE_RX_SPD_DIV_OFFSET;
9410ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_SPD_DIV_FORCE_RX_SPD_DIV_FORCE_MASK;
9420ade8cd8SKonstantin Porotchkin 		data |= 1 << HPIPE_SPD_DIV_FORCE_RX_SPD_DIV_FORCE_OFFSET;
9430ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_SPD_DIV_FORCE_TX_SPD_DIV_MASK;
9440ade8cd8SKonstantin Porotchkin 		data |= 1 << HPIPE_SPD_DIV_FORCE_TX_SPD_DIV_OFFSET;
9450ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_SPD_DIV_FORCE_TX_SPD_DIV_FORCE_MASK;
9460ade8cd8SKonstantin Porotchkin 		data |= 1 << HPIPE_SPD_DIV_FORCE_TX_SPD_DIV_FORCE_OFFSET;
9470ade8cd8SKonstantin Porotchkin 	} else {
9480ade8cd8SKonstantin Porotchkin 		mask = HPIPE_TXDIGCK_DIV_FORCE_MASK;
9490ade8cd8SKonstantin Porotchkin 		data = 0x1 << HPIPE_TXDIGCK_DIV_FORCE_OFFSET;
9500ade8cd8SKonstantin Porotchkin 	}
9510ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_SPD_DIV_FORCE_REG, data, mask);
9520ade8cd8SKonstantin Porotchkin 
9530ade8cd8SKonstantin Porotchkin 	/* Set analog parameters from ETP(HW) */
9540ade8cd8SKonstantin Porotchkin 	debug("stage: Analog parameters from ETP(HW)\n");
9550ade8cd8SKonstantin Porotchkin 	/* SERDES External Configuration 2 */
9560ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG2_PIN_DFE_EN_MASK;
9570ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG2_PIN_DFE_EN_OFFSET;
9580ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG2_REG, data, mask);
9590ade8cd8SKonstantin Porotchkin 	/* 0x7-DFE Resolution control */
9600ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_RES_FORCE_MASK;
9610ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_DFE_RES_FORCE_OFFSET;
9620ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_REG0, data, mask);
9630ade8cd8SKonstantin Porotchkin 	/* 0xd-G1_Setting_0 */
9640ade8cd8SKonstantin Porotchkin 	if (speed == COMPHY_SPEED_5_15625G) {
9650ade8cd8SKonstantin Porotchkin 		mask = HPIPE_G1_SET_0_G1_TX_EMPH1_MASK;
9660ade8cd8SKonstantin Porotchkin 		data = 0x6 << HPIPE_G1_SET_0_G1_TX_EMPH1_OFFSET;
9670ade8cd8SKonstantin Porotchkin 	} else {
9680ade8cd8SKonstantin Porotchkin 		mask = HPIPE_G1_SET_0_G1_TX_AMP_MASK;
96942a29337SGrzegorz Jaszczyk 		data = xfi_static_values->g1_amp <<
97042a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_0_G1_TX_AMP_OFFSET;
9710ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SET_0_G1_TX_EMPH1_MASK;
97242a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_emph <<
97342a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_0_G1_TX_EMPH1_OFFSET;
97442a29337SGrzegorz Jaszczyk 
97542a29337SGrzegorz Jaszczyk 		mask |= HPIPE_G1_SET_0_G1_TX_EMPH1_EN_MASK;
97642a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_emph_en <<
97742a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_0_G1_TX_EMPH1_EN_OFFSET;
97842a29337SGrzegorz Jaszczyk 		mask |= HPIPE_G1_SET_0_G1_TX_AMP_ADJ_MASK;
97942a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_tx_amp_adj <<
98042a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_0_G1_TX_AMP_ADJ_OFFSET;
9810ade8cd8SKonstantin Porotchkin 	}
9820ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SET_0_REG, data, mask);
9830ade8cd8SKonstantin Porotchkin 	/* Genration 1 setting 2 (G1_Setting_2) */
9840ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SET_2_G1_TX_EMPH0_MASK;
98542a29337SGrzegorz Jaszczyk 	data = xfi_static_values->g1_tx_emph <<
98642a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_2_G1_TX_EMPH0_OFFSET;
9870ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SET_2_G1_TX_EMPH0_EN_MASK;
98842a29337SGrzegorz Jaszczyk 	data |= xfi_static_values->g1_tx_emph_en <<
98942a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_2_G1_TX_EMPH0_EN_OFFSET;
9900ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SET_2_REG, data, mask);
9910ade8cd8SKonstantin Porotchkin 	/* Transmitter Slew Rate Control register (tx_reg1) */
9920ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_REG1_TX_EMPH_RES_MASK;
9930ade8cd8SKonstantin Porotchkin 	data = 0x3 << HPIPE_TX_REG1_TX_EMPH_RES_OFFSET;
9940ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_REG1_SLC_EN_MASK;
9950ade8cd8SKonstantin Porotchkin 	data |= 0x3f << HPIPE_TX_REG1_SLC_EN_OFFSET;
9960ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_REG1_REG, data, mask);
9970ade8cd8SKonstantin Porotchkin 	/* Impedance Calibration Control register (cal_reg1) */
9980ade8cd8SKonstantin Porotchkin 	mask = HPIPE_CAL_REG_1_EXT_TXIMP_MASK;
9990ade8cd8SKonstantin Porotchkin 	data = 0xe << HPIPE_CAL_REG_1_EXT_TXIMP_OFFSET;
10000ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_CAL_REG_1_EXT_TXIMP_EN_MASK;
10010ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_CAL_REG_1_EXT_TXIMP_EN_OFFSET;
10020ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_CAL_REG1_REG, data, mask);
10030ade8cd8SKonstantin Porotchkin 	/* Generation 1 Setting 5 (g1_setting_5) */
10040ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SETTING_5_G1_ICP_MASK;
10050ade8cd8SKonstantin Porotchkin 	data = 0 << HPIPE_CAL_REG_1_EXT_TXIMP_OFFSET;
10060ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SETTING_5_REG, data, mask);
10070ade8cd8SKonstantin Porotchkin 
10080ade8cd8SKonstantin Porotchkin 	/* 0xE-G1_Setting_1 */
10090ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SET_1_G1_RX_DFE_EN_MASK;
10100ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_G1_SET_1_G1_RX_DFE_EN_OFFSET;
10110ade8cd8SKonstantin Porotchkin 	if (speed == COMPHY_SPEED_5_15625G) {
10120ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SET_1_G1_RX_SELMUPI_MASK;
10130ade8cd8SKonstantin Porotchkin 		data |= 0x1 << HPIPE_G1_SET_1_G1_RX_SELMUPI_OFFSET;
10143c0024ccSGrzegorz Jaszczyk 		mask |= HPIPE_G1_SET_1_G1_RX_SELMUPF_MASK;
10153c0024ccSGrzegorz Jaszczyk 		data |= 0x1 << HPIPE_G1_SET_1_G1_RX_SELMUPF_OFFSET;
10160ade8cd8SKonstantin Porotchkin 	} else {
10170ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SET_1_G1_RX_SELMUPI_MASK;
101842a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_rx_selmupi <<
101942a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_1_G1_RX_SELMUPI_OFFSET;
10203c0024ccSGrzegorz Jaszczyk 		mask |= HPIPE_G1_SET_1_G1_RX_SELMUPF_MASK;
102142a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_rx_selmupf <<
102242a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_1_G1_RX_SELMUPF_OFFSET;
10230ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SET_1_G1_RX_SELMUFI_MASK;
102442a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_rx_selmufi <<
102542a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_1_G1_RX_SELMUFI_OFFSET;
10260ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SET_1_G1_RX_SELMUFF_MASK;
102742a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_rx_selmuff <<
102842a29337SGrzegorz Jaszczyk 				HPIPE_G1_SET_1_G1_RX_SELMUFF_OFFSET;
10290ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SET_1_G1_RX_DIGCK_DIV_MASK;
10300ade8cd8SKonstantin Porotchkin 		data |= 0x3 << HPIPE_G1_SET_1_G1_RX_DIGCK_DIV_OFFSET;
10310ade8cd8SKonstantin Porotchkin 	}
10320ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SET_1_REG, data, mask);
10330ade8cd8SKonstantin Porotchkin 
10340ade8cd8SKonstantin Porotchkin 	/* 0xA-DFE_Reg3 */
10350ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_F3_F5_DFE_EN_MASK;
10360ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_DFE_F3_F5_DFE_EN_OFFSET;
10370ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_DFE_F3_F5_DFE_CTRL_MASK;
10380ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_DFE_F3_F5_DFE_CTRL_OFFSET;
10390ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_F3_F5_REG, data, mask);
10400ade8cd8SKonstantin Porotchkin 
10410ade8cd8SKonstantin Porotchkin 	/* 0x111-G1_Setting_4 */
10420ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SETTINGS_4_G1_DFE_RES_MASK;
10430ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_G1_SETTINGS_4_G1_DFE_RES_OFFSET;
10440ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SETTINGS_4_REG, data, mask);
10450ade8cd8SKonstantin Porotchkin 	/* Genration 1 setting 3 (G1_Setting_3) */
10460ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SETTINGS_3_G1_FBCK_SEL_MASK;
10470ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_G1_SETTINGS_3_G1_FBCK_SEL_OFFSET;
10480ade8cd8SKonstantin Porotchkin 	if (speed == COMPHY_SPEED_5_15625G) {
10490ade8cd8SKonstantin Porotchkin 		/* Force FFE (Feed Forward Equalization) to 5G */
10500ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SETTINGS_3_G1_FFE_CAP_SEL_MASK;
10510ade8cd8SKonstantin Porotchkin 		data |= 0xf << HPIPE_G1_SETTINGS_3_G1_FFE_CAP_SEL_OFFSET;
10520ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SETTINGS_3_G1_FFE_RES_SEL_MASK;
10530ade8cd8SKonstantin Porotchkin 		data |= 0x4 << HPIPE_G1_SETTINGS_3_G1_FFE_RES_SEL_OFFSET;
10540ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_MASK;
10550ade8cd8SKonstantin Porotchkin 		data |= 0x1 << HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_OFFSET;
10560ade8cd8SKonstantin Porotchkin 		reg_set(hpipe_addr + HPIPE_G1_SETTINGS_3_REG, data, mask);
105742a29337SGrzegorz Jaszczyk 	} else {
105842a29337SGrzegorz Jaszczyk 		mask |= HPIPE_G1_SETTINGS_3_G1_FFE_CAP_SEL_MASK;
105942a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_ffe_cap_sel <<
106042a29337SGrzegorz Jaszczyk 			HPIPE_G1_SETTINGS_3_G1_FFE_CAP_SEL_OFFSET;
106142a29337SGrzegorz Jaszczyk 		mask |= HPIPE_G1_SETTINGS_3_G1_FFE_RES_SEL_MASK;
106242a29337SGrzegorz Jaszczyk 		data |= xfi_static_values->g1_ffe_res_sel <<
106342a29337SGrzegorz Jaszczyk 			HPIPE_G1_SETTINGS_3_G1_FFE_RES_SEL_OFFSET;
106442a29337SGrzegorz Jaszczyk 		mask |= HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_MASK;
106542a29337SGrzegorz Jaszczyk 		data |= 0x1 << HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_OFFSET;
106642a29337SGrzegorz Jaszczyk 		reg_set(hpipe_addr + HPIPE_G1_SETTINGS_3_REG, data, mask);
106742a29337SGrzegorz Jaszczyk 
106842a29337SGrzegorz Jaszczyk 		/* Use the value from CAL_OS_PH_EXT */
106942a29337SGrzegorz Jaszczyk 		mask = HPIPE_CAL_RXCLKALIGN_90_EXT_EN_MASK;
107042a29337SGrzegorz Jaszczyk 		data = 1 << HPIPE_CAL_RXCLKALIGN_90_EXT_EN_OFFSET;
107142a29337SGrzegorz Jaszczyk 		reg_set(hpipe_addr +
107242a29337SGrzegorz Jaszczyk 			HPIPE_RX_CLK_ALIGN90_AND_TX_IDLE_CALIB_CTRL_REG,
107342a29337SGrzegorz Jaszczyk 			data, mask);
107442a29337SGrzegorz Jaszczyk 
107542a29337SGrzegorz Jaszczyk 		/* Update align90 */
107642a29337SGrzegorz Jaszczyk 		mask = HPIPE_CAL_OS_PH_EXT_MASK;
107742a29337SGrzegorz Jaszczyk 		data = xfi_static_values->align90 << HPIPE_CAL_OS_PH_EXT_OFFSET;
107842a29337SGrzegorz Jaszczyk 		reg_set(hpipe_addr +
107942a29337SGrzegorz Jaszczyk 			HPIPE_RX_CLK_ALIGN90_AND_TX_IDLE_CALIB_CTRL_REG,
108042a29337SGrzegorz Jaszczyk 			data, mask);
108142a29337SGrzegorz Jaszczyk 
108242a29337SGrzegorz Jaszczyk 		/* Force DFE resolution (use gen table value) */
108342a29337SGrzegorz Jaszczyk 		mask = HPIPE_DFE_RES_FORCE_MASK;
108442a29337SGrzegorz Jaszczyk 		data = 0x0 << HPIPE_DFE_RES_FORCE_OFFSET;
108542a29337SGrzegorz Jaszczyk 		reg_set(hpipe_addr + HPIPE_DFE_REG0, data, mask);
108642a29337SGrzegorz Jaszczyk 
108742a29337SGrzegorz Jaszczyk 		/* 0x111-G1 DFE_Setting_4 */
108842a29337SGrzegorz Jaszczyk 		mask = HPIPE_G1_SETTINGS_4_G1_DFE_RES_MASK;
108942a29337SGrzegorz Jaszczyk 		data = xfi_static_values->g1_dfe_res <<
109042a29337SGrzegorz Jaszczyk 			HPIPE_G1_SETTINGS_4_G1_DFE_RES_OFFSET;
109142a29337SGrzegorz Jaszczyk 		reg_set(hpipe_addr + HPIPE_G1_SETTINGS_4_REG, data, mask);
109242a29337SGrzegorz Jaszczyk 	}
10930ade8cd8SKonstantin Porotchkin 
10940ade8cd8SKonstantin Porotchkin 	/* Connfigure RX training timer */
10950ade8cd8SKonstantin Porotchkin 	mask = HPIPE_RX_TRAIN_TIMER_MASK;
10960ade8cd8SKonstantin Porotchkin 	data = 0x13 << HPIPE_RX_TRAIN_TIMER_OFFSET;
10970ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_CTRL_5_REG, data, mask);
10980ade8cd8SKonstantin Porotchkin 
10990ade8cd8SKonstantin Porotchkin 	/* Enable TX train peak to peak hold */
11000ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_TRAIN_P2P_HOLD_MASK;
11010ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_TX_TRAIN_P2P_HOLD_OFFSET;
11020ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_CTRL_0_REG, data, mask);
11030ade8cd8SKonstantin Porotchkin 
11040ade8cd8SKonstantin Porotchkin 	/* Configure TX preset index */
11050ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_PRESET_INDEX_MASK;
11060ade8cd8SKonstantin Porotchkin 	data = 0x2 << HPIPE_TX_PRESET_INDEX_OFFSET;
11070ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_PRESET_INDEX_REG, data, mask);
11080ade8cd8SKonstantin Porotchkin 
11090ade8cd8SKonstantin Porotchkin 	/* Disable pattern lock lost timeout */
11100ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PATTERN_LOCK_LOST_TIMEOUT_EN_MASK;
11110ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_PATTERN_LOCK_LOST_TIMEOUT_EN_OFFSET;
11120ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_FRAME_DETECT_CTRL_3_REG, data, mask);
11130ade8cd8SKonstantin Porotchkin 
11140ade8cd8SKonstantin Porotchkin 	/* Configure TX training pattern and TX training 16bit auto */
11150ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_TRAIN_16BIT_AUTO_EN_MASK;
11160ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_TX_TRAIN_16BIT_AUTO_EN_OFFSET;
11170ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_TRAIN_PAT_SEL_MASK;
11180ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_TX_TRAIN_PAT_SEL_OFFSET;
11190ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_REG, data, mask);
11200ade8cd8SKonstantin Porotchkin 
11210ade8cd8SKonstantin Porotchkin 	/* Configure Training patten number */
11220ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TRAIN_PAT_NUM_MASK;
11230ade8cd8SKonstantin Porotchkin 	data = 0x88 << HPIPE_TRAIN_PAT_NUM_OFFSET;
11240ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_FRAME_DETECT_CTRL_0_REG, data, mask);
11250ade8cd8SKonstantin Porotchkin 
11260ade8cd8SKonstantin Porotchkin 	/* Configure differencial manchester encoter to ethernet mode */
11270ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DME_ETHERNET_MODE_MASK;
11280ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_DME_ETHERNET_MODE_OFFSET;
11290ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DME_REG, data, mask);
11300ade8cd8SKonstantin Porotchkin 
11310ade8cd8SKonstantin Porotchkin 	/* Configure VDD Continuous Calibration */
11320ade8cd8SKonstantin Porotchkin 	mask = HPIPE_CAL_VDD_CONT_MODE_MASK;
11330ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_CAL_VDD_CONT_MODE_OFFSET;
11340ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_VDD_CAL_0_REG, data, mask);
11350ade8cd8SKonstantin Porotchkin 
11360ade8cd8SKonstantin Porotchkin 	/* Trigger sampler enable pulse (by toggleing the bit) */
11370ade8cd8SKonstantin Porotchkin 	mask = HPIPE_RX_SAMPLER_OS_GAIN_MASK;
11380ade8cd8SKonstantin Porotchkin 	data = 0x3 << HPIPE_RX_SAMPLER_OS_GAIN_OFFSET;
11390ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_SMAPLER_MASK;
11400ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_SMAPLER_OFFSET;
11410ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_SAMPLER_N_PROC_CALIB_CTRL_REG, data, mask);
11420ade8cd8SKonstantin Porotchkin 	mask = HPIPE_SMAPLER_MASK;
11430ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_SMAPLER_OFFSET;
11440ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_SAMPLER_N_PROC_CALIB_CTRL_REG, data, mask);
11450ade8cd8SKonstantin Porotchkin 
11460ade8cd8SKonstantin Porotchkin 	/* Set External RX Regulator Control */
11470ade8cd8SKonstantin Porotchkin 	mask = HPIPE_EXT_SELLV_RXSAMPL_MASK;
11480ade8cd8SKonstantin Porotchkin 	data = 0x1A << HPIPE_EXT_SELLV_RXSAMPL_OFFSET;
11490ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_VDD_CAL_CTRL_REG, data, mask);
11500ade8cd8SKonstantin Porotchkin 
11510ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations- Power Up PLL,Tx,Rx\n");
11520ade8cd8SKonstantin Porotchkin 	/* SERDES External Configuration */
11530ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG0_SD_PU_PLL_MASK;
11540ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_PLL_OFFSET;
11550ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_RX_MASK;
11560ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_RX_OFFSET;
11570ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_TX_MASK;
11580ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_TX_OFFSET;
11590ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG0_REG, data, mask);
11600ade8cd8SKonstantin Porotchkin 
11610ade8cd8SKonstantin Porotchkin 	/* check PLL rx & tx ready */
11620ade8cd8SKonstantin Porotchkin 	addr = sd_ip_addr + SD_EXTERNAL_STATUS0_REG;
11630ade8cd8SKonstantin Porotchkin 	data = SD_EXTERNAL_STATUS0_PLL_RX_MASK |
11640ade8cd8SKonstantin Porotchkin 	       SD_EXTERNAL_STATUS0_PLL_TX_MASK;
11650ade8cd8SKonstantin Porotchkin 	mask = data;
11660ade8cd8SKonstantin Porotchkin 	data = polling_with_timeout(addr, data, mask,
11670ade8cd8SKonstantin Porotchkin 				    PLL_LOCK_TIMEOUT, REG_32BIT);
11680ade8cd8SKonstantin Porotchkin 	if (data != 0) {
11690ade8cd8SKonstantin Porotchkin 		if (data & SD_EXTERNAL_STATUS0_PLL_RX_MASK)
11700ade8cd8SKonstantin Porotchkin 			ERROR("RX PLL is not locked\n");
11710ade8cd8SKonstantin Porotchkin 		if (data & SD_EXTERNAL_STATUS0_PLL_TX_MASK)
11720ade8cd8SKonstantin Porotchkin 			ERROR("TX PLL is not locked\n");
11730ade8cd8SKonstantin Porotchkin 
11740ade8cd8SKonstantin Porotchkin 		ret = -ETIMEDOUT;
11750ade8cd8SKonstantin Porotchkin 	}
11760ade8cd8SKonstantin Porotchkin 
11770ade8cd8SKonstantin Porotchkin 	/* RX init */
11780ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RX_INIT_MASK;
11790ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG1_RX_INIT_OFFSET;
11800ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
11810ade8cd8SKonstantin Porotchkin 
11820ade8cd8SKonstantin Porotchkin 	/* check that RX init done */
11830ade8cd8SKonstantin Porotchkin 	addr = sd_ip_addr + SD_EXTERNAL_STATUS0_REG;
11840ade8cd8SKonstantin Porotchkin 	data = SD_EXTERNAL_STATUS0_RX_INIT_MASK;
11850ade8cd8SKonstantin Porotchkin 	mask = data;
11860ade8cd8SKonstantin Porotchkin 	data = polling_with_timeout(addr, data, mask, 100, REG_32BIT);
11870ade8cd8SKonstantin Porotchkin 	if (data != 0) {
11880ade8cd8SKonstantin Porotchkin 		ERROR("RX init failed\n");
11890ade8cd8SKonstantin Porotchkin 		ret = -ETIMEDOUT;
11900ade8cd8SKonstantin Porotchkin 	}
11910ade8cd8SKonstantin Porotchkin 
11920ade8cd8SKonstantin Porotchkin 	debug("stage: RF Reset\n");
11930ade8cd8SKonstantin Porotchkin 	/* RF Reset */
11940ade8cd8SKonstantin Porotchkin 	mask =  SD_EXTERNAL_CONFIG1_RX_INIT_MASK;
11950ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG1_RX_INIT_OFFSET;
11960ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RF_RESET_IN_MASK;
11970ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG1_RF_RESET_IN_OFFSET;
11980ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
11990ade8cd8SKonstantin Porotchkin 
12000ade8cd8SKonstantin Porotchkin 	debug_exit();
12010ade8cd8SKonstantin Porotchkin 
12020ade8cd8SKonstantin Porotchkin 	return ret;
12030ade8cd8SKonstantin Porotchkin }
12040ade8cd8SKonstantin Porotchkin 
12050ade8cd8SKonstantin Porotchkin static int mvebu_cp110_comphy_pcie_power_on(uint64_t comphy_base,
12060ade8cd8SKonstantin Porotchkin 				     uint8_t comphy_index, uint32_t comphy_mode)
12070ade8cd8SKonstantin Porotchkin {
12080ade8cd8SKonstantin Porotchkin 	int ret = 0;
12090ade8cd8SKonstantin Porotchkin 	uint32_t reg, mask, data, pcie_width;
12100ade8cd8SKonstantin Porotchkin 	uint32_t clk_dir;
12110ade8cd8SKonstantin Porotchkin 	uintptr_t hpipe_addr, comphy_addr, addr;
12120ade8cd8SKonstantin Porotchkin 	_Bool clk_src = COMPHY_GET_CLK_SRC(comphy_mode);
121355df84f9SIgal Liberman 	_Bool called_from_uboot = COMPHY_GET_CALLER(comphy_mode);
121455df84f9SIgal Liberman 
121555df84f9SIgal Liberman 	/* In Armada 8K DB boards, PCIe initialization can be executed
121655df84f9SIgal Liberman 	 * only once (PCIe reset performed during chip power on and
121755df84f9SIgal Liberman 	 * it cannot be executed via GPIO later).
121855df84f9SIgal Liberman 	 * This means that power on can be executed only once, so let's
121955df84f9SIgal Liberman 	 * mark if the caller is bootloader or Linux.
122055df84f9SIgal Liberman 	 * If bootloader -> run power on.
122155df84f9SIgal Liberman 	 * If Linux -> exit.
122255df84f9SIgal Liberman 	 *
122355df84f9SIgal Liberman 	 * TODO: In MacciatoBIN, PCIe reset is connected via GPIO,
122455df84f9SIgal Liberman 	 * so after GPIO reset is added to Linux Kernel, it can be
122555df84f9SIgal Liberman 	 * powered-on by Linux.
122655df84f9SIgal Liberman 	 */
122755df84f9SIgal Liberman 	if (!called_from_uboot)
122855df84f9SIgal Liberman 		return ret;
12290ade8cd8SKonstantin Porotchkin 
12300ade8cd8SKonstantin Porotchkin 	hpipe_addr = HPIPE_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
12310ade8cd8SKonstantin Porotchkin 				comphy_index);
12320ade8cd8SKonstantin Porotchkin 	comphy_addr = COMPHY_ADDR(comphy_base, comphy_index);
12330ade8cd8SKonstantin Porotchkin 	pcie_width = COMPHY_GET_PCIE_WIDTH(comphy_mode);
12340ade8cd8SKonstantin Porotchkin 
12350ade8cd8SKonstantin Porotchkin 	debug_enter();
12360ade8cd8SKonstantin Porotchkin 
12370ade8cd8SKonstantin Porotchkin 	spin_lock(&cp110_mac_reset_lock);
12380ade8cd8SKonstantin Porotchkin 
12390ade8cd8SKonstantin Porotchkin 	reg = mmio_read_32(SYS_CTRL_FROM_COMPHY_ADDR(comphy_base) +
12400ade8cd8SKonstantin Porotchkin 						SYS_CTRL_UINIT_SOFT_RESET_REG);
12410ade8cd8SKonstantin Porotchkin 	switch (comphy_index) {
12420ade8cd8SKonstantin Porotchkin 	case COMPHY_LANE0:
12430ade8cd8SKonstantin Porotchkin 		reg |= PCIE_MAC_RESET_MASK_PORT0;
12440ade8cd8SKonstantin Porotchkin 		break;
12450ade8cd8SKonstantin Porotchkin 	case COMPHY_LANE4:
12460ade8cd8SKonstantin Porotchkin 		reg |= PCIE_MAC_RESET_MASK_PORT1;
12470ade8cd8SKonstantin Porotchkin 		break;
12480ade8cd8SKonstantin Porotchkin 	case COMPHY_LANE5:
12490ade8cd8SKonstantin Porotchkin 		reg |= PCIE_MAC_RESET_MASK_PORT2;
12500ade8cd8SKonstantin Porotchkin 		break;
12510ade8cd8SKonstantin Porotchkin 	}
12520ade8cd8SKonstantin Porotchkin 
12530ade8cd8SKonstantin Porotchkin 	mmio_write_32(SYS_CTRL_FROM_COMPHY_ADDR(comphy_base) +
12540ade8cd8SKonstantin Porotchkin 					    SYS_CTRL_UINIT_SOFT_RESET_REG, reg);
12550ade8cd8SKonstantin Porotchkin 	spin_unlock(&cp110_mac_reset_lock);
12560ade8cd8SKonstantin Porotchkin 
12570ade8cd8SKonstantin Porotchkin 	/* Configure PIPE selector for PCIE */
12580ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_set_pipe_selector(comphy_base, comphy_index,
12590ade8cd8SKonstantin Porotchkin 					     comphy_mode);
12600ade8cd8SKonstantin Porotchkin 
12610ade8cd8SKonstantin Porotchkin 	/*
12620ade8cd8SKonstantin Porotchkin 	 * Read SAR (Sample-At-Reset) configuration for the PCIe clock
12630ade8cd8SKonstantin Porotchkin 	 * direction.
12640ade8cd8SKonstantin Porotchkin 	 *
12650ade8cd8SKonstantin Porotchkin 	 * SerDes Lane 4/5 got the PCIe ref-clock #1,
12660ade8cd8SKonstantin Porotchkin 	 * and SerDes Lane 0 got PCIe ref-clock #0
12670ade8cd8SKonstantin Porotchkin 	 */
12680ade8cd8SKonstantin Porotchkin 	reg = mmio_read_32(DFX_FROM_COMPHY_ADDR(comphy_base) +
12690ade8cd8SKonstantin Porotchkin 			   SAR_STATUS_0_REG);
12700ade8cd8SKonstantin Porotchkin 	if (comphy_index == COMPHY_LANE4 || comphy_index == COMPHY_LANE5)
12710ade8cd8SKonstantin Porotchkin 		clk_dir = (reg & SAR_RST_PCIE1_CLOCK_CONFIG_CP1_MASK) >>
12720ade8cd8SKonstantin Porotchkin 					  SAR_RST_PCIE1_CLOCK_CONFIG_CP1_OFFSET;
12730ade8cd8SKonstantin Porotchkin 	else
12740ade8cd8SKonstantin Porotchkin 		clk_dir = (reg & SAR_RST_PCIE0_CLOCK_CONFIG_CP1_MASK) >>
12750ade8cd8SKonstantin Porotchkin 					  SAR_RST_PCIE0_CLOCK_CONFIG_CP1_OFFSET;
12760ade8cd8SKonstantin Porotchkin 
12770ade8cd8SKonstantin Porotchkin 	debug("On lane %d\n", comphy_index);
12780ade8cd8SKonstantin Porotchkin 	debug("PCIe clock direction = %x\n", clk_dir);
12790ade8cd8SKonstantin Porotchkin 	debug("PCIe Width = %d\n", pcie_width);
12800ade8cd8SKonstantin Porotchkin 
12810ade8cd8SKonstantin Porotchkin 	/* enable PCIe X4 and X2 */
12820ade8cd8SKonstantin Porotchkin 	if (comphy_index == COMPHY_LANE0) {
12830ade8cd8SKonstantin Porotchkin 		if (pcie_width == PCIE_LNK_X4) {
12840ade8cd8SKonstantin Porotchkin 			data = 0x1 << COMMON_PHY_SD_CTRL1_PCIE_X4_EN_OFFSET;
12850ade8cd8SKonstantin Porotchkin 			mask = COMMON_PHY_SD_CTRL1_PCIE_X4_EN_MASK;
12860ade8cd8SKonstantin Porotchkin 			reg_set(comphy_base + COMMON_PHY_SD_CTRL1,
12870ade8cd8SKonstantin Porotchkin 				data, mask);
12880ade8cd8SKonstantin Porotchkin 		} else if (pcie_width == PCIE_LNK_X2) {
12890ade8cd8SKonstantin Porotchkin 			data = 0x1 << COMMON_PHY_SD_CTRL1_PCIE_X2_EN_OFFSET;
12900ade8cd8SKonstantin Porotchkin 			mask = COMMON_PHY_SD_CTRL1_PCIE_X2_EN_MASK;
12910ade8cd8SKonstantin Porotchkin 			reg_set(comphy_base + COMMON_PHY_SD_CTRL1, data, mask);
12920ade8cd8SKonstantin Porotchkin 		}
12930ade8cd8SKonstantin Porotchkin 	}
12940ade8cd8SKonstantin Porotchkin 
12950ade8cd8SKonstantin Porotchkin 	/* If PCIe clock is output and clock source from SerDes lane 5,
12960ade8cd8SKonstantin Porotchkin 	 * need to configure the clock-source MUX.
12970ade8cd8SKonstantin Porotchkin 	 * By default, the clock source is from lane 4
12980ade8cd8SKonstantin Porotchkin 	 */
12990ade8cd8SKonstantin Porotchkin 	if (clk_dir && clk_src && (comphy_index == COMPHY_LANE5)) {
13000ade8cd8SKonstantin Porotchkin 		data = DFX_DEV_GEN_PCIE_CLK_SRC_MUX <<
13010ade8cd8SKonstantin Porotchkin 						DFX_DEV_GEN_PCIE_CLK_SRC_OFFSET;
13020ade8cd8SKonstantin Porotchkin 		mask = DFX_DEV_GEN_PCIE_CLK_SRC_MASK;
13030ade8cd8SKonstantin Porotchkin 		reg_set(DFX_FROM_COMPHY_ADDR(comphy_base) +
13040ade8cd8SKonstantin Porotchkin 			DFX_DEV_GEN_CTRL12_REG, data, mask);
13050ade8cd8SKonstantin Porotchkin 	}
13060ade8cd8SKonstantin Porotchkin 
13070ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations - hard reset comphy\n");
13080ade8cd8SKonstantin Porotchkin 	/* RFU configurations - hard reset comphy */
13090ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_UP_MASK;
13100ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_UP_OFFSET;
13110ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PIPE_SELECT_MASK;
13120ade8cd8SKonstantin Porotchkin 	data |= 0x1 << COMMON_PHY_CFG1_PIPE_SELECT_OFFSET;
13130ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PWR_ON_RESET_MASK;
13140ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_PWR_ON_RESET_OFFSET;
13150ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_CORE_RSTN_MASK;
13160ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_CORE_RSTN_OFFSET;
13170ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_PHY_MODE_MASK;
13180ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_PHY_MODE_OFFSET;
13190ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
13200ade8cd8SKonstantin Porotchkin 
13210ade8cd8SKonstantin Porotchkin 	/* release from hard reset */
13220ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_ON_RESET_MASK;
13230ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_ON_RESET_OFFSET;
13240ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_CORE_RSTN_MASK;
13250ade8cd8SKonstantin Porotchkin 	data |= 0x1 << COMMON_PHY_CFG1_CORE_RSTN_OFFSET;
13260ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
13270ade8cd8SKonstantin Porotchkin 
13280ade8cd8SKonstantin Porotchkin 	/* Wait 1ms - until band gap and ref clock ready */
13290ade8cd8SKonstantin Porotchkin 	mdelay(1);
13300ade8cd8SKonstantin Porotchkin 	/* Start comphy Configuration */
13310ade8cd8SKonstantin Porotchkin 	debug("stage: Comphy configuration\n");
13320ade8cd8SKonstantin Porotchkin 	/* Set PIPE soft reset */
13330ade8cd8SKonstantin Porotchkin 	mask = HPIPE_RST_CLK_CTRL_PIPE_RST_MASK;
13340ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_RST_CLK_CTRL_PIPE_RST_OFFSET;
13350ade8cd8SKonstantin Porotchkin 	/* Set PHY datapath width mode for V0 */
13360ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RST_CLK_CTRL_FIXED_PCLK_MASK;
13370ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_RST_CLK_CTRL_FIXED_PCLK_OFFSET;
13380ade8cd8SKonstantin Porotchkin 	/* Set Data bus width USB mode for V0 */
13390ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RST_CLK_CTRL_PIPE_WIDTH_MASK;
13400ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_RST_CLK_CTRL_PIPE_WIDTH_OFFSET;
13410ade8cd8SKonstantin Porotchkin 	/* Set CORE_CLK output frequency for 250Mhz */
13420ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RST_CLK_CTRL_CORE_FREQ_SEL_MASK;
13430ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_RST_CLK_CTRL_CORE_FREQ_SEL_OFFSET;
13440ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_RST_CLK_CTRL_REG, data, mask);
13450ade8cd8SKonstantin Porotchkin 	/* Set PLL ready delay for 0x2 */
13460ade8cd8SKonstantin Porotchkin 	data = 0x2 << HPIPE_CLK_SRC_LO_PLL_RDY_DL_OFFSET;
13470ade8cd8SKonstantin Porotchkin 	mask = HPIPE_CLK_SRC_LO_PLL_RDY_DL_MASK;
13480ade8cd8SKonstantin Porotchkin 	if (pcie_width != PCIE_LNK_X1) {
13490ade8cd8SKonstantin Porotchkin 		data |= 0x1 << HPIPE_CLK_SRC_LO_BUNDLE_PERIOD_SEL_OFFSET;
13500ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_CLK_SRC_LO_BUNDLE_PERIOD_SEL_MASK;
13510ade8cd8SKonstantin Porotchkin 		data |= 0x1 << HPIPE_CLK_SRC_LO_BUNDLE_PERIOD_SCALE_OFFSET;
13520ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_CLK_SRC_LO_BUNDLE_PERIOD_SCALE_MASK;
13530ade8cd8SKonstantin Porotchkin 	}
13540ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_CLK_SRC_LO_REG, data, mask);
13550ade8cd8SKonstantin Porotchkin 
13560ade8cd8SKonstantin Porotchkin 	/* Set PIPE mode interface to PCIe3 - 0x1  & set lane order */
13570ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_CLK_SRC_HI_MODE_PIPE_OFFSET;
13580ade8cd8SKonstantin Porotchkin 	mask = HPIPE_CLK_SRC_HI_MODE_PIPE_MASK;
13590ade8cd8SKonstantin Porotchkin 	if (pcie_width != PCIE_LNK_X1) {
13600ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_CLK_SRC_HI_LANE_STRT_MASK;
13610ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_CLK_SRC_HI_LANE_MASTER_MASK;
13620ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_CLK_SRC_HI_LANE_BREAK_MASK;
13630ade8cd8SKonstantin Porotchkin 		if (comphy_index == 0) {
13640ade8cd8SKonstantin Porotchkin 			data |= 0x1 << HPIPE_CLK_SRC_HI_LANE_STRT_OFFSET;
13650ade8cd8SKonstantin Porotchkin 			data |= 0x1 << HPIPE_CLK_SRC_HI_LANE_MASTER_OFFSET;
13660ade8cd8SKonstantin Porotchkin 		} else if (comphy_index == (pcie_width - 1)) {
13670ade8cd8SKonstantin Porotchkin 			data |= 0x1 << HPIPE_CLK_SRC_HI_LANE_BREAK_OFFSET;
13680ade8cd8SKonstantin Porotchkin 		}
13690ade8cd8SKonstantin Porotchkin 	}
13700ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_CLK_SRC_HI_REG, data, mask);
13710ade8cd8SKonstantin Porotchkin 	/* Config update polarity equalization */
13720ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_CFG_UPDATE_POLARITY_OFFSET;
13730ade8cd8SKonstantin Porotchkin 	mask = HPIPE_CFG_UPDATE_POLARITY_MASK;
13740ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LANE_EQ_CFG1_REG, data, mask);
13750ade8cd8SKonstantin Porotchkin 	/* Set PIPE version 4 to mode enable */
13760ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_DFE_CTRL_28_PIPE4_OFFSET;
13770ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_CTRL_28_PIPE4_MASK;
13780ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_CTRL_28_REG, data, mask);
13790ade8cd8SKonstantin Porotchkin 	/* TODO: check if pcie clock is output/input - for bringup use input*/
13800ade8cd8SKonstantin Porotchkin 	/* Enable PIN clock 100M_125M */
13810ade8cd8SKonstantin Porotchkin 	mask = 0;
13820ade8cd8SKonstantin Porotchkin 	data = 0;
13830ade8cd8SKonstantin Porotchkin 	/* Only if clock is output, configure the clock-source mux */
13840ade8cd8SKonstantin Porotchkin 	if (clk_dir) {
13850ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_MISC_CLK100M_125M_MASK;
13860ade8cd8SKonstantin Porotchkin 		data |= 0x1 << HPIPE_MISC_CLK100M_125M_OFFSET;
13870ade8cd8SKonstantin Porotchkin 	}
13880ade8cd8SKonstantin Porotchkin 	/* Set PIN_TXDCLK_2X Clock Freq. Selection for outputs 500MHz clock */
13890ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_MISC_TXDCLK_2X_MASK;
13900ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_MISC_TXDCLK_2X_OFFSET;
13910ade8cd8SKonstantin Porotchkin 	/* Enable 500MHz Clock */
13920ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_MISC_CLK500_EN_MASK;
13930ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_MISC_CLK500_EN_OFFSET;
13940ade8cd8SKonstantin Porotchkin 	if (clk_dir) { /* output */
13950ade8cd8SKonstantin Porotchkin 		/* Set reference clock comes from group 1 */
13960ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_MISC_REFCLK_SEL_MASK;
13970ade8cd8SKonstantin Porotchkin 		data |= 0x0 << HPIPE_MISC_REFCLK_SEL_OFFSET;
13980ade8cd8SKonstantin Porotchkin 	} else {
13990ade8cd8SKonstantin Porotchkin 		/* Set reference clock comes from group 2 */
14000ade8cd8SKonstantin Porotchkin 		mask |= HPIPE_MISC_REFCLK_SEL_MASK;
14010ade8cd8SKonstantin Porotchkin 		data |= 0x1 << HPIPE_MISC_REFCLK_SEL_OFFSET;
14020ade8cd8SKonstantin Porotchkin 	}
14030ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_MISC_ICP_FORCE_MASK;
14040ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_MISC_ICP_FORCE_OFFSET;
14050ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_MISC_REG, data, mask);
14060ade8cd8SKonstantin Porotchkin 	if (clk_dir) { /* output */
14070ade8cd8SKonstantin Porotchkin 		/* Set reference frequcency select - 0x2 for 25MHz*/
14080ade8cd8SKonstantin Porotchkin 		mask = HPIPE_PWR_PLL_REF_FREQ_MASK;
14090ade8cd8SKonstantin Porotchkin 		data = 0x2 << HPIPE_PWR_PLL_REF_FREQ_OFFSET;
14100ade8cd8SKonstantin Porotchkin 	} else {
14110ade8cd8SKonstantin Porotchkin 		/* Set reference frequcency select - 0x0 for 100MHz*/
14120ade8cd8SKonstantin Porotchkin 		mask = HPIPE_PWR_PLL_REF_FREQ_MASK;
14130ade8cd8SKonstantin Porotchkin 		data = 0x0 << HPIPE_PWR_PLL_REF_FREQ_OFFSET;
14140ade8cd8SKonstantin Porotchkin 	}
14150ade8cd8SKonstantin Porotchkin 	/* Set PHY mode to PCIe */
14160ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_PLL_PHY_MODE_MASK;
14170ade8cd8SKonstantin Porotchkin 	data |= 0x3 << HPIPE_PWR_PLL_PHY_MODE_OFFSET;
14180ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_PLL_REG, data, mask);
14190ade8cd8SKonstantin Porotchkin 
14200ade8cd8SKonstantin Porotchkin 	/* ref clock alignment */
14210ade8cd8SKonstantin Porotchkin 	if (pcie_width != PCIE_LNK_X1) {
14220ade8cd8SKonstantin Porotchkin 		mask = HPIPE_LANE_ALIGN_OFF_MASK;
14230ade8cd8SKonstantin Porotchkin 		data = 0x0 << HPIPE_LANE_ALIGN_OFF_OFFSET;
14240ade8cd8SKonstantin Porotchkin 		reg_set(hpipe_addr + HPIPE_LANE_ALIGN_REG, data, mask);
14250ade8cd8SKonstantin Porotchkin 	}
14260ade8cd8SKonstantin Porotchkin 
14270ade8cd8SKonstantin Porotchkin 	/* Set the amount of time spent in the LoZ state - set for 0x7 only if
14280ade8cd8SKonstantin Porotchkin 	 * the PCIe clock is output
14290ade8cd8SKonstantin Porotchkin 	 */
14300ade8cd8SKonstantin Porotchkin 	if (clk_dir)
14310ade8cd8SKonstantin Porotchkin 		reg_set(hpipe_addr + HPIPE_GLOBAL_PM_CTRL,
14320ade8cd8SKonstantin Porotchkin 			0x7 << HPIPE_GLOBAL_PM_RXDLOZ_WAIT_OFFSET,
14330ade8cd8SKonstantin Porotchkin 			HPIPE_GLOBAL_PM_RXDLOZ_WAIT_MASK);
14340ade8cd8SKonstantin Porotchkin 
14350ade8cd8SKonstantin Porotchkin 	/* Set Maximal PHY Generation Setting(8Gbps) */
14360ade8cd8SKonstantin Porotchkin 	mask = HPIPE_INTERFACE_GEN_MAX_MASK;
14370ade8cd8SKonstantin Porotchkin 	data = 0x2 << HPIPE_INTERFACE_GEN_MAX_OFFSET;
14380ade8cd8SKonstantin Porotchkin 	/* Bypass frame detection and sync detection for RX DATA */
14390ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_INTERFACE_DET_BYPASS_MASK;
14400ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_INTERFACE_DET_BYPASS_OFFSET;
14410ade8cd8SKonstantin Porotchkin 	/* Set Link Train Mode (Tx training control pins are used) */
14420ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_INTERFACE_LINK_TRAIN_MASK;
14430ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_INTERFACE_LINK_TRAIN_OFFSET;
14440ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_INTERFACE_REG, data, mask);
14450ade8cd8SKonstantin Porotchkin 
14460ade8cd8SKonstantin Porotchkin 	/* Set Idle_sync enable */
14470ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PCIE_IDLE_SYNC_MASK;
14480ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_PCIE_IDLE_SYNC_OFFSET;
14490ade8cd8SKonstantin Porotchkin 	/* Select bits for PCIE Gen3(32bit) */
14500ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PCIE_SEL_BITS_MASK;
14510ade8cd8SKonstantin Porotchkin 	data |= 0x2 << HPIPE_PCIE_SEL_BITS_OFFSET;
14520ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PCIE_REG0, data, mask);
14530ade8cd8SKonstantin Porotchkin 
14540ade8cd8SKonstantin Porotchkin 	/* Enable Tx_adapt_g1 */
14550ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_TRAIN_CTRL_G1_MASK;
14560ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_TX_TRAIN_CTRL_G1_OFFSET;
14570ade8cd8SKonstantin Porotchkin 	/* Enable Tx_adapt_gn1 */
14580ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_TRAIN_CTRL_GN1_MASK;
14590ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_TX_TRAIN_CTRL_GN1_OFFSET;
14600ade8cd8SKonstantin Porotchkin 	/* Disable Tx_adapt_g0 */
14610ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_TRAIN_CTRL_G0_MASK;
14620ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_TX_TRAIN_CTRL_G0_OFFSET;
14630ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_CTRL_REG, data, mask);
14640ade8cd8SKonstantin Porotchkin 
14650ade8cd8SKonstantin Porotchkin 	/* Set reg_tx_train_chk_init */
14660ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_TRAIN_CHK_INIT_MASK;
14670ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_TX_TRAIN_CHK_INIT_OFFSET;
14680ade8cd8SKonstantin Porotchkin 	/* Enable TX_COE_FM_PIN_PCIE3_EN */
14690ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_TRAIN_COE_FM_PIN_PCIE3_MASK;
14700ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_TX_TRAIN_COE_FM_PIN_PCIE3_OFFSET;
14710ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_REG, data, mask);
14720ade8cd8SKonstantin Porotchkin 
14730ade8cd8SKonstantin Porotchkin 	debug("stage: TRx training parameters\n");
14740ade8cd8SKonstantin Porotchkin 	/* Set Preset sweep configurations */
14750ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_TX_STATUS_CHECK_MODE_MASK;
14760ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_TX_STATUS_CHECK_MODE_OFFSET;
14770ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_NUM_OF_PRESET_MASK;
14780ade8cd8SKonstantin Porotchkin 	data |= 0x7 << HPIPE_TX_NUM_OF_PRESET_OFFSET;
14790ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_SWEEP_PRESET_EN_MASK;
14800ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_TX_SWEEP_PRESET_EN_OFFSET;
14810ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_CTRL_11_REG, data, mask);
14820ade8cd8SKonstantin Porotchkin 
14830ade8cd8SKonstantin Porotchkin 	/* Tx train start configuration */
14840ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_TRAIN_START_SQ_EN_MASK;
14850ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_TX_TRAIN_START_SQ_EN_OFFSET;
14860ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_TRAIN_START_FRM_DET_EN_MASK;
14870ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_TX_TRAIN_START_FRM_DET_EN_OFFSET;
14880ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_TRAIN_START_FRM_LOCK_EN_MASK;
14890ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_TX_TRAIN_START_FRM_LOCK_EN_OFFSET;
14900ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_TX_TRAIN_WAIT_TIME_EN_MASK;
14910ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_TX_TRAIN_WAIT_TIME_EN_OFFSET;
14920ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_CTRL_5_REG, data, mask);
14930ade8cd8SKonstantin Porotchkin 
14940ade8cd8SKonstantin Porotchkin 	/* Enable Tx train P2P */
14950ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_TRAIN_P2P_HOLD_MASK;
14960ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_TX_TRAIN_P2P_HOLD_OFFSET;
14970ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_CTRL_0_REG, data, mask);
14980ade8cd8SKonstantin Porotchkin 
14990ade8cd8SKonstantin Porotchkin 	/* Configure Tx train timeout */
15000ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TRX_TRAIN_TIMER_MASK;
15010ade8cd8SKonstantin Porotchkin 	data = 0x17 << HPIPE_TRX_TRAIN_TIMER_OFFSET;
15020ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_CTRL_4_REG, data, mask);
15030ade8cd8SKonstantin Porotchkin 
15040ade8cd8SKonstantin Porotchkin 	/* Disable G0/G1/GN1 adaptation */
15050ade8cd8SKonstantin Porotchkin 	mask = HPIPE_TX_TRAIN_CTRL_G1_MASK | HPIPE_TX_TRAIN_CTRL_GN1_MASK
15060ade8cd8SKonstantin Porotchkin 		| HPIPE_TX_TRAIN_CTRL_G0_OFFSET;
15070ade8cd8SKonstantin Porotchkin 	data = 0;
15080ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TX_TRAIN_CTRL_REG, data, mask);
15090ade8cd8SKonstantin Porotchkin 
15100ade8cd8SKonstantin Porotchkin 	/* Disable DTL frequency loop */
15110ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_CTR_DTL_FLOOP_EN_MASK;
15120ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_PWR_CTR_DTL_FLOOP_EN_OFFSET;
15130ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_DTL_REG, data, mask);
15140ade8cd8SKonstantin Porotchkin 
15150ade8cd8SKonstantin Porotchkin 	/* Configure G3 DFE */
15160ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G3_DFE_RES_MASK;
15170ade8cd8SKonstantin Porotchkin 	data = 0x3 << HPIPE_G3_DFE_RES_OFFSET;
15180ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G3_SETTING_4_REG, data, mask);
15190ade8cd8SKonstantin Porotchkin 
15200ade8cd8SKonstantin Porotchkin 	/* Use TX/RX training result for DFE */
15210ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_RES_FORCE_MASK;
15220ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_DFE_RES_FORCE_OFFSET;
15230ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_REG0,  data, mask);
15240ade8cd8SKonstantin Porotchkin 
15250ade8cd8SKonstantin Porotchkin 	/* Configure initial and final coefficient value for receiver */
15260ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G3_SET_1_G3_RX_SELMUPI_MASK;
15270ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_G3_SET_1_G3_RX_SELMUPI_OFFSET;
15280ade8cd8SKonstantin Porotchkin 
15290ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_1_G3_RX_SELMUPF_MASK;
15300ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G3_SET_1_G3_RX_SELMUPF_OFFSET;
15310ade8cd8SKonstantin Porotchkin 
15320ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_SET_1_G3_SAMPLER_INPAIRX2_EN_MASK;
15330ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_G3_SET_1_G3_SAMPLER_INPAIRX2_EN_OFFSET;
15340ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G3_SET_1_REG,  data, mask);
15350ade8cd8SKonstantin Porotchkin 
15360ade8cd8SKonstantin Porotchkin 	/* Trigger sampler enable pulse */
15370ade8cd8SKonstantin Porotchkin 	mask = HPIPE_SMAPLER_MASK;
15380ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_SMAPLER_OFFSET;
15390ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_SAMPLER_N_PROC_CALIB_CTRL_REG, data, mask);
15400ade8cd8SKonstantin Porotchkin 	udelay(5);
15410ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_SAMPLER_N_PROC_CALIB_CTRL_REG, 0, mask);
15420ade8cd8SKonstantin Porotchkin 
15430ade8cd8SKonstantin Porotchkin 	/* FFE resistor tuning for different bandwidth  */
15440ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G3_FFE_DEG_RES_LEVEL_MASK;
15450ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_G3_FFE_DEG_RES_LEVEL_OFFSET;
15460ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G3_FFE_LOAD_RES_LEVEL_MASK;
15470ade8cd8SKonstantin Porotchkin 	data |= 0x3 << HPIPE_G3_FFE_LOAD_RES_LEVEL_OFFSET;
15480ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G3_SETTING_3_REG, data, mask);
15490ade8cd8SKonstantin Porotchkin 
15500ade8cd8SKonstantin Porotchkin 	/* Pattern lock lost timeout disable */
15510ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PATTERN_LOCK_LOST_TIMEOUT_EN_MASK;
15520ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_PATTERN_LOCK_LOST_TIMEOUT_EN_OFFSET;
15530ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_FRAME_DETECT_CTRL_3_REG, data, mask);
15540ade8cd8SKonstantin Porotchkin 
15550ade8cd8SKonstantin Porotchkin 	/* Configure DFE adaptations */
15560ade8cd8SKonstantin Porotchkin 	mask = HPIPE_CDR_RX_MAX_DFE_ADAPT_0_MASK;
15570ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_CDR_RX_MAX_DFE_ADAPT_0_OFFSET;
15580ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_CDR_RX_MAX_DFE_ADAPT_1_MASK;
15590ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_CDR_RX_MAX_DFE_ADAPT_1_OFFSET;
15600ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_CDR_MAX_DFE_ADAPT_0_MASK;
15610ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_CDR_MAX_DFE_ADAPT_0_OFFSET;
15620ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_CDR_MAX_DFE_ADAPT_1_MASK;
15630ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_CDR_MAX_DFE_ADAPT_1_OFFSET;
15640ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_CDR_CONTROL_REG, data, mask);
15650ade8cd8SKonstantin Porotchkin 
15660ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_TX_MAX_DFE_ADAPT_MASK;
15670ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_DFE_TX_MAX_DFE_ADAPT_OFFSET;
15680ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_CONTROL_REG, data, mask);
15690ade8cd8SKonstantin Porotchkin 
15700ade8cd8SKonstantin Porotchkin 	/* Genration 2 setting 1*/
15710ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G2_SET_1_G2_RX_SELMUPI_MASK;
15720ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_G2_SET_1_G2_RX_SELMUPI_OFFSET;
15733c0024ccSGrzegorz Jaszczyk 	mask |= HPIPE_G2_SET_1_G2_RX_SELMUPF_MASK;
15743c0024ccSGrzegorz Jaszczyk 	data |= 0x1 << HPIPE_G2_SET_1_G2_RX_SELMUPF_OFFSET;
15750ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G2_SET_1_G2_RX_SELMUFI_MASK;
15760ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_G2_SET_1_G2_RX_SELMUFI_OFFSET;
15770ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G2_SET_1_REG, data, mask);
15780ade8cd8SKonstantin Porotchkin 
15790ade8cd8SKonstantin Porotchkin 	/* DFE enable */
15800ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G2_DFE_RES_MASK;
15810ade8cd8SKonstantin Porotchkin 	data = 0x3 << HPIPE_G2_DFE_RES_OFFSET;
15820ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G2_SETTINGS_4_REG, data, mask);
15830ade8cd8SKonstantin Porotchkin 
15840ade8cd8SKonstantin Porotchkin 	/* Configure DFE Resolution */
15850ade8cd8SKonstantin Porotchkin 	mask = HPIPE_LANE_CFG4_DFE_EN_SEL_MASK;
15860ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_LANE_CFG4_DFE_EN_SEL_OFFSET;
15870ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LANE_CFG4_REG, data, mask);
15880ade8cd8SKonstantin Porotchkin 
15890ade8cd8SKonstantin Porotchkin 	/* VDD calibration control */
15900ade8cd8SKonstantin Porotchkin 	mask = HPIPE_EXT_SELLV_RXSAMPL_MASK;
15910ade8cd8SKonstantin Porotchkin 	data = 0x16 << HPIPE_EXT_SELLV_RXSAMPL_OFFSET;
15920ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_VDD_CAL_CTRL_REG, data, mask);
15930ade8cd8SKonstantin Porotchkin 
15940ade8cd8SKonstantin Porotchkin 	/* Set PLL Charge-pump Current Control */
15950ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G3_SETTING_5_G3_ICP_MASK;
15960ade8cd8SKonstantin Porotchkin 	data = 0x4 << HPIPE_G3_SETTING_5_G3_ICP_OFFSET;
15970ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G3_SETTING_5_REG, data, mask);
15980ade8cd8SKonstantin Porotchkin 
15990ade8cd8SKonstantin Porotchkin 	/* Set lane rqualization remote setting */
16000ade8cd8SKonstantin Porotchkin 	mask = HPIPE_LANE_CFG_FOM_DIRN_OVERRIDE_MASK;
16010ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_LANE_CFG_FOM_DIRN_OVERRIDE_OFFSET;
16020ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_LANE_CFG_FOM_ONLY_MODE_MASK;
16030ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_LANE_CFG_FOM_ONLY_MODE_OFFFSET;
16040ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_LANE_CFG_FOM_PRESET_VECTOR_MASK;
16050ade8cd8SKonstantin Porotchkin 	data |= 0x6 << HPIPE_LANE_CFG_FOM_PRESET_VECTOR_OFFSET;
16060ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LANE_EQ_REMOTE_SETTING_REG, data, mask);
16070ade8cd8SKonstantin Porotchkin 
16080ade8cd8SKonstantin Porotchkin 	mask = HPIPE_CFG_EQ_BUNDLE_DIS_MASK;
16090ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_CFG_EQ_BUNDLE_DIS_OFFSET;
16100ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LANE_EQ_CFG2_REG, data, mask);
16110ade8cd8SKonstantin Porotchkin 
16120ade8cd8SKonstantin Porotchkin 	debug("stage: Comphy power up\n");
16130ade8cd8SKonstantin Porotchkin 
16140ade8cd8SKonstantin Porotchkin 	/* For PCIe X4 or X2:
16150ade8cd8SKonstantin Porotchkin 	 * release from reset only after finish to configure all lanes
16160ade8cd8SKonstantin Porotchkin 	 */
16170ade8cd8SKonstantin Porotchkin 	if ((pcie_width == PCIE_LNK_X1) || (comphy_index == (pcie_width - 1))) {
16180ade8cd8SKonstantin Porotchkin 		uint32_t i, start_lane, end_lane;
16190ade8cd8SKonstantin Porotchkin 
16200ade8cd8SKonstantin Porotchkin 		if (pcie_width != PCIE_LNK_X1) {
16210ade8cd8SKonstantin Porotchkin 			/* allows writing to all lanes in one write */
16220ade8cd8SKonstantin Porotchkin 			data = 0x0;
16230ade8cd8SKonstantin Porotchkin 			if (pcie_width == PCIE_LNK_X2)
16240ade8cd8SKonstantin Porotchkin 				mask = COMMON_PHY_SD_CTRL1_COMPHY_0_1_PORT_MASK;
16250ade8cd8SKonstantin Porotchkin 			else if (pcie_width == PCIE_LNK_X4)
16260ade8cd8SKonstantin Porotchkin 				mask = COMMON_PHY_SD_CTRL1_COMPHY_0_3_PORT_MASK;
16270ade8cd8SKonstantin Porotchkin 			reg_set(comphy_base + COMMON_PHY_SD_CTRL1, data, mask);
16280ade8cd8SKonstantin Porotchkin 			start_lane = 0;
16290ade8cd8SKonstantin Porotchkin 			end_lane = pcie_width;
16300ade8cd8SKonstantin Porotchkin 
16310ade8cd8SKonstantin Porotchkin 			/* Release from PIPE soft reset
16320ade8cd8SKonstantin Porotchkin 			 * For PCIe by4 or by2:
16330ade8cd8SKonstantin Porotchkin 			 * release from soft reset all lanes - can't use
16340ade8cd8SKonstantin Porotchkin 			 * read modify write
16350ade8cd8SKonstantin Porotchkin 			 */
16360ade8cd8SKonstantin Porotchkin 			reg_set(HPIPE_ADDR(
16370ade8cd8SKonstantin Porotchkin 				COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base), 0) +
16380ade8cd8SKonstantin Porotchkin 				HPIPE_RST_CLK_CTRL_REG, 0x24, 0xffffffff);
16390ade8cd8SKonstantin Porotchkin 		} else {
16400ade8cd8SKonstantin Porotchkin 			start_lane = comphy_index;
16410ade8cd8SKonstantin Porotchkin 			end_lane = comphy_index + 1;
16420ade8cd8SKonstantin Porotchkin 
16430ade8cd8SKonstantin Porotchkin 			/* Release from PIPE soft reset
16440ade8cd8SKonstantin Porotchkin 			 * for PCIe by4 or by2:
16450ade8cd8SKonstantin Porotchkin 			 * release from soft reset all lanes
16460ade8cd8SKonstantin Porotchkin 			 */
16470ade8cd8SKonstantin Porotchkin 			reg_set(hpipe_addr + HPIPE_RST_CLK_CTRL_REG,
16480ade8cd8SKonstantin Porotchkin 				0x0 << HPIPE_RST_CLK_CTRL_PIPE_RST_OFFSET,
16490ade8cd8SKonstantin Porotchkin 				HPIPE_RST_CLK_CTRL_PIPE_RST_MASK);
16500ade8cd8SKonstantin Porotchkin 		}
16510ade8cd8SKonstantin Porotchkin 
16520ade8cd8SKonstantin Porotchkin 		if (pcie_width != PCIE_LNK_X1) {
16530ade8cd8SKonstantin Porotchkin 			/* disable writing to all lanes with one write */
16540ade8cd8SKonstantin Porotchkin 			if (pcie_width == PCIE_LNK_X2) {
16550ade8cd8SKonstantin Porotchkin 				data = (COMPHY_LANE0 <<
16560ade8cd8SKonstantin Porotchkin 				COMMON_PHY_SD_CTRL1_COMPHY_0_PORT_OFFSET) |
16570ade8cd8SKonstantin Porotchkin 				(COMPHY_LANE1 <<
16580ade8cd8SKonstantin Porotchkin 				COMMON_PHY_SD_CTRL1_COMPHY_1_PORT_OFFSET);
16590ade8cd8SKonstantin Porotchkin 				mask = COMMON_PHY_SD_CTRL1_COMPHY_0_1_PORT_MASK;
16600ade8cd8SKonstantin Porotchkin 			} else if (pcie_width == PCIE_LNK_X4) {
16610ade8cd8SKonstantin Porotchkin 				data = (COMPHY_LANE0 <<
16620ade8cd8SKonstantin Porotchkin 				COMMON_PHY_SD_CTRL1_COMPHY_0_PORT_OFFSET) |
16630ade8cd8SKonstantin Porotchkin 				(COMPHY_LANE1 <<
16640ade8cd8SKonstantin Porotchkin 				COMMON_PHY_SD_CTRL1_COMPHY_1_PORT_OFFSET) |
16650ade8cd8SKonstantin Porotchkin 				(COMPHY_LANE2 <<
16660ade8cd8SKonstantin Porotchkin 				COMMON_PHY_SD_CTRL1_COMPHY_2_PORT_OFFSET) |
16670ade8cd8SKonstantin Porotchkin 				(COMPHY_LANE3 <<
16680ade8cd8SKonstantin Porotchkin 				COMMON_PHY_SD_CTRL1_COMPHY_3_PORT_OFFSET);
16690ade8cd8SKonstantin Porotchkin 				mask = COMMON_PHY_SD_CTRL1_COMPHY_0_3_PORT_MASK;
16700ade8cd8SKonstantin Porotchkin 			}
16710ade8cd8SKonstantin Porotchkin 			reg_set(comphy_base + COMMON_PHY_SD_CTRL1,
16720ade8cd8SKonstantin Porotchkin 				data, mask);
16730ade8cd8SKonstantin Porotchkin 		}
16740ade8cd8SKonstantin Porotchkin 
16750ade8cd8SKonstantin Porotchkin 		debug("stage: Check PLL\n");
16760ade8cd8SKonstantin Porotchkin 		/* Read lane status */
16770ade8cd8SKonstantin Porotchkin 		for (i = start_lane; i < end_lane; i++) {
16780ade8cd8SKonstantin Porotchkin 			addr = HPIPE_ADDR(
16790ade8cd8SKonstantin Porotchkin 				COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base), i) +
16800ade8cd8SKonstantin Porotchkin 				HPIPE_LANE_STATUS1_REG;
16810ade8cd8SKonstantin Porotchkin 			data = HPIPE_LANE_STATUS1_PCLK_EN_MASK;
16820ade8cd8SKonstantin Porotchkin 			mask = data;
16830ade8cd8SKonstantin Porotchkin 			ret = polling_with_timeout(addr, data, mask,
16840ade8cd8SKonstantin Porotchkin 						   PLL_LOCK_TIMEOUT,
16850ade8cd8SKonstantin Porotchkin 						   REG_32BIT);
16860ade8cd8SKonstantin Porotchkin 			if (ret)
16870ade8cd8SKonstantin Porotchkin 				ERROR("Failed to lock PCIE PLL\n");
16880ade8cd8SKonstantin Porotchkin 		}
16890ade8cd8SKonstantin Porotchkin 	}
16900ade8cd8SKonstantin Porotchkin 
16910ade8cd8SKonstantin Porotchkin 	debug_exit();
16920ade8cd8SKonstantin Porotchkin 
16930ade8cd8SKonstantin Porotchkin 	return ret;
16940ade8cd8SKonstantin Porotchkin }
16950ade8cd8SKonstantin Porotchkin 
16960ade8cd8SKonstantin Porotchkin static int mvebu_cp110_comphy_rxaui_power_on(uint64_t comphy_base,
16970ade8cd8SKonstantin Porotchkin 				     uint8_t comphy_index, uint32_t comphy_mode)
16980ade8cd8SKonstantin Porotchkin {
16990ade8cd8SKonstantin Porotchkin 	uintptr_t hpipe_addr, sd_ip_addr, comphy_addr, addr;
17000ade8cd8SKonstantin Porotchkin 	uint32_t mask, data;
17010ade8cd8SKonstantin Porotchkin 	int ret = 0;
17020ade8cd8SKonstantin Porotchkin 
17030ade8cd8SKonstantin Porotchkin 	debug_enter();
17040ade8cd8SKonstantin Porotchkin 
17050ade8cd8SKonstantin Porotchkin 	hpipe_addr = HPIPE_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
17060ade8cd8SKonstantin Porotchkin 				comphy_index);
17070ade8cd8SKonstantin Porotchkin 	comphy_addr = COMPHY_ADDR(comphy_base, comphy_index);
17080ade8cd8SKonstantin Porotchkin 	sd_ip_addr = SD_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
17090ade8cd8SKonstantin Porotchkin 			     comphy_index);
17100ade8cd8SKonstantin Porotchkin 
17110ade8cd8SKonstantin Porotchkin 	/* configure phy selector for RXAUI */
17120ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_set_phy_selector(comphy_base, comphy_index,
17130ade8cd8SKonstantin Porotchkin 					    comphy_mode);
17140ade8cd8SKonstantin Porotchkin 
17150ade8cd8SKonstantin Porotchkin 	/* RFU configurations - hard reset comphy */
17160ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_UP_MASK;
17170ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_UP_OFFSET;
17180ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PIPE_SELECT_MASK;
17190ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_PIPE_SELECT_OFFSET;
17200ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
17210ade8cd8SKonstantin Porotchkin 
17220ade8cd8SKonstantin Porotchkin 	if (comphy_index == 2) {
17230ade8cd8SKonstantin Porotchkin 		reg_set(comphy_base + COMMON_PHY_SD_CTRL1,
17240ade8cd8SKonstantin Porotchkin 			0x1 << COMMON_PHY_SD_CTRL1_RXAUI0_OFFSET,
17250ade8cd8SKonstantin Porotchkin 			COMMON_PHY_SD_CTRL1_RXAUI0_MASK);
17260ade8cd8SKonstantin Porotchkin 	}
17270ade8cd8SKonstantin Porotchkin 	if (comphy_index == 4) {
17280ade8cd8SKonstantin Porotchkin 		reg_set(comphy_base + COMMON_PHY_SD_CTRL1,
17290ade8cd8SKonstantin Porotchkin 			0x1 << COMMON_PHY_SD_CTRL1_RXAUI1_OFFSET,
17300ade8cd8SKonstantin Porotchkin 			COMMON_PHY_SD_CTRL1_RXAUI1_MASK);
17310ade8cd8SKonstantin Porotchkin 	}
17320ade8cd8SKonstantin Porotchkin 
17330ade8cd8SKonstantin Porotchkin 	/* Select Baud Rate of Comphy And PD_PLL/Tx/Rx */
17340ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG0_SD_PU_PLL_MASK;
17350ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG0_SD_PU_PLL_OFFSET;
17360ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PHY_GEN_RX_MASK;
17370ade8cd8SKonstantin Porotchkin 	data |= 0xB << SD_EXTERNAL_CONFIG0_SD_PHY_GEN_RX_OFFSET;
17380ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PHY_GEN_TX_MASK;
17390ade8cd8SKonstantin Porotchkin 	data |= 0xB << SD_EXTERNAL_CONFIG0_SD_PHY_GEN_TX_OFFSET;
17400ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_RX_MASK;
17410ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG0_SD_PU_RX_OFFSET;
17420ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_TX_MASK;
17430ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG0_SD_PU_TX_OFFSET;
17440ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_HALF_BUS_MODE_MASK;
17450ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG0_HALF_BUS_MODE_OFFSET;
17460ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_MEDIA_MODE_MASK;
17470ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG0_MEDIA_MODE_OFFSET;
17480ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG0_REG, data, mask);
17490ade8cd8SKonstantin Porotchkin 
17500ade8cd8SKonstantin Porotchkin 	/* release from hard reset */
17510ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RESET_IN_MASK;
17520ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG1_RESET_IN_OFFSET;
17530ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RESET_CORE_MASK;
17540ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG1_RESET_CORE_OFFSET;
17550ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RF_RESET_IN_MASK;
17560ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG1_RF_RESET_IN_OFFSET;
17570ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
17580ade8cd8SKonstantin Porotchkin 
17590ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RESET_IN_MASK;
17600ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG1_RESET_IN_OFFSET;
17610ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RESET_CORE_MASK;
17620ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG1_RESET_CORE_OFFSET;
17630ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
17640ade8cd8SKonstantin Porotchkin 
17650ade8cd8SKonstantin Porotchkin 	/* Wait 1ms - until band gap and ref clock ready */
17660ade8cd8SKonstantin Porotchkin 	mdelay(1);
17670ade8cd8SKonstantin Porotchkin 
17680ade8cd8SKonstantin Porotchkin 	/* Start comphy Configuration */
17690ade8cd8SKonstantin Porotchkin 	debug("stage: Comphy configuration\n");
17700ade8cd8SKonstantin Porotchkin 	/* set reference clock */
17710ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_MISC_REG,
17720ade8cd8SKonstantin Porotchkin 		0x0 << HPIPE_MISC_REFCLK_SEL_OFFSET,
17730ade8cd8SKonstantin Porotchkin 		HPIPE_MISC_REFCLK_SEL_MASK);
17740ade8cd8SKonstantin Porotchkin 	/* Power and PLL Control */
17750ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_PLL_REF_FREQ_MASK;
17760ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_PWR_PLL_REF_FREQ_OFFSET;
17770ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_PLL_PHY_MODE_MASK;
17780ade8cd8SKonstantin Porotchkin 	data |= 0x4 << HPIPE_PWR_PLL_PHY_MODE_OFFSET;
17790ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_PLL_REG, data, mask);
17800ade8cd8SKonstantin Porotchkin 	/* Loopback register */
17810ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LOOPBACK_REG,
17820ade8cd8SKonstantin Porotchkin 		0x1 << HPIPE_LOOPBACK_SEL_OFFSET, HPIPE_LOOPBACK_SEL_MASK);
17830ade8cd8SKonstantin Porotchkin 	/* rx control 1 */
17840ade8cd8SKonstantin Porotchkin 	mask = HPIPE_RX_CONTROL_1_RXCLK2X_SEL_MASK;
17850ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_RX_CONTROL_1_RXCLK2X_SEL_OFFSET;
17860ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RX_CONTROL_1_CLK8T_EN_MASK;
17870ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_RX_CONTROL_1_CLK8T_EN_OFFSET;
17880ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_RX_CONTROL_1_REG, data, mask);
17890ade8cd8SKonstantin Porotchkin 	/* DTL Control */
17900ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_CTR_DTL_REG,
17910ade8cd8SKonstantin Porotchkin 		0x0 << HPIPE_PWR_CTR_DTL_FLOOP_EN_OFFSET,
17920ade8cd8SKonstantin Porotchkin 		HPIPE_PWR_CTR_DTL_FLOOP_EN_MASK);
17930ade8cd8SKonstantin Porotchkin 
17940ade8cd8SKonstantin Porotchkin 	/* Set analog parameters from ETP(HW) */
17950ade8cd8SKonstantin Porotchkin 	debug("stage: Analog parameters from ETP(HW)\n");
17960ade8cd8SKonstantin Porotchkin 	/* SERDES External Configuration 2 */
17970ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG2_REG,
17980ade8cd8SKonstantin Porotchkin 		0x1 << SD_EXTERNAL_CONFIG2_PIN_DFE_EN_OFFSET,
17990ade8cd8SKonstantin Porotchkin 		SD_EXTERNAL_CONFIG2_PIN_DFE_EN_MASK);
18000ade8cd8SKonstantin Porotchkin 	/* 0x7-DFE Resolution control */
18010ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_REG0, 0x1 << HPIPE_DFE_RES_FORCE_OFFSET,
18020ade8cd8SKonstantin Porotchkin 		HPIPE_DFE_RES_FORCE_MASK);
18030ade8cd8SKonstantin Porotchkin 	/* 0xd-G1_Setting_0 */
18040ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SET_0_REG,
18050ade8cd8SKonstantin Porotchkin 		0xd << HPIPE_G1_SET_0_G1_TX_EMPH1_OFFSET,
18060ade8cd8SKonstantin Porotchkin 		HPIPE_G1_SET_0_G1_TX_EMPH1_MASK);
18070ade8cd8SKonstantin Porotchkin 	/* 0xE-G1_Setting_1 */
18080ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SET_1_G1_RX_SELMUPI_MASK;
18090ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_G1_SET_1_G1_RX_SELMUPI_OFFSET;
18103c0024ccSGrzegorz Jaszczyk 	mask |= HPIPE_G1_SET_1_G1_RX_SELMUPF_MASK;
18113c0024ccSGrzegorz Jaszczyk 	data |= 0x1 << HPIPE_G1_SET_1_G1_RX_SELMUPF_OFFSET;
18120ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_G1_SET_1_G1_RX_DFE_EN_MASK;
18130ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_G1_SET_1_G1_RX_DFE_EN_OFFSET;
18140ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SET_1_REG, data, mask);
18150ade8cd8SKonstantin Porotchkin 	/* 0xA-DFE_Reg3 */
18160ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_F3_F5_DFE_EN_MASK;
18170ade8cd8SKonstantin Porotchkin 	data = 0x0 << HPIPE_DFE_F3_F5_DFE_EN_OFFSET;
18180ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_DFE_F3_F5_DFE_CTRL_MASK;
18190ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_DFE_F3_F5_DFE_CTRL_OFFSET;
18200ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_F3_F5_REG, data, mask);
18210ade8cd8SKonstantin Porotchkin 
18220ade8cd8SKonstantin Porotchkin 	/* 0x111-G1_Setting_4 */
18230ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G1_SETTINGS_4_G1_DFE_RES_MASK;
18240ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_G1_SETTINGS_4_G1_DFE_RES_OFFSET;
18250ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G1_SETTINGS_4_REG, data, mask);
18260ade8cd8SKonstantin Porotchkin 
18270ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations- Power Up PLL,Tx,Rx\n");
18280ade8cd8SKonstantin Porotchkin 	/* SERDES External Configuration */
18290ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG0_SD_PU_PLL_MASK;
18300ade8cd8SKonstantin Porotchkin 	data = 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_PLL_OFFSET;
18310ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_RX_MASK;
18320ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_RX_OFFSET;
18330ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG0_SD_PU_TX_MASK;
18340ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG0_SD_PU_TX_OFFSET;
18350ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG0_REG, data, mask);
18360ade8cd8SKonstantin Porotchkin 
18370ade8cd8SKonstantin Porotchkin 
18380ade8cd8SKonstantin Porotchkin 	/* check PLL rx & tx ready */
18390ade8cd8SKonstantin Porotchkin 	addr = sd_ip_addr + SD_EXTERNAL_STATUS0_REG;
18400ade8cd8SKonstantin Porotchkin 	data = SD_EXTERNAL_STATUS0_PLL_RX_MASK |
18410ade8cd8SKonstantin Porotchkin 		SD_EXTERNAL_STATUS0_PLL_TX_MASK;
18420ade8cd8SKonstantin Porotchkin 	mask = data;
18430ade8cd8SKonstantin Porotchkin 	data = polling_with_timeout(addr, data, mask, 15000, REG_32BIT);
18440ade8cd8SKonstantin Porotchkin 	if (data != 0) {
18450ade8cd8SKonstantin Porotchkin 		debug("Read from reg = %lx - value = 0x%x\n",
18460ade8cd8SKonstantin Porotchkin 		      sd_ip_addr + SD_EXTERNAL_STATUS0_REG, data);
18470ade8cd8SKonstantin Porotchkin 		ERROR("SD_EXTERNAL_STATUS0_PLL_RX is %d, -\"-_PLL_TX is %d\n",
18480ade8cd8SKonstantin Porotchkin 		      (data & SD_EXTERNAL_STATUS0_PLL_RX_MASK),
18490ade8cd8SKonstantin Porotchkin 		      (data & SD_EXTERNAL_STATUS0_PLL_TX_MASK));
18500ade8cd8SKonstantin Porotchkin 		ret = -ETIMEDOUT;
18510ade8cd8SKonstantin Porotchkin 	}
18520ade8cd8SKonstantin Porotchkin 
18530ade8cd8SKonstantin Porotchkin 	/* RX init */
18540ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG,
18550ade8cd8SKonstantin Porotchkin 		0x1 << SD_EXTERNAL_CONFIG1_RX_INIT_OFFSET,
18560ade8cd8SKonstantin Porotchkin 		SD_EXTERNAL_CONFIG1_RX_INIT_MASK);
18570ade8cd8SKonstantin Porotchkin 
18580ade8cd8SKonstantin Porotchkin 	/* check that RX init done */
18590ade8cd8SKonstantin Porotchkin 	addr = sd_ip_addr + SD_EXTERNAL_STATUS0_REG;
18600ade8cd8SKonstantin Porotchkin 	data = SD_EXTERNAL_STATUS0_RX_INIT_MASK;
18610ade8cd8SKonstantin Porotchkin 	mask = data;
18620ade8cd8SKonstantin Porotchkin 	data = polling_with_timeout(addr, data, mask, 100, REG_32BIT);
18630ade8cd8SKonstantin Porotchkin 	if (data != 0) {
18640ade8cd8SKonstantin Porotchkin 		debug("Read from reg = %lx - value = 0x%x\n",
18650ade8cd8SKonstantin Porotchkin 		      sd_ip_addr + SD_EXTERNAL_STATUS0_REG, data);
18660ade8cd8SKonstantin Porotchkin 		ERROR("SD_EXTERNAL_STATUS0_RX_INIT is 0\n");
18670ade8cd8SKonstantin Porotchkin 		ret = -ETIMEDOUT;
18680ade8cd8SKonstantin Porotchkin 	}
18690ade8cd8SKonstantin Porotchkin 
18700ade8cd8SKonstantin Porotchkin 	debug("stage: RF Reset\n");
18710ade8cd8SKonstantin Porotchkin 	/* RF Reset */
18720ade8cd8SKonstantin Porotchkin 	mask =  SD_EXTERNAL_CONFIG1_RX_INIT_MASK;
18730ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG1_RX_INIT_OFFSET;
18740ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RF_RESET_IN_MASK;
18750ade8cd8SKonstantin Porotchkin 	data |= 0x1 << SD_EXTERNAL_CONFIG1_RF_RESET_IN_OFFSET;
18760ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
18770ade8cd8SKonstantin Porotchkin 
18780ade8cd8SKonstantin Porotchkin 	debug_exit();
18790ade8cd8SKonstantin Porotchkin 
18800ade8cd8SKonstantin Porotchkin 	return ret;
18810ade8cd8SKonstantin Porotchkin }
18820ade8cd8SKonstantin Porotchkin 
18830ade8cd8SKonstantin Porotchkin static int mvebu_cp110_comphy_usb3_power_on(uint64_t comphy_base,
18840ade8cd8SKonstantin Porotchkin 				     uint8_t comphy_index, uint32_t comphy_mode)
18850ade8cd8SKonstantin Porotchkin {
18860ade8cd8SKonstantin Porotchkin 	uintptr_t hpipe_addr, comphy_addr, addr;
18870ade8cd8SKonstantin Porotchkin 	uint32_t mask, data;
18880ade8cd8SKonstantin Porotchkin 	int ret = 0;
18890ade8cd8SKonstantin Porotchkin 
18900ade8cd8SKonstantin Porotchkin 	debug_enter();
18910ade8cd8SKonstantin Porotchkin 
18920ade8cd8SKonstantin Porotchkin 	/* Configure PIPE selector for USB3 */
18930ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_set_pipe_selector(comphy_base, comphy_index,
18940ade8cd8SKonstantin Porotchkin 					     comphy_mode);
18950ade8cd8SKonstantin Porotchkin 
18960ade8cd8SKonstantin Porotchkin 	hpipe_addr = HPIPE_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
18970ade8cd8SKonstantin Porotchkin 				comphy_index);
18980ade8cd8SKonstantin Porotchkin 	comphy_addr = COMPHY_ADDR(comphy_base, comphy_index);
18990ade8cd8SKonstantin Porotchkin 
19000ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations - hard reset comphy\n");
19010ade8cd8SKonstantin Porotchkin 	/* RFU configurations - hard reset comphy */
19020ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_UP_MASK;
19030ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_UP_OFFSET;
19040ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PIPE_SELECT_MASK;
19050ade8cd8SKonstantin Porotchkin 	data |= 0x1 << COMMON_PHY_CFG1_PIPE_SELECT_OFFSET;
19060ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PWR_ON_RESET_MASK;
19070ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_PWR_ON_RESET_OFFSET;
19080ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_CORE_RSTN_MASK;
19090ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_CORE_RSTN_OFFSET;
19100ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_PHY_MODE_MASK;
19110ade8cd8SKonstantin Porotchkin 	data |= 0x1 << COMMON_PHY_PHY_MODE_OFFSET;
19120ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
19130ade8cd8SKonstantin Porotchkin 
19140ade8cd8SKonstantin Porotchkin 	/* release from hard reset */
19150ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_ON_RESET_MASK;
19160ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_ON_RESET_OFFSET;
19170ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_CORE_RSTN_MASK;
19180ade8cd8SKonstantin Porotchkin 	data |= 0x1 << COMMON_PHY_CFG1_CORE_RSTN_OFFSET;
19190ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
19200ade8cd8SKonstantin Porotchkin 
19210ade8cd8SKonstantin Porotchkin 	/* Wait 1ms - until band gap and ref clock ready */
19220ade8cd8SKonstantin Porotchkin 	mdelay(1);
19230ade8cd8SKonstantin Porotchkin 
19240ade8cd8SKonstantin Porotchkin 	/* Start comphy Configuration */
19250ade8cd8SKonstantin Porotchkin 	debug("stage: Comphy configuration\n");
19260ade8cd8SKonstantin Porotchkin 	/* Set PIPE soft reset */
19270ade8cd8SKonstantin Porotchkin 	mask = HPIPE_RST_CLK_CTRL_PIPE_RST_MASK;
19280ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_RST_CLK_CTRL_PIPE_RST_OFFSET;
19290ade8cd8SKonstantin Porotchkin 	/* Set PHY datapath width mode for V0 */
19300ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RST_CLK_CTRL_FIXED_PCLK_MASK;
19310ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_RST_CLK_CTRL_FIXED_PCLK_OFFSET;
19320ade8cd8SKonstantin Porotchkin 	/* Set Data bus width USB mode for V0 */
19330ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RST_CLK_CTRL_PIPE_WIDTH_MASK;
19340ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_RST_CLK_CTRL_PIPE_WIDTH_OFFSET;
19350ade8cd8SKonstantin Porotchkin 	/* Set CORE_CLK output frequency for 250Mhz */
19360ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_RST_CLK_CTRL_CORE_FREQ_SEL_MASK;
19370ade8cd8SKonstantin Porotchkin 	data |= 0x0 << HPIPE_RST_CLK_CTRL_CORE_FREQ_SEL_OFFSET;
19380ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_RST_CLK_CTRL_REG, data, mask);
19390ade8cd8SKonstantin Porotchkin 	/* Set PLL ready delay for 0x2 */
19400ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_CLK_SRC_LO_REG,
19410ade8cd8SKonstantin Porotchkin 		0x2 << HPIPE_CLK_SRC_LO_PLL_RDY_DL_OFFSET,
19420ade8cd8SKonstantin Porotchkin 		HPIPE_CLK_SRC_LO_PLL_RDY_DL_MASK);
19430ade8cd8SKonstantin Porotchkin 	/* Set reference clock to come from group 1 - 25Mhz */
19440ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_MISC_REG,
19450ade8cd8SKonstantin Porotchkin 		0x0 << HPIPE_MISC_REFCLK_SEL_OFFSET,
19460ade8cd8SKonstantin Porotchkin 		HPIPE_MISC_REFCLK_SEL_MASK);
19470ade8cd8SKonstantin Porotchkin 	/* Set reference frequcency select - 0x2 */
19480ade8cd8SKonstantin Porotchkin 	mask = HPIPE_PWR_PLL_REF_FREQ_MASK;
19490ade8cd8SKonstantin Porotchkin 	data = 0x2 << HPIPE_PWR_PLL_REF_FREQ_OFFSET;
19500ade8cd8SKonstantin Porotchkin 	/* Set PHY mode to USB - 0x5 */
19510ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_PWR_PLL_PHY_MODE_MASK;
19520ade8cd8SKonstantin Porotchkin 	data |= 0x5 << HPIPE_PWR_PLL_PHY_MODE_OFFSET;
19530ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_PWR_PLL_REG, data, mask);
19540ade8cd8SKonstantin Porotchkin 	/* Set the amount of time spent in the LoZ state - set for 0x7 */
19550ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_GLOBAL_PM_CTRL,
19560ade8cd8SKonstantin Porotchkin 		0x7 << HPIPE_GLOBAL_PM_RXDLOZ_WAIT_OFFSET,
19570ade8cd8SKonstantin Porotchkin 		HPIPE_GLOBAL_PM_RXDLOZ_WAIT_MASK);
19580ade8cd8SKonstantin Porotchkin 	/* Set max PHY generation setting - 5Gbps */
19590ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_INTERFACE_REG,
19600ade8cd8SKonstantin Porotchkin 		0x1 << HPIPE_INTERFACE_GEN_MAX_OFFSET,
19610ade8cd8SKonstantin Porotchkin 		HPIPE_INTERFACE_GEN_MAX_MASK);
19620ade8cd8SKonstantin Porotchkin 	/* Set select data width 20Bit (SEL_BITS[2:0]) */
19630ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LOOPBACK_REG,
19640ade8cd8SKonstantin Porotchkin 		0x1 << HPIPE_LOOPBACK_SEL_OFFSET,
19650ade8cd8SKonstantin Porotchkin 		HPIPE_LOOPBACK_SEL_MASK);
19660ade8cd8SKonstantin Porotchkin 	/* select de-emphasize 3.5db */
19670ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LANE_CONFIG0_REG,
19680ade8cd8SKonstantin Porotchkin 		0x1 << HPIPE_LANE_CONFIG0_TXDEEMPH0_OFFSET,
19690ade8cd8SKonstantin Porotchkin 		HPIPE_LANE_CONFIG0_TXDEEMPH0_MASK);
19700ade8cd8SKonstantin Porotchkin 	/* override tx margining from the MAC */
19710ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_TST_MODE_CTRL_REG,
19720ade8cd8SKonstantin Porotchkin 		0x1 << HPIPE_TST_MODE_CTRL_MODE_MARGIN_OFFSET,
19730ade8cd8SKonstantin Porotchkin 		HPIPE_TST_MODE_CTRL_MODE_MARGIN_MASK);
19740ade8cd8SKonstantin Porotchkin 
19750ade8cd8SKonstantin Porotchkin 	/* Start analog parameters from ETP(HW) */
19760ade8cd8SKonstantin Porotchkin 	debug("stage: Analog parameters from ETP(HW)\n");
19770ade8cd8SKonstantin Porotchkin 	/* Set Pin DFE_PAT_DIS -> Bit[1]: PIN_DFE_PAT_DIS = 0x0 */
19780ade8cd8SKonstantin Porotchkin 	mask = HPIPE_LANE_CFG4_DFE_CTRL_MASK;
19790ade8cd8SKonstantin Porotchkin 	data = 0x1 << HPIPE_LANE_CFG4_DFE_CTRL_OFFSET;
19800ade8cd8SKonstantin Porotchkin 	/* Set Override PHY DFE control pins for 0x1 */
19810ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_LANE_CFG4_DFE_OVER_MASK;
19820ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_LANE_CFG4_DFE_OVER_OFFSET;
19830ade8cd8SKonstantin Porotchkin 	/* Set Spread Spectrum Clock Enable fot 0x1 */
19840ade8cd8SKonstantin Porotchkin 	mask |= HPIPE_LANE_CFG4_SSC_CTRL_MASK;
19850ade8cd8SKonstantin Porotchkin 	data |= 0x1 << HPIPE_LANE_CFG4_SSC_CTRL_OFFSET;
19860ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_LANE_CFG4_REG, data, mask);
19870ade8cd8SKonstantin Porotchkin 	/* Confifure SSC amplitude */
19880ade8cd8SKonstantin Porotchkin 	mask = HPIPE_G2_TX_SSC_AMP_MASK;
19890ade8cd8SKonstantin Porotchkin 	data = 0x1f << HPIPE_G2_TX_SSC_AMP_OFFSET;
19900ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_G2_SET_2_REG, data, mask);
19910ade8cd8SKonstantin Porotchkin 	/* End of analog parameters */
19920ade8cd8SKonstantin Porotchkin 
19930ade8cd8SKonstantin Porotchkin 	debug("stage: Comphy power up\n");
19940ade8cd8SKonstantin Porotchkin 	/* Release from PIPE soft reset */
19950ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_RST_CLK_CTRL_REG,
19960ade8cd8SKonstantin Porotchkin 		0x0 << HPIPE_RST_CLK_CTRL_PIPE_RST_OFFSET,
19970ade8cd8SKonstantin Porotchkin 		HPIPE_RST_CLK_CTRL_PIPE_RST_MASK);
19980ade8cd8SKonstantin Porotchkin 
19990ade8cd8SKonstantin Porotchkin 	/* wait 15ms - for comphy calibration done */
20000ade8cd8SKonstantin Porotchkin 	debug("stage: Check PLL\n");
20010ade8cd8SKonstantin Porotchkin 	/* Read lane status */
20020ade8cd8SKonstantin Porotchkin 	addr = hpipe_addr + HPIPE_LANE_STATUS1_REG;
20030ade8cd8SKonstantin Porotchkin 	data = HPIPE_LANE_STATUS1_PCLK_EN_MASK;
20040ade8cd8SKonstantin Porotchkin 	mask = data;
20050ade8cd8SKonstantin Porotchkin 	data = polling_with_timeout(addr, data, mask, 15000, REG_32BIT);
20060ade8cd8SKonstantin Porotchkin 	if (data != 0) {
20070ade8cd8SKonstantin Porotchkin 		debug("Read from reg = %lx - value = 0x%x\n",
20080ade8cd8SKonstantin Porotchkin 			hpipe_addr + HPIPE_LANE_STATUS1_REG, data);
20090ade8cd8SKonstantin Porotchkin 		ERROR("HPIPE_LANE_STATUS1_PCLK_EN_MASK is 0\n");
20100ade8cd8SKonstantin Porotchkin 		ret = -ETIMEDOUT;
20110ade8cd8SKonstantin Porotchkin 	}
20120ade8cd8SKonstantin Porotchkin 
20130ade8cd8SKonstantin Porotchkin 	debug_exit();
20140ade8cd8SKonstantin Porotchkin 
20150ade8cd8SKonstantin Porotchkin 	return ret;
20160ade8cd8SKonstantin Porotchkin }
20170ade8cd8SKonstantin Porotchkin 
20188e8ec8cfSGrzegorz Jaszczyk static void rx_pre_train(uint64_t comphy_base, uint8_t comphy_index)
20198e8ec8cfSGrzegorz Jaszczyk {
20208e8ec8cfSGrzegorz Jaszczyk 	uintptr_t hpipe_addr;
20218e8ec8cfSGrzegorz Jaszczyk 	uint32_t mask, data;
20228e8ec8cfSGrzegorz Jaszczyk 
20238e8ec8cfSGrzegorz Jaszczyk 	hpipe_addr = HPIPE_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
20248e8ec8cfSGrzegorz Jaszczyk 				comphy_index);
20258e8ec8cfSGrzegorz Jaszczyk 
20268e8ec8cfSGrzegorz Jaszczyk 	debug("rx_training preparation\n\n");
20278e8ec8cfSGrzegorz Jaszczyk 
20288e8ec8cfSGrzegorz Jaszczyk 	mask = HPIPE_TRX0_GAIN_TRAIN_WITH_C_MASK;
20298e8ec8cfSGrzegorz Jaszczyk 	data = (0x1 << HPIPE_TRX0_GAIN_TRAIN_WITH_C_OFF);
20308e8ec8cfSGrzegorz Jaszczyk 	mask |= HPIPE_TRX0_GAIN_TRAIN_WITH_SAMPLER_MASK;
20318e8ec8cfSGrzegorz Jaszczyk 	data |= (0x0 << HPIPE_TRX0_GAIN_TRAIN_WITH_SAMPLER_OFF);
20328e8ec8cfSGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_TRX0_REG, data, mask);
20338e8ec8cfSGrzegorz Jaszczyk 
20348e8ec8cfSGrzegorz Jaszczyk 
20358e8ec8cfSGrzegorz Jaszczyk 	mask = HPIPE_TRX_REG2_SUMF_BOOST_TARGET_C_MASK;
20368e8ec8cfSGrzegorz Jaszczyk 	data = (0x1e << HPIPE_TRX_REG2_SUMF_BOOST_TARGET_C_OFF);
20378e8ec8cfSGrzegorz Jaszczyk 	mask |= HPIPE_TRX_REG2_SUMF_BOOST_TARGET_K_MASK;
20388e8ec8cfSGrzegorz Jaszczyk 	data |= (0x0 << HPIPE_TRX_REG2_SUMF_BOOST_TARGET_K_OFF);
20398e8ec8cfSGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_TRX_REG2, data, mask);
20408e8ec8cfSGrzegorz Jaszczyk 
20418e8ec8cfSGrzegorz Jaszczyk 	mask = HPIPE_TRX_REG1_MIN_BOOST_MODE_MASK;
20428e8ec8cfSGrzegorz Jaszczyk 	data = (0x1 << HPIPE_TRX_REG1_MIN_BOOST_MODE_OFF);
20438e8ec8cfSGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_TRX_REG1, data, mask);
20448e8ec8cfSGrzegorz Jaszczyk 
20458e8ec8cfSGrzegorz Jaszczyk 	mask = HPIPE_CRD2_CRD_MIDPOINT_SMALL_THRES_K_MASK;
20468e8ec8cfSGrzegorz Jaszczyk 	data = (0x8 << HPIPE_CRD2_CRD_MIDPOINT_SMALL_THRES_K_OFF);
20478e8ec8cfSGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_CDR_CONTROL1_REG, data, mask);
20488e8ec8cfSGrzegorz Jaszczyk 
20498e8ec8cfSGrzegorz Jaszczyk 	mask = HPIPE_CRD2_CRD_MIDPOINT_LARGE_THRES_K_MASK;
20508e8ec8cfSGrzegorz Jaszczyk 	data = (0x8 << HPIPE_CRD2_CRD_MIDPOINT_LARGE_THRES_K_OFF);
20518e8ec8cfSGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_CDR_CONTROL2_REG, data, mask);
20528e8ec8cfSGrzegorz Jaszczyk 
20538e8ec8cfSGrzegorz Jaszczyk 	mask = HPIPE_CRD_MIDPOINT_PHASE_OS_MASK;
20548e8ec8cfSGrzegorz Jaszczyk 	data = (0x0 << HPIPE_CRD_MIDPOINT_PHASE_OS_OFFSET);
20558e8ec8cfSGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_CDR_CONTROL_REG, data, mask);
20568e8ec8cfSGrzegorz Jaszczyk 
20578e8ec8cfSGrzegorz Jaszczyk 	mask = HPIPE_TRX_REG1_SUMFTAP_EN_MASK;
20588e8ec8cfSGrzegorz Jaszczyk 	data = (0x38 << HPIPE_TRX_REG1_SUMFTAP_EN_OFF);
20598e8ec8cfSGrzegorz Jaszczyk 	mask |= HPIPE_TRX_REG2_SUMF_BOOST_TARGET_C_MASK;
20608e8ec8cfSGrzegorz Jaszczyk 	data |= (0x1e << HPIPE_TRX_REG2_SUMF_BOOST_TARGET_C_OFF);
20618e8ec8cfSGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_TRX_REG1, data, mask);
20628e8ec8cfSGrzegorz Jaszczyk }
20638e8ec8cfSGrzegorz Jaszczyk 
206442a29337SGrzegorz Jaszczyk int mvebu_cp110_comphy_xfi_rx_training(uint64_t comphy_base,
206542a29337SGrzegorz Jaszczyk 					      uint8_t comphy_index)
20660ade8cd8SKonstantin Porotchkin {
20670ade8cd8SKonstantin Porotchkin 	uint32_t mask, data, timeout;
206842a29337SGrzegorz Jaszczyk 	uint32_t g1_ffe_cap_sel, g1_ffe_res_sel, align90, g1_dfe_res;
20698e8ec8cfSGrzegorz Jaszczyk 	uintptr_t hpipe_addr;
20700ade8cd8SKonstantin Porotchkin 
207142a29337SGrzegorz Jaszczyk 	uint8_t ap_nr, cp_nr;
207242a29337SGrzegorz Jaszczyk 
207342a29337SGrzegorz Jaszczyk 	mvebu_cp110_get_ap_and_cp_nr(&ap_nr, &cp_nr, comphy_base);
207442a29337SGrzegorz Jaszczyk 
20750ade8cd8SKonstantin Porotchkin 	hpipe_addr = HPIPE_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
20760ade8cd8SKonstantin Porotchkin 				comphy_index);
20770ade8cd8SKonstantin Porotchkin 
207842a29337SGrzegorz Jaszczyk 	debug_enter();
207942a29337SGrzegorz Jaszczyk 
20808e8ec8cfSGrzegorz Jaszczyk 	rx_pre_train(comphy_base, comphy_index);
208142a29337SGrzegorz Jaszczyk 
208242a29337SGrzegorz Jaszczyk 	debug("Preparation for rx_training\n\n");
208342a29337SGrzegorz Jaszczyk 
208442a29337SGrzegorz Jaszczyk 	/* Use the FFE table */
208542a29337SGrzegorz Jaszczyk 	mask = HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_MASK;
208642a29337SGrzegorz Jaszczyk 	data = 0 << HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_OFFSET;
208742a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_G1_SETTINGS_3_REG, data, mask);
208842a29337SGrzegorz Jaszczyk 
208942a29337SGrzegorz Jaszczyk 	/* Use auto-calibration value */
209042a29337SGrzegorz Jaszczyk 	mask = HPIPE_CAL_RXCLKALIGN_90_EXT_EN_MASK;
209142a29337SGrzegorz Jaszczyk 	data = 0 << HPIPE_CAL_RXCLKALIGN_90_EXT_EN_OFFSET;
209242a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_RX_CLK_ALIGN90_AND_TX_IDLE_CALIB_CTRL_REG,
209342a29337SGrzegorz Jaszczyk 		data, mask);
209442a29337SGrzegorz Jaszczyk 
209542a29337SGrzegorz Jaszczyk 	/* Use Tx/Rx training results */
209642a29337SGrzegorz Jaszczyk 	mask = HPIPE_DFE_RES_FORCE_MASK;
209742a29337SGrzegorz Jaszczyk 	data = 0 << HPIPE_DFE_RES_FORCE_OFFSET;
209842a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_DFE_REG0, data, mask);
209942a29337SGrzegorz Jaszczyk 
21008e8ec8cfSGrzegorz Jaszczyk 	debug("Enable RX training\n\n");
21010ade8cd8SKonstantin Porotchkin 
210242a29337SGrzegorz Jaszczyk 	mask = HPIPE_TRX_RX_TRAIN_EN_MASK;
210342a29337SGrzegorz Jaszczyk 	data = 0x1 << HPIPE_TRX_RX_TRAIN_EN_OFFSET;
210442a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_TRX_TRAIN_CTRL_0_REG, data, mask);
21050ade8cd8SKonstantin Porotchkin 
21060ade8cd8SKonstantin Porotchkin 	/* Check the result of RX training */
21070ade8cd8SKonstantin Porotchkin 	timeout = RX_TRAINING_TIMEOUT;
210842a29337SGrzegorz Jaszczyk 	mask = HPIPE_INTERRUPT_TRX_TRAIN_DONE_OFFSET |
210942a29337SGrzegorz Jaszczyk 		HPIPE_INTERRUPT_DFE_DONE_INT_OFFSET |
211042a29337SGrzegorz Jaszczyk 		HPIPE_INTERRUPT_RX_TRAIN_COMPLETE_INT_MASK;
21110ade8cd8SKonstantin Porotchkin 	while (timeout) {
211242a29337SGrzegorz Jaszczyk 		data = mmio_read_32(hpipe_addr + HPIPE_INTERRUPT_1_REGISTER);
211342a29337SGrzegorz Jaszczyk 		if (data & mask)
21140ade8cd8SKonstantin Porotchkin 			break;
21150ade8cd8SKonstantin Porotchkin 		mdelay(1);
21160ade8cd8SKonstantin Porotchkin 		timeout--;
21170ade8cd8SKonstantin Porotchkin 	}
21180ade8cd8SKonstantin Porotchkin 
211942a29337SGrzegorz Jaszczyk 	debug("RX training result: interrupt reg 0x%lx = 0x%x\n\n",
212042a29337SGrzegorz Jaszczyk 	       hpipe_addr + HPIPE_INTERRUPT_1_REGISTER, data);
212142a29337SGrzegorz Jaszczyk 
212242a29337SGrzegorz Jaszczyk 	if (timeout == 0 || data & HPIPE_TRX_TRAIN_TIME_OUT_INT_MASK) {
212342a29337SGrzegorz Jaszczyk 		ERROR("Rx training timeout...\n");
21240ade8cd8SKonstantin Porotchkin 		return -ETIMEDOUT;
21250ade8cd8SKonstantin Porotchkin 	}
21260ade8cd8SKonstantin Porotchkin 
212742a29337SGrzegorz Jaszczyk 	if (data & HPIPE_TRX_TRAIN_FAILED_MASK) {
212842a29337SGrzegorz Jaszczyk 		ERROR("Rx training failed...\n");
212942a29337SGrzegorz Jaszczyk 		return -EINVAL;
213042a29337SGrzegorz Jaszczyk 	}
213142a29337SGrzegorz Jaszczyk 
213242a29337SGrzegorz Jaszczyk 	mask = HPIPE_TRX_RX_TRAIN_EN_MASK;
213342a29337SGrzegorz Jaszczyk 	data = 0x0 << HPIPE_TRX_RX_TRAIN_EN_OFFSET;
213442a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_TRX_TRAIN_CTRL_0_REG, data, mask);
213542a29337SGrzegorz Jaszczyk 
213642a29337SGrzegorz Jaszczyk 	debug("Training done, reading results...\n\n");
213742a29337SGrzegorz Jaszczyk 
213842a29337SGrzegorz Jaszczyk 	mask = HPIPE_ADAPTED_FFE_ADAPTED_FFE_RES_MASK;
213942a29337SGrzegorz Jaszczyk 	g1_ffe_res_sel = ((mmio_read_32(hpipe_addr +
214042a29337SGrzegorz Jaszczyk 			   HPIPE_ADAPTED_FFE_CAPACITOR_COUNTER_CTRL_REG)
214142a29337SGrzegorz Jaszczyk 			   & mask) >> HPIPE_ADAPTED_FFE_ADAPTED_FFE_RES_OFFSET);
214242a29337SGrzegorz Jaszczyk 
214342a29337SGrzegorz Jaszczyk 	mask = HPIPE_ADAPTED_FFE_ADAPTED_FFE_CAP_MASK;
214442a29337SGrzegorz Jaszczyk 	g1_ffe_cap_sel = ((mmio_read_32(hpipe_addr +
214542a29337SGrzegorz Jaszczyk 			   HPIPE_ADAPTED_FFE_CAPACITOR_COUNTER_CTRL_REG)
214642a29337SGrzegorz Jaszczyk 			   & mask) >> HPIPE_ADAPTED_FFE_ADAPTED_FFE_CAP_OFFSET);
214742a29337SGrzegorz Jaszczyk 
214842a29337SGrzegorz Jaszczyk 	mask = HPIPE_DATA_PHASE_ADAPTED_OS_PH_MASK;
214942a29337SGrzegorz Jaszczyk 	align90 = ((mmio_read_32(hpipe_addr + HPIPE_DATA_PHASE_OFF_CTRL_REG)
215042a29337SGrzegorz Jaszczyk 		    & mask) >> HPIPE_DATA_PHASE_ADAPTED_OS_PH_OFFSET);
215142a29337SGrzegorz Jaszczyk 
215242a29337SGrzegorz Jaszczyk 	mask = HPIPE_ADAPTED_DFE_RES_MASK;
215342a29337SGrzegorz Jaszczyk 	g1_dfe_res = ((mmio_read_32(hpipe_addr +
215442a29337SGrzegorz Jaszczyk 		       HPIPE_ADAPTED_DFE_COEFFICIENT_1_REG)
215542a29337SGrzegorz Jaszczyk 		       & mask) >> HPIPE_ADAPTED_DFE_RES_OFFSET);
215642a29337SGrzegorz Jaszczyk 
215742a29337SGrzegorz Jaszczyk 	debug("================================================\n");
215842a29337SGrzegorz Jaszczyk 	debug("Switching to static configuration:\n");
215942a29337SGrzegorz Jaszczyk 	debug("FFE_RES = 0x%x FFE_CAP = 0x%x align90 = 0x%x g1_dfe_res 0x%x\n",
216042a29337SGrzegorz Jaszczyk 	       g1_ffe_res_sel, g1_ffe_cap_sel, align90, g1_dfe_res);
216142a29337SGrzegorz Jaszczyk 	debug("Result after training: 0x%lx= 0x%x, 0x%lx= 0x%x, 0x%lx = 0x%x\n",
216242a29337SGrzegorz Jaszczyk 	      (hpipe_addr + HPIPE_ADAPTED_FFE_CAPACITOR_COUNTER_CTRL_REG),
216342a29337SGrzegorz Jaszczyk 	       mmio_read_32(hpipe_addr +
216442a29337SGrzegorz Jaszczyk 			    HPIPE_ADAPTED_FFE_CAPACITOR_COUNTER_CTRL_REG),
216542a29337SGrzegorz Jaszczyk 			    (hpipe_addr + HPIPE_DATA_PHASE_OFF_CTRL_REG),
216642a29337SGrzegorz Jaszczyk 	       mmio_read_32(hpipe_addr + HPIPE_DATA_PHASE_OFF_CTRL_REG),
216742a29337SGrzegorz Jaszczyk 			    (hpipe_addr + HPIPE_ADAPTED_DFE_COEFFICIENT_1_REG),
216842a29337SGrzegorz Jaszczyk 	       mmio_read_32(hpipe_addr + HPIPE_ADAPTED_DFE_COEFFICIENT_1_REG));
216942a29337SGrzegorz Jaszczyk 	debug("================================================\n");
217042a29337SGrzegorz Jaszczyk 
217142a29337SGrzegorz Jaszczyk 	/* Update FFE_RES */
217242a29337SGrzegorz Jaszczyk 	mask = HPIPE_G1_SETTINGS_3_G1_FFE_RES_SEL_MASK;
217342a29337SGrzegorz Jaszczyk 	data = g1_ffe_res_sel << HPIPE_G1_SETTINGS_3_G1_FFE_RES_SEL_OFFSET;
217442a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_G1_SETTINGS_3_REG, data, mask);
217542a29337SGrzegorz Jaszczyk 
217642a29337SGrzegorz Jaszczyk 	/* Update FFE_CAP */
217742a29337SGrzegorz Jaszczyk 	mask = HPIPE_G1_SETTINGS_3_G1_FFE_CAP_SEL_MASK;
217842a29337SGrzegorz Jaszczyk 	data = g1_ffe_cap_sel << HPIPE_G1_SETTINGS_3_G1_FFE_CAP_SEL_OFFSET;
217942a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_G1_SETTINGS_3_REG, data, mask);
218042a29337SGrzegorz Jaszczyk 
218142a29337SGrzegorz Jaszczyk 	/* Bypass the FFE table settings and use the FFE settings directly from
218242a29337SGrzegorz Jaszczyk 	 * registers FFE_RES_SEL and FFE_CAP_SEL
21830ade8cd8SKonstantin Porotchkin 	 */
218442a29337SGrzegorz Jaszczyk 	mask = HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_MASK;
218542a29337SGrzegorz Jaszczyk 	data = 1 << HPIPE_G1_SETTINGS_3_G1_FFE_SETTING_FORCE_OFFSET;
218642a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_G1_SETTINGS_3_REG, data, mask);
21870ade8cd8SKonstantin Porotchkin 
218842a29337SGrzegorz Jaszczyk 	/* Force DFE resolution (use gen table value) */
21890ade8cd8SKonstantin Porotchkin 	mask = HPIPE_DFE_RES_FORCE_MASK;
21908e8ec8cfSGrzegorz Jaszczyk 	data = 0x1 << HPIPE_DFE_RES_FORCE_OFFSET;
21910ade8cd8SKonstantin Porotchkin 	reg_set(hpipe_addr + HPIPE_DFE_REG0, data, mask);
21920ade8cd8SKonstantin Porotchkin 
219342a29337SGrzegorz Jaszczyk 	/* 0x111-G1 DFE_Setting_4 */
219442a29337SGrzegorz Jaszczyk 	mask = HPIPE_G1_SETTINGS_4_G1_DFE_RES_MASK;
219542a29337SGrzegorz Jaszczyk 	data = g1_dfe_res << HPIPE_G1_SETTINGS_4_G1_DFE_RES_OFFSET;
219642a29337SGrzegorz Jaszczyk 	reg_set(hpipe_addr + HPIPE_G1_SETTINGS_4_REG, data, mask);
21970ade8cd8SKonstantin Porotchkin 
219842a29337SGrzegorz Jaszczyk 	printf("########################################################\n");
219942a29337SGrzegorz Jaszczyk 	printf("# To use trained values update the ATF sources:\n");
2200a2847172SGrzegorz Jaszczyk 	printf("# plat/marvell/armada/a8k/<board_type>/board/phy-porting-layer.h ");
220142a29337SGrzegorz Jaszczyk 	printf("file\n# with new values as below (for appropriate AP nr %d",
220242a29337SGrzegorz Jaszczyk 	       ap_nr);
220342a29337SGrzegorz Jaszczyk 	printf("and CP nr: %d comphy_index %d\n\n",
220442a29337SGrzegorz Jaszczyk 	       cp_nr, comphy_index);
220542a29337SGrzegorz Jaszczyk 	printf("static struct xfi_params xfi_static_values_tab[AP_NUM]");
220642a29337SGrzegorz Jaszczyk 	printf("[CP_NUM][MAX_LANE_NR] = {\n");
220742a29337SGrzegorz Jaszczyk 	printf("\t...\n");
220842a29337SGrzegorz Jaszczyk 	printf("\t.g1_ffe_res_sel = 0x%x,\n", g1_ffe_res_sel);
220942a29337SGrzegorz Jaszczyk 	printf("\t.g1_ffe_cap_sel = 0x%x,\n", g1_ffe_cap_sel);
221042a29337SGrzegorz Jaszczyk 	printf("\t.align90 = 0x%x,\n", align90);
221142a29337SGrzegorz Jaszczyk 	printf("\t.g1_dfe_res = 0x%x\n", g1_dfe_res);
221242a29337SGrzegorz Jaszczyk 	printf("\t...\n");
221342a29337SGrzegorz Jaszczyk 	printf("};\n\n");
221442a29337SGrzegorz Jaszczyk 	printf("########################################################\n");
221542a29337SGrzegorz Jaszczyk 
221642a29337SGrzegorz Jaszczyk 	rx_trainng_done[ap_nr][cp_nr][comphy_index] = 1;
221742a29337SGrzegorz Jaszczyk 
221842a29337SGrzegorz Jaszczyk 	return 0;
22190ade8cd8SKonstantin Porotchkin }
22200ade8cd8SKonstantin Porotchkin 
22210ade8cd8SKonstantin Porotchkin /* During AP the proper mode is auto-negotiated and the mac, pcs and serdes
22220ade8cd8SKonstantin Porotchkin  * configuration are done by the firmware loaded to the MG's CM3 for appropriate
22230ade8cd8SKonstantin Porotchkin  * negotiated mode. Therefore there is no need to configure the mac, pcs and
22240ade8cd8SKonstantin Porotchkin  * serdes from u-boot. The only thing that need to be setup is powering up
22250ade8cd8SKonstantin Porotchkin  * the comphy, which is done through Common PHY<n> Configuration 1 Register
22260ade8cd8SKonstantin Porotchkin  * (CP0: 0xF2441000, CP1: 0xF4441000). This step can't be done by MG's CM3,
22270ade8cd8SKonstantin Porotchkin  * since it doesn't have an access to this register-set (but it has access to
22280ade8cd8SKonstantin Porotchkin  * the network registers like: MG, AP, MAC, PCS, Serdes etc.)
22290ade8cd8SKonstantin Porotchkin  */
22300ade8cd8SKonstantin Porotchkin static int mvebu_cp110_comphy_ap_power_on(uint64_t comphy_base,
2231050eb19cSGrzegorz Jaszczyk 					  uint8_t comphy_index,
2232050eb19cSGrzegorz Jaszczyk 					  uint32_t comphy_mode)
22330ade8cd8SKonstantin Porotchkin {
22340ade8cd8SKonstantin Porotchkin 	uint32_t mask, data;
2235*5a9e46e6SGrzegorz Jaszczyk 	uint8_t ap_nr, cp_nr;
22360ade8cd8SKonstantin Porotchkin 	uintptr_t comphy_addr = comphy_addr =
22370ade8cd8SKonstantin Porotchkin 				COMPHY_ADDR(comphy_base, comphy_index);
22380ade8cd8SKonstantin Porotchkin 
2239050eb19cSGrzegorz Jaszczyk 	/* configure phy selector for XFI/SFI */
2240050eb19cSGrzegorz Jaszczyk 	mvebu_cp110_comphy_set_phy_selector(comphy_base, comphy_index,
2241050eb19cSGrzegorz Jaszczyk 					    comphy_mode);
22420ade8cd8SKonstantin Porotchkin 	debug_enter();
22430ade8cd8SKonstantin Porotchkin 	debug("stage: RFU configurations - hard reset comphy\n");
22440ade8cd8SKonstantin Porotchkin 	/* RFU configurations - hard reset comphy */
22450ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_UP_MASK;
22460ade8cd8SKonstantin Porotchkin 	data = 0x1 << COMMON_PHY_CFG1_PWR_UP_OFFSET;
22470ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_PIPE_SELECT_MASK;
22480ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_PIPE_SELECT_OFFSET;
22490ade8cd8SKonstantin Porotchkin 	reg_set(comphy_addr + COMMON_PHY_CFG1_REG, data, mask);
22500ade8cd8SKonstantin Porotchkin 	debug_exit();
22510ade8cd8SKonstantin Porotchkin 
2252*5a9e46e6SGrzegorz Jaszczyk 	/* Start AP Firmware */
2253*5a9e46e6SGrzegorz Jaszczyk 	mvebu_cp110_get_ap_and_cp_nr(&ap_nr, &cp_nr, comphy_base);
2254*5a9e46e6SGrzegorz Jaszczyk 	mg_start_ap_fw(cp_nr);
2255*5a9e46e6SGrzegorz Jaszczyk 
22560ade8cd8SKonstantin Porotchkin 	return 0;
22570ade8cd8SKonstantin Porotchkin }
22580ade8cd8SKonstantin Porotchkin 
22590ade8cd8SKonstantin Porotchkin /*
22600ade8cd8SKonstantin Porotchkin  * This function allows to reset the digital synchronizers between
22610ade8cd8SKonstantin Porotchkin  * the MAC and the PHY, it is required when the MAC changes its state.
22620ade8cd8SKonstantin Porotchkin  */
22630ade8cd8SKonstantin Porotchkin int mvebu_cp110_comphy_digital_reset(uint64_t comphy_base,
22640ade8cd8SKonstantin Porotchkin 				     uint8_t comphy_index,
22650ade8cd8SKonstantin Porotchkin 				     uint32_t comphy_mode, uint32_t command)
22660ade8cd8SKonstantin Porotchkin {
22670ade8cd8SKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
22680ade8cd8SKonstantin Porotchkin 	uintptr_t sd_ip_addr;
22690ade8cd8SKonstantin Porotchkin 	uint32_t mask, data;
22700ade8cd8SKonstantin Porotchkin 
22710ade8cd8SKonstantin Porotchkin 	sd_ip_addr = SD_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
22720ade8cd8SKonstantin Porotchkin 			     comphy_index);
22730ade8cd8SKonstantin Porotchkin 
22740ade8cd8SKonstantin Porotchkin 	switch (mode) {
22750ade8cd8SKonstantin Porotchkin 	case (COMPHY_SGMII_MODE):
22760ade8cd8SKonstantin Porotchkin 	case (COMPHY_HS_SGMII_MODE):
22770ade8cd8SKonstantin Porotchkin 	case (COMPHY_XFI_MODE):
22780ade8cd8SKonstantin Porotchkin 	case (COMPHY_SFI_MODE):
22790ade8cd8SKonstantin Porotchkin 	case (COMPHY_RXAUI_MODE):
22800ade8cd8SKonstantin Porotchkin 		mask = SD_EXTERNAL_CONFIG1_RF_RESET_IN_MASK;
22810ade8cd8SKonstantin Porotchkin 		data = ((command == COMPHY_COMMAND_DIGITAL_PWR_OFF) ?
22820ade8cd8SKonstantin Porotchkin 			0x0 : 0x1) << SD_EXTERNAL_CONFIG1_RF_RESET_IN_OFFSET;
22830ade8cd8SKonstantin Porotchkin 		reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
22840ade8cd8SKonstantin Porotchkin 		break;
22850ade8cd8SKonstantin Porotchkin 	default:
22860ade8cd8SKonstantin Porotchkin 		ERROR("comphy%d: Digital PWR ON/OFF is not supported\n",
22870ade8cd8SKonstantin Porotchkin 			comphy_index);
22880ade8cd8SKonstantin Porotchkin 			return -EINVAL;
22890ade8cd8SKonstantin Porotchkin 	}
22900ade8cd8SKonstantin Porotchkin 
22910ade8cd8SKonstantin Porotchkin 	return 0;
22920ade8cd8SKonstantin Porotchkin }
22930ade8cd8SKonstantin Porotchkin 
2294f858e989SGrzegorz Jaszczyk int mvebu_cp110_comphy_power_on(uint64_t comphy_base, uint8_t comphy_index,
22950ade8cd8SKonstantin Porotchkin 				uint64_t comphy_mode)
22960ade8cd8SKonstantin Porotchkin {
22970ade8cd8SKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
22980ade8cd8SKonstantin Porotchkin 	int err = 0;
22990ade8cd8SKonstantin Porotchkin 
23000ade8cd8SKonstantin Porotchkin 	debug_enter();
23010ade8cd8SKonstantin Porotchkin 
23020ade8cd8SKonstantin Porotchkin 	switch (mode) {
23030ade8cd8SKonstantin Porotchkin 	case(COMPHY_SATA_MODE):
23040ade8cd8SKonstantin Porotchkin 		err = mvebu_cp110_comphy_sata_power_on(comphy_base,
23050ade8cd8SKonstantin Porotchkin 						       comphy_index,
23060ade8cd8SKonstantin Porotchkin 						       comphy_mode);
23070ade8cd8SKonstantin Porotchkin 		break;
23080ade8cd8SKonstantin Porotchkin 	case(COMPHY_SGMII_MODE):
23090ade8cd8SKonstantin Porotchkin 	case(COMPHY_HS_SGMII_MODE):
23100ade8cd8SKonstantin Porotchkin 		err = mvebu_cp110_comphy_sgmii_power_on(comphy_base,
23110ade8cd8SKonstantin Porotchkin 							comphy_index,
23120ade8cd8SKonstantin Porotchkin 							comphy_mode);
23130ade8cd8SKonstantin Porotchkin 		break;
23140ade8cd8SKonstantin Porotchkin 	/* From comphy perspective, XFI and SFI are the same */
23150ade8cd8SKonstantin Porotchkin 	case (COMPHY_XFI_MODE):
23160ade8cd8SKonstantin Porotchkin 	case (COMPHY_SFI_MODE):
23170ade8cd8SKonstantin Porotchkin 		err = mvebu_cp110_comphy_xfi_power_on(comphy_base,
23180ade8cd8SKonstantin Porotchkin 						      comphy_index,
23190ade8cd8SKonstantin Porotchkin 						      comphy_mode);
23200ade8cd8SKonstantin Porotchkin 		break;
23210ade8cd8SKonstantin Porotchkin 	case (COMPHY_PCIE_MODE):
23220ade8cd8SKonstantin Porotchkin 		err = mvebu_cp110_comphy_pcie_power_on(comphy_base,
23230ade8cd8SKonstantin Porotchkin 						       comphy_index,
23240ade8cd8SKonstantin Porotchkin 						       comphy_mode);
23250ade8cd8SKonstantin Porotchkin 		break;
23260ade8cd8SKonstantin Porotchkin 	case (COMPHY_RXAUI_MODE):
23270ade8cd8SKonstantin Porotchkin 		err = mvebu_cp110_comphy_rxaui_power_on(comphy_base,
23280ade8cd8SKonstantin Porotchkin 							comphy_index,
23290ade8cd8SKonstantin Porotchkin 							comphy_mode);
233042a29337SGrzegorz Jaszczyk 		break;
23310ade8cd8SKonstantin Porotchkin 	case (COMPHY_USB3H_MODE):
23320ade8cd8SKonstantin Porotchkin 	case (COMPHY_USB3D_MODE):
23330ade8cd8SKonstantin Porotchkin 		err = mvebu_cp110_comphy_usb3_power_on(comphy_base,
23340ade8cd8SKonstantin Porotchkin 						       comphy_index,
23350ade8cd8SKonstantin Porotchkin 						       comphy_mode);
23360ade8cd8SKonstantin Porotchkin 		break;
23370ade8cd8SKonstantin Porotchkin 	case (COMPHY_AP_MODE):
2338050eb19cSGrzegorz Jaszczyk 		err = mvebu_cp110_comphy_ap_power_on(comphy_base, comphy_index,
2339050eb19cSGrzegorz Jaszczyk 						     comphy_mode);
23400ade8cd8SKonstantin Porotchkin 		break;
23410ade8cd8SKonstantin Porotchkin 	default:
2342f858e989SGrzegorz Jaszczyk 		ERROR("comphy%d: unsupported comphy mode\n", comphy_index);
23430ade8cd8SKonstantin Porotchkin 		err = -EINVAL;
23440ade8cd8SKonstantin Porotchkin 		break;
23450ade8cd8SKonstantin Porotchkin 	}
23460ade8cd8SKonstantin Porotchkin 
23470ade8cd8SKonstantin Porotchkin 	debug_exit();
23480ade8cd8SKonstantin Porotchkin 
23490ade8cd8SKonstantin Porotchkin 	return err;
23500ade8cd8SKonstantin Porotchkin }
23510ade8cd8SKonstantin Porotchkin 
235255df84f9SIgal Liberman int mvebu_cp110_comphy_power_off(uint64_t comphy_base, uint8_t comphy_index,
235355df84f9SIgal Liberman 				 uint64_t comphy_mode)
23540ade8cd8SKonstantin Porotchkin {
23550ade8cd8SKonstantin Porotchkin 	uintptr_t sd_ip_addr, comphy_ip_addr;
23560ade8cd8SKonstantin Porotchkin 	uint32_t mask, data;
235742a29337SGrzegorz Jaszczyk 	uint8_t ap_nr, cp_nr;
235855df84f9SIgal Liberman 	_Bool called_from_uboot = COMPHY_GET_CALLER(comphy_mode);
23590ade8cd8SKonstantin Porotchkin 
23600ade8cd8SKonstantin Porotchkin 	debug_enter();
23610ade8cd8SKonstantin Porotchkin 
236255df84f9SIgal Liberman 	/* Power-off might happen because of 2 things:
236355df84f9SIgal Liberman 	 *	1. Bootloader turns off unconnected lanes
236455df84f9SIgal Liberman 	 *	2. Linux turns off all lanes during boot
236555df84f9SIgal Liberman 	 *	   (and then reconfigure it).
236655df84f9SIgal Liberman 	 *
236755df84f9SIgal Liberman 	 * For PCIe, there's a problem:
236855df84f9SIgal Liberman 	 * In Armada 8K DB boards, PCIe initialization can be executed
236955df84f9SIgal Liberman 	 * only once (PCIe reset performed during chip power on and
237055df84f9SIgal Liberman 	 * it cannot be executed via GPIO later) so a lane configured to
237155df84f9SIgal Liberman 	 * PCIe should not be powered off by Linux.
237255df84f9SIgal Liberman 	 *
237355df84f9SIgal Liberman 	 * So, check 2 things:
237455df84f9SIgal Liberman 	 *	1. Is Linux called for power-off?
237555df84f9SIgal Liberman 	 *	2. Is the comphy configured to PCIe?
237655df84f9SIgal Liberman 	 * If the answer is YES for both 1 and 2, skip the power-off.
237755df84f9SIgal Liberman 	 *
237855df84f9SIgal Liberman 	 * TODO: In MacciatoBIN, PCIe reset is connected via GPIO,
237955df84f9SIgal Liberman 	 * so after GPIO reset is added to Linux Kernel, it can be
238055df84f9SIgal Liberman 	 * powered-off.
238155df84f9SIgal Liberman 	 */
238255df84f9SIgal Liberman 	if (!called_from_uboot) {
238355df84f9SIgal Liberman 		data = mmio_read_32(comphy_base +
238455df84f9SIgal Liberman 				    COMMON_SELECTOR_PIPE_REG_OFFSET);
238555df84f9SIgal Liberman 		data >>= (COMMON_SELECTOR_COMPHYN_FIELD_WIDTH * comphy_index);
238655df84f9SIgal Liberman 		data &= COMMON_SELECTOR_COMPHY_MASK;
238755df84f9SIgal Liberman 		if (data == COMMON_SELECTOR_PIPE_COMPHY_PCIE)
238855df84f9SIgal Liberman 			return 0;
238955df84f9SIgal Liberman 	}
239055df84f9SIgal Liberman 
239142a29337SGrzegorz Jaszczyk 	mvebu_cp110_get_ap_and_cp_nr(&ap_nr, &cp_nr, comphy_base);
239242a29337SGrzegorz Jaszczyk 
239342a29337SGrzegorz Jaszczyk 	if (rx_trainng_done[ap_nr][cp_nr][comphy_index]) {
239442a29337SGrzegorz Jaszczyk 		debug("Skip %s for comphy[%d][%d][%d], due to rx training\n",
239542a29337SGrzegorz Jaszczyk 		       __func__, ap_nr, cp_nr, comphy_index);
239642a29337SGrzegorz Jaszczyk 		return 0;
239742a29337SGrzegorz Jaszczyk 	}
239842a29337SGrzegorz Jaszczyk 
23990ade8cd8SKonstantin Porotchkin 	sd_ip_addr = SD_ADDR(COMPHY_PIPE_FROM_COMPHY_ADDR(comphy_base),
24000ade8cd8SKonstantin Porotchkin 			     comphy_index);
24010ade8cd8SKonstantin Porotchkin 	comphy_ip_addr = COMPHY_ADDR(comphy_base, comphy_index);
24020ade8cd8SKonstantin Porotchkin 
24030ade8cd8SKonstantin Porotchkin 	/* Hard reset the comphy, for Ethernet modes and Sata */
24040ade8cd8SKonstantin Porotchkin 	mask = SD_EXTERNAL_CONFIG1_RESET_IN_MASK;
24050ade8cd8SKonstantin Porotchkin 	data = 0x0 << SD_EXTERNAL_CONFIG1_RESET_IN_OFFSET;
24060ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RESET_CORE_MASK;
24070ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG1_RESET_CORE_OFFSET;
24080ade8cd8SKonstantin Porotchkin 	mask |= SD_EXTERNAL_CONFIG1_RF_RESET_IN_MASK;
24090ade8cd8SKonstantin Porotchkin 	data |= 0x0 << SD_EXTERNAL_CONFIG1_RF_RESET_IN_OFFSET;
24100ade8cd8SKonstantin Porotchkin 	reg_set(sd_ip_addr + SD_EXTERNAL_CONFIG1_REG, data, mask);
24110ade8cd8SKonstantin Porotchkin 
24120ade8cd8SKonstantin Porotchkin 	/* PCIe reset */
24130ade8cd8SKonstantin Porotchkin 	spin_lock(&cp110_mac_reset_lock);
24140ade8cd8SKonstantin Porotchkin 
24150ade8cd8SKonstantin Porotchkin 	/* The mvebu_cp110_comphy_power_off will be called only from Linux (to
24160ade8cd8SKonstantin Porotchkin 	 * override settings done by bootloader) and it will be relevant only
24170ade8cd8SKonstantin Porotchkin 	 * to PCIe (called before check if to skip pcie power off or not).
24180ade8cd8SKonstantin Porotchkin 	 */
24190ade8cd8SKonstantin Porotchkin 	data = mmio_read_32(SYS_CTRL_FROM_COMPHY_ADDR(comphy_base) +
24200ade8cd8SKonstantin Porotchkin 						 SYS_CTRL_UINIT_SOFT_RESET_REG);
24210ade8cd8SKonstantin Porotchkin 	switch (comphy_index) {
24220ade8cd8SKonstantin Porotchkin 	case COMPHY_LANE0:
24230ade8cd8SKonstantin Porotchkin 		data &= ~PCIE_MAC_RESET_MASK_PORT0;
24240ade8cd8SKonstantin Porotchkin 		break;
24250ade8cd8SKonstantin Porotchkin 	case COMPHY_LANE4:
24260ade8cd8SKonstantin Porotchkin 		data &= ~PCIE_MAC_RESET_MASK_PORT1;
24270ade8cd8SKonstantin Porotchkin 		break;
24280ade8cd8SKonstantin Porotchkin 	case COMPHY_LANE5:
24290ade8cd8SKonstantin Porotchkin 		data &= ~PCIE_MAC_RESET_MASK_PORT2;
24300ade8cd8SKonstantin Porotchkin 		break;
24310ade8cd8SKonstantin Porotchkin 	}
24320ade8cd8SKonstantin Porotchkin 
24330ade8cd8SKonstantin Porotchkin 	mmio_write_32(SYS_CTRL_FROM_COMPHY_ADDR(comphy_base) +
24340ade8cd8SKonstantin Porotchkin 					   SYS_CTRL_UINIT_SOFT_RESET_REG, data);
24350ade8cd8SKonstantin Porotchkin 	spin_unlock(&cp110_mac_reset_lock);
24360ade8cd8SKonstantin Porotchkin 
24370ade8cd8SKonstantin Porotchkin 	/* Hard reset the comphy, for PCIe and usb3 */
24380ade8cd8SKonstantin Porotchkin 	mask = COMMON_PHY_CFG1_PWR_ON_RESET_MASK;
24390ade8cd8SKonstantin Porotchkin 	data = 0x0 << COMMON_PHY_CFG1_PWR_ON_RESET_OFFSET;
24400ade8cd8SKonstantin Porotchkin 	mask |= COMMON_PHY_CFG1_CORE_RSTN_MASK;
24410ade8cd8SKonstantin Porotchkin 	data |= 0x0 << COMMON_PHY_CFG1_CORE_RSTN_OFFSET;
24420ade8cd8SKonstantin Porotchkin 	reg_set(comphy_ip_addr + COMMON_PHY_CFG1_REG, data, mask);
24430ade8cd8SKonstantin Porotchkin 
24440ade8cd8SKonstantin Porotchkin 	/* Clear comphy PHY and PIPE selector, can't rely on previous config. */
24450ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_clr_phy_selector(comphy_base, comphy_index);
24460ade8cd8SKonstantin Porotchkin 	mvebu_cp110_comphy_clr_pipe_selector(comphy_base, comphy_index);
24470ade8cd8SKonstantin Porotchkin 
24480ade8cd8SKonstantin Porotchkin 	debug_exit();
24490ade8cd8SKonstantin Porotchkin 
24500ade8cd8SKonstantin Porotchkin 	return 0;
24510ade8cd8SKonstantin Porotchkin }
2452