162306a36Sopenharmony_ci/***********************license start***************
262306a36Sopenharmony_ci * Author: Cavium Networks
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Contact: support@caviumnetworks.com
562306a36Sopenharmony_ci * This file is part of the OCTEON SDK
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (C) 2003-2018 Cavium, Inc.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This file is free software; you can redistribute it and/or modify
1062306a36Sopenharmony_ci * it under the terms of the GNU General Public License, Version 2, as
1162306a36Sopenharmony_ci * published by the Free Software Foundation.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * This file is distributed in the hope that it will be useful, but
1462306a36Sopenharmony_ci * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
1562306a36Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
1662306a36Sopenharmony_ci * NONINFRINGEMENT.  See the GNU General Public License for more
1762306a36Sopenharmony_ci * details.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
2062306a36Sopenharmony_ci * along with this file; if not, write to the Free Software
2162306a36Sopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2262306a36Sopenharmony_ci * or visit http://www.gnu.org/licenses/.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * This file may also be available under a different license from Cavium.
2562306a36Sopenharmony_ci * Contact Cavium Networks for more information
2662306a36Sopenharmony_ci ***********************license end**************************************/
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * Functions for RGMII/GMII/MII initialization, configuration,
3062306a36Sopenharmony_ci * and monitoring.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci#include <asm/octeon/octeon.h>
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include <asm/octeon/cvmx-config.h>
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#include <asm/octeon/cvmx-pko.h>
3762306a36Sopenharmony_ci#include <asm/octeon/cvmx-helper.h>
3862306a36Sopenharmony_ci#include <asm/octeon/cvmx-helper-board.h>
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#include <asm/octeon/cvmx-npi-defs.h>
4162306a36Sopenharmony_ci#include <asm/octeon/cvmx-gmxx-defs.h>
4262306a36Sopenharmony_ci#include <asm/octeon/cvmx-asxx-defs.h>
4362306a36Sopenharmony_ci#include <asm/octeon/cvmx-dbg-defs.h>
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/*
4662306a36Sopenharmony_ci * Probe RGMII ports and determine the number present
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * @interface: Interface to probe
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * Returns Number of RGMII/GMII/MII ports (0-4).
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_ciint __cvmx_helper_rgmii_probe(int interface)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	int num_ports = 0;
5562306a36Sopenharmony_ci	union cvmx_gmxx_inf_mode mode;
5662306a36Sopenharmony_ci	mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	if (mode.s.type) {
5962306a36Sopenharmony_ci		if (OCTEON_IS_MODEL(OCTEON_CN38XX)
6062306a36Sopenharmony_ci		    || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
6162306a36Sopenharmony_ci			cvmx_dprintf("ERROR: RGMII initialize called in "
6262306a36Sopenharmony_ci				     "SPI interface\n");
6362306a36Sopenharmony_ci		} else if (OCTEON_IS_MODEL(OCTEON_CN31XX)
6462306a36Sopenharmony_ci			   || OCTEON_IS_MODEL(OCTEON_CN30XX)
6562306a36Sopenharmony_ci			   || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
6662306a36Sopenharmony_ci			/*
6762306a36Sopenharmony_ci			 * On these chips "type" says we're in
6862306a36Sopenharmony_ci			 * GMII/MII mode. This limits us to 2 ports
6962306a36Sopenharmony_ci			 */
7062306a36Sopenharmony_ci			num_ports = 2;
7162306a36Sopenharmony_ci		} else {
7262306a36Sopenharmony_ci			cvmx_dprintf("ERROR: Unsupported Octeon model in %s\n",
7362306a36Sopenharmony_ci				     __func__);
7462306a36Sopenharmony_ci		}
7562306a36Sopenharmony_ci	} else {
7662306a36Sopenharmony_ci		if (OCTEON_IS_MODEL(OCTEON_CN38XX)
7762306a36Sopenharmony_ci		    || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
7862306a36Sopenharmony_ci			num_ports = 4;
7962306a36Sopenharmony_ci		} else if (OCTEON_IS_MODEL(OCTEON_CN31XX)
8062306a36Sopenharmony_ci			   || OCTEON_IS_MODEL(OCTEON_CN30XX)
8162306a36Sopenharmony_ci			   || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
8262306a36Sopenharmony_ci			num_ports = 3;
8362306a36Sopenharmony_ci		} else {
8462306a36Sopenharmony_ci			cvmx_dprintf("ERROR: Unsupported Octeon model in %s\n",
8562306a36Sopenharmony_ci				     __func__);
8662306a36Sopenharmony_ci		}
8762306a36Sopenharmony_ci	}
8862306a36Sopenharmony_ci	return num_ports;
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci/*
9262306a36Sopenharmony_ci * Put an RGMII interface in loopback mode. Internal packets sent
9362306a36Sopenharmony_ci * out will be received back again on the same port. Externally
9462306a36Sopenharmony_ci * received packets will echo back out.
9562306a36Sopenharmony_ci *
9662306a36Sopenharmony_ci * @port:   IPD port number to loop.
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_civoid cvmx_helper_rgmii_internal_loopback(int port)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	int interface = (port >> 4) & 1;
10162306a36Sopenharmony_ci	int index = port & 0xf;
10262306a36Sopenharmony_ci	uint64_t tmp;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	union cvmx_gmxx_prtx_cfg gmx_cfg;
10562306a36Sopenharmony_ci	gmx_cfg.u64 = 0;
10662306a36Sopenharmony_ci	gmx_cfg.s.duplex = 1;
10762306a36Sopenharmony_ci	gmx_cfg.s.slottime = 1;
10862306a36Sopenharmony_ci	gmx_cfg.s.speed = 1;
10962306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 1);
11062306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
11162306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
11262306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
11362306a36Sopenharmony_ci	tmp = cvmx_read_csr(CVMX_ASXX_PRT_LOOP(interface));
11462306a36Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_PRT_LOOP(interface), (1 << index) | tmp);
11562306a36Sopenharmony_ci	tmp = cvmx_read_csr(CVMX_ASXX_TX_PRT_EN(interface));
11662306a36Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(interface), (1 << index) | tmp);
11762306a36Sopenharmony_ci	tmp = cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface));
11862306a36Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface), (1 << index) | tmp);
11962306a36Sopenharmony_ci	gmx_cfg.s.en = 1;
12062306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci/*
12462306a36Sopenharmony_ci * Workaround ASX setup errata with CN38XX pass1
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci * @interface: Interface to setup
12762306a36Sopenharmony_ci * @port:      Port to setup (0..3)
12862306a36Sopenharmony_ci * @cpu_clock_hz:
12962306a36Sopenharmony_ci *		    Chip frequency in Hertz
13062306a36Sopenharmony_ci *
13162306a36Sopenharmony_ci * Returns Zero on success, negative on failure
13262306a36Sopenharmony_ci */
13362306a36Sopenharmony_cistatic int __cvmx_helper_errata_asx_pass1(int interface, int port,
13462306a36Sopenharmony_ci					  int cpu_clock_hz)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	/* Set hi water mark as per errata GMX-4 */
13762306a36Sopenharmony_ci	if (cpu_clock_hz >= 325000000 && cpu_clock_hz < 375000000)
13862306a36Sopenharmony_ci		cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 12);
13962306a36Sopenharmony_ci	else if (cpu_clock_hz >= 375000000 && cpu_clock_hz < 437000000)
14062306a36Sopenharmony_ci		cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 11);
14162306a36Sopenharmony_ci	else if (cpu_clock_hz >= 437000000 && cpu_clock_hz < 550000000)
14262306a36Sopenharmony_ci		cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 10);
14362306a36Sopenharmony_ci	else if (cpu_clock_hz >= 550000000 && cpu_clock_hz < 687000000)
14462306a36Sopenharmony_ci		cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 9);
14562306a36Sopenharmony_ci	else
14662306a36Sopenharmony_ci		cvmx_dprintf("Illegal clock frequency (%d). "
14762306a36Sopenharmony_ci			"CVMX_ASXX_TX_HI_WATERX not set\n", cpu_clock_hz);
14862306a36Sopenharmony_ci	return 0;
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci/*
15262306a36Sopenharmony_ci * Configure all of the ASX, GMX, and PKO registers required
15362306a36Sopenharmony_ci * to get RGMII to function on the supplied interface.
15462306a36Sopenharmony_ci *
15562306a36Sopenharmony_ci * @interface: PKO Interface to configure (0 or 1)
15662306a36Sopenharmony_ci *
15762306a36Sopenharmony_ci * Returns Zero on success
15862306a36Sopenharmony_ci */
15962306a36Sopenharmony_ciint __cvmx_helper_rgmii_enable(int interface)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	int num_ports = cvmx_helper_ports_on_interface(interface);
16262306a36Sopenharmony_ci	int port;
16362306a36Sopenharmony_ci	struct cvmx_sysinfo *sys_info_ptr = cvmx_sysinfo_get();
16462306a36Sopenharmony_ci	union cvmx_gmxx_inf_mode mode;
16562306a36Sopenharmony_ci	union cvmx_asxx_tx_prt_en asx_tx;
16662306a36Sopenharmony_ci	union cvmx_asxx_rx_prt_en asx_rx;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	if (mode.s.en == 0)
17162306a36Sopenharmony_ci		return -1;
17262306a36Sopenharmony_ci	if ((OCTEON_IS_MODEL(OCTEON_CN38XX) ||
17362306a36Sopenharmony_ci	     OCTEON_IS_MODEL(OCTEON_CN58XX)) && mode.s.type == 1)
17462306a36Sopenharmony_ci		/* Ignore SPI interfaces */
17562306a36Sopenharmony_ci		return -1;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	/* Configure the ASX registers needed to use the RGMII ports */
17862306a36Sopenharmony_ci	asx_tx.u64 = 0;
17962306a36Sopenharmony_ci	asx_tx.s.prt_en = cvmx_build_mask(num_ports);
18062306a36Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(interface), asx_tx.u64);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	asx_rx.u64 = 0;
18362306a36Sopenharmony_ci	asx_rx.s.prt_en = cvmx_build_mask(num_ports);
18462306a36Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface), asx_rx.u64);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	/* Configure the GMX registers needed to use the RGMII ports */
18762306a36Sopenharmony_ci	for (port = 0; port < num_ports; port++) {
18862306a36Sopenharmony_ci		/* Setting of CVMX_GMXX_TXX_THRESH has been moved to
18962306a36Sopenharmony_ci		   __cvmx_helper_setup_gmx() */
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci		if (cvmx_octeon_is_pass1())
19262306a36Sopenharmony_ci			__cvmx_helper_errata_asx_pass1(interface, port,
19362306a36Sopenharmony_ci						       sys_info_ptr->
19462306a36Sopenharmony_ci						       cpu_clock_hz);
19562306a36Sopenharmony_ci		else {
19662306a36Sopenharmony_ci			/*
19762306a36Sopenharmony_ci			 * Configure more flexible RGMII preamble
19862306a36Sopenharmony_ci			 * checking. Pass 1 doesn't support this
19962306a36Sopenharmony_ci			 * feature.
20062306a36Sopenharmony_ci			 */
20162306a36Sopenharmony_ci			union cvmx_gmxx_rxx_frm_ctl frm_ctl;
20262306a36Sopenharmony_ci			frm_ctl.u64 =
20362306a36Sopenharmony_ci			    cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL
20462306a36Sopenharmony_ci					  (port, interface));
20562306a36Sopenharmony_ci			/* New field, so must be compile time */
20662306a36Sopenharmony_ci			frm_ctl.s.pre_free = 1;
20762306a36Sopenharmony_ci			cvmx_write_csr(CVMX_GMXX_RXX_FRM_CTL(port, interface),
20862306a36Sopenharmony_ci				       frm_ctl.u64);
20962306a36Sopenharmony_ci		}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci		/*
21262306a36Sopenharmony_ci		 * Each pause frame transmitted will ask for about 10M
21362306a36Sopenharmony_ci		 * bit times before resume.  If buffer space comes
21462306a36Sopenharmony_ci		 * available before that time has expired, an XON
21562306a36Sopenharmony_ci		 * pause frame (0 time) will be transmitted to restart
21662306a36Sopenharmony_ci		 * the flow.
21762306a36Sopenharmony_ci		 */
21862306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_PAUSE_PKT_TIME(port, interface),
21962306a36Sopenharmony_ci			       20000);
22062306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_PAUSE_PKT_INTERVAL
22162306a36Sopenharmony_ci			       (port, interface), 19000);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci		if (OCTEON_IS_MODEL(OCTEON_CN50XX)) {
22462306a36Sopenharmony_ci			cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(port, interface),
22562306a36Sopenharmony_ci				       16);
22662306a36Sopenharmony_ci			cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(port, interface),
22762306a36Sopenharmony_ci				       16);
22862306a36Sopenharmony_ci		} else {
22962306a36Sopenharmony_ci			cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(port, interface),
23062306a36Sopenharmony_ci				       24);
23162306a36Sopenharmony_ci			cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(port, interface),
23262306a36Sopenharmony_ci				       24);
23362306a36Sopenharmony_ci		}
23462306a36Sopenharmony_ci	}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	__cvmx_helper_setup_gmx(interface, num_ports);
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	/* enable the ports now */
23962306a36Sopenharmony_ci	for (port = 0; port < num_ports; port++) {
24062306a36Sopenharmony_ci		union cvmx_gmxx_prtx_cfg gmx_cfg;
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci		gmx_cfg.u64 =
24362306a36Sopenharmony_ci		    cvmx_read_csr(CVMX_GMXX_PRTX_CFG(port, interface));
24462306a36Sopenharmony_ci		gmx_cfg.s.en = 1;
24562306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_PRTX_CFG(port, interface),
24662306a36Sopenharmony_ci			       gmx_cfg.u64);
24762306a36Sopenharmony_ci	}
24862306a36Sopenharmony_ci	__cvmx_interrupt_asxx_enable(interface);
24962306a36Sopenharmony_ci	__cvmx_interrupt_gmxx_enable(interface);
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	return 0;
25262306a36Sopenharmony_ci}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci/*
25562306a36Sopenharmony_ci * Return the link state of an IPD/PKO port as returned by
25662306a36Sopenharmony_ci * auto negotiation. The result of this function may not match
25762306a36Sopenharmony_ci * Octeon's link config if auto negotiation has changed since
25862306a36Sopenharmony_ci * the last call to cvmx_helper_link_set().
25962306a36Sopenharmony_ci *
26062306a36Sopenharmony_ci * @ipd_port: IPD/PKO port to query
26162306a36Sopenharmony_ci *
26262306a36Sopenharmony_ci * Returns Link state
26362306a36Sopenharmony_ci */
26462306a36Sopenharmony_ciunion cvmx_helper_link_info __cvmx_helper_rgmii_link_get(int ipd_port)
26562306a36Sopenharmony_ci{
26662306a36Sopenharmony_ci	int interface = cvmx_helper_get_interface_num(ipd_port);
26762306a36Sopenharmony_ci	int index = cvmx_helper_get_interface_index_num(ipd_port);
26862306a36Sopenharmony_ci	union cvmx_asxx_prt_loop asxx_prt_loop;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	asxx_prt_loop.u64 = cvmx_read_csr(CVMX_ASXX_PRT_LOOP(interface));
27162306a36Sopenharmony_ci	if (asxx_prt_loop.s.int_loop & (1 << index)) {
27262306a36Sopenharmony_ci		/* Force 1Gbps full duplex on internal loopback */
27362306a36Sopenharmony_ci		union cvmx_helper_link_info result;
27462306a36Sopenharmony_ci		result.u64 = 0;
27562306a36Sopenharmony_ci		result.s.full_duplex = 1;
27662306a36Sopenharmony_ci		result.s.link_up = 1;
27762306a36Sopenharmony_ci		result.s.speed = 1000;
27862306a36Sopenharmony_ci		return result;
27962306a36Sopenharmony_ci	} else
28062306a36Sopenharmony_ci		return __cvmx_helper_board_link_get(ipd_port);
28162306a36Sopenharmony_ci}
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci/*
28462306a36Sopenharmony_ci * Configure an IPD/PKO port for the specified link state. This
28562306a36Sopenharmony_ci * function does not influence auto negotiation at the PHY level.
28662306a36Sopenharmony_ci * The passed link state must always match the link state returned
28762306a36Sopenharmony_ci * by cvmx_helper_link_get().
28862306a36Sopenharmony_ci *
28962306a36Sopenharmony_ci * @ipd_port:  IPD/PKO port to configure
29062306a36Sopenharmony_ci * @link_info: The new link state
29162306a36Sopenharmony_ci *
29262306a36Sopenharmony_ci * Returns Zero on success, negative on failure
29362306a36Sopenharmony_ci */
29462306a36Sopenharmony_ciint __cvmx_helper_rgmii_link_set(int ipd_port,
29562306a36Sopenharmony_ci				 union cvmx_helper_link_info link_info)
29662306a36Sopenharmony_ci{
29762306a36Sopenharmony_ci	int result = 0;
29862306a36Sopenharmony_ci	int interface = cvmx_helper_get_interface_num(ipd_port);
29962306a36Sopenharmony_ci	int index = cvmx_helper_get_interface_index_num(ipd_port);
30062306a36Sopenharmony_ci	union cvmx_gmxx_prtx_cfg original_gmx_cfg;
30162306a36Sopenharmony_ci	union cvmx_gmxx_prtx_cfg new_gmx_cfg;
30262306a36Sopenharmony_ci	union cvmx_pko_mem_queue_qos pko_mem_queue_qos;
30362306a36Sopenharmony_ci	union cvmx_pko_mem_queue_qos pko_mem_queue_qos_save[16];
30462306a36Sopenharmony_ci	union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp;
30562306a36Sopenharmony_ci	union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp_save;
30662306a36Sopenharmony_ci	int i;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	/* Ignore speed sets in the simulator */
30962306a36Sopenharmony_ci	if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
31062306a36Sopenharmony_ci		return 0;
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	/* Read the current settings so we know the current enable state */
31362306a36Sopenharmony_ci	original_gmx_cfg.u64 =
31462306a36Sopenharmony_ci	    cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
31562306a36Sopenharmony_ci	new_gmx_cfg = original_gmx_cfg;
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	/* Disable the lowest level RX */
31862306a36Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface),
31962306a36Sopenharmony_ci		       cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface)) &
32062306a36Sopenharmony_ci				     ~(1 << index));
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	memset(pko_mem_queue_qos_save, 0, sizeof(pko_mem_queue_qos_save));
32362306a36Sopenharmony_ci	/* Disable all queues so that TX should become idle */
32462306a36Sopenharmony_ci	for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {
32562306a36Sopenharmony_ci		int queue = cvmx_pko_get_base_queue(ipd_port) + i;
32662306a36Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue);
32762306a36Sopenharmony_ci		pko_mem_queue_qos.u64 = cvmx_read_csr(CVMX_PKO_MEM_QUEUE_QOS);
32862306a36Sopenharmony_ci		pko_mem_queue_qos.s.pid = ipd_port;
32962306a36Sopenharmony_ci		pko_mem_queue_qos.s.qid = queue;
33062306a36Sopenharmony_ci		pko_mem_queue_qos_save[i] = pko_mem_queue_qos;
33162306a36Sopenharmony_ci		pko_mem_queue_qos.s.qos_mask = 0;
33262306a36Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_MEM_QUEUE_QOS, pko_mem_queue_qos.u64);
33362306a36Sopenharmony_ci	}
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci	/* Disable backpressure */
33662306a36Sopenharmony_ci	gmx_tx_ovr_bp.u64 = cvmx_read_csr(CVMX_GMXX_TX_OVR_BP(interface));
33762306a36Sopenharmony_ci	gmx_tx_ovr_bp_save = gmx_tx_ovr_bp;
33862306a36Sopenharmony_ci	gmx_tx_ovr_bp.s.bp &= ~(1 << index);
33962306a36Sopenharmony_ci	gmx_tx_ovr_bp.s.en |= 1 << index;
34062306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp.u64);
34162306a36Sopenharmony_ci	cvmx_read_csr(CVMX_GMXX_TX_OVR_BP(interface));
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	/*
34462306a36Sopenharmony_ci	 * Poll the GMX state machine waiting for it to become
34562306a36Sopenharmony_ci	 * idle. Preferably we should only change speed when it is
34662306a36Sopenharmony_ci	 * idle. If it doesn't become idle we will still do the speed
34762306a36Sopenharmony_ci	 * change, but there is a slight chance that GMX will
34862306a36Sopenharmony_ci	 * lockup.
34962306a36Sopenharmony_ci	 */
35062306a36Sopenharmony_ci	cvmx_write_csr(CVMX_NPI_DBG_SELECT,
35162306a36Sopenharmony_ci		       interface * 0x800 + index * 0x100 + 0x880);
35262306a36Sopenharmony_ci	CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, union cvmx_dbg_data, data & 7,
35362306a36Sopenharmony_ci			==, 0, 10000);
35462306a36Sopenharmony_ci	CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, union cvmx_dbg_data, data & 0xf,
35562306a36Sopenharmony_ci			==, 0, 10000);
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	/* Disable the port before we make any changes */
35862306a36Sopenharmony_ci	new_gmx_cfg.s.en = 0;
35962306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
36062306a36Sopenharmony_ci	cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	/* Set full/half duplex */
36362306a36Sopenharmony_ci	if (cvmx_octeon_is_pass1())
36462306a36Sopenharmony_ci		/* Half duplex is broken for 38XX Pass 1 */
36562306a36Sopenharmony_ci		new_gmx_cfg.s.duplex = 1;
36662306a36Sopenharmony_ci	else if (!link_info.s.link_up)
36762306a36Sopenharmony_ci		/* Force full duplex on down links */
36862306a36Sopenharmony_ci		new_gmx_cfg.s.duplex = 1;
36962306a36Sopenharmony_ci	else
37062306a36Sopenharmony_ci		new_gmx_cfg.s.duplex = link_info.s.full_duplex;
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	/* Set the link speed. Anything unknown is set to 1Gbps */
37362306a36Sopenharmony_ci	if (link_info.s.speed == 10) {
37462306a36Sopenharmony_ci		new_gmx_cfg.s.slottime = 0;
37562306a36Sopenharmony_ci		new_gmx_cfg.s.speed = 0;
37662306a36Sopenharmony_ci	} else if (link_info.s.speed == 100) {
37762306a36Sopenharmony_ci		new_gmx_cfg.s.slottime = 0;
37862306a36Sopenharmony_ci		new_gmx_cfg.s.speed = 0;
37962306a36Sopenharmony_ci	} else {
38062306a36Sopenharmony_ci		new_gmx_cfg.s.slottime = 1;
38162306a36Sopenharmony_ci		new_gmx_cfg.s.speed = 1;
38262306a36Sopenharmony_ci	}
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	/* Adjust the clocks */
38562306a36Sopenharmony_ci	if (link_info.s.speed == 10) {
38662306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 50);
38762306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);
38862306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);
38962306a36Sopenharmony_ci	} else if (link_info.s.speed == 100) {
39062306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 5);
39162306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);
39262306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);
39362306a36Sopenharmony_ci	} else {
39462306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 1);
39562306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
39662306a36Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
39762306a36Sopenharmony_ci	}
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
40062306a36Sopenharmony_ci		if ((link_info.s.speed == 10) || (link_info.s.speed == 100)) {
40162306a36Sopenharmony_ci			union cvmx_gmxx_inf_mode mode;
40262306a36Sopenharmony_ci			mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	/*
40562306a36Sopenharmony_ci	 * Port	 .en  .type  .p0mii  Configuration
40662306a36Sopenharmony_ci	 * ----	 ---  -----  ------  -----------------------------------------
40762306a36Sopenharmony_ci	 *  X	   0	 X	X    All links are disabled.
40862306a36Sopenharmony_ci	 *  0	   1	 X	0    Port 0 is RGMII
40962306a36Sopenharmony_ci	 *  0	   1	 X	1    Port 0 is MII
41062306a36Sopenharmony_ci	 *  1	   1	 0	X    Ports 1 and 2 are configured as RGMII ports.
41162306a36Sopenharmony_ci	 *  1	   1	 1	X    Port 1: GMII/MII; Port 2: disabled. GMII or
41262306a36Sopenharmony_ci	 *			     MII port is selected by GMX_PRT1_CFG[SPEED].
41362306a36Sopenharmony_ci	 */
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci			/* In MII mode, CLK_CNT = 1. */
41662306a36Sopenharmony_ci			if (((index == 0) && (mode.s.p0mii == 1))
41762306a36Sopenharmony_ci			    || ((index != 0) && (mode.s.type == 1))) {
41862306a36Sopenharmony_ci				cvmx_write_csr(CVMX_GMXX_TXX_CLK
41962306a36Sopenharmony_ci					       (index, interface), 1);
42062306a36Sopenharmony_ci			}
42162306a36Sopenharmony_ci		}
42262306a36Sopenharmony_ci	}
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci	/* Do a read to make sure all setup stuff is complete */
42562306a36Sopenharmony_ci	cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	/* Save the new GMX setting without enabling the port */
42862306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci	/* Enable the lowest level RX */
43162306a36Sopenharmony_ci	cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface),
43262306a36Sopenharmony_ci		       cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface)) | (1 <<
43362306a36Sopenharmony_ci									index));
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci	/* Re-enable the TX path */
43662306a36Sopenharmony_ci	for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {
43762306a36Sopenharmony_ci		int queue = cvmx_pko_get_base_queue(ipd_port) + i;
43862306a36Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue);
43962306a36Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_MEM_QUEUE_QOS,
44062306a36Sopenharmony_ci			       pko_mem_queue_qos_save[i].u64);
44162306a36Sopenharmony_ci	}
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	/* Restore backpressure */
44462306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp_save.u64);
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	/* Restore the GMX enable state. Port config is complete */
44762306a36Sopenharmony_ci	new_gmx_cfg.s.en = original_gmx_cfg.s.en;
44862306a36Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	return result;
45162306a36Sopenharmony_ci}
452