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 RGMII/GMII/MII initialization, configuration,
308c2ecf20Sopenharmony_ci * and monitoring.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci#include <asm/octeon/octeon.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-config.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-pko.h>
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-npi-defs.h>
418c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-gmxx-defs.h>
428c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-asxx-defs.h>
438c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-dbg-defs.h>
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/**
468c2ecf20Sopenharmony_ci * Probe RGMII ports and determine the number present
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * @interface: Interface to probe
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Returns Number of RGMII/GMII/MII ports (0-4).
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ciint __cvmx_helper_rgmii_probe(int interface)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	int num_ports = 0;
558c2ecf20Sopenharmony_ci	union cvmx_gmxx_inf_mode mode;
568c2ecf20Sopenharmony_ci	mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (mode.s.type) {
598c2ecf20Sopenharmony_ci		if (OCTEON_IS_MODEL(OCTEON_CN38XX)
608c2ecf20Sopenharmony_ci		    || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
618c2ecf20Sopenharmony_ci			cvmx_dprintf("ERROR: RGMII initialize called in "
628c2ecf20Sopenharmony_ci				     "SPI interface\n");
638c2ecf20Sopenharmony_ci		} else if (OCTEON_IS_MODEL(OCTEON_CN31XX)
648c2ecf20Sopenharmony_ci			   || OCTEON_IS_MODEL(OCTEON_CN30XX)
658c2ecf20Sopenharmony_ci			   || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
668c2ecf20Sopenharmony_ci			/*
678c2ecf20Sopenharmony_ci			 * On these chips "type" says we're in
688c2ecf20Sopenharmony_ci			 * GMII/MII mode. This limits us to 2 ports
698c2ecf20Sopenharmony_ci			 */
708c2ecf20Sopenharmony_ci			num_ports = 2;
718c2ecf20Sopenharmony_ci		} else {
728c2ecf20Sopenharmony_ci			cvmx_dprintf("ERROR: Unsupported Octeon model in %s\n",
738c2ecf20Sopenharmony_ci				     __func__);
748c2ecf20Sopenharmony_ci		}
758c2ecf20Sopenharmony_ci	} else {
768c2ecf20Sopenharmony_ci		if (OCTEON_IS_MODEL(OCTEON_CN38XX)
778c2ecf20Sopenharmony_ci		    || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
788c2ecf20Sopenharmony_ci			num_ports = 4;
798c2ecf20Sopenharmony_ci		} else if (OCTEON_IS_MODEL(OCTEON_CN31XX)
808c2ecf20Sopenharmony_ci			   || OCTEON_IS_MODEL(OCTEON_CN30XX)
818c2ecf20Sopenharmony_ci			   || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
828c2ecf20Sopenharmony_ci			num_ports = 3;
838c2ecf20Sopenharmony_ci		} else {
848c2ecf20Sopenharmony_ci			cvmx_dprintf("ERROR: Unsupported Octeon model in %s\n",
858c2ecf20Sopenharmony_ci				     __func__);
868c2ecf20Sopenharmony_ci		}
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci	return num_ports;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/**
928c2ecf20Sopenharmony_ci * Put an RGMII interface in loopback mode. Internal packets sent
938c2ecf20Sopenharmony_ci * out will be received back again on the same port. Externally
948c2ecf20Sopenharmony_ci * received packets will echo back out.
958c2ecf20Sopenharmony_ci *
968c2ecf20Sopenharmony_ci * @port:   IPD port number to loop.
978c2ecf20Sopenharmony_ci */
988c2ecf20Sopenharmony_civoid cvmx_helper_rgmii_internal_loopback(int port)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	int interface = (port >> 4) & 1;
1018c2ecf20Sopenharmony_ci	int index = port & 0xf;
1028c2ecf20Sopenharmony_ci	uint64_t tmp;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	union cvmx_gmxx_prtx_cfg gmx_cfg;
1058c2ecf20Sopenharmony_ci	gmx_cfg.u64 = 0;
1068c2ecf20Sopenharmony_ci	gmx_cfg.s.duplex = 1;
1078c2ecf20Sopenharmony_ci	gmx_cfg.s.slottime = 1;
1088c2ecf20Sopenharmony_ci	gmx_cfg.s.speed = 1;
1098c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 1);
1108c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
1118c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
1128c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
1138c2ecf20Sopenharmony_ci	tmp = cvmx_read_csr(CVMX_ASXX_PRT_LOOP(interface));
1148c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_PRT_LOOP(interface), (1 << index) | tmp);
1158c2ecf20Sopenharmony_ci	tmp = cvmx_read_csr(CVMX_ASXX_TX_PRT_EN(interface));
1168c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(interface), (1 << index) | tmp);
1178c2ecf20Sopenharmony_ci	tmp = cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface));
1188c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface), (1 << index) | tmp);
1198c2ecf20Sopenharmony_ci	gmx_cfg.s.en = 1;
1208c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/**
1248c2ecf20Sopenharmony_ci * Workaround ASX setup errata with CN38XX pass1
1258c2ecf20Sopenharmony_ci *
1268c2ecf20Sopenharmony_ci * @interface: Interface to setup
1278c2ecf20Sopenharmony_ci * @port:      Port to setup (0..3)
1288c2ecf20Sopenharmony_ci * @cpu_clock_hz:
1298c2ecf20Sopenharmony_ci *		    Chip frequency in Hertz
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_cistatic int __cvmx_helper_errata_asx_pass1(int interface, int port,
1348c2ecf20Sopenharmony_ci					  int cpu_clock_hz)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	/* Set hi water mark as per errata GMX-4 */
1378c2ecf20Sopenharmony_ci	if (cpu_clock_hz >= 325000000 && cpu_clock_hz < 375000000)
1388c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 12);
1398c2ecf20Sopenharmony_ci	else if (cpu_clock_hz >= 375000000 && cpu_clock_hz < 437000000)
1408c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 11);
1418c2ecf20Sopenharmony_ci	else if (cpu_clock_hz >= 437000000 && cpu_clock_hz < 550000000)
1428c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 10);
1438c2ecf20Sopenharmony_ci	else if (cpu_clock_hz >= 550000000 && cpu_clock_hz < 687000000)
1448c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 9);
1458c2ecf20Sopenharmony_ci	else
1468c2ecf20Sopenharmony_ci		cvmx_dprintf("Illegal clock frequency (%d). "
1478c2ecf20Sopenharmony_ci			"CVMX_ASXX_TX_HI_WATERX not set\n", cpu_clock_hz);
1488c2ecf20Sopenharmony_ci	return 0;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/**
1528c2ecf20Sopenharmony_ci * Configure all of the ASX, GMX, and PKO registers required
1538c2ecf20Sopenharmony_ci * to get RGMII to function on the supplied interface.
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * @interface: PKO Interface to configure (0 or 1)
1568c2ecf20Sopenharmony_ci *
1578c2ecf20Sopenharmony_ci * Returns Zero on success
1588c2ecf20Sopenharmony_ci */
1598c2ecf20Sopenharmony_ciint __cvmx_helper_rgmii_enable(int interface)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	int num_ports = cvmx_helper_ports_on_interface(interface);
1628c2ecf20Sopenharmony_ci	int port;
1638c2ecf20Sopenharmony_ci	struct cvmx_sysinfo *sys_info_ptr = cvmx_sysinfo_get();
1648c2ecf20Sopenharmony_ci	union cvmx_gmxx_inf_mode mode;
1658c2ecf20Sopenharmony_ci	union cvmx_asxx_tx_prt_en asx_tx;
1668c2ecf20Sopenharmony_ci	union cvmx_asxx_rx_prt_en asx_rx;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	if (mode.s.en == 0)
1718c2ecf20Sopenharmony_ci		return -1;
1728c2ecf20Sopenharmony_ci	if ((OCTEON_IS_MODEL(OCTEON_CN38XX) ||
1738c2ecf20Sopenharmony_ci	     OCTEON_IS_MODEL(OCTEON_CN58XX)) && mode.s.type == 1)
1748c2ecf20Sopenharmony_ci		/* Ignore SPI interfaces */
1758c2ecf20Sopenharmony_ci		return -1;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/* Configure the ASX registers needed to use the RGMII ports */
1788c2ecf20Sopenharmony_ci	asx_tx.u64 = 0;
1798c2ecf20Sopenharmony_ci	asx_tx.s.prt_en = cvmx_build_mask(num_ports);
1808c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(interface), asx_tx.u64);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	asx_rx.u64 = 0;
1838c2ecf20Sopenharmony_ci	asx_rx.s.prt_en = cvmx_build_mask(num_ports);
1848c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface), asx_rx.u64);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* Configure the GMX registers needed to use the RGMII ports */
1878c2ecf20Sopenharmony_ci	for (port = 0; port < num_ports; port++) {
1888c2ecf20Sopenharmony_ci		/* Setting of CVMX_GMXX_TXX_THRESH has been moved to
1898c2ecf20Sopenharmony_ci		   __cvmx_helper_setup_gmx() */
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci		if (cvmx_octeon_is_pass1())
1928c2ecf20Sopenharmony_ci			__cvmx_helper_errata_asx_pass1(interface, port,
1938c2ecf20Sopenharmony_ci						       sys_info_ptr->
1948c2ecf20Sopenharmony_ci						       cpu_clock_hz);
1958c2ecf20Sopenharmony_ci		else {
1968c2ecf20Sopenharmony_ci			/*
1978c2ecf20Sopenharmony_ci			 * Configure more flexible RGMII preamble
1988c2ecf20Sopenharmony_ci			 * checking. Pass 1 doesn't support this
1998c2ecf20Sopenharmony_ci			 * feature.
2008c2ecf20Sopenharmony_ci			 */
2018c2ecf20Sopenharmony_ci			union cvmx_gmxx_rxx_frm_ctl frm_ctl;
2028c2ecf20Sopenharmony_ci			frm_ctl.u64 =
2038c2ecf20Sopenharmony_ci			    cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL
2048c2ecf20Sopenharmony_ci					  (port, interface));
2058c2ecf20Sopenharmony_ci			/* New field, so must be compile time */
2068c2ecf20Sopenharmony_ci			frm_ctl.s.pre_free = 1;
2078c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_GMXX_RXX_FRM_CTL(port, interface),
2088c2ecf20Sopenharmony_ci				       frm_ctl.u64);
2098c2ecf20Sopenharmony_ci		}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci		/*
2128c2ecf20Sopenharmony_ci		 * Each pause frame transmitted will ask for about 10M
2138c2ecf20Sopenharmony_ci		 * bit times before resume.  If buffer space comes
2148c2ecf20Sopenharmony_ci		 * available before that time has expired, an XON
2158c2ecf20Sopenharmony_ci		 * pause frame (0 time) will be transmitted to restart
2168c2ecf20Sopenharmony_ci		 * the flow.
2178c2ecf20Sopenharmony_ci		 */
2188c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_PAUSE_PKT_TIME(port, interface),
2198c2ecf20Sopenharmony_ci			       20000);
2208c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_PAUSE_PKT_INTERVAL
2218c2ecf20Sopenharmony_ci			       (port, interface), 19000);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci		if (OCTEON_IS_MODEL(OCTEON_CN50XX)) {
2248c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(port, interface),
2258c2ecf20Sopenharmony_ci				       16);
2268c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(port, interface),
2278c2ecf20Sopenharmony_ci				       16);
2288c2ecf20Sopenharmony_ci		} else {
2298c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(port, interface),
2308c2ecf20Sopenharmony_ci				       24);
2318c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(port, interface),
2328c2ecf20Sopenharmony_ci				       24);
2338c2ecf20Sopenharmony_ci		}
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	__cvmx_helper_setup_gmx(interface, num_ports);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/* enable the ports now */
2398c2ecf20Sopenharmony_ci	for (port = 0; port < num_ports; port++) {
2408c2ecf20Sopenharmony_ci		union cvmx_gmxx_prtx_cfg gmx_cfg;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci		gmx_cfg.u64 =
2438c2ecf20Sopenharmony_ci		    cvmx_read_csr(CVMX_GMXX_PRTX_CFG(port, interface));
2448c2ecf20Sopenharmony_ci		gmx_cfg.s.en = 1;
2458c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_PRTX_CFG(port, interface),
2468c2ecf20Sopenharmony_ci			       gmx_cfg.u64);
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci	__cvmx_interrupt_asxx_enable(interface);
2498c2ecf20Sopenharmony_ci	__cvmx_interrupt_gmxx_enable(interface);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return 0;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/**
2558c2ecf20Sopenharmony_ci * Return the link state of an IPD/PKO port as returned by
2568c2ecf20Sopenharmony_ci * auto negotiation. The result of this function may not match
2578c2ecf20Sopenharmony_ci * Octeon's link config if auto negotiation has changed since
2588c2ecf20Sopenharmony_ci * the last call to cvmx_helper_link_set().
2598c2ecf20Sopenharmony_ci *
2608c2ecf20Sopenharmony_ci * @ipd_port: IPD/PKO port to query
2618c2ecf20Sopenharmony_ci *
2628c2ecf20Sopenharmony_ci * Returns Link state
2638c2ecf20Sopenharmony_ci */
2648c2ecf20Sopenharmony_ciunion cvmx_helper_link_info __cvmx_helper_rgmii_link_get(int ipd_port)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	int interface = cvmx_helper_get_interface_num(ipd_port);
2678c2ecf20Sopenharmony_ci	int index = cvmx_helper_get_interface_index_num(ipd_port);
2688c2ecf20Sopenharmony_ci	union cvmx_asxx_prt_loop asxx_prt_loop;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	asxx_prt_loop.u64 = cvmx_read_csr(CVMX_ASXX_PRT_LOOP(interface));
2718c2ecf20Sopenharmony_ci	if (asxx_prt_loop.s.int_loop & (1 << index)) {
2728c2ecf20Sopenharmony_ci		/* Force 1Gbps full duplex on internal loopback */
2738c2ecf20Sopenharmony_ci		union cvmx_helper_link_info result;
2748c2ecf20Sopenharmony_ci		result.u64 = 0;
2758c2ecf20Sopenharmony_ci		result.s.full_duplex = 1;
2768c2ecf20Sopenharmony_ci		result.s.link_up = 1;
2778c2ecf20Sopenharmony_ci		result.s.speed = 1000;
2788c2ecf20Sopenharmony_ci		return result;
2798c2ecf20Sopenharmony_ci	} else
2808c2ecf20Sopenharmony_ci		return __cvmx_helper_board_link_get(ipd_port);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci/**
2848c2ecf20Sopenharmony_ci * Configure an IPD/PKO port for the specified link state. This
2858c2ecf20Sopenharmony_ci * function does not influence auto negotiation at the PHY level.
2868c2ecf20Sopenharmony_ci * The passed link state must always match the link state returned
2878c2ecf20Sopenharmony_ci * by cvmx_helper_link_get().
2888c2ecf20Sopenharmony_ci *
2898c2ecf20Sopenharmony_ci * @ipd_port:  IPD/PKO port to configure
2908c2ecf20Sopenharmony_ci * @link_info: The new link state
2918c2ecf20Sopenharmony_ci *
2928c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
2938c2ecf20Sopenharmony_ci */
2948c2ecf20Sopenharmony_ciint __cvmx_helper_rgmii_link_set(int ipd_port,
2958c2ecf20Sopenharmony_ci				 union cvmx_helper_link_info link_info)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	int result = 0;
2988c2ecf20Sopenharmony_ci	int interface = cvmx_helper_get_interface_num(ipd_port);
2998c2ecf20Sopenharmony_ci	int index = cvmx_helper_get_interface_index_num(ipd_port);
3008c2ecf20Sopenharmony_ci	union cvmx_gmxx_prtx_cfg original_gmx_cfg;
3018c2ecf20Sopenharmony_ci	union cvmx_gmxx_prtx_cfg new_gmx_cfg;
3028c2ecf20Sopenharmony_ci	union cvmx_pko_mem_queue_qos pko_mem_queue_qos;
3038c2ecf20Sopenharmony_ci	union cvmx_pko_mem_queue_qos pko_mem_queue_qos_save[16];
3048c2ecf20Sopenharmony_ci	union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp;
3058c2ecf20Sopenharmony_ci	union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp_save;
3068c2ecf20Sopenharmony_ci	int i;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	/* Ignore speed sets in the simulator */
3098c2ecf20Sopenharmony_ci	if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
3108c2ecf20Sopenharmony_ci		return 0;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* Read the current settings so we know the current enable state */
3138c2ecf20Sopenharmony_ci	original_gmx_cfg.u64 =
3148c2ecf20Sopenharmony_ci	    cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
3158c2ecf20Sopenharmony_ci	new_gmx_cfg = original_gmx_cfg;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* Disable the lowest level RX */
3188c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface),
3198c2ecf20Sopenharmony_ci		       cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface)) &
3208c2ecf20Sopenharmony_ci				     ~(1 << index));
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	memset(pko_mem_queue_qos_save, 0, sizeof(pko_mem_queue_qos_save));
3238c2ecf20Sopenharmony_ci	/* Disable all queues so that TX should become idle */
3248c2ecf20Sopenharmony_ci	for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {
3258c2ecf20Sopenharmony_ci		int queue = cvmx_pko_get_base_queue(ipd_port) + i;
3268c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue);
3278c2ecf20Sopenharmony_ci		pko_mem_queue_qos.u64 = cvmx_read_csr(CVMX_PKO_MEM_QUEUE_QOS);
3288c2ecf20Sopenharmony_ci		pko_mem_queue_qos.s.pid = ipd_port;
3298c2ecf20Sopenharmony_ci		pko_mem_queue_qos.s.qid = queue;
3308c2ecf20Sopenharmony_ci		pko_mem_queue_qos_save[i] = pko_mem_queue_qos;
3318c2ecf20Sopenharmony_ci		pko_mem_queue_qos.s.qos_mask = 0;
3328c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_MEM_QUEUE_QOS, pko_mem_queue_qos.u64);
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* Disable backpressure */
3368c2ecf20Sopenharmony_ci	gmx_tx_ovr_bp.u64 = cvmx_read_csr(CVMX_GMXX_TX_OVR_BP(interface));
3378c2ecf20Sopenharmony_ci	gmx_tx_ovr_bp_save = gmx_tx_ovr_bp;
3388c2ecf20Sopenharmony_ci	gmx_tx_ovr_bp.s.bp &= ~(1 << index);
3398c2ecf20Sopenharmony_ci	gmx_tx_ovr_bp.s.en |= 1 << index;
3408c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp.u64);
3418c2ecf20Sopenharmony_ci	cvmx_read_csr(CVMX_GMXX_TX_OVR_BP(interface));
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	/*
3448c2ecf20Sopenharmony_ci	 * Poll the GMX state machine waiting for it to become
3458c2ecf20Sopenharmony_ci	 * idle. Preferably we should only change speed when it is
3468c2ecf20Sopenharmony_ci	 * idle. If it doesn't become idle we will still do the speed
3478c2ecf20Sopenharmony_ci	 * change, but there is a slight chance that GMX will
3488c2ecf20Sopenharmony_ci	 * lockup.
3498c2ecf20Sopenharmony_ci	 */
3508c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_NPI_DBG_SELECT,
3518c2ecf20Sopenharmony_ci		       interface * 0x800 + index * 0x100 + 0x880);
3528c2ecf20Sopenharmony_ci	CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, union cvmx_dbg_data, data & 7,
3538c2ecf20Sopenharmony_ci			==, 0, 10000);
3548c2ecf20Sopenharmony_ci	CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, union cvmx_dbg_data, data & 0xf,
3558c2ecf20Sopenharmony_ci			==, 0, 10000);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* Disable the port before we make any changes */
3588c2ecf20Sopenharmony_ci	new_gmx_cfg.s.en = 0;
3598c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
3608c2ecf20Sopenharmony_ci	cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	/* Set full/half duplex */
3638c2ecf20Sopenharmony_ci	if (cvmx_octeon_is_pass1())
3648c2ecf20Sopenharmony_ci		/* Half duplex is broken for 38XX Pass 1 */
3658c2ecf20Sopenharmony_ci		new_gmx_cfg.s.duplex = 1;
3668c2ecf20Sopenharmony_ci	else if (!link_info.s.link_up)
3678c2ecf20Sopenharmony_ci		/* Force full duplex on down links */
3688c2ecf20Sopenharmony_ci		new_gmx_cfg.s.duplex = 1;
3698c2ecf20Sopenharmony_ci	else
3708c2ecf20Sopenharmony_ci		new_gmx_cfg.s.duplex = link_info.s.full_duplex;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/* Set the link speed. Anything unknown is set to 1Gbps */
3738c2ecf20Sopenharmony_ci	if (link_info.s.speed == 10) {
3748c2ecf20Sopenharmony_ci		new_gmx_cfg.s.slottime = 0;
3758c2ecf20Sopenharmony_ci		new_gmx_cfg.s.speed = 0;
3768c2ecf20Sopenharmony_ci	} else if (link_info.s.speed == 100) {
3778c2ecf20Sopenharmony_ci		new_gmx_cfg.s.slottime = 0;
3788c2ecf20Sopenharmony_ci		new_gmx_cfg.s.speed = 0;
3798c2ecf20Sopenharmony_ci	} else {
3808c2ecf20Sopenharmony_ci		new_gmx_cfg.s.slottime = 1;
3818c2ecf20Sopenharmony_ci		new_gmx_cfg.s.speed = 1;
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* Adjust the clocks */
3858c2ecf20Sopenharmony_ci	if (link_info.s.speed == 10) {
3868c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 50);
3878c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);
3888c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);
3898c2ecf20Sopenharmony_ci	} else if (link_info.s.speed == 100) {
3908c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 5);
3918c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);
3928c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);
3938c2ecf20Sopenharmony_ci	} else {
3948c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 1);
3958c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
3968c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
4008c2ecf20Sopenharmony_ci		if ((link_info.s.speed == 10) || (link_info.s.speed == 100)) {
4018c2ecf20Sopenharmony_ci			union cvmx_gmxx_inf_mode mode;
4028c2ecf20Sopenharmony_ci			mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	/*
4058c2ecf20Sopenharmony_ci	 * Port	 .en  .type  .p0mii  Configuration
4068c2ecf20Sopenharmony_ci	 * ----	 ---  -----  ------  -----------------------------------------
4078c2ecf20Sopenharmony_ci	 *  X	   0	 X	X    All links are disabled.
4088c2ecf20Sopenharmony_ci	 *  0	   1	 X	0    Port 0 is RGMII
4098c2ecf20Sopenharmony_ci	 *  0	   1	 X	1    Port 0 is MII
4108c2ecf20Sopenharmony_ci	 *  1	   1	 0	X    Ports 1 and 2 are configured as RGMII ports.
4118c2ecf20Sopenharmony_ci	 *  1	   1	 1	X    Port 1: GMII/MII; Port 2: disabled. GMII or
4128c2ecf20Sopenharmony_ci	 *			     MII port is selected by GMX_PRT1_CFG[SPEED].
4138c2ecf20Sopenharmony_ci	 */
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci			/* In MII mode, CLK_CNT = 1. */
4168c2ecf20Sopenharmony_ci			if (((index == 0) && (mode.s.p0mii == 1))
4178c2ecf20Sopenharmony_ci			    || ((index != 0) && (mode.s.type == 1))) {
4188c2ecf20Sopenharmony_ci				cvmx_write_csr(CVMX_GMXX_TXX_CLK
4198c2ecf20Sopenharmony_ci					       (index, interface), 1);
4208c2ecf20Sopenharmony_ci			}
4218c2ecf20Sopenharmony_ci		}
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	/* Do a read to make sure all setup stuff is complete */
4258c2ecf20Sopenharmony_ci	cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* Save the new GMX setting without enabling the port */
4288c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	/* Enable the lowest level RX */
4318c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface),
4328c2ecf20Sopenharmony_ci		       cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface)) | (1 <<
4338c2ecf20Sopenharmony_ci									index));
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	/* Re-enable the TX path */
4368c2ecf20Sopenharmony_ci	for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {
4378c2ecf20Sopenharmony_ci		int queue = cvmx_pko_get_base_queue(ipd_port) + i;
4388c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue);
4398c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_MEM_QUEUE_QOS,
4408c2ecf20Sopenharmony_ci			       pko_mem_queue_qos_save[i].u64);
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	/* Restore backpressure */
4448c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp_save.u64);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/* Restore the GMX enable state. Port config is complete */
4478c2ecf20Sopenharmony_ci	new_gmx_cfg.s.en = original_gmx_cfg.s.en;
4488c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	return result;
4518c2ecf20Sopenharmony_ci}
452