blob: b1695a6adbb3bafcefcd14bbfd37a8c80ec43715 [file] [log] [blame]
Sylvain Munautbc9f5c42020-09-14 10:22:29 +02001/*
2 * e1.c
3 *
4 * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8#include <stdint.h>
9#include <stdbool.h>
10#include <string.h>
11
12#include "config.h"
13#include "console.h"
14#include "e1.h"
Harald Weltef74dad72020-12-15 23:32:53 +010015#include "e1_hw.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020016
17#include "dma.h"
18#include "led.h" // FIXME
Sylvain Munaut3da51512022-01-03 22:12:59 +010019#include "utils.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020020
21
Sylvain Munaut2c0c1362022-01-03 18:48:08 +010022// HW access
23// ---------
24
Sylvain Munaut3da51512022-01-03 22:12:59 +010025static volatile struct e1_core * const e1_regs_base = (void *)(E1_CORE_BASE);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020026static volatile uint8_t * const e1_data = (void *)(E1_DATA_BASE);
27
Sylvain Munaut2c0c1362022-01-03 18:48:08 +010028
29// Helpers
30// -------
31
Sylvain Munaut35856a12022-01-03 18:45:53 +010032static unsigned int
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020033e1_data_ofs(int mf, int frame, int ts)
34{
35 return (mf << 9) | (frame << 5) | ts;
36}
37
Sylvain Munaut35856a12022-01-03 18:45:53 +010038static volatile uint8_t *
Harald Weltea59ef2b2020-12-14 17:02:13 +010039e1_data_ptr(int mf, int frame, int ts)
40{
41 return &e1_data[e1_data_ofs(mf, frame, ts)];
42}
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020043
Sylvain Munaut2c0c1362022-01-03 18:48:08 +010044
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020045// FIFOs
46// -----
47/* Note: FIFO works at 'frame' level (i.e. 32 bytes) */
48
49struct e1_fifo {
50 /* Buffer zone associated with the FIFO */
51 unsigned int base;
52 unsigned int mask;
53
54 /* Pointers / Levels */
55 unsigned int wptr[2]; /* 0=committed 1=allocated */
56 unsigned int rptr[2]; /* 0=discared 1=peeked */
57};
58
59 /* Utils */
60static void
Sylvain Munaut29d82092022-01-10 12:44:53 +010061e1f_init(struct e1_fifo *fifo, unsigned int base, unsigned int len)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020062{
63 memset(fifo, 0x00, sizeof(struct e1_fifo));
64 fifo->base = base;
65 fifo->mask = len - 1;
66}
67
Sylvain Munaut29d82092022-01-10 12:44:53 +010068static void
69e1f_reset(struct e1_fifo *fifo)
70{
71 fifo->wptr[0] = fifo->wptr[1] = 0;
72 fifo->rptr[0] = fifo->rptr[1] = 0;
73}
74
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020075static unsigned int
76e1f_allocd_frames(struct e1_fifo *fifo)
77{
78 /* Number of frames that are allocated (i.e. where we can't write to) */
79 return (fifo->wptr[1] - fifo->rptr[0]) & fifo->mask;
80}
81
82static unsigned int
83e1f_valid_frames(struct e1_fifo *fifo)
84{
85 /* Number of valid frames */
86 return (fifo->wptr[0] - fifo->rptr[0]) & fifo->mask;
87}
88
89static unsigned int
90e1f_unseen_frames(struct e1_fifo *fifo)
91{
92 /* Number of valid frames that haven't been peeked yet */
93 return (fifo->wptr[0] - fifo->rptr[1]) & fifo->mask;
94}
95
96static unsigned int
97e1f_free_frames(struct e1_fifo *fifo)
98{
99 /* Number of frames that aren't allocated */
100 return (fifo->rptr[0] - fifo->wptr[1] - 1) & fifo->mask;
101}
102
103static unsigned int
104e1f_ofs_to_dma(unsigned int ofs)
105{
106 /* DMA address are 32-bits word address. Offsets are 32 byte address */
107 return (ofs << 3);
108}
109
110static unsigned int
111e1f_ofs_to_mf(unsigned int ofs)
112{
113 /* E1 Buffer Descriptors are always multiframe aligned */
114 return (ofs >> 4);
115}
116
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200117 /* Debug */
118static void
119e1f_debug(struct e1_fifo *fifo, const char *name)
120{
121 unsigned int la, lv, lu, lf;
122
123 la = e1f_allocd_frames(fifo);
124 lv = e1f_valid_frames(fifo);
125 lu = e1f_unseen_frames(fifo);
126 lf = e1f_free_frames(fifo);
127
128 printf("%s: R: %u / %u | W: %u / %u | A:%u V:%u U:%u F:%u\n",
129 name,
130 fifo->rptr[0], fifo->rptr[1], fifo->wptr[0], fifo->wptr[1],
131 la, lv, lu, lf
132 );
133}
134
135 /* Frame level read/write */
136static unsigned int
137e1f_frame_write(struct e1_fifo *fifo, unsigned int *ofs, unsigned int max_frames)
138{
139 unsigned int lf, le;
140
141 lf = e1f_free_frames(fifo);
142 le = fifo->mask - fifo->wptr[0] + 1;
143
144 if (max_frames > le)
145 max_frames = le;
146 if (max_frames > lf)
147 max_frames = lf;
148
149 *ofs = fifo->base + fifo->wptr[0];
150 fifo->wptr[1] = fifo->wptr[0] = (fifo->wptr[0] + max_frames) & fifo->mask;
151
152 return max_frames;
153}
154
155static unsigned int
Sylvain Munautde20fb72020-10-29 13:24:50 +0100156e1f_frame_read(struct e1_fifo *fifo, unsigned int *ofs, unsigned int max_frames)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200157{
158 unsigned int lu, le;
159
160 lu = e1f_unseen_frames(fifo);
161 le = fifo->mask - fifo->rptr[1] + 1;
162
163 if (max_frames > le)
164 max_frames = le;
165 if (max_frames > lu)
166 max_frames = lu;
167
168 *ofs = fifo->base + fifo->rptr[1];
169 fifo->rptr[0] = fifo->rptr[1] = (fifo->rptr[1] + max_frames) & fifo->mask;
170
171 return max_frames;
172}
173
174
175 /* MultiFrame level split read/write */
176static bool
177e1f_multiframe_write_prepare(struct e1_fifo *fifo, unsigned int *ofs)
178{
179 unsigned int lf;
180
181 lf = e1f_free_frames(fifo);
182 if (lf < 16)
183 return false;
184
185 *ofs = fifo->base + fifo->wptr[1];
186 fifo->wptr[1] = (fifo->wptr[1] + 16) & fifo->mask;
187
188 return true;
189}
190
191static void
192e1f_multiframe_write_commit(struct e1_fifo *fifo)
193{
194 fifo->wptr[0] = (fifo->wptr[0] + 16) & fifo->mask;
195}
196
197static bool
198e1f_multiframe_read_peek(struct e1_fifo *fifo, unsigned int *ofs)
199{
200 unsigned int lu;
201
202 lu = e1f_unseen_frames(fifo);
203 if (lu < 16)
204 return false;
205
206 *ofs = fifo->base + fifo->rptr[1];
207 fifo->rptr[1] = (fifo->rptr[1] + 16) & fifo->mask;
208
209 return true;
210}
211
212static void
213e1f_multiframe_read_discard(struct e1_fifo *fifo)
214{
215 fifo->rptr[0] = (fifo->rptr[0] + 16) & fifo->mask;
216}
217
218static void
219e1f_multiframe_empty(struct e1_fifo *fifo)
220{
221 fifo->rptr[0] = fifo->rptr[1] = (fifo->wptr[0] & ~15);
222}
223
224
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200225// Main logic
226// ----------
227
228enum e1_pipe_state {
Sylvain Munaut0cc16132022-01-10 13:26:15 +0100229 IDLE = 0, /* not running */
230 STARTING = 1, /* after e1_start(), waiting for priming */
231 RUN = 2, /* normal operation */
232 RECOVER = 3, /* after underflow, overflow or alignment error */
233 SHUTDOWN = 4, /* after e1_stop(), waiting for shutdown */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200234};
235
Sylvain Munaut3da51512022-01-03 22:12:59 +0100236struct e1_state {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200237 struct {
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100238 struct {
239 uint32_t cfg;
240 uint32_t val;
241 } cr;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200242 struct e1_fifo fifo;
243 int in_flight;
244 enum e1_pipe_state state;
245 } rx;
246
247 struct {
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100248 struct {
249 uint32_t cfg;
250 uint32_t val;
251 } cr;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200252 struct e1_fifo fifo;
253 int in_flight;
254 enum e1_pipe_state state;
255 } tx;
Sylvain Munaut2c0c1362022-01-03 18:48:08 +0100256
Harald Welte805f2cf2020-12-14 17:31:03 +0100257 struct e1_error_count errors;
Sylvain Munaut3da51512022-01-03 22:12:59 +0100258};
259
260static struct e1_state g_e1[2];
261
262
263static volatile struct e1_core *
264_get_regs(int port)
265{
266 if ((port < 0) || (port > 1))
267 panic("_get_regs invalid port %d", port);
268 return &e1_regs_base[port];
269}
270
271static struct e1_state *
272_get_state(int port)
273{
274 if ((port < 0) || (port > 1))
275 panic("_get_state invalid port %d", port);
276 return &g_e1[port];
277}
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200278
279
Sylvain Munaute9fe0dc2022-01-10 12:26:20 +0100280#define RXCR_PERMITTED ( \
281 E1_RX_CR_MODE_MASK )
282
283#define TXCR_PERMITTED ( \
284 E1_TX_CR_MODE_MASK | \
285 E1_TX_CR_TICK_MASK | \
286 E1_TX_CR_ALARM | \
287 E1_TX_CR_LOOPBACK | \
288 E1_TX_CR_LOOPBACK_CROSS )
289
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100290static void
291_e1_update_cr_val(int port)
292{
293 struct e1_state *e1 = _get_state(port);
294
295 /* RX */
296 if (e1->rx.state == IDLE) {
297 /* "Off" state: Force MFA mode to detect remote side */
298 e1->rx.cr.val = (e1->rx.cr.cfg & ~E1_RX_CR_MODE_MASK) | E1_RX_CR_ENABLE | E1_RX_CR_MODE_MFA;
299 } else {
300 /* "On state: Enabled + User config */
301 e1->rx.cr.val = e1->rx.cr.cfg | E1_RX_CR_ENABLE;
302 }
303
304 /* TX */
305 if (e1->tx.state == IDLE) {
306 /* "Off" state: We TX only OIS */
307 e1->tx.cr.val = (e1->tx.cr.cfg & ~(E1_TX_CR_MODE_MASK | E1_TX_CR_ALARM)) | E1_TX_CR_ENABLE | E1_TX_CR_MODE_TRSP;
308 } else {
309 /* "On state: Enabled + User config */
310 e1->tx.cr.val = e1->tx.cr.cfg | E1_TX_CR_ENABLE;
311 }
312}
313
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200314void
Sylvain Munaut3da51512022-01-03 22:12:59 +0100315e1_init(int port, uint16_t rx_cr, uint16_t tx_cr)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200316{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100317 volatile struct e1_core *e1_regs = _get_regs(port);
318 struct e1_state *e1 = _get_state(port);
319
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200320 /* Global state init */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100321 memset(e1, 0x00, sizeof(struct e1_state));
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200322
Sylvain Munaut29d82092022-01-10 12:44:53 +0100323 /* Initialize FIFOs */
324 e1f_init(&e1->rx.fifo, (512 * port) + 0, 256);
325 e1f_init(&e1->tx.fifo, (512 * port) + 256, 256);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200326
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100327 /* Flow state */
Sylvain Munaut0cc16132022-01-10 13:26:15 +0100328 e1->rx.state = IDLE;
329 e1->tx.state = IDLE;
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100330
331 /* Set config registers */
332 e1->rx.cr.cfg = rx_cr & RXCR_PERMITTED;
333 e1->tx.cr.cfg = tx_cr & TXCR_PERMITTED;
334
335 _e1_update_cr_val(port);
336
337 e1_regs->rx.csr = e1->rx.cr.val;
338 e1_regs->tx.csr = e1->tx.cr.val;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200339}
340
Harald Welte6add0aa2020-12-16 00:02:11 +0100341void
Sylvain Munaut3da51512022-01-03 22:12:59 +0100342e1_rx_config(int port, uint16_t cr)
Harald Welte6add0aa2020-12-16 00:02:11 +0100343{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100344 volatile struct e1_core *e1_regs = _get_regs(port);
345 struct e1_state *e1 = _get_state(port);
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100346 e1->rx.cr.cfg = cr & RXCR_PERMITTED;
347 _e1_update_cr_val(port);
348 e1_regs->rx.csr = e1->rx.cr.val;
Harald Welte6add0aa2020-12-16 00:02:11 +0100349}
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200350
Sylvain Munaut4fd71552022-01-10 12:28:28 +0100351void
352e1_tx_config(int port, uint16_t cr)
353{
354 volatile struct e1_core *e1_regs = _get_regs(port);
355 struct e1_state *e1 = _get_state(port);
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100356 e1->tx.cr.cfg = cr & TXCR_PERMITTED;
357 _e1_update_cr_val(port);
358 e1_regs->tx.csr = e1->tx.cr.val;
Sylvain Munaut4fd71552022-01-10 12:28:28 +0100359}
360
Sylvain Munaut0cc16132022-01-10 13:26:15 +0100361void
362e1_start(int port)
363{
364 volatile struct e1_core *e1_regs = _get_regs(port);
365 struct e1_state *e1 = _get_state(port);
366
367 /* Checks */
368 while ((e1->rx.state == SHUTDOWN) || (e1->tx.state == SHUTDOWN))
369 e1_poll(port);
370
371 if ((e1->rx.state != IDLE) || (e1->tx.state != IDLE))
372 panic("Invalid E1 hardware state (port=%d, rxs=%d, txs=%d)",
373 port, e1->rx.state, e1->tx.state);
374
375 /* Clear FIFOs */
376 e1f_reset(&e1->rx.fifo);
377 e1f_reset(&e1->tx.fifo);
378
379 /* Flow state */
380 e1->rx.state = STARTING;
381 e1->tx.state = STARTING;
382
383 /* Update CRs */
384 _e1_update_cr_val(port);
385
386 e1_regs->rx.csr = e1->rx.cr.val | E1_RX_CR_OVFL_CLR;
387 e1_regs->tx.csr = e1->tx.cr.val | E1_TX_CR_UNFL_CLR;
388}
389
390void
391e1_stop(int port)
392{
393 struct e1_state *e1 = _get_state(port);
394
395 /* Flow state */
396 e1->rx.state = SHUTDOWN;
397 e1->tx.state = SHUTDOWN;
398
399 /* Nothing else to do, e1_poll will stop submitting data and
400 * transition to IDLE when everything in-flight is done */
401}
402
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200403unsigned int
Sylvain Munaut3da51512022-01-03 22:12:59 +0100404e1_rx_need_data(int port, unsigned int usb_addr, unsigned int max_frames, unsigned int *pos)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200405{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100406 struct e1_state *e1 = _get_state(port);
Harald Welte51baa362022-01-01 15:22:25 +0100407 bool rai_received = false;
408 bool rai_possible = false;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200409 unsigned int ofs;
410 int tot_frames = 0;
Harald Welte51baa362022-01-01 15:22:25 +0100411 int n_frames, i;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200412
413 while (max_frames) {
414 /* Get some data from the FIFO */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100415 n_frames = e1f_frame_read(&e1->rx.fifo, &ofs, max_frames);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200416 if (!n_frames)
417 break;
418
Harald Weltedaff4f62020-12-14 17:39:23 +0100419 /* Give pos */
420 if (pos) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100421 *pos = ofs & e1->rx.fifo.mask;
Harald Weltedaff4f62020-12-14 17:39:23 +0100422 pos = NULL;
423 }
424
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200425 /* Copy from FIFO to USB */
426 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), false, NULL, NULL);
427
428 /* Prepare Next */
429 usb_addr += n_frames * (32 / 4);
430 max_frames -= n_frames;
431 tot_frames += n_frames;
432
Harald Welte51baa362022-01-01 15:22:25 +0100433 /* While DMA is running: Determine if remote end indicates any alarms */
434 for (i = 0; i < n_frames; i++) {
435 unsigned int frame_nr = ofs + i;
436 /* A bit is present in every odd frame TS0 */
437 if (frame_nr & 1) {
438 uint8_t ts0 = *e1_data_ptr(0, ofs + i, 0);
439 rai_possible = true;
440 if (ts0 & 0x20) {
441 rai_received = true;
442 break;
443 }
444 }
445 }
446
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200447 /* Wait for DMA completion */
448 while (dma_poll());
449 }
450
Harald Welte51baa362022-01-01 15:22:25 +0100451 if (rai_possible) {
452 if (rai_received) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100453 e1->errors.flags |= E1_ERR_F_RAI;
454 e1_platform_led_set(port, E1P_LED_YELLOW, E1P_LED_ST_ON);
Harald Welte51baa362022-01-01 15:22:25 +0100455 } else {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100456 e1->errors.flags &= ~E1_ERR_F_RAI;
457 e1_platform_led_set(port, E1P_LED_YELLOW, E1P_LED_ST_OFF);
Harald Welte51baa362022-01-01 15:22:25 +0100458 }
459 }
460
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200461 return tot_frames;
462}
463
464unsigned int
Sylvain Munaut3da51512022-01-03 22:12:59 +0100465e1_tx_feed_data(int port, unsigned int usb_addr, unsigned int frames)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200466{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100467 struct e1_state *e1 = _get_state(port);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200468 unsigned int ofs;
469 int n_frames;
470
471 while (frames) {
472 /* Get some space in FIFO */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100473 n_frames = e1f_frame_write(&e1->tx.fifo, &ofs, frames);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200474 if (!n_frames) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100475 printf("[!] TX FIFO Overflow (port=%d, req=%d, done=%d)\n", port, frames, n_frames);
476 e1f_debug(&e1->tx.fifo, "TX");
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200477 break;
478 }
479
480 /* Copy from USB to FIFO */
481 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), true, NULL, NULL);
482
483 /* Prepare next */
484 usb_addr += n_frames * (32 / 4);
485 frames -= n_frames;
486
487 /* Wait for DMA completion */
488 while (dma_poll());
489 }
490
491 return frames;
492}
493
494unsigned int
Sylvain Munaut3da51512022-01-03 22:12:59 +0100495e1_rx_level(int port)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200496{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100497 struct e1_state *e1 = _get_state(port);
498 return e1f_valid_frames(&e1->rx.fifo);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200499}
500
Sylvain Munaut4fd71552022-01-10 12:28:28 +0100501unsigned int
502e1_tx_level(int port)
503{
504 struct e1_state *e1 = _get_state(port);
505 return e1f_valid_frames(&e1->tx.fifo);
506}
507
Harald Welte805f2cf2020-12-14 17:31:03 +0100508const struct e1_error_count *
Sylvain Munaut3da51512022-01-03 22:12:59 +0100509e1_get_error_count(int port)
Harald Welte805f2cf2020-12-14 17:31:03 +0100510{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100511 struct e1_state *e1 = _get_state(port);
512 return &e1->errors;
Harald Welte805f2cf2020-12-14 17:31:03 +0100513}
514
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200515void
Sylvain Munaut3da51512022-01-03 22:12:59 +0100516e1_poll(int port)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200517{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100518 volatile struct e1_core *e1_regs = _get_regs(port);
519 struct e1_state *e1 = _get_state(port);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200520 uint32_t bd;
521 unsigned int ofs;
522
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200523 /* HACK: LED link status */
Harald Welte52765672020-12-15 18:35:42 +0100524 if (e1_regs->rx.csr & E1_RX_SR_ALIGNED) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100525 e1_platform_led_set(port, E1P_LED_GREEN, E1P_LED_ST_ON);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200526 led_color(0, 48, 0);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100527 e1->errors.flags &= ~(E1_ERR_F_LOS|E1_ERR_F_ALIGN_ERR);
Harald Welte52765672020-12-15 18:35:42 +0100528 } else {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100529 e1_platform_led_set(port, E1P_LED_GREEN, E1P_LED_ST_BLINK);
Sylvain Munautd6737bb2022-01-06 22:00:50 +0100530 e1_platform_led_set(port, E1P_LED_YELLOW, E1P_LED_ST_OFF);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200531 led_color(48, 0, 0);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100532 e1->errors.flags |= E1_ERR_F_ALIGN_ERR;
Harald Welte805f2cf2020-12-14 17:31:03 +0100533 /* TODO: completely off if rx tick counter not incrementing */
Harald Welte52765672020-12-15 18:35:42 +0100534 }
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200535
Sylvain Munaute98c0332022-01-10 12:46:49 +0100536 /* Active ? */
537 if ((e1->rx.state == IDLE) && (e1->tx.state == IDLE))
538 return;
539
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200540 /* Recover any done TX BD */
541 while ( (bd = e1_regs->tx.bd) & E1_BD_VALID ) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100542 e1f_multiframe_read_discard(&e1->tx.fifo);
543 e1->tx.in_flight--;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200544 }
545
546 /* Recover any done RX BD */
547 while ( (bd = e1_regs->rx.bd) & E1_BD_VALID ) {
548 /* FIXME: CRC status ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100549 e1f_multiframe_write_commit(&e1->rx.fifo);
Harald Welte805f2cf2020-12-14 17:31:03 +0100550 if ((bd & (E1_BD_CRC0 | E1_BD_CRC1)) != (E1_BD_CRC0 | E1_BD_CRC1)) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100551 printf("[!] E1 crc err (port=%d, bd=%03x)\n", port, bd);
552 e1->errors.crc++;
Harald Welte805f2cf2020-12-14 17:31:03 +0100553 }
Sylvain Munaut3da51512022-01-03 22:12:59 +0100554 e1->rx.in_flight--;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200555 }
556
557 /* Boot procedure */
Sylvain Munaut0cc16132022-01-10 13:26:15 +0100558 if (e1->tx.state == STARTING) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100559 if (e1f_unseen_frames(&e1->tx.fifo) < (16 * 5))
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200560 return;
561 /* HACK: LED flow status */
562 led_blink(true, 200, 1000);
563 led_breathe(true, 100, 200);
564 }
565
566 /* Handle RX */
Sylvain Munaut0cc16132022-01-10 13:26:15 +0100567 /* Bypass if OFF */
568 if (e1->rx.state == IDLE)
569 goto done_rx;
570
571 /* Shutdown */
572 if (e1->rx.state == SHUTDOWN) {
573 if (e1->rx.in_flight == 0) {
574 e1->rx.state = IDLE;
575 _e1_update_cr_val(port);
576 e1_regs->rx.csr = e1->rx.cr.val;
577 }
578 goto done_rx;
579 }
580
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200581 /* Misalign ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100582 if (e1->rx.state == RUN) {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200583 if (!(e1_regs->rx.csr & E1_RX_SR_ALIGNED)) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100584 printf("[!] E1 rx misalign (port=%d)\n", port);
585 e1->rx.state = RECOVER;
586 e1->errors.align++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200587 }
588 }
589
590 /* Overflow ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100591 if (e1->rx.state == RUN) {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200592 if (e1_regs->rx.csr & E1_RX_SR_OVFL) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100593 printf("[!] E1 overflow (port=%d, inf=%d)\n", port, e1->rx.in_flight);
594 e1->rx.state = RECOVER;
595 e1->errors.ovfl++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200596 }
597 }
598
599 /* Recover ready ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100600 if (e1->rx.state == RECOVER) {
601 if (e1->rx.in_flight != 0)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200602 goto done_rx;
Sylvain Munaut3da51512022-01-03 22:12:59 +0100603 e1f_multiframe_empty(&e1->rx.fifo);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200604 }
605
606 /* Fill new RX BD */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100607 while (e1->rx.in_flight < 4) {
608 if (!e1f_multiframe_write_prepare(&e1->rx.fifo, &ofs))
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200609 break;
610 e1_regs->rx.bd = e1f_ofs_to_mf(ofs);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100611 e1->rx.in_flight++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200612 }
613
614 /* Clear overflow if needed */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100615 if (e1->rx.state != RUN) {
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100616 e1_regs->rx.csr = e1->rx.cr.val | E1_RX_CR_OVFL_CLR;
Sylvain Munaut3da51512022-01-03 22:12:59 +0100617 e1->rx.state = RUN;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200618 }
619done_rx:
620
621 /* Handle TX */
Sylvain Munaut0cc16132022-01-10 13:26:15 +0100622 /* Bypass if OFF */
623 if (e1->tx.state == IDLE)
624 return;
625
626 /* Shutdown */
627 if (e1->tx.state == SHUTDOWN) {
628 if (e1->tx.in_flight == 0) {
629 e1->tx.state = IDLE;
630 _e1_update_cr_val(port);
631 e1_regs->tx.csr = e1->tx.cr.val;
632 }
633 return;
634 }
635
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200636 /* Underflow ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100637 if (e1->tx.state == RUN) {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200638 if (e1_regs->tx.csr & E1_TX_SR_UNFL) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100639 printf("[!] E1 underflow (port=%d, inf=%d)\n", port, e1->tx.in_flight);
640 e1->tx.state = RECOVER;
641 e1->errors.unfl++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200642 }
643 }
644
645 /* Recover ready ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100646 if (e1->tx.state == RECOVER) {
647 if (e1f_unseen_frames(&e1->tx.fifo) < (16 * 5))
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200648 return;
649 }
650
651 /* Fill new TX BD */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100652 while (e1->tx.in_flight < 4) {
653 if (!e1f_multiframe_read_peek(&e1->tx.fifo, &ofs))
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200654 break;
655 e1_regs->tx.bd = e1f_ofs_to_mf(ofs);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100656 e1->tx.in_flight++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200657 }
658
659 /* Clear underflow if needed */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100660 if (e1->tx.state != RUN) {
Sylvain Munaut9410bdf2022-01-10 13:18:19 +0100661 e1_regs->tx.csr = e1->tx.cr.val | E1_TX_CR_UNFL_CLR;
Sylvain Munaut3da51512022-01-03 22:12:59 +0100662 e1->tx.state = RUN;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200663 }
664}
665
666void
Sylvain Munaut3da51512022-01-03 22:12:59 +0100667e1_debug_print(int port, bool data)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200668{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100669 volatile struct e1_core *e1_regs = _get_regs(port);
670 struct e1_state *e1 = _get_state(port);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200671 volatile uint8_t *p;
672
Sylvain Munaut3da51512022-01-03 22:12:59 +0100673 printf("E1 port %d\n", port);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200674 printf("CSR: Rx %04x / Tx %04x\n", e1_regs->rx.csr, e1_regs->tx.csr);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100675 printf("InF: Rx %d / Tx %d\n", e1->rx.in_flight, e1->tx.in_flight);
676 printf("Sta: Rx %d / Tx %d\n", e1->rx.state, e1->tx.state);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200677
Sylvain Munaut3da51512022-01-03 22:12:59 +0100678 e1f_debug(&e1->rx.fifo, "Rx FIFO");
679 e1f_debug(&e1->tx.fifo, "Tx FIFO");
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200680
681 if (data) {
682 puts("\nE1 Data\n");
683 for (int f=0; f<16; f++) {
684 p = e1_data_ptr(0, f, 0);
685 for (int ts=0; ts<32; ts++)
686 printf(" %02x", p[ts]);
687 printf("\n");
688 }
689 }
690}