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