xref: /rk3399_ARM-atf/drivers/marvell/comphy/phy-comphy-3700.c (revision be33dce7407ddec0d6dcab58e7b206688f4a6b22)
16f8de19fSKonstantin Porotchkin /*
26f8de19fSKonstantin Porotchkin  * Copyright (C) 2018 Marvell International Ltd.
36f8de19fSKonstantin Porotchkin  *
46f8de19fSKonstantin Porotchkin  * SPDX-License-Identifier:     BSD-3-Clause
56f8de19fSKonstantin Porotchkin  * https://spdx.org/licenses
66f8de19fSKonstantin Porotchkin  */
76f8de19fSKonstantin Porotchkin 
86f8de19fSKonstantin Porotchkin #include <errno.h>
909d40e0eSAntonio Nino Diaz 
1009d40e0eSAntonio Nino Diaz #include <common/debug.h>
1109d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
1209d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1309d40e0eSAntonio Nino Diaz #include <lib/spinlock.h>
1409d40e0eSAntonio Nino Diaz 
156f8de19fSKonstantin Porotchkin #include <mvebu.h>
166f8de19fSKonstantin Porotchkin #include <mvebu_def.h>
175a91c439SPali Rohár #include <plat_marvell.h>
1809d40e0eSAntonio Nino Diaz 
196f8de19fSKonstantin Porotchkin #include "phy-comphy-3700.h"
206f8de19fSKonstantin Porotchkin #include "phy-comphy-common.h"
216f8de19fSKonstantin Porotchkin 
226f8de19fSKonstantin Porotchkin /*
236f8de19fSKonstantin Porotchkin  * COMPHY_INDIRECT_REG points to ahci address space but the ahci region used in
246f8de19fSKonstantin Porotchkin  * Linux is up to 0x178 so none will access it from Linux in runtime
256f8de19fSKonstantin Porotchkin  * concurrently.
266f8de19fSKonstantin Porotchkin  */
276f8de19fSKonstantin Porotchkin #define COMPHY_INDIRECT_REG	(MVEBU_REGS_BASE + 0xE0178)
286f8de19fSKonstantin Porotchkin 
296f8de19fSKonstantin Porotchkin /* The USB3_GBE1_PHY range is above USB3 registers used in dts */
306f8de19fSKonstantin Porotchkin #define USB3_GBE1_PHY		(MVEBU_REGS_BASE + 0x5C000)
316f8de19fSKonstantin Porotchkin #define COMPHY_SD_ADDR		(MVEBU_REGS_BASE + 0x1F000)
326f8de19fSKonstantin Porotchkin 
336f8de19fSKonstantin Porotchkin struct sgmii_phy_init_data_fix {
346f8de19fSKonstantin Porotchkin 	uint16_t addr;
356f8de19fSKonstantin Porotchkin 	uint16_t value;
366f8de19fSKonstantin Porotchkin };
376f8de19fSKonstantin Porotchkin 
386f8de19fSKonstantin Porotchkin /* Changes to 40M1G25 mode data required for running 40M3G125 init mode */
396f8de19fSKonstantin Porotchkin static struct sgmii_phy_init_data_fix sgmii_phy_init_fix[] = {
406f8de19fSKonstantin Porotchkin 	{0x005, 0x07CC}, {0x015, 0x0000}, {0x01B, 0x0000}, {0x01D, 0x0000},
416f8de19fSKonstantin Porotchkin 	{0x01E, 0x0000}, {0x01F, 0x0000}, {0x020, 0x0000}, {0x021, 0x0030},
426f8de19fSKonstantin Porotchkin 	{0x026, 0x0888}, {0x04D, 0x0152}, {0x04F, 0xA020}, {0x050, 0x07CC},
436f8de19fSKonstantin Porotchkin 	{0x053, 0xE9CA}, {0x055, 0xBD97}, {0x071, 0x3015}, {0x076, 0x03AA},
446f8de19fSKonstantin Porotchkin 	{0x07C, 0x0FDF}, {0x0C2, 0x3030}, {0x0C3, 0x8000}, {0x0E2, 0x5550},
456f8de19fSKonstantin Porotchkin 	{0x0E3, 0x12A4}, {0x0E4, 0x7D00}, {0x0E6, 0x0C83}, {0x101, 0xFCC0},
466f8de19fSKonstantin Porotchkin 	{0x104, 0x0C10}
476f8de19fSKonstantin Porotchkin };
486f8de19fSKonstantin Porotchkin 
496f8de19fSKonstantin Porotchkin /* 40M1G25 mode init data */
506f8de19fSKonstantin Porotchkin static uint16_t sgmii_phy_init[512] = {
516f8de19fSKonstantin Porotchkin 	/* 0       1       2       3       4       5       6       7 */
526f8de19fSKonstantin Porotchkin 	/*-----------------------------------------------------------*/
536f8de19fSKonstantin Porotchkin 	/* 8       9       A       B       C       D       E       F */
546f8de19fSKonstantin Porotchkin 	0x3110, 0xFD83, 0x6430, 0x412F, 0x82C0, 0x06FA, 0x4500, 0x6D26,	/* 00 */
556f8de19fSKonstantin Porotchkin 	0xAFC0, 0x8000, 0xC000, 0x0000, 0x2000, 0x49CC, 0x0BC9, 0x2A52,	/* 08 */
566f8de19fSKonstantin Porotchkin 	0x0BD2, 0x0CDE, 0x13D2, 0x0CE8, 0x1149, 0x10E0, 0x0000, 0x0000,	/* 10 */
576f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x4134, 0x0D2D, 0xFFFF,	/* 18 */
586f8de19fSKonstantin Porotchkin 	0xFFE0, 0x4030, 0x1016, 0x0030, 0x0000, 0x0800, 0x0866, 0x0000,	/* 20 */
596f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,	/* 28 */
606f8de19fSKonstantin Porotchkin 	0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* 30 */
616f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x000F, 0x6A62, 0x1988, 0x3100, 0x3100, 0x3100,	/* 38 */
626f8de19fSKonstantin Porotchkin 	0x3100, 0xA708, 0x2430, 0x0830, 0x1030, 0x4610, 0xFF00, 0xFF00,	/* 40 */
636f8de19fSKonstantin Porotchkin 	0x0060, 0x1000, 0x0400, 0x0040, 0x00F0, 0x0155, 0x1100, 0xA02A,	/* 48 */
646f8de19fSKonstantin Porotchkin 	0x06FA, 0x0080, 0xB008, 0xE3ED, 0x5002, 0xB592, 0x7A80, 0x0001,	/* 50 */
656f8de19fSKonstantin Porotchkin 	0x020A, 0x8820, 0x6014, 0x8054, 0xACAA, 0xFC88, 0x2A02, 0x45CF,	/* 58 */
666f8de19fSKonstantin Porotchkin 	0x000F, 0x1817, 0x2860, 0x064F, 0x0000, 0x0204, 0x1800, 0x6000,	/* 60 */
676f8de19fSKonstantin Porotchkin 	0x810F, 0x4F23, 0x4000, 0x4498, 0x0850, 0x0000, 0x000E, 0x1002,	/* 68 */
686f8de19fSKonstantin Porotchkin 	0x9D3A, 0x3009, 0xD066, 0x0491, 0x0001, 0x6AB0, 0x0399, 0x3780,	/* 70 */
696f8de19fSKonstantin Porotchkin 	0x0040, 0x5AC0, 0x4A80, 0x0000, 0x01DF, 0x0000, 0x0007, 0x0000,	/* 78 */
706f8de19fSKonstantin Porotchkin 	0x2D54, 0x00A1, 0x4000, 0x0100, 0xA20A, 0x0000, 0x0000, 0x0000,	/* 80 */
716f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x7400, 0x0E81, 0x1000, 0x1242, 0x0210,	/* 88 */
726f8de19fSKonstantin Porotchkin 	0x80DF, 0x0F1F, 0x2F3F, 0x4F5F, 0x6F7F, 0x0F1F, 0x2F3F, 0x4F5F,	/* 90 */
736f8de19fSKonstantin Porotchkin 	0x6F7F, 0x4BAD, 0x0000, 0x0000, 0x0800, 0x0000, 0x2400, 0xB651,	/* 98 */
746f8de19fSKonstantin Porotchkin 	0xC9E0, 0x4247, 0x0A24, 0x0000, 0xAF19, 0x1004, 0x0000, 0x0000,	/* A0 */
756f8de19fSKonstantin Porotchkin 	0x0000, 0x0013, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* A8 */
766f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* B0 */
776f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0060, 0x0000, 0x0000, 0x0000, 0x0000,	/* B8 */
786f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x3010, 0xFA00, 0x0000, 0x0000, 0x0000, 0x0003,	/* C0 */
796f8de19fSKonstantin Porotchkin 	0x1618, 0x8200, 0x8000, 0x0400, 0x050F, 0x0000, 0x0000, 0x0000,	/* C8 */
806f8de19fSKonstantin Porotchkin 	0x4C93, 0x0000, 0x1000, 0x1120, 0x0010, 0x1242, 0x1242, 0x1E00,	/* D0 */
816f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x00F8, 0x0000, 0x0041, 0x0800, 0x0000,	/* D8 */
826f8de19fSKonstantin Porotchkin 	0x82A0, 0x572E, 0x2490, 0x14A9, 0x4E00, 0x0000, 0x0803, 0x0541,	/* E0 */
836f8de19fSKonstantin Porotchkin 	0x0C15, 0x0000, 0x0000, 0x0400, 0x2626, 0x0000, 0x0000, 0x4200,	/* E8 */
846f8de19fSKonstantin Porotchkin 	0x0000, 0xAA55, 0x1020, 0x0000, 0x0000, 0x5010, 0x0000, 0x0000,	/* F0 */
856f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x5000, 0x0000, 0x0000, 0x0000, 0x02F2, 0x0000,	/* F8 */
866f8de19fSKonstantin Porotchkin 	0x101F, 0xFDC0, 0x4000, 0x8010, 0x0110, 0x0006, 0x0000, 0x0000,	/*100 */
876f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*108 */
886f8de19fSKonstantin Porotchkin 	0x04CF, 0x0000, 0x04CF, 0x0000, 0x04CF, 0x0000, 0x04C6, 0x0000,	/*110 */
896f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*118 */
906f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*120 */
916f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*128 */
926f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*130 */
936f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*138 */
946f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*140 */
956f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*148 */
966f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*150 */
976f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*158 */
986f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*160 */
996f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*168 */
1006f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*170 */
1016f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x00F0, 0x08A2, 0x3112, 0x0A14, 0x0000,	/*178 */
1026f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*180 */
1036f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*188 */
1046f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*190 */
1056f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*198 */
1066f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1A0 */
1076f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1A8 */
1086f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1B0 */
1096f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1B8 */
1106f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1C0 */
1116f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1C8 */
1126f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1D0 */
1136f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1D8 */
1146f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1E0 */
1156f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1E8 */
1166f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1F0 */
1176f8de19fSKonstantin Porotchkin 	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000	/*1F8 */
1186f8de19fSKonstantin Porotchkin };
1196f8de19fSKonstantin Porotchkin 
1206f8de19fSKonstantin Porotchkin /* PHY selector configures with corresponding modes */
1216f8de19fSKonstantin Porotchkin static void mvebu_a3700_comphy_set_phy_selector(uint8_t comphy_index,
1226f8de19fSKonstantin Porotchkin 						uint32_t comphy_mode)
1236f8de19fSKonstantin Porotchkin {
1246f8de19fSKonstantin Porotchkin 	uint32_t reg;
1256f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
1266f8de19fSKonstantin Porotchkin 
1276f8de19fSKonstantin Porotchkin 	reg = mmio_read_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG);
1286f8de19fSKonstantin Porotchkin 	switch (mode) {
1296f8de19fSKonstantin Porotchkin 	case (COMPHY_SATA_MODE):
1306f8de19fSKonstantin Porotchkin 		/* SATA must be in Lane2 */
1316f8de19fSKonstantin Porotchkin 		if (comphy_index == COMPHY_LANE2)
1326f8de19fSKonstantin Porotchkin 			reg &= ~COMPHY_SELECTOR_USB3_PHY_SEL_BIT;
1336f8de19fSKonstantin Porotchkin 		else
1346f8de19fSKonstantin Porotchkin 			goto error;
1356f8de19fSKonstantin Porotchkin 		break;
1366f8de19fSKonstantin Porotchkin 
1376f8de19fSKonstantin Porotchkin 	case (COMPHY_SGMII_MODE):
138a669983cSPali Rohár 	case (COMPHY_2500BASEX_MODE):
1396f8de19fSKonstantin Porotchkin 		if (comphy_index == COMPHY_LANE0)
1406f8de19fSKonstantin Porotchkin 			reg &= ~COMPHY_SELECTOR_USB3_GBE1_SEL_BIT;
1416f8de19fSKonstantin Porotchkin 		else if (comphy_index == COMPHY_LANE1)
1426f8de19fSKonstantin Porotchkin 			reg &= ~COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT;
1436f8de19fSKonstantin Porotchkin 		else
1446f8de19fSKonstantin Porotchkin 			goto error;
1456f8de19fSKonstantin Porotchkin 		break;
1466f8de19fSKonstantin Porotchkin 
1476f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3H_MODE):
1486f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3D_MODE):
1496f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3_MODE):
1506f8de19fSKonstantin Porotchkin 		if (comphy_index == COMPHY_LANE2)
1516f8de19fSKonstantin Porotchkin 			reg |= COMPHY_SELECTOR_USB3_PHY_SEL_BIT;
1526f8de19fSKonstantin Porotchkin 		else if (comphy_index == COMPHY_LANE0)
1536f8de19fSKonstantin Porotchkin 			reg |= COMPHY_SELECTOR_USB3_GBE1_SEL_BIT;
1546f8de19fSKonstantin Porotchkin 		else
1556f8de19fSKonstantin Porotchkin 			goto error;
1566f8de19fSKonstantin Porotchkin 		break;
1576f8de19fSKonstantin Porotchkin 
1586f8de19fSKonstantin Porotchkin 	case (COMPHY_PCIE_MODE):
1596f8de19fSKonstantin Porotchkin 		/* PCIE must be in Lane1 */
1606f8de19fSKonstantin Porotchkin 		if (comphy_index == COMPHY_LANE1)
1616f8de19fSKonstantin Porotchkin 			reg |= COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT;
1626f8de19fSKonstantin Porotchkin 		else
1636f8de19fSKonstantin Porotchkin 			goto error;
1646f8de19fSKonstantin Porotchkin 		break;
1656f8de19fSKonstantin Porotchkin 
1666f8de19fSKonstantin Porotchkin 	default:
1676f8de19fSKonstantin Porotchkin 		goto error;
1686f8de19fSKonstantin Porotchkin 	}
1696f8de19fSKonstantin Porotchkin 
1706f8de19fSKonstantin Porotchkin 	mmio_write_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG, reg);
1716f8de19fSKonstantin Porotchkin 	return;
1726f8de19fSKonstantin Porotchkin error:
1736f8de19fSKonstantin Porotchkin 	ERROR("COMPHY[%d] mode[%d] is invalid\n", comphy_index, mode);
1746f8de19fSKonstantin Porotchkin }
1756f8de19fSKonstantin Porotchkin 
176629dd61fSMarek Behún /*
177629dd61fSMarek Behún  * This is something like the inverse of the previous function: for given
178629dd61fSMarek Behún  * lane it returns COMPHY_*_MODE.
179629dd61fSMarek Behún  *
180629dd61fSMarek Behún  * It is useful when powering the phy off.
181629dd61fSMarek Behún  *
182629dd61fSMarek Behún  * This function returns COMPHY_USB3_MODE even if the phy was configured
183629dd61fSMarek Behún  * with COMPHY_USB3D_MODE or COMPHY_USB3H_MODE. (The usb3 phy initialization
184629dd61fSMarek Behún  * code does not differentiate between these modes.)
185629dd61fSMarek Behún  * Also it returns COMPHY_SGMII_MODE even if the phy was configures with
186a669983cSPali Rohár  * COMPHY_2500BASEX_MODE. (The sgmii phy initialization code does differentiate
187629dd61fSMarek Behún  * between these modes, but it is irrelevant when powering the phy off.)
188629dd61fSMarek Behún  */
189629dd61fSMarek Behún static int mvebu_a3700_comphy_get_mode(uint8_t comphy_index)
190629dd61fSMarek Behún {
191629dd61fSMarek Behún 	uint32_t reg;
192629dd61fSMarek Behún 
193629dd61fSMarek Behún 	reg = mmio_read_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG);
194629dd61fSMarek Behún 	switch (comphy_index) {
195629dd61fSMarek Behún 	case COMPHY_LANE0:
196629dd61fSMarek Behún 		if ((reg & COMPHY_SELECTOR_USB3_GBE1_SEL_BIT) != 0)
197629dd61fSMarek Behún 			return COMPHY_USB3_MODE;
198629dd61fSMarek Behún 		else
199629dd61fSMarek Behún 			return COMPHY_SGMII_MODE;
200629dd61fSMarek Behún 	case COMPHY_LANE1:
201629dd61fSMarek Behún 		if ((reg & COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT) != 0)
202629dd61fSMarek Behún 			return COMPHY_PCIE_MODE;
203629dd61fSMarek Behún 		else
204629dd61fSMarek Behún 			return COMPHY_SGMII_MODE;
205629dd61fSMarek Behún 	case COMPHY_LANE2:
206629dd61fSMarek Behún 		if ((reg & COMPHY_SELECTOR_USB3_PHY_SEL_BIT) != 0)
207629dd61fSMarek Behún 			return COMPHY_USB3_MODE;
208629dd61fSMarek Behún 		else
209629dd61fSMarek Behún 			return COMPHY_SATA_MODE;
210629dd61fSMarek Behún 	}
211629dd61fSMarek Behún 
212629dd61fSMarek Behún 	return COMPHY_UNUSED;
213629dd61fSMarek Behún }
214629dd61fSMarek Behún 
2156f8de19fSKonstantin Porotchkin /* It is only used for SATA and USB3 on comphy lane2. */
2166f8de19fSKonstantin Porotchkin static void comphy_set_indirect(uintptr_t addr, uint32_t offset, uint16_t data,
217*be33dce7SPali Rohár 				uint16_t mask, bool is_sata)
2186f8de19fSKonstantin Porotchkin {
2196f8de19fSKonstantin Porotchkin 	/*
2206f8de19fSKonstantin Porotchkin 	 * When Lane 2 PHY is for USB3, access the PHY registers
2216f8de19fSKonstantin Porotchkin 	 * through indirect Address and Data registers:
2226f8de19fSKonstantin Porotchkin 	 * INDIR_ACC_PHY_ADDR (RD00E0178h [31:0]),
2236f8de19fSKonstantin Porotchkin 	 * INDIR_ACC_PHY_DATA (RD00E017Ch [31:0]),
2246f8de19fSKonstantin Porotchkin 	 * within the SATA Host Controller registers, Lane 2 base register
2256f8de19fSKonstantin Porotchkin 	 * offset is 0x200
2266f8de19fSKonstantin Porotchkin 	 */
227*be33dce7SPali Rohár 	if (is_sata) {
2286f8de19fSKonstantin Porotchkin 		mmio_write_32(addr + COMPHY_LANE2_INDIR_ADDR_OFFSET, offset);
229*be33dce7SPali Rohár 	} else {
2306f8de19fSKonstantin Porotchkin 		mmio_write_32(addr + COMPHY_LANE2_INDIR_ADDR_OFFSET,
2316f8de19fSKonstantin Porotchkin 			      offset + USB3PHY_LANE2_REG_BASE_OFFSET);
232*be33dce7SPali Rohár 	}
2336f8de19fSKonstantin Porotchkin 
2346f8de19fSKonstantin Porotchkin 	reg_set(addr + COMPHY_LANE2_INDIR_DATA_OFFSET, data, mask);
2356f8de19fSKonstantin Porotchkin }
2366f8de19fSKonstantin Porotchkin 
237*be33dce7SPali Rohár /* It is only used for SATA on comphy lane2. */
238*be33dce7SPali Rohár static void comphy_sata_set_indirect(uintptr_t addr, uint32_t reg_offset,
239*be33dce7SPali Rohár 				     uint16_t data, uint16_t mask)
240*be33dce7SPali Rohár {
241*be33dce7SPali Rohár 	comphy_set_indirect(addr, reg_offset, data, mask, true);
242*be33dce7SPali Rohár }
243*be33dce7SPali Rohár 
244*be33dce7SPali Rohár /* It is only used for USB3 indirect access on comphy lane2. */
245*be33dce7SPali Rohár static void comphy_usb3_set_indirect(uintptr_t addr, uint32_t reg_offset,
246*be33dce7SPali Rohár 				     uint16_t data, uint16_t mask)
247*be33dce7SPali Rohár {
248*be33dce7SPali Rohár 	comphy_set_indirect(addr, reg_offset, data, mask, false);
249*be33dce7SPali Rohár }
250*be33dce7SPali Rohár 
251*be33dce7SPali Rohár /* It is only used for USB3 direct access not on comphy lane2. */
2526f8de19fSKonstantin Porotchkin static void comphy_usb3_set_direct(uintptr_t addr, uint32_t reg_offset,
253*be33dce7SPali Rohár 				   uint16_t data, uint16_t mask)
2546f8de19fSKonstantin Porotchkin {
2556f8de19fSKonstantin Porotchkin 	reg_set16((reg_offset * PHY_SHFT(USB3) + addr), data, mask);
2566f8de19fSKonstantin Porotchkin }
2576f8de19fSKonstantin Porotchkin 
2586f8de19fSKonstantin Porotchkin static void comphy_sgmii_phy_init(uint32_t comphy_index, uint32_t mode,
2596f8de19fSKonstantin Porotchkin 				  uintptr_t sd_ip_addr)
2606f8de19fSKonstantin Porotchkin {
2616f8de19fSKonstantin Porotchkin 	const int fix_arr_sz = ARRAY_SIZE(sgmii_phy_init_fix);
2626f8de19fSKonstantin Porotchkin 	int addr, fix_idx;
2636f8de19fSKonstantin Porotchkin 	uint16_t val;
2646f8de19fSKonstantin Porotchkin 
2656f8de19fSKonstantin Porotchkin 	fix_idx = 0;
2666f8de19fSKonstantin Porotchkin 	for (addr = 0; addr < 512; addr++) {
2676f8de19fSKonstantin Porotchkin 		/*
2686f8de19fSKonstantin Porotchkin 		 * All PHY register values are defined in full for 3.125Gbps
2696f8de19fSKonstantin Porotchkin 		 * SERDES speed. The values required for 1.25 Gbps are almost
2706f8de19fSKonstantin Porotchkin 		 * the same and only few registers should be "fixed" in
2716f8de19fSKonstantin Porotchkin 		 * comparison to 3.125 Gbps values. These register values are
2726f8de19fSKonstantin Porotchkin 		 * stored in "sgmii_phy_init_fix" array.
2736f8de19fSKonstantin Porotchkin 		 */
2746f8de19fSKonstantin Porotchkin 		if ((mode != COMPHY_SGMII_MODE) &&
2756f8de19fSKonstantin Porotchkin 		    (sgmii_phy_init_fix[fix_idx].addr == addr)) {
2766f8de19fSKonstantin Porotchkin 			/* Use new value */
2776f8de19fSKonstantin Porotchkin 			val = sgmii_phy_init_fix[fix_idx].value;
2786f8de19fSKonstantin Porotchkin 			if (fix_idx < fix_arr_sz)
2796f8de19fSKonstantin Porotchkin 				fix_idx++;
2806f8de19fSKonstantin Porotchkin 		} else {
2816f8de19fSKonstantin Porotchkin 			val = sgmii_phy_init[addr];
2826f8de19fSKonstantin Porotchkin 		}
2836f8de19fSKonstantin Porotchkin 
2846f8de19fSKonstantin Porotchkin 		reg_set16(SGMIIPHY_ADDR(addr, sd_ip_addr), val, 0xFFFF);
2856f8de19fSKonstantin Porotchkin 	}
2866f8de19fSKonstantin Porotchkin }
2876f8de19fSKonstantin Porotchkin 
2886f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_sata_power_on(uint8_t comphy_index,
2896f8de19fSKonstantin Porotchkin 					    uint32_t comphy_mode)
2906f8de19fSKonstantin Porotchkin {
2916f8de19fSKonstantin Porotchkin 	int ret = 0;
2926f8de19fSKonstantin Porotchkin 	uint32_t offset, data = 0, ref_clk;
2936f8de19fSKonstantin Porotchkin 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
2946f8de19fSKonstantin Porotchkin 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
2956f8de19fSKonstantin Porotchkin 
2966f8de19fSKonstantin Porotchkin 	debug_enter();
2976f8de19fSKonstantin Porotchkin 
2986f8de19fSKonstantin Porotchkin 	/* Configure phy selector for SATA */
2996f8de19fSKonstantin Porotchkin 	mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
3006f8de19fSKonstantin Porotchkin 
3016f8de19fSKonstantin Porotchkin 	/* Clear phy isolation mode to make it work in normal mode */
3026f8de19fSKonstantin Porotchkin 	offset =  COMPHY_ISOLATION_CTRL_REG + SATAPHY_LANE2_REG_BASE_OFFSET;
303*be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, 0, PHY_ISOLATE_MODE);
3046f8de19fSKonstantin Porotchkin 
3056f8de19fSKonstantin Porotchkin 	/* 0. Check the Polarity invert bits */
3066f8de19fSKonstantin Porotchkin 	if (invert & COMPHY_POLARITY_TXD_INVERT)
3076f8de19fSKonstantin Porotchkin 		data |= TXD_INVERT_BIT;
3086f8de19fSKonstantin Porotchkin 	if (invert & COMPHY_POLARITY_RXD_INVERT)
3096f8de19fSKonstantin Porotchkin 		data |= RXD_INVERT_BIT;
3106f8de19fSKonstantin Porotchkin 
3116f8de19fSKonstantin Porotchkin 	offset = COMPHY_SYNC_PATTERN_REG + SATAPHY_LANE2_REG_BASE_OFFSET;
312*be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, data, TXD_INVERT_BIT |
313*be33dce7SPali Rohár 				 RXD_INVERT_BIT);
3146f8de19fSKonstantin Porotchkin 
3156f8de19fSKonstantin Porotchkin 	/* 1. Select 40-bit data width width */
3166f8de19fSKonstantin Porotchkin 	offset = COMPHY_LOOPBACK_REG0 + SATAPHY_LANE2_REG_BASE_OFFSET;
317*be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, DATA_WIDTH_40BIT,
318*be33dce7SPali Rohár 				 SEL_DATA_WIDTH_MASK);
3196f8de19fSKonstantin Porotchkin 
3206f8de19fSKonstantin Porotchkin 	/* 2. Select reference clock(25M) and PHY mode (SATA) */
3216f8de19fSKonstantin Porotchkin 	offset = COMPHY_POWER_PLL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
3226f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40)
3236f8de19fSKonstantin Porotchkin 		ref_clk = REF_CLOCK_SPEED_40M;
3246f8de19fSKonstantin Porotchkin 	else
3256f8de19fSKonstantin Porotchkin 		ref_clk = REF_CLOCK_SPEED_25M;
3266f8de19fSKonstantin Porotchkin 
327*be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, ref_clk | PHY_MODE_SATA,
328*be33dce7SPali Rohár 				 REF_FREF_SEL_MASK | PHY_MODE_MASK);
3296f8de19fSKonstantin Porotchkin 
3306f8de19fSKonstantin Porotchkin 	/* 3. Use maximum PLL rate (no power save) */
3316f8de19fSKonstantin Porotchkin 	offset = COMPHY_KVCO_CAL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
332*be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, USE_MAX_PLL_RATE_BIT,
333*be33dce7SPali Rohár 				 USE_MAX_PLL_RATE_BIT);
3346f8de19fSKonstantin Porotchkin 
3356f8de19fSKonstantin Porotchkin 	/* 4. Reset reserved bit */
336*be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, COMPHY_RESERVED_REG, 0,
337*be33dce7SPali Rohár 				 PHYCTRL_FRM_PIN_BIT);
3386f8de19fSKonstantin Porotchkin 
3396f8de19fSKonstantin Porotchkin 	/* 5. Set vendor-specific configuration (It is done in sata driver) */
3406f8de19fSKonstantin Porotchkin 	/* XXX: in U-Boot below sequence was executed in this place, in Linux
3416f8de19fSKonstantin Porotchkin 	 * not.  Now it is done only in U-Boot before this comphy
3426f8de19fSKonstantin Porotchkin 	 * initialization - tests shows that it works ok, but in case of any
3436f8de19fSKonstantin Porotchkin 	 * future problem it is left for reference.
3446f8de19fSKonstantin Porotchkin 	 *   reg_set(MVEBU_REGS_BASE + 0xe00a0, 0, 0xffffffff);
3456f8de19fSKonstantin Porotchkin 	 *   reg_set(MVEBU_REGS_BASE + 0xe00a4, BIT(6), BIT(6));
3466f8de19fSKonstantin Porotchkin 	 */
3476f8de19fSKonstantin Porotchkin 
3486f8de19fSKonstantin Porotchkin 	/* Wait for > 55 us to allow PLL be enabled */
3496f8de19fSKonstantin Porotchkin 	udelay(PLL_SET_DELAY_US);
3506f8de19fSKonstantin Porotchkin 
3516f8de19fSKonstantin Porotchkin 	/* Polling status */
3526f8de19fSKonstantin Porotchkin 	mmio_write_32(comphy_indir_regs + COMPHY_LANE2_INDIR_ADDR_OFFSET,
3536f8de19fSKonstantin Porotchkin 		      COMPHY_LOOPBACK_REG0 + SATAPHY_LANE2_REG_BASE_OFFSET);
3546f8de19fSKonstantin Porotchkin 
3556f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(comphy_indir_regs +
3566f8de19fSKonstantin Porotchkin 				   COMPHY_LANE2_INDIR_DATA_OFFSET,
3576f8de19fSKonstantin Porotchkin 				   PLL_READY_TX_BIT, PLL_READY_TX_BIT,
3586f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
3596f8de19fSKonstantin Porotchkin 
3606f8de19fSKonstantin Porotchkin 	debug_exit();
3616f8de19fSKonstantin Porotchkin 
3626f8de19fSKonstantin Porotchkin 	return ret;
3636f8de19fSKonstantin Porotchkin }
3646f8de19fSKonstantin Porotchkin 
3656f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_sgmii_power_on(uint8_t comphy_index,
3666f8de19fSKonstantin Porotchkin 					     uint32_t comphy_mode)
3676f8de19fSKonstantin Porotchkin {
3686f8de19fSKonstantin Porotchkin 	int ret = 0;
3696f8de19fSKonstantin Porotchkin 	uint32_t mask, data, offset;
3706f8de19fSKonstantin Porotchkin 	uintptr_t sd_ip_addr;
3716f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
3726f8de19fSKonstantin Porotchkin 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
3736f8de19fSKonstantin Porotchkin 
3746f8de19fSKonstantin Porotchkin 	debug_enter();
3756f8de19fSKonstantin Porotchkin 
3766f8de19fSKonstantin Porotchkin 	/* Set selector */
3776f8de19fSKonstantin Porotchkin 	mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
3786f8de19fSKonstantin Porotchkin 
3796f8de19fSKonstantin Porotchkin 	/* Serdes IP Base address
3806f8de19fSKonstantin Porotchkin 	 * COMPHY Lane0 -- USB3/GBE1
3816f8de19fSKonstantin Porotchkin 	 * COMPHY Lane1 -- PCIe/GBE0
3826f8de19fSKonstantin Porotchkin 	 */
3836f8de19fSKonstantin Porotchkin 	if (comphy_index == COMPHY_LANE0) {
3846f8de19fSKonstantin Porotchkin 		/* Get usb3 and gbe */
3856f8de19fSKonstantin Porotchkin 		sd_ip_addr = USB3_GBE1_PHY;
3866f8de19fSKonstantin Porotchkin 	} else
3876f8de19fSKonstantin Porotchkin 		sd_ip_addr = COMPHY_SD_ADDR;
3886f8de19fSKonstantin Porotchkin 
3896f8de19fSKonstantin Porotchkin 	/*
3906f8de19fSKonstantin Porotchkin 	 * 1. Reset PHY by setting PHY input port PIN_RESET=1.
3916f8de19fSKonstantin Porotchkin 	 * 2. Set PHY input port PIN_TX_IDLE=1, PIN_PU_IVREF=1 to keep
3926f8de19fSKonstantin Porotchkin 	 *    PHY TXP/TXN output to idle state during PHY initialization
3936f8de19fSKonstantin Porotchkin 	 * 3. Set PHY input port PIN_PU_PLL=0, PIN_PU_RX=0, PIN_PU_TX=0.
3946f8de19fSKonstantin Porotchkin 	 */
3956f8de19fSKonstantin Porotchkin 	data = PIN_PU_IVEREF_BIT | PIN_TX_IDLE_BIT | PIN_RESET_COMPHY_BIT;
3966f8de19fSKonstantin Porotchkin 	mask = PIN_RESET_CORE_BIT | PIN_PU_PLL_BIT | PIN_PU_RX_BIT |
3976f8de19fSKonstantin Porotchkin 		PIN_PU_TX_BIT;
3986f8de19fSKonstantin Porotchkin 	offset = MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index);
3996f8de19fSKonstantin Porotchkin 	reg_set(offset, data, mask);
4006f8de19fSKonstantin Porotchkin 
4016f8de19fSKonstantin Porotchkin 	/* 4. Release reset to the PHY by setting PIN_RESET=0. */
4026f8de19fSKonstantin Porotchkin 	data = 0;
4036f8de19fSKonstantin Porotchkin 	mask = PIN_RESET_COMPHY_BIT;
4046f8de19fSKonstantin Porotchkin 	reg_set(offset, data, mask);
4056f8de19fSKonstantin Porotchkin 
4066f8de19fSKonstantin Porotchkin 	/*
4076f8de19fSKonstantin Porotchkin 	 * 5. Set PIN_PHY_GEN_TX[3:0] and PIN_PHY_GEN_RX[3:0] to decide COMPHY
4086f8de19fSKonstantin Porotchkin 	 * bit rate
4096f8de19fSKonstantin Porotchkin 	 */
4106f8de19fSKonstantin Porotchkin 	if (mode == COMPHY_SGMII_MODE) {
4116f8de19fSKonstantin Porotchkin 		/* SGMII 1G, SerDes speed 1.25G */
4126f8de19fSKonstantin Porotchkin 		data |= SD_SPEED_1_25_G << GEN_RX_SEL_OFFSET;
4136f8de19fSKonstantin Porotchkin 		data |= SD_SPEED_1_25_G << GEN_TX_SEL_OFFSET;
414a669983cSPali Rohár 	} else if (mode == COMPHY_2500BASEX_MODE) {
415a669983cSPali Rohár 		/* 2500Base-X, SerDes speed 3.125G */
4166f8de19fSKonstantin Porotchkin 		data |= SD_SPEED_2_5_G << GEN_RX_SEL_OFFSET;
4176f8de19fSKonstantin Porotchkin 		data |= SD_SPEED_2_5_G << GEN_TX_SEL_OFFSET;
4186f8de19fSKonstantin Porotchkin 	} else {
4196f8de19fSKonstantin Porotchkin 		/* Other rates are not supported */
4206f8de19fSKonstantin Porotchkin 		ERROR("unsupported SGMII speed on comphy lane%d\n",
4216f8de19fSKonstantin Porotchkin 			comphy_index);
4226f8de19fSKonstantin Porotchkin 		return -EINVAL;
4236f8de19fSKonstantin Porotchkin 	}
4246f8de19fSKonstantin Porotchkin 	mask = GEN_RX_SEL_MASK | GEN_TX_SEL_MASK;
4256f8de19fSKonstantin Porotchkin 	reg_set(offset, data, mask);
4266f8de19fSKonstantin Porotchkin 
4276f8de19fSKonstantin Porotchkin 	/*
4286f8de19fSKonstantin Porotchkin 	 * 6. Wait 10mS for bandgap and reference clocks to stabilize; then
4296f8de19fSKonstantin Porotchkin 	 * start SW programming.
4306f8de19fSKonstantin Porotchkin 	 */
4316f8de19fSKonstantin Porotchkin 	mdelay(10);
4326f8de19fSKonstantin Porotchkin 
4336f8de19fSKonstantin Porotchkin 	/* 7. Program COMPHY register PHY_MODE */
4346f8de19fSKonstantin Porotchkin 	data = PHY_MODE_SGMII;
4356f8de19fSKonstantin Porotchkin 	mask = PHY_MODE_MASK;
4366f8de19fSKonstantin Porotchkin 	reg_set16(SGMIIPHY_ADDR(COMPHY_POWER_PLL_CTRL, sd_ip_addr), data, mask);
4376f8de19fSKonstantin Porotchkin 
4386f8de19fSKonstantin Porotchkin 	/*
4396f8de19fSKonstantin Porotchkin 	 * 8. Set COMPHY register REFCLK_SEL to select the correct REFCLK
4406f8de19fSKonstantin Porotchkin 	 * source
4416f8de19fSKonstantin Porotchkin 	 */
4426f8de19fSKonstantin Porotchkin 	data = 0;
4436f8de19fSKonstantin Porotchkin 	mask = PHY_REF_CLK_SEL;
4446f8de19fSKonstantin Porotchkin 	reg_set16(SGMIIPHY_ADDR(COMPHY_MISC_REG0_ADDR, sd_ip_addr), data, mask);
4456f8de19fSKonstantin Porotchkin 
4466f8de19fSKonstantin Porotchkin 	/*
4476f8de19fSKonstantin Porotchkin 	 * 9. Set correct reference clock frequency in COMPHY register
4486f8de19fSKonstantin Porotchkin 	 * REF_FREF_SEL.
4496f8de19fSKonstantin Porotchkin 	 */
4506f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40)
4516f8de19fSKonstantin Porotchkin 		data = REF_CLOCK_SPEED_50M;
4526f8de19fSKonstantin Porotchkin 	else
4536f8de19fSKonstantin Porotchkin 		data = REF_CLOCK_SPEED_25M;
4546f8de19fSKonstantin Porotchkin 
4556f8de19fSKonstantin Porotchkin 	mask = REF_FREF_SEL_MASK;
4566f8de19fSKonstantin Porotchkin 	reg_set16(SGMIIPHY_ADDR(COMPHY_POWER_PLL_CTRL, sd_ip_addr), data, mask);
4576f8de19fSKonstantin Porotchkin 
4586f8de19fSKonstantin Porotchkin 	/* 10. Program COMPHY register PHY_GEN_MAX[1:0]
4596f8de19fSKonstantin Porotchkin 	 * This step is mentioned in the flow received from verification team.
4606f8de19fSKonstantin Porotchkin 	 * However the PHY_GEN_MAX value is only meaningful for other interfaces
4616f8de19fSKonstantin Porotchkin 	 * (not SGMII). For instance, it selects SATA speed 1.5/3/6 Gbps or PCIe
4626f8de19fSKonstantin Porotchkin 	 * speed 2.5/5 Gbps
4636f8de19fSKonstantin Porotchkin 	 */
4646f8de19fSKonstantin Porotchkin 
4656f8de19fSKonstantin Porotchkin 	/*
4666f8de19fSKonstantin Porotchkin 	 * 11. Program COMPHY register SEL_BITS to set correct parallel data
4676f8de19fSKonstantin Porotchkin 	 * bus width
4686f8de19fSKonstantin Porotchkin 	 */
4696f8de19fSKonstantin Porotchkin 	data = DATA_WIDTH_10BIT;
4706f8de19fSKonstantin Porotchkin 	mask = SEL_DATA_WIDTH_MASK;
4716f8de19fSKonstantin Porotchkin 	reg_set16(SGMIIPHY_ADDR(COMPHY_LOOPBACK_REG0, sd_ip_addr), data, mask);
4726f8de19fSKonstantin Porotchkin 
4736f8de19fSKonstantin Porotchkin 	/*
4746f8de19fSKonstantin Porotchkin 	 * 12. As long as DFE function needs to be enabled in any mode,
4756f8de19fSKonstantin Porotchkin 	 * COMPHY register DFE_UPDATE_EN[5:0] shall be programmed to 0x3F
4766f8de19fSKonstantin Porotchkin 	 * for real chip during COMPHY power on.
4776f8de19fSKonstantin Porotchkin 	 * The step 14 exists (and empty) in the original initialization flow
4786f8de19fSKonstantin Porotchkin 	 * obtained from the verification team. According to the functional
4796f8de19fSKonstantin Porotchkin 	 * specification DFE_UPDATE_EN already has the default value 0x3F
4806f8de19fSKonstantin Porotchkin 	 */
4816f8de19fSKonstantin Porotchkin 
4826f8de19fSKonstantin Porotchkin 	/*
4836f8de19fSKonstantin Porotchkin 	 * 13. Program COMPHY GEN registers.
4846f8de19fSKonstantin Porotchkin 	 * These registers should be programmed based on the lab testing result
4856f8de19fSKonstantin Porotchkin 	 * to achieve optimal performance. Please contact the CEA group to get
4866f8de19fSKonstantin Porotchkin 	 * the related GEN table during real chip bring-up. We only required to
4876f8de19fSKonstantin Porotchkin 	 * run though the entire registers programming flow defined by
4886f8de19fSKonstantin Porotchkin 	 * "comphy_sgmii_phy_init" when the REF clock is 40 MHz. For REF clock
4896f8de19fSKonstantin Porotchkin 	 * 25 MHz the default values stored in PHY registers are OK.
4906f8de19fSKonstantin Porotchkin 	 */
4916f8de19fSKonstantin Porotchkin 	debug("Running C-DPI phy init %s mode\n",
492a669983cSPali Rohár 	      mode == COMPHY_2500BASEX_MODE ? "2G5" : "1G");
4936f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40)
4946f8de19fSKonstantin Porotchkin 		comphy_sgmii_phy_init(comphy_index, mode, sd_ip_addr);
4956f8de19fSKonstantin Porotchkin 
4966f8de19fSKonstantin Porotchkin 	/*
4976f8de19fSKonstantin Porotchkin 	 * 14. [Simulation Only] should not be used for real chip.
4986f8de19fSKonstantin Porotchkin 	 * By pass power up calibration by programming EXT_FORCE_CAL_DONE
4996f8de19fSKonstantin Porotchkin 	 * (R02h[9]) to 1 to shorten COMPHY simulation time.
5006f8de19fSKonstantin Porotchkin 	 */
5016f8de19fSKonstantin Porotchkin 
5026f8de19fSKonstantin Porotchkin 	/*
5036f8de19fSKonstantin Porotchkin 	 * 15. [Simulation Only: should not be used for real chip]
5046f8de19fSKonstantin Porotchkin 	 * Program COMPHY register FAST_DFE_TIMER_EN=1 to shorten RX training
5056f8de19fSKonstantin Porotchkin 	 * simulation time.
5066f8de19fSKonstantin Porotchkin 	 */
5076f8de19fSKonstantin Porotchkin 
5086f8de19fSKonstantin Porotchkin 	/*
5096f8de19fSKonstantin Porotchkin 	 * 16. Check the PHY Polarity invert bit
5106f8de19fSKonstantin Porotchkin 	 */
5116f8de19fSKonstantin Porotchkin 	data = 0x0;
5126f8de19fSKonstantin Porotchkin 	if (invert & COMPHY_POLARITY_TXD_INVERT)
5136f8de19fSKonstantin Porotchkin 		data |= TXD_INVERT_BIT;
5146f8de19fSKonstantin Porotchkin 	if (invert & COMPHY_POLARITY_RXD_INVERT)
5156f8de19fSKonstantin Porotchkin 		data |= RXD_INVERT_BIT;
516ee4c70ebSPali Rohár 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
517ee4c70ebSPali Rohár 	reg_set16(SGMIIPHY_ADDR(COMPHY_SYNC_PATTERN_REG, sd_ip_addr), data, mask);
5186f8de19fSKonstantin Porotchkin 
5196f8de19fSKonstantin Porotchkin 	/*
5206f8de19fSKonstantin Porotchkin 	 * 17. Set PHY input ports PIN_PU_PLL, PIN_PU_TX and PIN_PU_RX to 1 to
5216f8de19fSKonstantin Porotchkin 	 * start PHY power up sequence. All the PHY register programming should
5226f8de19fSKonstantin Porotchkin 	 * be done before PIN_PU_PLL=1. There should be no register programming
5236f8de19fSKonstantin Porotchkin 	 * for normal PHY operation from this point.
5246f8de19fSKonstantin Porotchkin 	 */
5256f8de19fSKonstantin Porotchkin 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
5266f8de19fSKonstantin Porotchkin 		PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT,
5276f8de19fSKonstantin Porotchkin 		PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT);
5286f8de19fSKonstantin Porotchkin 
5296f8de19fSKonstantin Porotchkin 	/*
5306f8de19fSKonstantin Porotchkin 	 * 18. Wait for PHY power up sequence to finish by checking output ports
5316f8de19fSKonstantin Porotchkin 	 * PIN_PLL_READY_TX=1 and PIN_PLL_READY_RX=1.
5326f8de19fSKonstantin Porotchkin 	 */
5336f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
5346f8de19fSKonstantin Porotchkin 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
5356f8de19fSKonstantin Porotchkin 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
5366f8de19fSKonstantin Porotchkin 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
5376f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
5386f8de19fSKonstantin Porotchkin 	if (ret)
5396f8de19fSKonstantin Porotchkin 		ERROR("Failed to lock PLL for SGMII PHY %d\n", comphy_index);
5406f8de19fSKonstantin Porotchkin 
5416f8de19fSKonstantin Porotchkin 	/*
5426f8de19fSKonstantin Porotchkin 	 * 19. Set COMPHY input port PIN_TX_IDLE=0
5436f8de19fSKonstantin Porotchkin 	 */
5446f8de19fSKonstantin Porotchkin 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
5456f8de19fSKonstantin Porotchkin 		0x0, PIN_TX_IDLE_BIT);
5466f8de19fSKonstantin Porotchkin 
5476f8de19fSKonstantin Porotchkin 	/*
5486f8de19fSKonstantin Porotchkin 	 * 20. After valid data appear on PIN_RXDATA bus, set PIN_RX_INIT=1. To
5496f8de19fSKonstantin Porotchkin 	 * start RX initialization. PIN_RX_INIT_DONE will be cleared to 0 by the
5506f8de19fSKonstantin Porotchkin 	 * PHY After RX initialization is done, PIN_RX_INIT_DONE will be set to
5516f8de19fSKonstantin Porotchkin 	 * 1 by COMPHY Set PIN_RX_INIT=0 after PIN_RX_INIT_DONE= 1. Please
5526f8de19fSKonstantin Porotchkin 	 * refer to RX initialization part for details.
5536f8de19fSKonstantin Porotchkin 	 */
5546f8de19fSKonstantin Porotchkin 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
555ccec1bd5SPali Rohár 		PHY_RX_INIT_BIT, PHY_RX_INIT_BIT);
5566f8de19fSKonstantin Porotchkin 
5576f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
5586f8de19fSKonstantin Porotchkin 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
5596f8de19fSKonstantin Porotchkin 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
5606f8de19fSKonstantin Porotchkin 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
5616f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
5626f8de19fSKonstantin Porotchkin 	if (ret)
5636f8de19fSKonstantin Porotchkin 		ERROR("Failed to lock PLL for SGMII PHY %d\n", comphy_index);
5646f8de19fSKonstantin Porotchkin 
5656f8de19fSKonstantin Porotchkin 
5666f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
5676f8de19fSKonstantin Porotchkin 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
5686f8de19fSKonstantin Porotchkin 				   PHY_RX_INIT_DONE_BIT, PHY_RX_INIT_DONE_BIT,
5696f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
5706f8de19fSKonstantin Porotchkin 	if (ret)
5716f8de19fSKonstantin Porotchkin 		ERROR("Failed to init RX of SGMII PHY %d\n", comphy_index);
5726f8de19fSKonstantin Porotchkin 
5736f8de19fSKonstantin Porotchkin 	debug_exit();
5746f8de19fSKonstantin Porotchkin 
5756f8de19fSKonstantin Porotchkin 	return ret;
5766f8de19fSKonstantin Porotchkin }
5776f8de19fSKonstantin Porotchkin 
578629dd61fSMarek Behún static int mvebu_a3700_comphy_sgmii_power_off(uint8_t comphy_index)
579629dd61fSMarek Behún {
580629dd61fSMarek Behún 	int ret = 0;
581629dd61fSMarek Behún 	uint32_t mask, data, offset;
582629dd61fSMarek Behún 
583629dd61fSMarek Behún 	debug_enter();
584629dd61fSMarek Behún 
585629dd61fSMarek Behún 	data = PIN_RESET_CORE_BIT | PIN_RESET_COMPHY_BIT;
586ccec1bd5SPali Rohár 	mask = data;
587629dd61fSMarek Behún 	offset = MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index);
588629dd61fSMarek Behún 	reg_set(offset, data, mask);
589629dd61fSMarek Behún 
590629dd61fSMarek Behún 	debug_exit();
591629dd61fSMarek Behún 
592629dd61fSMarek Behún 	return ret;
593629dd61fSMarek Behún }
594629dd61fSMarek Behún 
5956f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_usb3_power_on(uint8_t comphy_index,
5966f8de19fSKonstantin Porotchkin 					    uint32_t comphy_mode)
5976f8de19fSKonstantin Porotchkin {
5986f8de19fSKonstantin Porotchkin 	int ret = 0;
5996f8de19fSKonstantin Porotchkin 	uintptr_t reg_base = 0;
6006f8de19fSKonstantin Porotchkin 	uint32_t mask, data, addr, cfg, ref_clk;
6016f8de19fSKonstantin Porotchkin 	void (*usb3_reg_set)(uintptr_t addr, uint32_t reg_offset, uint16_t data,
602*be33dce7SPali Rohár 			     uint16_t mask);
6036f8de19fSKonstantin Porotchkin 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
6046f8de19fSKonstantin Porotchkin 
6056f8de19fSKonstantin Porotchkin 	debug_enter();
6066f8de19fSKonstantin Porotchkin 
6076f8de19fSKonstantin Porotchkin 	/* Set phy seclector */
6086f8de19fSKonstantin Porotchkin 	mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
6096f8de19fSKonstantin Porotchkin 
6106f8de19fSKonstantin Porotchkin 	/* Set usb3 reg access func, Lane2 is indirect access */
6116f8de19fSKonstantin Porotchkin 	if (comphy_index == COMPHY_LANE2) {
612*be33dce7SPali Rohár 		usb3_reg_set = &comphy_usb3_set_indirect;
6136f8de19fSKonstantin Porotchkin 		reg_base = COMPHY_INDIRECT_REG;
6146f8de19fSKonstantin Porotchkin 	} else {
6156f8de19fSKonstantin Porotchkin 		/* Get the direct access register resource and map */
6166f8de19fSKonstantin Porotchkin 		usb3_reg_set = &comphy_usb3_set_direct;
6176f8de19fSKonstantin Porotchkin 		reg_base = USB3_GBE1_PHY;
6186f8de19fSKonstantin Porotchkin 	}
6196f8de19fSKonstantin Porotchkin 
6206f8de19fSKonstantin Porotchkin 	/*
6216f8de19fSKonstantin Porotchkin 	 * 0. Set PHY OTG Control(0x5d034), bit 4, Power up OTG module The
6226f8de19fSKonstantin Porotchkin 	 * register belong to UTMI module, so it is set in UTMI phy driver.
6236f8de19fSKonstantin Porotchkin 	 */
6246f8de19fSKonstantin Porotchkin 
6256f8de19fSKonstantin Porotchkin 	/*
6266f8de19fSKonstantin Porotchkin 	 * 1. Set PRD_TXDEEMPH (3.5db de-emph)
6276f8de19fSKonstantin Porotchkin 	 */
6286f8de19fSKonstantin Porotchkin 	mask = PRD_TXDEEMPH0_MASK | PRD_TXMARGIN_MASK | PRD_TXSWING_MASK |
6296f8de19fSKonstantin Porotchkin 		CFG_TX_ALIGN_POS_MASK;
6306f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_LANE_CFG0_ADDR, PRD_TXDEEMPH0_MASK,
631*be33dce7SPali Rohár 		     mask);
6326f8de19fSKonstantin Porotchkin 
6336f8de19fSKonstantin Porotchkin 	/*
6346f8de19fSKonstantin Porotchkin 	 * 2. Set BIT0: enable transmitter in high impedance mode
6356f8de19fSKonstantin Porotchkin 	 *    Set BIT[3:4]: delay 2 clock cycles for HiZ off latency
6366f8de19fSKonstantin Porotchkin 	 *    Set BIT6: Tx detect Rx at HiZ mode
6376f8de19fSKonstantin Porotchkin 	 *    Unset BIT15: set to 0 to set USB3 De-emphasize level to -3.5db
6386f8de19fSKonstantin Porotchkin 	 *            together with bit 0 of COMPHY_REG_LANE_CFG0_ADDR register
6396f8de19fSKonstantin Porotchkin 	 */
6406f8de19fSKonstantin Porotchkin 	mask = PRD_TXDEEMPH1_MASK | TX_DET_RX_MODE | GEN2_TX_DATA_DLY_MASK |
6416f8de19fSKonstantin Porotchkin 		TX_ELEC_IDLE_MODE_EN;
6426f8de19fSKonstantin Porotchkin 	data = TX_DET_RX_MODE | GEN2_TX_DATA_DLY_DEFT | TX_ELEC_IDLE_MODE_EN;
643*be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_REG_LANE_CFG1_ADDR, data, mask);
6446f8de19fSKonstantin Porotchkin 
6456f8de19fSKonstantin Porotchkin 	/*
6466f8de19fSKonstantin Porotchkin 	 * 3. Set Spread Spectrum Clock Enabled
6476f8de19fSKonstantin Porotchkin 	 */
6486f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_LANE_CFG4_ADDR,
649*be33dce7SPali Rohár 		     SPREAD_SPECTRUM_CLK_EN, SPREAD_SPECTRUM_CLK_EN);
6506f8de19fSKonstantin Porotchkin 
6516f8de19fSKonstantin Porotchkin 	/*
6526f8de19fSKonstantin Porotchkin 	 * 4. Set Override Margining Controls From the MAC:
6536f8de19fSKonstantin Porotchkin 	 *    Use margining signals from lane configuration
6546f8de19fSKonstantin Porotchkin 	 */
6556f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_TEST_MODE_CTRL_ADDR,
656*be33dce7SPali Rohár 		     MODE_MARGIN_OVERRIDE, REG_16_BIT_MASK);
6576f8de19fSKonstantin Porotchkin 
6586f8de19fSKonstantin Porotchkin 	/*
6596f8de19fSKonstantin Porotchkin 	 * 5. Set Lane-to-Lane Bundle Clock Sampling Period = per PCLK cycles
6606f8de19fSKonstantin Porotchkin 	 *    set Mode Clock Source = PCLK is generated from REFCLK
6616f8de19fSKonstantin Porotchkin 	 */
6626f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GLOB_CLK_SRC_LO_ADDR, 0x0,
6636f8de19fSKonstantin Porotchkin 		     (MODE_CLK_SRC | BUNDLE_PERIOD_SEL | BUNDLE_PERIOD_SCALE |
664*be33dce7SPali Rohár 		      BUNDLE_SAMPLE_CTRL | PLL_READY_DLY));
6656f8de19fSKonstantin Porotchkin 
6666f8de19fSKonstantin Porotchkin 	/*
6676f8de19fSKonstantin Porotchkin 	 * 6. Set G2 Spread Spectrum Clock Amplitude at 4K
6686f8de19fSKonstantin Porotchkin 	 */
6696f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GEN2_SET_2,
670*be33dce7SPali Rohár 		     G2_TX_SSC_AMP_VALUE_20, G2_TX_SSC_AMP_MASK);
6716f8de19fSKonstantin Porotchkin 
6726f8de19fSKonstantin Porotchkin 	/*
6736f8de19fSKonstantin Porotchkin 	 * 7. Unset G3 Spread Spectrum Clock Amplitude
6746f8de19fSKonstantin Porotchkin 	 *    set G3 TX and RX Register Master Current Select
6756f8de19fSKonstantin Porotchkin 	 */
6766f8de19fSKonstantin Porotchkin 	mask = G3_TX_SSC_AMP_MASK | G3_VREG_RXTX_MAS_ISET_MASK |
6776f8de19fSKonstantin Porotchkin 		RSVD_PH03FH_6_0_MASK;
6786f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GEN2_SET_3,
679*be33dce7SPali Rohár 		     G3_VREG_RXTX_MAS_ISET_60U, mask);
6806f8de19fSKonstantin Porotchkin 
6816f8de19fSKonstantin Porotchkin 	/*
6826f8de19fSKonstantin Porotchkin 	 * 8. Check crystal jumper setting and program the Power and PLL Control
6836f8de19fSKonstantin Porotchkin 	 * accordingly Change RX wait
6846f8de19fSKonstantin Porotchkin 	 */
6856f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40) {
6866f8de19fSKonstantin Porotchkin 		ref_clk = REF_CLOCK_SPEED_40M;
6876f8de19fSKonstantin Porotchkin 		cfg = CFG_PM_RXDLOZ_WAIT_12_UNIT;
6886f8de19fSKonstantin Porotchkin 
6896f8de19fSKonstantin Porotchkin 	} else {
6906f8de19fSKonstantin Porotchkin 		/* 25 MHz */
6916f8de19fSKonstantin Porotchkin 		ref_clk = USB3_REF_CLOCK_SPEED_25M;
6926f8de19fSKonstantin Porotchkin 		cfg = CFG_PM_RXDLOZ_WAIT_7_UNIT;
6936f8de19fSKonstantin Porotchkin 	}
6946f8de19fSKonstantin Porotchkin 
6956f8de19fSKonstantin Porotchkin 	mask = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
6966f8de19fSKonstantin Porotchkin 		PU_TX_INTP_BIT | PU_DFE_BIT | PLL_LOCK_BIT | PHY_MODE_MASK |
6976f8de19fSKonstantin Porotchkin 		REF_FREF_SEL_MASK;
6986f8de19fSKonstantin Porotchkin 	data = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
6996f8de19fSKonstantin Porotchkin 		PU_TX_INTP_BIT | PU_DFE_BIT | PHY_MODE_USB3 | ref_clk;
700*be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_POWER_PLL_CTRL, data,  mask);
7016f8de19fSKonstantin Porotchkin 
7026f8de19fSKonstantin Porotchkin 	mask = CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
7036f8de19fSKonstantin Porotchkin 		CFG_PM_RXDLOZ_WAIT_MASK;
7046f8de19fSKonstantin Porotchkin 	data = CFG_PM_RXDEN_WAIT_1_UNIT  | cfg;
705*be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_REG_PWR_MGM_TIM1_ADDR, data, mask);
7066f8de19fSKonstantin Porotchkin 
7076f8de19fSKonstantin Porotchkin 	/*
7086f8de19fSKonstantin Porotchkin 	 * 9. Enable idle sync
7096f8de19fSKonstantin Porotchkin 	 */
7106f8de19fSKonstantin Porotchkin 	data = UNIT_CTRL_DEFAULT_VALUE | IDLE_SYNC_EN;
711*be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_REG_UNIT_CTRL_ADDR, data, REG_16_BIT_MASK);
7126f8de19fSKonstantin Porotchkin 
7136f8de19fSKonstantin Porotchkin 	/*
7146f8de19fSKonstantin Porotchkin 	 * 10. Enable the output of 500M clock
7156f8de19fSKonstantin Porotchkin 	 */
7166f8de19fSKonstantin Porotchkin 	data = MISC_REG0_DEFAULT_VALUE | CLK500M_EN;
717*be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_MISC_REG0_ADDR, data, REG_16_BIT_MASK);
7186f8de19fSKonstantin Porotchkin 
7196f8de19fSKonstantin Porotchkin 	/*
7206f8de19fSKonstantin Porotchkin 	 * 11. Set 20-bit data width
7216f8de19fSKonstantin Porotchkin 	 */
7226f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_LOOPBACK_REG0, DATA_WIDTH_20BIT,
723*be33dce7SPali Rohár 		     REG_16_BIT_MASK);
7246f8de19fSKonstantin Porotchkin 
7256f8de19fSKonstantin Porotchkin 	/*
7266f8de19fSKonstantin Porotchkin 	 * 12. Override Speed_PLL value and use MAC PLL
7276f8de19fSKonstantin Porotchkin 	 */
7286f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_KVCO_CAL_CTRL,
7296f8de19fSKonstantin Porotchkin 		     (SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT),
730*be33dce7SPali Rohár 		     REG_16_BIT_MASK);
7316f8de19fSKonstantin Porotchkin 
7326f8de19fSKonstantin Porotchkin 	/*
7336f8de19fSKonstantin Porotchkin 	 * 13. Check the Polarity invert bit
7346f8de19fSKonstantin Porotchkin 	 */
735ee4c70ebSPali Rohár 	data = 0U;
736ee4c70ebSPali Rohár 	if (invert & COMPHY_POLARITY_TXD_INVERT) {
737ee4c70ebSPali Rohár 		data |= TXD_INVERT_BIT;
738ee4c70ebSPali Rohár 	}
739ee4c70ebSPali Rohár 	if (invert & COMPHY_POLARITY_RXD_INVERT) {
740ee4c70ebSPali Rohár 		data |= RXD_INVERT_BIT;
741ee4c70ebSPali Rohár 	}
742ee4c70ebSPali Rohár 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
743*be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_SYNC_PATTERN_REG, data, mask);
7446f8de19fSKonstantin Porotchkin 
7456f8de19fSKonstantin Porotchkin 	/*
7466f8de19fSKonstantin Porotchkin 	 * 14. Set max speed generation to USB3.0 5Gbps
7476f8de19fSKonstantin Porotchkin 	 */
7486f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_SYNC_MASK_GEN_REG, PHY_GEN_USB3_5G,
749*be33dce7SPali Rohár 		     PHY_GEN_MAX_MASK);
7506f8de19fSKonstantin Porotchkin 
7516f8de19fSKonstantin Porotchkin 	/*
7526f8de19fSKonstantin Porotchkin 	 * 15. Set capacitor value for FFE gain peaking to 0xF
7536f8de19fSKonstantin Porotchkin 	 */
7546f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GEN3_SETTINGS_3,
755*be33dce7SPali Rohár 		     COMPHY_GEN_FFE_CAP_SEL_VALUE, COMPHY_GEN_FFE_CAP_SEL_MASK);
7566f8de19fSKonstantin Porotchkin 
7576f8de19fSKonstantin Porotchkin 	/*
7586f8de19fSKonstantin Porotchkin 	 * 16. Release SW reset
7596f8de19fSKonstantin Porotchkin 	 */
7606f8de19fSKonstantin Porotchkin 	data = MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32 | MODE_REFDIV_BY_4;
7616f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GLOB_PHY_CTRL0_ADDR, data,
762*be33dce7SPali Rohár 		     REG_16_BIT_MASK);
7636f8de19fSKonstantin Porotchkin 
7646f8de19fSKonstantin Porotchkin 	/* Wait for > 55 us to allow PCLK be enabled */
7656f8de19fSKonstantin Porotchkin 	udelay(PLL_SET_DELAY_US);
7666f8de19fSKonstantin Porotchkin 
7676f8de19fSKonstantin Porotchkin 	if (comphy_index == COMPHY_LANE2) {
768b662232dSMarek Behún 		data = COMPHY_REG_LANE_STATUS1_ADDR + USB3PHY_LANE2_REG_BASE_OFFSET;
7696f8de19fSKonstantin Porotchkin 		mmio_write_32(reg_base + COMPHY_LANE2_INDIR_ADDR_OFFSET,
7706f8de19fSKonstantin Porotchkin 			      data);
7716f8de19fSKonstantin Porotchkin 
772b662232dSMarek Behún 		addr = reg_base + COMPHY_LANE2_INDIR_DATA_OFFSET;
7736f8de19fSKonstantin Porotchkin 		ret = polling_with_timeout(addr, TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
7746f8de19fSKonstantin Porotchkin 					   COMPHY_PLL_TIMEOUT, REG_32BIT);
7756f8de19fSKonstantin Porotchkin 	} else {
7766f8de19fSKonstantin Porotchkin 		ret = polling_with_timeout(LANE_STATUS1_ADDR(USB3) + reg_base,
7776f8de19fSKonstantin Porotchkin 					   TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
7786f8de19fSKonstantin Porotchkin 					   COMPHY_PLL_TIMEOUT, REG_16BIT);
7796f8de19fSKonstantin Porotchkin 	}
7806f8de19fSKonstantin Porotchkin 	if (ret)
7816f8de19fSKonstantin Porotchkin 		ERROR("Failed to lock USB3 PLL\n");
7826f8de19fSKonstantin Porotchkin 
7836f8de19fSKonstantin Porotchkin 	debug_exit();
7846f8de19fSKonstantin Porotchkin 
7856f8de19fSKonstantin Porotchkin 	return ret;
7866f8de19fSKonstantin Porotchkin }
7876f8de19fSKonstantin Porotchkin 
7886f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_pcie_power_on(uint8_t comphy_index,
7896f8de19fSKonstantin Porotchkin 					    uint32_t comphy_mode)
7906f8de19fSKonstantin Porotchkin {
7916f8de19fSKonstantin Porotchkin 	int ret;
7926f8de19fSKonstantin Porotchkin 	uint32_t ref_clk;
793ee4c70ebSPali Rohár 	uint32_t mask, data;
7946f8de19fSKonstantin Porotchkin 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
7956f8de19fSKonstantin Porotchkin 
7966f8de19fSKonstantin Porotchkin 	debug_enter();
7976f8de19fSKonstantin Porotchkin 
7986f8de19fSKonstantin Porotchkin 	/* 1. Enable max PLL. */
7996f8de19fSKonstantin Porotchkin 	reg_set16(LANE_CFG1_ADDR(PCIE) + COMPHY_SD_ADDR,
800ccec1bd5SPali Rohár 		  USE_MAX_PLL_RATE_EN, USE_MAX_PLL_RATE_EN);
8016f8de19fSKonstantin Porotchkin 
8026f8de19fSKonstantin Porotchkin 	/* 2. Select 20 bit SERDES interface. */
8036f8de19fSKonstantin Porotchkin 	reg_set16(GLOB_CLK_SRC_LO_ADDR(PCIE) + COMPHY_SD_ADDR,
804ccec1bd5SPali Rohár 		  CFG_SEL_20B, CFG_SEL_20B);
8056f8de19fSKonstantin Porotchkin 
8066f8de19fSKonstantin Porotchkin 	/* 3. Force to use reg setting for PCIe mode */
8076f8de19fSKonstantin Porotchkin 	reg_set16(MISC_REG1_ADDR(PCIE) + COMPHY_SD_ADDR,
808ccec1bd5SPali Rohár 		  SEL_BITS_PCIE_FORCE, SEL_BITS_PCIE_FORCE);
8096f8de19fSKonstantin Porotchkin 
8106f8de19fSKonstantin Porotchkin 	/* 4. Change RX wait */
8116f8de19fSKonstantin Porotchkin 	reg_set16(PWR_MGM_TIM1_ADDR(PCIE) + COMPHY_SD_ADDR,
8126f8de19fSKonstantin Porotchkin 		  CFG_PM_RXDEN_WAIT_1_UNIT | CFG_PM_RXDLOZ_WAIT_12_UNIT,
8136f8de19fSKonstantin Porotchkin 		  (CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
8146f8de19fSKonstantin Porotchkin 		   CFG_PM_RXDLOZ_WAIT_MASK));
8156f8de19fSKonstantin Porotchkin 
8166f8de19fSKonstantin Porotchkin 	/* 5. Enable idle sync */
8176f8de19fSKonstantin Porotchkin 	reg_set16(UNIT_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
8186f8de19fSKonstantin Porotchkin 		  UNIT_CTRL_DEFAULT_VALUE | IDLE_SYNC_EN, REG_16_BIT_MASK);
8196f8de19fSKonstantin Porotchkin 
8206f8de19fSKonstantin Porotchkin 	/* 6. Enable the output of 100M/125M/500M clock */
8216f8de19fSKonstantin Porotchkin 	reg_set16(MISC_REG0_ADDR(PCIE) + COMPHY_SD_ADDR,
82240d08192SPali Rohár 		  MISC_REG0_DEFAULT_VALUE | CLK500M_EN | TXDCLK_2X_SEL | CLK100M_125M_EN,
8236f8de19fSKonstantin Porotchkin 		  REG_16_BIT_MASK);
8246f8de19fSKonstantin Porotchkin 
8256f8de19fSKonstantin Porotchkin 	/*
8266f8de19fSKonstantin Porotchkin 	 * 7. Enable TX, PCIE global register, 0xd0074814, it is done in
8276f8de19fSKonstantin Porotchkin 	 * PCI-E driver
8286f8de19fSKonstantin Porotchkin 	 */
8296f8de19fSKonstantin Porotchkin 
8306f8de19fSKonstantin Porotchkin 	/*
8316f8de19fSKonstantin Porotchkin 	 * 8. Check crystal jumper setting and program the Power and PLL
8326f8de19fSKonstantin Porotchkin 	 * Control accordingly
8336f8de19fSKonstantin Porotchkin 	 */
8346f8de19fSKonstantin Porotchkin 
8356f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40)
8366f8de19fSKonstantin Porotchkin 		ref_clk = REF_CLOCK_SPEED_40M;
8376f8de19fSKonstantin Porotchkin 	else
8386f8de19fSKonstantin Porotchkin 		ref_clk = PCIE_REF_CLOCK_SPEED_25M;
8396f8de19fSKonstantin Porotchkin 
8406f8de19fSKonstantin Porotchkin 	reg_set16(PWR_PLL_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
8416f8de19fSKonstantin Porotchkin 		  (PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
8426f8de19fSKonstantin Porotchkin 		   PU_TX_INTP_BIT | PU_DFE_BIT | ref_clk | PHY_MODE_PCIE),
8436f8de19fSKonstantin Porotchkin 		  REG_16_BIT_MASK);
8446f8de19fSKonstantin Porotchkin 
8456f8de19fSKonstantin Porotchkin 	/* 9. Override Speed_PLL value and use MAC PLL */
8466f8de19fSKonstantin Porotchkin 	reg_set16(KVCO_CAL_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
8476f8de19fSKonstantin Porotchkin 		  SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT, REG_16_BIT_MASK);
8486f8de19fSKonstantin Porotchkin 
8496f8de19fSKonstantin Porotchkin 	/* 10. Check the Polarity invert bit */
850ee4c70ebSPali Rohár 	data = 0U;
851ee4c70ebSPali Rohár 	if (invert & COMPHY_POLARITY_TXD_INVERT) {
852ee4c70ebSPali Rohár 		data |= TXD_INVERT_BIT;
853ee4c70ebSPali Rohár 	}
854ee4c70ebSPali Rohár 	if (invert & COMPHY_POLARITY_RXD_INVERT) {
855ee4c70ebSPali Rohár 		data |= RXD_INVERT_BIT;
856ee4c70ebSPali Rohár 	}
857ee4c70ebSPali Rohár 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
858ee4c70ebSPali Rohár 	reg_set16(SYNC_PATTERN_REG_ADDR(PCIE) + COMPHY_SD_ADDR, data, mask);
8596f8de19fSKonstantin Porotchkin 
8606f8de19fSKonstantin Porotchkin 	/* 11. Release SW reset */
8616f8de19fSKonstantin Porotchkin 	reg_set16(GLOB_PHY_CTRL0_ADDR(PCIE) + COMPHY_SD_ADDR,
8626f8de19fSKonstantin Porotchkin 		  MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32,
8636f8de19fSKonstantin Porotchkin 		  SOFT_RESET | MODE_REFDIV);
8646f8de19fSKonstantin Porotchkin 
8656f8de19fSKonstantin Porotchkin 	/* Wait for > 55 us to allow PCLK be enabled */
8666f8de19fSKonstantin Porotchkin 	udelay(PLL_SET_DELAY_US);
8676f8de19fSKonstantin Porotchkin 
8686f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(LANE_STATUS1_ADDR(PCIE) + COMPHY_SD_ADDR,
8696f8de19fSKonstantin Porotchkin 				   TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
8706f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_16BIT);
8716f8de19fSKonstantin Porotchkin 	if (ret)
8726f8de19fSKonstantin Porotchkin 		ERROR("Failed to lock PCIE PLL\n");
8736f8de19fSKonstantin Porotchkin 
8746f8de19fSKonstantin Porotchkin 	debug_exit();
8756f8de19fSKonstantin Porotchkin 
8766f8de19fSKonstantin Porotchkin 	return ret;
8776f8de19fSKonstantin Porotchkin }
8786f8de19fSKonstantin Porotchkin 
8796f8de19fSKonstantin Porotchkin int mvebu_3700_comphy_power_on(uint8_t comphy_index, uint32_t comphy_mode)
8806f8de19fSKonstantin Porotchkin {
8816f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
8826f8de19fSKonstantin Porotchkin 	int ret = 0;
8836f8de19fSKonstantin Porotchkin 
8846f8de19fSKonstantin Porotchkin 	debug_enter();
8856f8de19fSKonstantin Porotchkin 
8866f8de19fSKonstantin Porotchkin 	switch (mode) {
8876f8de19fSKonstantin Porotchkin 	case(COMPHY_SATA_MODE):
8886f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_sata_power_on(comphy_index,
8896f8de19fSKonstantin Porotchkin 						       comphy_mode);
8906f8de19fSKonstantin Porotchkin 		break;
8916f8de19fSKonstantin Porotchkin 	case(COMPHY_SGMII_MODE):
892a669983cSPali Rohár 	case(COMPHY_2500BASEX_MODE):
8936f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_sgmii_power_on(comphy_index,
8946f8de19fSKonstantin Porotchkin 							comphy_mode);
8956f8de19fSKonstantin Porotchkin 		break;
8966f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3_MODE):
8976f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3H_MODE):
8986f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_usb3_power_on(comphy_index,
8996f8de19fSKonstantin Porotchkin 						       comphy_mode);
9006f8de19fSKonstantin Porotchkin 		break;
9016f8de19fSKonstantin Porotchkin 	case (COMPHY_PCIE_MODE):
9026f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_pcie_power_on(comphy_index,
9036f8de19fSKonstantin Porotchkin 						       comphy_mode);
9046f8de19fSKonstantin Porotchkin 		break;
9056f8de19fSKonstantin Porotchkin 	default:
9066f8de19fSKonstantin Porotchkin 		ERROR("comphy%d: unsupported comphy mode\n", comphy_index);
9076f8de19fSKonstantin Porotchkin 		ret = -EINVAL;
9086f8de19fSKonstantin Porotchkin 		break;
9096f8de19fSKonstantin Porotchkin 	}
9106f8de19fSKonstantin Porotchkin 
9116f8de19fSKonstantin Porotchkin 	debug_exit();
9126f8de19fSKonstantin Porotchkin 
9136f8de19fSKonstantin Porotchkin 	return ret;
9146f8de19fSKonstantin Porotchkin }
9156f8de19fSKonstantin Porotchkin 
9166f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_usb3_power_off(void)
9176f8de19fSKonstantin Porotchkin {
9186f8de19fSKonstantin Porotchkin 	/*
9196f8de19fSKonstantin Porotchkin 	 * Currently the USB3 MAC will control the USB3 PHY to set it to low
9206f8de19fSKonstantin Porotchkin 	 * state, thus do not need to power off USB3 PHY again.
9216f8de19fSKonstantin Porotchkin 	 */
9226f8de19fSKonstantin Porotchkin 	debug_enter();
9236f8de19fSKonstantin Porotchkin 	debug_exit();
9246f8de19fSKonstantin Porotchkin 
9256f8de19fSKonstantin Porotchkin 	return 0;
9266f8de19fSKonstantin Porotchkin }
9276f8de19fSKonstantin Porotchkin 
928fc299ce0SPali Rohár static int mvebu_a3700_comphy_sata_power_off(void)
9296f8de19fSKonstantin Porotchkin {
9306f8de19fSKonstantin Porotchkin 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
9316f8de19fSKonstantin Porotchkin 	uint32_t offset;
9326f8de19fSKonstantin Porotchkin 
9336f8de19fSKonstantin Porotchkin 	debug_enter();
9346f8de19fSKonstantin Porotchkin 
9356f8de19fSKonstantin Porotchkin 	/* Set phy isolation mode */
9366f8de19fSKonstantin Porotchkin 	offset = COMPHY_ISOLATION_CTRL_REG + SATAPHY_LANE2_REG_BASE_OFFSET;
937*be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, PHY_ISOLATE_MODE,
938*be33dce7SPali Rohár 				 PHY_ISOLATE_MODE);
9396f8de19fSKonstantin Porotchkin 
9406f8de19fSKonstantin Porotchkin 	/* Power off PLL, Tx, Rx */
9416f8de19fSKonstantin Porotchkin 	offset = COMPHY_POWER_PLL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
942*be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, 0,
943*be33dce7SPali Rohár 				 PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT);
9446f8de19fSKonstantin Porotchkin 
9456f8de19fSKonstantin Porotchkin 	debug_exit();
9466f8de19fSKonstantin Porotchkin 
9476f8de19fSKonstantin Porotchkin 	return 0;
9486f8de19fSKonstantin Porotchkin }
9496f8de19fSKonstantin Porotchkin 
9506f8de19fSKonstantin Porotchkin int mvebu_3700_comphy_power_off(uint8_t comphy_index, uint32_t comphy_mode)
9516f8de19fSKonstantin Porotchkin {
9526f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
9536f8de19fSKonstantin Porotchkin 	int err = 0;
9546f8de19fSKonstantin Porotchkin 
9556f8de19fSKonstantin Porotchkin 	debug_enter();
9566f8de19fSKonstantin Porotchkin 
957629dd61fSMarek Behún 	if (!mode) {
958629dd61fSMarek Behún 		/*
959629dd61fSMarek Behún 		 * The user did not specify which mode should be powered off.
960629dd61fSMarek Behún 		 * In this case we can identify this by reading the phy selector
961629dd61fSMarek Behún 		 * register.
962629dd61fSMarek Behún 		 */
963629dd61fSMarek Behún 		mode = mvebu_a3700_comphy_get_mode(comphy_index);
964629dd61fSMarek Behún 	}
965629dd61fSMarek Behún 
9666f8de19fSKonstantin Porotchkin 	switch (mode) {
967629dd61fSMarek Behún 	case(COMPHY_SGMII_MODE):
968a669983cSPali Rohár 	case(COMPHY_2500BASEX_MODE):
969629dd61fSMarek Behún 		err = mvebu_a3700_comphy_sgmii_power_off(comphy_index);
970629dd61fSMarek Behún 		break;
9716f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3_MODE):
9726f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3H_MODE):
9736f8de19fSKonstantin Porotchkin 		err = mvebu_a3700_comphy_usb3_power_off();
9746f8de19fSKonstantin Porotchkin 		break;
9756f8de19fSKonstantin Porotchkin 	case (COMPHY_SATA_MODE):
976fc299ce0SPali Rohár 		err = mvebu_a3700_comphy_sata_power_off();
9776f8de19fSKonstantin Porotchkin 		break;
9786f8de19fSKonstantin Porotchkin 
9796f8de19fSKonstantin Porotchkin 	default:
9806f8de19fSKonstantin Porotchkin 		debug("comphy%d: power off is not implemented for mode %d\n",
9816f8de19fSKonstantin Porotchkin 		      comphy_index, mode);
9826f8de19fSKonstantin Porotchkin 		break;
9836f8de19fSKonstantin Porotchkin 	}
9846f8de19fSKonstantin Porotchkin 
9856f8de19fSKonstantin Porotchkin 	debug_exit();
9866f8de19fSKonstantin Porotchkin 
9876f8de19fSKonstantin Porotchkin 	return err;
9886f8de19fSKonstantin Porotchkin }
9896f8de19fSKonstantin Porotchkin 
9906f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_sata_is_pll_locked(void)
9916f8de19fSKonstantin Porotchkin {
9926f8de19fSKonstantin Porotchkin 	uint32_t data, addr;
9936f8de19fSKonstantin Porotchkin 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
9946f8de19fSKonstantin Porotchkin 	int ret = 0;
9956f8de19fSKonstantin Porotchkin 
9966f8de19fSKonstantin Porotchkin 	debug_enter();
9976f8de19fSKonstantin Porotchkin 
9986f8de19fSKonstantin Porotchkin 	/* Polling status */
9996f8de19fSKonstantin Porotchkin 	mmio_write_32(comphy_indir_regs + COMPHY_LANE2_INDIR_ADDR_OFFSET,
10006f8de19fSKonstantin Porotchkin 	       COMPHY_LOOPBACK_REG0 + SATAPHY_LANE2_REG_BASE_OFFSET);
10016f8de19fSKonstantin Porotchkin 	addr = comphy_indir_regs + COMPHY_LANE2_INDIR_DATA_OFFSET;
10026f8de19fSKonstantin Porotchkin 	data = polling_with_timeout(addr, PLL_READY_TX_BIT, PLL_READY_TX_BIT,
10036f8de19fSKonstantin Porotchkin 				    COMPHY_PLL_TIMEOUT, REG_32BIT);
10046f8de19fSKonstantin Porotchkin 
10056f8de19fSKonstantin Porotchkin 	if (data != 0) {
10066f8de19fSKonstantin Porotchkin 		ERROR("TX PLL is not locked\n");
10076f8de19fSKonstantin Porotchkin 		ret = -ETIMEDOUT;
10086f8de19fSKonstantin Porotchkin 	}
10096f8de19fSKonstantin Porotchkin 
10106f8de19fSKonstantin Porotchkin 	debug_exit();
10116f8de19fSKonstantin Porotchkin 
10126f8de19fSKonstantin Porotchkin 	return ret;
10136f8de19fSKonstantin Porotchkin }
10146f8de19fSKonstantin Porotchkin 
10156f8de19fSKonstantin Porotchkin int mvebu_3700_comphy_is_pll_locked(uint8_t comphy_index, uint32_t comphy_mode)
10166f8de19fSKonstantin Porotchkin {
10176f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
10186f8de19fSKonstantin Porotchkin 	int ret = 0;
10196f8de19fSKonstantin Porotchkin 
10206f8de19fSKonstantin Porotchkin 	debug_enter();
10216f8de19fSKonstantin Porotchkin 
10226f8de19fSKonstantin Porotchkin 	switch (mode) {
10236f8de19fSKonstantin Porotchkin 	case(COMPHY_SATA_MODE):
10246f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_sata_is_pll_locked();
10256f8de19fSKonstantin Porotchkin 		break;
10266f8de19fSKonstantin Porotchkin 
10276f8de19fSKonstantin Porotchkin 	default:
10286f8de19fSKonstantin Porotchkin 		ERROR("comphy[%d] mode[%d] doesn't support PLL lock check\n",
10296f8de19fSKonstantin Porotchkin 			comphy_index, mode);
10306f8de19fSKonstantin Porotchkin 		ret = -EINVAL;
10316f8de19fSKonstantin Porotchkin 		break;
10326f8de19fSKonstantin Porotchkin 	}
10336f8de19fSKonstantin Porotchkin 
10346f8de19fSKonstantin Porotchkin 	debug_exit();
10356f8de19fSKonstantin Porotchkin 
10366f8de19fSKonstantin Porotchkin 	return ret;
10376f8de19fSKonstantin Porotchkin }
1038