xref: /rk3399_ARM-atf/drivers/marvell/comphy/phy-comphy-3700.c (revision c074f70ce5d85e1735b589b323fac99d7eb988b5)
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,
217be33dce7SPali 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 	 */
227be33dce7SPali Rohár 	if (is_sata) {
2286f8de19fSKonstantin Porotchkin 		mmio_write_32(addr + COMPHY_LANE2_INDIR_ADDR_OFFSET, offset);
229be33dce7SPali Rohár 	} else {
2306f8de19fSKonstantin Porotchkin 		mmio_write_32(addr + COMPHY_LANE2_INDIR_ADDR_OFFSET,
2316f8de19fSKonstantin Porotchkin 			      offset + USB3PHY_LANE2_REG_BASE_OFFSET);
232be33dce7SPali Rohár 	}
2336f8de19fSKonstantin Porotchkin 
2346f8de19fSKonstantin Porotchkin 	reg_set(addr + COMPHY_LANE2_INDIR_DATA_OFFSET, data, mask);
2356f8de19fSKonstantin Porotchkin }
2366f8de19fSKonstantin Porotchkin 
237be33dce7SPali Rohár /* It is only used for SATA on comphy lane2. */
238be33dce7SPali Rohár static void comphy_sata_set_indirect(uintptr_t addr, uint32_t reg_offset,
239be33dce7SPali Rohár 				     uint16_t data, uint16_t mask)
240be33dce7SPali Rohár {
241be33dce7SPali Rohár 	comphy_set_indirect(addr, reg_offset, data, mask, true);
242be33dce7SPali Rohár }
243be33dce7SPali Rohár 
244be33dce7SPali Rohár /* It is only used for USB3 indirect access on comphy lane2. */
245be33dce7SPali Rohár static void comphy_usb3_set_indirect(uintptr_t addr, uint32_t reg_offset,
246be33dce7SPali Rohár 				     uint16_t data, uint16_t mask)
247be33dce7SPali Rohár {
248be33dce7SPali Rohár 	comphy_set_indirect(addr, reg_offset, data, mask, false);
249be33dce7SPali Rohár }
250be33dce7SPali Rohár 
251be33dce7SPali 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,
253be33dce7SPali 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 
2580694b813SPali Rohár static void comphy_sgmii_phy_init(uintptr_t sd_ip_addr, bool is_1gbps)
2596f8de19fSKonstantin Porotchkin {
2606f8de19fSKonstantin Porotchkin 	const int fix_arr_sz = ARRAY_SIZE(sgmii_phy_init_fix);
2616f8de19fSKonstantin Porotchkin 	int addr, fix_idx;
2626f8de19fSKonstantin Porotchkin 	uint16_t val;
2636f8de19fSKonstantin Porotchkin 
2646f8de19fSKonstantin Porotchkin 	fix_idx = 0;
2656f8de19fSKonstantin Porotchkin 	for (addr = 0; addr < 512; addr++) {
2666f8de19fSKonstantin Porotchkin 		/*
2676f8de19fSKonstantin Porotchkin 		 * All PHY register values are defined in full for 3.125Gbps
2686f8de19fSKonstantin Porotchkin 		 * SERDES speed. The values required for 1.25 Gbps are almost
2696f8de19fSKonstantin Porotchkin 		 * the same and only few registers should be "fixed" in
2706f8de19fSKonstantin Porotchkin 		 * comparison to 3.125 Gbps values. These register values are
2716f8de19fSKonstantin Porotchkin 		 * stored in "sgmii_phy_init_fix" array.
2726f8de19fSKonstantin Porotchkin 		 */
2730694b813SPali Rohár 		if (!is_1gbps && sgmii_phy_init_fix[fix_idx].addr == addr) {
2746f8de19fSKonstantin Porotchkin 			/* Use new value */
2756f8de19fSKonstantin Porotchkin 			val = sgmii_phy_init_fix[fix_idx].value;
2766f8de19fSKonstantin Porotchkin 			if (fix_idx < fix_arr_sz)
2776f8de19fSKonstantin Porotchkin 				fix_idx++;
2786f8de19fSKonstantin Porotchkin 		} else {
2796f8de19fSKonstantin Porotchkin 			val = sgmii_phy_init[addr];
2806f8de19fSKonstantin Porotchkin 		}
2816f8de19fSKonstantin Porotchkin 
2826f8de19fSKonstantin Porotchkin 		reg_set16(SGMIIPHY_ADDR(addr, sd_ip_addr), val, 0xFFFF);
2836f8de19fSKonstantin Porotchkin 	}
2846f8de19fSKonstantin Porotchkin }
2856f8de19fSKonstantin Porotchkin 
2866f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_sata_power_on(uint8_t comphy_index,
2876f8de19fSKonstantin Porotchkin 					    uint32_t comphy_mode)
2886f8de19fSKonstantin Porotchkin {
2896f8de19fSKonstantin Porotchkin 	int ret = 0;
2906f8de19fSKonstantin Porotchkin 	uint32_t offset, data = 0, ref_clk;
2916f8de19fSKonstantin Porotchkin 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
2926f8de19fSKonstantin Porotchkin 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
2936f8de19fSKonstantin Porotchkin 
2946f8de19fSKonstantin Porotchkin 	debug_enter();
2956f8de19fSKonstantin Porotchkin 
2966f8de19fSKonstantin Porotchkin 	/* Configure phy selector for SATA */
2976f8de19fSKonstantin Porotchkin 	mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
2986f8de19fSKonstantin Porotchkin 
2996f8de19fSKonstantin Porotchkin 	/* Clear phy isolation mode to make it work in normal mode */
3006f8de19fSKonstantin Porotchkin 	offset =  COMPHY_ISOLATION_CTRL_REG + SATAPHY_LANE2_REG_BASE_OFFSET;
301be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, 0, PHY_ISOLATE_MODE);
3026f8de19fSKonstantin Porotchkin 
3036f8de19fSKonstantin Porotchkin 	/* 0. Check the Polarity invert bits */
3046f8de19fSKonstantin Porotchkin 	if (invert & COMPHY_POLARITY_TXD_INVERT)
3056f8de19fSKonstantin Porotchkin 		data |= TXD_INVERT_BIT;
3066f8de19fSKonstantin Porotchkin 	if (invert & COMPHY_POLARITY_RXD_INVERT)
3076f8de19fSKonstantin Porotchkin 		data |= RXD_INVERT_BIT;
3086f8de19fSKonstantin Porotchkin 
3096f8de19fSKonstantin Porotchkin 	offset = COMPHY_SYNC_PATTERN_REG + SATAPHY_LANE2_REG_BASE_OFFSET;
310be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, data, TXD_INVERT_BIT |
311be33dce7SPali Rohár 				 RXD_INVERT_BIT);
3126f8de19fSKonstantin Porotchkin 
3136f8de19fSKonstantin Porotchkin 	/* 1. Select 40-bit data width width */
3146f8de19fSKonstantin Porotchkin 	offset = COMPHY_LOOPBACK_REG0 + SATAPHY_LANE2_REG_BASE_OFFSET;
315be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, DATA_WIDTH_40BIT,
316be33dce7SPali Rohár 				 SEL_DATA_WIDTH_MASK);
3176f8de19fSKonstantin Porotchkin 
3186f8de19fSKonstantin Porotchkin 	/* 2. Select reference clock(25M) and PHY mode (SATA) */
3196f8de19fSKonstantin Porotchkin 	offset = COMPHY_POWER_PLL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
3206f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40)
3216f8de19fSKonstantin Porotchkin 		ref_clk = REF_CLOCK_SPEED_40M;
3226f8de19fSKonstantin Porotchkin 	else
3236f8de19fSKonstantin Porotchkin 		ref_clk = REF_CLOCK_SPEED_25M;
3246f8de19fSKonstantin Porotchkin 
325be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, ref_clk | PHY_MODE_SATA,
326be33dce7SPali Rohár 				 REF_FREF_SEL_MASK | PHY_MODE_MASK);
3276f8de19fSKonstantin Porotchkin 
3286f8de19fSKonstantin Porotchkin 	/* 3. Use maximum PLL rate (no power save) */
3296f8de19fSKonstantin Porotchkin 	offset = COMPHY_KVCO_CAL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
330be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, USE_MAX_PLL_RATE_BIT,
331be33dce7SPali Rohár 				 USE_MAX_PLL_RATE_BIT);
3326f8de19fSKonstantin Porotchkin 
3336f8de19fSKonstantin Porotchkin 	/* 4. Reset reserved bit */
334be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, COMPHY_RESERVED_REG, 0,
335be33dce7SPali Rohár 				 PHYCTRL_FRM_PIN_BIT);
3366f8de19fSKonstantin Porotchkin 
3376f8de19fSKonstantin Porotchkin 	/* 5. Set vendor-specific configuration (It is done in sata driver) */
3386f8de19fSKonstantin Porotchkin 	/* XXX: in U-Boot below sequence was executed in this place, in Linux
3396f8de19fSKonstantin Porotchkin 	 * not.  Now it is done only in U-Boot before this comphy
3406f8de19fSKonstantin Porotchkin 	 * initialization - tests shows that it works ok, but in case of any
3416f8de19fSKonstantin Porotchkin 	 * future problem it is left for reference.
3426f8de19fSKonstantin Porotchkin 	 *   reg_set(MVEBU_REGS_BASE + 0xe00a0, 0, 0xffffffff);
3436f8de19fSKonstantin Porotchkin 	 *   reg_set(MVEBU_REGS_BASE + 0xe00a4, BIT(6), BIT(6));
3446f8de19fSKonstantin Porotchkin 	 */
3456f8de19fSKonstantin Porotchkin 
3466f8de19fSKonstantin Porotchkin 	/* Wait for > 55 us to allow PLL be enabled */
3476f8de19fSKonstantin Porotchkin 	udelay(PLL_SET_DELAY_US);
3486f8de19fSKonstantin Porotchkin 
3496f8de19fSKonstantin Porotchkin 	/* Polling status */
3506f8de19fSKonstantin Porotchkin 	mmio_write_32(comphy_indir_regs + COMPHY_LANE2_INDIR_ADDR_OFFSET,
3516f8de19fSKonstantin Porotchkin 		      COMPHY_LOOPBACK_REG0 + SATAPHY_LANE2_REG_BASE_OFFSET);
3526f8de19fSKonstantin Porotchkin 
3536f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(comphy_indir_regs +
3546f8de19fSKonstantin Porotchkin 				   COMPHY_LANE2_INDIR_DATA_OFFSET,
3556f8de19fSKonstantin Porotchkin 				   PLL_READY_TX_BIT, PLL_READY_TX_BIT,
3566f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
3576f8de19fSKonstantin Porotchkin 
3586f8de19fSKonstantin Porotchkin 	debug_exit();
3596f8de19fSKonstantin Porotchkin 
3606f8de19fSKonstantin Porotchkin 	return ret;
3616f8de19fSKonstantin Porotchkin }
3626f8de19fSKonstantin Porotchkin 
3636f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_sgmii_power_on(uint8_t comphy_index,
3646f8de19fSKonstantin Porotchkin 					     uint32_t comphy_mode)
3656f8de19fSKonstantin Porotchkin {
3666f8de19fSKonstantin Porotchkin 	int ret = 0;
367*c074f70cSPali Rohár 	uint32_t mask, data;
368*c074f70cSPali Rohár 	uintptr_t offset;
3696f8de19fSKonstantin Porotchkin 	uintptr_t sd_ip_addr;
3706f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
3716f8de19fSKonstantin Porotchkin 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
3726f8de19fSKonstantin Porotchkin 
3736f8de19fSKonstantin Porotchkin 	debug_enter();
3746f8de19fSKonstantin Porotchkin 
3756f8de19fSKonstantin Porotchkin 	/* Set selector */
3766f8de19fSKonstantin Porotchkin 	mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
3776f8de19fSKonstantin Porotchkin 
3786f8de19fSKonstantin Porotchkin 	/* Serdes IP Base address
3796f8de19fSKonstantin Porotchkin 	 * COMPHY Lane0 -- USB3/GBE1
3806f8de19fSKonstantin Porotchkin 	 * COMPHY Lane1 -- PCIe/GBE0
3816f8de19fSKonstantin Porotchkin 	 */
3826f8de19fSKonstantin Porotchkin 	if (comphy_index == COMPHY_LANE0) {
3836f8de19fSKonstantin Porotchkin 		/* Get usb3 and gbe */
3846f8de19fSKonstantin Porotchkin 		sd_ip_addr = USB3_GBE1_PHY;
3856f8de19fSKonstantin Porotchkin 	} else
3866f8de19fSKonstantin Porotchkin 		sd_ip_addr = COMPHY_SD_ADDR;
3876f8de19fSKonstantin Porotchkin 
3886f8de19fSKonstantin Porotchkin 	/*
3896f8de19fSKonstantin Porotchkin 	 * 1. Reset PHY by setting PHY input port PIN_RESET=1.
3906f8de19fSKonstantin Porotchkin 	 * 2. Set PHY input port PIN_TX_IDLE=1, PIN_PU_IVREF=1 to keep
3916f8de19fSKonstantin Porotchkin 	 *    PHY TXP/TXN output to idle state during PHY initialization
3926f8de19fSKonstantin Porotchkin 	 * 3. Set PHY input port PIN_PU_PLL=0, PIN_PU_RX=0, PIN_PU_TX=0.
3936f8de19fSKonstantin Porotchkin 	 */
3946f8de19fSKonstantin Porotchkin 	data = PIN_PU_IVEREF_BIT | PIN_TX_IDLE_BIT | PIN_RESET_COMPHY_BIT;
3956f8de19fSKonstantin Porotchkin 	mask = PIN_RESET_CORE_BIT | PIN_PU_PLL_BIT | PIN_PU_RX_BIT |
3966f8de19fSKonstantin Porotchkin 		PIN_PU_TX_BIT;
3976f8de19fSKonstantin Porotchkin 	offset = MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index);
3986f8de19fSKonstantin Porotchkin 	reg_set(offset, data, mask);
3996f8de19fSKonstantin Porotchkin 
4006f8de19fSKonstantin Porotchkin 	/* 4. Release reset to the PHY by setting PIN_RESET=0. */
4016f8de19fSKonstantin Porotchkin 	data = 0;
4026f8de19fSKonstantin Porotchkin 	mask = PIN_RESET_COMPHY_BIT;
4036f8de19fSKonstantin Porotchkin 	reg_set(offset, data, mask);
4046f8de19fSKonstantin Porotchkin 
4056f8de19fSKonstantin Porotchkin 	/*
4066f8de19fSKonstantin Porotchkin 	 * 5. Set PIN_PHY_GEN_TX[3:0] and PIN_PHY_GEN_RX[3:0] to decide COMPHY
4076f8de19fSKonstantin Porotchkin 	 * bit rate
4086f8de19fSKonstantin Porotchkin 	 */
4096f8de19fSKonstantin Porotchkin 	if (mode == COMPHY_SGMII_MODE) {
4106f8de19fSKonstantin Porotchkin 		/* SGMII 1G, SerDes speed 1.25G */
4116f8de19fSKonstantin Porotchkin 		data |= SD_SPEED_1_25_G << GEN_RX_SEL_OFFSET;
4126f8de19fSKonstantin Porotchkin 		data |= SD_SPEED_1_25_G << GEN_TX_SEL_OFFSET;
413a669983cSPali Rohár 	} else if (mode == COMPHY_2500BASEX_MODE) {
414a669983cSPali Rohár 		/* 2500Base-X, SerDes speed 3.125G */
4156f8de19fSKonstantin Porotchkin 		data |= SD_SPEED_2_5_G << GEN_RX_SEL_OFFSET;
4166f8de19fSKonstantin Porotchkin 		data |= SD_SPEED_2_5_G << GEN_TX_SEL_OFFSET;
4176f8de19fSKonstantin Porotchkin 	} else {
4186f8de19fSKonstantin Porotchkin 		/* Other rates are not supported */
4196f8de19fSKonstantin Porotchkin 		ERROR("unsupported SGMII speed on comphy lane%d\n",
4206f8de19fSKonstantin Porotchkin 			comphy_index);
4216f8de19fSKonstantin Porotchkin 		return -EINVAL;
4226f8de19fSKonstantin Porotchkin 	}
4236f8de19fSKonstantin Porotchkin 	mask = GEN_RX_SEL_MASK | GEN_TX_SEL_MASK;
4246f8de19fSKonstantin Porotchkin 	reg_set(offset, data, mask);
4256f8de19fSKonstantin Porotchkin 
4266f8de19fSKonstantin Porotchkin 	/*
4276f8de19fSKonstantin Porotchkin 	 * 6. Wait 10mS for bandgap and reference clocks to stabilize; then
4286f8de19fSKonstantin Porotchkin 	 * start SW programming.
4296f8de19fSKonstantin Porotchkin 	 */
4306f8de19fSKonstantin Porotchkin 	mdelay(10);
4316f8de19fSKonstantin Porotchkin 
4326f8de19fSKonstantin Porotchkin 	/* 7. Program COMPHY register PHY_MODE */
4336f8de19fSKonstantin Porotchkin 	data = PHY_MODE_SGMII;
4346f8de19fSKonstantin Porotchkin 	mask = PHY_MODE_MASK;
4356f8de19fSKonstantin Porotchkin 	reg_set16(SGMIIPHY_ADDR(COMPHY_POWER_PLL_CTRL, sd_ip_addr), data, mask);
4366f8de19fSKonstantin Porotchkin 
4376f8de19fSKonstantin Porotchkin 	/*
4386f8de19fSKonstantin Porotchkin 	 * 8. Set COMPHY register REFCLK_SEL to select the correct REFCLK
4396f8de19fSKonstantin Porotchkin 	 * source
4406f8de19fSKonstantin Porotchkin 	 */
4416f8de19fSKonstantin Porotchkin 	data = 0;
4426f8de19fSKonstantin Porotchkin 	mask = PHY_REF_CLK_SEL;
4436f8de19fSKonstantin Porotchkin 	reg_set16(SGMIIPHY_ADDR(COMPHY_MISC_REG0_ADDR, sd_ip_addr), data, mask);
4446f8de19fSKonstantin Porotchkin 
4456f8de19fSKonstantin Porotchkin 	/*
4466f8de19fSKonstantin Porotchkin 	 * 9. Set correct reference clock frequency in COMPHY register
4476f8de19fSKonstantin Porotchkin 	 * REF_FREF_SEL.
4486f8de19fSKonstantin Porotchkin 	 */
4496f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40)
4506f8de19fSKonstantin Porotchkin 		data = REF_CLOCK_SPEED_50M;
4516f8de19fSKonstantin Porotchkin 	else
4526f8de19fSKonstantin Porotchkin 		data = REF_CLOCK_SPEED_25M;
4536f8de19fSKonstantin Porotchkin 
4546f8de19fSKonstantin Porotchkin 	mask = REF_FREF_SEL_MASK;
4556f8de19fSKonstantin Porotchkin 	reg_set16(SGMIIPHY_ADDR(COMPHY_POWER_PLL_CTRL, sd_ip_addr), data, mask);
4566f8de19fSKonstantin Porotchkin 
4576f8de19fSKonstantin Porotchkin 	/* 10. Program COMPHY register PHY_GEN_MAX[1:0]
4586f8de19fSKonstantin Porotchkin 	 * This step is mentioned in the flow received from verification team.
4596f8de19fSKonstantin Porotchkin 	 * However the PHY_GEN_MAX value is only meaningful for other interfaces
4606f8de19fSKonstantin Porotchkin 	 * (not SGMII). For instance, it selects SATA speed 1.5/3/6 Gbps or PCIe
4616f8de19fSKonstantin Porotchkin 	 * speed 2.5/5 Gbps
4626f8de19fSKonstantin Porotchkin 	 */
4636f8de19fSKonstantin Porotchkin 
4646f8de19fSKonstantin Porotchkin 	/*
4656f8de19fSKonstantin Porotchkin 	 * 11. Program COMPHY register SEL_BITS to set correct parallel data
4666f8de19fSKonstantin Porotchkin 	 * bus width
4676f8de19fSKonstantin Porotchkin 	 */
4686f8de19fSKonstantin Porotchkin 	data = DATA_WIDTH_10BIT;
4696f8de19fSKonstantin Porotchkin 	mask = SEL_DATA_WIDTH_MASK;
4706f8de19fSKonstantin Porotchkin 	reg_set16(SGMIIPHY_ADDR(COMPHY_LOOPBACK_REG0, sd_ip_addr), data, mask);
4716f8de19fSKonstantin Porotchkin 
4726f8de19fSKonstantin Porotchkin 	/*
4736f8de19fSKonstantin Porotchkin 	 * 12. As long as DFE function needs to be enabled in any mode,
4746f8de19fSKonstantin Porotchkin 	 * COMPHY register DFE_UPDATE_EN[5:0] shall be programmed to 0x3F
4756f8de19fSKonstantin Porotchkin 	 * for real chip during COMPHY power on.
4766f8de19fSKonstantin Porotchkin 	 * The step 14 exists (and empty) in the original initialization flow
4776f8de19fSKonstantin Porotchkin 	 * obtained from the verification team. According to the functional
4786f8de19fSKonstantin Porotchkin 	 * specification DFE_UPDATE_EN already has the default value 0x3F
4796f8de19fSKonstantin Porotchkin 	 */
4806f8de19fSKonstantin Porotchkin 
4816f8de19fSKonstantin Porotchkin 	/*
4826f8de19fSKonstantin Porotchkin 	 * 13. Program COMPHY GEN registers.
4836f8de19fSKonstantin Porotchkin 	 * These registers should be programmed based on the lab testing result
4846f8de19fSKonstantin Porotchkin 	 * to achieve optimal performance. Please contact the CEA group to get
4856f8de19fSKonstantin Porotchkin 	 * the related GEN table during real chip bring-up. We only required to
4866f8de19fSKonstantin Porotchkin 	 * run though the entire registers programming flow defined by
4876f8de19fSKonstantin Porotchkin 	 * "comphy_sgmii_phy_init" when the REF clock is 40 MHz. For REF clock
4886f8de19fSKonstantin Porotchkin 	 * 25 MHz the default values stored in PHY registers are OK.
4896f8de19fSKonstantin Porotchkin 	 */
4906f8de19fSKonstantin Porotchkin 	debug("Running C-DPI phy init %s mode\n",
491a669983cSPali Rohár 	      mode == COMPHY_2500BASEX_MODE ? "2G5" : "1G");
4926f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40)
4930694b813SPali Rohár 		comphy_sgmii_phy_init(sd_ip_addr, mode != COMPHY_2500BASEX_MODE);
4946f8de19fSKonstantin Porotchkin 
4956f8de19fSKonstantin Porotchkin 	/*
4966f8de19fSKonstantin Porotchkin 	 * 14. [Simulation Only] should not be used for real chip.
4976f8de19fSKonstantin Porotchkin 	 * By pass power up calibration by programming EXT_FORCE_CAL_DONE
4986f8de19fSKonstantin Porotchkin 	 * (R02h[9]) to 1 to shorten COMPHY simulation time.
4996f8de19fSKonstantin Porotchkin 	 */
5006f8de19fSKonstantin Porotchkin 
5016f8de19fSKonstantin Porotchkin 	/*
5026f8de19fSKonstantin Porotchkin 	 * 15. [Simulation Only: should not be used for real chip]
5036f8de19fSKonstantin Porotchkin 	 * Program COMPHY register FAST_DFE_TIMER_EN=1 to shorten RX training
5046f8de19fSKonstantin Porotchkin 	 * simulation time.
5056f8de19fSKonstantin Porotchkin 	 */
5066f8de19fSKonstantin Porotchkin 
5076f8de19fSKonstantin Porotchkin 	/*
5086f8de19fSKonstantin Porotchkin 	 * 16. Check the PHY Polarity invert bit
5096f8de19fSKonstantin Porotchkin 	 */
5106f8de19fSKonstantin Porotchkin 	data = 0x0;
5116f8de19fSKonstantin Porotchkin 	if (invert & COMPHY_POLARITY_TXD_INVERT)
5126f8de19fSKonstantin Porotchkin 		data |= TXD_INVERT_BIT;
5136f8de19fSKonstantin Porotchkin 	if (invert & COMPHY_POLARITY_RXD_INVERT)
5146f8de19fSKonstantin Porotchkin 		data |= RXD_INVERT_BIT;
515ee4c70ebSPali Rohár 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
516ee4c70ebSPali Rohár 	reg_set16(SGMIIPHY_ADDR(COMPHY_SYNC_PATTERN_REG, sd_ip_addr), data, mask);
5176f8de19fSKonstantin Porotchkin 
5186f8de19fSKonstantin Porotchkin 	/*
5196f8de19fSKonstantin Porotchkin 	 * 17. Set PHY input ports PIN_PU_PLL, PIN_PU_TX and PIN_PU_RX to 1 to
5206f8de19fSKonstantin Porotchkin 	 * start PHY power up sequence. All the PHY register programming should
5216f8de19fSKonstantin Porotchkin 	 * be done before PIN_PU_PLL=1. There should be no register programming
5226f8de19fSKonstantin Porotchkin 	 * for normal PHY operation from this point.
5236f8de19fSKonstantin Porotchkin 	 */
5246f8de19fSKonstantin Porotchkin 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
5256f8de19fSKonstantin Porotchkin 		PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT,
5266f8de19fSKonstantin Porotchkin 		PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT);
5276f8de19fSKonstantin Porotchkin 
5286f8de19fSKonstantin Porotchkin 	/*
5296f8de19fSKonstantin Porotchkin 	 * 18. Wait for PHY power up sequence to finish by checking output ports
5306f8de19fSKonstantin Porotchkin 	 * PIN_PLL_READY_TX=1 and PIN_PLL_READY_RX=1.
5316f8de19fSKonstantin Porotchkin 	 */
5326f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
5336f8de19fSKonstantin Porotchkin 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
5346f8de19fSKonstantin Porotchkin 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
5356f8de19fSKonstantin Porotchkin 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
5366f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
5376f8de19fSKonstantin Porotchkin 	if (ret)
5386f8de19fSKonstantin Porotchkin 		ERROR("Failed to lock PLL for SGMII PHY %d\n", comphy_index);
5396f8de19fSKonstantin Porotchkin 
5406f8de19fSKonstantin Porotchkin 	/*
5416f8de19fSKonstantin Porotchkin 	 * 19. Set COMPHY input port PIN_TX_IDLE=0
5426f8de19fSKonstantin Porotchkin 	 */
5436f8de19fSKonstantin Porotchkin 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
5446f8de19fSKonstantin Porotchkin 		0x0, PIN_TX_IDLE_BIT);
5456f8de19fSKonstantin Porotchkin 
5466f8de19fSKonstantin Porotchkin 	/*
5476f8de19fSKonstantin Porotchkin 	 * 20. After valid data appear on PIN_RXDATA bus, set PIN_RX_INIT=1. To
5486f8de19fSKonstantin Porotchkin 	 * start RX initialization. PIN_RX_INIT_DONE will be cleared to 0 by the
5496f8de19fSKonstantin Porotchkin 	 * PHY After RX initialization is done, PIN_RX_INIT_DONE will be set to
5506f8de19fSKonstantin Porotchkin 	 * 1 by COMPHY Set PIN_RX_INIT=0 after PIN_RX_INIT_DONE= 1. Please
5516f8de19fSKonstantin Porotchkin 	 * refer to RX initialization part for details.
5526f8de19fSKonstantin Porotchkin 	 */
5536f8de19fSKonstantin Porotchkin 	reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
554ccec1bd5SPali Rohár 		PHY_RX_INIT_BIT, PHY_RX_INIT_BIT);
5556f8de19fSKonstantin Porotchkin 
5566f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
5576f8de19fSKonstantin Porotchkin 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
5586f8de19fSKonstantin Porotchkin 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
5596f8de19fSKonstantin Porotchkin 				   PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
5606f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
5616f8de19fSKonstantin Porotchkin 	if (ret)
5626f8de19fSKonstantin Porotchkin 		ERROR("Failed to lock PLL for SGMII PHY %d\n", comphy_index);
5636f8de19fSKonstantin Porotchkin 
5646f8de19fSKonstantin Porotchkin 
5656f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
5666f8de19fSKonstantin Porotchkin 				   COMPHY_PHY_STATUS_OFFSET(comphy_index),
5676f8de19fSKonstantin Porotchkin 				   PHY_RX_INIT_DONE_BIT, PHY_RX_INIT_DONE_BIT,
5686f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_32BIT);
5696f8de19fSKonstantin Porotchkin 	if (ret)
5706f8de19fSKonstantin Porotchkin 		ERROR("Failed to init RX of SGMII PHY %d\n", comphy_index);
5716f8de19fSKonstantin Porotchkin 
5726f8de19fSKonstantin Porotchkin 	debug_exit();
5736f8de19fSKonstantin Porotchkin 
5746f8de19fSKonstantin Porotchkin 	return ret;
5756f8de19fSKonstantin Porotchkin }
5766f8de19fSKonstantin Porotchkin 
577629dd61fSMarek Behún static int mvebu_a3700_comphy_sgmii_power_off(uint8_t comphy_index)
578629dd61fSMarek Behún {
579629dd61fSMarek Behún 	int ret = 0;
580*c074f70cSPali Rohár 	uintptr_t offset;
581*c074f70cSPali Rohár 	uint32_t mask, data;
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;
600*c074f70cSPali Rohár 	uintptr_t addr;
601*c074f70cSPali Rohár 	uint32_t mask, data, cfg, ref_clk;
6026f8de19fSKonstantin Porotchkin 	void (*usb3_reg_set)(uintptr_t addr, uint32_t reg_offset, uint16_t data,
603be33dce7SPali Rohár 			     uint16_t mask);
6046f8de19fSKonstantin Porotchkin 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
6056f8de19fSKonstantin Porotchkin 
6066f8de19fSKonstantin Porotchkin 	debug_enter();
6076f8de19fSKonstantin Porotchkin 
6086f8de19fSKonstantin Porotchkin 	/* Set phy seclector */
6096f8de19fSKonstantin Porotchkin 	mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
6106f8de19fSKonstantin Porotchkin 
6116f8de19fSKonstantin Porotchkin 	/* Set usb3 reg access func, Lane2 is indirect access */
6126f8de19fSKonstantin Porotchkin 	if (comphy_index == COMPHY_LANE2) {
613be33dce7SPali Rohár 		usb3_reg_set = &comphy_usb3_set_indirect;
6146f8de19fSKonstantin Porotchkin 		reg_base = COMPHY_INDIRECT_REG;
6156f8de19fSKonstantin Porotchkin 	} else {
6166f8de19fSKonstantin Porotchkin 		/* Get the direct access register resource and map */
6176f8de19fSKonstantin Porotchkin 		usb3_reg_set = &comphy_usb3_set_direct;
6186f8de19fSKonstantin Porotchkin 		reg_base = USB3_GBE1_PHY;
6196f8de19fSKonstantin Porotchkin 	}
6206f8de19fSKonstantin Porotchkin 
6216f8de19fSKonstantin Porotchkin 	/*
6226f8de19fSKonstantin Porotchkin 	 * 0. Set PHY OTG Control(0x5d034), bit 4, Power up OTG module The
6236f8de19fSKonstantin Porotchkin 	 * register belong to UTMI module, so it is set in UTMI phy driver.
6246f8de19fSKonstantin Porotchkin 	 */
6256f8de19fSKonstantin Porotchkin 
6266f8de19fSKonstantin Porotchkin 	/*
6276f8de19fSKonstantin Porotchkin 	 * 1. Set PRD_TXDEEMPH (3.5db de-emph)
6286f8de19fSKonstantin Porotchkin 	 */
6296f8de19fSKonstantin Porotchkin 	mask = PRD_TXDEEMPH0_MASK | PRD_TXMARGIN_MASK | PRD_TXSWING_MASK |
6306f8de19fSKonstantin Porotchkin 		CFG_TX_ALIGN_POS_MASK;
6316f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_LANE_CFG0_ADDR, PRD_TXDEEMPH0_MASK,
632be33dce7SPali Rohár 		     mask);
6336f8de19fSKonstantin Porotchkin 
6346f8de19fSKonstantin Porotchkin 	/*
6356f8de19fSKonstantin Porotchkin 	 * 2. Set BIT0: enable transmitter in high impedance mode
6366f8de19fSKonstantin Porotchkin 	 *    Set BIT[3:4]: delay 2 clock cycles for HiZ off latency
6376f8de19fSKonstantin Porotchkin 	 *    Set BIT6: Tx detect Rx at HiZ mode
6386f8de19fSKonstantin Porotchkin 	 *    Unset BIT15: set to 0 to set USB3 De-emphasize level to -3.5db
6396f8de19fSKonstantin Porotchkin 	 *            together with bit 0 of COMPHY_REG_LANE_CFG0_ADDR register
6406f8de19fSKonstantin Porotchkin 	 */
6416f8de19fSKonstantin Porotchkin 	mask = PRD_TXDEEMPH1_MASK | TX_DET_RX_MODE | GEN2_TX_DATA_DLY_MASK |
6426f8de19fSKonstantin Porotchkin 		TX_ELEC_IDLE_MODE_EN;
6436f8de19fSKonstantin Porotchkin 	data = TX_DET_RX_MODE | GEN2_TX_DATA_DLY_DEFT | TX_ELEC_IDLE_MODE_EN;
644be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_REG_LANE_CFG1_ADDR, data, mask);
6456f8de19fSKonstantin Porotchkin 
6466f8de19fSKonstantin Porotchkin 	/*
6476f8de19fSKonstantin Porotchkin 	 * 3. Set Spread Spectrum Clock Enabled
6486f8de19fSKonstantin Porotchkin 	 */
6496f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_LANE_CFG4_ADDR,
650be33dce7SPali Rohár 		     SPREAD_SPECTRUM_CLK_EN, SPREAD_SPECTRUM_CLK_EN);
6516f8de19fSKonstantin Porotchkin 
6526f8de19fSKonstantin Porotchkin 	/*
6536f8de19fSKonstantin Porotchkin 	 * 4. Set Override Margining Controls From the MAC:
6546f8de19fSKonstantin Porotchkin 	 *    Use margining signals from lane configuration
6556f8de19fSKonstantin Porotchkin 	 */
6566f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_TEST_MODE_CTRL_ADDR,
657be33dce7SPali Rohár 		     MODE_MARGIN_OVERRIDE, REG_16_BIT_MASK);
6586f8de19fSKonstantin Porotchkin 
6596f8de19fSKonstantin Porotchkin 	/*
6606f8de19fSKonstantin Porotchkin 	 * 5. Set Lane-to-Lane Bundle Clock Sampling Period = per PCLK cycles
6616f8de19fSKonstantin Porotchkin 	 *    set Mode Clock Source = PCLK is generated from REFCLK
6626f8de19fSKonstantin Porotchkin 	 */
6636f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GLOB_CLK_SRC_LO_ADDR, 0x0,
6646f8de19fSKonstantin Porotchkin 		     (MODE_CLK_SRC | BUNDLE_PERIOD_SEL | BUNDLE_PERIOD_SCALE |
665be33dce7SPali Rohár 		      BUNDLE_SAMPLE_CTRL | PLL_READY_DLY));
6666f8de19fSKonstantin Porotchkin 
6676f8de19fSKonstantin Porotchkin 	/*
6686f8de19fSKonstantin Porotchkin 	 * 6. Set G2 Spread Spectrum Clock Amplitude at 4K
6696f8de19fSKonstantin Porotchkin 	 */
6706f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GEN2_SET_2,
671be33dce7SPali Rohár 		     G2_TX_SSC_AMP_VALUE_20, G2_TX_SSC_AMP_MASK);
6726f8de19fSKonstantin Porotchkin 
6736f8de19fSKonstantin Porotchkin 	/*
6746f8de19fSKonstantin Porotchkin 	 * 7. Unset G3 Spread Spectrum Clock Amplitude
6756f8de19fSKonstantin Porotchkin 	 *    set G3 TX and RX Register Master Current Select
6766f8de19fSKonstantin Porotchkin 	 */
6776f8de19fSKonstantin Porotchkin 	mask = G3_TX_SSC_AMP_MASK | G3_VREG_RXTX_MAS_ISET_MASK |
6786f8de19fSKonstantin Porotchkin 		RSVD_PH03FH_6_0_MASK;
6796f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GEN2_SET_3,
680be33dce7SPali Rohár 		     G3_VREG_RXTX_MAS_ISET_60U, mask);
6816f8de19fSKonstantin Porotchkin 
6826f8de19fSKonstantin Porotchkin 	/*
6836f8de19fSKonstantin Porotchkin 	 * 8. Check crystal jumper setting and program the Power and PLL Control
6846f8de19fSKonstantin Porotchkin 	 * accordingly Change RX wait
6856f8de19fSKonstantin Porotchkin 	 */
6866f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40) {
6876f8de19fSKonstantin Porotchkin 		ref_clk = REF_CLOCK_SPEED_40M;
6886f8de19fSKonstantin Porotchkin 		cfg = CFG_PM_RXDLOZ_WAIT_12_UNIT;
6896f8de19fSKonstantin Porotchkin 
6906f8de19fSKonstantin Porotchkin 	} else {
6916f8de19fSKonstantin Porotchkin 		/* 25 MHz */
6926f8de19fSKonstantin Porotchkin 		ref_clk = USB3_REF_CLOCK_SPEED_25M;
6936f8de19fSKonstantin Porotchkin 		cfg = CFG_PM_RXDLOZ_WAIT_7_UNIT;
6946f8de19fSKonstantin Porotchkin 	}
6956f8de19fSKonstantin Porotchkin 
6966f8de19fSKonstantin Porotchkin 	mask = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
6976f8de19fSKonstantin Porotchkin 		PU_TX_INTP_BIT | PU_DFE_BIT | PLL_LOCK_BIT | PHY_MODE_MASK |
6986f8de19fSKonstantin Porotchkin 		REF_FREF_SEL_MASK;
6996f8de19fSKonstantin Porotchkin 	data = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
7006f8de19fSKonstantin Porotchkin 		PU_TX_INTP_BIT | PU_DFE_BIT | PHY_MODE_USB3 | ref_clk;
701be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_POWER_PLL_CTRL, data,  mask);
7026f8de19fSKonstantin Porotchkin 
7036f8de19fSKonstantin Porotchkin 	mask = CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
7046f8de19fSKonstantin Porotchkin 		CFG_PM_RXDLOZ_WAIT_MASK;
7056f8de19fSKonstantin Porotchkin 	data = CFG_PM_RXDEN_WAIT_1_UNIT  | cfg;
706be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_REG_PWR_MGM_TIM1_ADDR, data, mask);
7076f8de19fSKonstantin Porotchkin 
7086f8de19fSKonstantin Porotchkin 	/*
7096f8de19fSKonstantin Porotchkin 	 * 9. Enable idle sync
7106f8de19fSKonstantin Porotchkin 	 */
7116f8de19fSKonstantin Porotchkin 	data = UNIT_CTRL_DEFAULT_VALUE | IDLE_SYNC_EN;
712be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_REG_UNIT_CTRL_ADDR, data, REG_16_BIT_MASK);
7136f8de19fSKonstantin Porotchkin 
7146f8de19fSKonstantin Porotchkin 	/*
7156f8de19fSKonstantin Porotchkin 	 * 10. Enable the output of 500M clock
7166f8de19fSKonstantin Porotchkin 	 */
7176f8de19fSKonstantin Porotchkin 	data = MISC_REG0_DEFAULT_VALUE | CLK500M_EN;
718be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_MISC_REG0_ADDR, data, REG_16_BIT_MASK);
7196f8de19fSKonstantin Porotchkin 
7206f8de19fSKonstantin Porotchkin 	/*
7216f8de19fSKonstantin Porotchkin 	 * 11. Set 20-bit data width
7226f8de19fSKonstantin Porotchkin 	 */
7236f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_LOOPBACK_REG0, DATA_WIDTH_20BIT,
724be33dce7SPali Rohár 		     REG_16_BIT_MASK);
7256f8de19fSKonstantin Porotchkin 
7266f8de19fSKonstantin Porotchkin 	/*
7276f8de19fSKonstantin Porotchkin 	 * 12. Override Speed_PLL value and use MAC PLL
7286f8de19fSKonstantin Porotchkin 	 */
7296f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_KVCO_CAL_CTRL,
7306f8de19fSKonstantin Porotchkin 		     (SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT),
731be33dce7SPali Rohár 		     REG_16_BIT_MASK);
7326f8de19fSKonstantin Porotchkin 
7336f8de19fSKonstantin Porotchkin 	/*
7346f8de19fSKonstantin Porotchkin 	 * 13. Check the Polarity invert bit
7356f8de19fSKonstantin Porotchkin 	 */
736ee4c70ebSPali Rohár 	data = 0U;
737ee4c70ebSPali Rohár 	if (invert & COMPHY_POLARITY_TXD_INVERT) {
738ee4c70ebSPali Rohár 		data |= TXD_INVERT_BIT;
739ee4c70ebSPali Rohár 	}
740ee4c70ebSPali Rohár 	if (invert & COMPHY_POLARITY_RXD_INVERT) {
741ee4c70ebSPali Rohár 		data |= RXD_INVERT_BIT;
742ee4c70ebSPali Rohár 	}
743ee4c70ebSPali Rohár 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
744be33dce7SPali Rohár 	usb3_reg_set(reg_base, COMPHY_SYNC_PATTERN_REG, data, mask);
7456f8de19fSKonstantin Porotchkin 
7466f8de19fSKonstantin Porotchkin 	/*
7476f8de19fSKonstantin Porotchkin 	 * 14. Set max speed generation to USB3.0 5Gbps
7486f8de19fSKonstantin Porotchkin 	 */
7496f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_SYNC_MASK_GEN_REG, PHY_GEN_USB3_5G,
750be33dce7SPali Rohár 		     PHY_GEN_MAX_MASK);
7516f8de19fSKonstantin Porotchkin 
7526f8de19fSKonstantin Porotchkin 	/*
7536f8de19fSKonstantin Porotchkin 	 * 15. Set capacitor value for FFE gain peaking to 0xF
7546f8de19fSKonstantin Porotchkin 	 */
7556f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GEN3_SETTINGS_3,
756be33dce7SPali Rohár 		     COMPHY_GEN_FFE_CAP_SEL_VALUE, COMPHY_GEN_FFE_CAP_SEL_MASK);
7576f8de19fSKonstantin Porotchkin 
7586f8de19fSKonstantin Porotchkin 	/*
7596f8de19fSKonstantin Porotchkin 	 * 16. Release SW reset
7606f8de19fSKonstantin Porotchkin 	 */
7616f8de19fSKonstantin Porotchkin 	data = MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32 | MODE_REFDIV_BY_4;
7626f8de19fSKonstantin Porotchkin 	usb3_reg_set(reg_base, COMPHY_REG_GLOB_PHY_CTRL0_ADDR, data,
763be33dce7SPali Rohár 		     REG_16_BIT_MASK);
7646f8de19fSKonstantin Porotchkin 
7656f8de19fSKonstantin Porotchkin 	/* Wait for > 55 us to allow PCLK be enabled */
7666f8de19fSKonstantin Porotchkin 	udelay(PLL_SET_DELAY_US);
7676f8de19fSKonstantin Porotchkin 
7686f8de19fSKonstantin Porotchkin 	if (comphy_index == COMPHY_LANE2) {
769b662232dSMarek Behún 		data = COMPHY_REG_LANE_STATUS1_ADDR + USB3PHY_LANE2_REG_BASE_OFFSET;
7706f8de19fSKonstantin Porotchkin 		mmio_write_32(reg_base + COMPHY_LANE2_INDIR_ADDR_OFFSET,
7716f8de19fSKonstantin Porotchkin 			      data);
7726f8de19fSKonstantin Porotchkin 
773b662232dSMarek Behún 		addr = reg_base + COMPHY_LANE2_INDIR_DATA_OFFSET;
7746f8de19fSKonstantin Porotchkin 		ret = polling_with_timeout(addr, TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
7756f8de19fSKonstantin Porotchkin 					   COMPHY_PLL_TIMEOUT, REG_32BIT);
7766f8de19fSKonstantin Porotchkin 	} else {
7776f8de19fSKonstantin Porotchkin 		ret = polling_with_timeout(LANE_STATUS1_ADDR(USB3) + reg_base,
7786f8de19fSKonstantin Porotchkin 					   TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
7796f8de19fSKonstantin Porotchkin 					   COMPHY_PLL_TIMEOUT, REG_16BIT);
7806f8de19fSKonstantin Porotchkin 	}
7816f8de19fSKonstantin Porotchkin 	if (ret)
7826f8de19fSKonstantin Porotchkin 		ERROR("Failed to lock USB3 PLL\n");
7836f8de19fSKonstantin Porotchkin 
7846f8de19fSKonstantin Porotchkin 	debug_exit();
7856f8de19fSKonstantin Porotchkin 
7866f8de19fSKonstantin Porotchkin 	return ret;
7876f8de19fSKonstantin Porotchkin }
7886f8de19fSKonstantin Porotchkin 
7896f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_pcie_power_on(uint8_t comphy_index,
7906f8de19fSKonstantin Porotchkin 					    uint32_t comphy_mode)
7916f8de19fSKonstantin Porotchkin {
7926f8de19fSKonstantin Porotchkin 	int ret;
7936f8de19fSKonstantin Porotchkin 	uint32_t ref_clk;
794ee4c70ebSPali Rohár 	uint32_t mask, data;
7956f8de19fSKonstantin Porotchkin 	int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
7966f8de19fSKonstantin Porotchkin 
7976f8de19fSKonstantin Porotchkin 	debug_enter();
7986f8de19fSKonstantin Porotchkin 
7996f8de19fSKonstantin Porotchkin 	/* 1. Enable max PLL. */
8006f8de19fSKonstantin Porotchkin 	reg_set16(LANE_CFG1_ADDR(PCIE) + COMPHY_SD_ADDR,
801ccec1bd5SPali Rohár 		  USE_MAX_PLL_RATE_EN, USE_MAX_PLL_RATE_EN);
8026f8de19fSKonstantin Porotchkin 
8036f8de19fSKonstantin Porotchkin 	/* 2. Select 20 bit SERDES interface. */
8046f8de19fSKonstantin Porotchkin 	reg_set16(GLOB_CLK_SRC_LO_ADDR(PCIE) + COMPHY_SD_ADDR,
805ccec1bd5SPali Rohár 		  CFG_SEL_20B, CFG_SEL_20B);
8066f8de19fSKonstantin Porotchkin 
8076f8de19fSKonstantin Porotchkin 	/* 3. Force to use reg setting for PCIe mode */
8086f8de19fSKonstantin Porotchkin 	reg_set16(MISC_REG1_ADDR(PCIE) + COMPHY_SD_ADDR,
809ccec1bd5SPali Rohár 		  SEL_BITS_PCIE_FORCE, SEL_BITS_PCIE_FORCE);
8106f8de19fSKonstantin Porotchkin 
8116f8de19fSKonstantin Porotchkin 	/* 4. Change RX wait */
8126f8de19fSKonstantin Porotchkin 	reg_set16(PWR_MGM_TIM1_ADDR(PCIE) + COMPHY_SD_ADDR,
8136f8de19fSKonstantin Porotchkin 		  CFG_PM_RXDEN_WAIT_1_UNIT | CFG_PM_RXDLOZ_WAIT_12_UNIT,
8146f8de19fSKonstantin Porotchkin 		  (CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
8156f8de19fSKonstantin Porotchkin 		   CFG_PM_RXDLOZ_WAIT_MASK));
8166f8de19fSKonstantin Porotchkin 
8176f8de19fSKonstantin Porotchkin 	/* 5. Enable idle sync */
8186f8de19fSKonstantin Porotchkin 	reg_set16(UNIT_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
8196f8de19fSKonstantin Porotchkin 		  UNIT_CTRL_DEFAULT_VALUE | IDLE_SYNC_EN, REG_16_BIT_MASK);
8206f8de19fSKonstantin Porotchkin 
8216f8de19fSKonstantin Porotchkin 	/* 6. Enable the output of 100M/125M/500M clock */
8226f8de19fSKonstantin Porotchkin 	reg_set16(MISC_REG0_ADDR(PCIE) + COMPHY_SD_ADDR,
82340d08192SPali Rohár 		  MISC_REG0_DEFAULT_VALUE | CLK500M_EN | TXDCLK_2X_SEL | CLK100M_125M_EN,
8246f8de19fSKonstantin Porotchkin 		  REG_16_BIT_MASK);
8256f8de19fSKonstantin Porotchkin 
8266f8de19fSKonstantin Porotchkin 	/*
8276f8de19fSKonstantin Porotchkin 	 * 7. Enable TX, PCIE global register, 0xd0074814, it is done in
8286f8de19fSKonstantin Porotchkin 	 * PCI-E driver
8296f8de19fSKonstantin Porotchkin 	 */
8306f8de19fSKonstantin Porotchkin 
8316f8de19fSKonstantin Porotchkin 	/*
8326f8de19fSKonstantin Porotchkin 	 * 8. Check crystal jumper setting and program the Power and PLL
8336f8de19fSKonstantin Porotchkin 	 * Control accordingly
8346f8de19fSKonstantin Porotchkin 	 */
8356f8de19fSKonstantin Porotchkin 
8366f8de19fSKonstantin Porotchkin 	if (get_ref_clk() == 40)
8376f8de19fSKonstantin Porotchkin 		ref_clk = REF_CLOCK_SPEED_40M;
8386f8de19fSKonstantin Porotchkin 	else
8396f8de19fSKonstantin Porotchkin 		ref_clk = PCIE_REF_CLOCK_SPEED_25M;
8406f8de19fSKonstantin Porotchkin 
8416f8de19fSKonstantin Porotchkin 	reg_set16(PWR_PLL_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
8426f8de19fSKonstantin Porotchkin 		  (PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
8436f8de19fSKonstantin Porotchkin 		   PU_TX_INTP_BIT | PU_DFE_BIT | ref_clk | PHY_MODE_PCIE),
8446f8de19fSKonstantin Porotchkin 		  REG_16_BIT_MASK);
8456f8de19fSKonstantin Porotchkin 
8466f8de19fSKonstantin Porotchkin 	/* 9. Override Speed_PLL value and use MAC PLL */
8476f8de19fSKonstantin Porotchkin 	reg_set16(KVCO_CAL_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
8486f8de19fSKonstantin Porotchkin 		  SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT, REG_16_BIT_MASK);
8496f8de19fSKonstantin Porotchkin 
8506f8de19fSKonstantin Porotchkin 	/* 10. Check the Polarity invert bit */
851ee4c70ebSPali Rohár 	data = 0U;
852ee4c70ebSPali Rohár 	if (invert & COMPHY_POLARITY_TXD_INVERT) {
853ee4c70ebSPali Rohár 		data |= TXD_INVERT_BIT;
854ee4c70ebSPali Rohár 	}
855ee4c70ebSPali Rohár 	if (invert & COMPHY_POLARITY_RXD_INVERT) {
856ee4c70ebSPali Rohár 		data |= RXD_INVERT_BIT;
857ee4c70ebSPali Rohár 	}
858ee4c70ebSPali Rohár 	mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
859ee4c70ebSPali Rohár 	reg_set16(SYNC_PATTERN_REG_ADDR(PCIE) + COMPHY_SD_ADDR, data, mask);
8606f8de19fSKonstantin Porotchkin 
8616f8de19fSKonstantin Porotchkin 	/* 11. Release SW reset */
8626f8de19fSKonstantin Porotchkin 	reg_set16(GLOB_PHY_CTRL0_ADDR(PCIE) + COMPHY_SD_ADDR,
8636f8de19fSKonstantin Porotchkin 		  MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32,
8646f8de19fSKonstantin Porotchkin 		  SOFT_RESET | MODE_REFDIV);
8656f8de19fSKonstantin Porotchkin 
8666f8de19fSKonstantin Porotchkin 	/* Wait for > 55 us to allow PCLK be enabled */
8676f8de19fSKonstantin Porotchkin 	udelay(PLL_SET_DELAY_US);
8686f8de19fSKonstantin Porotchkin 
8696f8de19fSKonstantin Porotchkin 	ret = polling_with_timeout(LANE_STATUS1_ADDR(PCIE) + COMPHY_SD_ADDR,
8706f8de19fSKonstantin Porotchkin 				   TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
8716f8de19fSKonstantin Porotchkin 				   COMPHY_PLL_TIMEOUT, REG_16BIT);
8726f8de19fSKonstantin Porotchkin 	if (ret)
8736f8de19fSKonstantin Porotchkin 		ERROR("Failed to lock PCIE PLL\n");
8746f8de19fSKonstantin Porotchkin 
8756f8de19fSKonstantin Porotchkin 	debug_exit();
8766f8de19fSKonstantin Porotchkin 
8776f8de19fSKonstantin Porotchkin 	return ret;
8786f8de19fSKonstantin Porotchkin }
8796f8de19fSKonstantin Porotchkin 
8806f8de19fSKonstantin Porotchkin int mvebu_3700_comphy_power_on(uint8_t comphy_index, uint32_t comphy_mode)
8816f8de19fSKonstantin Porotchkin {
8826f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
8836f8de19fSKonstantin Porotchkin 	int ret = 0;
8846f8de19fSKonstantin Porotchkin 
8856f8de19fSKonstantin Porotchkin 	debug_enter();
8866f8de19fSKonstantin Porotchkin 
8876f8de19fSKonstantin Porotchkin 	switch (mode) {
8886f8de19fSKonstantin Porotchkin 	case(COMPHY_SATA_MODE):
8896f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_sata_power_on(comphy_index,
8906f8de19fSKonstantin Porotchkin 						       comphy_mode);
8916f8de19fSKonstantin Porotchkin 		break;
8926f8de19fSKonstantin Porotchkin 	case(COMPHY_SGMII_MODE):
893a669983cSPali Rohár 	case(COMPHY_2500BASEX_MODE):
8946f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_sgmii_power_on(comphy_index,
8956f8de19fSKonstantin Porotchkin 							comphy_mode);
8966f8de19fSKonstantin Porotchkin 		break;
8976f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3_MODE):
8986f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3H_MODE):
8996f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_usb3_power_on(comphy_index,
9006f8de19fSKonstantin Porotchkin 						       comphy_mode);
9016f8de19fSKonstantin Porotchkin 		break;
9026f8de19fSKonstantin Porotchkin 	case (COMPHY_PCIE_MODE):
9036f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_pcie_power_on(comphy_index,
9046f8de19fSKonstantin Porotchkin 						       comphy_mode);
9056f8de19fSKonstantin Porotchkin 		break;
9066f8de19fSKonstantin Porotchkin 	default:
9076f8de19fSKonstantin Porotchkin 		ERROR("comphy%d: unsupported comphy mode\n", comphy_index);
9086f8de19fSKonstantin Porotchkin 		ret = -EINVAL;
9096f8de19fSKonstantin Porotchkin 		break;
9106f8de19fSKonstantin Porotchkin 	}
9116f8de19fSKonstantin Porotchkin 
9126f8de19fSKonstantin Porotchkin 	debug_exit();
9136f8de19fSKonstantin Porotchkin 
9146f8de19fSKonstantin Porotchkin 	return ret;
9156f8de19fSKonstantin Porotchkin }
9166f8de19fSKonstantin Porotchkin 
9176f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_usb3_power_off(void)
9186f8de19fSKonstantin Porotchkin {
9196f8de19fSKonstantin Porotchkin 	/*
9206f8de19fSKonstantin Porotchkin 	 * Currently the USB3 MAC will control the USB3 PHY to set it to low
9216f8de19fSKonstantin Porotchkin 	 * state, thus do not need to power off USB3 PHY again.
9226f8de19fSKonstantin Porotchkin 	 */
9236f8de19fSKonstantin Porotchkin 	debug_enter();
9246f8de19fSKonstantin Porotchkin 	debug_exit();
9256f8de19fSKonstantin Porotchkin 
9266f8de19fSKonstantin Porotchkin 	return 0;
9276f8de19fSKonstantin Porotchkin }
9286f8de19fSKonstantin Porotchkin 
929fc299ce0SPali Rohár static int mvebu_a3700_comphy_sata_power_off(void)
9306f8de19fSKonstantin Porotchkin {
9316f8de19fSKonstantin Porotchkin 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
9326f8de19fSKonstantin Porotchkin 	uint32_t offset;
9336f8de19fSKonstantin Porotchkin 
9346f8de19fSKonstantin Porotchkin 	debug_enter();
9356f8de19fSKonstantin Porotchkin 
9366f8de19fSKonstantin Porotchkin 	/* Set phy isolation mode */
9376f8de19fSKonstantin Porotchkin 	offset = COMPHY_ISOLATION_CTRL_REG + SATAPHY_LANE2_REG_BASE_OFFSET;
938be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, PHY_ISOLATE_MODE,
939be33dce7SPali Rohár 				 PHY_ISOLATE_MODE);
9406f8de19fSKonstantin Porotchkin 
9416f8de19fSKonstantin Porotchkin 	/* Power off PLL, Tx, Rx */
9426f8de19fSKonstantin Porotchkin 	offset = COMPHY_POWER_PLL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
943be33dce7SPali Rohár 	comphy_sata_set_indirect(comphy_indir_regs, offset, 0,
944be33dce7SPali Rohár 				 PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT);
9456f8de19fSKonstantin Porotchkin 
9466f8de19fSKonstantin Porotchkin 	debug_exit();
9476f8de19fSKonstantin Porotchkin 
9486f8de19fSKonstantin Porotchkin 	return 0;
9496f8de19fSKonstantin Porotchkin }
9506f8de19fSKonstantin Porotchkin 
9516f8de19fSKonstantin Porotchkin int mvebu_3700_comphy_power_off(uint8_t comphy_index, uint32_t comphy_mode)
9526f8de19fSKonstantin Porotchkin {
9536f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
9546f8de19fSKonstantin Porotchkin 	int err = 0;
9556f8de19fSKonstantin Porotchkin 
9566f8de19fSKonstantin Porotchkin 	debug_enter();
9576f8de19fSKonstantin Porotchkin 
958629dd61fSMarek Behún 	if (!mode) {
959629dd61fSMarek Behún 		/*
960629dd61fSMarek Behún 		 * The user did not specify which mode should be powered off.
961629dd61fSMarek Behún 		 * In this case we can identify this by reading the phy selector
962629dd61fSMarek Behún 		 * register.
963629dd61fSMarek Behún 		 */
964629dd61fSMarek Behún 		mode = mvebu_a3700_comphy_get_mode(comphy_index);
965629dd61fSMarek Behún 	}
966629dd61fSMarek Behún 
9676f8de19fSKonstantin Porotchkin 	switch (mode) {
968629dd61fSMarek Behún 	case(COMPHY_SGMII_MODE):
969a669983cSPali Rohár 	case(COMPHY_2500BASEX_MODE):
970629dd61fSMarek Behún 		err = mvebu_a3700_comphy_sgmii_power_off(comphy_index);
971629dd61fSMarek Behún 		break;
9726f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3_MODE):
9736f8de19fSKonstantin Porotchkin 	case (COMPHY_USB3H_MODE):
9746f8de19fSKonstantin Porotchkin 		err = mvebu_a3700_comphy_usb3_power_off();
9756f8de19fSKonstantin Porotchkin 		break;
9766f8de19fSKonstantin Porotchkin 	case (COMPHY_SATA_MODE):
977fc299ce0SPali Rohár 		err = mvebu_a3700_comphy_sata_power_off();
9786f8de19fSKonstantin Porotchkin 		break;
9796f8de19fSKonstantin Porotchkin 
9806f8de19fSKonstantin Porotchkin 	default:
9816f8de19fSKonstantin Porotchkin 		debug("comphy%d: power off is not implemented for mode %d\n",
9826f8de19fSKonstantin Porotchkin 		      comphy_index, mode);
9836f8de19fSKonstantin Porotchkin 		break;
9846f8de19fSKonstantin Porotchkin 	}
9856f8de19fSKonstantin Porotchkin 
9866f8de19fSKonstantin Porotchkin 	debug_exit();
9876f8de19fSKonstantin Porotchkin 
9886f8de19fSKonstantin Porotchkin 	return err;
9896f8de19fSKonstantin Porotchkin }
9906f8de19fSKonstantin Porotchkin 
9916f8de19fSKonstantin Porotchkin static int mvebu_a3700_comphy_sata_is_pll_locked(void)
9926f8de19fSKonstantin Porotchkin {
9936f8de19fSKonstantin Porotchkin 	uint32_t data, addr;
9946f8de19fSKonstantin Porotchkin 	uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
9956f8de19fSKonstantin Porotchkin 	int ret = 0;
9966f8de19fSKonstantin Porotchkin 
9976f8de19fSKonstantin Porotchkin 	debug_enter();
9986f8de19fSKonstantin Porotchkin 
9996f8de19fSKonstantin Porotchkin 	/* Polling status */
10006f8de19fSKonstantin Porotchkin 	mmio_write_32(comphy_indir_regs + COMPHY_LANE2_INDIR_ADDR_OFFSET,
10016f8de19fSKonstantin Porotchkin 	       COMPHY_LOOPBACK_REG0 + SATAPHY_LANE2_REG_BASE_OFFSET);
10026f8de19fSKonstantin Porotchkin 	addr = comphy_indir_regs + COMPHY_LANE2_INDIR_DATA_OFFSET;
10036f8de19fSKonstantin Porotchkin 	data = polling_with_timeout(addr, PLL_READY_TX_BIT, PLL_READY_TX_BIT,
10046f8de19fSKonstantin Porotchkin 				    COMPHY_PLL_TIMEOUT, REG_32BIT);
10056f8de19fSKonstantin Porotchkin 
10066f8de19fSKonstantin Porotchkin 	if (data != 0) {
10076f8de19fSKonstantin Porotchkin 		ERROR("TX PLL is not locked\n");
10086f8de19fSKonstantin Porotchkin 		ret = -ETIMEDOUT;
10096f8de19fSKonstantin Porotchkin 	}
10106f8de19fSKonstantin Porotchkin 
10116f8de19fSKonstantin Porotchkin 	debug_exit();
10126f8de19fSKonstantin Porotchkin 
10136f8de19fSKonstantin Porotchkin 	return ret;
10146f8de19fSKonstantin Porotchkin }
10156f8de19fSKonstantin Porotchkin 
10166f8de19fSKonstantin Porotchkin int mvebu_3700_comphy_is_pll_locked(uint8_t comphy_index, uint32_t comphy_mode)
10176f8de19fSKonstantin Porotchkin {
10186f8de19fSKonstantin Porotchkin 	int mode = COMPHY_GET_MODE(comphy_mode);
10196f8de19fSKonstantin Porotchkin 	int ret = 0;
10206f8de19fSKonstantin Porotchkin 
10216f8de19fSKonstantin Porotchkin 	debug_enter();
10226f8de19fSKonstantin Porotchkin 
10236f8de19fSKonstantin Porotchkin 	switch (mode) {
10246f8de19fSKonstantin Porotchkin 	case(COMPHY_SATA_MODE):
10256f8de19fSKonstantin Porotchkin 		ret = mvebu_a3700_comphy_sata_is_pll_locked();
10266f8de19fSKonstantin Porotchkin 		break;
10276f8de19fSKonstantin Porotchkin 
10286f8de19fSKonstantin Porotchkin 	default:
10296f8de19fSKonstantin Porotchkin 		ERROR("comphy[%d] mode[%d] doesn't support PLL lock check\n",
10306f8de19fSKonstantin Porotchkin 			comphy_index, mode);
10316f8de19fSKonstantin Porotchkin 		ret = -EINVAL;
10326f8de19fSKonstantin Porotchkin 		break;
10336f8de19fSKonstantin Porotchkin 	}
10346f8de19fSKonstantin Porotchkin 
10356f8de19fSKonstantin Porotchkin 	debug_exit();
10366f8de19fSKonstantin Porotchkin 
10376f8de19fSKonstantin Porotchkin 	return ret;
10386f8de19fSKonstantin Porotchkin }
1039