Path: blob/master/arch/mips/cavium-octeon/executive/cvmx-helper-rgmii.c
26481 views
/***********************license start***************1* Author: Cavium Networks2*3* Contact: [email protected]4* This file is part of the OCTEON SDK5*6* Copyright (C) 2003-2018 Cavium, Inc.7*8* This file is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License, Version 2, as10* published by the Free Software Foundation.11*12* This file is distributed in the hope that it will be useful, but13* AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty14* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or15* NONINFRINGEMENT. See the GNU General Public License for more16* details.17*18* You should have received a copy of the GNU General Public License19* along with this file; if not, write to the Free Software20* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA21* or visit http://www.gnu.org/licenses/.22*23* This file may also be available under a different license from Cavium.24* Contact Cavium Networks for more information25***********************license end**************************************/2627/*28* Functions for RGMII/GMII/MII initialization, configuration,29* and monitoring.30*/31#include <asm/octeon/octeon.h>3233#include <asm/octeon/cvmx-config.h>3435#include <asm/octeon/cvmx-pko.h>36#include <asm/octeon/cvmx-helper.h>37#include <asm/octeon/cvmx-helper-board.h>3839#include <asm/octeon/cvmx-npi-defs.h>40#include <asm/octeon/cvmx-gmxx-defs.h>41#include <asm/octeon/cvmx-asxx-defs.h>42#include <asm/octeon/cvmx-dbg-defs.h>4344/*45* Probe RGMII ports and determine the number present46*47* @interface: Interface to probe48*49* Returns Number of RGMII/GMII/MII ports (0-4).50*/51int __cvmx_helper_rgmii_probe(int interface)52{53int num_ports = 0;54union cvmx_gmxx_inf_mode mode;55mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));5657if (mode.s.type) {58if (OCTEON_IS_MODEL(OCTEON_CN38XX)59|| OCTEON_IS_MODEL(OCTEON_CN58XX)) {60cvmx_dprintf("ERROR: RGMII initialize called in "61"SPI interface\n");62} else if (OCTEON_IS_MODEL(OCTEON_CN31XX)63|| OCTEON_IS_MODEL(OCTEON_CN30XX)64|| OCTEON_IS_MODEL(OCTEON_CN50XX)) {65/*66* On these chips "type" says we're in67* GMII/MII mode. This limits us to 2 ports68*/69num_ports = 2;70} else {71cvmx_dprintf("ERROR: Unsupported Octeon model in %s\n",72__func__);73}74} else {75if (OCTEON_IS_MODEL(OCTEON_CN38XX)76|| OCTEON_IS_MODEL(OCTEON_CN58XX)) {77num_ports = 4;78} else if (OCTEON_IS_MODEL(OCTEON_CN31XX)79|| OCTEON_IS_MODEL(OCTEON_CN30XX)80|| OCTEON_IS_MODEL(OCTEON_CN50XX)) {81num_ports = 3;82} else {83cvmx_dprintf("ERROR: Unsupported Octeon model in %s\n",84__func__);85}86}87return num_ports;88}8990/*91* Put an RGMII interface in loopback mode. Internal packets sent92* out will be received back again on the same port. Externally93* received packets will echo back out.94*95* @port: IPD port number to loop.96*/97void cvmx_helper_rgmii_internal_loopback(int port)98{99int interface = (port >> 4) & 1;100int index = port & 0xf;101uint64_t tmp;102103union cvmx_gmxx_prtx_cfg gmx_cfg;104gmx_cfg.u64 = 0;105gmx_cfg.s.duplex = 1;106gmx_cfg.s.slottime = 1;107gmx_cfg.s.speed = 1;108cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 1);109cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);110cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);111cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);112tmp = cvmx_read_csr(CVMX_ASXX_PRT_LOOP(interface));113cvmx_write_csr(CVMX_ASXX_PRT_LOOP(interface), (1 << index) | tmp);114tmp = cvmx_read_csr(CVMX_ASXX_TX_PRT_EN(interface));115cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(interface), (1 << index) | tmp);116tmp = cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface));117cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface), (1 << index) | tmp);118gmx_cfg.s.en = 1;119cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);120}121122/*123* Workaround ASX setup errata with CN38XX pass1124*125* @interface: Interface to setup126* @port: Port to setup (0..3)127* @cpu_clock_hz:128* Chip frequency in Hertz129*130* Returns Zero on success, negative on failure131*/132static int __cvmx_helper_errata_asx_pass1(int interface, int port,133int cpu_clock_hz)134{135/* Set hi water mark as per errata GMX-4 */136if (cpu_clock_hz >= 325000000 && cpu_clock_hz < 375000000)137cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 12);138else if (cpu_clock_hz >= 375000000 && cpu_clock_hz < 437000000)139cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 11);140else if (cpu_clock_hz >= 437000000 && cpu_clock_hz < 550000000)141cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 10);142else if (cpu_clock_hz >= 550000000 && cpu_clock_hz < 687000000)143cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 9);144else145cvmx_dprintf("Illegal clock frequency (%d). "146"CVMX_ASXX_TX_HI_WATERX not set\n", cpu_clock_hz);147return 0;148}149150/*151* Configure all of the ASX, GMX, and PKO registers required152* to get RGMII to function on the supplied interface.153*154* @interface: PKO Interface to configure (0 or 1)155*156* Returns Zero on success157*/158int __cvmx_helper_rgmii_enable(int interface)159{160int num_ports = cvmx_helper_ports_on_interface(interface);161int port;162struct cvmx_sysinfo *sys_info_ptr = cvmx_sysinfo_get();163union cvmx_gmxx_inf_mode mode;164union cvmx_asxx_tx_prt_en asx_tx;165union cvmx_asxx_rx_prt_en asx_rx;166167mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));168169if (mode.s.en == 0)170return -1;171if ((OCTEON_IS_MODEL(OCTEON_CN38XX) ||172OCTEON_IS_MODEL(OCTEON_CN58XX)) && mode.s.type == 1)173/* Ignore SPI interfaces */174return -1;175176/* Configure the ASX registers needed to use the RGMII ports */177asx_tx.u64 = 0;178asx_tx.s.prt_en = cvmx_build_mask(num_ports);179cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(interface), asx_tx.u64);180181asx_rx.u64 = 0;182asx_rx.s.prt_en = cvmx_build_mask(num_ports);183cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface), asx_rx.u64);184185/* Configure the GMX registers needed to use the RGMII ports */186for (port = 0; port < num_ports; port++) {187/* Setting of CVMX_GMXX_TXX_THRESH has been moved to188__cvmx_helper_setup_gmx() */189190if (cvmx_octeon_is_pass1())191__cvmx_helper_errata_asx_pass1(interface, port,192sys_info_ptr->193cpu_clock_hz);194else {195/*196* Configure more flexible RGMII preamble197* checking. Pass 1 doesn't support this198* feature.199*/200union cvmx_gmxx_rxx_frm_ctl frm_ctl;201frm_ctl.u64 =202cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL203(port, interface));204/* New field, so must be compile time */205frm_ctl.s.pre_free = 1;206cvmx_write_csr(CVMX_GMXX_RXX_FRM_CTL(port, interface),207frm_ctl.u64);208}209210/*211* Each pause frame transmitted will ask for about 10M212* bit times before resume. If buffer space comes213* available before that time has expired, an XON214* pause frame (0 time) will be transmitted to restart215* the flow.216*/217cvmx_write_csr(CVMX_GMXX_TXX_PAUSE_PKT_TIME(port, interface),21820000);219cvmx_write_csr(CVMX_GMXX_TXX_PAUSE_PKT_INTERVAL220(port, interface), 19000);221222if (OCTEON_IS_MODEL(OCTEON_CN50XX)) {223cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(port, interface),22416);225cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(port, interface),22616);227} else {228cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(port, interface),22924);230cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(port, interface),23124);232}233}234235__cvmx_helper_setup_gmx(interface, num_ports);236237/* enable the ports now */238for (port = 0; port < num_ports; port++) {239union cvmx_gmxx_prtx_cfg gmx_cfg;240241gmx_cfg.u64 =242cvmx_read_csr(CVMX_GMXX_PRTX_CFG(port, interface));243gmx_cfg.s.en = 1;244cvmx_write_csr(CVMX_GMXX_PRTX_CFG(port, interface),245gmx_cfg.u64);246}247__cvmx_interrupt_asxx_enable(interface);248__cvmx_interrupt_gmxx_enable(interface);249250return 0;251}252253/*254* Return the link state of an IPD/PKO port as returned by255* auto negotiation. The result of this function may not match256* Octeon's link config if auto negotiation has changed since257* the last call to cvmx_helper_link_set().258*259* @ipd_port: IPD/PKO port to query260*261* Returns Link state262*/263union cvmx_helper_link_info __cvmx_helper_rgmii_link_get(int ipd_port)264{265int interface = cvmx_helper_get_interface_num(ipd_port);266int index = cvmx_helper_get_interface_index_num(ipd_port);267union cvmx_asxx_prt_loop asxx_prt_loop;268269asxx_prt_loop.u64 = cvmx_read_csr(CVMX_ASXX_PRT_LOOP(interface));270if (asxx_prt_loop.s.int_loop & (1 << index)) {271/* Force 1Gbps full duplex on internal loopback */272union cvmx_helper_link_info result;273result.u64 = 0;274result.s.full_duplex = 1;275result.s.link_up = 1;276result.s.speed = 1000;277return result;278} else279return __cvmx_helper_board_link_get(ipd_port);280}281282/*283* Configure an IPD/PKO port for the specified link state. This284* function does not influence auto negotiation at the PHY level.285* The passed link state must always match the link state returned286* by cvmx_helper_link_get().287*288* @ipd_port: IPD/PKO port to configure289* @link_info: The new link state290*291* Returns Zero on success, negative on failure292*/293int __cvmx_helper_rgmii_link_set(int ipd_port,294union cvmx_helper_link_info link_info)295{296int result = 0;297int interface = cvmx_helper_get_interface_num(ipd_port);298int index = cvmx_helper_get_interface_index_num(ipd_port);299union cvmx_gmxx_prtx_cfg original_gmx_cfg;300union cvmx_gmxx_prtx_cfg new_gmx_cfg;301union cvmx_pko_mem_queue_qos pko_mem_queue_qos;302union cvmx_pko_mem_queue_qos pko_mem_queue_qos_save[16];303union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp;304union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp_save;305int i;306307/* Ignore speed sets in the simulator */308if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)309return 0;310311/* Read the current settings so we know the current enable state */312original_gmx_cfg.u64 =313cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));314new_gmx_cfg = original_gmx_cfg;315316/* Disable the lowest level RX */317cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface),318cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface)) &319~(1 << index));320321memset(pko_mem_queue_qos_save, 0, sizeof(pko_mem_queue_qos_save));322/* Disable all queues so that TX should become idle */323for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {324int queue = cvmx_pko_get_base_queue(ipd_port) + i;325cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue);326pko_mem_queue_qos.u64 = cvmx_read_csr(CVMX_PKO_MEM_QUEUE_QOS);327pko_mem_queue_qos.s.pid = ipd_port;328pko_mem_queue_qos.s.qid = queue;329pko_mem_queue_qos_save[i] = pko_mem_queue_qos;330pko_mem_queue_qos.s.qos_mask = 0;331cvmx_write_csr(CVMX_PKO_MEM_QUEUE_QOS, pko_mem_queue_qos.u64);332}333334/* Disable backpressure */335gmx_tx_ovr_bp.u64 = cvmx_read_csr(CVMX_GMXX_TX_OVR_BP(interface));336gmx_tx_ovr_bp_save = gmx_tx_ovr_bp;337gmx_tx_ovr_bp.s.bp &= ~(1 << index);338gmx_tx_ovr_bp.s.en |= 1 << index;339cvmx_write_csr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp.u64);340cvmx_read_csr(CVMX_GMXX_TX_OVR_BP(interface));341342/*343* Poll the GMX state machine waiting for it to become344* idle. Preferably we should only change speed when it is345* idle. If it doesn't become idle we will still do the speed346* change, but there is a slight chance that GMX will347* lockup.348*/349cvmx_write_csr(CVMX_NPI_DBG_SELECT,350interface * 0x800 + index * 0x100 + 0x880);351CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, union cvmx_dbg_data, data & 7,352==, 0, 10000);353CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, union cvmx_dbg_data, data & 0xf,354==, 0, 10000);355356/* Disable the port before we make any changes */357new_gmx_cfg.s.en = 0;358cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);359cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));360361/* Set full/half duplex */362if (cvmx_octeon_is_pass1())363/* Half duplex is broken for 38XX Pass 1 */364new_gmx_cfg.s.duplex = 1;365else if (!link_info.s.link_up)366/* Force full duplex on down links */367new_gmx_cfg.s.duplex = 1;368else369new_gmx_cfg.s.duplex = link_info.s.full_duplex;370371/* Set the link speed. Anything unknown is set to 1Gbps */372if (link_info.s.speed == 10) {373new_gmx_cfg.s.slottime = 0;374new_gmx_cfg.s.speed = 0;375} else if (link_info.s.speed == 100) {376new_gmx_cfg.s.slottime = 0;377new_gmx_cfg.s.speed = 0;378} else {379new_gmx_cfg.s.slottime = 1;380new_gmx_cfg.s.speed = 1;381}382383/* Adjust the clocks */384if (link_info.s.speed == 10) {385cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 50);386cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);387cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);388} else if (link_info.s.speed == 100) {389cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 5);390cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);391cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);392} else {393cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 1);394cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);395cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);396}397398if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN50XX)) {399if ((link_info.s.speed == 10) || (link_info.s.speed == 100)) {400union cvmx_gmxx_inf_mode mode;401mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));402403/*404* Port .en .type .p0mii Configuration405* ---- --- ----- ------ -----------------------------------------406* X 0 X X All links are disabled.407* 0 1 X 0 Port 0 is RGMII408* 0 1 X 1 Port 0 is MII409* 1 1 0 X Ports 1 and 2 are configured as RGMII ports.410* 1 1 1 X Port 1: GMII/MII; Port 2: disabled. GMII or411* MII port is selected by GMX_PRT1_CFG[SPEED].412*/413414/* In MII mode, CLK_CNT = 1. */415if (((index == 0) && (mode.s.p0mii == 1))416|| ((index != 0) && (mode.s.type == 1))) {417cvmx_write_csr(CVMX_GMXX_TXX_CLK418(index, interface), 1);419}420}421}422423/* Do a read to make sure all setup stuff is complete */424cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));425426/* Save the new GMX setting without enabling the port */427cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);428429/* Enable the lowest level RX */430cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface),431cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface)) | (1 <<432index));433434/* Re-enable the TX path */435for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {436int queue = cvmx_pko_get_base_queue(ipd_port) + i;437cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue);438cvmx_write_csr(CVMX_PKO_MEM_QUEUE_QOS,439pko_mem_queue_qos_save[i].u64);440}441442/* Restore backpressure */443cvmx_write_csr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp_save.u64);444445/* Restore the GMX enable state. Port config is complete */446new_gmx_cfg.s.en = original_gmx_cfg.s.en;447cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);448449return result;450}451452453