blob: 11402c6c332e9f363aa0c7cb76cb59e68378c271 [file] [log] [blame]
Sylvain Munautbc9f5c42020-09-14 10:22:29 +02001/*
2 * usb_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 <no2usb/usb_hw.h>
13#include <no2usb/usb_priv.h>
14
15#include "console.h"
16#include "misc.h"
Harald Welte2df1f802020-12-14 17:46:53 +010017#include "e1.h"
Harald Welte9469e042020-12-15 23:09:40 +010018#include "e1_hw.h"
19
20#include "ice1usb_proto.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020021
22struct {
Harald Welte30fc5602020-12-14 15:56:28 +010023 bool running; /* are we running (transceiving USB data)? */
24 int out_bdi; /* buffer descriptor index for OUT EP */
25 int in_bdi; /* buffer descriptor index for IN EP */
Harald Welte9469e042020-12-15 23:09:40 +010026 struct ice1usb_tx_config tx_cfg;
27 struct ice1usb_rx_config rx_cfg;
Harald Welte805f2cf2020-12-14 17:31:03 +010028 struct e1_error_count last_err;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020029} g_usb_e1;
30
Harald Welte9469e042020-12-15 23:09:40 +010031/* default configuration at power-up */
32static const struct ice1usb_tx_config tx_cfg_default = {
33 .mode = ICE1USB_TX_MODE_TS0_CRC4_E,
34 .timing = ICE1USB_TX_TIME_SRC_LOCAL,
35 .ext_loopback = ICE1USB_TX_EXT_LOOPBACK_OFF,
36 .alarm = 0,
37};
38
39static const struct ice1usb_rx_config rx_cfg_default = {
40 .mode = ICE1USB_RX_MODE_MULTIFRAME,
41};
42
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020043
44/* Hack */
Harald Weltedaff4f62020-12-14 17:39:23 +010045unsigned int e1_rx_need_data(unsigned int usb_addr, unsigned int max_len, unsigned int *pos);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020046unsigned int e1_tx_feed_data(unsigned int usb_addr, unsigned int len);
47unsigned int e1_tx_level(void);
48unsigned int e1_rx_level(void);
49/* ---- */
50
51bool
52usb_ep_boot(const struct usb_intf_desc *intf, uint8_t ep_addr, bool dual_bd);
53
54static void
55_usb_fill_feedback_ep(void)
56{
57 static uint16_t ticks_prev = 0;
58 uint16_t ticks;
59 uint32_t val = 8192;
60 unsigned int level;
61
Harald Welte2b7dadf2020-12-14 15:56:15 +010062 /* Compute real E1 tick count (with safety against bad values) */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020063 ticks = e1_tick_read();
64 val = (ticks - ticks_prev) & 0xffff;
65 ticks_prev = ticks;
66 if ((val < 7168) | (val > 9216))
67 val = 8192;
68
69 /* Bias depending on TX fifo level */
70 level = e1_tx_level();
71 if (level < (3 * 16))
72 val += 256;
73 else if (level > (8 * 16))
74 val -= 256;
75
76 /* Prepare buffer */
77 usb_data_write(64, &val, 4);
78 usb_ep_regs[1].in.bd[0].ptr = 64;
79 usb_ep_regs[1].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(3);
80}
81
82
83void
84usb_e1_run(void)
85{
86 int bdi;
87
88 if (!g_usb_e1.running)
89 return;
90
Harald Welte805f2cf2020-12-14 17:31:03 +010091 /* EP3 IRQ */
92 if ((usb_ep_regs[3].in.bd[0].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) {
93 const struct e1_error_count *cur_err = e1_get_error_count();
94 if (memcmp(cur_err, &g_usb_e1.last_err, sizeof(*cur_err))) {
95 struct ice1usb_irq errmsg = {
96 .type = ICE1USB_IRQ_T_ERRCNT,
97 .u = {
98 .errors = {
99 .crc = cur_err->crc,
100 .align = cur_err->align,
101 .ovfl = cur_err->ovfl,
102 .unfl = cur_err->unfl,
103 .flags = cur_err->flags,
104 }
105 }
106 };
107 printf("E");
108 usb_data_write(usb_ep_regs[3].in.bd[0].ptr, &errmsg, sizeof(errmsg));
109 usb_ep_regs[3].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(sizeof(errmsg));
110 g_usb_e1.last_err = *cur_err;
111 }
112 }
113
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200114 /* EP2 IN */
115 bdi = g_usb_e1.in_bdi;
116
117 while ((usb_ep_regs[2].in.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
118 {
119 uint32_t ptr = usb_ep_regs[2].in.bd[bdi].ptr;
120 uint32_t hdr;
Harald Welte2df1f802020-12-14 17:46:53 +0100121 unsigned int pos;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200122
123 /* Error check */
124 if ((usb_ep_regs[2].in.bd[bdi].csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR)
125 puts("Err EP2 IN\n");
126
127 /* Get some data from E1 */
128 int n = e1_rx_level();
129
130 if (n > 64)
131 n = 12;
132 else if (n > 32)
133 n = 10;
134 else if (n > 8)
135 n = 8;
136 else if (!n)
137 break;
138
Harald Welte2df1f802020-12-14 17:46:53 +0100139 n = e1_rx_need_data((ptr >> 2) + 1, n, &pos);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200140
Harald Welte2df1f802020-12-14 17:46:53 +0100141 /* Write header: currently version and pos (mfr/fr number) */
142 hdr = (0 << 28) | (pos & 0xff);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200143 usb_data_write(ptr, &hdr, 4);
144
145 /* Resubmit */
146 usb_ep_regs[2].in.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN((n * 32) + 4);
147
148 /* Next BDI */
149 bdi ^= 1;
150 g_usb_e1.in_bdi = bdi;
151 }
152
153 /* EP1 OUT */
154 bdi = g_usb_e1.out_bdi;
155
156 while ((usb_ep_regs[1].out.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
157 {
158 uint32_t ptr = usb_ep_regs[1].out.bd[bdi].ptr;
159 uint32_t csr = usb_ep_regs[1].out.bd[bdi].csr;
160 uint32_t hdr;
161
162 /* Error check */
163 if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR) {
164 puts("Err EP1 OUT\n");
165 goto refill;
166 }
167
168 /* Grab header */
169 usb_data_read(&hdr, ptr, 4);
170
171 /* Empty data into the FIFO */
172 int n = ((csr & USB_BD_LEN_MSK) - 4) / 32;
173 n = e1_tx_feed_data((ptr >> 2) + 1, n);
174
175refill:
176 /* Refill it */
177 usb_ep_regs[1].out.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
178
179 /* Next BDI */
180 bdi ^= 1;
181 g_usb_e1.out_bdi = bdi;
182
183 static int x = 0;
184 if ((x++ & 0xff) == 0xff)
185 puts(".");
186 }
187
188 /* EP1 IN */
189 if ((usb_ep_regs[1].in.bd[0].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
190 {
191 _usb_fill_feedback_ep();
192 }
193}
194
195static const struct usb_intf_desc *
196_find_intf(const struct usb_conf_desc *conf, uint8_t idx)
197{
198 const struct usb_intf_desc *intf = NULL;
199 const void *sod, *eod;
200
201 if (!conf)
202 return NULL;
203
204 sod = conf;
205 eod = sod + conf->wTotalLength;
206
207 while (1) {
208 sod = usb_desc_find(sod, eod, USB_DT_INTF);
209 if (!sod)
210 break;
211
212 intf = (void*)sod;
213 if (intf->bInterfaceNumber == idx)
214 return intf;
215
216 sod = usb_desc_next(sod);
217 }
218
219 return NULL;
220}
Harald Weltec8271852020-12-14 13:48:09 +0100221
222static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200223_e1_set_conf(const struct usb_conf_desc *conf)
224{
225 const struct usb_intf_desc *intf;
226
227 printf("e1 set_conf %08x\n", conf);
228 if (!conf)
229 return USB_FND_SUCCESS;
230
231 intf = _find_intf(conf, 0);
232 if (!intf)
233 return USB_FND_ERROR;
234
235 printf("e1 set_conf %08x\n", intf);
236
237 usb_ep_boot(intf, 0x01, true);
238 usb_ep_boot(intf, 0x81, true);
239 usb_ep_boot(intf, 0x82, true);
Harald Welte805f2cf2020-12-14 17:31:03 +0100240 usb_ep_boot(intf, 0x83, true);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200241
242 return USB_FND_SUCCESS;
243}
244
Harald Welte9469e042020-12-15 23:09:40 +0100245static void _perform_tx_config(void)
246{
247 const struct ice1usb_tx_config *cfg = &g_usb_e1.tx_cfg;
248 e1_tx_config( ((cfg->mode & 3) << 1) |
249 ((cfg->timing & 1) << 3) |
250 ((cfg->alarm & 1) << 4) |
251 ((cfg->ext_loopback & 3) << 5) );
252}
253
254static void _perform_rx_config(void)
255{
256 const struct ice1usb_rx_config *cfg = &g_usb_e1.rx_cfg;
257 e1_rx_config((cfg->mode << 1));
258}
259
Harald Weltec8271852020-12-14 13:48:09 +0100260static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200261_e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
262{
263 if (base->bInterfaceNumber != 0)
264 return USB_FND_CONTINUE;
265
Harald Weltee6b0fe82020-12-17 22:22:05 +0100266 switch (sel->bAlternateSetting) {
267 case 0:
268 if (!g_usb_e1.running)
269 return USB_FND_SUCCESS;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200270
Harald Weltee6b0fe82020-12-17 22:22:05 +0100271 /* disable E1 rx/tx */
272 e1_init(0, 0);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200273
Harald Weltee6b0fe82020-12-17 22:22:05 +0100274 /* EP1 OUT */
275 usb_ep_regs[1].out.bd[0].csr = 0;
276 usb_ep_regs[1].out.bd[1].csr = 0;
Harald Welte9469e042020-12-15 23:09:40 +0100277
Harald Weltee6b0fe82020-12-17 22:22:05 +0100278 /* EP1 IN (feedback) */
279 usb_ep_regs[1].in.bd[0].csr = 0;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200280
Harald Weltee6b0fe82020-12-17 22:22:05 +0100281 /* EP2 IN (data) */
282 usb_ep_regs[2].in.bd[0].csr = 0;
283 usb_ep_regs[2].in.bd[1].csr = 0;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200284
Harald Weltee6b0fe82020-12-17 22:22:05 +0100285 /* EP3 IN: Interrupt */
286 usb_ep_regs[3].in.bd[0].csr = 0;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200287
Harald Weltee6b0fe82020-12-17 22:22:05 +0100288 g_usb_e1.running = false;
289 break;
290 case 1:
291 /* Hack to avoid re-setting while running ... avoid BD desync */
292 if (g_usb_e1.running)
293 return USB_FND_SUCCESS;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200294
Harald Weltee6b0fe82020-12-17 22:22:05 +0100295 _perform_rx_config();
296 _perform_tx_config();
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200297
Harald Weltee6b0fe82020-12-17 22:22:05 +0100298 g_usb_e1.running = true;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200299
Harald Weltee6b0fe82020-12-17 22:22:05 +0100300 /* Configure EP1 OUT / EP2 IN */
301 usb_ep_regs[1].out.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */
302 usb_ep_regs[2].in.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200303
Harald Weltee6b0fe82020-12-17 22:22:05 +0100304 /* Configure EP1 IN (feedback) */
305 usb_ep_regs[1].in.status = USB_EP_TYPE_ISOC; /* Type=Isochronous, single buffered */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200306
Harald Weltee6b0fe82020-12-17 22:22:05 +0100307 /* EP2 IN: Prepare two buffers */
308 g_usb_e1.in_bdi = 0;
309 usb_ep_regs[2].in.bd[0].ptr = 1024;
310 usb_ep_regs[2].in.bd[0].csr = 0;
311
312 usb_ep_regs[2].in.bd[1].ptr = 1536;
313 usb_ep_regs[2].in.bd[1].csr = 0;
314
315 /* EP1 OUT: Queue two buffers */
316 g_usb_e1.out_bdi = 0;
317 usb_ep_regs[1].out.bd[0].ptr = 1024;
318 usb_ep_regs[1].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
319
320 usb_ep_regs[1].out.bd[1].ptr = 1536;
321 usb_ep_regs[1].out.bd[1].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
322
323 /* EP1 IN: Queue buffer */
324 _usb_fill_feedback_ep();
325
326 /* EP3 IN: Interrupt */
327 usb_ep_regs[3].in.status = USB_EP_TYPE_INT;
328 usb_ep_regs[3].in.bd[0].ptr = 68;
329 usb_ep_regs[3].in.bd[0].csr = 0;
330 break;
331 default:
332 return USB_FND_ERROR;
333 }
Harald Welte805f2cf2020-12-14 17:31:03 +0100334
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200335 return USB_FND_SUCCESS;
336}
337
Harald Weltec8271852020-12-14 13:48:09 +0100338static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200339_e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
340{
341 if (base->bInterfaceNumber != 0)
342 return USB_FND_CONTINUE;
343
344 *alt = g_usb_e1.running ? 1 : 0;
345
346 return USB_FND_SUCCESS;
347}
348
Harald Welte9469e042020-12-15 23:09:40 +0100349static bool
350_set_tx_mode_done(struct usb_xfer *xfer)
351{
352 const struct ice1usb_tx_config *cfg = (const struct ice1usb_tx_config *) xfer->data;
353 printf("set_tx_mode %02x%02x%02x%02x\r\n",
354 xfer->data[0], xfer->data[1], xfer->data[2], xfer->data[3]);
355 g_usb_e1.tx_cfg = *cfg;
356 _perform_tx_config();
357 return true;
358}
359
360static bool
361_set_rx_mode_done(struct usb_xfer *xfer)
362{
363 const struct ice1usb_rx_config *cfg = (const struct ice1usb_rx_config *) xfer->data;
364 printf("set_rx_mode %02x\r\n", xfer->data[0]);
365 g_usb_e1.rx_cfg = *cfg;
366 _perform_rx_config();
367 return true;
368}
369
370/* per-interface requests */
371static enum usb_fnd_resp
372_e1_ctrl_req_intf(struct usb_ctrl_req *req, struct usb_xfer *xfer)
373{
374 unsigned int i;
375
376 switch (req->bRequest) {
377 case ICE1USB_INTF_GET_CAPABILITIES:
378 /* no optional capabilities yet */
379 xfer->len = 0;
380 break;
381 case ICE1USB_INTF_SET_TX_CFG:
382 if (req->wLength < sizeof(struct ice1usb_tx_config))
383 return USB_FND_ERROR;
384 xfer->cb_done = _set_tx_mode_done;
385 xfer->cb_ctx = req;
386 xfer->len = sizeof(struct ice1usb_tx_config);
387 break;
388 case ICE1USB_INTF_GET_TX_CFG:
389 if (req->wLength < sizeof(struct ice1usb_tx_config))
390 return USB_FND_ERROR;
391 memcpy(xfer->data, &g_usb_e1.tx_cfg, sizeof(struct ice1usb_tx_config));
392 xfer->len = sizeof(struct ice1usb_tx_config);
393 break;
394 case ICE1USB_INTF_SET_RX_CFG:
395 if (req->wLength < sizeof(struct ice1usb_rx_config))
396 return USB_FND_ERROR;
397 xfer->cb_done = _set_rx_mode_done;
398 xfer->cb_ctx = req;
399 xfer->len = sizeof(struct ice1usb_rx_config);
400 break;
401 case ICE1USB_INTF_GET_RX_CFG:
402 if (req->wLength < sizeof(struct ice1usb_rx_config))
403 return USB_FND_ERROR;
404 memcpy(xfer->data, &g_usb_e1.rx_cfg, sizeof(struct ice1usb_rx_config));
405 xfer->len = sizeof(struct ice1usb_rx_config);
406 break;
407 default:
408 return USB_FND_ERROR;
409 }
410
411 return USB_FND_SUCCESS;
412}
413
414/* device-global requests */
415static enum usb_fnd_resp
416_e1_ctrl_req_dev(struct usb_ctrl_req *req, struct usb_xfer *xfer)
417{
418 switch (req->bRequest) {
419 case ICE1USB_DEV_GET_CAPABILITIES:
420 xfer->data[0] = (1 << ICE1USB_DEV_CAP_GPSDO);
421 xfer->len = 1;
422 break;
423 default:
424 return USB_FND_ERROR;
425 }
426
427 return USB_FND_SUCCESS;
428}
429
430
431/* USB host issues a control request to us */
432static enum usb_fnd_resp
433_e1_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
434{
435 if (USB_REQ_TYPE(req) != USB_REQ_TYPE_VENDOR)
436 return USB_FND_CONTINUE;
437
438 switch (USB_REQ_RCPT(req)) {
439 case USB_REQ_RCPT_DEV:
440 return _e1_ctrl_req_dev(req, xfer);
441 case USB_REQ_RCPT_INTF:
442 if (req->wIndex != 0)
443 return USB_FND_ERROR;
444 return _e1_ctrl_req_intf(req, xfer);
445 case USB_REQ_RCPT_EP:
446 case USB_REQ_RCPT_OTHER:
447 default:
448 return USB_FND_ERROR;
449 }
450}
451
452
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200453static struct usb_fn_drv _e1_drv = {
454 .set_conf = _e1_set_conf,
455 .set_intf = _e1_set_intf,
456 .get_intf = _e1_get_intf,
Harald Welte9469e042020-12-15 23:09:40 +0100457 .ctrl_req = _e1_ctrl_req,
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200458};
459
460void
461usb_e1_init(void)
462{
463 memset(&g_usb_e1, 0x00, sizeof(g_usb_e1));
Harald Welte9469e042020-12-15 23:09:40 +0100464 g_usb_e1.tx_cfg = tx_cfg_default;
465 g_usb_e1.rx_cfg = rx_cfg_default;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200466 usb_register_function_driver(&_e1_drv);
467}