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