blob: 0698de5dc9761063b2fd4750c1c0fbeb8d128b9b [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;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020028} g_usb_e1;
29
Harald Welte9469e042020-12-15 23:09:40 +010030/* default configuration at power-up */
31static const struct ice1usb_tx_config tx_cfg_default = {
32 .mode = ICE1USB_TX_MODE_TS0_CRC4_E,
33 .timing = ICE1USB_TX_TIME_SRC_LOCAL,
34 .ext_loopback = ICE1USB_TX_EXT_LOOPBACK_OFF,
35 .alarm = 0,
36};
37
38static const struct ice1usb_rx_config rx_cfg_default = {
39 .mode = ICE1USB_RX_MODE_MULTIFRAME,
40};
41
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020042
43/* Hack */
Harald Weltedaff4f62020-12-14 17:39:23 +010044unsigned int e1_rx_need_data(unsigned int usb_addr, unsigned int max_len, unsigned int *pos);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020045unsigned int e1_tx_feed_data(unsigned int usb_addr, unsigned int len);
46unsigned int e1_tx_level(void);
47unsigned int e1_rx_level(void);
48/* ---- */
49
50bool
51usb_ep_boot(const struct usb_intf_desc *intf, uint8_t ep_addr, bool dual_bd);
52
53static void
54_usb_fill_feedback_ep(void)
55{
56 static uint16_t ticks_prev = 0;
57 uint16_t ticks;
58 uint32_t val = 8192;
59 unsigned int level;
60
Harald Welte2b7dadf2020-12-14 15:56:15 +010061 /* Compute real E1 tick count (with safety against bad values) */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020062 ticks = e1_tick_read();
63 val = (ticks - ticks_prev) & 0xffff;
64 ticks_prev = ticks;
65 if ((val < 7168) | (val > 9216))
66 val = 8192;
67
68 /* Bias depending on TX fifo level */
69 level = e1_tx_level();
70 if (level < (3 * 16))
71 val += 256;
72 else if (level > (8 * 16))
73 val -= 256;
74
75 /* Prepare buffer */
76 usb_data_write(64, &val, 4);
77 usb_ep_regs[1].in.bd[0].ptr = 64;
78 usb_ep_regs[1].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(3);
79}
80
81
82void
83usb_e1_run(void)
84{
85 int bdi;
86
87 if (!g_usb_e1.running)
88 return;
89
90 /* EP2 IN */
91 bdi = g_usb_e1.in_bdi;
92
93 while ((usb_ep_regs[2].in.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
94 {
95 uint32_t ptr = usb_ep_regs[2].in.bd[bdi].ptr;
96 uint32_t hdr;
Harald Welte2df1f802020-12-14 17:46:53 +010097 unsigned int pos;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020098
99 /* Error check */
100 if ((usb_ep_regs[2].in.bd[bdi].csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR)
101 puts("Err EP2 IN\n");
102
103 /* Get some data from E1 */
104 int n = e1_rx_level();
105
106 if (n > 64)
107 n = 12;
108 else if (n > 32)
109 n = 10;
110 else if (n > 8)
111 n = 8;
112 else if (!n)
113 break;
114
Harald Welte2df1f802020-12-14 17:46:53 +0100115 n = e1_rx_need_data((ptr >> 2) + 1, n, &pos);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200116
Harald Welte2df1f802020-12-14 17:46:53 +0100117 /* Write header: currently version and pos (mfr/fr number) */
118 hdr = (0 << 28) | (pos & 0xff);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200119 usb_data_write(ptr, &hdr, 4);
120
121 /* Resubmit */
122 usb_ep_regs[2].in.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN((n * 32) + 4);
123
124 /* Next BDI */
125 bdi ^= 1;
126 g_usb_e1.in_bdi = bdi;
127 }
128
129 /* EP1 OUT */
130 bdi = g_usb_e1.out_bdi;
131
132 while ((usb_ep_regs[1].out.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
133 {
134 uint32_t ptr = usb_ep_regs[1].out.bd[bdi].ptr;
135 uint32_t csr = usb_ep_regs[1].out.bd[bdi].csr;
136 uint32_t hdr;
137
138 /* Error check */
139 if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR) {
140 puts("Err EP1 OUT\n");
141 goto refill;
142 }
143
144 /* Grab header */
145 usb_data_read(&hdr, ptr, 4);
146
147 /* Empty data into the FIFO */
148 int n = ((csr & USB_BD_LEN_MSK) - 4) / 32;
149 n = e1_tx_feed_data((ptr >> 2) + 1, n);
150
151refill:
152 /* Refill it */
153 usb_ep_regs[1].out.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
154
155 /* Next BDI */
156 bdi ^= 1;
157 g_usb_e1.out_bdi = bdi;
158
159 static int x = 0;
160 if ((x++ & 0xff) == 0xff)
161 puts(".");
162 }
163
164 /* EP1 IN */
165 if ((usb_ep_regs[1].in.bd[0].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
166 {
167 _usb_fill_feedback_ep();
168 }
169}
170
171static const struct usb_intf_desc *
172_find_intf(const struct usb_conf_desc *conf, uint8_t idx)
173{
174 const struct usb_intf_desc *intf = NULL;
175 const void *sod, *eod;
176
177 if (!conf)
178 return NULL;
179
180 sod = conf;
181 eod = sod + conf->wTotalLength;
182
183 while (1) {
184 sod = usb_desc_find(sod, eod, USB_DT_INTF);
185 if (!sod)
186 break;
187
188 intf = (void*)sod;
189 if (intf->bInterfaceNumber == idx)
190 return intf;
191
192 sod = usb_desc_next(sod);
193 }
194
195 return NULL;
196}
Harald Weltec8271852020-12-14 13:48:09 +0100197
198static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200199_e1_set_conf(const struct usb_conf_desc *conf)
200{
201 const struct usb_intf_desc *intf;
202
203 printf("e1 set_conf %08x\n", conf);
204 if (!conf)
205 return USB_FND_SUCCESS;
206
207 intf = _find_intf(conf, 0);
208 if (!intf)
209 return USB_FND_ERROR;
210
211 printf("e1 set_conf %08x\n", intf);
212
213 usb_ep_boot(intf, 0x01, true);
214 usb_ep_boot(intf, 0x81, true);
215 usb_ep_boot(intf, 0x82, true);
216
217 return USB_FND_SUCCESS;
218}
219
Harald Welte9469e042020-12-15 23:09:40 +0100220static void _perform_tx_config(void)
221{
222 const struct ice1usb_tx_config *cfg = &g_usb_e1.tx_cfg;
223 e1_tx_config( ((cfg->mode & 3) << 1) |
224 ((cfg->timing & 1) << 3) |
225 ((cfg->alarm & 1) << 4) |
226 ((cfg->ext_loopback & 3) << 5) );
227}
228
229static void _perform_rx_config(void)
230{
231 const struct ice1usb_rx_config *cfg = &g_usb_e1.rx_cfg;
232 e1_rx_config((cfg->mode << 1));
233}
234
Harald Weltec8271852020-12-14 13:48:09 +0100235static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200236_e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
237{
238 if (base->bInterfaceNumber != 0)
239 return USB_FND_CONTINUE;
240
241 if (sel->bAlternateSetting != 1)
242 return USB_FND_SUCCESS;
243
244 /* Hack to avoid re-setting while running ... avoid BD desync */
245 if (g_usb_e1.running)
246 return USB_FND_SUCCESS;
247
Harald Welte9469e042020-12-15 23:09:40 +0100248 _perform_rx_config();
249 _perform_tx_config();
250
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200251 g_usb_e1.running = true;
252
253 /* Configure EP1 OUT / EP2 IN */
254 usb_ep_regs[1].out.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */
255 usb_ep_regs[2].in.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */
256
257 /* Configure EP1 IN (feedback) */
258 usb_ep_regs[1].in.status = USB_EP_TYPE_ISOC; /* Type=Isochronous, single buffered */
259
260 /* EP2 IN: Prepare two buffers */
261 usb_ep_regs[2].in.bd[0].ptr = 1024;
262 usb_ep_regs[2].in.bd[0].csr = 0;
263
264 usb_ep_regs[2].in.bd[1].ptr = 1536;
265 usb_ep_regs[2].in.bd[1].csr = 0;
266
267 /* EP1 OUT: Queue two buffers */
268 usb_ep_regs[1].out.bd[0].ptr = 1024;
269 usb_ep_regs[1].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
270
271 usb_ep_regs[1].out.bd[1].ptr = 1536;
272 usb_ep_regs[1].out.bd[1].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
273
274 /* EP1 IN: Queue buffer */
275 _usb_fill_feedback_ep();
276
277 return USB_FND_SUCCESS;
278}
279
Harald Weltec8271852020-12-14 13:48:09 +0100280static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200281_e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
282{
283 if (base->bInterfaceNumber != 0)
284 return USB_FND_CONTINUE;
285
286 *alt = g_usb_e1.running ? 1 : 0;
287
288 return USB_FND_SUCCESS;
289}
290
Harald Welte9469e042020-12-15 23:09:40 +0100291static bool
292_set_tx_mode_done(struct usb_xfer *xfer)
293{
294 const struct ice1usb_tx_config *cfg = (const struct ice1usb_tx_config *) xfer->data;
295 printf("set_tx_mode %02x%02x%02x%02x\r\n",
296 xfer->data[0], xfer->data[1], xfer->data[2], xfer->data[3]);
297 g_usb_e1.tx_cfg = *cfg;
298 _perform_tx_config();
299 return true;
300}
301
302static bool
303_set_rx_mode_done(struct usb_xfer *xfer)
304{
305 const struct ice1usb_rx_config *cfg = (const struct ice1usb_rx_config *) xfer->data;
306 printf("set_rx_mode %02x\r\n", xfer->data[0]);
307 g_usb_e1.rx_cfg = *cfg;
308 _perform_rx_config();
309 return true;
310}
311
312/* per-interface requests */
313static enum usb_fnd_resp
314_e1_ctrl_req_intf(struct usb_ctrl_req *req, struct usb_xfer *xfer)
315{
316 unsigned int i;
317
318 switch (req->bRequest) {
319 case ICE1USB_INTF_GET_CAPABILITIES:
320 /* no optional capabilities yet */
321 xfer->len = 0;
322 break;
323 case ICE1USB_INTF_SET_TX_CFG:
324 if (req->wLength < sizeof(struct ice1usb_tx_config))
325 return USB_FND_ERROR;
326 xfer->cb_done = _set_tx_mode_done;
327 xfer->cb_ctx = req;
328 xfer->len = sizeof(struct ice1usb_tx_config);
329 break;
330 case ICE1USB_INTF_GET_TX_CFG:
331 if (req->wLength < sizeof(struct ice1usb_tx_config))
332 return USB_FND_ERROR;
333 memcpy(xfer->data, &g_usb_e1.tx_cfg, sizeof(struct ice1usb_tx_config));
334 xfer->len = sizeof(struct ice1usb_tx_config);
335 break;
336 case ICE1USB_INTF_SET_RX_CFG:
337 if (req->wLength < sizeof(struct ice1usb_rx_config))
338 return USB_FND_ERROR;
339 xfer->cb_done = _set_rx_mode_done;
340 xfer->cb_ctx = req;
341 xfer->len = sizeof(struct ice1usb_rx_config);
342 break;
343 case ICE1USB_INTF_GET_RX_CFG:
344 if (req->wLength < sizeof(struct ice1usb_rx_config))
345 return USB_FND_ERROR;
346 memcpy(xfer->data, &g_usb_e1.rx_cfg, sizeof(struct ice1usb_rx_config));
347 xfer->len = sizeof(struct ice1usb_rx_config);
348 break;
349 default:
350 return USB_FND_ERROR;
351 }
352
353 return USB_FND_SUCCESS;
354}
355
356/* device-global requests */
357static enum usb_fnd_resp
358_e1_ctrl_req_dev(struct usb_ctrl_req *req, struct usb_xfer *xfer)
359{
360 switch (req->bRequest) {
361 case ICE1USB_DEV_GET_CAPABILITIES:
362 xfer->data[0] = (1 << ICE1USB_DEV_CAP_GPSDO);
363 xfer->len = 1;
364 break;
365 default:
366 return USB_FND_ERROR;
367 }
368
369 return USB_FND_SUCCESS;
370}
371
372
373/* USB host issues a control request to us */
374static enum usb_fnd_resp
375_e1_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
376{
377 if (USB_REQ_TYPE(req) != USB_REQ_TYPE_VENDOR)
378 return USB_FND_CONTINUE;
379
380 switch (USB_REQ_RCPT(req)) {
381 case USB_REQ_RCPT_DEV:
382 return _e1_ctrl_req_dev(req, xfer);
383 case USB_REQ_RCPT_INTF:
384 if (req->wIndex != 0)
385 return USB_FND_ERROR;
386 return _e1_ctrl_req_intf(req, xfer);
387 case USB_REQ_RCPT_EP:
388 case USB_REQ_RCPT_OTHER:
389 default:
390 return USB_FND_ERROR;
391 }
392}
393
394
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200395static struct usb_fn_drv _e1_drv = {
396 .set_conf = _e1_set_conf,
397 .set_intf = _e1_set_intf,
398 .get_intf = _e1_get_intf,
Harald Welte9469e042020-12-15 23:09:40 +0100399 .ctrl_req = _e1_ctrl_req,
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200400};
401
402void
403usb_e1_init(void)
404{
405 memset(&g_usb_e1, 0x00, sizeof(g_usb_e1));
Harald Welte9469e042020-12-15 23:09:40 +0100406 g_usb_e1.tx_cfg = tx_cfg_default;
407 g_usb_e1.rx_cfg = rx_cfg_default;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200408 usb_register_function_driver(&_e1_drv);
409}