Path: blob/master/arch/mips/cavium-octeon/executive/cvmx-spi.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-2008 Cavium Networks7*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*29* Support library for the SPI30*/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-spi.h>3738#include <asm/octeon/cvmx-spxx-defs.h>39#include <asm/octeon/cvmx-stxx-defs.h>40#include <asm/octeon/cvmx-srxx-defs.h>4142#define INVOKE_CB(function_p, args...) \43do { \44if (function_p) { \45res = function_p(args); \46if (res) \47return res; \48} \49} while (0)5051#if CVMX_ENABLE_DEBUG_PRINTS52static const char *modes[] =53{ "UNKNOWN", "TX Halfplex", "Rx Halfplex", "Duplex" };54#endif5556/* Default callbacks, can be overridden57* using cvmx_spi_get_callbacks/cvmx_spi_set_callbacks58*/59static cvmx_spi_callbacks_t cvmx_spi_callbacks = {60.reset_cb = cvmx_spi_reset_cb,61.calendar_setup_cb = cvmx_spi_calendar_setup_cb,62.clock_detect_cb = cvmx_spi_clock_detect_cb,63.training_cb = cvmx_spi_training_cb,64.calendar_sync_cb = cvmx_spi_calendar_sync_cb,65.interface_up_cb = cvmx_spi_interface_up_cb66};6768/*69* Get current SPI4 initialization callbacks70*71* @callbacks: Pointer to the callbacks structure.to fill72*73* Returns Pointer to cvmx_spi_callbacks_t structure.74*/75void cvmx_spi_get_callbacks(cvmx_spi_callbacks_t *callbacks)76{77memcpy(callbacks, &cvmx_spi_callbacks, sizeof(cvmx_spi_callbacks));78}7980/*81* Set new SPI4 initialization callbacks82*83* @new_callbacks: Pointer to an updated callbacks structure.84*/85void cvmx_spi_set_callbacks(cvmx_spi_callbacks_t *new_callbacks)86{87memcpy(&cvmx_spi_callbacks, new_callbacks, sizeof(cvmx_spi_callbacks));88}8990/*91* Initialize and start the SPI interface.92*93* @interface: The identifier of the packet interface to configure and94* use as a SPI interface.95* @mode: The operating mode for the SPI interface. The interface96* can operate as a full duplex (both Tx and Rx data paths97* active) or as a halfplex (either the Tx data path is98* active or the Rx data path is active, but not both).99* @timeout: Timeout to wait for clock synchronization in seconds100* @num_ports: Number of SPI ports to configure101*102* Returns Zero on success, negative of failure.103*/104int cvmx_spi_start_interface(int interface, cvmx_spi_mode_t mode, int timeout,105int num_ports)106{107int res = -1;108109if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))110return res;111112/* Callback to perform SPI4 reset */113INVOKE_CB(cvmx_spi_callbacks.reset_cb, interface, mode);114115/* Callback to perform calendar setup */116INVOKE_CB(cvmx_spi_callbacks.calendar_setup_cb, interface, mode,117num_ports);118119/* Callback to perform clock detection */120INVOKE_CB(cvmx_spi_callbacks.clock_detect_cb, interface, mode, timeout);121122/* Callback to perform SPI4 link training */123INVOKE_CB(cvmx_spi_callbacks.training_cb, interface, mode, timeout);124125/* Callback to perform calendar sync */126INVOKE_CB(cvmx_spi_callbacks.calendar_sync_cb, interface, mode,127timeout);128129/* Callback to handle interface coming up */130INVOKE_CB(cvmx_spi_callbacks.interface_up_cb, interface, mode);131132return res;133}134135/*136* This routine restarts the SPI interface after it has lost synchronization137* with its correspondent system.138*139* @interface: The identifier of the packet interface to configure and140* use as a SPI interface.141* @mode: The operating mode for the SPI interface. The interface142* can operate as a full duplex (both Tx and Rx data paths143* active) or as a halfplex (either the Tx data path is144* active or the Rx data path is active, but not both).145* @timeout: Timeout to wait for clock synchronization in seconds146*147* Returns Zero on success, negative of failure.148*/149int cvmx_spi_restart_interface(int interface, cvmx_spi_mode_t mode, int timeout)150{151int res = -1;152153if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))154return res;155156cvmx_dprintf("SPI%d: Restart %s\n", interface, modes[mode]);157158/* Callback to perform SPI4 reset */159INVOKE_CB(cvmx_spi_callbacks.reset_cb, interface, mode);160161/* NOTE: Calendar setup is not performed during restart */162/* Refer to cvmx_spi_start_interface() for the full sequence */163164/* Callback to perform clock detection */165INVOKE_CB(cvmx_spi_callbacks.clock_detect_cb, interface, mode, timeout);166167/* Callback to perform SPI4 link training */168INVOKE_CB(cvmx_spi_callbacks.training_cb, interface, mode, timeout);169170/* Callback to perform calendar sync */171INVOKE_CB(cvmx_spi_callbacks.calendar_sync_cb, interface, mode,172timeout);173174/* Callback to handle interface coming up */175INVOKE_CB(cvmx_spi_callbacks.interface_up_cb, interface, mode);176177return res;178}179EXPORT_SYMBOL_GPL(cvmx_spi_restart_interface);180181/*182* Callback to perform SPI4 reset183*184* @interface: The identifier of the packet interface to configure and185* use as a SPI interface.186* @mode: The operating mode for the SPI interface. The interface187* can operate as a full duplex (both Tx and Rx data paths188* active) or as a halfplex (either the Tx data path is189* active or the Rx data path is active, but not both).190*191* Returns Zero on success, non-zero error code on failure (will cause192* SPI initialization to abort)193*/194int cvmx_spi_reset_cb(int interface, cvmx_spi_mode_t mode)195{196union cvmx_spxx_dbg_deskew_ctl spxx_dbg_deskew_ctl;197union cvmx_spxx_clk_ctl spxx_clk_ctl;198union cvmx_spxx_bist_stat spxx_bist_stat;199union cvmx_spxx_int_msk spxx_int_msk;200union cvmx_stxx_int_msk stxx_int_msk;201union cvmx_spxx_trn4_ctl spxx_trn4_ctl;202int index;203uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;204205/* Disable SPI error events while we run BIST */206spxx_int_msk.u64 = cvmx_read_csr(CVMX_SPXX_INT_MSK(interface));207cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), 0);208stxx_int_msk.u64 = cvmx_read_csr(CVMX_STXX_INT_MSK(interface));209cvmx_write_csr(CVMX_STXX_INT_MSK(interface), 0);210211/* Run BIST in the SPI interface */212cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), 0);213cvmx_write_csr(CVMX_STXX_COM_CTL(interface), 0);214spxx_clk_ctl.u64 = 0;215spxx_clk_ctl.s.runbist = 1;216cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);217__delay(10 * MS);218spxx_bist_stat.u64 = cvmx_read_csr(CVMX_SPXX_BIST_STAT(interface));219if (spxx_bist_stat.s.stat0)220cvmx_dprintf221("ERROR SPI%d: BIST failed on receive datapath FIFO\n",222interface);223if (spxx_bist_stat.s.stat1)224cvmx_dprintf("ERROR SPI%d: BIST failed on RX calendar table\n",225interface);226if (spxx_bist_stat.s.stat2)227cvmx_dprintf("ERROR SPI%d: BIST failed on TX calendar table\n",228interface);229230/* Clear the calendar table after BIST to fix parity errors */231for (index = 0; index < 32; index++) {232union cvmx_srxx_spi4_calx srxx_spi4_calx;233union cvmx_stxx_spi4_calx stxx_spi4_calx;234235srxx_spi4_calx.u64 = 0;236srxx_spi4_calx.s.oddpar = 1;237cvmx_write_csr(CVMX_SRXX_SPI4_CALX(index, interface),238srxx_spi4_calx.u64);239240stxx_spi4_calx.u64 = 0;241stxx_spi4_calx.s.oddpar = 1;242cvmx_write_csr(CVMX_STXX_SPI4_CALX(index, interface),243stxx_spi4_calx.u64);244}245246/* Re enable reporting of error interrupts */247cvmx_write_csr(CVMX_SPXX_INT_REG(interface),248cvmx_read_csr(CVMX_SPXX_INT_REG(interface)));249cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), spxx_int_msk.u64);250cvmx_write_csr(CVMX_STXX_INT_REG(interface),251cvmx_read_csr(CVMX_STXX_INT_REG(interface)));252cvmx_write_csr(CVMX_STXX_INT_MSK(interface), stxx_int_msk.u64);253254/* Setup the CLKDLY right in the middle */255spxx_clk_ctl.u64 = 0;256spxx_clk_ctl.s.seetrn = 0;257spxx_clk_ctl.s.clkdly = 0x10;258spxx_clk_ctl.s.runbist = 0;259spxx_clk_ctl.s.statdrv = 0;260/* This should always be on the opposite edge as statdrv */261spxx_clk_ctl.s.statrcv = 1;262spxx_clk_ctl.s.sndtrn = 0;263spxx_clk_ctl.s.drptrn = 0;264spxx_clk_ctl.s.rcvtrn = 0;265spxx_clk_ctl.s.srxdlck = 0;266cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);267__delay(100 * MS);268269/* Reset SRX0 DLL */270spxx_clk_ctl.s.srxdlck = 1;271cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);272273/* Waiting for Inf0 Spi4 RX DLL to lock */274__delay(100 * MS);275276/* Enable dynamic alignment */277spxx_trn4_ctl.s.trntest = 0;278spxx_trn4_ctl.s.jitter = 1;279spxx_trn4_ctl.s.clr_boot = 1;280spxx_trn4_ctl.s.set_boot = 0;281if (OCTEON_IS_MODEL(OCTEON_CN58XX))282spxx_trn4_ctl.s.maxdist = 3;283else284spxx_trn4_ctl.s.maxdist = 8;285spxx_trn4_ctl.s.macro_en = 1;286spxx_trn4_ctl.s.mux_en = 1;287cvmx_write_csr(CVMX_SPXX_TRN4_CTL(interface), spxx_trn4_ctl.u64);288289spxx_dbg_deskew_ctl.u64 = 0;290cvmx_write_csr(CVMX_SPXX_DBG_DESKEW_CTL(interface),291spxx_dbg_deskew_ctl.u64);292293return 0;294}295296/*297* Callback to setup calendar and miscellaneous settings before clock detection298*299* @interface: The identifier of the packet interface to configure and300* use as a SPI interface.301* @mode: The operating mode for the SPI interface. The interface302* can operate as a full duplex (both Tx and Rx data paths303* active) or as a halfplex (either the Tx data path is304* active or the Rx data path is active, but not both).305* @num_ports: Number of ports to configure on SPI306*307* Returns Zero on success, non-zero error code on failure (will cause308* SPI initialization to abort)309*/310int cvmx_spi_calendar_setup_cb(int interface, cvmx_spi_mode_t mode,311int num_ports)312{313int port;314int index;315if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {316union cvmx_srxx_com_ctl srxx_com_ctl;317union cvmx_srxx_spi4_stat srxx_spi4_stat;318319/* SRX0 number of Ports */320srxx_com_ctl.u64 = 0;321srxx_com_ctl.s.prts = num_ports - 1;322srxx_com_ctl.s.st_en = 0;323srxx_com_ctl.s.inf_en = 0;324cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);325326/* SRX0 Calendar Table. This round robbins through all ports */327port = 0;328index = 0;329while (port < num_ports) {330union cvmx_srxx_spi4_calx srxx_spi4_calx;331srxx_spi4_calx.u64 = 0;332srxx_spi4_calx.s.prt0 = port++;333srxx_spi4_calx.s.prt1 = port++;334srxx_spi4_calx.s.prt2 = port++;335srxx_spi4_calx.s.prt3 = port++;336srxx_spi4_calx.s.oddpar =337~(cvmx_dpop(srxx_spi4_calx.u64) & 1);338cvmx_write_csr(CVMX_SRXX_SPI4_CALX(index, interface),339srxx_spi4_calx.u64);340index++;341}342srxx_spi4_stat.u64 = 0;343srxx_spi4_stat.s.len = num_ports;344srxx_spi4_stat.s.m = 1;345cvmx_write_csr(CVMX_SRXX_SPI4_STAT(interface),346srxx_spi4_stat.u64);347}348349if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {350union cvmx_stxx_arb_ctl stxx_arb_ctl;351union cvmx_gmxx_tx_spi_max gmxx_tx_spi_max;352union cvmx_gmxx_tx_spi_thresh gmxx_tx_spi_thresh;353union cvmx_gmxx_tx_spi_ctl gmxx_tx_spi_ctl;354union cvmx_stxx_spi4_stat stxx_spi4_stat;355union cvmx_stxx_spi4_dat stxx_spi4_dat;356357/* STX0 Config */358stxx_arb_ctl.u64 = 0;359stxx_arb_ctl.s.igntpa = 0;360stxx_arb_ctl.s.mintrn = 0;361cvmx_write_csr(CVMX_STXX_ARB_CTL(interface), stxx_arb_ctl.u64);362363gmxx_tx_spi_max.u64 = 0;364gmxx_tx_spi_max.s.max1 = 8;365gmxx_tx_spi_max.s.max2 = 4;366gmxx_tx_spi_max.s.slice = 0;367cvmx_write_csr(CVMX_GMXX_TX_SPI_MAX(interface),368gmxx_tx_spi_max.u64);369370gmxx_tx_spi_thresh.u64 = 0;371gmxx_tx_spi_thresh.s.thresh = 4;372cvmx_write_csr(CVMX_GMXX_TX_SPI_THRESH(interface),373gmxx_tx_spi_thresh.u64);374375gmxx_tx_spi_ctl.u64 = 0;376gmxx_tx_spi_ctl.s.tpa_clr = 0;377gmxx_tx_spi_ctl.s.cont_pkt = 0;378cvmx_write_csr(CVMX_GMXX_TX_SPI_CTL(interface),379gmxx_tx_spi_ctl.u64);380381/* STX0 Training Control */382stxx_spi4_dat.u64 = 0;383/*Minimum needed by dynamic alignment */384stxx_spi4_dat.s.alpha = 32;385stxx_spi4_dat.s.max_t = 0xFFFF; /*Minimum interval is 0x20 */386cvmx_write_csr(CVMX_STXX_SPI4_DAT(interface),387stxx_spi4_dat.u64);388389/* STX0 Calendar Table. This round robbins through all ports */390port = 0;391index = 0;392while (port < num_ports) {393union cvmx_stxx_spi4_calx stxx_spi4_calx;394stxx_spi4_calx.u64 = 0;395stxx_spi4_calx.s.prt0 = port++;396stxx_spi4_calx.s.prt1 = port++;397stxx_spi4_calx.s.prt2 = port++;398stxx_spi4_calx.s.prt3 = port++;399stxx_spi4_calx.s.oddpar =400~(cvmx_dpop(stxx_spi4_calx.u64) & 1);401cvmx_write_csr(CVMX_STXX_SPI4_CALX(index, interface),402stxx_spi4_calx.u64);403index++;404}405stxx_spi4_stat.u64 = 0;406stxx_spi4_stat.s.len = num_ports;407stxx_spi4_stat.s.m = 1;408cvmx_write_csr(CVMX_STXX_SPI4_STAT(interface),409stxx_spi4_stat.u64);410}411412return 0;413}414415/*416* Callback to perform clock detection417*418* @interface: The identifier of the packet interface to configure and419* use as a SPI interface.420* @mode: The operating mode for the SPI interface. The interface421* can operate as a full duplex (both Tx and Rx data paths422* active) or as a halfplex (either the Tx data path is423* active or the Rx data path is active, but not both).424* @timeout: Timeout to wait for clock synchronization in seconds425*426* Returns Zero on success, non-zero error code on failure (will cause427* SPI initialization to abort)428*/429int cvmx_spi_clock_detect_cb(int interface, cvmx_spi_mode_t mode, int timeout)430{431int clock_transitions;432union cvmx_spxx_clk_stat stat;433uint64_t timeout_time;434uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;435436/*437* Regardless of operating mode, both Tx and Rx clocks must be438* present for the SPI interface to operate.439*/440cvmx_dprintf("SPI%d: Waiting to see TsClk...\n", interface);441timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;442/*443* Require 100 clock transitions in order to avoid any noise444* in the beginning.445*/446clock_transitions = 100;447do {448stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));449if (stat.s.s4clk0 && stat.s.s4clk1 && clock_transitions) {450/*451* We've seen a clock transition, so decrement452* the number we still need.453*/454clock_transitions--;455cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);456stat.s.s4clk0 = 0;457stat.s.s4clk1 = 0;458}459if (cvmx_get_cycle() > timeout_time) {460cvmx_dprintf("SPI%d: Timeout\n", interface);461return -1;462}463} while (stat.s.s4clk0 == 0 || stat.s.s4clk1 == 0);464465cvmx_dprintf("SPI%d: Waiting to see RsClk...\n", interface);466timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;467/*468* Require 100 clock transitions in order to avoid any noise in the469* beginning.470*/471clock_transitions = 100;472do {473stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));474if (stat.s.d4clk0 && stat.s.d4clk1 && clock_transitions) {475/*476* We've seen a clock transition, so decrement477* the number we still need478*/479clock_transitions--;480cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);481stat.s.d4clk0 = 0;482stat.s.d4clk1 = 0;483}484if (cvmx_get_cycle() > timeout_time) {485cvmx_dprintf("SPI%d: Timeout\n", interface);486return -1;487}488} while (stat.s.d4clk0 == 0 || stat.s.d4clk1 == 0);489490return 0;491}492493/*494* Callback to perform link training495*496* @interface: The identifier of the packet interface to configure and497* use as a SPI interface.498* @mode: The operating mode for the SPI interface. The interface499* can operate as a full duplex (both Tx and Rx data paths500* active) or as a halfplex (either the Tx data path is501* active or the Rx data path is active, but not both).502* @timeout: Timeout to wait for link to be trained (in seconds)503*504* Returns Zero on success, non-zero error code on failure (will cause505* SPI initialization to abort)506*/507int cvmx_spi_training_cb(int interface, cvmx_spi_mode_t mode, int timeout)508{509union cvmx_spxx_trn4_ctl spxx_trn4_ctl;510union cvmx_spxx_clk_stat stat;511uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;512uint64_t timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;513int rx_training_needed;514515/* SRX0 & STX0 Inf0 Links are configured - begin training */516union cvmx_spxx_clk_ctl spxx_clk_ctl;517spxx_clk_ctl.u64 = 0;518spxx_clk_ctl.s.seetrn = 0;519spxx_clk_ctl.s.clkdly = 0x10;520spxx_clk_ctl.s.runbist = 0;521spxx_clk_ctl.s.statdrv = 0;522/* This should always be on the opposite edge as statdrv */523spxx_clk_ctl.s.statrcv = 1;524spxx_clk_ctl.s.sndtrn = 1;525spxx_clk_ctl.s.drptrn = 1;526spxx_clk_ctl.s.rcvtrn = 1;527spxx_clk_ctl.s.srxdlck = 1;528cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);529__delay(1000 * MS);530531/* SRX0 clear the boot bit */532spxx_trn4_ctl.u64 = cvmx_read_csr(CVMX_SPXX_TRN4_CTL(interface));533spxx_trn4_ctl.s.clr_boot = 1;534cvmx_write_csr(CVMX_SPXX_TRN4_CTL(interface), spxx_trn4_ctl.u64);535536/* Wait for the training sequence to complete */537cvmx_dprintf("SPI%d: Waiting for training\n", interface);538__delay(1000 * MS);539/* Wait a really long time here */540timeout_time = cvmx_get_cycle() + 1000ull * MS * 600;541/*542* The HRM says we must wait for 34 + 16 * MAXDIST training sequences.543* We'll be pessimistic and wait for a lot more.544*/545rx_training_needed = 500;546do {547stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));548if (stat.s.srxtrn && rx_training_needed) {549rx_training_needed--;550cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);551stat.s.srxtrn = 0;552}553if (cvmx_get_cycle() > timeout_time) {554cvmx_dprintf("SPI%d: Timeout\n", interface);555return -1;556}557} while (stat.s.srxtrn == 0);558559return 0;560}561562/*563* Callback to perform calendar data synchronization564*565* @interface: The identifier of the packet interface to configure and566* use as a SPI interface.567* @mode: The operating mode for the SPI interface. The interface568* can operate as a full duplex (both Tx and Rx data paths569* active) or as a halfplex (either the Tx data path is570* active or the Rx data path is active, but not both).571* @timeout: Timeout to wait for calendar data in seconds572*573* Returns Zero on success, non-zero error code on failure (will cause574* SPI initialization to abort)575*/576int cvmx_spi_calendar_sync_cb(int interface, cvmx_spi_mode_t mode, int timeout)577{578uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;579if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {580/* SRX0 interface should be good, send calendar data */581union cvmx_srxx_com_ctl srxx_com_ctl;582cvmx_dprintf583("SPI%d: Rx is synchronized, start sending calendar data\n",584interface);585srxx_com_ctl.u64 = cvmx_read_csr(CVMX_SRXX_COM_CTL(interface));586srxx_com_ctl.s.inf_en = 1;587srxx_com_ctl.s.st_en = 1;588cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);589}590591if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {592/* STX0 has achieved sync */593/* The corespondant board should be sending calendar data */594/* Enable the STX0 STAT receiver. */595union cvmx_spxx_clk_stat stat;596uint64_t timeout_time;597union cvmx_stxx_com_ctl stxx_com_ctl;598stxx_com_ctl.u64 = 0;599stxx_com_ctl.s.st_en = 1;600cvmx_write_csr(CVMX_STXX_COM_CTL(interface), stxx_com_ctl.u64);601602/* Waiting for calendar sync on STX0 STAT */603cvmx_dprintf("SPI%d: Waiting to sync on STX[%d] STAT\n",604interface, interface);605timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;606/* SPX0_CLK_STAT - SPX0_CLK_STAT[STXCAL] should be 1 (bit10) */607do {608stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));609if (cvmx_get_cycle() > timeout_time) {610cvmx_dprintf("SPI%d: Timeout\n", interface);611return -1;612}613} while (stat.s.stxcal == 0);614}615616return 0;617}618619/*620* Callback to handle interface up621*622* @interface: The identifier of the packet interface to configure and623* use as a SPI interface.624* @mode: The operating mode for the SPI interface. The interface625* can operate as a full duplex (both Tx and Rx data paths626* active) or as a halfplex (either the Tx data path is627* active or the Rx data path is active, but not both).628*629* Returns Zero on success, non-zero error code on failure (will cause630* SPI initialization to abort)631*/632int cvmx_spi_interface_up_cb(int interface, cvmx_spi_mode_t mode)633{634union cvmx_gmxx_rxx_frm_min gmxx_rxx_frm_min;635union cvmx_gmxx_rxx_frm_max gmxx_rxx_frm_max;636union cvmx_gmxx_rxx_jabber gmxx_rxx_jabber;637638if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {639union cvmx_srxx_com_ctl srxx_com_ctl;640srxx_com_ctl.u64 = cvmx_read_csr(CVMX_SRXX_COM_CTL(interface));641srxx_com_ctl.s.inf_en = 1;642cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);643cvmx_dprintf("SPI%d: Rx is now up\n", interface);644}645646if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {647union cvmx_stxx_com_ctl stxx_com_ctl;648stxx_com_ctl.u64 = cvmx_read_csr(CVMX_STXX_COM_CTL(interface));649stxx_com_ctl.s.inf_en = 1;650cvmx_write_csr(CVMX_STXX_COM_CTL(interface), stxx_com_ctl.u64);651cvmx_dprintf("SPI%d: Tx is now up\n", interface);652}653654gmxx_rxx_frm_min.u64 = 0;655gmxx_rxx_frm_min.s.len = 64;656cvmx_write_csr(CVMX_GMXX_RXX_FRM_MIN(0, interface),657gmxx_rxx_frm_min.u64);658gmxx_rxx_frm_max.u64 = 0;659gmxx_rxx_frm_max.s.len = 64 * 1024 - 4;660cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX(0, interface),661gmxx_rxx_frm_max.u64);662gmxx_rxx_jabber.u64 = 0;663gmxx_rxx_jabber.s.cnt = 64 * 1024 - 4;664cvmx_write_csr(CVMX_GMXX_RXX_JABBER(0, interface), gmxx_rxx_jabber.u64);665666return 0;667}668669670