-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a64_usbhost.c
424 lines (343 loc) · 11.8 KB
/
a64_usbhost.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/****************************************************************************
* boards/arm64/a64/a641020-evk/src/a64_usbhost.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <sched.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/kthread.h>
#include <nuttx/usb/usbdev.h>
#include <nuttx/usb/usbhost.h>
#include <nuttx/usb/usbdev_trace.h>
#include <nuttx/usb/ehci.h>
#include "arm64_arch.h"
#include "hardware/a64_usbotg.h"
#include "a64_pio.h"
#include "a64_ehci.h"
#include <arch/board/board.h> /* Must always be included last */
#if defined(CONFIG_A64_USBOTG) || defined(CONFIG_USBHOST)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_USBHOST_DEFPRIO
# define CONFIG_USBHOST_DEFPRIO 50
#endif
#ifndef CONFIG_USBHOST_STACKSIZE
# ifdef CONFIG_USBHOST_HUB
# define CONFIG_USBHOST_STACKSIZE 1536
# else
# define CONFIG_USBHOST_STACKSIZE 1024
# endif
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/* Retained device driver handle */
static struct usbhost_connection_s *g_ehciconn;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: ehci_waiter
*
* Description:
* Wait for USB devices to be connected to the EHCI root hub.
*
****************************************************************************/
static int ehci_waiter(int argc, char *argv[])
{
struct usbhost_hubport_s *hport;
uinfo("ehci_waiter: Running\n");
for (; ; )
{
/* Wait for the device to change state */
DEBUGVERIFY(CONN_WAIT(g_ehciconn, &hport));
syslog(LOG_INFO, "ehci_waiter: %s\n",
hport->connected ? "connected" : "disconnected");
/* Did we just become connected? */
if (hport->connected)
{
/* Yes.. enumerate the newly connected device */
CONN_ENUMERATE(g_ehciconn, hport);
}
}
/* Keep the compiler from complaining */
return 0;
}
#define A64_CCU_ADDR 0x01c20000 /* CCU 0x01c2:0000-0x01c2:03ff 1K */
// Set the bit at an address
static void set_bit(unsigned long addr, uint8_t bit)
{
_info("0x%lx Bit %d\n", addr, bit);
modreg32(1 << bit, 1 << bit, addr);
}
// Enable USB Clocks
// https://github.com/lupyuen/pinephone-nuttx-usb#usb-controller-clocks
// https://github.com/lupyuen/pinephone-nuttx-usb#enable-usb-controller-clocks
static void a64_usbhost_clk_enable(void)
{
// usb0_phy: CLK_USB_PHY0
// 0x0cc BIT(8)
_info("CLK_USB_PHY0\n");
#define CLK_USB_PHY0 (A64_CCU_ADDR + 0x0cc)
#define CLK_USB_PHY0_BIT 8
set_bit(CLK_USB_PHY0, CLK_USB_PHY0_BIT);
// usb1_phy: CLK_USB_PHY1
// 0x0cc BIT(9)
_info("CLK_USB_PHY1\n");
#define CLK_USB_PHY1 (A64_CCU_ADDR + 0x0cc)
#define CLK_USB_PHY1_BIT 9
set_bit(CLK_USB_PHY1, CLK_USB_PHY1_BIT);
// EHCI0: CLK_BUS_OHCI0, CLK_BUS_EHCI0, CLK_USB_OHCI0
// 0x060 BIT(28)
_info("CLK_BUS_OHCI0\n");
#define CLK_BUS_OHCI0 (A64_CCU_ADDR + 0x060)
#define CLK_BUS_OHCI0_BIT 28
set_bit(CLK_BUS_OHCI0, CLK_BUS_OHCI0_BIT);
// 0x060 BIT(24)
_info("CLK_BUS_EHCI0\n");
#define CLK_BUS_EHCI0 (A64_CCU_ADDR + 0x060)
#define CLK_BUS_EHCI0_BIT 24
set_bit(CLK_BUS_EHCI0, CLK_BUS_EHCI0_BIT);
// 0x0cc BIT(16)
_info("CLK_USB_OHCI0\n");
#define CLK_USB_OHCI0 (A64_CCU_ADDR + 0x0cc)
#define CLK_USB_OHCI0_BIT 16
set_bit(CLK_USB_OHCI0, CLK_USB_OHCI0_BIT);
// EHCI1: CLK_BUS_OHCI1, CLK_BUS_EHCI1, CLK_USB_OHCI1
// 0x060 BIT(29)
_info("CLK_BUS_OHCI1\n");
#define CLK_BUS_OHCI1 (A64_CCU_ADDR + 0x060)
#define CLK_BUS_OHCI1_BIT 29
set_bit(CLK_BUS_OHCI1, CLK_BUS_OHCI1_BIT);
// 0x060 BIT(25)
_info("CLK_BUS_EHCI1\n");
#define CLK_BUS_EHCI1 (A64_CCU_ADDR + 0x060)
#define CLK_BUS_EHCI1_BIT 25
set_bit(CLK_BUS_EHCI1, CLK_BUS_EHCI1_BIT);
// 0x0cc BIT(17)
_info("CLK_USB_OHCI1\n");
#define CLK_USB_OHCI1 (A64_CCU_ADDR + 0x0cc)
#define CLK_USB_OHCI1_BIT 17
set_bit(CLK_USB_OHCI1, CLK_USB_OHCI1_BIT);
}
// Deassert USB Resets
// https://github.com/lupyuen/pinephone-nuttx-usb#usb-controller-reset
// https://github.com/lupyuen/pinephone-nuttx-usb#reset-usb-controller
static void a64_usbhost_reset_deassert(void)
{
// usb0_reset: RST_USB_PHY0
// 0x0cc BIT(0)
_info("RST_USB_PHY0\n");
#define RST_USB_PHY0 (A64_CCU_ADDR + 0x0cc)
#define RST_USB_PHY0_BIT 0
set_bit(RST_USB_PHY0, RST_USB_PHY0_BIT);
// usb1_reset: RST_USB_PHY1
// 0x0cc BIT(1)
_info("RST_USB_PHY1\n");
#define RST_USB_PHY1 (A64_CCU_ADDR + 0x0cc)
#define RST_USB_PHY1_BIT 1
set_bit(RST_USB_PHY1, RST_USB_PHY1_BIT);
// EHCI0: RST_BUS_OHCI0, RST_BUS_EHCI0
// 0x2c0 BIT(28)
_info("RST_BUS_OHCI0\n");
#define RST_BUS_OHCI0 (A64_CCU_ADDR + 0x2c0)
#define RST_BUS_OHCI0_BIT 28
set_bit(RST_BUS_OHCI0, RST_BUS_OHCI0_BIT);
// 0x2c0 BIT(24)
_info("RST_BUS_EHCI0\n");
#define RST_BUS_EHCI0 (A64_CCU_ADDR + 0x2c0)
#define RST_BUS_EHCI0_BIT 24
set_bit(RST_BUS_EHCI0, RST_BUS_EHCI0_BIT);
// EHCI1: RST_BUS_OHCI1, RST_BUS_EHCI1
// 0x2c0 BIT(29)
_info("RST_BUS_OHCI1\n");
#define RST_BUS_OHCI1 (A64_CCU_ADDR + 0x2c0)
#define RST_BUS_OHCI1_BIT 29
set_bit(RST_BUS_OHCI1, RST_BUS_OHCI1_BIT);
// 0x2c0 BIT(25)
_info("RST_BUS_EHCI1\n");
#define RST_BUS_EHCI1 (A64_CCU_ADDR + 0x2c0)
#define RST_BUS_EHCI1_BIT 25
set_bit(RST_BUS_EHCI1, RST_BUS_EHCI1_BIT);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: a64_usbhost_initialize
*
* Description:
* Called at application startup time to initialize the USB host
* functionality.
* This function will start a thread that will monitor for device
* connection/disconnection events.
*
****************************************************************************/
int a64_usbhost_initialize(void)
{
int ret;
// Enable the USB Clocks
a64_usbhost_clk_enable();
// Deassert the USB Resets
a64_usbhost_reset_deassert();
// TODO: a64_clockall_usboh3();
/* Make sure we don't accidentally switch on USB bus power */
_info("TODO: switch off USB bus power\n");////
// TODO: *((uint32_t *)A64_USBNC_USB_OTG1_CTRL) = USBNC_PWR_POL;
// TODO: *((uint32_t *)0x400d9030) = (1 << 21);
// TODO: *((uint32_t *)0x400d9000) = 0;
/* Setup pins, with power initially off */
_info("TODO: Setup pins, with power initially off\n");////
// TODO: a64_config_gpio(GPIO_USBOTG_PWR);
// TODO: a64_config_gpio(GPIO_USBOTG_OC);
// TODO: a64_config_gpio(GPIO_USBOTG_ID);
/* First, register all of the class drivers needed to support the drivers
* that we care about
*/
#ifdef CONFIG_USBHOST_HUB
/* Initialize USB hub support */
ret = usbhost_hub_initialize();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: usbhost_hub_initialize failed: %d\n", ret);
}
#endif
#ifdef CONFIG_USBHOST_MSC
/* Register the USB host Mass Storage Class */
ret = usbhost_msc_initialize();
if (ret != OK)
{
syslog(LOG_ERR,
"ERROR: Failed to register the mass storage class: %d\n", ret);
}
#endif
#ifdef CONFIG_USBHOST_CDCACM
/* Register the CDC/ACM serial class */
ret = usbhost_cdcacm_initialize();
if (ret != OK)
{
uerr("ERROR: Failed to register the CDC/ACM serial class\n");
}
#endif
#ifdef CONFIG_USBHOST_HIDKBD
/* Register the USB host HID keyboard class driver */
ret = usbhost_kbdinit();
if (ret != OK)
{
uerr("ERROR: Failed to register the KBD class\n");
}
#endif
/* Then get an instance of the USB EHCI interface. */
g_ehciconn = a64_ehci_initialize(0);
if (!g_ehciconn)
{
uerr("ERROR: a64_ehci_initialize failed\n");
return -ENODEV;
}
/* Start a thread to handle device connection. */
_info("1\n");////
ret = kthread_create("EHCI Monitor", CONFIG_USBHOST_DEFPRIO,
CONFIG_USBHOST_STACKSIZE,
ehci_waiter, NULL);
if (ret < 0)
{
_info("2\n");////
uerr("ERROR: Failed to create ehci_waiter task: %d\n", ret);
return -ENODEV;
}
// Init LTE Modem moved to pinephone_modem_init in
// https://github.com/lupyuen2/wip-pinephone-nuttx/blob/usb/boards/arm64/a64/pinephone/src/pinephone_bringup.c
return OK;
}
/****************************************************************************
* Name: a64_usbhost_vbusdrive
*
* Description:
* Enable/disable driving of VBUS 5V output. This function must be
* provided by each platform that implements the OHCI or EHCI host
* interface
*
* Input Parameters:
* rhport - Selects root hub port to be powered host interface.
* Since the A64 has only a downstream port, zero is
* the only possible value for this parameter.
* enable - true: enable VBUS power; false: disable VBUS power
*
* Returned Value:
* None
*
****************************************************************************/
#define HCOR ((volatile struct ehci_hcor_s *)A64_USBOTG_HCOR_BASE)
void a64_usbhost_vbusdrive(int rhport, bool enable)
{
uint32_t regval;
uinfo("RHPort%d: enable=%d\n", rhport + 1, enable);
/* The A64 has only a single root hub port */
if (rhport == 0)
{
/* Then enable or disable VBUS power */
regval = HCOR->portsc[rhport];
regval &= ~EHCI_PORTSC_PP;
if (enable)
{
regval |= EHCI_PORTSC_PP;
}
HCOR->portsc[rhport] = regval;
}
}
/****************************************************************************
* Name: a64_setup_overcurrent
*
* Description:
* Setup to receive an interrupt-level callback if an overcurrent condition
* is detected.
*
* Input Parameters:
* handler - New overcurrent interrupt handler
* arg - The argument that will accompany the interrupt
*
* Returned Value:
* Zero (OK) returned on success; a negated errno value is returned on
* failure.
*
****************************************************************************/
#if 0 /* Not ready yet */
int a64_setup_overcurrent(xcpt_t handler, void *arg)
{
irqstate_t flags;
/* Disable interrupts until we are done. This guarantees that the
* following operations are atomic.
*/
flags = enter_critical_section();
/* Configure the interrupt */
#warning Missing logic
leave_critical_section(flags);
return OK;
}
#endif /* 0 */
#endif /* CONFIG_A64_USBOTG || CONFIG_USBHOST */