blob: 1d8fd30daee0b8305d5bf7daf2c5e264bbefc292 [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
19
20
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020021static volatile struct e1_core * const e1_regs = (void *)(E1_CORE_BASE);
22static volatile uint8_t * const e1_data = (void *)(E1_DATA_BASE);
23
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020024unsigned int
25e1_data_ofs(int mf, int frame, int ts)
26{
27 return (mf << 9) | (frame << 5) | ts;
28}
29
Harald Weltea59ef2b2020-12-14 17:02:13 +010030volatile uint8_t *
31e1_data_ptr(int mf, int frame, int ts)
32{
33 return &e1_data[e1_data_ofs(mf, frame, ts)];
34}
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020035
36// FIFOs
37// -----
38/* Note: FIFO works at 'frame' level (i.e. 32 bytes) */
39
40struct e1_fifo {
41 /* Buffer zone associated with the FIFO */
42 unsigned int base;
43 unsigned int mask;
44
45 /* Pointers / Levels */
46 unsigned int wptr[2]; /* 0=committed 1=allocated */
47 unsigned int rptr[2]; /* 0=discared 1=peeked */
48};
49
50 /* Utils */
51static void
52e1f_reset(struct e1_fifo *fifo, unsigned int base, unsigned int len)
53{
54 memset(fifo, 0x00, sizeof(struct e1_fifo));
55 fifo->base = base;
56 fifo->mask = len - 1;
57}
58
59static unsigned int
60e1f_allocd_frames(struct e1_fifo *fifo)
61{
62 /* Number of frames that are allocated (i.e. where we can't write to) */
63 return (fifo->wptr[1] - fifo->rptr[0]) & fifo->mask;
64}
65
66static unsigned int
67e1f_valid_frames(struct e1_fifo *fifo)
68{
69 /* Number of valid frames */
70 return (fifo->wptr[0] - fifo->rptr[0]) & fifo->mask;
71}
72
73static unsigned int
74e1f_unseen_frames(struct e1_fifo *fifo)
75{
76 /* Number of valid frames that haven't been peeked yet */
77 return (fifo->wptr[0] - fifo->rptr[1]) & fifo->mask;
78}
79
80static unsigned int
81e1f_free_frames(struct e1_fifo *fifo)
82{
83 /* Number of frames that aren't allocated */
84 return (fifo->rptr[0] - fifo->wptr[1] - 1) & fifo->mask;
85}
86
87static unsigned int
88e1f_ofs_to_dma(unsigned int ofs)
89{
90 /* DMA address are 32-bits word address. Offsets are 32 byte address */
91 return (ofs << 3);
92}
93
94static unsigned int
95e1f_ofs_to_mf(unsigned int ofs)
96{
97 /* E1 Buffer Descriptors are always multiframe aligned */
98 return (ofs >> 4);
99}
100
101
102 /* Debug */
103static void
104e1f_debug(struct e1_fifo *fifo, const char *name)
105{
106 unsigned int la, lv, lu, lf;
107
108 la = e1f_allocd_frames(fifo);
109 lv = e1f_valid_frames(fifo);
110 lu = e1f_unseen_frames(fifo);
111 lf = e1f_free_frames(fifo);
112
113 printf("%s: R: %u / %u | W: %u / %u | A:%u V:%u U:%u F:%u\n",
114 name,
115 fifo->rptr[0], fifo->rptr[1], fifo->wptr[0], fifo->wptr[1],
116 la, lv, lu, lf
117 );
118}
119
120 /* Frame level read/write */
121static unsigned int
122e1f_frame_write(struct e1_fifo *fifo, unsigned int *ofs, unsigned int max_frames)
123{
124 unsigned int lf, le;
125
126 lf = e1f_free_frames(fifo);
127 le = fifo->mask - fifo->wptr[0] + 1;
128
129 if (max_frames > le)
130 max_frames = le;
131 if (max_frames > lf)
132 max_frames = lf;
133
134 *ofs = fifo->base + fifo->wptr[0];
135 fifo->wptr[1] = fifo->wptr[0] = (fifo->wptr[0] + max_frames) & fifo->mask;
136
137 return max_frames;
138}
139
140static unsigned int
Sylvain Munautde20fb72020-10-29 13:24:50 +0100141e1f_frame_read(struct e1_fifo *fifo, unsigned int *ofs, unsigned int max_frames)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200142{
143 unsigned int lu, le;
144
145 lu = e1f_unseen_frames(fifo);
146 le = fifo->mask - fifo->rptr[1] + 1;
147
148 if (max_frames > le)
149 max_frames = le;
150 if (max_frames > lu)
151 max_frames = lu;
152
153 *ofs = fifo->base + fifo->rptr[1];
154 fifo->rptr[0] = fifo->rptr[1] = (fifo->rptr[1] + max_frames) & fifo->mask;
155
156 return max_frames;
157}
158
159
160 /* MultiFrame level split read/write */
161static bool
162e1f_multiframe_write_prepare(struct e1_fifo *fifo, unsigned int *ofs)
163{
164 unsigned int lf;
165
166 lf = e1f_free_frames(fifo);
167 if (lf < 16)
168 return false;
169
170 *ofs = fifo->base + fifo->wptr[1];
171 fifo->wptr[1] = (fifo->wptr[1] + 16) & fifo->mask;
172
173 return true;
174}
175
176static void
177e1f_multiframe_write_commit(struct e1_fifo *fifo)
178{
179 fifo->wptr[0] = (fifo->wptr[0] + 16) & fifo->mask;
180}
181
182static bool
183e1f_multiframe_read_peek(struct e1_fifo *fifo, unsigned int *ofs)
184{
185 unsigned int lu;
186
187 lu = e1f_unseen_frames(fifo);
188 if (lu < 16)
189 return false;
190
191 *ofs = fifo->base + fifo->rptr[1];
192 fifo->rptr[1] = (fifo->rptr[1] + 16) & fifo->mask;
193
194 return true;
195}
196
197static void
198e1f_multiframe_read_discard(struct e1_fifo *fifo)
199{
200 fifo->rptr[0] = (fifo->rptr[0] + 16) & fifo->mask;
201}
202
203static void
204e1f_multiframe_empty(struct e1_fifo *fifo)
205{
206 fifo->rptr[0] = fifo->rptr[1] = (fifo->wptr[0] & ~15);
207}
208
209
210
211// Main logic
212// ----------
213
214enum e1_pipe_state {
Harald Welte30fc5602020-12-14 15:56:28 +0100215 IDLE = 0, /* not yet initialized */
216 BOOT = 1, /* after e1_init(), regiters are programmed */
217 RUN = 2, /* normal operation */
218 RECOVER = 3, /* after underflow, overflow or alignment error */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200219};
220
221static struct {
222 struct {
223 uint32_t cr;
224 struct e1_fifo fifo;
225 int in_flight;
226 enum e1_pipe_state state;
227 } rx;
228
229 struct {
230 uint32_t cr;
231 struct e1_fifo fifo;
232 int in_flight;
233 enum e1_pipe_state state;
234 } tx;
235} g_e1;
236
237
238
239
240void
241e1_init(bool clk_mode)
242{
243 /* Global state init */
244 memset(&g_e1, 0x00, sizeof(g_e1));
245
246 /* Reset FIFOs */
247 e1f_reset(&g_e1.rx.fifo, 0, 128);
248 e1f_reset(&g_e1.tx.fifo, 128, 128);
249
250 /* Enable Rx */
Harald Welte7f74fe62020-12-27 14:33:37 +0100251 g_e1.rx.cr = E1_RX_CR_MODE_MFA |
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200252 E1_RX_CR_ENABLE;
Harald Welte7f74fe62020-12-27 14:33:37 +0100253 e1_regs->rx.csr = E1_RX_CR_OVFL_CLR | g_e1.rx.cr;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200254
255 /* Enable Tx */
Harald Welte7f74fe62020-12-27 14:33:37 +0100256 g_e1.tx.cr = (clk_mode ? E1_TX_CR_TICK_REMOTE : E1_TX_CR_TICK_LOCAL) |
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200257 E1_TX_CR_MODE_TS0_CRC_E |
258 E1_TX_CR_ENABLE;
Harald Welte7f74fe62020-12-27 14:33:37 +0100259 e1_regs->tx.csr = E1_TX_CR_UNFL_CLR | g_e1.tx.cr;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200260
261 /* State */
262 g_e1.rx.state = BOOT;
263 g_e1.tx.state = BOOT;
264}
265
Harald Welte6add0aa2020-12-16 00:02:11 +0100266#define TXCR_PERMITTED ( \
267 E1_TX_CR_MODE_TS0_CRC_E | \
268 E1_TX_CR_TICK_REMOTE | \
269 E1_TX_CR_ALARM | \
270 E1_TX_CR_LOOPBACK | \
271 E1_TX_CR_LOOPBACK_CROSS )
272
273void
274e1_tx_config(uint16_t cr)
275{
276 g_e1.tx.cr = (g_e1.tx.cr & ~TXCR_PERMITTED) | (cr & TXCR_PERMITTED);
277 e1_regs->tx.csr = g_e1.tx.cr;
278}
279
280#define RXCR_PERMITTED ( \
281 E1_RX_CR_MODE_MFA )
282
283void
284e1_rx_config(uint16_t cr)
285{
286 g_e1.rx.cr = (g_e1.rx.cr & ~RXCR_PERMITTED) | (cr & RXCR_PERMITTED);
287 e1_regs->rx.csr = g_e1.rx.cr;
288}
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200289
290#include "dma.h"
291
292unsigned int
Harald Weltedaff4f62020-12-14 17:39:23 +0100293e1_rx_need_data(unsigned int usb_addr, unsigned int max_frames, unsigned int *pos)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200294{
295 unsigned int ofs;
296 int tot_frames = 0;
297 int n_frames;
298
299 while (max_frames) {
300 /* Get some data from the FIFO */
301 n_frames = e1f_frame_read(&g_e1.rx.fifo, &ofs, max_frames);
302 if (!n_frames)
303 break;
304
Harald Weltedaff4f62020-12-14 17:39:23 +0100305 /* Give pos */
306 if (pos) {
307 *pos = ofs & g_e1.rx.fifo.mask;
308 pos = NULL;
309 }
310
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200311 /* Copy from FIFO to USB */
312 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), false, NULL, NULL);
313
314 /* Prepare Next */
315 usb_addr += n_frames * (32 / 4);
316 max_frames -= n_frames;
317 tot_frames += n_frames;
318
319 /* Wait for DMA completion */
320 while (dma_poll());
321 }
322
323 return tot_frames;
324}
325
326unsigned int
327e1_tx_feed_data(unsigned int usb_addr, unsigned int frames)
328{
329 unsigned int ofs;
330 int n_frames;
331
332 while (frames) {
333 /* Get some space in FIFO */
334 n_frames = e1f_frame_write(&g_e1.tx.fifo, &ofs, frames);
335 if (!n_frames) {
336 printf("[!] TX FIFO Overflow %d %d\n", frames, n_frames);
337 break;
338 }
339
340 /* Copy from USB to FIFO */
341 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), true, NULL, NULL);
342
343 /* Prepare next */
344 usb_addr += n_frames * (32 / 4);
345 frames -= n_frames;
346
347 /* Wait for DMA completion */
348 while (dma_poll());
349 }
350
351 return frames;
352}
353
354unsigned int
355e1_tx_level(void)
356{
357 return e1f_valid_frames(&g_e1.tx.fifo);
358}
359
360unsigned int
361e1_rx_level(void)
362{
363 return e1f_valid_frames(&g_e1.rx.fifo);
364}
365
366void
367e1_poll(void)
368{
369 uint32_t bd;
370 unsigned int ofs;
371
372 /* Active ? */
373 if ((g_e1.rx.state == IDLE) && (g_e1.tx.state == IDLE))
374 return;
375
376 /* HACK: LED link status */
Harald Welte52765672020-12-15 18:35:42 +0100377 if (e1_regs->rx.csr & E1_RX_SR_ALIGNED) {
378 e1_platform_led_set(0, E1P_LED_GREEN, E1P_LED_ST_ON);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200379 led_color(0, 48, 0);
Harald Welte52765672020-12-15 18:35:42 +0100380 } else {
381 e1_platform_led_set(0, E1P_LED_GREEN, E1P_LED_ST_BLINK);
382 /* TODO: completely off if rx tick counter not incrementing */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200383 led_color(48, 0, 0);
Harald Welte52765672020-12-15 18:35:42 +0100384 }
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200385
386 /* Recover any done TX BD */
387 while ( (bd = e1_regs->tx.bd) & E1_BD_VALID ) {
388 e1f_multiframe_read_discard(&g_e1.tx.fifo);
389 g_e1.tx.in_flight--;
390 }
391
392 /* Recover any done RX BD */
393 while ( (bd = e1_regs->rx.bd) & E1_BD_VALID ) {
394 /* FIXME: CRC status ? */
395 e1f_multiframe_write_commit(&g_e1.rx.fifo);
396 if ((bd & (E1_BD_CRC0 | E1_BD_CRC1)) != (E1_BD_CRC0 | E1_BD_CRC1))
397 printf("b: %03x\n", bd);
398 g_e1.rx.in_flight--;
399 }
400
401 /* Boot procedure */
402 if (g_e1.tx.state == BOOT) {
403 if (e1f_unseen_frames(&g_e1.tx.fifo) < (16 * 5))
404 return;
405 /* HACK: LED flow status */
406 led_blink(true, 200, 1000);
407 led_breathe(true, 100, 200);
408 }
409
410 /* Handle RX */
411 /* Misalign ? */
412 if (g_e1.rx.state == RUN) {
413 if (!(e1_regs->rx.csr & E1_RX_SR_ALIGNED)) {
414 printf("[!] E1 rx misalign\n");
415 g_e1.rx.state = RECOVER;
416 }
417 }
418
419 /* Overflow ? */
420 if (g_e1.rx.state == RUN) {
421 if (e1_regs->rx.csr & E1_RX_SR_OVFL) {
422 printf("[!] E1 overflow %d\n", g_e1.rx.in_flight);
423 g_e1.rx.state = RECOVER;
424 }
425 }
426
427 /* Recover ready ? */
428 if (g_e1.rx.state == RECOVER) {
429 if (g_e1.rx.in_flight != 0)
430 goto done_rx;
431 e1f_multiframe_empty(&g_e1.rx.fifo);
432 }
433
434 /* Fill new RX BD */
435 while (g_e1.rx.in_flight < 4) {
436 if (!e1f_multiframe_write_prepare(&g_e1.rx.fifo, &ofs))
437 break;
438 e1_regs->rx.bd = e1f_ofs_to_mf(ofs);
439 g_e1.rx.in_flight++;
440 }
441
442 /* Clear overflow if needed */
443 if (g_e1.rx.state != RUN) {
444 e1_regs->rx.csr = g_e1.rx.cr | E1_RX_CR_OVFL_CLR;
445 g_e1.rx.state = RUN;
446 }
447done_rx:
448
449 /* Handle TX */
450 /* Underflow ? */
451 if (g_e1.tx.state == RUN) {
452 if (e1_regs->tx.csr & E1_TX_SR_UNFL) {
453 printf("[!] E1 underflow %d\n", g_e1.tx.in_flight);
454 g_e1.tx.state = RECOVER;
455 }
456 }
457
458 /* Recover ready ? */
459 if (g_e1.tx.state == RECOVER) {
460 if (e1f_unseen_frames(&g_e1.tx.fifo) < (16 * 5))
461 return;
462 }
463
464 /* Fill new TX BD */
465 while (g_e1.tx.in_flight < 4) {
466 if (!e1f_multiframe_read_peek(&g_e1.tx.fifo, &ofs))
467 break;
468 e1_regs->tx.bd = e1f_ofs_to_mf(ofs);
469 g_e1.tx.in_flight++;
470 }
471
472 /* Clear underflow if needed */
473 if (g_e1.tx.state != RUN) {
474 e1_regs->tx.csr = g_e1.tx.cr | E1_TX_CR_UNFL_CLR;
475 g_e1.tx.state = RUN;
476 }
477}
478
479void
480e1_debug_print(bool data)
481{
482 volatile uint8_t *p;
483
484 puts("E1\n");
485 printf("CSR: Rx %04x / Tx %04x\n", e1_regs->rx.csr, e1_regs->tx.csr);
486 printf("InF: Rx %d / Tx %d\n", g_e1.rx.in_flight, g_e1.tx.in_flight);
487 printf("Sta: Rx %d / Tx %d\n", g_e1.rx.state, g_e1.tx.state);
488
489 e1f_debug(&g_e1.rx.fifo, "Rx FIFO");
490 e1f_debug(&g_e1.tx.fifo, "Tx FIFO");
491
492 if (data) {
493 puts("\nE1 Data\n");
494 for (int f=0; f<16; f++) {
495 p = e1_data_ptr(0, f, 0);
496 for (int ts=0; ts<32; ts++)
497 printf(" %02x", p[ts]);
498 printf("\n");
499 }
500 }
501}