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-2008 Cavium Networks
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 *
308c2ecf20Sopenharmony_ci * Support library for the SPI
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-spi.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-spxx-defs.h>
408c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-stxx-defs.h>
418c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-srxx-defs.h>
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define INVOKE_CB(function_p, args...)		\
448c2ecf20Sopenharmony_ci	do {					\
458c2ecf20Sopenharmony_ci		if (function_p) {		\
468c2ecf20Sopenharmony_ci			res = function_p(args); \
478c2ecf20Sopenharmony_ci			if (res)		\
488c2ecf20Sopenharmony_ci				return res;	\
498c2ecf20Sopenharmony_ci		}				\
508c2ecf20Sopenharmony_ci	} while (0)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#if CVMX_ENABLE_DEBUG_PRINTS
538c2ecf20Sopenharmony_cistatic const char *modes[] =
548c2ecf20Sopenharmony_ci    { "UNKNOWN", "TX Halfplex", "Rx Halfplex", "Duplex" };
558c2ecf20Sopenharmony_ci#endif
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* Default callbacks, can be overridden
588c2ecf20Sopenharmony_ci *  using cvmx_spi_get_callbacks/cvmx_spi_set_callbacks
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_cistatic cvmx_spi_callbacks_t cvmx_spi_callbacks = {
618c2ecf20Sopenharmony_ci	.reset_cb = cvmx_spi_reset_cb,
628c2ecf20Sopenharmony_ci	.calendar_setup_cb = cvmx_spi_calendar_setup_cb,
638c2ecf20Sopenharmony_ci	.clock_detect_cb = cvmx_spi_clock_detect_cb,
648c2ecf20Sopenharmony_ci	.training_cb = cvmx_spi_training_cb,
658c2ecf20Sopenharmony_ci	.calendar_sync_cb = cvmx_spi_calendar_sync_cb,
668c2ecf20Sopenharmony_ci	.interface_up_cb = cvmx_spi_interface_up_cb
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/**
708c2ecf20Sopenharmony_ci * Get current SPI4 initialization callbacks
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * @callbacks:	Pointer to the callbacks structure.to fill
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci * Returns Pointer to cvmx_spi_callbacks_t structure.
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_civoid cvmx_spi_get_callbacks(cvmx_spi_callbacks_t *callbacks)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	memcpy(callbacks, &cvmx_spi_callbacks, sizeof(cvmx_spi_callbacks));
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/**
828c2ecf20Sopenharmony_ci * Set new SPI4 initialization callbacks
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * @new_callbacks:  Pointer to an updated callbacks structure.
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_civoid cvmx_spi_set_callbacks(cvmx_spi_callbacks_t *new_callbacks)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	memcpy(&cvmx_spi_callbacks, new_callbacks, sizeof(cvmx_spi_callbacks));
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/**
928c2ecf20Sopenharmony_ci * Initialize and start the SPI interface.
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * @interface: The identifier of the packet interface to configure and
958c2ecf20Sopenharmony_ci *		    use as a SPI interface.
968c2ecf20Sopenharmony_ci * @mode:      The operating mode for the SPI interface. The interface
978c2ecf20Sopenharmony_ci *		    can operate as a full duplex (both Tx and Rx data paths
988c2ecf20Sopenharmony_ci *		    active) or as a halfplex (either the Tx data path is
998c2ecf20Sopenharmony_ci *		    active or the Rx data path is active, but not both).
1008c2ecf20Sopenharmony_ci * @timeout:   Timeout to wait for clock synchronization in seconds
1018c2ecf20Sopenharmony_ci * @num_ports: Number of SPI ports to configure
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * Returns Zero on success, negative of failure.
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_ciint cvmx_spi_start_interface(int interface, cvmx_spi_mode_t mode, int timeout,
1068c2ecf20Sopenharmony_ci			     int num_ports)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	int res = -1;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))
1118c2ecf20Sopenharmony_ci		return res;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* Callback to perform SPI4 reset */
1148c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.reset_cb, interface, mode);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* Callback to perform calendar setup */
1178c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.calendar_setup_cb, interface, mode,
1188c2ecf20Sopenharmony_ci		  num_ports);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Callback to perform clock detection */
1218c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.clock_detect_cb, interface, mode, timeout);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* Callback to perform SPI4 link training */
1248c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.training_cb, interface, mode, timeout);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* Callback to perform calendar sync */
1278c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.calendar_sync_cb, interface, mode,
1288c2ecf20Sopenharmony_ci		  timeout);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/* Callback to handle interface coming up */
1318c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.interface_up_cb, interface, mode);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	return res;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/**
1378c2ecf20Sopenharmony_ci * This routine restarts the SPI interface after it has lost synchronization
1388c2ecf20Sopenharmony_ci * with its correspondent system.
1398c2ecf20Sopenharmony_ci *
1408c2ecf20Sopenharmony_ci * @interface: The identifier of the packet interface to configure and
1418c2ecf20Sopenharmony_ci *		    use as a SPI interface.
1428c2ecf20Sopenharmony_ci * @mode:      The operating mode for the SPI interface. The interface
1438c2ecf20Sopenharmony_ci *		    can operate as a full duplex (both Tx and Rx data paths
1448c2ecf20Sopenharmony_ci *		    active) or as a halfplex (either the Tx data path is
1458c2ecf20Sopenharmony_ci *		    active or the Rx data path is active, but not both).
1468c2ecf20Sopenharmony_ci * @timeout:   Timeout to wait for clock synchronization in seconds
1478c2ecf20Sopenharmony_ci *
1488c2ecf20Sopenharmony_ci * Returns Zero on success, negative of failure.
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_ciint cvmx_spi_restart_interface(int interface, cvmx_spi_mode_t mode, int timeout)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	int res = -1;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))
1558c2ecf20Sopenharmony_ci		return res;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	cvmx_dprintf("SPI%d: Restart %s\n", interface, modes[mode]);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	/* Callback to perform SPI4 reset */
1608c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.reset_cb, interface, mode);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	/* NOTE: Calendar setup is not performed during restart */
1638c2ecf20Sopenharmony_ci	/*	 Refer to cvmx_spi_start_interface() for the full sequence */
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* Callback to perform clock detection */
1668c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.clock_detect_cb, interface, mode, timeout);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/* Callback to perform SPI4 link training */
1698c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.training_cb, interface, mode, timeout);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* Callback to perform calendar sync */
1728c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.calendar_sync_cb, interface, mode,
1738c2ecf20Sopenharmony_ci		  timeout);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* Callback to handle interface coming up */
1768c2ecf20Sopenharmony_ci	INVOKE_CB(cvmx_spi_callbacks.interface_up_cb, interface, mode);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	return res;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cvmx_spi_restart_interface);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/**
1838c2ecf20Sopenharmony_ci * Callback to perform SPI4 reset
1848c2ecf20Sopenharmony_ci *
1858c2ecf20Sopenharmony_ci * @interface: The identifier of the packet interface to configure and
1868c2ecf20Sopenharmony_ci *		    use as a SPI interface.
1878c2ecf20Sopenharmony_ci * @mode:      The operating mode for the SPI interface. The interface
1888c2ecf20Sopenharmony_ci *		    can operate as a full duplex (both Tx and Rx data paths
1898c2ecf20Sopenharmony_ci *		    active) or as a halfplex (either the Tx data path is
1908c2ecf20Sopenharmony_ci *		    active or the Rx data path is active, but not both).
1918c2ecf20Sopenharmony_ci *
1928c2ecf20Sopenharmony_ci * Returns Zero on success, non-zero error code on failure (will cause
1938c2ecf20Sopenharmony_ci * SPI initialization to abort)
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_ciint cvmx_spi_reset_cb(int interface, cvmx_spi_mode_t mode)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	union cvmx_spxx_dbg_deskew_ctl spxx_dbg_deskew_ctl;
1988c2ecf20Sopenharmony_ci	union cvmx_spxx_clk_ctl spxx_clk_ctl;
1998c2ecf20Sopenharmony_ci	union cvmx_spxx_bist_stat spxx_bist_stat;
2008c2ecf20Sopenharmony_ci	union cvmx_spxx_int_msk spxx_int_msk;
2018c2ecf20Sopenharmony_ci	union cvmx_stxx_int_msk stxx_int_msk;
2028c2ecf20Sopenharmony_ci	union cvmx_spxx_trn4_ctl spxx_trn4_ctl;
2038c2ecf20Sopenharmony_ci	int index;
2048c2ecf20Sopenharmony_ci	uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/* Disable SPI error events while we run BIST */
2078c2ecf20Sopenharmony_ci	spxx_int_msk.u64 = cvmx_read_csr(CVMX_SPXX_INT_MSK(interface));
2088c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), 0);
2098c2ecf20Sopenharmony_ci	stxx_int_msk.u64 = cvmx_read_csr(CVMX_STXX_INT_MSK(interface));
2108c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_STXX_INT_MSK(interface), 0);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/* Run BIST in the SPI interface */
2138c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), 0);
2148c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_STXX_COM_CTL(interface), 0);
2158c2ecf20Sopenharmony_ci	spxx_clk_ctl.u64 = 0;
2168c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.runbist = 1;
2178c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
2188c2ecf20Sopenharmony_ci	__delay(10 * MS);
2198c2ecf20Sopenharmony_ci	spxx_bist_stat.u64 = cvmx_read_csr(CVMX_SPXX_BIST_STAT(interface));
2208c2ecf20Sopenharmony_ci	if (spxx_bist_stat.s.stat0)
2218c2ecf20Sopenharmony_ci		cvmx_dprintf
2228c2ecf20Sopenharmony_ci		    ("ERROR SPI%d: BIST failed on receive datapath FIFO\n",
2238c2ecf20Sopenharmony_ci		     interface);
2248c2ecf20Sopenharmony_ci	if (spxx_bist_stat.s.stat1)
2258c2ecf20Sopenharmony_ci		cvmx_dprintf("ERROR SPI%d: BIST failed on RX calendar table\n",
2268c2ecf20Sopenharmony_ci			     interface);
2278c2ecf20Sopenharmony_ci	if (spxx_bist_stat.s.stat2)
2288c2ecf20Sopenharmony_ci		cvmx_dprintf("ERROR SPI%d: BIST failed on TX calendar table\n",
2298c2ecf20Sopenharmony_ci			     interface);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/* Clear the calendar table after BIST to fix parity errors */
2328c2ecf20Sopenharmony_ci	for (index = 0; index < 32; index++) {
2338c2ecf20Sopenharmony_ci		union cvmx_srxx_spi4_calx srxx_spi4_calx;
2348c2ecf20Sopenharmony_ci		union cvmx_stxx_spi4_calx stxx_spi4_calx;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci		srxx_spi4_calx.u64 = 0;
2378c2ecf20Sopenharmony_ci		srxx_spi4_calx.s.oddpar = 1;
2388c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_SRXX_SPI4_CALX(index, interface),
2398c2ecf20Sopenharmony_ci			       srxx_spi4_calx.u64);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		stxx_spi4_calx.u64 = 0;
2428c2ecf20Sopenharmony_ci		stxx_spi4_calx.s.oddpar = 1;
2438c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_STXX_SPI4_CALX(index, interface),
2448c2ecf20Sopenharmony_ci			       stxx_spi4_calx.u64);
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	/* Re enable reporting of error interrupts */
2488c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_INT_REG(interface),
2498c2ecf20Sopenharmony_ci		       cvmx_read_csr(CVMX_SPXX_INT_REG(interface)));
2508c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), spxx_int_msk.u64);
2518c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_STXX_INT_REG(interface),
2528c2ecf20Sopenharmony_ci		       cvmx_read_csr(CVMX_STXX_INT_REG(interface)));
2538c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_STXX_INT_MSK(interface), stxx_int_msk.u64);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	/* Setup the CLKDLY right in the middle */
2568c2ecf20Sopenharmony_ci	spxx_clk_ctl.u64 = 0;
2578c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.seetrn = 0;
2588c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.clkdly = 0x10;
2598c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.runbist = 0;
2608c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.statdrv = 0;
2618c2ecf20Sopenharmony_ci	/* This should always be on the opposite edge as statdrv */
2628c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.statrcv = 1;
2638c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.sndtrn = 0;
2648c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.drptrn = 0;
2658c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.rcvtrn = 0;
2668c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.srxdlck = 0;
2678c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
2688c2ecf20Sopenharmony_ci	__delay(100 * MS);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	/* Reset SRX0 DLL */
2718c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.srxdlck = 1;
2728c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Waiting for Inf0 Spi4 RX DLL to lock */
2758c2ecf20Sopenharmony_ci	__delay(100 * MS);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* Enable dynamic alignment */
2788c2ecf20Sopenharmony_ci	spxx_trn4_ctl.s.trntest = 0;
2798c2ecf20Sopenharmony_ci	spxx_trn4_ctl.s.jitter = 1;
2808c2ecf20Sopenharmony_ci	spxx_trn4_ctl.s.clr_boot = 1;
2818c2ecf20Sopenharmony_ci	spxx_trn4_ctl.s.set_boot = 0;
2828c2ecf20Sopenharmony_ci	if (OCTEON_IS_MODEL(OCTEON_CN58XX))
2838c2ecf20Sopenharmony_ci		spxx_trn4_ctl.s.maxdist = 3;
2848c2ecf20Sopenharmony_ci	else
2858c2ecf20Sopenharmony_ci		spxx_trn4_ctl.s.maxdist = 8;
2868c2ecf20Sopenharmony_ci	spxx_trn4_ctl.s.macro_en = 1;
2878c2ecf20Sopenharmony_ci	spxx_trn4_ctl.s.mux_en = 1;
2888c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_TRN4_CTL(interface), spxx_trn4_ctl.u64);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	spxx_dbg_deskew_ctl.u64 = 0;
2918c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_DBG_DESKEW_CTL(interface),
2928c2ecf20Sopenharmony_ci		       spxx_dbg_deskew_ctl.u64);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return 0;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/**
2988c2ecf20Sopenharmony_ci * Callback to setup calendar and miscellaneous settings before clock detection
2998c2ecf20Sopenharmony_ci *
3008c2ecf20Sopenharmony_ci * @interface: The identifier of the packet interface to configure and
3018c2ecf20Sopenharmony_ci *		    use as a SPI interface.
3028c2ecf20Sopenharmony_ci * @mode:      The operating mode for the SPI interface. The interface
3038c2ecf20Sopenharmony_ci *		    can operate as a full duplex (both Tx and Rx data paths
3048c2ecf20Sopenharmony_ci *		    active) or as a halfplex (either the Tx data path is
3058c2ecf20Sopenharmony_ci *		    active or the Rx data path is active, but not both).
3068c2ecf20Sopenharmony_ci * @num_ports: Number of ports to configure on SPI
3078c2ecf20Sopenharmony_ci *
3088c2ecf20Sopenharmony_ci * Returns Zero on success, non-zero error code on failure (will cause
3098c2ecf20Sopenharmony_ci * SPI initialization to abort)
3108c2ecf20Sopenharmony_ci */
3118c2ecf20Sopenharmony_ciint cvmx_spi_calendar_setup_cb(int interface, cvmx_spi_mode_t mode,
3128c2ecf20Sopenharmony_ci			       int num_ports)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	int port;
3158c2ecf20Sopenharmony_ci	int index;
3168c2ecf20Sopenharmony_ci	if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
3178c2ecf20Sopenharmony_ci		union cvmx_srxx_com_ctl srxx_com_ctl;
3188c2ecf20Sopenharmony_ci		union cvmx_srxx_spi4_stat srxx_spi4_stat;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci		/* SRX0 number of Ports */
3218c2ecf20Sopenharmony_ci		srxx_com_ctl.u64 = 0;
3228c2ecf20Sopenharmony_ci		srxx_com_ctl.s.prts = num_ports - 1;
3238c2ecf20Sopenharmony_ci		srxx_com_ctl.s.st_en = 0;
3248c2ecf20Sopenharmony_ci		srxx_com_ctl.s.inf_en = 0;
3258c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci		/* SRX0 Calendar Table. This round robbins through all ports */
3288c2ecf20Sopenharmony_ci		port = 0;
3298c2ecf20Sopenharmony_ci		index = 0;
3308c2ecf20Sopenharmony_ci		while (port < num_ports) {
3318c2ecf20Sopenharmony_ci			union cvmx_srxx_spi4_calx srxx_spi4_calx;
3328c2ecf20Sopenharmony_ci			srxx_spi4_calx.u64 = 0;
3338c2ecf20Sopenharmony_ci			srxx_spi4_calx.s.prt0 = port++;
3348c2ecf20Sopenharmony_ci			srxx_spi4_calx.s.prt1 = port++;
3358c2ecf20Sopenharmony_ci			srxx_spi4_calx.s.prt2 = port++;
3368c2ecf20Sopenharmony_ci			srxx_spi4_calx.s.prt3 = port++;
3378c2ecf20Sopenharmony_ci			srxx_spi4_calx.s.oddpar =
3388c2ecf20Sopenharmony_ci			    ~(cvmx_dpop(srxx_spi4_calx.u64) & 1);
3398c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_SRXX_SPI4_CALX(index, interface),
3408c2ecf20Sopenharmony_ci				       srxx_spi4_calx.u64);
3418c2ecf20Sopenharmony_ci			index++;
3428c2ecf20Sopenharmony_ci		}
3438c2ecf20Sopenharmony_ci		srxx_spi4_stat.u64 = 0;
3448c2ecf20Sopenharmony_ci		srxx_spi4_stat.s.len = num_ports;
3458c2ecf20Sopenharmony_ci		srxx_spi4_stat.s.m = 1;
3468c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_SRXX_SPI4_STAT(interface),
3478c2ecf20Sopenharmony_ci			       srxx_spi4_stat.u64);
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
3518c2ecf20Sopenharmony_ci		union cvmx_stxx_arb_ctl stxx_arb_ctl;
3528c2ecf20Sopenharmony_ci		union cvmx_gmxx_tx_spi_max gmxx_tx_spi_max;
3538c2ecf20Sopenharmony_ci		union cvmx_gmxx_tx_spi_thresh gmxx_tx_spi_thresh;
3548c2ecf20Sopenharmony_ci		union cvmx_gmxx_tx_spi_ctl gmxx_tx_spi_ctl;
3558c2ecf20Sopenharmony_ci		union cvmx_stxx_spi4_stat stxx_spi4_stat;
3568c2ecf20Sopenharmony_ci		union cvmx_stxx_spi4_dat stxx_spi4_dat;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		/* STX0 Config */
3598c2ecf20Sopenharmony_ci		stxx_arb_ctl.u64 = 0;
3608c2ecf20Sopenharmony_ci		stxx_arb_ctl.s.igntpa = 0;
3618c2ecf20Sopenharmony_ci		stxx_arb_ctl.s.mintrn = 0;
3628c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_STXX_ARB_CTL(interface), stxx_arb_ctl.u64);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci		gmxx_tx_spi_max.u64 = 0;
3658c2ecf20Sopenharmony_ci		gmxx_tx_spi_max.s.max1 = 8;
3668c2ecf20Sopenharmony_ci		gmxx_tx_spi_max.s.max2 = 4;
3678c2ecf20Sopenharmony_ci		gmxx_tx_spi_max.s.slice = 0;
3688c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TX_SPI_MAX(interface),
3698c2ecf20Sopenharmony_ci			       gmxx_tx_spi_max.u64);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		gmxx_tx_spi_thresh.u64 = 0;
3728c2ecf20Sopenharmony_ci		gmxx_tx_spi_thresh.s.thresh = 4;
3738c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TX_SPI_THRESH(interface),
3748c2ecf20Sopenharmony_ci			       gmxx_tx_spi_thresh.u64);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci		gmxx_tx_spi_ctl.u64 = 0;
3778c2ecf20Sopenharmony_ci		gmxx_tx_spi_ctl.s.tpa_clr = 0;
3788c2ecf20Sopenharmony_ci		gmxx_tx_spi_ctl.s.cont_pkt = 0;
3798c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_GMXX_TX_SPI_CTL(interface),
3808c2ecf20Sopenharmony_ci			       gmxx_tx_spi_ctl.u64);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci		/* STX0 Training Control */
3838c2ecf20Sopenharmony_ci		stxx_spi4_dat.u64 = 0;
3848c2ecf20Sopenharmony_ci		/*Minimum needed by dynamic alignment */
3858c2ecf20Sopenharmony_ci		stxx_spi4_dat.s.alpha = 32;
3868c2ecf20Sopenharmony_ci		stxx_spi4_dat.s.max_t = 0xFFFF; /*Minimum interval is 0x20 */
3878c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_STXX_SPI4_DAT(interface),
3888c2ecf20Sopenharmony_ci			       stxx_spi4_dat.u64);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci		/* STX0 Calendar Table. This round robbins through all ports */
3918c2ecf20Sopenharmony_ci		port = 0;
3928c2ecf20Sopenharmony_ci		index = 0;
3938c2ecf20Sopenharmony_ci		while (port < num_ports) {
3948c2ecf20Sopenharmony_ci			union cvmx_stxx_spi4_calx stxx_spi4_calx;
3958c2ecf20Sopenharmony_ci			stxx_spi4_calx.u64 = 0;
3968c2ecf20Sopenharmony_ci			stxx_spi4_calx.s.prt0 = port++;
3978c2ecf20Sopenharmony_ci			stxx_spi4_calx.s.prt1 = port++;
3988c2ecf20Sopenharmony_ci			stxx_spi4_calx.s.prt2 = port++;
3998c2ecf20Sopenharmony_ci			stxx_spi4_calx.s.prt3 = port++;
4008c2ecf20Sopenharmony_ci			stxx_spi4_calx.s.oddpar =
4018c2ecf20Sopenharmony_ci			    ~(cvmx_dpop(stxx_spi4_calx.u64) & 1);
4028c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_STXX_SPI4_CALX(index, interface),
4038c2ecf20Sopenharmony_ci				       stxx_spi4_calx.u64);
4048c2ecf20Sopenharmony_ci			index++;
4058c2ecf20Sopenharmony_ci		}
4068c2ecf20Sopenharmony_ci		stxx_spi4_stat.u64 = 0;
4078c2ecf20Sopenharmony_ci		stxx_spi4_stat.s.len = num_ports;
4088c2ecf20Sopenharmony_ci		stxx_spi4_stat.s.m = 1;
4098c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_STXX_SPI4_STAT(interface),
4108c2ecf20Sopenharmony_ci			       stxx_spi4_stat.u64);
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	return 0;
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci/**
4178c2ecf20Sopenharmony_ci * Callback to perform clock detection
4188c2ecf20Sopenharmony_ci *
4198c2ecf20Sopenharmony_ci * @interface: The identifier of the packet interface to configure and
4208c2ecf20Sopenharmony_ci *		    use as a SPI interface.
4218c2ecf20Sopenharmony_ci * @mode:      The operating mode for the SPI interface. The interface
4228c2ecf20Sopenharmony_ci *		    can operate as a full duplex (both Tx and Rx data paths
4238c2ecf20Sopenharmony_ci *		    active) or as a halfplex (either the Tx data path is
4248c2ecf20Sopenharmony_ci *		    active or the Rx data path is active, but not both).
4258c2ecf20Sopenharmony_ci * @timeout:   Timeout to wait for clock synchronization in seconds
4268c2ecf20Sopenharmony_ci *
4278c2ecf20Sopenharmony_ci * Returns Zero on success, non-zero error code on failure (will cause
4288c2ecf20Sopenharmony_ci * SPI initialization to abort)
4298c2ecf20Sopenharmony_ci */
4308c2ecf20Sopenharmony_ciint cvmx_spi_clock_detect_cb(int interface, cvmx_spi_mode_t mode, int timeout)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	int clock_transitions;
4338c2ecf20Sopenharmony_ci	union cvmx_spxx_clk_stat stat;
4348c2ecf20Sopenharmony_ci	uint64_t timeout_time;
4358c2ecf20Sopenharmony_ci	uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/*
4388c2ecf20Sopenharmony_ci	 * Regardless of operating mode, both Tx and Rx clocks must be
4398c2ecf20Sopenharmony_ci	 * present for the SPI interface to operate.
4408c2ecf20Sopenharmony_ci	 */
4418c2ecf20Sopenharmony_ci	cvmx_dprintf("SPI%d: Waiting to see TsClk...\n", interface);
4428c2ecf20Sopenharmony_ci	timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
4438c2ecf20Sopenharmony_ci	/*
4448c2ecf20Sopenharmony_ci	 * Require 100 clock transitions in order to avoid any noise
4458c2ecf20Sopenharmony_ci	 * in the beginning.
4468c2ecf20Sopenharmony_ci	 */
4478c2ecf20Sopenharmony_ci	clock_transitions = 100;
4488c2ecf20Sopenharmony_ci	do {
4498c2ecf20Sopenharmony_ci		stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
4508c2ecf20Sopenharmony_ci		if (stat.s.s4clk0 && stat.s.s4clk1 && clock_transitions) {
4518c2ecf20Sopenharmony_ci			/*
4528c2ecf20Sopenharmony_ci			 * We've seen a clock transition, so decrement
4538c2ecf20Sopenharmony_ci			 * the number we still need.
4548c2ecf20Sopenharmony_ci			 */
4558c2ecf20Sopenharmony_ci			clock_transitions--;
4568c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
4578c2ecf20Sopenharmony_ci			stat.s.s4clk0 = 0;
4588c2ecf20Sopenharmony_ci			stat.s.s4clk1 = 0;
4598c2ecf20Sopenharmony_ci		}
4608c2ecf20Sopenharmony_ci		if (cvmx_get_cycle() > timeout_time) {
4618c2ecf20Sopenharmony_ci			cvmx_dprintf("SPI%d: Timeout\n", interface);
4628c2ecf20Sopenharmony_ci			return -1;
4638c2ecf20Sopenharmony_ci		}
4648c2ecf20Sopenharmony_ci	} while (stat.s.s4clk0 == 0 || stat.s.s4clk1 == 0);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	cvmx_dprintf("SPI%d: Waiting to see RsClk...\n", interface);
4678c2ecf20Sopenharmony_ci	timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
4688c2ecf20Sopenharmony_ci	/*
4698c2ecf20Sopenharmony_ci	 * Require 100 clock transitions in order to avoid any noise in the
4708c2ecf20Sopenharmony_ci	 * beginning.
4718c2ecf20Sopenharmony_ci	 */
4728c2ecf20Sopenharmony_ci	clock_transitions = 100;
4738c2ecf20Sopenharmony_ci	do {
4748c2ecf20Sopenharmony_ci		stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
4758c2ecf20Sopenharmony_ci		if (stat.s.d4clk0 && stat.s.d4clk1 && clock_transitions) {
4768c2ecf20Sopenharmony_ci			/*
4778c2ecf20Sopenharmony_ci			 * We've seen a clock transition, so decrement
4788c2ecf20Sopenharmony_ci			 * the number we still need
4798c2ecf20Sopenharmony_ci			 */
4808c2ecf20Sopenharmony_ci			clock_transitions--;
4818c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
4828c2ecf20Sopenharmony_ci			stat.s.d4clk0 = 0;
4838c2ecf20Sopenharmony_ci			stat.s.d4clk1 = 0;
4848c2ecf20Sopenharmony_ci		}
4858c2ecf20Sopenharmony_ci		if (cvmx_get_cycle() > timeout_time) {
4868c2ecf20Sopenharmony_ci			cvmx_dprintf("SPI%d: Timeout\n", interface);
4878c2ecf20Sopenharmony_ci			return -1;
4888c2ecf20Sopenharmony_ci		}
4898c2ecf20Sopenharmony_ci	} while (stat.s.d4clk0 == 0 || stat.s.d4clk1 == 0);
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	return 0;
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci/**
4958c2ecf20Sopenharmony_ci * Callback to perform link training
4968c2ecf20Sopenharmony_ci *
4978c2ecf20Sopenharmony_ci * @interface: The identifier of the packet interface to configure and
4988c2ecf20Sopenharmony_ci *		    use as a SPI interface.
4998c2ecf20Sopenharmony_ci * @mode:      The operating mode for the SPI interface. The interface
5008c2ecf20Sopenharmony_ci *		    can operate as a full duplex (both Tx and Rx data paths
5018c2ecf20Sopenharmony_ci *		    active) or as a halfplex (either the Tx data path is
5028c2ecf20Sopenharmony_ci *		    active or the Rx data path is active, but not both).
5038c2ecf20Sopenharmony_ci * @timeout:   Timeout to wait for link to be trained (in seconds)
5048c2ecf20Sopenharmony_ci *
5058c2ecf20Sopenharmony_ci * Returns Zero on success, non-zero error code on failure (will cause
5068c2ecf20Sopenharmony_ci * SPI initialization to abort)
5078c2ecf20Sopenharmony_ci */
5088c2ecf20Sopenharmony_ciint cvmx_spi_training_cb(int interface, cvmx_spi_mode_t mode, int timeout)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	union cvmx_spxx_trn4_ctl spxx_trn4_ctl;
5118c2ecf20Sopenharmony_ci	union cvmx_spxx_clk_stat stat;
5128c2ecf20Sopenharmony_ci	uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
5138c2ecf20Sopenharmony_ci	uint64_t timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
5148c2ecf20Sopenharmony_ci	int rx_training_needed;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	/* SRX0 & STX0 Inf0 Links are configured - begin training */
5178c2ecf20Sopenharmony_ci	union cvmx_spxx_clk_ctl spxx_clk_ctl;
5188c2ecf20Sopenharmony_ci	spxx_clk_ctl.u64 = 0;
5198c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.seetrn = 0;
5208c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.clkdly = 0x10;
5218c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.runbist = 0;
5228c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.statdrv = 0;
5238c2ecf20Sopenharmony_ci	/* This should always be on the opposite edge as statdrv */
5248c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.statrcv = 1;
5258c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.sndtrn = 1;
5268c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.drptrn = 1;
5278c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.rcvtrn = 1;
5288c2ecf20Sopenharmony_ci	spxx_clk_ctl.s.srxdlck = 1;
5298c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
5308c2ecf20Sopenharmony_ci	__delay(1000 * MS);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* SRX0 clear the boot bit */
5338c2ecf20Sopenharmony_ci	spxx_trn4_ctl.u64 = cvmx_read_csr(CVMX_SPXX_TRN4_CTL(interface));
5348c2ecf20Sopenharmony_ci	spxx_trn4_ctl.s.clr_boot = 1;
5358c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_SPXX_TRN4_CTL(interface), spxx_trn4_ctl.u64);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	/* Wait for the training sequence to complete */
5388c2ecf20Sopenharmony_ci	cvmx_dprintf("SPI%d: Waiting for training\n", interface);
5398c2ecf20Sopenharmony_ci	__delay(1000 * MS);
5408c2ecf20Sopenharmony_ci	/* Wait a really long time here */
5418c2ecf20Sopenharmony_ci	timeout_time = cvmx_get_cycle() + 1000ull * MS * 600;
5428c2ecf20Sopenharmony_ci	/*
5438c2ecf20Sopenharmony_ci	 * The HRM says we must wait for 34 + 16 * MAXDIST training sequences.
5448c2ecf20Sopenharmony_ci	 * We'll be pessimistic and wait for a lot more.
5458c2ecf20Sopenharmony_ci	 */
5468c2ecf20Sopenharmony_ci	rx_training_needed = 500;
5478c2ecf20Sopenharmony_ci	do {
5488c2ecf20Sopenharmony_ci		stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
5498c2ecf20Sopenharmony_ci		if (stat.s.srxtrn && rx_training_needed) {
5508c2ecf20Sopenharmony_ci			rx_training_needed--;
5518c2ecf20Sopenharmony_ci			cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
5528c2ecf20Sopenharmony_ci			stat.s.srxtrn = 0;
5538c2ecf20Sopenharmony_ci		}
5548c2ecf20Sopenharmony_ci		if (cvmx_get_cycle() > timeout_time) {
5558c2ecf20Sopenharmony_ci			cvmx_dprintf("SPI%d: Timeout\n", interface);
5568c2ecf20Sopenharmony_ci			return -1;
5578c2ecf20Sopenharmony_ci		}
5588c2ecf20Sopenharmony_ci	} while (stat.s.srxtrn == 0);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	return 0;
5618c2ecf20Sopenharmony_ci}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci/**
5648c2ecf20Sopenharmony_ci * Callback to perform calendar data synchronization
5658c2ecf20Sopenharmony_ci *
5668c2ecf20Sopenharmony_ci * @interface: The identifier of the packet interface to configure and
5678c2ecf20Sopenharmony_ci *		    use as a SPI interface.
5688c2ecf20Sopenharmony_ci * @mode:      The operating mode for the SPI interface. The interface
5698c2ecf20Sopenharmony_ci *		    can operate as a full duplex (both Tx and Rx data paths
5708c2ecf20Sopenharmony_ci *		    active) or as a halfplex (either the Tx data path is
5718c2ecf20Sopenharmony_ci *		    active or the Rx data path is active, but not both).
5728c2ecf20Sopenharmony_ci * @timeout:   Timeout to wait for calendar data in seconds
5738c2ecf20Sopenharmony_ci *
5748c2ecf20Sopenharmony_ci * Returns Zero on success, non-zero error code on failure (will cause
5758c2ecf20Sopenharmony_ci * SPI initialization to abort)
5768c2ecf20Sopenharmony_ci */
5778c2ecf20Sopenharmony_ciint cvmx_spi_calendar_sync_cb(int interface, cvmx_spi_mode_t mode, int timeout)
5788c2ecf20Sopenharmony_ci{
5798c2ecf20Sopenharmony_ci	uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
5808c2ecf20Sopenharmony_ci	if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
5818c2ecf20Sopenharmony_ci		/* SRX0 interface should be good, send calendar data */
5828c2ecf20Sopenharmony_ci		union cvmx_srxx_com_ctl srxx_com_ctl;
5838c2ecf20Sopenharmony_ci		cvmx_dprintf
5848c2ecf20Sopenharmony_ci		    ("SPI%d: Rx is synchronized, start sending calendar data\n",
5858c2ecf20Sopenharmony_ci		     interface);
5868c2ecf20Sopenharmony_ci		srxx_com_ctl.u64 = cvmx_read_csr(CVMX_SRXX_COM_CTL(interface));
5878c2ecf20Sopenharmony_ci		srxx_com_ctl.s.inf_en = 1;
5888c2ecf20Sopenharmony_ci		srxx_com_ctl.s.st_en = 1;
5898c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
5938c2ecf20Sopenharmony_ci		/* STX0 has achieved sync */
5948c2ecf20Sopenharmony_ci		/* The corespondant board should be sending calendar data */
5958c2ecf20Sopenharmony_ci		/* Enable the STX0 STAT receiver. */
5968c2ecf20Sopenharmony_ci		union cvmx_spxx_clk_stat stat;
5978c2ecf20Sopenharmony_ci		uint64_t timeout_time;
5988c2ecf20Sopenharmony_ci		union cvmx_stxx_com_ctl stxx_com_ctl;
5998c2ecf20Sopenharmony_ci		stxx_com_ctl.u64 = 0;
6008c2ecf20Sopenharmony_ci		stxx_com_ctl.s.st_en = 1;
6018c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_STXX_COM_CTL(interface), stxx_com_ctl.u64);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		/* Waiting for calendar sync on STX0 STAT */
6048c2ecf20Sopenharmony_ci		cvmx_dprintf("SPI%d: Waiting to sync on STX[%d] STAT\n",
6058c2ecf20Sopenharmony_ci			     interface, interface);
6068c2ecf20Sopenharmony_ci		timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
6078c2ecf20Sopenharmony_ci		/* SPX0_CLK_STAT - SPX0_CLK_STAT[STXCAL] should be 1 (bit10) */
6088c2ecf20Sopenharmony_ci		do {
6098c2ecf20Sopenharmony_ci			stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
6108c2ecf20Sopenharmony_ci			if (cvmx_get_cycle() > timeout_time) {
6118c2ecf20Sopenharmony_ci				cvmx_dprintf("SPI%d: Timeout\n", interface);
6128c2ecf20Sopenharmony_ci				return -1;
6138c2ecf20Sopenharmony_ci			}
6148c2ecf20Sopenharmony_ci		} while (stat.s.stxcal == 0);
6158c2ecf20Sopenharmony_ci	}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	return 0;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci/**
6218c2ecf20Sopenharmony_ci * Callback to handle interface up
6228c2ecf20Sopenharmony_ci *
6238c2ecf20Sopenharmony_ci * @interface: The identifier of the packet interface to configure and
6248c2ecf20Sopenharmony_ci *		    use as a SPI interface.
6258c2ecf20Sopenharmony_ci * @mode:      The operating mode for the SPI interface. The interface
6268c2ecf20Sopenharmony_ci *		    can operate as a full duplex (both Tx and Rx data paths
6278c2ecf20Sopenharmony_ci *		    active) or as a halfplex (either the Tx data path is
6288c2ecf20Sopenharmony_ci *		    active or the Rx data path is active, but not both).
6298c2ecf20Sopenharmony_ci *
6308c2ecf20Sopenharmony_ci * Returns Zero on success, non-zero error code on failure (will cause
6318c2ecf20Sopenharmony_ci * SPI initialization to abort)
6328c2ecf20Sopenharmony_ci */
6338c2ecf20Sopenharmony_ciint cvmx_spi_interface_up_cb(int interface, cvmx_spi_mode_t mode)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	union cvmx_gmxx_rxx_frm_min gmxx_rxx_frm_min;
6368c2ecf20Sopenharmony_ci	union cvmx_gmxx_rxx_frm_max gmxx_rxx_frm_max;
6378c2ecf20Sopenharmony_ci	union cvmx_gmxx_rxx_jabber gmxx_rxx_jabber;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
6408c2ecf20Sopenharmony_ci		union cvmx_srxx_com_ctl srxx_com_ctl;
6418c2ecf20Sopenharmony_ci		srxx_com_ctl.u64 = cvmx_read_csr(CVMX_SRXX_COM_CTL(interface));
6428c2ecf20Sopenharmony_ci		srxx_com_ctl.s.inf_en = 1;
6438c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
6448c2ecf20Sopenharmony_ci		cvmx_dprintf("SPI%d: Rx is now up\n", interface);
6458c2ecf20Sopenharmony_ci	}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
6488c2ecf20Sopenharmony_ci		union cvmx_stxx_com_ctl stxx_com_ctl;
6498c2ecf20Sopenharmony_ci		stxx_com_ctl.u64 = cvmx_read_csr(CVMX_STXX_COM_CTL(interface));
6508c2ecf20Sopenharmony_ci		stxx_com_ctl.s.inf_en = 1;
6518c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_STXX_COM_CTL(interface), stxx_com_ctl.u64);
6528c2ecf20Sopenharmony_ci		cvmx_dprintf("SPI%d: Tx is now up\n", interface);
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	gmxx_rxx_frm_min.u64 = 0;
6568c2ecf20Sopenharmony_ci	gmxx_rxx_frm_min.s.len = 64;
6578c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_RXX_FRM_MIN(0, interface),
6588c2ecf20Sopenharmony_ci		       gmxx_rxx_frm_min.u64);
6598c2ecf20Sopenharmony_ci	gmxx_rxx_frm_max.u64 = 0;
6608c2ecf20Sopenharmony_ci	gmxx_rxx_frm_max.s.len = 64 * 1024 - 4;
6618c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX(0, interface),
6628c2ecf20Sopenharmony_ci		       gmxx_rxx_frm_max.u64);
6638c2ecf20Sopenharmony_ci	gmxx_rxx_jabber.u64 = 0;
6648c2ecf20Sopenharmony_ci	gmxx_rxx_jabber.s.cnt = 64 * 1024 - 4;
6658c2ecf20Sopenharmony_ci	cvmx_write_csr(CVMX_GMXX_RXX_JABBER(0, interface), gmxx_rxx_jabber.u64);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	return 0;
6688c2ecf20Sopenharmony_ci}
669