blob: b1771f5af8e4180b460d5befb532eac41f4a71f5 [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
266
267#include "dma.h"
268
269unsigned int
Harald Weltedaff4f62020-12-14 17:39:23 +0100270e1_rx_need_data(unsigned int usb_addr, unsigned int max_frames, unsigned int *pos)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200271{
272 unsigned int ofs;
273 int tot_frames = 0;
274 int n_frames;
275
276 while (max_frames) {
277 /* Get some data from the FIFO */
278 n_frames = e1f_frame_read(&g_e1.rx.fifo, &ofs, max_frames);
279 if (!n_frames)
280 break;
281
Harald Weltedaff4f62020-12-14 17:39:23 +0100282 /* Give pos */
283 if (pos) {
284 *pos = ofs & g_e1.rx.fifo.mask;
285 pos = NULL;
286 }
287
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200288 /* Copy from FIFO to USB */
289 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), false, NULL, NULL);
290
291 /* Prepare Next */
292 usb_addr += n_frames * (32 / 4);
293 max_frames -= n_frames;
294 tot_frames += n_frames;
295
296 /* Wait for DMA completion */
297 while (dma_poll());
298 }
299
300 return tot_frames;
301}
302
303unsigned int
304e1_tx_feed_data(unsigned int usb_addr, unsigned int frames)
305{
306 unsigned int ofs;
307 int n_frames;
308
309 while (frames) {
310 /* Get some space in FIFO */
311 n_frames = e1f_frame_write(&g_e1.tx.fifo, &ofs, frames);
312 if (!n_frames) {
313 printf("[!] TX FIFO Overflow %d %d\n", frames, n_frames);
314 break;
315 }
316
317 /* Copy from USB to FIFO */
318 dma_exec(e1f_ofs_to_dma(ofs), usb_addr, n_frames * (32 / 4), true, NULL, NULL);
319
320 /* Prepare next */
321 usb_addr += n_frames * (32 / 4);
322 frames -= n_frames;
323
324 /* Wait for DMA completion */
325 while (dma_poll());
326 }
327
328 return frames;
329}
330
331unsigned int
332e1_tx_level(void)
333{
334 return e1f_valid_frames(&g_e1.tx.fifo);
335}
336
337unsigned int
338e1_rx_level(void)
339{
340 return e1f_valid_frames(&g_e1.rx.fifo);
341}
342
343void
344e1_poll(void)
345{
346 uint32_t bd;
347 unsigned int ofs;
348
349 /* Active ? */
350 if ((g_e1.rx.state == IDLE) && (g_e1.tx.state == IDLE))
351 return;
352
353 /* HACK: LED link status */
Harald Welte52765672020-12-15 18:35:42 +0100354 if (e1_regs->rx.csr & E1_RX_SR_ALIGNED) {
355 e1_platform_led_set(0, E1P_LED_GREEN, E1P_LED_ST_ON);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200356 led_color(0, 48, 0);
Harald Welte52765672020-12-15 18:35:42 +0100357 } else {
358 e1_platform_led_set(0, E1P_LED_GREEN, E1P_LED_ST_BLINK);
359 /* TODO: completely off if rx tick counter not incrementing */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200360 led_color(48, 0, 0);
Harald Welte52765672020-12-15 18:35:42 +0100361 }
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200362
363 /* Recover any done TX BD */
364 while ( (bd = e1_regs->tx.bd) & E1_BD_VALID ) {
365 e1f_multiframe_read_discard(&g_e1.tx.fifo);
366 g_e1.tx.in_flight--;
367 }
368
369 /* Recover any done RX BD */
370 while ( (bd = e1_regs->rx.bd) & E1_BD_VALID ) {
371 /* FIXME: CRC status ? */
372 e1f_multiframe_write_commit(&g_e1.rx.fifo);
373 if ((bd & (E1_BD_CRC0 | E1_BD_CRC1)) != (E1_BD_CRC0 | E1_BD_CRC1))
374 printf("b: %03x\n", bd);
375 g_e1.rx.in_flight--;
376 }
377
378 /* Boot procedure */
379 if (g_e1.tx.state == BOOT) {
380 if (e1f_unseen_frames(&g_e1.tx.fifo) < (16 * 5))
381 return;
382 /* HACK: LED flow status */
383 led_blink(true, 200, 1000);
384 led_breathe(true, 100, 200);
385 }
386
387 /* Handle RX */
388 /* Misalign ? */
389 if (g_e1.rx.state == RUN) {
390 if (!(e1_regs->rx.csr & E1_RX_SR_ALIGNED)) {
391 printf("[!] E1 rx misalign\n");
392 g_e1.rx.state = RECOVER;
393 }
394 }
395
396 /* Overflow ? */
397 if (g_e1.rx.state == RUN) {
398 if (e1_regs->rx.csr & E1_RX_SR_OVFL) {
399 printf("[!] E1 overflow %d\n", g_e1.rx.in_flight);
400 g_e1.rx.state = RECOVER;
401 }
402 }
403
404 /* Recover ready ? */
405 if (g_e1.rx.state == RECOVER) {
406 if (g_e1.rx.in_flight != 0)
407 goto done_rx;
408 e1f_multiframe_empty(&g_e1.rx.fifo);
409 }
410
411 /* Fill new RX BD */
412 while (g_e1.rx.in_flight < 4) {
413 if (!e1f_multiframe_write_prepare(&g_e1.rx.fifo, &ofs))
414 break;
415 e1_regs->rx.bd = e1f_ofs_to_mf(ofs);
416 g_e1.rx.in_flight++;
417 }
418
419 /* Clear overflow if needed */
420 if (g_e1.rx.state != RUN) {
421 e1_regs->rx.csr = g_e1.rx.cr | E1_RX_CR_OVFL_CLR;
422 g_e1.rx.state = RUN;
423 }
424done_rx:
425
426 /* Handle TX */
427 /* Underflow ? */
428 if (g_e1.tx.state == RUN) {
429 if (e1_regs->tx.csr & E1_TX_SR_UNFL) {
430 printf("[!] E1 underflow %d\n", g_e1.tx.in_flight);
431 g_e1.tx.state = RECOVER;
432 }
433 }
434
435 /* Recover ready ? */
436 if (g_e1.tx.state == RECOVER) {
437 if (e1f_unseen_frames(&g_e1.tx.fifo) < (16 * 5))
438 return;
439 }
440
441 /* Fill new TX BD */
442 while (g_e1.tx.in_flight < 4) {
443 if (!e1f_multiframe_read_peek(&g_e1.tx.fifo, &ofs))
444 break;
445 e1_regs->tx.bd = e1f_ofs_to_mf(ofs);
446 g_e1.tx.in_flight++;
447 }
448
449 /* Clear underflow if needed */
450 if (g_e1.tx.state != RUN) {
451 e1_regs->tx.csr = g_e1.tx.cr | E1_TX_CR_UNFL_CLR;
452 g_e1.tx.state = RUN;
453 }
454}
455
456void
457e1_debug_print(bool data)
458{
459 volatile uint8_t *p;
460
461 puts("E1\n");
462 printf("CSR: Rx %04x / Tx %04x\n", e1_regs->rx.csr, e1_regs->tx.csr);
463 printf("InF: Rx %d / Tx %d\n", g_e1.rx.in_flight, g_e1.tx.in_flight);
464 printf("Sta: Rx %d / Tx %d\n", g_e1.rx.state, g_e1.tx.state);
465
466 e1f_debug(&g_e1.rx.fifo, "Rx FIFO");
467 e1f_debug(&g_e1.tx.fifo, "Tx FIFO");
468
469 if (data) {
470 puts("\nE1 Data\n");
471 for (int f=0; f<16; f++) {
472 p = e1_data_ptr(0, f, 0);
473 for (int ts=0; ts<32; ts++)
474 printf(" %02x", p[ts]);
475 printf("\n");
476 }
477 }
478}