1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <asm/octeon/octeon.h>
33
34#include <asm/octeon/cvmx-config.h>
35
36#include <asm/octeon/cvmx-pko.h>
37#include <asm/octeon/cvmx-spi.h>
38
39#include <asm/octeon/cvmx-spxx-defs.h>
40#include <asm/octeon/cvmx-stxx-defs.h>
41#include <asm/octeon/cvmx-srxx-defs.h>
42
43#define INVOKE_CB(function_p, args...) \
44 do { \
45 if (function_p) { \
46 res = function_p(args); \
47 if (res) \
48 return res; \
49 } \
50 } while (0)
51
52#if CVMX_ENABLE_DEBUG_PRINTS
53static const char *modes[] =
54 { "UNKNOWN", "TX Halfplex", "Rx Halfplex", "Duplex" };
55#endif
56
57
58
59
60static cvmx_spi_callbacks_t cvmx_spi_callbacks = {
61 .reset_cb = cvmx_spi_reset_cb,
62 .calendar_setup_cb = cvmx_spi_calendar_setup_cb,
63 .clock_detect_cb = cvmx_spi_clock_detect_cb,
64 .training_cb = cvmx_spi_training_cb,
65 .calendar_sync_cb = cvmx_spi_calendar_sync_cb,
66 .interface_up_cb = cvmx_spi_interface_up_cb
67};
68
69
70
71
72
73
74
75
76void cvmx_spi_get_callbacks(cvmx_spi_callbacks_t *callbacks)
77{
78 memcpy(callbacks, &cvmx_spi_callbacks, sizeof(cvmx_spi_callbacks));
79}
80
81
82
83
84
85
86void cvmx_spi_set_callbacks(cvmx_spi_callbacks_t *new_callbacks)
87{
88 memcpy(&cvmx_spi_callbacks, new_callbacks, sizeof(cvmx_spi_callbacks));
89}
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105int cvmx_spi_start_interface(int interface, cvmx_spi_mode_t mode, int timeout,
106 int num_ports)
107{
108 int res = -1;
109
110 if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))
111 return res;
112
113
114 INVOKE_CB(cvmx_spi_callbacks.reset_cb, interface, mode);
115
116
117 INVOKE_CB(cvmx_spi_callbacks.calendar_setup_cb, interface, mode,
118 num_ports);
119
120
121 INVOKE_CB(cvmx_spi_callbacks.clock_detect_cb, interface, mode, timeout);
122
123
124 INVOKE_CB(cvmx_spi_callbacks.training_cb, interface, mode, timeout);
125
126
127 INVOKE_CB(cvmx_spi_callbacks.calendar_sync_cb, interface, mode,
128 timeout);
129
130
131 INVOKE_CB(cvmx_spi_callbacks.interface_up_cb, interface, mode);
132
133 return res;
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150int cvmx_spi_restart_interface(int interface, cvmx_spi_mode_t mode, int timeout)
151{
152 int res = -1;
153
154 if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))
155 return res;
156
157 cvmx_dprintf("SPI%d: Restart %s\n", interface, modes[mode]);
158
159
160 INVOKE_CB(cvmx_spi_callbacks.reset_cb, interface, mode);
161
162
163
164
165
166 INVOKE_CB(cvmx_spi_callbacks.clock_detect_cb, interface, mode, timeout);
167
168
169 INVOKE_CB(cvmx_spi_callbacks.training_cb, interface, mode, timeout);
170
171
172 INVOKE_CB(cvmx_spi_callbacks.calendar_sync_cb, interface, mode,
173 timeout);
174
175
176 INVOKE_CB(cvmx_spi_callbacks.interface_up_cb, interface, mode);
177
178 return res;
179}
180EXPORT_SYMBOL_GPL(cvmx_spi_restart_interface);
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195int cvmx_spi_reset_cb(int interface, cvmx_spi_mode_t mode)
196{
197 union cvmx_spxx_dbg_deskew_ctl spxx_dbg_deskew_ctl;
198 union cvmx_spxx_clk_ctl spxx_clk_ctl;
199 union cvmx_spxx_bist_stat spxx_bist_stat;
200 union cvmx_spxx_int_msk spxx_int_msk;
201 union cvmx_stxx_int_msk stxx_int_msk;
202 union cvmx_spxx_trn4_ctl spxx_trn4_ctl;
203 int index;
204 uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
205
206
207 spxx_int_msk.u64 = cvmx_read_csr(CVMX_SPXX_INT_MSK(interface));
208 cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), 0);
209 stxx_int_msk.u64 = cvmx_read_csr(CVMX_STXX_INT_MSK(interface));
210 cvmx_write_csr(CVMX_STXX_INT_MSK(interface), 0);
211
212
213 cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), 0);
214 cvmx_write_csr(CVMX_STXX_COM_CTL(interface), 0);
215 spxx_clk_ctl.u64 = 0;
216 spxx_clk_ctl.s.runbist = 1;
217 cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
218 __delay(10 * MS);
219 spxx_bist_stat.u64 = cvmx_read_csr(CVMX_SPXX_BIST_STAT(interface));
220 if (spxx_bist_stat.s.stat0)
221 cvmx_dprintf
222 ("ERROR SPI%d: BIST failed on receive datapath FIFO\n",
223 interface);
224 if (spxx_bist_stat.s.stat1)
225 cvmx_dprintf("ERROR SPI%d: BIST failed on RX calendar table\n",
226 interface);
227 if (spxx_bist_stat.s.stat2)
228 cvmx_dprintf("ERROR SPI%d: BIST failed on TX calendar table\n",
229 interface);
230
231
232 for (index = 0; index < 32; index++) {
233 union cvmx_srxx_spi4_calx srxx_spi4_calx;
234 union cvmx_stxx_spi4_calx stxx_spi4_calx;
235
236 srxx_spi4_calx.u64 = 0;
237 srxx_spi4_calx.s.oddpar = 1;
238 cvmx_write_csr(CVMX_SRXX_SPI4_CALX(index, interface),
239 srxx_spi4_calx.u64);
240
241 stxx_spi4_calx.u64 = 0;
242 stxx_spi4_calx.s.oddpar = 1;
243 cvmx_write_csr(CVMX_STXX_SPI4_CALX(index, interface),
244 stxx_spi4_calx.u64);
245 }
246
247
248 cvmx_write_csr(CVMX_SPXX_INT_REG(interface),
249 cvmx_read_csr(CVMX_SPXX_INT_REG(interface)));
250 cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), spxx_int_msk.u64);
251 cvmx_write_csr(CVMX_STXX_INT_REG(interface),
252 cvmx_read_csr(CVMX_STXX_INT_REG(interface)));
253 cvmx_write_csr(CVMX_STXX_INT_MSK(interface), stxx_int_msk.u64);
254
255
256 spxx_clk_ctl.u64 = 0;
257 spxx_clk_ctl.s.seetrn = 0;
258 spxx_clk_ctl.s.clkdly = 0x10;
259 spxx_clk_ctl.s.runbist = 0;
260 spxx_clk_ctl.s.statdrv = 0;
261
262 spxx_clk_ctl.s.statrcv = 1;
263 spxx_clk_ctl.s.sndtrn = 0;
264 spxx_clk_ctl.s.drptrn = 0;
265 spxx_clk_ctl.s.rcvtrn = 0;
266 spxx_clk_ctl.s.srxdlck = 0;
267 cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
268 __delay(100 * MS);
269
270
271 spxx_clk_ctl.s.srxdlck = 1;
272 cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
273
274
275 __delay(100 * MS);
276
277
278 spxx_trn4_ctl.s.trntest = 0;
279 spxx_trn4_ctl.s.jitter = 1;
280 spxx_trn4_ctl.s.clr_boot = 1;
281 spxx_trn4_ctl.s.set_boot = 0;
282 if (OCTEON_IS_MODEL(OCTEON_CN58XX))
283 spxx_trn4_ctl.s.maxdist = 3;
284 else
285 spxx_trn4_ctl.s.maxdist = 8;
286 spxx_trn4_ctl.s.macro_en = 1;
287 spxx_trn4_ctl.s.mux_en = 1;
288 cvmx_write_csr(CVMX_SPXX_TRN4_CTL(interface), spxx_trn4_ctl.u64);
289
290 spxx_dbg_deskew_ctl.u64 = 0;
291 cvmx_write_csr(CVMX_SPXX_DBG_DESKEW_CTL(interface),
292 spxx_dbg_deskew_ctl.u64);
293
294 return 0;
295}
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311int cvmx_spi_calendar_setup_cb(int interface, cvmx_spi_mode_t mode,
312 int num_ports)
313{
314 int port;
315 int index;
316 if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
317 union cvmx_srxx_com_ctl srxx_com_ctl;
318 union cvmx_srxx_spi4_stat srxx_spi4_stat;
319
320
321 srxx_com_ctl.u64 = 0;
322 srxx_com_ctl.s.prts = num_ports - 1;
323 srxx_com_ctl.s.st_en = 0;
324 srxx_com_ctl.s.inf_en = 0;
325 cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
326
327
328 port = 0;
329 index = 0;
330 while (port < num_ports) {
331 union cvmx_srxx_spi4_calx srxx_spi4_calx;
332 srxx_spi4_calx.u64 = 0;
333 srxx_spi4_calx.s.prt0 = port++;
334 srxx_spi4_calx.s.prt1 = port++;
335 srxx_spi4_calx.s.prt2 = port++;
336 srxx_spi4_calx.s.prt3 = port++;
337 srxx_spi4_calx.s.oddpar =
338 ~(cvmx_dpop(srxx_spi4_calx.u64) & 1);
339 cvmx_write_csr(CVMX_SRXX_SPI4_CALX(index, interface),
340 srxx_spi4_calx.u64);
341 index++;
342 }
343 srxx_spi4_stat.u64 = 0;
344 srxx_spi4_stat.s.len = num_ports;
345 srxx_spi4_stat.s.m = 1;
346 cvmx_write_csr(CVMX_SRXX_SPI4_STAT(interface),
347 srxx_spi4_stat.u64);
348 }
349
350 if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
351 union cvmx_stxx_arb_ctl stxx_arb_ctl;
352 union cvmx_gmxx_tx_spi_max gmxx_tx_spi_max;
353 union cvmx_gmxx_tx_spi_thresh gmxx_tx_spi_thresh;
354 union cvmx_gmxx_tx_spi_ctl gmxx_tx_spi_ctl;
355 union cvmx_stxx_spi4_stat stxx_spi4_stat;
356 union cvmx_stxx_spi4_dat stxx_spi4_dat;
357
358
359 stxx_arb_ctl.u64 = 0;
360 stxx_arb_ctl.s.igntpa = 0;
361 stxx_arb_ctl.s.mintrn = 0;
362 cvmx_write_csr(CVMX_STXX_ARB_CTL(interface), stxx_arb_ctl.u64);
363
364 gmxx_tx_spi_max.u64 = 0;
365 gmxx_tx_spi_max.s.max1 = 8;
366 gmxx_tx_spi_max.s.max2 = 4;
367 gmxx_tx_spi_max.s.slice = 0;
368 cvmx_write_csr(CVMX_GMXX_TX_SPI_MAX(interface),
369 gmxx_tx_spi_max.u64);
370
371 gmxx_tx_spi_thresh.u64 = 0;
372 gmxx_tx_spi_thresh.s.thresh = 4;
373 cvmx_write_csr(CVMX_GMXX_TX_SPI_THRESH(interface),
374 gmxx_tx_spi_thresh.u64);
375
376 gmxx_tx_spi_ctl.u64 = 0;
377 gmxx_tx_spi_ctl.s.tpa_clr = 0;
378 gmxx_tx_spi_ctl.s.cont_pkt = 0;
379 cvmx_write_csr(CVMX_GMXX_TX_SPI_CTL(interface),
380 gmxx_tx_spi_ctl.u64);
381
382
383 stxx_spi4_dat.u64 = 0;
384
385 stxx_spi4_dat.s.alpha = 32;
386 stxx_spi4_dat.s.max_t = 0xFFFF;
387 cvmx_write_csr(CVMX_STXX_SPI4_DAT(interface),
388 stxx_spi4_dat.u64);
389
390
391 port = 0;
392 index = 0;
393 while (port < num_ports) {
394 union cvmx_stxx_spi4_calx stxx_spi4_calx;
395 stxx_spi4_calx.u64 = 0;
396 stxx_spi4_calx.s.prt0 = port++;
397 stxx_spi4_calx.s.prt1 = port++;
398 stxx_spi4_calx.s.prt2 = port++;
399 stxx_spi4_calx.s.prt3 = port++;
400 stxx_spi4_calx.s.oddpar =
401 ~(cvmx_dpop(stxx_spi4_calx.u64) & 1);
402 cvmx_write_csr(CVMX_STXX_SPI4_CALX(index, interface),
403 stxx_spi4_calx.u64);
404 index++;
405 }
406 stxx_spi4_stat.u64 = 0;
407 stxx_spi4_stat.s.len = num_ports;
408 stxx_spi4_stat.s.m = 1;
409 cvmx_write_csr(CVMX_STXX_SPI4_STAT(interface),
410 stxx_spi4_stat.u64);
411 }
412
413 return 0;
414}
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430int cvmx_spi_clock_detect_cb(int interface, cvmx_spi_mode_t mode, int timeout)
431{
432 int clock_transitions;
433 union cvmx_spxx_clk_stat stat;
434 uint64_t timeout_time;
435 uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
436
437
438
439
440
441 cvmx_dprintf("SPI%d: Waiting to see TsClk...\n", interface);
442 timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
443
444
445
446
447 clock_transitions = 100;
448 do {
449 stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
450 if (stat.s.s4clk0 && stat.s.s4clk1 && clock_transitions) {
451
452
453
454
455 clock_transitions--;
456 cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
457 stat.s.s4clk0 = 0;
458 stat.s.s4clk1 = 0;
459 }
460 if (cvmx_get_cycle() > timeout_time) {
461 cvmx_dprintf("SPI%d: Timeout\n", interface);
462 return -1;
463 }
464 } while (stat.s.s4clk0 == 0 || stat.s.s4clk1 == 0);
465
466 cvmx_dprintf("SPI%d: Waiting to see RsClk...\n", interface);
467 timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
468
469
470
471
472 clock_transitions = 100;
473 do {
474 stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
475 if (stat.s.d4clk0 && stat.s.d4clk1 && clock_transitions) {
476
477
478
479
480 clock_transitions--;
481 cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
482 stat.s.d4clk0 = 0;
483 stat.s.d4clk1 = 0;
484 }
485 if (cvmx_get_cycle() > timeout_time) {
486 cvmx_dprintf("SPI%d: Timeout\n", interface);
487 return -1;
488 }
489 } while (stat.s.d4clk0 == 0 || stat.s.d4clk1 == 0);
490
491 return 0;
492}
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508int cvmx_spi_training_cb(int interface, cvmx_spi_mode_t mode, int timeout)
509{
510 union cvmx_spxx_trn4_ctl spxx_trn4_ctl;
511 union cvmx_spxx_clk_stat stat;
512 uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
513 uint64_t timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
514 int rx_training_needed;
515
516
517 union cvmx_spxx_clk_ctl spxx_clk_ctl;
518 spxx_clk_ctl.u64 = 0;
519 spxx_clk_ctl.s.seetrn = 0;
520 spxx_clk_ctl.s.clkdly = 0x10;
521 spxx_clk_ctl.s.runbist = 0;
522 spxx_clk_ctl.s.statdrv = 0;
523
524 spxx_clk_ctl.s.statrcv = 1;
525 spxx_clk_ctl.s.sndtrn = 1;
526 spxx_clk_ctl.s.drptrn = 1;
527 spxx_clk_ctl.s.rcvtrn = 1;
528 spxx_clk_ctl.s.srxdlck = 1;
529 cvmx_write_csr(CVMX_SPXX_CLK_CTL(interface), spxx_clk_ctl.u64);
530 __delay(1000 * MS);
531
532
533 spxx_trn4_ctl.u64 = cvmx_read_csr(CVMX_SPXX_TRN4_CTL(interface));
534 spxx_trn4_ctl.s.clr_boot = 1;
535 cvmx_write_csr(CVMX_SPXX_TRN4_CTL(interface), spxx_trn4_ctl.u64);
536
537
538 cvmx_dprintf("SPI%d: Waiting for training\n", interface);
539 __delay(1000 * MS);
540
541 timeout_time = cvmx_get_cycle() + 1000ull * MS * 600;
542
543
544
545
546 rx_training_needed = 500;
547 do {
548 stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
549 if (stat.s.srxtrn && rx_training_needed) {
550 rx_training_needed--;
551 cvmx_write_csr(CVMX_SPXX_CLK_STAT(interface), stat.u64);
552 stat.s.srxtrn = 0;
553 }
554 if (cvmx_get_cycle() > timeout_time) {
555 cvmx_dprintf("SPI%d: Timeout\n", interface);
556 return -1;
557 }
558 } while (stat.s.srxtrn == 0);
559
560 return 0;
561}
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577int cvmx_spi_calendar_sync_cb(int interface, cvmx_spi_mode_t mode, int timeout)
578{
579 uint64_t MS = cvmx_sysinfo_get()->cpu_clock_hz / 1000;
580 if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
581
582 union cvmx_srxx_com_ctl srxx_com_ctl;
583 cvmx_dprintf
584 ("SPI%d: Rx is synchronized, start sending calendar data\n",
585 interface);
586 srxx_com_ctl.u64 = cvmx_read_csr(CVMX_SRXX_COM_CTL(interface));
587 srxx_com_ctl.s.inf_en = 1;
588 srxx_com_ctl.s.st_en = 1;
589 cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
590 }
591
592 if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
593
594
595
596 union cvmx_spxx_clk_stat stat;
597 uint64_t timeout_time;
598 union cvmx_stxx_com_ctl stxx_com_ctl;
599 stxx_com_ctl.u64 = 0;
600 stxx_com_ctl.s.st_en = 1;
601 cvmx_write_csr(CVMX_STXX_COM_CTL(interface), stxx_com_ctl.u64);
602
603
604 cvmx_dprintf("SPI%d: Waiting to sync on STX[%d] STAT\n",
605 interface, interface);
606 timeout_time = cvmx_get_cycle() + 1000ull * MS * timeout;
607
608 do {
609 stat.u64 = cvmx_read_csr(CVMX_SPXX_CLK_STAT(interface));
610 if (cvmx_get_cycle() > timeout_time) {
611 cvmx_dprintf("SPI%d: Timeout\n", interface);
612 return -1;
613 }
614 } while (stat.s.stxcal == 0);
615 }
616
617 return 0;
618}
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633int cvmx_spi_interface_up_cb(int interface, cvmx_spi_mode_t mode)
634{
635 union cvmx_gmxx_rxx_frm_min gmxx_rxx_frm_min;
636 union cvmx_gmxx_rxx_frm_max gmxx_rxx_frm_max;
637 union cvmx_gmxx_rxx_jabber gmxx_rxx_jabber;
638
639 if (mode & CVMX_SPI_MODE_RX_HALFPLEX) {
640 union cvmx_srxx_com_ctl srxx_com_ctl;
641 srxx_com_ctl.u64 = cvmx_read_csr(CVMX_SRXX_COM_CTL(interface));
642 srxx_com_ctl.s.inf_en = 1;
643 cvmx_write_csr(CVMX_SRXX_COM_CTL(interface), srxx_com_ctl.u64);
644 cvmx_dprintf("SPI%d: Rx is now up\n", interface);
645 }
646
647 if (mode & CVMX_SPI_MODE_TX_HALFPLEX) {
648 union cvmx_stxx_com_ctl stxx_com_ctl;
649 stxx_com_ctl.u64 = cvmx_read_csr(CVMX_STXX_COM_CTL(interface));
650 stxx_com_ctl.s.inf_en = 1;
651 cvmx_write_csr(CVMX_STXX_COM_CTL(interface), stxx_com_ctl.u64);
652 cvmx_dprintf("SPI%d: Tx is now up\n", interface);
653 }
654
655 gmxx_rxx_frm_min.u64 = 0;
656 gmxx_rxx_frm_min.s.len = 64;
657 cvmx_write_csr(CVMX_GMXX_RXX_FRM_MIN(0, interface),
658 gmxx_rxx_frm_min.u64);
659 gmxx_rxx_frm_max.u64 = 0;
660 gmxx_rxx_frm_max.s.len = 64 * 1024 - 4;
661 cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX(0, interface),
662 gmxx_rxx_frm_max.u64);
663 gmxx_rxx_jabber.u64 = 0;
664 gmxx_rxx_jabber.s.cnt = 64 * 1024 - 4;
665 cvmx_write_csr(CVMX_GMXX_RXX_JABBER(0, interface), gmxx_rxx_jabber.u64);
666
667 return 0;
668}
669