blob: 0f1d89d3889d6d6adf2d35cd7d7cc027132a2a03 [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"
15
16#include "dma.h"
17#include "led.h" // FIXME
18
19
20// Hardware
21// --------
22
23struct e1_chan {
24 uint32_t csr;
25 uint32_t bd;
26} __attribute__((packed,aligned(4)));
27
28struct e1_core {
29 struct e1_chan rx;
30 struct e1_chan tx;
31} __attribute__((packed,aligned(4)));
32
Harald Welte30fc5602020-12-14 15:56:28 +010033/* E1 receiver control register */
34#define E1_RX_CR_ENABLE (1 << 0) /* Enable receiver */
35#define E1_RX_CR_MODE_TRSP (0 << 1) /* Request no alignment at all */
36#define E1_RX_CR_MODE_BYTE (1 << 1) /* Request byte-level alignment */
37#define E1_RX_CR_MODE_BFA (2 << 1) /* Request Basic Frame Alignment */
38#define E1_RX_CR_MODE_MFA (3 << 1) /* Request Multi-Frame Alignment */
39#define E1_RX_CR_OVFL_CLR (1 << 12) /* Clear Rx overflow condition */
40
41/* E1 receiver status register */
42#define E1_RX_SR_ENABLED (1 << 0) /* Indicate Rx is enabled */
43#define E1_RX_SR_ALIGNED (1 << 1) /* Indicate Alignment achieved */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020044#define E1_RX_SR_BD_IN_EMPTY (1 << 8)
45#define E1_RX_SR_BD_IN_FULL (1 << 9)
46#define E1_RX_SR_BD_OUT_EMPTY (1 << 10)
47#define E1_RX_SR_BD_OUT_FULL (1 << 11)
Harald Welte30fc5602020-12-14 15:56:28 +010048#define E1_RX_SR_OVFL (1 << 12) /* Indicate Rx overflow */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020049
Harald Welte30fc5602020-12-14 15:56:28 +010050/* E1 transmitter control register */
51#define E1_TX_CR_ENABLE (1 << 0) /* Enable transmitter */
52#define E1_TX_CR_MODE_TRSP (0 << 1) /* Transparent bit-stream mode */
53#define E1_TX_CR_MODE_TS0 (1 << 1) /* Generate TS0 in framer */
54#define E1_TX_CR_MODE_TS0_CRC (2 << 1) /* Generate TS0 + CRC4 in framer */
55#define E1_TX_CR_MODE_TS0_CRC_E (3 << 1) /* Generate TS0 + CRC4 + E-bits (based on Rx) in framer */
56#define E1_TX_CR_TICK_LOCAL (0 << 3) /* use local clock for Tx */
57#define E1_TX_CR_TICK_REMOTE (1 << 3) /* use recovered remote clock for Tx */
58#define E1_TX_CR_ALARM (1 << 4) /* indicate ALARM to remote */
59#define E1_TX_CR_LOOPBACK (1 << 5) /* external loopback enable/diasble */
60#define E1_TX_CR_LOOPBACK_CROSS (1 << 6) /* source of loopback: local (0) or other (1) port */
61#define E1_TX_CR_UNFL_CLR (1 << 12) /* Clear Tx underflow condition */
62
63/* E1 transmitter status register */
64#define E1_TX_SR_ENABLED (1 << 0) /* Indicate Tx is enabled */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020065#define E1_TX_SR_BD_IN_EMPTY (1 << 8)
66#define E1_TX_SR_BD_IN_FULL (1 << 9)
67#define E1_TX_SR_BD_OUT_EMPTY (1 << 10)
68#define E1_TX_SR_BD_OUT_FULL (1 << 11)
Harald Welte30fc5602020-12-14 15:56:28 +010069#define E1_TX_SR_UNFL (1 << 12) /* Indicate Tx underflow */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020070
Harald Welte30fc5602020-12-14 15:56:28 +010071/* E1 buffer descriptor flags */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020072#define E1_BD_VALID (1 << 15)
73#define E1_BD_CRC1 (1 << 14)
74#define E1_BD_CRC0 (1 << 13)
75#define E1_BD_ADDR(x) ((x) & 0x7f)
76#define E1_BD_ADDR_MSK 0x7f
77#define E1_BD_ADDR_SHFT 0
78
79
80static volatile struct e1_core * const e1_regs = (void *)(E1_CORE_BASE);
81static volatile uint8_t * const e1_data = (void *)(E1_DATA_BASE);
82
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020083unsigned int
84e1_data_ofs(int mf, int frame, int ts)
85{
86 return (mf << 9) | (frame << 5) | ts;
87}
88
Harald Weltea59ef2b2020-12-14 17:02:13 +010089volatile uint8_t *
90e1_data_ptr(int mf, int frame, int ts)
91{
92 return &e1_data[e1_data_ofs(mf, frame, ts)];
93}
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020094
95// FIFOs
96// -----
97/* Note: FIFO works at 'frame' level (i.e. 32 bytes) */
98
99struct e1_fifo {
100 /* Buffer zone associated with the FIFO */
101 unsigned int base;
102 unsigned int mask;
103
104 /* Pointers / Levels */
105 unsigned int wptr[2]; /* 0=committed 1=allocated */
106 unsigned int rptr[2]; /* 0=discared 1=peeked */
107};
108
109 /* Utils */
110static void
111e1f_reset(struct e1_fifo *fifo, unsigned int base, unsigned int len)
112{
113 memset(fifo, 0x00, sizeof(struct e1_fifo));
114 fifo->base = base;
115 fifo->mask = len - 1;
116}
117
118static unsigned int
119e1f_allocd_frames(struct e1_fifo *fifo)
120{
121 /* Number of frames that are allocated (i.e. where we can't write to) */
122 return (fifo->wptr[1] - fifo->rptr[0]) & fifo->mask;
123}
124
125static unsigned int
126e1f_valid_frames(struct e1_fifo *fifo)
127{
128 /* Number of valid frames */
129 return (fifo->wptr[0] - fifo->rptr[0]) & fifo->mask;
130}
131
132static unsigned int
133e1f_unseen_frames(struct e1_fifo *fifo)
134{
135 /* Number of valid frames that haven't been peeked yet */
136 return (fifo->wptr[0] - fifo->rptr[1]) & fifo->mask;
137}
138
139static unsigned int
140e1f_free_frames(struct e1_fifo *fifo)
141{
142 /* Number of frames that aren't allocated */
143 return (fifo->rptr[0] - fifo->wptr[1] - 1) & fifo->mask;
144}
145
146static unsigned int
147e1f_ofs_to_dma(unsigned int ofs)
148{
149 /* DMA address are 32-bits word address. Offsets are 32 byte address */
150 return (ofs << 3);
151}
152
153static unsigned int
154e1f_ofs_to_mf(unsigned int ofs)
155{
156 /* E1 Buffer Descriptors are always multiframe aligned */
157 return (ofs >> 4);
158}
159
160
161 /* Debug */
162static void
163e1f_debug(struct e1_fifo *fifo, const char *name)
164{
165 unsigned int la, lv, lu, lf;
166
167 la = e1f_allocd_frames(fifo);
168 lv = e1f_valid_frames(fifo);
169 lu = e1f_unseen_frames(fifo);
170 lf = e1f_free_frames(fifo);
171
172 printf("%s: R: %u / %u | W: %u / %u | A:%u V:%u U:%u F:%u\n",
173 name,
174 fifo->rptr[0], fifo->rptr[1], fifo->wptr[0], fifo->wptr[1],
175 la, lv, lu, lf
176 );
177}
178
179 /* Frame level read/write */
180static unsigned int
181e1f_frame_write(struct e1_fifo *fifo, unsigned int *ofs, unsigned int max_frames)
182{
183 unsigned int lf, le;
184
185 lf = e1f_free_frames(fifo);
186 le = fifo->mask - fifo->wptr[0] + 1;
187
188 if (max_frames > le)
189 max_frames = le;
190 if (max_frames > lf)
191 max_frames = lf;
192
193 *ofs = fifo->base + fifo->wptr[0];
194 fifo->wptr[1] = fifo->wptr[0] = (fifo->wptr[0] + max_frames) & fifo->mask;
195
196 return max_frames;
197}
198
199static unsigned int
Sylvain Munautde20fb72020-10-29 13:24:50 +0100200e1f_frame_read(struct e1_fifo *fifo, unsigned int *ofs, unsigned int max_frames)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200201{
202 unsigned int lu, le;
203
204 lu = e1f_unseen_frames(fifo);
205 le = fifo->mask - fifo->rptr[1] + 1;
206
207 if (max_frames > le)
208 max_frames = le;
209 if (max_frames > lu)
210 max_frames = lu;
211
212 *ofs = fifo->base + fifo->rptr[1];
213 fifo->rptr[0] = fifo->rptr[1] = (fifo->rptr[1] + max_frames) & fifo->mask;
214
215 return max_frames;
216}
217
218
219 /* MultiFrame level split read/write */
220static bool
221e1f_multiframe_write_prepare(struct e1_fifo *fifo, unsigned int *ofs)
222{
223 unsigned int lf;
224
225 lf = e1f_free_frames(fifo);
226 if (lf < 16)
227 return false;
228
229 *ofs = fifo->base + fifo->wptr[1];
230 fifo->wptr[1] = (fifo->wptr[1] + 16) & fifo->mask;
231
232 return true;
233}
234
235static void
236e1f_multiframe_write_commit(struct e1_fifo *fifo)
237{
238 fifo->wptr[0] = (fifo->wptr[0] + 16) & fifo->mask;
239}
240
241static bool
242e1f_multiframe_read_peek(struct e1_fifo *fifo, unsigned int *ofs)
243{
244 unsigned int lu;
245
246 lu = e1f_unseen_frames(fifo);
247 if (lu < 16)
248 return false;
249
250 *ofs = fifo->base + fifo->rptr[1];
251 fifo->rptr[1] = (fifo->rptr[1] + 16) & fifo->mask;
252
253 return true;
254}
255
256static void
257e1f_multiframe_read_discard(struct e1_fifo *fifo)
258{
259 fifo->rptr[0] = (fifo->rptr[0] + 16) & fifo->mask;
260}
261
262static void
263e1f_multiframe_empty(struct e1_fifo *fifo)
264{
265 fifo->rptr[0] = fifo->rptr[1] = (fifo->wptr[0] & ~15);
266}
267
268
269
270// Main logic
271// ----------
272
273enum e1_pipe_state {
Harald Welte30fc5602020-12-14 15:56:28 +0100274 IDLE = 0, /* not yet initialized */
275 BOOT = 1, /* after e1_init(), regiters are programmed */
276 RUN = 2, /* normal operation */
277 RECOVER = 3, /* after underflow, overflow or alignment error */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200278};
279
280static struct {
281 struct {
282 uint32_t cr;
283 struct e1_fifo fifo;
284 int in_flight;
285 enum e1_pipe_state state;
286 } rx;
287
288 struct {
289 uint32_t cr;
290 struct e1_fifo fifo;
291 int in_flight;
292 enum e1_pipe_state state;
293 } tx;
294} g_e1;
295
296
297
298
299void
300e1_init(bool clk_mode)
301{
302 /* Global state init */
303 memset(&g_e1, 0x00, sizeof(g_e1));
304
305 /* Reset FIFOs */
306 e1f_reset(&g_e1.rx.fifo, 0, 128);
307 e1f_reset(&g_e1.tx.fifo, 128, 128);
308
309 /* Enable Rx */
310 g_e1.rx.cr = E1_RX_CR_OVFL_CLR |
311 E1_RX_CR_MODE_MFA |
312 E1_RX_CR_ENABLE;
313 e1_regs->rx.csr = g_e1.rx.cr;
314
315 /* Enable Tx */
316 g_e1.tx.cr = E1_TX_CR_UNFL_CLR |
317 (clk_mode ? E1_TX_CR_TICK_REMOTE : E1_TX_CR_TICK_LOCAL) |
318 E1_TX_CR_MODE_TS0_CRC_E |
319 E1_TX_CR_ENABLE;
320 e1_regs->tx.csr = g_e1.tx.cr;
321
322 /* State */
323 g_e1.rx.state = BOOT;
324 g_e1.tx.state = BOOT;
325}
326
327
328#include "dma.h"
329
330unsigned int
Harald Weltedaff4f62020-12-14 17:39:23 +0100331e1_rx_need_data(unsigned int usb_addr, unsigned int max_frames, unsigned int *pos)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200332{
333 unsigned int ofs;
334 int tot_frames = 0;
335 int n_frames;
336
337 while (max_frames) {
338 /* Get some data from the FIFO */
339 n_frames = e1f_frame_read(&g_e1.rx.fifo, &ofs, max_frames);
340 if (!n_frames)
341 break;
342
Harald Weltedaff4f62020-12-14 17:39:23 +0100343 /* Give pos */
344 if (pos) {
345 *pos = ofs & g_e1.rx.fifo.mask;
346 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
357 /* Wait for DMA completion */
358 while (dma_poll());
359 }
360
361 return tot_frames;
362}
363
364unsigned int
365e1_tx_feed_data(unsigned int usb_addr, unsigned int frames)
366{
367 unsigned int ofs;
368 int n_frames;
369
370 while (frames) {
371 /* Get some space in FIFO */
372 n_frames = e1f_frame_write(&g_e1.tx.fifo, &ofs, frames);
373 if (!n_frames) {
374 printf("[!] TX FIFO Overflow %d %d\n", frames, n_frames);
375 break;
376 }
377
378 /* Copy from USB to FIFO */
379 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), true, NULL, NULL);
380
381 /* Prepare next */
382 usb_addr += n_frames * (32 / 4);
383 frames -= n_frames;
384
385 /* Wait for DMA completion */
386 while (dma_poll());
387 }
388
389 return frames;
390}
391
392unsigned int
393e1_tx_level(void)
394{
395 return e1f_valid_frames(&g_e1.tx.fifo);
396}
397
398unsigned int
399e1_rx_level(void)
400{
401 return e1f_valid_frames(&g_e1.rx.fifo);
402}
403
404void
405e1_poll(void)
406{
407 uint32_t bd;
408 unsigned int ofs;
409
410 /* Active ? */
411 if ((g_e1.rx.state == IDLE) && (g_e1.tx.state == IDLE))
412 return;
413
414 /* HACK: LED link status */
Harald Welte175b37c2020-12-14 17:03:12 +0100415 if (e1_regs->rx.csr & E1_RX_SR_ALIGNED)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200416 led_color(0, 48, 0);
417 else
418 led_color(48, 0, 0);
419
420 /* Recover any done TX BD */
421 while ( (bd = e1_regs->tx.bd) & E1_BD_VALID ) {
422 e1f_multiframe_read_discard(&g_e1.tx.fifo);
423 g_e1.tx.in_flight--;
424 }
425
426 /* Recover any done RX BD */
427 while ( (bd = e1_regs->rx.bd) & E1_BD_VALID ) {
428 /* FIXME: CRC status ? */
429 e1f_multiframe_write_commit(&g_e1.rx.fifo);
430 if ((bd & (E1_BD_CRC0 | E1_BD_CRC1)) != (E1_BD_CRC0 | E1_BD_CRC1))
431 printf("b: %03x\n", bd);
432 g_e1.rx.in_flight--;
433 }
434
435 /* Boot procedure */
436 if (g_e1.tx.state == BOOT) {
437 if (e1f_unseen_frames(&g_e1.tx.fifo) < (16 * 5))
438 return;
439 /* HACK: LED flow status */
440 led_blink(true, 200, 1000);
441 led_breathe(true, 100, 200);
442 }
443
444 /* Handle RX */
445 /* Misalign ? */
446 if (g_e1.rx.state == RUN) {
447 if (!(e1_regs->rx.csr & E1_RX_SR_ALIGNED)) {
448 printf("[!] E1 rx misalign\n");
449 g_e1.rx.state = RECOVER;
450 }
451 }
452
453 /* Overflow ? */
454 if (g_e1.rx.state == RUN) {
455 if (e1_regs->rx.csr & E1_RX_SR_OVFL) {
456 printf("[!] E1 overflow %d\n", g_e1.rx.in_flight);
457 g_e1.rx.state = RECOVER;
458 }
459 }
460
461 /* Recover ready ? */
462 if (g_e1.rx.state == RECOVER) {
463 if (g_e1.rx.in_flight != 0)
464 goto done_rx;
465 e1f_multiframe_empty(&g_e1.rx.fifo);
466 }
467
468 /* Fill new RX BD */
469 while (g_e1.rx.in_flight < 4) {
470 if (!e1f_multiframe_write_prepare(&g_e1.rx.fifo, &ofs))
471 break;
472 e1_regs->rx.bd = e1f_ofs_to_mf(ofs);
473 g_e1.rx.in_flight++;
474 }
475
476 /* Clear overflow if needed */
477 if (g_e1.rx.state != RUN) {
478 e1_regs->rx.csr = g_e1.rx.cr | E1_RX_CR_OVFL_CLR;
479 g_e1.rx.state = RUN;
480 }
481done_rx:
482
483 /* Handle TX */
484 /* Underflow ? */
485 if (g_e1.tx.state == RUN) {
486 if (e1_regs->tx.csr & E1_TX_SR_UNFL) {
487 printf("[!] E1 underflow %d\n", g_e1.tx.in_flight);
488 g_e1.tx.state = RECOVER;
489 }
490 }
491
492 /* Recover ready ? */
493 if (g_e1.tx.state == RECOVER) {
494 if (e1f_unseen_frames(&g_e1.tx.fifo) < (16 * 5))
495 return;
496 }
497
498 /* Fill new TX BD */
499 while (g_e1.tx.in_flight < 4) {
500 if (!e1f_multiframe_read_peek(&g_e1.tx.fifo, &ofs))
501 break;
502 e1_regs->tx.bd = e1f_ofs_to_mf(ofs);
503 g_e1.tx.in_flight++;
504 }
505
506 /* Clear underflow if needed */
507 if (g_e1.tx.state != RUN) {
508 e1_regs->tx.csr = g_e1.tx.cr | E1_TX_CR_UNFL_CLR;
509 g_e1.tx.state = RUN;
510 }
511}
512
513void
514e1_debug_print(bool data)
515{
516 volatile uint8_t *p;
517
518 puts("E1\n");
519 printf("CSR: Rx %04x / Tx %04x\n", e1_regs->rx.csr, e1_regs->tx.csr);
520 printf("InF: Rx %d / Tx %d\n", g_e1.rx.in_flight, g_e1.tx.in_flight);
521 printf("Sta: Rx %d / Tx %d\n", g_e1.rx.state, g_e1.tx.state);
522
523 e1f_debug(&g_e1.rx.fifo, "Rx FIFO");
524 e1f_debug(&g_e1.tx.fifo, "Tx FIFO");
525
526 if (data) {
527 puts("\nE1 Data\n");
528 for (int f=0; f<16; f++) {
529 p = e1_data_ptr(0, f, 0);
530 for (int ts=0; ts<32; ts++)
531 printf(" %02x", p[ts]);
532 printf("\n");
533 }
534 }
535}