blob: 09bc6d2d4316d16c918bf5aa2b7fb221e80407c3 [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 {
Harald Welte30fc5602020-12-14 15:56:28 +0100229 IDLE = 0, /* not yet initialized */
230 BOOT = 1, /* after e1_init(), regiters are programmed */
231 RUN = 2, /* normal operation */
232 RECOVER = 3, /* after underflow, overflow or alignment error */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200233};
234
Sylvain Munaut3da51512022-01-03 22:12:59 +0100235struct e1_state {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200236 struct {
237 uint32_t cr;
238 struct e1_fifo fifo;
239 int in_flight;
240 enum e1_pipe_state state;
241 } rx;
242
243 struct {
244 uint32_t cr;
245 struct e1_fifo fifo;
246 int in_flight;
247 enum e1_pipe_state state;
248 } tx;
Sylvain Munaut2c0c1362022-01-03 18:48:08 +0100249
Harald Welte805f2cf2020-12-14 17:31:03 +0100250 struct e1_error_count errors;
Sylvain Munaut3da51512022-01-03 22:12:59 +0100251};
252
253static struct e1_state g_e1[2];
254
255
256static volatile struct e1_core *
257_get_regs(int port)
258{
259 if ((port < 0) || (port > 1))
260 panic("_get_regs invalid port %d", port);
261 return &e1_regs_base[port];
262}
263
264static struct e1_state *
265_get_state(int port)
266{
267 if ((port < 0) || (port > 1))
268 panic("_get_state invalid port %d", port);
269 return &g_e1[port];
270}
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200271
272
Sylvain Munaute9fe0dc2022-01-10 12:26:20 +0100273#define RXCR_PERMITTED ( \
274 E1_RX_CR_MODE_MASK )
275
276#define TXCR_PERMITTED ( \
277 E1_TX_CR_MODE_MASK | \
278 E1_TX_CR_TICK_MASK | \
279 E1_TX_CR_ALARM | \
280 E1_TX_CR_LOOPBACK | \
281 E1_TX_CR_LOOPBACK_CROSS )
282
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200283void
Sylvain Munaut3da51512022-01-03 22:12:59 +0100284e1_init(int port, uint16_t rx_cr, uint16_t tx_cr)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200285{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100286 volatile struct e1_core *e1_regs = _get_regs(port);
287 struct e1_state *e1 = _get_state(port);
288
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200289 /* Global state init */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100290 memset(e1, 0x00, sizeof(struct e1_state));
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200291
Sylvain Munaut29d82092022-01-10 12:44:53 +0100292 /* Initialize FIFOs */
293 e1f_init(&e1->rx.fifo, (512 * port) + 0, 256);
294 e1f_init(&e1->tx.fifo, (512 * port) + 256, 256);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200295
296 /* Enable Rx */
Sylvain Munaute9fe0dc2022-01-10 12:26:20 +0100297 e1->rx.cr = E1_RX_CR_ENABLE | (rx_cr & RXCR_PERMITTED);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100298 e1_regs->rx.csr = E1_RX_CR_OVFL_CLR | e1->rx.cr;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200299
300 /* Enable Tx */
Sylvain Munaute9fe0dc2022-01-10 12:26:20 +0100301 e1->tx.cr = E1_TX_CR_ENABLE | (tx_cr & TXCR_PERMITTED);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100302 e1_regs->tx.csr = E1_TX_CR_UNFL_CLR | e1->tx.cr;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200303
304 /* State */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100305 e1->rx.state = BOOT;
306 e1->tx.state = BOOT;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200307}
308
Harald Welte6add0aa2020-12-16 00:02:11 +0100309void
Sylvain Munaut3da51512022-01-03 22:12:59 +0100310e1_rx_config(int port, uint16_t cr)
Harald Welte6add0aa2020-12-16 00:02:11 +0100311{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100312 volatile struct e1_core *e1_regs = _get_regs(port);
313 struct e1_state *e1 = _get_state(port);
314 e1->rx.cr = (e1->rx.cr & ~RXCR_PERMITTED) | (cr & RXCR_PERMITTED);
315 e1_regs->rx.csr = e1->rx.cr;
Harald Welte6add0aa2020-12-16 00:02:11 +0100316}
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200317
Sylvain Munaut4fd71552022-01-10 12:28:28 +0100318void
319e1_tx_config(int port, uint16_t cr)
320{
321 volatile struct e1_core *e1_regs = _get_regs(port);
322 struct e1_state *e1 = _get_state(port);
323 e1->tx.cr = (e1->tx.cr & ~TXCR_PERMITTED) | (cr & TXCR_PERMITTED);
324 e1_regs->tx.csr = e1->tx.cr;
325}
326
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200327unsigned int
Sylvain Munaut3da51512022-01-03 22:12:59 +0100328e1_rx_need_data(int port, unsigned int usb_addr, unsigned int max_frames, unsigned int *pos)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200329{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100330 struct e1_state *e1 = _get_state(port);
Harald Welte51baa362022-01-01 15:22:25 +0100331 bool rai_received = false;
332 bool rai_possible = false;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200333 unsigned int ofs;
334 int tot_frames = 0;
Harald Welte51baa362022-01-01 15:22:25 +0100335 int n_frames, i;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200336
337 while (max_frames) {
338 /* Get some data from the FIFO */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100339 n_frames = e1f_frame_read(&e1->rx.fifo, &ofs, max_frames);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200340 if (!n_frames)
341 break;
342
Harald Weltedaff4f62020-12-14 17:39:23 +0100343 /* Give pos */
344 if (pos) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100345 *pos = ofs & e1->rx.fifo.mask;
Harald Weltedaff4f62020-12-14 17:39:23 +0100346 pos = NULL;
347 }
348
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200349 /* Copy from FIFO to USB */
350 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), false, NULL, NULL);
351
352 /* Prepare Next */
353 usb_addr += n_frames * (32 / 4);
354 max_frames -= n_frames;
355 tot_frames += n_frames;
356
Harald Welte51baa362022-01-01 15:22:25 +0100357 /* While DMA is running: Determine if remote end indicates any alarms */
358 for (i = 0; i < n_frames; i++) {
359 unsigned int frame_nr = ofs + i;
360 /* A bit is present in every odd frame TS0 */
361 if (frame_nr & 1) {
362 uint8_t ts0 = *e1_data_ptr(0, ofs + i, 0);
363 rai_possible = true;
364 if (ts0 & 0x20) {
365 rai_received = true;
366 break;
367 }
368 }
369 }
370
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200371 /* Wait for DMA completion */
372 while (dma_poll());
373 }
374
Harald Welte51baa362022-01-01 15:22:25 +0100375 if (rai_possible) {
376 if (rai_received) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100377 e1->errors.flags |= E1_ERR_F_RAI;
378 e1_platform_led_set(port, E1P_LED_YELLOW, E1P_LED_ST_ON);
Harald Welte51baa362022-01-01 15:22:25 +0100379 } else {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100380 e1->errors.flags &= ~E1_ERR_F_RAI;
381 e1_platform_led_set(port, E1P_LED_YELLOW, E1P_LED_ST_OFF);
Harald Welte51baa362022-01-01 15:22:25 +0100382 }
383 }
384
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200385 return tot_frames;
386}
387
388unsigned int
Sylvain Munaut3da51512022-01-03 22:12:59 +0100389e1_tx_feed_data(int port, unsigned int usb_addr, unsigned int frames)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200390{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100391 struct e1_state *e1 = _get_state(port);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200392 unsigned int ofs;
393 int n_frames;
394
395 while (frames) {
396 /* Get some space in FIFO */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100397 n_frames = e1f_frame_write(&e1->tx.fifo, &ofs, frames);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200398 if (!n_frames) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100399 printf("[!] TX FIFO Overflow (port=%d, req=%d, done=%d)\n", port, frames, n_frames);
400 e1f_debug(&e1->tx.fifo, "TX");
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200401 break;
402 }
403
404 /* Copy from USB to FIFO */
405 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), true, NULL, NULL);
406
407 /* Prepare next */
408 usb_addr += n_frames * (32 / 4);
409 frames -= n_frames;
410
411 /* Wait for DMA completion */
412 while (dma_poll());
413 }
414
415 return frames;
416}
417
418unsigned int
Sylvain Munaut3da51512022-01-03 22:12:59 +0100419e1_rx_level(int port)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200420{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100421 struct e1_state *e1 = _get_state(port);
422 return e1f_valid_frames(&e1->rx.fifo);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200423}
424
Sylvain Munaut4fd71552022-01-10 12:28:28 +0100425unsigned int
426e1_tx_level(int port)
427{
428 struct e1_state *e1 = _get_state(port);
429 return e1f_valid_frames(&e1->tx.fifo);
430}
431
Harald Welte805f2cf2020-12-14 17:31:03 +0100432const struct e1_error_count *
Sylvain Munaut3da51512022-01-03 22:12:59 +0100433e1_get_error_count(int port)
Harald Welte805f2cf2020-12-14 17:31:03 +0100434{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100435 struct e1_state *e1 = _get_state(port);
436 return &e1->errors;
Harald Welte805f2cf2020-12-14 17:31:03 +0100437}
438
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200439void
Sylvain Munaut3da51512022-01-03 22:12:59 +0100440e1_poll(int port)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200441{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100442 volatile struct e1_core *e1_regs = _get_regs(port);
443 struct e1_state *e1 = _get_state(port);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200444 uint32_t bd;
445 unsigned int ofs;
446
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200447 /* HACK: LED link status */
Harald Welte52765672020-12-15 18:35:42 +0100448 if (e1_regs->rx.csr & E1_RX_SR_ALIGNED) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100449 e1_platform_led_set(port, E1P_LED_GREEN, E1P_LED_ST_ON);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200450 led_color(0, 48, 0);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100451 e1->errors.flags &= ~(E1_ERR_F_LOS|E1_ERR_F_ALIGN_ERR);
Harald Welte52765672020-12-15 18:35:42 +0100452 } else {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100453 e1_platform_led_set(port, E1P_LED_GREEN, E1P_LED_ST_BLINK);
Sylvain Munautd6737bb2022-01-06 22:00:50 +0100454 e1_platform_led_set(port, E1P_LED_YELLOW, E1P_LED_ST_OFF);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200455 led_color(48, 0, 0);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100456 e1->errors.flags |= E1_ERR_F_ALIGN_ERR;
Harald Welte805f2cf2020-12-14 17:31:03 +0100457 /* TODO: completely off if rx tick counter not incrementing */
Harald Welte52765672020-12-15 18:35:42 +0100458 }
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200459
Sylvain Munaute98c0332022-01-10 12:46:49 +0100460 /* Active ? */
461 if ((e1->rx.state == IDLE) && (e1->tx.state == IDLE))
462 return;
463
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200464 /* Recover any done TX BD */
465 while ( (bd = e1_regs->tx.bd) & E1_BD_VALID ) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100466 e1f_multiframe_read_discard(&e1->tx.fifo);
467 e1->tx.in_flight--;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200468 }
469
470 /* Recover any done RX BD */
471 while ( (bd = e1_regs->rx.bd) & E1_BD_VALID ) {
472 /* FIXME: CRC status ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100473 e1f_multiframe_write_commit(&e1->rx.fifo);
Harald Welte805f2cf2020-12-14 17:31:03 +0100474 if ((bd & (E1_BD_CRC0 | E1_BD_CRC1)) != (E1_BD_CRC0 | E1_BD_CRC1)) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100475 printf("[!] E1 crc err (port=%d, bd=%03x)\n", port, bd);
476 e1->errors.crc++;
Harald Welte805f2cf2020-12-14 17:31:03 +0100477 }
Sylvain Munaut3da51512022-01-03 22:12:59 +0100478 e1->rx.in_flight--;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200479 }
480
481 /* Boot procedure */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100482 if (e1->tx.state == BOOT) {
483 if (e1f_unseen_frames(&e1->tx.fifo) < (16 * 5))
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200484 return;
485 /* HACK: LED flow status */
486 led_blink(true, 200, 1000);
487 led_breathe(true, 100, 200);
488 }
489
490 /* Handle RX */
491 /* Misalign ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100492 if (e1->rx.state == RUN) {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200493 if (!(e1_regs->rx.csr & E1_RX_SR_ALIGNED)) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100494 printf("[!] E1 rx misalign (port=%d)\n", port);
495 e1->rx.state = RECOVER;
496 e1->errors.align++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200497 }
498 }
499
500 /* Overflow ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100501 if (e1->rx.state == RUN) {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200502 if (e1_regs->rx.csr & E1_RX_SR_OVFL) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100503 printf("[!] E1 overflow (port=%d, inf=%d)\n", port, e1->rx.in_flight);
504 e1->rx.state = RECOVER;
505 e1->errors.ovfl++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200506 }
507 }
508
509 /* Recover ready ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100510 if (e1->rx.state == RECOVER) {
511 if (e1->rx.in_flight != 0)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200512 goto done_rx;
Sylvain Munaut3da51512022-01-03 22:12:59 +0100513 e1f_multiframe_empty(&e1->rx.fifo);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200514 }
515
516 /* Fill new RX BD */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100517 while (e1->rx.in_flight < 4) {
518 if (!e1f_multiframe_write_prepare(&e1->rx.fifo, &ofs))
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200519 break;
520 e1_regs->rx.bd = e1f_ofs_to_mf(ofs);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100521 e1->rx.in_flight++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200522 }
523
524 /* Clear overflow if needed */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100525 if (e1->rx.state != RUN) {
526 e1_regs->rx.csr = e1->rx.cr | E1_RX_CR_OVFL_CLR;
527 e1->rx.state = RUN;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200528 }
529done_rx:
530
531 /* Handle TX */
532 /* Underflow ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100533 if (e1->tx.state == RUN) {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200534 if (e1_regs->tx.csr & E1_TX_SR_UNFL) {
Sylvain Munaut3da51512022-01-03 22:12:59 +0100535 printf("[!] E1 underflow (port=%d, inf=%d)\n", port, e1->tx.in_flight);
536 e1->tx.state = RECOVER;
537 e1->errors.unfl++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200538 }
539 }
540
541 /* Recover ready ? */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100542 if (e1->tx.state == RECOVER) {
543 if (e1f_unseen_frames(&e1->tx.fifo) < (16 * 5))
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200544 return;
545 }
546
547 /* Fill new TX BD */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100548 while (e1->tx.in_flight < 4) {
549 if (!e1f_multiframe_read_peek(&e1->tx.fifo, &ofs))
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200550 break;
551 e1_regs->tx.bd = e1f_ofs_to_mf(ofs);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100552 e1->tx.in_flight++;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200553 }
554
555 /* Clear underflow if needed */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100556 if (e1->tx.state != RUN) {
557 e1_regs->tx.csr = e1->tx.cr | E1_TX_CR_UNFL_CLR;
558 e1->tx.state = RUN;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200559 }
560}
561
562void
Sylvain Munaut3da51512022-01-03 22:12:59 +0100563e1_debug_print(int port, bool data)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200564{
Sylvain Munaut3da51512022-01-03 22:12:59 +0100565 volatile struct e1_core *e1_regs = _get_regs(port);
566 struct e1_state *e1 = _get_state(port);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200567 volatile uint8_t *p;
568
Sylvain Munaut3da51512022-01-03 22:12:59 +0100569 printf("E1 port %d\n", port);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200570 printf("CSR: Rx %04x / Tx %04x\n", e1_regs->rx.csr, e1_regs->tx.csr);
Sylvain Munaut3da51512022-01-03 22:12:59 +0100571 printf("InF: Rx %d / Tx %d\n", e1->rx.in_flight, e1->tx.in_flight);
572 printf("Sta: Rx %d / Tx %d\n", e1->rx.state, e1->tx.state);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200573
Sylvain Munaut3da51512022-01-03 22:12:59 +0100574 e1f_debug(&e1->rx.fifo, "Rx FIFO");
575 e1f_debug(&e1->tx.fifo, "Tx FIFO");
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200576
577 if (data) {
578 puts("\nE1 Data\n");
579 for (int f=0; f<16; f++) {
580 p = e1_data_ptr(0, f, 0);
581 for (int ts=0; ts<32; ts++)
582 printf(" %02x", p[ts]);
583 printf("\n");
584 }
585 }
586}