blob: a7ab7fb76e4c5fa81d58f40b9c545491bd0642a9 [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 Welte394b6802020-12-14 17:46:53 +010017#include "e1.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020018
19struct {
Harald Welte37701e92020-12-14 15:56:28 +010020 bool running; /* are we running (transceiving USB data)? */
21 int out_bdi; /* buffer descriptor index for OUT EP */
22 int in_bdi; /* buffer descriptor index for IN EP */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020023} g_usb_e1;
24
25
26/* Hack */
Harald Welteb7aced62020-12-14 17:39:23 +010027unsigned int e1_rx_need_data(unsigned int usb_addr, unsigned int max_len, unsigned int *pos);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020028unsigned int e1_tx_feed_data(unsigned int usb_addr, unsigned int len);
29unsigned int e1_tx_level(void);
30unsigned int e1_rx_level(void);
31/* ---- */
32
33bool
34usb_ep_boot(const struct usb_intf_desc *intf, uint8_t ep_addr, bool dual_bd);
35
36static void
37_usb_fill_feedback_ep(void)
38{
39 static uint16_t ticks_prev = 0;
40 uint16_t ticks;
41 uint32_t val = 8192;
42 unsigned int level;
43
Harald Welte2b7dadf2020-12-14 15:56:15 +010044 /* Compute real E1 tick count (with safety against bad values) */
Harald Welte6603e1f2020-12-15 18:38:37 +010045 ticks = e1_tick_read(0);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020046 val = (ticks - ticks_prev) & 0xffff;
47 ticks_prev = ticks;
48 if ((val < 7168) | (val > 9216))
49 val = 8192;
50
51 /* Bias depending on TX fifo level */
52 level = e1_tx_level();
53 if (level < (3 * 16))
54 val += 256;
55 else if (level > (8 * 16))
56 val -= 256;
57
58 /* Prepare buffer */
59 usb_data_write(64, &val, 4);
60 usb_ep_regs[1].in.bd[0].ptr = 64;
61 usb_ep_regs[1].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(3);
62}
63
64
65void
66usb_e1_run(void)
67{
68 int bdi;
69
70 if (!g_usb_e1.running)
71 return;
72
73 /* EP2 IN */
74 bdi = g_usb_e1.in_bdi;
75
76 while ((usb_ep_regs[2].in.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
77 {
78 uint32_t ptr = usb_ep_regs[2].in.bd[bdi].ptr;
Harald Welte394b6802020-12-14 17:46:53 +010079 uint32_t errmask;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020080 uint32_t hdr;
Harald Welte394b6802020-12-14 17:46:53 +010081 unsigned int pos;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020082
83 /* Error check */
84 if ((usb_ep_regs[2].in.bd[bdi].csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR)
85 puts("Err EP2 IN\n");
86
87 /* Get some data from E1 */
88 int n = e1_rx_level();
89
90 if (n > 64)
91 n = 12;
92 else if (n > 32)
93 n = 10;
94 else if (n > 8)
95 n = 8;
96 else if (!n)
97 break;
98
Harald Welte394b6802020-12-14 17:46:53 +010099 n = e1_rx_need_data((ptr >> 2) + 1, n, &pos);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200100
101 /* Write header */
Harald Welte394b6802020-12-14 17:46:53 +0100102 errmask = e1_get_and_clear_errors();
103 hdr = (4 << 28) | ((errmask & 0xff) << 8) | (pos & 0xff);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200104 usb_data_write(ptr, &hdr, 4);
105
106 /* Resubmit */
107 usb_ep_regs[2].in.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN((n * 32) + 4);
108
109 /* Next BDI */
110 bdi ^= 1;
111 g_usb_e1.in_bdi = bdi;
112 }
113
114 /* EP1 OUT */
115 bdi = g_usb_e1.out_bdi;
116
117 while ((usb_ep_regs[1].out.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
118 {
119 uint32_t ptr = usb_ep_regs[1].out.bd[bdi].ptr;
120 uint32_t csr = usb_ep_regs[1].out.bd[bdi].csr;
121 uint32_t hdr;
122
123 /* Error check */
124 if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR) {
125 puts("Err EP1 OUT\n");
126 goto refill;
127 }
128
129 /* Grab header */
130 usb_data_read(&hdr, ptr, 4);
131
132 /* Empty data into the FIFO */
133 int n = ((csr & USB_BD_LEN_MSK) - 4) / 32;
134 n = e1_tx_feed_data((ptr >> 2) + 1, n);
135
136refill:
137 /* Refill it */
138 usb_ep_regs[1].out.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
139
140 /* Next BDI */
141 bdi ^= 1;
142 g_usb_e1.out_bdi = bdi;
143
144 static int x = 0;
145 if ((x++ & 0xff) == 0xff)
146 puts(".");
147 }
148
149 /* EP1 IN */
150 if ((usb_ep_regs[1].in.bd[0].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
151 {
152 _usb_fill_feedback_ep();
153 }
154}
155
156static const struct usb_intf_desc *
157_find_intf(const struct usb_conf_desc *conf, uint8_t idx)
158{
159 const struct usb_intf_desc *intf = NULL;
160 const void *sod, *eod;
161
162 if (!conf)
163 return NULL;
164
165 sod = conf;
166 eod = sod + conf->wTotalLength;
167
168 while (1) {
169 sod = usb_desc_find(sod, eod, USB_DT_INTF);
170 if (!sod)
171 break;
172
173 intf = (void*)sod;
174 if (intf->bInterfaceNumber == idx)
175 return intf;
176
177 sod = usb_desc_next(sod);
178 }
179
180 return NULL;
181}
Harald Weltec8271852020-12-14 13:48:09 +0100182
183static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200184_e1_set_conf(const struct usb_conf_desc *conf)
185{
186 const struct usb_intf_desc *intf;
187
188 printf("e1 set_conf %08x\n", conf);
189 if (!conf)
190 return USB_FND_SUCCESS;
191
192 intf = _find_intf(conf, 0);
193 if (!intf)
194 return USB_FND_ERROR;
195
196 printf("e1 set_conf %08x\n", intf);
197
198 usb_ep_boot(intf, 0x01, true);
199 usb_ep_boot(intf, 0x81, true);
200 usb_ep_boot(intf, 0x82, true);
Harald Welte22bcd182020-12-15 08:47:26 +0100201 usb_ep_boot(intf, 0x83, true);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200202
203 return USB_FND_SUCCESS;
204}
205
Harald Weltec8271852020-12-14 13:48:09 +0100206static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200207_e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
208{
209 if (base->bInterfaceNumber != 0)
210 return USB_FND_CONTINUE;
211
212 if (sel->bAlternateSetting != 1)
213 return USB_FND_SUCCESS;
214
215 /* Hack to avoid re-setting while running ... avoid BD desync */
216 if (g_usb_e1.running)
217 return USB_FND_SUCCESS;
218
219 g_usb_e1.running = true;
220
221 /* Configure EP1 OUT / EP2 IN */
222 usb_ep_regs[1].out.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */
223 usb_ep_regs[2].in.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */
224
225 /* Configure EP1 IN (feedback) */
226 usb_ep_regs[1].in.status = USB_EP_TYPE_ISOC; /* Type=Isochronous, single buffered */
227
228 /* EP2 IN: Prepare two buffers */
229 usb_ep_regs[2].in.bd[0].ptr = 1024;
230 usb_ep_regs[2].in.bd[0].csr = 0;
231
232 usb_ep_regs[2].in.bd[1].ptr = 1536;
233 usb_ep_regs[2].in.bd[1].csr = 0;
234
235 /* EP1 OUT: Queue two buffers */
236 usb_ep_regs[1].out.bd[0].ptr = 1024;
237 usb_ep_regs[1].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
238
239 usb_ep_regs[1].out.bd[1].ptr = 1536;
240 usb_ep_regs[1].out.bd[1].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388);
241
242 /* EP1 IN: Queue buffer */
243 _usb_fill_feedback_ep();
244
Harald Welte22bcd182020-12-15 08:47:26 +0100245 /* EP3 IN: Interrupt */
246 usb_ep_regs[3].in.status = USB_EP_TYPE_INT;
247 usb_ep_regs[3].in.bd[0].ptr = 0;
248 usb_ep_regs[3].in.bd[0].csr = 0;
249
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200250 return USB_FND_SUCCESS;
251}
252
Harald Weltec8271852020-12-14 13:48:09 +0100253static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200254_e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
255{
256 if (base->bInterfaceNumber != 0)
257 return USB_FND_CONTINUE;
258
259 *alt = g_usb_e1.running ? 1 : 0;
260
261 return USB_FND_SUCCESS;
262}
263
264static struct usb_fn_drv _e1_drv = {
265 .set_conf = _e1_set_conf,
266 .set_intf = _e1_set_intf,
267 .get_intf = _e1_get_intf,
268};
269
270void
271usb_e1_init(void)
272{
273 memset(&g_usb_e1, 0x00, sizeof(g_usb_e1));
274 usb_register_function_driver(&_e1_drv);
275}