18c2ecf20Sopenharmony_ci/***********************license start***************
28c2ecf20Sopenharmony_ci * Author: Cavium Networks
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Contact: support@caviumnetworks.com
58c2ecf20Sopenharmony_ci * This file is part of the OCTEON SDK
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2003-2018 Cavium, Inc.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This file is free software; you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License, Version 2, as
118c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This file is distributed in the hope that it will be useful, but
148c2ecf20Sopenharmony_ci * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
158c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
168c2ecf20Sopenharmony_ci * NONINFRINGEMENT.  See the GNU General Public License for more
178c2ecf20Sopenharmony_ci * details.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
208c2ecf20Sopenharmony_ci * along with this file; if not, write to the Free Software
218c2ecf20Sopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
228c2ecf20Sopenharmony_ci * or visit http://www.gnu.org/licenses/.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * This file may also be available under a different license from Cavium.
258c2ecf20Sopenharmony_ci * Contact Cavium Networks for more information
268c2ecf20Sopenharmony_ci ***********************license end**************************************/
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * Functions for SGMII initialization, configuration,
308c2ecf20Sopenharmony_ci * and monitoring.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <asm/octeon/octeon.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-config.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-helper.h>
388c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-helper-board.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-gmxx-defs.h>
418c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-pcsx-defs.h>
428c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-pcsxx-defs.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/**
458c2ecf20Sopenharmony_ci * Perform initialization required only once for an SGMII port.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * @interface: Interface to init
488c2ecf20Sopenharmony_ci * @index:     Index of prot on the interface
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic int __cvmx_helper_sgmii_hardware_init_one_time(int interface, int index)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	const uint64_t clock_mhz = cvmx_sysinfo_get()->cpu_clock_hz / 1000000;
558c2ecf20Sopenharmony_ci	union cvmx_pcsx_miscx_ctl_reg pcs_misc_ctl_reg;
568c2ecf20Sopenharmony_ci	union cvmx_pcsx_linkx_timer_count_reg pcsx_linkx_timer_count_reg;
578c2ecf20Sopenharmony_ci	union cvmx_gmxx_prtx_cfg gmxx_prtx_cfg;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/* Disable GMX */
608c2ecf20Sopenharmony_ci	gmxx_prtx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
618c2ecf20Sopenharmony_ci	gmxx_prtx_cfg.s.en = 0;
628c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/*
658c2ecf20Sopenharmony_ci	 * Write PCS*_LINK*_TIMER_COUNT_REG[COUNT] with the
668c2ecf20Sopenharmony_ci	 * appropriate value. 1000BASE-X specifies a 10ms
678c2ecf20Sopenharmony_ci	 * interval. SGMII specifies a 1.6ms interval.
688c2ecf20Sopenharmony_ci	 */
698c2ecf20Sopenharmony_ci	pcs_misc_ctl_reg.u64 =
708c2ecf20Sopenharmony_ci	    cvmx_read_csr(CVMX_PCSX_MISCX_CTL_REG(index, interface));
718c2ecf20Sopenharmony_ci	pcsx_linkx_timer_count_reg.u64 =
728c2ecf20Sopenharmony_ci	    cvmx_read_csr(CVMX_PCSX_LINKX_TIMER_COUNT_REG(index, interface));
738c2ecf20Sopenharmony_ci	if (pcs_misc_ctl_reg.s.mode) {
748c2ecf20Sopenharmony_ci		/* 1000BASE-X */
758c2ecf20Sopenharmony_ci		pcsx_linkx_timer_count_reg.s.count =
768c2ecf20Sopenharmony_ci		    (10000ull * clock_mhz) >> 10;
778c2ecf20Sopenharmony_ci	} else {
788c2ecf20Sopenharmony_ci		/* SGMII */
798c2ecf20Sopenharmony_ci		pcsx_linkx_timer_count_reg.s.count =
808c2ecf20Sopenharmony_ci		    (1600ull * clock_mhz) >> 10;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_PCSX_LINKX_TIMER_COUNT_REG(index, interface),
838c2ecf20Sopenharmony_ci		       pcsx_linkx_timer_count_reg.u64);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/*
868c2ecf20Sopenharmony_ci	 * Write the advertisement register to be used as the
878c2ecf20Sopenharmony_ci	 * tx_Config_Reg<D15:D0> of the autonegotiation.  In
888c2ecf20Sopenharmony_ci	 * 1000BASE-X mode, tx_Config_Reg<D15:D0> is PCS*_AN*_ADV_REG.
898c2ecf20Sopenharmony_ci	 * In SGMII PHY mode, tx_Config_Reg<D15:D0> is
908c2ecf20Sopenharmony_ci	 * PCS*_SGM*_AN_ADV_REG.  In SGMII MAC mode,
918c2ecf20Sopenharmony_ci	 * tx_Config_Reg<D15:D0> is the fixed value 0x4001, so this
928c2ecf20Sopenharmony_ci	 * step can be skipped.
938c2ecf20Sopenharmony_ci	 */
948c2ecf20Sopenharmony_ci	if (pcs_misc_ctl_reg.s.mode) {
958c2ecf20Sopenharmony_ci		/* 1000BASE-X */
968c2ecf20Sopenharmony_ci		union cvmx_pcsx_anx_adv_reg pcsx_anx_adv_reg;
978c2ecf20Sopenharmony_ci		pcsx_anx_adv_reg.u64 =
988c2ecf20Sopenharmony_ci		    cvmx_read_csr(CVMX_PCSX_ANX_ADV_REG(index, interface));
998c2ecf20Sopenharmony_ci		pcsx_anx_adv_reg.s.rem_flt = 0;
1008c2ecf20Sopenharmony_ci		pcsx_anx_adv_reg.s.pause = 3;
1018c2ecf20Sopenharmony_ci		pcsx_anx_adv_reg.s.hfd = 1;
1028c2ecf20Sopenharmony_ci		pcsx_anx_adv_reg.s.fd = 1;
1038c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_PCSX_ANX_ADV_REG(index, interface),
1048c2ecf20Sopenharmony_ci			       pcsx_anx_adv_reg.u64);
1058c2ecf20Sopenharmony_ci	} else {
1068c2ecf20Sopenharmony_ci		union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
1078c2ecf20Sopenharmony_ci		pcsx_miscx_ctl_reg.u64 =
1088c2ecf20Sopenharmony_ci		    cvmx_read_csr(CVMX_PCSX_MISCX_CTL_REG(index, interface));
1098c2ecf20Sopenharmony_ci		if (pcsx_miscx_ctl_reg.s.mac_phy) {
1108c2ecf20Sopenharmony_ci			/* PHY Mode */
1118c2ecf20Sopenharmony_ci			union cvmx_pcsx_sgmx_an_adv_reg pcsx_sgmx_an_adv_reg;
1128c2ecf20Sopenharmony_ci			pcsx_sgmx_an_adv_reg.u64 =
1138c2ecf20Sopenharmony_ci			    cvmx_read_csr(CVMX_PCSX_SGMX_AN_ADV_REG
1148c2ecf20Sopenharmony_ci					  (index, interface));
1158c2ecf20Sopenharmony_ci			pcsx_sgmx_an_adv_reg.s.link = 1;
1168c2ecf20Sopenharmony_ci			pcsx_sgmx_an_adv_reg.s.dup = 1;
1178c2ecf20Sopenharmony_ci			pcsx_sgmx_an_adv_reg.s.speed = 2;
1188c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_PCSX_SGMX_AN_ADV_REG
1198c2ecf20Sopenharmony_ci				       (index, interface),
1208c2ecf20Sopenharmony_ci				       pcsx_sgmx_an_adv_reg.u64);
1218c2ecf20Sopenharmony_ci		} else {
1228c2ecf20Sopenharmony_ci			/* MAC Mode - Nothing to do */
1238c2ecf20Sopenharmony_ci		}
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci	return 0;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/**
1298c2ecf20Sopenharmony_ci * Initialize the SERTES link for the first time or after a loss
1308c2ecf20Sopenharmony_ci * of link.
1318c2ecf20Sopenharmony_ci *
1328c2ecf20Sopenharmony_ci * @interface: Interface to init
1338c2ecf20Sopenharmony_ci * @index:     Index of prot on the interface
1348c2ecf20Sopenharmony_ci *
1358c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_cistatic int __cvmx_helper_sgmii_hardware_init_link(int interface, int index)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	union cvmx_pcsx_mrx_control_reg control_reg;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/*
1428c2ecf20Sopenharmony_ci	 * Take PCS through a reset sequence.
1438c2ecf20Sopenharmony_ci	 * PCS*_MR*_CONTROL_REG[PWR_DN] should be cleared to zero.
1448c2ecf20Sopenharmony_ci	 * Write PCS*_MR*_CONTROL_REG[RESET]=1 (while not changing the
1458c2ecf20Sopenharmony_ci	 * value of the other PCS*_MR*_CONTROL_REG bits).  Read
1468c2ecf20Sopenharmony_ci	 * PCS*_MR*_CONTROL_REG[RESET] until it changes value to
1478c2ecf20Sopenharmony_ci	 * zero.
1488c2ecf20Sopenharmony_ci	 */
1498c2ecf20Sopenharmony_ci	control_reg.u64 =
1508c2ecf20Sopenharmony_ci	    cvmx_read_csr(CVMX_PCSX_MRX_CONTROL_REG(index, interface));
1518c2ecf20Sopenharmony_ci	if (cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) {
1528c2ecf20Sopenharmony_ci		control_reg.s.reset = 1;
1538c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_PCSX_MRX_CONTROL_REG(index, interface),
1548c2ecf20Sopenharmony_ci			       control_reg.u64);
1558c2ecf20Sopenharmony_ci		if (CVMX_WAIT_FOR_FIELD64
1568c2ecf20Sopenharmony_ci		    (CVMX_PCSX_MRX_CONTROL_REG(index, interface),
1578c2ecf20Sopenharmony_ci		     union cvmx_pcsx_mrx_control_reg, reset, ==, 0, 10000)) {
1588c2ecf20Sopenharmony_ci			cvmx_dprintf("SGMII%d: Timeout waiting for port %d "
1598c2ecf20Sopenharmony_ci				     "to finish reset\n",
1608c2ecf20Sopenharmony_ci			     interface, index);
1618c2ecf20Sopenharmony_ci			return -1;
1628c2ecf20Sopenharmony_ci		}
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/*
1668c2ecf20Sopenharmony_ci	 * Write PCS*_MR*_CONTROL_REG[RST_AN]=1 to ensure a fresh
1678c2ecf20Sopenharmony_ci	 * sgmii negotiation starts.
1688c2ecf20Sopenharmony_ci	 */
1698c2ecf20Sopenharmony_ci	control_reg.s.rst_an = 1;
1708c2ecf20Sopenharmony_ci	control_reg.s.an_en = 1;
1718c2ecf20Sopenharmony_ci	control_reg.s.pwr_dn = 0;
1728c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_PCSX_MRX_CONTROL_REG(index, interface),
1738c2ecf20Sopenharmony_ci		       control_reg.u64);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/*
1768c2ecf20Sopenharmony_ci	 * Wait for PCS*_MR*_STATUS_REG[AN_CPT] to be set, indicating
1778c2ecf20Sopenharmony_ci	 * that sgmii autonegotiation is complete. In MAC mode this
1788c2ecf20Sopenharmony_ci	 * isn't an ethernet link, but a link between Octeon and the
1798c2ecf20Sopenharmony_ci	 * PHY.
1808c2ecf20Sopenharmony_ci	 */
1818c2ecf20Sopenharmony_ci	if ((cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) &&
1828c2ecf20Sopenharmony_ci	    CVMX_WAIT_FOR_FIELD64(CVMX_PCSX_MRX_STATUS_REG(index, interface),
1838c2ecf20Sopenharmony_ci				  union cvmx_pcsx_mrx_status_reg, an_cpt, ==, 1,
1848c2ecf20Sopenharmony_ci				  10000)) {
1858c2ecf20Sopenharmony_ci		/* cvmx_dprintf("SGMII%d: Port %d link timeout\n", interface, index); */
1868c2ecf20Sopenharmony_ci		return -1;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	return 0;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci/**
1928c2ecf20Sopenharmony_ci * Configure an SGMII link to the specified speed after the SERTES
1938c2ecf20Sopenharmony_ci * link is up.
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * @interface: Interface to init
1968c2ecf20Sopenharmony_ci * @index:     Index of prot on the interface
1978c2ecf20Sopenharmony_ci * @link_info: Link state to configure
1988c2ecf20Sopenharmony_ci *
1998c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
2008c2ecf20Sopenharmony_ci */
2018c2ecf20Sopenharmony_cistatic int __cvmx_helper_sgmii_hardware_init_link_speed(int interface,
2028c2ecf20Sopenharmony_ci							int index,
2038c2ecf20Sopenharmony_ci							union cvmx_helper_link_info
2048c2ecf20Sopenharmony_ci							link_info)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	int is_enabled;
2078c2ecf20Sopenharmony_ci	union cvmx_gmxx_prtx_cfg gmxx_prtx_cfg;
2088c2ecf20Sopenharmony_ci	union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Disable GMX before we make any changes. Remember the enable state */
2118c2ecf20Sopenharmony_ci	gmxx_prtx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
2128c2ecf20Sopenharmony_ci	is_enabled = gmxx_prtx_cfg.s.en;
2138c2ecf20Sopenharmony_ci	gmxx_prtx_cfg.s.en = 0;
2148c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/* Wait for GMX to be idle */
2178c2ecf20Sopenharmony_ci	if (CVMX_WAIT_FOR_FIELD64
2188c2ecf20Sopenharmony_ci	    (CVMX_GMXX_PRTX_CFG(index, interface), union cvmx_gmxx_prtx_cfg,
2198c2ecf20Sopenharmony_ci	     rx_idle, ==, 1, 10000)
2208c2ecf20Sopenharmony_ci	    || CVMX_WAIT_FOR_FIELD64(CVMX_GMXX_PRTX_CFG(index, interface),
2218c2ecf20Sopenharmony_ci				     union cvmx_gmxx_prtx_cfg, tx_idle, ==, 1,
2228c2ecf20Sopenharmony_ci				     10000)) {
2238c2ecf20Sopenharmony_ci		cvmx_dprintf
2248c2ecf20Sopenharmony_ci		    ("SGMII%d: Timeout waiting for port %d to be idle\n",
2258c2ecf20Sopenharmony_ci		     interface, index);
2268c2ecf20Sopenharmony_ci		return -1;
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Read GMX CFG again to make sure the disable completed */
2308c2ecf20Sopenharmony_ci	gmxx_prtx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/*
2338c2ecf20Sopenharmony_ci	 * Get the misc control for PCS. We will need to set the
2348c2ecf20Sopenharmony_ci	 * duplication amount.
2358c2ecf20Sopenharmony_ci	 */
2368c2ecf20Sopenharmony_ci	pcsx_miscx_ctl_reg.u64 =
2378c2ecf20Sopenharmony_ci	    cvmx_read_csr(CVMX_PCSX_MISCX_CTL_REG(index, interface));
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/*
2408c2ecf20Sopenharmony_ci	 * Use GMXENO to force the link down if the status we get says
2418c2ecf20Sopenharmony_ci	 * it should be down.
2428c2ecf20Sopenharmony_ci	 */
2438c2ecf20Sopenharmony_ci	pcsx_miscx_ctl_reg.s.gmxeno = !link_info.s.link_up;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	/* Only change the duplex setting if the link is up */
2468c2ecf20Sopenharmony_ci	if (link_info.s.link_up)
2478c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.duplex = link_info.s.full_duplex;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	/* Do speed based setting for GMX */
2508c2ecf20Sopenharmony_ci	switch (link_info.s.speed) {
2518c2ecf20Sopenharmony_ci	case 10:
2528c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.speed = 0;
2538c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.speed_msb = 1;
2548c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.slottime = 0;
2558c2ecf20Sopenharmony_ci		/* Setting from GMX-603 */
2568c2ecf20Sopenharmony_ci		pcsx_miscx_ctl_reg.s.samp_pt = 25;
2578c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 64);
2588c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);
2598c2ecf20Sopenharmony_ci		break;
2608c2ecf20Sopenharmony_ci	case 100:
2618c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.speed = 0;
2628c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.speed_msb = 0;
2638c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.slottime = 0;
2648c2ecf20Sopenharmony_ci		pcsx_miscx_ctl_reg.s.samp_pt = 0x5;
2658c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 64);
2668c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);
2678c2ecf20Sopenharmony_ci		break;
2688c2ecf20Sopenharmony_ci	case 1000:
2698c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.speed = 1;
2708c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.speed_msb = 0;
2718c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.slottime = 1;
2728c2ecf20Sopenharmony_ci		pcsx_miscx_ctl_reg.s.samp_pt = 1;
2738c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 512);
2748c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 8192);
2758c2ecf20Sopenharmony_ci		break;
2768c2ecf20Sopenharmony_ci	default:
2778c2ecf20Sopenharmony_ci		break;
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	/* Write the new misc control for PCS */
2818c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_PCSX_MISCX_CTL_REG(index, interface),
2828c2ecf20Sopenharmony_ci		       pcsx_miscx_ctl_reg.u64);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* Write the new GMX settings with the port still disabled */
2858c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* Read GMX CFG again to make sure the config completed */
2888c2ecf20Sopenharmony_ci	gmxx_prtx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	/* Restore the enabled / disabled state */
2918c2ecf20Sopenharmony_ci	gmxx_prtx_cfg.s.en = is_enabled;
2928c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return 0;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/**
2988c2ecf20Sopenharmony_ci * Bring up the SGMII interface to be ready for packet I/O but
2998c2ecf20Sopenharmony_ci * leave I/O disabled using the GMX override. This function
3008c2ecf20Sopenharmony_ci * follows the bringup documented in 10.6.3 of the manual.
3018c2ecf20Sopenharmony_ci *
3028c2ecf20Sopenharmony_ci * @interface: Interface to bringup
3038c2ecf20Sopenharmony_ci * @num_ports: Number of ports on the interface
3048c2ecf20Sopenharmony_ci *
3058c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
3068c2ecf20Sopenharmony_ci */
3078c2ecf20Sopenharmony_cistatic int __cvmx_helper_sgmii_hardware_init(int interface, int num_ports)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	int index;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	__cvmx_helper_setup_gmx(interface, num_ports);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	for (index = 0; index < num_ports; index++) {
3148c2ecf20Sopenharmony_ci		int ipd_port = cvmx_helper_get_ipd_port(interface, index);
3158c2ecf20Sopenharmony_ci		__cvmx_helper_sgmii_hardware_init_one_time(interface, index);
3168c2ecf20Sopenharmony_ci		/* Linux kernel driver will call ....link_set with the
3178c2ecf20Sopenharmony_ci		 * proper link state. In the simulator there is no
3188c2ecf20Sopenharmony_ci		 * link state polling and hence it is set from
3198c2ecf20Sopenharmony_ci		 * here.
3208c2ecf20Sopenharmony_ci		 */
3218c2ecf20Sopenharmony_ci		if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
3228c2ecf20Sopenharmony_ci			__cvmx_helper_sgmii_link_set(ipd_port,
3238c2ecf20Sopenharmony_ci				       __cvmx_helper_sgmii_link_get(ipd_port));
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	return 0;
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ciint __cvmx_helper_sgmii_enumerate(int interface)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	return 4;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci/**
3348c2ecf20Sopenharmony_ci * Probe a SGMII interface and determine the number of ports
3358c2ecf20Sopenharmony_ci * connected to it. The SGMII interface should still be down after
3368c2ecf20Sopenharmony_ci * this call.
3378c2ecf20Sopenharmony_ci *
3388c2ecf20Sopenharmony_ci * @interface: Interface to probe
3398c2ecf20Sopenharmony_ci *
3408c2ecf20Sopenharmony_ci * Returns Number of ports on the interface. Zero to disable.
3418c2ecf20Sopenharmony_ci */
3428c2ecf20Sopenharmony_ciint __cvmx_helper_sgmii_probe(int interface)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	union cvmx_gmxx_inf_mode mode;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/*
3478c2ecf20Sopenharmony_ci	 * Due to errata GMX-700 on CN56XXp1.x and CN52XXp1.x, the
3488c2ecf20Sopenharmony_ci	 * interface needs to be enabled before IPD otherwise per port
3498c2ecf20Sopenharmony_ci	 * backpressure may not work properly
3508c2ecf20Sopenharmony_ci	 */
3518c2ecf20Sopenharmony_ci	mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
3528c2ecf20Sopenharmony_ci	mode.s.en = 1;
3538c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_INF_MODE(interface), mode.u64);
3548c2ecf20Sopenharmony_ci	return __cvmx_helper_sgmii_enumerate(interface);
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci/**
3588c2ecf20Sopenharmony_ci * Bringup and enable a SGMII interface. After this call packet
3598c2ecf20Sopenharmony_ci * I/O should be fully functional. This is called with IPD
3608c2ecf20Sopenharmony_ci * enabled but PKO disabled.
3618c2ecf20Sopenharmony_ci *
3628c2ecf20Sopenharmony_ci * @interface: Interface to bring up
3638c2ecf20Sopenharmony_ci *
3648c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
3658c2ecf20Sopenharmony_ci */
3668c2ecf20Sopenharmony_ciint __cvmx_helper_sgmii_enable(int interface)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	int num_ports = cvmx_helper_ports_on_interface(interface);
3698c2ecf20Sopenharmony_ci	int index;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	__cvmx_helper_sgmii_hardware_init(interface, num_ports);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	for (index = 0; index < num_ports; index++) {
3748c2ecf20Sopenharmony_ci		union cvmx_gmxx_prtx_cfg gmxx_prtx_cfg;
3758c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.u64 =
3768c2ecf20Sopenharmony_ci		    cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
3778c2ecf20Sopenharmony_ci		gmxx_prtx_cfg.s.en = 1;
3788c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
3798c2ecf20Sopenharmony_ci			       gmxx_prtx_cfg.u64);
3808c2ecf20Sopenharmony_ci		__cvmx_interrupt_pcsx_intx_en_reg_enable(index, interface);
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci	__cvmx_interrupt_pcsxx_int_en_reg_enable(interface);
3838c2ecf20Sopenharmony_ci	__cvmx_interrupt_gmxx_enable(interface);
3848c2ecf20Sopenharmony_ci	return 0;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci/**
3888c2ecf20Sopenharmony_ci * Return the link state of an IPD/PKO port as returned by
3898c2ecf20Sopenharmony_ci * auto negotiation. The result of this function may not match
3908c2ecf20Sopenharmony_ci * Octeon's link config if auto negotiation has changed since
3918c2ecf20Sopenharmony_ci * the last call to cvmx_helper_link_set().
3928c2ecf20Sopenharmony_ci *
3938c2ecf20Sopenharmony_ci * @ipd_port: IPD/PKO port to query
3948c2ecf20Sopenharmony_ci *
3958c2ecf20Sopenharmony_ci * Returns Link state
3968c2ecf20Sopenharmony_ci */
3978c2ecf20Sopenharmony_ciunion cvmx_helper_link_info __cvmx_helper_sgmii_link_get(int ipd_port)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	union cvmx_helper_link_info result;
4008c2ecf20Sopenharmony_ci	union cvmx_pcsx_miscx_ctl_reg pcs_misc_ctl_reg;
4018c2ecf20Sopenharmony_ci	int interface = cvmx_helper_get_interface_num(ipd_port);
4028c2ecf20Sopenharmony_ci	int index = cvmx_helper_get_interface_index_num(ipd_port);
4038c2ecf20Sopenharmony_ci	union cvmx_pcsx_mrx_control_reg pcsx_mrx_control_reg;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	result.u64 = 0;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM) {
4088c2ecf20Sopenharmony_ci		/* The simulator gives you a simulated 1Gbps full duplex link */
4098c2ecf20Sopenharmony_ci		result.s.link_up = 1;
4108c2ecf20Sopenharmony_ci		result.s.full_duplex = 1;
4118c2ecf20Sopenharmony_ci		result.s.speed = 1000;
4128c2ecf20Sopenharmony_ci		return result;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	pcsx_mrx_control_reg.u64 =
4168c2ecf20Sopenharmony_ci	    cvmx_read_csr(CVMX_PCSX_MRX_CONTROL_REG(index, interface));
4178c2ecf20Sopenharmony_ci	if (pcsx_mrx_control_reg.s.loopbck1) {
4188c2ecf20Sopenharmony_ci		/* Force 1Gbps full duplex link for internal loopback */
4198c2ecf20Sopenharmony_ci		result.s.link_up = 1;
4208c2ecf20Sopenharmony_ci		result.s.full_duplex = 1;
4218c2ecf20Sopenharmony_ci		result.s.speed = 1000;
4228c2ecf20Sopenharmony_ci		return result;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	pcs_misc_ctl_reg.u64 =
4268c2ecf20Sopenharmony_ci	    cvmx_read_csr(CVMX_PCSX_MISCX_CTL_REG(index, interface));
4278c2ecf20Sopenharmony_ci	if (pcs_misc_ctl_reg.s.mode) {
4288c2ecf20Sopenharmony_ci		/* 1000BASE-X */
4298c2ecf20Sopenharmony_ci		/* FIXME */
4308c2ecf20Sopenharmony_ci	} else {
4318c2ecf20Sopenharmony_ci		union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
4328c2ecf20Sopenharmony_ci		pcsx_miscx_ctl_reg.u64 =
4338c2ecf20Sopenharmony_ci		    cvmx_read_csr(CVMX_PCSX_MISCX_CTL_REG(index, interface));
4348c2ecf20Sopenharmony_ci		if (pcsx_miscx_ctl_reg.s.mac_phy) {
4358c2ecf20Sopenharmony_ci			/* PHY Mode */
4368c2ecf20Sopenharmony_ci			union cvmx_pcsx_mrx_status_reg pcsx_mrx_status_reg;
4378c2ecf20Sopenharmony_ci			union cvmx_pcsx_anx_results_reg pcsx_anx_results_reg;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci			/*
4408c2ecf20Sopenharmony_ci			 * Don't bother continuing if the SERTES low
4418c2ecf20Sopenharmony_ci			 * level link is down
4428c2ecf20Sopenharmony_ci			 */
4438c2ecf20Sopenharmony_ci			pcsx_mrx_status_reg.u64 =
4448c2ecf20Sopenharmony_ci			    cvmx_read_csr(CVMX_PCSX_MRX_STATUS_REG
4458c2ecf20Sopenharmony_ci					  (index, interface));
4468c2ecf20Sopenharmony_ci			if (pcsx_mrx_status_reg.s.lnk_st == 0) {
4478c2ecf20Sopenharmony_ci				if (__cvmx_helper_sgmii_hardware_init_link
4488c2ecf20Sopenharmony_ci				    (interface, index) != 0)
4498c2ecf20Sopenharmony_ci					return result;
4508c2ecf20Sopenharmony_ci			}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci			/* Read the autoneg results */
4538c2ecf20Sopenharmony_ci			pcsx_anx_results_reg.u64 =
4548c2ecf20Sopenharmony_ci			    cvmx_read_csr(CVMX_PCSX_ANX_RESULTS_REG
4558c2ecf20Sopenharmony_ci					  (index, interface));
4568c2ecf20Sopenharmony_ci			if (pcsx_anx_results_reg.s.an_cpt) {
4578c2ecf20Sopenharmony_ci				/*
4588c2ecf20Sopenharmony_ci				 * Auto negotiation is complete. Set
4598c2ecf20Sopenharmony_ci				 * status accordingly.
4608c2ecf20Sopenharmony_ci				 */
4618c2ecf20Sopenharmony_ci				result.s.full_duplex =
4628c2ecf20Sopenharmony_ci				    pcsx_anx_results_reg.s.dup;
4638c2ecf20Sopenharmony_ci				result.s.link_up =
4648c2ecf20Sopenharmony_ci				    pcsx_anx_results_reg.s.link_ok;
4658c2ecf20Sopenharmony_ci				switch (pcsx_anx_results_reg.s.spd) {
4668c2ecf20Sopenharmony_ci				case 0:
4678c2ecf20Sopenharmony_ci					result.s.speed = 10;
4688c2ecf20Sopenharmony_ci					break;
4698c2ecf20Sopenharmony_ci				case 1:
4708c2ecf20Sopenharmony_ci					result.s.speed = 100;
4718c2ecf20Sopenharmony_ci					break;
4728c2ecf20Sopenharmony_ci				case 2:
4738c2ecf20Sopenharmony_ci					result.s.speed = 1000;
4748c2ecf20Sopenharmony_ci					break;
4758c2ecf20Sopenharmony_ci				default:
4768c2ecf20Sopenharmony_ci					result.s.speed = 0;
4778c2ecf20Sopenharmony_ci					result.s.link_up = 0;
4788c2ecf20Sopenharmony_ci					break;
4798c2ecf20Sopenharmony_ci				}
4808c2ecf20Sopenharmony_ci			} else {
4818c2ecf20Sopenharmony_ci				/*
4828c2ecf20Sopenharmony_ci				 * Auto negotiation isn't
4838c2ecf20Sopenharmony_ci				 * complete. Return link down.
4848c2ecf20Sopenharmony_ci				 */
4858c2ecf20Sopenharmony_ci				result.s.speed = 0;
4868c2ecf20Sopenharmony_ci				result.s.link_up = 0;
4878c2ecf20Sopenharmony_ci			}
4888c2ecf20Sopenharmony_ci		} else {	/* MAC Mode */
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci			result = __cvmx_helper_board_link_get(ipd_port);
4918c2ecf20Sopenharmony_ci		}
4928c2ecf20Sopenharmony_ci	}
4938c2ecf20Sopenharmony_ci	return result;
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci/**
4978c2ecf20Sopenharmony_ci * Configure an IPD/PKO port for the specified link state. This
4988c2ecf20Sopenharmony_ci * function does not influence auto negotiation at the PHY level.
4998c2ecf20Sopenharmony_ci * The passed link state must always match the link state returned
5008c2ecf20Sopenharmony_ci * by cvmx_helper_link_get().
5018c2ecf20Sopenharmony_ci *
5028c2ecf20Sopenharmony_ci * @ipd_port:  IPD/PKO port to configure
5038c2ecf20Sopenharmony_ci * @link_info: The new link state
5048c2ecf20Sopenharmony_ci *
5058c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
5068c2ecf20Sopenharmony_ci */
5078c2ecf20Sopenharmony_ciint __cvmx_helper_sgmii_link_set(int ipd_port,
5088c2ecf20Sopenharmony_ci				 union cvmx_helper_link_info link_info)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	int interface = cvmx_helper_get_interface_num(ipd_port);
5118c2ecf20Sopenharmony_ci	int index = cvmx_helper_get_interface_index_num(ipd_port);
5128c2ecf20Sopenharmony_ci	__cvmx_helper_sgmii_hardware_init_link(interface, index);
5138c2ecf20Sopenharmony_ci	return __cvmx_helper_sgmii_hardware_init_link_speed(interface, index,
5148c2ecf20Sopenharmony_ci							    link_info);
5158c2ecf20Sopenharmony_ci}
516