xref: /OK3568_Linux_fs/kernel/arch/mips/cavium-octeon/executive/cvmx-spi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /***********************license start***************
2*4882a593Smuzhiyun  * Author: Cavium Networks
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Contact: support@caviumnetworks.com
5*4882a593Smuzhiyun  * This file is part of the OCTEON SDK
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2003-2008 Cavium Networks
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This file is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of the GNU General Public License, Version 2, as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This file is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15*4882a593Smuzhiyun  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16*4882a593Smuzhiyun  * NONINFRINGEMENT.  See the GNU General Public License for more
17*4882a593Smuzhiyun  * details.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
20*4882a593Smuzhiyun  * along with this file; if not, write to the Free Software
21*4882a593Smuzhiyun  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22*4882a593Smuzhiyun  * or visit http://www.gnu.org/licenses/.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * This file may also be available under a different license from Cavium.
25*4882a593Smuzhiyun  * Contact Cavium Networks for more information
26*4882a593Smuzhiyun  ***********************license end**************************************/
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * Support library for the SPI
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun #include <asm/octeon/octeon.h>
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <asm/octeon/cvmx-config.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <asm/octeon/cvmx-pko.h>
37*4882a593Smuzhiyun #include <asm/octeon/cvmx-spi.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include <asm/octeon/cvmx-spxx-defs.h>
40*4882a593Smuzhiyun #include <asm/octeon/cvmx-stxx-defs.h>
41*4882a593Smuzhiyun #include <asm/octeon/cvmx-srxx-defs.h>
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define INVOKE_CB(function_p, args...)		\
44*4882a593Smuzhiyun 	do {					\
45*4882a593Smuzhiyun 		if (function_p) {		\
46*4882a593Smuzhiyun 			res = function_p(args); \
47*4882a593Smuzhiyun 			if (res)		\
48*4882a593Smuzhiyun 				return res;	\
49*4882a593Smuzhiyun 		}				\
50*4882a593Smuzhiyun 	} while (0)
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #if CVMX_ENABLE_DEBUG_PRINTS
53*4882a593Smuzhiyun static const char *modes[] =
54*4882a593Smuzhiyun     { "UNKNOWN", "TX Halfplex", "Rx Halfplex", "Duplex" };
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /* Default callbacks, can be overridden
58*4882a593Smuzhiyun  *  using cvmx_spi_get_callbacks/cvmx_spi_set_callbacks
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun static cvmx_spi_callbacks_t cvmx_spi_callbacks = {
61*4882a593Smuzhiyun 	.reset_cb = cvmx_spi_reset_cb,
62*4882a593Smuzhiyun 	.calendar_setup_cb = cvmx_spi_calendar_setup_cb,
63*4882a593Smuzhiyun 	.clock_detect_cb = cvmx_spi_clock_detect_cb,
64*4882a593Smuzhiyun 	.training_cb = cvmx_spi_training_cb,
65*4882a593Smuzhiyun 	.calendar_sync_cb = cvmx_spi_calendar_sync_cb,
66*4882a593Smuzhiyun 	.interface_up_cb = cvmx_spi_interface_up_cb
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /**
70*4882a593Smuzhiyun  * Get current SPI4 initialization callbacks
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * @callbacks:	Pointer to the callbacks structure.to fill
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * Returns Pointer to cvmx_spi_callbacks_t structure.
75*4882a593Smuzhiyun  */
cvmx_spi_get_callbacks(cvmx_spi_callbacks_t * callbacks)76*4882a593Smuzhiyun void cvmx_spi_get_callbacks(cvmx_spi_callbacks_t *callbacks)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	memcpy(callbacks, &cvmx_spi_callbacks, sizeof(cvmx_spi_callbacks));
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /**
82*4882a593Smuzhiyun  * Set new SPI4 initialization callbacks
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * @new_callbacks:  Pointer to an updated callbacks structure.
85*4882a593Smuzhiyun  */
cvmx_spi_set_callbacks(cvmx_spi_callbacks_t * new_callbacks)86*4882a593Smuzhiyun void cvmx_spi_set_callbacks(cvmx_spi_callbacks_t *new_callbacks)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	memcpy(&cvmx_spi_callbacks, new_callbacks, sizeof(cvmx_spi_callbacks));
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun  * Initialize and start the SPI interface.
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * @interface: The identifier of the packet interface to configure and
95*4882a593Smuzhiyun  *		    use as a SPI interface.
96*4882a593Smuzhiyun  * @mode:      The operating mode for the SPI interface. The interface
97*4882a593Smuzhiyun  *		    can operate as a full duplex (both Tx and Rx data paths
98*4882a593Smuzhiyun  *		    active) or as a halfplex (either the Tx data path is
99*4882a593Smuzhiyun  *		    active or the Rx data path is active, but not both).
100*4882a593Smuzhiyun  * @timeout:   Timeout to wait for clock synchronization in seconds
101*4882a593Smuzhiyun  * @num_ports: Number of SPI ports to configure
102*4882a593Smuzhiyun  *
103*4882a593Smuzhiyun  * Returns Zero on success, negative of failure.
104*4882a593Smuzhiyun  */
cvmx_spi_start_interface(int interface,cvmx_spi_mode_t mode,int timeout,int num_ports)105*4882a593Smuzhiyun int cvmx_spi_start_interface(int interface, cvmx_spi_mode_t mode, int timeout,
106*4882a593Smuzhiyun 			     int num_ports)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	int res = -1;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))
111*4882a593Smuzhiyun 		return res;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* Callback to perform SPI4 reset */
114*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.reset_cb, interface, mode);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	/* Callback to perform calendar setup */
117*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.calendar_setup_cb, interface, mode,
118*4882a593Smuzhiyun 		  num_ports);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	/* Callback to perform clock detection */
121*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.clock_detect_cb, interface, mode, timeout);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/* Callback to perform SPI4 link training */
124*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.training_cb, interface, mode, timeout);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/* Callback to perform calendar sync */
127*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.calendar_sync_cb, interface, mode,
128*4882a593Smuzhiyun 		  timeout);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	/* Callback to handle interface coming up */
131*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.interface_up_cb, interface, mode);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	return res;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun  * This routine restarts the SPI interface after it has lost synchronization
138*4882a593Smuzhiyun  * with its correspondent system.
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * @interface: The identifier of the packet interface to configure and
141*4882a593Smuzhiyun  *		    use as a SPI interface.
142*4882a593Smuzhiyun  * @mode:      The operating mode for the SPI interface. The interface
143*4882a593Smuzhiyun  *		    can operate as a full duplex (both Tx and Rx data paths
144*4882a593Smuzhiyun  *		    active) or as a halfplex (either the Tx data path is
145*4882a593Smuzhiyun  *		    active or the Rx data path is active, but not both).
146*4882a593Smuzhiyun  * @timeout:   Timeout to wait for clock synchronization in seconds
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * Returns Zero on success, negative of failure.
149*4882a593Smuzhiyun  */
cvmx_spi_restart_interface(int interface,cvmx_spi_mode_t mode,int timeout)150*4882a593Smuzhiyun int cvmx_spi_restart_interface(int interface, cvmx_spi_mode_t mode, int timeout)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	int res = -1;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))
155*4882a593Smuzhiyun 		return res;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	cvmx_dprintf("SPI%d: Restart %s\n", interface, modes[mode]);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* Callback to perform SPI4 reset */
160*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.reset_cb, interface, mode);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	/* NOTE: Calendar setup is not performed during restart */
163*4882a593Smuzhiyun 	/*	 Refer to cvmx_spi_start_interface() for the full sequence */
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* Callback to perform clock detection */
166*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.clock_detect_cb, interface, mode, timeout);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/* Callback to perform SPI4 link training */
169*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.training_cb, interface, mode, timeout);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/* Callback to perform calendar sync */
172*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.calendar_sync_cb, interface, mode,
173*4882a593Smuzhiyun 		  timeout);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	/* Callback to handle interface coming up */
176*4882a593Smuzhiyun 	INVOKE_CB(cvmx_spi_callbacks.interface_up_cb, interface, mode);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	return res;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cvmx_spi_restart_interface);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun  * Callback to perform SPI4 reset
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  * @interface: The identifier of the packet interface to configure and
186*4882a593Smuzhiyun  *		    use as a SPI interface.
187*4882a593Smuzhiyun  * @mode:      The operating mode for the SPI interface. The interface
188*4882a593Smuzhiyun  *		    can operate as a full duplex (both Tx and Rx data paths
189*4882a593Smuzhiyun  *		    active) or as a halfplex (either the Tx data path is
190*4882a593Smuzhiyun  *		    active or the Rx data path is active, but not both).
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * Returns Zero on success, non-zero error code on failure (will cause
193*4882a593Smuzhiyun  * SPI initialization to abort)
194*4882a593Smuzhiyun  */
cvmx_spi_reset_cb(int interface,cvmx_spi_mode_t mode)195*4882a593Smuzhiyun int cvmx_spi_reset_cb(int interface, cvmx_spi_mode_t mode)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	union cvmx_spxx_dbg_deskew_ctl spxx_dbg_deskew_ctl;
198*4882a593Smuzhiyun 	union cvmx_spxx_clk_ctl spxx_clk_ctl;
199*4882a593Smuzhiyun 	union cvmx_spxx_bist_stat spxx_bist_stat;
200*4882a593Smuzhiyun 	union cvmx_spxx_int_msk spxx_int_msk;
201*4882a593Smuzhiyun 	union cvmx_stxx_int_msk stxx_int_msk;
202*4882a593Smuzhiyun 	union cvmx_spxx_trn4_ctl spxx_trn4_ctl;
203*4882a593Smuzhiyun 	int index;
204*4882a593Smuzhiyun 	uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/* Disable SPI error events while we run BIST */
207*4882a593Smuzhiyun 	spxx_int_msk.u64 = cvmx_read_csr(CVMX_SPXX_INT_MSK(interface));
208*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), 0);
209*4882a593Smuzhiyun 	stxx_int_msk.u64 = cvmx_read_csr(CVMX_STXX_INT_MSK(interface));
210*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_STXX_INT_MSK(interface), 0);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/* Run BIST in the SPI interface */
213*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), 0);
214*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_STXX_COM_CTL(interface), 0);
215*4882a593Smuzhiyun 	spxx_clk_ctl.u64 = 0;
216*4882a593Smuzhiyun 	spxx_clk_ctl.s.runbist = 1;
217*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
218*4882a593Smuzhiyun 	__delay(10 * MS);
219*4882a593Smuzhiyun 	spxx_bist_stat.u64 = cvmx_read_csr(CVMX_SPXX_BIST_STAT(interface));
220*4882a593Smuzhiyun 	if (spxx_bist_stat.s.stat0)
221*4882a593Smuzhiyun 		cvmx_dprintf
222*4882a593Smuzhiyun 		    ("ERROR SPI%d: BIST failed on receive datapath FIFO\n",
223*4882a593Smuzhiyun 		     interface);
224*4882a593Smuzhiyun 	if (spxx_bist_stat.s.stat1)
225*4882a593Smuzhiyun 		cvmx_dprintf("ERROR SPI%d: BIST failed on RX calendar table\n",
226*4882a593Smuzhiyun 			     interface);
227*4882a593Smuzhiyun 	if (spxx_bist_stat.s.stat2)
228*4882a593Smuzhiyun 		cvmx_dprintf("ERROR SPI%d: BIST failed on TX calendar table\n",
229*4882a593Smuzhiyun 			     interface);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* Clear the calendar table after BIST to fix parity errors */
232*4882a593Smuzhiyun 	for (index = 0; index < 32; index++) {
233*4882a593Smuzhiyun 		union cvmx_srxx_spi4_calx srxx_spi4_calx;
234*4882a593Smuzhiyun 		union cvmx_stxx_spi4_calx stxx_spi4_calx;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		srxx_spi4_calx.u64 = 0;
237*4882a593Smuzhiyun 		srxx_spi4_calx.s.oddpar = 1;
238*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_SRXX_SPI4_CALX(index, interface),
239*4882a593Smuzhiyun 			       srxx_spi4_calx.u64);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 		stxx_spi4_calx.u64 = 0;
242*4882a593Smuzhiyun 		stxx_spi4_calx.s.oddpar = 1;
243*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_STXX_SPI4_CALX(index, interface),
244*4882a593Smuzhiyun 			       stxx_spi4_calx.u64);
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* Re enable reporting of error interrupts */
248*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_INT_REG(interface),
249*4882a593Smuzhiyun 		       cvmx_read_csr(CVMX_SPXX_INT_REG(interface)));
250*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), spxx_int_msk.u64);
251*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_STXX_INT_REG(interface),
252*4882a593Smuzhiyun 		       cvmx_read_csr(CVMX_STXX_INT_REG(interface)));
253*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_STXX_INT_MSK(interface), stxx_int_msk.u64);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/* Setup the CLKDLY right in the middle */
256*4882a593Smuzhiyun 	spxx_clk_ctl.u64 = 0;
257*4882a593Smuzhiyun 	spxx_clk_ctl.s.seetrn = 0;
258*4882a593Smuzhiyun 	spxx_clk_ctl.s.clkdly = 0x10;
259*4882a593Smuzhiyun 	spxx_clk_ctl.s.runbist = 0;
260*4882a593Smuzhiyun 	spxx_clk_ctl.s.statdrv = 0;
261*4882a593Smuzhiyun 	/* This should always be on the opposite edge as statdrv */
262*4882a593Smuzhiyun 	spxx_clk_ctl.s.statrcv = 1;
263*4882a593Smuzhiyun 	spxx_clk_ctl.s.sndtrn = 0;
264*4882a593Smuzhiyun 	spxx_clk_ctl.s.drptrn = 0;
265*4882a593Smuzhiyun 	spxx_clk_ctl.s.rcvtrn = 0;
266*4882a593Smuzhiyun 	spxx_clk_ctl.s.srxdlck = 0;
267*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
268*4882a593Smuzhiyun 	__delay(100 * MS);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* Reset SRX0 DLL */
271*4882a593Smuzhiyun 	spxx_clk_ctl.s.srxdlck = 1;
272*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* Waiting for Inf0 Spi4 RX DLL to lock */
275*4882a593Smuzhiyun 	__delay(100 * MS);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	/* Enable dynamic alignment */
278*4882a593Smuzhiyun 	spxx_trn4_ctl.s.trntest = 0;
279*4882a593Smuzhiyun 	spxx_trn4_ctl.s.jitter = 1;
280*4882a593Smuzhiyun 	spxx_trn4_ctl.s.clr_boot = 1;
281*4882a593Smuzhiyun 	spxx_trn4_ctl.s.set_boot = 0;
282*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN58XX))
283*4882a593Smuzhiyun 		spxx_trn4_ctl.s.maxdist = 3;
284*4882a593Smuzhiyun 	else
285*4882a593Smuzhiyun 		spxx_trn4_ctl.s.maxdist = 8;
286*4882a593Smuzhiyun 	spxx_trn4_ctl.s.macro_en = 1;
287*4882a593Smuzhiyun 	spxx_trn4_ctl.s.mux_en = 1;
288*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_TRN4_CTL(interface), spxx_trn4_ctl.u64);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	spxx_dbg_deskew_ctl.u64 = 0;
291*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_DBG_DESKEW_CTL(interface),
292*4882a593Smuzhiyun 		       spxx_dbg_deskew_ctl.u64);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	return 0;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun /**
298*4882a593Smuzhiyun  * Callback to setup calendar and miscellaneous settings before clock detection
299*4882a593Smuzhiyun  *
300*4882a593Smuzhiyun  * @interface: The identifier of the packet interface to configure and
301*4882a593Smuzhiyun  *		    use as a SPI interface.
302*4882a593Smuzhiyun  * @mode:      The operating mode for the SPI interface. The interface
303*4882a593Smuzhiyun  *		    can operate as a full duplex (both Tx and Rx data paths
304*4882a593Smuzhiyun  *		    active) or as a halfplex (either the Tx data path is
305*4882a593Smuzhiyun  *		    active or the Rx data path is active, but not both).
306*4882a593Smuzhiyun  * @num_ports: Number of ports to configure on SPI
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * Returns Zero on success, non-zero error code on failure (will cause
309*4882a593Smuzhiyun  * SPI initialization to abort)
310*4882a593Smuzhiyun  */
cvmx_spi_calendar_setup_cb(int interface,cvmx_spi_mode_t mode,int num_ports)311*4882a593Smuzhiyun int cvmx_spi_calendar_setup_cb(int interface, cvmx_spi_mode_t mode,
312*4882a593Smuzhiyun 			       int num_ports)
313*4882a593Smuzhiyun {
314*4882a593Smuzhiyun 	int port;
315*4882a593Smuzhiyun 	int index;
316*4882a593Smuzhiyun 	if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
317*4882a593Smuzhiyun 		union cvmx_srxx_com_ctl srxx_com_ctl;
318*4882a593Smuzhiyun 		union cvmx_srxx_spi4_stat srxx_spi4_stat;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		/* SRX0 number of Ports */
321*4882a593Smuzhiyun 		srxx_com_ctl.u64 = 0;
322*4882a593Smuzhiyun 		srxx_com_ctl.s.prts = num_ports - 1;
323*4882a593Smuzhiyun 		srxx_com_ctl.s.st_en = 0;
324*4882a593Smuzhiyun 		srxx_com_ctl.s.inf_en = 0;
325*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 		/* SRX0 Calendar Table. This round robbins through all ports */
328*4882a593Smuzhiyun 		port = 0;
329*4882a593Smuzhiyun 		index = 0;
330*4882a593Smuzhiyun 		while (port < num_ports) {
331*4882a593Smuzhiyun 			union cvmx_srxx_spi4_calx srxx_spi4_calx;
332*4882a593Smuzhiyun 			srxx_spi4_calx.u64 = 0;
333*4882a593Smuzhiyun 			srxx_spi4_calx.s.prt0 = port++;
334*4882a593Smuzhiyun 			srxx_spi4_calx.s.prt1 = port++;
335*4882a593Smuzhiyun 			srxx_spi4_calx.s.prt2 = port++;
336*4882a593Smuzhiyun 			srxx_spi4_calx.s.prt3 = port++;
337*4882a593Smuzhiyun 			srxx_spi4_calx.s.oddpar =
338*4882a593Smuzhiyun 			    ~(cvmx_dpop(srxx_spi4_calx.u64) & 1);
339*4882a593Smuzhiyun 			cvmx_write_csr(CVMX_SRXX_SPI4_CALX(index, interface),
340*4882a593Smuzhiyun 				       srxx_spi4_calx.u64);
341*4882a593Smuzhiyun 			index++;
342*4882a593Smuzhiyun 		}
343*4882a593Smuzhiyun 		srxx_spi4_stat.u64 = 0;
344*4882a593Smuzhiyun 		srxx_spi4_stat.s.len = num_ports;
345*4882a593Smuzhiyun 		srxx_spi4_stat.s.m = 1;
346*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_SRXX_SPI4_STAT(interface),
347*4882a593Smuzhiyun 			       srxx_spi4_stat.u64);
348*4882a593Smuzhiyun 	}
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
351*4882a593Smuzhiyun 		union cvmx_stxx_arb_ctl stxx_arb_ctl;
352*4882a593Smuzhiyun 		union cvmx_gmxx_tx_spi_max gmxx_tx_spi_max;
353*4882a593Smuzhiyun 		union cvmx_gmxx_tx_spi_thresh gmxx_tx_spi_thresh;
354*4882a593Smuzhiyun 		union cvmx_gmxx_tx_spi_ctl gmxx_tx_spi_ctl;
355*4882a593Smuzhiyun 		union cvmx_stxx_spi4_stat stxx_spi4_stat;
356*4882a593Smuzhiyun 		union cvmx_stxx_spi4_dat stxx_spi4_dat;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		/* STX0 Config */
359*4882a593Smuzhiyun 		stxx_arb_ctl.u64 = 0;
360*4882a593Smuzhiyun 		stxx_arb_ctl.s.igntpa = 0;
361*4882a593Smuzhiyun 		stxx_arb_ctl.s.mintrn = 0;
362*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_STXX_ARB_CTL(interface), stxx_arb_ctl.u64);
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 		gmxx_tx_spi_max.u64 = 0;
365*4882a593Smuzhiyun 		gmxx_tx_spi_max.s.max1 = 8;
366*4882a593Smuzhiyun 		gmxx_tx_spi_max.s.max2 = 4;
367*4882a593Smuzhiyun 		gmxx_tx_spi_max.s.slice = 0;
368*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_GMXX_TX_SPI_MAX(interface),
369*4882a593Smuzhiyun 			       gmxx_tx_spi_max.u64);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 		gmxx_tx_spi_thresh.u64 = 0;
372*4882a593Smuzhiyun 		gmxx_tx_spi_thresh.s.thresh = 4;
373*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_GMXX_TX_SPI_THRESH(interface),
374*4882a593Smuzhiyun 			       gmxx_tx_spi_thresh.u64);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 		gmxx_tx_spi_ctl.u64 = 0;
377*4882a593Smuzhiyun 		gmxx_tx_spi_ctl.s.tpa_clr = 0;
378*4882a593Smuzhiyun 		gmxx_tx_spi_ctl.s.cont_pkt = 0;
379*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_GMXX_TX_SPI_CTL(interface),
380*4882a593Smuzhiyun 			       gmxx_tx_spi_ctl.u64);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 		/* STX0 Training Control */
383*4882a593Smuzhiyun 		stxx_spi4_dat.u64 = 0;
384*4882a593Smuzhiyun 		/*Minimum needed by dynamic alignment */
385*4882a593Smuzhiyun 		stxx_spi4_dat.s.alpha = 32;
386*4882a593Smuzhiyun 		stxx_spi4_dat.s.max_t = 0xFFFF; /*Minimum interval is 0x20 */
387*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_STXX_SPI4_DAT(interface),
388*4882a593Smuzhiyun 			       stxx_spi4_dat.u64);
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 		/* STX0 Calendar Table. This round robbins through all ports */
391*4882a593Smuzhiyun 		port = 0;
392*4882a593Smuzhiyun 		index = 0;
393*4882a593Smuzhiyun 		while (port < num_ports) {
394*4882a593Smuzhiyun 			union cvmx_stxx_spi4_calx stxx_spi4_calx;
395*4882a593Smuzhiyun 			stxx_spi4_calx.u64 = 0;
396*4882a593Smuzhiyun 			stxx_spi4_calx.s.prt0 = port++;
397*4882a593Smuzhiyun 			stxx_spi4_calx.s.prt1 = port++;
398*4882a593Smuzhiyun 			stxx_spi4_calx.s.prt2 = port++;
399*4882a593Smuzhiyun 			stxx_spi4_calx.s.prt3 = port++;
400*4882a593Smuzhiyun 			stxx_spi4_calx.s.oddpar =
401*4882a593Smuzhiyun 			    ~(cvmx_dpop(stxx_spi4_calx.u64) & 1);
402*4882a593Smuzhiyun 			cvmx_write_csr(CVMX_STXX_SPI4_CALX(index, interface),
403*4882a593Smuzhiyun 				       stxx_spi4_calx.u64);
404*4882a593Smuzhiyun 			index++;
405*4882a593Smuzhiyun 		}
406*4882a593Smuzhiyun 		stxx_spi4_stat.u64 = 0;
407*4882a593Smuzhiyun 		stxx_spi4_stat.s.len = num_ports;
408*4882a593Smuzhiyun 		stxx_spi4_stat.s.m = 1;
409*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_STXX_SPI4_STAT(interface),
410*4882a593Smuzhiyun 			       stxx_spi4_stat.u64);
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	return 0;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun /**
417*4882a593Smuzhiyun  * Callback to perform clock detection
418*4882a593Smuzhiyun  *
419*4882a593Smuzhiyun  * @interface: The identifier of the packet interface to configure and
420*4882a593Smuzhiyun  *		    use as a SPI interface.
421*4882a593Smuzhiyun  * @mode:      The operating mode for the SPI interface. The interface
422*4882a593Smuzhiyun  *		    can operate as a full duplex (both Tx and Rx data paths
423*4882a593Smuzhiyun  *		    active) or as a halfplex (either the Tx data path is
424*4882a593Smuzhiyun  *		    active or the Rx data path is active, but not both).
425*4882a593Smuzhiyun  * @timeout:   Timeout to wait for clock synchronization in seconds
426*4882a593Smuzhiyun  *
427*4882a593Smuzhiyun  * Returns Zero on success, non-zero error code on failure (will cause
428*4882a593Smuzhiyun  * SPI initialization to abort)
429*4882a593Smuzhiyun  */
cvmx_spi_clock_detect_cb(int interface,cvmx_spi_mode_t mode,int timeout)430*4882a593Smuzhiyun int cvmx_spi_clock_detect_cb(int interface, cvmx_spi_mode_t mode, int timeout)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun 	int clock_transitions;
433*4882a593Smuzhiyun 	union cvmx_spxx_clk_stat stat;
434*4882a593Smuzhiyun 	uint64_t timeout_time;
435*4882a593Smuzhiyun 	uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	/*
438*4882a593Smuzhiyun 	 * Regardless of operating mode, both Tx and Rx clocks must be
439*4882a593Smuzhiyun 	 * present for the SPI interface to operate.
440*4882a593Smuzhiyun 	 */
441*4882a593Smuzhiyun 	cvmx_dprintf("SPI%d: Waiting to see TsClk...\n", interface);
442*4882a593Smuzhiyun 	timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
443*4882a593Smuzhiyun 	/*
444*4882a593Smuzhiyun 	 * Require 100 clock transitions in order to avoid any noise
445*4882a593Smuzhiyun 	 * in the beginning.
446*4882a593Smuzhiyun 	 */
447*4882a593Smuzhiyun 	clock_transitions = 100;
448*4882a593Smuzhiyun 	do {
449*4882a593Smuzhiyun 		stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
450*4882a593Smuzhiyun 		if (stat.s.s4clk0 && stat.s.s4clk1 && clock_transitions) {
451*4882a593Smuzhiyun 			/*
452*4882a593Smuzhiyun 			 * We've seen a clock transition, so decrement
453*4882a593Smuzhiyun 			 * the number we still need.
454*4882a593Smuzhiyun 			 */
455*4882a593Smuzhiyun 			clock_transitions--;
456*4882a593Smuzhiyun 			cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
457*4882a593Smuzhiyun 			stat.s.s4clk0 = 0;
458*4882a593Smuzhiyun 			stat.s.s4clk1 = 0;
459*4882a593Smuzhiyun 		}
460*4882a593Smuzhiyun 		if (cvmx_get_cycle() > timeout_time) {
461*4882a593Smuzhiyun 			cvmx_dprintf("SPI%d: Timeout\n", interface);
462*4882a593Smuzhiyun 			return -1;
463*4882a593Smuzhiyun 		}
464*4882a593Smuzhiyun 	} while (stat.s.s4clk0 == 0 || stat.s.s4clk1 == 0);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	cvmx_dprintf("SPI%d: Waiting to see RsClk...\n", interface);
467*4882a593Smuzhiyun 	timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
468*4882a593Smuzhiyun 	/*
469*4882a593Smuzhiyun 	 * Require 100 clock transitions in order to avoid any noise in the
470*4882a593Smuzhiyun 	 * beginning.
471*4882a593Smuzhiyun 	 */
472*4882a593Smuzhiyun 	clock_transitions = 100;
473*4882a593Smuzhiyun 	do {
474*4882a593Smuzhiyun 		stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
475*4882a593Smuzhiyun 		if (stat.s.d4clk0 && stat.s.d4clk1 && clock_transitions) {
476*4882a593Smuzhiyun 			/*
477*4882a593Smuzhiyun 			 * We've seen a clock transition, so decrement
478*4882a593Smuzhiyun 			 * the number we still need
479*4882a593Smuzhiyun 			 */
480*4882a593Smuzhiyun 			clock_transitions--;
481*4882a593Smuzhiyun 			cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
482*4882a593Smuzhiyun 			stat.s.d4clk0 = 0;
483*4882a593Smuzhiyun 			stat.s.d4clk1 = 0;
484*4882a593Smuzhiyun 		}
485*4882a593Smuzhiyun 		if (cvmx_get_cycle() > timeout_time) {
486*4882a593Smuzhiyun 			cvmx_dprintf("SPI%d: Timeout\n", interface);
487*4882a593Smuzhiyun 			return -1;
488*4882a593Smuzhiyun 		}
489*4882a593Smuzhiyun 	} while (stat.s.d4clk0 == 0 || stat.s.d4clk1 == 0);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	return 0;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun /**
495*4882a593Smuzhiyun  * Callback to perform link training
496*4882a593Smuzhiyun  *
497*4882a593Smuzhiyun  * @interface: The identifier of the packet interface to configure and
498*4882a593Smuzhiyun  *		    use as a SPI interface.
499*4882a593Smuzhiyun  * @mode:      The operating mode for the SPI interface. The interface
500*4882a593Smuzhiyun  *		    can operate as a full duplex (both Tx and Rx data paths
501*4882a593Smuzhiyun  *		    active) or as a halfplex (either the Tx data path is
502*4882a593Smuzhiyun  *		    active or the Rx data path is active, but not both).
503*4882a593Smuzhiyun  * @timeout:   Timeout to wait for link to be trained (in seconds)
504*4882a593Smuzhiyun  *
505*4882a593Smuzhiyun  * Returns Zero on success, non-zero error code on failure (will cause
506*4882a593Smuzhiyun  * SPI initialization to abort)
507*4882a593Smuzhiyun  */
cvmx_spi_training_cb(int interface,cvmx_spi_mode_t mode,int timeout)508*4882a593Smuzhiyun int cvmx_spi_training_cb(int interface, cvmx_spi_mode_t mode, int timeout)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	union cvmx_spxx_trn4_ctl spxx_trn4_ctl;
511*4882a593Smuzhiyun 	union cvmx_spxx_clk_stat stat;
512*4882a593Smuzhiyun 	uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
513*4882a593Smuzhiyun 	uint64_t timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
514*4882a593Smuzhiyun 	int rx_training_needed;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	/* SRX0 & STX0 Inf0 Links are configured - begin training */
517*4882a593Smuzhiyun 	union cvmx_spxx_clk_ctl spxx_clk_ctl;
518*4882a593Smuzhiyun 	spxx_clk_ctl.u64 = 0;
519*4882a593Smuzhiyun 	spxx_clk_ctl.s.seetrn = 0;
520*4882a593Smuzhiyun 	spxx_clk_ctl.s.clkdly = 0x10;
521*4882a593Smuzhiyun 	spxx_clk_ctl.s.runbist = 0;
522*4882a593Smuzhiyun 	spxx_clk_ctl.s.statdrv = 0;
523*4882a593Smuzhiyun 	/* This should always be on the opposite edge as statdrv */
524*4882a593Smuzhiyun 	spxx_clk_ctl.s.statrcv = 1;
525*4882a593Smuzhiyun 	spxx_clk_ctl.s.sndtrn = 1;
526*4882a593Smuzhiyun 	spxx_clk_ctl.s.drptrn = 1;
527*4882a593Smuzhiyun 	spxx_clk_ctl.s.rcvtrn = 1;
528*4882a593Smuzhiyun 	spxx_clk_ctl.s.srxdlck = 1;
529*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
530*4882a593Smuzhiyun 	__delay(1000 * MS);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	/* SRX0 clear the boot bit */
533*4882a593Smuzhiyun 	spxx_trn4_ctl.u64 = cvmx_read_csr(CVMX_SPXX_TRN4_CTL(interface));
534*4882a593Smuzhiyun 	spxx_trn4_ctl.s.clr_boot = 1;
535*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_SPXX_TRN4_CTL(interface), spxx_trn4_ctl.u64);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	/* Wait for the training sequence to complete */
538*4882a593Smuzhiyun 	cvmx_dprintf("SPI%d: Waiting for training\n", interface);
539*4882a593Smuzhiyun 	__delay(1000 * MS);
540*4882a593Smuzhiyun 	/* Wait a really long time here */
541*4882a593Smuzhiyun 	timeout_time = cvmx_get_cycle() + 1000ull * MS * 600;
542*4882a593Smuzhiyun 	/*
543*4882a593Smuzhiyun 	 * The HRM says we must wait for 34 + 16 * MAXDIST training sequences.
544*4882a593Smuzhiyun 	 * We'll be pessimistic and wait for a lot more.
545*4882a593Smuzhiyun 	 */
546*4882a593Smuzhiyun 	rx_training_needed = 500;
547*4882a593Smuzhiyun 	do {
548*4882a593Smuzhiyun 		stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
549*4882a593Smuzhiyun 		if (stat.s.srxtrn && rx_training_needed) {
550*4882a593Smuzhiyun 			rx_training_needed--;
551*4882a593Smuzhiyun 			cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
552*4882a593Smuzhiyun 			stat.s.srxtrn = 0;
553*4882a593Smuzhiyun 		}
554*4882a593Smuzhiyun 		if (cvmx_get_cycle() > timeout_time) {
555*4882a593Smuzhiyun 			cvmx_dprintf("SPI%d: Timeout\n", interface);
556*4882a593Smuzhiyun 			return -1;
557*4882a593Smuzhiyun 		}
558*4882a593Smuzhiyun 	} while (stat.s.srxtrn == 0);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	return 0;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun /**
564*4882a593Smuzhiyun  * Callback to perform calendar data synchronization
565*4882a593Smuzhiyun  *
566*4882a593Smuzhiyun  * @interface: The identifier of the packet interface to configure and
567*4882a593Smuzhiyun  *		    use as a SPI interface.
568*4882a593Smuzhiyun  * @mode:      The operating mode for the SPI interface. The interface
569*4882a593Smuzhiyun  *		    can operate as a full duplex (both Tx and Rx data paths
570*4882a593Smuzhiyun  *		    active) or as a halfplex (either the Tx data path is
571*4882a593Smuzhiyun  *		    active or the Rx data path is active, but not both).
572*4882a593Smuzhiyun  * @timeout:   Timeout to wait for calendar data in seconds
573*4882a593Smuzhiyun  *
574*4882a593Smuzhiyun  * Returns Zero on success, non-zero error code on failure (will cause
575*4882a593Smuzhiyun  * SPI initialization to abort)
576*4882a593Smuzhiyun  */
cvmx_spi_calendar_sync_cb(int interface,cvmx_spi_mode_t mode,int timeout)577*4882a593Smuzhiyun int cvmx_spi_calendar_sync_cb(int interface, cvmx_spi_mode_t mode, int timeout)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun 	uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
580*4882a593Smuzhiyun 	if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
581*4882a593Smuzhiyun 		/* SRX0 interface should be good, send calendar data */
582*4882a593Smuzhiyun 		union cvmx_srxx_com_ctl srxx_com_ctl;
583*4882a593Smuzhiyun 		cvmx_dprintf
584*4882a593Smuzhiyun 		    ("SPI%d: Rx is synchronized, start sending calendar data\n",
585*4882a593Smuzhiyun 		     interface);
586*4882a593Smuzhiyun 		srxx_com_ctl.u64 = cvmx_read_csr(CVMX_SRXX_COM_CTL(interface));
587*4882a593Smuzhiyun 		srxx_com_ctl.s.inf_en = 1;
588*4882a593Smuzhiyun 		srxx_com_ctl.s.st_en = 1;
589*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
593*4882a593Smuzhiyun 		/* STX0 has achieved sync */
594*4882a593Smuzhiyun 		/* The corespondant board should be sending calendar data */
595*4882a593Smuzhiyun 		/* Enable the STX0 STAT receiver. */
596*4882a593Smuzhiyun 		union cvmx_spxx_clk_stat stat;
597*4882a593Smuzhiyun 		uint64_t timeout_time;
598*4882a593Smuzhiyun 		union cvmx_stxx_com_ctl stxx_com_ctl;
599*4882a593Smuzhiyun 		stxx_com_ctl.u64 = 0;
600*4882a593Smuzhiyun 		stxx_com_ctl.s.st_en = 1;
601*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_STXX_COM_CTL(interface), stxx_com_ctl.u64);
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 		/* Waiting for calendar sync on STX0 STAT */
604*4882a593Smuzhiyun 		cvmx_dprintf("SPI%d: Waiting to sync on STX[%d] STAT\n",
605*4882a593Smuzhiyun 			     interface, interface);
606*4882a593Smuzhiyun 		timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
607*4882a593Smuzhiyun 		/* SPX0_CLK_STAT - SPX0_CLK_STAT[STXCAL] should be 1 (bit10) */
608*4882a593Smuzhiyun 		do {
609*4882a593Smuzhiyun 			stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
610*4882a593Smuzhiyun 			if (cvmx_get_cycle() > timeout_time) {
611*4882a593Smuzhiyun 				cvmx_dprintf("SPI%d: Timeout\n", interface);
612*4882a593Smuzhiyun 				return -1;
613*4882a593Smuzhiyun 			}
614*4882a593Smuzhiyun 		} while (stat.s.stxcal == 0);
615*4882a593Smuzhiyun 	}
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	return 0;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun /**
621*4882a593Smuzhiyun  * Callback to handle interface up
622*4882a593Smuzhiyun  *
623*4882a593Smuzhiyun  * @interface: The identifier of the packet interface to configure and
624*4882a593Smuzhiyun  *		    use as a SPI interface.
625*4882a593Smuzhiyun  * @mode:      The operating mode for the SPI interface. The interface
626*4882a593Smuzhiyun  *		    can operate as a full duplex (both Tx and Rx data paths
627*4882a593Smuzhiyun  *		    active) or as a halfplex (either the Tx data path is
628*4882a593Smuzhiyun  *		    active or the Rx data path is active, but not both).
629*4882a593Smuzhiyun  *
630*4882a593Smuzhiyun  * Returns Zero on success, non-zero error code on failure (will cause
631*4882a593Smuzhiyun  * SPI initialization to abort)
632*4882a593Smuzhiyun  */
cvmx_spi_interface_up_cb(int interface,cvmx_spi_mode_t mode)633*4882a593Smuzhiyun int cvmx_spi_interface_up_cb(int interface, cvmx_spi_mode_t mode)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun 	union cvmx_gmxx_rxx_frm_min gmxx_rxx_frm_min;
636*4882a593Smuzhiyun 	union cvmx_gmxx_rxx_frm_max gmxx_rxx_frm_max;
637*4882a593Smuzhiyun 	union cvmx_gmxx_rxx_jabber gmxx_rxx_jabber;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
640*4882a593Smuzhiyun 		union cvmx_srxx_com_ctl srxx_com_ctl;
641*4882a593Smuzhiyun 		srxx_com_ctl.u64 = cvmx_read_csr(CVMX_SRXX_COM_CTL(interface));
642*4882a593Smuzhiyun 		srxx_com_ctl.s.inf_en = 1;
643*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
644*4882a593Smuzhiyun 		cvmx_dprintf("SPI%d: Rx is now up\n", interface);
645*4882a593Smuzhiyun 	}
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
648*4882a593Smuzhiyun 		union cvmx_stxx_com_ctl stxx_com_ctl;
649*4882a593Smuzhiyun 		stxx_com_ctl.u64 = cvmx_read_csr(CVMX_STXX_COM_CTL(interface));
650*4882a593Smuzhiyun 		stxx_com_ctl.s.inf_en = 1;
651*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_STXX_COM_CTL(interface), stxx_com_ctl.u64);
652*4882a593Smuzhiyun 		cvmx_dprintf("SPI%d: Tx is now up\n", interface);
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	gmxx_rxx_frm_min.u64 = 0;
656*4882a593Smuzhiyun 	gmxx_rxx_frm_min.s.len = 64;
657*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_GMXX_RXX_FRM_MIN(0, interface),
658*4882a593Smuzhiyun 		       gmxx_rxx_frm_min.u64);
659*4882a593Smuzhiyun 	gmxx_rxx_frm_max.u64 = 0;
660*4882a593Smuzhiyun 	gmxx_rxx_frm_max.s.len = 64 * 1024 - 4;
661*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX(0, interface),
662*4882a593Smuzhiyun 		       gmxx_rxx_frm_max.u64);
663*4882a593Smuzhiyun 	gmxx_rxx_jabber.u64 = 0;
664*4882a593Smuzhiyun 	gmxx_rxx_jabber.s.cnt = 64 * 1024 - 4;
665*4882a593Smuzhiyun 	cvmx_write_csr(CVMX_GMXX_RXX_JABBER(0, interface), gmxx_rxx_jabber.u64);
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	return 0;
668*4882a593Smuzhiyun }
669