xref: /arm-trusted-firmware/drivers/marvell/comphy/phy-comphy-3700.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (C) 2018-2021 Marvell International Ltd.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier:     BSD-3-Clause
5*91f16700Schasinglulu  * https://spdx.org/licenses
6*91f16700Schasinglulu  */
7*91f16700Schasinglulu 
8*91f16700Schasinglulu #include <errno.h>
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <common/debug.h>
11*91f16700Schasinglulu #include <drivers/delay_timer.h>
12*91f16700Schasinglulu #include <lib/mmio.h>
13*91f16700Schasinglulu #include <lib/spinlock.h>
14*91f16700Schasinglulu 
15*91f16700Schasinglulu #include <mvebu.h>
16*91f16700Schasinglulu #include <mvebu_def.h>
17*91f16700Schasinglulu #include <plat_marvell.h>
18*91f16700Schasinglulu 
19*91f16700Schasinglulu #include "phy-comphy-3700.h"
20*91f16700Schasinglulu #include "phy-comphy-common.h"
21*91f16700Schasinglulu 
22*91f16700Schasinglulu /*
23*91f16700Schasinglulu  * COMPHY_INDIRECT_REG points to ahci address space but the ahci region used in
24*91f16700Schasinglulu  * Linux is up to 0x178 so none will access it from Linux in runtime
25*91f16700Schasinglulu  * concurrently.
26*91f16700Schasinglulu  */
27*91f16700Schasinglulu #define COMPHY_INDIRECT_REG	(MVEBU_REGS_BASE + 0xE0178)
28*91f16700Schasinglulu 
29*91f16700Schasinglulu /* The USB3_GBE1_PHY range is above USB3 registers used in dts */
30*91f16700Schasinglulu #define USB3_GBE1_PHY		(MVEBU_REGS_BASE + 0x5C000)
31*91f16700Schasinglulu #define COMPHY_SD_ADDR		(MVEBU_REGS_BASE + 0x1F000)
32*91f16700Schasinglulu 
33*91f16700Schasinglulu struct sgmii_phy_init_data_fix {
34*91f16700Schasinglulu 	uint16_t addr;
35*91f16700Schasinglulu 	uint16_t value;
36*91f16700Schasinglulu };
37*91f16700Schasinglulu 
38*91f16700Schasinglulu /* Changes to 40M1G25 mode data required for running 40M3G125 init mode */
39*91f16700Schasinglulu static struct sgmii_phy_init_data_fix sgmii_phy_init_fix[] = {
40*91f16700Schasinglulu 	{0x005, 0x07CC}, {0x015, 0x0000}, {0x01B, 0x0000}, {0x01D, 0x0000},
41*91f16700Schasinglulu 	{0x01E, 0x0000}, {0x01F, 0x0000}, {0x020, 0x0000}, {0x021, 0x0030},
42*91f16700Schasinglulu 	{0x026, 0x0888}, {0x04D, 0x0152}, {0x04F, 0xA020}, {0x050, 0x07CC},
43*91f16700Schasinglulu 	{0x053, 0xE9CA}, {0x055, 0xBD97}, {0x071, 0x3015}, {0x076, 0x03AA},
44*91f16700Schasinglulu 	{0x07C, 0x0FDF}, {0x0C2, 0x3030}, {0x0C3, 0x8000}, {0x0E2, 0x5550},
45*91f16700Schasinglulu 	{0x0E3, 0x12A4}, {0x0E4, 0x7D00}, {0x0E6, 0x0C83}, {0x101, 0xFCC0},
46*91f16700Schasinglulu 	{0x104, 0x0C10}
47*91f16700Schasinglulu };
48*91f16700Schasinglulu 
49*91f16700Schasinglulu /* 40M1G25 mode init data */
50*91f16700Schasinglulu static uint16_t sgmii_phy_init[512] = {
51*91f16700Schasinglulu 	/* 0       1       2       3       4       5       6       7 */
52*91f16700Schasinglulu 	/*-----------------------------------------------------------*/
53*91f16700Schasinglulu 	/* 8       9       A       B       C       D       E       F */
54*91f16700Schasinglulu 	0x3110, 0xFD83, 0x6430, 0x412F, 0x82C0, 0x06FA, 0x4500, 0x6D26,	/* 00 */
55*91f16700Schasinglulu 	0xAFC0, 0x8000, 0xC000, 0x0000, 0x2000, 0x49CC, 0x0BC9, 0x2A52,	/* 08 */
56*91f16700Schasinglulu 	0x0BD2, 0x0CDE, 0x13D2, 0x0CE8, 0x1149, 0x10E0, 0x0000, 0x0000,	/* 10 */
57*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x4134, 0x0D2D, 0xFFFF,	/* 18 */
58*91f16700Schasinglulu 	0xFFE0, 0x4030, 0x1016, 0x0030, 0x0000, 0x0800, 0x0866, 0x0000,	/* 20 */
59*91f16700Schasinglulu 	0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,	/* 28 */
60*91f16700Schasinglulu 	0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* 30 */
61*91f16700Schasinglulu 	0x0000, 0x0000, 0x000F, 0x6A62, 0x1988, 0x3100, 0x3100, 0x3100,	/* 38 */
62*91f16700Schasinglulu 	0x3100, 0xA708, 0x2430, 0x0830, 0x1030, 0x4610, 0xFF00, 0xFF00,	/* 40 */
63*91f16700Schasinglulu 	0x0060, 0x1000, 0x0400, 0x0040, 0x00F0, 0x0155, 0x1100, 0xA02A,	/* 48 */
64*91f16700Schasinglulu 	0x06FA, 0x0080, 0xB008, 0xE3ED, 0x5002, 0xB592, 0x7A80, 0x0001,	/* 50 */
65*91f16700Schasinglulu 	0x020A, 0x8820, 0x6014, 0x8054, 0xACAA, 0xFC88, 0x2A02, 0x45CF,	/* 58 */
66*91f16700Schasinglulu 	0x000F, 0x1817, 0x2860, 0x064F, 0x0000, 0x0204, 0x1800, 0x6000,	/* 60 */
67*91f16700Schasinglulu 	0x810F, 0x4F23, 0x4000, 0x4498, 0x0850, 0x0000, 0x000E, 0x1002,	/* 68 */
68*91f16700Schasinglulu 	0x9D3A, 0x3009, 0xD066, 0x0491, 0x0001, 0x6AB0, 0x0399, 0x3780,	/* 70 */
69*91f16700Schasinglulu 	0x0040, 0x5AC0, 0x4A80, 0x0000, 0x01DF, 0x0000, 0x0007, 0x0000,	/* 78 */
70*91f16700Schasinglulu 	0x2D54, 0x00A1, 0x4000, 0x0100, 0xA20A, 0x0000, 0x0000, 0x0000,	/* 80 */
71*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x7400, 0x0E81, 0x1000, 0x1242, 0x0210,	/* 88 */
72*91f16700Schasinglulu 	0x80DF, 0x0F1F, 0x2F3F, 0x4F5F, 0x6F7F, 0x0F1F, 0x2F3F, 0x4F5F,	/* 90 */
73*91f16700Schasinglulu 	0x6F7F, 0x4BAD, 0x0000, 0x0000, 0x0800, 0x0000, 0x2400, 0xB651,	/* 98 */
74*91f16700Schasinglulu 	0xC9E0, 0x4247, 0x0A24, 0x0000, 0xAF19, 0x1004, 0x0000, 0x0000,	/* A0 */
75*91f16700Schasinglulu 	0x0000, 0x0013, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* A8 */
76*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* B0 */
77*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0060, 0x0000, 0x0000, 0x0000, 0x0000,	/* B8 */
78*91f16700Schasinglulu 	0x0000, 0x0000, 0x3010, 0xFA00, 0x0000, 0x0000, 0x0000, 0x0003,	/* C0 */
79*91f16700Schasinglulu 	0x1618, 0x8200, 0x8000, 0x0400, 0x050F, 0x0000, 0x0000, 0x0000,	/* C8 */
80*91f16700Schasinglulu 	0x4C93, 0x0000, 0x1000, 0x1120, 0x0010, 0x1242, 0x1242, 0x1E00,	/* D0 */
81*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x00F8, 0x0000, 0x0041, 0x0800, 0x0000,	/* D8 */
82*91f16700Schasinglulu 	0x82A0, 0x572E, 0x2490, 0x14A9, 0x4E00, 0x0000, 0x0803, 0x0541,	/* E0 */
83*91f16700Schasinglulu 	0x0C15, 0x0000, 0x0000, 0x0400, 0x2626, 0x0000, 0x0000, 0x4200,	/* E8 */
84*91f16700Schasinglulu 	0x0000, 0xAA55, 0x1020, 0x0000, 0x0000, 0x5010, 0x0000, 0x0000,	/* F0 */
85*91f16700Schasinglulu 	0x0000, 0x0000, 0x5000, 0x0000, 0x0000, 0x0000, 0x02F2, 0x0000,	/* F8 */
86*91f16700Schasinglulu 	0x101F, 0xFDC0, 0x4000, 0x8010, 0x0110, 0x0006, 0x0000, 0x0000,	/*100 */
87*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*108 */
88*91f16700Schasinglulu 	0x04CF, 0x0000, 0x04CF, 0x0000, 0x04CF, 0x0000, 0x04C6, 0x0000,	/*110 */
89*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*118 */
90*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*120 */
91*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*128 */
92*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*130 */
93*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*138 */
94*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*140 */
95*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*148 */
96*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*150 */
97*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*158 */
98*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*160 */
99*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*168 */
100*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*170 */
101*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x00F0, 0x08A2, 0x3112, 0x0A14, 0x0000,	/*178 */
102*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*180 */
103*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*188 */
104*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*190 */
105*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*198 */
106*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1A0 */
107*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1A8 */
108*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1B0 */
109*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1B8 */
110*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1C0 */
111*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1C8 */
112*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1D0 */
113*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1D8 */
114*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1E0 */
115*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1E8 */
116*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1F0 */
117*91f16700Schasinglulu 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000	/*1F8 */
118*91f16700Schasinglulu };
119*91f16700Schasinglulu 
120*91f16700Schasinglulu /* PHY selector configures with corresponding modes */
121*91f16700Schasinglulu static int mvebu_a3700_comphy_set_phy_selector(uint8_t comphy_index,
122*91f16700Schasinglulu 						uint32_t comphy_mode)
123*91f16700Schasinglulu {
124*91f16700Schasinglulu 	uint32_t reg;
125*91f16700Schasinglulu 	int mode = COMPHY_GET_MODE(comphy_mode);
126*91f16700Schasinglulu 
127*91f16700Schasinglulu 	reg = mmio_read_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG);
128*91f16700Schasinglulu 	switch (mode) {
129*91f16700Schasinglulu 	case (COMPHY_SATA_MODE):
130*91f16700Schasinglulu 		/* SATA must be in Lane2 */
131*91f16700Schasinglulu 		if (comphy_index == COMPHY_LANE2)
132*91f16700Schasinglulu 			reg &= ~COMPHY_SELECTOR_USB3_PHY_SEL_BIT;
133*91f16700Schasinglulu 		else
134*91f16700Schasinglulu 			goto error;
135*91f16700Schasinglulu 		break;
136*91f16700Schasinglulu 
137*91f16700Schasinglulu 	case (COMPHY_SGMII_MODE):
138*91f16700Schasinglulu 	case (COMPHY_2500BASEX_MODE):
139*91f16700Schasinglulu 		if (comphy_index == COMPHY_LANE0)
140*91f16700Schasinglulu 			reg &= ~COMPHY_SELECTOR_USB3_GBE1_SEL_BIT;
141*91f16700Schasinglulu 		else if (comphy_index == COMPHY_LANE1)
142*91f16700Schasinglulu 			reg &= ~COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT;
143*91f16700Schasinglulu 		else
144*91f16700Schasinglulu 			goto error;
145*91f16700Schasinglulu 		break;
146*91f16700Schasinglulu 
147*91f16700Schasinglulu 	case (COMPHY_USB3H_MODE):
148*91f16700Schasinglulu 	case (COMPHY_USB3D_MODE):
149*91f16700Schasinglulu 	case (COMPHY_USB3_MODE):
150*91f16700Schasinglulu 		if (comphy_index == COMPHY_LANE2)
151*91f16700Schasinglulu 			reg |= COMPHY_SELECTOR_USB3_PHY_SEL_BIT;
152*91f16700Schasinglulu 		else if (comphy_index == COMPHY_LANE0)
153*91f16700Schasinglulu 			reg |= COMPHY_SELECTOR_USB3_GBE1_SEL_BIT;
154*91f16700Schasinglulu 		else
155*91f16700Schasinglulu 			goto error;
156*91f16700Schasinglulu 		break;
157*91f16700Schasinglulu 
158*91f16700Schasinglulu 	case (COMPHY_PCIE_MODE):
159*91f16700Schasinglulu 		/* PCIE must be in Lane1 */
160*91f16700Schasinglulu 		if (comphy_index == COMPHY_LANE1)
161*91f16700Schasinglulu 			reg |= COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT;
162*91f16700Schasinglulu 		else
163*91f16700Schasinglulu 			goto error;
164*91f16700Schasinglulu 		break;
165*91f16700Schasinglulu 
166*91f16700Schasinglulu 	default:
167*91f16700Schasinglulu 		goto error;
168*91f16700Schasinglulu 	}
169*91f16700Schasinglulu 
170*91f16700Schasinglulu 	mmio_write_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG, reg);
171*91f16700Schasinglulu 	return 0;
172*91f16700Schasinglulu error:
173*91f16700Schasinglulu 	ERROR("COMPHY[%d] mode[%d] is invalid\n", comphy_index, mode);
174*91f16700Schasinglulu 	return -EINVAL;
175*91f16700Schasinglulu }
176*91f16700Schasinglulu 
177*91f16700Schasinglulu /*
178*91f16700Schasinglulu  * This is something like the inverse of the previous function: for given
179*91f16700Schasinglulu  * lane it returns COMPHY_*_MODE.
180*91f16700Schasinglulu  *
181*91f16700Schasinglulu  * It is useful when powering the phy off.
182*91f16700Schasinglulu  *
183*91f16700Schasinglulu  * This function returns COMPHY_USB3_MODE even if the phy was configured
184*91f16700Schasinglulu  * with COMPHY_USB3D_MODE or COMPHY_USB3H_MODE. (The usb3 phy initialization
185*91f16700Schasinglulu  * code does not differentiate between these modes.)
186*91f16700Schasinglulu  * Also it returns COMPHY_SGMII_MODE even if the phy was configures with
187*91f16700Schasinglulu  * COMPHY_2500BASEX_MODE. (The sgmii phy initialization code does differentiate
188*91f16700Schasinglulu  * between these modes, but it is irrelevant when powering the phy off.)
189*91f16700Schasinglulu  */
190*91f16700Schasinglulu static int mvebu_a3700_comphy_get_mode(uint8_t comphy_index)
191*91f16700Schasinglulu {
192*91f16700Schasinglulu 	uint32_t reg;
193*91f16700Schasinglulu 
194*91f16700Schasinglulu 	reg = mmio_read_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG);
195*91f16700Schasinglulu 	switch (comphy_index) {
196*91f16700Schasinglulu 	case COMPHY_LANE0:
197*91f16700Schasinglulu 		if ((reg & COMPHY_SELECTOR_USB3_GBE1_SEL_BIT) != 0)
198*91f16700Schasinglulu 			return COMPHY_USB3_MODE;
199*91f16700Schasinglulu 		else
200*91f16700Schasinglulu 			return COMPHY_SGMII_MODE;
201*91f16700Schasinglulu 	case COMPHY_LANE1:
202*91f16700Schasinglulu 		if ((reg & COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT) != 0)
203*91f16700Schasinglulu 			return COMPHY_PCIE_MODE;
204*91f16700Schasinglulu 		else
205*91f16700Schasinglulu 			return COMPHY_SGMII_MODE;
206*91f16700Schasinglulu 	case COMPHY_LANE2:
207*91f16700Schasinglulu 		if ((reg & COMPHY_SELECTOR_USB3_PHY_SEL_BIT) != 0)
208*91f16700Schasinglulu 			return COMPHY_USB3_MODE;
209*91f16700Schasinglulu 		else
210*91f16700Schasinglulu 			return COMPHY_SATA_MODE;
211*91f16700Schasinglulu 	}
212*91f16700Schasinglulu 
213*91f16700Schasinglulu 	return COMPHY_UNUSED;
214*91f16700Schasinglulu }
215*91f16700Schasinglulu 
216*91f16700Schasinglulu /* It is only used for SATA and USB3 on comphy lane2. */
217*91f16700Schasinglulu static void comphy_set_indirect(uintptr_t addr, uint32_t offset, uint16_t data,
218*91f16700Schasinglulu 				uint16_t mask, bool is_sata)
219*91f16700Schasinglulu {
220*91f16700Schasinglulu 	/*
221*91f16700Schasinglulu 	 * When Lane 2 PHY is for USB3, access the PHY registers
222*91f16700Schasinglulu 	 * through indirect Address and Data registers:
223*91f16700Schasinglulu 	 * INDIR_ACC_PHY_ADDR (RD00E0178h [31:0]),
224*91f16700Schasinglulu 	 * INDIR_ACC_PHY_DATA (RD00E017Ch [31:0]),
225*91f16700Schasinglulu 	 * within the SATA Host Controller registers, Lane 2 base register
226*91f16700Schasinglulu 	 * offset is 0x200
227*91f16700Schasinglulu 	 */
228*91f16700Schasinglulu 	if (is_sata) {
229*91f16700Schasinglulu 		mmio_write_32(addr + COMPHY_LANE2_INDIR_ADDR_OFFSET, offset);
230*91f16700Schasinglulu 	} else {
231*91f16700Schasinglulu 		mmio_write_32(addr + COMPHY_LANE2_INDIR_ADDR_OFFSET,
232*91f16700Schasinglulu 			      offset + USB3PHY_LANE2_REG_BASE_OFFSET);
233*91f16700Schasinglulu 	}
234*91f16700Schasinglulu 
235*91f16700Schasinglulu 	reg_set(addr + COMPHY_LANE2_INDIR_DATA_OFFSET, data, mask);
236*91f16700Schasinglulu }
237*91f16700Schasinglulu 
238*91f16700Schasinglulu /* It is only used for SATA on comphy lane2. */
239*91f16700Schasinglulu static void comphy_sata_set_indirect(uintptr_t addr, uint32_t reg_offset,
240*91f16700Schasinglulu 				     uint16_t data, uint16_t mask)
241*91f16700Schasinglulu {
242*91f16700Schasinglulu 	comphy_set_indirect(addr, reg_offset, data, mask, true);
243*91f16700Schasinglulu }
244*91f16700Schasinglulu 
245*91f16700Schasinglulu /* It is only used for USB3 indirect access on comphy lane2. */
246*91f16700Schasinglulu static void comphy_usb3_set_indirect(uintptr_t addr, uint32_t reg_offset,
247*91f16700Schasinglulu 				     uint16_t data, uint16_t mask)
248*91f16700Schasinglulu {
249*91f16700Schasinglulu 	comphy_set_indirect(addr, reg_offset, data, mask, false);
250*91f16700Schasinglulu }
251*91f16700Schasinglulu 
252*91f16700Schasinglulu /* It is only used for USB3 direct access not on comphy lane2. */
253*91f16700Schasinglulu static void comphy_usb3_set_direct(uintptr_t addr, uint32_t reg_offset,
254*91f16700Schasinglulu 				   uint16_t data, uint16_t mask)
255*91f16700Schasinglulu {
256*91f16700Schasinglulu 	reg_set16((reg_offset * PHY_SHFT(USB3) + addr), data, mask);
257*91f16700Schasinglulu }
258*91f16700Schasinglulu 
259*91f16700Schasinglulu static void comphy_sgmii_phy_init(uintptr_t sd_ip_addr, bool is_1gbps)
260*91f16700Schasinglulu {
261*91f16700Schasinglulu 	const int fix_arr_sz = ARRAY_SIZE(sgmii_phy_init_fix);
262*91f16700Schasinglulu 	int addr, fix_idx;
263*91f16700Schasinglulu 	uint16_t val;
264*91f16700Schasinglulu 
265*91f16700Schasinglulu 	fix_idx = 0;
266*91f16700Schasinglulu 	for (addr = 0; addr < 512; addr++) {
267*91f16700Schasinglulu 		/*
268*91f16700Schasinglulu 		 * All PHY register values are defined in full for 3.125Gbps
269*91f16700Schasinglulu 		 * SERDES speed. The values required for 1.25 Gbps are almost
270*91f16700Schasinglulu 		 * the same and only few registers should be "fixed" in
271*91f16700Schasinglulu 		 * comparison to 3.125 Gbps values. These register values are
272*91f16700Schasinglulu 		 * stored in "sgmii_phy_init_fix" array.
273*91f16700Schasinglulu 		 */
274*91f16700Schasinglulu 		if (!is_1gbps && sgmii_phy_init_fix[fix_idx].addr == addr) {
275*91f16700Schasinglulu 			/* Use new value */
276*91f16700Schasinglulu 			val = sgmii_phy_init_fix[fix_idx].value;
277*91f16700Schasinglulu 			if (fix_idx < fix_arr_sz)
278*91f16700Schasinglulu 				fix_idx++;
279*91f16700Schasinglulu 		} else {
280*91f16700Schasinglulu 			val = sgmii_phy_init[addr];
281*91f16700Schasinglulu 		}
282*91f16700Schasinglulu 
283*91f16700Schasinglulu 		reg_set16(SGMIIPHY_ADDR(addr, sd_ip_addr), val, 0xFFFF);
284*91f16700Schasinglulu 	}
285*91f16700Schasinglulu }
286*91f16700Schasinglulu 
287*91f16700Schasinglulu static int mvebu_a3700_comphy_sata_power_on(uint8_t comphy_index,
288*91f16700Schasinglulu 					    uint32_t comphy_mode)
289*91f16700Schasinglulu {
290*91f16700Schasinglulu 	int ret;
291*91f16700Schasinglulu 	uint32_t offset, data = 0, ref_clk;
292*91f16700Schasinglulu 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
293*91f16700Schasinglulu 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
294*91f16700Schasinglulu 
295*91f16700Schasinglulu 	debug_enter();
296*91f16700Schasinglulu 
297*91f16700Schasinglulu 	/* Configure phy selector for SATA */
298*91f16700Schasinglulu 	ret = mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
299*91f16700Schasinglulu 	if (ret) {
300*91f16700Schasinglulu 		return ret;
301*91f16700Schasinglulu 	}
302*91f16700Schasinglulu 
303*91f16700Schasinglulu 	/* Clear phy isolation mode to make it work in normal mode */
304*91f16700Schasinglulu 	offset =  COMPHY_ISOLATION_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
305*91f16700Schasinglulu 	comphy_sata_set_indirect(comphy_indir_regs, offset, 0, PHY_ISOLATE_MODE);
306*91f16700Schasinglulu 
307*91f16700Schasinglulu 	/* 0. Check the Polarity invert bits */
308*91f16700Schasinglulu 	if (invert & COMPHY_POLARITY_TXD_INVERT)
309*91f16700Schasinglulu 		data |= TXD_INVERT_BIT;
310*91f16700Schasinglulu 	if (invert & COMPHY_POLARITY_RXD_INVERT)
311*91f16700Schasinglulu 		data |= RXD_INVERT_BIT;
312*91f16700Schasinglulu 
313*91f16700Schasinglulu 	offset = COMPHY_SYNC_PATTERN + SATAPHY_LANE2_REG_BASE_OFFSET;
314*91f16700Schasinglulu 	comphy_sata_set_indirect(comphy_indir_regs, offset, data, TXD_INVERT_BIT |
315*91f16700Schasinglulu 				 RXD_INVERT_BIT);
316*91f16700Schasinglulu 
317*91f16700Schasinglulu 	/* 1. Select 40-bit data width width */
318*91f16700Schasinglulu 	offset = COMPHY_DIG_LOOPBACK_EN + SATAPHY_LANE2_REG_BASE_OFFSET;
319*91f16700Schasinglulu 	comphy_sata_set_indirect(comphy_indir_regs, offset, DATA_WIDTH_40BIT,
320*91f16700Schasinglulu 				 SEL_DATA_WIDTH_MASK);
321*91f16700Schasinglulu 
322*91f16700Schasinglulu 	/* 2. Select reference clock(25M) and PHY mode (SATA) */
323*91f16700Schasinglulu 	offset = COMPHY_POWER_PLL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
324*91f16700Schasinglulu 	if (get_ref_clk() == 40)
325*91f16700Schasinglulu 		ref_clk = REF_FREF_SEL_SERDES_40MHZ;
326*91f16700Schasinglulu 	else
327*91f16700Schasinglulu 		ref_clk = REF_FREF_SEL_SERDES_25MHZ;
328*91f16700Schasinglulu 
329*91f16700Schasinglulu 	comphy_sata_set_indirect(comphy_indir_regs, offset, ref_clk | PHY_MODE_SATA,
330*91f16700Schasinglulu 				 REF_FREF_SEL_MASK | PHY_MODE_MASK);
331*91f16700Schasinglulu 
332*91f16700Schasinglulu 	/* 3. Use maximum PLL rate (no power save) */
333*91f16700Schasinglulu 	offset = COMPHY_KVCO_CAL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
334*91f16700Schasinglulu 	comphy_sata_set_indirect(comphy_indir_regs, offset, USE_MAX_PLL_RATE_BIT,
335*91f16700Schasinglulu 				 USE_MAX_PLL_RATE_BIT);
336*91f16700Schasinglulu 
337*91f16700Schasinglulu 	/* 4. Reset reserved bit */
338*91f16700Schasinglulu 	comphy_sata_set_indirect(comphy_indir_regs, COMPHY_RESERVED_REG, 0,
339*91f16700Schasinglulu 				 PHYCTRL_FRM_PIN_BIT);
340*91f16700Schasinglulu 
341*91f16700Schasinglulu 	/* 5. Set vendor-specific configuration (It is done in sata driver) */
342*91f16700Schasinglulu 	/* XXX: in U-Boot below sequence was executed in this place, in Linux
343*91f16700Schasinglulu 	 * not.  Now it is done only in U-Boot before this comphy
344*91f16700Schasinglulu 	 * initialization - tests shows that it works ok, but in case of any
345*91f16700Schasinglulu 	 * future problem it is left for reference.
346*91f16700Schasinglulu 	 *   reg_set(MVEBU_REGS_BASE + 0xe00a0, 0, 0xffffffff);
347*91f16700Schasinglulu 	 *   reg_set(MVEBU_REGS_BASE + 0xe00a4, BIT(6), BIT(6));
348*91f16700Schasinglulu 	 */
349*91f16700Schasinglulu 
350*91f16700Schasinglulu 	/* Wait for > 55 us to allow PLL be enabled */
351*91f16700Schasinglulu 	udelay(PLL_SET_DELAY_US);
352*91f16700Schasinglulu 
353*91f16700Schasinglulu 	/* Polling status */
354*91f16700Schasinglulu 	mmio_write_32(comphy_indir_regs + COMPHY_LANE2_INDIR_ADDR_OFFSET,
355*91f16700Schasinglulu 		      COMPHY_DIG_LOOPBACK_EN + SATAPHY_LANE2_REG_BASE_OFFSET);
356*91f16700Schasinglulu 
357*91f16700Schasinglulu 	ret = polling_with_timeout(comphy_indir_regs +
358*91f16700Schasinglulu 				   COMPHY_LANE2_INDIR_DATA_OFFSET,
359*91f16700Schasinglulu 				   PLL_READY_TX_BIT, PLL_READY_TX_BIT,
360*91f16700Schasinglulu 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
361*91f16700Schasinglulu 	if (ret) {
362*91f16700Schasinglulu 		return -ETIMEDOUT;
363*91f16700Schasinglulu 	}
364*91f16700Schasinglulu 
365*91f16700Schasinglulu 	debug_exit();
366*91f16700Schasinglulu 
367*91f16700Schasinglulu 	return 0;
368*91f16700Schasinglulu }
369*91f16700Schasinglulu 
370*91f16700Schasinglulu static int mvebu_a3700_comphy_sgmii_power_on(uint8_t comphy_index,
371*91f16700Schasinglulu 					     uint32_t comphy_mode)
372*91f16700Schasinglulu {
373*91f16700Schasinglulu 	int ret;
374*91f16700Schasinglulu 	uint32_t mask, data;
375*91f16700Schasinglulu 	uintptr_t offset;
376*91f16700Schasinglulu 	uintptr_t sd_ip_addr;
377*91f16700Schasinglulu 	int mode = COMPHY_GET_MODE(comphy_mode);
378*91f16700Schasinglulu 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
379*91f16700Schasinglulu 
380*91f16700Schasinglulu 	debug_enter();
381*91f16700Schasinglulu 
382*91f16700Schasinglulu 	/* Set selector */
383*91f16700Schasinglulu 	ret = mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
384*91f16700Schasinglulu 	if (ret) {
385*91f16700Schasinglulu 		return ret;
386*91f16700Schasinglulu 	}
387*91f16700Schasinglulu 
388*91f16700Schasinglulu 	/* Serdes IP Base address
389*91f16700Schasinglulu 	 * COMPHY Lane0 -- USB3/GBE1
390*91f16700Schasinglulu 	 * COMPHY Lane1 -- PCIe/GBE0
391*91f16700Schasinglulu 	 */
392*91f16700Schasinglulu 	if (comphy_index == COMPHY_LANE0) {
393*91f16700Schasinglulu 		/* Get usb3 and gbe */
394*91f16700Schasinglulu 		sd_ip_addr = USB3_GBE1_PHY;
395*91f16700Schasinglulu 	} else
396*91f16700Schasinglulu 		sd_ip_addr = COMPHY_SD_ADDR;
397*91f16700Schasinglulu 
398*91f16700Schasinglulu 	/*
399*91f16700Schasinglulu 	 * 1. Reset PHY by setting PHY input port PIN_RESET=1.
400*91f16700Schasinglulu 	 * 2. Set PHY input port PIN_TX_IDLE=1, PIN_PU_IVREF=1 to keep
401*91f16700Schasinglulu 	 *    PHY TXP/TXN output to idle state during PHY initialization
402*91f16700Schasinglulu 	 * 3. Set PHY input port PIN_PU_PLL=0, PIN_PU_RX=0, PIN_PU_TX=0.
403*91f16700Schasinglulu 	 */
404*91f16700Schasinglulu 	data = PIN_PU_IVREF_BIT | PIN_TX_IDLE_BIT | PIN_RESET_COMPHY_BIT;
405*91f16700Schasinglulu 	mask = data | PIN_RESET_CORE_BIT | PIN_PU_PLL_BIT | PIN_PU_RX_BIT |
406*91f16700Schasinglulu 		PIN_PU_TX_BIT;
407*91f16700Schasinglulu 	offset = MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index);
408*91f16700Schasinglulu 	reg_set(offset, data, mask);
409*91f16700Schasinglulu 
410*91f16700Schasinglulu 	/* 4. Release reset to the PHY by setting PIN_RESET=0. */
411*91f16700Schasinglulu 	data = 0;
412*91f16700Schasinglulu 	mask = PIN_RESET_COMPHY_BIT;
413*91f16700Schasinglulu 	reg_set(offset, data, mask);
414*91f16700Schasinglulu 
415*91f16700Schasinglulu 	/*
416*91f16700Schasinglulu 	 * 5. Set PIN_PHY_GEN_TX[3:0] and PIN_PHY_GEN_RX[3:0] to decide COMPHY
417*91f16700Schasinglulu 	 * bit rate
418*91f16700Schasinglulu 	 */
419*91f16700Schasinglulu 	if (mode == COMPHY_SGMII_MODE) {
420*91f16700Schasinglulu 		/* SGMII 1G, SerDes speed 1.25G */
421*91f16700Schasinglulu 		data |= SD_SPEED_1_25_G << GEN_RX_SEL_OFFSET;
422*91f16700Schasinglulu 		data |= SD_SPEED_1_25_G << GEN_TX_SEL_OFFSET;
423*91f16700Schasinglulu 	} else if (mode == COMPHY_2500BASEX_MODE) {
424*91f16700Schasinglulu 		/* 2500Base-X, SerDes speed 3.125G */
425*91f16700Schasinglulu 		data |= SD_SPEED_3_125_G << GEN_RX_SEL_OFFSET;
426*91f16700Schasinglulu 		data |= SD_SPEED_3_125_G << GEN_TX_SEL_OFFSET;
427*91f16700Schasinglulu 	} else {
428*91f16700Schasinglulu 		/* Other rates are not supported */
429*91f16700Schasinglulu 		ERROR("unsupported SGMII speed on comphy lane%d\n",
430*91f16700Schasinglulu 			comphy_index);
431*91f16700Schasinglulu 		return -EINVAL;
432*91f16700Schasinglulu 	}
433*91f16700Schasinglulu 	mask = GEN_RX_SEL_MASK | GEN_TX_SEL_MASK;
434*91f16700Schasinglulu 	reg_set(offset, data, mask);
435*91f16700Schasinglulu 
436*91f16700Schasinglulu 	/*
437*91f16700Schasinglulu 	 * 6. Wait 10mS for bandgap and reference clocks to stabilize; then
438*91f16700Schasinglulu 	 * start SW programming.
439*91f16700Schasinglulu 	 */
440*91f16700Schasinglulu 	mdelay(10);
441*91f16700Schasinglulu 
442*91f16700Schasinglulu 	/* 7. Program COMPHY register PHY_MODE */
443*91f16700Schasinglulu 	data = PHY_MODE_SGMII;
444*91f16700Schasinglulu 	mask = PHY_MODE_MASK;
445*91f16700Schasinglulu 	reg_set16(SGMIIPHY_ADDR(COMPHY_POWER_PLL_CTRL, sd_ip_addr), data, mask);
446*91f16700Schasinglulu 
447*91f16700Schasinglulu 	/*
448*91f16700Schasinglulu 	 * 8. Set COMPHY register REFCLK_SEL to select the correct REFCLK
449*91f16700Schasinglulu 	 * source
450*91f16700Schasinglulu 	 */
451*91f16700Schasinglulu 	data = 0;
452*91f16700Schasinglulu 	mask = PHY_REF_CLK_SEL;
453*91f16700Schasinglulu 	reg_set16(SGMIIPHY_ADDR(COMPHY_MISC_CTRL0, sd_ip_addr), data, mask);
454*91f16700Schasinglulu 
455*91f16700Schasinglulu 	/*
456*91f16700Schasinglulu 	 * 9. Set correct reference clock frequency in COMPHY register
457*91f16700Schasinglulu 	 * REF_FREF_SEL.
458*91f16700Schasinglulu 	 */
459*91f16700Schasinglulu 	if (get_ref_clk() == 40)
460*91f16700Schasinglulu 		data = REF_FREF_SEL_SERDES_50MHZ;
461*91f16700Schasinglulu 	else
462*91f16700Schasinglulu 		data = REF_FREF_SEL_SERDES_25MHZ;
463*91f16700Schasinglulu 
464*91f16700Schasinglulu 	mask = REF_FREF_SEL_MASK;
465*91f16700Schasinglulu 	reg_set16(SGMIIPHY_ADDR(COMPHY_POWER_PLL_CTRL, sd_ip_addr), data, mask);
466*91f16700Schasinglulu 
467*91f16700Schasinglulu 	/* 10. Program COMPHY register PHY_GEN_MAX[1:0]
468*91f16700Schasinglulu 	 * This step is mentioned in the flow received from verification team.
469*91f16700Schasinglulu 	 * However the PHY_GEN_MAX value is only meaningful for other interfaces
470*91f16700Schasinglulu 	 * (not SGMII). For instance, it selects SATA speed 1.5/3/6 Gbps or PCIe
471*91f16700Schasinglulu 	 * speed 2.5/5 Gbps
472*91f16700Schasinglulu 	 */
473*91f16700Schasinglulu 
474*91f16700Schasinglulu 	/*
475*91f16700Schasinglulu 	 * 11. Program COMPHY register SEL_BITS to set correct parallel data
476*91f16700Schasinglulu 	 * bus width
477*91f16700Schasinglulu 	 */
478*91f16700Schasinglulu 	data = DATA_WIDTH_10BIT;
479*91f16700Schasinglulu 	mask = SEL_DATA_WIDTH_MASK;
480*91f16700Schasinglulu 	reg_set16(SGMIIPHY_ADDR(COMPHY_DIG_LOOPBACK_EN, sd_ip_addr),
481*91f16700Schasinglulu 		  data, mask);
482*91f16700Schasinglulu 
483*91f16700Schasinglulu 	/*
484*91f16700Schasinglulu 	 * 12. As long as DFE function needs to be enabled in any mode,
485*91f16700Schasinglulu 	 * COMPHY register DFE_UPDATE_EN[5:0] shall be programmed to 0x3F
486*91f16700Schasinglulu 	 * for real chip during COMPHY power on.
487*91f16700Schasinglulu 	 * The step 14 exists (and empty) in the original initialization flow
488*91f16700Schasinglulu 	 * obtained from the verification team. According to the functional
489*91f16700Schasinglulu 	 * specification DFE_UPDATE_EN already has the default value 0x3F
490*91f16700Schasinglulu 	 */
491*91f16700Schasinglulu 
492*91f16700Schasinglulu 	/*
493*91f16700Schasinglulu 	 * 13. Program COMPHY GEN registers.
494*91f16700Schasinglulu 	 * These registers should be programmed based on the lab testing result
495*91f16700Schasinglulu 	 * to achieve optimal performance. Please contact the CEA group to get
496*91f16700Schasinglulu 	 * the related GEN table during real chip bring-up. We only required to
497*91f16700Schasinglulu 	 * run though the entire registers programming flow defined by
498*91f16700Schasinglulu 	 * "comphy_sgmii_phy_init" when the REF clock is 40 MHz. For REF clock
499*91f16700Schasinglulu 	 * 25 MHz the default values stored in PHY registers are OK.
500*91f16700Schasinglulu 	 */
501*91f16700Schasinglulu 	debug("Running C-DPI phy init %s mode\n",
502*91f16700Schasinglulu 	      mode == COMPHY_2500BASEX_MODE ? "2G5" : "1G");
503*91f16700Schasinglulu 	if (get_ref_clk() == 40)
504*91f16700Schasinglulu 		comphy_sgmii_phy_init(sd_ip_addr, mode != COMPHY_2500BASEX_MODE);
505*91f16700Schasinglulu 
506*91f16700Schasinglulu 	/*
507*91f16700Schasinglulu 	 * 14. [Simulation Only] should not be used for real chip.
508*91f16700Schasinglulu 	 * By pass power up calibration by programming EXT_FORCE_CAL_DONE
509*91f16700Schasinglulu 	 * (R02h[9]) to 1 to shorten COMPHY simulation time.
510*91f16700Schasinglulu 	 */
511*91f16700Schasinglulu 
512*91f16700Schasinglulu 	/*
513*91f16700Schasinglulu 	 * 15. [Simulation Only: should not be used for real chip]
514*91f16700Schasinglulu 	 * Program COMPHY register FAST_DFE_TIMER_EN=1 to shorten RX training
515*91f16700Schasinglulu 	 * simulation time.
516*91f16700Schasinglulu 	 */
517*91f16700Schasinglulu 
518*91f16700Schasinglulu 	/*
519*91f16700Schasinglulu 	 * 16. Check the PHY Polarity invert bit
520*91f16700Schasinglulu 	 */
521*91f16700Schasinglulu 	data = 0x0;
522*91f16700Schasinglulu 	if (invert & COMPHY_POLARITY_TXD_INVERT)
523*91f16700Schasinglulu 		data |= TXD_INVERT_BIT;
524*91f16700Schasinglulu 	if (invert & COMPHY_POLARITY_RXD_INVERT)
525*91f16700Schasinglulu 		data |= RXD_INVERT_BIT;
526*91f16700Schasinglulu 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
527*91f16700Schasinglulu 	reg_set16(SGMIIPHY_ADDR(COMPHY_SYNC_PATTERN, sd_ip_addr), data, mask);
528*91f16700Schasinglulu 
529*91f16700Schasinglulu 	/*
530*91f16700Schasinglulu 	 * 17. Set PHY input ports PIN_PU_PLL, PIN_PU_TX and PIN_PU_RX to 1 to
531*91f16700Schasinglulu 	 * start PHY power up sequence. All the PHY register programming should
532*91f16700Schasinglulu 	 * be done before PIN_PU_PLL=1. There should be no register programming
533*91f16700Schasinglulu 	 * for normal PHY operation from this point.
534*91f16700Schasinglulu 	 */
535*91f16700Schasinglulu 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
536*91f16700Schasinglulu 		PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT,
537*91f16700Schasinglulu 		PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT);
538*91f16700Schasinglulu 
539*91f16700Schasinglulu 	/*
540*91f16700Schasinglulu 	 * 18. Wait for PHY power up sequence to finish by checking output ports
541*91f16700Schasinglulu 	 * PIN_PLL_READY_TX=1 and PIN_PLL_READY_RX=1.
542*91f16700Schasinglulu 	 */
543*91f16700Schasinglulu 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
544*91f16700Schasinglulu 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
545*91f16700Schasinglulu 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
546*91f16700Schasinglulu 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
547*91f16700Schasinglulu 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
548*91f16700Schasinglulu 	if (ret) {
549*91f16700Schasinglulu 		ERROR("Failed to lock PLL for SGMII PHY %d\n", comphy_index);
550*91f16700Schasinglulu 		return -ETIMEDOUT;
551*91f16700Schasinglulu 	}
552*91f16700Schasinglulu 
553*91f16700Schasinglulu 	/*
554*91f16700Schasinglulu 	 * 19. Set COMPHY input port PIN_TX_IDLE=0
555*91f16700Schasinglulu 	 */
556*91f16700Schasinglulu 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
557*91f16700Schasinglulu 		0x0, PIN_TX_IDLE_BIT);
558*91f16700Schasinglulu 
559*91f16700Schasinglulu 	/*
560*91f16700Schasinglulu 	 * 20. After valid data appear on PIN_RXDATA bus, set PIN_RX_INIT=1. To
561*91f16700Schasinglulu 	 * start RX initialization. PIN_RX_INIT_DONE will be cleared to 0 by the
562*91f16700Schasinglulu 	 * PHY After RX initialization is done, PIN_RX_INIT_DONE will be set to
563*91f16700Schasinglulu 	 * 1 by COMPHY Set PIN_RX_INIT=0 after PIN_RX_INIT_DONE= 1. Please
564*91f16700Schasinglulu 	 * refer to RX initialization part for details.
565*91f16700Schasinglulu 	 */
566*91f16700Schasinglulu 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
567*91f16700Schasinglulu 		PHY_RX_INIT_BIT, PHY_RX_INIT_BIT);
568*91f16700Schasinglulu 
569*91f16700Schasinglulu 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
570*91f16700Schasinglulu 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
571*91f16700Schasinglulu 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
572*91f16700Schasinglulu 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
573*91f16700Schasinglulu 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
574*91f16700Schasinglulu 	if (ret) {
575*91f16700Schasinglulu 		ERROR("Failed to lock PLL for SGMII PHY %d\n", comphy_index);
576*91f16700Schasinglulu 		return -ETIMEDOUT;
577*91f16700Schasinglulu 	}
578*91f16700Schasinglulu 
579*91f16700Schasinglulu 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
580*91f16700Schasinglulu 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
581*91f16700Schasinglulu 				   PHY_RX_INIT_DONE_BIT, PHY_RX_INIT_DONE_BIT,
582*91f16700Schasinglulu 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
583*91f16700Schasinglulu 	if (ret) {
584*91f16700Schasinglulu 		ERROR("Failed to init RX of SGMII PHY %d\n", comphy_index);
585*91f16700Schasinglulu 		return -ETIMEDOUT;
586*91f16700Schasinglulu 	}
587*91f16700Schasinglulu 
588*91f16700Schasinglulu 	debug_exit();
589*91f16700Schasinglulu 
590*91f16700Schasinglulu 	return 0;
591*91f16700Schasinglulu }
592*91f16700Schasinglulu 
593*91f16700Schasinglulu static int mvebu_a3700_comphy_sgmii_power_off(uint8_t comphy_index)
594*91f16700Schasinglulu {
595*91f16700Schasinglulu 	uintptr_t offset;
596*91f16700Schasinglulu 	uint32_t mask, data;
597*91f16700Schasinglulu 
598*91f16700Schasinglulu 	debug_enter();
599*91f16700Schasinglulu 
600*91f16700Schasinglulu 	data = PIN_RESET_CORE_BIT | PIN_RESET_COMPHY_BIT;
601*91f16700Schasinglulu 	mask = data;
602*91f16700Schasinglulu 	offset = MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index);
603*91f16700Schasinglulu 	reg_set(offset, data, mask);
604*91f16700Schasinglulu 
605*91f16700Schasinglulu 	debug_exit();
606*91f16700Schasinglulu 
607*91f16700Schasinglulu 	return 0;
608*91f16700Schasinglulu }
609*91f16700Schasinglulu 
610*91f16700Schasinglulu static int mvebu_a3700_comphy_usb3_power_on(uint8_t comphy_index,
611*91f16700Schasinglulu 					    uint32_t comphy_mode)
612*91f16700Schasinglulu {
613*91f16700Schasinglulu 	int ret;
614*91f16700Schasinglulu 	uintptr_t reg_base = 0;
615*91f16700Schasinglulu 	uintptr_t addr;
616*91f16700Schasinglulu 	uint32_t mask, data, cfg, ref_clk;
617*91f16700Schasinglulu 	void (*usb3_reg_set)(uintptr_t addr, uint32_t reg_offset, uint16_t data,
618*91f16700Schasinglulu 			     uint16_t mask);
619*91f16700Schasinglulu 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
620*91f16700Schasinglulu 
621*91f16700Schasinglulu 	debug_enter();
622*91f16700Schasinglulu 
623*91f16700Schasinglulu 	/* Set phy seclector */
624*91f16700Schasinglulu 	ret = mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
625*91f16700Schasinglulu 	if (ret) {
626*91f16700Schasinglulu 		return ret;
627*91f16700Schasinglulu 	}
628*91f16700Schasinglulu 
629*91f16700Schasinglulu 	/* Set usb3 reg access func, Lane2 is indirect access */
630*91f16700Schasinglulu 	if (comphy_index == COMPHY_LANE2) {
631*91f16700Schasinglulu 		usb3_reg_set = &comphy_usb3_set_indirect;
632*91f16700Schasinglulu 		reg_base = COMPHY_INDIRECT_REG;
633*91f16700Schasinglulu 	} else {
634*91f16700Schasinglulu 		/* Get the direct access register resource and map */
635*91f16700Schasinglulu 		usb3_reg_set = &comphy_usb3_set_direct;
636*91f16700Schasinglulu 		reg_base = USB3_GBE1_PHY;
637*91f16700Schasinglulu 	}
638*91f16700Schasinglulu 
639*91f16700Schasinglulu 	/*
640*91f16700Schasinglulu 	 * 0. Set PHY OTG Control(0x5d034), bit 4, Power up OTG module The
641*91f16700Schasinglulu 	 * register belong to UTMI module, so it is set in UTMI phy driver.
642*91f16700Schasinglulu 	 */
643*91f16700Schasinglulu 
644*91f16700Schasinglulu 	/*
645*91f16700Schasinglulu 	 * 1. Set PRD_TXDEEMPH (3.5db de-emph)
646*91f16700Schasinglulu 	 */
647*91f16700Schasinglulu 	mask = PRD_TXDEEMPH0_MASK | PRD_TXMARGIN_MASK | PRD_TXSWING_MASK |
648*91f16700Schasinglulu 		CFG_TX_ALIGN_POS_MASK;
649*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_LANE_CFG0, PRD_TXDEEMPH0_MASK, mask);
650*91f16700Schasinglulu 
651*91f16700Schasinglulu 	/*
652*91f16700Schasinglulu 	 * 2. Set BIT0: enable transmitter in high impedance mode
653*91f16700Schasinglulu 	 *    Set BIT[3:4]: delay 2 clock cycles for HiZ off latency
654*91f16700Schasinglulu 	 *    Set BIT6: Tx detect Rx at HiZ mode
655*91f16700Schasinglulu 	 *    Unset BIT15: set to 0 to set USB3 De-emphasize level to -3.5db
656*91f16700Schasinglulu 	 *            together with bit 0 of COMPHY_LANE_CFG0 register
657*91f16700Schasinglulu 	 */
658*91f16700Schasinglulu 	mask = PRD_TXDEEMPH1_MASK | TX_DET_RX_MODE | GEN2_TX_DATA_DLY_MASK |
659*91f16700Schasinglulu 		TX_ELEC_IDLE_MODE_EN;
660*91f16700Schasinglulu 	data = TX_DET_RX_MODE | GEN2_TX_DATA_DLY_DEFT | TX_ELEC_IDLE_MODE_EN;
661*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_LANE_CFG1, data, mask);
662*91f16700Schasinglulu 
663*91f16700Schasinglulu 	/*
664*91f16700Schasinglulu 	 * 3. Set Spread Spectrum Clock Enabled
665*91f16700Schasinglulu 	 */
666*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_LANE_CFG4,
667*91f16700Schasinglulu 		     SPREAD_SPECTRUM_CLK_EN, SPREAD_SPECTRUM_CLK_EN);
668*91f16700Schasinglulu 
669*91f16700Schasinglulu 	/*
670*91f16700Schasinglulu 	 * 4. Set Override Margining Controls From the MAC:
671*91f16700Schasinglulu 	 *    Use margining signals from lane configuration
672*91f16700Schasinglulu 	 */
673*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_TEST_MODE_CTRL,
674*91f16700Schasinglulu 		     MODE_MARGIN_OVERRIDE, REG_16_BIT_MASK);
675*91f16700Schasinglulu 
676*91f16700Schasinglulu 	/*
677*91f16700Schasinglulu 	 * 5. Set Lane-to-Lane Bundle Clock Sampling Period = per PCLK cycles
678*91f16700Schasinglulu 	 *    set Mode Clock Source = PCLK is generated from REFCLK
679*91f16700Schasinglulu 	 */
680*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_CLK_SRC_LO, 0x0,
681*91f16700Schasinglulu 		     (MODE_CLK_SRC | BUNDLE_PERIOD_SEL |
682*91f16700Schasinglulu 		      BUNDLE_PERIOD_SCALE_MASK | BUNDLE_SAMPLE_CTRL |
683*91f16700Schasinglulu 		      PLL_READY_DLY_MASK));
684*91f16700Schasinglulu 
685*91f16700Schasinglulu 	/*
686*91f16700Schasinglulu 	 * 6. Set G2 Spread Spectrum Clock Amplitude at 4K
687*91f16700Schasinglulu 	 */
688*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_GEN2_SET2,
689*91f16700Schasinglulu 		     GS2_TX_SSC_AMP_VALUE_20, GS2_TX_SSC_AMP_MASK);
690*91f16700Schasinglulu 
691*91f16700Schasinglulu 	/*
692*91f16700Schasinglulu 	 * 7. Unset G3 Spread Spectrum Clock Amplitude
693*91f16700Schasinglulu 	 *    set G3 TX and RX Register Master Current Select
694*91f16700Schasinglulu 	 */
695*91f16700Schasinglulu 	mask = GS2_TX_SSC_AMP_MASK | GS2_VREG_RXTX_MAS_ISET_MASK |
696*91f16700Schasinglulu 		GS2_RSVD_6_0_MASK;
697*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_GEN3_SET2,
698*91f16700Schasinglulu 		     GS2_VREG_RXTX_MAS_ISET_60U, mask);
699*91f16700Schasinglulu 
700*91f16700Schasinglulu 	/*
701*91f16700Schasinglulu 	 * 8. Check crystal jumper setting and program the Power and PLL Control
702*91f16700Schasinglulu 	 * accordingly Change RX wait
703*91f16700Schasinglulu 	 */
704*91f16700Schasinglulu 	if (get_ref_clk() == 40) {
705*91f16700Schasinglulu 		ref_clk = REF_FREF_SEL_PCIE_USB3_40MHZ;
706*91f16700Schasinglulu 		cfg = CFG_PM_RXDLOZ_WAIT_12_UNIT;
707*91f16700Schasinglulu 
708*91f16700Schasinglulu 	} else {
709*91f16700Schasinglulu 		/* 25 MHz */
710*91f16700Schasinglulu 		ref_clk = REF_FREF_SEL_PCIE_USB3_25MHZ;
711*91f16700Schasinglulu 		cfg = CFG_PM_RXDLOZ_WAIT_7_UNIT;
712*91f16700Schasinglulu 	}
713*91f16700Schasinglulu 
714*91f16700Schasinglulu 	mask = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
715*91f16700Schasinglulu 		PU_TX_INTP_BIT | PU_DFE_BIT | PLL_LOCK_BIT | PHY_MODE_MASK |
716*91f16700Schasinglulu 		REF_FREF_SEL_MASK;
717*91f16700Schasinglulu 	data = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
718*91f16700Schasinglulu 		PU_TX_INTP_BIT | PU_DFE_BIT | PHY_MODE_USB3 | ref_clk;
719*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_POWER_PLL_CTRL, data,  mask);
720*91f16700Schasinglulu 
721*91f16700Schasinglulu 	mask = CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
722*91f16700Schasinglulu 		CFG_PM_RXDLOZ_WAIT_MASK;
723*91f16700Schasinglulu 	data = CFG_PM_RXDEN_WAIT_1_UNIT  | cfg;
724*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_PWR_MGM_TIM1, data, mask);
725*91f16700Schasinglulu 
726*91f16700Schasinglulu 	/*
727*91f16700Schasinglulu 	 * 9. Enable idle sync
728*91f16700Schasinglulu 	 */
729*91f16700Schasinglulu 	data = IDLE_SYNC_EN_DEFAULT_VALUE | IDLE_SYNC_EN;
730*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_IDLE_SYNC_EN, data, REG_16_BIT_MASK);
731*91f16700Schasinglulu 
732*91f16700Schasinglulu 	/*
733*91f16700Schasinglulu 	 * 10. Enable the output of 500M clock
734*91f16700Schasinglulu 	 */
735*91f16700Schasinglulu 	data = MISC_CTRL0_DEFAULT_VALUE | CLK500M_EN;
736*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_MISC_CTRL0, data, REG_16_BIT_MASK);
737*91f16700Schasinglulu 
738*91f16700Schasinglulu 	/*
739*91f16700Schasinglulu 	 * 11. Set 20-bit data width
740*91f16700Schasinglulu 	 */
741*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_DIG_LOOPBACK_EN, DATA_WIDTH_20BIT,
742*91f16700Schasinglulu 		     REG_16_BIT_MASK);
743*91f16700Schasinglulu 
744*91f16700Schasinglulu 	/*
745*91f16700Schasinglulu 	 * 12. Override Speed_PLL value and use MAC PLL
746*91f16700Schasinglulu 	 */
747*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_KVCO_CAL_CTRL,
748*91f16700Schasinglulu 		     (SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT),
749*91f16700Schasinglulu 		     REG_16_BIT_MASK);
750*91f16700Schasinglulu 
751*91f16700Schasinglulu 	/*
752*91f16700Schasinglulu 	 * 13. Check the Polarity invert bit
753*91f16700Schasinglulu 	 */
754*91f16700Schasinglulu 	data = 0U;
755*91f16700Schasinglulu 	if (invert & COMPHY_POLARITY_TXD_INVERT) {
756*91f16700Schasinglulu 		data |= TXD_INVERT_BIT;
757*91f16700Schasinglulu 	}
758*91f16700Schasinglulu 	if (invert & COMPHY_POLARITY_RXD_INVERT) {
759*91f16700Schasinglulu 		data |= RXD_INVERT_BIT;
760*91f16700Schasinglulu 	}
761*91f16700Schasinglulu 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
762*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_SYNC_PATTERN, data, mask);
763*91f16700Schasinglulu 
764*91f16700Schasinglulu 	/*
765*91f16700Schasinglulu 	 * 14. Set max speed generation to USB3.0 5Gbps
766*91f16700Schasinglulu 	 */
767*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_SYNC_MASK_GEN, PHY_GEN_MAX_USB3_5G,
768*91f16700Schasinglulu 		     PHY_GEN_MAX_MASK);
769*91f16700Schasinglulu 
770*91f16700Schasinglulu 	/*
771*91f16700Schasinglulu 	 * 15. Set capacitor value for FFE gain peaking to 0xF
772*91f16700Schasinglulu 	 */
773*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_GEN2_SET3,
774*91f16700Schasinglulu 		     GS3_FFE_CAP_SEL_VALUE, GS3_FFE_CAP_SEL_MASK);
775*91f16700Schasinglulu 
776*91f16700Schasinglulu 	/*
777*91f16700Schasinglulu 	 * 16. Release SW reset
778*91f16700Schasinglulu 	 */
779*91f16700Schasinglulu 	data = MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32 | MODE_REFDIV_BY_4;
780*91f16700Schasinglulu 	usb3_reg_set(reg_base, COMPHY_RST_CLK_CTRL, data, REG_16_BIT_MASK);
781*91f16700Schasinglulu 
782*91f16700Schasinglulu 	/* Wait for > 55 us to allow PCLK be enabled */
783*91f16700Schasinglulu 	udelay(PLL_SET_DELAY_US);
784*91f16700Schasinglulu 
785*91f16700Schasinglulu 	if (comphy_index == COMPHY_LANE2) {
786*91f16700Schasinglulu 		data = COMPHY_LANE_STAT1 + USB3PHY_LANE2_REG_BASE_OFFSET;
787*91f16700Schasinglulu 		mmio_write_32(reg_base + COMPHY_LANE2_INDIR_ADDR_OFFSET,
788*91f16700Schasinglulu 			      data);
789*91f16700Schasinglulu 
790*91f16700Schasinglulu 		addr = reg_base + COMPHY_LANE2_INDIR_DATA_OFFSET;
791*91f16700Schasinglulu 		ret = polling_with_timeout(addr, TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
792*91f16700Schasinglulu 					   COMPHY_PLL_TIMEOUT, REG_32BIT);
793*91f16700Schasinglulu 	} else {
794*91f16700Schasinglulu 		ret = polling_with_timeout(LANE_STAT1_ADDR(USB3) + reg_base,
795*91f16700Schasinglulu 					   TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
796*91f16700Schasinglulu 					   COMPHY_PLL_TIMEOUT, REG_16BIT);
797*91f16700Schasinglulu 	}
798*91f16700Schasinglulu 	if (ret) {
799*91f16700Schasinglulu 		ERROR("Failed to lock USB3 PLL\n");
800*91f16700Schasinglulu 		return -ETIMEDOUT;
801*91f16700Schasinglulu 	}
802*91f16700Schasinglulu 
803*91f16700Schasinglulu 	debug_exit();
804*91f16700Schasinglulu 
805*91f16700Schasinglulu 	return 0;
806*91f16700Schasinglulu }
807*91f16700Schasinglulu 
808*91f16700Schasinglulu static int mvebu_a3700_comphy_pcie_power_on(uint8_t comphy_index,
809*91f16700Schasinglulu 					    uint32_t comphy_mode)
810*91f16700Schasinglulu {
811*91f16700Schasinglulu 	int ret;
812*91f16700Schasinglulu 	uint32_t ref_clk;
813*91f16700Schasinglulu 	uint32_t mask, data;
814*91f16700Schasinglulu 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
815*91f16700Schasinglulu 
816*91f16700Schasinglulu 	debug_enter();
817*91f16700Schasinglulu 
818*91f16700Schasinglulu 	/* Configure phy selector for PCIe */
819*91f16700Schasinglulu 	ret = mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
820*91f16700Schasinglulu 	if (ret) {
821*91f16700Schasinglulu 		return ret;
822*91f16700Schasinglulu 	}
823*91f16700Schasinglulu 
824*91f16700Schasinglulu 	/* 1. Enable max PLL. */
825*91f16700Schasinglulu 	reg_set16(LANE_CFG1_ADDR(PCIE) + COMPHY_SD_ADDR,
826*91f16700Schasinglulu 		  USE_MAX_PLL_RATE_EN, USE_MAX_PLL_RATE_EN);
827*91f16700Schasinglulu 
828*91f16700Schasinglulu 	/* 2. Select 20 bit SERDES interface. */
829*91f16700Schasinglulu 	reg_set16(CLK_SRC_LO_ADDR(PCIE) + COMPHY_SD_ADDR,
830*91f16700Schasinglulu 		  CFG_SEL_20B, CFG_SEL_20B);
831*91f16700Schasinglulu 
832*91f16700Schasinglulu 	/* 3. Force to use reg setting for PCIe mode */
833*91f16700Schasinglulu 	reg_set16(MISC_CTRL1_ADDR(PCIE) + COMPHY_SD_ADDR,
834*91f16700Schasinglulu 		  SEL_BITS_PCIE_FORCE, SEL_BITS_PCIE_FORCE);
835*91f16700Schasinglulu 
836*91f16700Schasinglulu 	/* 4. Change RX wait */
837*91f16700Schasinglulu 	reg_set16(PWR_MGM_TIM1_ADDR(PCIE) + COMPHY_SD_ADDR,
838*91f16700Schasinglulu 		  CFG_PM_RXDEN_WAIT_1_UNIT | CFG_PM_RXDLOZ_WAIT_12_UNIT,
839*91f16700Schasinglulu 		  (CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
840*91f16700Schasinglulu 		   CFG_PM_RXDLOZ_WAIT_MASK));
841*91f16700Schasinglulu 
842*91f16700Schasinglulu 	/* 5. Enable idle sync */
843*91f16700Schasinglulu 	reg_set16(IDLE_SYNC_EN_ADDR(PCIE) + COMPHY_SD_ADDR,
844*91f16700Schasinglulu 		  IDLE_SYNC_EN_DEFAULT_VALUE | IDLE_SYNC_EN, REG_16_BIT_MASK);
845*91f16700Schasinglulu 
846*91f16700Schasinglulu 	/* 6. Enable the output of 100M/125M/500M clock */
847*91f16700Schasinglulu 	reg_set16(MISC_CTRL0_ADDR(PCIE) + COMPHY_SD_ADDR,
848*91f16700Schasinglulu 		  MISC_CTRL0_DEFAULT_VALUE | CLK500M_EN | TXDCLK_2X_SEL | CLK100M_125M_EN,
849*91f16700Schasinglulu 		  REG_16_BIT_MASK);
850*91f16700Schasinglulu 
851*91f16700Schasinglulu 	/*
852*91f16700Schasinglulu 	 * 7. Enable TX, PCIE global register, 0xd0074814, it is done in
853*91f16700Schasinglulu 	 * PCI-E driver
854*91f16700Schasinglulu 	 */
855*91f16700Schasinglulu 
856*91f16700Schasinglulu 	/*
857*91f16700Schasinglulu 	 * 8. Check crystal jumper setting and program the Power and PLL
858*91f16700Schasinglulu 	 * Control accordingly
859*91f16700Schasinglulu 	 */
860*91f16700Schasinglulu 
861*91f16700Schasinglulu 	if (get_ref_clk() == 40)
862*91f16700Schasinglulu 		ref_clk = REF_FREF_SEL_PCIE_USB3_40MHZ;
863*91f16700Schasinglulu 	else
864*91f16700Schasinglulu 		ref_clk = REF_FREF_SEL_PCIE_USB3_25MHZ;
865*91f16700Schasinglulu 
866*91f16700Schasinglulu 	reg_set16(PWR_PLL_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
867*91f16700Schasinglulu 		  (PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
868*91f16700Schasinglulu 		   PU_TX_INTP_BIT | PU_DFE_BIT | ref_clk | PHY_MODE_PCIE),
869*91f16700Schasinglulu 		  REG_16_BIT_MASK);
870*91f16700Schasinglulu 
871*91f16700Schasinglulu 	/* 9. Override Speed_PLL value and use MAC PLL */
872*91f16700Schasinglulu 	reg_set16(KVCO_CAL_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
873*91f16700Schasinglulu 		  SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT, REG_16_BIT_MASK);
874*91f16700Schasinglulu 
875*91f16700Schasinglulu 	/* 10. Check the Polarity invert bit */
876*91f16700Schasinglulu 	data = 0U;
877*91f16700Schasinglulu 	if (invert & COMPHY_POLARITY_TXD_INVERT) {
878*91f16700Schasinglulu 		data |= TXD_INVERT_BIT;
879*91f16700Schasinglulu 	}
880*91f16700Schasinglulu 	if (invert & COMPHY_POLARITY_RXD_INVERT) {
881*91f16700Schasinglulu 		data |= RXD_INVERT_BIT;
882*91f16700Schasinglulu 	}
883*91f16700Schasinglulu 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
884*91f16700Schasinglulu 	reg_set16(SYNC_PATTERN_ADDR(PCIE) + COMPHY_SD_ADDR, data, mask);
885*91f16700Schasinglulu 
886*91f16700Schasinglulu 	/* 11. Release SW reset */
887*91f16700Schasinglulu 	data = MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32;
888*91f16700Schasinglulu 	mask = data | SOFT_RESET | MODE_REFDIV_MASK;
889*91f16700Schasinglulu 	reg_set16(RST_CLK_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR, data, mask);
890*91f16700Schasinglulu 
891*91f16700Schasinglulu 	/* Wait for > 55 us to allow PCLK be enabled */
892*91f16700Schasinglulu 	udelay(PLL_SET_DELAY_US);
893*91f16700Schasinglulu 
894*91f16700Schasinglulu 	ret = polling_with_timeout(LANE_STAT1_ADDR(PCIE) + COMPHY_SD_ADDR,
895*91f16700Schasinglulu 				   TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
896*91f16700Schasinglulu 				   COMPHY_PLL_TIMEOUT, REG_16BIT);
897*91f16700Schasinglulu 	if (ret) {
898*91f16700Schasinglulu 		ERROR("Failed to lock PCIE PLL\n");
899*91f16700Schasinglulu 		return -ETIMEDOUT;
900*91f16700Schasinglulu 	}
901*91f16700Schasinglulu 
902*91f16700Schasinglulu 	debug_exit();
903*91f16700Schasinglulu 
904*91f16700Schasinglulu 	return 0;
905*91f16700Schasinglulu }
906*91f16700Schasinglulu 
907*91f16700Schasinglulu int mvebu_3700_comphy_power_on(uint8_t comphy_index, uint32_t comphy_mode)
908*91f16700Schasinglulu {
909*91f16700Schasinglulu 	int mode = COMPHY_GET_MODE(comphy_mode);
910*91f16700Schasinglulu 	int ret = 0;
911*91f16700Schasinglulu 
912*91f16700Schasinglulu 	debug_enter();
913*91f16700Schasinglulu 
914*91f16700Schasinglulu 	switch (mode) {
915*91f16700Schasinglulu 	case(COMPHY_SATA_MODE):
916*91f16700Schasinglulu 		ret = mvebu_a3700_comphy_sata_power_on(comphy_index,
917*91f16700Schasinglulu 						       comphy_mode);
918*91f16700Schasinglulu 		break;
919*91f16700Schasinglulu 	case(COMPHY_SGMII_MODE):
920*91f16700Schasinglulu 	case(COMPHY_2500BASEX_MODE):
921*91f16700Schasinglulu 		ret = mvebu_a3700_comphy_sgmii_power_on(comphy_index,
922*91f16700Schasinglulu 							comphy_mode);
923*91f16700Schasinglulu 		break;
924*91f16700Schasinglulu 	case (COMPHY_USB3_MODE):
925*91f16700Schasinglulu 	case (COMPHY_USB3H_MODE):
926*91f16700Schasinglulu 		ret = mvebu_a3700_comphy_usb3_power_on(comphy_index,
927*91f16700Schasinglulu 						       comphy_mode);
928*91f16700Schasinglulu 		break;
929*91f16700Schasinglulu 	case (COMPHY_PCIE_MODE):
930*91f16700Schasinglulu 		ret = mvebu_a3700_comphy_pcie_power_on(comphy_index,
931*91f16700Schasinglulu 						       comphy_mode);
932*91f16700Schasinglulu 		break;
933*91f16700Schasinglulu 	default:
934*91f16700Schasinglulu 		ERROR("comphy%d: unsupported comphy mode\n", comphy_index);
935*91f16700Schasinglulu 		ret = -EINVAL;
936*91f16700Schasinglulu 		break;
937*91f16700Schasinglulu 	}
938*91f16700Schasinglulu 
939*91f16700Schasinglulu 	debug_exit();
940*91f16700Schasinglulu 
941*91f16700Schasinglulu 	return ret;
942*91f16700Schasinglulu }
943*91f16700Schasinglulu 
944*91f16700Schasinglulu static int mvebu_a3700_comphy_usb3_power_off(void)
945*91f16700Schasinglulu {
946*91f16700Schasinglulu 	/*
947*91f16700Schasinglulu 	 * Currently the USB3 MAC will control the USB3 PHY to set it to low
948*91f16700Schasinglulu 	 * state, thus do not need to power off USB3 PHY again.
949*91f16700Schasinglulu 	 */
950*91f16700Schasinglulu 	debug_enter();
951*91f16700Schasinglulu 	debug_exit();
952*91f16700Schasinglulu 
953*91f16700Schasinglulu 	return 0;
954*91f16700Schasinglulu }
955*91f16700Schasinglulu 
956*91f16700Schasinglulu static int mvebu_a3700_comphy_sata_power_off(void)
957*91f16700Schasinglulu {
958*91f16700Schasinglulu 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
959*91f16700Schasinglulu 	uint32_t offset;
960*91f16700Schasinglulu 
961*91f16700Schasinglulu 	debug_enter();
962*91f16700Schasinglulu 
963*91f16700Schasinglulu 	/* Set phy isolation mode */
964*91f16700Schasinglulu 	offset = COMPHY_ISOLATION_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
965*91f16700Schasinglulu 	comphy_sata_set_indirect(comphy_indir_regs, offset, PHY_ISOLATE_MODE,
966*91f16700Schasinglulu 				 PHY_ISOLATE_MODE);
967*91f16700Schasinglulu 
968*91f16700Schasinglulu 	/* Power off PLL, Tx, Rx */
969*91f16700Schasinglulu 	offset = COMPHY_POWER_PLL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
970*91f16700Schasinglulu 	comphy_sata_set_indirect(comphy_indir_regs, offset, 0,
971*91f16700Schasinglulu 				 PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT);
972*91f16700Schasinglulu 
973*91f16700Schasinglulu 	debug_exit();
974*91f16700Schasinglulu 
975*91f16700Schasinglulu 	return 0;
976*91f16700Schasinglulu }
977*91f16700Schasinglulu 
978*91f16700Schasinglulu int mvebu_3700_comphy_power_off(uint8_t comphy_index, uint32_t comphy_mode)
979*91f16700Schasinglulu {
980*91f16700Schasinglulu 	int mode = COMPHY_GET_MODE(comphy_mode);
981*91f16700Schasinglulu 	int err = 0;
982*91f16700Schasinglulu 
983*91f16700Schasinglulu 	debug_enter();
984*91f16700Schasinglulu 
985*91f16700Schasinglulu 	if (!mode) {
986*91f16700Schasinglulu 		/*
987*91f16700Schasinglulu 		 * The user did not specify which mode should be powered off.
988*91f16700Schasinglulu 		 * In this case we can identify this by reading the phy selector
989*91f16700Schasinglulu 		 * register.
990*91f16700Schasinglulu 		 */
991*91f16700Schasinglulu 		mode = mvebu_a3700_comphy_get_mode(comphy_index);
992*91f16700Schasinglulu 	}
993*91f16700Schasinglulu 
994*91f16700Schasinglulu 	switch (mode) {
995*91f16700Schasinglulu 	case(COMPHY_SGMII_MODE):
996*91f16700Schasinglulu 	case(COMPHY_2500BASEX_MODE):
997*91f16700Schasinglulu 		err = mvebu_a3700_comphy_sgmii_power_off(comphy_index);
998*91f16700Schasinglulu 		break;
999*91f16700Schasinglulu 	case (COMPHY_USB3_MODE):
1000*91f16700Schasinglulu 	case (COMPHY_USB3H_MODE):
1001*91f16700Schasinglulu 		err = mvebu_a3700_comphy_usb3_power_off();
1002*91f16700Schasinglulu 		break;
1003*91f16700Schasinglulu 	case (COMPHY_SATA_MODE):
1004*91f16700Schasinglulu 		err = mvebu_a3700_comphy_sata_power_off();
1005*91f16700Schasinglulu 		break;
1006*91f16700Schasinglulu 
1007*91f16700Schasinglulu 	default:
1008*91f16700Schasinglulu 		debug("comphy%d: power off is not implemented for mode %d\n",
1009*91f16700Schasinglulu 		      comphy_index, mode);
1010*91f16700Schasinglulu 		break;
1011*91f16700Schasinglulu 	}
1012*91f16700Schasinglulu 
1013*91f16700Schasinglulu 	debug_exit();
1014*91f16700Schasinglulu 
1015*91f16700Schasinglulu 	return err;
1016*91f16700Schasinglulu }
1017*91f16700Schasinglulu 
1018*91f16700Schasinglulu static int mvebu_a3700_comphy_sata_is_pll_locked(void)
1019*91f16700Schasinglulu {
1020*91f16700Schasinglulu 	uint32_t data, addr;
1021*91f16700Schasinglulu 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
1022*91f16700Schasinglulu 	int ret = 0;
1023*91f16700Schasinglulu 
1024*91f16700Schasinglulu 	debug_enter();
1025*91f16700Schasinglulu 
1026*91f16700Schasinglulu 	/* Polling status */
1027*91f16700Schasinglulu 	mmio_write_32(comphy_indir_regs + COMPHY_LANE2_INDIR_ADDR_OFFSET,
1028*91f16700Schasinglulu 	       COMPHY_DIG_LOOPBACK_EN + SATAPHY_LANE2_REG_BASE_OFFSET);
1029*91f16700Schasinglulu 	addr = comphy_indir_regs + COMPHY_LANE2_INDIR_DATA_OFFSET;
1030*91f16700Schasinglulu 	data = polling_with_timeout(addr, PLL_READY_TX_BIT, PLL_READY_TX_BIT,
1031*91f16700Schasinglulu 				    COMPHY_PLL_TIMEOUT, REG_32BIT);
1032*91f16700Schasinglulu 
1033*91f16700Schasinglulu 	if (data != 0) {
1034*91f16700Schasinglulu 		ERROR("TX PLL is not locked\n");
1035*91f16700Schasinglulu 		ret = -ETIMEDOUT;
1036*91f16700Schasinglulu 	}
1037*91f16700Schasinglulu 
1038*91f16700Schasinglulu 	debug_exit();
1039*91f16700Schasinglulu 
1040*91f16700Schasinglulu 	return ret;
1041*91f16700Schasinglulu }
1042*91f16700Schasinglulu 
1043*91f16700Schasinglulu int mvebu_3700_comphy_is_pll_locked(uint8_t comphy_index, uint32_t comphy_mode)
1044*91f16700Schasinglulu {
1045*91f16700Schasinglulu 	int mode = COMPHY_GET_MODE(comphy_mode);
1046*91f16700Schasinglulu 	int ret = 0;
1047*91f16700Schasinglulu 
1048*91f16700Schasinglulu 	debug_enter();
1049*91f16700Schasinglulu 
1050*91f16700Schasinglulu 	switch (mode) {
1051*91f16700Schasinglulu 	case(COMPHY_SATA_MODE):
1052*91f16700Schasinglulu 		ret = mvebu_a3700_comphy_sata_is_pll_locked();
1053*91f16700Schasinglulu 		break;
1054*91f16700Schasinglulu 
1055*91f16700Schasinglulu 	default:
1056*91f16700Schasinglulu 		ERROR("comphy[%d] mode[%d] doesn't support PLL lock check\n",
1057*91f16700Schasinglulu 			comphy_index, mode);
1058*91f16700Schasinglulu 		ret = -EINVAL;
1059*91f16700Schasinglulu 		break;
1060*91f16700Schasinglulu 	}
1061*91f16700Schasinglulu 
1062*91f16700Schasinglulu 	debug_exit();
1063*91f16700Schasinglulu 
1064*91f16700Schasinglulu 	return ret;
1065*91f16700Schasinglulu }
1066