gdb: fix invalid sigprocmask call
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.9 / 0031-Add-dwc_otg-driver.patch
1 From 65f57e56fcf9d40383718f0bcd9e6f95a34ca1aa Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 1 May 2013 19:46:17 +0100
4 Subject: [PATCH] Add dwc_otg driver
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: popcornmix <popcornmix@gmail.com>
10
11 usb: dwc: fix lockdep false positive
12
13 Signed-off-by: Kari Suvanto <karis79@gmail.com>
14
15 usb: dwc: fix inconsistent lock state
16
17 Signed-off-by: Kari Suvanto <karis79@gmail.com>
18
19 Add FIQ patch to dwc_otg driver. Enable with dwc_otg.fiq_fix_enable=1. Should give about 10% more ARM performance.
20 Thanks to Gordon and Costas
21
22 Avoid dynamic memory allocation for channel lock in USB driver. Thanks ddv2005.
23
24 Add NAK holdoff scheme. Enabled by default, disable with dwc_otg.nak_holdoff_enable=0. Thanks gsh
25
26 Make sure we wait for the reset to finish
27
28 dwc_otg: fix bug in dwc_otg_hcd.c resulting in silent kernel
29 memory corruption, escalating to OOPS under high USB load.
30
31 dwc_otg: Fix unsafe access of QTD during URB enqueue
32
33 In dwc_otg_hcd_urb_enqueue during qtd creation, it was possible that the
34 transaction could complete almost immediately after the qtd was assigned
35 to a host channel during URB enqueue, which meant the qtd pointer was no
36 longer valid having been completed and removed. Usually, this resulted in
37 an OOPS during URB submission. By predetermining whether transactions
38 need to be queued or not, this unsafe pointer access is avoided.
39
40 This bug was only evident on the Pi model A where a device was attached
41 that had no periodic endpoints (e.g. USB pendrive or some wlan devices).
42
43 dwc_otg: Fix incorrect URB allocation error handling
44
45 If the memory allocation for a dwc_otg_urb failed, the kernel would OOPS
46 because for some reason a member of the *unallocated* struct was set to
47 zero. Error handling changed to fail correctly.
48
49 dwc_otg: fix potential use-after-free case in interrupt handler
50
51 If a transaction had previously aborted, certain interrupts are
52 enabled to track error counts and reset where necessary. On IN
53 endpoints the host generates an ACK interrupt near-simultaneously
54 with completion of transfer. In the case where this transfer had
55 previously had an error, this results in a use-after-free on
56 the QTD memory space with a 1-byte length being overwritten to
57 0x00.
58
59 dwc_otg: add handling of SPLIT transaction data toggle errors
60
61 Previously a data toggle error on packets from a USB1.1 device behind
62 a TT would result in the Pi locking up as the driver never handled
63 the associated interrupt. Patch adds basic retry mechanism and
64 interrupt acknowledgement to cater for either a chance toggle error or
65 for devices that have a broken initial toggle state (FT8U232/FT232BM).
66
67 dwc_otg: implement tasklet for returning URBs to usbcore hcd layer
68
69 The dwc_otg driver interrupt handler for transfer completion will spend
70 a very long time with interrupts disabled when a URB is completed -
71 this is because usb_hcd_giveback_urb is called from within the handler
72 which for a USB device driver with complicated processing (e.g. webcam)
73 will take an exorbitant amount of time to complete. This results in
74 missed completion interrupts for other USB packets which lead to them
75 being dropped due to microframe overruns.
76
77 This patch splits returning the URB to the usb hcd layer into a
78 high-priority tasklet. This will have most benefit for isochronous IN
79 transfers but will also have incidental benefit where multiple periodic
80 devices are active at once.
81
82 dwc_otg: fix NAK holdoff and allow on split transactions only
83
84 This corrects a bug where if a single active non-periodic endpoint
85 had at least one transaction in its qh, on frnum == MAX_FRNUM the qh
86 would get skipped and never get queued again. This would result in
87 a silent device until error detection (automatic or otherwise) would
88 either reset the device or flush and requeue the URBs.
89
90 Additionally the NAK holdoff was enabled for all transactions - this
91 would potentially stall a HS endpoint for 1ms if a previous error state
92 enabled this interrupt and the next response was a NAK. Fix so that
93 only split transactions get held off.
94
95 dwc_otg: Call usb_hcd_unlink_urb_from_ep with lock held in completion handler
96
97 usb_hcd_unlink_urb_from_ep must be called with the HCD lock held. Calling it
98 asynchronously in the tasklet was not safe (regression in
99 c4564d4a1a0a9b10d4419e48239f5d99e88d2667).
100
101 This change unlinks it from the endpoint prior to queueing it for handling in
102 the tasklet, and also adds a check to ensure the urb is OK to be unlinked
103 before doing so.
104
105 NULL pointer dereference kernel oopses had been observed in usb_hcd_giveback_urb
106 when a USB device was unplugged/replugged during data transfer. This effect
107 was reproduced using automated USB port power control, hundreds of replug
108 events were performed during active transfers to confirm that the problem was
109 eliminated.
110
111 USB fix using a FIQ to implement split transactions
112
113 This commit adds a FIQ implementaion that schedules
114 the split transactions using a FIQ so we don't get
115 held off by the interrupt latency of Linux
116
117 dwc_otg: fix device attributes and avoid kernel warnings on boot
118
119 dcw_otg: avoid logging function that can cause panics
120
121 See: https://github.com/raspberrypi/firmware/issues/21
122 Thanks to cleverca22 for fix
123
124 dwc_otg: mask correct interrupts after transaction error recovery
125
126 The dwc_otg driver will unmask certain interrupts on a transaction
127 that previously halted in the error state in order to reset the
128 QTD error count. The various fine-grained interrupt handlers do not
129 consider that other interrupts besides themselves were unmasked.
130
131 By disabling the two other interrupts only ever enabled in DMA mode
132 for this purpose, we can avoid unnecessary function calls in the
133 IRQ handler. This will also prevent an unneccesary FIQ interrupt
134 from being generated if the FIQ is enabled.
135
136 dwc_otg: fiq: prevent FIQ thrash and incorrect state passing to IRQ
137
138 In the case of a transaction to a device that had previously aborted
139 due to an error, several interrupts are enabled to reset the error
140 count when a device responds. This has the side-effect of making the
141 FIQ thrash because the hardware will generate multiple instances of
142 a NAK on an IN bulk/interrupt endpoint and multiple instances of ACK
143 on an OUT bulk/interrupt endpoint. Make the FIQ mask and clear the
144 associated interrupts.
145
146 Additionally, on non-split transactions make sure that only unmasked
147 interrupts are cleared. This caused a hard-to-trigger but serious
148 race condition when you had the combination of an endpoint awaiting
149 error recovery and a transaction completed on an endpoint - due to
150 the sequencing and timing of interrupts generated by the dwc_otg core,
151 it was possible to confuse the IRQ handler.
152
153 Fix function tracing
154
155 dwc_otg: whitespace cleanup in dwc_otg_urb_enqueue
156
157 dwc_otg: prevent OOPSes during device disconnects
158
159 The dwc_otg_urb_enqueue function is thread-unsafe. In particular the
160 access of urb->hcpriv, usb_hcd_link_urb_to_ep, dwc_otg_urb->qtd and
161 friends does not occur within a critical section and so if a device
162 was unplugged during activity there was a high chance that the
163 usbcore hub_thread would try to disable the endpoint with partially-
164 formed entries in the URB queue. This would result in BUG() or null
165 pointer dereferences.
166
167 Fix so that access of urb->hcpriv, enqueuing to the hardware and
168 adding to usbcore endpoint URB lists is contained within a single
169 critical section.
170
171 dwc_otg: prevent BUG() in TT allocation if hub address is > 16
172
173 A fixed-size array is used to track TT allocation. This was
174 previously set to 16 which caused a crash because
175 dwc_otg_hcd_allocate_port would read past the end of the array.
176
177 This was hit if a hub was plugged in which enumerated as addr > 16,
178 due to previous device resets or unplugs.
179
180 Also add #ifdef FIQ_DEBUG around hcd->hub_port_alloc[], which grows
181 to a large size if 128 hub addresses are supported. This field is
182 for debug only for tracking which frame an allocate happened in.
183
184 dwc_otg: make channel halts with unknown state less damaging
185
186 If the IRQ received a channel halt interrupt through the FIQ
187 with no other bits set, the IRQ would not release the host
188 channel and never complete the URB.
189
190 Add catchall handling to treat as a transaction error and retry.
191
192 dwc_otg: fiq_split: use TTs with more granularity
193
194 This fixes certain issues with split transaction scheduling.
195
196 - Isochronous multi-packet OUT transactions now hog the TT until
197 they are completed - this prevents hubs aborting transactions
198 if they get a periodic start-split out-of-order
199 - Don't perform TT allocation on non-periodic endpoints - this
200 allows simultaneous use of the TT's bulk/control and periodic
201 transaction buffers
202
203 This commit will mainly affect USB audio playback.
204
205 dwc_otg: fix potential sleep while atomic during urb enqueue
206
207 Fixes a regression introduced with eb1b482a. Kmalloc called from
208 dwc_otg_hcd_qtd_add / dwc_otg_hcd_qtd_create did not always have
209 the GPF_ATOMIC flag set. Force this flag when inside the larger
210 critical section.
211
212 dwc_otg: make fiq_split_enable imply fiq_fix_enable
213
214 Failing to set up the FIQ correctly would result in
215 "IRQ 32: nobody cared" errors in dmesg.
216
217 dwc_otg: prevent crashes on host port disconnects
218
219 Fix several issues resulting in crashes or inconsistent state
220 if a Model A root port was disconnected.
221
222 - Clean up queue heads properly in kill_urbs_in_qh_list by
223 removing the empty QHs from the schedule lists
224 - Set the halt status properly to prevent IRQ handlers from
225 using freed memory
226 - Add fiq_split related cleanup for saved registers
227 - Make microframe scheduling reclaim host channels if
228 active during a disconnect
229 - Abort URBs with -ESHUTDOWN status response, informing
230 device drivers so they respond in a more correct fashion
231 and don't try to resubmit URBs
232 - Prevent IRQ handlers from attempting to handle channel
233 interrupts if the associated URB was dequeued (and the
234 driver state was cleared)
235
236 dwc_otg: prevent leaking URBs during enqueue
237
238 A dwc_otg_urb would get leaked if the HCD enqueue function
239 failed for any reason. Free the URB at the appropriate points.
240
241 dwc_otg: Enable NAK holdoff for control split transactions
242
243 Certain low-speed devices take a very long time to complete a
244 data or status stage of a control transaction, producing NAK
245 responses until they complete internal processing - the USB2.0
246 spec limit is up to 500mS. This causes the same type of interrupt
247 storm as seen with USB-serial dongles prior to c8edb238.
248
249 In certain circumstances, usually while booting, this interrupt
250 storm could cause SD card timeouts.
251
252 dwc_otg: Fix for occasional lockup on boot when doing a USB reset
253
254 dwc_otg: Don't issue traffic to LS devices in FS mode
255
256 Issuing low-speed packets when the root port is in full-speed mode
257 causes the root port to stop responding. Explicitly fail when
258 enqueuing URBs to a LS endpoint on a FS bus.
259
260 Fix ARM architecture issue with local_irq_restore()
261
262 If local_fiq_enable() is called before a local_irq_restore(flags) where
263 the flags variable has the F bit set, the FIQ will be erroneously disabled.
264
265 Fixup arch_local_irq_restore to avoid trampling the F bit in CPSR.
266
267 Also fix some of the hacks previously implemented for previous dwc_otg
268 incarnations.
269
270 dwc_otg: fiq_fsm: Base commit for driver rewrite
271
272 This commit removes the previous FIQ fixes entirely and adds fiq_fsm.
273
274 This rewrite features much more complete support for split transactions
275 and takes into account several OTG hardware bugs. High-speed
276 isochronous transactions are also capable of being performed by fiq_fsm.
277
278 All driver options have been removed and replaced with:
279 - dwc_otg.fiq_enable (bool)
280 - dwc_otg.fiq_fsm_enable (bool)
281 - dwc_otg.fiq_fsm_mask (bitmask)
282 - dwc_otg.nak_holdoff (unsigned int)
283
284 Defaults are specified such that fiq_fsm behaves similarly to the
285 previously implemented FIQ fixes.
286
287 fiq_fsm: Push error recovery into the FIQ when fiq_fsm is used
288
289 If the transfer associated with a QTD failed due to a bus error, the HCD
290 would retry the transfer up to 3 times (implementing the USB2.0
291 three-strikes retry in software).
292
293 Due to the masking mechanism used by fiq_fsm, it is only possible to pass
294 a single interrupt through to the HCD per-transfer.
295
296 In this instance host channels would fall off the radar because the error
297 reset would function, but the subsequent channel halt would be lost.
298
299 Push the error count reset into the FIQ handler.
300
301 fiq_fsm: Implement timeout mechanism
302
303 For full-speed endpoints with a large packet size, interrupt latency
304 runs the risk of the FIQ starting a transaction too late in a full-speed
305 frame. If the device is still transmitting data when EOF2 for the
306 downstream frame occurs, the hub will disable the port. This change is
307 not reflected in the hub status endpoint and the device becomes
308 unresponsive.
309
310 Prevent high-bandwidth transactions from being started too late in a
311 frame. The mechanism is not guaranteed: a combination of bit stuffing
312 and hub latency may still result in a device overrunning.
313
314 fiq_fsm: fix bounce buffer utilisation for Isochronous OUT
315
316 Multi-packet isochronous OUT transactions were subject to a few bounday
317 bugs. Fix them.
318
319 Audio playback is now much more robust: however, an issue stands with
320 devices that have adaptive sinks - ALSA plays samples too fast.
321
322 dwc_otg: Return full-speed frame numbers in HS mode
323
324 The frame counter increments on every *microframe* in high-speed mode.
325 Most device drivers expect this number to be in full-speed frames - this
326 caused considerable confusion to e.g. snd_usb_audio which uses the
327 frame counter to estimate the number of samples played.
328
329 fiq_fsm: save PID on completion of interrupt OUT transfers
330
331 Also add edge case handling for interrupt transports.
332
333 Note that for periodic split IN, data toggles are unimplemented in the
334 OTG host hardware - it unconditionally accepts any PID.
335
336 fiq_fsm: add missing case for fiq_fsm_tt_in_use()
337
338 Certain combinations of bitrate and endpoint activity could
339 result in a periodic transaction erroneously getting started
340 while the previous Isochronous OUT was still active.
341
342 fiq_fsm: clear hcintmsk for aborted transactions
343
344 Prevents the FIQ from erroneously handling interrupts
345 on a timed out channel.
346
347 fiq_fsm: enable by default
348
349 fiq_fsm: fix dequeues for non-periodic split transactions
350
351 If a dequeue happened between the SSPLIT and CSPLIT phases of the
352 transaction, the HCD would never receive an interrupt.
353
354 fiq_fsm: Disable by default
355
356 fiq_fsm: Handle HC babble errors
357
358 The HCTSIZ transfer size field raises a babble interrupt if
359 the counter wraps. Handle the resulting interrupt in this case.
360
361 dwc_otg: fix interrupt registration for fiq_enable=0
362
363 Additionally make the module parameter conditional for wherever
364 hcd->fiq_state is touched.
365
366 fiq_fsm: Enable by default
367
368 dwc_otg: Fix various issues with root port and transaction errors
369
370 Process the host port interrupts correctly (and don't trample them).
371 Root port hotplug now functional again.
372
373 Fix a few thinkos with the transaction error passthrough for fiq_fsm.
374
375 fiq_fsm: Implement hack for Split Interrupt transactions
376
377 Hubs aren't too picky about which endpoint we send Control type split
378 transactions to. By treating Interrupt transfers as Control, it is
379 possible to use the non-periodic queue in the OTG core as well as the
380 non-periodic FIFOs in the hub itself. This massively reduces the
381 microframe exclusivity/contention that periodic split transactions
382 otherwise have to enforce.
383
384 It goes without saying that this is a fairly egregious USB specification
385 violation, but it works.
386
387 Original idea by Hans Petter Selasky @ FreeBSD.org.
388
389 dwc_otg: FIQ support on SMP. Set up FIQ stack and handler on Core 0 only.
390
391 dwc_otg: introduce fiq_fsm_spin(un|)lock()
392
393 SMP safety for the FIQ relies on register read-modify write cycles being
394 completed in the correct order. Several places in the DWC code modify
395 registers also touched by the FIQ. Protect these by a bare-bones lock
396 mechanism.
397
398 This also makes it possible to run the FIQ and IRQ handlers on different
399 cores.
400
401 fiq_fsm: fix build on bcm2708 and bcm2709 platforms
402
403 dwc_otg: put some barriers back where they should be for UP
404
405 bcm2709/dwc_otg: Setup FIQ on core 1 if >1 core active
406
407 dwc_otg: fixup read-modify-write in critical paths
408
409 Be more careful about read-modify-write on registers that the FIQ
410 also touches.
411
412 Guard fiq_fsm_spin_lock with fiq_enable check
413
414 fiq_fsm: Falling out of the state machine isn't fatal
415
416 This edge case can be hit if the port is disabled while the FIQ is
417 in the middle of a transaction. Make the effects less severe.
418
419 Also get rid of the useless return value.
420
421 squash: dwc_otg: Allow to build without SMP
422
423 usb: core: make overcurrent messages more prominent
424
425 Hub overcurrent messages are more serious than "debug". Increase loglevel.
426
427 usb: dwc_otg: Don't use dma_to_virt()
428
429 Commit 6ce0d20 changes dma_to_virt() which breaks this driver.
430 Open code the old dma_to_virt() implementation to work around this.
431
432 Limit the use of __bus_to_virt() to cases where transfer_buffer_length
433 is set and transfer_buffer is not set. This is done to increase the
434 chance that this driver will also work on ARCH_BCM2835.
435
436 transfer_buffer should not be NULL if the length is set, but the
437 comment in the code indicates that there are situations where this
438 might happen. drivers/usb/isp1760/isp1760-hcd.c also has a similar
439 comment pointing to a possible: 'usb storage / SCSI bug'.
440
441 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
442
443 dwc_otg: Fix crash when fiq_enable=0
444
445 dwc_otg: fiq_fsm: Make high-speed isochronous strided transfers work properly
446
447 Certain low-bandwidth high-speed USB devices (specialist audio devices,
448 compressed-frame webcams) have packet intervals > 1 microframe.
449
450 Stride these transfers in the FIQ by using the start-of-frame interrupt
451 to restart the channel at the right time.
452
453 dwc_otg: Force host mode to fix incorrect compute module boards
454
455 dwc_otg: Add ARCH_BCM2835 support
456
457 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
458
459 dwc_otg: Simplify FIQ irq number code
460
461 Dropping ATAGS means we can simplify the FIQ irq number code.
462 Also add error checking on the returned irq number.
463
464 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
465
466 dwc_otg: Remove duplicate gadget probe/unregister function
467
468 dwc_otg: Properly set the HFIR
469
470 Douglas Anderson reported:
471
472 According to the most up to date version of the dwc2 databook, the FRINT
473 field of the HFIR register should be programmed to:
474 * 125 us * (PHY clock freq for HS) - 1
475 * 1000 us * (PHY clock freq for FS/LS) - 1
476
477 This is opposed to older versions of the doc that claimed it should be:
478 * 125 us * (PHY clock freq for HS)
479 * 1000 us * (PHY clock freq for FS/LS)
480
481 and reported lower timing jitter on a USB analyser
482
483 dcw_otg: trim xfer length when buffer larger than allocated size is received
484
485 dwc_otg: Don't free qh align buffers in atomic context
486
487 dwc_otg: Enable the hack for Split Interrupt transactions by default
488
489 dwc_otg.fiq_fsm_mask=0xF has long been a suggestion for users with audio stutters or other USB bandwidth issues.
490 So far we are aware of many success stories but no failure caused by this setting.
491 Make it a default to learn more.
492
493 See: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=70437
494
495 Signed-off-by: popcornmix <popcornmix@gmail.com>
496
497 dwc_otg: Use kzalloc when suitable
498
499 dwc_otg: Pass struct device to dma_alloc*()
500
501 This makes it possible to get the bus address from Device Tree.
502
503 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
504 ---
505 arch/arm/include/asm/irqflags.h | 16 +-
506 arch/arm/kernel/fiqasm.S | 4 +
507 drivers/usb/Makefile | 1 +
508 drivers/usb/core/generic.c | 1 +
509 drivers/usb/core/hub.c | 2 +-
510 drivers/usb/core/message.c | 79 +
511 drivers/usb/core/otg_whitelist.h | 114 +-
512 drivers/usb/gadget/file_storage.c | 3676 ++++++++++
513 drivers/usb/host/Kconfig | 13 +
514 drivers/usb/host/Makefile | 2 +
515 drivers/usb/host/dwc_common_port/Makefile | 58 +
516 drivers/usb/host/dwc_common_port/Makefile.fbsd | 17 +
517 drivers/usb/host/dwc_common_port/Makefile.linux | 49 +
518 drivers/usb/host/dwc_common_port/changes.txt | 174 +
519 drivers/usb/host/dwc_common_port/doc/doxygen.cfg | 270 +
520 drivers/usb/host/dwc_common_port/dwc_cc.c | 532 ++
521 drivers/usb/host/dwc_common_port/dwc_cc.h | 224 +
522 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c | 1308 ++++
523 .../usb/host/dwc_common_port/dwc_common_linux.c | 1418 ++++
524 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c | 1275 ++++
525 drivers/usb/host/dwc_common_port/dwc_crypto.c | 308 +
526 drivers/usb/host/dwc_common_port/dwc_crypto.h | 111 +
527 drivers/usb/host/dwc_common_port/dwc_dh.c | 291 +
528 drivers/usb/host/dwc_common_port/dwc_dh.h | 106 +
529 drivers/usb/host/dwc_common_port/dwc_list.h | 594 ++
530 drivers/usb/host/dwc_common_port/dwc_mem.c | 245 +
531 drivers/usb/host/dwc_common_port/dwc_modpow.c | 636 ++
532 drivers/usb/host/dwc_common_port/dwc_modpow.h | 34 +
533 drivers/usb/host/dwc_common_port/dwc_notifier.c | 319 +
534 drivers/usb/host/dwc_common_port/dwc_notifier.h | 122 +
535 drivers/usb/host/dwc_common_port/dwc_os.h | 1276 ++++
536 drivers/usb/host/dwc_common_port/usb.h | 946 +++
537 drivers/usb/host/dwc_otg/Makefile | 82 +
538 drivers/usb/host/dwc_otg/doc/doxygen.cfg | 224 +
539 drivers/usb/host/dwc_otg/dummy_audio.c | 1574 +++++
540 drivers/usb/host/dwc_otg/dwc_cfi_common.h | 142 +
541 drivers/usb/host/dwc_otg/dwc_otg_adp.c | 854 +++
542 drivers/usb/host/dwc_otg/dwc_otg_adp.h | 80 +
543 drivers/usb/host/dwc_otg/dwc_otg_attr.c | 1210 ++++
544 drivers/usb/host/dwc_otg/dwc_otg_attr.h | 89 +
545 drivers/usb/host/dwc_otg/dwc_otg_cfi.c | 1876 +++++
546 drivers/usb/host/dwc_otg/dwc_otg_cfi.h | 320 +
547 drivers/usb/host/dwc_otg/dwc_otg_cil.c | 7141 ++++++++++++++++++++
548 drivers/usb/host/dwc_otg/dwc_otg_cil.h | 1464 ++++
549 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c | 1594 +++++
550 drivers/usb/host/dwc_otg/dwc_otg_core_if.h | 705 ++
551 drivers/usb/host/dwc_otg/dwc_otg_dbg.h | 117 +
552 drivers/usb/host/dwc_otg/dwc_otg_driver.c | 1757 +++++
553 drivers/usb/host/dwc_otg/dwc_otg_driver.h | 86 +
554 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 1355 ++++
555 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 370 +
556 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S | 80 +
557 drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 4260 ++++++++++++
558 drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 868 +++
559 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c | 1139 ++++
560 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h | 417 ++
561 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 2727 ++++++++
562 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 1005 +++
563 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c | 963 +++
564 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h | 188 +
565 drivers/usb/host/dwc_otg/dwc_otg_pcd.c | 2725 ++++++++
566 drivers/usb/host/dwc_otg/dwc_otg_pcd.h | 273 +
567 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h | 361 +
568 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c | 5148 ++++++++++++++
569 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c | 1280 ++++
570 drivers/usb/host/dwc_otg/dwc_otg_regs.h | 2550 +++++++
571 drivers/usb/host/dwc_otg/test/Makefile | 16 +
572 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm | 337 +
573 drivers/usb/host/dwc_otg/test/test_mod_param.pl | 133 +
574 drivers/usb/host/dwc_otg/test/test_sysfs.pl | 193 +
575 70 files changed, 59908 insertions(+), 16 deletions(-)
576 create mode 100644 drivers/usb/gadget/file_storage.c
577 create mode 100644 drivers/usb/host/dwc_common_port/Makefile
578 create mode 100644 drivers/usb/host/dwc_common_port/Makefile.fbsd
579 create mode 100644 drivers/usb/host/dwc_common_port/Makefile.linux
580 create mode 100644 drivers/usb/host/dwc_common_port/changes.txt
581 create mode 100644 drivers/usb/host/dwc_common_port/doc/doxygen.cfg
582 create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.c
583 create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.h
584 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
585 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_linux.c
586 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
587 create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.c
588 create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.h
589 create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.c
590 create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.h
591 create mode 100644 drivers/usb/host/dwc_common_port/dwc_list.h
592 create mode 100644 drivers/usb/host/dwc_common_port/dwc_mem.c
593 create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.c
594 create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.h
595 create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.c
596 create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.h
597 create mode 100644 drivers/usb/host/dwc_common_port/dwc_os.h
598 create mode 100644 drivers/usb/host/dwc_common_port/usb.h
599 create mode 100644 drivers/usb/host/dwc_otg/Makefile
600 create mode 100644 drivers/usb/host/dwc_otg/doc/doxygen.cfg
601 create mode 100644 drivers/usb/host/dwc_otg/dummy_audio.c
602 create mode 100644 drivers/usb/host/dwc_otg/dwc_cfi_common.h
603 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.c
604 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.h
605 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.c
606 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.h
607 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.c
608 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.h
609 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.c
610 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.h
611 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
612 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_core_if.h
613 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_dbg.h
614 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.c
615 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.h
616 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
617 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
618 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
619 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.c
620 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.h
621 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
622 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
623 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
624 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
625 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
626 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
627 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.c
628 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.h
629 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
630 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
631 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
632 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_regs.h
633 create mode 100644 drivers/usb/host/dwc_otg/test/Makefile
634 create mode 100644 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
635 create mode 100644 drivers/usb/host/dwc_otg/test/test_mod_param.pl
636 create mode 100644 drivers/usb/host/dwc_otg/test/test_sysfs.pl
637
638 --- a/arch/arm/include/asm/irqflags.h
639 +++ b/arch/arm/include/asm/irqflags.h
640 @@ -162,13 +162,23 @@ static inline unsigned long arch_local_s
641 }
642
643 /*
644 - * restore saved IRQ & FIQ state
645 + * restore saved IRQ state
646 */
647 #define arch_local_irq_restore arch_local_irq_restore
648 static inline void arch_local_irq_restore(unsigned long flags)
649 {
650 - asm volatile(
651 - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
652 + unsigned long temp = 0;
653 + flags &= ~(1 << 6);
654 + asm volatile (
655 + " mrs %0, cpsr"
656 + : "=r" (temp)
657 + :
658 + : "memory", "cc");
659 + /* Preserve FIQ bit */
660 + temp &= (1 << 6);
661 + flags = flags | temp;
662 + asm volatile (
663 + " msr cpsr_c, %0 @ local_irq_restore"
664 :
665 : "r" (flags)
666 : "memory", "cc");
667 --- a/arch/arm/kernel/fiqasm.S
668 +++ b/arch/arm/kernel/fiqasm.S
669 @@ -47,3 +47,7 @@ ENTRY(__get_fiq_regs)
670 mov r0, r0 @ avoid hazard prior to ARMv4
671 ret lr
672 ENDPROC(__get_fiq_regs)
673 +
674 +ENTRY(__FIQ_Branch)
675 + mov pc, r8
676 +ENDPROC(__FIQ_Branch)
677 --- a/drivers/usb/Makefile
678 +++ b/drivers/usb/Makefile
679 @@ -7,6 +7,7 @@
680 obj-$(CONFIG_USB) += core/
681 obj-$(CONFIG_USB_SUPPORT) += phy/
682
683 +obj-$(CONFIG_USB_DWCOTG) += host/
684 obj-$(CONFIG_USB_DWC3) += dwc3/
685 obj-$(CONFIG_USB_DWC2) += dwc2/
686 obj-$(CONFIG_USB_ISP1760) += isp1760/
687 --- a/drivers/usb/core/generic.c
688 +++ b/drivers/usb/core/generic.c
689 @@ -152,6 +152,7 @@ int usb_choose_configuration(struct usb_
690 dev_warn(&udev->dev,
691 "no configuration chosen from %d choice%s\n",
692 num_configs, plural(num_configs));
693 + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
694 }
695 return i;
696 }
697 --- a/drivers/usb/core/hub.c
698 +++ b/drivers/usb/core/hub.c
699 @@ -5009,7 +5009,7 @@ static void port_event(struct usb_hub *h
700 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
701 u16 status = 0, unused;
702
703 - dev_dbg(&port_dev->dev, "over-current change\n");
704 + dev_notice(&port_dev->dev, "over-current change\n");
705 usb_clear_port_feature(hdev, port1,
706 USB_PORT_FEAT_C_OVER_CURRENT);
707 msleep(100); /* Cool down */
708 --- a/drivers/usb/core/message.c
709 +++ b/drivers/usb/core/message.c
710 @@ -1908,6 +1908,85 @@ free_interfaces:
711 if (cp->string == NULL &&
712 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
713 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
714 +/* Uncomment this define to enable the HS Electrical Test support */
715 +#define DWC_HS_ELECT_TST 1
716 +#ifdef DWC_HS_ELECT_TST
717 + /* Here we implement the HS Electrical Test support. The
718 + * tester uses a vendor ID of 0x1A0A to indicate we should
719 + * run a special test sequence. The product ID tells us
720 + * which sequence to run. We invoke the test sequence by
721 + * sending a non-standard SetFeature command to our root
722 + * hub port. Our dwc_otg_hcd_hub_control() routine will
723 + * recognize the command and perform the desired test
724 + * sequence.
725 + */
726 + if (dev->descriptor.idVendor == 0x1A0A) {
727 + /* HSOTG Electrical Test */
728 + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
729 +
730 + if (dev->bus && dev->bus->root_hub) {
731 + struct usb_device *hdev = dev->bus->root_hub;
732 + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
733 +
734 + switch (dev->descriptor.idProduct) {
735 + case 0x0101: /* TEST_SE0_NAK */
736 + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
737 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
738 + USB_REQ_SET_FEATURE, USB_RT_PORT,
739 + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
740 + break;
741 +
742 + case 0x0102: /* TEST_J */
743 + dev_warn(&dev->dev, "TEST_J\n");
744 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
745 + USB_REQ_SET_FEATURE, USB_RT_PORT,
746 + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
747 + break;
748 +
749 + case 0x0103: /* TEST_K */
750 + dev_warn(&dev->dev, "TEST_K\n");
751 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
752 + USB_REQ_SET_FEATURE, USB_RT_PORT,
753 + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
754 + break;
755 +
756 + case 0x0104: /* TEST_PACKET */
757 + dev_warn(&dev->dev, "TEST_PACKET\n");
758 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
759 + USB_REQ_SET_FEATURE, USB_RT_PORT,
760 + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
761 + break;
762 +
763 + case 0x0105: /* TEST_FORCE_ENABLE */
764 + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
765 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
766 + USB_REQ_SET_FEATURE, USB_RT_PORT,
767 + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
768 + break;
769 +
770 + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
771 + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
772 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
773 + USB_REQ_SET_FEATURE, USB_RT_PORT,
774 + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
775 + break;
776 +
777 + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
778 + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
779 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
780 + USB_REQ_SET_FEATURE, USB_RT_PORT,
781 + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
782 + break;
783 +
784 + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
785 + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
786 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
787 + USB_REQ_SET_FEATURE, USB_RT_PORT,
788 + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
789 + }
790 + }
791 + }
792 +#endif /* DWC_HS_ELECT_TST */
793
794 /* Now that the interfaces are installed, re-enable LPM. */
795 usb_unlocked_enable_lpm(dev);
796 --- a/drivers/usb/core/otg_whitelist.h
797 +++ b/drivers/usb/core/otg_whitelist.h
798 @@ -19,33 +19,82 @@
799 static struct usb_device_id whitelist_table[] = {
800
801 /* hubs are optional in OTG, but very handy ... */
802 +#define CERT_WITHOUT_HUBS
803 +#if defined(CERT_WITHOUT_HUBS)
804 +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
805 +#else
806 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
807 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
808 +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
809 +#endif
810
811 #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
812 /* FIXME actually, printers are NOT supposed to use device classes;
813 * they're supposed to use interface classes...
814 */
815 -{ USB_DEVICE_INFO(7, 1, 1) },
816 -{ USB_DEVICE_INFO(7, 1, 2) },
817 -{ USB_DEVICE_INFO(7, 1, 3) },
818 +//{ USB_DEVICE_INFO(7, 1, 1) },
819 +//{ USB_DEVICE_INFO(7, 1, 2) },
820 +//{ USB_DEVICE_INFO(7, 1, 3) },
821 #endif
822
823 #ifdef CONFIG_USB_NET_CDCETHER
824 /* Linux-USB CDC Ethernet gadget */
825 -{ USB_DEVICE(0x0525, 0xa4a1), },
826 +//{ USB_DEVICE(0x0525, 0xa4a1), },
827 /* Linux-USB CDC Ethernet + RNDIS gadget */
828 -{ USB_DEVICE(0x0525, 0xa4a2), },
829 +//{ USB_DEVICE(0x0525, 0xa4a2), },
830 #endif
831
832 #if IS_ENABLED(CONFIG_USB_TEST)
833 /* gadget zero, for testing */
834 -{ USB_DEVICE(0x0525, 0xa4a0), },
835 +//{ USB_DEVICE(0x0525, 0xa4a0), },
836 #endif
837
838 +/* OPT Tester */
839 +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
840 +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
841 +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
842 +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
843 +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
844 +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
845 +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
846 +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
847 +
848 +/* Sony cameras */
849 +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
850 +
851 +/* Memory Devices */
852 +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
853 +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
854 +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
855 +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
856 +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
857 +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
858 +
859 +/* HP Printers */
860 +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
861 +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
862 +
863 +/* Speakers */
864 +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
865 +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
866 +
867 { } /* Terminating entry */
868 };
869
870 +static inline void report_errors(struct usb_device *dev)
871 +{
872 + /* OTG MESSAGE: report errors here, customize to match your product */
873 + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
874 + le16_to_cpu(dev->descriptor.idVendor),
875 + le16_to_cpu(dev->descriptor.idProduct));
876 + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
877 + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
878 + } else {
879 + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
880 + }
881 +}
882 +
883 +
884 static int is_targeted(struct usb_device *dev)
885 {
886 struct usb_device_id *id = whitelist_table;
887 @@ -95,16 +144,57 @@ static int is_targeted(struct usb_device
888 continue;
889
890 return 1;
891 - }
892 + /* NOTE: can't use usb_match_id() since interface caches
893 + * aren't set up yet. this is cut/paste from that code.
894 + */
895 + for (id = whitelist_table; id->match_flags; id++) {
896 +#ifdef DEBUG
897 + dev_dbg(&dev->dev,
898 + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
899 + id->idVendor,
900 + id->idProduct,
901 + id->bDeviceClass,
902 + id->bDeviceSubClass,
903 + id->bDeviceProtocol);
904 +#endif
905
906 - /* add other match criteria here ... */
907 + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
908 + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
909 + continue;
910 +
911 + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
912 + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
913 + continue;
914 +
915 + /* No need to test id->bcdDevice_lo != 0, since 0 is never
916 + greater than any unsigned number. */
917 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
918 + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
919 + continue;
920 +
921 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
922 + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
923 + continue;
924 +
925 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
926 + (id->bDeviceClass != dev->descriptor.bDeviceClass))
927 + continue;
928 +
929 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
930 + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
931 + continue;
932 +
933 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
934 + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
935 + continue;
936
937 + return 1;
938 + }
939 + }
940
941 - /* OTG MESSAGE: report errors here, customize to match your product */
942 - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
943 - le16_to_cpu(dev->descriptor.idVendor),
944 - le16_to_cpu(dev->descriptor.idProduct));
945 + /* add other match criteria here ... */
946
947 + report_errors(dev);
948 return 0;
949 }
950
951 --- /dev/null
952 +++ b/drivers/usb/gadget/file_storage.c
953 @@ -0,0 +1,3676 @@
954 +/*
955 + * file_storage.c -- File-backed USB Storage Gadget, for USB development
956 + *
957 + * Copyright (C) 2003-2008 Alan Stern
958 + * All rights reserved.
959 + *
960 + * Redistribution and use in source and binary forms, with or without
961 + * modification, are permitted provided that the following conditions
962 + * are met:
963 + * 1. Redistributions of source code must retain the above copyright
964 + * notice, this list of conditions, and the following disclaimer,
965 + * without modification.
966 + * 2. Redistributions in binary form must reproduce the above copyright
967 + * notice, this list of conditions and the following disclaimer in the
968 + * documentation and/or other materials provided with the distribution.
969 + * 3. The names of the above-listed copyright holders may not be used
970 + * to endorse or promote products derived from this software without
971 + * specific prior written permission.
972 + *
973 + * ALTERNATIVELY, this software may be distributed under the terms of the
974 + * GNU General Public License ("GPL") as published by the Free Software
975 + * Foundation, either version 2 of that License or (at your option) any
976 + * later version.
977 + *
978 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
979 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
980 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
981 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
982 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
983 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
984 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
985 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
986 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
987 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
988 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
989 + */
990 +
991 +
992 +/*
993 + * The File-backed Storage Gadget acts as a USB Mass Storage device,
994 + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
995 + * to providing an example of a genuinely useful gadget driver for a USB
996 + * device, it also illustrates a technique of double-buffering for increased
997 + * throughput. Last but not least, it gives an easy way to probe the
998 + * behavior of the Mass Storage drivers in a USB host.
999 + *
1000 + * Backing storage is provided by a regular file or a block device, specified
1001 + * by the "file" module parameter. Access can be limited to read-only by
1002 + * setting the optional "ro" module parameter. (For CD-ROM emulation,
1003 + * access is always read-only.) The gadget will indicate that it has
1004 + * removable media if the optional "removable" module parameter is set.
1005 + *
1006 + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
1007 + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
1008 + * by the optional "transport" module parameter. It also supports the
1009 + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
1010 + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
1011 + * the optional "protocol" module parameter. In addition, the default
1012 + * Vendor ID, Product ID, release number and serial number can be overridden.
1013 + *
1014 + * There is support for multiple logical units (LUNs), each of which has
1015 + * its own backing file. The number of LUNs can be set using the optional
1016 + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
1017 + * files are specified using comma-separated lists for "file" and "ro".
1018 + * The default number of LUNs is taken from the number of "file" elements;
1019 + * it is 1 if "file" is not given. If "removable" is not set then a backing
1020 + * file must be specified for each LUN. If it is set, then an unspecified
1021 + * or empty backing filename means the LUN's medium is not loaded. Ideally
1022 + * each LUN would be settable independently as a disk drive or a CD-ROM
1023 + * drive, but currently all LUNs have to be the same type. The CD-ROM
1024 + * emulation includes a single data track and no audio tracks; hence there
1025 + * need be only one backing file per LUN.
1026 + *
1027 + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
1028 + * needed (an interrupt-out endpoint is also needed for CBI). The memory
1029 + * requirement amounts to two 16K buffers, size configurable by a parameter.
1030 + * Support is included for both full-speed and high-speed operation.
1031 + *
1032 + * Note that the driver is slightly non-portable in that it assumes a
1033 + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
1034 + * interrupt-in endpoints. With most device controllers this isn't an
1035 + * issue, but there may be some with hardware restrictions that prevent
1036 + * a buffer from being used by more than one endpoint.
1037 + *
1038 + * Module options:
1039 + *
1040 + * file=filename[,filename...]
1041 + * Required if "removable" is not set, names of
1042 + * the files or block devices used for
1043 + * backing storage
1044 + * serial=HHHH... Required serial number (string of hex chars)
1045 + * ro=b[,b...] Default false, booleans for read-only access
1046 + * removable Default false, boolean for removable media
1047 + * luns=N Default N = number of filenames, number of
1048 + * LUNs to support
1049 + * nofua=b[,b...] Default false, booleans for ignore FUA flag
1050 + * in SCSI WRITE(10,12) commands
1051 + * stall Default determined according to the type of
1052 + * USB device controller (usually true),
1053 + * boolean to permit the driver to halt
1054 + * bulk endpoints
1055 + * cdrom Default false, boolean for whether to emulate
1056 + * a CD-ROM drive
1057 + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
1058 + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
1059 + * ATAPI, QIC, UFI, 8070, or SCSI;
1060 + * also 1 - 6)
1061 + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
1062 + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
1063 + * release=0xRRRR Override the USB release number (bcdDevice)
1064 + * buflen=N Default N=16384, buffer size used (will be
1065 + * rounded down to a multiple of
1066 + * PAGE_CACHE_SIZE)
1067 + *
1068 + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
1069 + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
1070 + * default values are used for everything else.
1071 + *
1072 + * The pathnames of the backing files and the ro settings are available in
1073 + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
1074 + * the gadget's sysfs directory. If the "removable" option is set, writing to
1075 + * these files will simulate ejecting/loading the medium (writing an empty
1076 + * line means eject) and adjusting a write-enable tab. Changes to the ro
1077 + * setting are not allowed when the medium is loaded or if CD-ROM emulation
1078 + * is being used.
1079 + *
1080 + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
1081 + * The driver's SCSI command interface was based on the "Information
1082 + * technology - Small Computer System Interface - 2" document from
1083 + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
1084 + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
1085 + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
1086 + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
1087 + * document, Revision 1.0, December 14, 1998, available at
1088 + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
1089 + */
1090 +
1091 +
1092 +/*
1093 + * Driver Design
1094 + *
1095 + * The FSG driver is fairly straightforward. There is a main kernel
1096 + * thread that handles most of the work. Interrupt routines field
1097 + * callbacks from the controller driver: bulk- and interrupt-request
1098 + * completion notifications, endpoint-0 events, and disconnect events.
1099 + * Completion events are passed to the main thread by wakeup calls. Many
1100 + * ep0 requests are handled at interrupt time, but SetInterface,
1101 + * SetConfiguration, and device reset requests are forwarded to the
1102 + * thread in the form of "exceptions" using SIGUSR1 signals (since they
1103 + * should interrupt any ongoing file I/O operations).
1104 + *
1105 + * The thread's main routine implements the standard command/data/status
1106 + * parts of a SCSI interaction. It and its subroutines are full of tests
1107 + * for pending signals/exceptions -- all this polling is necessary since
1108 + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
1109 + * indication that the driver really wants to be running in userspace.)
1110 + * An important point is that so long as the thread is alive it keeps an
1111 + * open reference to the backing file. This will prevent unmounting
1112 + * the backing file's underlying filesystem and could cause problems
1113 + * during system shutdown, for example. To prevent such problems, the
1114 + * thread catches INT, TERM, and KILL signals and converts them into
1115 + * an EXIT exception.
1116 + *
1117 + * In normal operation the main thread is started during the gadget's
1118 + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
1119 + * exit when it receives a signal, and there's no point leaving the
1120 + * gadget running when the thread is dead. So just before the thread
1121 + * exits, it deregisters the gadget driver. This makes things a little
1122 + * tricky: The driver is deregistered at two places, and the exiting
1123 + * thread can indirectly call fsg_unbind() which in turn can tell the
1124 + * thread to exit. The first problem is resolved through the use of the
1125 + * REGISTERED atomic bitflag; the driver will only be deregistered once.
1126 + * The second problem is resolved by having fsg_unbind() check
1127 + * fsg->state; it won't try to stop the thread if the state is already
1128 + * FSG_STATE_TERMINATED.
1129 + *
1130 + * To provide maximum throughput, the driver uses a circular pipeline of
1131 + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
1132 + * arbitrarily long; in practice the benefits don't justify having more
1133 + * than 2 stages (i.e., double buffering). But it helps to think of the
1134 + * pipeline as being a long one. Each buffer head contains a bulk-in and
1135 + * a bulk-out request pointer (since the buffer can be used for both
1136 + * output and input -- directions always are given from the host's
1137 + * point of view) as well as a pointer to the buffer and various state
1138 + * variables.
1139 + *
1140 + * Use of the pipeline follows a simple protocol. There is a variable
1141 + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
1142 + * At any time that buffer head may still be in use from an earlier
1143 + * request, so each buffer head has a state variable indicating whether
1144 + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
1145 + * buffer head to be EMPTY, filling the buffer either by file I/O or by
1146 + * USB I/O (during which the buffer head is BUSY), and marking the buffer
1147 + * head FULL when the I/O is complete. Then the buffer will be emptied
1148 + * (again possibly by USB I/O, during which it is marked BUSY) and
1149 + * finally marked EMPTY again (possibly by a completion routine).
1150 + *
1151 + * A module parameter tells the driver to avoid stalling the bulk
1152 + * endpoints wherever the transport specification allows. This is
1153 + * necessary for some UDCs like the SuperH, which cannot reliably clear a
1154 + * halt on a bulk endpoint. However, under certain circumstances the
1155 + * Bulk-only specification requires a stall. In such cases the driver
1156 + * will halt the endpoint and set a flag indicating that it should clear
1157 + * the halt in software during the next device reset. Hopefully this
1158 + * will permit everything to work correctly. Furthermore, although the
1159 + * specification allows the bulk-out endpoint to halt when the host sends
1160 + * too much data, implementing this would cause an unavoidable race.
1161 + * The driver will always use the "no-stall" approach for OUT transfers.
1162 + *
1163 + * One subtle point concerns sending status-stage responses for ep0
1164 + * requests. Some of these requests, such as device reset, can involve
1165 + * interrupting an ongoing file I/O operation, which might take an
1166 + * arbitrarily long time. During that delay the host might give up on
1167 + * the original ep0 request and issue a new one. When that happens the
1168 + * driver should not notify the host about completion of the original
1169 + * request, as the host will no longer be waiting for it. So the driver
1170 + * assigns to each ep0 request a unique tag, and it keeps track of the
1171 + * tag value of the request associated with a long-running exception
1172 + * (device-reset, interface-change, or configuration-change). When the
1173 + * exception handler is finished, the status-stage response is submitted
1174 + * only if the current ep0 request tag is equal to the exception request
1175 + * tag. Thus only the most recently received ep0 request will get a
1176 + * status-stage response.
1177 + *
1178 + * Warning: This driver source file is too long. It ought to be split up
1179 + * into a header file plus about 3 separate .c files, to handle the details
1180 + * of the Gadget, USB Mass Storage, and SCSI protocols.
1181 + */
1182 +
1183 +
1184 +/* #define VERBOSE_DEBUG */
1185 +/* #define DUMP_MSGS */
1186 +
1187 +
1188 +#include <linux/blkdev.h>
1189 +#include <linux/completion.h>
1190 +#include <linux/dcache.h>
1191 +#include <linux/delay.h>
1192 +#include <linux/device.h>
1193 +#include <linux/fcntl.h>
1194 +#include <linux/file.h>
1195 +#include <linux/fs.h>
1196 +#include <linux/kref.h>
1197 +#include <linux/kthread.h>
1198 +#include <linux/limits.h>
1199 +#include <linux/module.h>
1200 +#include <linux/rwsem.h>
1201 +#include <linux/slab.h>
1202 +#include <linux/spinlock.h>
1203 +#include <linux/string.h>
1204 +#include <linux/freezer.h>
1205 +#include <linux/utsname.h>
1206 +
1207 +#include <linux/usb/ch9.h>
1208 +#include <linux/usb/gadget.h>
1209 +
1210 +#include "gadget_chips.h"
1211 +
1212 +
1213 +
1214 +/*
1215 + * Kbuild is not very cooperative with respect to linking separately
1216 + * compiled library objects into one module. So for now we won't use
1217 + * separate compilation ... ensuring init/exit sections work to shrink
1218 + * the runtime footprint, and giving us at least some parts of what
1219 + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
1220 + */
1221 +#include "usbstring.c"
1222 +#include "config.c"
1223 +#include "epautoconf.c"
1224 +
1225 +/*-------------------------------------------------------------------------*/
1226 +
1227 +#define DRIVER_DESC "File-backed Storage Gadget"
1228 +#define DRIVER_NAME "g_file_storage"
1229 +#define DRIVER_VERSION "1 September 2010"
1230 +
1231 +static char fsg_string_manufacturer[64];
1232 +static const char fsg_string_product[] = DRIVER_DESC;
1233 +static const char fsg_string_config[] = "Self-powered";
1234 +static const char fsg_string_interface[] = "Mass Storage";
1235 +
1236 +
1237 +#include "storage_common.c"
1238 +
1239 +
1240 +MODULE_DESCRIPTION(DRIVER_DESC);
1241 +MODULE_AUTHOR("Alan Stern");
1242 +MODULE_LICENSE("Dual BSD/GPL");
1243 +
1244 +/*
1245 + * This driver assumes self-powered hardware and has no way for users to
1246 + * trigger remote wakeup. It uses autoconfiguration to select endpoints
1247 + * and endpoint addresses.
1248 + */
1249 +
1250 +
1251 +/*-------------------------------------------------------------------------*/
1252 +
1253 +
1254 +/* Encapsulate the module parameter settings */
1255 +
1256 +static struct {
1257 + char *file[FSG_MAX_LUNS];
1258 + char *serial;
1259 + bool ro[FSG_MAX_LUNS];
1260 + bool nofua[FSG_MAX_LUNS];
1261 + unsigned int num_filenames;
1262 + unsigned int num_ros;
1263 + unsigned int num_nofuas;
1264 + unsigned int nluns;
1265 +
1266 + bool removable;
1267 + bool can_stall;
1268 + bool cdrom;
1269 +
1270 + char *transport_parm;
1271 + char *protocol_parm;
1272 + unsigned short vendor;
1273 + unsigned short product;
1274 + unsigned short release;
1275 + unsigned int buflen;
1276 +
1277 + int transport_type;
1278 + char *transport_name;
1279 + int protocol_type;
1280 + char *protocol_name;
1281 +
1282 +} mod_data = { // Default values
1283 + .transport_parm = "BBB",
1284 + .protocol_parm = "SCSI",
1285 + .removable = 0,
1286 + .can_stall = 1,
1287 + .cdrom = 0,
1288 + .vendor = FSG_VENDOR_ID,
1289 + .product = FSG_PRODUCT_ID,
1290 + .release = 0xffff, // Use controller chip type
1291 + .buflen = 16384,
1292 + };
1293 +
1294 +
1295 +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
1296 + S_IRUGO);
1297 +MODULE_PARM_DESC(file, "names of backing files or devices");
1298 +
1299 +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
1300 +MODULE_PARM_DESC(serial, "USB serial number");
1301 +
1302 +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
1303 +MODULE_PARM_DESC(ro, "true to force read-only");
1304 +
1305 +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
1306 + S_IRUGO);
1307 +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
1308 +
1309 +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
1310 +MODULE_PARM_DESC(luns, "number of LUNs");
1311 +
1312 +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
1313 +MODULE_PARM_DESC(removable, "true to simulate removable media");
1314 +
1315 +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
1316 +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
1317 +
1318 +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
1319 +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
1320 +
1321 +/* In the non-TEST version, only the module parameters listed above
1322 + * are available. */
1323 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1324 +
1325 +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
1326 +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
1327 +
1328 +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
1329 +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
1330 + "8070, or SCSI)");
1331 +
1332 +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
1333 +MODULE_PARM_DESC(vendor, "USB Vendor ID");
1334 +
1335 +module_param_named(product, mod_data.product, ushort, S_IRUGO);
1336 +MODULE_PARM_DESC(product, "USB Product ID");
1337 +
1338 +module_param_named(release, mod_data.release, ushort, S_IRUGO);
1339 +MODULE_PARM_DESC(release, "USB release number");
1340 +
1341 +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
1342 +MODULE_PARM_DESC(buflen, "I/O buffer size");
1343 +
1344 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1345 +
1346 +
1347 +/*
1348 + * These definitions will permit the compiler to avoid generating code for
1349 + * parts of the driver that aren't used in the non-TEST version. Even gcc
1350 + * can recognize when a test of a constant expression yields a dead code
1351 + * path.
1352 + */
1353 +
1354 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1355 +
1356 +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
1357 +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
1358 +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
1359 +
1360 +#else
1361 +
1362 +#define transport_is_bbb() 1
1363 +#define transport_is_cbi() 0
1364 +#define protocol_is_scsi() 1
1365 +
1366 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1367 +
1368 +
1369 +/*-------------------------------------------------------------------------*/
1370 +
1371 +
1372 +struct fsg_dev {
1373 + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
1374 + spinlock_t lock;
1375 + struct usb_gadget *gadget;
1376 +
1377 + /* filesem protects: backing files in use */
1378 + struct rw_semaphore filesem;
1379 +
1380 + /* reference counting: wait until all LUNs are released */
1381 + struct kref ref;
1382 +
1383 + struct usb_ep *ep0; // Handy copy of gadget->ep0
1384 + struct usb_request *ep0req; // For control responses
1385 + unsigned int ep0_req_tag;
1386 + const char *ep0req_name;
1387 +
1388 + struct usb_request *intreq; // For interrupt responses
1389 + int intreq_busy;
1390 + struct fsg_buffhd *intr_buffhd;
1391 +
1392 + unsigned int bulk_out_maxpacket;
1393 + enum fsg_state state; // For exception handling
1394 + unsigned int exception_req_tag;
1395 +
1396 + u8 config, new_config;
1397 +
1398 + unsigned int running : 1;
1399 + unsigned int bulk_in_enabled : 1;
1400 + unsigned int bulk_out_enabled : 1;
1401 + unsigned int intr_in_enabled : 1;
1402 + unsigned int phase_error : 1;
1403 + unsigned int short_packet_received : 1;
1404 + unsigned int bad_lun_okay : 1;
1405 +
1406 + unsigned long atomic_bitflags;
1407 +#define REGISTERED 0
1408 +#define IGNORE_BULK_OUT 1
1409 +#define SUSPENDED 2
1410 +
1411 + struct usb_ep *bulk_in;
1412 + struct usb_ep *bulk_out;
1413 + struct usb_ep *intr_in;
1414 +
1415 + struct fsg_buffhd *next_buffhd_to_fill;
1416 + struct fsg_buffhd *next_buffhd_to_drain;
1417 +
1418 + int thread_wakeup_needed;
1419 + struct completion thread_notifier;
1420 + struct task_struct *thread_task;
1421 +
1422 + int cmnd_size;
1423 + u8 cmnd[MAX_COMMAND_SIZE];
1424 + enum data_direction data_dir;
1425 + u32 data_size;
1426 + u32 data_size_from_cmnd;
1427 + u32 tag;
1428 + unsigned int lun;
1429 + u32 residue;
1430 + u32 usb_amount_left;
1431 +
1432 + /* The CB protocol offers no way for a host to know when a command
1433 + * has completed. As a result the next command may arrive early,
1434 + * and we will still have to handle it. For that reason we need
1435 + * a buffer to store new commands when using CB (or CBI, which
1436 + * does not oblige a host to wait for command completion either). */
1437 + int cbbuf_cmnd_size;
1438 + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
1439 +
1440 + unsigned int nluns;
1441 + struct fsg_lun *luns;
1442 + struct fsg_lun *curlun;
1443 + /* Must be the last entry */
1444 + struct fsg_buffhd buffhds[];
1445 +};
1446 +
1447 +typedef void (*fsg_routine_t)(struct fsg_dev *);
1448 +
1449 +static int exception_in_progress(struct fsg_dev *fsg)
1450 +{
1451 + return (fsg->state > FSG_STATE_IDLE);
1452 +}
1453 +
1454 +/* Make bulk-out requests be divisible by the maxpacket size */
1455 +static void set_bulk_out_req_length(struct fsg_dev *fsg,
1456 + struct fsg_buffhd *bh, unsigned int length)
1457 +{
1458 + unsigned int rem;
1459 +
1460 + bh->bulk_out_intended_length = length;
1461 + rem = length % fsg->bulk_out_maxpacket;
1462 + if (rem > 0)
1463 + length += fsg->bulk_out_maxpacket - rem;
1464 + bh->outreq->length = length;
1465 +}
1466 +
1467 +static struct fsg_dev *the_fsg;
1468 +static struct usb_gadget_driver fsg_driver;
1469 +
1470 +
1471 +/*-------------------------------------------------------------------------*/
1472 +
1473 +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
1474 +{
1475 + const char *name;
1476 +
1477 + if (ep == fsg->bulk_in)
1478 + name = "bulk-in";
1479 + else if (ep == fsg->bulk_out)
1480 + name = "bulk-out";
1481 + else
1482 + name = ep->name;
1483 + DBG(fsg, "%s set halt\n", name);
1484 + return usb_ep_set_halt(ep);
1485 +}
1486 +
1487 +
1488 +/*-------------------------------------------------------------------------*/
1489 +
1490 +/*
1491 + * DESCRIPTORS ... most are static, but strings and (full) configuration
1492 + * descriptors are built on demand. Also the (static) config and interface
1493 + * descriptors are adjusted during fsg_bind().
1494 + */
1495 +
1496 +/* There is only one configuration. */
1497 +#define CONFIG_VALUE 1
1498 +
1499 +static struct usb_device_descriptor
1500 +device_desc = {
1501 + .bLength = sizeof device_desc,
1502 + .bDescriptorType = USB_DT_DEVICE,
1503 +
1504 + .bcdUSB = cpu_to_le16(0x0200),
1505 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
1506 +
1507 + /* The next three values can be overridden by module parameters */
1508 + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
1509 + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
1510 + .bcdDevice = cpu_to_le16(0xffff),
1511 +
1512 + .iManufacturer = FSG_STRING_MANUFACTURER,
1513 + .iProduct = FSG_STRING_PRODUCT,
1514 + .iSerialNumber = FSG_STRING_SERIAL,
1515 + .bNumConfigurations = 1,
1516 +};
1517 +
1518 +static struct usb_config_descriptor
1519 +config_desc = {
1520 + .bLength = sizeof config_desc,
1521 + .bDescriptorType = USB_DT_CONFIG,
1522 +
1523 + /* wTotalLength computed by usb_gadget_config_buf() */
1524 + .bNumInterfaces = 1,
1525 + .bConfigurationValue = CONFIG_VALUE,
1526 + .iConfiguration = FSG_STRING_CONFIG,
1527 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1528 + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
1529 +};
1530 +
1531 +
1532 +static struct usb_qualifier_descriptor
1533 +dev_qualifier = {
1534 + .bLength = sizeof dev_qualifier,
1535 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
1536 +
1537 + .bcdUSB = cpu_to_le16(0x0200),
1538 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
1539 +
1540 + .bNumConfigurations = 1,
1541 +};
1542 +
1543 +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
1544 +{
1545 + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
1546 + buf += USB_DT_BOS_SIZE;
1547 +
1548 + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
1549 + buf += USB_DT_USB_EXT_CAP_SIZE;
1550 +
1551 + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
1552 +
1553 + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
1554 + + USB_DT_USB_EXT_CAP_SIZE;
1555 +}
1556 +
1557 +/*
1558 + * Config descriptors must agree with the code that sets configurations
1559 + * and with code managing interfaces and their altsettings. They must
1560 + * also handle different speeds and other-speed requests.
1561 + */
1562 +static int populate_config_buf(struct usb_gadget *gadget,
1563 + u8 *buf, u8 type, unsigned index)
1564 +{
1565 + enum usb_device_speed speed = gadget->speed;
1566 + int len;
1567 + const struct usb_descriptor_header **function;
1568 +
1569 + if (index > 0)
1570 + return -EINVAL;
1571 +
1572 + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
1573 + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
1574 + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
1575 + ? (const struct usb_descriptor_header **)fsg_hs_function
1576 + : (const struct usb_descriptor_header **)fsg_fs_function;
1577 +
1578 + /* for now, don't advertise srp-only devices */
1579 + if (!gadget_is_otg(gadget))
1580 + function++;
1581 +
1582 + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
1583 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1584 + return len;
1585 +}
1586 +
1587 +
1588 +/*-------------------------------------------------------------------------*/
1589 +
1590 +/* These routines may be called in process context or in_irq */
1591 +
1592 +/* Caller must hold fsg->lock */
1593 +static void wakeup_thread(struct fsg_dev *fsg)
1594 +{
1595 + /* Tell the main thread that something has happened */
1596 + fsg->thread_wakeup_needed = 1;
1597 + if (fsg->thread_task)
1598 + wake_up_process(fsg->thread_task);
1599 +}
1600 +
1601 +
1602 +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
1603 +{
1604 + unsigned long flags;
1605 +
1606 + /* Do nothing if a higher-priority exception is already in progress.
1607 + * If a lower-or-equal priority exception is in progress, preempt it
1608 + * and notify the main thread by sending it a signal. */
1609 + spin_lock_irqsave(&fsg->lock, flags);
1610 + if (fsg->state <= new_state) {
1611 + fsg->exception_req_tag = fsg->ep0_req_tag;
1612 + fsg->state = new_state;
1613 + if (fsg->thread_task)
1614 + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
1615 + fsg->thread_task);
1616 + }
1617 + spin_unlock_irqrestore(&fsg->lock, flags);
1618 +}
1619 +
1620 +
1621 +/*-------------------------------------------------------------------------*/
1622 +
1623 +/* The disconnect callback and ep0 routines. These always run in_irq,
1624 + * except that ep0_queue() is called in the main thread to acknowledge
1625 + * completion of various requests: set config, set interface, and
1626 + * Bulk-only device reset. */
1627 +
1628 +static void fsg_disconnect(struct usb_gadget *gadget)
1629 +{
1630 + struct fsg_dev *fsg = get_gadget_data(gadget);
1631 +
1632 + DBG(fsg, "disconnect or port reset\n");
1633 + raise_exception(fsg, FSG_STATE_DISCONNECT);
1634 +}
1635 +
1636 +
1637 +static int ep0_queue(struct fsg_dev *fsg)
1638 +{
1639 + int rc;
1640 +
1641 + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
1642 + if (rc != 0 && rc != -ESHUTDOWN) {
1643 +
1644 + /* We can't do much more than wait for a reset */
1645 + WARNING(fsg, "error in submission: %s --> %d\n",
1646 + fsg->ep0->name, rc);
1647 + }
1648 + return rc;
1649 +}
1650 +
1651 +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
1652 +{
1653 + struct fsg_dev *fsg = ep->driver_data;
1654 +
1655 + if (req->actual > 0)
1656 + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
1657 + if (req->status || req->actual != req->length)
1658 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1659 + req->status, req->actual, req->length);
1660 + if (req->status == -ECONNRESET) // Request was cancelled
1661 + usb_ep_fifo_flush(ep);
1662 +
1663 + if (req->status == 0 && req->context)
1664 + ((fsg_routine_t) (req->context))(fsg);
1665 +}
1666 +
1667 +
1668 +/*-------------------------------------------------------------------------*/
1669 +
1670 +/* Bulk and interrupt endpoint completion handlers.
1671 + * These always run in_irq. */
1672 +
1673 +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
1674 +{
1675 + struct fsg_dev *fsg = ep->driver_data;
1676 + struct fsg_buffhd *bh = req->context;
1677 +
1678 + if (req->status || req->actual != req->length)
1679 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1680 + req->status, req->actual, req->length);
1681 + if (req->status == -ECONNRESET) // Request was cancelled
1682 + usb_ep_fifo_flush(ep);
1683 +
1684 + /* Hold the lock while we update the request and buffer states */
1685 + smp_wmb();
1686 + spin_lock(&fsg->lock);
1687 + bh->inreq_busy = 0;
1688 + bh->state = BUF_STATE_EMPTY;
1689 + wakeup_thread(fsg);
1690 + spin_unlock(&fsg->lock);
1691 +}
1692 +
1693 +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
1694 +{
1695 + struct fsg_dev *fsg = ep->driver_data;
1696 + struct fsg_buffhd *bh = req->context;
1697 +
1698 + dump_msg(fsg, "bulk-out", req->buf, req->actual);
1699 + if (req->status || req->actual != bh->bulk_out_intended_length)
1700 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1701 + req->status, req->actual,
1702 + bh->bulk_out_intended_length);
1703 + if (req->status == -ECONNRESET) // Request was cancelled
1704 + usb_ep_fifo_flush(ep);
1705 +
1706 + /* Hold the lock while we update the request and buffer states */
1707 + smp_wmb();
1708 + spin_lock(&fsg->lock);
1709 + bh->outreq_busy = 0;
1710 + bh->state = BUF_STATE_FULL;
1711 + wakeup_thread(fsg);
1712 + spin_unlock(&fsg->lock);
1713 +}
1714 +
1715 +
1716 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1717 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
1718 +{
1719 + struct fsg_dev *fsg = ep->driver_data;
1720 + struct fsg_buffhd *bh = req->context;
1721 +
1722 + if (req->status || req->actual != req->length)
1723 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1724 + req->status, req->actual, req->length);
1725 + if (req->status == -ECONNRESET) // Request was cancelled
1726 + usb_ep_fifo_flush(ep);
1727 +
1728 + /* Hold the lock while we update the request and buffer states */
1729 + smp_wmb();
1730 + spin_lock(&fsg->lock);
1731 + fsg->intreq_busy = 0;
1732 + bh->state = BUF_STATE_EMPTY;
1733 + wakeup_thread(fsg);
1734 + spin_unlock(&fsg->lock);
1735 +}
1736 +
1737 +#else
1738 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
1739 +{}
1740 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1741 +
1742 +
1743 +/*-------------------------------------------------------------------------*/
1744 +
1745 +/* Ep0 class-specific handlers. These always run in_irq. */
1746 +
1747 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1748 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1749 +{
1750 + struct usb_request *req = fsg->ep0req;
1751 + static u8 cbi_reset_cmnd[6] = {
1752 + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
1753 +
1754 + /* Error in command transfer? */
1755 + if (req->status || req->length != req->actual ||
1756 + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
1757 +
1758 + /* Not all controllers allow a protocol stall after
1759 + * receiving control-out data, but we'll try anyway. */
1760 + fsg_set_halt(fsg, fsg->ep0);
1761 + return; // Wait for reset
1762 + }
1763 +
1764 + /* Is it the special reset command? */
1765 + if (req->actual >= sizeof cbi_reset_cmnd &&
1766 + memcmp(req->buf, cbi_reset_cmnd,
1767 + sizeof cbi_reset_cmnd) == 0) {
1768 +
1769 + /* Raise an exception to stop the current operation
1770 + * and reinitialize our state. */
1771 + DBG(fsg, "cbi reset request\n");
1772 + raise_exception(fsg, FSG_STATE_RESET);
1773 + return;
1774 + }
1775 +
1776 + VDBG(fsg, "CB[I] accept device-specific command\n");
1777 + spin_lock(&fsg->lock);
1778 +
1779 + /* Save the command for later */
1780 + if (fsg->cbbuf_cmnd_size)
1781 + WARNING(fsg, "CB[I] overwriting previous command\n");
1782 + fsg->cbbuf_cmnd_size = req->actual;
1783 + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
1784 +
1785 + wakeup_thread(fsg);
1786 + spin_unlock(&fsg->lock);
1787 +}
1788 +
1789 +#else
1790 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1791 +{}
1792 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1793 +
1794 +
1795 +static int class_setup_req(struct fsg_dev *fsg,
1796 + const struct usb_ctrlrequest *ctrl)
1797 +{
1798 + struct usb_request *req = fsg->ep0req;
1799 + int value = -EOPNOTSUPP;
1800 + u16 w_index = le16_to_cpu(ctrl->wIndex);
1801 + u16 w_value = le16_to_cpu(ctrl->wValue);
1802 + u16 w_length = le16_to_cpu(ctrl->wLength);
1803 +
1804 + if (!fsg->config)
1805 + return value;
1806 +
1807 + /* Handle Bulk-only class-specific requests */
1808 + if (transport_is_bbb()) {
1809 + switch (ctrl->bRequest) {
1810 +
1811 + case US_BULK_RESET_REQUEST:
1812 + if (ctrl->bRequestType != (USB_DIR_OUT |
1813 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1814 + break;
1815 + if (w_index != 0 || w_value != 0 || w_length != 0) {
1816 + value = -EDOM;
1817 + break;
1818 + }
1819 +
1820 + /* Raise an exception to stop the current operation
1821 + * and reinitialize our state. */
1822 + DBG(fsg, "bulk reset request\n");
1823 + raise_exception(fsg, FSG_STATE_RESET);
1824 + value = DELAYED_STATUS;
1825 + break;
1826 +
1827 + case US_BULK_GET_MAX_LUN:
1828 + if (ctrl->bRequestType != (USB_DIR_IN |
1829 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1830 + break;
1831 + if (w_index != 0 || w_value != 0 || w_length != 1) {
1832 + value = -EDOM;
1833 + break;
1834 + }
1835 + VDBG(fsg, "get max LUN\n");
1836 + *(u8 *) req->buf = fsg->nluns - 1;
1837 + value = 1;
1838 + break;
1839 + }
1840 + }
1841 +
1842 + /* Handle CBI class-specific requests */
1843 + else {
1844 + switch (ctrl->bRequest) {
1845 +
1846 + case USB_CBI_ADSC_REQUEST:
1847 + if (ctrl->bRequestType != (USB_DIR_OUT |
1848 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1849 + break;
1850 + if (w_index != 0 || w_value != 0) {
1851 + value = -EDOM;
1852 + break;
1853 + }
1854 + if (w_length > MAX_COMMAND_SIZE) {
1855 + value = -EOVERFLOW;
1856 + break;
1857 + }
1858 + value = w_length;
1859 + fsg->ep0req->context = received_cbi_adsc;
1860 + break;
1861 + }
1862 + }
1863 +
1864 + if (value == -EOPNOTSUPP)
1865 + VDBG(fsg,
1866 + "unknown class-specific control req "
1867 + "%02x.%02x v%04x i%04x l%u\n",
1868 + ctrl->bRequestType, ctrl->bRequest,
1869 + le16_to_cpu(ctrl->wValue), w_index, w_length);
1870 + return value;
1871 +}
1872 +
1873 +
1874 +/*-------------------------------------------------------------------------*/
1875 +
1876 +/* Ep0 standard request handlers. These always run in_irq. */
1877 +
1878 +static int standard_setup_req(struct fsg_dev *fsg,
1879 + const struct usb_ctrlrequest *ctrl)
1880 +{
1881 + struct usb_request *req = fsg->ep0req;
1882 + int value = -EOPNOTSUPP;
1883 + u16 w_index = le16_to_cpu(ctrl->wIndex);
1884 + u16 w_value = le16_to_cpu(ctrl->wValue);
1885 +
1886 + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
1887 + * but config change events will also reconfigure hardware. */
1888 + switch (ctrl->bRequest) {
1889 +
1890 + case USB_REQ_GET_DESCRIPTOR:
1891 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1892 + USB_RECIP_DEVICE))
1893 + break;
1894 + switch (w_value >> 8) {
1895 +
1896 + case USB_DT_DEVICE:
1897 + VDBG(fsg, "get device descriptor\n");
1898 + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
1899 + value = sizeof device_desc;
1900 + memcpy(req->buf, &device_desc, value);
1901 + break;
1902 + case USB_DT_DEVICE_QUALIFIER:
1903 + VDBG(fsg, "get device qualifier\n");
1904 + if (!gadget_is_dualspeed(fsg->gadget) ||
1905 + fsg->gadget->speed == USB_SPEED_SUPER)
1906 + break;
1907 + /*
1908 + * Assume ep0 uses the same maxpacket value for both
1909 + * speeds
1910 + */
1911 + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
1912 + value = sizeof dev_qualifier;
1913 + memcpy(req->buf, &dev_qualifier, value);
1914 + break;
1915 +
1916 + case USB_DT_OTHER_SPEED_CONFIG:
1917 + VDBG(fsg, "get other-speed config descriptor\n");
1918 + if (!gadget_is_dualspeed(fsg->gadget) ||
1919 + fsg->gadget->speed == USB_SPEED_SUPER)
1920 + break;
1921 + goto get_config;
1922 + case USB_DT_CONFIG:
1923 + VDBG(fsg, "get configuration descriptor\n");
1924 +get_config:
1925 + value = populate_config_buf(fsg->gadget,
1926 + req->buf,
1927 + w_value >> 8,
1928 + w_value & 0xff);
1929 + break;
1930 +
1931 + case USB_DT_STRING:
1932 + VDBG(fsg, "get string descriptor\n");
1933 +
1934 + /* wIndex == language code */
1935 + value = usb_gadget_get_string(&fsg_stringtab,
1936 + w_value & 0xff, req->buf);
1937 + break;
1938 +
1939 + case USB_DT_BOS:
1940 + VDBG(fsg, "get bos descriptor\n");
1941 +
1942 + if (gadget_is_superspeed(fsg->gadget))
1943 + value = populate_bos(fsg, req->buf);
1944 + break;
1945 + }
1946 +
1947 + break;
1948 +
1949 + /* One config, two speeds */
1950 + case USB_REQ_SET_CONFIGURATION:
1951 + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
1952 + USB_RECIP_DEVICE))
1953 + break;
1954 + VDBG(fsg, "set configuration\n");
1955 + if (w_value == CONFIG_VALUE || w_value == 0) {
1956 + fsg->new_config = w_value;
1957 +
1958 + /* Raise an exception to wipe out previous transaction
1959 + * state (queued bufs, etc) and set the new config. */
1960 + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
1961 + value = DELAYED_STATUS;
1962 + }
1963 + break;
1964 + case USB_REQ_GET_CONFIGURATION:
1965 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1966 + USB_RECIP_DEVICE))
1967 + break;
1968 + VDBG(fsg, "get configuration\n");
1969 + *(u8 *) req->buf = fsg->config;
1970 + value = 1;
1971 + break;
1972 +
1973 + case USB_REQ_SET_INTERFACE:
1974 + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
1975 + USB_RECIP_INTERFACE))
1976 + break;
1977 + if (fsg->config && w_index == 0) {
1978 +
1979 + /* Raise an exception to wipe out previous transaction
1980 + * state (queued bufs, etc) and install the new
1981 + * interface altsetting. */
1982 + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
1983 + value = DELAYED_STATUS;
1984 + }
1985 + break;
1986 + case USB_REQ_GET_INTERFACE:
1987 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1988 + USB_RECIP_INTERFACE))
1989 + break;
1990 + if (!fsg->config)
1991 + break;
1992 + if (w_index != 0) {
1993 + value = -EDOM;
1994 + break;
1995 + }
1996 + VDBG(fsg, "get interface\n");
1997 + *(u8 *) req->buf = 0;
1998 + value = 1;
1999 + break;
2000 +
2001 + default:
2002 + VDBG(fsg,
2003 + "unknown control req %02x.%02x v%04x i%04x l%u\n",
2004 + ctrl->bRequestType, ctrl->bRequest,
2005 + w_value, w_index, le16_to_cpu(ctrl->wLength));
2006 + }
2007 +
2008 + return value;
2009 +}
2010 +
2011 +
2012 +static int fsg_setup(struct usb_gadget *gadget,
2013 + const struct usb_ctrlrequest *ctrl)
2014 +{
2015 + struct fsg_dev *fsg = get_gadget_data(gadget);
2016 + int rc;
2017 + int w_length = le16_to_cpu(ctrl->wLength);
2018 +
2019 + ++fsg->ep0_req_tag; // Record arrival of a new request
2020 + fsg->ep0req->context = NULL;
2021 + fsg->ep0req->length = 0;
2022 + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
2023 +
2024 + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
2025 + rc = class_setup_req(fsg, ctrl);
2026 + else
2027 + rc = standard_setup_req(fsg, ctrl);
2028 +
2029 + /* Respond with data/status or defer until later? */
2030 + if (rc >= 0 && rc != DELAYED_STATUS) {
2031 + rc = min(rc, w_length);
2032 + fsg->ep0req->length = rc;
2033 + fsg->ep0req->zero = rc < w_length;
2034 + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
2035 + "ep0-in" : "ep0-out");
2036 + rc = ep0_queue(fsg);
2037 + }
2038 +
2039 + /* Device either stalls (rc < 0) or reports success */
2040 + return rc;
2041 +}
2042 +
2043 +
2044 +/*-------------------------------------------------------------------------*/
2045 +
2046 +/* All the following routines run in process context */
2047 +
2048 +
2049 +/* Use this for bulk or interrupt transfers, not ep0 */
2050 +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
2051 + struct usb_request *req, int *pbusy,
2052 + enum fsg_buffer_state *state)
2053 +{
2054 + int rc;
2055 +
2056 + if (ep == fsg->bulk_in)
2057 + dump_msg(fsg, "bulk-in", req->buf, req->length);
2058 + else if (ep == fsg->intr_in)
2059 + dump_msg(fsg, "intr-in", req->buf, req->length);
2060 +
2061 + spin_lock_irq(&fsg->lock);
2062 + *pbusy = 1;
2063 + *state = BUF_STATE_BUSY;
2064 + spin_unlock_irq(&fsg->lock);
2065 + rc = usb_ep_queue(ep, req, GFP_KERNEL);
2066 + if (rc != 0) {
2067 + *pbusy = 0;
2068 + *state = BUF_STATE_EMPTY;
2069 +
2070 + /* We can't do much more than wait for a reset */
2071 +
2072 + /* Note: currently the net2280 driver fails zero-length
2073 + * submissions if DMA is enabled. */
2074 + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
2075 + req->length == 0))
2076 + WARNING(fsg, "error in submission: %s --> %d\n",
2077 + ep->name, rc);
2078 + }
2079 +}
2080 +
2081 +
2082 +static int sleep_thread(struct fsg_dev *fsg)
2083 +{
2084 + int rc = 0;
2085 +
2086 + /* Wait until a signal arrives or we are woken up */
2087 + for (;;) {
2088 + try_to_freeze();
2089 + set_current_state(TASK_INTERRUPTIBLE);
2090 + if (signal_pending(current)) {
2091 + rc = -EINTR;
2092 + break;
2093 + }
2094 + if (fsg->thread_wakeup_needed)
2095 + break;
2096 + schedule();
2097 + }
2098 + __set_current_state(TASK_RUNNING);
2099 + fsg->thread_wakeup_needed = 0;
2100 + return rc;
2101 +}
2102 +
2103 +
2104 +/*-------------------------------------------------------------------------*/
2105 +
2106 +static int do_read(struct fsg_dev *fsg)
2107 +{
2108 + struct fsg_lun *curlun = fsg->curlun;
2109 + u32 lba;
2110 + struct fsg_buffhd *bh;
2111 + int rc;
2112 + u32 amount_left;
2113 + loff_t file_offset, file_offset_tmp;
2114 + unsigned int amount;
2115 + ssize_t nread;
2116 +
2117 + /* Get the starting Logical Block Address and check that it's
2118 + * not too big */
2119 + if (fsg->cmnd[0] == READ_6)
2120 + lba = get_unaligned_be24(&fsg->cmnd[1]);
2121 + else {
2122 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2123 +
2124 + /* We allow DPO (Disable Page Out = don't save data in the
2125 + * cache) and FUA (Force Unit Access = don't read from the
2126 + * cache), but we don't implement them. */
2127 + if ((fsg->cmnd[1] & ~0x18) != 0) {
2128 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2129 + return -EINVAL;
2130 + }
2131 + }
2132 + if (lba >= curlun->num_sectors) {
2133 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2134 + return -EINVAL;
2135 + }
2136 + file_offset = ((loff_t) lba) << curlun->blkbits;
2137 +
2138 + /* Carry out the file reads */
2139 + amount_left = fsg->data_size_from_cmnd;
2140 + if (unlikely(amount_left == 0))
2141 + return -EIO; // No default reply
2142 +
2143 + for (;;) {
2144 +
2145 + /* Figure out how much we need to read:
2146 + * Try to read the remaining amount.
2147 + * But don't read more than the buffer size.
2148 + * And don't try to read past the end of the file.
2149 + */
2150 + amount = min((unsigned int) amount_left, mod_data.buflen);
2151 + amount = min((loff_t) amount,
2152 + curlun->file_length - file_offset);
2153 +
2154 + /* Wait for the next buffer to become available */
2155 + bh = fsg->next_buffhd_to_fill;
2156 + while (bh->state != BUF_STATE_EMPTY) {
2157 + rc = sleep_thread(fsg);
2158 + if (rc)
2159 + return rc;
2160 + }
2161 +
2162 + /* If we were asked to read past the end of file,
2163 + * end with an empty buffer. */
2164 + if (amount == 0) {
2165 + curlun->sense_data =
2166 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2167 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2168 + curlun->info_valid = 1;
2169 + bh->inreq->length = 0;
2170 + bh->state = BUF_STATE_FULL;
2171 + break;
2172 + }
2173 +
2174 + /* Perform the read */
2175 + file_offset_tmp = file_offset;
2176 + nread = vfs_read(curlun->filp,
2177 + (char __user *) bh->buf,
2178 + amount, &file_offset_tmp);
2179 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
2180 + (unsigned long long) file_offset,
2181 + (int) nread);
2182 + if (signal_pending(current))
2183 + return -EINTR;
2184 +
2185 + if (nread < 0) {
2186 + LDBG(curlun, "error in file read: %d\n",
2187 + (int) nread);
2188 + nread = 0;
2189 + } else if (nread < amount) {
2190 + LDBG(curlun, "partial file read: %d/%u\n",
2191 + (int) nread, amount);
2192 + nread = round_down(nread, curlun->blksize);
2193 + }
2194 + file_offset += nread;
2195 + amount_left -= nread;
2196 + fsg->residue -= nread;
2197 +
2198 + /* Except at the end of the transfer, nread will be
2199 + * equal to the buffer size, which is divisible by the
2200 + * bulk-in maxpacket size.
2201 + */
2202 + bh->inreq->length = nread;
2203 + bh->state = BUF_STATE_FULL;
2204 +
2205 + /* If an error occurred, report it and its position */
2206 + if (nread < amount) {
2207 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
2208 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2209 + curlun->info_valid = 1;
2210 + break;
2211 + }
2212 +
2213 + if (amount_left == 0)
2214 + break; // No more left to read
2215 +
2216 + /* Send this buffer and go read some more */
2217 + bh->inreq->zero = 0;
2218 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
2219 + &bh->inreq_busy, &bh->state);
2220 + fsg->next_buffhd_to_fill = bh->next;
2221 + }
2222 +
2223 + return -EIO; // No default reply
2224 +}
2225 +
2226 +
2227 +/*-------------------------------------------------------------------------*/
2228 +
2229 +static int do_write(struct fsg_dev *fsg)
2230 +{
2231 + struct fsg_lun *curlun = fsg->curlun;
2232 + u32 lba;
2233 + struct fsg_buffhd *bh;
2234 + int get_some_more;
2235 + u32 amount_left_to_req, amount_left_to_write;
2236 + loff_t usb_offset, file_offset, file_offset_tmp;
2237 + unsigned int amount;
2238 + ssize_t nwritten;
2239 + int rc;
2240 +
2241 + if (curlun->ro) {
2242 + curlun->sense_data = SS_WRITE_PROTECTED;
2243 + return -EINVAL;
2244 + }
2245 + spin_lock(&curlun->filp->f_lock);
2246 + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
2247 + spin_unlock(&curlun->filp->f_lock);
2248 +
2249 + /* Get the starting Logical Block Address and check that it's
2250 + * not too big */
2251 + if (fsg->cmnd[0] == WRITE_6)
2252 + lba = get_unaligned_be24(&fsg->cmnd[1]);
2253 + else {
2254 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2255 +
2256 + /* We allow DPO (Disable Page Out = don't save data in the
2257 + * cache) and FUA (Force Unit Access = write directly to the
2258 + * medium). We don't implement DPO; we implement FUA by
2259 + * performing synchronous output. */
2260 + if ((fsg->cmnd[1] & ~0x18) != 0) {
2261 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2262 + return -EINVAL;
2263 + }
2264 + /* FUA */
2265 + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
2266 + spin_lock(&curlun->filp->f_lock);
2267 + curlun->filp->f_flags |= O_DSYNC;
2268 + spin_unlock(&curlun->filp->f_lock);
2269 + }
2270 + }
2271 + if (lba >= curlun->num_sectors) {
2272 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2273 + return -EINVAL;
2274 + }
2275 +
2276 + /* Carry out the file writes */
2277 + get_some_more = 1;
2278 + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
2279 + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
2280 +
2281 + while (amount_left_to_write > 0) {
2282 +
2283 + /* Queue a request for more data from the host */
2284 + bh = fsg->next_buffhd_to_fill;
2285 + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
2286 +
2287 + /* Figure out how much we want to get:
2288 + * Try to get the remaining amount,
2289 + * but not more than the buffer size.
2290 + */
2291 + amount = min(amount_left_to_req, mod_data.buflen);
2292 +
2293 + /* Beyond the end of the backing file? */
2294 + if (usb_offset >= curlun->file_length) {
2295 + get_some_more = 0;
2296 + curlun->sense_data =
2297 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2298 + curlun->sense_data_info = usb_offset >> curlun->blkbits;
2299 + curlun->info_valid = 1;
2300 + continue;
2301 + }
2302 +
2303 + /* Get the next buffer */
2304 + usb_offset += amount;
2305 + fsg->usb_amount_left -= amount;
2306 + amount_left_to_req -= amount;
2307 + if (amount_left_to_req == 0)
2308 + get_some_more = 0;
2309 +
2310 + /* Except at the end of the transfer, amount will be
2311 + * equal to the buffer size, which is divisible by
2312 + * the bulk-out maxpacket size.
2313 + */
2314 + set_bulk_out_req_length(fsg, bh, amount);
2315 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
2316 + &bh->outreq_busy, &bh->state);
2317 + fsg->next_buffhd_to_fill = bh->next;
2318 + continue;
2319 + }
2320 +
2321 + /* Write the received data to the backing file */
2322 + bh = fsg->next_buffhd_to_drain;
2323 + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
2324 + break; // We stopped early
2325 + if (bh->state == BUF_STATE_FULL) {
2326 + smp_rmb();
2327 + fsg->next_buffhd_to_drain = bh->next;
2328 + bh->state = BUF_STATE_EMPTY;
2329 +
2330 + /* Did something go wrong with the transfer? */
2331 + if (bh->outreq->status != 0) {
2332 + curlun->sense_data = SS_COMMUNICATION_FAILURE;
2333 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2334 + curlun->info_valid = 1;
2335 + break;
2336 + }
2337 +
2338 + amount = bh->outreq->actual;
2339 + if (curlun->file_length - file_offset < amount) {
2340 + LERROR(curlun,
2341 + "write %u @ %llu beyond end %llu\n",
2342 + amount, (unsigned long long) file_offset,
2343 + (unsigned long long) curlun->file_length);
2344 + amount = curlun->file_length - file_offset;
2345 + }
2346 +
2347 + /* Don't accept excess data. The spec doesn't say
2348 + * what to do in this case. We'll ignore the error.
2349 + */
2350 + amount = min(amount, bh->bulk_out_intended_length);
2351 +
2352 + /* Don't write a partial block */
2353 + amount = round_down(amount, curlun->blksize);
2354 + if (amount == 0)
2355 + goto empty_write;
2356 +
2357 + /* Perform the write */
2358 + file_offset_tmp = file_offset;
2359 + nwritten = vfs_write(curlun->filp,
2360 + (char __user *) bh->buf,
2361 + amount, &file_offset_tmp);
2362 + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
2363 + (unsigned long long) file_offset,
2364 + (int) nwritten);
2365 + if (signal_pending(current))
2366 + return -EINTR; // Interrupted!
2367 +
2368 + if (nwritten < 0) {
2369 + LDBG(curlun, "error in file write: %d\n",
2370 + (int) nwritten);
2371 + nwritten = 0;
2372 + } else if (nwritten < amount) {
2373 + LDBG(curlun, "partial file write: %d/%u\n",
2374 + (int) nwritten, amount);
2375 + nwritten = round_down(nwritten, curlun->blksize);
2376 + }
2377 + file_offset += nwritten;
2378 + amount_left_to_write -= nwritten;
2379 + fsg->residue -= nwritten;
2380 +
2381 + /* If an error occurred, report it and its position */
2382 + if (nwritten < amount) {
2383 + curlun->sense_data = SS_WRITE_ERROR;
2384 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2385 + curlun->info_valid = 1;
2386 + break;
2387 + }
2388 +
2389 + empty_write:
2390 + /* Did the host decide to stop early? */
2391 + if (bh->outreq->actual < bh->bulk_out_intended_length) {
2392 + fsg->short_packet_received = 1;
2393 + break;
2394 + }
2395 + continue;
2396 + }
2397 +
2398 + /* Wait for something to happen */
2399 + rc = sleep_thread(fsg);
2400 + if (rc)
2401 + return rc;
2402 + }
2403 +
2404 + return -EIO; // No default reply
2405 +}
2406 +
2407 +
2408 +/*-------------------------------------------------------------------------*/
2409 +
2410 +static int do_synchronize_cache(struct fsg_dev *fsg)
2411 +{
2412 + struct fsg_lun *curlun = fsg->curlun;
2413 + int rc;
2414 +
2415 + /* We ignore the requested LBA and write out all file's
2416 + * dirty data buffers. */
2417 + rc = fsg_lun_fsync_sub(curlun);
2418 + if (rc)
2419 + curlun->sense_data = SS_WRITE_ERROR;
2420 + return 0;
2421 +}
2422 +
2423 +
2424 +/*-------------------------------------------------------------------------*/
2425 +
2426 +static void invalidate_sub(struct fsg_lun *curlun)
2427 +{
2428 + struct file *filp = curlun->filp;
2429 + struct inode *inode = filp->f_path.dentry->d_inode;
2430 + unsigned long rc;
2431 +
2432 + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
2433 + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
2434 +}
2435 +
2436 +static int do_verify(struct fsg_dev *fsg)
2437 +{
2438 + struct fsg_lun *curlun = fsg->curlun;
2439 + u32 lba;
2440 + u32 verification_length;
2441 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
2442 + loff_t file_offset, file_offset_tmp;
2443 + u32 amount_left;
2444 + unsigned int amount;
2445 + ssize_t nread;
2446 +
2447 + /* Get the starting Logical Block Address and check that it's
2448 + * not too big */
2449 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2450 + if (lba >= curlun->num_sectors) {
2451 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2452 + return -EINVAL;
2453 + }
2454 +
2455 + /* We allow DPO (Disable Page Out = don't save data in the
2456 + * cache) but we don't implement it. */
2457 + if ((fsg->cmnd[1] & ~0x10) != 0) {
2458 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2459 + return -EINVAL;
2460 + }
2461 +
2462 + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
2463 + if (unlikely(verification_length == 0))
2464 + return -EIO; // No default reply
2465 +
2466 + /* Prepare to carry out the file verify */
2467 + amount_left = verification_length << curlun->blkbits;
2468 + file_offset = ((loff_t) lba) << curlun->blkbits;
2469 +
2470 + /* Write out all the dirty buffers before invalidating them */
2471 + fsg_lun_fsync_sub(curlun);
2472 + if (signal_pending(current))
2473 + return -EINTR;
2474 +
2475 + invalidate_sub(curlun);
2476 + if (signal_pending(current))
2477 + return -EINTR;
2478 +
2479 + /* Just try to read the requested blocks */
2480 + while (amount_left > 0) {
2481 +
2482 + /* Figure out how much we need to read:
2483 + * Try to read the remaining amount, but not more than
2484 + * the buffer size.
2485 + * And don't try to read past the end of the file.
2486 + */
2487 + amount = min((unsigned int) amount_left, mod_data.buflen);
2488 + amount = min((loff_t) amount,
2489 + curlun->file_length - file_offset);
2490 + if (amount == 0) {
2491 + curlun->sense_data =
2492 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2493 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2494 + curlun->info_valid = 1;
2495 + break;
2496 + }
2497 +
2498 + /* Perform the read */
2499 + file_offset_tmp = file_offset;
2500 + nread = vfs_read(curlun->filp,
2501 + (char __user *) bh->buf,
2502 + amount, &file_offset_tmp);
2503 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
2504 + (unsigned long long) file_offset,
2505 + (int) nread);
2506 + if (signal_pending(current))
2507 + return -EINTR;
2508 +
2509 + if (nread < 0) {
2510 + LDBG(curlun, "error in file verify: %d\n",
2511 + (int) nread);
2512 + nread = 0;
2513 + } else if (nread < amount) {
2514 + LDBG(curlun, "partial file verify: %d/%u\n",
2515 + (int) nread, amount);
2516 + nread = round_down(nread, curlun->blksize);
2517 + }
2518 + if (nread == 0) {
2519 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
2520 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2521 + curlun->info_valid = 1;
2522 + break;
2523 + }
2524 + file_offset += nread;
2525 + amount_left -= nread;
2526 + }
2527 + return 0;
2528 +}
2529 +
2530 +
2531 +/*-------------------------------------------------------------------------*/
2532 +
2533 +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2534 +{
2535 + u8 *buf = (u8 *) bh->buf;
2536 +
2537 + static char vendor_id[] = "Linux ";
2538 + static char product_disk_id[] = "File-Stor Gadget";
2539 + static char product_cdrom_id[] = "File-CD Gadget ";
2540 +
2541 + if (!fsg->curlun) { // Unsupported LUNs are okay
2542 + fsg->bad_lun_okay = 1;
2543 + memset(buf, 0, 36);
2544 + buf[0] = 0x7f; // Unsupported, no device-type
2545 + buf[4] = 31; // Additional length
2546 + return 36;
2547 + }
2548 +
2549 + memset(buf, 0, 8);
2550 + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
2551 + if (mod_data.removable)
2552 + buf[1] = 0x80;
2553 + buf[2] = 2; // ANSI SCSI level 2
2554 + buf[3] = 2; // SCSI-2 INQUIRY data format
2555 + buf[4] = 31; // Additional length
2556 + // No special options
2557 + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
2558 + (mod_data.cdrom ? product_cdrom_id :
2559 + product_disk_id),
2560 + mod_data.release);
2561 + return 36;
2562 +}
2563 +
2564 +
2565 +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2566 +{
2567 + struct fsg_lun *curlun = fsg->curlun;
2568 + u8 *buf = (u8 *) bh->buf;
2569 + u32 sd, sdinfo;
2570 + int valid;
2571 +
2572 + /*
2573 + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
2574 + *
2575 + * If a REQUEST SENSE command is received from an initiator
2576 + * with a pending unit attention condition (before the target
2577 + * generates the contingent allegiance condition), then the
2578 + * target shall either:
2579 + * a) report any pending sense data and preserve the unit
2580 + * attention condition on the logical unit, or,
2581 + * b) report the unit attention condition, may discard any
2582 + * pending sense data, and clear the unit attention
2583 + * condition on the logical unit for that initiator.
2584 + *
2585 + * FSG normally uses option a); enable this code to use option b).
2586 + */
2587 +#if 0
2588 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
2589 + curlun->sense_data = curlun->unit_attention_data;
2590 + curlun->unit_attention_data = SS_NO_SENSE;
2591 + }
2592 +#endif
2593 +
2594 + if (!curlun) { // Unsupported LUNs are okay
2595 + fsg->bad_lun_okay = 1;
2596 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2597 + sdinfo = 0;
2598 + valid = 0;
2599 + } else {
2600 + sd = curlun->sense_data;
2601 + sdinfo = curlun->sense_data_info;
2602 + valid = curlun->info_valid << 7;
2603 + curlun->sense_data = SS_NO_SENSE;
2604 + curlun->sense_data_info = 0;
2605 + curlun->info_valid = 0;
2606 + }
2607 +
2608 + memset(buf, 0, 18);
2609 + buf[0] = valid | 0x70; // Valid, current error
2610 + buf[2] = SK(sd);
2611 + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
2612 + buf[7] = 18 - 8; // Additional sense length
2613 + buf[12] = ASC(sd);
2614 + buf[13] = ASCQ(sd);
2615 + return 18;
2616 +}
2617 +
2618 +
2619 +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2620 +{
2621 + struct fsg_lun *curlun = fsg->curlun;
2622 + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2623 + int pmi = fsg->cmnd[8];
2624 + u8 *buf = (u8 *) bh->buf;
2625 +
2626 + /* Check the PMI and LBA fields */
2627 + if (pmi > 1 || (pmi == 0 && lba != 0)) {
2628 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2629 + return -EINVAL;
2630 + }
2631 +
2632 + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
2633 + /* Max logical block */
2634 + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
2635 + return 8;
2636 +}
2637 +
2638 +
2639 +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2640 +{
2641 + struct fsg_lun *curlun = fsg->curlun;
2642 + int msf = fsg->cmnd[1] & 0x02;
2643 + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2644 + u8 *buf = (u8 *) bh->buf;
2645 +
2646 + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
2647 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2648 + return -EINVAL;
2649 + }
2650 + if (lba >= curlun->num_sectors) {
2651 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2652 + return -EINVAL;
2653 + }
2654 +
2655 + memset(buf, 0, 8);
2656 + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
2657 + store_cdrom_address(&buf[4], msf, lba);
2658 + return 8;
2659 +}
2660 +
2661 +
2662 +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2663 +{
2664 + struct fsg_lun *curlun = fsg->curlun;
2665 + int msf = fsg->cmnd[1] & 0x02;
2666 + int start_track = fsg->cmnd[6];
2667 + u8 *buf = (u8 *) bh->buf;
2668 +
2669 + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
2670 + start_track > 1) {
2671 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2672 + return -EINVAL;
2673 + }
2674 +
2675 + memset(buf, 0, 20);
2676 + buf[1] = (20-2); /* TOC data length */
2677 + buf[2] = 1; /* First track number */
2678 + buf[3] = 1; /* Last track number */
2679 + buf[5] = 0x16; /* Data track, copying allowed */
2680 + buf[6] = 0x01; /* Only track is number 1 */
2681 + store_cdrom_address(&buf[8], msf, 0);
2682 +
2683 + buf[13] = 0x16; /* Lead-out track is data */
2684 + buf[14] = 0xAA; /* Lead-out track number */
2685 + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
2686 + return 20;
2687 +}
2688 +
2689 +
2690 +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2691 +{
2692 + struct fsg_lun *curlun = fsg->curlun;
2693 + int mscmnd = fsg->cmnd[0];
2694 + u8 *buf = (u8 *) bh->buf;
2695 + u8 *buf0 = buf;
2696 + int pc, page_code;
2697 + int changeable_values, all_pages;
2698 + int valid_page = 0;
2699 + int len, limit;
2700 +
2701 + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
2702 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2703 + return -EINVAL;
2704 + }
2705 + pc = fsg->cmnd[2] >> 6;
2706 + page_code = fsg->cmnd[2] & 0x3f;
2707 + if (pc == 3) {
2708 + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
2709 + return -EINVAL;
2710 + }
2711 + changeable_values = (pc == 1);
2712 + all_pages = (page_code == 0x3f);
2713 +
2714 + /* Write the mode parameter header. Fixed values are: default
2715 + * medium type, no cache control (DPOFUA), and no block descriptors.
2716 + * The only variable value is the WriteProtect bit. We will fill in
2717 + * the mode data length later. */
2718 + memset(buf, 0, 8);
2719 + if (mscmnd == MODE_SENSE) {
2720 + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
2721 + buf += 4;
2722 + limit = 255;
2723 + } else { // MODE_SENSE_10
2724 + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
2725 + buf += 8;
2726 + limit = 65535; // Should really be mod_data.buflen
2727 + }
2728 +
2729 + /* No block descriptors */
2730 +
2731 + /* The mode pages, in numerical order. The only page we support
2732 + * is the Caching page. */
2733 + if (page_code == 0x08 || all_pages) {
2734 + valid_page = 1;
2735 + buf[0] = 0x08; // Page code
2736 + buf[1] = 10; // Page length
2737 + memset(buf+2, 0, 10); // None of the fields are changeable
2738 +
2739 + if (!changeable_values) {
2740 + buf[2] = 0x04; // Write cache enable,
2741 + // Read cache not disabled
2742 + // No cache retention priorities
2743 + put_unaligned_be16(0xffff, &buf[4]);
2744 + /* Don't disable prefetch */
2745 + /* Minimum prefetch = 0 */
2746 + put_unaligned_be16(0xffff, &buf[8]);
2747 + /* Maximum prefetch */
2748 + put_unaligned_be16(0xffff, &buf[10]);
2749 + /* Maximum prefetch ceiling */
2750 + }
2751 + buf += 12;
2752 + }
2753 +
2754 + /* Check that a valid page was requested and the mode data length
2755 + * isn't too long. */
2756 + len = buf - buf0;
2757 + if (!valid_page || len > limit) {
2758 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2759 + return -EINVAL;
2760 + }
2761 +
2762 + /* Store the mode data length */
2763 + if (mscmnd == MODE_SENSE)
2764 + buf0[0] = len - 1;
2765 + else
2766 + put_unaligned_be16(len - 2, buf0);
2767 + return len;
2768 +}
2769 +
2770 +
2771 +static int do_start_stop(struct fsg_dev *fsg)
2772 +{
2773 + struct fsg_lun *curlun = fsg->curlun;
2774 + int loej, start;
2775 +
2776 + if (!mod_data.removable) {
2777 + curlun->sense_data = SS_INVALID_COMMAND;
2778 + return -EINVAL;
2779 + }
2780 +
2781 + // int immed = fsg->cmnd[1] & 0x01;
2782 + loej = fsg->cmnd[4] & 0x02;
2783 + start = fsg->cmnd[4] & 0x01;
2784 +
2785 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
2786 + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
2787 + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
2788 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2789 + return -EINVAL;
2790 + }
2791 +
2792 + if (!start) {
2793 +
2794 + /* Are we allowed to unload the media? */
2795 + if (curlun->prevent_medium_removal) {
2796 + LDBG(curlun, "unload attempt prevented\n");
2797 + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
2798 + return -EINVAL;
2799 + }
2800 + if (loej) { // Simulate an unload/eject
2801 + up_read(&fsg->filesem);
2802 + down_write(&fsg->filesem);
2803 + fsg_lun_close(curlun);
2804 + up_write(&fsg->filesem);
2805 + down_read(&fsg->filesem);
2806 + }
2807 + } else {
2808 +
2809 + /* Our emulation doesn't support mounting; the medium is
2810 + * available for use as soon as it is loaded. */
2811 + if (!fsg_lun_is_open(curlun)) {
2812 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2813 + return -EINVAL;
2814 + }
2815 + }
2816 +#endif
2817 + return 0;
2818 +}
2819 +
2820 +
2821 +static int do_prevent_allow(struct fsg_dev *fsg)
2822 +{
2823 + struct fsg_lun *curlun = fsg->curlun;
2824 + int prevent;
2825 +
2826 + if (!mod_data.removable) {
2827 + curlun->sense_data = SS_INVALID_COMMAND;
2828 + return -EINVAL;
2829 + }
2830 +
2831 + prevent = fsg->cmnd[4] & 0x01;
2832 + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
2833 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2834 + return -EINVAL;
2835 + }
2836 +
2837 + if (curlun->prevent_medium_removal && !prevent)
2838 + fsg_lun_fsync_sub(curlun);
2839 + curlun->prevent_medium_removal = prevent;
2840 + return 0;
2841 +}
2842 +
2843 +
2844 +static int do_read_format_capacities(struct fsg_dev *fsg,
2845 + struct fsg_buffhd *bh)
2846 +{
2847 + struct fsg_lun *curlun = fsg->curlun;
2848 + u8 *buf = (u8 *) bh->buf;
2849 +
2850 + buf[0] = buf[1] = buf[2] = 0;
2851 + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
2852 + buf += 4;
2853 +
2854 + put_unaligned_be32(curlun->num_sectors, &buf[0]);
2855 + /* Number of blocks */
2856 + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
2857 + buf[4] = 0x02; /* Current capacity */
2858 + return 12;
2859 +}
2860 +
2861 +
2862 +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2863 +{
2864 + struct fsg_lun *curlun = fsg->curlun;
2865 +
2866 + /* We don't support MODE SELECT */
2867 + curlun->sense_data = SS_INVALID_COMMAND;
2868 + return -EINVAL;
2869 +}
2870 +
2871 +
2872 +/*-------------------------------------------------------------------------*/
2873 +
2874 +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
2875 +{
2876 + int rc;
2877 +
2878 + rc = fsg_set_halt(fsg, fsg->bulk_in);
2879 + if (rc == -EAGAIN)
2880 + VDBG(fsg, "delayed bulk-in endpoint halt\n");
2881 + while (rc != 0) {
2882 + if (rc != -EAGAIN) {
2883 + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
2884 + rc = 0;
2885 + break;
2886 + }
2887 +
2888 + /* Wait for a short time and then try again */
2889 + if (msleep_interruptible(100) != 0)
2890 + return -EINTR;
2891 + rc = usb_ep_set_halt(fsg->bulk_in);
2892 + }
2893 + return rc;
2894 +}
2895 +
2896 +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
2897 +{
2898 + int rc;
2899 +
2900 + DBG(fsg, "bulk-in set wedge\n");
2901 + rc = usb_ep_set_wedge(fsg->bulk_in);
2902 + if (rc == -EAGAIN)
2903 + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
2904 + while (rc != 0) {
2905 + if (rc != -EAGAIN) {
2906 + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
2907 + rc = 0;
2908 + break;
2909 + }
2910 +
2911 + /* Wait for a short time and then try again */
2912 + if (msleep_interruptible(100) != 0)
2913 + return -EINTR;
2914 + rc = usb_ep_set_wedge(fsg->bulk_in);
2915 + }
2916 + return rc;
2917 +}
2918 +
2919 +static int throw_away_data(struct fsg_dev *fsg)
2920 +{
2921 + struct fsg_buffhd *bh;
2922 + u32 amount;
2923 + int rc;
2924 +
2925 + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
2926 + fsg->usb_amount_left > 0) {
2927 +
2928 + /* Throw away the data in a filled buffer */
2929 + if (bh->state == BUF_STATE_FULL) {
2930 + smp_rmb();
2931 + bh->state = BUF_STATE_EMPTY;
2932 + fsg->next_buffhd_to_drain = bh->next;
2933 +
2934 + /* A short packet or an error ends everything */
2935 + if (bh->outreq->actual < bh->bulk_out_intended_length ||
2936 + bh->outreq->status != 0) {
2937 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2938 + return -EINTR;
2939 + }
2940 + continue;
2941 + }
2942 +
2943 + /* Try to submit another request if we need one */
2944 + bh = fsg->next_buffhd_to_fill;
2945 + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
2946 + amount = min(fsg->usb_amount_left,
2947 + (u32) mod_data.buflen);
2948 +
2949 + /* Except at the end of the transfer, amount will be
2950 + * equal to the buffer size, which is divisible by
2951 + * the bulk-out maxpacket size.
2952 + */
2953 + set_bulk_out_req_length(fsg, bh, amount);
2954 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
2955 + &bh->outreq_busy, &bh->state);
2956 + fsg->next_buffhd_to_fill = bh->next;
2957 + fsg->usb_amount_left -= amount;
2958 + continue;
2959 + }
2960 +
2961 + /* Otherwise wait for something to happen */
2962 + rc = sleep_thread(fsg);
2963 + if (rc)
2964 + return rc;
2965 + }
2966 + return 0;
2967 +}
2968 +
2969 +
2970 +static int finish_reply(struct fsg_dev *fsg)
2971 +{
2972 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
2973 + int rc = 0;
2974 +
2975 + switch (fsg->data_dir) {
2976 + case DATA_DIR_NONE:
2977 + break; // Nothing to send
2978 +
2979 + /* If we don't know whether the host wants to read or write,
2980 + * this must be CB or CBI with an unknown command. We mustn't
2981 + * try to send or receive any data. So stall both bulk pipes
2982 + * if we can and wait for a reset. */
2983 + case DATA_DIR_UNKNOWN:
2984 + if (mod_data.can_stall) {
2985 + fsg_set_halt(fsg, fsg->bulk_out);
2986 + rc = halt_bulk_in_endpoint(fsg);
2987 + }
2988 + break;
2989 +
2990 + /* All but the last buffer of data must have already been sent */
2991 + case DATA_DIR_TO_HOST:
2992 + if (fsg->data_size == 0)
2993 + ; // Nothing to send
2994 +
2995 + /* If there's no residue, simply send the last buffer */
2996 + else if (fsg->residue == 0) {
2997 + bh->inreq->zero = 0;
2998 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
2999 + &bh->inreq_busy, &bh->state);
3000 + fsg->next_buffhd_to_fill = bh->next;
3001 + }
3002 +
3003 + /* There is a residue. For CB and CBI, simply mark the end
3004 + * of the data with a short packet. However, if we are
3005 + * allowed to stall, there was no data at all (residue ==
3006 + * data_size), and the command failed (invalid LUN or
3007 + * sense data is set), then halt the bulk-in endpoint
3008 + * instead. */
3009 + else if (!transport_is_bbb()) {
3010 + if (mod_data.can_stall &&
3011 + fsg->residue == fsg->data_size &&
3012 + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
3013 + bh->state = BUF_STATE_EMPTY;
3014 + rc = halt_bulk_in_endpoint(fsg);
3015 + } else {
3016 + bh->inreq->zero = 1;
3017 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3018 + &bh->inreq_busy, &bh->state);
3019 + fsg->next_buffhd_to_fill = bh->next;
3020 + }
3021 + }
3022 +
3023 + /*
3024 + * For Bulk-only, mark the end of the data with a short
3025 + * packet. If we are allowed to stall, halt the bulk-in
3026 + * endpoint. (Note: This violates the Bulk-Only Transport
3027 + * specification, which requires us to pad the data if we
3028 + * don't halt the endpoint. Presumably nobody will mind.)
3029 + */
3030 + else {
3031 + bh->inreq->zero = 1;
3032 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3033 + &bh->inreq_busy, &bh->state);
3034 + fsg->next_buffhd_to_fill = bh->next;
3035 + if (mod_data.can_stall)
3036 + rc = halt_bulk_in_endpoint(fsg);
3037 + }
3038 + break;
3039 +
3040 + /* We have processed all we want from the data the host has sent.
3041 + * There may still be outstanding bulk-out requests. */
3042 + case DATA_DIR_FROM_HOST:
3043 + if (fsg->residue == 0)
3044 + ; // Nothing to receive
3045 +
3046 + /* Did the host stop sending unexpectedly early? */
3047 + else if (fsg->short_packet_received) {
3048 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3049 + rc = -EINTR;
3050 + }
3051 +
3052 + /* We haven't processed all the incoming data. Even though
3053 + * we may be allowed to stall, doing so would cause a race.
3054 + * The controller may already have ACK'ed all the remaining
3055 + * bulk-out packets, in which case the host wouldn't see a
3056 + * STALL. Not realizing the endpoint was halted, it wouldn't
3057 + * clear the halt -- leading to problems later on. */
3058 +#if 0
3059 + else if (mod_data.can_stall) {
3060 + fsg_set_halt(fsg, fsg->bulk_out);
3061 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3062 + rc = -EINTR;
3063 + }
3064 +#endif
3065 +
3066 + /* We can't stall. Read in the excess data and throw it
3067 + * all away. */
3068 + else
3069 + rc = throw_away_data(fsg);
3070 + break;
3071 + }
3072 + return rc;
3073 +}
3074 +
3075 +
3076 +static int send_status(struct fsg_dev *fsg)
3077 +{
3078 + struct fsg_lun *curlun = fsg->curlun;
3079 + struct fsg_buffhd *bh;
3080 + int rc;
3081 + u8 status = US_BULK_STAT_OK;
3082 + u32 sd, sdinfo = 0;
3083 +
3084 + /* Wait for the next buffer to become available */
3085 + bh = fsg->next_buffhd_to_fill;
3086 + while (bh->state != BUF_STATE_EMPTY) {
3087 + rc = sleep_thread(fsg);
3088 + if (rc)
3089 + return rc;
3090 + }
3091 +
3092 + if (curlun) {
3093 + sd = curlun->sense_data;
3094 + sdinfo = curlun->sense_data_info;
3095 + } else if (fsg->bad_lun_okay)
3096 + sd = SS_NO_SENSE;
3097 + else
3098 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
3099 +
3100 + if (fsg->phase_error) {
3101 + DBG(fsg, "sending phase-error status\n");
3102 + status = US_BULK_STAT_PHASE;
3103 + sd = SS_INVALID_COMMAND;
3104 + } else if (sd != SS_NO_SENSE) {
3105 + DBG(fsg, "sending command-failure status\n");
3106 + status = US_BULK_STAT_FAIL;
3107 + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
3108 + " info x%x\n",
3109 + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
3110 + }
3111 +
3112 + if (transport_is_bbb()) {
3113 + struct bulk_cs_wrap *csw = bh->buf;
3114 +
3115 + /* Store and send the Bulk-only CSW */
3116 + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
3117 + csw->Tag = fsg->tag;
3118 + csw->Residue = cpu_to_le32(fsg->residue);
3119 + csw->Status = status;
3120 +
3121 + bh->inreq->length = US_BULK_CS_WRAP_LEN;
3122 + bh->inreq->zero = 0;
3123 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3124 + &bh->inreq_busy, &bh->state);
3125 +
3126 + } else if (mod_data.transport_type == USB_PR_CB) {
3127 +
3128 + /* Control-Bulk transport has no status phase! */
3129 + return 0;
3130 +
3131 + } else { // USB_PR_CBI
3132 + struct interrupt_data *buf = bh->buf;
3133 +
3134 + /* Store and send the Interrupt data. UFI sends the ASC
3135 + * and ASCQ bytes. Everything else sends a Type (which
3136 + * is always 0) and the status Value. */
3137 + if (mod_data.protocol_type == USB_SC_UFI) {
3138 + buf->bType = ASC(sd);
3139 + buf->bValue = ASCQ(sd);
3140 + } else {
3141 + buf->bType = 0;
3142 + buf->bValue = status;
3143 + }
3144 + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
3145 +
3146 + fsg->intr_buffhd = bh; // Point to the right buffhd
3147 + fsg->intreq->buf = bh->inreq->buf;
3148 + fsg->intreq->context = bh;
3149 + start_transfer(fsg, fsg->intr_in, fsg->intreq,
3150 + &fsg->intreq_busy, &bh->state);
3151 + }
3152 +
3153 + fsg->next_buffhd_to_fill = bh->next;
3154 + return 0;
3155 +}
3156 +
3157 +
3158 +/*-------------------------------------------------------------------------*/
3159 +
3160 +/* Check whether the command is properly formed and whether its data size
3161 + * and direction agree with the values we already have. */
3162 +static int check_command(struct fsg_dev *fsg, int cmnd_size,
3163 + enum data_direction data_dir, unsigned int mask,
3164 + int needs_medium, const char *name)
3165 +{
3166 + int i;
3167 + int lun = fsg->cmnd[1] >> 5;
3168 + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
3169 + char hdlen[20];
3170 + struct fsg_lun *curlun;
3171 +
3172 + /* Adjust the expected cmnd_size for protocol encapsulation padding.
3173 + * Transparent SCSI doesn't pad. */
3174 + if (protocol_is_scsi())
3175 + ;
3176 +
3177 + /* There's some disagreement as to whether RBC pads commands or not.
3178 + * We'll play it safe and accept either form. */
3179 + else if (mod_data.protocol_type == USB_SC_RBC) {
3180 + if (fsg->cmnd_size == 12)
3181 + cmnd_size = 12;
3182 +
3183 + /* All the other protocols pad to 12 bytes */
3184 + } else
3185 + cmnd_size = 12;
3186 +
3187 + hdlen[0] = 0;
3188 + if (fsg->data_dir != DATA_DIR_UNKNOWN)
3189 + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
3190 + fsg->data_size);
3191 + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
3192 + name, cmnd_size, dirletter[(int) data_dir],
3193 + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
3194 +
3195 + /* We can't reply at all until we know the correct data direction
3196 + * and size. */
3197 + if (fsg->data_size_from_cmnd == 0)
3198 + data_dir = DATA_DIR_NONE;
3199 + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
3200 + fsg->data_dir = data_dir;
3201 + fsg->data_size = fsg->data_size_from_cmnd;
3202 +
3203 + } else { // Bulk-only
3204 + if (fsg->data_size < fsg->data_size_from_cmnd) {
3205 +
3206 + /* Host data size < Device data size is a phase error.
3207 + * Carry out the command, but only transfer as much
3208 + * as we are allowed. */
3209 + fsg->data_size_from_cmnd = fsg->data_size;
3210 + fsg->phase_error = 1;
3211 + }
3212 + }
3213 + fsg->residue = fsg->usb_amount_left = fsg->data_size;
3214 +
3215 + /* Conflicting data directions is a phase error */
3216 + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
3217 + fsg->phase_error = 1;
3218 + return -EINVAL;
3219 + }
3220 +
3221 + /* Verify the length of the command itself */
3222 + if (cmnd_size != fsg->cmnd_size) {
3223 +
3224 + /* Special case workaround: There are plenty of buggy SCSI
3225 + * implementations. Many have issues with cbw->Length
3226 + * field passing a wrong command size. For those cases we
3227 + * always try to work around the problem by using the length
3228 + * sent by the host side provided it is at least as large
3229 + * as the correct command length.
3230 + * Examples of such cases would be MS-Windows, which issues
3231 + * REQUEST SENSE with cbw->Length == 12 where it should
3232 + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
3233 + * REQUEST SENSE with cbw->Length == 10 where it should
3234 + * be 6 as well.
3235 + */
3236 + if (cmnd_size <= fsg->cmnd_size) {
3237 + DBG(fsg, "%s is buggy! Expected length %d "
3238 + "but we got %d\n", name,
3239 + cmnd_size, fsg->cmnd_size);
3240 + cmnd_size = fsg->cmnd_size;
3241 + } else {
3242 + fsg->phase_error = 1;
3243 + return -EINVAL;
3244 + }
3245 + }
3246 +
3247 + /* Check that the LUN values are consistent */
3248 + if (transport_is_bbb()) {
3249 + if (fsg->lun != lun)
3250 + DBG(fsg, "using LUN %d from CBW, "
3251 + "not LUN %d from CDB\n",
3252 + fsg->lun, lun);
3253 + }
3254 +
3255 + /* Check the LUN */
3256 + curlun = fsg->curlun;
3257 + if (curlun) {
3258 + if (fsg->cmnd[0] != REQUEST_SENSE) {
3259 + curlun->sense_data = SS_NO_SENSE;
3260 + curlun->sense_data_info = 0;
3261 + curlun->info_valid = 0;
3262 + }
3263 + } else {
3264 + fsg->bad_lun_okay = 0;
3265 +
3266 + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
3267 + * to use unsupported LUNs; all others may not. */
3268 + if (fsg->cmnd[0] != INQUIRY &&
3269 + fsg->cmnd[0] != REQUEST_SENSE) {
3270 + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
3271 + return -EINVAL;
3272 + }
3273 + }
3274 +
3275 + /* If a unit attention condition exists, only INQUIRY and
3276 + * REQUEST SENSE commands are allowed; anything else must fail. */
3277 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
3278 + fsg->cmnd[0] != INQUIRY &&
3279 + fsg->cmnd[0] != REQUEST_SENSE) {
3280 + curlun->sense_data = curlun->unit_attention_data;
3281 + curlun->unit_attention_data = SS_NO_SENSE;
3282 + return -EINVAL;
3283 + }
3284 +
3285 + /* Check that only command bytes listed in the mask are non-zero */
3286 + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
3287 + for (i = 1; i < cmnd_size; ++i) {
3288 + if (fsg->cmnd[i] && !(mask & (1 << i))) {
3289 + if (curlun)
3290 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3291 + return -EINVAL;
3292 + }
3293 + }
3294 +
3295 + /* If the medium isn't mounted and the command needs to access
3296 + * it, return an error. */
3297 + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
3298 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
3299 + return -EINVAL;
3300 + }
3301 +
3302 + return 0;
3303 +}
3304 +
3305 +/* wrapper of check_command for data size in blocks handling */
3306 +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
3307 + enum data_direction data_dir, unsigned int mask,
3308 + int needs_medium, const char *name)
3309 +{
3310 + if (fsg->curlun)
3311 + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
3312 + return check_command(fsg, cmnd_size, data_dir,
3313 + mask, needs_medium, name);
3314 +}
3315 +
3316 +static int do_scsi_command(struct fsg_dev *fsg)
3317 +{
3318 + struct fsg_buffhd *bh;
3319 + int rc;
3320 + int reply = -EINVAL;
3321 + int i;
3322 + static char unknown[16];
3323 +
3324 + dump_cdb(fsg);
3325 +
3326 + /* Wait for the next buffer to become available for data or status */
3327 + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
3328 + while (bh->state != BUF_STATE_EMPTY) {
3329 + rc = sleep_thread(fsg);
3330 + if (rc)
3331 + return rc;
3332 + }
3333 + fsg->phase_error = 0;
3334 + fsg->short_packet_received = 0;
3335 +
3336 + down_read(&fsg->filesem); // We're using the backing file
3337 + switch (fsg->cmnd[0]) {
3338 +
3339 + case INQUIRY:
3340 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3341 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3342 + (1<<4), 0,
3343 + "INQUIRY")) == 0)
3344 + reply = do_inquiry(fsg, bh);
3345 + break;
3346 +
3347 + case MODE_SELECT:
3348 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3349 + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
3350 + (1<<1) | (1<<4), 0,
3351 + "MODE SELECT(6)")) == 0)
3352 + reply = do_mode_select(fsg, bh);
3353 + break;
3354 +
3355 + case MODE_SELECT_10:
3356 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3357 + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
3358 + (1<<1) | (3<<7), 0,
3359 + "MODE SELECT(10)")) == 0)
3360 + reply = do_mode_select(fsg, bh);
3361 + break;
3362 +
3363 + case MODE_SENSE:
3364 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3365 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3366 + (1<<1) | (1<<2) | (1<<4), 0,
3367 + "MODE SENSE(6)")) == 0)
3368 + reply = do_mode_sense(fsg, bh);
3369 + break;
3370 +
3371 + case MODE_SENSE_10:
3372 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3373 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3374 + (1<<1) | (1<<2) | (3<<7), 0,
3375 + "MODE SENSE(10)")) == 0)
3376 + reply = do_mode_sense(fsg, bh);
3377 + break;
3378 +
3379 + case ALLOW_MEDIUM_REMOVAL:
3380 + fsg->data_size_from_cmnd = 0;
3381 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
3382 + (1<<4), 0,
3383 + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
3384 + reply = do_prevent_allow(fsg);
3385 + break;
3386 +
3387 + case READ_6:
3388 + i = fsg->cmnd[4];
3389 + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
3390 + if ((reply = check_command_size_in_blocks(fsg, 6,
3391 + DATA_DIR_TO_HOST,
3392 + (7<<1) | (1<<4), 1,
3393 + "READ(6)")) == 0)
3394 + reply = do_read(fsg);
3395 + break;
3396 +
3397 + case READ_10:
3398 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3399 + if ((reply = check_command_size_in_blocks(fsg, 10,
3400 + DATA_DIR_TO_HOST,
3401 + (1<<1) | (0xf<<2) | (3<<7), 1,
3402 + "READ(10)")) == 0)
3403 + reply = do_read(fsg);
3404 + break;
3405 +
3406 + case READ_12:
3407 + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
3408 + if ((reply = check_command_size_in_blocks(fsg, 12,
3409 + DATA_DIR_TO_HOST,
3410 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
3411 + "READ(12)")) == 0)
3412 + reply = do_read(fsg);
3413 + break;
3414 +
3415 + case READ_CAPACITY:
3416 + fsg->data_size_from_cmnd = 8;
3417 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3418 + (0xf<<2) | (1<<8), 1,
3419 + "READ CAPACITY")) == 0)
3420 + reply = do_read_capacity(fsg, bh);
3421 + break;
3422 +
3423 + case READ_HEADER:
3424 + if (!mod_data.cdrom)
3425 + goto unknown_cmnd;
3426 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3427 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3428 + (3<<7) | (0x1f<<1), 1,
3429 + "READ HEADER")) == 0)
3430 + reply = do_read_header(fsg, bh);
3431 + break;
3432 +
3433 + case READ_TOC:
3434 + if (!mod_data.cdrom)
3435 + goto unknown_cmnd;
3436 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3437 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3438 + (7<<6) | (1<<1), 1,
3439 + "READ TOC")) == 0)
3440 + reply = do_read_toc(fsg, bh);
3441 + break;
3442 +
3443 + case READ_FORMAT_CAPACITIES:
3444 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3445 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3446 + (3<<7), 1,
3447 + "READ FORMAT CAPACITIES")) == 0)
3448 + reply = do_read_format_capacities(fsg, bh);
3449 + break;
3450 +
3451 + case REQUEST_SENSE:
3452 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3453 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3454 + (1<<4), 0,
3455 + "REQUEST SENSE")) == 0)
3456 + reply = do_request_sense(fsg, bh);
3457 + break;
3458 +
3459 + case START_STOP:
3460 + fsg->data_size_from_cmnd = 0;
3461 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
3462 + (1<<1) | (1<<4), 0,
3463 + "START-STOP UNIT")) == 0)
3464 + reply = do_start_stop(fsg);
3465 + break;
3466 +
3467 + case SYNCHRONIZE_CACHE:
3468 + fsg->data_size_from_cmnd = 0;
3469 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3470 + (0xf<<2) | (3<<7), 1,
3471 + "SYNCHRONIZE CACHE")) == 0)
3472 + reply = do_synchronize_cache(fsg);
3473 + break;
3474 +
3475 + case TEST_UNIT_READY:
3476 + fsg->data_size_from_cmnd = 0;
3477 + reply = check_command(fsg, 6, DATA_DIR_NONE,
3478 + 0, 1,
3479 + "TEST UNIT READY");
3480 + break;
3481 +
3482 + /* Although optional, this command is used by MS-Windows. We
3483 + * support a minimal version: BytChk must be 0. */
3484 + case VERIFY:
3485 + fsg->data_size_from_cmnd = 0;
3486 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3487 + (1<<1) | (0xf<<2) | (3<<7), 1,
3488 + "VERIFY")) == 0)
3489 + reply = do_verify(fsg);
3490 + break;
3491 +
3492 + case WRITE_6:
3493 + i = fsg->cmnd[4];
3494 + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
3495 + if ((reply = check_command_size_in_blocks(fsg, 6,
3496 + DATA_DIR_FROM_HOST,
3497 + (7<<1) | (1<<4), 1,
3498 + "WRITE(6)")) == 0)
3499 + reply = do_write(fsg);
3500 + break;
3501 +
3502 + case WRITE_10:
3503 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3504 + if ((reply = check_command_size_in_blocks(fsg, 10,
3505 + DATA_DIR_FROM_HOST,
3506 + (1<<1) | (0xf<<2) | (3<<7), 1,
3507 + "WRITE(10)")) == 0)
3508 + reply = do_write(fsg);
3509 + break;
3510 +
3511 + case WRITE_12:
3512 + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
3513 + if ((reply = check_command_size_in_blocks(fsg, 12,
3514 + DATA_DIR_FROM_HOST,
3515 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
3516 + "WRITE(12)")) == 0)
3517 + reply = do_write(fsg);
3518 + break;
3519 +
3520 + /* Some mandatory commands that we recognize but don't implement.
3521 + * They don't mean much in this setting. It's left as an exercise
3522 + * for anyone interested to implement RESERVE and RELEASE in terms
3523 + * of Posix locks. */
3524 + case FORMAT_UNIT:
3525 + case RELEASE:
3526 + case RESERVE:
3527 + case SEND_DIAGNOSTIC:
3528 + // Fall through
3529 +
3530 + default:
3531 + unknown_cmnd:
3532 + fsg->data_size_from_cmnd = 0;
3533 + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
3534 + if ((reply = check_command(fsg, fsg->cmnd_size,
3535 + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
3536 + fsg->curlun->sense_data = SS_INVALID_COMMAND;
3537 + reply = -EINVAL;
3538 + }
3539 + break;
3540 + }
3541 + up_read(&fsg->filesem);
3542 +
3543 + if (reply == -EINTR || signal_pending(current))
3544 + return -EINTR;
3545 +
3546 + /* Set up the single reply buffer for finish_reply() */
3547 + if (reply == -EINVAL)
3548 + reply = 0; // Error reply length
3549 + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
3550 + reply = min((u32) reply, fsg->data_size_from_cmnd);
3551 + bh->inreq->length = reply;
3552 + bh->state = BUF_STATE_FULL;
3553 + fsg->residue -= reply;
3554 + } // Otherwise it's already set
3555 +
3556 + return 0;
3557 +}
3558 +
3559 +
3560 +/*-------------------------------------------------------------------------*/
3561 +
3562 +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3563 +{
3564 + struct usb_request *req = bh->outreq;
3565 + struct bulk_cb_wrap *cbw = req->buf;
3566 +
3567 + /* Was this a real packet? Should it be ignored? */
3568 + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
3569 + return -EINVAL;
3570 +
3571 + /* Is the CBW valid? */
3572 + if (req->actual != US_BULK_CB_WRAP_LEN ||
3573 + cbw->Signature != cpu_to_le32(
3574 + US_BULK_CB_SIGN)) {
3575 + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
3576 + req->actual,
3577 + le32_to_cpu(cbw->Signature));
3578 +
3579 + /* The Bulk-only spec says we MUST stall the IN endpoint
3580 + * (6.6.1), so it's unavoidable. It also says we must
3581 + * retain this state until the next reset, but there's
3582 + * no way to tell the controller driver it should ignore
3583 + * Clear-Feature(HALT) requests.
3584 + *
3585 + * We aren't required to halt the OUT endpoint; instead
3586 + * we can simply accept and discard any data received
3587 + * until the next reset. */
3588 + wedge_bulk_in_endpoint(fsg);
3589 + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
3590 + return -EINVAL;
3591 + }
3592 +
3593 + /* Is the CBW meaningful? */
3594 + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
3595 + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
3596 + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
3597 + "cmdlen %u\n",
3598 + cbw->Lun, cbw->Flags, cbw->Length);
3599 +
3600 + /* We can do anything we want here, so let's stall the
3601 + * bulk pipes if we are allowed to. */
3602 + if (mod_data.can_stall) {
3603 + fsg_set_halt(fsg, fsg->bulk_out);
3604 + halt_bulk_in_endpoint(fsg);
3605 + }
3606 + return -EINVAL;
3607 + }
3608 +
3609 + /* Save the command for later */
3610 + fsg->cmnd_size = cbw->Length;
3611 + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
3612 + if (cbw->Flags & US_BULK_FLAG_IN)
3613 + fsg->data_dir = DATA_DIR_TO_HOST;
3614 + else
3615 + fsg->data_dir = DATA_DIR_FROM_HOST;
3616 + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
3617 + if (fsg->data_size == 0)
3618 + fsg->data_dir = DATA_DIR_NONE;
3619 + fsg->lun = cbw->Lun;
3620 + fsg->tag = cbw->Tag;
3621 + return 0;
3622 +}
3623 +
3624 +
3625 +static int get_next_command(struct fsg_dev *fsg)
3626 +{
3627 + struct fsg_buffhd *bh;
3628 + int rc = 0;
3629 +
3630 + if (transport_is_bbb()) {
3631 +
3632 + /* Wait for the next buffer to become available */
3633 + bh = fsg->next_buffhd_to_fill;
3634 + while (bh->state != BUF_STATE_EMPTY) {
3635 + rc = sleep_thread(fsg);
3636 + if (rc)
3637 + return rc;
3638 + }
3639 +
3640 + /* Queue a request to read a Bulk-only CBW */
3641 + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
3642 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
3643 + &bh->outreq_busy, &bh->state);
3644 +
3645 + /* We will drain the buffer in software, which means we
3646 + * can reuse it for the next filling. No need to advance
3647 + * next_buffhd_to_fill. */
3648 +
3649 + /* Wait for the CBW to arrive */
3650 + while (bh->state != BUF_STATE_FULL) {
3651 + rc = sleep_thread(fsg);
3652 + if (rc)
3653 + return rc;
3654 + }
3655 + smp_rmb();
3656 + rc = received_cbw(fsg, bh);
3657 + bh->state = BUF_STATE_EMPTY;
3658 +
3659 + } else { // USB_PR_CB or USB_PR_CBI
3660 +
3661 + /* Wait for the next command to arrive */
3662 + while (fsg->cbbuf_cmnd_size == 0) {
3663 + rc = sleep_thread(fsg);
3664 + if (rc)
3665 + return rc;
3666 + }
3667 +
3668 + /* Is the previous status interrupt request still busy?
3669 + * The host is allowed to skip reading the status,
3670 + * so we must cancel it. */
3671 + if (fsg->intreq_busy)
3672 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
3673 +
3674 + /* Copy the command and mark the buffer empty */
3675 + fsg->data_dir = DATA_DIR_UNKNOWN;
3676 + spin_lock_irq(&fsg->lock);
3677 + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
3678 + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
3679 + fsg->cbbuf_cmnd_size = 0;
3680 + spin_unlock_irq(&fsg->lock);
3681 +
3682 + /* Use LUN from the command */
3683 + fsg->lun = fsg->cmnd[1] >> 5;
3684 + }
3685 +
3686 + /* Update current lun */
3687 + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
3688 + fsg->curlun = &fsg->luns[fsg->lun];
3689 + else
3690 + fsg->curlun = NULL;
3691 +
3692 + return rc;
3693 +}
3694 +
3695 +
3696 +/*-------------------------------------------------------------------------*/
3697 +
3698 +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
3699 + const struct usb_endpoint_descriptor *d)
3700 +{
3701 + int rc;
3702 +
3703 + ep->driver_data = fsg;
3704 + ep->desc = d;
3705 + rc = usb_ep_enable(ep);
3706 + if (rc)
3707 + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
3708 + return rc;
3709 +}
3710 +
3711 +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
3712 + struct usb_request **preq)
3713 +{
3714 + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
3715 + if (*preq)
3716 + return 0;
3717 + ERROR(fsg, "can't allocate request for %s\n", ep->name);
3718 + return -ENOMEM;
3719 +}
3720 +
3721 +/*
3722 + * Reset interface setting and re-init endpoint state (toggle etc).
3723 + * Call with altsetting < 0 to disable the interface. The only other
3724 + * available altsetting is 0, which enables the interface.
3725 + */
3726 +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
3727 +{
3728 + int rc = 0;
3729 + int i;
3730 + const struct usb_endpoint_descriptor *d;
3731 +
3732 + if (fsg->running)
3733 + DBG(fsg, "reset interface\n");
3734 +
3735 +reset:
3736 + /* Deallocate the requests */
3737 + for (i = 0; i < fsg_num_buffers; ++i) {
3738 + struct fsg_buffhd *bh = &fsg->buffhds[i];
3739 +
3740 + if (bh->inreq) {
3741 + usb_ep_free_request(fsg->bulk_in, bh->inreq);
3742 + bh->inreq = NULL;
3743 + }
3744 + if (bh->outreq) {
3745 + usb_ep_free_request(fsg->bulk_out, bh->outreq);
3746 + bh->outreq = NULL;
3747 + }
3748 + }
3749 + if (fsg->intreq) {
3750 + usb_ep_free_request(fsg->intr_in, fsg->intreq);
3751 + fsg->intreq = NULL;
3752 + }
3753 +
3754 + /* Disable the endpoints */
3755 + if (fsg->bulk_in_enabled) {
3756 + usb_ep_disable(fsg->bulk_in);
3757 + fsg->bulk_in_enabled = 0;
3758 + }
3759 + if (fsg->bulk_out_enabled) {
3760 + usb_ep_disable(fsg->bulk_out);
3761 + fsg->bulk_out_enabled = 0;
3762 + }
3763 + if (fsg->intr_in_enabled) {
3764 + usb_ep_disable(fsg->intr_in);
3765 + fsg->intr_in_enabled = 0;
3766 + }
3767 +
3768 + fsg->running = 0;
3769 + if (altsetting < 0 || rc != 0)
3770 + return rc;
3771 +
3772 + DBG(fsg, "set interface %d\n", altsetting);
3773 +
3774 + /* Enable the endpoints */
3775 + d = fsg_ep_desc(fsg->gadget,
3776 + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
3777 + &fsg_ss_bulk_in_desc);
3778 + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
3779 + goto reset;
3780 + fsg->bulk_in_enabled = 1;
3781 +
3782 + d = fsg_ep_desc(fsg->gadget,
3783 + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
3784 + &fsg_ss_bulk_out_desc);
3785 + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
3786 + goto reset;
3787 + fsg->bulk_out_enabled = 1;
3788 + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
3789 + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
3790 +
3791 + if (transport_is_cbi()) {
3792 + d = fsg_ep_desc(fsg->gadget,
3793 + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
3794 + &fsg_ss_intr_in_desc);
3795 + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
3796 + goto reset;
3797 + fsg->intr_in_enabled = 1;
3798 + }
3799 +
3800 + /* Allocate the requests */
3801 + for (i = 0; i < fsg_num_buffers; ++i) {
3802 + struct fsg_buffhd *bh = &fsg->buffhds[i];
3803 +
3804 + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
3805 + goto reset;
3806 + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
3807 + goto reset;
3808 + bh->inreq->buf = bh->outreq->buf = bh->buf;
3809 + bh->inreq->context = bh->outreq->context = bh;
3810 + bh->inreq->complete = bulk_in_complete;
3811 + bh->outreq->complete = bulk_out_complete;
3812 + }
3813 + if (transport_is_cbi()) {
3814 + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
3815 + goto reset;
3816 + fsg->intreq->complete = intr_in_complete;
3817 + }
3818 +
3819 + fsg->running = 1;
3820 + for (i = 0; i < fsg->nluns; ++i)
3821 + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
3822 + return rc;
3823 +}
3824 +
3825 +
3826 +/*
3827 + * Change our operational configuration. This code must agree with the code
3828 + * that returns config descriptors, and with interface altsetting code.
3829 + *
3830 + * It's also responsible for power management interactions. Some
3831 + * configurations might not work with our current power sources.
3832 + * For now we just assume the gadget is always self-powered.
3833 + */
3834 +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
3835 +{
3836 + int rc = 0;
3837 +
3838 + /* Disable the single interface */
3839 + if (fsg->config != 0) {
3840 + DBG(fsg, "reset config\n");
3841 + fsg->config = 0;
3842 + rc = do_set_interface(fsg, -1);
3843 + }
3844 +
3845 + /* Enable the interface */
3846 + if (new_config != 0) {
3847 + fsg->config = new_config;
3848 + if ((rc = do_set_interface(fsg, 0)) != 0)
3849 + fsg->config = 0; // Reset on errors
3850 + else
3851 + INFO(fsg, "%s config #%d\n",
3852 + usb_speed_string(fsg->gadget->speed),
3853 + fsg->config);
3854 + }
3855 + return rc;
3856 +}
3857 +
3858 +
3859 +/*-------------------------------------------------------------------------*/
3860 +
3861 +static void handle_exception(struct fsg_dev *fsg)
3862 +{
3863 + siginfo_t info;
3864 + int sig;
3865 + int i;
3866 + int num_active;
3867 + struct fsg_buffhd *bh;
3868 + enum fsg_state old_state;
3869 + u8 new_config;
3870 + struct fsg_lun *curlun;
3871 + unsigned int exception_req_tag;
3872 + int rc;
3873 +
3874 + /* Clear the existing signals. Anything but SIGUSR1 is converted
3875 + * into a high-priority EXIT exception. */
3876 + for (;;) {
3877 + sig = dequeue_signal_lock(current, &current->blocked, &info);
3878 + if (!sig)
3879 + break;
3880 + if (sig != SIGUSR1) {
3881 + if (fsg->state < FSG_STATE_EXIT)
3882 + DBG(fsg, "Main thread exiting on signal\n");
3883 + raise_exception(fsg, FSG_STATE_EXIT);
3884 + }
3885 + }
3886 +
3887 + /* Cancel all the pending transfers */
3888 + if (fsg->intreq_busy)
3889 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
3890 + for (i = 0; i < fsg_num_buffers; ++i) {
3891 + bh = &fsg->buffhds[i];
3892 + if (bh->inreq_busy)
3893 + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
3894 + if (bh->outreq_busy)
3895 + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
3896 + }
3897 +
3898 + /* Wait until everything is idle */
3899 + for (;;) {
3900 + num_active = fsg->intreq_busy;
3901 + for (i = 0; i < fsg_num_buffers; ++i) {
3902 + bh = &fsg->buffhds[i];
3903 + num_active += bh->inreq_busy + bh->outreq_busy;
3904 + }
3905 + if (num_active == 0)
3906 + break;
3907 + if (sleep_thread(fsg))
3908 + return;
3909 + }
3910 +
3911 + /* Clear out the controller's fifos */
3912 + if (fsg->bulk_in_enabled)
3913 + usb_ep_fifo_flush(fsg->bulk_in);
3914 + if (fsg->bulk_out_enabled)
3915 + usb_ep_fifo_flush(fsg->bulk_out);
3916 + if (fsg->intr_in_enabled)
3917 + usb_ep_fifo_flush(fsg->intr_in);
3918 +
3919 + /* Reset the I/O buffer states and pointers, the SCSI
3920 + * state, and the exception. Then invoke the handler. */
3921 + spin_lock_irq(&fsg->lock);
3922 +
3923 + for (i = 0; i < fsg_num_buffers; ++i) {
3924 + bh = &fsg->buffhds[i];
3925 + bh->state = BUF_STATE_EMPTY;
3926 + }
3927 + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
3928 + &fsg->buffhds[0];
3929 +
3930 + exception_req_tag = fsg->exception_req_tag;
3931 + new_config = fsg->new_config;
3932 + old_state = fsg->state;
3933 +
3934 + if (old_state == FSG_STATE_ABORT_BULK_OUT)
3935 + fsg->state = FSG_STATE_STATUS_PHASE;
3936 + else {
3937 + for (i = 0; i < fsg->nluns; ++i) {
3938 + curlun = &fsg->luns[i];
3939 + curlun->prevent_medium_removal = 0;
3940 + curlun->sense_data = curlun->unit_attention_data =
3941 + SS_NO_SENSE;
3942 + curlun->sense_data_info = 0;
3943 + curlun->info_valid = 0;
3944 + }
3945 + fsg->state = FSG_STATE_IDLE;
3946 + }
3947 + spin_unlock_irq(&fsg->lock);
3948 +
3949 + /* Carry out any extra actions required for the exception */
3950 + switch (old_state) {
3951 + default:
3952 + break;
3953 +
3954 + case FSG_STATE_ABORT_BULK_OUT:
3955 + send_status(fsg);
3956 + spin_lock_irq(&fsg->lock);
3957 + if (fsg->state == FSG_STATE_STATUS_PHASE)
3958 + fsg->state = FSG_STATE_IDLE;
3959 + spin_unlock_irq(&fsg->lock);
3960 + break;
3961 +
3962 + case FSG_STATE_RESET:
3963 + /* In case we were forced against our will to halt a
3964 + * bulk endpoint, clear the halt now. (The SuperH UDC
3965 + * requires this.) */
3966 + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
3967 + usb_ep_clear_halt(fsg->bulk_in);
3968 +
3969 + if (transport_is_bbb()) {
3970 + if (fsg->ep0_req_tag == exception_req_tag)
3971 + ep0_queue(fsg); // Complete the status stage
3972 +
3973 + } else if (transport_is_cbi())
3974 + send_status(fsg); // Status by interrupt pipe
3975 +
3976 + /* Technically this should go here, but it would only be
3977 + * a waste of time. Ditto for the INTERFACE_CHANGE and
3978 + * CONFIG_CHANGE cases. */
3979 + // for (i = 0; i < fsg->nluns; ++i)
3980 + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
3981 + break;
3982 +
3983 + case FSG_STATE_INTERFACE_CHANGE:
3984 + rc = do_set_interface(fsg, 0);
3985 + if (fsg->ep0_req_tag != exception_req_tag)
3986 + break;
3987 + if (rc != 0) // STALL on errors
3988 + fsg_set_halt(fsg, fsg->ep0);
3989 + else // Complete the status stage
3990 + ep0_queue(fsg);
3991 + break;
3992 +
3993 + case FSG_STATE_CONFIG_CHANGE:
3994 + rc = do_set_config(fsg, new_config);
3995 + if (fsg->ep0_req_tag != exception_req_tag)
3996 + break;
3997 + if (rc != 0) // STALL on errors
3998 + fsg_set_halt(fsg, fsg->ep0);
3999 + else // Complete the status stage
4000 + ep0_queue(fsg);
4001 + break;
4002 +
4003 + case FSG_STATE_DISCONNECT:
4004 + for (i = 0; i < fsg->nluns; ++i)
4005 + fsg_lun_fsync_sub(fsg->luns + i);
4006 + do_set_config(fsg, 0); // Unconfigured state
4007 + break;
4008 +
4009 + case FSG_STATE_EXIT:
4010 + case FSG_STATE_TERMINATED:
4011 + do_set_config(fsg, 0); // Free resources
4012 + spin_lock_irq(&fsg->lock);
4013 + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
4014 + spin_unlock_irq(&fsg->lock);
4015 + break;
4016 + }
4017 +}
4018 +
4019 +
4020 +/*-------------------------------------------------------------------------*/
4021 +
4022 +static int fsg_main_thread(void *fsg_)
4023 +{
4024 + struct fsg_dev *fsg = fsg_;
4025 +
4026 + /* Allow the thread to be killed by a signal, but set the signal mask
4027 + * to block everything but INT, TERM, KILL, and USR1. */
4028 + allow_signal(SIGINT);
4029 + allow_signal(SIGTERM);
4030 + allow_signal(SIGKILL);
4031 + allow_signal(SIGUSR1);
4032 +
4033 + /* Allow the thread to be frozen */
4034 + set_freezable();
4035 +
4036 + /* Arrange for userspace references to be interpreted as kernel
4037 + * pointers. That way we can pass a kernel pointer to a routine
4038 + * that expects a __user pointer and it will work okay. */
4039 + set_fs(get_ds());
4040 +
4041 + /* The main loop */
4042 + while (fsg->state != FSG_STATE_TERMINATED) {
4043 + if (exception_in_progress(fsg) || signal_pending(current)) {
4044 + handle_exception(fsg);
4045 + continue;
4046 + }
4047 +
4048 + if (!fsg->running) {
4049 + sleep_thread(fsg);
4050 + continue;
4051 + }
4052 +
4053 + if (get_next_command(fsg))
4054 + continue;
4055 +
4056 + spin_lock_irq(&fsg->lock);
4057 + if (!exception_in_progress(fsg))
4058 + fsg->state = FSG_STATE_DATA_PHASE;
4059 + spin_unlock_irq(&fsg->lock);
4060 +
4061 + if (do_scsi_command(fsg) || finish_reply(fsg))
4062 + continue;
4063 +
4064 + spin_lock_irq(&fsg->lock);
4065 + if (!exception_in_progress(fsg))
4066 + fsg->state = FSG_STATE_STATUS_PHASE;
4067 + spin_unlock_irq(&fsg->lock);
4068 +
4069 + if (send_status(fsg))
4070 + continue;
4071 +
4072 + spin_lock_irq(&fsg->lock);
4073 + if (!exception_in_progress(fsg))
4074 + fsg->state = FSG_STATE_IDLE;
4075 + spin_unlock_irq(&fsg->lock);
4076 + }
4077 +
4078 + spin_lock_irq(&fsg->lock);
4079 + fsg->thread_task = NULL;
4080 + spin_unlock_irq(&fsg->lock);
4081 +
4082 + /* If we are exiting because of a signal, unregister the
4083 + * gadget driver. */
4084 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
4085 + usb_gadget_unregister_driver(&fsg_driver);
4086 +
4087 + /* Let the unbind and cleanup routines know the thread has exited */
4088 + complete_and_exit(&fsg->thread_notifier, 0);
4089 +}
4090 +
4091 +
4092 +/*-------------------------------------------------------------------------*/
4093 +
4094 +
4095 +/* The write permissions and store_xxx pointers are set in fsg_bind() */
4096 +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
4097 +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
4098 +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
4099 +
4100 +
4101 +/*-------------------------------------------------------------------------*/
4102 +
4103 +static void fsg_release(struct kref *ref)
4104 +{
4105 + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
4106 +
4107 + kfree(fsg->luns);
4108 + kfree(fsg);
4109 +}
4110 +
4111 +static void lun_release(struct device *dev)
4112 +{
4113 + struct rw_semaphore *filesem = dev_get_drvdata(dev);
4114 + struct fsg_dev *fsg =
4115 + container_of(filesem, struct fsg_dev, filesem);
4116 +
4117 + kref_put(&fsg->ref, fsg_release);
4118 +}
4119 +
4120 +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
4121 +{
4122 + struct fsg_dev *fsg = get_gadget_data(gadget);
4123 + int i;
4124 + struct fsg_lun *curlun;
4125 + struct usb_request *req = fsg->ep0req;
4126 +
4127 + DBG(fsg, "unbind\n");
4128 + clear_bit(REGISTERED, &fsg->atomic_bitflags);
4129 +
4130 + /* If the thread isn't already dead, tell it to exit now */
4131 + if (fsg->state != FSG_STATE_TERMINATED) {
4132 + raise_exception(fsg, FSG_STATE_EXIT);
4133 + wait_for_completion(&fsg->thread_notifier);
4134 +
4135 + /* The cleanup routine waits for this completion also */
4136 + complete(&fsg->thread_notifier);
4137 + }
4138 +
4139 + /* Unregister the sysfs attribute files and the LUNs */
4140 + for (i = 0; i < fsg->nluns; ++i) {
4141 + curlun = &fsg->luns[i];
4142 + if (curlun->registered) {
4143 + device_remove_file(&curlun->dev, &dev_attr_nofua);
4144 + device_remove_file(&curlun->dev, &dev_attr_ro);
4145 + device_remove_file(&curlun->dev, &dev_attr_file);
4146 + fsg_lun_close(curlun);
4147 + device_unregister(&curlun->dev);
4148 + curlun->registered = 0;
4149 + }
4150 + }
4151 +
4152 + /* Free the data buffers */
4153 + for (i = 0; i < fsg_num_buffers; ++i)
4154 + kfree(fsg->buffhds[i].buf);
4155 +
4156 + /* Free the request and buffer for endpoint 0 */
4157 + if (req) {
4158 + kfree(req->buf);
4159 + usb_ep_free_request(fsg->ep0, req);
4160 + }
4161 +
4162 + set_gadget_data(gadget, NULL);
4163 +}
4164 +
4165 +
4166 +static int __init check_parameters(struct fsg_dev *fsg)
4167 +{
4168 + int prot;
4169 + int gcnum;
4170 +
4171 + /* Store the default values */
4172 + mod_data.transport_type = USB_PR_BULK;
4173 + mod_data.transport_name = "Bulk-only";
4174 + mod_data.protocol_type = USB_SC_SCSI;
4175 + mod_data.protocol_name = "Transparent SCSI";
4176 +
4177 + /* Some peripheral controllers are known not to be able to
4178 + * halt bulk endpoints correctly. If one of them is present,
4179 + * disable stalls.
4180 + */
4181 + if (gadget_is_at91(fsg->gadget))
4182 + mod_data.can_stall = 0;
4183 +
4184 + if (mod_data.release == 0xffff) { // Parameter wasn't set
4185 + gcnum = usb_gadget_controller_number(fsg->gadget);
4186 + if (gcnum >= 0)
4187 + mod_data.release = 0x0300 + gcnum;
4188 + else {
4189 + WARNING(fsg, "controller '%s' not recognized\n",
4190 + fsg->gadget->name);
4191 + mod_data.release = 0x0399;
4192 + }
4193 + }
4194 +
4195 + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
4196 +
4197 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
4198 + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
4199 + ; // Use default setting
4200 + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
4201 + mod_data.transport_type = USB_PR_CB;
4202 + mod_data.transport_name = "Control-Bulk";
4203 + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
4204 + mod_data.transport_type = USB_PR_CBI;
4205 + mod_data.transport_name = "Control-Bulk-Interrupt";
4206 + } else {
4207 + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
4208 + return -EINVAL;
4209 + }
4210 +
4211 + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
4212 + prot == USB_SC_SCSI) {
4213 + ; // Use default setting
4214 + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
4215 + prot == USB_SC_RBC) {
4216 + mod_data.protocol_type = USB_SC_RBC;
4217 + mod_data.protocol_name = "RBC";
4218 + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
4219 + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
4220 + prot == USB_SC_8020) {
4221 + mod_data.protocol_type = USB_SC_8020;
4222 + mod_data.protocol_name = "8020i (ATAPI)";
4223 + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
4224 + prot == USB_SC_QIC) {
4225 + mod_data.protocol_type = USB_SC_QIC;
4226 + mod_data.protocol_name = "QIC-157";
4227 + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
4228 + prot == USB_SC_UFI) {
4229 + mod_data.protocol_type = USB_SC_UFI;
4230 + mod_data.protocol_name = "UFI";
4231 + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
4232 + prot == USB_SC_8070) {
4233 + mod_data.protocol_type = USB_SC_8070;
4234 + mod_data.protocol_name = "8070i";
4235 + } else {
4236 + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
4237 + return -EINVAL;
4238 + }
4239 +
4240 + mod_data.buflen &= PAGE_CACHE_MASK;
4241 + if (mod_data.buflen <= 0) {
4242 + ERROR(fsg, "invalid buflen\n");
4243 + return -ETOOSMALL;
4244 + }
4245 +
4246 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
4247 +
4248 + /* Serial string handling.
4249 + * On a real device, the serial string would be loaded
4250 + * from permanent storage. */
4251 + if (mod_data.serial) {
4252 + const char *ch;
4253 + unsigned len = 0;
4254 +
4255 + /* Sanity check :
4256 + * The CB[I] specification limits the serial string to
4257 + * 12 uppercase hexadecimal characters.
4258 + * BBB need at least 12 uppercase hexadecimal characters,
4259 + * with a maximum of 126. */
4260 + for (ch = mod_data.serial; *ch; ++ch) {
4261 + ++len;
4262 + if ((*ch < '0' || *ch > '9') &&
4263 + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
4264 + WARNING(fsg,
4265 + "Invalid serial string character: %c\n",
4266 + *ch);
4267 + goto no_serial;
4268 + }
4269 + }
4270 + if (len > 126 ||
4271 + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
4272 + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
4273 + WARNING(fsg, "Invalid serial string length!\n");
4274 + goto no_serial;
4275 + }
4276 + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
4277 + } else {
4278 + WARNING(fsg, "No serial-number string provided!\n");
4279 + no_serial:
4280 + device_desc.iSerialNumber = 0;
4281 + }
4282 +
4283 + return 0;
4284 +}
4285 +
4286 +
4287 +static int __init fsg_bind(struct usb_gadget *gadget)
4288 +{
4289 + struct fsg_dev *fsg = the_fsg;
4290 + int rc;
4291 + int i;
4292 + struct fsg_lun *curlun;
4293 + struct usb_ep *ep;
4294 + struct usb_request *req;
4295 + char *pathbuf, *p;
4296 +
4297 + fsg->gadget = gadget;
4298 + set_gadget_data(gadget, fsg);
4299 + fsg->ep0 = gadget->ep0;
4300 + fsg->ep0->driver_data = fsg;
4301 +
4302 + if ((rc = check_parameters(fsg)) != 0)
4303 + goto out;
4304 +
4305 + if (mod_data.removable) { // Enable the store_xxx attributes
4306 + dev_attr_file.attr.mode = 0644;
4307 + dev_attr_file.store = fsg_store_file;
4308 + if (!mod_data.cdrom) {
4309 + dev_attr_ro.attr.mode = 0644;
4310 + dev_attr_ro.store = fsg_store_ro;
4311 + }
4312 + }
4313 +
4314 + /* Only for removable media? */
4315 + dev_attr_nofua.attr.mode = 0644;
4316 + dev_attr_nofua.store = fsg_store_nofua;
4317 +
4318 + /* Find out how many LUNs there should be */
4319 + i = mod_data.nluns;
4320 + if (i == 0)
4321 + i = max(mod_data.num_filenames, 1u);
4322 + if (i > FSG_MAX_LUNS) {
4323 + ERROR(fsg, "invalid number of LUNs: %d\n", i);
4324 + rc = -EINVAL;
4325 + goto out;
4326 + }
4327 +
4328 + /* Create the LUNs, open their backing files, and register the
4329 + * LUN devices in sysfs. */
4330 + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
4331 + if (!fsg->luns) {
4332 + rc = -ENOMEM;
4333 + goto out;
4334 + }
4335 + fsg->nluns = i;
4336 +
4337 + for (i = 0; i < fsg->nluns; ++i) {
4338 + curlun = &fsg->luns[i];
4339 + curlun->cdrom = !!mod_data.cdrom;
4340 + curlun->ro = mod_data.cdrom || mod_data.ro[i];
4341 + curlun->initially_ro = curlun->ro;
4342 + curlun->removable = mod_data.removable;
4343 + curlun->nofua = mod_data.nofua[i];
4344 + curlun->dev.release = lun_release;
4345 + curlun->dev.parent = &gadget->dev;
4346 + curlun->dev.driver = &fsg_driver.driver;
4347 + dev_set_drvdata(&curlun->dev, &fsg->filesem);
4348 + dev_set_name(&curlun->dev,"%s-lun%d",
4349 + dev_name(&gadget->dev), i);
4350 +
4351 + kref_get(&fsg->ref);
4352 + rc = device_register(&curlun->dev);
4353 + if (rc) {
4354 + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
4355 + put_device(&curlun->dev);
4356 + goto out;
4357 + }
4358 + curlun->registered = 1;
4359 +
4360 + rc = device_create_file(&curlun->dev, &dev_attr_ro);
4361 + if (rc)
4362 + goto out;
4363 + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
4364 + if (rc)
4365 + goto out;
4366 + rc = device_create_file(&curlun->dev, &dev_attr_file);
4367 + if (rc)
4368 + goto out;
4369 +
4370 + if (mod_data.file[i] && *mod_data.file[i]) {
4371 + rc = fsg_lun_open(curlun, mod_data.file[i]);
4372 + if (rc)
4373 + goto out;
4374 + } else if (!mod_data.removable) {
4375 + ERROR(fsg, "no file given for LUN%d\n", i);
4376 + rc = -EINVAL;
4377 + goto out;
4378 + }
4379 + }
4380 +
4381 + /* Find all the endpoints we will use */
4382 + usb_ep_autoconfig_reset(gadget);
4383 + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
4384 + if (!ep)
4385 + goto autoconf_fail;
4386 + ep->driver_data = fsg; // claim the endpoint
4387 + fsg->bulk_in = ep;
4388 +
4389 + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
4390 + if (!ep)
4391 + goto autoconf_fail;
4392 + ep->driver_data = fsg; // claim the endpoint
4393 + fsg->bulk_out = ep;
4394 +
4395 + if (transport_is_cbi()) {
4396 + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
4397 + if (!ep)
4398 + goto autoconf_fail;
4399 + ep->driver_data = fsg; // claim the endpoint
4400 + fsg->intr_in = ep;
4401 + }
4402 +
4403 + /* Fix up the descriptors */
4404 + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
4405 + device_desc.idProduct = cpu_to_le16(mod_data.product);
4406 + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
4407 +
4408 + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
4409 + fsg_intf_desc.bNumEndpoints = i;
4410 + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
4411 + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
4412 + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4413 +
4414 + if (gadget_is_dualspeed(gadget)) {
4415 + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4416 +
4417 + /* Assume endpoint addresses are the same for both speeds */
4418 + fsg_hs_bulk_in_desc.bEndpointAddress =
4419 + fsg_fs_bulk_in_desc.bEndpointAddress;
4420 + fsg_hs_bulk_out_desc.bEndpointAddress =
4421 + fsg_fs_bulk_out_desc.bEndpointAddress;
4422 + fsg_hs_intr_in_desc.bEndpointAddress =
4423 + fsg_fs_intr_in_desc.bEndpointAddress;
4424 + }
4425 +
4426 + if (gadget_is_superspeed(gadget)) {
4427 + unsigned max_burst;
4428 +
4429 + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4430 +
4431 + /* Calculate bMaxBurst, we know packet size is 1024 */
4432 + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
4433 +
4434 + /* Assume endpoint addresses are the same for both speeds */
4435 + fsg_ss_bulk_in_desc.bEndpointAddress =
4436 + fsg_fs_bulk_in_desc.bEndpointAddress;
4437 + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
4438 +
4439 + fsg_ss_bulk_out_desc.bEndpointAddress =
4440 + fsg_fs_bulk_out_desc.bEndpointAddress;
4441 + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
4442 + }
4443 +
4444 + if (gadget_is_otg(gadget))
4445 + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
4446 +
4447 + rc = -ENOMEM;
4448 +
4449 + /* Allocate the request and buffer for endpoint 0 */
4450 + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
4451 + if (!req)
4452 + goto out;
4453 + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
4454 + if (!req->buf)
4455 + goto out;
4456 + req->complete = ep0_complete;
4457 +
4458 + /* Allocate the data buffers */
4459 + for (i = 0; i < fsg_num_buffers; ++i) {
4460 + struct fsg_buffhd *bh = &fsg->buffhds[i];
4461 +
4462 + /* Allocate for the bulk-in endpoint. We assume that
4463 + * the buffer will also work with the bulk-out (and
4464 + * interrupt-in) endpoint. */
4465 + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
4466 + if (!bh->buf)
4467 + goto out;
4468 + bh->next = bh + 1;
4469 + }
4470 + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
4471 +
4472 + /* This should reflect the actual gadget power source */
4473 + usb_gadget_set_selfpowered(gadget);
4474 +
4475 + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
4476 + "%s %s with %s",
4477 + init_utsname()->sysname, init_utsname()->release,
4478 + gadget->name);
4479 +
4480 + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
4481 + "file-storage-gadget");
4482 + if (IS_ERR(fsg->thread_task)) {
4483 + rc = PTR_ERR(fsg->thread_task);
4484 + goto out;
4485 + }
4486 +
4487 + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
4488 + INFO(fsg, "NOTE: This driver is deprecated. "
4489 + "Consider using g_mass_storage instead.\n");
4490 + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
4491 +
4492 + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
4493 + for (i = 0; i < fsg->nluns; ++i) {
4494 + curlun = &fsg->luns[i];
4495 + if (fsg_lun_is_open(curlun)) {
4496 + p = NULL;
4497 + if (pathbuf) {
4498 + p = d_path(&curlun->filp->f_path,
4499 + pathbuf, PATH_MAX);
4500 + if (IS_ERR(p))
4501 + p = NULL;
4502 + }
4503 + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
4504 + curlun->ro, curlun->nofua, (p ? p : "(error)"));
4505 + }
4506 + }
4507 + kfree(pathbuf);
4508 +
4509 + DBG(fsg, "transport=%s (x%02x)\n",
4510 + mod_data.transport_name, mod_data.transport_type);
4511 + DBG(fsg, "protocol=%s (x%02x)\n",
4512 + mod_data.protocol_name, mod_data.protocol_type);
4513 + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
4514 + mod_data.vendor, mod_data.product, mod_data.release);
4515 + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
4516 + mod_data.removable, mod_data.can_stall,
4517 + mod_data.cdrom, mod_data.buflen);
4518 + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
4519 +
4520 + set_bit(REGISTERED, &fsg->atomic_bitflags);
4521 +
4522 + /* Tell the thread to start working */
4523 + wake_up_process(fsg->thread_task);
4524 + return 0;
4525 +
4526 +autoconf_fail:
4527 + ERROR(fsg, "unable to autoconfigure all endpoints\n");
4528 + rc = -ENOTSUPP;
4529 +
4530 +out:
4531 + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
4532 + fsg_unbind(gadget);
4533 + complete(&fsg->thread_notifier);
4534 + return rc;
4535 +}
4536 +
4537 +
4538 +/*-------------------------------------------------------------------------*/
4539 +
4540 +static void fsg_suspend(struct usb_gadget *gadget)
4541 +{
4542 + struct fsg_dev *fsg = get_gadget_data(gadget);
4543 +
4544 + DBG(fsg, "suspend\n");
4545 + set_bit(SUSPENDED, &fsg->atomic_bitflags);
4546 +}
4547 +
4548 +static void fsg_resume(struct usb_gadget *gadget)
4549 +{
4550 + struct fsg_dev *fsg = get_gadget_data(gadget);
4551 +
4552 + DBG(fsg, "resume\n");
4553 + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
4554 +}
4555 +
4556 +
4557 +/*-------------------------------------------------------------------------*/
4558 +
4559 +static struct usb_gadget_driver fsg_driver = {
4560 + .max_speed = USB_SPEED_SUPER,
4561 + .function = (char *) fsg_string_product,
4562 + .unbind = fsg_unbind,
4563 + .disconnect = fsg_disconnect,
4564 + .setup = fsg_setup,
4565 + .suspend = fsg_suspend,
4566 + .resume = fsg_resume,
4567 +
4568 + .driver = {
4569 + .name = DRIVER_NAME,
4570 + .owner = THIS_MODULE,
4571 + // .release = ...
4572 + // .suspend = ...
4573 + // .resume = ...
4574 + },
4575 +};
4576 +
4577 +
4578 +static int __init fsg_alloc(void)
4579 +{
4580 + struct fsg_dev *fsg;
4581 +
4582 + fsg = kzalloc(sizeof *fsg +
4583 + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
4584 +
4585 + if (!fsg)
4586 + return -ENOMEM;
4587 + spin_lock_init(&fsg->lock);
4588 + init_rwsem(&fsg->filesem);
4589 + kref_init(&fsg->ref);
4590 + init_completion(&fsg->thread_notifier);
4591 +
4592 + the_fsg = fsg;
4593 + return 0;
4594 +}
4595 +
4596 +
4597 +static int __init fsg_init(void)
4598 +{
4599 + int rc;
4600 + struct fsg_dev *fsg;
4601 +
4602 + rc = fsg_num_buffers_validate();
4603 + if (rc != 0)
4604 + return rc;
4605 +
4606 + if ((rc = fsg_alloc()) != 0)
4607 + return rc;
4608 + fsg = the_fsg;
4609 + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
4610 + kref_put(&fsg->ref, fsg_release);
4611 + return rc;
4612 +}
4613 +module_init(fsg_init);
4614 +
4615 +
4616 +static void __exit fsg_cleanup(void)
4617 +{
4618 + struct fsg_dev *fsg = the_fsg;
4619 +
4620 + /* Unregister the driver iff the thread hasn't already done so */
4621 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
4622 + usb_gadget_unregister_driver(&fsg_driver);
4623 +
4624 + /* Wait for the thread to finish up */
4625 + wait_for_completion(&fsg->thread_notifier);
4626 +
4627 + kref_put(&fsg->ref, fsg_release);
4628 +}
4629 +module_exit(fsg_cleanup);
4630 --- a/drivers/usb/host/Kconfig
4631 +++ b/drivers/usb/host/Kconfig
4632 @@ -762,6 +762,19 @@ config USB_HWA_HCD
4633 To compile this driver a module, choose M here: the module
4634 will be called "hwa-hc".
4635
4636 +config USB_DWCOTG
4637 + tristate "Synopsis DWC host support"
4638 + depends on USB
4639 + help
4640 + The Synopsis DWC controller is a dual-role
4641 + host/peripheral/OTG ("On The Go") USB controllers.
4642 +
4643 + Enable this option to support this IP in host controller mode.
4644 + If unsure, say N.
4645 +
4646 + To compile this driver as a module, choose M here: the
4647 + modules built will be called dwc_otg and dwc_common_port.
4648 +
4649 config USB_IMX21_HCD
4650 tristate "i.MX21 HCD support"
4651 depends on ARM && ARCH_MXC
4652 --- a/drivers/usb/host/Makefile
4653 +++ b/drivers/usb/host/Makefile
4654 @@ -74,6 +74,8 @@ obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
4655 obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
4656 obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
4657 obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
4658 +
4659 +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
4660 obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
4661 obj-$(CONFIG_USB_FSL_USB2) += fsl-mph-dr-of.o
4662 obj-$(CONFIG_USB_EHCI_FSL) += fsl-mph-dr-of.o
4663 --- /dev/null
4664 +++ b/drivers/usb/host/dwc_common_port/Makefile
4665 @@ -0,0 +1,58 @@
4666 +#
4667 +# Makefile for DWC_common library
4668 +#
4669 +
4670 +ifneq ($(KERNELRELEASE),)
4671 +
4672 +ccflags-y += -DDWC_LINUX
4673 +#ccflags-y += -DDEBUG
4674 +#ccflags-y += -DDWC_DEBUG_REGS
4675 +#ccflags-y += -DDWC_DEBUG_MEMORY
4676 +
4677 +ccflags-y += -DDWC_LIBMODULE
4678 +ccflags-y += -DDWC_CCLIB
4679 +#ccflags-y += -DDWC_CRYPTOLIB
4680 +ccflags-y += -DDWC_NOTIFYLIB
4681 +ccflags-y += -DDWC_UTFLIB
4682 +
4683 +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
4684 +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
4685 + dwc_crypto.o dwc_notifier.o \
4686 + dwc_common_linux.o dwc_mem.o
4687 +
4688 +kernrelwd := $(subst ., ,$(KERNELRELEASE))
4689 +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
4690 +
4691 +ifneq ($(kernrel3),2.6.20)
4692 +# grayg - I only know that we use ccflags-y in 2.6.31 actually
4693 +ccflags-y += $(CPPFLAGS)
4694 +endif
4695 +
4696 +else
4697 +
4698 +#ifeq ($(KDIR),)
4699 +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
4700 +#endif
4701 +
4702 +ifeq ($(ARCH),)
4703 +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
4704 + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
4705 +endif
4706 +
4707 +ifeq ($(DOXYGEN),)
4708 +DOXYGEN := doxygen
4709 +endif
4710 +
4711 +default:
4712 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
4713 +
4714 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
4715 + $(DOXYGEN) doc/doxygen.cfg
4716 +
4717 +tags: $(wildcard *.[hc])
4718 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
4719 +
4720 +endif
4721 +
4722 +clean:
4723 + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
4724 --- /dev/null
4725 +++ b/drivers/usb/host/dwc_common_port/Makefile.fbsd
4726 @@ -0,0 +1,17 @@
4727 +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
4728 +CFLAGS += -DDWC_FREEBSD
4729 +CFLAGS += -DDEBUG
4730 +#CFLAGS += -DDWC_DEBUG_REGS
4731 +#CFLAGS += -DDWC_DEBUG_MEMORY
4732 +
4733 +#CFLAGS += -DDWC_LIBMODULE
4734 +#CFLAGS += -DDWC_CCLIB
4735 +#CFLAGS += -DDWC_CRYPTOLIB
4736 +#CFLAGS += -DDWC_NOTIFYLIB
4737 +#CFLAGS += -DDWC_UTFLIB
4738 +
4739 +KMOD = dwc_common_port_lib
4740 +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
4741 + dwc_common_fbsd.c dwc_mem.c
4742 +
4743 +.include <bsd.kmod.mk>
4744 --- /dev/null
4745 +++ b/drivers/usb/host/dwc_common_port/Makefile.linux
4746 @@ -0,0 +1,49 @@
4747 +#
4748 +# Makefile for DWC_common library
4749 +#
4750 +ifneq ($(KERNELRELEASE),)
4751 +
4752 +ccflags-y += -DDWC_LINUX
4753 +#ccflags-y += -DDEBUG
4754 +#ccflags-y += -DDWC_DEBUG_REGS
4755 +#ccflags-y += -DDWC_DEBUG_MEMORY
4756 +
4757 +ccflags-y += -DDWC_LIBMODULE
4758 +ccflags-y += -DDWC_CCLIB
4759 +ccflags-y += -DDWC_CRYPTOLIB
4760 +ccflags-y += -DDWC_NOTIFYLIB
4761 +ccflags-y += -DDWC_UTFLIB
4762 +
4763 +obj-m := dwc_common_port_lib.o
4764 +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
4765 + dwc_crypto.o dwc_notifier.o \
4766 + dwc_common_linux.o dwc_mem.o
4767 +
4768 +else
4769 +
4770 +ifeq ($(KDIR),)
4771 +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
4772 +endif
4773 +
4774 +ifeq ($(ARCH),)
4775 +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
4776 + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
4777 +endif
4778 +
4779 +ifeq ($(DOXYGEN),)
4780 +DOXYGEN := doxygen
4781 +endif
4782 +
4783 +default:
4784 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
4785 +
4786 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
4787 + $(DOXYGEN) doc/doxygen.cfg
4788 +
4789 +tags: $(wildcard *.[hc])
4790 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
4791 +
4792 +endif
4793 +
4794 +clean:
4795 + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
4796 --- /dev/null
4797 +++ b/drivers/usb/host/dwc_common_port/changes.txt
4798 @@ -0,0 +1,174 @@
4799 +
4800 +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
4801 +IO context struct. The IO context struct should live in an os-dependent struct
4802 +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
4803 +named 'os_dep' embedded in the main device struct. So there these calls look
4804 +like this:
4805 +
4806 + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
4807 +
4808 + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
4809 + &pcd->dev_global_regs->dcfg, 0);
4810 +
4811 +Note that for the existing Linux driver ports, it is not necessary to actually
4812 +define the 'ioctx' member in the os-dependent struct. Since Linux does not
4813 +require an IO context, its macros for dwc_read_reg32() and friends do not
4814 +use the context pointer, so it is optimized away by the compiler. But it is
4815 +necessary to add the pointer parameter to all of the call sites, to be ready
4816 +for any future ports (such as FreeBSD) which do require an IO context.
4817 +
4818 +
4819 +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
4820 +take an additional parameter, a pointer to a memory context. Examples:
4821 +
4822 + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
4823 +
4824 + dwc_free(&usb3_dev->os_dep.memctx, addr);
4825 +
4826 +Again, for the Linux ports, it is not necessary to actually define the memctx
4827 +member, but it is necessary to add the pointer parameter to all of the call
4828 +sites.
4829 +
4830 +
4831 +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
4832 +
4833 + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
4834 +
4835 + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
4836 +
4837 +
4838 +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
4839 +
4840 + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
4841 +
4842 + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
4843 +
4844 +
4845 +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
4846 +
4847 + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
4848 +
4849 + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
4850 +
4851 +
4852 +Same for dwc_timer_alloc(). Example:
4853 +
4854 + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
4855 + cb_func, cb_data);
4856 +
4857 +
4858 +Same for dwc_waitq_alloc(). Example:
4859 +
4860 + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
4861 +
4862 +
4863 +Same for dwc_thread_run(). Example:
4864 +
4865 + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
4866 + "dwc_usb3_thd1", data);
4867 +
4868 +
4869 +Same for dwc_workq_alloc(). Example:
4870 +
4871 + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
4872 +
4873 +
4874 +Same for dwc_task_alloc(). Example:
4875 +
4876 + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
4877 + cb_func, cb_data);
4878 +
4879 +
4880 +In addition to the context pointer additions, a few core functions have had
4881 +other changes made to their parameters:
4882 +
4883 +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
4884 +has been changed from a uint64_t to a dwc_irqflags_t.
4885 +
4886 +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
4887 +FreeBSD equivalent of that function requires it.
4888 +
4889 +And, in addition to the context pointer, dwc_task_alloc() also adds a
4890 +'char *name' parameter, to be consistent with dwc_thread_run() and
4891 +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
4892 +requires a unique name.
4893 +
4894 +
4895 +Here is a complete list of the core functions that now take a pointer to a
4896 +context as their first parameter:
4897 +
4898 + dwc_read_reg32
4899 + dwc_read_reg64
4900 + dwc_write_reg32
4901 + dwc_write_reg64
4902 + dwc_modify_reg32
4903 + dwc_modify_reg64
4904 + dwc_alloc
4905 + dwc_alloc_atomic
4906 + dwc_strdup
4907 + dwc_free
4908 + dwc_dma_alloc
4909 + dwc_dma_free
4910 + dwc_mutex_alloc
4911 + dwc_mutex_free
4912 + dwc_spinlock_alloc
4913 + dwc_spinlock_free
4914 + dwc_timer_alloc
4915 + dwc_waitq_alloc
4916 + dwc_thread_run
4917 + dwc_workq_alloc
4918 + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
4919 +
4920 +And here are the core functions that have other changes to their parameters:
4921 +
4922 + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
4923 + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
4924 + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
4925 +
4926 +
4927 +
4928 +The changes to the core functions also require some of the other library
4929 +functions to change:
4930 +
4931 + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
4932 + (for memory allocation) as the 1st param and a 'void *mtxctx'
4933 + (for mutex allocation) as the 2nd param.
4934 +
4935 + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
4936 + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
4937 + 'void *memctx' as the 1st param.
4938 +
4939 + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
4940 + 'void *memctx' as the 1st param.
4941 +
4942 + dwc_modpow() now takes a 'void *memctx' as the 1st param.
4943 +
4944 + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
4945 + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
4946 + param, and also now returns an integer value that is non-zero if
4947 + allocation of its data structures or work queue fails.
4948 +
4949 + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
4950 +
4951 + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
4952 + param, and also now returns an integer value that is non-zero if
4953 + allocation of its data structures fails.
4954 +
4955 +
4956 +
4957 +Other miscellaneous changes:
4958 +
4959 +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
4960 +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
4961 +
4962 +The following #define's have been added to allow selectively compiling library
4963 +features:
4964 +
4965 + DWC_CCLIB
4966 + DWC_CRYPTOLIB
4967 + DWC_NOTIFYLIB
4968 + DWC_UTFLIB
4969 +
4970 +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
4971 +module code in dwc_common_linux.c is not compiled in. This allows linking the
4972 +library code directly into a driver module, instead of as a standalone module.
4973 --- /dev/null
4974 +++ b/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
4975 @@ -0,0 +1,270 @@
4976 +# Doxyfile 1.4.5
4977 +
4978 +#---------------------------------------------------------------------------
4979 +# Project related configuration options
4980 +#---------------------------------------------------------------------------
4981 +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
4982 +PROJECT_NUMBER =
4983 +OUTPUT_DIRECTORY = doc
4984 +CREATE_SUBDIRS = NO
4985 +OUTPUT_LANGUAGE = English
4986 +BRIEF_MEMBER_DESC = YES
4987 +REPEAT_BRIEF = YES
4988 +ABBREVIATE_BRIEF = "The $name class" \
4989 + "The $name widget" \
4990 + "The $name file" \
4991 + is \
4992 + provides \
4993 + specifies \
4994 + contains \
4995 + represents \
4996 + a \
4997 + an \
4998 + the
4999 +ALWAYS_DETAILED_SEC = YES
5000 +INLINE_INHERITED_MEMB = NO
5001 +FULL_PATH_NAMES = NO
5002 +STRIP_FROM_PATH = ..
5003 +STRIP_FROM_INC_PATH =
5004 +SHORT_NAMES = NO
5005 +JAVADOC_AUTOBRIEF = YES
5006 +MULTILINE_CPP_IS_BRIEF = NO
5007 +DETAILS_AT_TOP = YES
5008 +INHERIT_DOCS = YES
5009 +SEPARATE_MEMBER_PAGES = NO
5010 +TAB_SIZE = 8
5011 +ALIASES =
5012 +OPTIMIZE_OUTPUT_FOR_C = YES
5013 +OPTIMIZE_OUTPUT_JAVA = NO
5014 +BUILTIN_STL_SUPPORT = NO
5015 +DISTRIBUTE_GROUP_DOC = NO
5016 +SUBGROUPING = NO
5017 +#---------------------------------------------------------------------------
5018 +# Build related configuration options
5019 +#---------------------------------------------------------------------------
5020 +EXTRACT_ALL = NO
5021 +EXTRACT_PRIVATE = NO
5022 +EXTRACT_STATIC = YES
5023 +EXTRACT_LOCAL_CLASSES = NO
5024 +EXTRACT_LOCAL_METHODS = NO
5025 +HIDE_UNDOC_MEMBERS = NO
5026 +HIDE_UNDOC_CLASSES = NO
5027 +HIDE_FRIEND_COMPOUNDS = NO
5028 +HIDE_IN_BODY_DOCS = NO
5029 +INTERNAL_DOCS = NO
5030 +CASE_SENSE_NAMES = YES
5031 +HIDE_SCOPE_NAMES = NO
5032 +SHOW_INCLUDE_FILES = NO
5033 +INLINE_INFO = YES
5034 +SORT_MEMBER_DOCS = NO
5035 +SORT_BRIEF_DOCS = NO
5036 +SORT_BY_SCOPE_NAME = NO
5037 +GENERATE_TODOLIST = YES
5038 +GENERATE_TESTLIST = YES
5039 +GENERATE_BUGLIST = YES
5040 +GENERATE_DEPRECATEDLIST= YES
5041 +ENABLED_SECTIONS =
5042 +MAX_INITIALIZER_LINES = 30
5043 +SHOW_USED_FILES = YES
5044 +SHOW_DIRECTORIES = YES
5045 +FILE_VERSION_FILTER =
5046 +#---------------------------------------------------------------------------
5047 +# configuration options related to warning and progress messages
5048 +#---------------------------------------------------------------------------
5049 +QUIET = YES
5050 +WARNINGS = YES
5051 +WARN_IF_UNDOCUMENTED = NO
5052 +WARN_IF_DOC_ERROR = YES
5053 +WARN_NO_PARAMDOC = YES
5054 +WARN_FORMAT = "$file:$line: $text"
5055 +WARN_LOGFILE =
5056 +#---------------------------------------------------------------------------
5057 +# configuration options related to the input files
5058 +#---------------------------------------------------------------------------
5059 +INPUT = .
5060 +FILE_PATTERNS = *.c \
5061 + *.cc \
5062 + *.cxx \
5063 + *.cpp \
5064 + *.c++ \
5065 + *.d \
5066 + *.java \
5067 + *.ii \
5068 + *.ixx \
5069 + *.ipp \
5070 + *.i++ \
5071 + *.inl \
5072 + *.h \
5073 + *.hh \
5074 + *.hxx \
5075 + *.hpp \
5076 + *.h++ \
5077 + *.idl \
5078 + *.odl \
5079 + *.cs \
5080 + *.php \
5081 + *.php3 \
5082 + *.inc \
5083 + *.m \
5084 + *.mm \
5085 + *.dox \
5086 + *.py \
5087 + *.C \
5088 + *.CC \
5089 + *.C++ \
5090 + *.II \
5091 + *.I++ \
5092 + *.H \
5093 + *.HH \
5094 + *.H++ \
5095 + *.CS \
5096 + *.PHP \
5097 + *.PHP3 \
5098 + *.M \
5099 + *.MM \
5100 + *.PY
5101 +RECURSIVE = NO
5102 +EXCLUDE =
5103 +EXCLUDE_SYMLINKS = NO
5104 +EXCLUDE_PATTERNS =
5105 +EXAMPLE_PATH =
5106 +EXAMPLE_PATTERNS = *
5107 +EXAMPLE_RECURSIVE = NO
5108 +IMAGE_PATH =
5109 +INPUT_FILTER =
5110 +FILTER_PATTERNS =
5111 +FILTER_SOURCE_FILES = NO
5112 +#---------------------------------------------------------------------------
5113 +# configuration options related to source browsing
5114 +#---------------------------------------------------------------------------
5115 +SOURCE_BROWSER = NO
5116 +INLINE_SOURCES = NO
5117 +STRIP_CODE_COMMENTS = YES
5118 +REFERENCED_BY_RELATION = YES
5119 +REFERENCES_RELATION = YES
5120 +USE_HTAGS = NO
5121 +VERBATIM_HEADERS = NO
5122 +#---------------------------------------------------------------------------
5123 +# configuration options related to the alphabetical class index
5124 +#---------------------------------------------------------------------------
5125 +ALPHABETICAL_INDEX = NO
5126 +COLS_IN_ALPHA_INDEX = 5
5127 +IGNORE_PREFIX =
5128 +#---------------------------------------------------------------------------
5129 +# configuration options related to the HTML output
5130 +#---------------------------------------------------------------------------
5131 +GENERATE_HTML = YES
5132 +HTML_OUTPUT = html
5133 +HTML_FILE_EXTENSION = .html
5134 +HTML_HEADER =
5135 +HTML_FOOTER =
5136 +HTML_STYLESHEET =
5137 +HTML_ALIGN_MEMBERS = YES
5138 +GENERATE_HTMLHELP = NO
5139 +CHM_FILE =
5140 +HHC_LOCATION =
5141 +GENERATE_CHI = NO
5142 +BINARY_TOC = NO
5143 +TOC_EXPAND = NO
5144 +DISABLE_INDEX = NO
5145 +ENUM_VALUES_PER_LINE = 4
5146 +GENERATE_TREEVIEW = YES
5147 +TREEVIEW_WIDTH = 250
5148 +#---------------------------------------------------------------------------
5149 +# configuration options related to the LaTeX output
5150 +#---------------------------------------------------------------------------
5151 +GENERATE_LATEX = NO
5152 +LATEX_OUTPUT = latex
5153 +LATEX_CMD_NAME = latex
5154 +MAKEINDEX_CMD_NAME = makeindex
5155 +COMPACT_LATEX = NO
5156 +PAPER_TYPE = a4wide
5157 +EXTRA_PACKAGES =
5158 +LATEX_HEADER =
5159 +PDF_HYPERLINKS = NO
5160 +USE_PDFLATEX = NO
5161 +LATEX_BATCHMODE = NO
5162 +LATEX_HIDE_INDICES = NO
5163 +#---------------------------------------------------------------------------
5164 +# configuration options related to the RTF output
5165 +#---------------------------------------------------------------------------
5166 +GENERATE_RTF = NO
5167 +RTF_OUTPUT = rtf
5168 +COMPACT_RTF = NO
5169 +RTF_HYPERLINKS = NO
5170 +RTF_STYLESHEET_FILE =
5171 +RTF_EXTENSIONS_FILE =
5172 +#---------------------------------------------------------------------------
5173 +# configuration options related to the man page output
5174 +#---------------------------------------------------------------------------
5175 +GENERATE_MAN = NO
5176 +MAN_OUTPUT = man
5177 +MAN_EXTENSION = .3
5178 +MAN_LINKS = NO
5179 +#---------------------------------------------------------------------------
5180 +# configuration options related to the XML output
5181 +#---------------------------------------------------------------------------
5182 +GENERATE_XML = NO
5183 +XML_OUTPUT = xml
5184 +XML_SCHEMA =
5185 +XML_DTD =
5186 +XML_PROGRAMLISTING = YES
5187 +#---------------------------------------------------------------------------
5188 +# configuration options for the AutoGen Definitions output
5189 +#---------------------------------------------------------------------------
5190 +GENERATE_AUTOGEN_DEF = NO
5191 +#---------------------------------------------------------------------------
5192 +# configuration options related to the Perl module output
5193 +#---------------------------------------------------------------------------
5194 +GENERATE_PERLMOD = NO
5195 +PERLMOD_LATEX = NO
5196 +PERLMOD_PRETTY = YES
5197 +PERLMOD_MAKEVAR_PREFIX =
5198 +#---------------------------------------------------------------------------
5199 +# Configuration options related to the preprocessor
5200 +#---------------------------------------------------------------------------
5201 +ENABLE_PREPROCESSING = YES
5202 +MACRO_EXPANSION = NO
5203 +EXPAND_ONLY_PREDEF = NO
5204 +SEARCH_INCLUDES = YES
5205 +INCLUDE_PATH =
5206 +INCLUDE_FILE_PATTERNS =
5207 +PREDEFINED = DEBUG DEBUG_MEMORY
5208 +EXPAND_AS_DEFINED =
5209 +SKIP_FUNCTION_MACROS = YES
5210 +#---------------------------------------------------------------------------
5211 +# Configuration::additions related to external references
5212 +#---------------------------------------------------------------------------
5213 +TAGFILES =
5214 +GENERATE_TAGFILE =
5215 +ALLEXTERNALS = NO
5216 +EXTERNAL_GROUPS = YES
5217 +PERL_PATH = /usr/bin/perl
5218 +#---------------------------------------------------------------------------
5219 +# Configuration options related to the dot tool
5220 +#---------------------------------------------------------------------------
5221 +CLASS_DIAGRAMS = YES
5222 +HIDE_UNDOC_RELATIONS = YES
5223 +HAVE_DOT = NO
5224 +CLASS_GRAPH = YES
5225 +COLLABORATION_GRAPH = YES
5226 +GROUP_GRAPHS = YES
5227 +UML_LOOK = NO
5228 +TEMPLATE_RELATIONS = NO
5229 +INCLUDE_GRAPH = NO
5230 +INCLUDED_BY_GRAPH = YES
5231 +CALL_GRAPH = NO
5232 +GRAPHICAL_HIERARCHY = YES
5233 +DIRECTORY_GRAPH = YES
5234 +DOT_IMAGE_FORMAT = png
5235 +DOT_PATH =
5236 +DOTFILE_DIRS =
5237 +MAX_DOT_GRAPH_DEPTH = 1000
5238 +DOT_TRANSPARENT = NO
5239 +DOT_MULTI_TARGETS = NO
5240 +GENERATE_LEGEND = YES
5241 +DOT_CLEANUP = YES
5242 +#---------------------------------------------------------------------------
5243 +# Configuration::additions related to the search engine
5244 +#---------------------------------------------------------------------------
5245 +SEARCHENGINE = NO
5246 --- /dev/null
5247 +++ b/drivers/usb/host/dwc_common_port/dwc_cc.c
5248 @@ -0,0 +1,532 @@
5249 +/* =========================================================================
5250 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
5251 + * $Revision: #4 $
5252 + * $Date: 2010/11/04 $
5253 + * $Change: 1621692 $
5254 + *
5255 + * Synopsys Portability Library Software and documentation
5256 + * (hereinafter, "Software") is an Unsupported proprietary work of
5257 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
5258 + * between Synopsys and you.
5259 + *
5260 + * The Software IS NOT an item of Licensed Software or Licensed Product
5261 + * under any End User Software License Agreement or Agreement for
5262 + * Licensed Product with Synopsys or any supplement thereto. You are
5263 + * permitted to use and redistribute this Software in source and binary
5264 + * forms, with or without modification, provided that redistributions
5265 + * of source code must retain this notice. You may not view, use,
5266 + * disclose, copy or distribute this file or any information contained
5267 + * herein except pursuant to this license grant from Synopsys. If you
5268 + * do not agree with this notice, including the disclaimer below, then
5269 + * you are not authorized to use the Software.
5270 + *
5271 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
5272 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5273 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
5274 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
5275 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5276 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5277 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5278 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
5279 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5280 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
5281 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
5282 + * DAMAGE.
5283 + * ========================================================================= */
5284 +#ifdef DWC_CCLIB
5285 +
5286 +#include "dwc_cc.h"
5287 +
5288 +typedef struct dwc_cc
5289 +{
5290 + uint32_t uid;
5291 + uint8_t chid[16];
5292 + uint8_t cdid[16];
5293 + uint8_t ck[16];
5294 + uint8_t *name;
5295 + uint8_t length;
5296 + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
5297 +} dwc_cc_t;
5298 +
5299 +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
5300 +
5301 +/** The main structure for CC management. */
5302 +struct dwc_cc_if
5303 +{
5304 + dwc_mutex_t *mutex;
5305 + char *filename;
5306 +
5307 + unsigned is_host:1;
5308 +
5309 + dwc_notifier_t *notifier;
5310 +
5311 + struct context_list list;
5312 +};
5313 +
5314 +#ifdef DEBUG
5315 +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
5316 +{
5317 + int i;
5318 + DWC_PRINTF("%s: ", name);
5319 + for (i=0; i<len; i++) {
5320 + DWC_PRINTF("%02x ", bytes[i]);
5321 + }
5322 + DWC_PRINTF("\n");
5323 +}
5324 +#else
5325 +#define dump_bytes(x...)
5326 +#endif
5327 +
5328 +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
5329 +{
5330 + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
5331 + if (!cc) {
5332 + return NULL;
5333 + }
5334 + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
5335 +
5336 + if (name) {
5337 + cc->length = length;
5338 + cc->name = dwc_alloc(mem_ctx, length);
5339 + if (!cc->name) {
5340 + dwc_free(mem_ctx, cc);
5341 + return NULL;
5342 + }
5343 +
5344 + DWC_MEMCPY(cc->name, name, length);
5345 + }
5346 +
5347 + return cc;
5348 +}
5349 +
5350 +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
5351 +{
5352 + if (cc->name) {
5353 + dwc_free(mem_ctx, cc->name);
5354 + }
5355 + dwc_free(mem_ctx, cc);
5356 +}
5357 +
5358 +static uint32_t next_uid(dwc_cc_if_t *cc_if)
5359 +{
5360 + uint32_t uid = 0;
5361 + dwc_cc_t *cc;
5362 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5363 + if (cc->uid > uid) {
5364 + uid = cc->uid;
5365 + }
5366 + }
5367 +
5368 + if (uid == 0) {
5369 + uid = 255;
5370 + }
5371 +
5372 + return uid + 1;
5373 +}
5374 +
5375 +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
5376 +{
5377 + dwc_cc_t *cc;
5378 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5379 + if (cc->uid == uid) {
5380 + return cc;
5381 + }
5382 + }
5383 + return NULL;
5384 +}
5385 +
5386 +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
5387 +{
5388 + unsigned int size = 0;
5389 + dwc_cc_t *cc;
5390 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5391 + size += (48 + 1);
5392 + if (cc->name) {
5393 + size += cc->length;
5394 + }
5395 + }
5396 + return size;
5397 +}
5398 +
5399 +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
5400 +{
5401 + uint32_t uid = 0;
5402 + dwc_cc_t *cc;
5403 +
5404 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5405 + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
5406 + uid = cc->uid;
5407 + break;
5408 + }
5409 + }
5410 + return uid;
5411 +}
5412 +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
5413 +{
5414 + uint32_t uid = 0;
5415 + dwc_cc_t *cc;
5416 +
5417 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5418 + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
5419 + uid = cc->uid;
5420 + break;
5421 + }
5422 + }
5423 + return uid;
5424 +}
5425 +
5426 +/* Internal cc_add */
5427 +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
5428 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5429 +{
5430 + dwc_cc_t *cc;
5431 + uint32_t uid;
5432 +
5433 + if (cc_if->is_host) {
5434 + uid = cc_match_cdid(cc_if, cdid);
5435 + }
5436 + else {
5437 + uid = cc_match_chid(cc_if, chid);
5438 + }
5439 +
5440 + if (uid) {
5441 + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
5442 + cc = cc_find(cc_if, uid);
5443 + }
5444 + else {
5445 + cc = alloc_cc(mem_ctx, name, length);
5446 + cc->uid = next_uid(cc_if);
5447 + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
5448 + }
5449 +
5450 + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
5451 + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
5452 + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
5453 +
5454 + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
5455 + dump_bytes("CHID", cc->chid, 16);
5456 + dump_bytes("CDID", cc->cdid, 16);
5457 + dump_bytes("CK", cc->ck, 16);
5458 + return cc->uid;
5459 +}
5460 +
5461 +/* Internal cc_clear */
5462 +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
5463 +{
5464 + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
5465 + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
5466 + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
5467 + free_cc(mem_ctx, cc);
5468 + }
5469 +}
5470 +
5471 +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
5472 + dwc_notifier_t *notifier, unsigned is_host)
5473 +{
5474 + dwc_cc_if_t *cc_if = NULL;
5475 +
5476 + /* Allocate a common_cc_if structure */
5477 + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
5478 +
5479 + if (!cc_if)
5480 + return NULL;
5481 +
5482 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
5483 + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
5484 +#else
5485 + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
5486 +#endif
5487 + if (!cc_if->mutex) {
5488 + dwc_free(mem_ctx, cc_if);
5489 + return NULL;
5490 + }
5491 +
5492 + DWC_CIRCLEQ_INIT(&cc_if->list);
5493 + cc_if->is_host = is_host;
5494 + cc_if->notifier = notifier;
5495 + return cc_if;
5496 +}
5497 +
5498 +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
5499 +{
5500 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
5501 + DWC_MUTEX_FREE(cc_if->mutex);
5502 +#else
5503 + dwc_mutex_free(mtx_ctx, cc_if->mutex);
5504 +#endif
5505 + cc_clear(mem_ctx, cc_if);
5506 + dwc_free(mem_ctx, cc_if);
5507 +}
5508 +
5509 +static void cc_changed(dwc_cc_if_t *cc_if)
5510 +{
5511 + if (cc_if->notifier) {
5512 + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
5513 + }
5514 +}
5515 +
5516 +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
5517 +{
5518 + DWC_MUTEX_LOCK(cc_if->mutex);
5519 + cc_clear(mem_ctx, cc_if);
5520 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5521 + cc_changed(cc_if);
5522 +}
5523 +
5524 +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
5525 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5526 +{
5527 + uint32_t uid;
5528 +
5529 + DWC_MUTEX_LOCK(cc_if->mutex);
5530 + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
5531 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5532 + cc_changed(cc_if);
5533 +
5534 + return uid;
5535 +}
5536 +
5537 +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
5538 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5539 +{
5540 + dwc_cc_t* cc;
5541 +
5542 + DWC_DEBUGC("Change connection context %d", id);
5543 +
5544 + DWC_MUTEX_LOCK(cc_if->mutex);
5545 + cc = cc_find(cc_if, id);
5546 + if (!cc) {
5547 + DWC_ERROR("Uid %d not found in cc list\n", id);
5548 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5549 + return;
5550 + }
5551 +
5552 + if (chid) {
5553 + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
5554 + }
5555 + if (cdid) {
5556 + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
5557 + }
5558 + if (ck) {
5559 + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
5560 + }
5561 +
5562 + if (name) {
5563 + if (cc->name) {
5564 + dwc_free(mem_ctx, cc->name);
5565 + }
5566 + cc->name = dwc_alloc(mem_ctx, length);
5567 + if (!cc->name) {
5568 + DWC_ERROR("Out of memory in dwc_cc_change()\n");
5569 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5570 + return;
5571 + }
5572 + cc->length = length;
5573 + DWC_MEMCPY(cc->name, name, length);
5574 + }
5575 +
5576 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5577 +
5578 + cc_changed(cc_if);
5579 +
5580 + DWC_DEBUGC("Changed connection context id=%d\n", id);
5581 + dump_bytes("New CHID", cc->chid, 16);
5582 + dump_bytes("New CDID", cc->cdid, 16);
5583 + dump_bytes("New CK", cc->ck, 16);
5584 +}
5585 +
5586 +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
5587 +{
5588 + dwc_cc_t *cc;
5589 +
5590 + DWC_DEBUGC("Removing connection context %d", id);
5591 +
5592 + DWC_MUTEX_LOCK(cc_if->mutex);
5593 + cc = cc_find(cc_if, id);
5594 + if (!cc) {
5595 + DWC_ERROR("Uid %d not found in cc list\n", id);
5596 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5597 + return;
5598 + }
5599 +
5600 + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
5601 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5602 + free_cc(mem_ctx, cc);
5603 +
5604 + cc_changed(cc_if);
5605 +}
5606 +
5607 +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
5608 +{
5609 + uint8_t *buf, *x;
5610 + uint8_t zero = 0;
5611 + dwc_cc_t *cc;
5612 +
5613 + DWC_MUTEX_LOCK(cc_if->mutex);
5614 + *length = cc_data_size(cc_if);
5615 + if (!(*length)) {
5616 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5617 + return NULL;
5618 + }
5619 +
5620 + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
5621 +
5622 + buf = dwc_alloc(mem_ctx, *length);
5623 + if (!buf) {
5624 + *length = 0;
5625 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5626 + return NULL;
5627 + }
5628 +
5629 + x = buf;
5630 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5631 + DWC_MEMCPY(x, cc->chid, 16);
5632 + x += 16;
5633 + DWC_MEMCPY(x, cc->cdid, 16);
5634 + x += 16;
5635 + DWC_MEMCPY(x, cc->ck, 16);
5636 + x += 16;
5637 + if (cc->name) {
5638 + DWC_MEMCPY(x, &cc->length, 1);
5639 + x += 1;
5640 + DWC_MEMCPY(x, cc->name, cc->length);
5641 + x += cc->length;
5642 + }
5643 + else {
5644 + DWC_MEMCPY(x, &zero, 1);
5645 + x += 1;
5646 + }
5647 + }
5648 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5649 +
5650 + return buf;
5651 +}
5652 +
5653 +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
5654 +{
5655 + uint8_t name_length;
5656 + uint8_t *name;
5657 + uint8_t *chid;
5658 + uint8_t *cdid;
5659 + uint8_t *ck;
5660 + uint32_t i = 0;
5661 +
5662 + DWC_MUTEX_LOCK(cc_if->mutex);
5663 + cc_clear(mem_ctx, cc_if);
5664 +
5665 + while (i < length) {
5666 + chid = &data[i];
5667 + i += 16;
5668 + cdid = &data[i];
5669 + i += 16;
5670 + ck = &data[i];
5671 + i += 16;
5672 +
5673 + name_length = data[i];
5674 + i ++;
5675 +
5676 + if (name_length) {
5677 + name = &data[i];
5678 + i += name_length;
5679 + }
5680 + else {
5681 + name = NULL;
5682 + }
5683 +
5684 + /* check to see if we haven't overflown the buffer */
5685 + if (i > length) {
5686 + DWC_ERROR("Data format error while attempting to load CCs "
5687 + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
5688 + break;
5689 + }
5690 +
5691 + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
5692 + }
5693 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5694 +
5695 + cc_changed(cc_if);
5696 +}
5697 +
5698 +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
5699 +{
5700 + uint32_t uid = 0;
5701 +
5702 + DWC_MUTEX_LOCK(cc_if->mutex);
5703 + uid = cc_match_chid(cc_if, chid);
5704 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5705 + return uid;
5706 +}
5707 +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
5708 +{
5709 + uint32_t uid = 0;
5710 +
5711 + DWC_MUTEX_LOCK(cc_if->mutex);
5712 + uid = cc_match_cdid(cc_if, cdid);
5713 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5714 + return uid;
5715 +}
5716 +
5717 +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
5718 +{
5719 + uint8_t *ck = NULL;
5720 + dwc_cc_t *cc;
5721 +
5722 + DWC_MUTEX_LOCK(cc_if->mutex);
5723 + cc = cc_find(cc_if, id);
5724 + if (cc) {
5725 + ck = cc->ck;
5726 + }
5727 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5728 +
5729 + return ck;
5730 +
5731 +}
5732 +
5733 +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
5734 +{
5735 + uint8_t *retval = NULL;
5736 + dwc_cc_t *cc;
5737 +
5738 + DWC_MUTEX_LOCK(cc_if->mutex);
5739 + cc = cc_find(cc_if, id);
5740 + if (cc) {
5741 + retval = cc->chid;
5742 + }
5743 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5744 +
5745 + return retval;
5746 +}
5747 +
5748 +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
5749 +{
5750 + uint8_t *retval = NULL;
5751 + dwc_cc_t *cc;
5752 +
5753 + DWC_MUTEX_LOCK(cc_if->mutex);
5754 + cc = cc_find(cc_if, id);
5755 + if (cc) {
5756 + retval = cc->cdid;
5757 + }
5758 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5759 +
5760 + return retval;
5761 +}
5762 +
5763 +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
5764 +{
5765 + uint8_t *retval = NULL;
5766 + dwc_cc_t *cc;
5767 +
5768 + DWC_MUTEX_LOCK(cc_if->mutex);
5769 + *length = 0;
5770 + cc = cc_find(cc_if, id);
5771 + if (cc) {
5772 + *length = cc->length;
5773 + retval = cc->name;
5774 + }
5775 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5776 +
5777 + return retval;
5778 +}
5779 +
5780 +#endif /* DWC_CCLIB */
5781 --- /dev/null
5782 +++ b/drivers/usb/host/dwc_common_port/dwc_cc.h
5783 @@ -0,0 +1,224 @@
5784 +/* =========================================================================
5785 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
5786 + * $Revision: #4 $
5787 + * $Date: 2010/09/28 $
5788 + * $Change: 1596182 $
5789 + *
5790 + * Synopsys Portability Library Software and documentation
5791 + * (hereinafter, "Software") is an Unsupported proprietary work of
5792 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
5793 + * between Synopsys and you.
5794 + *
5795 + * The Software IS NOT an item of Licensed Software or Licensed Product
5796 + * under any End User Software License Agreement or Agreement for
5797 + * Licensed Product with Synopsys or any supplement thereto. You are
5798 + * permitted to use and redistribute this Software in source and binary
5799 + * forms, with or without modification, provided that redistributions
5800 + * of source code must retain this notice. You may not view, use,
5801 + * disclose, copy or distribute this file or any information contained
5802 + * herein except pursuant to this license grant from Synopsys. If you
5803 + * do not agree with this notice, including the disclaimer below, then
5804 + * you are not authorized to use the Software.
5805 + *
5806 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
5807 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5808 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
5809 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
5810 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5811 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5812 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5813 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
5814 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5815 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
5816 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
5817 + * DAMAGE.
5818 + * ========================================================================= */
5819 +#ifndef _DWC_CC_H_
5820 +#define _DWC_CC_H_
5821 +
5822 +#ifdef __cplusplus
5823 +extern "C" {
5824 +#endif
5825 +
5826 +/** @file
5827 + *
5828 + * This file defines the Context Context library.
5829 + *
5830 + * The main data structure is dwc_cc_if_t which is returned by either the
5831 + * dwc_cc_if_alloc function or returned by the module to the user via a provided
5832 + * function. The data structure is opaque and should only be manipulated via the
5833 + * functions provied in this API.
5834 + *
5835 + * It manages a list of connection contexts and operations can be performed to
5836 + * add, remove, query, search, and change, those contexts. Additionally,
5837 + * a dwc_notifier_t object can be requested from the manager so that
5838 + * the user can be notified whenever the context list has changed.
5839 + */
5840 +
5841 +#include "dwc_os.h"
5842 +#include "dwc_list.h"
5843 +#include "dwc_notifier.h"
5844 +
5845 +
5846 +/* Notifications */
5847 +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
5848 +
5849 +struct dwc_cc_if;
5850 +typedef struct dwc_cc_if dwc_cc_if_t;
5851 +
5852 +
5853 +/** @name Connection Context Operations */
5854 +/** @{ */
5855 +
5856 +/** This function allocates memory for a dwc_cc_if_t structure, initializes
5857 + * fields to default values, and returns a pointer to the structure or NULL on
5858 + * error. */
5859 +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
5860 + dwc_notifier_t *notifier, unsigned is_host);
5861 +
5862 +/** Frees the memory for the specified CC structure allocated from
5863 + * dwc_cc_if_alloc(). */
5864 +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
5865 +
5866 +/** Removes all contexts from the connection context list */
5867 +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
5868 +
5869 +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
5870 + * If a CHID already exists, the CK and name are overwritten. Statistics are
5871 + * not overwritten.
5872 + *
5873 + * @param cc_if The cc_if structure.
5874 + * @param chid A pointer to the 16-byte CHID. This value will be copied.
5875 + * @param ck A pointer to the 16-byte CK. This value will be copied.
5876 + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
5877 + * @param name An optional host friendly name as defined in the association model
5878 + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
5879 + * @param length The length othe unicode string.
5880 + * @return A unique identifier used to refer to this context that is valid for
5881 + * as long as this context is still in the list. */
5882 +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
5883 + uint8_t *cdid, uint8_t *ck, uint8_t *name,
5884 + uint8_t length);
5885 +
5886 +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
5887 + * list, preserving any accumulated statistics. This would typically be called
5888 + * if the host decideds to change the context with a SET_CONNECTION request.
5889 + *
5890 + * @param cc_if The cc_if structure.
5891 + * @param id The identifier of the connection context.
5892 + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
5893 + * indicates no change.
5894 + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
5895 + * indicates no change.
5896 + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
5897 + * indicates no change.
5898 + * @param name Host friendly name UTF16-LE. NULL indicates no change.
5899 + * @param length Length of name. */
5900 +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
5901 + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
5902 + uint8_t *name, uint8_t length);
5903 +
5904 +/** Remove the specified connection context.
5905 + * @param cc_if The cc_if structure.
5906 + * @param id The identifier of the connection context to remove. */
5907 +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
5908 +
5909 +/** Get a binary block of data for the connection context list and attributes.
5910 + * This data can be used by the OS specific driver to save the connection
5911 + * context list into non-volatile memory.
5912 + *
5913 + * @param cc_if The cc_if structure.
5914 + * @param length Return the length of the data buffer.
5915 + * @return A pointer to the data buffer. The memory for this buffer should be
5916 + * freed with DWC_FREE() after use. */
5917 +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
5918 + unsigned int *length);
5919 +
5920 +/** Restore the connection context list from the binary data that was previously
5921 + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
5922 + * driver to load a connection context list from non-volatile memory.
5923 + *
5924 + * @param cc_if The cc_if structure.
5925 + * @param data The data bytes as returned from dwc_cc_data_for_save.
5926 + * @param length The length of the data. */
5927 +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
5928 + uint8_t *data, unsigned int length);
5929 +
5930 +/** Find the connection context from the specified CHID.
5931 + *
5932 + * @param cc_if The cc_if structure.
5933 + * @param chid A pointer to the CHID data.
5934 + * @return A non-zero identifier of the connection context if the CHID matches.
5935 + * Otherwise returns 0. */
5936 +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
5937 +
5938 +/** Find the connection context from the specified CDID.
5939 + *
5940 + * @param cc_if The cc_if structure.
5941 + * @param cdid A pointer to the CDID data.
5942 + * @return A non-zero identifier of the connection context if the CHID matches.
5943 + * Otherwise returns 0. */
5944 +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
5945 +
5946 +/** Retrieve the CK from the specified connection context.
5947 + *
5948 + * @param cc_if The cc_if structure.
5949 + * @param id The identifier of the connection context.
5950 + * @return A pointer to the CK data. The memory does not need to be freed. */
5951 +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
5952 +
5953 +/** Retrieve the CHID from the specified connection context.
5954 + *
5955 + * @param cc_if The cc_if structure.
5956 + * @param id The identifier of the connection context.
5957 + * @return A pointer to the CHID data. The memory does not need to be freed. */
5958 +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
5959 +
5960 +/** Retrieve the CDID from the specified connection context.
5961 + *
5962 + * @param cc_if The cc_if structure.
5963 + * @param id The identifier of the connection context.
5964 + * @return A pointer to the CDID data. The memory does not need to be freed. */
5965 +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
5966 +
5967 +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
5968 +
5969 +/** Checks a buffer for non-zero.
5970 + * @param id A pointer to a 16 byte buffer.
5971 + * @return true if the 16 byte value is non-zero. */
5972 +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
5973 + int i;
5974 + for (i=0; i<16; i++) {
5975 + if (id[i]) return 1;
5976 + }
5977 + return 0;
5978 +}
5979 +
5980 +/** Checks a buffer for zero.
5981 + * @param id A pointer to a 16 byte buffer.
5982 + * @return true if the 16 byte value is zero. */
5983 +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
5984 + return !dwc_assoc_is_not_zero_id(id);
5985 +}
5986 +
5987 +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
5988 + * buffer. */
5989 +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
5990 + char *ptr = buffer;
5991 + int i;
5992 + for (i=0; i<16; i++) {
5993 + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
5994 + if (i < 15) {
5995 + ptr += DWC_SPRINTF(ptr, " ");
5996 + }
5997 + }
5998 + return ptr - buffer;
5999 +}
6000 +
6001 +/** @} */
6002 +
6003 +#ifdef __cplusplus
6004 +}
6005 +#endif
6006 +
6007 +#endif /* _DWC_CC_H_ */
6008 --- /dev/null
6009 +++ b/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
6010 @@ -0,0 +1,1308 @@
6011 +#include "dwc_os.h"
6012 +#include "dwc_list.h"
6013 +
6014 +#ifdef DWC_CCLIB
6015 +# include "dwc_cc.h"
6016 +#endif
6017 +
6018 +#ifdef DWC_CRYPTOLIB
6019 +# include "dwc_modpow.h"
6020 +# include "dwc_dh.h"
6021 +# include "dwc_crypto.h"
6022 +#endif
6023 +
6024 +#ifdef DWC_NOTIFYLIB
6025 +# include "dwc_notifier.h"
6026 +#endif
6027 +
6028 +/* OS-Level Implementations */
6029 +
6030 +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
6031 +
6032 +
6033 +/* MISC */
6034 +
6035 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
6036 +{
6037 + return memset(dest, byte, size);
6038 +}
6039 +
6040 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
6041 +{
6042 + return memcpy(dest, src, size);
6043 +}
6044 +
6045 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
6046 +{
6047 + bcopy(src, dest, size);
6048 + return dest;
6049 +}
6050 +
6051 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
6052 +{
6053 + return memcmp(m1, m2, size);
6054 +}
6055 +
6056 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
6057 +{
6058 + return strncmp(s1, s2, size);
6059 +}
6060 +
6061 +int DWC_STRCMP(void *s1, void *s2)
6062 +{
6063 + return strcmp(s1, s2);
6064 +}
6065 +
6066 +int DWC_STRLEN(char const *str)
6067 +{
6068 + return strlen(str);
6069 +}
6070 +
6071 +char *DWC_STRCPY(char *to, char const *from)
6072 +{
6073 + return strcpy(to, from);
6074 +}
6075 +
6076 +char *DWC_STRDUP(char const *str)
6077 +{
6078 + int len = DWC_STRLEN(str) + 1;
6079 + char *new = DWC_ALLOC_ATOMIC(len);
6080 +
6081 + if (!new) {
6082 + return NULL;
6083 + }
6084 +
6085 + DWC_MEMCPY(new, str, len);
6086 + return new;
6087 +}
6088 +
6089 +int DWC_ATOI(char *str, int32_t *value)
6090 +{
6091 + char *end = NULL;
6092 +
6093 + *value = strtol(str, &end, 0);
6094 + if (*end == '\0') {
6095 + return 0;
6096 + }
6097 +
6098 + return -1;
6099 +}
6100 +
6101 +int DWC_ATOUI(char *str, uint32_t *value)
6102 +{
6103 + char *end = NULL;
6104 +
6105 + *value = strtoul(str, &end, 0);
6106 + if (*end == '\0') {
6107 + return 0;
6108 + }
6109 +
6110 + return -1;
6111 +}
6112 +
6113 +
6114 +#ifdef DWC_UTFLIB
6115 +/* From usbstring.c */
6116 +
6117 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
6118 +{
6119 + int count = 0;
6120 + u8 c;
6121 + u16 uchar;
6122 +
6123 + /* this insists on correct encodings, though not minimal ones.
6124 + * BUT it currently rejects legit 4-byte UTF-8 code points,
6125 + * which need surrogate pairs. (Unicode 3.1 can use them.)
6126 + */
6127 + while (len != 0 && (c = (u8) *s++) != 0) {
6128 + if (unlikely(c & 0x80)) {
6129 + // 2-byte sequence:
6130 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
6131 + if ((c & 0xe0) == 0xc0) {
6132 + uchar = (c & 0x1f) << 6;
6133 +
6134 + c = (u8) *s++;
6135 + if ((c & 0xc0) != 0xc0)
6136 + goto fail;
6137 + c &= 0x3f;
6138 + uchar |= c;
6139 +
6140 + // 3-byte sequence (most CJKV characters):
6141 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
6142 + } else if ((c & 0xf0) == 0xe0) {
6143 + uchar = (c & 0x0f) << 12;
6144 +
6145 + c = (u8) *s++;
6146 + if ((c & 0xc0) != 0xc0)
6147 + goto fail;
6148 + c &= 0x3f;
6149 + uchar |= c << 6;
6150 +
6151 + c = (u8) *s++;
6152 + if ((c & 0xc0) != 0xc0)
6153 + goto fail;
6154 + c &= 0x3f;
6155 + uchar |= c;
6156 +
6157 + /* no bogus surrogates */
6158 + if (0xd800 <= uchar && uchar <= 0xdfff)
6159 + goto fail;
6160 +
6161 + // 4-byte sequence (surrogate pairs, currently rare):
6162 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
6163 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
6164 + // (uuuuu = wwww + 1)
6165 + // FIXME accept the surrogate code points (only)
6166 + } else
6167 + goto fail;
6168 + } else
6169 + uchar = c;
6170 + put_unaligned (cpu_to_le16 (uchar), cp++);
6171 + count++;
6172 + len--;
6173 + }
6174 + return count;
6175 +fail:
6176 + return -1;
6177 +}
6178 +
6179 +#endif /* DWC_UTFLIB */
6180 +
6181 +
6182 +/* dwc_debug.h */
6183 +
6184 +dwc_bool_t DWC_IN_IRQ(void)
6185 +{
6186 +// return in_irq();
6187 + return 0;
6188 +}
6189 +
6190 +dwc_bool_t DWC_IN_BH(void)
6191 +{
6192 +// return in_softirq();
6193 + return 0;
6194 +}
6195 +
6196 +void DWC_VPRINTF(char *format, va_list args)
6197 +{
6198 + vprintf(format, args);
6199 +}
6200 +
6201 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
6202 +{
6203 + return vsnprintf(str, size, format, args);
6204 +}
6205 +
6206 +void DWC_PRINTF(char *format, ...)
6207 +{
6208 + va_list args;
6209 +
6210 + va_start(args, format);
6211 + DWC_VPRINTF(format, args);
6212 + va_end(args);
6213 +}
6214 +
6215 +int DWC_SPRINTF(char *buffer, char *format, ...)
6216 +{
6217 + int retval;
6218 + va_list args;
6219 +
6220 + va_start(args, format);
6221 + retval = vsprintf(buffer, format, args);
6222 + va_end(args);
6223 + return retval;
6224 +}
6225 +
6226 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
6227 +{
6228 + int retval;
6229 + va_list args;
6230 +
6231 + va_start(args, format);
6232 + retval = vsnprintf(buffer, size, format, args);
6233 + va_end(args);
6234 + return retval;
6235 +}
6236 +
6237 +void __DWC_WARN(char *format, ...)
6238 +{
6239 + va_list args;
6240 +
6241 + va_start(args, format);
6242 + DWC_VPRINTF(format, args);
6243 + va_end(args);
6244 +}
6245 +
6246 +void __DWC_ERROR(char *format, ...)
6247 +{
6248 + va_list args;
6249 +
6250 + va_start(args, format);
6251 + DWC_VPRINTF(format, args);
6252 + va_end(args);
6253 +}
6254 +
6255 +void DWC_EXCEPTION(char *format, ...)
6256 +{
6257 + va_list args;
6258 +
6259 + va_start(args, format);
6260 + DWC_VPRINTF(format, args);
6261 + va_end(args);
6262 +// BUG_ON(1); ???
6263 +}
6264 +
6265 +#ifdef DEBUG
6266 +void __DWC_DEBUG(char *format, ...)
6267 +{
6268 + va_list args;
6269 +
6270 + va_start(args, format);
6271 + DWC_VPRINTF(format, args);
6272 + va_end(args);
6273 +}
6274 +#endif
6275 +
6276 +
6277 +/* dwc_mem.h */
6278 +
6279 +#if 0
6280 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
6281 + uint32_t align,
6282 + uint32_t alloc)
6283 +{
6284 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
6285 + size, align, alloc);
6286 + return (dwc_pool_t *)pool;
6287 +}
6288 +
6289 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
6290 +{
6291 + dma_pool_destroy((struct dma_pool *)pool);
6292 +}
6293 +
6294 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
6295 +{
6296 +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
6297 + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
6298 +}
6299 +
6300 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
6301 +{
6302 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
6303 + memset(..);
6304 +}
6305 +
6306 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
6307 +{
6308 + dma_pool_free(pool, vaddr, daddr);
6309 +}
6310 +#endif
6311 +
6312 +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
6313 +{
6314 + if (error)
6315 + return;
6316 + *(bus_addr_t *)arg = segs[0].ds_addr;
6317 +}
6318 +
6319 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
6320 +{
6321 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
6322 + int error;
6323 +
6324 + error = bus_dma_tag_create(
6325 +#if __FreeBSD_version >= 700000
6326 + bus_get_dma_tag(dma->dev), /* parent */
6327 +#else
6328 + NULL, /* parent */
6329 +#endif
6330 + 4, 0, /* alignment, bounds */
6331 + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
6332 + BUS_SPACE_MAXADDR, /* highaddr */
6333 + NULL, NULL, /* filter, filterarg */
6334 + size, /* maxsize */
6335 + 1, /* nsegments */
6336 + size, /* maxsegsize */
6337 + 0, /* flags */
6338 + NULL, /* lockfunc */
6339 + NULL, /* lockarg */
6340 + &dma->dma_tag);
6341 + if (error) {
6342 + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
6343 + __func__, error);
6344 + goto fail_0;
6345 + }
6346 +
6347 + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
6348 + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
6349 + if (error) {
6350 + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
6351 + __func__, (uintmax_t)size, error);
6352 + goto fail_1;
6353 + }
6354 +
6355 + dma->dma_paddr = 0;
6356 + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
6357 + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
6358 + if (error || dma->dma_paddr == 0) {
6359 + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
6360 + __func__, error);
6361 + goto fail_2;
6362 + }
6363 +
6364 + *dma_addr = dma->dma_paddr;
6365 + return dma->dma_vaddr;
6366 +
6367 +fail_2:
6368 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
6369 +fail_1:
6370 + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
6371 + bus_dma_tag_destroy(dma->dma_tag);
6372 +fail_0:
6373 + dma->dma_map = NULL;
6374 + dma->dma_tag = NULL;
6375 +
6376 + return NULL;
6377 +}
6378 +
6379 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
6380 +{
6381 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
6382 +
6383 + if (dma->dma_tag == NULL)
6384 + return;
6385 + if (dma->dma_map != NULL) {
6386 + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
6387 + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6388 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
6389 + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
6390 + dma->dma_map = NULL;
6391 + }
6392 +
6393 + bus_dma_tag_destroy(dma->dma_tag);
6394 + dma->dma_tag = NULL;
6395 +}
6396 +
6397 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
6398 +{
6399 + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
6400 +}
6401 +
6402 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
6403 +{
6404 + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6405 +}
6406 +
6407 +void __DWC_FREE(void *mem_ctx, void *addr)
6408 +{
6409 + free(addr, M_DEVBUF);
6410 +}
6411 +
6412 +
6413 +#ifdef DWC_CRYPTOLIB
6414 +/* dwc_crypto.h */
6415 +
6416 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
6417 +{
6418 + get_random_bytes(buffer, length);
6419 +}
6420 +
6421 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
6422 +{
6423 + struct crypto_blkcipher *tfm;
6424 + struct blkcipher_desc desc;
6425 + struct scatterlist sgd;
6426 + struct scatterlist sgs;
6427 +
6428 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
6429 + if (tfm == NULL) {
6430 + printk("failed to load transform for aes CBC\n");
6431 + return -1;
6432 + }
6433 +
6434 + crypto_blkcipher_setkey(tfm, key, keylen);
6435 + crypto_blkcipher_set_iv(tfm, iv, 16);
6436 +
6437 + sg_init_one(&sgd, out, messagelen);
6438 + sg_init_one(&sgs, message, messagelen);
6439 +
6440 + desc.tfm = tfm;
6441 + desc.flags = 0;
6442 +
6443 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
6444 + crypto_free_blkcipher(tfm);
6445 + DWC_ERROR("AES CBC encryption failed");
6446 + return -1;
6447 + }
6448 +
6449 + crypto_free_blkcipher(tfm);
6450 + return 0;
6451 +}
6452 +
6453 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
6454 +{
6455 + struct crypto_hash *tfm;
6456 + struct hash_desc desc;
6457 + struct scatterlist sg;
6458 +
6459 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
6460 + if (IS_ERR(tfm)) {
6461 + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
6462 + return 0;
6463 + }
6464 + desc.tfm = tfm;
6465 + desc.flags = 0;
6466 +
6467 + sg_init_one(&sg, message, len);
6468 + crypto_hash_digest(&desc, &sg, len, out);
6469 + crypto_free_hash(tfm);
6470 +
6471 + return 1;
6472 +}
6473 +
6474 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
6475 + uint8_t *key, uint32_t keylen, uint8_t *out)
6476 +{
6477 + struct crypto_hash *tfm;
6478 + struct hash_desc desc;
6479 + struct scatterlist sg;
6480 +
6481 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
6482 + if (IS_ERR(tfm)) {
6483 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
6484 + return 0;
6485 + }
6486 + desc.tfm = tfm;
6487 + desc.flags = 0;
6488 +
6489 + sg_init_one(&sg, message, messagelen);
6490 + crypto_hash_setkey(tfm, key, keylen);
6491 + crypto_hash_digest(&desc, &sg, messagelen, out);
6492 + crypto_free_hash(tfm);
6493 +
6494 + return 1;
6495 +}
6496 +
6497 +#endif /* DWC_CRYPTOLIB */
6498 +
6499 +
6500 +/* Byte Ordering Conversions */
6501 +
6502 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
6503 +{
6504 +#ifdef __LITTLE_ENDIAN
6505 + return *p;
6506 +#else
6507 + uint8_t *u_p = (uint8_t *)p;
6508 +
6509 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6510 +#endif
6511 +}
6512 +
6513 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
6514 +{
6515 +#ifdef __BIG_ENDIAN
6516 + return *p;
6517 +#else
6518 + uint8_t *u_p = (uint8_t *)p;
6519 +
6520 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6521 +#endif
6522 +}
6523 +
6524 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
6525 +{
6526 +#ifdef __LITTLE_ENDIAN
6527 + return *p;
6528 +#else
6529 + uint8_t *u_p = (uint8_t *)p;
6530 +
6531 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6532 +#endif
6533 +}
6534 +
6535 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
6536 +{
6537 +#ifdef __BIG_ENDIAN
6538 + return *p;
6539 +#else
6540 + uint8_t *u_p = (uint8_t *)p;
6541 +
6542 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6543 +#endif
6544 +}
6545 +
6546 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
6547 +{
6548 +#ifdef __LITTLE_ENDIAN
6549 + return *p;
6550 +#else
6551 + uint8_t *u_p = (uint8_t *)p;
6552 + return (u_p[1] | (u_p[0] << 8));
6553 +#endif
6554 +}
6555 +
6556 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
6557 +{
6558 +#ifdef __BIG_ENDIAN
6559 + return *p;
6560 +#else
6561 + uint8_t *u_p = (uint8_t *)p;
6562 + return (u_p[1] | (u_p[0] << 8));
6563 +#endif
6564 +}
6565 +
6566 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
6567 +{
6568 +#ifdef __LITTLE_ENDIAN
6569 + return *p;
6570 +#else
6571 + uint8_t *u_p = (uint8_t *)p;
6572 + return (u_p[1] | (u_p[0] << 8));
6573 +#endif
6574 +}
6575 +
6576 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
6577 +{
6578 +#ifdef __BIG_ENDIAN
6579 + return *p;
6580 +#else
6581 + uint8_t *u_p = (uint8_t *)p;
6582 + return (u_p[1] | (u_p[0] << 8));
6583 +#endif
6584 +}
6585 +
6586 +
6587 +/* Registers */
6588 +
6589 +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
6590 +{
6591 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6592 + bus_size_t ior = (bus_size_t)reg;
6593 +
6594 + return bus_space_read_4(io->iot, io->ioh, ior);
6595 +}
6596 +
6597 +#if 0
6598 +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
6599 +{
6600 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6601 + bus_size_t ior = (bus_size_t)reg;
6602 +
6603 + return bus_space_read_8(io->iot, io->ioh, ior);
6604 +}
6605 +#endif
6606 +
6607 +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
6608 +{
6609 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6610 + bus_size_t ior = (bus_size_t)reg;
6611 +
6612 + bus_space_write_4(io->iot, io->ioh, ior, value);
6613 +}
6614 +
6615 +#if 0
6616 +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
6617 +{
6618 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6619 + bus_size_t ior = (bus_size_t)reg;
6620 +
6621 + bus_space_write_8(io->iot, io->ioh, ior, value);
6622 +}
6623 +#endif
6624 +
6625 +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
6626 + uint32_t set_mask)
6627 +{
6628 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6629 + bus_size_t ior = (bus_size_t)reg;
6630 +
6631 + bus_space_write_4(io->iot, io->ioh, ior,
6632 + (bus_space_read_4(io->iot, io->ioh, ior) &
6633 + ~clear_mask) | set_mask);
6634 +}
6635 +
6636 +#if 0
6637 +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
6638 + uint64_t set_mask)
6639 +{
6640 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6641 + bus_size_t ior = (bus_size_t)reg;
6642 +
6643 + bus_space_write_8(io->iot, io->ioh, ior,
6644 + (bus_space_read_8(io->iot, io->ioh, ior) &
6645 + ~clear_mask) | set_mask);
6646 +}
6647 +#endif
6648 +
6649 +
6650 +/* Locking */
6651 +
6652 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
6653 +{
6654 + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
6655 +
6656 + if (!sl) {
6657 + DWC_ERROR("Cannot allocate memory for spinlock");
6658 + return NULL;
6659 + }
6660 +
6661 + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
6662 + return (dwc_spinlock_t *)sl;
6663 +}
6664 +
6665 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
6666 +{
6667 + struct mtx *sl = (struct mtx *)lock;
6668 +
6669 + mtx_destroy(sl);
6670 + DWC_FREE(sl);
6671 +}
6672 +
6673 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
6674 +{
6675 + mtx_lock_spin((struct mtx *)lock); // ???
6676 +}
6677 +
6678 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
6679 +{
6680 + mtx_unlock_spin((struct mtx *)lock); // ???
6681 +}
6682 +
6683 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
6684 +{
6685 + mtx_lock_spin((struct mtx *)lock);
6686 +}
6687 +
6688 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
6689 +{
6690 + mtx_unlock_spin((struct mtx *)lock);
6691 +}
6692 +
6693 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
6694 +{
6695 + struct mtx *m;
6696 + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
6697 +
6698 + if (!mutex) {
6699 + DWC_ERROR("Cannot allocate memory for mutex");
6700 + return NULL;
6701 + }
6702 +
6703 + m = (struct mtx *)mutex;
6704 + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
6705 + return mutex;
6706 +}
6707 +
6708 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
6709 +#else
6710 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
6711 +{
6712 + mtx_destroy((struct mtx *)mutex);
6713 + DWC_FREE(mutex);
6714 +}
6715 +#endif
6716 +
6717 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
6718 +{
6719 + struct mtx *m = (struct mtx *)mutex;
6720 +
6721 + mtx_lock(m);
6722 +}
6723 +
6724 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
6725 +{
6726 + struct mtx *m = (struct mtx *)mutex;
6727 +
6728 + return mtx_trylock(m);
6729 +}
6730 +
6731 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
6732 +{
6733 + struct mtx *m = (struct mtx *)mutex;
6734 +
6735 + mtx_unlock(m);
6736 +}
6737 +
6738 +
6739 +/* Timing */
6740 +
6741 +void DWC_UDELAY(uint32_t usecs)
6742 +{
6743 + DELAY(usecs);
6744 +}
6745 +
6746 +void DWC_MDELAY(uint32_t msecs)
6747 +{
6748 + do {
6749 + DELAY(1000);
6750 + } while (--msecs);
6751 +}
6752 +
6753 +void DWC_MSLEEP(uint32_t msecs)
6754 +{
6755 + struct timeval tv;
6756 +
6757 + tv.tv_sec = msecs / 1000;
6758 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
6759 + pause("dw3slp", tvtohz(&tv));
6760 +}
6761 +
6762 +uint32_t DWC_TIME(void)
6763 +{
6764 + struct timeval tv;
6765 +
6766 + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
6767 + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
6768 +}
6769 +
6770 +
6771 +/* Timers */
6772 +
6773 +struct dwc_timer {
6774 + struct callout t;
6775 + char *name;
6776 + dwc_spinlock_t *lock;
6777 + dwc_timer_callback_t cb;
6778 + void *data;
6779 +};
6780 +
6781 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
6782 +{
6783 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
6784 +
6785 + if (!t) {
6786 + DWC_ERROR("Cannot allocate memory for timer");
6787 + return NULL;
6788 + }
6789 +
6790 + callout_init(&t->t, 1);
6791 +
6792 + t->name = DWC_STRDUP(name);
6793 + if (!t->name) {
6794 + DWC_ERROR("Cannot allocate memory for timer->name");
6795 + goto no_name;
6796 + }
6797 +
6798 + t->lock = DWC_SPINLOCK_ALLOC();
6799 + if (!t->lock) {
6800 + DWC_ERROR("Cannot allocate memory for lock");
6801 + goto no_lock;
6802 + }
6803 +
6804 + t->cb = cb;
6805 + t->data = data;
6806 +
6807 + return t;
6808 +
6809 + no_lock:
6810 + DWC_FREE(t->name);
6811 + no_name:
6812 + DWC_FREE(t);
6813 +
6814 + return NULL;
6815 +}
6816 +
6817 +void DWC_TIMER_FREE(dwc_timer_t *timer)
6818 +{
6819 + callout_stop(&timer->t);
6820 + DWC_SPINLOCK_FREE(timer->lock);
6821 + DWC_FREE(timer->name);
6822 + DWC_FREE(timer);
6823 +}
6824 +
6825 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
6826 +{
6827 + struct timeval tv;
6828 +
6829 + tv.tv_sec = time / 1000;
6830 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
6831 + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
6832 +}
6833 +
6834 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
6835 +{
6836 + callout_stop(&timer->t);
6837 +}
6838 +
6839 +
6840 +/* Wait Queues */
6841 +
6842 +struct dwc_waitq {
6843 + struct mtx lock;
6844 + int abort;
6845 +};
6846 +
6847 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
6848 +{
6849 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
6850 +
6851 + if (!wq) {
6852 + DWC_ERROR("Cannot allocate memory for waitqueue");
6853 + return NULL;
6854 + }
6855 +
6856 + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
6857 + wq->abort = 0;
6858 +
6859 + return wq;
6860 +}
6861 +
6862 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
6863 +{
6864 + mtx_destroy(&wq->lock);
6865 + DWC_FREE(wq);
6866 +}
6867 +
6868 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
6869 +{
6870 +// intrmask_t ipl;
6871 + int result = 0;
6872 +
6873 + mtx_lock(&wq->lock);
6874 +// ipl = splbio();
6875 +
6876 + /* Skip the sleep if already aborted or triggered */
6877 + if (!wq->abort && !cond(data)) {
6878 +// splx(ipl);
6879 + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
6880 +// ipl = splbio();
6881 + }
6882 +
6883 + if (result == ERESTART) { // signaled - restart
6884 + result = -DWC_E_RESTART;
6885 +
6886 + } else if (result == EINTR) { // signaled - interrupt
6887 + result = -DWC_E_ABORT;
6888 +
6889 + } else if (wq->abort) {
6890 + result = -DWC_E_ABORT;
6891 +
6892 + } else {
6893 + result = 0;
6894 + }
6895 +
6896 + wq->abort = 0;
6897 +// splx(ipl);
6898 + mtx_unlock(&wq->lock);
6899 + return result;
6900 +}
6901 +
6902 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
6903 + void *data, int32_t msecs)
6904 +{
6905 + struct timeval tv, tv1, tv2;
6906 +// intrmask_t ipl;
6907 + int result = 0;
6908 +
6909 + tv.tv_sec = msecs / 1000;
6910 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
6911 +
6912 + mtx_lock(&wq->lock);
6913 +// ipl = splbio();
6914 +
6915 + /* Skip the sleep if already aborted or triggered */
6916 + if (!wq->abort && !cond(data)) {
6917 +// splx(ipl);
6918 + getmicrouptime(&tv1);
6919 + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
6920 + getmicrouptime(&tv2);
6921 +// ipl = splbio();
6922 + }
6923 +
6924 + if (result == 0) { // awoken
6925 + if (wq->abort) {
6926 + result = -DWC_E_ABORT;
6927 + } else {
6928 + tv2.tv_usec -= tv1.tv_usec;
6929 + if (tv2.tv_usec < 0) {
6930 + tv2.tv_usec += 1000000;
6931 + tv2.tv_sec--;
6932 + }
6933 +
6934 + tv2.tv_sec -= tv1.tv_sec;
6935 + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
6936 + result = msecs - result;
6937 + if (result <= 0)
6938 + result = 1;
6939 + }
6940 + } else if (result == ERESTART) { // signaled - restart
6941 + result = -DWC_E_RESTART;
6942 +
6943 + } else if (result == EINTR) { // signaled - interrupt
6944 + result = -DWC_E_ABORT;
6945 +
6946 + } else { // timed out
6947 + result = -DWC_E_TIMEOUT;
6948 + }
6949 +
6950 + wq->abort = 0;
6951 +// splx(ipl);
6952 + mtx_unlock(&wq->lock);
6953 + return result;
6954 +}
6955 +
6956 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
6957 +{
6958 + wakeup(wq);
6959 +}
6960 +
6961 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
6962 +{
6963 +// intrmask_t ipl;
6964 +
6965 + mtx_lock(&wq->lock);
6966 +// ipl = splbio();
6967 + wq->abort = 1;
6968 + wakeup(wq);
6969 +// splx(ipl);
6970 + mtx_unlock(&wq->lock);
6971 +}
6972 +
6973 +
6974 +/* Threading */
6975 +
6976 +struct dwc_thread {
6977 + struct proc *proc;
6978 + int abort;
6979 +};
6980 +
6981 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
6982 +{
6983 + int retval;
6984 + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
6985 +
6986 + if (!thread) {
6987 + return NULL;
6988 + }
6989 +
6990 + thread->abort = 0;
6991 + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
6992 + RFPROC | RFNOWAIT, 0, "%s", name);
6993 + if (retval) {
6994 + DWC_FREE(thread);
6995 + return NULL;
6996 + }
6997 +
6998 + return thread;
6999 +}
7000 +
7001 +int DWC_THREAD_STOP(dwc_thread_t *thread)
7002 +{
7003 + int retval;
7004 +
7005 + thread->abort = 1;
7006 + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
7007 +
7008 + if (retval == 0) {
7009 + /* DWC_THREAD_EXIT() will free the thread struct */
7010 + return 0;
7011 + }
7012 +
7013 + /* NOTE: We leak the thread struct if thread doesn't die */
7014 +
7015 + if (retval == EWOULDBLOCK) {
7016 + return -DWC_E_TIMEOUT;
7017 + }
7018 +
7019 + return -DWC_E_UNKNOWN;
7020 +}
7021 +
7022 +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
7023 +{
7024 + return thread->abort;
7025 +}
7026 +
7027 +void DWC_THREAD_EXIT(dwc_thread_t *thread)
7028 +{
7029 + wakeup(&thread->abort);
7030 + DWC_FREE(thread);
7031 + kthread_exit(0);
7032 +}
7033 +
7034 +
7035 +/* tasklets
7036 + - Runs in interrupt context (cannot sleep)
7037 + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
7038 + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
7039 + */
7040 +struct dwc_tasklet {
7041 + struct task t;
7042 + dwc_tasklet_callback_t cb;
7043 + void *data;
7044 +};
7045 +
7046 +static void tasklet_callback(void *data, int pending) // what to do with pending ???
7047 +{
7048 + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
7049 +
7050 + task->cb(task->data);
7051 +}
7052 +
7053 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
7054 +{
7055 + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
7056 +
7057 + if (task) {
7058 + task->cb = cb;
7059 + task->data = data;
7060 + TASK_INIT(&task->t, 0, tasklet_callback, task);
7061 + } else {
7062 + DWC_ERROR("Cannot allocate memory for tasklet");
7063 + }
7064 +
7065 + return task;
7066 +}
7067 +
7068 +void DWC_TASK_FREE(dwc_tasklet_t *task)
7069 +{
7070 + taskqueue_drain(taskqueue_fast, &task->t); // ???
7071 + DWC_FREE(task);
7072 +}
7073 +
7074 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
7075 +{
7076 + /* Uses predefined system queue */
7077 + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
7078 +}
7079 +
7080 +
7081 +/* workqueues
7082 + - Runs in process context (can sleep)
7083 + */
7084 +typedef struct work_container {
7085 + dwc_work_callback_t cb;
7086 + void *data;
7087 + dwc_workq_t *wq;
7088 + char *name;
7089 + int hz;
7090 +
7091 +#ifdef DEBUG
7092 + DWC_CIRCLEQ_ENTRY(work_container) entry;
7093 +#endif
7094 + struct task task;
7095 +} work_container_t;
7096 +
7097 +#ifdef DEBUG
7098 +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
7099 +#endif
7100 +
7101 +struct dwc_workq {
7102 + struct taskqueue *taskq;
7103 + dwc_spinlock_t *lock;
7104 + dwc_waitq_t *waitq;
7105 + int pending;
7106 +
7107 +#ifdef DEBUG
7108 + struct work_container_queue entries;
7109 +#endif
7110 +};
7111 +
7112 +static void do_work(void *data, int pending) // what to do with pending ???
7113 +{
7114 + work_container_t *container = (work_container_t *)data;
7115 + dwc_workq_t *wq = container->wq;
7116 + dwc_irqflags_t flags;
7117 +
7118 + if (container->hz) {
7119 + pause("dw3wrk", container->hz);
7120 + }
7121 +
7122 + container->cb(container->data);
7123 + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
7124 +
7125 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7126 +
7127 +#ifdef DEBUG
7128 + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
7129 +#endif
7130 + if (container->name)
7131 + DWC_FREE(container->name);
7132 + DWC_FREE(container);
7133 + wq->pending--;
7134 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7135 + DWC_WAITQ_TRIGGER(wq->waitq);
7136 +}
7137 +
7138 +static int work_done(void *data)
7139 +{
7140 + dwc_workq_t *workq = (dwc_workq_t *)data;
7141 +
7142 + return workq->pending == 0;
7143 +}
7144 +
7145 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
7146 +{
7147 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
7148 +}
7149 +
7150 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
7151 +{
7152 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
7153 +
7154 + if (!wq) {
7155 + DWC_ERROR("Cannot allocate memory for workqueue");
7156 + return NULL;
7157 + }
7158 +
7159 + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
7160 + if (!wq->taskq) {
7161 + DWC_ERROR("Cannot allocate memory for taskqueue");
7162 + goto no_taskq;
7163 + }
7164 +
7165 + wq->pending = 0;
7166 +
7167 + wq->lock = DWC_SPINLOCK_ALLOC();
7168 + if (!wq->lock) {
7169 + DWC_ERROR("Cannot allocate memory for spinlock");
7170 + goto no_lock;
7171 + }
7172 +
7173 + wq->waitq = DWC_WAITQ_ALLOC();
7174 + if (!wq->waitq) {
7175 + DWC_ERROR("Cannot allocate memory for waitqueue");
7176 + goto no_waitq;
7177 + }
7178 +
7179 + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
7180 +
7181 +#ifdef DEBUG
7182 + DWC_CIRCLEQ_INIT(&wq->entries);
7183 +#endif
7184 + return wq;
7185 +
7186 + no_waitq:
7187 + DWC_SPINLOCK_FREE(wq->lock);
7188 + no_lock:
7189 + taskqueue_free(wq->taskq);
7190 + no_taskq:
7191 + DWC_FREE(wq);
7192 +
7193 + return NULL;
7194 +}
7195 +
7196 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
7197 +{
7198 +#ifdef DEBUG
7199 + dwc_irqflags_t flags;
7200 +
7201 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7202 +
7203 + if (wq->pending != 0) {
7204 + struct work_container *container;
7205 +
7206 + DWC_ERROR("Destroying work queue with pending work");
7207 +
7208 + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
7209 + DWC_ERROR("Work %s still pending", container->name);
7210 + }
7211 + }
7212 +
7213 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7214 +#endif
7215 + DWC_WAITQ_FREE(wq->waitq);
7216 + DWC_SPINLOCK_FREE(wq->lock);
7217 + taskqueue_free(wq->taskq);
7218 + DWC_FREE(wq);
7219 +}
7220 +
7221 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
7222 + char *format, ...)
7223 +{
7224 + dwc_irqflags_t flags;
7225 + work_container_t *container;
7226 + static char name[128];
7227 + va_list args;
7228 +
7229 + va_start(args, format);
7230 + DWC_VSNPRINTF(name, 128, format, args);
7231 + va_end(args);
7232 +
7233 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7234 + wq->pending++;
7235 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7236 + DWC_WAITQ_TRIGGER(wq->waitq);
7237 +
7238 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
7239 + if (!container) {
7240 + DWC_ERROR("Cannot allocate memory for container");
7241 + return;
7242 + }
7243 +
7244 + container->name = DWC_STRDUP(name);
7245 + if (!container->name) {
7246 + DWC_ERROR("Cannot allocate memory for container->name");
7247 + DWC_FREE(container);
7248 + return;
7249 + }
7250 +
7251 + container->cb = cb;
7252 + container->data = data;
7253 + container->wq = wq;
7254 + container->hz = 0;
7255 +
7256 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
7257 +
7258 + TASK_INIT(&container->task, 0, do_work, container);
7259 +
7260 +#ifdef DEBUG
7261 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
7262 +#endif
7263 + taskqueue_enqueue_fast(wq->taskq, &container->task);
7264 +}
7265 +
7266 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
7267 + void *data, uint32_t time, char *format, ...)
7268 +{
7269 + dwc_irqflags_t flags;
7270 + work_container_t *container;
7271 + static char name[128];
7272 + struct timeval tv;
7273 + va_list args;
7274 +
7275 + va_start(args, format);
7276 + DWC_VSNPRINTF(name, 128, format, args);
7277 + va_end(args);
7278 +
7279 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7280 + wq->pending++;
7281 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7282 + DWC_WAITQ_TRIGGER(wq->waitq);
7283 +
7284 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
7285 + if (!container) {
7286 + DWC_ERROR("Cannot allocate memory for container");
7287 + return;
7288 + }
7289 +
7290 + container->name = DWC_STRDUP(name);
7291 + if (!container->name) {
7292 + DWC_ERROR("Cannot allocate memory for container->name");
7293 + DWC_FREE(container);
7294 + return;
7295 + }
7296 +
7297 + container->cb = cb;
7298 + container->data = data;
7299 + container->wq = wq;
7300 +
7301 + tv.tv_sec = time / 1000;
7302 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
7303 + container->hz = tvtohz(&tv);
7304 +
7305 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
7306 +
7307 + TASK_INIT(&container->task, 0, do_work, container);
7308 +
7309 +#ifdef DEBUG
7310 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
7311 +#endif
7312 + taskqueue_enqueue_fast(wq->taskq, &container->task);
7313 +}
7314 +
7315 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
7316 +{
7317 + return wq->pending;
7318 +}
7319 --- /dev/null
7320 +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
7321 @@ -0,0 +1,1418 @@
7322 +#include <linux/kernel.h>
7323 +#include <linux/init.h>
7324 +#include <linux/module.h>
7325 +#include <linux/kthread.h>
7326 +
7327 +#ifdef DWC_CCLIB
7328 +# include "dwc_cc.h"
7329 +#endif
7330 +
7331 +#ifdef DWC_CRYPTOLIB
7332 +# include "dwc_modpow.h"
7333 +# include "dwc_dh.h"
7334 +# include "dwc_crypto.h"
7335 +#endif
7336 +
7337 +#ifdef DWC_NOTIFYLIB
7338 +# include "dwc_notifier.h"
7339 +#endif
7340 +
7341 +/* OS-Level Implementations */
7342 +
7343 +/* This is the Linux kernel implementation of the DWC platform library. */
7344 +#include <linux/moduleparam.h>
7345 +#include <linux/ctype.h>
7346 +#include <linux/crypto.h>
7347 +#include <linux/delay.h>
7348 +#include <linux/device.h>
7349 +#include <linux/dma-mapping.h>
7350 +#include <linux/cdev.h>
7351 +#include <linux/errno.h>
7352 +#include <linux/interrupt.h>
7353 +#include <linux/jiffies.h>
7354 +#include <linux/list.h>
7355 +#include <linux/pci.h>
7356 +#include <linux/random.h>
7357 +#include <linux/scatterlist.h>
7358 +#include <linux/slab.h>
7359 +#include <linux/stat.h>
7360 +#include <linux/string.h>
7361 +#include <linux/timer.h>
7362 +#include <linux/usb.h>
7363 +
7364 +#include <linux/version.h>
7365 +
7366 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
7367 +# include <linux/usb/gadget.h>
7368 +#else
7369 +# include <linux/usb_gadget.h>
7370 +#endif
7371 +
7372 +#include <asm/io.h>
7373 +#include <asm/page.h>
7374 +#include <asm/uaccess.h>
7375 +#include <asm/unaligned.h>
7376 +
7377 +#include "dwc_os.h"
7378 +#include "dwc_list.h"
7379 +
7380 +
7381 +/* MISC */
7382 +
7383 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
7384 +{
7385 + return memset(dest, byte, size);
7386 +}
7387 +
7388 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
7389 +{
7390 + return memcpy(dest, src, size);
7391 +}
7392 +
7393 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
7394 +{
7395 + return memmove(dest, src, size);
7396 +}
7397 +
7398 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
7399 +{
7400 + return memcmp(m1, m2, size);
7401 +}
7402 +
7403 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
7404 +{
7405 + return strncmp(s1, s2, size);
7406 +}
7407 +
7408 +int DWC_STRCMP(void *s1, void *s2)
7409 +{
7410 + return strcmp(s1, s2);
7411 +}
7412 +
7413 +int DWC_STRLEN(char const *str)
7414 +{
7415 + return strlen(str);
7416 +}
7417 +
7418 +char *DWC_STRCPY(char *to, char const *from)
7419 +{
7420 + return strcpy(to, from);
7421 +}
7422 +
7423 +char *DWC_STRDUP(char const *str)
7424 +{
7425 + int len = DWC_STRLEN(str) + 1;
7426 + char *new = DWC_ALLOC_ATOMIC(len);
7427 +
7428 + if (!new) {
7429 + return NULL;
7430 + }
7431 +
7432 + DWC_MEMCPY(new, str, len);
7433 + return new;
7434 +}
7435 +
7436 +int DWC_ATOI(const char *str, int32_t *value)
7437 +{
7438 + char *end = NULL;
7439 +
7440 + *value = simple_strtol(str, &end, 0);
7441 + if (*end == '\0') {
7442 + return 0;
7443 + }
7444 +
7445 + return -1;
7446 +}
7447 +
7448 +int DWC_ATOUI(const char *str, uint32_t *value)
7449 +{
7450 + char *end = NULL;
7451 +
7452 + *value = simple_strtoul(str, &end, 0);
7453 + if (*end == '\0') {
7454 + return 0;
7455 + }
7456 +
7457 + return -1;
7458 +}
7459 +
7460 +
7461 +#ifdef DWC_UTFLIB
7462 +/* From usbstring.c */
7463 +
7464 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
7465 +{
7466 + int count = 0;
7467 + u8 c;
7468 + u16 uchar;
7469 +
7470 + /* this insists on correct encodings, though not minimal ones.
7471 + * BUT it currently rejects legit 4-byte UTF-8 code points,
7472 + * which need surrogate pairs. (Unicode 3.1 can use them.)
7473 + */
7474 + while (len != 0 && (c = (u8) *s++) != 0) {
7475 + if (unlikely(c & 0x80)) {
7476 + // 2-byte sequence:
7477 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
7478 + if ((c & 0xe0) == 0xc0) {
7479 + uchar = (c & 0x1f) << 6;
7480 +
7481 + c = (u8) *s++;
7482 + if ((c & 0xc0) != 0xc0)
7483 + goto fail;
7484 + c &= 0x3f;
7485 + uchar |= c;
7486 +
7487 + // 3-byte sequence (most CJKV characters):
7488 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
7489 + } else if ((c & 0xf0) == 0xe0) {
7490 + uchar = (c & 0x0f) << 12;
7491 +
7492 + c = (u8) *s++;
7493 + if ((c & 0xc0) != 0xc0)
7494 + goto fail;
7495 + c &= 0x3f;
7496 + uchar |= c << 6;
7497 +
7498 + c = (u8) *s++;
7499 + if ((c & 0xc0) != 0xc0)
7500 + goto fail;
7501 + c &= 0x3f;
7502 + uchar |= c;
7503 +
7504 + /* no bogus surrogates */
7505 + if (0xd800 <= uchar && uchar <= 0xdfff)
7506 + goto fail;
7507 +
7508 + // 4-byte sequence (surrogate pairs, currently rare):
7509 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
7510 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
7511 + // (uuuuu = wwww + 1)
7512 + // FIXME accept the surrogate code points (only)
7513 + } else
7514 + goto fail;
7515 + } else
7516 + uchar = c;
7517 + put_unaligned (cpu_to_le16 (uchar), cp++);
7518 + count++;
7519 + len--;
7520 + }
7521 + return count;
7522 +fail:
7523 + return -1;
7524 +}
7525 +#endif /* DWC_UTFLIB */
7526 +
7527 +
7528 +/* dwc_debug.h */
7529 +
7530 +dwc_bool_t DWC_IN_IRQ(void)
7531 +{
7532 + return in_irq();
7533 +}
7534 +
7535 +dwc_bool_t DWC_IN_BH(void)
7536 +{
7537 + return in_softirq();
7538 +}
7539 +
7540 +void DWC_VPRINTF(char *format, va_list args)
7541 +{
7542 + vprintk(format, args);
7543 +}
7544 +
7545 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
7546 +{
7547 + return vsnprintf(str, size, format, args);
7548 +}
7549 +
7550 +void DWC_PRINTF(char *format, ...)
7551 +{
7552 + va_list args;
7553 +
7554 + va_start(args, format);
7555 + DWC_VPRINTF(format, args);
7556 + va_end(args);
7557 +}
7558 +
7559 +int DWC_SPRINTF(char *buffer, char *format, ...)
7560 +{
7561 + int retval;
7562 + va_list args;
7563 +
7564 + va_start(args, format);
7565 + retval = vsprintf(buffer, format, args);
7566 + va_end(args);
7567 + return retval;
7568 +}
7569 +
7570 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
7571 +{
7572 + int retval;
7573 + va_list args;
7574 +
7575 + va_start(args, format);
7576 + retval = vsnprintf(buffer, size, format, args);
7577 + va_end(args);
7578 + return retval;
7579 +}
7580 +
7581 +void __DWC_WARN(char *format, ...)
7582 +{
7583 + va_list args;
7584 +
7585 + va_start(args, format);
7586 + DWC_PRINTF(KERN_WARNING);
7587 + DWC_VPRINTF(format, args);
7588 + va_end(args);
7589 +}
7590 +
7591 +void __DWC_ERROR(char *format, ...)
7592 +{
7593 + va_list args;
7594 +
7595 + va_start(args, format);
7596 + DWC_PRINTF(KERN_ERR);
7597 + DWC_VPRINTF(format, args);
7598 + va_end(args);
7599 +}
7600 +
7601 +void DWC_EXCEPTION(char *format, ...)
7602 +{
7603 + va_list args;
7604 +
7605 + va_start(args, format);
7606 + DWC_PRINTF(KERN_ERR);
7607 + DWC_VPRINTF(format, args);
7608 + va_end(args);
7609 + BUG_ON(1);
7610 +}
7611 +
7612 +#ifdef DEBUG
7613 +void __DWC_DEBUG(char *format, ...)
7614 +{
7615 + va_list args;
7616 +
7617 + va_start(args, format);
7618 + DWC_PRINTF(KERN_DEBUG);
7619 + DWC_VPRINTF(format, args);
7620 + va_end(args);
7621 +}
7622 +#endif
7623 +
7624 +
7625 +/* dwc_mem.h */
7626 +
7627 +#if 0
7628 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
7629 + uint32_t align,
7630 + uint32_t alloc)
7631 +{
7632 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
7633 + size, align, alloc);
7634 + return (dwc_pool_t *)pool;
7635 +}
7636 +
7637 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
7638 +{
7639 + dma_pool_destroy((struct dma_pool *)pool);
7640 +}
7641 +
7642 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
7643 +{
7644 + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
7645 +}
7646 +
7647 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
7648 +{
7649 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
7650 + memset(..);
7651 +}
7652 +
7653 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
7654 +{
7655 + dma_pool_free(pool, vaddr, daddr);
7656 +}
7657 +#endif
7658 +
7659 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
7660 +{
7661 + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
7662 +}
7663 +
7664 +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
7665 +{
7666 + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
7667 +}
7668 +
7669 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
7670 +{
7671 + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
7672 +}
7673 +
7674 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
7675 +{
7676 + return kzalloc(size, GFP_KERNEL);
7677 +}
7678 +
7679 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
7680 +{
7681 + return kzalloc(size, GFP_ATOMIC);
7682 +}
7683 +
7684 +void __DWC_FREE(void *mem_ctx, void *addr)
7685 +{
7686 + kfree(addr);
7687 +}
7688 +
7689 +
7690 +#ifdef DWC_CRYPTOLIB
7691 +/* dwc_crypto.h */
7692 +
7693 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
7694 +{
7695 + get_random_bytes(buffer, length);
7696 +}
7697 +
7698 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
7699 +{
7700 + struct crypto_blkcipher *tfm;
7701 + struct blkcipher_desc desc;
7702 + struct scatterlist sgd;
7703 + struct scatterlist sgs;
7704 +
7705 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
7706 + if (tfm == NULL) {
7707 + printk("failed to load transform for aes CBC\n");
7708 + return -1;
7709 + }
7710 +
7711 + crypto_blkcipher_setkey(tfm, key, keylen);
7712 + crypto_blkcipher_set_iv(tfm, iv, 16);
7713 +
7714 + sg_init_one(&sgd, out, messagelen);
7715 + sg_init_one(&sgs, message, messagelen);
7716 +
7717 + desc.tfm = tfm;
7718 + desc.flags = 0;
7719 +
7720 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
7721 + crypto_free_blkcipher(tfm);
7722 + DWC_ERROR("AES CBC encryption failed");
7723 + return -1;
7724 + }
7725 +
7726 + crypto_free_blkcipher(tfm);
7727 + return 0;
7728 +}
7729 +
7730 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
7731 +{
7732 + struct crypto_hash *tfm;
7733 + struct hash_desc desc;
7734 + struct scatterlist sg;
7735 +
7736 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
7737 + if (IS_ERR(tfm)) {
7738 + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
7739 + return 0;
7740 + }
7741 + desc.tfm = tfm;
7742 + desc.flags = 0;
7743 +
7744 + sg_init_one(&sg, message, len);
7745 + crypto_hash_digest(&desc, &sg, len, out);
7746 + crypto_free_hash(tfm);
7747 +
7748 + return 1;
7749 +}
7750 +
7751 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
7752 + uint8_t *key, uint32_t keylen, uint8_t *out)
7753 +{
7754 + struct crypto_hash *tfm;
7755 + struct hash_desc desc;
7756 + struct scatterlist sg;
7757 +
7758 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
7759 + if (IS_ERR(tfm)) {
7760 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
7761 + return 0;
7762 + }
7763 + desc.tfm = tfm;
7764 + desc.flags = 0;
7765 +
7766 + sg_init_one(&sg, message, messagelen);
7767 + crypto_hash_setkey(tfm, key, keylen);
7768 + crypto_hash_digest(&desc, &sg, messagelen, out);
7769 + crypto_free_hash(tfm);
7770 +
7771 + return 1;
7772 +}
7773 +#endif /* DWC_CRYPTOLIB */
7774 +
7775 +
7776 +/* Byte Ordering Conversions */
7777 +
7778 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
7779 +{
7780 +#ifdef __LITTLE_ENDIAN
7781 + return *p;
7782 +#else
7783 + uint8_t *u_p = (uint8_t *)p;
7784 +
7785 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7786 +#endif
7787 +}
7788 +
7789 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
7790 +{
7791 +#ifdef __BIG_ENDIAN
7792 + return *p;
7793 +#else
7794 + uint8_t *u_p = (uint8_t *)p;
7795 +
7796 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7797 +#endif
7798 +}
7799 +
7800 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
7801 +{
7802 +#ifdef __LITTLE_ENDIAN
7803 + return *p;
7804 +#else
7805 + uint8_t *u_p = (uint8_t *)p;
7806 +
7807 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7808 +#endif
7809 +}
7810 +
7811 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
7812 +{
7813 +#ifdef __BIG_ENDIAN
7814 + return *p;
7815 +#else
7816 + uint8_t *u_p = (uint8_t *)p;
7817 +
7818 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7819 +#endif
7820 +}
7821 +
7822 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
7823 +{
7824 +#ifdef __LITTLE_ENDIAN
7825 + return *p;
7826 +#else
7827 + uint8_t *u_p = (uint8_t *)p;
7828 + return (u_p[1] | (u_p[0] << 8));
7829 +#endif
7830 +}
7831 +
7832 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
7833 +{
7834 +#ifdef __BIG_ENDIAN
7835 + return *p;
7836 +#else
7837 + uint8_t *u_p = (uint8_t *)p;
7838 + return (u_p[1] | (u_p[0] << 8));
7839 +#endif
7840 +}
7841 +
7842 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
7843 +{
7844 +#ifdef __LITTLE_ENDIAN
7845 + return *p;
7846 +#else
7847 + uint8_t *u_p = (uint8_t *)p;
7848 + return (u_p[1] | (u_p[0] << 8));
7849 +#endif
7850 +}
7851 +
7852 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
7853 +{
7854 +#ifdef __BIG_ENDIAN
7855 + return *p;
7856 +#else
7857 + uint8_t *u_p = (uint8_t *)p;
7858 + return (u_p[1] | (u_p[0] << 8));
7859 +#endif
7860 +}
7861 +
7862 +
7863 +/* Registers */
7864 +
7865 +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
7866 +{
7867 + return readl(reg);
7868 +}
7869 +
7870 +#if 0
7871 +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
7872 +{
7873 +}
7874 +#endif
7875 +
7876 +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
7877 +{
7878 + writel(value, reg);
7879 +}
7880 +
7881 +#if 0
7882 +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
7883 +{
7884 +}
7885 +#endif
7886 +
7887 +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
7888 +{
7889 + writel((readl(reg) & ~clear_mask) | set_mask, reg);
7890 +}
7891 +
7892 +#if 0
7893 +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
7894 +{
7895 +}
7896 +#endif
7897 +
7898 +
7899 +/* Locking */
7900 +
7901 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
7902 +{
7903 + spinlock_t *sl = (spinlock_t *)1;
7904 +
7905 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
7906 + sl = DWC_ALLOC(sizeof(*sl));
7907 + if (!sl) {
7908 + DWC_ERROR("Cannot allocate memory for spinlock\n");
7909 + return NULL;
7910 + }
7911 +
7912 + spin_lock_init(sl);
7913 +#endif
7914 + return (dwc_spinlock_t *)sl;
7915 +}
7916 +
7917 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
7918 +{
7919 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
7920 + DWC_FREE(lock);
7921 +#endif
7922 +}
7923 +
7924 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
7925 +{
7926 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
7927 + spin_lock((spinlock_t *)lock);
7928 +#endif
7929 +}
7930 +
7931 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
7932 +{
7933 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
7934 + spin_unlock((spinlock_t *)lock);
7935 +#endif
7936 +}
7937 +
7938 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
7939 +{
7940 + dwc_irqflags_t f;
7941 +
7942 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
7943 + spin_lock_irqsave((spinlock_t *)lock, f);
7944 +#else
7945 + local_irq_save(f);
7946 +#endif
7947 + *flags = f;
7948 +}
7949 +
7950 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
7951 +{
7952 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
7953 + spin_unlock_irqrestore((spinlock_t *)lock, flags);
7954 +#else
7955 + local_irq_restore(flags);
7956 +#endif
7957 +}
7958 +
7959 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
7960 +{
7961 + struct mutex *m;
7962 + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
7963 +
7964 + if (!mutex) {
7965 + DWC_ERROR("Cannot allocate memory for mutex\n");
7966 + return NULL;
7967 + }
7968 +
7969 + m = (struct mutex *)mutex;
7970 + mutex_init(m);
7971 + return mutex;
7972 +}
7973 +
7974 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
7975 +#else
7976 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
7977 +{
7978 + mutex_destroy((struct mutex *)mutex);
7979 + DWC_FREE(mutex);
7980 +}
7981 +#endif
7982 +
7983 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
7984 +{
7985 + struct mutex *m = (struct mutex *)mutex;
7986 + mutex_lock(m);
7987 +}
7988 +
7989 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
7990 +{
7991 + struct mutex *m = (struct mutex *)mutex;
7992 + return mutex_trylock(m);
7993 +}
7994 +
7995 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
7996 +{
7997 + struct mutex *m = (struct mutex *)mutex;
7998 + mutex_unlock(m);
7999 +}
8000 +
8001 +
8002 +/* Timing */
8003 +
8004 +void DWC_UDELAY(uint32_t usecs)
8005 +{
8006 + udelay(usecs);
8007 +}
8008 +
8009 +void DWC_MDELAY(uint32_t msecs)
8010 +{
8011 + mdelay(msecs);
8012 +}
8013 +
8014 +void DWC_MSLEEP(uint32_t msecs)
8015 +{
8016 + msleep(msecs);
8017 +}
8018 +
8019 +uint32_t DWC_TIME(void)
8020 +{
8021 + return jiffies_to_msecs(jiffies);
8022 +}
8023 +
8024 +
8025 +/* Timers */
8026 +
8027 +struct dwc_timer {
8028 + struct timer_list *t;
8029 + char *name;
8030 + dwc_timer_callback_t cb;
8031 + void *data;
8032 + uint8_t scheduled;
8033 + dwc_spinlock_t *lock;
8034 +};
8035 +
8036 +static void timer_callback(unsigned long data)
8037 +{
8038 + dwc_timer_t *timer = (dwc_timer_t *)data;
8039 + dwc_irqflags_t flags;
8040 +
8041 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8042 + timer->scheduled = 0;
8043 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8044 + DWC_DEBUGC("Timer %s callback", timer->name);
8045 + timer->cb(timer->data);
8046 +}
8047 +
8048 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
8049 +{
8050 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
8051 +
8052 + if (!t) {
8053 + DWC_ERROR("Cannot allocate memory for timer");
8054 + return NULL;
8055 + }
8056 +
8057 + t->t = DWC_ALLOC(sizeof(*t->t));
8058 + if (!t->t) {
8059 + DWC_ERROR("Cannot allocate memory for timer->t");
8060 + goto no_timer;
8061 + }
8062 +
8063 + t->name = DWC_STRDUP(name);
8064 + if (!t->name) {
8065 + DWC_ERROR("Cannot allocate memory for timer->name");
8066 + goto no_name;
8067 + }
8068 +
8069 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
8070 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
8071 +#else
8072 + t->lock = DWC_SPINLOCK_ALLOC();
8073 +#endif
8074 + if (!t->lock) {
8075 + DWC_ERROR("Cannot allocate memory for lock");
8076 + goto no_lock;
8077 + }
8078 +
8079 + t->scheduled = 0;
8080 + t->t->expires = jiffies;
8081 + setup_timer(t->t, timer_callback, (unsigned long)t);
8082 +
8083 + t->cb = cb;
8084 + t->data = data;
8085 +
8086 + return t;
8087 +
8088 + no_lock:
8089 + DWC_FREE(t->name);
8090 + no_name:
8091 + DWC_FREE(t->t);
8092 + no_timer:
8093 + DWC_FREE(t);
8094 + return NULL;
8095 +}
8096 +
8097 +void DWC_TIMER_FREE(dwc_timer_t *timer)
8098 +{
8099 + dwc_irqflags_t flags;
8100 +
8101 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8102 +
8103 + if (timer->scheduled) {
8104 + del_timer(timer->t);
8105 + timer->scheduled = 0;
8106 + }
8107 +
8108 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8109 + DWC_SPINLOCK_FREE(timer->lock);
8110 + DWC_FREE(timer->t);
8111 + DWC_FREE(timer->name);
8112 + DWC_FREE(timer);
8113 +}
8114 +
8115 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
8116 +{
8117 + dwc_irqflags_t flags;
8118 +
8119 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8120 +
8121 + if (!timer->scheduled) {
8122 + timer->scheduled = 1;
8123 + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
8124 + timer->t->expires = jiffies + msecs_to_jiffies(time);
8125 + add_timer(timer->t);
8126 + } else {
8127 + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
8128 + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
8129 + }
8130 +
8131 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8132 +}
8133 +
8134 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
8135 +{
8136 + del_timer(timer->t);
8137 +}
8138 +
8139 +
8140 +/* Wait Queues */
8141 +
8142 +struct dwc_waitq {
8143 + wait_queue_head_t queue;
8144 + int abort;
8145 +};
8146 +
8147 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
8148 +{
8149 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
8150 +
8151 + if (!wq) {
8152 + DWC_ERROR("Cannot allocate memory for waitqueue\n");
8153 + return NULL;
8154 + }
8155 +
8156 + init_waitqueue_head(&wq->queue);
8157 + wq->abort = 0;
8158 + return wq;
8159 +}
8160 +
8161 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
8162 +{
8163 + DWC_FREE(wq);
8164 +}
8165 +
8166 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
8167 +{
8168 + int result = wait_event_interruptible(wq->queue,
8169 + cond(data) || wq->abort);
8170 + if (result == -ERESTARTSYS) {
8171 + wq->abort = 0;
8172 + return -DWC_E_RESTART;
8173 + }
8174 +
8175 + if (wq->abort == 1) {
8176 + wq->abort = 0;
8177 + return -DWC_E_ABORT;
8178 + }
8179 +
8180 + wq->abort = 0;
8181 +
8182 + if (result == 0) {
8183 + return 0;
8184 + }
8185 +
8186 + return -DWC_E_UNKNOWN;
8187 +}
8188 +
8189 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
8190 + void *data, int32_t msecs)
8191 +{
8192 + int32_t tmsecs;
8193 + int result = wait_event_interruptible_timeout(wq->queue,
8194 + cond(data) || wq->abort,
8195 + msecs_to_jiffies(msecs));
8196 + if (result == -ERESTARTSYS) {
8197 + wq->abort = 0;
8198 + return -DWC_E_RESTART;
8199 + }
8200 +
8201 + if (wq->abort == 1) {
8202 + wq->abort = 0;
8203 + return -DWC_E_ABORT;
8204 + }
8205 +
8206 + wq->abort = 0;
8207 +
8208 + if (result > 0) {
8209 + tmsecs = jiffies_to_msecs(result);
8210 + if (!tmsecs) {
8211 + return 1;
8212 + }
8213 +
8214 + return tmsecs;
8215 + }
8216 +
8217 + if (result == 0) {
8218 + return -DWC_E_TIMEOUT;
8219 + }
8220 +
8221 + return -DWC_E_UNKNOWN;
8222 +}
8223 +
8224 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
8225 +{
8226 + wq->abort = 0;
8227 + wake_up_interruptible(&wq->queue);
8228 +}
8229 +
8230 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
8231 +{
8232 + wq->abort = 1;
8233 + wake_up_interruptible(&wq->queue);
8234 +}
8235 +
8236 +
8237 +/* Threading */
8238 +
8239 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
8240 +{
8241 + struct task_struct *thread = kthread_run(func, data, name);
8242 +
8243 + if (thread == ERR_PTR(-ENOMEM)) {
8244 + return NULL;
8245 + }
8246 +
8247 + return (dwc_thread_t *)thread;
8248 +}
8249 +
8250 +int DWC_THREAD_STOP(dwc_thread_t *thread)
8251 +{
8252 + return kthread_stop((struct task_struct *)thread);
8253 +}
8254 +
8255 +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
8256 +{
8257 + return kthread_should_stop();
8258 +}
8259 +
8260 +
8261 +/* tasklets
8262 + - run in interrupt context (cannot sleep)
8263 + - each tasklet runs on a single CPU
8264 + - different tasklets can be running simultaneously on different CPUs
8265 + */
8266 +struct dwc_tasklet {
8267 + struct tasklet_struct t;
8268 + dwc_tasklet_callback_t cb;
8269 + void *data;
8270 +};
8271 +
8272 +static void tasklet_callback(unsigned long data)
8273 +{
8274 + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
8275 + t->cb(t->data);
8276 +}
8277 +
8278 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
8279 +{
8280 + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
8281 +
8282 + if (t) {
8283 + t->cb = cb;
8284 + t->data = data;
8285 + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
8286 + } else {
8287 + DWC_ERROR("Cannot allocate memory for tasklet\n");
8288 + }
8289 +
8290 + return t;
8291 +}
8292 +
8293 +void DWC_TASK_FREE(dwc_tasklet_t *task)
8294 +{
8295 + DWC_FREE(task);
8296 +}
8297 +
8298 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
8299 +{
8300 + tasklet_schedule(&task->t);
8301 +}
8302 +
8303 +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
8304 +{
8305 + tasklet_hi_schedule(&task->t);
8306 +}
8307 +
8308 +
8309 +/* workqueues
8310 + - run in process context (can sleep)
8311 + */
8312 +typedef struct work_container {
8313 + dwc_work_callback_t cb;
8314 + void *data;
8315 + dwc_workq_t *wq;
8316 + char *name;
8317 +
8318 +#ifdef DEBUG
8319 + DWC_CIRCLEQ_ENTRY(work_container) entry;
8320 +#endif
8321 + struct delayed_work work;
8322 +} work_container_t;
8323 +
8324 +#ifdef DEBUG
8325 +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
8326 +#endif
8327 +
8328 +struct dwc_workq {
8329 + struct workqueue_struct *wq;
8330 + dwc_spinlock_t *lock;
8331 + dwc_waitq_t *waitq;
8332 + int pending;
8333 +
8334 +#ifdef DEBUG
8335 + struct work_container_queue entries;
8336 +#endif
8337 +};
8338 +
8339 +static void do_work(struct work_struct *work)
8340 +{
8341 + dwc_irqflags_t flags;
8342 + struct delayed_work *dw = container_of(work, struct delayed_work, work);
8343 + work_container_t *container = container_of(dw, struct work_container, work);
8344 + dwc_workq_t *wq = container->wq;
8345 +
8346 + container->cb(container->data);
8347 +
8348 +#ifdef DEBUG
8349 + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
8350 +#endif
8351 + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
8352 + if (container->name) {
8353 + DWC_FREE(container->name);
8354 + }
8355 + DWC_FREE(container);
8356 +
8357 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8358 + wq->pending--;
8359 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8360 + DWC_WAITQ_TRIGGER(wq->waitq);
8361 +}
8362 +
8363 +static int work_done(void *data)
8364 +{
8365 + dwc_workq_t *workq = (dwc_workq_t *)data;
8366 + return workq->pending == 0;
8367 +}
8368 +
8369 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
8370 +{
8371 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
8372 +}
8373 +
8374 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
8375 +{
8376 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
8377 +
8378 + if (!wq) {
8379 + return NULL;
8380 + }
8381 +
8382 + wq->wq = create_singlethread_workqueue(name);
8383 + if (!wq->wq) {
8384 + goto no_wq;
8385 + }
8386 +
8387 + wq->pending = 0;
8388 +
8389 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
8390 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
8391 +#else
8392 + wq->lock = DWC_SPINLOCK_ALLOC();
8393 +#endif
8394 + if (!wq->lock) {
8395 + goto no_lock;
8396 + }
8397 +
8398 + wq->waitq = DWC_WAITQ_ALLOC();
8399 + if (!wq->waitq) {
8400 + goto no_waitq;
8401 + }
8402 +
8403 +#ifdef DEBUG
8404 + DWC_CIRCLEQ_INIT(&wq->entries);
8405 +#endif
8406 + return wq;
8407 +
8408 + no_waitq:
8409 + DWC_SPINLOCK_FREE(wq->lock);
8410 + no_lock:
8411 + destroy_workqueue(wq->wq);
8412 + no_wq:
8413 + DWC_FREE(wq);
8414 +
8415 + return NULL;
8416 +}
8417 +
8418 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
8419 +{
8420 +#ifdef DEBUG
8421 + if (wq->pending != 0) {
8422 + struct work_container *wc;
8423 + DWC_ERROR("Destroying work queue with pending work");
8424 + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
8425 + DWC_ERROR("Work %s still pending", wc->name);
8426 + }
8427 + }
8428 +#endif
8429 + destroy_workqueue(wq->wq);
8430 + DWC_SPINLOCK_FREE(wq->lock);
8431 + DWC_WAITQ_FREE(wq->waitq);
8432 + DWC_FREE(wq);
8433 +}
8434 +
8435 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
8436 + char *format, ...)
8437 +{
8438 + dwc_irqflags_t flags;
8439 + work_container_t *container;
8440 + static char name[128];
8441 + va_list args;
8442 +
8443 + va_start(args, format);
8444 + DWC_VSNPRINTF(name, 128, format, args);
8445 + va_end(args);
8446 +
8447 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8448 + wq->pending++;
8449 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8450 + DWC_WAITQ_TRIGGER(wq->waitq);
8451 +
8452 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
8453 + if (!container) {
8454 + DWC_ERROR("Cannot allocate memory for container\n");
8455 + return;
8456 + }
8457 +
8458 + container->name = DWC_STRDUP(name);
8459 + if (!container->name) {
8460 + DWC_ERROR("Cannot allocate memory for container->name\n");
8461 + DWC_FREE(container);
8462 + return;
8463 + }
8464 +
8465 + container->cb = cb;
8466 + container->data = data;
8467 + container->wq = wq;
8468 + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
8469 + INIT_WORK(&container->work.work, do_work);
8470 +
8471 +#ifdef DEBUG
8472 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
8473 +#endif
8474 + queue_work(wq->wq, &container->work.work);
8475 +}
8476 +
8477 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
8478 + void *data, uint32_t time, char *format, ...)
8479 +{
8480 + dwc_irqflags_t flags;
8481 + work_container_t *container;
8482 + static char name[128];
8483 + va_list args;
8484 +
8485 + va_start(args, format);
8486 + DWC_VSNPRINTF(name, 128, format, args);
8487 + va_end(args);
8488 +
8489 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8490 + wq->pending++;
8491 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8492 + DWC_WAITQ_TRIGGER(wq->waitq);
8493 +
8494 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
8495 + if (!container) {
8496 + DWC_ERROR("Cannot allocate memory for container\n");
8497 + return;
8498 + }
8499 +
8500 + container->name = DWC_STRDUP(name);
8501 + if (!container->name) {
8502 + DWC_ERROR("Cannot allocate memory for container->name\n");
8503 + DWC_FREE(container);
8504 + return;
8505 + }
8506 +
8507 + container->cb = cb;
8508 + container->data = data;
8509 + container->wq = wq;
8510 + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
8511 + INIT_DELAYED_WORK(&container->work, do_work);
8512 +
8513 +#ifdef DEBUG
8514 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
8515 +#endif
8516 + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
8517 +}
8518 +
8519 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
8520 +{
8521 + return wq->pending;
8522 +}
8523 +
8524 +
8525 +#ifdef DWC_LIBMODULE
8526 +
8527 +#ifdef DWC_CCLIB
8528 +/* CC */
8529 +EXPORT_SYMBOL(dwc_cc_if_alloc);
8530 +EXPORT_SYMBOL(dwc_cc_if_free);
8531 +EXPORT_SYMBOL(dwc_cc_clear);
8532 +EXPORT_SYMBOL(dwc_cc_add);
8533 +EXPORT_SYMBOL(dwc_cc_remove);
8534 +EXPORT_SYMBOL(dwc_cc_change);
8535 +EXPORT_SYMBOL(dwc_cc_data_for_save);
8536 +EXPORT_SYMBOL(dwc_cc_restore_from_data);
8537 +EXPORT_SYMBOL(dwc_cc_match_chid);
8538 +EXPORT_SYMBOL(dwc_cc_match_cdid);
8539 +EXPORT_SYMBOL(dwc_cc_ck);
8540 +EXPORT_SYMBOL(dwc_cc_chid);
8541 +EXPORT_SYMBOL(dwc_cc_cdid);
8542 +EXPORT_SYMBOL(dwc_cc_name);
8543 +#endif /* DWC_CCLIB */
8544 +
8545 +#ifdef DWC_CRYPTOLIB
8546 +# ifndef CONFIG_MACH_IPMATE
8547 +/* Modpow */
8548 +EXPORT_SYMBOL(dwc_modpow);
8549 +
8550 +/* DH */
8551 +EXPORT_SYMBOL(dwc_dh_modpow);
8552 +EXPORT_SYMBOL(dwc_dh_derive_keys);
8553 +EXPORT_SYMBOL(dwc_dh_pk);
8554 +# endif /* CONFIG_MACH_IPMATE */
8555 +
8556 +/* Crypto */
8557 +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
8558 +EXPORT_SYMBOL(dwc_wusb_cmf);
8559 +EXPORT_SYMBOL(dwc_wusb_prf);
8560 +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
8561 +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
8562 +EXPORT_SYMBOL(dwc_wusb_gen_key);
8563 +EXPORT_SYMBOL(dwc_wusb_gen_mic);
8564 +#endif /* DWC_CRYPTOLIB */
8565 +
8566 +/* Notification */
8567 +#ifdef DWC_NOTIFYLIB
8568 +EXPORT_SYMBOL(dwc_alloc_notification_manager);
8569 +EXPORT_SYMBOL(dwc_free_notification_manager);
8570 +EXPORT_SYMBOL(dwc_register_notifier);
8571 +EXPORT_SYMBOL(dwc_unregister_notifier);
8572 +EXPORT_SYMBOL(dwc_add_observer);
8573 +EXPORT_SYMBOL(dwc_remove_observer);
8574 +EXPORT_SYMBOL(dwc_notify);
8575 +#endif
8576 +
8577 +/* Memory Debugging Routines */
8578 +#ifdef DWC_DEBUG_MEMORY
8579 +EXPORT_SYMBOL(dwc_alloc_debug);
8580 +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
8581 +EXPORT_SYMBOL(dwc_free_debug);
8582 +EXPORT_SYMBOL(dwc_dma_alloc_debug);
8583 +EXPORT_SYMBOL(dwc_dma_free_debug);
8584 +#endif
8585 +
8586 +EXPORT_SYMBOL(DWC_MEMSET);
8587 +EXPORT_SYMBOL(DWC_MEMCPY);
8588 +EXPORT_SYMBOL(DWC_MEMMOVE);
8589 +EXPORT_SYMBOL(DWC_MEMCMP);
8590 +EXPORT_SYMBOL(DWC_STRNCMP);
8591 +EXPORT_SYMBOL(DWC_STRCMP);
8592 +EXPORT_SYMBOL(DWC_STRLEN);
8593 +EXPORT_SYMBOL(DWC_STRCPY);
8594 +EXPORT_SYMBOL(DWC_STRDUP);
8595 +EXPORT_SYMBOL(DWC_ATOI);
8596 +EXPORT_SYMBOL(DWC_ATOUI);
8597 +
8598 +#ifdef DWC_UTFLIB
8599 +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
8600 +#endif /* DWC_UTFLIB */
8601 +
8602 +EXPORT_SYMBOL(DWC_IN_IRQ);
8603 +EXPORT_SYMBOL(DWC_IN_BH);
8604 +EXPORT_SYMBOL(DWC_VPRINTF);
8605 +EXPORT_SYMBOL(DWC_VSNPRINTF);
8606 +EXPORT_SYMBOL(DWC_PRINTF);
8607 +EXPORT_SYMBOL(DWC_SPRINTF);
8608 +EXPORT_SYMBOL(DWC_SNPRINTF);
8609 +EXPORT_SYMBOL(__DWC_WARN);
8610 +EXPORT_SYMBOL(__DWC_ERROR);
8611 +EXPORT_SYMBOL(DWC_EXCEPTION);
8612 +
8613 +#ifdef DEBUG
8614 +EXPORT_SYMBOL(__DWC_DEBUG);
8615 +#endif
8616 +
8617 +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
8618 +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
8619 +EXPORT_SYMBOL(__DWC_DMA_FREE);
8620 +EXPORT_SYMBOL(__DWC_ALLOC);
8621 +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
8622 +EXPORT_SYMBOL(__DWC_FREE);
8623 +
8624 +#ifdef DWC_CRYPTOLIB
8625 +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
8626 +EXPORT_SYMBOL(DWC_AES_CBC);
8627 +EXPORT_SYMBOL(DWC_SHA256);
8628 +EXPORT_SYMBOL(DWC_HMAC_SHA256);
8629 +#endif
8630 +
8631 +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
8632 +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
8633 +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
8634 +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
8635 +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
8636 +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
8637 +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
8638 +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
8639 +EXPORT_SYMBOL(DWC_READ_REG32);
8640 +EXPORT_SYMBOL(DWC_WRITE_REG32);
8641 +EXPORT_SYMBOL(DWC_MODIFY_REG32);
8642 +
8643 +#if 0
8644 +EXPORT_SYMBOL(DWC_READ_REG64);
8645 +EXPORT_SYMBOL(DWC_WRITE_REG64);
8646 +EXPORT_SYMBOL(DWC_MODIFY_REG64);
8647 +#endif
8648 +
8649 +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
8650 +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
8651 +EXPORT_SYMBOL(DWC_SPINLOCK);
8652 +EXPORT_SYMBOL(DWC_SPINUNLOCK);
8653 +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
8654 +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
8655 +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
8656 +
8657 +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
8658 +EXPORT_SYMBOL(DWC_MUTEX_FREE);
8659 +#endif
8660 +
8661 +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
8662 +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
8663 +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
8664 +EXPORT_SYMBOL(DWC_UDELAY);
8665 +EXPORT_SYMBOL(DWC_MDELAY);
8666 +EXPORT_SYMBOL(DWC_MSLEEP);
8667 +EXPORT_SYMBOL(DWC_TIME);
8668 +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
8669 +EXPORT_SYMBOL(DWC_TIMER_FREE);
8670 +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
8671 +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
8672 +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
8673 +EXPORT_SYMBOL(DWC_WAITQ_FREE);
8674 +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
8675 +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
8676 +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
8677 +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
8678 +EXPORT_SYMBOL(DWC_THREAD_RUN);
8679 +EXPORT_SYMBOL(DWC_THREAD_STOP);
8680 +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
8681 +EXPORT_SYMBOL(DWC_TASK_ALLOC);
8682 +EXPORT_SYMBOL(DWC_TASK_FREE);
8683 +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
8684 +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
8685 +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
8686 +EXPORT_SYMBOL(DWC_WORKQ_FREE);
8687 +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
8688 +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
8689 +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
8690 +
8691 +static int dwc_common_port_init_module(void)
8692 +{
8693 + int result = 0;
8694 +
8695 + printk(KERN_DEBUG "Module dwc_common_port init\n" );
8696 +
8697 +#ifdef DWC_DEBUG_MEMORY
8698 + result = dwc_memory_debug_start(NULL);
8699 + if (result) {
8700 + printk(KERN_ERR
8701 + "dwc_memory_debug_start() failed with error %d\n",
8702 + result);
8703 + return result;
8704 + }
8705 +#endif
8706 +
8707 +#ifdef DWC_NOTIFYLIB
8708 + result = dwc_alloc_notification_manager(NULL, NULL);
8709 + if (result) {
8710 + printk(KERN_ERR
8711 + "dwc_alloc_notification_manager() failed with error %d\n",
8712 + result);
8713 + return result;
8714 + }
8715 +#endif
8716 + return result;
8717 +}
8718 +
8719 +static void dwc_common_port_exit_module(void)
8720 +{
8721 + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
8722 +
8723 +#ifdef DWC_NOTIFYLIB
8724 + dwc_free_notification_manager();
8725 +#endif
8726 +
8727 +#ifdef DWC_DEBUG_MEMORY
8728 + dwc_memory_debug_stop();
8729 +#endif
8730 +}
8731 +
8732 +module_init(dwc_common_port_init_module);
8733 +module_exit(dwc_common_port_exit_module);
8734 +
8735 +MODULE_DESCRIPTION("DWC Common Library - Portable version");
8736 +MODULE_AUTHOR("Synopsys Inc.");
8737 +MODULE_LICENSE ("GPL");
8738 +
8739 +#endif /* DWC_LIBMODULE */
8740 --- /dev/null
8741 +++ b/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
8742 @@ -0,0 +1,1275 @@
8743 +#include "dwc_os.h"
8744 +#include "dwc_list.h"
8745 +
8746 +#ifdef DWC_CCLIB
8747 +# include "dwc_cc.h"
8748 +#endif
8749 +
8750 +#ifdef DWC_CRYPTOLIB
8751 +# include "dwc_modpow.h"
8752 +# include "dwc_dh.h"
8753 +# include "dwc_crypto.h"
8754 +#endif
8755 +
8756 +#ifdef DWC_NOTIFYLIB
8757 +# include "dwc_notifier.h"
8758 +#endif
8759 +
8760 +/* OS-Level Implementations */
8761 +
8762 +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
8763 +
8764 +
8765 +/* MISC */
8766 +
8767 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
8768 +{
8769 + return memset(dest, byte, size);
8770 +}
8771 +
8772 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
8773 +{
8774 + return memcpy(dest, src, size);
8775 +}
8776 +
8777 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
8778 +{
8779 + bcopy(src, dest, size);
8780 + return dest;
8781 +}
8782 +
8783 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
8784 +{
8785 + return memcmp(m1, m2, size);
8786 +}
8787 +
8788 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
8789 +{
8790 + return strncmp(s1, s2, size);
8791 +}
8792 +
8793 +int DWC_STRCMP(void *s1, void *s2)
8794 +{
8795 + return strcmp(s1, s2);
8796 +}
8797 +
8798 +int DWC_STRLEN(char const *str)
8799 +{
8800 + return strlen(str);
8801 +}
8802 +
8803 +char *DWC_STRCPY(char *to, char const *from)
8804 +{
8805 + return strcpy(to, from);
8806 +}
8807 +
8808 +char *DWC_STRDUP(char const *str)
8809 +{
8810 + int len = DWC_STRLEN(str) + 1;
8811 + char *new = DWC_ALLOC_ATOMIC(len);
8812 +
8813 + if (!new) {
8814 + return NULL;
8815 + }
8816 +
8817 + DWC_MEMCPY(new, str, len);
8818 + return new;
8819 +}
8820 +
8821 +int DWC_ATOI(char *str, int32_t *value)
8822 +{
8823 + char *end = NULL;
8824 +
8825 + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
8826 + * should be equivalent on 2's complement machines
8827 + */
8828 + *value = strtoul(str, &end, 0);
8829 + if (*end == '\0') {
8830 + return 0;
8831 + }
8832 +
8833 + return -1;
8834 +}
8835 +
8836 +int DWC_ATOUI(char *str, uint32_t *value)
8837 +{
8838 + char *end = NULL;
8839 +
8840 + *value = strtoul(str, &end, 0);
8841 + if (*end == '\0') {
8842 + return 0;
8843 + }
8844 +
8845 + return -1;
8846 +}
8847 +
8848 +
8849 +#ifdef DWC_UTFLIB
8850 +/* From usbstring.c */
8851 +
8852 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
8853 +{
8854 + int count = 0;
8855 + u8 c;
8856 + u16 uchar;
8857 +
8858 + /* this insists on correct encodings, though not minimal ones.
8859 + * BUT it currently rejects legit 4-byte UTF-8 code points,
8860 + * which need surrogate pairs. (Unicode 3.1 can use them.)
8861 + */
8862 + while (len != 0 && (c = (u8) *s++) != 0) {
8863 + if (unlikely(c & 0x80)) {
8864 + // 2-byte sequence:
8865 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
8866 + if ((c & 0xe0) == 0xc0) {
8867 + uchar = (c & 0x1f) << 6;
8868 +
8869 + c = (u8) *s++;
8870 + if ((c & 0xc0) != 0xc0)
8871 + goto fail;
8872 + c &= 0x3f;
8873 + uchar |= c;
8874 +
8875 + // 3-byte sequence (most CJKV characters):
8876 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
8877 + } else if ((c & 0xf0) == 0xe0) {
8878 + uchar = (c & 0x0f) << 12;
8879 +
8880 + c = (u8) *s++;
8881 + if ((c & 0xc0) != 0xc0)
8882 + goto fail;
8883 + c &= 0x3f;
8884 + uchar |= c << 6;
8885 +
8886 + c = (u8) *s++;
8887 + if ((c & 0xc0) != 0xc0)
8888 + goto fail;
8889 + c &= 0x3f;
8890 + uchar |= c;
8891 +
8892 + /* no bogus surrogates */
8893 + if (0xd800 <= uchar && uchar <= 0xdfff)
8894 + goto fail;
8895 +
8896 + // 4-byte sequence (surrogate pairs, currently rare):
8897 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
8898 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
8899 + // (uuuuu = wwww + 1)
8900 + // FIXME accept the surrogate code points (only)
8901 + } else
8902 + goto fail;
8903 + } else
8904 + uchar = c;
8905 + put_unaligned (cpu_to_le16 (uchar), cp++);
8906 + count++;
8907 + len--;
8908 + }
8909 + return count;
8910 +fail:
8911 + return -1;
8912 +}
8913 +
8914 +#endif /* DWC_UTFLIB */
8915 +
8916 +
8917 +/* dwc_debug.h */
8918 +
8919 +dwc_bool_t DWC_IN_IRQ(void)
8920 +{
8921 +// return in_irq();
8922 + return 0;
8923 +}
8924 +
8925 +dwc_bool_t DWC_IN_BH(void)
8926 +{
8927 +// return in_softirq();
8928 + return 0;
8929 +}
8930 +
8931 +void DWC_VPRINTF(char *format, va_list args)
8932 +{
8933 + vprintf(format, args);
8934 +}
8935 +
8936 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
8937 +{
8938 + return vsnprintf(str, size, format, args);
8939 +}
8940 +
8941 +void DWC_PRINTF(char *format, ...)
8942 +{
8943 + va_list args;
8944 +
8945 + va_start(args, format);
8946 + DWC_VPRINTF(format, args);
8947 + va_end(args);
8948 +}
8949 +
8950 +int DWC_SPRINTF(char *buffer, char *format, ...)
8951 +{
8952 + int retval;
8953 + va_list args;
8954 +
8955 + va_start(args, format);
8956 + retval = vsprintf(buffer, format, args);
8957 + va_end(args);
8958 + return retval;
8959 +}
8960 +
8961 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
8962 +{
8963 + int retval;
8964 + va_list args;
8965 +
8966 + va_start(args, format);
8967 + retval = vsnprintf(buffer, size, format, args);
8968 + va_end(args);
8969 + return retval;
8970 +}
8971 +
8972 +void __DWC_WARN(char *format, ...)
8973 +{
8974 + va_list args;
8975 +
8976 + va_start(args, format);
8977 + DWC_VPRINTF(format, args);
8978 + va_end(args);
8979 +}
8980 +
8981 +void __DWC_ERROR(char *format, ...)
8982 +{
8983 + va_list args;
8984 +
8985 + va_start(args, format);
8986 + DWC_VPRINTF(format, args);
8987 + va_end(args);
8988 +}
8989 +
8990 +void DWC_EXCEPTION(char *format, ...)
8991 +{
8992 + va_list args;
8993 +
8994 + va_start(args, format);
8995 + DWC_VPRINTF(format, args);
8996 + va_end(args);
8997 +// BUG_ON(1); ???
8998 +}
8999 +
9000 +#ifdef DEBUG
9001 +void __DWC_DEBUG(char *format, ...)
9002 +{
9003 + va_list args;
9004 +
9005 + va_start(args, format);
9006 + DWC_VPRINTF(format, args);
9007 + va_end(args);
9008 +}
9009 +#endif
9010 +
9011 +
9012 +/* dwc_mem.h */
9013 +
9014 +#if 0
9015 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
9016 + uint32_t align,
9017 + uint32_t alloc)
9018 +{
9019 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
9020 + size, align, alloc);
9021 + return (dwc_pool_t *)pool;
9022 +}
9023 +
9024 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
9025 +{
9026 + dma_pool_destroy((struct dma_pool *)pool);
9027 +}
9028 +
9029 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
9030 +{
9031 +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
9032 + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
9033 +}
9034 +
9035 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
9036 +{
9037 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
9038 + memset(..);
9039 +}
9040 +
9041 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
9042 +{
9043 + dma_pool_free(pool, vaddr, daddr);
9044 +}
9045 +#endif
9046 +
9047 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
9048 +{
9049 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
9050 + int error;
9051 +
9052 + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
9053 + sizeof(dma->segs) / sizeof(dma->segs[0]),
9054 + &dma->nsegs, BUS_DMA_NOWAIT);
9055 + if (error) {
9056 + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
9057 + (uintmax_t)size, error);
9058 + goto fail_0;
9059 + }
9060 +
9061 + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
9062 + (caddr_t *)&dma->dma_vaddr,
9063 + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
9064 + if (error) {
9065 + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
9066 + goto fail_1;
9067 + }
9068 +
9069 + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
9070 + BUS_DMA_NOWAIT, &dma->dma_map);
9071 + if (error) {
9072 + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
9073 + goto fail_2;
9074 + }
9075 +
9076 + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
9077 + size, NULL, BUS_DMA_NOWAIT);
9078 + if (error) {
9079 + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
9080 + goto fail_3;
9081 + }
9082 +
9083 + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
9084 + *dma_addr = dma->dma_paddr;
9085 + return dma->dma_vaddr;
9086 +
9087 +fail_3:
9088 + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
9089 +fail_2:
9090 + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
9091 +fail_1:
9092 + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
9093 +fail_0:
9094 + dma->dma_map = NULL;
9095 + dma->dma_vaddr = NULL;
9096 + dma->nsegs = 0;
9097 +
9098 + return NULL;
9099 +}
9100 +
9101 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
9102 +{
9103 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
9104 +
9105 + if (dma->dma_map != NULL) {
9106 + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
9107 + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9108 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
9109 + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
9110 + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
9111 + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
9112 + dma->dma_paddr = 0;
9113 + dma->dma_map = NULL;
9114 + dma->dma_vaddr = NULL;
9115 + dma->nsegs = 0;
9116 + }
9117 +}
9118 +
9119 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
9120 +{
9121 + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
9122 +}
9123 +
9124 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
9125 +{
9126 + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
9127 +}
9128 +
9129 +void __DWC_FREE(void *mem_ctx, void *addr)
9130 +{
9131 + free(addr, M_DEVBUF);
9132 +}
9133 +
9134 +
9135 +#ifdef DWC_CRYPTOLIB
9136 +/* dwc_crypto.h */
9137 +
9138 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
9139 +{
9140 + get_random_bytes(buffer, length);
9141 +}
9142 +
9143 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
9144 +{
9145 + struct crypto_blkcipher *tfm;
9146 + struct blkcipher_desc desc;
9147 + struct scatterlist sgd;
9148 + struct scatterlist sgs;
9149 +
9150 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
9151 + if (tfm == NULL) {
9152 + printk("failed to load transform for aes CBC\n");
9153 + return -1;
9154 + }
9155 +
9156 + crypto_blkcipher_setkey(tfm, key, keylen);
9157 + crypto_blkcipher_set_iv(tfm, iv, 16);
9158 +
9159 + sg_init_one(&sgd, out, messagelen);
9160 + sg_init_one(&sgs, message, messagelen);
9161 +
9162 + desc.tfm = tfm;
9163 + desc.flags = 0;
9164 +
9165 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
9166 + crypto_free_blkcipher(tfm);
9167 + DWC_ERROR("AES CBC encryption failed");
9168 + return -1;
9169 + }
9170 +
9171 + crypto_free_blkcipher(tfm);
9172 + return 0;
9173 +}
9174 +
9175 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
9176 +{
9177 + struct crypto_hash *tfm;
9178 + struct hash_desc desc;
9179 + struct scatterlist sg;
9180 +
9181 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
9182 + if (IS_ERR(tfm)) {
9183 + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
9184 + return 0;
9185 + }
9186 + desc.tfm = tfm;
9187 + desc.flags = 0;
9188 +
9189 + sg_init_one(&sg, message, len);
9190 + crypto_hash_digest(&desc, &sg, len, out);
9191 + crypto_free_hash(tfm);
9192 +
9193 + return 1;
9194 +}
9195 +
9196 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
9197 + uint8_t *key, uint32_t keylen, uint8_t *out)
9198 +{
9199 + struct crypto_hash *tfm;
9200 + struct hash_desc desc;
9201 + struct scatterlist sg;
9202 +
9203 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
9204 + if (IS_ERR(tfm)) {
9205 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
9206 + return 0;
9207 + }
9208 + desc.tfm = tfm;
9209 + desc.flags = 0;
9210 +
9211 + sg_init_one(&sg, message, messagelen);
9212 + crypto_hash_setkey(tfm, key, keylen);
9213 + crypto_hash_digest(&desc, &sg, messagelen, out);
9214 + crypto_free_hash(tfm);
9215 +
9216 + return 1;
9217 +}
9218 +
9219 +#endif /* DWC_CRYPTOLIB */
9220 +
9221 +
9222 +/* Byte Ordering Conversions */
9223 +
9224 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
9225 +{
9226 +#ifdef __LITTLE_ENDIAN
9227 + return *p;
9228 +#else
9229 + uint8_t *u_p = (uint8_t *)p;
9230 +
9231 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9232 +#endif
9233 +}
9234 +
9235 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
9236 +{
9237 +#ifdef __BIG_ENDIAN
9238 + return *p;
9239 +#else
9240 + uint8_t *u_p = (uint8_t *)p;
9241 +
9242 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9243 +#endif
9244 +}
9245 +
9246 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
9247 +{
9248 +#ifdef __LITTLE_ENDIAN
9249 + return *p;
9250 +#else
9251 + uint8_t *u_p = (uint8_t *)p;
9252 +
9253 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9254 +#endif
9255 +}
9256 +
9257 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
9258 +{
9259 +#ifdef __BIG_ENDIAN
9260 + return *p;
9261 +#else
9262 + uint8_t *u_p = (uint8_t *)p;
9263 +
9264 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9265 +#endif
9266 +}
9267 +
9268 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
9269 +{
9270 +#ifdef __LITTLE_ENDIAN
9271 + return *p;
9272 +#else
9273 + uint8_t *u_p = (uint8_t *)p;
9274 + return (u_p[1] | (u_p[0] << 8));
9275 +#endif
9276 +}
9277 +
9278 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
9279 +{
9280 +#ifdef __BIG_ENDIAN
9281 + return *p;
9282 +#else
9283 + uint8_t *u_p = (uint8_t *)p;
9284 + return (u_p[1] | (u_p[0] << 8));
9285 +#endif
9286 +}
9287 +
9288 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
9289 +{
9290 +#ifdef __LITTLE_ENDIAN
9291 + return *p;
9292 +#else
9293 + uint8_t *u_p = (uint8_t *)p;
9294 + return (u_p[1] | (u_p[0] << 8));
9295 +#endif
9296 +}
9297 +
9298 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
9299 +{
9300 +#ifdef __BIG_ENDIAN
9301 + return *p;
9302 +#else
9303 + uint8_t *u_p = (uint8_t *)p;
9304 + return (u_p[1] | (u_p[0] << 8));
9305 +#endif
9306 +}
9307 +
9308 +
9309 +/* Registers */
9310 +
9311 +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
9312 +{
9313 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9314 + bus_size_t ior = (bus_size_t)reg;
9315 +
9316 + return bus_space_read_4(io->iot, io->ioh, ior);
9317 +}
9318 +
9319 +#if 0
9320 +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
9321 +{
9322 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9323 + bus_size_t ior = (bus_size_t)reg;
9324 +
9325 + return bus_space_read_8(io->iot, io->ioh, ior);
9326 +}
9327 +#endif
9328 +
9329 +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
9330 +{
9331 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9332 + bus_size_t ior = (bus_size_t)reg;
9333 +
9334 + bus_space_write_4(io->iot, io->ioh, ior, value);
9335 +}
9336 +
9337 +#if 0
9338 +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
9339 +{
9340 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9341 + bus_size_t ior = (bus_size_t)reg;
9342 +
9343 + bus_space_write_8(io->iot, io->ioh, ior, value);
9344 +}
9345 +#endif
9346 +
9347 +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
9348 + uint32_t set_mask)
9349 +{
9350 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9351 + bus_size_t ior = (bus_size_t)reg;
9352 +
9353 + bus_space_write_4(io->iot, io->ioh, ior,
9354 + (bus_space_read_4(io->iot, io->ioh, ior) &
9355 + ~clear_mask) | set_mask);
9356 +}
9357 +
9358 +#if 0
9359 +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
9360 + uint64_t set_mask)
9361 +{
9362 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9363 + bus_size_t ior = (bus_size_t)reg;
9364 +
9365 + bus_space_write_8(io->iot, io->ioh, ior,
9366 + (bus_space_read_8(io->iot, io->ioh, ior) &
9367 + ~clear_mask) | set_mask);
9368 +}
9369 +#endif
9370 +
9371 +
9372 +/* Locking */
9373 +
9374 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
9375 +{
9376 + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
9377 +
9378 + if (!sl) {
9379 + DWC_ERROR("Cannot allocate memory for spinlock");
9380 + return NULL;
9381 + }
9382 +
9383 + simple_lock_init(sl);
9384 + return (dwc_spinlock_t *)sl;
9385 +}
9386 +
9387 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
9388 +{
9389 + struct simplelock *sl = (struct simplelock *)lock;
9390 +
9391 + DWC_FREE(sl);
9392 +}
9393 +
9394 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
9395 +{
9396 + simple_lock((struct simplelock *)lock);
9397 +}
9398 +
9399 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
9400 +{
9401 + simple_unlock((struct simplelock *)lock);
9402 +}
9403 +
9404 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
9405 +{
9406 + simple_lock((struct simplelock *)lock);
9407 + *flags = splbio();
9408 +}
9409 +
9410 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
9411 +{
9412 + splx(flags);
9413 + simple_unlock((struct simplelock *)lock);
9414 +}
9415 +
9416 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
9417 +{
9418 + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
9419 +
9420 + if (!mutex) {
9421 + DWC_ERROR("Cannot allocate memory for mutex");
9422 + return NULL;
9423 + }
9424 +
9425 + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
9426 + return mutex;
9427 +}
9428 +
9429 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
9430 +#else
9431 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
9432 +{
9433 + DWC_FREE(mutex);
9434 +}
9435 +#endif
9436 +
9437 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
9438 +{
9439 + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
9440 +}
9441 +
9442 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
9443 +{
9444 + int status;
9445 +
9446 + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
9447 + return status == 0;
9448 +}
9449 +
9450 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
9451 +{
9452 + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
9453 +}
9454 +
9455 +
9456 +/* Timing */
9457 +
9458 +void DWC_UDELAY(uint32_t usecs)
9459 +{
9460 + DELAY(usecs);
9461 +}
9462 +
9463 +void DWC_MDELAY(uint32_t msecs)
9464 +{
9465 + do {
9466 + DELAY(1000);
9467 + } while (--msecs);
9468 +}
9469 +
9470 +void DWC_MSLEEP(uint32_t msecs)
9471 +{
9472 + struct timeval tv;
9473 +
9474 + tv.tv_sec = msecs / 1000;
9475 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
9476 + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
9477 +}
9478 +
9479 +uint32_t DWC_TIME(void)
9480 +{
9481 + struct timeval tv;
9482 +
9483 + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
9484 + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
9485 +}
9486 +
9487 +
9488 +/* Timers */
9489 +
9490 +struct dwc_timer {
9491 + struct callout t;
9492 + char *name;
9493 + dwc_spinlock_t *lock;
9494 + dwc_timer_callback_t cb;
9495 + void *data;
9496 +};
9497 +
9498 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
9499 +{
9500 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
9501 +
9502 + if (!t) {
9503 + DWC_ERROR("Cannot allocate memory for timer");
9504 + return NULL;
9505 + }
9506 +
9507 + callout_init(&t->t);
9508 +
9509 + t->name = DWC_STRDUP(name);
9510 + if (!t->name) {
9511 + DWC_ERROR("Cannot allocate memory for timer->name");
9512 + goto no_name;
9513 + }
9514 +
9515 + t->lock = DWC_SPINLOCK_ALLOC();
9516 + if (!t->lock) {
9517 + DWC_ERROR("Cannot allocate memory for timer->lock");
9518 + goto no_lock;
9519 + }
9520 +
9521 + t->cb = cb;
9522 + t->data = data;
9523 +
9524 + return t;
9525 +
9526 + no_lock:
9527 + DWC_FREE(t->name);
9528 + no_name:
9529 + DWC_FREE(t);
9530 +
9531 + return NULL;
9532 +}
9533 +
9534 +void DWC_TIMER_FREE(dwc_timer_t *timer)
9535 +{
9536 + callout_stop(&timer->t);
9537 + DWC_SPINLOCK_FREE(timer->lock);
9538 + DWC_FREE(timer->name);
9539 + DWC_FREE(timer);
9540 +}
9541 +
9542 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
9543 +{
9544 + struct timeval tv;
9545 +
9546 + tv.tv_sec = time / 1000;
9547 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
9548 + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
9549 +}
9550 +
9551 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
9552 +{
9553 + callout_stop(&timer->t);
9554 +}
9555 +
9556 +
9557 +/* Wait Queues */
9558 +
9559 +struct dwc_waitq {
9560 + struct simplelock lock;
9561 + int abort;
9562 +};
9563 +
9564 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
9565 +{
9566 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
9567 +
9568 + if (!wq) {
9569 + DWC_ERROR("Cannot allocate memory for waitqueue");
9570 + return NULL;
9571 + }
9572 +
9573 + simple_lock_init(&wq->lock);
9574 + wq->abort = 0;
9575 +
9576 + return wq;
9577 +}
9578 +
9579 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
9580 +{
9581 + DWC_FREE(wq);
9582 +}
9583 +
9584 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
9585 +{
9586 + int ipl;
9587 + int result = 0;
9588 +
9589 + simple_lock(&wq->lock);
9590 + ipl = splbio();
9591 +
9592 + /* Skip the sleep if already aborted or triggered */
9593 + if (!wq->abort && !cond(data)) {
9594 + splx(ipl);
9595 + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
9596 + ipl = splbio();
9597 + }
9598 +
9599 + if (result == 0) { // awoken
9600 + if (wq->abort) {
9601 + wq->abort = 0;
9602 + result = -DWC_E_ABORT;
9603 + } else {
9604 + result = 0;
9605 + }
9606 +
9607 + splx(ipl);
9608 + simple_unlock(&wq->lock);
9609 + } else {
9610 + wq->abort = 0;
9611 + splx(ipl);
9612 + simple_unlock(&wq->lock);
9613 +
9614 + if (result == ERESTART) { // signaled - restart
9615 + result = -DWC_E_RESTART;
9616 + } else { // signaled - must be EINTR
9617 + result = -DWC_E_ABORT;
9618 + }
9619 + }
9620 +
9621 + return result;
9622 +}
9623 +
9624 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
9625 + void *data, int32_t msecs)
9626 +{
9627 + struct timeval tv, tv1, tv2;
9628 + int ipl;
9629 + int result = 0;
9630 +
9631 + tv.tv_sec = msecs / 1000;
9632 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
9633 +
9634 + simple_lock(&wq->lock);
9635 + ipl = splbio();
9636 +
9637 + /* Skip the sleep if already aborted or triggered */
9638 + if (!wq->abort && !cond(data)) {
9639 + splx(ipl);
9640 + getmicrouptime(&tv1);
9641 + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
9642 + getmicrouptime(&tv2);
9643 + ipl = splbio();
9644 + }
9645 +
9646 + if (result == 0) { // awoken
9647 + if (wq->abort) {
9648 + wq->abort = 0;
9649 + splx(ipl);
9650 + simple_unlock(&wq->lock);
9651 + result = -DWC_E_ABORT;
9652 + } else {
9653 + splx(ipl);
9654 + simple_unlock(&wq->lock);
9655 +
9656 + tv2.tv_usec -= tv1.tv_usec;
9657 + if (tv2.tv_usec < 0) {
9658 + tv2.tv_usec += 1000000;
9659 + tv2.tv_sec--;
9660 + }
9661 +
9662 + tv2.tv_sec -= tv1.tv_sec;
9663 + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
9664 + result = msecs - result;
9665 + if (result <= 0)
9666 + result = 1;
9667 + }
9668 + } else {
9669 + wq->abort = 0;
9670 + splx(ipl);
9671 + simple_unlock(&wq->lock);
9672 +
9673 + if (result == ERESTART) { // signaled - restart
9674 + result = -DWC_E_RESTART;
9675 +
9676 + } else if (result == EINTR) { // signaled - interrupt
9677 + result = -DWC_E_ABORT;
9678 +
9679 + } else { // timed out
9680 + result = -DWC_E_TIMEOUT;
9681 + }
9682 + }
9683 +
9684 + return result;
9685 +}
9686 +
9687 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
9688 +{
9689 + wakeup(wq);
9690 +}
9691 +
9692 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
9693 +{
9694 + int ipl;
9695 +
9696 + simple_lock(&wq->lock);
9697 + ipl = splbio();
9698 + wq->abort = 1;
9699 + wakeup(wq);
9700 + splx(ipl);
9701 + simple_unlock(&wq->lock);
9702 +}
9703 +
9704 +
9705 +/* Threading */
9706 +
9707 +struct dwc_thread {
9708 + struct proc *proc;
9709 + int abort;
9710 +};
9711 +
9712 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
9713 +{
9714 + int retval;
9715 + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
9716 +
9717 + if (!thread) {
9718 + return NULL;
9719 + }
9720 +
9721 + thread->abort = 0;
9722 + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
9723 + "%s", name);
9724 + if (retval) {
9725 + DWC_FREE(thread);
9726 + return NULL;
9727 + }
9728 +
9729 + return thread;
9730 +}
9731 +
9732 +int DWC_THREAD_STOP(dwc_thread_t *thread)
9733 +{
9734 + int retval;
9735 +
9736 + thread->abort = 1;
9737 + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
9738 +
9739 + if (retval == 0) {
9740 + /* DWC_THREAD_EXIT() will free the thread struct */
9741 + return 0;
9742 + }
9743 +
9744 + /* NOTE: We leak the thread struct if thread doesn't die */
9745 +
9746 + if (retval == EWOULDBLOCK) {
9747 + return -DWC_E_TIMEOUT;
9748 + }
9749 +
9750 + return -DWC_E_UNKNOWN;
9751 +}
9752 +
9753 +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
9754 +{
9755 + return thread->abort;
9756 +}
9757 +
9758 +void DWC_THREAD_EXIT(dwc_thread_t *thread)
9759 +{
9760 + wakeup(&thread->abort);
9761 + DWC_FREE(thread);
9762 + kthread_exit(0);
9763 +}
9764 +
9765 +/* tasklets
9766 + - Runs in interrupt context (cannot sleep)
9767 + - Each tasklet runs on a single CPU
9768 + - Different tasklets can be running simultaneously on different CPUs
9769 + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
9770 + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
9771 + */
9772 +struct dwc_tasklet {
9773 + dwc_tasklet_callback_t cb;
9774 + void *data;
9775 +};
9776 +
9777 +static void tasklet_callback(void *data)
9778 +{
9779 + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
9780 +
9781 + task->cb(task->data);
9782 +}
9783 +
9784 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
9785 +{
9786 + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
9787 +
9788 + if (task) {
9789 + task->cb = cb;
9790 + task->data = data;
9791 + } else {
9792 + DWC_ERROR("Cannot allocate memory for tasklet");
9793 + }
9794 +
9795 + return task;
9796 +}
9797 +
9798 +void DWC_TASK_FREE(dwc_tasklet_t *task)
9799 +{
9800 + DWC_FREE(task);
9801 +}
9802 +
9803 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
9804 +{
9805 + tasklet_callback(task);
9806 +}
9807 +
9808 +
9809 +/* workqueues
9810 + - Runs in process context (can sleep)
9811 + */
9812 +typedef struct work_container {
9813 + dwc_work_callback_t cb;
9814 + void *data;
9815 + dwc_workq_t *wq;
9816 + char *name;
9817 + int hz;
9818 + struct work task;
9819 +} work_container_t;
9820 +
9821 +struct dwc_workq {
9822 + struct workqueue *taskq;
9823 + dwc_spinlock_t *lock;
9824 + dwc_waitq_t *waitq;
9825 + int pending;
9826 + struct work_container *container;
9827 +};
9828 +
9829 +static void do_work(struct work *task, void *data)
9830 +{
9831 + dwc_workq_t *wq = (dwc_workq_t *)data;
9832 + work_container_t *container = wq->container;
9833 + dwc_irqflags_t flags;
9834 +
9835 + if (container->hz) {
9836 + tsleep(container, 0, "dw3wrk", container->hz);
9837 + }
9838 +
9839 + container->cb(container->data);
9840 + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
9841 +
9842 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
9843 + if (container->name)
9844 + DWC_FREE(container->name);
9845 + DWC_FREE(container);
9846 + wq->pending--;
9847 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
9848 + DWC_WAITQ_TRIGGER(wq->waitq);
9849 +}
9850 +
9851 +static int work_done(void *data)
9852 +{
9853 + dwc_workq_t *workq = (dwc_workq_t *)data;
9854 +
9855 + return workq->pending == 0;
9856 +}
9857 +
9858 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
9859 +{
9860 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
9861 +}
9862 +
9863 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
9864 +{
9865 + int result;
9866 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
9867 +
9868 + if (!wq) {
9869 + DWC_ERROR("Cannot allocate memory for workqueue");
9870 + return NULL;
9871 + }
9872 +
9873 + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
9874 + IPL_BIO, 0);
9875 + if (result) {
9876 + DWC_ERROR("Cannot create workqueue");
9877 + goto no_taskq;
9878 + }
9879 +
9880 + wq->pending = 0;
9881 +
9882 + wq->lock = DWC_SPINLOCK_ALLOC();
9883 + if (!wq->lock) {
9884 + DWC_ERROR("Cannot allocate memory for spinlock");
9885 + goto no_lock;
9886 + }
9887 +
9888 + wq->waitq = DWC_WAITQ_ALLOC();
9889 + if (!wq->waitq) {
9890 + DWC_ERROR("Cannot allocate memory for waitqueue");
9891 + goto no_waitq;
9892 + }
9893 +
9894 + return wq;
9895 +
9896 + no_waitq:
9897 + DWC_SPINLOCK_FREE(wq->lock);
9898 + no_lock:
9899 + workqueue_destroy(wq->taskq);
9900 + no_taskq:
9901 + DWC_FREE(wq);
9902 +
9903 + return NULL;
9904 +}
9905 +
9906 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
9907 +{
9908 +#ifdef DEBUG
9909 + dwc_irqflags_t flags;
9910 +
9911 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
9912 +
9913 + if (wq->pending != 0) {
9914 + struct work_container *container = wq->container;
9915 +
9916 + DWC_ERROR("Destroying work queue with pending work");
9917 +
9918 + if (container && container->name) {
9919 + DWC_ERROR("Work %s still pending", container->name);
9920 + }
9921 + }
9922 +
9923 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
9924 +#endif
9925 + DWC_WAITQ_FREE(wq->waitq);
9926 + DWC_SPINLOCK_FREE(wq->lock);
9927 + workqueue_destroy(wq->taskq);
9928 + DWC_FREE(wq);
9929 +}
9930 +
9931 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
9932 + char *format, ...)
9933 +{
9934 + dwc_irqflags_t flags;
9935 + work_container_t *container;
9936 + static char name[128];
9937 + va_list args;
9938 +
9939 + va_start(args, format);
9940 + DWC_VSNPRINTF(name, 128, format, args);
9941 + va_end(args);
9942 +
9943 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
9944 + wq->pending++;
9945 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
9946 + DWC_WAITQ_TRIGGER(wq->waitq);
9947 +
9948 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
9949 + if (!container) {
9950 + DWC_ERROR("Cannot allocate memory for container");
9951 + return;
9952 + }
9953 +
9954 + container->name = DWC_STRDUP(name);
9955 + if (!container->name) {
9956 + DWC_ERROR("Cannot allocate memory for container->name");
9957 + DWC_FREE(container);
9958 + return;
9959 + }
9960 +
9961 + container->cb = cb;
9962 + container->data = data;
9963 + container->wq = wq;
9964 + container->hz = 0;
9965 + wq->container = container;
9966 +
9967 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
9968 + workqueue_enqueue(wq->taskq, &container->task);
9969 +}
9970 +
9971 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
9972 + void *data, uint32_t time, char *format, ...)
9973 +{
9974 + dwc_irqflags_t flags;
9975 + work_container_t *container;
9976 + static char name[128];
9977 + struct timeval tv;
9978 + va_list args;
9979 +
9980 + va_start(args, format);
9981 + DWC_VSNPRINTF(name, 128, format, args);
9982 + va_end(args);
9983 +
9984 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
9985 + wq->pending++;
9986 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
9987 + DWC_WAITQ_TRIGGER(wq->waitq);
9988 +
9989 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
9990 + if (!container) {
9991 + DWC_ERROR("Cannot allocate memory for container");
9992 + return;
9993 + }
9994 +
9995 + container->name = DWC_STRDUP(name);
9996 + if (!container->name) {
9997 + DWC_ERROR("Cannot allocate memory for container->name");
9998 + DWC_FREE(container);
9999 + return;
10000 + }
10001 +
10002 + container->cb = cb;
10003 + container->data = data;
10004 + container->wq = wq;
10005 + tv.tv_sec = time / 1000;
10006 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
10007 + container->hz = tvtohz(&tv);
10008 + wq->container = container;
10009 +
10010 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
10011 + workqueue_enqueue(wq->taskq, &container->task);
10012 +}
10013 +
10014 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
10015 +{
10016 + return wq->pending;
10017 +}
10018 --- /dev/null
10019 +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.c
10020 @@ -0,0 +1,308 @@
10021 +/* =========================================================================
10022 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
10023 + * $Revision: #5 $
10024 + * $Date: 2010/09/28 $
10025 + * $Change: 1596182 $
10026 + *
10027 + * Synopsys Portability Library Software and documentation
10028 + * (hereinafter, "Software") is an Unsupported proprietary work of
10029 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10030 + * between Synopsys and you.
10031 + *
10032 + * The Software IS NOT an item of Licensed Software or Licensed Product
10033 + * under any End User Software License Agreement or Agreement for
10034 + * Licensed Product with Synopsys or any supplement thereto. You are
10035 + * permitted to use and redistribute this Software in source and binary
10036 + * forms, with or without modification, provided that redistributions
10037 + * of source code must retain this notice. You may not view, use,
10038 + * disclose, copy or distribute this file or any information contained
10039 + * herein except pursuant to this license grant from Synopsys. If you
10040 + * do not agree with this notice, including the disclaimer below, then
10041 + * you are not authorized to use the Software.
10042 + *
10043 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10044 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10045 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10046 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10047 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10048 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10049 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10050 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10051 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10052 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10053 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10054 + * DAMAGE.
10055 + * ========================================================================= */
10056 +
10057 +/** @file
10058 + * This file contains the WUSB cryptographic routines.
10059 + */
10060 +
10061 +#ifdef DWC_CRYPTOLIB
10062 +
10063 +#include "dwc_crypto.h"
10064 +#include "usb.h"
10065 +
10066 +#ifdef DEBUG
10067 +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
10068 +{
10069 + int i;
10070 + DWC_PRINTF("%s: ", name);
10071 + for (i=0; i<len; i++) {
10072 + DWC_PRINTF("%02x ", bytes[i]);
10073 + }
10074 + DWC_PRINTF("\n");
10075 +}
10076 +#else
10077 +#define dump_bytes(x...)
10078 +#endif
10079 +
10080 +/* Display a block */
10081 +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
10082 +{
10083 +#ifdef DWC_DEBUG_CRYPTO
10084 + int i, blksize = 16;
10085 +
10086 + DWC_DEBUG("%s", prefix);
10087 +
10088 + if (suffix == NULL) {
10089 + suffix = "\n";
10090 + blksize = a;
10091 + }
10092 +
10093 + for (i = 0; i < blksize; i++)
10094 + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
10095 + DWC_PRINT(suffix);
10096 +#endif
10097 +}
10098 +
10099 +/**
10100 + * Encrypts an array of bytes using the AES encryption engine.
10101 + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
10102 + * in-place.
10103 + *
10104 + * @return 0 on success, negative error code on error.
10105 + */
10106 +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
10107 +{
10108 + u8 block_t[16];
10109 + DWC_MEMSET(block_t, 0, 16);
10110 +
10111 + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
10112 +}
10113 +
10114 +/**
10115 + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
10116 + * This function takes a data string and returns the encrypted CBC
10117 + * Counter-mode MIC.
10118 + *
10119 + * @param key The 128-bit symmetric key.
10120 + * @param nonce The CCM nonce.
10121 + * @param label The unique 14-byte ASCII text label.
10122 + * @param bytes The byte array to be encrypted.
10123 + * @param len Length of the byte array.
10124 + * @param result Byte array to receive the 8-byte encrypted MIC.
10125 + */
10126 +void dwc_wusb_cmf(u8 *key, u8 *nonce,
10127 + char *label, u8 *bytes, int len, u8 *result)
10128 +{
10129 + u8 block_m[16];
10130 + u8 block_x[16];
10131 + u8 block_t[8];
10132 + int idx, blkNum;
10133 + u16 la = (u16)(len + 14);
10134 +
10135 + /* Set the AES-128 key */
10136 + //dwc_aes_setkey(tfm, key, 16);
10137 +
10138 + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
10139 + block_m[0] = 0x59;
10140 + for (idx = 0; idx < 13; idx++)
10141 + block_m[idx + 1] = nonce[idx];
10142 + block_m[14] = 0;
10143 + block_m[15] = 0;
10144 +
10145 + /* Produce the CBC IV */
10146 + dwc_wusb_aes_encrypt(block_m, key, block_x);
10147 + show_block(block_m, "CBC IV in: ", "\n", 0);
10148 + show_block(block_x, "CBC IV out:", "\n", 0);
10149 +
10150 + /* Fill block B1 from l(a) = Blen + 14, and A */
10151 + block_x[0] ^= (u8)(la >> 8);
10152 + block_x[1] ^= (u8)la;
10153 + for (idx = 0; idx < 14; idx++)
10154 + block_x[idx + 2] ^= label[idx];
10155 + show_block(block_x, "After xor: ", "b1\n", 16);
10156 +
10157 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10158 + show_block(block_x, "After AES: ", "b1\n", 16);
10159 +
10160 + idx = 0;
10161 + blkNum = 0;
10162 +
10163 + /* Fill remaining blocks with B */
10164 + while (len-- > 0) {
10165 + block_x[idx] ^= *bytes++;
10166 + if (++idx >= 16) {
10167 + idx = 0;
10168 + show_block(block_x, "After xor: ", "\n", blkNum);
10169 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10170 + show_block(block_x, "After AES: ", "\n", blkNum);
10171 + blkNum++;
10172 + }
10173 + }
10174 +
10175 + /* Handle partial last block */
10176 + if (idx > 0) {
10177 + show_block(block_x, "After xor: ", "\n", blkNum);
10178 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10179 + show_block(block_x, "After AES: ", "\n", blkNum);
10180 + }
10181 +
10182 + /* Save the MIC tag */
10183 + DWC_MEMCPY(block_t, block_x, 8);
10184 + show_block(block_t, "MIC tag : ", NULL, 8);
10185 +
10186 + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
10187 + block_m[0] = 0x01;
10188 + block_m[14] = 0;
10189 + block_m[15] = 0;
10190 +
10191 + /* Encrypt the counter */
10192 + dwc_wusb_aes_encrypt(block_m, key, block_x);
10193 + show_block(block_x, "CTR[MIC] : ", NULL, 8);
10194 +
10195 + /* XOR with MIC tag */
10196 + for (idx = 0; idx < 8; idx++) {
10197 + block_t[idx] ^= block_x[idx];
10198 + }
10199 +
10200 + /* Return result to caller */
10201 + DWC_MEMCPY(result, block_t, 8);
10202 + show_block(result, "CCM-MIC : ", NULL, 8);
10203 +
10204 +}
10205 +
10206 +/**
10207 + * The PRF function described in section 6.5 of the WUSB spec. This function
10208 + * concatenates MIC values returned from dwc_cmf() to create a value of
10209 + * the requested length.
10210 + *
10211 + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
10212 + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
10213 + * @param result Byte array to receive the result.
10214 + */
10215 +void dwc_wusb_prf(int prf_len, u8 *key,
10216 + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
10217 +{
10218 + int i;
10219 +
10220 + nonce[0] = 0;
10221 + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
10222 + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
10223 + result += 8;
10224 + }
10225 +}
10226 +
10227 +/**
10228 + * Fills in CCM Nonce per the WUSB spec.
10229 + *
10230 + * @param[in] haddr Host address.
10231 + * @param[in] daddr Device address.
10232 + * @param[in] tkid Session Key(PTK) identifier.
10233 + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
10234 + */
10235 +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
10236 + uint8_t *nonce)
10237 +{
10238 +
10239 + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
10240 +
10241 + DWC_MEMSET(&nonce[0], 0, 16);
10242 +
10243 + DWC_MEMCPY(&nonce[6], tkid, 3);
10244 + nonce[9] = daddr & 0xFF;
10245 + nonce[10] = (daddr >> 8) & 0xFF;
10246 + nonce[11] = haddr & 0xFF;
10247 + nonce[12] = (haddr >> 8) & 0xFF;
10248 +
10249 + dump_bytes("CCM nonce", nonce, 16);
10250 +}
10251 +
10252 +/**
10253 + * Generates a 16-byte cryptographic-grade random number for the Host/Device
10254 + * Nonce.
10255 + */
10256 +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
10257 +{
10258 + uint8_t inonce[16];
10259 + uint32_t temp[4];
10260 +
10261 + /* Fill in the Nonce */
10262 + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
10263 + inonce[9] = addr & 0xFF;
10264 + inonce[10] = (addr >> 8) & 0xFF;
10265 + inonce[11] = inonce[9];
10266 + inonce[12] = inonce[10];
10267 +
10268 + /* Collect "randomness samples" */
10269 + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
10270 +
10271 + dwc_wusb_prf_128((uint8_t *)temp, nonce,
10272 + "Random Numbers", (uint8_t *)temp, sizeof(temp),
10273 + nonce);
10274 +}
10275 +
10276 +/**
10277 + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
10278 + * WUSB spec.
10279 + *
10280 + * @param[in] ccm_nonce Pointer to CCM Nonce.
10281 + * @param[in] mk Master Key to derive the session from
10282 + * @param[in] hnonce Pointer to Host Nonce.
10283 + * @param[in] dnonce Pointer to Device Nonce.
10284 + * @param[out] kck Pointer to where the KCK output is to be written.
10285 + * @param[out] ptk Pointer to where the PTK output is to be written.
10286 + */
10287 +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
10288 + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
10289 +{
10290 + uint8_t idata[32];
10291 + uint8_t odata[32];
10292 +
10293 + dump_bytes("ck", mk, 16);
10294 + dump_bytes("hnonce", hnonce, 16);
10295 + dump_bytes("dnonce", dnonce, 16);
10296 +
10297 + /* The data is the HNonce and DNonce concatenated */
10298 + DWC_MEMCPY(&idata[0], hnonce, 16);
10299 + DWC_MEMCPY(&idata[16], dnonce, 16);
10300 +
10301 + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
10302 +
10303 + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
10304 + DWC_MEMCPY(kck, &odata[0], 16);
10305 + DWC_MEMCPY(ptk, &odata[16], 16);
10306 +
10307 + dump_bytes("kck", kck, 16);
10308 + dump_bytes("ptk", ptk, 16);
10309 +}
10310 +
10311 +/**
10312 + * Generates the Message Integrity Code over the Handshake data per the
10313 + * WUSB spec.
10314 + *
10315 + * @param ccm_nonce Pointer to CCM Nonce.
10316 + * @param kck Pointer to Key Confirmation Key.
10317 + * @param data Pointer to Handshake data to be checked.
10318 + * @param mic Pointer to where the MIC output is to be written.
10319 + */
10320 +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
10321 + uint8_t *data, uint8_t *mic)
10322 +{
10323 +
10324 + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
10325 + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
10326 +}
10327 +
10328 +#endif /* DWC_CRYPTOLIB */
10329 --- /dev/null
10330 +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.h
10331 @@ -0,0 +1,111 @@
10332 +/* =========================================================================
10333 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
10334 + * $Revision: #3 $
10335 + * $Date: 2010/09/28 $
10336 + * $Change: 1596182 $
10337 + *
10338 + * Synopsys Portability Library Software and documentation
10339 + * (hereinafter, "Software") is an Unsupported proprietary work of
10340 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10341 + * between Synopsys and you.
10342 + *
10343 + * The Software IS NOT an item of Licensed Software or Licensed Product
10344 + * under any End User Software License Agreement or Agreement for
10345 + * Licensed Product with Synopsys or any supplement thereto. You are
10346 + * permitted to use and redistribute this Software in source and binary
10347 + * forms, with or without modification, provided that redistributions
10348 + * of source code must retain this notice. You may not view, use,
10349 + * disclose, copy or distribute this file or any information contained
10350 + * herein except pursuant to this license grant from Synopsys. If you
10351 + * do not agree with this notice, including the disclaimer below, then
10352 + * you are not authorized to use the Software.
10353 + *
10354 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10355 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10356 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10357 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10358 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10359 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10360 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10361 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10362 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10363 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10364 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10365 + * DAMAGE.
10366 + * ========================================================================= */
10367 +
10368 +#ifndef _DWC_CRYPTO_H_
10369 +#define _DWC_CRYPTO_H_
10370 +
10371 +#ifdef __cplusplus
10372 +extern "C" {
10373 +#endif
10374 +
10375 +/** @file
10376 + *
10377 + * This file contains declarations for the WUSB Cryptographic routines as
10378 + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
10379 + * modules.
10380 + */
10381 +
10382 +#include "dwc_os.h"
10383 +
10384 +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
10385 +
10386 +void dwc_wusb_cmf(u8 *key, u8 *nonce,
10387 + char *label, u8 *bytes, int len, u8 *result);
10388 +void dwc_wusb_prf(int prf_len, u8 *key,
10389 + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
10390 +
10391 +/**
10392 + * The PRF-64 function described in section 6.5 of the WUSB spec.
10393 + *
10394 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10395 + */
10396 +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
10397 + char *label, u8 *bytes, int len, u8 *result)
10398 +{
10399 + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
10400 +}
10401 +
10402 +/**
10403 + * The PRF-128 function described in section 6.5 of the WUSB spec.
10404 + *
10405 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10406 + */
10407 +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
10408 + char *label, u8 *bytes, int len, u8 *result)
10409 +{
10410 + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
10411 +}
10412 +
10413 +/**
10414 + * The PRF-256 function described in section 6.5 of the WUSB spec.
10415 + *
10416 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10417 + */
10418 +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
10419 + char *label, u8 *bytes, int len, u8 *result)
10420 +{
10421 + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
10422 +}
10423 +
10424 +
10425 +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
10426 + uint8_t *nonce);
10427 +void dwc_wusb_gen_nonce(uint16_t addr,
10428 + uint8_t *nonce);
10429 +
10430 +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
10431 + uint8_t *hnonce, uint8_t *dnonce,
10432 + uint8_t *kck, uint8_t *ptk);
10433 +
10434 +
10435 +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
10436 + *kck, uint8_t *data, uint8_t *mic);
10437 +
10438 +#ifdef __cplusplus
10439 +}
10440 +#endif
10441 +
10442 +#endif /* _DWC_CRYPTO_H_ */
10443 --- /dev/null
10444 +++ b/drivers/usb/host/dwc_common_port/dwc_dh.c
10445 @@ -0,0 +1,291 @@
10446 +/* =========================================================================
10447 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
10448 + * $Revision: #3 $
10449 + * $Date: 2010/09/28 $
10450 + * $Change: 1596182 $
10451 + *
10452 + * Synopsys Portability Library Software and documentation
10453 + * (hereinafter, "Software") is an Unsupported proprietary work of
10454 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10455 + * between Synopsys and you.
10456 + *
10457 + * The Software IS NOT an item of Licensed Software or Licensed Product
10458 + * under any End User Software License Agreement or Agreement for
10459 + * Licensed Product with Synopsys or any supplement thereto. You are
10460 + * permitted to use and redistribute this Software in source and binary
10461 + * forms, with or without modification, provided that redistributions
10462 + * of source code must retain this notice. You may not view, use,
10463 + * disclose, copy or distribute this file or any information contained
10464 + * herein except pursuant to this license grant from Synopsys. If you
10465 + * do not agree with this notice, including the disclaimer below, then
10466 + * you are not authorized to use the Software.
10467 + *
10468 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10469 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10470 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10471 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10472 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10473 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10474 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10475 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10476 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10477 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10478 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10479 + * DAMAGE.
10480 + * ========================================================================= */
10481 +#ifdef DWC_CRYPTOLIB
10482 +
10483 +#ifndef CONFIG_MACH_IPMATE
10484 +
10485 +#include "dwc_dh.h"
10486 +#include "dwc_modpow.h"
10487 +
10488 +#ifdef DEBUG
10489 +/* This function prints out a buffer in the format described in the Association
10490 + * Model specification. */
10491 +static void dh_dump(char *str, void *_num, int len)
10492 +{
10493 + uint8_t *num = _num;
10494 + int i;
10495 + DWC_PRINTF("%s\n", str);
10496 + for (i = 0; i < len; i ++) {
10497 + DWC_PRINTF("%02x", num[i]);
10498 + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
10499 + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
10500 + }
10501 +
10502 + DWC_PRINTF("\n");
10503 +}
10504 +#else
10505 +#define dh_dump(_x...) do {; } while(0)
10506 +#endif
10507 +
10508 +/* Constant g value */
10509 +static __u32 dh_g[] = {
10510 + 0x02000000,
10511 +};
10512 +
10513 +/* Constant p value */
10514 +static __u32 dh_p[] = {
10515 + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
10516 + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
10517 + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
10518 + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
10519 + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
10520 + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
10521 + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
10522 + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
10523 + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
10524 + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
10525 + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
10526 + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
10527 +};
10528 +
10529 +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
10530 +{
10531 + uint8_t *in = _in;
10532 + uint8_t *out = _out;
10533 + int i;
10534 + for (i=0; i<len; i++) {
10535 + out[i] = in[len-1-i];
10536 + }
10537 +}
10538 +
10539 +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
10540 + * big endian numbers of size len, in bytes. Each len value must be a multiple
10541 + * of 4. */
10542 +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
10543 + void *exp, uint32_t exp_len,
10544 + void *mod, uint32_t mod_len,
10545 + void *out)
10546 +{
10547 + /* modpow() takes little endian numbers. AM uses big-endian. This
10548 + * function swaps bytes of numbers before passing onto modpow. */
10549 +
10550 + int retval = 0;
10551 + uint32_t *result;
10552 +
10553 + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
10554 + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
10555 + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
10556 +
10557 + dh_swap_bytes(num, &bignum_num[1], num_len);
10558 + bignum_num[0] = num_len / 4;
10559 +
10560 + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
10561 + bignum_exp[0] = exp_len / 4;
10562 +
10563 + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
10564 + bignum_mod[0] = mod_len / 4;
10565 +
10566 + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
10567 + if (!result) {
10568 + retval = -1;
10569 + goto dh_modpow_nomem;
10570 + }
10571 +
10572 + dh_swap_bytes(&result[1], out, result[0] * 4);
10573 + dwc_free(mem_ctx, result);
10574 +
10575 + dh_modpow_nomem:
10576 + dwc_free(mem_ctx, bignum_num);
10577 + dwc_free(mem_ctx, bignum_exp);
10578 + dwc_free(mem_ctx, bignum_mod);
10579 + return retval;
10580 +}
10581 +
10582 +
10583 +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
10584 +{
10585 + int retval;
10586 + uint8_t m3[385];
10587 +
10588 +#ifndef DH_TEST_VECTORS
10589 + DWC_RANDOM_BYTES(exp, 32);
10590 +#endif
10591 +
10592 + /* Compute the pkd */
10593 + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
10594 + exp, 32,
10595 + dh_p, 384, pk))) {
10596 + return retval;
10597 + }
10598 +
10599 + m3[384] = nd;
10600 + DWC_MEMCPY(&m3[0], pk, 384);
10601 + DWC_SHA256(m3, 385, hash);
10602 +
10603 + dh_dump("PK", pk, 384);
10604 + dh_dump("SHA-256(M3)", hash, 32);
10605 + return 0;
10606 +}
10607 +
10608 +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
10609 + uint8_t *exp, int is_host,
10610 + char *dd, uint8_t *ck, uint8_t *kdk)
10611 +{
10612 + int retval;
10613 + uint8_t mv[784];
10614 + uint8_t sha_result[32];
10615 + uint8_t dhkey[384];
10616 + uint8_t shared_secret[384];
10617 + char *message;
10618 + uint32_t vd;
10619 +
10620 + uint8_t *pk;
10621 +
10622 + if (is_host) {
10623 + pk = pkd;
10624 + }
10625 + else {
10626 + pk = pkh;
10627 + }
10628 +
10629 + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
10630 + exp, 32,
10631 + dh_p, 384, shared_secret))) {
10632 + return retval;
10633 + }
10634 + dh_dump("Shared Secret", shared_secret, 384);
10635 +
10636 + DWC_SHA256(shared_secret, 384, dhkey);
10637 + dh_dump("DHKEY", dhkey, 384);
10638 +
10639 + DWC_MEMCPY(&mv[0], pkd, 384);
10640 + DWC_MEMCPY(&mv[384], pkh, 384);
10641 + DWC_MEMCPY(&mv[768], "displayed digest", 16);
10642 + dh_dump("MV", mv, 784);
10643 +
10644 + DWC_SHA256(mv, 784, sha_result);
10645 + dh_dump("SHA-256(MV)", sha_result, 32);
10646 + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
10647 +
10648 + dh_swap_bytes(sha_result, &vd, 4);
10649 +#ifdef DEBUG
10650 + DWC_PRINTF("Vd (decimal) = %d\n", vd);
10651 +#endif
10652 +
10653 + switch (nd) {
10654 + case 2:
10655 + vd = vd % 100;
10656 + DWC_SPRINTF(dd, "%02d", vd);
10657 + break;
10658 + case 3:
10659 + vd = vd % 1000;
10660 + DWC_SPRINTF(dd, "%03d", vd);
10661 + break;
10662 + case 4:
10663 + vd = vd % 10000;
10664 + DWC_SPRINTF(dd, "%04d", vd);
10665 + break;
10666 + }
10667 +#ifdef DEBUG
10668 + DWC_PRINTF("Display Digits: %s\n", dd);
10669 +#endif
10670 +
10671 + message = "connection key";
10672 + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
10673 + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
10674 + DWC_MEMCPY(ck, sha_result, 16);
10675 +
10676 + message = "key derivation key";
10677 + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
10678 + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
10679 + DWC_MEMCPY(kdk, sha_result, 32);
10680 +
10681 + return 0;
10682 +}
10683 +
10684 +
10685 +#ifdef DH_TEST_VECTORS
10686 +
10687 +static __u8 dh_a[] = {
10688 + 0x44, 0x00, 0x51, 0xd6,
10689 + 0xf0, 0xb5, 0x5e, 0xa9,
10690 + 0x67, 0xab, 0x31, 0xc6,
10691 + 0x8a, 0x8b, 0x5e, 0x37,
10692 + 0xd9, 0x10, 0xda, 0xe0,
10693 + 0xe2, 0xd4, 0x59, 0xa4,
10694 + 0x86, 0x45, 0x9c, 0xaa,
10695 + 0xdf, 0x36, 0x75, 0x16,
10696 +};
10697 +
10698 +static __u8 dh_b[] = {
10699 + 0x5d, 0xae, 0xc7, 0x86,
10700 + 0x79, 0x80, 0xa3, 0x24,
10701 + 0x8c, 0xe3, 0x57, 0x8f,
10702 + 0xc7, 0x5f, 0x1b, 0x0f,
10703 + 0x2d, 0xf8, 0x9d, 0x30,
10704 + 0x6f, 0xa4, 0x52, 0xcd,
10705 + 0xe0, 0x7a, 0x04, 0x8a,
10706 + 0xde, 0xd9, 0x26, 0x56,
10707 +};
10708 +
10709 +void dwc_run_dh_test_vectors(void *mem_ctx)
10710 +{
10711 + uint8_t pkd[384];
10712 + uint8_t pkh[384];
10713 + uint8_t hashd[32];
10714 + uint8_t hashh[32];
10715 + uint8_t ck[16];
10716 + uint8_t kdk[32];
10717 + char dd[5];
10718 +
10719 + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
10720 +
10721 + /* compute the PKd and SHA-256(PKd || Nd) */
10722 + DWC_PRINTF("Computing PKd\n");
10723 + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
10724 +
10725 + /* compute the PKd and SHA-256(PKh || Nd) */
10726 + DWC_PRINTF("Computing PKh\n");
10727 + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
10728 +
10729 + /* compute the dhkey */
10730 + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
10731 +}
10732 +#endif /* DH_TEST_VECTORS */
10733 +
10734 +#endif /* !CONFIG_MACH_IPMATE */
10735 +
10736 +#endif /* DWC_CRYPTOLIB */
10737 --- /dev/null
10738 +++ b/drivers/usb/host/dwc_common_port/dwc_dh.h
10739 @@ -0,0 +1,106 @@
10740 +/* =========================================================================
10741 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
10742 + * $Revision: #4 $
10743 + * $Date: 2010/09/28 $
10744 + * $Change: 1596182 $
10745 + *
10746 + * Synopsys Portability Library Software and documentation
10747 + * (hereinafter, "Software") is an Unsupported proprietary work of
10748 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10749 + * between Synopsys and you.
10750 + *
10751 + * The Software IS NOT an item of Licensed Software or Licensed Product
10752 + * under any End User Software License Agreement or Agreement for
10753 + * Licensed Product with Synopsys or any supplement thereto. You are
10754 + * permitted to use and redistribute this Software in source and binary
10755 + * forms, with or without modification, provided that redistributions
10756 + * of source code must retain this notice. You may not view, use,
10757 + * disclose, copy or distribute this file or any information contained
10758 + * herein except pursuant to this license grant from Synopsys. If you
10759 + * do not agree with this notice, including the disclaimer below, then
10760 + * you are not authorized to use the Software.
10761 + *
10762 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10763 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10764 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10765 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10766 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10767 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10768 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10769 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10770 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10771 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10772 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10773 + * DAMAGE.
10774 + * ========================================================================= */
10775 +#ifndef _DWC_DH_H_
10776 +#define _DWC_DH_H_
10777 +
10778 +#ifdef __cplusplus
10779 +extern "C" {
10780 +#endif
10781 +
10782 +#include "dwc_os.h"
10783 +
10784 +/** @file
10785 + *
10786 + * This file defines the common functions on device and host for performing
10787 + * numeric association as defined in the WUSB spec. They are only to be
10788 + * used internally by the DWC UWB modules. */
10789 +
10790 +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
10791 +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
10792 + uint8_t *key, uint32_t keylen,
10793 + uint8_t *out);
10794 +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
10795 + void *exp, uint32_t exp_len,
10796 + void *mod, uint32_t mod_len,
10797 + void *out);
10798 +
10799 +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
10800 + *
10801 + * PK = g^exp mod p.
10802 + *
10803 + * Input:
10804 + * Nd = Number of digits on the device.
10805 + *
10806 + * Output:
10807 + * exp = A 32-byte buffer to be filled with a randomly generated number.
10808 + * used as either A or B.
10809 + * pk = A 384-byte buffer to be filled with the PKH or PKD.
10810 + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
10811 + */
10812 +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
10813 +
10814 +/** Computes the DHKEY, and VD.
10815 + *
10816 + * If called from host, then it will comput DHKEY=PKD^exp % p.
10817 + * If called from device, then it will comput DHKEY=PKH^exp % p.
10818 + *
10819 + * Input:
10820 + * pkd = The PKD value.
10821 + * pkh = The PKH value.
10822 + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
10823 + * is_host = Set to non zero if a WUSB host is calling this function.
10824 + *
10825 + * Output:
10826 +
10827 + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
10828 + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
10829 + * null termination character. This buffer can be used directly for display.
10830 + * ck = A 16-byte buffer to be filled with the CK.
10831 + * kdk = A 32-byte buffer to be filled with the KDK.
10832 + */
10833 +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
10834 + uint8_t *exp, int is_host,
10835 + char *dd, uint8_t *ck, uint8_t *kdk);
10836 +
10837 +#ifdef DH_TEST_VECTORS
10838 +extern void dwc_run_dh_test_vectors(void);
10839 +#endif
10840 +
10841 +#ifdef __cplusplus
10842 +}
10843 +#endif
10844 +
10845 +#endif /* _DWC_DH_H_ */
10846 --- /dev/null
10847 +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
10848 @@ -0,0 +1,594 @@
10849 +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
10850 +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
10851 +
10852 +/*
10853 + * Copyright (c) 1991, 1993
10854 + * The Regents of the University of California. All rights reserved.
10855 + *
10856 + * Redistribution and use in source and binary forms, with or without
10857 + * modification, are permitted provided that the following conditions
10858 + * are met:
10859 + * 1. Redistributions of source code must retain the above copyright
10860 + * notice, this list of conditions and the following disclaimer.
10861 + * 2. Redistributions in binary form must reproduce the above copyright
10862 + * notice, this list of conditions and the following disclaimer in the
10863 + * documentation and/or other materials provided with the distribution.
10864 + * 3. Neither the name of the University nor the names of its contributors
10865 + * may be used to endorse or promote products derived from this software
10866 + * without specific prior written permission.
10867 + *
10868 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
10869 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10870 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
10871 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
10872 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
10873 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
10874 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10875 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
10876 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
10877 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
10878 + * SUCH DAMAGE.
10879 + *
10880 + * @(#)queue.h 8.5 (Berkeley) 8/20/94
10881 + */
10882 +
10883 +#ifndef _DWC_LIST_H_
10884 +#define _DWC_LIST_H_
10885 +
10886 +#ifdef __cplusplus
10887 +extern "C" {
10888 +#endif
10889 +
10890 +/** @file
10891 + *
10892 + * This file defines linked list operations. It is derived from BSD with
10893 + * only the MACRO names being prefixed with DWC_. This is because a few of
10894 + * these names conflict with those on Linux. For documentation on use, see the
10895 + * inline comments in the source code. The original license for this source
10896 + * code applies and is preserved in the dwc_list.h source file.
10897 + */
10898 +
10899 +/*
10900 + * This file defines five types of data structures: singly-linked lists,
10901 + * lists, simple queues, tail queues, and circular queues.
10902 + *
10903 + *
10904 + * A singly-linked list is headed by a single forward pointer. The elements
10905 + * are singly linked for minimum space and pointer manipulation overhead at
10906 + * the expense of O(n) removal for arbitrary elements. New elements can be
10907 + * added to the list after an existing element or at the head of the list.
10908 + * Elements being removed from the head of the list should use the explicit
10909 + * macro for this purpose for optimum efficiency. A singly-linked list may
10910 + * only be traversed in the forward direction. Singly-linked lists are ideal
10911 + * for applications with large datasets and few or no removals or for
10912 + * implementing a LIFO queue.
10913 + *
10914 + * A list is headed by a single forward pointer (or an array of forward
10915 + * pointers for a hash table header). The elements are doubly linked
10916 + * so that an arbitrary element can be removed without a need to
10917 + * traverse the list. New elements can be added to the list before
10918 + * or after an existing element or at the head of the list. A list
10919 + * may only be traversed in the forward direction.
10920 + *
10921 + * A simple queue is headed by a pair of pointers, one the head of the
10922 + * list and the other to the tail of the list. The elements are singly
10923 + * linked to save space, so elements can only be removed from the
10924 + * head of the list. New elements can be added to the list before or after
10925 + * an existing element, at the head of the list, or at the end of the
10926 + * list. A simple queue may only be traversed in the forward direction.
10927 + *
10928 + * A tail queue is headed by a pair of pointers, one to the head of the
10929 + * list and the other to the tail of the list. The elements are doubly
10930 + * linked so that an arbitrary element can be removed without a need to
10931 + * traverse the list. New elements can be added to the list before or
10932 + * after an existing element, at the head of the list, or at the end of
10933 + * the list. A tail queue may be traversed in either direction.
10934 + *
10935 + * A circle queue is headed by a pair of pointers, one to the head of the
10936 + * list and the other to the tail of the list. The elements are doubly
10937 + * linked so that an arbitrary element can be removed without a need to
10938 + * traverse the list. New elements can be added to the list before or after
10939 + * an existing element, at the head of the list, or at the end of the list.
10940 + * A circle queue may be traversed in either direction, but has a more
10941 + * complex end of list detection.
10942 + *
10943 + * For details on the use of these macros, see the queue(3) manual page.
10944 + */
10945 +
10946 +/*
10947 + * Double-linked List.
10948 + */
10949 +
10950 +typedef struct dwc_list_link {
10951 + struct dwc_list_link *next;
10952 + struct dwc_list_link *prev;
10953 +} dwc_list_link_t;
10954 +
10955 +#define DWC_LIST_INIT(link) do { \
10956 + (link)->next = (link); \
10957 + (link)->prev = (link); \
10958 +} while (0)
10959 +
10960 +#define DWC_LIST_FIRST(link) ((link)->next)
10961 +#define DWC_LIST_LAST(link) ((link)->prev)
10962 +#define DWC_LIST_END(link) (link)
10963 +#define DWC_LIST_NEXT(link) ((link)->next)
10964 +#define DWC_LIST_PREV(link) ((link)->prev)
10965 +#define DWC_LIST_EMPTY(link) \
10966 + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
10967 +#define DWC_LIST_ENTRY(link, type, field) \
10968 + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
10969 +
10970 +#if 0
10971 +#define DWC_LIST_INSERT_HEAD(list, link) do { \
10972 + (link)->next = (list)->next; \
10973 + (link)->prev = (list); \
10974 + (list)->next->prev = (link); \
10975 + (list)->next = (link); \
10976 +} while (0)
10977 +
10978 +#define DWC_LIST_INSERT_TAIL(list, link) do { \
10979 + (link)->next = (list); \
10980 + (link)->prev = (list)->prev; \
10981 + (list)->prev->next = (link); \
10982 + (list)->prev = (link); \
10983 +} while (0)
10984 +#else
10985 +#define DWC_LIST_INSERT_HEAD(list, link) do { \
10986 + dwc_list_link_t *__next__ = (list)->next; \
10987 + __next__->prev = (link); \
10988 + (link)->next = __next__; \
10989 + (link)->prev = (list); \
10990 + (list)->next = (link); \
10991 +} while (0)
10992 +
10993 +#define DWC_LIST_INSERT_TAIL(list, link) do { \
10994 + dwc_list_link_t *__prev__ = (list)->prev; \
10995 + (list)->prev = (link); \
10996 + (link)->next = (list); \
10997 + (link)->prev = __prev__; \
10998 + __prev__->next = (link); \
10999 +} while (0)
11000 +#endif
11001 +
11002 +#if 0
11003 +static inline void __list_add(struct list_head *new,
11004 + struct list_head *prev,
11005 + struct list_head *next)
11006 +{
11007 + next->prev = new;
11008 + new->next = next;
11009 + new->prev = prev;
11010 + prev->next = new;
11011 +}
11012 +
11013 +static inline void list_add(struct list_head *new, struct list_head *head)
11014 +{
11015 + __list_add(new, head, head->next);
11016 +}
11017 +
11018 +static inline void list_add_tail(struct list_head *new, struct list_head *head)
11019 +{
11020 + __list_add(new, head->prev, head);
11021 +}
11022 +
11023 +static inline void __list_del(struct list_head * prev, struct list_head * next)
11024 +{
11025 + next->prev = prev;
11026 + prev->next = next;
11027 +}
11028 +
11029 +static inline void list_del(struct list_head *entry)
11030 +{
11031 + __list_del(entry->prev, entry->next);
11032 + entry->next = LIST_POISON1;
11033 + entry->prev = LIST_POISON2;
11034 +}
11035 +#endif
11036 +
11037 +#define DWC_LIST_REMOVE(link) do { \
11038 + (link)->next->prev = (link)->prev; \
11039 + (link)->prev->next = (link)->next; \
11040 +} while (0)
11041 +
11042 +#define DWC_LIST_REMOVE_INIT(link) do { \
11043 + DWC_LIST_REMOVE(link); \
11044 + DWC_LIST_INIT(link); \
11045 +} while (0)
11046 +
11047 +#define DWC_LIST_MOVE_HEAD(list, link) do { \
11048 + DWC_LIST_REMOVE(link); \
11049 + DWC_LIST_INSERT_HEAD(list, link); \
11050 +} while (0)
11051 +
11052 +#define DWC_LIST_MOVE_TAIL(list, link) do { \
11053 + DWC_LIST_REMOVE(link); \
11054 + DWC_LIST_INSERT_TAIL(list, link); \
11055 +} while (0)
11056 +
11057 +#define DWC_LIST_FOREACH(var, list) \
11058 + for((var) = DWC_LIST_FIRST(list); \
11059 + (var) != DWC_LIST_END(list); \
11060 + (var) = DWC_LIST_NEXT(var))
11061 +
11062 +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
11063 + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
11064 + (var) != DWC_LIST_END(list); \
11065 + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
11066 +
11067 +#define DWC_LIST_FOREACH_REVERSE(var, list) \
11068 + for((var) = DWC_LIST_LAST(list); \
11069 + (var) != DWC_LIST_END(list); \
11070 + (var) = DWC_LIST_PREV(var))
11071 +
11072 +/*
11073 + * Singly-linked List definitions.
11074 + */
11075 +#define DWC_SLIST_HEAD(name, type) \
11076 +struct name { \
11077 + struct type *slh_first; /* first element */ \
11078 +}
11079 +
11080 +#define DWC_SLIST_HEAD_INITIALIZER(head) \
11081 + { NULL }
11082 +
11083 +#define DWC_SLIST_ENTRY(type) \
11084 +struct { \
11085 + struct type *sle_next; /* next element */ \
11086 +}
11087 +
11088 +/*
11089 + * Singly-linked List access methods.
11090 + */
11091 +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
11092 +#define DWC_SLIST_END(head) NULL
11093 +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
11094 +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
11095 +
11096 +#define DWC_SLIST_FOREACH(var, head, field) \
11097 + for((var) = SLIST_FIRST(head); \
11098 + (var) != SLIST_END(head); \
11099 + (var) = SLIST_NEXT(var, field))
11100 +
11101 +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
11102 + for((varp) = &SLIST_FIRST((head)); \
11103 + ((var) = *(varp)) != SLIST_END(head); \
11104 + (varp) = &SLIST_NEXT((var), field))
11105 +
11106 +/*
11107 + * Singly-linked List functions.
11108 + */
11109 +#define DWC_SLIST_INIT(head) { \
11110 + SLIST_FIRST(head) = SLIST_END(head); \
11111 +}
11112 +
11113 +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
11114 + (elm)->field.sle_next = (slistelm)->field.sle_next; \
11115 + (slistelm)->field.sle_next = (elm); \
11116 +} while (0)
11117 +
11118 +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
11119 + (elm)->field.sle_next = (head)->slh_first; \
11120 + (head)->slh_first = (elm); \
11121 +} while (0)
11122 +
11123 +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
11124 + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
11125 +} while (0)
11126 +
11127 +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
11128 + (head)->slh_first = (head)->slh_first->field.sle_next; \
11129 +} while (0)
11130 +
11131 +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
11132 + if ((head)->slh_first == (elm)) { \
11133 + SLIST_REMOVE_HEAD((head), field); \
11134 + } \
11135 + else { \
11136 + struct type *curelm = (head)->slh_first; \
11137 + while( curelm->field.sle_next != (elm) ) \
11138 + curelm = curelm->field.sle_next; \
11139 + curelm->field.sle_next = \
11140 + curelm->field.sle_next->field.sle_next; \
11141 + } \
11142 +} while (0)
11143 +
11144 +/*
11145 + * Simple queue definitions.
11146 + */
11147 +#define DWC_SIMPLEQ_HEAD(name, type) \
11148 +struct name { \
11149 + struct type *sqh_first; /* first element */ \
11150 + struct type **sqh_last; /* addr of last next element */ \
11151 +}
11152 +
11153 +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
11154 + { NULL, &(head).sqh_first }
11155 +
11156 +#define DWC_SIMPLEQ_ENTRY(type) \
11157 +struct { \
11158 + struct type *sqe_next; /* next element */ \
11159 +}
11160 +
11161 +/*
11162 + * Simple queue access methods.
11163 + */
11164 +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
11165 +#define DWC_SIMPLEQ_END(head) NULL
11166 +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
11167 +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
11168 +
11169 +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
11170 + for((var) = SIMPLEQ_FIRST(head); \
11171 + (var) != SIMPLEQ_END(head); \
11172 + (var) = SIMPLEQ_NEXT(var, field))
11173 +
11174 +/*
11175 + * Simple queue functions.
11176 + */
11177 +#define DWC_SIMPLEQ_INIT(head) do { \
11178 + (head)->sqh_first = NULL; \
11179 + (head)->sqh_last = &(head)->sqh_first; \
11180 +} while (0)
11181 +
11182 +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
11183 + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
11184 + (head)->sqh_last = &(elm)->field.sqe_next; \
11185 + (head)->sqh_first = (elm); \
11186 +} while (0)
11187 +
11188 +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
11189 + (elm)->field.sqe_next = NULL; \
11190 + *(head)->sqh_last = (elm); \
11191 + (head)->sqh_last = &(elm)->field.sqe_next; \
11192 +} while (0)
11193 +
11194 +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
11195 + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
11196 + (head)->sqh_last = &(elm)->field.sqe_next; \
11197 + (listelm)->field.sqe_next = (elm); \
11198 +} while (0)
11199 +
11200 +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
11201 + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
11202 + (head)->sqh_last = &(head)->sqh_first; \
11203 +} while (0)
11204 +
11205 +/*
11206 + * Tail queue definitions.
11207 + */
11208 +#define DWC_TAILQ_HEAD(name, type) \
11209 +struct name { \
11210 + struct type *tqh_first; /* first element */ \
11211 + struct type **tqh_last; /* addr of last next element */ \
11212 +}
11213 +
11214 +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
11215 + { NULL, &(head).tqh_first }
11216 +
11217 +#define DWC_TAILQ_ENTRY(type) \
11218 +struct { \
11219 + struct type *tqe_next; /* next element */ \
11220 + struct type **tqe_prev; /* address of previous next element */ \
11221 +}
11222 +
11223 +/*
11224 + * tail queue access methods
11225 + */
11226 +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
11227 +#define DWC_TAILQ_END(head) NULL
11228 +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
11229 +#define DWC_TAILQ_LAST(head, headname) \
11230 + (*(((struct headname *)((head)->tqh_last))->tqh_last))
11231 +/* XXX */
11232 +#define DWC_TAILQ_PREV(elm, headname, field) \
11233 + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
11234 +#define DWC_TAILQ_EMPTY(head) \
11235 + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
11236 +
11237 +#define DWC_TAILQ_FOREACH(var, head, field) \
11238 + for ((var) = DWC_TAILQ_FIRST(head); \
11239 + (var) != DWC_TAILQ_END(head); \
11240 + (var) = DWC_TAILQ_NEXT(var, field))
11241 +
11242 +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
11243 + for ((var) = DWC_TAILQ_LAST(head, headname); \
11244 + (var) != DWC_TAILQ_END(head); \
11245 + (var) = DWC_TAILQ_PREV(var, headname, field))
11246 +
11247 +/*
11248 + * Tail queue functions.
11249 + */
11250 +#define DWC_TAILQ_INIT(head) do { \
11251 + (head)->tqh_first = NULL; \
11252 + (head)->tqh_last = &(head)->tqh_first; \
11253 +} while (0)
11254 +
11255 +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
11256 + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
11257 + (head)->tqh_first->field.tqe_prev = \
11258 + &(elm)->field.tqe_next; \
11259 + else \
11260 + (head)->tqh_last = &(elm)->field.tqe_next; \
11261 + (head)->tqh_first = (elm); \
11262 + (elm)->field.tqe_prev = &(head)->tqh_first; \
11263 +} while (0)
11264 +
11265 +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
11266 + (elm)->field.tqe_next = NULL; \
11267 + (elm)->field.tqe_prev = (head)->tqh_last; \
11268 + *(head)->tqh_last = (elm); \
11269 + (head)->tqh_last = &(elm)->field.tqe_next; \
11270 +} while (0)
11271 +
11272 +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
11273 + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
11274 + (elm)->field.tqe_next->field.tqe_prev = \
11275 + &(elm)->field.tqe_next; \
11276 + else \
11277 + (head)->tqh_last = &(elm)->field.tqe_next; \
11278 + (listelm)->field.tqe_next = (elm); \
11279 + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
11280 +} while (0)
11281 +
11282 +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
11283 + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
11284 + (elm)->field.tqe_next = (listelm); \
11285 + *(listelm)->field.tqe_prev = (elm); \
11286 + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
11287 +} while (0)
11288 +
11289 +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
11290 + if (((elm)->field.tqe_next) != NULL) \
11291 + (elm)->field.tqe_next->field.tqe_prev = \
11292 + (elm)->field.tqe_prev; \
11293 + else \
11294 + (head)->tqh_last = (elm)->field.tqe_prev; \
11295 + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
11296 +} while (0)
11297 +
11298 +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
11299 + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
11300 + (elm2)->field.tqe_next->field.tqe_prev = \
11301 + &(elm2)->field.tqe_next; \
11302 + else \
11303 + (head)->tqh_last = &(elm2)->field.tqe_next; \
11304 + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
11305 + *(elm2)->field.tqe_prev = (elm2); \
11306 +} while (0)
11307 +
11308 +/*
11309 + * Circular queue definitions.
11310 + */
11311 +#define DWC_CIRCLEQ_HEAD(name, type) \
11312 +struct name { \
11313 + struct type *cqh_first; /* first element */ \
11314 + struct type *cqh_last; /* last element */ \
11315 +}
11316 +
11317 +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
11318 + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
11319 +
11320 +#define DWC_CIRCLEQ_ENTRY(type) \
11321 +struct { \
11322 + struct type *cqe_next; /* next element */ \
11323 + struct type *cqe_prev; /* previous element */ \
11324 +}
11325 +
11326 +/*
11327 + * Circular queue access methods
11328 + */
11329 +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
11330 +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
11331 +#define DWC_CIRCLEQ_END(head) ((void *)(head))
11332 +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
11333 +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
11334 +#define DWC_CIRCLEQ_EMPTY(head) \
11335 + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
11336 +
11337 +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
11338 +
11339 +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
11340 + for((var) = DWC_CIRCLEQ_FIRST(head); \
11341 + (var) != DWC_CIRCLEQ_END(head); \
11342 + (var) = DWC_CIRCLEQ_NEXT(var, field))
11343 +
11344 +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
11345 + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
11346 + (var) != DWC_CIRCLEQ_END(head); \
11347 + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
11348 +
11349 +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
11350 + for((var) = DWC_CIRCLEQ_LAST(head); \
11351 + (var) != DWC_CIRCLEQ_END(head); \
11352 + (var) = DWC_CIRCLEQ_PREV(var, field))
11353 +
11354 +/*
11355 + * Circular queue functions.
11356 + */
11357 +#define DWC_CIRCLEQ_INIT(head) do { \
11358 + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
11359 + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
11360 +} while (0)
11361 +
11362 +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
11363 + (elm)->field.cqe_next = NULL; \
11364 + (elm)->field.cqe_prev = NULL; \
11365 +} while (0)
11366 +
11367 +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
11368 + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
11369 + (elm)->field.cqe_prev = (listelm); \
11370 + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
11371 + (head)->cqh_last = (elm); \
11372 + else \
11373 + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
11374 + (listelm)->field.cqe_next = (elm); \
11375 +} while (0)
11376 +
11377 +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
11378 + (elm)->field.cqe_next = (listelm); \
11379 + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
11380 + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
11381 + (head)->cqh_first = (elm); \
11382 + else \
11383 + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
11384 + (listelm)->field.cqe_prev = (elm); \
11385 +} while (0)
11386 +
11387 +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
11388 + (elm)->field.cqe_next = (head)->cqh_first; \
11389 + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
11390 + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
11391 + (head)->cqh_last = (elm); \
11392 + else \
11393 + (head)->cqh_first->field.cqe_prev = (elm); \
11394 + (head)->cqh_first = (elm); \
11395 +} while (0)
11396 +
11397 +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
11398 + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
11399 + (elm)->field.cqe_prev = (head)->cqh_last; \
11400 + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
11401 + (head)->cqh_first = (elm); \
11402 + else \
11403 + (head)->cqh_last->field.cqe_next = (elm); \
11404 + (head)->cqh_last = (elm); \
11405 +} while (0)
11406 +
11407 +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
11408 + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
11409 + (head)->cqh_last = (elm)->field.cqe_prev; \
11410 + else \
11411 + (elm)->field.cqe_next->field.cqe_prev = \
11412 + (elm)->field.cqe_prev; \
11413 + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
11414 + (head)->cqh_first = (elm)->field.cqe_next; \
11415 + else \
11416 + (elm)->field.cqe_prev->field.cqe_next = \
11417 + (elm)->field.cqe_next; \
11418 +} while (0)
11419 +
11420 +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
11421 + DWC_CIRCLEQ_REMOVE(head, elm, field); \
11422 + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
11423 +} while (0)
11424 +
11425 +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
11426 + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
11427 + DWC_CIRCLEQ_END(head)) \
11428 + (head).cqh_last = (elm2); \
11429 + else \
11430 + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
11431 + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
11432 + DWC_CIRCLEQ_END(head)) \
11433 + (head).cqh_first = (elm2); \
11434 + else \
11435 + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
11436 +} while (0)
11437 +
11438 +#ifdef __cplusplus
11439 +}
11440 +#endif
11441 +
11442 +#endif /* _DWC_LIST_H_ */
11443 --- /dev/null
11444 +++ b/drivers/usb/host/dwc_common_port/dwc_mem.c
11445 @@ -0,0 +1,245 @@
11446 +/* Memory Debugging */
11447 +#ifdef DWC_DEBUG_MEMORY
11448 +
11449 +#include "dwc_os.h"
11450 +#include "dwc_list.h"
11451 +
11452 +struct allocation {
11453 + void *addr;
11454 + void *ctx;
11455 + char *func;
11456 + int line;
11457 + uint32_t size;
11458 + int dma;
11459 + DWC_CIRCLEQ_ENTRY(allocation) entry;
11460 +};
11461 +
11462 +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
11463 +
11464 +struct allocation_manager {
11465 + void *mem_ctx;
11466 + struct allocation_queue allocations;
11467 +
11468 + /* statistics */
11469 + int num;
11470 + int num_freed;
11471 + int num_active;
11472 + uint32_t total;
11473 + uint32_t cur;
11474 + uint32_t max;
11475 +};
11476 +
11477 +static struct allocation_manager *manager = NULL;
11478 +
11479 +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
11480 + int dma)
11481 +{
11482 + struct allocation *a;
11483 +
11484 + DWC_ASSERT(manager != NULL, "manager not allocated");
11485 +
11486 + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
11487 + if (!a) {
11488 + return -DWC_E_NO_MEMORY;
11489 + }
11490 +
11491 + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
11492 + if (!a->func) {
11493 + __DWC_FREE(manager->mem_ctx, a);
11494 + return -DWC_E_NO_MEMORY;
11495 + }
11496 +
11497 + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
11498 + a->addr = addr;
11499 + a->ctx = ctx;
11500 + a->line = line;
11501 + a->size = size;
11502 + a->dma = dma;
11503 + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
11504 +
11505 + /* Update stats */
11506 + manager->num++;
11507 + manager->num_active++;
11508 + manager->total += size;
11509 + manager->cur += size;
11510 +
11511 + if (manager->max < manager->cur) {
11512 + manager->max = manager->cur;
11513 + }
11514 +
11515 + return 0;
11516 +}
11517 +
11518 +static struct allocation *find_allocation(void *ctx, void *addr)
11519 +{
11520 + struct allocation *a;
11521 +
11522 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11523 + if (a->ctx == ctx && a->addr == addr) {
11524 + return a;
11525 + }
11526 + }
11527 +
11528 + return NULL;
11529 +}
11530 +
11531 +static void free_allocation(void *ctx, void *addr, char const *func, int line)
11532 +{
11533 + struct allocation *a = find_allocation(ctx, addr);
11534 +
11535 + if (!a) {
11536 + DWC_ASSERT(0,
11537 + "Free of address %p that was never allocated or already freed %s:%d",
11538 + addr, func, line);
11539 + return;
11540 + }
11541 +
11542 + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
11543 +
11544 + manager->num_active--;
11545 + manager->num_freed++;
11546 + manager->cur -= a->size;
11547 + __DWC_FREE(manager->mem_ctx, a->func);
11548 + __DWC_FREE(manager->mem_ctx, a);
11549 +}
11550 +
11551 +int dwc_memory_debug_start(void *mem_ctx)
11552 +{
11553 + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
11554 +
11555 + if (manager) {
11556 + return -DWC_E_BUSY;
11557 + }
11558 +
11559 + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
11560 + if (!manager) {
11561 + return -DWC_E_NO_MEMORY;
11562 + }
11563 +
11564 + DWC_CIRCLEQ_INIT(&manager->allocations);
11565 + manager->mem_ctx = mem_ctx;
11566 + manager->num = 0;
11567 + manager->num_freed = 0;
11568 + manager->num_active = 0;
11569 + manager->total = 0;
11570 + manager->cur = 0;
11571 + manager->max = 0;
11572 +
11573 + return 0;
11574 +}
11575 +
11576 +void dwc_memory_debug_stop(void)
11577 +{
11578 + struct allocation *a;
11579 +
11580 + dwc_memory_debug_report();
11581 +
11582 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11583 + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
11584 + free_allocation(a->ctx, a->addr, NULL, -1);
11585 + }
11586 +
11587 + __DWC_FREE(manager->mem_ctx, manager);
11588 +}
11589 +
11590 +void dwc_memory_debug_report(void)
11591 +{
11592 + struct allocation *a;
11593 +
11594 + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
11595 + DWC_PRINTF("Num Allocations = %d\n", manager->num);
11596 + DWC_PRINTF("Freed = %d\n", manager->num_freed);
11597 + DWC_PRINTF("Active = %d\n", manager->num_active);
11598 + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
11599 + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
11600 + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
11601 + DWC_PRINTF("Unfreed allocations:\n");
11602 +
11603 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11604 + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
11605 + a->addr, a->size, a->func, a->line, a->dma);
11606 + }
11607 +}
11608 +
11609 +/* The replacement functions */
11610 +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
11611 +{
11612 + void *addr = __DWC_ALLOC(mem_ctx, size);
11613 +
11614 + if (!addr) {
11615 + return NULL;
11616 + }
11617 +
11618 + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
11619 + __DWC_FREE(mem_ctx, addr);
11620 + return NULL;
11621 + }
11622 +
11623 + return addr;
11624 +}
11625 +
11626 +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
11627 + int line)
11628 +{
11629 + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
11630 +
11631 + if (!addr) {
11632 + return NULL;
11633 + }
11634 +
11635 + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
11636 + __DWC_FREE(mem_ctx, addr);
11637 + return NULL;
11638 + }
11639 +
11640 + return addr;
11641 +}
11642 +
11643 +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
11644 +{
11645 + free_allocation(mem_ctx, addr, func, line);
11646 + __DWC_FREE(mem_ctx, addr);
11647 +}
11648 +
11649 +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
11650 + char const *func, int line)
11651 +{
11652 + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
11653 +
11654 + if (!addr) {
11655 + return NULL;
11656 + }
11657 +
11658 + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
11659 + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
11660 + return NULL;
11661 + }
11662 +
11663 + return addr;
11664 +}
11665 +
11666 +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
11667 + dwc_dma_t *dma_addr, char const *func, int line)
11668 +{
11669 + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
11670 +
11671 + if (!addr) {
11672 + return NULL;
11673 + }
11674 +
11675 + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
11676 + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
11677 + return NULL;
11678 + }
11679 +
11680 + return addr;
11681 +}
11682 +
11683 +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
11684 + dwc_dma_t dma_addr, char const *func, int line)
11685 +{
11686 + free_allocation(dma_ctx, virt_addr, func, line);
11687 + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
11688 +}
11689 +
11690 +#endif /* DWC_DEBUG_MEMORY */
11691 --- /dev/null
11692 +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.c
11693 @@ -0,0 +1,636 @@
11694 +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
11695 + *
11696 + * PuTTY is copyright 1997-2007 Simon Tatham.
11697 + *
11698 + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
11699 + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
11700 + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
11701 + * Kuhn, and CORE SDI S.A.
11702 + *
11703 + * Permission is hereby granted, free of charge, to any person
11704 + * obtaining a copy of this software and associated documentation files
11705 + * (the "Software"), to deal in the Software without restriction,
11706 + * including without limitation the rights to use, copy, modify, merge,
11707 + * publish, distribute, sublicense, and/or sell copies of the Software,
11708 + * and to permit persons to whom the Software is furnished to do so,
11709 + * subject to the following conditions:
11710 + *
11711 + * The above copyright notice and this permission notice shall be
11712 + * included in all copies or substantial portions of the Software.
11713 +
11714 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11715 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11716 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11717 + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
11718 + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
11719 + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
11720 + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11721 + *
11722 + */
11723 +#ifdef DWC_CRYPTOLIB
11724 +
11725 +#ifndef CONFIG_MACH_IPMATE
11726 +
11727 +#include "dwc_modpow.h"
11728 +
11729 +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
11730 +#define BIGNUM_TOP_BIT 0x80000000UL
11731 +#define BIGNUM_INT_BITS 32
11732 +
11733 +
11734 +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
11735 +{
11736 + void *p;
11737 + size *= n;
11738 + if (size == 0) size = 1;
11739 + p = dwc_alloc(mem_ctx, size);
11740 + return p;
11741 +}
11742 +
11743 +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
11744 +#define sfree dwc_free
11745 +
11746 +/*
11747 + * Usage notes:
11748 + * * Do not call the DIVMOD_WORD macro with expressions such as array
11749 + * subscripts, as some implementations object to this (see below).
11750 + * * Note that none of the division methods below will cope if the
11751 + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
11752 + * to avoid this case.
11753 + * If this condition occurs, in the case of the x86 DIV instruction,
11754 + * an overflow exception will occur, which (according to a correspondent)
11755 + * will manifest on Windows as something like
11756 + * 0xC0000095: Integer overflow
11757 + * The C variant won't give the right answer, either.
11758 + */
11759 +
11760 +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
11761 +
11762 +#if defined __GNUC__ && defined __i386__
11763 +#define DIVMOD_WORD(q, r, hi, lo, w) \
11764 + __asm__("div %2" : \
11765 + "=d" (r), "=a" (q) : \
11766 + "r" (w), "d" (hi), "a" (lo))
11767 +#else
11768 +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
11769 + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
11770 + q = n / w; \
11771 + r = n % w; \
11772 +} while (0)
11773 +#endif
11774 +
11775 +// q = n / w;
11776 +// r = n % w;
11777 +
11778 +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
11779 +
11780 +#define BIGNUM_INTERNAL
11781 +
11782 +static Bignum newbn(void *mem_ctx, int length)
11783 +{
11784 + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
11785 + //if (!b)
11786 + //abort(); /* FIXME */
11787 + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
11788 + b[0] = length;
11789 + return b;
11790 +}
11791 +
11792 +void freebn(void *mem_ctx, Bignum b)
11793 +{
11794 + /*
11795 + * Burn the evidence, just in case.
11796 + */
11797 + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
11798 + sfree(mem_ctx, b);
11799 +}
11800 +
11801 +/*
11802 + * Compute c = a * b.
11803 + * Input is in the first len words of a and b.
11804 + * Result is returned in the first 2*len words of c.
11805 + */
11806 +static void internal_mul(BignumInt *a, BignumInt *b,
11807 + BignumInt *c, int len)
11808 +{
11809 + int i, j;
11810 + BignumDblInt t;
11811 +
11812 + for (j = 0; j < 2 * len; j++)
11813 + c[j] = 0;
11814 +
11815 + for (i = len - 1; i >= 0; i--) {
11816 + t = 0;
11817 + for (j = len - 1; j >= 0; j--) {
11818 + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
11819 + t += (BignumDblInt) c[i + j + 1];
11820 + c[i + j + 1] = (BignumInt) t;
11821 + t = t >> BIGNUM_INT_BITS;
11822 + }
11823 + c[i] = (BignumInt) t;
11824 + }
11825 +}
11826 +
11827 +static void internal_add_shifted(BignumInt *number,
11828 + unsigned n, int shift)
11829 +{
11830 + int word = 1 + (shift / BIGNUM_INT_BITS);
11831 + int bshift = shift % BIGNUM_INT_BITS;
11832 + BignumDblInt addend;
11833 +
11834 + addend = (BignumDblInt)n << bshift;
11835 +
11836 + while (addend) {
11837 + addend += number[word];
11838 + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
11839 + addend >>= BIGNUM_INT_BITS;
11840 + word++;
11841 + }
11842 +}
11843 +
11844 +/*
11845 + * Compute a = a % m.
11846 + * Input in first alen words of a and first mlen words of m.
11847 + * Output in first alen words of a
11848 + * (of which first alen-mlen words will be zero).
11849 + * The MSW of m MUST have its high bit set.
11850 + * Quotient is accumulated in the `quotient' array, which is a Bignum
11851 + * rather than the internal bigendian format. Quotient parts are shifted
11852 + * left by `qshift' before adding into quot.
11853 + */
11854 +static void internal_mod(BignumInt *a, int alen,
11855 + BignumInt *m, int mlen,
11856 + BignumInt *quot, int qshift)
11857 +{
11858 + BignumInt m0, m1;
11859 + unsigned int h;
11860 + int i, k;
11861 +
11862 + m0 = m[0];
11863 + if (mlen > 1)
11864 + m1 = m[1];
11865 + else
11866 + m1 = 0;
11867 +
11868 + for (i = 0; i <= alen - mlen; i++) {
11869 + BignumDblInt t;
11870 + unsigned int q, r, c, ai1;
11871 +
11872 + if (i == 0) {
11873 + h = 0;
11874 + } else {
11875 + h = a[i - 1];
11876 + a[i - 1] = 0;
11877 + }
11878 +
11879 + if (i == alen - 1)
11880 + ai1 = 0;
11881 + else
11882 + ai1 = a[i + 1];
11883 +
11884 + /* Find q = h:a[i] / m0 */
11885 + if (h >= m0) {
11886 + /*
11887 + * Special case.
11888 + *
11889 + * To illustrate it, suppose a BignumInt is 8 bits, and
11890 + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
11891 + * our initial division will be 0xA123 / 0xA1, which
11892 + * will give a quotient of 0x100 and a divide overflow.
11893 + * However, the invariants in this division algorithm
11894 + * are not violated, since the full number A1:23:... is
11895 + * _less_ than the quotient prefix A1:B2:... and so the
11896 + * following correction loop would have sorted it out.
11897 + *
11898 + * In this situation we set q to be the largest
11899 + * quotient we _can_ stomach (0xFF, of course).
11900 + */
11901 + q = BIGNUM_INT_MASK;
11902 + } else {
11903 + /* Macro doesn't want an array subscript expression passed
11904 + * into it (see definition), so use a temporary. */
11905 + BignumInt tmplo = a[i];
11906 + DIVMOD_WORD(q, r, h, tmplo, m0);
11907 +
11908 + /* Refine our estimate of q by looking at
11909 + h:a[i]:a[i+1] / m0:m1 */
11910 + t = MUL_WORD(m1, q);
11911 + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
11912 + q--;
11913 + t -= m1;
11914 + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
11915 + if (r >= (BignumDblInt) m0 &&
11916 + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
11917 + }
11918 + }
11919 +
11920 + /* Subtract q * m from a[i...] */
11921 + c = 0;
11922 + for (k = mlen - 1; k >= 0; k--) {
11923 + t = MUL_WORD(q, m[k]);
11924 + t += c;
11925 + c = (unsigned)(t >> BIGNUM_INT_BITS);
11926 + if ((BignumInt) t > a[i + k])
11927 + c++;
11928 + a[i + k] -= (BignumInt) t;
11929 + }
11930 +
11931 + /* Add back m in case of borrow */
11932 + if (c != h) {
11933 + t = 0;
11934 + for (k = mlen - 1; k >= 0; k--) {
11935 + t += m[k];
11936 + t += a[i + k];
11937 + a[i + k] = (BignumInt) t;
11938 + t = t >> BIGNUM_INT_BITS;
11939 + }
11940 + q--;
11941 + }
11942 + if (quot)
11943 + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
11944 + }
11945 +}
11946 +
11947 +/*
11948 + * Compute p % mod.
11949 + * The most significant word of mod MUST be non-zero.
11950 + * We assume that the result array is the same size as the mod array.
11951 + * We optionally write out a quotient if `quotient' is non-NULL.
11952 + * We can avoid writing out the result if `result' is NULL.
11953 + */
11954 +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
11955 +{
11956 + BignumInt *n, *m;
11957 + int mshift;
11958 + int plen, mlen, i, j;
11959 +
11960 + /* Allocate m of size mlen, copy mod to m */
11961 + /* We use big endian internally */
11962 + mlen = mod[0];
11963 + m = snewn(mem_ctx, mlen, BignumInt);
11964 + //if (!m)
11965 + //abort(); /* FIXME */
11966 + for (j = 0; j < mlen; j++)
11967 + m[j] = mod[mod[0] - j];
11968 +
11969 + /* Shift m left to make msb bit set */
11970 + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
11971 + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
11972 + break;
11973 + if (mshift) {
11974 + for (i = 0; i < mlen - 1; i++)
11975 + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
11976 + m[mlen - 1] = m[mlen - 1] << mshift;
11977 + }
11978 +
11979 + plen = p[0];
11980 + /* Ensure plen > mlen */
11981 + if (plen <= mlen)
11982 + plen = mlen + 1;
11983 +
11984 + /* Allocate n of size plen, copy p to n */
11985 + n = snewn(mem_ctx, plen, BignumInt);
11986 + //if (!n)
11987 + //abort(); /* FIXME */
11988 + for (j = 0; j < plen; j++)
11989 + n[j] = 0;
11990 + for (j = 1; j <= (int)p[0]; j++)
11991 + n[plen - j] = p[j];
11992 +
11993 + /* Main computation */
11994 + internal_mod(n, plen, m, mlen, quotient, mshift);
11995 +
11996 + /* Fixup result in case the modulus was shifted */
11997 + if (mshift) {
11998 + for (i = plen - mlen - 1; i < plen - 1; i++)
11999 + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
12000 + n[plen - 1] = n[plen - 1] << mshift;
12001 + internal_mod(n, plen, m, mlen, quotient, 0);
12002 + for (i = plen - 1; i >= plen - mlen; i--)
12003 + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
12004 + }
12005 +
12006 + /* Copy result to buffer */
12007 + if (result) {
12008 + for (i = 1; i <= (int)result[0]; i++) {
12009 + int j = plen - i;
12010 + result[i] = j >= 0 ? n[j] : 0;
12011 + }
12012 + }
12013 +
12014 + /* Free temporary arrays */
12015 + for (i = 0; i < mlen; i++)
12016 + m[i] = 0;
12017 + sfree(mem_ctx, m);
12018 + for (i = 0; i < plen; i++)
12019 + n[i] = 0;
12020 + sfree(mem_ctx, n);
12021 +}
12022 +
12023 +/*
12024 + * Simple remainder.
12025 + */
12026 +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
12027 +{
12028 + Bignum r = newbn(mem_ctx, b[0]);
12029 + bigdivmod(mem_ctx, a, b, r, NULL);
12030 + return r;
12031 +}
12032 +
12033 +/*
12034 + * Compute (base ^ exp) % mod.
12035 + */
12036 +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
12037 +{
12038 + BignumInt *a, *b, *n, *m;
12039 + int mshift;
12040 + int mlen, i, j;
12041 + Bignum base, result;
12042 +
12043 + /*
12044 + * The most significant word of mod needs to be non-zero. It
12045 + * should already be, but let's make sure.
12046 + */
12047 + //assert(mod[mod[0]] != 0);
12048 +
12049 + /*
12050 + * Make sure the base is smaller than the modulus, by reducing
12051 + * it modulo the modulus if not.
12052 + */
12053 + base = bigmod(mem_ctx, base_in, mod);
12054 +
12055 + /* Allocate m of size mlen, copy mod to m */
12056 + /* We use big endian internally */
12057 + mlen = mod[0];
12058 + m = snewn(mem_ctx, mlen, BignumInt);
12059 + //if (!m)
12060 + //abort(); /* FIXME */
12061 + for (j = 0; j < mlen; j++)
12062 + m[j] = mod[mod[0] - j];
12063 +
12064 + /* Shift m left to make msb bit set */
12065 + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
12066 + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
12067 + break;
12068 + if (mshift) {
12069 + for (i = 0; i < mlen - 1; i++)
12070 + m[i] =
12071 + (m[i] << mshift) | (m[i + 1] >>
12072 + (BIGNUM_INT_BITS - mshift));
12073 + m[mlen - 1] = m[mlen - 1] << mshift;
12074 + }
12075 +
12076 + /* Allocate n of size mlen, copy base to n */
12077 + n = snewn(mem_ctx, mlen, BignumInt);
12078 + //if (!n)
12079 + //abort(); /* FIXME */
12080 + i = mlen - base[0];
12081 + for (j = 0; j < i; j++)
12082 + n[j] = 0;
12083 + for (j = 0; j < base[0]; j++)
12084 + n[i + j] = base[base[0] - j];
12085 +
12086 + /* Allocate a and b of size 2*mlen. Set a = 1 */
12087 + a = snewn(mem_ctx, 2 * mlen, BignumInt);
12088 + //if (!a)
12089 + //abort(); /* FIXME */
12090 + b = snewn(mem_ctx, 2 * mlen, BignumInt);
12091 + //if (!b)
12092 + //abort(); /* FIXME */
12093 + for (i = 0; i < 2 * mlen; i++)
12094 + a[i] = 0;
12095 + a[2 * mlen - 1] = 1;
12096 +
12097 + /* Skip leading zero bits of exp. */
12098 + i = 0;
12099 + j = BIGNUM_INT_BITS - 1;
12100 + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
12101 + j--;
12102 + if (j < 0) {
12103 + i++;
12104 + j = BIGNUM_INT_BITS - 1;
12105 + }
12106 + }
12107 +
12108 + /* Main computation */
12109 + while (i < exp[0]) {
12110 + while (j >= 0) {
12111 + internal_mul(a + mlen, a + mlen, b, mlen);
12112 + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
12113 + if ((exp[exp[0] - i] & (1 << j)) != 0) {
12114 + internal_mul(b + mlen, n, a, mlen);
12115 + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
12116 + } else {
12117 + BignumInt *t;
12118 + t = a;
12119 + a = b;
12120 + b = t;
12121 + }
12122 + j--;
12123 + }
12124 + i++;
12125 + j = BIGNUM_INT_BITS - 1;
12126 + }
12127 +
12128 + /* Fixup result in case the modulus was shifted */
12129 + if (mshift) {
12130 + for (i = mlen - 1; i < 2 * mlen - 1; i++)
12131 + a[i] =
12132 + (a[i] << mshift) | (a[i + 1] >>
12133 + (BIGNUM_INT_BITS - mshift));
12134 + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
12135 + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
12136 + for (i = 2 * mlen - 1; i >= mlen; i--)
12137 + a[i] =
12138 + (a[i] >> mshift) | (a[i - 1] <<
12139 + (BIGNUM_INT_BITS - mshift));
12140 + }
12141 +
12142 + /* Copy result to buffer */
12143 + result = newbn(mem_ctx, mod[0]);
12144 + for (i = 0; i < mlen; i++)
12145 + result[result[0] - i] = a[i + mlen];
12146 + while (result[0] > 1 && result[result[0]] == 0)
12147 + result[0]--;
12148 +
12149 + /* Free temporary arrays */
12150 + for (i = 0; i < 2 * mlen; i++)
12151 + a[i] = 0;
12152 + sfree(mem_ctx, a);
12153 + for (i = 0; i < 2 * mlen; i++)
12154 + b[i] = 0;
12155 + sfree(mem_ctx, b);
12156 + for (i = 0; i < mlen; i++)
12157 + m[i] = 0;
12158 + sfree(mem_ctx, m);
12159 + for (i = 0; i < mlen; i++)
12160 + n[i] = 0;
12161 + sfree(mem_ctx, n);
12162 +
12163 + freebn(mem_ctx, base);
12164 +
12165 + return result;
12166 +}
12167 +
12168 +
12169 +#ifdef UNITTEST
12170 +
12171 +static __u32 dh_p[] = {
12172 + 96,
12173 + 0xFFFFFFFF,
12174 + 0xFFFFFFFF,
12175 + 0xA93AD2CA,
12176 + 0x4B82D120,
12177 + 0xE0FD108E,
12178 + 0x43DB5BFC,
12179 + 0x74E5AB31,
12180 + 0x08E24FA0,
12181 + 0xBAD946E2,
12182 + 0x770988C0,
12183 + 0x7A615D6C,
12184 + 0xBBE11757,
12185 + 0x177B200C,
12186 + 0x521F2B18,
12187 + 0x3EC86A64,
12188 + 0xD8760273,
12189 + 0xD98A0864,
12190 + 0xF12FFA06,
12191 + 0x1AD2EE6B,
12192 + 0xCEE3D226,
12193 + 0x4A25619D,
12194 + 0x1E8C94E0,
12195 + 0xDB0933D7,
12196 + 0xABF5AE8C,
12197 + 0xA6E1E4C7,
12198 + 0xB3970F85,
12199 + 0x5D060C7D,
12200 + 0x8AEA7157,
12201 + 0x58DBEF0A,
12202 + 0xECFB8504,
12203 + 0xDF1CBA64,
12204 + 0xA85521AB,
12205 + 0x04507A33,
12206 + 0xAD33170D,
12207 + 0x8AAAC42D,
12208 + 0x15728E5A,
12209 + 0x98FA0510,
12210 + 0x15D22618,
12211 + 0xEA956AE5,
12212 + 0x3995497C,
12213 + 0x95581718,
12214 + 0xDE2BCBF6,
12215 + 0x6F4C52C9,
12216 + 0xB5C55DF0,
12217 + 0xEC07A28F,
12218 + 0x9B2783A2,
12219 + 0x180E8603,
12220 + 0xE39E772C,
12221 + 0x2E36CE3B,
12222 + 0x32905E46,
12223 + 0xCA18217C,
12224 + 0xF1746C08,
12225 + 0x4ABC9804,
12226 + 0x670C354E,
12227 + 0x7096966D,
12228 + 0x9ED52907,
12229 + 0x208552BB,
12230 + 0x1C62F356,
12231 + 0xDCA3AD96,
12232 + 0x83655D23,
12233 + 0xFD24CF5F,
12234 + 0x69163FA8,
12235 + 0x1C55D39A,
12236 + 0x98DA4836,
12237 + 0xA163BF05,
12238 + 0xC2007CB8,
12239 + 0xECE45B3D,
12240 + 0x49286651,
12241 + 0x7C4B1FE6,
12242 + 0xAE9F2411,
12243 + 0x5A899FA5,
12244 + 0xEE386BFB,
12245 + 0xF406B7ED,
12246 + 0x0BFF5CB6,
12247 + 0xA637ED6B,
12248 + 0xF44C42E9,
12249 + 0x625E7EC6,
12250 + 0xE485B576,
12251 + 0x6D51C245,
12252 + 0x4FE1356D,
12253 + 0xF25F1437,
12254 + 0x302B0A6D,
12255 + 0xCD3A431B,
12256 + 0xEF9519B3,
12257 + 0x8E3404DD,
12258 + 0x514A0879,
12259 + 0x3B139B22,
12260 + 0x020BBEA6,
12261 + 0x8A67CC74,
12262 + 0x29024E08,
12263 + 0x80DC1CD1,
12264 + 0xC4C6628B,
12265 + 0x2168C234,
12266 + 0xC90FDAA2,
12267 + 0xFFFFFFFF,
12268 + 0xFFFFFFFF,
12269 +};
12270 +
12271 +static __u32 dh_a[] = {
12272 + 8,
12273 + 0xdf367516,
12274 + 0x86459caa,
12275 + 0xe2d459a4,
12276 + 0xd910dae0,
12277 + 0x8a8b5e37,
12278 + 0x67ab31c6,
12279 + 0xf0b55ea9,
12280 + 0x440051d6,
12281 +};
12282 +
12283 +static __u32 dh_b[] = {
12284 + 8,
12285 + 0xded92656,
12286 + 0xe07a048a,
12287 + 0x6fa452cd,
12288 + 0x2df89d30,
12289 + 0xc75f1b0f,
12290 + 0x8ce3578f,
12291 + 0x7980a324,
12292 + 0x5daec786,
12293 +};
12294 +
12295 +static __u32 dh_g[] = {
12296 + 1,
12297 + 2,
12298 +};
12299 +
12300 +int main(void)
12301 +{
12302 + int i;
12303 + __u32 *k;
12304 + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
12305 +
12306 + printf("\n\n");
12307 + for (i=0; i<k[0]; i++) {
12308 + __u32 word32 = k[k[0] - i];
12309 + __u16 l = word32 & 0xffff;
12310 + __u16 m = (word32 & 0xffff0000) >> 16;
12311 + printf("%04x %04x ", m, l);
12312 + if (!((i + 1)%13)) printf("\n");
12313 + }
12314 + printf("\n\n");
12315 +
12316 + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
12317 + printf("PASS\n\n");
12318 + }
12319 + else {
12320 + printf("FAIL\n\n");
12321 + }
12322 +
12323 +}
12324 +
12325 +#endif /* UNITTEST */
12326 +
12327 +#endif /* CONFIG_MACH_IPMATE */
12328 +
12329 +#endif /*DWC_CRYPTOLIB */
12330 --- /dev/null
12331 +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.h
12332 @@ -0,0 +1,34 @@
12333 +/*
12334 + * dwc_modpow.h
12335 + * See dwc_modpow.c for license and changes
12336 + */
12337 +#ifndef _DWC_MODPOW_H
12338 +#define _DWC_MODPOW_H
12339 +
12340 +#ifdef __cplusplus
12341 +extern "C" {
12342 +#endif
12343 +
12344 +#include "dwc_os.h"
12345 +
12346 +/** @file
12347 + *
12348 + * This file defines the module exponentiation function which is only used
12349 + * internally by the DWC UWB modules for calculation of PKs during numeric
12350 + * association. The routine is taken from the PUTTY, an open source terminal
12351 + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
12352 + *
12353 + */
12354 +
12355 +typedef uint32_t BignumInt;
12356 +typedef uint64_t BignumDblInt;
12357 +typedef BignumInt *Bignum;
12358 +
12359 +/* Compute modular exponentiaion */
12360 +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
12361 +
12362 +#ifdef __cplusplus
12363 +}
12364 +#endif
12365 +
12366 +#endif /* _LINUX_BIGNUM_H */
12367 --- /dev/null
12368 +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.c
12369 @@ -0,0 +1,319 @@
12370 +#ifdef DWC_NOTIFYLIB
12371 +
12372 +#include "dwc_notifier.h"
12373 +#include "dwc_list.h"
12374 +
12375 +typedef struct dwc_observer {
12376 + void *observer;
12377 + dwc_notifier_callback_t callback;
12378 + void *data;
12379 + char *notification;
12380 + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
12381 +} observer_t;
12382 +
12383 +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
12384 +
12385 +typedef struct dwc_notifier {
12386 + void *mem_ctx;
12387 + void *object;
12388 + struct observer_queue observers;
12389 + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
12390 +} notifier_t;
12391 +
12392 +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
12393 +
12394 +typedef struct manager {
12395 + void *mem_ctx;
12396 + void *wkq_ctx;
12397 + dwc_workq_t *wq;
12398 +// dwc_mutex_t *mutex;
12399 + struct notifier_queue notifiers;
12400 +} manager_t;
12401 +
12402 +static manager_t *manager = NULL;
12403 +
12404 +static int create_manager(void *mem_ctx, void *wkq_ctx)
12405 +{
12406 + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
12407 + if (!manager) {
12408 + return -DWC_E_NO_MEMORY;
12409 + }
12410 +
12411 + DWC_CIRCLEQ_INIT(&manager->notifiers);
12412 +
12413 + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
12414 + if (!manager->wq) {
12415 + return -DWC_E_NO_MEMORY;
12416 + }
12417 +
12418 + return 0;
12419 +}
12420 +
12421 +static void free_manager(void)
12422 +{
12423 + dwc_workq_free(manager->wq);
12424 +
12425 + /* All notifiers must have unregistered themselves before this module
12426 + * can be removed. Hitting this assertion indicates a programmer
12427 + * error. */
12428 + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
12429 + "Notification manager being freed before all notifiers have been removed");
12430 + dwc_free(manager->mem_ctx, manager);
12431 +}
12432 +
12433 +#ifdef DEBUG
12434 +static void dump_manager(void)
12435 +{
12436 + notifier_t *n;
12437 + observer_t *o;
12438 +
12439 + DWC_ASSERT(manager, "Notification manager not found");
12440 +
12441 + DWC_DEBUG("List of all notifiers and observers:\n");
12442 + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
12443 + DWC_DEBUG("Notifier %p has observers:\n", n->object);
12444 + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
12445 + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
12446 + }
12447 + }
12448 +}
12449 +#else
12450 +#define dump_manager(...)
12451 +#endif
12452 +
12453 +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
12454 + dwc_notifier_callback_t callback, void *data)
12455 +{
12456 + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
12457 +
12458 + if (!new_observer) {
12459 + return NULL;
12460 + }
12461 +
12462 + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
12463 + new_observer->observer = observer;
12464 + new_observer->notification = notification;
12465 + new_observer->callback = callback;
12466 + new_observer->data = data;
12467 + return new_observer;
12468 +}
12469 +
12470 +static void free_observer(void *mem_ctx, observer_t *observer)
12471 +{
12472 + dwc_free(mem_ctx, observer);
12473 +}
12474 +
12475 +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
12476 +{
12477 + notifier_t *notifier;
12478 +
12479 + if (!object) {
12480 + return NULL;
12481 + }
12482 +
12483 + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
12484 + if (!notifier) {
12485 + return NULL;
12486 + }
12487 +
12488 + DWC_CIRCLEQ_INIT(&notifier->observers);
12489 + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
12490 +
12491 + notifier->mem_ctx = mem_ctx;
12492 + notifier->object = object;
12493 + return notifier;
12494 +}
12495 +
12496 +static void free_notifier(notifier_t *notifier)
12497 +{
12498 + observer_t *observer;
12499 +
12500 + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
12501 + free_observer(notifier->mem_ctx, observer);
12502 + }
12503 +
12504 + dwc_free(notifier->mem_ctx, notifier);
12505 +}
12506 +
12507 +static notifier_t *find_notifier(void *object)
12508 +{
12509 + notifier_t *notifier;
12510 +
12511 + DWC_ASSERT(manager, "Notification manager not found");
12512 +
12513 + if (!object) {
12514 + return NULL;
12515 + }
12516 +
12517 + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
12518 + if (notifier->object == object) {
12519 + return notifier;
12520 + }
12521 + }
12522 +
12523 + return NULL;
12524 +}
12525 +
12526 +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
12527 +{
12528 + return create_manager(mem_ctx, wkq_ctx);
12529 +}
12530 +
12531 +void dwc_free_notification_manager(void)
12532 +{
12533 + free_manager();
12534 +}
12535 +
12536 +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
12537 +{
12538 + notifier_t *notifier;
12539 +
12540 + DWC_ASSERT(manager, "Notification manager not found");
12541 +
12542 + notifier = find_notifier(object);
12543 + if (notifier) {
12544 + DWC_ERROR("Notifier %p is already registered\n", object);
12545 + return NULL;
12546 + }
12547 +
12548 + notifier = alloc_notifier(mem_ctx, object);
12549 + if (!notifier) {
12550 + return NULL;
12551 + }
12552 +
12553 + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
12554 +
12555 + DWC_INFO("Notifier %p registered", object);
12556 + dump_manager();
12557 +
12558 + return notifier;
12559 +}
12560 +
12561 +void dwc_unregister_notifier(dwc_notifier_t *notifier)
12562 +{
12563 + DWC_ASSERT(manager, "Notification manager not found");
12564 +
12565 + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
12566 + observer_t *o;
12567 +
12568 + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
12569 + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
12570 + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
12571 + }
12572 +
12573 + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
12574 + "Notifier %p has active observers when removing", notifier);
12575 + }
12576 +
12577 + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
12578 + free_notifier(notifier);
12579 +
12580 + DWC_INFO("Notifier unregistered");
12581 + dump_manager();
12582 +}
12583 +
12584 +/* Add an observer to observe the notifier for a particular state, event, or notification. */
12585 +int dwc_add_observer(void *observer, void *object, char *notification,
12586 + dwc_notifier_callback_t callback, void *data)
12587 +{
12588 + notifier_t *notifier = find_notifier(object);
12589 + observer_t *new_observer;
12590 +
12591 + if (!notifier) {
12592 + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
12593 + return -DWC_E_INVALID;
12594 + }
12595 +
12596 + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
12597 + if (!new_observer) {
12598 + return -DWC_E_NO_MEMORY;
12599 + }
12600 +
12601 + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
12602 +
12603 + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
12604 + observer, object, notification, callback, data);
12605 +
12606 + dump_manager();
12607 + return 0;
12608 +}
12609 +
12610 +int dwc_remove_observer(void *observer)
12611 +{
12612 + notifier_t *n;
12613 +
12614 + DWC_ASSERT(manager, "Notification manager not found");
12615 +
12616 + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
12617 + observer_t *o;
12618 + observer_t *o2;
12619 +
12620 + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
12621 + if (o->observer == observer) {
12622 + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
12623 + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
12624 + o->observer, n->object, o->notification);
12625 + free_observer(n->mem_ctx, o);
12626 + }
12627 + }
12628 + }
12629 +
12630 + dump_manager();
12631 + return 0;
12632 +}
12633 +
12634 +typedef struct callback_data {
12635 + void *mem_ctx;
12636 + dwc_notifier_callback_t cb;
12637 + void *observer;
12638 + void *data;
12639 + void *object;
12640 + char *notification;
12641 + void *notification_data;
12642 +} cb_data_t;
12643 +
12644 +static void cb_task(void *data)
12645 +{
12646 + cb_data_t *cb = (cb_data_t *)data;
12647 +
12648 + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
12649 + dwc_free(cb->mem_ctx, cb);
12650 +}
12651 +
12652 +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
12653 +{
12654 + observer_t *o;
12655 +
12656 + DWC_ASSERT(manager, "Notification manager not found");
12657 +
12658 + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
12659 + int len = DWC_STRLEN(notification);
12660 +
12661 + if (DWC_STRLEN(o->notification) != len) {
12662 + continue;
12663 + }
12664 +
12665 + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
12666 + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
12667 +
12668 + if (!cb_data) {
12669 + DWC_ERROR("Failed to allocate callback data\n");
12670 + return;
12671 + }
12672 +
12673 + cb_data->mem_ctx = notifier->mem_ctx;
12674 + cb_data->cb = o->callback;
12675 + cb_data->observer = o->observer;
12676 + cb_data->data = o->data;
12677 + cb_data->object = notifier->object;
12678 + cb_data->notification = notification;
12679 + cb_data->notification_data = notification_data;
12680 + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
12681 + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
12682 + "Notify callback from %p for Notification %s, to observer %p",
12683 + cb_data->object, notification, cb_data->observer);
12684 + }
12685 + }
12686 +}
12687 +
12688 +#endif /* DWC_NOTIFYLIB */
12689 --- /dev/null
12690 +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.h
12691 @@ -0,0 +1,122 @@
12692 +
12693 +#ifndef __DWC_NOTIFIER_H__
12694 +#define __DWC_NOTIFIER_H__
12695 +
12696 +#ifdef __cplusplus
12697 +extern "C" {
12698 +#endif
12699 +
12700 +#include "dwc_os.h"
12701 +
12702 +/** @file
12703 + *
12704 + * A simple implementation of the Observer pattern. Any "module" can
12705 + * register as an observer or notifier. The notion of "module" is abstract and
12706 + * can mean anything used to identify either an observer or notifier. Usually
12707 + * it will be a pointer to a data structure which contains some state, ie an
12708 + * object.
12709 + *
12710 + * Before any notifiers can be added, the global notification manager must be
12711 + * brought up with dwc_alloc_notification_manager().
12712 + * dwc_free_notification_manager() will bring it down and free all resources.
12713 + * These would typically be called upon module load and unload. The
12714 + * notification manager is a single global instance that handles all registered
12715 + * observable modules and observers so this should be done only once.
12716 + *
12717 + * A module can be observable by using Notifications to publicize some general
12718 + * information about it's state or operation. It does not care who listens, or
12719 + * even if anyone listens, or what they do with the information. The observable
12720 + * modules do not need to know any information about it's observers or their
12721 + * interface, or their state or data.
12722 + *
12723 + * Any module can register to emit Notifications. It should publish a list of
12724 + * notifications that it can emit and their behavior, such as when they will get
12725 + * triggered, and what information will be provided to the observer. Then it
12726 + * should register itself as an observable module. See dwc_register_notifier().
12727 + *
12728 + * Any module can observe any observable, registered module, provided it has a
12729 + * handle to the other module and knows what notifications to observe. See
12730 + * dwc_add_observer().
12731 + *
12732 + * A function of type dwc_notifier_callback_t is called whenever a notification
12733 + * is triggered with one or more observers observing it. This function is
12734 + * called in it's own process so it may sleep or block if needed. It is
12735 + * guaranteed to be called sometime after the notification has occurred and will
12736 + * be called once per each time the notification is triggered. It will NOT be
12737 + * called in the same process context used to trigger the notification.
12738 + *
12739 + * @section Limitiations
12740 + *
12741 + * Keep in mind that Notifications that can be triggered in rapid sucession may
12742 + * schedule too many processes too handle. Be aware of this limitation when
12743 + * designing to use notifications, and only add notifications for appropriate
12744 + * observable information.
12745 + *
12746 + * Also Notification callbacks are not synchronous. If you need to synchronize
12747 + * the behavior between module/observer you must use other means. And perhaps
12748 + * that will mean Notifications are not the proper solution.
12749 + */
12750 +
12751 +struct dwc_notifier;
12752 +typedef struct dwc_notifier dwc_notifier_t;
12753 +
12754 +/** The callback function must be of this type.
12755 + *
12756 + * @param object This is the object that is being observed.
12757 + * @param notification This is the notification that was triggered.
12758 + * @param observer This is the observer
12759 + * @param notification_data This is notification-specific data that the notifier
12760 + * has included in this notification. The value of this should be published in
12761 + * the documentation of the observable module with the notifications.
12762 + * @param user_data This is any custom data that the observer provided when
12763 + * adding itself as an observer to the notification. */
12764 +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
12765 + void *notification_data, void *user_data);
12766 +
12767 +/** Brings up the notification manager. */
12768 +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
12769 +/** Brings down the notification manager. */
12770 +extern void dwc_free_notification_manager(void);
12771 +
12772 +/** This function registers an observable module. A dwc_notifier_t object is
12773 + * returned to the observable module. This is an opaque object that is used by
12774 + * the observable module to trigger notifications. This object should only be
12775 + * accessible to functions that are authorized to trigger notifications for this
12776 + * module. Observers do not need this object. */
12777 +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
12778 +
12779 +/** This function unregisters an observable module. All observers have to be
12780 + * removed prior to unregistration. */
12781 +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
12782 +
12783 +/** Add a module as an observer to the observable module. The observable module
12784 + * needs to have previously registered with the notification manager.
12785 + *
12786 + * @param observer The observer module
12787 + * @param object The module to observe
12788 + * @param notification The notification to observe
12789 + * @param callback The callback function to call
12790 + * @param user_data Any additional user data to pass into the callback function */
12791 +extern int dwc_add_observer(void *observer, void *object, char *notification,
12792 + dwc_notifier_callback_t callback, void *user_data);
12793 +
12794 +/** Removes the specified observer from all notifications that it is currently
12795 + * observing. */
12796 +extern int dwc_remove_observer(void *observer);
12797 +
12798 +/** This function triggers a Notification. It should be called by the
12799 + * observable module, or any module or library which the observable module
12800 + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
12801 + *
12802 + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
12803 + * their own process context for each trigger. Callbacks can be blocking.
12804 + * dwc_notify can be called from interrupt context if needed.
12805 + *
12806 + */
12807 +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
12808 +
12809 +#ifdef __cplusplus
12810 +}
12811 +#endif
12812 +
12813 +#endif /* __DWC_NOTIFIER_H__ */
12814 --- /dev/null
12815 +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
12816 @@ -0,0 +1,1276 @@
12817 +/* =========================================================================
12818 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
12819 + * $Revision: #14 $
12820 + * $Date: 2010/11/04 $
12821 + * $Change: 1621695 $
12822 + *
12823 + * Synopsys Portability Library Software and documentation
12824 + * (hereinafter, "Software") is an Unsupported proprietary work of
12825 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
12826 + * between Synopsys and you.
12827 + *
12828 + * The Software IS NOT an item of Licensed Software or Licensed Product
12829 + * under any End User Software License Agreement or Agreement for
12830 + * Licensed Product with Synopsys or any supplement thereto. You are
12831 + * permitted to use and redistribute this Software in source and binary
12832 + * forms, with or without modification, provided that redistributions
12833 + * of source code must retain this notice. You may not view, use,
12834 + * disclose, copy or distribute this file or any information contained
12835 + * herein except pursuant to this license grant from Synopsys. If you
12836 + * do not agree with this notice, including the disclaimer below, then
12837 + * you are not authorized to use the Software.
12838 + *
12839 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
12840 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12841 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12842 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
12843 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12844 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12845 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12846 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
12847 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12848 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
12849 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
12850 + * DAMAGE.
12851 + * ========================================================================= */
12852 +#ifndef _DWC_OS_H_
12853 +#define _DWC_OS_H_
12854 +
12855 +#ifdef __cplusplus
12856 +extern "C" {
12857 +#endif
12858 +
12859 +/** @file
12860 + *
12861 + * DWC portability library, low level os-wrapper functions
12862 + *
12863 + */
12864 +
12865 +/* These basic types need to be defined by some OS header file or custom header
12866 + * file for your specific target architecture.
12867 + *
12868 + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
12869 + *
12870 + * Any custom or alternate header file must be added and enabled here.
12871 + */
12872 +
12873 +#ifdef DWC_LINUX
12874 +# include <linux/types.h>
12875 +# ifdef CONFIG_DEBUG_MUTEXES
12876 +# include <linux/mutex.h>
12877 +# endif
12878 +# include <linux/spinlock.h>
12879 +# include <linux/errno.h>
12880 +# include <stdarg.h>
12881 +#endif
12882 +
12883 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
12884 +# include <os_dep.h>
12885 +#endif
12886 +
12887 +
12888 +/** @name Primitive Types and Values */
12889 +
12890 +/** We define a boolean type for consistency. Can be either YES or NO */
12891 +typedef uint8_t dwc_bool_t;
12892 +#define YES 1
12893 +#define NO 0
12894 +
12895 +#ifdef DWC_LINUX
12896 +
12897 +/** @name Error Codes */
12898 +#define DWC_E_INVALID EINVAL
12899 +#define DWC_E_NO_MEMORY ENOMEM
12900 +#define DWC_E_NO_DEVICE ENODEV
12901 +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
12902 +#define DWC_E_TIMEOUT ETIMEDOUT
12903 +#define DWC_E_BUSY EBUSY
12904 +#define DWC_E_AGAIN EAGAIN
12905 +#define DWC_E_RESTART ERESTART
12906 +#define DWC_E_ABORT ECONNABORTED
12907 +#define DWC_E_SHUTDOWN ESHUTDOWN
12908 +#define DWC_E_NO_DATA ENODATA
12909 +#define DWC_E_DISCONNECT ECONNRESET
12910 +#define DWC_E_UNKNOWN EINVAL
12911 +#define DWC_E_NO_STREAM_RES ENOSR
12912 +#define DWC_E_COMMUNICATION ECOMM
12913 +#define DWC_E_OVERFLOW EOVERFLOW
12914 +#define DWC_E_PROTOCOL EPROTO
12915 +#define DWC_E_IN_PROGRESS EINPROGRESS
12916 +#define DWC_E_PIPE EPIPE
12917 +#define DWC_E_IO EIO
12918 +#define DWC_E_NO_SPACE ENOSPC
12919 +
12920 +#else
12921 +
12922 +/** @name Error Codes */
12923 +#define DWC_E_INVALID 1001
12924 +#define DWC_E_NO_MEMORY 1002
12925 +#define DWC_E_NO_DEVICE 1003
12926 +#define DWC_E_NOT_SUPPORTED 1004
12927 +#define DWC_E_TIMEOUT 1005
12928 +#define DWC_E_BUSY 1006
12929 +#define DWC_E_AGAIN 1007
12930 +#define DWC_E_RESTART 1008
12931 +#define DWC_E_ABORT 1009
12932 +#define DWC_E_SHUTDOWN 1010
12933 +#define DWC_E_NO_DATA 1011
12934 +#define DWC_E_DISCONNECT 2000
12935 +#define DWC_E_UNKNOWN 3000
12936 +#define DWC_E_NO_STREAM_RES 4001
12937 +#define DWC_E_COMMUNICATION 4002
12938 +#define DWC_E_OVERFLOW 4003
12939 +#define DWC_E_PROTOCOL 4004
12940 +#define DWC_E_IN_PROGRESS 4005
12941 +#define DWC_E_PIPE 4006
12942 +#define DWC_E_IO 4007
12943 +#define DWC_E_NO_SPACE 4008
12944 +
12945 +#endif
12946 +
12947 +
12948 +/** @name Tracing/Logging Functions
12949 + *
12950 + * These function provide the capability to add tracing, debugging, and error
12951 + * messages, as well exceptions as assertions. The WUDEV uses these
12952 + * extensively. These could be logged to the main console, the serial port, an
12953 + * internal buffer, etc. These functions could also be no-op if they are too
12954 + * expensive on your system. By default undefining the DEBUG macro already
12955 + * no-ops some of these functions. */
12956 +
12957 +/** Returns non-zero if in interrupt context. */
12958 +extern dwc_bool_t DWC_IN_IRQ(void);
12959 +#define dwc_in_irq DWC_IN_IRQ
12960 +
12961 +/** Returns "IRQ" if DWC_IN_IRQ is true. */
12962 +static inline char *dwc_irq(void) {
12963 + return DWC_IN_IRQ() ? "IRQ" : "";
12964 +}
12965 +
12966 +/** Returns non-zero if in bottom-half context. */
12967 +extern dwc_bool_t DWC_IN_BH(void);
12968 +#define dwc_in_bh DWC_IN_BH
12969 +
12970 +/** Returns "BH" if DWC_IN_BH is true. */
12971 +static inline char *dwc_bh(void) {
12972 + return DWC_IN_BH() ? "BH" : "";
12973 +}
12974 +
12975 +/**
12976 + * A vprintf() clone. Just call vprintf if you've got it.
12977 + */
12978 +extern void DWC_VPRINTF(char *format, va_list args);
12979 +#define dwc_vprintf DWC_VPRINTF
12980 +
12981 +/**
12982 + * A vsnprintf() clone. Just call vprintf if you've got it.
12983 + */
12984 +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
12985 +#define dwc_vsnprintf DWC_VSNPRINTF
12986 +
12987 +/**
12988 + * printf() clone. Just call printf if you've go it.
12989 + */
12990 +extern void DWC_PRINTF(char *format, ...)
12991 +/* This provides compiler level static checking of the parameters if you're
12992 + * using GCC. */
12993 +#ifdef __GNUC__
12994 + __attribute__ ((format(printf, 1, 2)));
12995 +#else
12996 + ;
12997 +#endif
12998 +#define dwc_printf DWC_PRINTF
12999 +
13000 +/**
13001 + * sprintf() clone. Just call sprintf if you've got it.
13002 + */
13003 +extern int DWC_SPRINTF(char *string, char *format, ...)
13004 +#ifdef __GNUC__
13005 + __attribute__ ((format(printf, 2, 3)));
13006 +#else
13007 + ;
13008 +#endif
13009 +#define dwc_sprintf DWC_SPRINTF
13010 +
13011 +/**
13012 + * snprintf() clone. Just call snprintf if you've got it.
13013 + */
13014 +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
13015 +#ifdef __GNUC__
13016 + __attribute__ ((format(printf, 3, 4)));
13017 +#else
13018 + ;
13019 +#endif
13020 +#define dwc_snprintf DWC_SNPRINTF
13021 +
13022 +/**
13023 + * Prints a WARNING message. On systems that don't differentiate between
13024 + * warnings and regular log messages, just print it. Indicates that something
13025 + * may be wrong with the driver. Works like printf().
13026 + *
13027 + * Use the DWC_WARN macro to call this function.
13028 + */
13029 +extern void __DWC_WARN(char *format, ...)
13030 +#ifdef __GNUC__
13031 + __attribute__ ((format(printf, 1, 2)));
13032 +#else
13033 + ;
13034 +#endif
13035 +
13036 +/**
13037 + * Prints an error message. On systems that don't differentiate between errors
13038 + * and regular log messages, just print it. Indicates that something went wrong
13039 + * with the driver. Works like printf().
13040 + *
13041 + * Use the DWC_ERROR macro to call this function.
13042 + */
13043 +extern void __DWC_ERROR(char *format, ...)
13044 +#ifdef __GNUC__
13045 + __attribute__ ((format(printf, 1, 2)));
13046 +#else
13047 + ;
13048 +#endif
13049 +
13050 +/**
13051 + * Prints an exception error message and takes some user-defined action such as
13052 + * print out a backtrace or trigger a breakpoint. Indicates that something went
13053 + * abnormally wrong with the driver such as programmer error, or other
13054 + * exceptional condition. It should not be ignored so even on systems without
13055 + * printing capability, some action should be taken to notify the developer of
13056 + * it. Works like printf().
13057 + */
13058 +extern void DWC_EXCEPTION(char *format, ...)
13059 +#ifdef __GNUC__
13060 + __attribute__ ((format(printf, 1, 2)));
13061 +#else
13062 + ;
13063 +#endif
13064 +#define dwc_exception DWC_EXCEPTION
13065 +
13066 +#ifndef DWC_OTG_DEBUG_LEV
13067 +#define DWC_OTG_DEBUG_LEV 0
13068 +#endif
13069 +
13070 +#ifdef DEBUG
13071 +/**
13072 + * Prints out a debug message. Used for logging/trace messages.
13073 + *
13074 + * Use the DWC_DEBUG macro to call this function
13075 + */
13076 +extern void __DWC_DEBUG(char *format, ...)
13077 +#ifdef __GNUC__
13078 + __attribute__ ((format(printf, 1, 2)));
13079 +#else
13080 + ;
13081 +#endif
13082 +#else
13083 +#define __DWC_DEBUG printk
13084 +#endif
13085 +
13086 +/**
13087 + * Prints out a Debug message.
13088 + */
13089 +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
13090 + __func__, dwc_irq(), ## _args)
13091 +#define dwc_debug DWC_DEBUG
13092 +/**
13093 + * Prints out a Debug message if enabled at compile time.
13094 + */
13095 +#if DWC_OTG_DEBUG_LEV > 0
13096 +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
13097 +#else
13098 +#define DWC_DEBUGC(_format, _args...)
13099 +#endif
13100 +#define dwc_debugc DWC_DEBUGC
13101 +/**
13102 + * Prints out an informative message.
13103 + */
13104 +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
13105 + dwc_irq(), ## _args)
13106 +#define dwc_info DWC_INFO
13107 +/**
13108 + * Prints out an informative message if enabled at compile time.
13109 + */
13110 +#if DWC_OTG_DEBUG_LEV > 1
13111 +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
13112 +#else
13113 +#define DWC_INFOC(_format, _args...)
13114 +#endif
13115 +#define dwc_infoc DWC_INFOC
13116 +/**
13117 + * Prints out a warning message.
13118 + */
13119 +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
13120 + dwc_irq(), __func__, __LINE__, ## _args)
13121 +#define dwc_warn DWC_WARN
13122 +/**
13123 + * Prints out an error message.
13124 + */
13125 +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
13126 + dwc_irq(), __func__, __LINE__, ## _args)
13127 +#define dwc_error DWC_ERROR
13128 +
13129 +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
13130 + dwc_irq(), __func__, __LINE__, ## _args)
13131 +#define dwc_proto_error DWC_PROTO_ERROR
13132 +
13133 +#ifdef DEBUG
13134 +/** Prints out a exception error message if the _expr expression fails. Disabled
13135 + * if DEBUG is not enabled. */
13136 +#define DWC_ASSERT(_expr, _format, _args...) do { \
13137 + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
13138 + __FILE__, __LINE__, ## _args); } \
13139 + } while (0)
13140 +#else
13141 +#define DWC_ASSERT(_x...)
13142 +#endif
13143 +#define dwc_assert DWC_ASSERT
13144 +
13145 +
13146 +/** @name Byte Ordering
13147 + * The following functions are for conversions between processor's byte ordering
13148 + * and specific ordering you want.
13149 + */
13150 +
13151 +/** Converts 32 bit data in CPU byte ordering to little endian. */
13152 +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
13153 +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
13154 +
13155 +/** Converts 32 bit data in CPU byte orderint to big endian. */
13156 +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
13157 +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
13158 +
13159 +/** Converts 32 bit little endian data to CPU byte ordering. */
13160 +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
13161 +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
13162 +
13163 +/** Converts 32 bit big endian data to CPU byte ordering. */
13164 +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
13165 +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
13166 +
13167 +/** Converts 16 bit data in CPU byte ordering to little endian. */
13168 +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
13169 +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
13170 +
13171 +/** Converts 16 bit data in CPU byte orderint to big endian. */
13172 +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
13173 +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
13174 +
13175 +/** Converts 16 bit little endian data to CPU byte ordering. */
13176 +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
13177 +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
13178 +
13179 +/** Converts 16 bit bi endian data to CPU byte ordering. */
13180 +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
13181 +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
13182 +
13183 +
13184 +/** @name Register Read/Write
13185 + *
13186 + * The following six functions should be implemented to read/write registers of
13187 + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
13188 + * The reg value is a pointer to the register calculated from the void *base
13189 + * variable passed into the driver when it is started. */
13190 +
13191 +#ifdef DWC_LINUX
13192 +/* Linux doesn't need any extra parameters for register read/write, so we
13193 + * just throw away the IO context parameter.
13194 + */
13195 +/** Reads the content of a 32-bit register. */
13196 +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
13197 +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
13198 +
13199 +/** Reads the content of a 64-bit register. */
13200 +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
13201 +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
13202 +
13203 +/** Writes to a 32-bit register. */
13204 +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
13205 +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
13206 +
13207 +/** Writes to a 64-bit register. */
13208 +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
13209 +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
13210 +
13211 +/**
13212 + * Modify bit values in a register. Using the
13213 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13214 + */
13215 +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
13216 +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
13217 +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
13218 +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
13219 +
13220 +#endif /* DWC_LINUX */
13221 +
13222 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13223 +typedef struct dwc_ioctx {
13224 + struct device *dev;
13225 + bus_space_tag_t iot;
13226 + bus_space_handle_t ioh;
13227 +} dwc_ioctx_t;
13228 +
13229 +/** BSD needs two extra parameters for register read/write, so we pass
13230 + * them in using the IO context parameter.
13231 + */
13232 +/** Reads the content of a 32-bit register. */
13233 +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
13234 +#define dwc_read_reg32 DWC_READ_REG32
13235 +
13236 +/** Reads the content of a 64-bit register. */
13237 +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
13238 +#define dwc_read_reg64 DWC_READ_REG64
13239 +
13240 +/** Writes to a 32-bit register. */
13241 +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
13242 +#define dwc_write_reg32 DWC_WRITE_REG32
13243 +
13244 +/** Writes to a 64-bit register. */
13245 +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
13246 +#define dwc_write_reg64 DWC_WRITE_REG64
13247 +
13248 +/**
13249 + * Modify bit values in a register. Using the
13250 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13251 + */
13252 +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
13253 +#define dwc_modify_reg32 DWC_MODIFY_REG32
13254 +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
13255 +#define dwc_modify_reg64 DWC_MODIFY_REG64
13256 +
13257 +#endif /* DWC_FREEBSD || DWC_NETBSD */
13258 +
13259 +/** @cond */
13260 +
13261 +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
13262 + * register writes. */
13263 +
13264 +#ifdef DWC_LINUX
13265 +
13266 +# ifdef DWC_DEBUG_REGS
13267 +
13268 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13269 +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
13270 + return DWC_READ_REG32(&container->regs->_reg[num]); \
13271 +} \
13272 +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
13273 + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
13274 + &(((uint32_t*)container->regs->_reg)[num]), data); \
13275 + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
13276 +}
13277 +
13278 +#define dwc_define_read_write_reg(_reg,_container_type) \
13279 +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
13280 + return DWC_READ_REG32(&container->regs->_reg); \
13281 +} \
13282 +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
13283 + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
13284 + DWC_WRITE_REG32(&container->regs->_reg, data); \
13285 +}
13286 +
13287 +# else /* DWC_DEBUG_REGS */
13288 +
13289 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13290 +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
13291 + return DWC_READ_REG32(&container->regs->_reg[num]); \
13292 +} \
13293 +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
13294 + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
13295 +}
13296 +
13297 +#define dwc_define_read_write_reg(_reg,_container_type) \
13298 +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
13299 + return DWC_READ_REG32(&container->regs->_reg); \
13300 +} \
13301 +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
13302 + DWC_WRITE_REG32(&container->regs->_reg, data); \
13303 +}
13304 +
13305 +# endif /* DWC_DEBUG_REGS */
13306 +
13307 +#endif /* DWC_LINUX */
13308 +
13309 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13310 +
13311 +# ifdef DWC_DEBUG_REGS
13312 +
13313 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13314 +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
13315 + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
13316 +} \
13317 +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
13318 + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
13319 + &(((uint32_t*)container->regs->_reg)[num]), data); \
13320 + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
13321 +}
13322 +
13323 +#define dwc_define_read_write_reg(_reg,_container_type) \
13324 +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
13325 + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
13326 +} \
13327 +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
13328 + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
13329 + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
13330 +}
13331 +
13332 +# else /* DWC_DEBUG_REGS */
13333 +
13334 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13335 +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
13336 + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
13337 +} \
13338 +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
13339 + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
13340 +}
13341 +
13342 +#define dwc_define_read_write_reg(_reg,_container_type) \
13343 +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
13344 + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
13345 +} \
13346 +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
13347 + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
13348 +}
13349 +
13350 +# endif /* DWC_DEBUG_REGS */
13351 +
13352 +#endif /* DWC_FREEBSD || DWC_NETBSD */
13353 +
13354 +/** @endcond */
13355 +
13356 +
13357 +#ifdef DWC_CRYPTOLIB
13358 +/** @name Crypto Functions
13359 + *
13360 + * These are the low-level cryptographic functions used by the driver. */
13361 +
13362 +/** Perform AES CBC */
13363 +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
13364 +#define dwc_aes_cbc DWC_AES_CBC
13365 +
13366 +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
13367 +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
13368 +#define dwc_random_bytes DWC_RANDOM_BYTES
13369 +
13370 +/** Perform the SHA-256 hash function */
13371 +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
13372 +#define dwc_sha256 DWC_SHA256
13373 +
13374 +/** Calculated the HMAC-SHA256 */
13375 +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
13376 +#define dwc_hmac_sha256 DWC_HMAC_SHA256
13377 +
13378 +#endif /* DWC_CRYPTOLIB */
13379 +
13380 +
13381 +/** @name Memory Allocation
13382 + *
13383 + * These function provide access to memory allocation. There are only 2 DMA
13384 + * functions and 3 Regular memory functions that need to be implemented. None
13385 + * of the memory debugging routines need to be implemented. The allocation
13386 + * routines all ZERO the contents of the memory.
13387 + *
13388 + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
13389 + * This checks for memory leaks, keeping track of alloc/free pairs. It also
13390 + * keeps track of how much memory the driver is using at any given time. */
13391 +
13392 +#define DWC_PAGE_SIZE 4096
13393 +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
13394 +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
13395 +
13396 +#define DWC_INVALID_DMA_ADDR 0x0
13397 +
13398 +#ifdef DWC_LINUX
13399 +/** Type for a DMA address */
13400 +typedef dma_addr_t dwc_dma_t;
13401 +#endif
13402 +
13403 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13404 +typedef bus_addr_t dwc_dma_t;
13405 +#endif
13406 +
13407 +#ifdef DWC_FREEBSD
13408 +typedef struct dwc_dmactx {
13409 + struct device *dev;
13410 + bus_dma_tag_t dma_tag;
13411 + bus_dmamap_t dma_map;
13412 + bus_addr_t dma_paddr;
13413 + void *dma_vaddr;
13414 +} dwc_dmactx_t;
13415 +#endif
13416 +
13417 +#ifdef DWC_NETBSD
13418 +typedef struct dwc_dmactx {
13419 + struct device *dev;
13420 + bus_dma_tag_t dma_tag;
13421 + bus_dmamap_t dma_map;
13422 + bus_dma_segment_t segs[1];
13423 + int nsegs;
13424 + bus_addr_t dma_paddr;
13425 + void *dma_vaddr;
13426 +} dwc_dmactx_t;
13427 +#endif
13428 +
13429 +/* @todo these functions will be added in the future */
13430 +#if 0
13431 +/**
13432 + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
13433 + * allocated from this pool will be guaranteed to meet the size, alignment, and
13434 + * boundary requirements specified.
13435 + *
13436 + * @param[in] size Specifies the size of the buffers that will be allocated from
13437 + * this pool.
13438 + * @param[in] align Specifies the byte alignment requirements of the buffers
13439 + * allocated from this pool. Must be a power of 2.
13440 + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
13441 + * this pool must not cross.
13442 + *
13443 + * @returns A pointer to an internal opaque structure which is not to be
13444 + * accessed outside of these library functions. Use this handle to specify
13445 + * which pools to allocate/free DMA buffers from and also to destroy the pool,
13446 + * when you are done with it.
13447 + */
13448 +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
13449 +
13450 +/**
13451 + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
13452 + */
13453 +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
13454 +
13455 +/**
13456 + * Allocate a buffer from the specified DMA pool and zeros its contents.
13457 + */
13458 +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
13459 +
13460 +/**
13461 + * Free a previously allocated buffer from the DMA pool.
13462 + */
13463 +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
13464 +#endif
13465 +
13466 +/** Allocates a DMA capable buffer and zeroes its contents. */
13467 +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
13468 +
13469 +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
13470 +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
13471 +
13472 +/** Frees a previously allocated buffer. */
13473 +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
13474 +
13475 +/** Allocates a block of memory and zeroes its contents. */
13476 +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
13477 +
13478 +/** Allocates a block of memory and zeroes its contents, in an atomic manner
13479 + * which can be used inside interrupt context. The size should be sufficiently
13480 + * small, a few KB at most, such that failures are not likely to occur. Can just call
13481 + * __DWC_ALLOC if it is atomic. */
13482 +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
13483 +
13484 +/** Frees a previously allocated buffer. */
13485 +extern void __DWC_FREE(void *mem_ctx, void *addr);
13486 +
13487 +#ifndef DWC_DEBUG_MEMORY
13488 +
13489 +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
13490 +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
13491 +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
13492 +
13493 +# ifdef DWC_LINUX
13494 +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) __DWC_DMA_ALLOC(_dev, _size_, _dma_)
13495 +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) __DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_)
13496 +#define DWC_DMA_FREE(_dev, _size_,_virt_, _dma_) __DWC_DMA_FREE(_dev, _size_, _virt_, _dma_)
13497 +# endif
13498 +
13499 +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13500 +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
13501 +#define DWC_DMA_FREE __DWC_DMA_FREE
13502 +# endif
13503 +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
13504 +
13505 +#else /* DWC_DEBUG_MEMORY */
13506 +
13507 +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
13508 +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
13509 +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
13510 +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
13511 + char const *func, int line);
13512 +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
13513 + char const *func, int line);
13514 +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
13515 + dwc_dma_t dma_addr, char const *func, int line);
13516 +
13517 +extern int dwc_memory_debug_start(void *mem_ctx);
13518 +extern void dwc_memory_debug_stop(void);
13519 +extern void dwc_memory_debug_report(void);
13520 +
13521 +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
13522 +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
13523 + __func__, __LINE__)
13524 +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
13525 +
13526 +# ifdef DWC_LINUX
13527 +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) \
13528 + dwc_dma_alloc_debug(_dev, _size_, _dma_, __func__, __LINE__)
13529 +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) \
13530 + dwc_dma_alloc_atomic_debug(_dev, _size_, _dma_, __func__, __LINE__)
13531 +#define DWC_DMA_FREE(_dev, _size_, _virt_, _dma_) \
13532 + dwc_dma_free_debug(_dev, _size_, _virt_, _dma_, __func__, __LINE__)
13533 +# endif
13534 +
13535 +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13536 +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
13537 + _dma_, __func__, __LINE__)
13538 +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
13539 + _virt_, _dma_, __func__, __LINE__)
13540 +# endif
13541 +
13542 +#endif /* DWC_DEBUG_MEMORY */
13543 +
13544 +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
13545 +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
13546 +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
13547 +
13548 +#ifdef DWC_LINUX
13549 +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
13550 + * just throw away the DMA context parameter.
13551 + */
13552 +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
13553 +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
13554 +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
13555 +#endif
13556 +
13557 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13558 +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
13559 + * them in using the DMA context parameter.
13560 + */
13561 +#define dwc_dma_alloc DWC_DMA_ALLOC
13562 +#define dwc_dma_free DWC_DMA_FREE
13563 +#endif
13564 +
13565 +
13566 +/** @name Memory and String Processing */
13567 +
13568 +/** memset() clone */
13569 +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
13570 +#define dwc_memset DWC_MEMSET
13571 +
13572 +/** memcpy() clone */
13573 +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
13574 +#define dwc_memcpy DWC_MEMCPY
13575 +
13576 +/** memmove() clone */
13577 +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
13578 +#define dwc_memmove DWC_MEMMOVE
13579 +
13580 +/** memcmp() clone */
13581 +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
13582 +#define dwc_memcmp DWC_MEMCMP
13583 +
13584 +/** strcmp() clone */
13585 +extern int DWC_STRCMP(void *s1, void *s2);
13586 +#define dwc_strcmp DWC_STRCMP
13587 +
13588 +/** strncmp() clone */
13589 +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
13590 +#define dwc_strncmp DWC_STRNCMP
13591 +
13592 +/** strlen() clone, for NULL terminated ASCII strings */
13593 +extern int DWC_STRLEN(char const *str);
13594 +#define dwc_strlen DWC_STRLEN
13595 +
13596 +/** strcpy() clone, for NULL terminated ASCII strings */
13597 +extern char *DWC_STRCPY(char *to, const char *from);
13598 +#define dwc_strcpy DWC_STRCPY
13599 +
13600 +/** strdup() clone. If you wish to use memory allocation debugging, this
13601 + * implementation of strdup should use the DWC_* memory routines instead of
13602 + * calling a predefined strdup. Otherwise the memory allocated by this routine
13603 + * will not be seen by the debugging routines. */
13604 +extern char *DWC_STRDUP(char const *str);
13605 +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
13606 +
13607 +/** NOT an atoi() clone. Read the description carefully. Returns an integer
13608 + * converted from the string str in base 10 unless the string begins with a "0x"
13609 + * in which case it is base 16. String must be a NULL terminated sequence of
13610 + * ASCII characters and may optionally begin with whitespace, a + or -, and a
13611 + * "0x" prefix if base 16. The remaining characters must be valid digits for
13612 + * the number and end with a NULL character. If any invalid characters are
13613 + * encountered or it returns with a negative error code and the results of the
13614 + * conversion are undefined. On sucess it returns 0. Overflow conditions are
13615 + * undefined. An example implementation using atoi() can be referenced from the
13616 + * Linux implementation. */
13617 +extern int DWC_ATOI(const char *str, int32_t *value);
13618 +#define dwc_atoi DWC_ATOI
13619 +
13620 +/** Same as above but for unsigned. */
13621 +extern int DWC_ATOUI(const char *str, uint32_t *value);
13622 +#define dwc_atoui DWC_ATOUI
13623 +
13624 +#ifdef DWC_UTFLIB
13625 +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
13626 +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
13627 +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
13628 +#endif
13629 +
13630 +
13631 +/** @name Wait queues
13632 + *
13633 + * Wait queues provide a means of synchronizing between threads or processes. A
13634 + * process can block on a waitq if some condition is not true, waiting for it to
13635 + * become true. When the waitq is triggered all waiting process will get
13636 + * unblocked and the condition will be check again. Waitqs should be triggered
13637 + * every time a condition can potentially change.*/
13638 +struct dwc_waitq;
13639 +
13640 +/** Type for a waitq */
13641 +typedef struct dwc_waitq dwc_waitq_t;
13642 +
13643 +/** The type of waitq condition callback function. This is called every time
13644 + * condition is evaluated. */
13645 +typedef int (*dwc_waitq_condition_t)(void *data);
13646 +
13647 +/** Allocate a waitq */
13648 +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
13649 +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
13650 +
13651 +/** Free a waitq */
13652 +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
13653 +#define dwc_waitq_free DWC_WAITQ_FREE
13654 +
13655 +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
13656 + * condition again. The function returns when the condition becomes true. The return value
13657 + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
13658 +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
13659 +#define dwc_waitq_wait DWC_WAITQ_WAIT
13660 +
13661 +/** Check the condition and if it is false, block on the waitq. When unblocked,
13662 + * check the condition again. The function returns when the condition become
13663 + * true or the timeout has passed. The return value is 0 on condition true or
13664 + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
13665 + * error. */
13666 +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
13667 + void *data, int32_t msecs);
13668 +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
13669 +
13670 +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
13671 + * has potentially changed. */
13672 +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
13673 +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
13674 +
13675 +/** Unblock all processes waiting on the waitq with an ABORTED result. */
13676 +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
13677 +#define dwc_waitq_abort DWC_WAITQ_ABORT
13678 +
13679 +
13680 +/** @name Threads
13681 + *
13682 + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
13683 + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
13684 + * returns the value from the thread.
13685 + */
13686 +
13687 +struct dwc_thread;
13688 +
13689 +/** Type for a thread */
13690 +typedef struct dwc_thread dwc_thread_t;
13691 +
13692 +/** The thread function */
13693 +typedef int (*dwc_thread_function_t)(void *data);
13694 +
13695 +/** Create a thread and start it running the thread_function. Returns a handle
13696 + * to the thread */
13697 +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
13698 +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
13699 +
13700 +/** Stops a thread. Return the value returned by the thread. Or will return
13701 + * DWC_ABORT if the thread never started. */
13702 +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
13703 +#define dwc_thread_stop DWC_THREAD_STOP
13704 +
13705 +/** Signifies to the thread that it must stop. */
13706 +#ifdef DWC_LINUX
13707 +/* Linux doesn't need any parameters for kthread_should_stop() */
13708 +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
13709 +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
13710 +
13711 +/* No thread_exit function in Linux */
13712 +#define dwc_thread_exit(_thrd_)
13713 +#endif
13714 +
13715 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13716 +/** BSD needs the thread pointer for kthread_suspend_check() */
13717 +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
13718 +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
13719 +
13720 +/** The thread must call this to exit. */
13721 +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
13722 +#define dwc_thread_exit DWC_THREAD_EXIT
13723 +#endif
13724 +
13725 +
13726 +/** @name Work queues
13727 + *
13728 + * Workqs are used to queue a callback function to be called at some later time,
13729 + * in another thread. */
13730 +struct dwc_workq;
13731 +
13732 +/** Type for a workq */
13733 +typedef struct dwc_workq dwc_workq_t;
13734 +
13735 +/** The type of the callback function to be called. */
13736 +typedef void (*dwc_work_callback_t)(void *data);
13737 +
13738 +/** Allocate a workq */
13739 +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
13740 +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
13741 +
13742 +/** Free a workq. All work must be completed before being freed. */
13743 +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
13744 +#define dwc_workq_free DWC_WORKQ_FREE
13745 +
13746 +/** Schedule a callback on the workq, passing in data. The function will be
13747 + * scheduled at some later time. */
13748 +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
13749 + void *data, char *format, ...)
13750 +#ifdef __GNUC__
13751 + __attribute__ ((format(printf, 4, 5)));
13752 +#else
13753 + ;
13754 +#endif
13755 +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
13756 +
13757 +/** Schedule a callback on the workq, that will be called until at least
13758 + * given number miliseconds have passed. */
13759 +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
13760 + void *data, uint32_t time, char *format, ...)
13761 +#ifdef __GNUC__
13762 + __attribute__ ((format(printf, 5, 6)));
13763 +#else
13764 + ;
13765 +#endif
13766 +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
13767 +
13768 +/** The number of processes in the workq */
13769 +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
13770 +#define dwc_workq_pending DWC_WORKQ_PENDING
13771 +
13772 +/** Blocks until all the work in the workq is complete or timed out. Returns <
13773 + * 0 on timeout. */
13774 +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
13775 +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
13776 +
13777 +
13778 +/** @name Tasklets
13779 + *
13780 + */
13781 +struct dwc_tasklet;
13782 +
13783 +/** Type for a tasklet */
13784 +typedef struct dwc_tasklet dwc_tasklet_t;
13785 +
13786 +/** The type of the callback function to be called */
13787 +typedef void (*dwc_tasklet_callback_t)(void *data);
13788 +
13789 +/** Allocates a tasklet */
13790 +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
13791 +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
13792 +
13793 +/** Frees a tasklet */
13794 +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
13795 +#define dwc_task_free DWC_TASK_FREE
13796 +
13797 +/** Schedules a tasklet to run */
13798 +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
13799 +#define dwc_task_schedule DWC_TASK_SCHEDULE
13800 +
13801 +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
13802 +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
13803 +
13804 +/** @name Timer
13805 + *
13806 + * Callbacks must be small and atomic.
13807 + */
13808 +struct dwc_timer;
13809 +
13810 +/** Type for a timer */
13811 +typedef struct dwc_timer dwc_timer_t;
13812 +
13813 +/** The type of the callback function to be called */
13814 +typedef void (*dwc_timer_callback_t)(void *data);
13815 +
13816 +/** Allocates a timer */
13817 +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
13818 +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
13819 +
13820 +/** Frees a timer */
13821 +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
13822 +#define dwc_timer_free DWC_TIMER_FREE
13823 +
13824 +/** Schedules the timer to run at time ms from now. And will repeat at every
13825 + * repeat_interval msec therafter
13826 + *
13827 + * Modifies a timer that is still awaiting execution to a new expiration time.
13828 + * The mod_time is added to the old time. */
13829 +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
13830 +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
13831 +
13832 +/** Disables the timer from execution. */
13833 +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
13834 +#define dwc_timer_cancel DWC_TIMER_CANCEL
13835 +
13836 +
13837 +/** @name Spinlocks
13838 + *
13839 + * These locks are used when the work between the lock/unlock is atomic and
13840 + * short. Interrupts are also disabled during the lock/unlock and thus they are
13841 + * suitable to lock between interrupt/non-interrupt context. They also lock
13842 + * between processes if you have multiple CPUs or Preemption. If you don't have
13843 + * multiple CPUS or Preemption, then the you can simply implement the
13844 + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
13845 + * the work between the lock/unlock is atomic, the process context will never
13846 + * change, and so you never have to lock between processes. */
13847 +
13848 +struct dwc_spinlock;
13849 +
13850 +/** Type for a spinlock */
13851 +typedef struct dwc_spinlock dwc_spinlock_t;
13852 +
13853 +/** Type for the 'flags' argument to spinlock funtions */
13854 +typedef unsigned long dwc_irqflags_t;
13855 +
13856 +/** Returns an initialized lock variable. This function should allocate and
13857 + * initialize the OS-specific data structure used for locking. This data
13858 + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
13859 + * be freed by the DWC_FREE_LOCK when it is no longer used.
13860 + *
13861 + * For Linux Spinlock Debugging make it macro because the debugging routines use
13862 + * the symbol name to determine recursive locking. Using a wrapper function
13863 + * makes it falsely think recursive locking occurs. */
13864 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
13865 +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
13866 + lock = DWC_ALLOC(sizeof(spinlock_t)); \
13867 + if (lock) { \
13868 + spin_lock_init((spinlock_t *)lock); \
13869 + } \
13870 +})
13871 +#else
13872 +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
13873 +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
13874 +#endif
13875 +
13876 +/** Frees an initialized lock variable. */
13877 +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
13878 +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
13879 +
13880 +/** Disables interrupts and blocks until it acquires the lock.
13881 + *
13882 + * @param lock Pointer to the spinlock.
13883 + * @param flags Unsigned long for irq flags storage.
13884 + */
13885 +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
13886 +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
13887 +
13888 +/** Re-enables the interrupt and releases the lock.
13889 + *
13890 + * @param lock Pointer to the spinlock.
13891 + * @param flags Unsigned long for irq flags storage. Must be the same as was
13892 + * passed into DWC_LOCK.
13893 + */
13894 +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
13895 +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
13896 +
13897 +/** Blocks until it acquires the lock.
13898 + *
13899 + * @param lock Pointer to the spinlock.
13900 + */
13901 +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
13902 +#define dwc_spinlock DWC_SPINLOCK
13903 +
13904 +/** Releases the lock.
13905 + *
13906 + * @param lock Pointer to the spinlock.
13907 + */
13908 +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
13909 +#define dwc_spinunlock DWC_SPINUNLOCK
13910 +
13911 +
13912 +/** @name Mutexes
13913 + *
13914 + * Unlike spinlocks Mutexes lock only between processes and the work between the
13915 + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
13916 + */
13917 +
13918 +struct dwc_mutex;
13919 +
13920 +/** Type for a mutex */
13921 +typedef struct dwc_mutex dwc_mutex_t;
13922 +
13923 +/* For Linux Mutex Debugging make it inline because the debugging routines use
13924 + * the symbol to determine recursive locking. This makes it falsely think
13925 + * recursive locking occurs. */
13926 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
13927 +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
13928 + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
13929 + mutex_init((struct mutex *)__mutexp); \
13930 +})
13931 +#endif
13932 +
13933 +/** Allocate a mutex */
13934 +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
13935 +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
13936 +
13937 +/* For memory leak debugging when using Linux Mutex Debugging */
13938 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
13939 +#define DWC_MUTEX_FREE(__mutexp) do { \
13940 + mutex_destroy((struct mutex *)__mutexp); \
13941 + DWC_FREE(__mutexp); \
13942 +} while(0)
13943 +#else
13944 +/** Free a mutex */
13945 +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
13946 +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
13947 +#endif
13948 +
13949 +/** Lock a mutex */
13950 +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
13951 +#define dwc_mutex_lock DWC_MUTEX_LOCK
13952 +
13953 +/** Non-blocking lock returns 1 on successful lock. */
13954 +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
13955 +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
13956 +
13957 +/** Unlock a mutex */
13958 +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
13959 +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
13960 +
13961 +
13962 +/** @name Time */
13963 +
13964 +/** Microsecond delay.
13965 + *
13966 + * @param usecs Microseconds to delay.
13967 + */
13968 +extern void DWC_UDELAY(uint32_t usecs);
13969 +#define dwc_udelay DWC_UDELAY
13970 +
13971 +/** Millisecond delay.
13972 + *
13973 + * @param msecs Milliseconds to delay.
13974 + */
13975 +extern void DWC_MDELAY(uint32_t msecs);
13976 +#define dwc_mdelay DWC_MDELAY
13977 +
13978 +/** Non-busy waiting.
13979 + * Sleeps for specified number of milliseconds.
13980 + *
13981 + * @param msecs Milliseconds to sleep.
13982 + */
13983 +extern void DWC_MSLEEP(uint32_t msecs);
13984 +#define dwc_msleep DWC_MSLEEP
13985 +
13986 +/**
13987 + * Returns number of milliseconds since boot.
13988 + */
13989 +extern uint32_t DWC_TIME(void);
13990 +#define dwc_time DWC_TIME
13991 +
13992 +
13993 +
13994 +
13995 +/* @mainpage DWC Portability and Common Library
13996 + *
13997 + * This is the documentation for the DWC Portability and Common Library.
13998 + *
13999 + * @section intro Introduction
14000 + *
14001 + * The DWC Portability library consists of wrapper calls and data structures to
14002 + * all low-level functions which are typically provided by the OS. The WUDEV
14003 + * driver uses only these functions. In order to port the WUDEV driver, only
14004 + * the functions in this library need to be re-implemented, with the same
14005 + * behavior as documented here.
14006 + *
14007 + * The Common library consists of higher level functions, which rely only on
14008 + * calling the functions from the DWC Portability library. These common
14009 + * routines are shared across modules. Some of the common libraries need to be
14010 + * used directly by the driver programmer when porting WUDEV. Such as the
14011 + * parameter and notification libraries.
14012 + *
14013 + * @section low Portability Library OS Wrapper Functions
14014 + *
14015 + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
14016 + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
14017 + * these functions are included in the dwc_os.h file.
14018 + *
14019 + * There are many functions here covering a wide array of OS services. Please
14020 + * see dwc_os.h for details, and implementation notes for each function.
14021 + *
14022 + * @section common Common Library Functions
14023 + *
14024 + * Any function starting with dwc and in all lowercase is a common library
14025 + * routine. These functions have a portable implementation and do not need to
14026 + * be reimplemented when porting. The common routines can be used by any
14027 + * driver, and some must be used by the end user to control the drivers. For
14028 + * example, you must use the Parameter common library in order to set the
14029 + * parameters in the WUDEV module.
14030 + *
14031 + * The common libraries consist of the following:
14032 + *
14033 + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
14034 + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
14035 + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
14036 + * - Lists - Used internally and can be used by end-user. See dwc_list.h
14037 + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
14038 + * - Modpow - Used internally only. See dwc_modpow.h
14039 + * - DH - Used internally only. See dwc_dh.h
14040 + * - Crypto - Used internally only. See dwc_crypto.h
14041 + *
14042 + *
14043 + * @section prereq Prerequistes For dwc_os.h
14044 + * @subsection types Data Types
14045 + *
14046 + * The dwc_os.h file assumes that several low-level data types are pre defined for the
14047 + * compilation environment. These data types are:
14048 + *
14049 + * - uint8_t - unsigned 8-bit data type
14050 + * - int8_t - signed 8-bit data type
14051 + * - uint16_t - unsigned 16-bit data type
14052 + * - int16_t - signed 16-bit data type
14053 + * - uint32_t - unsigned 32-bit data type
14054 + * - int32_t - signed 32-bit data type
14055 + * - uint64_t - unsigned 64-bit data type
14056 + * - int64_t - signed 64-bit data type
14057 + *
14058 + * Ensure that these are defined before using dwc_os.h. The easiest way to do
14059 + * that is to modify the top of the file to include the appropriate header.
14060 + * This is already done for the Linux environment. If the DWC_LINUX macro is
14061 + * defined, the correct header will be added. A standard header <stdint.h> is
14062 + * also used for environments where standard C headers are available.
14063 + *
14064 + * @subsection stdarg Variable Arguments
14065 + *
14066 + * Variable arguments are provided by a standard C header <stdarg.h>. it is
14067 + * available in Both the Linux and ANSI C enviornment. An equivalent must be
14068 + * provided in your enviornment in order to use dwc_os.h with the debug and
14069 + * tracing message functionality.
14070 + *
14071 + * @subsection thread Threading
14072 + *
14073 + * WUDEV Core must be run on an operating system that provides for multiple
14074 + * threads/processes. Threading can be implemented in many ways, even in
14075 + * embedded systems without an operating system. At the bare minimum, the
14076 + * system should be able to start any number of processes at any time to handle
14077 + * special work. It need not be a pre-emptive system. Process context can
14078 + * change upon a call to a blocking function. The hardware interrupt context
14079 + * that calls the module's ISR() function must be differentiable from process
14080 + * context, even if your processes are impemented via a hardware interrupt.
14081 + * Further locking mechanism between process must exist (or be implemented), and
14082 + * process context must have a way to disable interrupts for a period of time to
14083 + * lock them out. If all of this exists, the functions in dwc_os.h related to
14084 + * threading should be able to be implemented with the defined behavior.
14085 + *
14086 + */
14087 +
14088 +#ifdef __cplusplus
14089 +}
14090 +#endif
14091 +
14092 +#endif /* _DWC_OS_H_ */
14093 --- /dev/null
14094 +++ b/drivers/usb/host/dwc_common_port/usb.h
14095 @@ -0,0 +1,946 @@
14096 +/*
14097 + * Copyright (c) 1998 The NetBSD Foundation, Inc.
14098 + * All rights reserved.
14099 + *
14100 + * This code is derived from software contributed to The NetBSD Foundation
14101 + * by Lennart Augustsson (lennart@augustsson.net) at
14102 + * Carlstedt Research & Technology.
14103 + *
14104 + * Redistribution and use in source and binary forms, with or without
14105 + * modification, are permitted provided that the following conditions
14106 + * are met:
14107 + * 1. Redistributions of source code must retain the above copyright
14108 + * notice, this list of conditions and the following disclaimer.
14109 + * 2. Redistributions in binary form must reproduce the above copyright
14110 + * notice, this list of conditions and the following disclaimer in the
14111 + * documentation and/or other materials provided with the distribution.
14112 + * 3. All advertising materials mentioning features or use of this software
14113 + * must display the following acknowledgement:
14114 + * This product includes software developed by the NetBSD
14115 + * Foundation, Inc. and its contributors.
14116 + * 4. Neither the name of The NetBSD Foundation nor the names of its
14117 + * contributors may be used to endorse or promote products derived
14118 + * from this software without specific prior written permission.
14119 + *
14120 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14121 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
14122 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14123 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
14124 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
14125 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
14126 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
14127 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
14128 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
14129 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
14130 + * POSSIBILITY OF SUCH DAMAGE.
14131 + */
14132 +
14133 +/* Modified by Synopsys, Inc, 12/12/2007 */
14134 +
14135 +
14136 +#ifndef _USB_H_
14137 +#define _USB_H_
14138 +
14139 +#ifdef __cplusplus
14140 +extern "C" {
14141 +#endif
14142 +
14143 +/*
14144 + * The USB records contain some unaligned little-endian word
14145 + * components. The U[SG]ETW macros take care of both the alignment
14146 + * and endian problem and should always be used to access non-byte
14147 + * values.
14148 + */
14149 +typedef u_int8_t uByte;
14150 +typedef u_int8_t uWord[2];
14151 +typedef u_int8_t uDWord[4];
14152 +
14153 +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
14154 +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
14155 +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
14156 + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
14157 +
14158 +#if 1
14159 +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
14160 +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
14161 +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
14162 +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
14163 + (w)[1] = (u_int8_t)((v) >> 8), \
14164 + (w)[2] = (u_int8_t)((v) >> 16), \
14165 + (w)[3] = (u_int8_t)((v) >> 24))
14166 +#else
14167 +/*
14168 + * On little-endian machines that can handle unanliged accesses
14169 + * (e.g. i386) these macros can be replaced by the following.
14170 + */
14171 +#define UGETW(w) (*(u_int16_t *)(w))
14172 +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
14173 +#define UGETDW(w) (*(u_int32_t *)(w))
14174 +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
14175 +#endif
14176 +
14177 +/*
14178 + * Macros for accessing UAS IU fields, which are big-endian
14179 + */
14180 +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
14181 +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
14182 +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
14183 + ((x) >> 8) & 0xff, (x) & 0xff }
14184 +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
14185 +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
14186 +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
14187 +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
14188 + (w)[1] = (u_int8_t)((v) >> 16), \
14189 + (w)[2] = (u_int8_t)((v) >> 8), \
14190 + (w)[3] = (u_int8_t)(v))
14191 +
14192 +#define UPACKED __attribute__((__packed__))
14193 +
14194 +typedef struct {
14195 + uByte bmRequestType;
14196 + uByte bRequest;
14197 + uWord wValue;
14198 + uWord wIndex;
14199 + uWord wLength;
14200 +} UPACKED usb_device_request_t;
14201 +
14202 +#define UT_GET_DIR(a) ((a) & 0x80)
14203 +#define UT_WRITE 0x00
14204 +#define UT_READ 0x80
14205 +
14206 +#define UT_GET_TYPE(a) ((a) & 0x60)
14207 +#define UT_STANDARD 0x00
14208 +#define UT_CLASS 0x20
14209 +#define UT_VENDOR 0x40
14210 +
14211 +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
14212 +#define UT_DEVICE 0x00
14213 +#define UT_INTERFACE 0x01
14214 +#define UT_ENDPOINT 0x02
14215 +#define UT_OTHER 0x03
14216 +
14217 +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
14218 +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
14219 +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
14220 +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
14221 +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
14222 +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
14223 +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
14224 +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
14225 +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
14226 +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
14227 +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
14228 +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
14229 +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
14230 +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
14231 +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
14232 +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
14233 +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
14234 +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
14235 +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
14236 +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
14237 +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
14238 +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
14239 +
14240 +/* Requests */
14241 +#define UR_GET_STATUS 0x00
14242 +#define USTAT_STANDARD_STATUS 0x00
14243 +#define WUSTAT_WUSB_FEATURE 0x01
14244 +#define WUSTAT_CHANNEL_INFO 0x02
14245 +#define WUSTAT_RECEIVED_DATA 0x03
14246 +#define WUSTAT_MAS_AVAILABILITY 0x04
14247 +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
14248 +#define UR_CLEAR_FEATURE 0x01
14249 +#define UR_SET_FEATURE 0x03
14250 +#define UR_SET_AND_TEST_FEATURE 0x0c
14251 +#define UR_SET_ADDRESS 0x05
14252 +#define UR_GET_DESCRIPTOR 0x06
14253 +#define UDESC_DEVICE 0x01
14254 +#define UDESC_CONFIG 0x02
14255 +#define UDESC_STRING 0x03
14256 +#define UDESC_INTERFACE 0x04
14257 +#define UDESC_ENDPOINT 0x05
14258 +#define UDESC_SS_USB_COMPANION 0x30
14259 +#define UDESC_DEVICE_QUALIFIER 0x06
14260 +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
14261 +#define UDESC_INTERFACE_POWER 0x08
14262 +#define UDESC_OTG 0x09
14263 +#define WUDESC_SECURITY 0x0c
14264 +#define WUDESC_KEY 0x0d
14265 +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
14266 +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
14267 +#define WUD_KEY_TYPE_ASSOC 0x01
14268 +#define WUD_KEY_TYPE_GTK 0x02
14269 +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
14270 +#define WUD_KEY_ORIGIN_HOST 0x00
14271 +#define WUD_KEY_ORIGIN_DEVICE 0x01
14272 +#define WUDESC_ENCRYPTION_TYPE 0x0e
14273 +#define WUDESC_BOS 0x0f
14274 +#define WUDESC_DEVICE_CAPABILITY 0x10
14275 +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
14276 +#define UDESC_BOS 0x0f
14277 +#define UDESC_DEVICE_CAPABILITY 0x10
14278 +#define UDESC_CS_DEVICE 0x21 /* class specific */
14279 +#define UDESC_CS_CONFIG 0x22
14280 +#define UDESC_CS_STRING 0x23
14281 +#define UDESC_CS_INTERFACE 0x24
14282 +#define UDESC_CS_ENDPOINT 0x25
14283 +#define UDESC_HUB 0x29
14284 +#define UR_SET_DESCRIPTOR 0x07
14285 +#define UR_GET_CONFIG 0x08
14286 +#define UR_SET_CONFIG 0x09
14287 +#define UR_GET_INTERFACE 0x0a
14288 +#define UR_SET_INTERFACE 0x0b
14289 +#define UR_SYNCH_FRAME 0x0c
14290 +#define WUR_SET_ENCRYPTION 0x0d
14291 +#define WUR_GET_ENCRYPTION 0x0e
14292 +#define WUR_SET_HANDSHAKE 0x0f
14293 +#define WUR_GET_HANDSHAKE 0x10
14294 +#define WUR_SET_CONNECTION 0x11
14295 +#define WUR_SET_SECURITY_DATA 0x12
14296 +#define WUR_GET_SECURITY_DATA 0x13
14297 +#define WUR_SET_WUSB_DATA 0x14
14298 +#define WUDATA_DRPIE_INFO 0x01
14299 +#define WUDATA_TRANSMIT_DATA 0x02
14300 +#define WUDATA_TRANSMIT_PARAMS 0x03
14301 +#define WUDATA_RECEIVE_PARAMS 0x04
14302 +#define WUDATA_TRANSMIT_POWER 0x05
14303 +#define WUR_LOOPBACK_DATA_WRITE 0x15
14304 +#define WUR_LOOPBACK_DATA_READ 0x16
14305 +#define WUR_SET_INTERFACE_DS 0x17
14306 +
14307 +/* Feature numbers */
14308 +#define UF_ENDPOINT_HALT 0
14309 +#define UF_DEVICE_REMOTE_WAKEUP 1
14310 +#define UF_TEST_MODE 2
14311 +#define UF_DEVICE_B_HNP_ENABLE 3
14312 +#define UF_DEVICE_A_HNP_SUPPORT 4
14313 +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
14314 +#define WUF_WUSB 3
14315 +#define WUF_TX_DRPIE 0x0
14316 +#define WUF_DEV_XMIT_PACKET 0x1
14317 +#define WUF_COUNT_PACKETS 0x2
14318 +#define WUF_CAPTURE_PACKETS 0x3
14319 +#define UF_FUNCTION_SUSPEND 0
14320 +#define UF_U1_ENABLE 48
14321 +#define UF_U2_ENABLE 49
14322 +#define UF_LTM_ENABLE 50
14323 +
14324 +/* Class requests from the USB 2.0 hub spec, table 11-15 */
14325 +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
14326 +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
14327 +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
14328 +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
14329 +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
14330 +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
14331 +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
14332 +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
14333 +
14334 +#ifdef _MSC_VER
14335 +#include <pshpack1.h>
14336 +#endif
14337 +
14338 +typedef struct {
14339 + uByte bLength;
14340 + uByte bDescriptorType;
14341 + uByte bDescriptorSubtype;
14342 +} UPACKED usb_descriptor_t;
14343 +
14344 +typedef struct {
14345 + uByte bLength;
14346 + uByte bDescriptorType;
14347 +} UPACKED usb_descriptor_header_t;
14348 +
14349 +typedef struct {
14350 + uByte bLength;
14351 + uByte bDescriptorType;
14352 + uWord bcdUSB;
14353 +#define UD_USB_2_0 0x0200
14354 +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
14355 + uByte bDeviceClass;
14356 + uByte bDeviceSubClass;
14357 + uByte bDeviceProtocol;
14358 + uByte bMaxPacketSize;
14359 + /* The fields below are not part of the initial descriptor. */
14360 + uWord idVendor;
14361 + uWord idProduct;
14362 + uWord bcdDevice;
14363 + uByte iManufacturer;
14364 + uByte iProduct;
14365 + uByte iSerialNumber;
14366 + uByte bNumConfigurations;
14367 +} UPACKED usb_device_descriptor_t;
14368 +#define USB_DEVICE_DESCRIPTOR_SIZE 18
14369 +
14370 +typedef struct {
14371 + uByte bLength;
14372 + uByte bDescriptorType;
14373 + uWord wTotalLength;
14374 + uByte bNumInterface;
14375 + uByte bConfigurationValue;
14376 + uByte iConfiguration;
14377 +#define UC_ATT_ONE (1 << 7) /* must be set */
14378 +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
14379 +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
14380 +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
14381 + uByte bmAttributes;
14382 +#define UC_BUS_POWERED 0x80
14383 +#define UC_SELF_POWERED 0x40
14384 +#define UC_REMOTE_WAKEUP 0x20
14385 + uByte bMaxPower; /* max current in 2 mA units */
14386 +#define UC_POWER_FACTOR 2
14387 +} UPACKED usb_config_descriptor_t;
14388 +#define USB_CONFIG_DESCRIPTOR_SIZE 9
14389 +
14390 +typedef struct {
14391 + uByte bLength;
14392 + uByte bDescriptorType;
14393 + uByte bInterfaceNumber;
14394 + uByte bAlternateSetting;
14395 + uByte bNumEndpoints;
14396 + uByte bInterfaceClass;
14397 + uByte bInterfaceSubClass;
14398 + uByte bInterfaceProtocol;
14399 + uByte iInterface;
14400 +} UPACKED usb_interface_descriptor_t;
14401 +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
14402 +
14403 +typedef struct {
14404 + uByte bLength;
14405 + uByte bDescriptorType;
14406 + uByte bEndpointAddress;
14407 +#define UE_GET_DIR(a) ((a) & 0x80)
14408 +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
14409 +#define UE_DIR_IN 0x80
14410 +#define UE_DIR_OUT 0x00
14411 +#define UE_ADDR 0x0f
14412 +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
14413 + uByte bmAttributes;
14414 +#define UE_XFERTYPE 0x03
14415 +#define UE_CONTROL 0x00
14416 +#define UE_ISOCHRONOUS 0x01
14417 +#define UE_BULK 0x02
14418 +#define UE_INTERRUPT 0x03
14419 +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
14420 +#define UE_ISO_TYPE 0x0c
14421 +#define UE_ISO_ASYNC 0x04
14422 +#define UE_ISO_ADAPT 0x08
14423 +#define UE_ISO_SYNC 0x0c
14424 +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
14425 + uWord wMaxPacketSize;
14426 + uByte bInterval;
14427 +} UPACKED usb_endpoint_descriptor_t;
14428 +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
14429 +
14430 +typedef struct ss_endpoint_companion_descriptor {
14431 + uByte bLength;
14432 + uByte bDescriptorType;
14433 + uByte bMaxBurst;
14434 +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
14435 +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
14436 +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
14437 +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
14438 + uByte bmAttributes;
14439 + uWord wBytesPerInterval;
14440 +} UPACKED ss_endpoint_companion_descriptor_t;
14441 +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
14442 +
14443 +typedef struct {
14444 + uByte bLength;
14445 + uByte bDescriptorType;
14446 + uWord bString[127];
14447 +} UPACKED usb_string_descriptor_t;
14448 +#define USB_MAX_STRING_LEN 128
14449 +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
14450 +
14451 +/* Hub specific request */
14452 +#define UR_GET_BUS_STATE 0x02
14453 +#define UR_CLEAR_TT_BUFFER 0x08
14454 +#define UR_RESET_TT 0x09
14455 +#define UR_GET_TT_STATE 0x0a
14456 +#define UR_STOP_TT 0x0b
14457 +
14458 +/* Hub features */
14459 +#define UHF_C_HUB_LOCAL_POWER 0
14460 +#define UHF_C_HUB_OVER_CURRENT 1
14461 +#define UHF_PORT_CONNECTION 0
14462 +#define UHF_PORT_ENABLE 1
14463 +#define UHF_PORT_SUSPEND 2
14464 +#define UHF_PORT_OVER_CURRENT 3
14465 +#define UHF_PORT_RESET 4
14466 +#define UHF_PORT_L1 5
14467 +#define UHF_PORT_POWER 8
14468 +#define UHF_PORT_LOW_SPEED 9
14469 +#define UHF_PORT_HIGH_SPEED 10
14470 +#define UHF_C_PORT_CONNECTION 16
14471 +#define UHF_C_PORT_ENABLE 17
14472 +#define UHF_C_PORT_SUSPEND 18
14473 +#define UHF_C_PORT_OVER_CURRENT 19
14474 +#define UHF_C_PORT_RESET 20
14475 +#define UHF_C_PORT_L1 23
14476 +#define UHF_PORT_TEST 21
14477 +#define UHF_PORT_INDICATOR 22
14478 +
14479 +typedef struct {
14480 + uByte bDescLength;
14481 + uByte bDescriptorType;
14482 + uByte bNbrPorts;
14483 + uWord wHubCharacteristics;
14484 +#define UHD_PWR 0x0003
14485 +#define UHD_PWR_GANGED 0x0000
14486 +#define UHD_PWR_INDIVIDUAL 0x0001
14487 +#define UHD_PWR_NO_SWITCH 0x0002
14488 +#define UHD_COMPOUND 0x0004
14489 +#define UHD_OC 0x0018
14490 +#define UHD_OC_GLOBAL 0x0000
14491 +#define UHD_OC_INDIVIDUAL 0x0008
14492 +#define UHD_OC_NONE 0x0010
14493 +#define UHD_TT_THINK 0x0060
14494 +#define UHD_TT_THINK_8 0x0000
14495 +#define UHD_TT_THINK_16 0x0020
14496 +#define UHD_TT_THINK_24 0x0040
14497 +#define UHD_TT_THINK_32 0x0060
14498 +#define UHD_PORT_IND 0x0080
14499 + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
14500 +#define UHD_PWRON_FACTOR 2
14501 + uByte bHubContrCurrent;
14502 + uByte DeviceRemovable[32]; /* max 255 ports */
14503 +#define UHD_NOT_REMOV(desc, i) \
14504 + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
14505 + /* deprecated */ uByte PortPowerCtrlMask[1];
14506 +} UPACKED usb_hub_descriptor_t;
14507 +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
14508 +
14509 +typedef struct {
14510 + uByte bLength;
14511 + uByte bDescriptorType;
14512 + uWord bcdUSB;
14513 + uByte bDeviceClass;
14514 + uByte bDeviceSubClass;
14515 + uByte bDeviceProtocol;
14516 + uByte bMaxPacketSize0;
14517 + uByte bNumConfigurations;
14518 + uByte bReserved;
14519 +} UPACKED usb_device_qualifier_t;
14520 +#define USB_DEVICE_QUALIFIER_SIZE 10
14521 +
14522 +typedef struct {
14523 + uByte bLength;
14524 + uByte bDescriptorType;
14525 + uByte bmAttributes;
14526 +#define UOTG_SRP 0x01
14527 +#define UOTG_HNP 0x02
14528 +} UPACKED usb_otg_descriptor_t;
14529 +
14530 +/* OTG feature selectors */
14531 +#define UOTG_B_HNP_ENABLE 3
14532 +#define UOTG_A_HNP_SUPPORT 4
14533 +#define UOTG_A_ALT_HNP_SUPPORT 5
14534 +
14535 +typedef struct {
14536 + uWord wStatus;
14537 +/* Device status flags */
14538 +#define UDS_SELF_POWERED 0x0001
14539 +#define UDS_REMOTE_WAKEUP 0x0002
14540 +/* Endpoint status flags */
14541 +#define UES_HALT 0x0001
14542 +} UPACKED usb_status_t;
14543 +
14544 +typedef struct {
14545 + uWord wHubStatus;
14546 +#define UHS_LOCAL_POWER 0x0001
14547 +#define UHS_OVER_CURRENT 0x0002
14548 + uWord wHubChange;
14549 +} UPACKED usb_hub_status_t;
14550 +
14551 +typedef struct {
14552 + uWord wPortStatus;
14553 +#define UPS_CURRENT_CONNECT_STATUS 0x0001
14554 +#define UPS_PORT_ENABLED 0x0002
14555 +#define UPS_SUSPEND 0x0004
14556 +#define UPS_OVERCURRENT_INDICATOR 0x0008
14557 +#define UPS_RESET 0x0010
14558 +#define UPS_PORT_POWER 0x0100
14559 +#define UPS_LOW_SPEED 0x0200
14560 +#define UPS_HIGH_SPEED 0x0400
14561 +#define UPS_PORT_TEST 0x0800
14562 +#define UPS_PORT_INDICATOR 0x1000
14563 + uWord wPortChange;
14564 +#define UPS_C_CONNECT_STATUS 0x0001
14565 +#define UPS_C_PORT_ENABLED 0x0002
14566 +#define UPS_C_SUSPEND 0x0004
14567 +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
14568 +#define UPS_C_PORT_RESET 0x0010
14569 +} UPACKED usb_port_status_t;
14570 +
14571 +#ifdef _MSC_VER
14572 +#include <poppack.h>
14573 +#endif
14574 +
14575 +/* Device class codes */
14576 +#define UDCLASS_IN_INTERFACE 0x00
14577 +#define UDCLASS_COMM 0x02
14578 +#define UDCLASS_HUB 0x09
14579 +#define UDSUBCLASS_HUB 0x00
14580 +#define UDPROTO_FSHUB 0x00
14581 +#define UDPROTO_HSHUBSTT 0x01
14582 +#define UDPROTO_HSHUBMTT 0x02
14583 +#define UDCLASS_DIAGNOSTIC 0xdc
14584 +#define UDCLASS_WIRELESS 0xe0
14585 +#define UDSUBCLASS_RF 0x01
14586 +#define UDPROTO_BLUETOOTH 0x01
14587 +#define UDCLASS_VENDOR 0xff
14588 +
14589 +/* Interface class codes */
14590 +#define UICLASS_UNSPEC 0x00
14591 +
14592 +#define UICLASS_AUDIO 0x01
14593 +#define UISUBCLASS_AUDIOCONTROL 1
14594 +#define UISUBCLASS_AUDIOSTREAM 2
14595 +#define UISUBCLASS_MIDISTREAM 3
14596 +
14597 +#define UICLASS_CDC 0x02 /* communication */
14598 +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
14599 +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
14600 +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
14601 +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
14602 +#define UISUBCLASS_CAPI_CONTROLMODEL 5
14603 +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
14604 +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
14605 +#define UIPROTO_CDC_AT 1
14606 +
14607 +#define UICLASS_HID 0x03
14608 +#define UISUBCLASS_BOOT 1
14609 +#define UIPROTO_BOOT_KEYBOARD 1
14610 +
14611 +#define UICLASS_PHYSICAL 0x05
14612 +
14613 +#define UICLASS_IMAGE 0x06
14614 +
14615 +#define UICLASS_PRINTER 0x07
14616 +#define UISUBCLASS_PRINTER 1
14617 +#define UIPROTO_PRINTER_UNI 1
14618 +#define UIPROTO_PRINTER_BI 2
14619 +#define UIPROTO_PRINTER_1284 3
14620 +
14621 +#define UICLASS_MASS 0x08
14622 +#define UISUBCLASS_RBC 1
14623 +#define UISUBCLASS_SFF8020I 2
14624 +#define UISUBCLASS_QIC157 3
14625 +#define UISUBCLASS_UFI 4
14626 +#define UISUBCLASS_SFF8070I 5
14627 +#define UISUBCLASS_SCSI 6
14628 +#define UIPROTO_MASS_CBI_I 0
14629 +#define UIPROTO_MASS_CBI 1
14630 +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
14631 +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
14632 +
14633 +#define UICLASS_HUB 0x09
14634 +#define UISUBCLASS_HUB 0
14635 +#define UIPROTO_FSHUB 0
14636 +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
14637 +#define UIPROTO_HSHUBMTT 1
14638 +
14639 +#define UICLASS_CDC_DATA 0x0a
14640 +#define UISUBCLASS_DATA 0
14641 +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
14642 +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
14643 +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
14644 +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
14645 +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
14646 +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
14647 +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
14648 +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
14649 +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
14650 +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
14651 +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
14652 +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
14653 +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
14654 +
14655 +#define UICLASS_SMARTCARD 0x0b
14656 +
14657 +/*#define UICLASS_FIRM_UPD 0x0c*/
14658 +
14659 +#define UICLASS_SECURITY 0x0d
14660 +
14661 +#define UICLASS_DIAGNOSTIC 0xdc
14662 +
14663 +#define UICLASS_WIRELESS 0xe0
14664 +#define UISUBCLASS_RF 0x01
14665 +#define UIPROTO_BLUETOOTH 0x01
14666 +
14667 +#define UICLASS_APPL_SPEC 0xfe
14668 +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
14669 +#define UISUBCLASS_IRDA 2
14670 +#define UIPROTO_IRDA 0
14671 +
14672 +#define UICLASS_VENDOR 0xff
14673 +
14674 +#define USB_HUB_MAX_DEPTH 5
14675 +
14676 +/*
14677 + * Minimum time a device needs to be powered down to go through
14678 + * a power cycle. XXX Are these time in the spec?
14679 + */
14680 +#define USB_POWER_DOWN_TIME 200 /* ms */
14681 +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
14682 +
14683 +#if 0
14684 +/* These are the values from the spec. */
14685 +#define USB_PORT_RESET_DELAY 10 /* ms */
14686 +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
14687 +#define USB_PORT_RESET_RECOVERY 10 /* ms */
14688 +#define USB_PORT_POWERUP_DELAY 100 /* ms */
14689 +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
14690 +#define USB_RESUME_DELAY (20*5) /* ms */
14691 +#define USB_RESUME_WAIT 10 /* ms */
14692 +#define USB_RESUME_RECOVERY 10 /* ms */
14693 +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
14694 +#else
14695 +/* Allow for marginal (i.e. non-conforming) devices. */
14696 +#define USB_PORT_RESET_DELAY 50 /* ms */
14697 +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
14698 +#define USB_PORT_RESET_RECOVERY 250 /* ms */
14699 +#define USB_PORT_POWERUP_DELAY 300 /* ms */
14700 +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
14701 +#define USB_RESUME_DELAY (50*5) /* ms */
14702 +#define USB_RESUME_WAIT 50 /* ms */
14703 +#define USB_RESUME_RECOVERY 50 /* ms */
14704 +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
14705 +#endif
14706 +
14707 +#define USB_MIN_POWER 100 /* mA */
14708 +#define USB_MAX_POWER 500 /* mA */
14709 +
14710 +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
14711 +
14712 +#define USB_UNCONFIG_NO 0
14713 +#define USB_UNCONFIG_INDEX (-1)
14714 +
14715 +/*** ioctl() related stuff ***/
14716 +
14717 +struct usb_ctl_request {
14718 + int ucr_addr;
14719 + usb_device_request_t ucr_request;
14720 + void *ucr_data;
14721 + int ucr_flags;
14722 +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
14723 + int ucr_actlen; /* actual length transferred */
14724 +};
14725 +
14726 +struct usb_alt_interface {
14727 + int uai_config_index;
14728 + int uai_interface_index;
14729 + int uai_alt_no;
14730 +};
14731 +
14732 +#define USB_CURRENT_CONFIG_INDEX (-1)
14733 +#define USB_CURRENT_ALT_INDEX (-1)
14734 +
14735 +struct usb_config_desc {
14736 + int ucd_config_index;
14737 + usb_config_descriptor_t ucd_desc;
14738 +};
14739 +
14740 +struct usb_interface_desc {
14741 + int uid_config_index;
14742 + int uid_interface_index;
14743 + int uid_alt_index;
14744 + usb_interface_descriptor_t uid_desc;
14745 +};
14746 +
14747 +struct usb_endpoint_desc {
14748 + int ued_config_index;
14749 + int ued_interface_index;
14750 + int ued_alt_index;
14751 + int ued_endpoint_index;
14752 + usb_endpoint_descriptor_t ued_desc;
14753 +};
14754 +
14755 +struct usb_full_desc {
14756 + int ufd_config_index;
14757 + u_int ufd_size;
14758 + u_char *ufd_data;
14759 +};
14760 +
14761 +struct usb_string_desc {
14762 + int usd_string_index;
14763 + int usd_language_id;
14764 + usb_string_descriptor_t usd_desc;
14765 +};
14766 +
14767 +struct usb_ctl_report_desc {
14768 + int ucrd_size;
14769 + u_char ucrd_data[1024]; /* filled data size will vary */
14770 +};
14771 +
14772 +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
14773 +
14774 +#define USB_MAX_DEVNAMES 4
14775 +#define USB_MAX_DEVNAMELEN 16
14776 +struct usb_device_info {
14777 + u_int8_t udi_bus;
14778 + u_int8_t udi_addr; /* device address */
14779 + usb_event_cookie_t udi_cookie;
14780 + char udi_product[USB_MAX_STRING_LEN];
14781 + char udi_vendor[USB_MAX_STRING_LEN];
14782 + char udi_release[8];
14783 + u_int16_t udi_productNo;
14784 + u_int16_t udi_vendorNo;
14785 + u_int16_t udi_releaseNo;
14786 + u_int8_t udi_class;
14787 + u_int8_t udi_subclass;
14788 + u_int8_t udi_protocol;
14789 + u_int8_t udi_config;
14790 + u_int8_t udi_speed;
14791 +#define USB_SPEED_UNKNOWN 0
14792 +#define USB_SPEED_LOW 1
14793 +#define USB_SPEED_FULL 2
14794 +#define USB_SPEED_HIGH 3
14795 +#define USB_SPEED_VARIABLE 4
14796 +#define USB_SPEED_SUPER 5
14797 + int udi_power; /* power consumption in mA, 0 if selfpowered */
14798 + int udi_nports;
14799 + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
14800 + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
14801 +#define USB_PORT_ENABLED 0xff
14802 +#define USB_PORT_SUSPENDED 0xfe
14803 +#define USB_PORT_POWERED 0xfd
14804 +#define USB_PORT_DISABLED 0xfc
14805 +};
14806 +
14807 +struct usb_ctl_report {
14808 + int ucr_report;
14809 + u_char ucr_data[1024]; /* filled data size will vary */
14810 +};
14811 +
14812 +struct usb_device_stats {
14813 + u_long uds_requests[4]; /* indexed by transfer type UE_* */
14814 +};
14815 +
14816 +#define WUSB_MIN_IE 0x80
14817 +#define WUSB_WCTA_IE 0x80
14818 +#define WUSB_WCONNECTACK_IE 0x81
14819 +#define WUSB_WHOSTINFO_IE 0x82
14820 +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
14821 +#define WUHI_CA_RECONN 0x00
14822 +#define WUHI_CA_LIMITED 0x01
14823 +#define WUHI_CA_ALL 0x03
14824 +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
14825 +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
14826 +#define WUSB_WDEV_DISCONNECT_IE 0x84
14827 +#define WUSB_WHOST_DISCONNECT_IE 0x85
14828 +#define WUSB_WRELEASE_CHANNEL_IE 0x86
14829 +#define WUSB_WWORK_IE 0x87
14830 +#define WUSB_WCHANNEL_STOP_IE 0x88
14831 +#define WUSB_WDEV_KEEPALIVE_IE 0x89
14832 +#define WUSB_WISOCH_DISCARD_IE 0x8A
14833 +#define WUSB_WRESETDEVICE_IE 0x8B
14834 +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
14835 +#define WUSB_MAX_IE 0x8C
14836 +
14837 +/* Device Notification Types */
14838 +
14839 +#define WUSB_DN_MIN 0x01
14840 +#define WUSB_DN_CONNECT 0x01
14841 +# define WUSB_DA_OLDCONN 0x00
14842 +# define WUSB_DA_NEWCONN 0x01
14843 +# define WUSB_DA_SELF_BEACON 0x02
14844 +# define WUSB_DA_DIR_BEACON 0x04
14845 +# define WUSB_DA_NO_BEACON 0x06
14846 +#define WUSB_DN_DISCONNECT 0x02
14847 +#define WUSB_DN_EPRDY 0x03
14848 +#define WUSB_DN_MASAVAILCHANGED 0x04
14849 +#define WUSB_DN_REMOTEWAKEUP 0x05
14850 +#define WUSB_DN_SLEEP 0x06
14851 +#define WUSB_DN_ALIVE 0x07
14852 +#define WUSB_DN_MAX 0x07
14853 +
14854 +#ifdef _MSC_VER
14855 +#include <pshpack1.h>
14856 +#endif
14857 +
14858 +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
14859 +typedef struct wusb_hndshk_data {
14860 + uByte bMessageNumber;
14861 + uByte bStatus;
14862 + uByte tTKID[3];
14863 + uByte bReserved;
14864 + uByte CDID[16];
14865 + uByte Nonce[16];
14866 + uByte MIC[8];
14867 +} UPACKED wusb_hndshk_data_t;
14868 +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
14869 +
14870 +/* WUSB Connection Context */
14871 +typedef struct wusb_conn_context {
14872 + uByte CHID [16];
14873 + uByte CDID [16];
14874 + uByte CK [16];
14875 +} UPACKED wusb_conn_context_t;
14876 +
14877 +/* WUSB Security Descriptor */
14878 +typedef struct wusb_security_desc {
14879 + uByte bLength;
14880 + uByte bDescriptorType;
14881 + uWord wTotalLength;
14882 + uByte bNumEncryptionTypes;
14883 +} UPACKED wusb_security_desc_t;
14884 +
14885 +/* WUSB Encryption Type Descriptor */
14886 +typedef struct wusb_encrypt_type_desc {
14887 + uByte bLength;
14888 + uByte bDescriptorType;
14889 +
14890 + uByte bEncryptionType;
14891 +#define WUETD_UNSECURE 0
14892 +#define WUETD_WIRED 1
14893 +#define WUETD_CCM_1 2
14894 +#define WUETD_RSA_1 3
14895 +
14896 + uByte bEncryptionValue;
14897 + uByte bAuthKeyIndex;
14898 +} UPACKED wusb_encrypt_type_desc_t;
14899 +
14900 +/* WUSB Key Descriptor */
14901 +typedef struct wusb_key_desc {
14902 + uByte bLength;
14903 + uByte bDescriptorType;
14904 + uByte tTKID[3];
14905 + uByte bReserved;
14906 + uByte KeyData[1]; /* variable length */
14907 +} UPACKED wusb_key_desc_t;
14908 +
14909 +/* WUSB BOS Descriptor (Binary device Object Store) */
14910 +typedef struct wusb_bos_desc {
14911 + uByte bLength;
14912 + uByte bDescriptorType;
14913 + uWord wTotalLength;
14914 + uByte bNumDeviceCaps;
14915 +} UPACKED wusb_bos_desc_t;
14916 +
14917 +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
14918 +typedef struct usb_dev_cap_20_ext_desc {
14919 + uByte bLength;
14920 + uByte bDescriptorType;
14921 + uByte bDevCapabilityType;
14922 +#define USB_20_EXT_LPM 0x02
14923 + uDWord bmAttributes;
14924 +} UPACKED usb_dev_cap_20_ext_desc_t;
14925 +
14926 +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
14927 +typedef struct usb_dev_cap_ss_usb {
14928 + uByte bLength;
14929 + uByte bDescriptorType;
14930 + uByte bDevCapabilityType;
14931 +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
14932 + uByte bmAttributes;
14933 +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
14934 +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
14935 +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
14936 +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
14937 + uWord wSpeedsSupported;
14938 + uByte bFunctionalitySupport;
14939 + uByte bU1DevExitLat;
14940 + uWord wU2DevExitLat;
14941 +} UPACKED usb_dev_cap_ss_usb_t;
14942 +
14943 +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
14944 +typedef struct usb_dev_cap_container_id {
14945 + uByte bLength;
14946 + uByte bDescriptorType;
14947 + uByte bDevCapabilityType;
14948 + uByte bReserved;
14949 + uByte containerID[16];
14950 +} UPACKED usb_dev_cap_container_id_t;
14951 +
14952 +/* Device Capability Type Codes */
14953 +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
14954 +
14955 +/* Device Capability Descriptor */
14956 +typedef struct wusb_dev_cap_desc {
14957 + uByte bLength;
14958 + uByte bDescriptorType;
14959 + uByte bDevCapabilityType;
14960 + uByte caps[1]; /* Variable length */
14961 +} UPACKED wusb_dev_cap_desc_t;
14962 +
14963 +/* Device Capability Descriptor */
14964 +typedef struct wusb_dev_cap_uwb_desc {
14965 + uByte bLength;
14966 + uByte bDescriptorType;
14967 + uByte bDevCapabilityType;
14968 + uByte bmAttributes;
14969 + uWord wPHYRates; /* Bitmap */
14970 + uByte bmTFITXPowerInfo;
14971 + uByte bmFFITXPowerInfo;
14972 + uWord bmBandGroup;
14973 + uByte bReserved;
14974 +} UPACKED wusb_dev_cap_uwb_desc_t;
14975 +
14976 +/* Wireless USB Endpoint Companion Descriptor */
14977 +typedef struct wusb_endpoint_companion_desc {
14978 + uByte bLength;
14979 + uByte bDescriptorType;
14980 + uByte bMaxBurst;
14981 + uByte bMaxSequence;
14982 + uWord wMaxStreamDelay;
14983 + uWord wOverTheAirPacketSize;
14984 + uByte bOverTheAirInterval;
14985 + uByte bmCompAttributes;
14986 +} UPACKED wusb_endpoint_companion_desc_t;
14987 +
14988 +/* Wireless USB Numeric Association M1 Data Structure */
14989 +typedef struct wusb_m1_data {
14990 + uByte version;
14991 + uWord langId;
14992 + uByte deviceFriendlyNameLength;
14993 + uByte sha_256_m3[32];
14994 + uByte deviceFriendlyName[256];
14995 +} UPACKED wusb_m1_data_t;
14996 +
14997 +typedef struct wusb_m2_data {
14998 + uByte version;
14999 + uWord langId;
15000 + uByte hostFriendlyNameLength;
15001 + uByte pkh[384];
15002 + uByte hostFriendlyName[256];
15003 +} UPACKED wusb_m2_data_t;
15004 +
15005 +typedef struct wusb_m3_data {
15006 + uByte pkd[384];
15007 + uByte nd;
15008 +} UPACKED wusb_m3_data_t;
15009 +
15010 +typedef struct wusb_m4_data {
15011 + uDWord _attributeTypeIdAndLength_1;
15012 + uWord associationTypeId;
15013 +
15014 + uDWord _attributeTypeIdAndLength_2;
15015 + uWord associationSubTypeId;
15016 +
15017 + uDWord _attributeTypeIdAndLength_3;
15018 + uDWord length;
15019 +
15020 + uDWord _attributeTypeIdAndLength_4;
15021 + uDWord associationStatus;
15022 +
15023 + uDWord _attributeTypeIdAndLength_5;
15024 + uByte chid[16];
15025 +
15026 + uDWord _attributeTypeIdAndLength_6;
15027 + uByte cdid[16];
15028 +
15029 + uDWord _attributeTypeIdAndLength_7;
15030 + uByte bandGroups[2];
15031 +} UPACKED wusb_m4_data_t;
15032 +
15033 +#ifdef _MSC_VER
15034 +#include <poppack.h>
15035 +#endif
15036 +
15037 +#ifdef __cplusplus
15038 +}
15039 +#endif
15040 +
15041 +#endif /* _USB_H_ */
15042 --- /dev/null
15043 +++ b/drivers/usb/host/dwc_otg/Makefile
15044 @@ -0,0 +1,82 @@
15045 +#
15046 +# Makefile for DWC_otg Highspeed USB controller driver
15047 +#
15048 +
15049 +ifneq ($(KERNELRELEASE),)
15050 +
15051 +# Use the BUS_INTERFACE variable to compile the software for either
15052 +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
15053 +ifeq ($(BUS_INTERFACE),)
15054 +# BUS_INTERFACE = -DPCI_INTERFACE
15055 +# BUS_INTERFACE = -DLM_INTERFACE
15056 + BUS_INTERFACE = -DPLATFORM_INTERFACE
15057 +endif
15058 +
15059 +#ccflags-y += -DDEBUG
15060 +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
15061 +
15062 +# Use one of the following flags to compile the software in host-only or
15063 +# device-only mode.
15064 +#ccflags-y += -DDWC_HOST_ONLY
15065 +#ccflags-y += -DDWC_DEVICE_ONLY
15066 +
15067 +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
15068 +#ccflags-y += -DDWC_EN_ISOC
15069 +ccflags-y += -I$(obj)/../dwc_common_port
15070 +#ccflags-y += -I$(PORTLIB)
15071 +ccflags-y += -DDWC_LINUX
15072 +ccflags-y += $(CFI)
15073 +ccflags-y += $(BUS_INTERFACE)
15074 +#ccflags-y += -DDWC_DEV_SRPCAP
15075 +
15076 +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
15077 +
15078 +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
15079 +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
15080 +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
15081 +dwc_otg-objs += dwc_otg_hcd.o dwc_otg_hcd_linux.o dwc_otg_hcd_intr.o dwc_otg_hcd_queue.o dwc_otg_hcd_ddma.o
15082 +dwc_otg-objs += dwc_otg_adp.o
15083 +dwc_otg-objs += dwc_otg_fiq_fsm.o
15084 +dwc_otg-objs += dwc_otg_fiq_stub.o
15085 +ifneq ($(CFI),)
15086 +dwc_otg-objs += dwc_otg_cfi.o
15087 +endif
15088 +
15089 +kernrelwd := $(subst ., ,$(KERNELRELEASE))
15090 +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
15091 +
15092 +ifneq ($(kernrel3),2.6.20)
15093 +ccflags-y += $(CPPFLAGS)
15094 +endif
15095 +
15096 +else
15097 +
15098 +PWD := $(shell pwd)
15099 +PORTLIB := $(PWD)/../dwc_common_port
15100 +
15101 +# Command paths
15102 +CTAGS := $(CTAGS)
15103 +DOXYGEN := $(DOXYGEN)
15104 +
15105 +default: portlib
15106 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
15107 +
15108 +install: default
15109 + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
15110 + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
15111 +
15112 +portlib:
15113 + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
15114 + cp $(PORTLIB)/Module.symvers $(PWD)/
15115 +
15116 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
15117 + $(DOXYGEN) doc/doxygen.cfg
15118 +
15119 +tags: $(wildcard *.[hc])
15120 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
15121 +
15122 +
15123 +clean:
15124 + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
15125 +
15126 +endif
15127 --- /dev/null
15128 +++ b/drivers/usb/host/dwc_otg/doc/doxygen.cfg
15129 @@ -0,0 +1,224 @@
15130 +# Doxyfile 1.3.9.1
15131 +
15132 +#---------------------------------------------------------------------------
15133 +# Project related configuration options
15134 +#---------------------------------------------------------------------------
15135 +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
15136 +PROJECT_NUMBER = v3.00a
15137 +OUTPUT_DIRECTORY = ./doc/
15138 +CREATE_SUBDIRS = NO
15139 +OUTPUT_LANGUAGE = English
15140 +BRIEF_MEMBER_DESC = YES
15141 +REPEAT_BRIEF = YES
15142 +ABBREVIATE_BRIEF = "The $name class" \
15143 + "The $name widget" \
15144 + "The $name file" \
15145 + is \
15146 + provides \
15147 + specifies \
15148 + contains \
15149 + represents \
15150 + a \
15151 + an \
15152 + the
15153 +ALWAYS_DETAILED_SEC = NO
15154 +INLINE_INHERITED_MEMB = NO
15155 +FULL_PATH_NAMES = NO
15156 +STRIP_FROM_PATH =
15157 +STRIP_FROM_INC_PATH =
15158 +SHORT_NAMES = NO
15159 +JAVADOC_AUTOBRIEF = YES
15160 +MULTILINE_CPP_IS_BRIEF = NO
15161 +INHERIT_DOCS = YES
15162 +DISTRIBUTE_GROUP_DOC = NO
15163 +TAB_SIZE = 8
15164 +ALIASES =
15165 +OPTIMIZE_OUTPUT_FOR_C = YES
15166 +OPTIMIZE_OUTPUT_JAVA = NO
15167 +SUBGROUPING = YES
15168 +#---------------------------------------------------------------------------
15169 +# Build related configuration options
15170 +#---------------------------------------------------------------------------
15171 +EXTRACT_ALL = NO
15172 +EXTRACT_PRIVATE = YES
15173 +EXTRACT_STATIC = YES
15174 +EXTRACT_LOCAL_CLASSES = YES
15175 +EXTRACT_LOCAL_METHODS = NO
15176 +HIDE_UNDOC_MEMBERS = NO
15177 +HIDE_UNDOC_CLASSES = NO
15178 +HIDE_FRIEND_COMPOUNDS = NO
15179 +HIDE_IN_BODY_DOCS = NO
15180 +INTERNAL_DOCS = NO
15181 +CASE_SENSE_NAMES = NO
15182 +HIDE_SCOPE_NAMES = NO
15183 +SHOW_INCLUDE_FILES = YES
15184 +INLINE_INFO = YES
15185 +SORT_MEMBER_DOCS = NO
15186 +SORT_BRIEF_DOCS = NO
15187 +SORT_BY_SCOPE_NAME = NO
15188 +GENERATE_TODOLIST = YES
15189 +GENERATE_TESTLIST = YES
15190 +GENERATE_BUGLIST = YES
15191 +GENERATE_DEPRECATEDLIST= YES
15192 +ENABLED_SECTIONS =
15193 +MAX_INITIALIZER_LINES = 30
15194 +SHOW_USED_FILES = YES
15195 +SHOW_DIRECTORIES = YES
15196 +#---------------------------------------------------------------------------
15197 +# configuration options related to warning and progress messages
15198 +#---------------------------------------------------------------------------
15199 +QUIET = YES
15200 +WARNINGS = YES
15201 +WARN_IF_UNDOCUMENTED = NO
15202 +WARN_IF_DOC_ERROR = YES
15203 +WARN_FORMAT = "$file:$line: $text"
15204 +WARN_LOGFILE =
15205 +#---------------------------------------------------------------------------
15206 +# configuration options related to the input files
15207 +#---------------------------------------------------------------------------
15208 +INPUT = .
15209 +FILE_PATTERNS = *.c \
15210 + *.h \
15211 + ./linux/*.c \
15212 + ./linux/*.h
15213 +RECURSIVE = NO
15214 +EXCLUDE = ./test/ \
15215 + ./dwc_otg/.AppleDouble/
15216 +EXCLUDE_SYMLINKS = YES
15217 +EXCLUDE_PATTERNS = *.mod.*
15218 +EXAMPLE_PATH =
15219 +EXAMPLE_PATTERNS = *
15220 +EXAMPLE_RECURSIVE = NO
15221 +IMAGE_PATH =
15222 +INPUT_FILTER =
15223 +FILTER_PATTERNS =
15224 +FILTER_SOURCE_FILES = NO
15225 +#---------------------------------------------------------------------------
15226 +# configuration options related to source browsing
15227 +#---------------------------------------------------------------------------
15228 +SOURCE_BROWSER = YES
15229 +INLINE_SOURCES = NO
15230 +STRIP_CODE_COMMENTS = YES
15231 +REFERENCED_BY_RELATION = NO
15232 +REFERENCES_RELATION = NO
15233 +VERBATIM_HEADERS = NO
15234 +#---------------------------------------------------------------------------
15235 +# configuration options related to the alphabetical class index
15236 +#---------------------------------------------------------------------------
15237 +ALPHABETICAL_INDEX = NO
15238 +COLS_IN_ALPHA_INDEX = 5
15239 +IGNORE_PREFIX =
15240 +#---------------------------------------------------------------------------
15241 +# configuration options related to the HTML output
15242 +#---------------------------------------------------------------------------
15243 +GENERATE_HTML = YES
15244 +HTML_OUTPUT = html
15245 +HTML_FILE_EXTENSION = .html
15246 +HTML_HEADER =
15247 +HTML_FOOTER =
15248 +HTML_STYLESHEET =
15249 +HTML_ALIGN_MEMBERS = YES
15250 +GENERATE_HTMLHELP = NO
15251 +CHM_FILE =
15252 +HHC_LOCATION =
15253 +GENERATE_CHI = NO
15254 +BINARY_TOC = NO
15255 +TOC_EXPAND = NO
15256 +DISABLE_INDEX = NO
15257 +ENUM_VALUES_PER_LINE = 4
15258 +GENERATE_TREEVIEW = YES
15259 +TREEVIEW_WIDTH = 250
15260 +#---------------------------------------------------------------------------
15261 +# configuration options related to the LaTeX output
15262 +#---------------------------------------------------------------------------
15263 +GENERATE_LATEX = NO
15264 +LATEX_OUTPUT = latex
15265 +LATEX_CMD_NAME = latex
15266 +MAKEINDEX_CMD_NAME = makeindex
15267 +COMPACT_LATEX = NO
15268 +PAPER_TYPE = a4wide
15269 +EXTRA_PACKAGES =
15270 +LATEX_HEADER =
15271 +PDF_HYPERLINKS = NO
15272 +USE_PDFLATEX = NO
15273 +LATEX_BATCHMODE = NO
15274 +LATEX_HIDE_INDICES = NO
15275 +#---------------------------------------------------------------------------
15276 +# configuration options related to the RTF output
15277 +#---------------------------------------------------------------------------
15278 +GENERATE_RTF = NO
15279 +RTF_OUTPUT = rtf
15280 +COMPACT_RTF = NO
15281 +RTF_HYPERLINKS = NO
15282 +RTF_STYLESHEET_FILE =
15283 +RTF_EXTENSIONS_FILE =
15284 +#---------------------------------------------------------------------------
15285 +# configuration options related to the man page output
15286 +#---------------------------------------------------------------------------
15287 +GENERATE_MAN = NO
15288 +MAN_OUTPUT = man
15289 +MAN_EXTENSION = .3
15290 +MAN_LINKS = NO
15291 +#---------------------------------------------------------------------------
15292 +# configuration options related to the XML output
15293 +#---------------------------------------------------------------------------
15294 +GENERATE_XML = NO
15295 +XML_OUTPUT = xml
15296 +XML_SCHEMA =
15297 +XML_DTD =
15298 +XML_PROGRAMLISTING = YES
15299 +#---------------------------------------------------------------------------
15300 +# configuration options for the AutoGen Definitions output
15301 +#---------------------------------------------------------------------------
15302 +GENERATE_AUTOGEN_DEF = NO
15303 +#---------------------------------------------------------------------------
15304 +# configuration options related to the Perl module output
15305 +#---------------------------------------------------------------------------
15306 +GENERATE_PERLMOD = NO
15307 +PERLMOD_LATEX = NO
15308 +PERLMOD_PRETTY = YES
15309 +PERLMOD_MAKEVAR_PREFIX =
15310 +#---------------------------------------------------------------------------
15311 +# Configuration options related to the preprocessor
15312 +#---------------------------------------------------------------------------
15313 +ENABLE_PREPROCESSING = YES
15314 +MACRO_EXPANSION = YES
15315 +EXPAND_ONLY_PREDEF = YES
15316 +SEARCH_INCLUDES = YES
15317 +INCLUDE_PATH =
15318 +INCLUDE_FILE_PATTERNS =
15319 +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
15320 +EXPAND_AS_DEFINED = DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW DWC_OTG_DEVICE_ATTR_BITFIELD_STORE DWC_OTG_DEVICE_ATTR_BITFIELD_RW DWC_OTG_DEVICE_ATTR_BITFIELD_RO DWC_OTG_DEVICE_ATTR_REG_SHOW DWC_OTG_DEVICE_ATTR_REG_STORE DWC_OTG_DEVICE_ATTR_REG32_RW DWC_OTG_DEVICE_ATTR_REG32_RO DWC_EN_ISOC
15321 +SKIP_FUNCTION_MACROS = NO
15322 +#---------------------------------------------------------------------------
15323 +# Configuration::additions related to external references
15324 +#---------------------------------------------------------------------------
15325 +TAGFILES =
15326 +GENERATE_TAGFILE =
15327 +ALLEXTERNALS = NO
15328 +EXTERNAL_GROUPS = YES
15329 +PERL_PATH = /usr/bin/perl
15330 +#---------------------------------------------------------------------------
15331 +# Configuration options related to the dot tool
15332 +#---------------------------------------------------------------------------
15333 +CLASS_DIAGRAMS = YES
15334 +HIDE_UNDOC_RELATIONS = YES
15335 +HAVE_DOT = NO
15336 +CLASS_GRAPH = YES
15337 +COLLABORATION_GRAPH = YES
15338 +UML_LOOK = NO
15339 +TEMPLATE_RELATIONS = NO
15340 +INCLUDE_GRAPH = YES
15341 +INCLUDED_BY_GRAPH = YES
15342 +CALL_GRAPH = NO
15343 +GRAPHICAL_HIERARCHY = YES
15344 +DOT_IMAGE_FORMAT = png
15345 +DOT_PATH =
15346 +DOTFILE_DIRS =
15347 +MAX_DOT_GRAPH_DEPTH = 1000
15348 +GENERATE_LEGEND = YES
15349 +DOT_CLEANUP = YES
15350 +#---------------------------------------------------------------------------
15351 +# Configuration::additions related to the search engine
15352 +#---------------------------------------------------------------------------
15353 +SEARCHENGINE = NO
15354 --- /dev/null
15355 +++ b/drivers/usb/host/dwc_otg/dummy_audio.c
15356 @@ -0,0 +1,1574 @@
15357 +/*
15358 + * zero.c -- Gadget Zero, for USB development
15359 + *
15360 + * Copyright (C) 2003-2004 David Brownell
15361 + * All rights reserved.
15362 + *
15363 + * Redistribution and use in source and binary forms, with or without
15364 + * modification, are permitted provided that the following conditions
15365 + * are met:
15366 + * 1. Redistributions of source code must retain the above copyright
15367 + * notice, this list of conditions, and the following disclaimer,
15368 + * without modification.
15369 + * 2. Redistributions in binary form must reproduce the above copyright
15370 + * notice, this list of conditions and the following disclaimer in the
15371 + * documentation and/or other materials provided with the distribution.
15372 + * 3. The names of the above-listed copyright holders may not be used
15373 + * to endorse or promote products derived from this software without
15374 + * specific prior written permission.
15375 + *
15376 + * ALTERNATIVELY, this software may be distributed under the terms of the
15377 + * GNU General Public License ("GPL") as published by the Free Software
15378 + * Foundation, either version 2 of that License or (at your option) any
15379 + * later version.
15380 + *
15381 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
15382 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15383 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15384 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
15385 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15386 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
15387 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
15388 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
15389 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
15390 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
15391 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15392 + */
15393 +
15394 +
15395 +/*
15396 + * Gadget Zero only needs two bulk endpoints, and is an example of how you
15397 + * can write a hardware-agnostic gadget driver running inside a USB device.
15398 + *
15399 + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
15400 + * affect most of the driver.
15401 + *
15402 + * Use it with the Linux host/master side "usbtest" driver to get a basic
15403 + * functional test of your device-side usb stack, or with "usb-skeleton".
15404 + *
15405 + * It supports two similar configurations. One sinks whatever the usb host
15406 + * writes, and in return sources zeroes. The other loops whatever the host
15407 + * writes back, so the host can read it. Module options include:
15408 + *
15409 + * buflen=N default N=4096, buffer size used
15410 + * qlen=N default N=32, how many buffers in the loopback queue
15411 + * loopdefault default false, list loopback config first
15412 + *
15413 + * Many drivers will only have one configuration, letting them be much
15414 + * simpler if they also don't support high speed operation (like this
15415 + * driver does).
15416 + */
15417 +
15418 +#include <linux/config.h>
15419 +#include <linux/module.h>
15420 +#include <linux/kernel.h>
15421 +#include <linux/delay.h>
15422 +#include <linux/ioport.h>
15423 +#include <linux/sched.h>
15424 +#include <linux/slab.h>
15425 +#include <linux/smp_lock.h>
15426 +#include <linux/errno.h>
15427 +#include <linux/init.h>
15428 +#include <linux/timer.h>
15429 +#include <linux/list.h>
15430 +#include <linux/interrupt.h>
15431 +#include <linux/uts.h>
15432 +#include <linux/version.h>
15433 +#include <linux/device.h>
15434 +#include <linux/moduleparam.h>
15435 +#include <linux/proc_fs.h>
15436 +
15437 +#include <asm/byteorder.h>
15438 +#include <asm/io.h>
15439 +#include <asm/irq.h>
15440 +#include <asm/system.h>
15441 +#include <asm/unaligned.h>
15442 +
15443 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
15444 +# include <linux/usb/ch9.h>
15445 +#else
15446 +# include <linux/usb_ch9.h>
15447 +#endif
15448 +
15449 +#include <linux/usb_gadget.h>
15450 +
15451 +
15452 +/*-------------------------------------------------------------------------*/
15453 +/*-------------------------------------------------------------------------*/
15454 +
15455 +
15456 +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
15457 +{
15458 + int count = 0;
15459 + u8 c;
15460 + u16 uchar;
15461 +
15462 + /* this insists on correct encodings, though not minimal ones.
15463 + * BUT it currently rejects legit 4-byte UTF-8 code points,
15464 + * which need surrogate pairs. (Unicode 3.1 can use them.)
15465 + */
15466 + while (len != 0 && (c = (u8) *s++) != 0) {
15467 + if (unlikely(c & 0x80)) {
15468 + // 2-byte sequence:
15469 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
15470 + if ((c & 0xe0) == 0xc0) {
15471 + uchar = (c & 0x1f) << 6;
15472 +
15473 + c = (u8) *s++;
15474 + if ((c & 0xc0) != 0xc0)
15475 + goto fail;
15476 + c &= 0x3f;
15477 + uchar |= c;
15478 +
15479 + // 3-byte sequence (most CJKV characters):
15480 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
15481 + } else if ((c & 0xf0) == 0xe0) {
15482 + uchar = (c & 0x0f) << 12;
15483 +
15484 + c = (u8) *s++;
15485 + if ((c & 0xc0) != 0xc0)
15486 + goto fail;
15487 + c &= 0x3f;
15488 + uchar |= c << 6;
15489 +
15490 + c = (u8) *s++;
15491 + if ((c & 0xc0) != 0xc0)
15492 + goto fail;
15493 + c &= 0x3f;
15494 + uchar |= c;
15495 +
15496 + /* no bogus surrogates */
15497 + if (0xd800 <= uchar && uchar <= 0xdfff)
15498 + goto fail;
15499 +
15500 + // 4-byte sequence (surrogate pairs, currently rare):
15501 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
15502 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
15503 + // (uuuuu = wwww + 1)
15504 + // FIXME accept the surrogate code points (only)
15505 +
15506 + } else
15507 + goto fail;
15508 + } else
15509 + uchar = c;
15510 + put_unaligned (cpu_to_le16 (uchar), cp++);
15511 + count++;
15512 + len--;
15513 + }
15514 + return count;
15515 +fail:
15516 + return -1;
15517 +}
15518 +
15519 +
15520 +/**
15521 + * usb_gadget_get_string - fill out a string descriptor
15522 + * @table: of c strings encoded using UTF-8
15523 + * @id: string id, from low byte of wValue in get string descriptor
15524 + * @buf: at least 256 bytes
15525 + *
15526 + * Finds the UTF-8 string matching the ID, and converts it into a
15527 + * string descriptor in utf16-le.
15528 + * Returns length of descriptor (always even) or negative errno
15529 + *
15530 + * If your driver needs stings in multiple languages, you'll probably
15531 + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
15532 + * using this routine after choosing which set of UTF-8 strings to use.
15533 + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
15534 + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
15535 + * characters (which are also widely used in C strings).
15536 + */
15537 +int
15538 +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
15539 +{
15540 + struct usb_string *s;
15541 + int len;
15542 +
15543 + /* descriptor 0 has the language id */
15544 + if (id == 0) {
15545 + buf [0] = 4;
15546 + buf [1] = USB_DT_STRING;
15547 + buf [2] = (u8) table->language;
15548 + buf [3] = (u8) (table->language >> 8);
15549 + return 4;
15550 + }
15551 + for (s = table->strings; s && s->s; s++)
15552 + if (s->id == id)
15553 + break;
15554 +
15555 + /* unrecognized: stall. */
15556 + if (!s || !s->s)
15557 + return -EINVAL;
15558 +
15559 + /* string descriptors have length, tag, then UTF16-LE text */
15560 + len = min ((size_t) 126, strlen (s->s));
15561 + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
15562 + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
15563 + if (len < 0)
15564 + return -EINVAL;
15565 + buf [0] = (len + 1) * 2;
15566 + buf [1] = USB_DT_STRING;
15567 + return buf [0];
15568 +}
15569 +
15570 +
15571 +/*-------------------------------------------------------------------------*/
15572 +/*-------------------------------------------------------------------------*/
15573 +
15574 +
15575 +/**
15576 + * usb_descriptor_fillbuf - fill buffer with descriptors
15577 + * @buf: Buffer to be filled
15578 + * @buflen: Size of buf
15579 + * @src: Array of descriptor pointers, terminated by null pointer.
15580 + *
15581 + * Copies descriptors into the buffer, returning the length or a
15582 + * negative error code if they can't all be copied. Useful when
15583 + * assembling descriptors for an associated set of interfaces used
15584 + * as part of configuring a composite device; or in other cases where
15585 + * sets of descriptors need to be marshaled.
15586 + */
15587 +int
15588 +usb_descriptor_fillbuf(void *buf, unsigned buflen,
15589 + const struct usb_descriptor_header **src)
15590 +{
15591 + u8 *dest = buf;
15592 +
15593 + if (!src)
15594 + return -EINVAL;
15595 +
15596 + /* fill buffer from src[] until null descriptor ptr */
15597 + for (; 0 != *src; src++) {
15598 + unsigned len = (*src)->bLength;
15599 +
15600 + if (len > buflen)
15601 + return -EINVAL;
15602 + memcpy(dest, *src, len);
15603 + buflen -= len;
15604 + dest += len;
15605 + }
15606 + return dest - (u8 *)buf;
15607 +}
15608 +
15609 +
15610 +/**
15611 + * usb_gadget_config_buf - builts a complete configuration descriptor
15612 + * @config: Header for the descriptor, including characteristics such
15613 + * as power requirements and number of interfaces.
15614 + * @desc: Null-terminated vector of pointers to the descriptors (interface,
15615 + * endpoint, etc) defining all functions in this device configuration.
15616 + * @buf: Buffer for the resulting configuration descriptor.
15617 + * @length: Length of buffer. If this is not big enough to hold the
15618 + * entire configuration descriptor, an error code will be returned.
15619 + *
15620 + * This copies descriptors into the response buffer, building a descriptor
15621 + * for that configuration. It returns the buffer length or a negative
15622 + * status code. The config.wTotalLength field is set to match the length
15623 + * of the result, but other descriptor fields (including power usage and
15624 + * interface count) must be set by the caller.
15625 + *
15626 + * Gadget drivers could use this when constructing a config descriptor
15627 + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
15628 + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
15629 + */
15630 +int usb_gadget_config_buf(
15631 + const struct usb_config_descriptor *config,
15632 + void *buf,
15633 + unsigned length,
15634 + const struct usb_descriptor_header **desc
15635 +)
15636 +{
15637 + struct usb_config_descriptor *cp = buf;
15638 + int len;
15639 +
15640 + /* config descriptor first */
15641 + if (length < USB_DT_CONFIG_SIZE || !desc)
15642 + return -EINVAL;
15643 + *cp = *config;
15644 +
15645 + /* then interface/endpoint/class/vendor/... */
15646 + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
15647 + length - USB_DT_CONFIG_SIZE, desc);
15648 + if (len < 0)
15649 + return len;
15650 + len += USB_DT_CONFIG_SIZE;
15651 + if (len > 0xffff)
15652 + return -EINVAL;
15653 +
15654 + /* patch up the config descriptor */
15655 + cp->bLength = USB_DT_CONFIG_SIZE;
15656 + cp->bDescriptorType = USB_DT_CONFIG;
15657 + cp->wTotalLength = cpu_to_le16(len);
15658 + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
15659 + return len;
15660 +}
15661 +
15662 +/*-------------------------------------------------------------------------*/
15663 +/*-------------------------------------------------------------------------*/
15664 +
15665 +
15666 +#define RBUF_LEN (1024*1024)
15667 +static int rbuf_start;
15668 +static int rbuf_len;
15669 +static __u8 rbuf[RBUF_LEN];
15670 +
15671 +/*-------------------------------------------------------------------------*/
15672 +
15673 +#define DRIVER_VERSION "St Patrick's Day 2004"
15674 +
15675 +static const char shortname [] = "zero";
15676 +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
15677 +
15678 +static const char source_sink [] = "source and sink data";
15679 +static const char loopback [] = "loop input to output";
15680 +
15681 +/*-------------------------------------------------------------------------*/
15682 +
15683 +/*
15684 + * driver assumes self-powered hardware, and
15685 + * has no way for users to trigger remote wakeup.
15686 + *
15687 + * this version autoconfigures as much as possible,
15688 + * which is reasonable for most "bulk-only" drivers.
15689 + */
15690 +static const char *EP_IN_NAME; /* source */
15691 +static const char *EP_OUT_NAME; /* sink */
15692 +
15693 +/*-------------------------------------------------------------------------*/
15694 +
15695 +/* big enough to hold our biggest descriptor */
15696 +#define USB_BUFSIZ 512
15697 +
15698 +struct zero_dev {
15699 + spinlock_t lock;
15700 + struct usb_gadget *gadget;
15701 + struct usb_request *req; /* for control responses */
15702 +
15703 + /* when configured, we have one of two configs:
15704 + * - source data (in to host) and sink it (out from host)
15705 + * - or loop it back (out from host back in to host)
15706 + */
15707 + u8 config;
15708 + struct usb_ep *in_ep, *out_ep;
15709 +
15710 + /* autoresume timer */
15711 + struct timer_list resume;
15712 +};
15713 +
15714 +#define xprintk(d,level,fmt,args...) \
15715 + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
15716 +
15717 +#ifdef DEBUG
15718 +#define DBG(dev,fmt,args...) \
15719 + xprintk(dev , KERN_DEBUG , fmt , ## args)
15720 +#else
15721 +#define DBG(dev,fmt,args...) \
15722 + do { } while (0)
15723 +#endif /* DEBUG */
15724 +
15725 +#ifdef VERBOSE
15726 +#define VDBG DBG
15727 +#else
15728 +#define VDBG(dev,fmt,args...) \
15729 + do { } while (0)
15730 +#endif /* VERBOSE */
15731 +
15732 +#define ERROR(dev,fmt,args...) \
15733 + xprintk(dev , KERN_ERR , fmt , ## args)
15734 +#define WARN(dev,fmt,args...) \
15735 + xprintk(dev , KERN_WARNING , fmt , ## args)
15736 +#define INFO(dev,fmt,args...) \
15737 + xprintk(dev , KERN_INFO , fmt , ## args)
15738 +
15739 +/*-------------------------------------------------------------------------*/
15740 +
15741 +static unsigned buflen = 4096;
15742 +static unsigned qlen = 32;
15743 +static unsigned pattern = 0;
15744 +
15745 +module_param (buflen, uint, S_IRUGO|S_IWUSR);
15746 +module_param (qlen, uint, S_IRUGO|S_IWUSR);
15747 +module_param (pattern, uint, S_IRUGO|S_IWUSR);
15748 +
15749 +/*
15750 + * if it's nonzero, autoresume says how many seconds to wait
15751 + * before trying to wake up the host after suspend.
15752 + */
15753 +static unsigned autoresume = 0;
15754 +module_param (autoresume, uint, 0);
15755 +
15756 +/*
15757 + * Normally the "loopback" configuration is second (index 1) so
15758 + * it's not the default. Here's where to change that order, to
15759 + * work better with hosts where config changes are problematic.
15760 + * Or controllers (like superh) that only support one config.
15761 + */
15762 +static int loopdefault = 0;
15763 +
15764 +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
15765 +
15766 +/*-------------------------------------------------------------------------*/
15767 +
15768 +/* Thanks to NetChip Technologies for donating this product ID.
15769 + *
15770 + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
15771 + * Instead: allocate your own, using normal USB-IF procedures.
15772 + */
15773 +#ifndef CONFIG_USB_ZERO_HNPTEST
15774 +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
15775 +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
15776 +#else
15777 +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
15778 +#define DRIVER_PRODUCT_NUM 0xbadd
15779 +#endif
15780 +
15781 +/*-------------------------------------------------------------------------*/
15782 +
15783 +/*
15784 + * DESCRIPTORS ... most are static, but strings and (full)
15785 + * configuration descriptors are built on demand.
15786 + */
15787 +
15788 +/*
15789 +#define STRING_MANUFACTURER 25
15790 +#define STRING_PRODUCT 42
15791 +#define STRING_SERIAL 101
15792 +*/
15793 +#define STRING_MANUFACTURER 1
15794 +#define STRING_PRODUCT 2
15795 +#define STRING_SERIAL 3
15796 +
15797 +#define STRING_SOURCE_SINK 250
15798 +#define STRING_LOOPBACK 251
15799 +
15800 +/*
15801 + * This device advertises two configurations; these numbers work
15802 + * on a pxa250 as well as more flexible hardware.
15803 + */
15804 +#define CONFIG_SOURCE_SINK 3
15805 +#define CONFIG_LOOPBACK 2
15806 +
15807 +/*
15808 +static struct usb_device_descriptor
15809 +device_desc = {
15810 + .bLength = sizeof device_desc,
15811 + .bDescriptorType = USB_DT_DEVICE,
15812 +
15813 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
15814 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
15815 +
15816 + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
15817 + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
15818 + .iManufacturer = STRING_MANUFACTURER,
15819 + .iProduct = STRING_PRODUCT,
15820 + .iSerialNumber = STRING_SERIAL,
15821 + .bNumConfigurations = 2,
15822 +};
15823 +*/
15824 +static struct usb_device_descriptor
15825 +device_desc = {
15826 + .bLength = sizeof device_desc,
15827 + .bDescriptorType = USB_DT_DEVICE,
15828 + .bcdUSB = __constant_cpu_to_le16 (0x0100),
15829 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
15830 + .bDeviceSubClass = 0,
15831 + .bDeviceProtocol = 0,
15832 + .bMaxPacketSize0 = 64,
15833 + .bcdDevice = __constant_cpu_to_le16 (0x0100),
15834 + .idVendor = __constant_cpu_to_le16 (0x0499),
15835 + .idProduct = __constant_cpu_to_le16 (0x3002),
15836 + .iManufacturer = STRING_MANUFACTURER,
15837 + .iProduct = STRING_PRODUCT,
15838 + .iSerialNumber = STRING_SERIAL,
15839 + .bNumConfigurations = 1,
15840 +};
15841 +
15842 +static struct usb_config_descriptor
15843 +z_config = {
15844 + .bLength = sizeof z_config,
15845 + .bDescriptorType = USB_DT_CONFIG,
15846 +
15847 + /* compute wTotalLength on the fly */
15848 + .bNumInterfaces = 2,
15849 + .bConfigurationValue = 1,
15850 + .iConfiguration = 0,
15851 + .bmAttributes = 0x40,
15852 + .bMaxPower = 0, /* self-powered */
15853 +};
15854 +
15855 +
15856 +static struct usb_otg_descriptor
15857 +otg_descriptor = {
15858 + .bLength = sizeof otg_descriptor,
15859 + .bDescriptorType = USB_DT_OTG,
15860 +
15861 + .bmAttributes = USB_OTG_SRP,
15862 +};
15863 +
15864 +/* one interface in each configuration */
15865 +#ifdef CONFIG_USB_GADGET_DUALSPEED
15866 +
15867 +/*
15868 + * usb 2.0 devices need to expose both high speed and full speed
15869 + * descriptors, unless they only run at full speed.
15870 + *
15871 + * that means alternate endpoint descriptors (bigger packets)
15872 + * and a "device qualifier" ... plus more construction options
15873 + * for the config descriptor.
15874 + */
15875 +
15876 +static struct usb_qualifier_descriptor
15877 +dev_qualifier = {
15878 + .bLength = sizeof dev_qualifier,
15879 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
15880 +
15881 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
15882 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
15883 +
15884 + .bNumConfigurations = 2,
15885 +};
15886 +
15887 +
15888 +struct usb_cs_as_general_descriptor {
15889 + __u8 bLength;
15890 + __u8 bDescriptorType;
15891 +
15892 + __u8 bDescriptorSubType;
15893 + __u8 bTerminalLink;
15894 + __u8 bDelay;
15895 + __u16 wFormatTag;
15896 +} __attribute__ ((packed));
15897 +
15898 +struct usb_cs_as_format_descriptor {
15899 + __u8 bLength;
15900 + __u8 bDescriptorType;
15901 +
15902 + __u8 bDescriptorSubType;
15903 + __u8 bFormatType;
15904 + __u8 bNrChannels;
15905 + __u8 bSubframeSize;
15906 + __u8 bBitResolution;
15907 + __u8 bSamfreqType;
15908 + __u8 tLowerSamFreq[3];
15909 + __u8 tUpperSamFreq[3];
15910 +} __attribute__ ((packed));
15911 +
15912 +static const struct usb_interface_descriptor
15913 +z_audio_control_if_desc = {
15914 + .bLength = sizeof z_audio_control_if_desc,
15915 + .bDescriptorType = USB_DT_INTERFACE,
15916 + .bInterfaceNumber = 0,
15917 + .bAlternateSetting = 0,
15918 + .bNumEndpoints = 0,
15919 + .bInterfaceClass = USB_CLASS_AUDIO,
15920 + .bInterfaceSubClass = 0x1,
15921 + .bInterfaceProtocol = 0,
15922 + .iInterface = 0,
15923 +};
15924 +
15925 +static const struct usb_interface_descriptor
15926 +z_audio_if_desc = {
15927 + .bLength = sizeof z_audio_if_desc,
15928 + .bDescriptorType = USB_DT_INTERFACE,
15929 + .bInterfaceNumber = 1,
15930 + .bAlternateSetting = 0,
15931 + .bNumEndpoints = 0,
15932 + .bInterfaceClass = USB_CLASS_AUDIO,
15933 + .bInterfaceSubClass = 0x2,
15934 + .bInterfaceProtocol = 0,
15935 + .iInterface = 0,
15936 +};
15937 +
15938 +static const struct usb_interface_descriptor
15939 +z_audio_if_desc2 = {
15940 + .bLength = sizeof z_audio_if_desc,
15941 + .bDescriptorType = USB_DT_INTERFACE,
15942 + .bInterfaceNumber = 1,
15943 + .bAlternateSetting = 1,
15944 + .bNumEndpoints = 1,
15945 + .bInterfaceClass = USB_CLASS_AUDIO,
15946 + .bInterfaceSubClass = 0x2,
15947 + .bInterfaceProtocol = 0,
15948 + .iInterface = 0,
15949 +};
15950 +
15951 +static const struct usb_cs_as_general_descriptor
15952 +z_audio_cs_as_if_desc = {
15953 + .bLength = 7,
15954 + .bDescriptorType = 0x24,
15955 +
15956 + .bDescriptorSubType = 0x01,
15957 + .bTerminalLink = 0x01,
15958 + .bDelay = 0x0,
15959 + .wFormatTag = __constant_cpu_to_le16 (0x0001)
15960 +};
15961 +
15962 +
15963 +static const struct usb_cs_as_format_descriptor
15964 +z_audio_cs_as_format_desc = {
15965 + .bLength = 0xe,
15966 + .bDescriptorType = 0x24,
15967 +
15968 + .bDescriptorSubType = 2,
15969 + .bFormatType = 1,
15970 + .bNrChannels = 1,
15971 + .bSubframeSize = 1,
15972 + .bBitResolution = 8,
15973 + .bSamfreqType = 0,
15974 + .tLowerSamFreq = {0x7e, 0x13, 0x00},
15975 + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
15976 +};
15977 +
15978 +static const struct usb_endpoint_descriptor
15979 +z_iso_ep = {
15980 + .bLength = 0x09,
15981 + .bDescriptorType = 0x05,
15982 + .bEndpointAddress = 0x04,
15983 + .bmAttributes = 0x09,
15984 + .wMaxPacketSize = 0x0038,
15985 + .bInterval = 0x01,
15986 + .bRefresh = 0x00,
15987 + .bSynchAddress = 0x00,
15988 +};
15989 +
15990 +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
15991 +
15992 +// 9 bytes
15993 +static char z_ac_interface_header_desc[] =
15994 +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
15995 +
15996 +// 12 bytes
15997 +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
15998 + 0x03, 0x00, 0x00, 0x00};
15999 +// 13 bytes
16000 +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
16001 + 0x02, 0x00, 0x02, 0x00, 0x00};
16002 +// 9 bytes
16003 +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
16004 + 0x00};
16005 +
16006 +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
16007 + 0x00};
16008 +
16009 +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16010 +
16011 +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
16012 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16013 +
16014 +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
16015 + 0x00};
16016 +
16017 +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16018 +
16019 +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
16020 + 0x00};
16021 +
16022 +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16023 +
16024 +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
16025 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16026 +
16027 +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
16028 + 0x00};
16029 +
16030 +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16031 +
16032 +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
16033 + 0x00};
16034 +
16035 +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16036 +
16037 +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
16038 + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16039 +
16040 +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
16041 + 0x00};
16042 +
16043 +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16044 +
16045 +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
16046 + 0x00};
16047 +
16048 +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16049 +
16050 +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
16051 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16052 +
16053 +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
16054 + 0x00};
16055 +
16056 +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16057 +
16058 +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
16059 + 0x00};
16060 +
16061 +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16062 +
16063 +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
16064 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16065 +
16066 +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
16067 + 0x00};
16068 +
16069 +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16070 +
16071 +
16072 +
16073 +static const struct usb_descriptor_header *z_function [] = {
16074 + (struct usb_descriptor_header *) &z_audio_control_if_desc,
16075 + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
16076 + (struct usb_descriptor_header *) &z_0,
16077 + (struct usb_descriptor_header *) &z_1,
16078 + (struct usb_descriptor_header *) &z_2,
16079 + (struct usb_descriptor_header *) &z_audio_if_desc,
16080 + (struct usb_descriptor_header *) &z_audio_if_desc2,
16081 + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
16082 + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
16083 + (struct usb_descriptor_header *) &z_iso_ep,
16084 + (struct usb_descriptor_header *) &z_iso_ep2,
16085 + (struct usb_descriptor_header *) &za_0,
16086 + (struct usb_descriptor_header *) &za_1,
16087 + (struct usb_descriptor_header *) &za_2,
16088 + (struct usb_descriptor_header *) &za_3,
16089 + (struct usb_descriptor_header *) &za_4,
16090 + (struct usb_descriptor_header *) &za_5,
16091 + (struct usb_descriptor_header *) &za_6,
16092 + (struct usb_descriptor_header *) &za_7,
16093 + (struct usb_descriptor_header *) &za_8,
16094 + (struct usb_descriptor_header *) &za_9,
16095 + (struct usb_descriptor_header *) &za_10,
16096 + (struct usb_descriptor_header *) &za_11,
16097 + (struct usb_descriptor_header *) &za_12,
16098 + (struct usb_descriptor_header *) &za_13,
16099 + (struct usb_descriptor_header *) &za_14,
16100 + (struct usb_descriptor_header *) &za_15,
16101 + (struct usb_descriptor_header *) &za_16,
16102 + (struct usb_descriptor_header *) &za_17,
16103 + (struct usb_descriptor_header *) &za_18,
16104 + (struct usb_descriptor_header *) &za_19,
16105 + (struct usb_descriptor_header *) &za_20,
16106 + (struct usb_descriptor_header *) &za_21,
16107 + (struct usb_descriptor_header *) &za_22,
16108 + (struct usb_descriptor_header *) &za_23,
16109 + (struct usb_descriptor_header *) &za_24,
16110 + NULL,
16111 +};
16112 +
16113 +/* maxpacket and other transfer characteristics vary by speed. */
16114 +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
16115 +
16116 +#else
16117 +
16118 +/* if there's no high speed support, maxpacket doesn't change. */
16119 +#define ep_desc(g,hs,fs) fs
16120 +
16121 +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
16122 +
16123 +static char manufacturer [40];
16124 +//static char serial [40];
16125 +static char serial [] = "Ser 00 em";
16126 +
16127 +/* static strings, in UTF-8 */
16128 +static struct usb_string strings [] = {
16129 + { STRING_MANUFACTURER, manufacturer, },
16130 + { STRING_PRODUCT, longname, },
16131 + { STRING_SERIAL, serial, },
16132 + { STRING_LOOPBACK, loopback, },
16133 + { STRING_SOURCE_SINK, source_sink, },
16134 + { } /* end of list */
16135 +};
16136 +
16137 +static struct usb_gadget_strings stringtab = {
16138 + .language = 0x0409, /* en-us */
16139 + .strings = strings,
16140 +};
16141 +
16142 +/*
16143 + * config descriptors are also handcrafted. these must agree with code
16144 + * that sets configurations, and with code managing interfaces and their
16145 + * altsettings. other complexity may come from:
16146 + *
16147 + * - high speed support, including "other speed config" rules
16148 + * - multiple configurations
16149 + * - interfaces with alternate settings
16150 + * - embedded class or vendor-specific descriptors
16151 + *
16152 + * this handles high speed, and has a second config that could as easily
16153 + * have been an alternate interface setting (on most hardware).
16154 + *
16155 + * NOTE: to demonstrate (and test) more USB capabilities, this driver
16156 + * should include an altsetting to test interrupt transfers, including
16157 + * high bandwidth modes at high speed. (Maybe work like Intel's test
16158 + * device?)
16159 + */
16160 +static int
16161 +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
16162 +{
16163 + int len;
16164 + const struct usb_descriptor_header **function;
16165 +
16166 + function = z_function;
16167 + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
16168 + if (len < 0)
16169 + return len;
16170 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
16171 + return len;
16172 +}
16173 +
16174 +/*-------------------------------------------------------------------------*/
16175 +
16176 +static struct usb_request *
16177 +alloc_ep_req (struct usb_ep *ep, unsigned length)
16178 +{
16179 + struct usb_request *req;
16180 +
16181 + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
16182 + if (req) {
16183 + req->length = length;
16184 + req->buf = usb_ep_alloc_buffer (ep, length,
16185 + &req->dma, GFP_ATOMIC);
16186 + if (!req->buf) {
16187 + usb_ep_free_request (ep, req);
16188 + req = NULL;
16189 + }
16190 + }
16191 + return req;
16192 +}
16193 +
16194 +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
16195 +{
16196 + if (req->buf)
16197 + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
16198 + usb_ep_free_request (ep, req);
16199 +}
16200 +
16201 +/*-------------------------------------------------------------------------*/
16202 +
16203 +/* optionally require specific source/sink data patterns */
16204 +
16205 +static int
16206 +check_read_data (
16207 + struct zero_dev *dev,
16208 + struct usb_ep *ep,
16209 + struct usb_request *req
16210 +)
16211 +{
16212 + unsigned i;
16213 + u8 *buf = req->buf;
16214 +
16215 + for (i = 0; i < req->actual; i++, buf++) {
16216 + switch (pattern) {
16217 + /* all-zeroes has no synchronization issues */
16218 + case 0:
16219 + if (*buf == 0)
16220 + continue;
16221 + break;
16222 + /* mod63 stays in sync with short-terminated transfers,
16223 + * or otherwise when host and gadget agree on how large
16224 + * each usb transfer request should be. resync is done
16225 + * with set_interface or set_config.
16226 + */
16227 + case 1:
16228 + if (*buf == (u8)(i % 63))
16229 + continue;
16230 + break;
16231 + }
16232 + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
16233 + usb_ep_set_halt (ep);
16234 + return -EINVAL;
16235 + }
16236 + return 0;
16237 +}
16238 +
16239 +/*-------------------------------------------------------------------------*/
16240 +
16241 +static void zero_reset_config (struct zero_dev *dev)
16242 +{
16243 + if (dev->config == 0)
16244 + return;
16245 +
16246 + DBG (dev, "reset config\n");
16247 +
16248 + /* just disable endpoints, forcing completion of pending i/o.
16249 + * all our completion handlers free their requests in this case.
16250 + */
16251 + if (dev->in_ep) {
16252 + usb_ep_disable (dev->in_ep);
16253 + dev->in_ep = NULL;
16254 + }
16255 + if (dev->out_ep) {
16256 + usb_ep_disable (dev->out_ep);
16257 + dev->out_ep = NULL;
16258 + }
16259 + dev->config = 0;
16260 + del_timer (&dev->resume);
16261 +}
16262 +
16263 +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
16264 +
16265 +static void
16266 +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
16267 +{
16268 + struct zero_dev *dev = ep->driver_data;
16269 + int status = req->status;
16270 + int i, j;
16271 +
16272 + switch (status) {
16273 +
16274 + case 0: /* normal completion? */
16275 + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
16276 + for (i=0, j=rbuf_start; i<req->actual; i++) {
16277 + //printk ("%02x ", ((__u8*)req->buf)[i]);
16278 + rbuf[j] = ((__u8*)req->buf)[i];
16279 + j++;
16280 + if (j >= RBUF_LEN) j=0;
16281 + }
16282 + rbuf_start = j;
16283 + //printk ("\n\n");
16284 +
16285 + if (rbuf_len < RBUF_LEN) {
16286 + rbuf_len += req->actual;
16287 + if (rbuf_len > RBUF_LEN) {
16288 + rbuf_len = RBUF_LEN;
16289 + }
16290 + }
16291 +
16292 + break;
16293 +
16294 + /* this endpoint is normally active while we're configured */
16295 + case -ECONNABORTED: /* hardware forced ep reset */
16296 + case -ECONNRESET: /* request dequeued */
16297 + case -ESHUTDOWN: /* disconnect from host */
16298 + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
16299 + req->actual, req->length);
16300 + if (ep == dev->out_ep)
16301 + check_read_data (dev, ep, req);
16302 + free_ep_req (ep, req);
16303 + return;
16304 +
16305 + case -EOVERFLOW: /* buffer overrun on read means that
16306 + * we didn't provide a big enough
16307 + * buffer.
16308 + */
16309 + default:
16310 +#if 1
16311 + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
16312 + status, req->actual, req->length);
16313 +#endif
16314 + case -EREMOTEIO: /* short read */
16315 + break;
16316 + }
16317 +
16318 + status = usb_ep_queue (ep, req, GFP_ATOMIC);
16319 + if (status) {
16320 + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
16321 + ep->name, req->length, status);
16322 + usb_ep_set_halt (ep);
16323 + /* FIXME recover later ... somehow */
16324 + }
16325 +}
16326 +
16327 +static struct usb_request *
16328 +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
16329 +{
16330 + struct usb_request *req;
16331 + int status;
16332 +
16333 + req = alloc_ep_req (ep, 512);
16334 + if (!req)
16335 + return NULL;
16336 +
16337 + req->complete = zero_isoc_complete;
16338 +
16339 + status = usb_ep_queue (ep, req, gfp_flags);
16340 + if (status) {
16341 + struct zero_dev *dev = ep->driver_data;
16342 +
16343 + ERROR (dev, "start %s --> %d\n", ep->name, status);
16344 + free_ep_req (ep, req);
16345 + req = NULL;
16346 + }
16347 +
16348 + return req;
16349 +}
16350 +
16351 +/* change our operational config. this code must agree with the code
16352 + * that returns config descriptors, and altsetting code.
16353 + *
16354 + * it's also responsible for power management interactions. some
16355 + * configurations might not work with our current power sources.
16356 + *
16357 + * note that some device controller hardware will constrain what this
16358 + * code can do, perhaps by disallowing more than one configuration or
16359 + * by limiting configuration choices (like the pxa2xx).
16360 + */
16361 +static int
16362 +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
16363 +{
16364 + int result = 0;
16365 + struct usb_gadget *gadget = dev->gadget;
16366 + const struct usb_endpoint_descriptor *d;
16367 + struct usb_ep *ep;
16368 +
16369 + if (number == dev->config)
16370 + return 0;
16371 +
16372 + zero_reset_config (dev);
16373 +
16374 + gadget_for_each_ep (ep, gadget) {
16375 +
16376 + if (strcmp (ep->name, "ep4") == 0) {
16377 +
16378 + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
16379 + result = usb_ep_enable (ep, d);
16380 +
16381 + if (result == 0) {
16382 + ep->driver_data = dev;
16383 + dev->in_ep = ep;
16384 +
16385 + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
16386 +
16387 + dev->in_ep = ep;
16388 + continue;
16389 + }
16390 +
16391 + usb_ep_disable (ep);
16392 + result = -EIO;
16393 + }
16394 + }
16395 +
16396 + }
16397 +
16398 + dev->config = number;
16399 + return result;
16400 +}
16401 +
16402 +/*-------------------------------------------------------------------------*/
16403 +
16404 +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
16405 +{
16406 + if (req->status || req->actual != req->length)
16407 + DBG ((struct zero_dev *) ep->driver_data,
16408 + "setup complete --> %d, %d/%d\n",
16409 + req->status, req->actual, req->length);
16410 +}
16411 +
16412 +/*
16413 + * The setup() callback implements all the ep0 functionality that's
16414 + * not handled lower down, in hardware or the hardware driver (like
16415 + * device and endpoint feature flags, and their status). It's all
16416 + * housekeeping for the gadget function we're implementing. Most of
16417 + * the work is in config-specific setup.
16418 + */
16419 +static int
16420 +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
16421 +{
16422 + struct zero_dev *dev = get_gadget_data (gadget);
16423 + struct usb_request *req = dev->req;
16424 + int value = -EOPNOTSUPP;
16425 +
16426 + /* usually this stores reply data in the pre-allocated ep0 buffer,
16427 + * but config change events will reconfigure hardware.
16428 + */
16429 + req->zero = 0;
16430 + switch (ctrl->bRequest) {
16431 +
16432 + case USB_REQ_GET_DESCRIPTOR:
16433 +
16434 + switch (ctrl->wValue >> 8) {
16435 +
16436 + case USB_DT_DEVICE:
16437 + value = min (ctrl->wLength, (u16) sizeof device_desc);
16438 + memcpy (req->buf, &device_desc, value);
16439 + break;
16440 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16441 + case USB_DT_DEVICE_QUALIFIER:
16442 + if (!gadget->is_dualspeed)
16443 + break;
16444 + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
16445 + memcpy (req->buf, &dev_qualifier, value);
16446 + break;
16447 +
16448 + case USB_DT_OTHER_SPEED_CONFIG:
16449 + if (!gadget->is_dualspeed)
16450 + break;
16451 + // FALLTHROUGH
16452 +#endif /* CONFIG_USB_GADGET_DUALSPEED */
16453 + case USB_DT_CONFIG:
16454 + value = config_buf (gadget, req->buf,
16455 + ctrl->wValue >> 8,
16456 + ctrl->wValue & 0xff);
16457 + if (value >= 0)
16458 + value = min (ctrl->wLength, (u16) value);
16459 + break;
16460 +
16461 + case USB_DT_STRING:
16462 + /* wIndex == language code.
16463 + * this driver only handles one language, you can
16464 + * add string tables for other languages, using
16465 + * any UTF-8 characters
16466 + */
16467 + value = usb_gadget_get_string (&stringtab,
16468 + ctrl->wValue & 0xff, req->buf);
16469 + if (value >= 0) {
16470 + value = min (ctrl->wLength, (u16) value);
16471 + }
16472 + break;
16473 + }
16474 + break;
16475 +
16476 + /* currently two configs, two speeds */
16477 + case USB_REQ_SET_CONFIGURATION:
16478 + if (ctrl->bRequestType != 0)
16479 + goto unknown;
16480 +
16481 + spin_lock (&dev->lock);
16482 + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
16483 + spin_unlock (&dev->lock);
16484 + break;
16485 + case USB_REQ_GET_CONFIGURATION:
16486 + if (ctrl->bRequestType != USB_DIR_IN)
16487 + goto unknown;
16488 + *(u8 *)req->buf = dev->config;
16489 + value = min (ctrl->wLength, (u16) 1);
16490 + break;
16491 +
16492 + /* until we add altsetting support, or other interfaces,
16493 + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
16494 + * and already killed pending endpoint I/O.
16495 + */
16496 + case USB_REQ_SET_INTERFACE:
16497 +
16498 + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
16499 + goto unknown;
16500 + spin_lock (&dev->lock);
16501 + if (dev->config) {
16502 + u8 config = dev->config;
16503 +
16504 + /* resets interface configuration, forgets about
16505 + * previous transaction state (queued bufs, etc)
16506 + * and re-inits endpoint state (toggle etc)
16507 + * no response queued, just zero status == success.
16508 + * if we had more than one interface we couldn't
16509 + * use this "reset the config" shortcut.
16510 + */
16511 + zero_reset_config (dev);
16512 + zero_set_config (dev, config, GFP_ATOMIC);
16513 + value = 0;
16514 + }
16515 + spin_unlock (&dev->lock);
16516 + break;
16517 + case USB_REQ_GET_INTERFACE:
16518 + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
16519 + value = ctrl->wLength;
16520 + break;
16521 + }
16522 + else {
16523 + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
16524 + goto unknown;
16525 + if (!dev->config)
16526 + break;
16527 + if (ctrl->wIndex != 0) {
16528 + value = -EDOM;
16529 + break;
16530 + }
16531 + *(u8 *)req->buf = 0;
16532 + value = min (ctrl->wLength, (u16) 1);
16533 + }
16534 + break;
16535 +
16536 + /*
16537 + * These are the same vendor-specific requests supported by
16538 + * Intel's USB 2.0 compliance test devices. We exceed that
16539 + * device spec by allowing multiple-packet requests.
16540 + */
16541 + case 0x5b: /* control WRITE test -- fill the buffer */
16542 + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
16543 + goto unknown;
16544 + if (ctrl->wValue || ctrl->wIndex)
16545 + break;
16546 + /* just read that many bytes into the buffer */
16547 + if (ctrl->wLength > USB_BUFSIZ)
16548 + break;
16549 + value = ctrl->wLength;
16550 + break;
16551 + case 0x5c: /* control READ test -- return the buffer */
16552 + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
16553 + goto unknown;
16554 + if (ctrl->wValue || ctrl->wIndex)
16555 + break;
16556 + /* expect those bytes are still in the buffer; send back */
16557 + if (ctrl->wLength > USB_BUFSIZ
16558 + || ctrl->wLength != req->length)
16559 + break;
16560 + value = ctrl->wLength;
16561 + break;
16562 +
16563 + case 0x01: // SET_CUR
16564 + case 0x02:
16565 + case 0x03:
16566 + case 0x04:
16567 + case 0x05:
16568 + value = ctrl->wLength;
16569 + break;
16570 + case 0x81:
16571 + switch (ctrl->wValue) {
16572 + case 0x0201:
16573 + case 0x0202:
16574 + ((u8*)req->buf)[0] = 0x00;
16575 + ((u8*)req->buf)[1] = 0xe3;
16576 + break;
16577 + case 0x0300:
16578 + case 0x0500:
16579 + ((u8*)req->buf)[0] = 0x00;
16580 + break;
16581 + }
16582 + //((u8*)req->buf)[0] = 0x81;
16583 + //((u8*)req->buf)[1] = 0x81;
16584 + value = ctrl->wLength;
16585 + break;
16586 + case 0x82:
16587 + switch (ctrl->wValue) {
16588 + case 0x0201:
16589 + case 0x0202:
16590 + ((u8*)req->buf)[0] = 0x00;
16591 + ((u8*)req->buf)[1] = 0xc3;
16592 + break;
16593 + case 0x0300:
16594 + case 0x0500:
16595 + ((u8*)req->buf)[0] = 0x00;
16596 + break;
16597 + }
16598 + //((u8*)req->buf)[0] = 0x82;
16599 + //((u8*)req->buf)[1] = 0x82;
16600 + value = ctrl->wLength;
16601 + break;
16602 + case 0x83:
16603 + switch (ctrl->wValue) {
16604 + case 0x0201:
16605 + case 0x0202:
16606 + ((u8*)req->buf)[0] = 0x00;
16607 + ((u8*)req->buf)[1] = 0x00;
16608 + break;
16609 + case 0x0300:
16610 + ((u8*)req->buf)[0] = 0x60;
16611 + break;
16612 + case 0x0500:
16613 + ((u8*)req->buf)[0] = 0x18;
16614 + break;
16615 + }
16616 + //((u8*)req->buf)[0] = 0x83;
16617 + //((u8*)req->buf)[1] = 0x83;
16618 + value = ctrl->wLength;
16619 + break;
16620 + case 0x84:
16621 + switch (ctrl->wValue) {
16622 + case 0x0201:
16623 + case 0x0202:
16624 + ((u8*)req->buf)[0] = 0x00;
16625 + ((u8*)req->buf)[1] = 0x01;
16626 + break;
16627 + case 0x0300:
16628 + case 0x0500:
16629 + ((u8*)req->buf)[0] = 0x08;
16630 + break;
16631 + }
16632 + //((u8*)req->buf)[0] = 0x84;
16633 + //((u8*)req->buf)[1] = 0x84;
16634 + value = ctrl->wLength;
16635 + break;
16636 + case 0x85:
16637 + ((u8*)req->buf)[0] = 0x85;
16638 + ((u8*)req->buf)[1] = 0x85;
16639 + value = ctrl->wLength;
16640 + break;
16641 +
16642 +
16643 + default:
16644 +unknown:
16645 + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
16646 + ctrl->bRequestType, ctrl->bRequest,
16647 + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
16648 + }
16649 +
16650 + /* respond with data transfer before status phase? */
16651 + if (value >= 0) {
16652 + req->length = value;
16653 + req->zero = value < ctrl->wLength
16654 + && (value % gadget->ep0->maxpacket) == 0;
16655 + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
16656 + if (value < 0) {
16657 + DBG (dev, "ep_queue < 0 --> %d\n", value);
16658 + req->status = 0;
16659 + zero_setup_complete (gadget->ep0, req);
16660 + }
16661 + }
16662 +
16663 + /* device either stalls (value < 0) or reports success */
16664 + return value;
16665 +}
16666 +
16667 +static void
16668 +zero_disconnect (struct usb_gadget *gadget)
16669 +{
16670 + struct zero_dev *dev = get_gadget_data (gadget);
16671 + unsigned long flags;
16672 +
16673 + spin_lock_irqsave (&dev->lock, flags);
16674 + zero_reset_config (dev);
16675 +
16676 + /* a more significant application might have some non-usb
16677 + * activities to quiesce here, saving resources like power
16678 + * or pushing the notification up a network stack.
16679 + */
16680 + spin_unlock_irqrestore (&dev->lock, flags);
16681 +
16682 + /* next we may get setup() calls to enumerate new connections;
16683 + * or an unbind() during shutdown (including removing module).
16684 + */
16685 +}
16686 +
16687 +static void
16688 +zero_autoresume (unsigned long _dev)
16689 +{
16690 + struct zero_dev *dev = (struct zero_dev *) _dev;
16691 + int status;
16692 +
16693 + /* normally the host would be woken up for something
16694 + * more significant than just a timer firing...
16695 + */
16696 + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
16697 + status = usb_gadget_wakeup (dev->gadget);
16698 + DBG (dev, "wakeup --> %d\n", status);
16699 + }
16700 +}
16701 +
16702 +/*-------------------------------------------------------------------------*/
16703 +
16704 +static void
16705 +zero_unbind (struct usb_gadget *gadget)
16706 +{
16707 + struct zero_dev *dev = get_gadget_data (gadget);
16708 +
16709 + DBG (dev, "unbind\n");
16710 +
16711 + /* we've already been disconnected ... no i/o is active */
16712 + if (dev->req)
16713 + free_ep_req (gadget->ep0, dev->req);
16714 + del_timer_sync (&dev->resume);
16715 + kfree (dev);
16716 + set_gadget_data (gadget, NULL);
16717 +}
16718 +
16719 +static int
16720 +zero_bind (struct usb_gadget *gadget)
16721 +{
16722 + struct zero_dev *dev;
16723 + //struct usb_ep *ep;
16724 +
16725 + printk("binding\n");
16726 + /*
16727 + * DRIVER POLICY CHOICE: you may want to do this differently.
16728 + * One thing to avoid is reusing a bcdDevice revision code
16729 + * with different host-visible configurations or behavior
16730 + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
16731 + */
16732 + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
16733 +
16734 +
16735 + /* ok, we made sense of the hardware ... */
16736 + dev = kzalloc (sizeof *dev, SLAB_KERNEL);
16737 + if (!dev)
16738 + return -ENOMEM;
16739 + spin_lock_init (&dev->lock);
16740 + dev->gadget = gadget;
16741 + set_gadget_data (gadget, dev);
16742 +
16743 + /* preallocate control response and buffer */
16744 + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
16745 + if (!dev->req)
16746 + goto enomem;
16747 + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
16748 + &dev->req->dma, GFP_KERNEL);
16749 + if (!dev->req->buf)
16750 + goto enomem;
16751 +
16752 + dev->req->complete = zero_setup_complete;
16753 +
16754 + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
16755 +
16756 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16757 + /* assume ep0 uses the same value for both speeds ... */
16758 + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
16759 +
16760 + /* and that all endpoints are dual-speed */
16761 + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
16762 + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
16763 +#endif
16764 +
16765 + usb_gadget_set_selfpowered (gadget);
16766 +
16767 + init_timer (&dev->resume);
16768 + dev->resume.function = zero_autoresume;
16769 + dev->resume.data = (unsigned long) dev;
16770 +
16771 + gadget->ep0->driver_data = dev;
16772 +
16773 + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
16774 + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
16775 + EP_OUT_NAME, EP_IN_NAME);
16776 +
16777 + snprintf (manufacturer, sizeof manufacturer,
16778 + UTS_SYSNAME " " UTS_RELEASE " with %s",
16779 + gadget->name);
16780 +
16781 + return 0;
16782 +
16783 +enomem:
16784 + zero_unbind (gadget);
16785 + return -ENOMEM;
16786 +}
16787 +
16788 +/*-------------------------------------------------------------------------*/
16789 +
16790 +static void
16791 +zero_suspend (struct usb_gadget *gadget)
16792 +{
16793 + struct zero_dev *dev = get_gadget_data (gadget);
16794 +
16795 + if (gadget->speed == USB_SPEED_UNKNOWN)
16796 + return;
16797 +
16798 + if (autoresume) {
16799 + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
16800 + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
16801 + } else
16802 + DBG (dev, "suspend\n");
16803 +}
16804 +
16805 +static void
16806 +zero_resume (struct usb_gadget *gadget)
16807 +{
16808 + struct zero_dev *dev = get_gadget_data (gadget);
16809 +
16810 + DBG (dev, "resume\n");
16811 + del_timer (&dev->resume);
16812 +}
16813 +
16814 +
16815 +/*-------------------------------------------------------------------------*/
16816 +
16817 +static struct usb_gadget_driver zero_driver = {
16818 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16819 + .speed = USB_SPEED_HIGH,
16820 +#else
16821 + .speed = USB_SPEED_FULL,
16822 +#endif
16823 + .function = (char *) longname,
16824 + .bind = zero_bind,
16825 + .unbind = zero_unbind,
16826 +
16827 + .setup = zero_setup,
16828 + .disconnect = zero_disconnect,
16829 +
16830 + .suspend = zero_suspend,
16831 + .resume = zero_resume,
16832 +
16833 + .driver = {
16834 + .name = (char *) shortname,
16835 + // .shutdown = ...
16836 + // .suspend = ...
16837 + // .resume = ...
16838 + },
16839 +};
16840 +
16841 +MODULE_AUTHOR ("David Brownell");
16842 +MODULE_LICENSE ("Dual BSD/GPL");
16843 +
16844 +static struct proc_dir_entry *pdir, *pfile;
16845 +
16846 +static int isoc_read_data (char *page, char **start,
16847 + off_t off, int count,
16848 + int *eof, void *data)
16849 +{
16850 + int i;
16851 + static int c = 0;
16852 + static int done = 0;
16853 + static int s = 0;
16854 +
16855 +/*
16856 + printk ("\ncount: %d\n", count);
16857 + printk ("rbuf_start: %d\n", rbuf_start);
16858 + printk ("rbuf_len: %d\n", rbuf_len);
16859 + printk ("off: %d\n", off);
16860 + printk ("start: %p\n\n", *start);
16861 +*/
16862 + if (done) {
16863 + c = 0;
16864 + done = 0;
16865 + *eof = 1;
16866 + return 0;
16867 + }
16868 +
16869 + if (c == 0) {
16870 + if (rbuf_len == RBUF_LEN)
16871 + s = rbuf_start;
16872 + else s = 0;
16873 + }
16874 +
16875 + for (i=0; i<count && c<rbuf_len; i++, c++) {
16876 + page[i] = rbuf[(c+s) % RBUF_LEN];
16877 + }
16878 + *start = page;
16879 +
16880 + if (c >= rbuf_len) {
16881 + *eof = 1;
16882 + done = 1;
16883 + }
16884 +
16885 +
16886 + return i;
16887 +}
16888 +
16889 +static int __init init (void)
16890 +{
16891 +
16892 + int retval = 0;
16893 +
16894 + pdir = proc_mkdir("isoc_test", NULL);
16895 + if(pdir == NULL) {
16896 + retval = -ENOMEM;
16897 + printk("Error creating dir\n");
16898 + goto done;
16899 + }
16900 + pdir->owner = THIS_MODULE;
16901 +
16902 + pfile = create_proc_read_entry("isoc_data",
16903 + 0444, pdir,
16904 + isoc_read_data,
16905 + NULL);
16906 + if (pfile == NULL) {
16907 + retval = -ENOMEM;
16908 + printk("Error creating file\n");
16909 + goto no_file;
16910 + }
16911 + pfile->owner = THIS_MODULE;
16912 +
16913 + return usb_gadget_register_driver (&zero_driver);
16914 +
16915 + no_file:
16916 + remove_proc_entry("isoc_data", NULL);
16917 + done:
16918 + return retval;
16919 +}
16920 +module_init (init);
16921 +
16922 +static void __exit cleanup (void)
16923 +{
16924 +
16925 + usb_gadget_unregister_driver (&zero_driver);
16926 +
16927 + remove_proc_entry("isoc_data", pdir);
16928 + remove_proc_entry("isoc_test", NULL);
16929 +}
16930 +module_exit (cleanup);
16931 --- /dev/null
16932 +++ b/drivers/usb/host/dwc_otg/dwc_cfi_common.h
16933 @@ -0,0 +1,142 @@
16934 +/* ==========================================================================
16935 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
16936 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
16937 + * otherwise expressly agreed to in writing between Synopsys and you.
16938 + *
16939 + * The Software IS NOT an item of Licensed Software or Licensed Product under
16940 + * any End User Software License Agreement or Agreement for Licensed Product
16941 + * with Synopsys or any supplement thereto. You are permitted to use and
16942 + * redistribute this Software in source and binary forms, with or without
16943 + * modification, provided that redistributions of source code must retain this
16944 + * notice. You may not view, use, disclose, copy or distribute this file or
16945 + * any information contained herein except pursuant to this license grant from
16946 + * Synopsys. If you do not agree with this notice, including the disclaimer
16947 + * below, then you are not authorized to use the Software.
16948 + *
16949 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
16950 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16951 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16952 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
16953 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16954 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16955 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16956 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16957 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
16958 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
16959 + * DAMAGE.
16960 + * ========================================================================== */
16961 +
16962 +#if !defined(__DWC_CFI_COMMON_H__)
16963 +#define __DWC_CFI_COMMON_H__
16964 +
16965 +//#include <linux/types.h>
16966 +
16967 +/**
16968 + * @file
16969 + *
16970 + * This file contains the CFI specific common constants, interfaces
16971 + * (functions and macros) and structures for Linux. No PCD specific
16972 + * data structure or definition is to be included in this file.
16973 + *
16974 + */
16975 +
16976 +/** This is a request for all Core Features */
16977 +#define VEN_CORE_GET_FEATURES 0xB1
16978 +
16979 +/** This is a request to get the value of a specific Core Feature */
16980 +#define VEN_CORE_GET_FEATURE 0xB2
16981 +
16982 +/** This command allows the host to set the value of a specific Core Feature */
16983 +#define VEN_CORE_SET_FEATURE 0xB3
16984 +
16985 +/** This command allows the host to set the default values of
16986 + * either all or any specific Core Feature
16987 + */
16988 +#define VEN_CORE_RESET_FEATURES 0xB4
16989 +
16990 +/** This command forces the PCD to write the deferred values of a Core Features */
16991 +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
16992 +
16993 +/** This request reads a DWORD value from a register at the specified offset */
16994 +#define VEN_CORE_READ_REGISTER 0xB6
16995 +
16996 +/** This request writes a DWORD value into a register at the specified offset */
16997 +#define VEN_CORE_WRITE_REGISTER 0xB7
16998 +
16999 +/** This structure is the header of the Core Features dataset returned to
17000 + * the Host
17001 + */
17002 +struct cfi_all_features_header {
17003 +/** The features header structure length is */
17004 +#define CFI_ALL_FEATURES_HDR_LEN 8
17005 + /**
17006 + * The total length of the features dataset returned to the Host
17007 + */
17008 + uint16_t wTotalLen;
17009 +
17010 + /**
17011 + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
17012 + * This field identifies the version of the CFI Specification with which
17013 + * the device is compliant.
17014 + */
17015 + uint16_t wVersion;
17016 +
17017 + /** The ID of the Core */
17018 + uint16_t wCoreID;
17019 +#define CFI_CORE_ID_UDC 1
17020 +#define CFI_CORE_ID_OTG 2
17021 +#define CFI_CORE_ID_WUDEV 3
17022 +
17023 + /** Number of features returned by VEN_CORE_GET_FEATURES request */
17024 + uint16_t wNumFeatures;
17025 +} UPACKED;
17026 +
17027 +typedef struct cfi_all_features_header cfi_all_features_header_t;
17028 +
17029 +/** This structure is a header of the Core Feature descriptor dataset returned to
17030 + * the Host after the VEN_CORE_GET_FEATURES request
17031 + */
17032 +struct cfi_feature_desc_header {
17033 +#define CFI_FEATURE_DESC_HDR_LEN 8
17034 +
17035 + /** The feature ID */
17036 + uint16_t wFeatureID;
17037 +
17038 + /** Length of this feature descriptor in bytes - including the
17039 + * length of the feature name string
17040 + */
17041 + uint16_t wLength;
17042 +
17043 + /** The data length of this feature in bytes */
17044 + uint16_t wDataLength;
17045 +
17046 + /**
17047 + * Attributes of this features
17048 + * D0: Access rights
17049 + * 0 - Read/Write
17050 + * 1 - Read only
17051 + */
17052 + uint8_t bmAttributes;
17053 +#define CFI_FEATURE_ATTR_RO 1
17054 +#define CFI_FEATURE_ATTR_RW 0
17055 +
17056 + /** Length of the feature name in bytes */
17057 + uint8_t bNameLen;
17058 +
17059 + /** The feature name buffer */
17060 + //uint8_t *name;
17061 +} UPACKED;
17062 +
17063 +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
17064 +
17065 +/**
17066 + * This structure describes a NULL terminated string referenced by its id field.
17067 + * It is very similar to usb_string structure but has the id field type set to 16-bit.
17068 + */
17069 +struct cfi_string {
17070 + uint16_t id;
17071 + const uint8_t *s;
17072 +};
17073 +typedef struct cfi_string cfi_string_t;
17074 +
17075 +#endif
17076 --- /dev/null
17077 +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.c
17078 @@ -0,0 +1,854 @@
17079 +/* ==========================================================================
17080 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
17081 + * $Revision: #12 $
17082 + * $Date: 2011/10/26 $
17083 + * $Change: 1873028 $
17084 + *
17085 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
17086 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
17087 + * otherwise expressly agreed to in writing between Synopsys and you.
17088 + *
17089 + * The Software IS NOT an item of Licensed Software or Licensed Product under
17090 + * any End User Software License Agreement or Agreement for Licensed Product
17091 + * with Synopsys or any supplement thereto. You are permitted to use and
17092 + * redistribute this Software in source and binary forms, with or without
17093 + * modification, provided that redistributions of source code must retain this
17094 + * notice. You may not view, use, disclose, copy or distribute this file or
17095 + * any information contained herein except pursuant to this license grant from
17096 + * Synopsys. If you do not agree with this notice, including the disclaimer
17097 + * below, then you are not authorized to use the Software.
17098 + *
17099 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
17100 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17101 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17102 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
17103 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17104 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17105 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17106 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17107 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17108 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
17109 + * DAMAGE.
17110 + * ========================================================================== */
17111 +
17112 +#include "dwc_os.h"
17113 +#include "dwc_otg_regs.h"
17114 +#include "dwc_otg_cil.h"
17115 +#include "dwc_otg_adp.h"
17116 +
17117 +/** @file
17118 + *
17119 + * This file contains the most of the Attach Detect Protocol implementation for
17120 + * the driver to support OTG Rev2.0.
17121 + *
17122 + */
17123 +
17124 +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
17125 +{
17126 + adpctl_data_t adpctl;
17127 +
17128 + adpctl.d32 = value;
17129 + adpctl.b.ar = 0x2;
17130 +
17131 + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
17132 +
17133 + while (adpctl.b.ar) {
17134 + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
17135 + }
17136 +
17137 +}
17138 +
17139 +/**
17140 + * Function is called to read ADP registers
17141 + */
17142 +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
17143 +{
17144 + adpctl_data_t adpctl;
17145 +
17146 + adpctl.d32 = 0;
17147 + adpctl.b.ar = 0x1;
17148 +
17149 + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
17150 +
17151 + while (adpctl.b.ar) {
17152 + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
17153 + }
17154 +
17155 + return adpctl.d32;
17156 +}
17157 +
17158 +/**
17159 + * Function is called to read ADPCTL register and filter Write-clear bits
17160 + */
17161 +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
17162 +{
17163 + adpctl_data_t adpctl;
17164 +
17165 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17166 + adpctl.b.adp_tmout_int = 0;
17167 + adpctl.b.adp_prb_int = 0;
17168 + adpctl.b.adp_tmout_int = 0;
17169 +
17170 + return adpctl.d32;
17171 +}
17172 +
17173 +/**
17174 + * Function is called to write ADP registers
17175 + */
17176 +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
17177 + uint32_t set)
17178 +{
17179 + dwc_otg_adp_write_reg(core_if,
17180 + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
17181 +}
17182 +
17183 +static void adp_sense_timeout(void *ptr)
17184 +{
17185 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
17186 + core_if->adp.sense_timer_started = 0;
17187 + DWC_PRINTF("ADP SENSE TIMEOUT\n");
17188 + if (core_if->adp_enable) {
17189 + dwc_otg_adp_sense_stop(core_if);
17190 + dwc_otg_adp_probe_start(core_if);
17191 + }
17192 +}
17193 +
17194 +/**
17195 + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
17196 + */
17197 +static void adp_vbuson_timeout(void *ptr)
17198 +{
17199 + gpwrdn_data_t gpwrdn;
17200 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
17201 + hprt0_data_t hprt0 = {.d32 = 0 };
17202 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
17203 + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
17204 + if (core_if) {
17205 + core_if->adp.vbuson_timer_started = 0;
17206 + /* Turn off vbus */
17207 + hprt0.b.prtpwr = 1;
17208 + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
17209 + gpwrdn.d32 = 0;
17210 +
17211 + /* Power off the core */
17212 + if (core_if->power_down == 2) {
17213 + /* Enable Wakeup Logic */
17214 +// gpwrdn.b.wkupactiv = 1;
17215 + gpwrdn.b.pmuactv = 0;
17216 + gpwrdn.b.pwrdnrstn = 1;
17217 + gpwrdn.b.pwrdnclmp = 1;
17218 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17219 + gpwrdn.d32);
17220 +
17221 + /* Suspend the Phy Clock */
17222 + pcgcctl.b.stoppclk = 1;
17223 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
17224 +
17225 + /* Switch on VDD */
17226 +// gpwrdn.b.wkupactiv = 1;
17227 + gpwrdn.b.pmuactv = 1;
17228 + gpwrdn.b.pwrdnrstn = 1;
17229 + gpwrdn.b.pwrdnclmp = 1;
17230 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17231 + gpwrdn.d32);
17232 + } else {
17233 + /* Enable Power Down Logic */
17234 + gpwrdn.b.pmuintsel = 1;
17235 + gpwrdn.b.pmuactv = 1;
17236 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17237 + }
17238 +
17239 + /* Power off the core */
17240 + if (core_if->power_down == 2) {
17241 + gpwrdn.d32 = 0;
17242 + gpwrdn.b.pwrdnswtch = 1;
17243 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
17244 + gpwrdn.d32, 0);
17245 + }
17246 +
17247 + /* Unmask SRP detected interrupt from Power Down Logic */
17248 + gpwrdn.d32 = 0;
17249 + gpwrdn.b.srp_det_msk = 1;
17250 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17251 +
17252 + dwc_otg_adp_probe_start(core_if);
17253 + dwc_otg_dump_global_registers(core_if);
17254 + dwc_otg_dump_host_registers(core_if);
17255 + }
17256 +
17257 +}
17258 +
17259 +/**
17260 + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
17261 + * not asserted within 1.1 seconds.
17262 + *
17263 + * @param core_if the pointer to core_if strucure.
17264 + */
17265 +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
17266 +{
17267 + core_if->adp.vbuson_timer_started = 1;
17268 + if (core_if->adp.vbuson_timer)
17269 + {
17270 + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
17271 + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
17272 + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
17273 + } else {
17274 + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
17275 + }
17276 +}
17277 +
17278 +#if 0
17279 +/**
17280 + * Masks all DWC OTG core interrupts
17281 + *
17282 + */
17283 +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
17284 +{
17285 + int i;
17286 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
17287 +
17288 + /* Mask Host Interrupts */
17289 +
17290 + /* Clear and disable HCINTs */
17291 + for (i = 0; i < core_if->core_params->host_channels; i++) {
17292 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
17293 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
17294 +
17295 + }
17296 +
17297 + /* Clear and disable HAINT */
17298 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
17299 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
17300 +
17301 + /* Mask Device Interrupts */
17302 + if (!core_if->multiproc_int_enable) {
17303 + /* Clear and disable IN Endpoint interrupts */
17304 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
17305 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
17306 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
17307 + diepint, 0xFFFFFFFF);
17308 + }
17309 +
17310 + /* Clear and disable OUT Endpoint interrupts */
17311 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
17312 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
17313 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
17314 + doepint, 0xFFFFFFFF);
17315 + }
17316 +
17317 + /* Clear and disable DAINT */
17318 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
17319 + 0xFFFFFFFF);
17320 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
17321 + } else {
17322 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
17323 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
17324 + diepeachintmsk[i], 0);
17325 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
17326 + diepint, 0xFFFFFFFF);
17327 + }
17328 +
17329 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
17330 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
17331 + doepeachintmsk[i], 0);
17332 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
17333 + doepint, 0xFFFFFFFF);
17334 + }
17335 +
17336 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
17337 + 0);
17338 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
17339 + 0xFFFFFFFF);
17340 +
17341 + }
17342 +
17343 + /* Disable interrupts */
17344 + ahbcfg.b.glblintrmsk = 1;
17345 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
17346 +
17347 + /* Disable all interrupts. */
17348 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
17349 +
17350 + /* Clear any pending interrupts */
17351 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17352 +
17353 + /* Clear any pending OTG Interrupts */
17354 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
17355 +}
17356 +
17357 +/**
17358 + * Unmask Port Connection Detected interrupt
17359 + *
17360 + */
17361 +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
17362 +{
17363 + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
17364 +
17365 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
17366 +}
17367 +#endif
17368 +
17369 +/**
17370 + * Starts the ADP Probing
17371 + *
17372 + * @param core_if the pointer to core_if structure.
17373 + */
17374 +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
17375 +{
17376 +
17377 + adpctl_data_t adpctl = {.d32 = 0};
17378 + gpwrdn_data_t gpwrdn;
17379 +#if 0
17380 + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
17381 + .b.adp_sns_int = 1, b.adp_tmout_int};
17382 +#endif
17383 + dwc_otg_disable_global_interrupts(core_if);
17384 + DWC_PRINTF("ADP Probe Start\n");
17385 + core_if->adp.probe_enabled = 1;
17386 +
17387 + adpctl.b.adpres = 1;
17388 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17389 +
17390 + while (adpctl.b.adpres) {
17391 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17392 + }
17393 +
17394 + adpctl.d32 = 0;
17395 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17396 +
17397 + /* In Host mode unmask SRP detected interrupt */
17398 + gpwrdn.d32 = 0;
17399 + gpwrdn.b.sts_chngint_msk = 1;
17400 + if (!gpwrdn.b.idsts) {
17401 + gpwrdn.b.srp_det_msk = 1;
17402 + }
17403 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17404 +
17405 + adpctl.b.adp_tmout_int_msk = 1;
17406 + adpctl.b.adp_prb_int_msk = 1;
17407 + adpctl.b.prb_dschg = 1;
17408 + adpctl.b.prb_delta = 1;
17409 + adpctl.b.prb_per = 1;
17410 + adpctl.b.adpen = 1;
17411 + adpctl.b.enaprb = 1;
17412 +
17413 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17414 + DWC_PRINTF("ADP Probe Finish\n");
17415 + return 0;
17416 +}
17417 +
17418 +/**
17419 + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
17420 + * within 3 seconds.
17421 + *
17422 + * @param core_if the pointer to core_if strucure.
17423 + */
17424 +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
17425 +{
17426 + core_if->adp.sense_timer_started = 1;
17427 + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
17428 +}
17429 +
17430 +/**
17431 + * Starts the ADP Sense
17432 + *
17433 + * @param core_if the pointer to core_if strucure.
17434 + */
17435 +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
17436 +{
17437 + adpctl_data_t adpctl;
17438 +
17439 + DWC_PRINTF("ADP Sense Start\n");
17440 +
17441 + /* Unmask ADP sense interrupt and mask all other from the core */
17442 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17443 + adpctl.b.adp_sns_int_msk = 1;
17444 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17445 + dwc_otg_disable_global_interrupts(core_if); // vahrama
17446 +
17447 + /* Set ADP reset bit*/
17448 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17449 + adpctl.b.adpres = 1;
17450 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17451 +
17452 + while (adpctl.b.adpres) {
17453 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17454 + }
17455 +
17456 + adpctl.b.adpres = 0;
17457 + adpctl.b.adpen = 1;
17458 + adpctl.b.enasns = 1;
17459 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17460 +
17461 + dwc_otg_adp_sense_timer_start(core_if);
17462 +
17463 + return 0;
17464 +}
17465 +
17466 +/**
17467 + * Stops the ADP Probing
17468 + *
17469 + * @param core_if the pointer to core_if strucure.
17470 + */
17471 +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
17472 +{
17473 +
17474 + adpctl_data_t adpctl;
17475 + DWC_PRINTF("Stop ADP probe\n");
17476 + core_if->adp.probe_enabled = 0;
17477 + core_if->adp.probe_counter = 0;
17478 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17479 +
17480 + adpctl.b.adpen = 0;
17481 + adpctl.b.adp_prb_int = 1;
17482 + adpctl.b.adp_tmout_int = 1;
17483 + adpctl.b.adp_sns_int = 1;
17484 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17485 +
17486 + return 0;
17487 +}
17488 +
17489 +/**
17490 + * Stops the ADP Sensing
17491 + *
17492 + * @param core_if the pointer to core_if strucure.
17493 + */
17494 +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
17495 +{
17496 + adpctl_data_t adpctl;
17497 +
17498 + core_if->adp.sense_enabled = 0;
17499 +
17500 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17501 + adpctl.b.enasns = 0;
17502 + adpctl.b.adp_sns_int = 1;
17503 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17504 +
17505 + return 0;
17506 +}
17507 +
17508 +/**
17509 + * Called to turn on the VBUS after initial ADP probe in host mode.
17510 + * If port power was already enabled in cil_hcd_start function then
17511 + * only schedule a timer.
17512 + *
17513 + * @param core_if the pointer to core_if structure.
17514 + */
17515 +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
17516 +{
17517 + hprt0_data_t hprt0 = {.d32 = 0 };
17518 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
17519 + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
17520 +
17521 + if (hprt0.b.prtpwr == 0) {
17522 + hprt0.b.prtpwr = 1;
17523 + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
17524 + }
17525 +
17526 + dwc_otg_adp_vbuson_timer_start(core_if);
17527 +}
17528 +
17529 +/**
17530 + * Called right after driver is loaded
17531 + * to perform initial actions for ADP
17532 + *
17533 + * @param core_if the pointer to core_if structure.
17534 + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
17535 + */
17536 +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
17537 +{
17538 + gpwrdn_data_t gpwrdn;
17539 +
17540 + DWC_PRINTF("ADP Initial Start\n");
17541 + core_if->adp.adp_started = 1;
17542 +
17543 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17544 + dwc_otg_disable_global_interrupts(core_if);
17545 + if (is_host) {
17546 + DWC_PRINTF("HOST MODE\n");
17547 + /* Enable Power Down Logic Interrupt*/
17548 + gpwrdn.d32 = 0;
17549 + gpwrdn.b.pmuintsel = 1;
17550 + gpwrdn.b.pmuactv = 1;
17551 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17552 + /* Initialize first ADP probe to obtain Ramp Time value */
17553 + core_if->adp.initial_probe = 1;
17554 + dwc_otg_adp_probe_start(core_if);
17555 + } else {
17556 + gotgctl_data_t gotgctl;
17557 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
17558 + DWC_PRINTF("DEVICE MODE\n");
17559 + if (gotgctl.b.bsesvld == 0) {
17560 + /* Enable Power Down Logic Interrupt*/
17561 + gpwrdn.d32 = 0;
17562 + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
17563 + gpwrdn.b.pmuintsel = 1;
17564 + gpwrdn.b.pmuactv = 1;
17565 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17566 + core_if->adp.initial_probe = 1;
17567 + dwc_otg_adp_probe_start(core_if);
17568 + } else {
17569 + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
17570 + core_if->op_state = B_PERIPHERAL;
17571 + dwc_otg_core_init(core_if);
17572 + dwc_otg_enable_global_interrupts(core_if);
17573 + cil_pcd_start(core_if);
17574 + dwc_otg_dump_global_registers(core_if);
17575 + dwc_otg_dump_dev_registers(core_if);
17576 + }
17577 + }
17578 +}
17579 +
17580 +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
17581 +{
17582 + core_if->adp.adp_started = 0;
17583 + core_if->adp.initial_probe = 0;
17584 + core_if->adp.probe_timer_values[0] = -1;
17585 + core_if->adp.probe_timer_values[1] = -1;
17586 + core_if->adp.probe_enabled = 0;
17587 + core_if->adp.sense_enabled = 0;
17588 + core_if->adp.sense_timer_started = 0;
17589 + core_if->adp.vbuson_timer_started = 0;
17590 + core_if->adp.probe_counter = 0;
17591 + core_if->adp.gpwrdn = 0;
17592 + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
17593 + /* Initialize timers */
17594 + core_if->adp.sense_timer =
17595 + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
17596 + core_if->adp.vbuson_timer =
17597 + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
17598 + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
17599 + {
17600 + DWC_ERROR("Could not allocate memory for ADP timers\n");
17601 + }
17602 +}
17603 +
17604 +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
17605 +{
17606 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
17607 + gpwrdn.b.pmuintsel = 1;
17608 + gpwrdn.b.pmuactv = 1;
17609 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17610 +
17611 + if (core_if->adp.probe_enabled)
17612 + dwc_otg_adp_probe_stop(core_if);
17613 + if (core_if->adp.sense_enabled)
17614 + dwc_otg_adp_sense_stop(core_if);
17615 + if (core_if->adp.sense_timer_started)
17616 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
17617 + if (core_if->adp.vbuson_timer_started)
17618 + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
17619 + DWC_TIMER_FREE(core_if->adp.sense_timer);
17620 + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
17621 +}
17622 +
17623 +/////////////////////////////////////////////////////////////////////
17624 +////////////// ADP Interrupt Handlers ///////////////////////////////
17625 +/////////////////////////////////////////////////////////////////////
17626 +/**
17627 + * This function sets Ramp Timer values
17628 + */
17629 +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
17630 +{
17631 + if (core_if->adp.probe_timer_values[0] == -1) {
17632 + core_if->adp.probe_timer_values[0] = val;
17633 + core_if->adp.probe_timer_values[1] = -1;
17634 + return 1;
17635 + } else {
17636 + core_if->adp.probe_timer_values[1] =
17637 + core_if->adp.probe_timer_values[0];
17638 + core_if->adp.probe_timer_values[0] = val;
17639 + return 0;
17640 + }
17641 +}
17642 +
17643 +/**
17644 + * This function compares Ramp Timer values
17645 + */
17646 +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
17647 +{
17648 + uint32_t diff;
17649 + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
17650 + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
17651 + else
17652 + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
17653 + if(diff < 2) {
17654 + return 0;
17655 + } else {
17656 + return 1;
17657 + }
17658 +}
17659 +
17660 +/**
17661 + * This function handles ADP Probe Interrupts
17662 + */
17663 +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
17664 + uint32_t val)
17665 +{
17666 + adpctl_data_t adpctl = {.d32 = 0 };
17667 + gpwrdn_data_t gpwrdn, temp;
17668 + adpctl.d32 = val;
17669 +
17670 + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17671 + core_if->adp.probe_counter++;
17672 + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17673 + if (adpctl.b.rtim == 0 && !temp.b.idsts){
17674 + DWC_PRINTF("RTIM value is 0\n");
17675 + goto exit;
17676 + }
17677 + if (set_timer_value(core_if, adpctl.b.rtim) &&
17678 + core_if->adp.initial_probe) {
17679 + core_if->adp.initial_probe = 0;
17680 + dwc_otg_adp_probe_stop(core_if);
17681 + gpwrdn.d32 = 0;
17682 + gpwrdn.b.pmuactv = 1;
17683 + gpwrdn.b.pmuintsel = 1;
17684 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17685 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17686 +
17687 + /* check which value is for device mode and which for Host mode */
17688 + if (!temp.b.idsts) { /* considered host mode value is 0 */
17689 + /*
17690 + * Turn on VBUS after initial ADP probe.
17691 + */
17692 + core_if->op_state = A_HOST;
17693 + dwc_otg_enable_global_interrupts(core_if);
17694 + DWC_SPINUNLOCK(core_if->lock);
17695 + cil_hcd_start(core_if);
17696 + dwc_otg_adp_turnon_vbus(core_if);
17697 + DWC_SPINLOCK(core_if->lock);
17698 + } else {
17699 + /*
17700 + * Initiate SRP after initial ADP probe.
17701 + */
17702 + dwc_otg_enable_global_interrupts(core_if);
17703 + dwc_otg_initiate_srp(core_if);
17704 + }
17705 + } else if (core_if->adp.probe_counter > 2){
17706 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17707 + if (compare_timer_values(core_if)) {
17708 + DWC_PRINTF("Difference in timer values !!! \n");
17709 +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
17710 + dwc_otg_adp_probe_stop(core_if);
17711 +
17712 + /* Power on the core */
17713 + if (core_if->power_down == 2) {
17714 + gpwrdn.b.pwrdnswtch = 1;
17715 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17716 + gpwrdn, 0, gpwrdn.d32);
17717 + }
17718 +
17719 + /* check which value is for device mode and which for Host mode */
17720 + if (!temp.b.idsts) { /* considered host mode value is 0 */
17721 + /* Disable Interrupt from Power Down Logic */
17722 + gpwrdn.d32 = 0;
17723 + gpwrdn.b.pmuintsel = 1;
17724 + gpwrdn.b.pmuactv = 1;
17725 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17726 + gpwrdn, gpwrdn.d32, 0);
17727 +
17728 + /*
17729 + * Initialize the Core for Host mode.
17730 + */
17731 + core_if->op_state = A_HOST;
17732 + dwc_otg_core_init(core_if);
17733 + dwc_otg_enable_global_interrupts(core_if);
17734 + cil_hcd_start(core_if);
17735 + } else {
17736 + gotgctl_data_t gotgctl;
17737 + /* Mask SRP detected interrupt from Power Down Logic */
17738 + gpwrdn.d32 = 0;
17739 + gpwrdn.b.srp_det_msk = 1;
17740 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17741 + gpwrdn, gpwrdn.d32, 0);
17742 +
17743 + /* Disable Power Down Logic */
17744 + gpwrdn.d32 = 0;
17745 + gpwrdn.b.pmuintsel = 1;
17746 + gpwrdn.b.pmuactv = 1;
17747 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17748 + gpwrdn, gpwrdn.d32, 0);
17749 +
17750 + /*
17751 + * Initialize the Core for Device mode.
17752 + */
17753 + core_if->op_state = B_PERIPHERAL;
17754 + dwc_otg_core_init(core_if);
17755 + dwc_otg_enable_global_interrupts(core_if);
17756 + cil_pcd_start(core_if);
17757 +
17758 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
17759 + if (!gotgctl.b.bsesvld) {
17760 + dwc_otg_initiate_srp(core_if);
17761 + }
17762 + }
17763 + }
17764 + if (core_if->power_down == 2) {
17765 + if (gpwrdn.b.bsessvld) {
17766 + /* Mask SRP detected interrupt from Power Down Logic */
17767 + gpwrdn.d32 = 0;
17768 + gpwrdn.b.srp_det_msk = 1;
17769 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17770 +
17771 + /* Disable Power Down Logic */
17772 + gpwrdn.d32 = 0;
17773 + gpwrdn.b.pmuactv = 1;
17774 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17775 +
17776 + /*
17777 + * Initialize the Core for Device mode.
17778 + */
17779 + core_if->op_state = B_PERIPHERAL;
17780 + dwc_otg_core_init(core_if);
17781 + dwc_otg_enable_global_interrupts(core_if);
17782 + cil_pcd_start(core_if);
17783 + }
17784 + }
17785 + }
17786 +exit:
17787 + /* Clear interrupt */
17788 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17789 + adpctl.b.adp_prb_int = 1;
17790 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17791 +
17792 + return 0;
17793 +}
17794 +
17795 +/**
17796 + * This function hadles ADP Sense Interrupt
17797 + */
17798 +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
17799 +{
17800 + adpctl_data_t adpctl;
17801 + /* Stop ADP Sense timer */
17802 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
17803 +
17804 + /* Restart ADP Sense timer */
17805 + dwc_otg_adp_sense_timer_start(core_if);
17806 +
17807 + /* Clear interrupt */
17808 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17809 + adpctl.b.adp_sns_int = 1;
17810 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17811 +
17812 + return 0;
17813 +}
17814 +
17815 +/**
17816 + * This function handles ADP Probe Interrupts
17817 + */
17818 +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
17819 + uint32_t val)
17820 +{
17821 + adpctl_data_t adpctl = {.d32 = 0 };
17822 + adpctl.d32 = val;
17823 + set_timer_value(core_if, adpctl.b.rtim);
17824 +
17825 + /* Clear interrupt */
17826 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17827 + adpctl.b.adp_tmout_int = 1;
17828 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17829 +
17830 + return 0;
17831 +}
17832 +
17833 +/**
17834 + * ADP Interrupt handler.
17835 + *
17836 + */
17837 +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
17838 +{
17839 + int retval = 0;
17840 + adpctl_data_t adpctl = {.d32 = 0};
17841 +
17842 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17843 + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
17844 +
17845 + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
17846 + DWC_PRINTF("ADP Sense interrupt\n");
17847 + retval |= dwc_otg_adp_handle_sns_intr(core_if);
17848 + }
17849 + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
17850 + DWC_PRINTF("ADP timeout interrupt\n");
17851 + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
17852 + }
17853 + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
17854 + DWC_PRINTF("ADP Probe interrupt\n");
17855 + adpctl.b.adp_prb_int = 1;
17856 + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
17857 + }
17858 +
17859 +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
17860 + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
17861 + DWC_PRINTF("RETURN FROM ADP ISR\n");
17862 +
17863 + return retval;
17864 +}
17865 +
17866 +/**
17867 + *
17868 + * @param core_if Programming view of DWC_otg controller.
17869 + */
17870 +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
17871 +{
17872 +
17873 +#ifndef DWC_HOST_ONLY
17874 + hprt0_data_t hprt0;
17875 + gpwrdn_data_t gpwrdn;
17876 + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
17877 +
17878 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17879 + /* check which value is for device mode and which for Host mode */
17880 + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
17881 + DWC_PRINTF("SRP: Host mode\n");
17882 +
17883 + if (core_if->adp_enable) {
17884 + dwc_otg_adp_probe_stop(core_if);
17885 +
17886 + /* Power on the core */
17887 + if (core_if->power_down == 2) {
17888 + gpwrdn.b.pwrdnswtch = 1;
17889 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17890 + gpwrdn, 0, gpwrdn.d32);
17891 + }
17892 +
17893 + core_if->op_state = A_HOST;
17894 + dwc_otg_core_init(core_if);
17895 + dwc_otg_enable_global_interrupts(core_if);
17896 + cil_hcd_start(core_if);
17897 + }
17898 +
17899 + /* Turn on the port power bit. */
17900 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
17901 + hprt0.b.prtpwr = 1;
17902 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
17903 +
17904 + /* Start the Connection timer. So a message can be displayed
17905 + * if connect does not occur within 10 seconds. */
17906 + cil_hcd_session_start(core_if);
17907 + } else {
17908 + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
17909 + if (core_if->adp_enable) {
17910 + dwc_otg_adp_probe_stop(core_if);
17911 +
17912 + /* Power on the core */
17913 + if (core_if->power_down == 2) {
17914 + gpwrdn.b.pwrdnswtch = 1;
17915 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17916 + gpwrdn, 0, gpwrdn.d32);
17917 + }
17918 +
17919 + gpwrdn.d32 = 0;
17920 + gpwrdn.b.pmuactv = 0;
17921 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17922 + gpwrdn.d32);
17923 +
17924 + core_if->op_state = B_PERIPHERAL;
17925 + dwc_otg_core_init(core_if);
17926 + dwc_otg_enable_global_interrupts(core_if);
17927 + cil_pcd_start(core_if);
17928 + }
17929 + }
17930 +#endif
17931 + return 1;
17932 +}
17933 --- /dev/null
17934 +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.h
17935 @@ -0,0 +1,80 @@
17936 +/* ==========================================================================
17937 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
17938 + * $Revision: #7 $
17939 + * $Date: 2011/10/24 $
17940 + * $Change: 1871159 $
17941 + *
17942 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
17943 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
17944 + * otherwise expressly agreed to in writing between Synopsys and you.
17945 + *
17946 + * The Software IS NOT an item of Licensed Software or Licensed Product under
17947 + * any End User Software License Agreement or Agreement for Licensed Product
17948 + * with Synopsys or any supplement thereto. You are permitted to use and
17949 + * redistribute this Software in source and binary forms, with or without
17950 + * modification, provided that redistributions of source code must retain this
17951 + * notice. You may not view, use, disclose, copy or distribute this file or
17952 + * any information contained herein except pursuant to this license grant from
17953 + * Synopsys. If you do not agree with this notice, including the disclaimer
17954 + * below, then you are not authorized to use the Software.
17955 + *
17956 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
17957 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17958 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17959 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
17960 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17961 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17962 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17963 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17964 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17965 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
17966 + * DAMAGE.
17967 + * ========================================================================== */
17968 +
17969 +#ifndef __DWC_OTG_ADP_H__
17970 +#define __DWC_OTG_ADP_H__
17971 +
17972 +/**
17973 + * @file
17974 + *
17975 + * This file contains the Attach Detect Protocol interfaces and defines
17976 + * (functions) and structures for Linux.
17977 + *
17978 + */
17979 +
17980 +#define DWC_OTG_ADP_UNATTACHED 0
17981 +#define DWC_OTG_ADP_ATTACHED 1
17982 +#define DWC_OTG_ADP_UNKOWN 2
17983 +
17984 +typedef struct dwc_otg_adp {
17985 + uint32_t adp_started;
17986 + uint32_t initial_probe;
17987 + int32_t probe_timer_values[2];
17988 + uint32_t probe_enabled;
17989 + uint32_t sense_enabled;
17990 + dwc_timer_t *sense_timer;
17991 + uint32_t sense_timer_started;
17992 + dwc_timer_t *vbuson_timer;
17993 + uint32_t vbuson_timer_started;
17994 + uint32_t attached;
17995 + uint32_t probe_counter;
17996 + uint32_t gpwrdn;
17997 +} dwc_otg_adp_t;
17998 +
17999 +/**
18000 + * Attach Detect Protocol functions
18001 + */
18002 +
18003 +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
18004 +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
18005 +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
18006 +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
18007 +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
18008 +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
18009 +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
18010 +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
18011 +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
18012 +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
18013 +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
18014 +
18015 +#endif //__DWC_OTG_ADP_H__
18016 --- /dev/null
18017 +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.c
18018 @@ -0,0 +1,1210 @@
18019 +/* ==========================================================================
18020 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
18021 + * $Revision: #44 $
18022 + * $Date: 2010/11/29 $
18023 + * $Change: 1636033 $
18024 + *
18025 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
18026 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
18027 + * otherwise expressly agreed to in writing between Synopsys and you.
18028 + *
18029 + * The Software IS NOT an item of Licensed Software or Licensed Product under
18030 + * any End User Software License Agreement or Agreement for Licensed Product
18031 + * with Synopsys or any supplement thereto. You are permitted to use and
18032 + * redistribute this Software in source and binary forms, with or without
18033 + * modification, provided that redistributions of source code must retain this
18034 + * notice. You may not view, use, disclose, copy or distribute this file or
18035 + * any information contained herein except pursuant to this license grant from
18036 + * Synopsys. If you do not agree with this notice, including the disclaimer
18037 + * below, then you are not authorized to use the Software.
18038 + *
18039 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
18040 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18041 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18042 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
18043 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18044 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18045 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
18046 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18047 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18048 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
18049 + * DAMAGE.
18050 + * ========================================================================== */
18051 +
18052 +/** @file
18053 + *
18054 + * The diagnostic interface will provide access to the controller for
18055 + * bringing up the hardware and testing. The Linux driver attributes
18056 + * feature will be used to provide the Linux Diagnostic
18057 + * Interface. These attributes are accessed through sysfs.
18058 + */
18059 +
18060 +/** @page "Linux Module Attributes"
18061 + *
18062 + * The Linux module attributes feature is used to provide the Linux
18063 + * Diagnostic Interface. These attributes are accessed through sysfs.
18064 + * The diagnostic interface will provide access to the controller for
18065 + * bringing up the hardware and testing.
18066 +
18067 + The following table shows the attributes.
18068 + <table>
18069 + <tr>
18070 + <td><b> Name</b></td>
18071 + <td><b> Description</b></td>
18072 + <td><b> Access</b></td>
18073 + </tr>
18074 +
18075 + <tr>
18076 + <td> mode </td>
18077 + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
18078 + <td> Read</td>
18079 + </tr>
18080 +
18081 + <tr>
18082 + <td> hnpcapable </td>
18083 + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
18084 + Read returns the current value.</td>
18085 + <td> Read/Write</td>
18086 + </tr>
18087 +
18088 + <tr>
18089 + <td> srpcapable </td>
18090 + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
18091 + Read returns the current value.</td>
18092 + <td> Read/Write</td>
18093 + </tr>
18094 +
18095 + <tr>
18096 + <td> hsic_connect </td>
18097 + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
18098 + Read returns the current value.</td>
18099 + <td> Read/Write</td>
18100 + </tr>
18101 +
18102 + <tr>
18103 + <td> inv_sel_hsic </td>
18104 + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
18105 + Read returns the current value.</td>
18106 + <td> Read/Write</td>
18107 + </tr>
18108 +
18109 + <tr>
18110 + <td> hnp </td>
18111 + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
18112 + <td> Read/Write</td>
18113 + </tr>
18114 +
18115 + <tr>
18116 + <td> srp </td>
18117 + <td> Initiates the Session Request Protocol. Read returns the status.</td>
18118 + <td> Read/Write</td>
18119 + </tr>
18120 +
18121 + <tr>
18122 + <td> buspower </td>
18123 + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
18124 + <td> Read/Write</td>
18125 + </tr>
18126 +
18127 + <tr>
18128 + <td> bussuspend </td>
18129 + <td> Suspends the USB bus.</td>
18130 + <td> Read/Write</td>
18131 + </tr>
18132 +
18133 + <tr>
18134 + <td> busconnected </td>
18135 + <td> Gets the connection status of the bus</td>
18136 + <td> Read</td>
18137 + </tr>
18138 +
18139 + <tr>
18140 + <td> gotgctl </td>
18141 + <td> Gets or sets the Core Control Status Register.</td>
18142 + <td> Read/Write</td>
18143 + </tr>
18144 +
18145 + <tr>
18146 + <td> gusbcfg </td>
18147 + <td> Gets or sets the Core USB Configuration Register</td>
18148 + <td> Read/Write</td>
18149 + </tr>
18150 +
18151 + <tr>
18152 + <td> grxfsiz </td>
18153 + <td> Gets or sets the Receive FIFO Size Register</td>
18154 + <td> Read/Write</td>
18155 + </tr>
18156 +
18157 + <tr>
18158 + <td> gnptxfsiz </td>
18159 + <td> Gets or sets the non-periodic Transmit Size Register</td>
18160 + <td> Read/Write</td>
18161 + </tr>
18162 +
18163 + <tr>
18164 + <td> gpvndctl </td>
18165 + <td> Gets or sets the PHY Vendor Control Register</td>
18166 + <td> Read/Write</td>
18167 + </tr>
18168 +
18169 + <tr>
18170 + <td> ggpio </td>
18171 + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
18172 + or sets the upper 16 bits.</td>
18173 + <td> Read/Write</td>
18174 + </tr>
18175 +
18176 + <tr>
18177 + <td> guid </td>
18178 + <td> Gets or sets the value of the User ID Register</td>
18179 + <td> Read/Write</td>
18180 + </tr>
18181 +
18182 + <tr>
18183 + <td> gsnpsid </td>
18184 + <td> Gets the value of the Synopsys ID Regester</td>
18185 + <td> Read</td>
18186 + </tr>
18187 +
18188 + <tr>
18189 + <td> devspeed </td>
18190 + <td> Gets or sets the device speed setting in the DCFG register</td>
18191 + <td> Read/Write</td>
18192 + </tr>
18193 +
18194 + <tr>
18195 + <td> enumspeed </td>
18196 + <td> Gets the device enumeration Speed.</td>
18197 + <td> Read</td>
18198 + </tr>
18199 +
18200 + <tr>
18201 + <td> hptxfsiz </td>
18202 + <td> Gets the value of the Host Periodic Transmit FIFO</td>
18203 + <td> Read</td>
18204 + </tr>
18205 +
18206 + <tr>
18207 + <td> hprt0 </td>
18208 + <td> Gets or sets the value in the Host Port Control and Status Register</td>
18209 + <td> Read/Write</td>
18210 + </tr>
18211 +
18212 + <tr>
18213 + <td> regoffset </td>
18214 + <td> Sets the register offset for the next Register Access</td>
18215 + <td> Read/Write</td>
18216 + </tr>
18217 +
18218 + <tr>
18219 + <td> regvalue </td>
18220 + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
18221 + <td> Read/Write</td>
18222 + </tr>
18223 +
18224 + <tr>
18225 + <td> remote_wakeup </td>
18226 + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
18227 + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
18228 + Wakeup signalling bit in the Device Control Register is set for 1
18229 + milli-second.</td>
18230 + <td> Read/Write</td>
18231 + </tr>
18232 +
18233 + <tr>
18234 + <td> rem_wakeup_pwrdn </td>
18235 + <td> On read, shows the status core - hibernated or not. On write, initiates
18236 + a remote wakeup of the device from Hibernation. </td>
18237 + <td> Read/Write</td>
18238 + </tr>
18239 +
18240 + <tr>
18241 + <td> mode_ch_tim_en </td>
18242 + <td> This bit is used to enable or disable the host core to wait for 200 PHY
18243 + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
18244 + after Suspend or LPM. </td>
18245 + <td> Read/Write</td>
18246 + </tr>
18247 +
18248 + <tr>
18249 + <td> fr_interval </td>
18250 + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
18251 + reload HFIR register during runtime. The application can write a value to this
18252 + register only after the Port Enable bit of the Host Port Control and Status
18253 + register (HPRT.PrtEnaPort) has been set </td>
18254 + <td> Read/Write</td>
18255 + </tr>
18256 +
18257 + <tr>
18258 + <td> disconnect_us </td>
18259 + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
18260 + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
18261 + <td> Read/Write</td>
18262 + </tr>
18263 +
18264 + <tr>
18265 + <td> regdump </td>
18266 + <td> Dumps the contents of core registers.</td>
18267 + <td> Read</td>
18268 + </tr>
18269 +
18270 + <tr>
18271 + <td> spramdump </td>
18272 + <td> Dumps the contents of core registers.</td>
18273 + <td> Read</td>
18274 + </tr>
18275 +
18276 + <tr>
18277 + <td> hcddump </td>
18278 + <td> Dumps the current HCD state.</td>
18279 + <td> Read</td>
18280 + </tr>
18281 +
18282 + <tr>
18283 + <td> hcd_frrem </td>
18284 + <td> Shows the average value of the Frame Remaining
18285 + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
18286 + occurs. This can be used to determine the average interrupt latency. Also
18287 + shows the average Frame Remaining value for start_transfer and the "a" and
18288 + "b" sample points. The "a" and "b" sample points may be used during debugging
18289 + bto determine how long it takes to execute a section of the HCD code.</td>
18290 + <td> Read</td>
18291 + </tr>
18292 +
18293 + <tr>
18294 + <td> rd_reg_test </td>
18295 + <td> Displays the time required to read the GNPTXFSIZ register many times
18296 + (the output shows the number of times the register is read).
18297 + <td> Read</td>
18298 + </tr>
18299 +
18300 + <tr>
18301 + <td> wr_reg_test </td>
18302 + <td> Displays the time required to write the GNPTXFSIZ register many times
18303 + (the output shows the number of times the register is written).
18304 + <td> Read</td>
18305 + </tr>
18306 +
18307 + <tr>
18308 + <td> lpm_response </td>
18309 + <td> Gets or sets lpm_response mode. Applicable only in device mode.
18310 + <td> Write</td>
18311 + </tr>
18312 +
18313 + <tr>
18314 + <td> sleep_status </td>
18315 + <td> Shows sleep status of device.
18316 + <td> Read</td>
18317 + </tr>
18318 +
18319 + </table>
18320 +
18321 + Example usage:
18322 + To get the current mode:
18323 + cat /sys/devices/lm0/mode
18324 +
18325 + To power down the USB:
18326 + echo 0 > /sys/devices/lm0/buspower
18327 + */
18328 +
18329 +#include "dwc_otg_os_dep.h"
18330 +#include "dwc_os.h"
18331 +#include "dwc_otg_driver.h"
18332 +#include "dwc_otg_attr.h"
18333 +#include "dwc_otg_core_if.h"
18334 +#include "dwc_otg_pcd_if.h"
18335 +#include "dwc_otg_hcd_if.h"
18336 +
18337 +/*
18338 + * MACROs for defining sysfs attribute
18339 + */
18340 +#ifdef LM_INTERFACE
18341 +
18342 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18343 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18344 +{ \
18345 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18346 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18347 + uint32_t val; \
18348 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18349 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18350 +}
18351 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18352 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18353 + const char *buf, size_t count) \
18354 +{ \
18355 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18356 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18357 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18358 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18359 + return count; \
18360 +}
18361 +
18362 +#elif defined(PCI_INTERFACE)
18363 +
18364 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18365 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18366 +{ \
18367 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18368 + uint32_t val; \
18369 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18370 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18371 +}
18372 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18373 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18374 + const char *buf, size_t count) \
18375 +{ \
18376 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18377 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18378 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18379 + return count; \
18380 +}
18381 +
18382 +#elif defined(PLATFORM_INTERFACE)
18383 +
18384 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18385 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18386 +{ \
18387 + struct platform_device *platform_dev = \
18388 + container_of(_dev, struct platform_device, dev); \
18389 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18390 + uint32_t val; \
18391 + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
18392 + __func__, _dev, platform_dev, otg_dev); \
18393 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18394 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18395 +}
18396 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18397 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18398 + const char *buf, size_t count) \
18399 +{ \
18400 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18401 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18402 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18403 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18404 + return count; \
18405 +}
18406 +#endif
18407 +
18408 +/*
18409 + * MACROs for defining sysfs attribute for 32-bit registers
18410 + */
18411 +#ifdef LM_INTERFACE
18412 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18413 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18414 +{ \
18415 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18416 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18417 + uint32_t val; \
18418 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18419 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18420 +}
18421 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18422 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18423 + const char *buf, size_t count) \
18424 +{ \
18425 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18426 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18427 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18428 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18429 + return count; \
18430 +}
18431 +#elif defined(PCI_INTERFACE)
18432 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18433 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18434 +{ \
18435 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18436 + uint32_t val; \
18437 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18438 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18439 +}
18440 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18441 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18442 + const char *buf, size_t count) \
18443 +{ \
18444 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18445 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18446 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18447 + return count; \
18448 +}
18449 +
18450 +#elif defined(PLATFORM_INTERFACE)
18451 +#include "dwc_otg_dbg.h"
18452 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18453 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18454 +{ \
18455 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18456 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18457 + uint32_t val; \
18458 + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
18459 + __func__, _dev, platform_dev, otg_dev); \
18460 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18461 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18462 +}
18463 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18464 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18465 + const char *buf, size_t count) \
18466 +{ \
18467 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18468 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18469 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18470 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18471 + return count; \
18472 +}
18473 +
18474 +#endif
18475 +
18476 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
18477 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18478 +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18479 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
18480 +
18481 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
18482 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18483 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
18484 +
18485 +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
18486 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18487 +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18488 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
18489 +
18490 +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
18491 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18492 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
18493 +
18494 +/** @name Functions for Show/Store of Attributes */
18495 +/**@{*/
18496 +
18497 +/**
18498 + * Helper function returning the otg_device structure of the given device
18499 + */
18500 +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
18501 +{
18502 + dwc_otg_device_t *otg_dev;
18503 + DWC_OTG_GETDRVDEV(otg_dev, _dev);
18504 + return otg_dev;
18505 +}
18506 +
18507 +/**
18508 + * Show the register offset of the Register Access.
18509 + */
18510 +static ssize_t regoffset_show(struct device *_dev,
18511 + struct device_attribute *attr, char *buf)
18512 +{
18513 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18514 + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
18515 + otg_dev->os_dep.reg_offset);
18516 +}
18517 +
18518 +/**
18519 + * Set the register offset for the next Register Access Read/Write
18520 + */
18521 +static ssize_t regoffset_store(struct device *_dev,
18522 + struct device_attribute *attr,
18523 + const char *buf, size_t count)
18524 +{
18525 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18526 + uint32_t offset = simple_strtoul(buf, NULL, 16);
18527 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
18528 + if (offset < SZ_256K) {
18529 +#elif defined(PCI_INTERFACE)
18530 + if (offset < 0x00040000) {
18531 +#endif
18532 + otg_dev->os_dep.reg_offset = offset;
18533 + } else {
18534 + dev_err(_dev, "invalid offset\n");
18535 + }
18536 +
18537 + return count;
18538 +}
18539 +
18540 +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
18541 +
18542 +/**
18543 + * Show the value of the register at the offset in the reg_offset
18544 + * attribute.
18545 + */
18546 +static ssize_t regvalue_show(struct device *_dev,
18547 + struct device_attribute *attr, char *buf)
18548 +{
18549 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18550 + uint32_t val;
18551 + volatile uint32_t *addr;
18552 +
18553 + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
18554 + /* Calculate the address */
18555 + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
18556 + (uint8_t *) otg_dev->os_dep.base);
18557 + val = DWC_READ_REG32(addr);
18558 + return snprintf(buf,
18559 + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
18560 + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
18561 + val);
18562 + } else {
18563 + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
18564 + return sprintf(buf, "invalid offset\n");
18565 + }
18566 +}
18567 +
18568 +/**
18569 + * Store the value in the register at the offset in the reg_offset
18570 + * attribute.
18571 + *
18572 + */
18573 +static ssize_t regvalue_store(struct device *_dev,
18574 + struct device_attribute *attr,
18575 + const char *buf, size_t count)
18576 +{
18577 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18578 + volatile uint32_t *addr;
18579 + uint32_t val = simple_strtoul(buf, NULL, 16);
18580 + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
18581 + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
18582 + /* Calculate the address */
18583 + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
18584 + (uint8_t *) otg_dev->os_dep.base);
18585 + DWC_WRITE_REG32(addr, val);
18586 + } else {
18587 + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
18588 + otg_dev->os_dep.reg_offset);
18589 + }
18590 + return count;
18591 +}
18592 +
18593 +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
18594 +
18595 +/*
18596 + * Attributes
18597 + */
18598 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
18599 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
18600 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
18601 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
18602 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
18603 +
18604 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
18605 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
18606 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
18607 +
18608 +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
18609 +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
18610 + &(otg_dev->core_if->core_global_regs->gusbcfg),
18611 + "GUSBCFG");
18612 +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
18613 + &(otg_dev->core_if->core_global_regs->grxfsiz),
18614 + "GRXFSIZ");
18615 +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
18616 + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
18617 + "GNPTXFSIZ");
18618 +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
18619 + &(otg_dev->core_if->core_global_regs->gpvndctl),
18620 + "GPVNDCTL");
18621 +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
18622 + &(otg_dev->core_if->core_global_regs->ggpio),
18623 + "GGPIO");
18624 +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
18625 + "GUID");
18626 +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
18627 + &(otg_dev->core_if->core_global_regs->gsnpsid),
18628 + "GSNPSID");
18629 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
18630 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
18631 +
18632 +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
18633 + &(otg_dev->core_if->core_global_regs->hptxfsiz),
18634 + "HPTXFSIZ");
18635 +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
18636 +
18637 +/**
18638 + * @todo Add code to initiate the HNP.
18639 + */
18640 +/**
18641 + * Show the HNP status bit
18642 + */
18643 +static ssize_t hnp_show(struct device *_dev,
18644 + struct device_attribute *attr, char *buf)
18645 +{
18646 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18647 + return sprintf(buf, "HstNegScs = 0x%x\n",
18648 + dwc_otg_get_hnpstatus(otg_dev->core_if));
18649 +}
18650 +
18651 +/**
18652 + * Set the HNP Request bit
18653 + */
18654 +static ssize_t hnp_store(struct device *_dev,
18655 + struct device_attribute *attr,
18656 + const char *buf, size_t count)
18657 +{
18658 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18659 + uint32_t in = simple_strtoul(buf, NULL, 16);
18660 + dwc_otg_set_hnpreq(otg_dev->core_if, in);
18661 + return count;
18662 +}
18663 +
18664 +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
18665 +
18666 +/**
18667 + * @todo Add code to initiate the SRP.
18668 + */
18669 +/**
18670 + * Show the SRP status bit
18671 + */
18672 +static ssize_t srp_show(struct device *_dev,
18673 + struct device_attribute *attr, char *buf)
18674 +{
18675 +#ifndef DWC_HOST_ONLY
18676 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18677 + return sprintf(buf, "SesReqScs = 0x%x\n",
18678 + dwc_otg_get_srpstatus(otg_dev->core_if));
18679 +#else
18680 + return sprintf(buf, "Host Only Mode!\n");
18681 +#endif
18682 +}
18683 +
18684 +/**
18685 + * Set the SRP Request bit
18686 + */
18687 +static ssize_t srp_store(struct device *_dev,
18688 + struct device_attribute *attr,
18689 + const char *buf, size_t count)
18690 +{
18691 +#ifndef DWC_HOST_ONLY
18692 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18693 + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
18694 +#endif
18695 + return count;
18696 +}
18697 +
18698 +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
18699 +
18700 +/**
18701 + * @todo Need to do more for power on/off?
18702 + */
18703 +/**
18704 + * Show the Bus Power status
18705 + */
18706 +static ssize_t buspower_show(struct device *_dev,
18707 + struct device_attribute *attr, char *buf)
18708 +{
18709 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18710 + return sprintf(buf, "Bus Power = 0x%x\n",
18711 + dwc_otg_get_prtpower(otg_dev->core_if));
18712 +}
18713 +
18714 +/**
18715 + * Set the Bus Power status
18716 + */
18717 +static ssize_t buspower_store(struct device *_dev,
18718 + struct device_attribute *attr,
18719 + const char *buf, size_t count)
18720 +{
18721 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18722 + uint32_t on = simple_strtoul(buf, NULL, 16);
18723 + dwc_otg_set_prtpower(otg_dev->core_if, on);
18724 + return count;
18725 +}
18726 +
18727 +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
18728 +
18729 +/**
18730 + * @todo Need to do more for suspend?
18731 + */
18732 +/**
18733 + * Show the Bus Suspend status
18734 + */
18735 +static ssize_t bussuspend_show(struct device *_dev,
18736 + struct device_attribute *attr, char *buf)
18737 +{
18738 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18739 + return sprintf(buf, "Bus Suspend = 0x%x\n",
18740 + dwc_otg_get_prtsuspend(otg_dev->core_if));
18741 +}
18742 +
18743 +/**
18744 + * Set the Bus Suspend status
18745 + */
18746 +static ssize_t bussuspend_store(struct device *_dev,
18747 + struct device_attribute *attr,
18748 + const char *buf, size_t count)
18749 +{
18750 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18751 + uint32_t in = simple_strtoul(buf, NULL, 16);
18752 + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
18753 + return count;
18754 +}
18755 +
18756 +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
18757 +
18758 +/**
18759 + * Show the Mode Change Ready Timer status
18760 + */
18761 +static ssize_t mode_ch_tim_en_show(struct device *_dev,
18762 + struct device_attribute *attr, char *buf)
18763 +{
18764 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18765 + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
18766 + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
18767 +}
18768 +
18769 +/**
18770 + * Set the Mode Change Ready Timer status
18771 + */
18772 +static ssize_t mode_ch_tim_en_store(struct device *_dev,
18773 + struct device_attribute *attr,
18774 + const char *buf, size_t count)
18775 +{
18776 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18777 + uint32_t in = simple_strtoul(buf, NULL, 16);
18778 + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
18779 + return count;
18780 +}
18781 +
18782 +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
18783 +
18784 +/**
18785 + * Show the value of HFIR Frame Interval bitfield
18786 + */
18787 +static ssize_t fr_interval_show(struct device *_dev,
18788 + struct device_attribute *attr, char *buf)
18789 +{
18790 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18791 + return sprintf(buf, "Frame Interval = 0x%x\n",
18792 + dwc_otg_get_fr_interval(otg_dev->core_if));
18793 +}
18794 +
18795 +/**
18796 + * Set the HFIR Frame Interval value
18797 + */
18798 +static ssize_t fr_interval_store(struct device *_dev,
18799 + struct device_attribute *attr,
18800 + const char *buf, size_t count)
18801 +{
18802 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18803 + uint32_t in = simple_strtoul(buf, NULL, 10);
18804 + dwc_otg_set_fr_interval(otg_dev->core_if, in);
18805 + return count;
18806 +}
18807 +
18808 +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
18809 +
18810 +/**
18811 + * Show the status of Remote Wakeup.
18812 + */
18813 +static ssize_t remote_wakeup_show(struct device *_dev,
18814 + struct device_attribute *attr, char *buf)
18815 +{
18816 +#ifndef DWC_HOST_ONLY
18817 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18818 +
18819 + return sprintf(buf,
18820 + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
18821 + dwc_otg_get_remotewakesig(otg_dev->core_if),
18822 + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
18823 + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
18824 +#else
18825 + return sprintf(buf, "Host Only Mode!\n");
18826 +#endif /* DWC_HOST_ONLY */
18827 +}
18828 +
18829 +/**
18830 + * Initiate a remote wakeup of the host. The Device control register
18831 + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
18832 + * flag is set.
18833 + *
18834 + */
18835 +static ssize_t remote_wakeup_store(struct device *_dev,
18836 + struct device_attribute *attr,
18837 + const char *buf, size_t count)
18838 +{
18839 +#ifndef DWC_HOST_ONLY
18840 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18841 + uint32_t val = simple_strtoul(buf, NULL, 16);
18842 +
18843 + if (val & 1) {
18844 + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
18845 + } else {
18846 + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
18847 + }
18848 +#endif /* DWC_HOST_ONLY */
18849 + return count;
18850 +}
18851 +
18852 +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
18853 + remote_wakeup_store);
18854 +
18855 +/**
18856 + * Show the whether core is hibernated or not.
18857 + */
18858 +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
18859 + struct device_attribute *attr, char *buf)
18860 +{
18861 +#ifndef DWC_HOST_ONLY
18862 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18863 +
18864 + if (dwc_otg_get_core_state(otg_dev->core_if)) {
18865 + DWC_PRINTF("Core is in hibernation\n");
18866 + } else {
18867 + DWC_PRINTF("Core is not in hibernation\n");
18868 + }
18869 +#endif /* DWC_HOST_ONLY */
18870 + return 0;
18871 +}
18872 +
18873 +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
18874 + int rem_wakeup, int reset);
18875 +
18876 +/**
18877 + * Initiate a remote wakeup of the device to exit from hibernation.
18878 + */
18879 +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
18880 + struct device_attribute *attr,
18881 + const char *buf, size_t count)
18882 +{
18883 +#ifndef DWC_HOST_ONLY
18884 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18885 + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
18886 +#endif
18887 + return count;
18888 +}
18889 +
18890 +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
18891 + rem_wakeup_pwrdn_store);
18892 +
18893 +static ssize_t disconnect_us(struct device *_dev,
18894 + struct device_attribute *attr,
18895 + const char *buf, size_t count)
18896 +{
18897 +
18898 +#ifndef DWC_HOST_ONLY
18899 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18900 + uint32_t val = simple_strtoul(buf, NULL, 16);
18901 + DWC_PRINTF("The Passed value is %04x\n", val);
18902 +
18903 + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
18904 +
18905 +#endif /* DWC_HOST_ONLY */
18906 + return count;
18907 +}
18908 +
18909 +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
18910 +
18911 +/**
18912 + * Dump global registers and either host or device registers (depending on the
18913 + * current mode of the core).
18914 + */
18915 +static ssize_t regdump_show(struct device *_dev,
18916 + struct device_attribute *attr, char *buf)
18917 +{
18918 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18919 +
18920 + dwc_otg_dump_global_registers(otg_dev->core_if);
18921 + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
18922 + dwc_otg_dump_host_registers(otg_dev->core_if);
18923 + } else {
18924 + dwc_otg_dump_dev_registers(otg_dev->core_if);
18925 +
18926 + }
18927 + return sprintf(buf, "Register Dump\n");
18928 +}
18929 +
18930 +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
18931 +
18932 +/**
18933 + * Dump global registers and either host or device registers (depending on the
18934 + * current mode of the core).
18935 + */
18936 +static ssize_t spramdump_show(struct device *_dev,
18937 + struct device_attribute *attr, char *buf)
18938 +{
18939 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18940 +
18941 + //dwc_otg_dump_spram(otg_dev->core_if);
18942 +
18943 + return sprintf(buf, "SPRAM Dump\n");
18944 +}
18945 +
18946 +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
18947 +
18948 +/**
18949 + * Dump the current hcd state.
18950 + */
18951 +static ssize_t hcddump_show(struct device *_dev,
18952 + struct device_attribute *attr, char *buf)
18953 +{
18954 +#ifndef DWC_DEVICE_ONLY
18955 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18956 + dwc_otg_hcd_dump_state(otg_dev->hcd);
18957 +#endif /* DWC_DEVICE_ONLY */
18958 + return sprintf(buf, "HCD Dump\n");
18959 +}
18960 +
18961 +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
18962 +
18963 +/**
18964 + * Dump the average frame remaining at SOF. This can be used to
18965 + * determine average interrupt latency. Frame remaining is also shown for
18966 + * start transfer and two additional sample points.
18967 + */
18968 +static ssize_t hcd_frrem_show(struct device *_dev,
18969 + struct device_attribute *attr, char *buf)
18970 +{
18971 +#ifndef DWC_DEVICE_ONLY
18972 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18973 +
18974 + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
18975 +#endif /* DWC_DEVICE_ONLY */
18976 + return sprintf(buf, "HCD Dump Frame Remaining\n");
18977 +}
18978 +
18979 +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
18980 +
18981 +/**
18982 + * Displays the time required to read the GNPTXFSIZ register many times (the
18983 + * output shows the number of times the register is read).
18984 + */
18985 +#define RW_REG_COUNT 10000000
18986 +#define MSEC_PER_JIFFIE 1000/HZ
18987 +static ssize_t rd_reg_test_show(struct device *_dev,
18988 + struct device_attribute *attr, char *buf)
18989 +{
18990 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18991 + int i;
18992 + int time;
18993 + int start_jiffies;
18994 +
18995 + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
18996 + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
18997 + start_jiffies = jiffies;
18998 + for (i = 0; i < RW_REG_COUNT; i++) {
18999 + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
19000 + }
19001 + time = jiffies - start_jiffies;
19002 + return sprintf(buf,
19003 + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
19004 + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
19005 +}
19006 +
19007 +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
19008 +
19009 +/**
19010 + * Displays the time required to write the GNPTXFSIZ register many times (the
19011 + * output shows the number of times the register is written).
19012 + */
19013 +static ssize_t wr_reg_test_show(struct device *_dev,
19014 + struct device_attribute *attr, char *buf)
19015 +{
19016 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19017 + uint32_t reg_val;
19018 + int i;
19019 + int time;
19020 + int start_jiffies;
19021 +
19022 + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
19023 + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
19024 + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
19025 + start_jiffies = jiffies;
19026 + for (i = 0; i < RW_REG_COUNT; i++) {
19027 + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
19028 + }
19029 + time = jiffies - start_jiffies;
19030 + return sprintf(buf,
19031 + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
19032 + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
19033 +}
19034 +
19035 +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
19036 +
19037 +#ifdef CONFIG_USB_DWC_OTG_LPM
19038 +
19039 +/**
19040 +* Show the lpm_response attribute.
19041 +*/
19042 +static ssize_t lpmresp_show(struct device *_dev,
19043 + struct device_attribute *attr, char *buf)
19044 +{
19045 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19046 +
19047 + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
19048 + return sprintf(buf, "** LPM is DISABLED **\n");
19049 +
19050 + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
19051 + return sprintf(buf, "** Current mode is not device mode\n");
19052 + }
19053 + return sprintf(buf, "lpm_response = %d\n",
19054 + dwc_otg_get_lpmresponse(otg_dev->core_if));
19055 +}
19056 +
19057 +/**
19058 +* Store the lpm_response attribute.
19059 +*/
19060 +static ssize_t lpmresp_store(struct device *_dev,
19061 + struct device_attribute *attr,
19062 + const char *buf, size_t count)
19063 +{
19064 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19065 + uint32_t val = simple_strtoul(buf, NULL, 16);
19066 +
19067 + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
19068 + return 0;
19069 + }
19070 +
19071 + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
19072 + return 0;
19073 + }
19074 +
19075 + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
19076 + return count;
19077 +}
19078 +
19079 +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
19080 +
19081 +/**
19082 +* Show the sleep_status attribute.
19083 +*/
19084 +static ssize_t sleepstatus_show(struct device *_dev,
19085 + struct device_attribute *attr, char *buf)
19086 +{
19087 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19088 + return sprintf(buf, "Sleep Status = %d\n",
19089 + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
19090 +}
19091 +
19092 +/**
19093 + * Store the sleep_status attribure.
19094 + */
19095 +static ssize_t sleepstatus_store(struct device *_dev,
19096 + struct device_attribute *attr,
19097 + const char *buf, size_t count)
19098 +{
19099 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19100 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
19101 +
19102 + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
19103 + if (dwc_otg_is_host_mode(core_if)) {
19104 +
19105 + DWC_PRINTF("Host initiated resume\n");
19106 + dwc_otg_set_prtresume(otg_dev->core_if, 1);
19107 + }
19108 + }
19109 +
19110 + return count;
19111 +}
19112 +
19113 +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
19114 + sleepstatus_store);
19115 +
19116 +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
19117 +
19118 +/**@}*/
19119 +
19120 +/**
19121 + * Create the device files
19122 + */
19123 +void dwc_otg_attr_create(
19124 +#ifdef LM_INTERFACE
19125 + struct lm_device *dev
19126 +#elif defined(PCI_INTERFACE)
19127 + struct pci_dev *dev
19128 +#elif defined(PLATFORM_INTERFACE)
19129 + struct platform_device *dev
19130 +#endif
19131 + )
19132 +{
19133 + int error;
19134 +
19135 + error = device_create_file(&dev->dev, &dev_attr_regoffset);
19136 + error = device_create_file(&dev->dev, &dev_attr_regvalue);
19137 + error = device_create_file(&dev->dev, &dev_attr_mode);
19138 + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
19139 + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
19140 + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
19141 + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
19142 + error = device_create_file(&dev->dev, &dev_attr_hnp);
19143 + error = device_create_file(&dev->dev, &dev_attr_srp);
19144 + error = device_create_file(&dev->dev, &dev_attr_buspower);
19145 + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
19146 + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
19147 + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
19148 + error = device_create_file(&dev->dev, &dev_attr_busconnected);
19149 + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
19150 + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
19151 + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
19152 + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
19153 + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
19154 + error = device_create_file(&dev->dev, &dev_attr_ggpio);
19155 + error = device_create_file(&dev->dev, &dev_attr_guid);
19156 + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
19157 + error = device_create_file(&dev->dev, &dev_attr_devspeed);
19158 + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
19159 + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
19160 + error = device_create_file(&dev->dev, &dev_attr_hprt0);
19161 + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
19162 + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
19163 + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
19164 + error = device_create_file(&dev->dev, &dev_attr_regdump);
19165 + error = device_create_file(&dev->dev, &dev_attr_spramdump);
19166 + error = device_create_file(&dev->dev, &dev_attr_hcddump);
19167 + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
19168 + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
19169 + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
19170 +#ifdef CONFIG_USB_DWC_OTG_LPM
19171 + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
19172 + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
19173 +#endif
19174 +}
19175 +
19176 +/**
19177 + * Remove the device files
19178 + */
19179 +void dwc_otg_attr_remove(
19180 +#ifdef LM_INTERFACE
19181 + struct lm_device *dev
19182 +#elif defined(PCI_INTERFACE)
19183 + struct pci_dev *dev
19184 +#elif defined(PLATFORM_INTERFACE)
19185 + struct platform_device *dev
19186 +#endif
19187 + )
19188 +{
19189 + device_remove_file(&dev->dev, &dev_attr_regoffset);
19190 + device_remove_file(&dev->dev, &dev_attr_regvalue);
19191 + device_remove_file(&dev->dev, &dev_attr_mode);
19192 + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
19193 + device_remove_file(&dev->dev, &dev_attr_srpcapable);
19194 + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
19195 + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
19196 + device_remove_file(&dev->dev, &dev_attr_hnp);
19197 + device_remove_file(&dev->dev, &dev_attr_srp);
19198 + device_remove_file(&dev->dev, &dev_attr_buspower);
19199 + device_remove_file(&dev->dev, &dev_attr_bussuspend);
19200 + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
19201 + device_remove_file(&dev->dev, &dev_attr_fr_interval);
19202 + device_remove_file(&dev->dev, &dev_attr_busconnected);
19203 + device_remove_file(&dev->dev, &dev_attr_gotgctl);
19204 + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
19205 + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
19206 + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
19207 + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
19208 + device_remove_file(&dev->dev, &dev_attr_ggpio);
19209 + device_remove_file(&dev->dev, &dev_attr_guid);
19210 + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
19211 + device_remove_file(&dev->dev, &dev_attr_devspeed);
19212 + device_remove_file(&dev->dev, &dev_attr_enumspeed);
19213 + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
19214 + device_remove_file(&dev->dev, &dev_attr_hprt0);
19215 + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
19216 + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
19217 + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
19218 + device_remove_file(&dev->dev, &dev_attr_regdump);
19219 + device_remove_file(&dev->dev, &dev_attr_spramdump);
19220 + device_remove_file(&dev->dev, &dev_attr_hcddump);
19221 + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
19222 + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
19223 + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
19224 +#ifdef CONFIG_USB_DWC_OTG_LPM
19225 + device_remove_file(&dev->dev, &dev_attr_lpm_response);
19226 + device_remove_file(&dev->dev, &dev_attr_sleep_status);
19227 +#endif
19228 +}
19229 --- /dev/null
19230 +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.h
19231 @@ -0,0 +1,89 @@
19232 +/* ==========================================================================
19233 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
19234 + * $Revision: #13 $
19235 + * $Date: 2010/06/21 $
19236 + * $Change: 1532021 $
19237 + *
19238 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
19239 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
19240 + * otherwise expressly agreed to in writing between Synopsys and you.
19241 + *
19242 + * The Software IS NOT an item of Licensed Software or Licensed Product under
19243 + * any End User Software License Agreement or Agreement for Licensed Product
19244 + * with Synopsys or any supplement thereto. You are permitted to use and
19245 + * redistribute this Software in source and binary forms, with or without
19246 + * modification, provided that redistributions of source code must retain this
19247 + * notice. You may not view, use, disclose, copy or distribute this file or
19248 + * any information contained herein except pursuant to this license grant from
19249 + * Synopsys. If you do not agree with this notice, including the disclaimer
19250 + * below, then you are not authorized to use the Software.
19251 + *
19252 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
19253 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19254 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19255 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
19256 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19257 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19258 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19259 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19260 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19261 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
19262 + * DAMAGE.
19263 + * ========================================================================== */
19264 +
19265 +#if !defined(__DWC_OTG_ATTR_H__)
19266 +#define __DWC_OTG_ATTR_H__
19267 +
19268 +/** @file
19269 + * This file contains the interface to the Linux device attributes.
19270 + */
19271 +extern struct device_attribute dev_attr_regoffset;
19272 +extern struct device_attribute dev_attr_regvalue;
19273 +
19274 +extern struct device_attribute dev_attr_mode;
19275 +extern struct device_attribute dev_attr_hnpcapable;
19276 +extern struct device_attribute dev_attr_srpcapable;
19277 +extern struct device_attribute dev_attr_hnp;
19278 +extern struct device_attribute dev_attr_srp;
19279 +extern struct device_attribute dev_attr_buspower;
19280 +extern struct device_attribute dev_attr_bussuspend;
19281 +extern struct device_attribute dev_attr_mode_ch_tim_en;
19282 +extern struct device_attribute dev_attr_fr_interval;
19283 +extern struct device_attribute dev_attr_busconnected;
19284 +extern struct device_attribute dev_attr_gotgctl;
19285 +extern struct device_attribute dev_attr_gusbcfg;
19286 +extern struct device_attribute dev_attr_grxfsiz;
19287 +extern struct device_attribute dev_attr_gnptxfsiz;
19288 +extern struct device_attribute dev_attr_gpvndctl;
19289 +extern struct device_attribute dev_attr_ggpio;
19290 +extern struct device_attribute dev_attr_guid;
19291 +extern struct device_attribute dev_attr_gsnpsid;
19292 +extern struct device_attribute dev_attr_devspeed;
19293 +extern struct device_attribute dev_attr_enumspeed;
19294 +extern struct device_attribute dev_attr_hptxfsiz;
19295 +extern struct device_attribute dev_attr_hprt0;
19296 +#ifdef CONFIG_USB_DWC_OTG_LPM
19297 +extern struct device_attribute dev_attr_lpm_response;
19298 +extern struct device_attribute devi_attr_sleep_status;
19299 +#endif
19300 +
19301 +void dwc_otg_attr_create(
19302 +#ifdef LM_INTERFACE
19303 + struct lm_device *dev
19304 +#elif defined(PCI_INTERFACE)
19305 + struct pci_dev *dev
19306 +#elif defined(PLATFORM_INTERFACE)
19307 + struct platform_device *dev
19308 +#endif
19309 + );
19310 +
19311 +void dwc_otg_attr_remove(
19312 +#ifdef LM_INTERFACE
19313 + struct lm_device *dev
19314 +#elif defined(PCI_INTERFACE)
19315 + struct pci_dev *dev
19316 +#elif defined(PLATFORM_INTERFACE)
19317 + struct platform_device *dev
19318 +#endif
19319 + );
19320 +#endif
19321 --- /dev/null
19322 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
19323 @@ -0,0 +1,1876 @@
19324 +/* ==========================================================================
19325 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
19326 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
19327 + * otherwise expressly agreed to in writing between Synopsys and you.
19328 + *
19329 + * The Software IS NOT an item of Licensed Software or Licensed Product under
19330 + * any End User Software License Agreement or Agreement for Licensed Product
19331 + * with Synopsys or any supplement thereto. You are permitted to use and
19332 + * redistribute this Software in source and binary forms, with or without
19333 + * modification, provided that redistributions of source code must retain this
19334 + * notice. You may not view, use, disclose, copy or distribute this file or
19335 + * any information contained herein except pursuant to this license grant from
19336 + * Synopsys. If you do not agree with this notice, including the disclaimer
19337 + * below, then you are not authorized to use the Software.
19338 + *
19339 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
19340 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19341 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19342 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
19343 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19344 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19345 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19346 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19347 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19348 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
19349 + * DAMAGE.
19350 + * ========================================================================== */
19351 +
19352 +/** @file
19353 + *
19354 + * This file contains the most of the CFI(Core Feature Interface)
19355 + * implementation for the OTG.
19356 + */
19357 +
19358 +#ifdef DWC_UTE_CFI
19359 +
19360 +#include "dwc_otg_pcd.h"
19361 +#include "dwc_otg_cfi.h"
19362 +
19363 +/** This definition should actually migrate to the Portability Library */
19364 +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
19365 +
19366 +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
19367 +
19368 +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
19369 +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
19370 + struct dwc_otg_pcd *pcd,
19371 + struct cfi_usb_ctrlrequest *ctrl_req);
19372 +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
19373 +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19374 + struct cfi_usb_ctrlrequest *req);
19375 +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19376 + struct cfi_usb_ctrlrequest *req);
19377 +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19378 + struct cfi_usb_ctrlrequest *req);
19379 +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
19380 + struct cfi_usb_ctrlrequest *req);
19381 +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
19382 +
19383 +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
19384 +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
19385 +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
19386 +
19387 +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
19388 +
19389 +/** This is the header of the all features descriptor */
19390 +static cfi_all_features_header_t all_props_desc_header = {
19391 + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
19392 + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
19393 + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
19394 +};
19395 +
19396 +/** This is an array of statically allocated feature descriptors */
19397 +static cfi_feature_desc_header_t prop_descs[] = {
19398 +
19399 + /* FT_ID_DMA_MODE */
19400 + {
19401 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
19402 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19403 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
19404 + },
19405 +
19406 + /* FT_ID_DMA_BUFFER_SETUP */
19407 + {
19408 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
19409 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19410 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19411 + },
19412 +
19413 + /* FT_ID_DMA_BUFF_ALIGN */
19414 + {
19415 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
19416 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19417 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19418 + },
19419 +
19420 + /* FT_ID_DMA_CONCAT_SETUP */
19421 + {
19422 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
19423 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19424 + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19425 + },
19426 +
19427 + /* FT_ID_DMA_CIRCULAR */
19428 + {
19429 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
19430 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19431 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19432 + },
19433 +
19434 + /* FT_ID_THRESHOLD_SETUP */
19435 + {
19436 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
19437 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19438 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19439 + },
19440 +
19441 + /* FT_ID_DFIFO_DEPTH */
19442 + {
19443 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
19444 + .bmAttributes = CFI_FEATURE_ATTR_RO,
19445 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19446 + },
19447 +
19448 + /* FT_ID_TX_FIFO_DEPTH */
19449 + {
19450 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
19451 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19452 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19453 + },
19454 +
19455 + /* FT_ID_RX_FIFO_DEPTH */
19456 + {
19457 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
19458 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19459 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19460 + }
19461 +};
19462 +
19463 +/** The table of feature names */
19464 +cfi_string_t prop_name_table[] = {
19465 + {FT_ID_DMA_MODE, "dma_mode"},
19466 + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
19467 + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
19468 + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
19469 + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
19470 + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
19471 + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
19472 + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
19473 + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
19474 + {}
19475 +};
19476 +
19477 +/************************************************************************/
19478 +
19479 +/**
19480 + * Returns the name of the feature by its ID
19481 + * or NULL if no featute ID matches.
19482 + *
19483 + */
19484 +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
19485 +{
19486 + cfi_string_t *pstr;
19487 + *len = 0;
19488 +
19489 + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
19490 + if (pstr->id == prop_id) {
19491 + *len = DWC_STRLEN(pstr->s);
19492 + return pstr->s;
19493 + }
19494 + }
19495 + return NULL;
19496 +}
19497 +
19498 +/**
19499 + * This function handles all CFI specific control requests.
19500 + *
19501 + * Return a negative value to stall the DCE.
19502 + */
19503 +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
19504 +{
19505 + int retval = 0;
19506 + dwc_otg_pcd_ep_t *ep = NULL;
19507 + cfiobject_t *cfi = pcd->cfi;
19508 + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
19509 + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
19510 + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
19511 + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
19512 + uint32_t regaddr = 0;
19513 + uint32_t regval = 0;
19514 +
19515 + /* Save this Control Request in the CFI object.
19516 + * The data field will be assigned in the data stage completion CB function.
19517 + */
19518 + cfi->ctrl_req = *ctrl;
19519 + cfi->ctrl_req.data = NULL;
19520 +
19521 + cfi->need_gadget_att = 0;
19522 + cfi->need_status_in_complete = 0;
19523 +
19524 + switch (ctrl->bRequest) {
19525 + case VEN_CORE_GET_FEATURES:
19526 + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
19527 + if (retval >= 0) {
19528 + //dump_msg(cfi->buf_in.buf, retval);
19529 + ep = &pcd->ep0;
19530 +
19531 + retval = min((uint16_t) retval, wLen);
19532 + /* Transfer this buffer to the host through the EP0-IN EP */
19533 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19534 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19535 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19536 + ep->dwc_ep.xfer_len = retval;
19537 + ep->dwc_ep.xfer_count = 0;
19538 + ep->dwc_ep.sent_zlp = 0;
19539 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19540 +
19541 + pcd->ep0_pending = 1;
19542 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19543 + }
19544 + retval = 0;
19545 + break;
19546 +
19547 + case VEN_CORE_GET_FEATURE:
19548 + CFI_INFO("VEN_CORE_GET_FEATURE\n");
19549 + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
19550 + pcd, ctrl);
19551 + if (retval >= 0) {
19552 + ep = &pcd->ep0;
19553 +
19554 + retval = min((uint16_t) retval, wLen);
19555 + /* Transfer this buffer to the host through the EP0-IN EP */
19556 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19557 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19558 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19559 + ep->dwc_ep.xfer_len = retval;
19560 + ep->dwc_ep.xfer_count = 0;
19561 + ep->dwc_ep.sent_zlp = 0;
19562 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19563 +
19564 + pcd->ep0_pending = 1;
19565 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19566 + }
19567 + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
19568 + dump_msg(cfi->buf_in.buf, retval);
19569 + break;
19570 +
19571 + case VEN_CORE_SET_FEATURE:
19572 + CFI_INFO("VEN_CORE_SET_FEATURE\n");
19573 + /* Set up an XFER to get the data stage of the control request,
19574 + * which is the new value of the feature to be modified.
19575 + */
19576 + ep = &pcd->ep0;
19577 + ep->dwc_ep.is_in = 0;
19578 + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
19579 + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
19580 + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
19581 + ep->dwc_ep.xfer_len = wLen;
19582 + ep->dwc_ep.xfer_count = 0;
19583 + ep->dwc_ep.sent_zlp = 0;
19584 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19585 +
19586 + pcd->ep0_pending = 1;
19587 + /* Read the control write's data stage */
19588 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19589 + retval = 0;
19590 + break;
19591 +
19592 + case VEN_CORE_RESET_FEATURES:
19593 + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
19594 + cfi->need_gadget_att = 1;
19595 + cfi->need_status_in_complete = 1;
19596 + retval = cfi_preproc_reset(pcd, ctrl);
19597 + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
19598 + break;
19599 +
19600 + case VEN_CORE_ACTIVATE_FEATURES:
19601 + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
19602 + break;
19603 +
19604 + case VEN_CORE_READ_REGISTER:
19605 + CFI_INFO("VEN_CORE_READ_REGISTER\n");
19606 + /* wValue optionally contains the HI WORD of the register offset and
19607 + * wIndex contains the LOW WORD of the register offset
19608 + */
19609 + if (wValue == 0) {
19610 + /* @TODO - MAS - fix the access to the base field */
19611 + regaddr = 0;
19612 + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
19613 + //GET_CORE_IF(pcd)->co
19614 + regaddr |= wIndex;
19615 + } else {
19616 + regaddr = (wValue << 16) | wIndex;
19617 + }
19618 +
19619 + /* Read a 32-bit value of the memory at the regaddr */
19620 + regval = DWC_READ_REG32((uint32_t *) regaddr);
19621 +
19622 + ep = &pcd->ep0;
19623 + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
19624 + ep->dwc_ep.is_in = 1;
19625 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19626 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19627 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19628 + ep->dwc_ep.xfer_len = wLen;
19629 + ep->dwc_ep.xfer_count = 0;
19630 + ep->dwc_ep.sent_zlp = 0;
19631 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19632 +
19633 + pcd->ep0_pending = 1;
19634 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19635 + cfi->need_gadget_att = 0;
19636 + retval = 0;
19637 + break;
19638 +
19639 + case VEN_CORE_WRITE_REGISTER:
19640 + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
19641 + /* Set up an XFER to get the data stage of the control request,
19642 + * which is the new value of the register to be modified.
19643 + */
19644 + ep = &pcd->ep0;
19645 + ep->dwc_ep.is_in = 0;
19646 + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
19647 + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
19648 + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
19649 + ep->dwc_ep.xfer_len = wLen;
19650 + ep->dwc_ep.xfer_count = 0;
19651 + ep->dwc_ep.sent_zlp = 0;
19652 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19653 +
19654 + pcd->ep0_pending = 1;
19655 + /* Read the control write's data stage */
19656 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19657 + retval = 0;
19658 + break;
19659 +
19660 + default:
19661 + retval = -DWC_E_NOT_SUPPORTED;
19662 + break;
19663 + }
19664 +
19665 + return retval;
19666 +}
19667 +
19668 +/**
19669 + * This function prepares the core features descriptors and copies its
19670 + * raw representation into the buffer <buf>.
19671 + *
19672 + * The buffer structure is as follows:
19673 + * all_features_header (8 bytes)
19674 + * features_#1 (8 bytes + feature name string length)
19675 + * features_#2 (8 bytes + feature name string length)
19676 + * .....
19677 + * features_#n - where n=the total count of feature descriptors
19678 + */
19679 +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
19680 +{
19681 + cfi_feature_desc_header_t *prop_hdr = prop_descs;
19682 + cfi_feature_desc_header_t *prop;
19683 + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
19684 + cfi_all_features_header_t *tmp;
19685 + uint8_t *tmpbuf = buf;
19686 + const uint8_t *pname = NULL;
19687 + int i, j, namelen = 0, totlen;
19688 +
19689 + /* Prepare and copy the core features into the buffer */
19690 + CFI_INFO("%s:\n", __func__);
19691 +
19692 + tmp = (cfi_all_features_header_t *) tmpbuf;
19693 + *tmp = *all_props_hdr;
19694 + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
19695 +
19696 + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
19697 + for (i = 0; i < j; i++, prop_hdr++) {
19698 + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
19699 + prop = (cfi_feature_desc_header_t *) tmpbuf;
19700 + *prop = *prop_hdr;
19701 +
19702 + prop->bNameLen = namelen;
19703 + prop->wLength =
19704 + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
19705 + namelen);
19706 +
19707 + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
19708 + dwc_memcpy(tmpbuf, pname, namelen);
19709 + tmpbuf += namelen;
19710 + }
19711 +
19712 + totlen = tmpbuf - buf;
19713 +
19714 + if (totlen > 0) {
19715 + tmp = (cfi_all_features_header_t *) buf;
19716 + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
19717 + }
19718 +
19719 + return totlen;
19720 +}
19721 +
19722 +/**
19723 + * This function releases all the dynamic memory in the CFI object.
19724 + */
19725 +static void cfi_release(cfiobject_t * cfiobj)
19726 +{
19727 + cfi_ep_t *cfiep;
19728 + dwc_list_link_t *tmp;
19729 +
19730 + CFI_INFO("%s\n", __func__);
19731 +
19732 + if (cfiobj->buf_in.buf) {
19733 + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
19734 + cfiobj->buf_in.addr);
19735 + cfiobj->buf_in.buf = NULL;
19736 + }
19737 +
19738 + if (cfiobj->buf_out.buf) {
19739 + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
19740 + cfiobj->buf_out.addr);
19741 + cfiobj->buf_out.buf = NULL;
19742 + }
19743 +
19744 + /* Free the Buffer Setup values for each EP */
19745 + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
19746 + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
19747 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
19748 + cfi_free_ep_bs_dyn_data(cfiep);
19749 + }
19750 +}
19751 +
19752 +/**
19753 + * This function frees the dynamically allocated EP buffer setup data.
19754 + */
19755 +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
19756 +{
19757 + if (cfiep->bm_sg) {
19758 + DWC_FREE(cfiep->bm_sg);
19759 + cfiep->bm_sg = NULL;
19760 + }
19761 +
19762 + if (cfiep->bm_align) {
19763 + DWC_FREE(cfiep->bm_align);
19764 + cfiep->bm_align = NULL;
19765 + }
19766 +
19767 + if (cfiep->bm_concat) {
19768 + if (NULL != cfiep->bm_concat->wTxBytes) {
19769 + DWC_FREE(cfiep->bm_concat->wTxBytes);
19770 + cfiep->bm_concat->wTxBytes = NULL;
19771 + }
19772 + DWC_FREE(cfiep->bm_concat);
19773 + cfiep->bm_concat = NULL;
19774 + }
19775 +}
19776 +
19777 +/**
19778 + * This function initializes the default values of the features
19779 + * for a specific endpoint and should be called only once when
19780 + * the EP is enabled first time.
19781 + */
19782 +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
19783 +{
19784 + int retval = 0;
19785 +
19786 + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
19787 + if (NULL == cfiep->bm_sg) {
19788 + CFI_INFO("Failed to allocate memory for SG feature value\n");
19789 + return -DWC_E_NO_MEMORY;
19790 + }
19791 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
19792 +
19793 + /* For the Concatenation feature's default value we do not allocate
19794 + * memory for the wTxBytes field - it will be done in the set_feature_value
19795 + * request handler.
19796 + */
19797 + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
19798 + if (NULL == cfiep->bm_concat) {
19799 + CFI_INFO
19800 + ("Failed to allocate memory for CONCATENATION feature value\n");
19801 + DWC_FREE(cfiep->bm_sg);
19802 + return -DWC_E_NO_MEMORY;
19803 + }
19804 + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
19805 +
19806 + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
19807 + if (NULL == cfiep->bm_align) {
19808 + CFI_INFO
19809 + ("Failed to allocate memory for Alignment feature value\n");
19810 + DWC_FREE(cfiep->bm_sg);
19811 + DWC_FREE(cfiep->bm_concat);
19812 + return -DWC_E_NO_MEMORY;
19813 + }
19814 + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
19815 +
19816 + return retval;
19817 +}
19818 +
19819 +/**
19820 + * The callback function that notifies the CFI on the activation of
19821 + * an endpoint in the PCD. The following steps are done in this function:
19822 + *
19823 + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
19824 + * active endpoint)
19825 + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
19826 + * Set the Buffer Mode to standard
19827 + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
19828 + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
19829 + */
19830 +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
19831 + struct dwc_otg_pcd_ep *ep)
19832 +{
19833 + cfi_ep_t *cfiep;
19834 + int retval = -DWC_E_NOT_SUPPORTED;
19835 +
19836 + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
19837 + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
19838 + /* MAS - Check whether this endpoint already is in the list */
19839 + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
19840 +
19841 + if (NULL == cfiep) {
19842 + /* Allocate a cfi_ep_t object */
19843 + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
19844 + if (NULL == cfiep) {
19845 + CFI_INFO
19846 + ("Unable to allocate memory for <cfiep> in function %s\n",
19847 + __func__);
19848 + return -DWC_E_NO_MEMORY;
19849 + }
19850 + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
19851 +
19852 + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
19853 + cfiep->ep = ep;
19854 +
19855 + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
19856 + ep->dwc_ep.descs =
19857 + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
19858 + sizeof(dwc_otg_dma_desc_t),
19859 + &ep->dwc_ep.descs_dma_addr);
19860 +
19861 + if (NULL == ep->dwc_ep.descs) {
19862 + DWC_FREE(cfiep);
19863 + return -DWC_E_NO_MEMORY;
19864 + }
19865 +
19866 + DWC_LIST_INIT(&cfiep->lh);
19867 +
19868 + /* Set the buffer mode to BM_STANDARD. It will be modified
19869 + * when building descriptors for a specific buffer mode */
19870 + ep->dwc_ep.buff_mode = BM_STANDARD;
19871 +
19872 + /* Create and initialize the default values for this EP's Buffer modes */
19873 + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
19874 + return retval;
19875 +
19876 + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
19877 + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
19878 + retval = 0;
19879 + } else { /* The sought EP already is in the list */
19880 + CFI_INFO("%s: The sought EP already is in the list\n",
19881 + __func__);
19882 + }
19883 +
19884 + return retval;
19885 +}
19886 +
19887 +/**
19888 + * This function is called when the data stage of a 3-stage Control Write request
19889 + * is complete.
19890 + *
19891 + */
19892 +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
19893 + struct dwc_otg_pcd *pcd)
19894 +{
19895 + uint32_t addr, reg_value;
19896 + uint16_t wIndex, wValue;
19897 + uint8_t bRequest;
19898 + uint8_t *buf = cfi->buf_out.buf;
19899 + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
19900 + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
19901 + int retval = -DWC_E_NOT_SUPPORTED;
19902 +
19903 + CFI_INFO("%s\n", __func__);
19904 +
19905 + bRequest = ctrl_req->bRequest;
19906 + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
19907 + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
19908 +
19909 + /*
19910 + * Save the pointer to the data stage in the ctrl_req's <data> field.
19911 + * The request should be already saved in the command stage by now.
19912 + */
19913 + ctrl_req->data = cfi->buf_out.buf;
19914 + cfi->need_status_in_complete = 0;
19915 + cfi->need_gadget_att = 0;
19916 +
19917 + switch (bRequest) {
19918 + case VEN_CORE_WRITE_REGISTER:
19919 + /* The buffer contains raw data of the new value for the register */
19920 + reg_value = *((uint32_t *) buf);
19921 + if (wValue == 0) {
19922 + addr = 0;
19923 + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
19924 + addr += wIndex;
19925 + } else {
19926 + addr = (wValue << 16) | wIndex;
19927 + }
19928 +
19929 + //writel(reg_value, addr);
19930 +
19931 + retval = 0;
19932 + cfi->need_status_in_complete = 1;
19933 + break;
19934 +
19935 + case VEN_CORE_SET_FEATURE:
19936 + /* The buffer contains raw data of the new value of the feature */
19937 + retval = cfi_set_feature_value(pcd);
19938 + if (retval < 0)
19939 + return retval;
19940 +
19941 + cfi->need_status_in_complete = 1;
19942 + break;
19943 +
19944 + default:
19945 + break;
19946 + }
19947 +
19948 + return retval;
19949 +}
19950 +
19951 +/**
19952 + * This function builds the DMA descriptors for the SG buffer mode.
19953 + */
19954 +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
19955 + dwc_otg_pcd_request_t * req)
19956 +{
19957 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
19958 + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
19959 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
19960 + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
19961 + dma_addr_t buff_addr = req->dma;
19962 + int i;
19963 + uint32_t txsize, off;
19964 +
19965 + txsize = sgval->wSize;
19966 + off = sgval->bOffset;
19967 +
19968 +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
19969 +// __func__, cfiep->ep->ep.name, txsize, off);
19970 +
19971 + for (i = 0; i < sgval->bCount; i++) {
19972 + desc->status.b.bs = BS_HOST_BUSY;
19973 + desc->buf = buff_addr;
19974 + desc->status.b.l = 0;
19975 + desc->status.b.ioc = 0;
19976 + desc->status.b.sp = 0;
19977 + desc->status.b.bytes = txsize;
19978 + desc->status.b.bs = BS_HOST_READY;
19979 +
19980 + /* Set the next address of the buffer */
19981 + buff_addr += txsize + off;
19982 + desc_last = desc;
19983 + desc++;
19984 + }
19985 +
19986 + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
19987 + desc_last->status.b.l = 1;
19988 + desc_last->status.b.ioc = 1;
19989 + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
19990 + /* Save the last DMA descriptor pointer */
19991 + cfiep->dma_desc_last = desc_last;
19992 + cfiep->desc_count = sgval->bCount;
19993 +}
19994 +
19995 +/**
19996 + * This function builds the DMA descriptors for the Concatenation buffer mode.
19997 + */
19998 +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
19999 + dwc_otg_pcd_request_t * req)
20000 +{
20001 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
20002 + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
20003 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
20004 + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
20005 + dma_addr_t buff_addr = req->dma;
20006 + int i;
20007 + uint16_t *txsize;
20008 +
20009 + txsize = concatval->wTxBytes;
20010 +
20011 + for (i = 0; i < concatval->hdr.bDescCount; i++) {
20012 + desc->buf = buff_addr;
20013 + desc->status.b.bs = BS_HOST_BUSY;
20014 + desc->status.b.l = 0;
20015 + desc->status.b.ioc = 0;
20016 + desc->status.b.sp = 0;
20017 + desc->status.b.bytes = *txsize;
20018 + desc->status.b.bs = BS_HOST_READY;
20019 +
20020 + txsize++;
20021 + /* Set the next address of the buffer */
20022 + buff_addr += UGETW(ep->desc->wMaxPacketSize);
20023 + desc_last = desc;
20024 + desc++;
20025 + }
20026 +
20027 + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
20028 + desc_last->status.b.l = 1;
20029 + desc_last->status.b.ioc = 1;
20030 + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
20031 + cfiep->dma_desc_last = desc_last;
20032 + cfiep->desc_count = concatval->hdr.bDescCount;
20033 +}
20034 +
20035 +/**
20036 + * This function builds the DMA descriptors for the Circular buffer mode
20037 + */
20038 +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20039 + dwc_otg_pcd_request_t * req)
20040 +{
20041 + /* @todo: MAS - add implementation when this feature needs to be tested */
20042 +}
20043 +
20044 +/**
20045 + * This function builds the DMA descriptors for the Alignment buffer mode
20046 + */
20047 +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20048 + dwc_otg_pcd_request_t * req)
20049 +{
20050 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
20051 + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
20052 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
20053 + dma_addr_t buff_addr = req->dma;
20054 +
20055 + desc->status.b.bs = BS_HOST_BUSY;
20056 + desc->status.b.l = 1;
20057 + desc->status.b.ioc = 1;
20058 + desc->status.b.sp = ep->dwc_ep.sent_zlp;
20059 + desc->status.b.bytes = req->length;
20060 + /* Adjust the buffer alignment */
20061 + desc->buf = (buff_addr + alignval->bAlign);
20062 + desc->status.b.bs = BS_HOST_READY;
20063 + cfiep->dma_desc_last = desc;
20064 + cfiep->desc_count = 1;
20065 +}
20066 +
20067 +/**
20068 + * This function builds the DMA descriptors chain for different modes of the
20069 + * buffer setup of an endpoint.
20070 + */
20071 +static void cfi_build_descriptors(struct cfiobject *cfi,
20072 + struct dwc_otg_pcd *pcd,
20073 + struct dwc_otg_pcd_ep *ep,
20074 + dwc_otg_pcd_request_t * req)
20075 +{
20076 + cfi_ep_t *cfiep;
20077 +
20078 + /* Get the cfiep by the dwc_otg_pcd_ep */
20079 + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
20080 + if (NULL == cfiep) {
20081 + CFI_INFO("%s: Unable to find a matching active endpoint\n",
20082 + __func__);
20083 + return;
20084 + }
20085 +
20086 + cfiep->xfer_len = req->length;
20087 +
20088 + /* Iterate through all the DMA descriptors */
20089 + switch (cfiep->ep->dwc_ep.buff_mode) {
20090 + case BM_SG:
20091 + cfi_build_sg_descs(cfi, cfiep, req);
20092 + break;
20093 +
20094 + case BM_CONCAT:
20095 + cfi_build_concat_descs(cfi, cfiep, req);
20096 + break;
20097 +
20098 + case BM_CIRCULAR:
20099 + cfi_build_circ_descs(cfi, cfiep, req);
20100 + break;
20101 +
20102 + case BM_ALIGN:
20103 + cfi_build_align_descs(cfi, cfiep, req);
20104 + break;
20105 +
20106 + default:
20107 + break;
20108 + }
20109 +}
20110 +
20111 +/**
20112 + * Allocate DMA buffer for different Buffer modes.
20113 + */
20114 +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
20115 + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
20116 + unsigned size, gfp_t flags)
20117 +{
20118 + return DWC_DMA_ALLOC(size, dma);
20119 +}
20120 +
20121 +/**
20122 + * This function initializes the CFI object.
20123 + */
20124 +int init_cfi(cfiobject_t * cfiobj)
20125 +{
20126 + CFI_INFO("%s\n", __func__);
20127 +
20128 + /* Allocate a buffer for IN XFERs */
20129 + cfiobj->buf_in.buf =
20130 + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
20131 + if (NULL == cfiobj->buf_in.buf) {
20132 + CFI_INFO("Unable to allocate buffer for INs\n");
20133 + return -DWC_E_NO_MEMORY;
20134 + }
20135 +
20136 + /* Allocate a buffer for OUT XFERs */
20137 + cfiobj->buf_out.buf =
20138 + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
20139 + if (NULL == cfiobj->buf_out.buf) {
20140 + CFI_INFO("Unable to allocate buffer for OUT\n");
20141 + return -DWC_E_NO_MEMORY;
20142 + }
20143 +
20144 + /* Initialize the callback function pointers */
20145 + cfiobj->ops.release = cfi_release;
20146 + cfiobj->ops.ep_enable = cfi_ep_enable;
20147 + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
20148 + cfiobj->ops.build_descriptors = cfi_build_descriptors;
20149 + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
20150 +
20151 + /* Initialize the list of active endpoints in the CFI object */
20152 + DWC_LIST_INIT(&cfiobj->active_eps);
20153 +
20154 + return 0;
20155 +}
20156 +
20157 +/**
20158 + * This function reads the required feature's current value into the buffer
20159 + *
20160 + * @retval: Returns negative as error, or the data length of the feature
20161 + */
20162 +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
20163 + struct dwc_otg_pcd *pcd,
20164 + struct cfi_usb_ctrlrequest *ctrl_req)
20165 +{
20166 + int retval = -DWC_E_NOT_SUPPORTED;
20167 + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
20168 + uint16_t dfifo, rxfifo, txfifo;
20169 +
20170 + switch (ctrl_req->wIndex) {
20171 + /* Whether the DDMA is enabled or not */
20172 + case FT_ID_DMA_MODE:
20173 + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
20174 + retval = 1;
20175 + break;
20176 +
20177 + case FT_ID_DMA_BUFFER_SETUP:
20178 + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
20179 + break;
20180 +
20181 + case FT_ID_DMA_BUFF_ALIGN:
20182 + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
20183 + break;
20184 +
20185 + case FT_ID_DMA_CONCAT_SETUP:
20186 + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
20187 + break;
20188 +
20189 + case FT_ID_DMA_CIRCULAR:
20190 + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
20191 + break;
20192 +
20193 + case FT_ID_THRESHOLD_SETUP:
20194 + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
20195 + break;
20196 +
20197 + case FT_ID_DFIFO_DEPTH:
20198 + dfifo = get_dfifo_size(coreif);
20199 + *((uint16_t *) buf) = dfifo;
20200 + retval = sizeof(uint16_t);
20201 + break;
20202 +
20203 + case FT_ID_TX_FIFO_DEPTH:
20204 + retval = get_txfifo_size(pcd, ctrl_req->wValue);
20205 + if (retval >= 0) {
20206 + txfifo = retval;
20207 + *((uint16_t *) buf) = txfifo;
20208 + retval = sizeof(uint16_t);
20209 + }
20210 + break;
20211 +
20212 + case FT_ID_RX_FIFO_DEPTH:
20213 + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
20214 + if (retval >= 0) {
20215 + rxfifo = retval;
20216 + *((uint16_t *) buf) = rxfifo;
20217 + retval = sizeof(uint16_t);
20218 + }
20219 + break;
20220 + }
20221 +
20222 + return retval;
20223 +}
20224 +
20225 +/**
20226 + * This function resets the SG for the specified EP to its default value
20227 + */
20228 +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
20229 +{
20230 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20231 + return 0;
20232 +}
20233 +
20234 +/**
20235 + * This function resets the Alignment for the specified EP to its default value
20236 + */
20237 +static int cfi_reset_align_val(cfi_ep_t * cfiep)
20238 +{
20239 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20240 + return 0;
20241 +}
20242 +
20243 +/**
20244 + * This function resets the Concatenation for the specified EP to its default value
20245 + * This function will also set the value of the wTxBytes field to NULL after
20246 + * freeing the memory previously allocated for this field.
20247 + */
20248 +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
20249 +{
20250 + /* First we need to free the wTxBytes field */
20251 + if (cfiep->bm_concat->wTxBytes) {
20252 + DWC_FREE(cfiep->bm_concat->wTxBytes);
20253 + cfiep->bm_concat->wTxBytes = NULL;
20254 + }
20255 +
20256 + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
20257 + return 0;
20258 +}
20259 +
20260 +/**
20261 + * This function resets all the buffer setups of the specified endpoint
20262 + */
20263 +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
20264 +{
20265 + cfi_reset_sg_val(cfiep);
20266 + cfi_reset_align_val(cfiep);
20267 + cfi_reset_concat_val(cfiep);
20268 + return 0;
20269 +}
20270 +
20271 +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
20272 + uint8_t rx_rst, uint8_t tx_rst)
20273 +{
20274 + int retval = -DWC_E_INVALID;
20275 + uint16_t tx_siz[15];
20276 + uint16_t rx_siz = 0;
20277 + dwc_otg_pcd_ep_t *ep = NULL;
20278 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
20279 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
20280 +
20281 + if (rx_rst) {
20282 + rx_siz = params->dev_rx_fifo_size;
20283 + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
20284 + }
20285 +
20286 + if (tx_rst) {
20287 + if (ep_addr == 0) {
20288 + int i;
20289 +
20290 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20291 + tx_siz[i] =
20292 + core_if->core_params->dev_tx_fifo_size[i];
20293 + core_if->core_params->dev_tx_fifo_size[i] =
20294 + core_if->init_txfsiz[i];
20295 + }
20296 + } else {
20297 +
20298 + ep = get_ep_by_addr(pcd, ep_addr);
20299 +
20300 + if (NULL == ep) {
20301 + CFI_INFO
20302 + ("%s: Unable to get the endpoint addr=0x%02x\n",
20303 + __func__, ep_addr);
20304 + return -DWC_E_INVALID;
20305 + }
20306 +
20307 + tx_siz[0] =
20308 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
20309 + 1];
20310 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
20311 + GET_CORE_IF(pcd)->init_txfsiz[ep->
20312 + dwc_ep.tx_fifo_num -
20313 + 1];
20314 + }
20315 + }
20316 +
20317 + if (resize_fifos(GET_CORE_IF(pcd))) {
20318 + retval = 0;
20319 + } else {
20320 + CFI_INFO
20321 + ("%s: Error resetting the feature Reset All(FIFO size)\n",
20322 + __func__);
20323 + if (rx_rst) {
20324 + params->dev_rx_fifo_size = rx_siz;
20325 + }
20326 +
20327 + if (tx_rst) {
20328 + if (ep_addr == 0) {
20329 + int i;
20330 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
20331 + i++) {
20332 + core_if->
20333 + core_params->dev_tx_fifo_size[i] =
20334 + tx_siz[i];
20335 + }
20336 + } else {
20337 + params->dev_tx_fifo_size[ep->
20338 + dwc_ep.tx_fifo_num -
20339 + 1] = tx_siz[0];
20340 + }
20341 + }
20342 + retval = -DWC_E_INVALID;
20343 + }
20344 + return retval;
20345 +}
20346 +
20347 +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
20348 +{
20349 + int retval = 0;
20350 + cfi_ep_t *cfiep;
20351 + cfiobject_t *cfi = pcd->cfi;
20352 + dwc_list_link_t *tmp;
20353 +
20354 + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
20355 + if (retval < 0) {
20356 + return retval;
20357 + }
20358 +
20359 + /* If the EP address is known then reset the features for only that EP */
20360 + if (addr) {
20361 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20362 + if (NULL == cfiep) {
20363 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20364 + __func__, addr);
20365 + return -DWC_E_INVALID;
20366 + }
20367 + retval = cfi_ep_reset_all_setup_vals(cfiep);
20368 + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
20369 + }
20370 + /* Otherwise (wValue == 0), reset all features of all EP's */
20371 + else {
20372 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20373 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20374 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20375 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20376 + retval = cfi_ep_reset_all_setup_vals(cfiep);
20377 + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
20378 + if (retval < 0) {
20379 + CFI_INFO
20380 + ("%s: Error resetting the feature Reset All\n",
20381 + __func__);
20382 + return retval;
20383 + }
20384 + }
20385 + }
20386 + return retval;
20387 +}
20388 +
20389 +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
20390 + uint8_t addr)
20391 +{
20392 + int retval = 0;
20393 + cfi_ep_t *cfiep;
20394 + cfiobject_t *cfi = pcd->cfi;
20395 + dwc_list_link_t *tmp;
20396 +
20397 + /* If the EP address is known then reset the features for only that EP */
20398 + if (addr) {
20399 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20400 + if (NULL == cfiep) {
20401 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20402 + __func__, addr);
20403 + return -DWC_E_INVALID;
20404 + }
20405 + retval = cfi_reset_sg_val(cfiep);
20406 + }
20407 + /* Otherwise (wValue == 0), reset all features of all EP's */
20408 + else {
20409 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20410 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20411 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20412 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20413 + retval = cfi_reset_sg_val(cfiep);
20414 + if (retval < 0) {
20415 + CFI_INFO
20416 + ("%s: Error resetting the feature Buffer Setup\n",
20417 + __func__);
20418 + return retval;
20419 + }
20420 + }
20421 + }
20422 + return retval;
20423 +}
20424 +
20425 +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
20426 +{
20427 + int retval = 0;
20428 + cfi_ep_t *cfiep;
20429 + cfiobject_t *cfi = pcd->cfi;
20430 + dwc_list_link_t *tmp;
20431 +
20432 + /* If the EP address is known then reset the features for only that EP */
20433 + if (addr) {
20434 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20435 + if (NULL == cfiep) {
20436 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20437 + __func__, addr);
20438 + return -DWC_E_INVALID;
20439 + }
20440 + retval = cfi_reset_concat_val(cfiep);
20441 + }
20442 + /* Otherwise (wValue == 0), reset all features of all EP's */
20443 + else {
20444 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20445 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20446 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20447 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20448 + retval = cfi_reset_concat_val(cfiep);
20449 + if (retval < 0) {
20450 + CFI_INFO
20451 + ("%s: Error resetting the feature Concatenation Value\n",
20452 + __func__);
20453 + return retval;
20454 + }
20455 + }
20456 + }
20457 + return retval;
20458 +}
20459 +
20460 +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
20461 +{
20462 + int retval = 0;
20463 + cfi_ep_t *cfiep;
20464 + cfiobject_t *cfi = pcd->cfi;
20465 + dwc_list_link_t *tmp;
20466 +
20467 + /* If the EP address is known then reset the features for only that EP */
20468 + if (addr) {
20469 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20470 + if (NULL == cfiep) {
20471 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20472 + __func__, addr);
20473 + return -DWC_E_INVALID;
20474 + }
20475 + retval = cfi_reset_align_val(cfiep);
20476 + }
20477 + /* Otherwise (wValue == 0), reset all features of all EP's */
20478 + else {
20479 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20480 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20481 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20482 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20483 + retval = cfi_reset_align_val(cfiep);
20484 + if (retval < 0) {
20485 + CFI_INFO
20486 + ("%s: Error resetting the feature Aliignment Value\n",
20487 + __func__);
20488 + return retval;
20489 + }
20490 + }
20491 + }
20492 + return retval;
20493 +
20494 +}
20495 +
20496 +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
20497 + struct cfi_usb_ctrlrequest *req)
20498 +{
20499 + int retval = 0;
20500 +
20501 + switch (req->wIndex) {
20502 + case 0:
20503 + /* Reset all features */
20504 + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
20505 + break;
20506 +
20507 + case FT_ID_DMA_BUFFER_SETUP:
20508 + /* Reset the SG buffer setup */
20509 + retval =
20510 + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
20511 + break;
20512 +
20513 + case FT_ID_DMA_CONCAT_SETUP:
20514 + /* Reset the Concatenation buffer setup */
20515 + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
20516 + break;
20517 +
20518 + case FT_ID_DMA_BUFF_ALIGN:
20519 + /* Reset the Alignment buffer setup */
20520 + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
20521 + break;
20522 +
20523 + case FT_ID_TX_FIFO_DEPTH:
20524 + retval =
20525 + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
20526 + pcd->cfi->need_gadget_att = 0;
20527 + break;
20528 +
20529 + case FT_ID_RX_FIFO_DEPTH:
20530 + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
20531 + pcd->cfi->need_gadget_att = 0;
20532 + break;
20533 + default:
20534 + break;
20535 + }
20536 + return retval;
20537 +}
20538 +
20539 +/**
20540 + * This function sets a new value for the SG buffer setup.
20541 + */
20542 +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20543 +{
20544 + uint8_t inaddr, outaddr;
20545 + cfi_ep_t *epin, *epout;
20546 + ddma_sg_buffer_setup_t *psgval;
20547 + uint32_t desccount, size;
20548 +
20549 + CFI_INFO("%s\n", __func__);
20550 +
20551 + psgval = (ddma_sg_buffer_setup_t *) buf;
20552 + desccount = (uint32_t) psgval->bCount;
20553 + size = (uint32_t) psgval->wSize;
20554 +
20555 + /* Check the DMA descriptor count */
20556 + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
20557 + CFI_INFO
20558 + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
20559 + __func__, MAX_DMA_DESCS_PER_EP);
20560 + return -DWC_E_INVALID;
20561 + }
20562 +
20563 + /* Check the DMA descriptor count */
20564 +
20565 + if (size == 0) {
20566 +
20567 + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
20568 + __func__);
20569 +
20570 + return -DWC_E_INVALID;
20571 +
20572 + }
20573 +
20574 + inaddr = psgval->bInEndpointAddress;
20575 + outaddr = psgval->bOutEndpointAddress;
20576 +
20577 + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
20578 + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
20579 +
20580 + if (NULL == epin || NULL == epout) {
20581 + CFI_INFO
20582 + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
20583 + __func__, inaddr, outaddr);
20584 + return -DWC_E_INVALID;
20585 + }
20586 +
20587 + epin->ep->dwc_ep.buff_mode = BM_SG;
20588 + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
20589 +
20590 + epout->ep->dwc_ep.buff_mode = BM_SG;
20591 + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
20592 +
20593 + return 0;
20594 +}
20595 +
20596 +/**
20597 + * This function sets a new value for the buffer Alignment setup.
20598 + */
20599 +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20600 +{
20601 + cfi_ep_t *ep;
20602 + uint8_t addr;
20603 + ddma_align_buffer_setup_t *palignval;
20604 +
20605 + palignval = (ddma_align_buffer_setup_t *) buf;
20606 + addr = palignval->bEndpointAddress;
20607 +
20608 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20609 +
20610 + if (NULL == ep) {
20611 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20612 + __func__, addr);
20613 + return -DWC_E_INVALID;
20614 + }
20615 +
20616 + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
20617 + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
20618 +
20619 + return 0;
20620 +}
20621 +
20622 +/**
20623 + * This function sets a new value for the Concatenation buffer setup.
20624 + */
20625 +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20626 +{
20627 + uint8_t addr;
20628 + cfi_ep_t *ep;
20629 + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
20630 + uint16_t *pVals;
20631 + uint32_t desccount;
20632 + int i;
20633 + uint16_t mps;
20634 +
20635 + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
20636 + desccount = (uint32_t) pConcatValHdr->bDescCount;
20637 + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
20638 +
20639 + /* Check the DMA descriptor count */
20640 + if (desccount > MAX_DMA_DESCS_PER_EP) {
20641 + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
20642 + __func__, MAX_DMA_DESCS_PER_EP);
20643 + return -DWC_E_INVALID;
20644 + }
20645 +
20646 + addr = pConcatValHdr->bEndpointAddress;
20647 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20648 + if (NULL == ep) {
20649 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20650 + __func__, addr);
20651 + return -DWC_E_INVALID;
20652 + }
20653 +
20654 + mps = UGETW(ep->ep->desc->wMaxPacketSize);
20655 +
20656 +#if 0
20657 + for (i = 0; i < desccount; i++) {
20658 + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
20659 + }
20660 + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
20661 +#endif
20662 +
20663 + /* Check the wTxSizes to be less than or equal to the mps */
20664 + for (i = 0; i < desccount; i++) {
20665 + if (pVals[i] > mps) {
20666 + CFI_INFO
20667 + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
20668 + __func__, i, pVals[i]);
20669 + return -DWC_E_INVALID;
20670 + }
20671 + }
20672 +
20673 + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
20674 + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
20675 +
20676 + /* Free the previously allocated storage for the wTxBytes */
20677 + if (ep->bm_concat->wTxBytes) {
20678 + DWC_FREE(ep->bm_concat->wTxBytes);
20679 + }
20680 +
20681 + /* Allocate a new storage for the wTxBytes field */
20682 + ep->bm_concat->wTxBytes =
20683 + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
20684 + if (NULL == ep->bm_concat->wTxBytes) {
20685 + CFI_INFO("%s: Unable to allocate memory\n", __func__);
20686 + return -DWC_E_NO_MEMORY;
20687 + }
20688 +
20689 + /* Copy the new values into the wTxBytes filed */
20690 + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
20691 + sizeof(uint16_t) * pConcatValHdr->bDescCount);
20692 +
20693 + return 0;
20694 +}
20695 +
20696 +/**
20697 + * This function calculates the total of all FIFO sizes
20698 + *
20699 + * @param core_if Programming view of DWC_otg controller
20700 + *
20701 + * @return The total of data FIFO sizes.
20702 + *
20703 + */
20704 +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
20705 +{
20706 + dwc_otg_core_params_t *params = core_if->core_params;
20707 + uint16_t dfifo_total = 0;
20708 + int i;
20709 +
20710 + /* The shared RxFIFO size */
20711 + dfifo_total =
20712 + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
20713 +
20714 + /* Add up each TxFIFO size to the total */
20715 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20716 + dfifo_total += params->dev_tx_fifo_size[i];
20717 + }
20718 +
20719 + return dfifo_total;
20720 +}
20721 +
20722 +/**
20723 + * This function returns Rx FIFO size
20724 + *
20725 + * @param core_if Programming view of DWC_otg controller
20726 + *
20727 + * @return The total of data FIFO sizes.
20728 + *
20729 + */
20730 +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
20731 +{
20732 + switch (wValue >> 8) {
20733 + case 0:
20734 + return (core_if->pwron_rxfsiz <
20735 + 32768) ? core_if->pwron_rxfsiz : 32768;
20736 + break;
20737 + case 1:
20738 + return core_if->core_params->dev_rx_fifo_size;
20739 + break;
20740 + default:
20741 + return -DWC_E_INVALID;
20742 + break;
20743 + }
20744 +}
20745 +
20746 +/**
20747 + * This function returns Tx FIFO size for IN EP
20748 + *
20749 + * @param core_if Programming view of DWC_otg controller
20750 + *
20751 + * @return The total of data FIFO sizes.
20752 + *
20753 + */
20754 +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
20755 +{
20756 + dwc_otg_pcd_ep_t *ep;
20757 +
20758 + ep = get_ep_by_addr(pcd, wValue & 0xff);
20759 +
20760 + if (NULL == ep) {
20761 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20762 + __func__, wValue & 0xff);
20763 + return -DWC_E_INVALID;
20764 + }
20765 +
20766 + if (!ep->dwc_ep.is_in) {
20767 + CFI_INFO
20768 + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
20769 + __func__, wValue & 0xff);
20770 + return -DWC_E_INVALID;
20771 + }
20772 +
20773 + switch (wValue >> 8) {
20774 + case 0:
20775 + return (GET_CORE_IF(pcd)->pwron_txfsiz
20776 + [ep->dwc_ep.tx_fifo_num - 1] <
20777 + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
20778 + dwc_ep.tx_fifo_num
20779 + - 1] : 32768;
20780 + break;
20781 + case 1:
20782 + return GET_CORE_IF(pcd)->core_params->
20783 + dev_tx_fifo_size[ep->dwc_ep.num - 1];
20784 + break;
20785 + default:
20786 + return -DWC_E_INVALID;
20787 + break;
20788 + }
20789 +}
20790 +
20791 +/**
20792 + * This function checks if the submitted combination of
20793 + * device mode FIFO sizes is possible or not.
20794 + *
20795 + * @param core_if Programming view of DWC_otg controller
20796 + *
20797 + * @return 1 if possible, 0 otherwise.
20798 + *
20799 + */
20800 +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
20801 +{
20802 + uint16_t dfifo_actual = 0;
20803 + dwc_otg_core_params_t *params = core_if->core_params;
20804 + uint16_t start_addr = 0;
20805 + int i;
20806 +
20807 + dfifo_actual =
20808 + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
20809 +
20810 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20811 + dfifo_actual += params->dev_tx_fifo_size[i];
20812 + }
20813 +
20814 + if (dfifo_actual > core_if->total_fifo_size) {
20815 + return 0;
20816 + }
20817 +
20818 + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
20819 + return 0;
20820 +
20821 + if (params->dev_nperio_tx_fifo_size > 32768
20822 + || params->dev_nperio_tx_fifo_size < 16)
20823 + return 0;
20824 +
20825 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20826 +
20827 + if (params->dev_tx_fifo_size[i] > 768
20828 + || params->dev_tx_fifo_size[i] < 4)
20829 + return 0;
20830 + }
20831 +
20832 + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
20833 + return 0;
20834 + start_addr = params->dev_rx_fifo_size;
20835 +
20836 + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
20837 + return 0;
20838 + start_addr += params->dev_nperio_tx_fifo_size;
20839 +
20840 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20841 +
20842 + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
20843 + return 0;
20844 + start_addr += params->dev_tx_fifo_size[i];
20845 + }
20846 +
20847 + return 1;
20848 +}
20849 +
20850 +/**
20851 + * This function resizes Device mode FIFOs
20852 + *
20853 + * @param core_if Programming view of DWC_otg controller
20854 + *
20855 + * @return 1 if successful, 0 otherwise
20856 + *
20857 + */
20858 +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
20859 +{
20860 + int i = 0;
20861 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
20862 + dwc_otg_core_params_t *params = core_if->core_params;
20863 + uint32_t rx_fifo_size;
20864 + fifosize_data_t nptxfifosize;
20865 + fifosize_data_t txfifosize[15];
20866 +
20867 + uint32_t rx_fsz_bak;
20868 + uint32_t nptxfsz_bak;
20869 + uint32_t txfsz_bak[15];
20870 +
20871 + uint16_t start_address;
20872 + uint8_t retval = 1;
20873 +
20874 + if (!check_fifo_sizes(core_if)) {
20875 + return 0;
20876 + }
20877 +
20878 + /* Configure data FIFO sizes */
20879 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
20880 + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
20881 + rx_fifo_size = params->dev_rx_fifo_size;
20882 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
20883 +
20884 + /*
20885 + * Tx FIFOs These FIFOs are numbered from 1 to 15.
20886 + * Indexes of the FIFO size module parameters in the
20887 + * dev_tx_fifo_size array and the FIFO size registers in
20888 + * the dtxfsiz array run from 0 to 14.
20889 + */
20890 +
20891 + /* Non-periodic Tx FIFO */
20892 + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
20893 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
20894 + start_address = params->dev_rx_fifo_size;
20895 + nptxfifosize.b.startaddr = start_address;
20896 +
20897 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
20898 +
20899 + start_address += nptxfifosize.b.depth;
20900 +
20901 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20902 + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
20903 +
20904 + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
20905 + txfifosize[i].b.startaddr = start_address;
20906 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
20907 + txfifosize[i].d32);
20908 +
20909 + start_address += txfifosize[i].b.depth;
20910 + }
20911 +
20912 + /** Check if register values are set correctly */
20913 + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
20914 + retval = 0;
20915 + }
20916 +
20917 + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
20918 + retval = 0;
20919 + }
20920 +
20921 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20922 + if (txfifosize[i].d32 !=
20923 + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
20924 + retval = 0;
20925 + }
20926 + }
20927 +
20928 + /** If register values are not set correctly, reset old values */
20929 + if (retval == 0) {
20930 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
20931 +
20932 + /* Non-periodic Tx FIFO */
20933 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
20934 +
20935 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20936 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
20937 + txfsz_bak[i]);
20938 + }
20939 + }
20940 + } else {
20941 + return 0;
20942 + }
20943 +
20944 + /* Flush the FIFOs */
20945 + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
20946 + dwc_otg_flush_rx_fifo(core_if);
20947 +
20948 + return retval;
20949 +}
20950 +
20951 +/**
20952 + * This function sets a new value for the buffer Alignment setup.
20953 + */
20954 +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
20955 +{
20956 + int retval;
20957 + uint32_t fsiz;
20958 + uint16_t size;
20959 + uint16_t ep_addr;
20960 + dwc_otg_pcd_ep_t *ep;
20961 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
20962 + tx_fifo_size_setup_t *ptxfifoval;
20963 +
20964 + ptxfifoval = (tx_fifo_size_setup_t *) buf;
20965 + ep_addr = ptxfifoval->bEndpointAddress;
20966 + size = ptxfifoval->wDepth;
20967 +
20968 + ep = get_ep_by_addr(pcd, ep_addr);
20969 +
20970 + CFI_INFO
20971 + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
20972 + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
20973 +
20974 + if (NULL == ep) {
20975 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20976 + __func__, ep_addr);
20977 + return -DWC_E_INVALID;
20978 + }
20979 +
20980 + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
20981 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
20982 +
20983 + if (resize_fifos(GET_CORE_IF(pcd))) {
20984 + retval = 0;
20985 + } else {
20986 + CFI_INFO
20987 + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
20988 + __func__, ep_addr);
20989 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
20990 + retval = -DWC_E_INVALID;
20991 + }
20992 +
20993 + return retval;
20994 +}
20995 +
20996 +/**
20997 + * This function sets a new value for the buffer Alignment setup.
20998 + */
20999 +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
21000 +{
21001 + int retval;
21002 + uint32_t fsiz;
21003 + uint16_t size;
21004 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
21005 + rx_fifo_size_setup_t *prxfifoval;
21006 +
21007 + prxfifoval = (rx_fifo_size_setup_t *) buf;
21008 + size = prxfifoval->wDepth;
21009 +
21010 + fsiz = params->dev_rx_fifo_size;
21011 + params->dev_rx_fifo_size = size;
21012 +
21013 + if (resize_fifos(GET_CORE_IF(pcd))) {
21014 + retval = 0;
21015 + } else {
21016 + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
21017 + __func__);
21018 + params->dev_rx_fifo_size = fsiz;
21019 + retval = -DWC_E_INVALID;
21020 + }
21021 +
21022 + return retval;
21023 +}
21024 +
21025 +/**
21026 + * This function reads the SG of an EP's buffer setup into the buffer buf
21027 + */
21028 +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21029 + struct cfi_usb_ctrlrequest *req)
21030 +{
21031 + int retval = -DWC_E_INVALID;
21032 + uint8_t addr;
21033 + cfi_ep_t *ep;
21034 +
21035 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21036 + addr = req->wValue & 0xFF;
21037 + if (addr == 0) /* The address should be non-zero */
21038 + return retval;
21039 +
21040 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21041 + if (NULL == ep) {
21042 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21043 + __func__, addr);
21044 + return retval;
21045 + }
21046 +
21047 + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
21048 + retval = BS_SG_VAL_DESC_LEN;
21049 + return retval;
21050 +}
21051 +
21052 +/**
21053 + * This function reads the Concatenation value of an EP's buffer mode into
21054 + * the buffer buf
21055 + */
21056 +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21057 + struct cfi_usb_ctrlrequest *req)
21058 +{
21059 + int retval = -DWC_E_INVALID;
21060 + uint8_t addr;
21061 + cfi_ep_t *ep;
21062 + uint8_t desc_count;
21063 +
21064 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21065 + addr = req->wValue & 0xFF;
21066 + if (addr == 0) /* The address should be non-zero */
21067 + return retval;
21068 +
21069 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21070 + if (NULL == ep) {
21071 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21072 + __func__, addr);
21073 + return retval;
21074 + }
21075 +
21076 + /* Copy the header to the buffer */
21077 + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
21078 + /* Advance the buffer pointer by the header size */
21079 + buf += BS_CONCAT_VAL_HDR_LEN;
21080 +
21081 + desc_count = ep->bm_concat->hdr.bDescCount;
21082 + /* Copy alll the wTxBytes to the buffer */
21083 + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
21084 +
21085 + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
21086 + return retval;
21087 +}
21088 +
21089 +/**
21090 + * This function reads the buffer Alignment value of an EP's buffer mode into
21091 + * the buffer buf
21092 + *
21093 + * @return The total number of bytes copied to the buffer or negative error code.
21094 + */
21095 +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21096 + struct cfi_usb_ctrlrequest *req)
21097 +{
21098 + int retval = -DWC_E_INVALID;
21099 + uint8_t addr;
21100 + cfi_ep_t *ep;
21101 +
21102 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21103 + addr = req->wValue & 0xFF;
21104 + if (addr == 0) /* The address should be non-zero */
21105 + return retval;
21106 +
21107 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21108 + if (NULL == ep) {
21109 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21110 + __func__, addr);
21111 + return retval;
21112 + }
21113 +
21114 + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
21115 + retval = BS_ALIGN_VAL_HDR_LEN;
21116 +
21117 + return retval;
21118 +}
21119 +
21120 +/**
21121 + * This function sets a new value for the specified feature
21122 + *
21123 + * @param pcd A pointer to the PCD object
21124 + *
21125 + * @return 0 if successful, negative error code otherwise to stall the DCE.
21126 + */
21127 +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
21128 +{
21129 + int retval = -DWC_E_NOT_SUPPORTED;
21130 + uint16_t wIndex, wValue;
21131 + uint8_t bRequest;
21132 + struct dwc_otg_core_if *coreif;
21133 + cfiobject_t *cfi = pcd->cfi;
21134 + struct cfi_usb_ctrlrequest *ctrl_req;
21135 + uint8_t *buf;
21136 + ctrl_req = &cfi->ctrl_req;
21137 +
21138 + buf = pcd->cfi->ctrl_req.data;
21139 +
21140 + coreif = GET_CORE_IF(pcd);
21141 + bRequest = ctrl_req->bRequest;
21142 + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
21143 + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
21144 +
21145 + /* See which feature is to be modified */
21146 + switch (wIndex) {
21147 + case FT_ID_DMA_BUFFER_SETUP:
21148 + /* Modify the feature */
21149 + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
21150 + return retval;
21151 +
21152 + /* And send this request to the gadget */
21153 + cfi->need_gadget_att = 1;
21154 + break;
21155 +
21156 + case FT_ID_DMA_BUFF_ALIGN:
21157 + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
21158 + return retval;
21159 + cfi->need_gadget_att = 1;
21160 + break;
21161 +
21162 + case FT_ID_DMA_CONCAT_SETUP:
21163 + /* Modify the feature */
21164 + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
21165 + return retval;
21166 + cfi->need_gadget_att = 1;
21167 + break;
21168 +
21169 + case FT_ID_DMA_CIRCULAR:
21170 + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
21171 + break;
21172 +
21173 + case FT_ID_THRESHOLD_SETUP:
21174 + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
21175 + break;
21176 +
21177 + case FT_ID_DFIFO_DEPTH:
21178 + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
21179 + break;
21180 +
21181 + case FT_ID_TX_FIFO_DEPTH:
21182 + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
21183 + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
21184 + return retval;
21185 + cfi->need_gadget_att = 0;
21186 + break;
21187 +
21188 + case FT_ID_RX_FIFO_DEPTH:
21189 + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
21190 + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
21191 + return retval;
21192 + cfi->need_gadget_att = 0;
21193 + break;
21194 + }
21195 +
21196 + return retval;
21197 +}
21198 +
21199 +#endif //DWC_UTE_CFI
21200 --- /dev/null
21201 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
21202 @@ -0,0 +1,320 @@
21203 +/* ==========================================================================
21204 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
21205 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
21206 + * otherwise expressly agreed to in writing between Synopsys and you.
21207 + *
21208 + * The Software IS NOT an item of Licensed Software or Licensed Product under
21209 + * any End User Software License Agreement or Agreement for Licensed Product
21210 + * with Synopsys or any supplement thereto. You are permitted to use and
21211 + * redistribute this Software in source and binary forms, with or without
21212 + * modification, provided that redistributions of source code must retain this
21213 + * notice. You may not view, use, disclose, copy or distribute this file or
21214 + * any information contained herein except pursuant to this license grant from
21215 + * Synopsys. If you do not agree with this notice, including the disclaimer
21216 + * below, then you are not authorized to use the Software.
21217 + *
21218 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
21219 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21220 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21221 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
21222 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21223 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21224 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21225 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21226 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21227 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
21228 + * DAMAGE.
21229 + * ========================================================================== */
21230 +
21231 +#if !defined(__DWC_OTG_CFI_H__)
21232 +#define __DWC_OTG_CFI_H__
21233 +
21234 +#include "dwc_otg_pcd.h"
21235 +#include "dwc_cfi_common.h"
21236 +
21237 +/**
21238 + * @file
21239 + * This file contains the CFI related OTG PCD specific common constants,
21240 + * interfaces(functions and macros) and data structures.The CFI Protocol is an
21241 + * optional interface for internal testing purposes that a DUT may implement to
21242 + * support testing of configurable features.
21243 + *
21244 + */
21245 +
21246 +struct dwc_otg_pcd;
21247 +struct dwc_otg_pcd_ep;
21248 +
21249 +/** OTG CFI Features (properties) ID constants */
21250 +/** This is a request for all Core Features */
21251 +#define FT_ID_DMA_MODE 0x0001
21252 +#define FT_ID_DMA_BUFFER_SETUP 0x0002
21253 +#define FT_ID_DMA_BUFF_ALIGN 0x0003
21254 +#define FT_ID_DMA_CONCAT_SETUP 0x0004
21255 +#define FT_ID_DMA_CIRCULAR 0x0005
21256 +#define FT_ID_THRESHOLD_SETUP 0x0006
21257 +#define FT_ID_DFIFO_DEPTH 0x0007
21258 +#define FT_ID_TX_FIFO_DEPTH 0x0008
21259 +#define FT_ID_RX_FIFO_DEPTH 0x0009
21260 +
21261 +/**********************************************************/
21262 +#define CFI_INFO_DEF
21263 +
21264 +#ifdef CFI_INFO_DEF
21265 +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
21266 +#else
21267 +#define CFI_INFO(fmt...)
21268 +#endif
21269 +
21270 +#define min(x,y) ({ \
21271 + x < y ? x : y; })
21272 +
21273 +#define max(x,y) ({ \
21274 + x > y ? x : y; })
21275 +
21276 +/**
21277 + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
21278 + * also used for setting up a buffer for Circular DDMA.
21279 + */
21280 +struct _ddma_sg_buffer_setup {
21281 +#define BS_SG_VAL_DESC_LEN 6
21282 + /* The OUT EP address */
21283 + uint8_t bOutEndpointAddress;
21284 + /* The IN EP address */
21285 + uint8_t bInEndpointAddress;
21286 + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
21287 + uint8_t bOffset;
21288 + /* The number of transfer segments (a DMA descriptors per each segment) */
21289 + uint8_t bCount;
21290 + /* Size (in byte) of each transfer segment */
21291 + uint16_t wSize;
21292 +} __attribute__ ((packed));
21293 +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
21294 +
21295 +/** Descriptor DMA Concatenation Buffer setup structure */
21296 +struct _ddma_concat_buffer_setup_hdr {
21297 +#define BS_CONCAT_VAL_HDR_LEN 4
21298 + /* The endpoint for which the buffer is to be set up */
21299 + uint8_t bEndpointAddress;
21300 + /* The count of descriptors to be used */
21301 + uint8_t bDescCount;
21302 + /* The total size of the transfer */
21303 + uint16_t wSize;
21304 +} __attribute__ ((packed));
21305 +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
21306 +
21307 +/** Descriptor DMA Concatenation Buffer setup structure */
21308 +struct _ddma_concat_buffer_setup {
21309 + /* The SG header */
21310 + ddma_concat_buffer_setup_hdr_t hdr;
21311 +
21312 + /* The XFER sizes pointer (allocated dynamically) */
21313 + uint16_t *wTxBytes;
21314 +} __attribute__ ((packed));
21315 +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
21316 +
21317 +/** Descriptor DMA Alignment Buffer setup structure */
21318 +struct _ddma_align_buffer_setup {
21319 +#define BS_ALIGN_VAL_HDR_LEN 2
21320 + uint8_t bEndpointAddress;
21321 + uint8_t bAlign;
21322 +} __attribute__ ((packed));
21323 +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
21324 +
21325 +/** Transmit FIFO Size setup structure */
21326 +struct _tx_fifo_size_setup {
21327 + uint8_t bEndpointAddress;
21328 + uint16_t wDepth;
21329 +} __attribute__ ((packed));
21330 +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
21331 +
21332 +/** Transmit FIFO Size setup structure */
21333 +struct _rx_fifo_size_setup {
21334 + uint16_t wDepth;
21335 +} __attribute__ ((packed));
21336 +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
21337 +
21338 +/**
21339 + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
21340 + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
21341 + * to the data returned in the data stage of a 3-stage Control Write requests.
21342 + */
21343 +struct cfi_usb_ctrlrequest {
21344 + uint8_t bRequestType;
21345 + uint8_t bRequest;
21346 + uint16_t wValue;
21347 + uint16_t wIndex;
21348 + uint16_t wLength;
21349 + uint8_t *data;
21350 +} UPACKED;
21351 +
21352 +/*---------------------------------------------------------------------------*/
21353 +
21354 +/**
21355 + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
21356 + * This structure is used to store the buffer setup data for any
21357 + * enabled endpoint in the PCD.
21358 + */
21359 +struct cfi_ep {
21360 + /* Entry for the list container */
21361 + dwc_list_link_t lh;
21362 + /* Pointer to the active PCD endpoint structure */
21363 + struct dwc_otg_pcd_ep *ep;
21364 + /* The last descriptor in the chain of DMA descriptors of the endpoint */
21365 + struct dwc_otg_dma_desc *dma_desc_last;
21366 + /* The SG feature value */
21367 + ddma_sg_buffer_setup_t *bm_sg;
21368 + /* The Circular feature value */
21369 + ddma_sg_buffer_setup_t *bm_circ;
21370 + /* The Concatenation feature value */
21371 + ddma_concat_buffer_setup_t *bm_concat;
21372 + /* The Alignment feature value */
21373 + ddma_align_buffer_setup_t *bm_align;
21374 + /* XFER length */
21375 + uint32_t xfer_len;
21376 + /*
21377 + * Count of DMA descriptors currently used.
21378 + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
21379 + * defined in the dwc_otg_cil.h
21380 + */
21381 + uint32_t desc_count;
21382 +};
21383 +typedef struct cfi_ep cfi_ep_t;
21384 +
21385 +typedef struct cfi_dma_buff {
21386 +#define CFI_IN_BUF_LEN 1024
21387 +#define CFI_OUT_BUF_LEN 1024
21388 + dma_addr_t addr;
21389 + uint8_t *buf;
21390 +} cfi_dma_buff_t;
21391 +
21392 +struct cfiobject;
21393 +
21394 +/**
21395 + * This is the interface for the CFI operations.
21396 + *
21397 + * @param ep_enable Called when any endpoint is enabled and activated.
21398 + * @param release Called when the CFI object is released and it needs to correctly
21399 + * deallocate the dynamic memory
21400 + * @param ctrl_write_complete Called when the data stage of the request is complete
21401 + */
21402 +typedef struct cfi_ops {
21403 + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
21404 + struct dwc_otg_pcd_ep * ep);
21405 + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
21406 + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
21407 + unsigned size, gfp_t flags);
21408 + void (*release) (struct cfiobject * cfi);
21409 + int (*ctrl_write_complete) (struct cfiobject * cfi,
21410 + struct dwc_otg_pcd * pcd);
21411 + void (*build_descriptors) (struct cfiobject * cfi,
21412 + struct dwc_otg_pcd * pcd,
21413 + struct dwc_otg_pcd_ep * ep,
21414 + dwc_otg_pcd_request_t * req);
21415 +} cfi_ops_t;
21416 +
21417 +struct cfiobject {
21418 + cfi_ops_t ops;
21419 + struct dwc_otg_pcd *pcd;
21420 + struct usb_gadget *gadget;
21421 +
21422 + /* Buffers used to send/receive CFI-related request data */
21423 + cfi_dma_buff_t buf_in;
21424 + cfi_dma_buff_t buf_out;
21425 +
21426 + /* CFI specific Control request wrapper */
21427 + struct cfi_usb_ctrlrequest ctrl_req;
21428 +
21429 + /* The list of active EP's in the PCD of type cfi_ep_t */
21430 + dwc_list_link_t active_eps;
21431 +
21432 + /* This flag shall control the propagation of a specific request
21433 + * to the gadget's processing routines.
21434 + * 0 - no gadget handling
21435 + * 1 - the gadget needs to know about this request (w/o completing a status
21436 + * phase - just return a 0 to the _setup callback)
21437 + */
21438 + uint8_t need_gadget_att;
21439 +
21440 + /* Flag indicating whether the status IN phase needs to be
21441 + * completed by the PCD
21442 + */
21443 + uint8_t need_status_in_complete;
21444 +};
21445 +typedef struct cfiobject cfiobject_t;
21446 +
21447 +#define DUMP_MSG
21448 +
21449 +#if defined(DUMP_MSG)
21450 +static inline void dump_msg(const u8 * buf, unsigned int length)
21451 +{
21452 + unsigned int start, num, i;
21453 + char line[52], *p;
21454 +
21455 + if (length >= 512)
21456 + return;
21457 +
21458 + start = 0;
21459 + while (length > 0) {
21460 + num = min(length, 16u);
21461 + p = line;
21462 + for (i = 0; i < num; ++i) {
21463 + if (i == 8)
21464 + *p++ = ' ';
21465 + DWC_SPRINTF(p, " %02x", buf[i]);
21466 + p += 3;
21467 + }
21468 + *p = 0;
21469 + DWC_DEBUG("%6x: %s\n", start, line);
21470 + buf += num;
21471 + start += num;
21472 + length -= num;
21473 + }
21474 +}
21475 +#else
21476 +static inline void dump_msg(const u8 * buf, unsigned int length)
21477 +{
21478 +}
21479 +#endif
21480 +
21481 +/**
21482 + * This function returns a pointer to cfi_ep_t object with the addr address.
21483 + */
21484 +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
21485 + uint8_t addr)
21486 +{
21487 + struct cfi_ep *pcfiep;
21488 + dwc_list_link_t *tmp;
21489 +
21490 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
21491 + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
21492 +
21493 + if (pcfiep->ep->desc->bEndpointAddress == addr) {
21494 + return pcfiep;
21495 + }
21496 + }
21497 +
21498 + return NULL;
21499 +}
21500 +
21501 +/**
21502 + * This function returns a pointer to cfi_ep_t object that matches
21503 + * the dwc_otg_pcd_ep object.
21504 + */
21505 +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
21506 + struct dwc_otg_pcd_ep *ep)
21507 +{
21508 + struct cfi_ep *pcfiep = NULL;
21509 + dwc_list_link_t *tmp;
21510 +
21511 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
21512 + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
21513 + if (pcfiep->ep == ep) {
21514 + return pcfiep;
21515 + }
21516 + }
21517 + return NULL;
21518 +}
21519 +
21520 +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
21521 +
21522 +#endif /* (__DWC_OTG_CFI_H__) */
21523 --- /dev/null
21524 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
21525 @@ -0,0 +1,7141 @@
21526 +/* ==========================================================================
21527 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
21528 + * $Revision: #191 $
21529 + * $Date: 2012/08/10 $
21530 + * $Change: 2047372 $
21531 + *
21532 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
21533 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
21534 + * otherwise expressly agreed to in writing between Synopsys and you.
21535 + *
21536 + * The Software IS NOT an item of Licensed Software or Licensed Product under
21537 + * any End User Software License Agreement or Agreement for Licensed Product
21538 + * with Synopsys or any supplement thereto. You are permitted to use and
21539 + * redistribute this Software in source and binary forms, with or without
21540 + * modification, provided that redistributions of source code must retain this
21541 + * notice. You may not view, use, disclose, copy or distribute this file or
21542 + * any information contained herein except pursuant to this license grant from
21543 + * Synopsys. If you do not agree with this notice, including the disclaimer
21544 + * below, then you are not authorized to use the Software.
21545 + *
21546 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
21547 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21548 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21549 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
21550 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21551 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21552 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21553 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21554 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21555 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
21556 + * DAMAGE.
21557 + * ========================================================================== */
21558 +
21559 +/** @file
21560 + *
21561 + * The Core Interface Layer provides basic services for accessing and
21562 + * managing the DWC_otg hardware. These services are used by both the
21563 + * Host Controller Driver and the Peripheral Controller Driver.
21564 + *
21565 + * The CIL manages the memory map for the core so that the HCD and PCD
21566 + * don't have to do this separately. It also handles basic tasks like
21567 + * reading/writing the registers and data FIFOs in the controller.
21568 + * Some of the data access functions provide encapsulation of several
21569 + * operations required to perform a task, such as writing multiple
21570 + * registers to start a transfer. Finally, the CIL performs basic
21571 + * services that are not specific to either the host or device modes
21572 + * of operation. These services include management of the OTG Host
21573 + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
21574 + * Diagnostic API is also provided to allow testing of the controller
21575 + * hardware.
21576 + *
21577 + * The Core Interface Layer has the following requirements:
21578 + * - Provides basic controller operations.
21579 + * - Minimal use of OS services.
21580 + * - The OS services used will be abstracted by using inline functions
21581 + * or macros.
21582 + *
21583 + */
21584 +
21585 +#include "dwc_os.h"
21586 +#include "dwc_otg_regs.h"
21587 +#include "dwc_otg_cil.h"
21588 +
21589 +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
21590 +
21591 +/**
21592 + * This function is called to initialize the DWC_otg CSR data
21593 + * structures. The register addresses in the device and host
21594 + * structures are initialized from the base address supplied by the
21595 + * caller. The calling function must make the OS calls to get the
21596 + * base address of the DWC_otg controller registers. The core_params
21597 + * argument holds the parameters that specify how the core should be
21598 + * configured.
21599 + *
21600 + * @param reg_base_addr Base address of DWC_otg core registers
21601 + *
21602 + */
21603 +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
21604 +{
21605 + dwc_otg_core_if_t *core_if = 0;
21606 + dwc_otg_dev_if_t *dev_if = 0;
21607 + dwc_otg_host_if_t *host_if = 0;
21608 + uint8_t *reg_base = (uint8_t *) reg_base_addr;
21609 + int i = 0;
21610 +
21611 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
21612 +
21613 + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
21614 +
21615 + if (core_if == NULL) {
21616 + DWC_DEBUGPL(DBG_CIL,
21617 + "Allocation of dwc_otg_core_if_t failed\n");
21618 + return 0;
21619 + }
21620 + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
21621 +
21622 + /*
21623 + * Allocate the Device Mode structures.
21624 + */
21625 + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
21626 +
21627 + if (dev_if == NULL) {
21628 + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
21629 + DWC_FREE(core_if);
21630 + return 0;
21631 + }
21632 +
21633 + dev_if->dev_global_regs =
21634 + (dwc_otg_device_global_regs_t *) (reg_base +
21635 + DWC_DEV_GLOBAL_REG_OFFSET);
21636 +
21637 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21638 + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
21639 + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
21640 + (i * DWC_EP_REG_OFFSET));
21641 +
21642 + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
21643 + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
21644 + (i * DWC_EP_REG_OFFSET));
21645 + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
21646 + i, &dev_if->in_ep_regs[i]->diepctl);
21647 + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
21648 + i, &dev_if->out_ep_regs[i]->doepctl);
21649 + }
21650 +
21651 + dev_if->speed = 0; // unknown
21652 +
21653 + core_if->dev_if = dev_if;
21654 +
21655 + /*
21656 + * Allocate the Host Mode structures.
21657 + */
21658 + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
21659 +
21660 + if (host_if == NULL) {
21661 + DWC_DEBUGPL(DBG_CIL,
21662 + "Allocation of dwc_otg_host_if_t failed\n");
21663 + DWC_FREE(dev_if);
21664 + DWC_FREE(core_if);
21665 + return 0;
21666 + }
21667 +
21668 + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
21669 + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
21670 +
21671 + host_if->hprt0 =
21672 + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
21673 +
21674 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21675 + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
21676 + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
21677 + (i * DWC_OTG_CHAN_REGS_OFFSET));
21678 + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
21679 + i, &host_if->hc_regs[i]->hcchar);
21680 + }
21681 +
21682 + host_if->num_host_channels = MAX_EPS_CHANNELS;
21683 + core_if->host_if = host_if;
21684 +
21685 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21686 + core_if->data_fifo[i] =
21687 + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
21688 + (i * DWC_OTG_DATA_FIFO_SIZE));
21689 + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
21690 + i, (unsigned long)core_if->data_fifo[i]);
21691 + }
21692 +
21693 + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
21694 +
21695 + /* Initiate lx_state to L3 disconnected state */
21696 + core_if->lx_state = DWC_OTG_L3;
21697 + /*
21698 + * Store the contents of the hardware configuration registers here for
21699 + * easy access later.
21700 + */
21701 + core_if->hwcfg1.d32 =
21702 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
21703 + core_if->hwcfg2.d32 =
21704 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
21705 + core_if->hwcfg3.d32 =
21706 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
21707 + core_if->hwcfg4.d32 =
21708 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
21709 +
21710 + /* Force host mode to get HPTXFSIZ exact power on value */
21711 + {
21712 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
21713 + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
21714 + gusbcfg.b.force_host_mode = 1;
21715 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
21716 + dwc_mdelay(100);
21717 + core_if->hptxfsiz.d32 =
21718 + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
21719 + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
21720 + gusbcfg.b.force_host_mode = 1;
21721 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
21722 + dwc_mdelay(100);
21723 + }
21724 +
21725 + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
21726 + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
21727 + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
21728 + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
21729 +
21730 + core_if->hcfg.d32 =
21731 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
21732 + core_if->dcfg.d32 =
21733 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
21734 +
21735 + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
21736 + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
21737 +
21738 + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
21739 + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
21740 + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
21741 + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
21742 + core_if->hwcfg2.b.num_host_chan);
21743 + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
21744 + core_if->hwcfg2.b.nonperio_tx_q_depth);
21745 + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
21746 + core_if->hwcfg2.b.host_perio_tx_q_depth);
21747 + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
21748 + core_if->hwcfg2.b.dev_token_q_depth);
21749 +
21750 + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
21751 + core_if->hwcfg3.b.dfifo_depth);
21752 + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
21753 + core_if->hwcfg3.b.xfer_size_cntr_width);
21754 +
21755 + /*
21756 + * Set the SRP sucess bit for FS-I2c
21757 + */
21758 + core_if->srp_success = 0;
21759 + core_if->srp_timer_started = 0;
21760 +
21761 + /*
21762 + * Create new workqueue and init works
21763 + */
21764 + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
21765 + if (core_if->wq_otg == 0) {
21766 + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
21767 + DWC_FREE(host_if);
21768 + DWC_FREE(dev_if);
21769 + DWC_FREE(core_if);
21770 + return 0;
21771 + }
21772 +
21773 + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
21774 +
21775 + DWC_PRINTF("Core Release: %x.%x%x%x\n",
21776 + (core_if->snpsid >> 12 & 0xF),
21777 + (core_if->snpsid >> 8 & 0xF),
21778 + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
21779 +
21780 + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
21781 + w_wakeup_detected, core_if);
21782 + if (core_if->wkp_timer == 0) {
21783 + DWC_WARN("DWC_TIMER_ALLOC failed\n");
21784 + DWC_FREE(host_if);
21785 + DWC_FREE(dev_if);
21786 + DWC_WORKQ_FREE(core_if->wq_otg);
21787 + DWC_FREE(core_if);
21788 + return 0;
21789 + }
21790 +
21791 + if (dwc_otg_setup_params(core_if)) {
21792 + DWC_WARN("Error while setting core params\n");
21793 + }
21794 +
21795 + core_if->hibernation_suspend = 0;
21796 +
21797 + /** ADP initialization */
21798 + dwc_otg_adp_init(core_if);
21799 +
21800 + return core_if;
21801 +}
21802 +
21803 +/**
21804 + * This function frees the structures allocated by dwc_otg_cil_init().
21805 + *
21806 + * @param core_if The core interface pointer returned from
21807 + * dwc_otg_cil_init().
21808 + *
21809 + */
21810 +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
21811 +{
21812 + dctl_data_t dctl = {.d32 = 0 };
21813 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
21814 +
21815 + /* Disable all interrupts */
21816 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
21817 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
21818 +
21819 + dctl.b.sftdiscon = 1;
21820 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
21821 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
21822 + dctl.d32);
21823 + }
21824 +
21825 + if (core_if->wq_otg) {
21826 + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
21827 + DWC_WORKQ_FREE(core_if->wq_otg);
21828 + }
21829 + if (core_if->dev_if) {
21830 + DWC_FREE(core_if->dev_if);
21831 + }
21832 + if (core_if->host_if) {
21833 + DWC_FREE(core_if->host_if);
21834 + }
21835 +
21836 + /** Remove ADP Stuff */
21837 + dwc_otg_adp_remove(core_if);
21838 + if (core_if->core_params) {
21839 + DWC_FREE(core_if->core_params);
21840 + }
21841 + if (core_if->wkp_timer) {
21842 + DWC_TIMER_FREE(core_if->wkp_timer);
21843 + }
21844 + if (core_if->srp_timer) {
21845 + DWC_TIMER_FREE(core_if->srp_timer);
21846 + }
21847 + DWC_FREE(core_if);
21848 +}
21849 +
21850 +/**
21851 + * This function enables the controller's Global Interrupt in the AHB Config
21852 + * register.
21853 + *
21854 + * @param core_if Programming view of DWC_otg controller.
21855 + */
21856 +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
21857 +{
21858 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
21859 + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
21860 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
21861 +}
21862 +
21863 +/**
21864 + * This function disables the controller's Global Interrupt in the AHB Config
21865 + * register.
21866 + *
21867 + * @param core_if Programming view of DWC_otg controller.
21868 + */
21869 +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
21870 +{
21871 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
21872 + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
21873 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
21874 +}
21875 +
21876 +/**
21877 + * This function initializes the commmon interrupts, used in both
21878 + * device and host modes.
21879 + *
21880 + * @param core_if Programming view of the DWC_otg controller
21881 + *
21882 + */
21883 +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
21884 +{
21885 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
21886 + gintmsk_data_t intr_mask = {.d32 = 0 };
21887 +
21888 + /* Clear any pending OTG Interrupts */
21889 + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
21890 +
21891 + /* Clear any pending interrupts */
21892 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
21893 +
21894 + /*
21895 + * Enable the interrupts in the GINTMSK.
21896 + */
21897 + intr_mask.b.modemismatch = 1;
21898 + intr_mask.b.otgintr = 1;
21899 +
21900 + if (!core_if->dma_enable) {
21901 + intr_mask.b.rxstsqlvl = 1;
21902 + }
21903 +
21904 + intr_mask.b.conidstschng = 1;
21905 + intr_mask.b.wkupintr = 1;
21906 + intr_mask.b.disconnect = 0;
21907 + intr_mask.b.usbsuspend = 1;
21908 + intr_mask.b.sessreqintr = 1;
21909 +#ifdef CONFIG_USB_DWC_OTG_LPM
21910 + if (core_if->core_params->lpm_enable) {
21911 + intr_mask.b.lpmtranrcvd = 1;
21912 + }
21913 +#endif
21914 + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
21915 +}
21916 +
21917 +/*
21918 + * The restore operation is modified to support Synopsys Emulated Powerdown and
21919 + * Hibernation. This function is for exiting from Device mode hibernation by
21920 + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
21921 + * @param core_if Programming view of DWC_otg controller.
21922 + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
21923 + * @param reset - indicates whether resume is initiated by Reset.
21924 + */
21925 +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
21926 + int rem_wakeup, int reset)
21927 +{
21928 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
21929 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
21930 + dctl_data_t dctl = {.d32 = 0 };
21931 +
21932 + int timeout = 2000;
21933 +
21934 + if (!core_if->hibernation_suspend) {
21935 + DWC_PRINTF("Already exited from Hibernation\n");
21936 + return 1;
21937 + }
21938 +
21939 + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
21940 + /* Switch-on voltage to the core */
21941 + gpwrdn.b.pwrdnswtch = 1;
21942 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21943 + dwc_udelay(10);
21944 +
21945 + /* Reset core */
21946 + gpwrdn.d32 = 0;
21947 + gpwrdn.b.pwrdnrstn = 1;
21948 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21949 + dwc_udelay(10);
21950 +
21951 + /* Assert Restore signal */
21952 + gpwrdn.d32 = 0;
21953 + gpwrdn.b.restore = 1;
21954 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
21955 + dwc_udelay(10);
21956 +
21957 + /* Disable power clamps */
21958 + gpwrdn.d32 = 0;
21959 + gpwrdn.b.pwrdnclmp = 1;
21960 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21961 +
21962 + if (rem_wakeup) {
21963 + dwc_udelay(70);
21964 + }
21965 +
21966 + /* Deassert Reset core */
21967 + gpwrdn.d32 = 0;
21968 + gpwrdn.b.pwrdnrstn = 1;
21969 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
21970 + dwc_udelay(10);
21971 +
21972 + /* Disable PMU interrupt */
21973 + gpwrdn.d32 = 0;
21974 + gpwrdn.b.pmuintsel = 1;
21975 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21976 +
21977 + /* Mask interrupts from gpwrdn */
21978 + gpwrdn.d32 = 0;
21979 + gpwrdn.b.connect_det_msk = 1;
21980 + gpwrdn.b.srp_det_msk = 1;
21981 + gpwrdn.b.disconn_det_msk = 1;
21982 + gpwrdn.b.rst_det_msk = 1;
21983 + gpwrdn.b.lnstchng_msk = 1;
21984 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21985 +
21986 + /* Indicates that we are going out from hibernation */
21987 + core_if->hibernation_suspend = 0;
21988 +
21989 + /*
21990 + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
21991 + * indicates restore from remote_wakeup
21992 + */
21993 + restore_essential_regs(core_if, rem_wakeup, 0);
21994 +
21995 + /*
21996 + * Wait a little for seeing new value of variable hibernation_suspend if
21997 + * Restore done interrupt received before polling
21998 + */
21999 + dwc_udelay(10);
22000 +
22001 + if (core_if->hibernation_suspend == 0) {
22002 + /*
22003 + * Wait For Restore_done Interrupt. This mechanism of polling the
22004 + * interrupt is introduced to avoid any possible race conditions
22005 + */
22006 + do {
22007 + gintsts_data_t gintsts;
22008 + gintsts.d32 =
22009 + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
22010 + if (gintsts.b.restoredone) {
22011 + gintsts.d32 = 0;
22012 + gintsts.b.restoredone = 1;
22013 + DWC_WRITE_REG32(&core_if->core_global_regs->
22014 + gintsts, gintsts.d32);
22015 + DWC_PRINTF("Restore Done Interrupt seen\n");
22016 + break;
22017 + }
22018 + dwc_udelay(10);
22019 + } while (--timeout);
22020 + if (!timeout) {
22021 + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
22022 + }
22023 + }
22024 + /* Clear all pending interupts */
22025 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22026 +
22027 + /* De-assert Restore */
22028 + gpwrdn.d32 = 0;
22029 + gpwrdn.b.restore = 1;
22030 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22031 + dwc_udelay(10);
22032 +
22033 + if (!rem_wakeup) {
22034 + pcgcctl.d32 = 0;
22035 + pcgcctl.b.rstpdwnmodule = 1;
22036 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
22037 + }
22038 +
22039 + /* Restore GUSBCFG and DCFG */
22040 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
22041 + core_if->gr_backup->gusbcfg_local);
22042 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
22043 + core_if->dr_backup->dcfg);
22044 +
22045 + /* De-assert Wakeup Logic */
22046 + gpwrdn.d32 = 0;
22047 + gpwrdn.b.pmuactv = 1;
22048 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22049 + dwc_udelay(10);
22050 +
22051 + if (!rem_wakeup) {
22052 + /* Set Device programming done bit */
22053 + dctl.b.pwronprgdone = 1;
22054 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
22055 + } else {
22056 + /* Start Remote Wakeup Signaling */
22057 + dctl.d32 = core_if->dr_backup->dctl;
22058 + dctl.b.rmtwkupsig = 1;
22059 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
22060 + }
22061 +
22062 + dwc_mdelay(2);
22063 + /* Clear all pending interupts */
22064 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22065 +
22066 + /* Restore global registers */
22067 + dwc_otg_restore_global_regs(core_if);
22068 + /* Restore device global registers */
22069 + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
22070 +
22071 + if (rem_wakeup) {
22072 + dwc_mdelay(7);
22073 + dctl.d32 = 0;
22074 + dctl.b.rmtwkupsig = 1;
22075 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
22076 + }
22077 +
22078 + core_if->hibernation_suspend = 0;
22079 + /* The core will be in ON STATE */
22080 + core_if->lx_state = DWC_OTG_L0;
22081 + DWC_PRINTF("Hibernation recovery completes here\n");
22082 +
22083 + return 1;
22084 +}
22085 +
22086 +/*
22087 + * The restore operation is modified to support Synopsys Emulated Powerdown and
22088 + * Hibernation. This function is for exiting from Host mode hibernation by
22089 + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
22090 + * @param core_if Programming view of DWC_otg controller.
22091 + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
22092 + * @param reset - indicates whether resume is initiated by Reset.
22093 + */
22094 +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
22095 + int rem_wakeup, int reset)
22096 +{
22097 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
22098 + hprt0_data_t hprt0 = {.d32 = 0 };
22099 +
22100 + int timeout = 2000;
22101 +
22102 + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
22103 + /* Switch-on voltage to the core */
22104 + gpwrdn.b.pwrdnswtch = 1;
22105 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22106 + dwc_udelay(10);
22107 +
22108 + /* Reset core */
22109 + gpwrdn.d32 = 0;
22110 + gpwrdn.b.pwrdnrstn = 1;
22111 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22112 + dwc_udelay(10);
22113 +
22114 + /* Assert Restore signal */
22115 + gpwrdn.d32 = 0;
22116 + gpwrdn.b.restore = 1;
22117 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22118 + dwc_udelay(10);
22119 +
22120 + /* Disable power clamps */
22121 + gpwrdn.d32 = 0;
22122 + gpwrdn.b.pwrdnclmp = 1;
22123 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22124 +
22125 + if (!rem_wakeup) {
22126 + dwc_udelay(50);
22127 + }
22128 +
22129 + /* Deassert Reset core */
22130 + gpwrdn.d32 = 0;
22131 + gpwrdn.b.pwrdnrstn = 1;
22132 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22133 + dwc_udelay(10);
22134 +
22135 + /* Disable PMU interrupt */
22136 + gpwrdn.d32 = 0;
22137 + gpwrdn.b.pmuintsel = 1;
22138 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22139 +
22140 + gpwrdn.d32 = 0;
22141 + gpwrdn.b.connect_det_msk = 1;
22142 + gpwrdn.b.srp_det_msk = 1;
22143 + gpwrdn.b.disconn_det_msk = 1;
22144 + gpwrdn.b.rst_det_msk = 1;
22145 + gpwrdn.b.lnstchng_msk = 1;
22146 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22147 +
22148 + /* Indicates that we are going out from hibernation */
22149 + core_if->hibernation_suspend = 0;
22150 +
22151 + /* Set Restore Essential Regs bit in PCGCCTL register */
22152 + restore_essential_regs(core_if, rem_wakeup, 1);
22153 +
22154 + /* Wait a little for seeing new value of variable hibernation_suspend if
22155 + * Restore done interrupt received before polling */
22156 + dwc_udelay(10);
22157 +
22158 + if (core_if->hibernation_suspend == 0) {
22159 + /* Wait For Restore_done Interrupt. This mechanism of polling the
22160 + * interrupt is introduced to avoid any possible race conditions
22161 + */
22162 + do {
22163 + gintsts_data_t gintsts;
22164 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
22165 + if (gintsts.b.restoredone) {
22166 + gintsts.d32 = 0;
22167 + gintsts.b.restoredone = 1;
22168 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
22169 + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
22170 + break;
22171 + }
22172 + dwc_udelay(10);
22173 + } while (--timeout);
22174 + if (!timeout) {
22175 + DWC_WARN("Restore Done interrupt wasn't generated\n");
22176 + }
22177 + }
22178 +
22179 + /* Set the flag's value to 0 again after receiving restore done interrupt */
22180 + core_if->hibernation_suspend = 0;
22181 +
22182 + /* This step is not described in functional spec but if not wait for this
22183 + * delay, mismatch interrupts occurred because just after restore core is
22184 + * in Device mode(gintsts.curmode == 0) */
22185 + dwc_mdelay(100);
22186 +
22187 + /* Clear all pending interrupts */
22188 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22189 +
22190 + /* De-assert Restore */
22191 + gpwrdn.d32 = 0;
22192 + gpwrdn.b.restore = 1;
22193 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22194 + dwc_udelay(10);
22195 +
22196 + /* Restore GUSBCFG and HCFG */
22197 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
22198 + core_if->gr_backup->gusbcfg_local);
22199 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
22200 + core_if->hr_backup->hcfg_local);
22201 +
22202 + /* De-assert Wakeup Logic */
22203 + gpwrdn.d32 = 0;
22204 + gpwrdn.b.pmuactv = 1;
22205 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22206 + dwc_udelay(10);
22207 +
22208 + /* Start the Resume operation by programming HPRT0 */
22209 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22210 + hprt0.b.prtpwr = 1;
22211 + hprt0.b.prtena = 0;
22212 + hprt0.b.prtsusp = 0;
22213 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22214 +
22215 + DWC_PRINTF("Resume Starts Now\n");
22216 + if (!reset) { // Indicates it is Resume Operation
22217 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22218 + hprt0.b.prtres = 1;
22219 + hprt0.b.prtpwr = 1;
22220 + hprt0.b.prtena = 0;
22221 + hprt0.b.prtsusp = 0;
22222 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22223 +
22224 + if (!rem_wakeup)
22225 + hprt0.b.prtres = 0;
22226 + /* Wait for Resume time and then program HPRT again */
22227 + dwc_mdelay(100);
22228 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22229 +
22230 + } else { // Indicates it is Reset Operation
22231 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22232 + hprt0.b.prtrst = 1;
22233 + hprt0.b.prtpwr = 1;
22234 + hprt0.b.prtena = 0;
22235 + hprt0.b.prtsusp = 0;
22236 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22237 + /* Wait for Reset time and then program HPRT again */
22238 + dwc_mdelay(60);
22239 + hprt0.b.prtrst = 0;
22240 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22241 + }
22242 + /* Clear all interrupt status */
22243 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
22244 + hprt0.b.prtconndet = 1;
22245 + hprt0.b.prtenchng = 1;
22246 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22247 +
22248 + /* Clear all pending interupts */
22249 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22250 +
22251 + /* Restore global registers */
22252 + dwc_otg_restore_global_regs(core_if);
22253 + /* Restore host global registers */
22254 + dwc_otg_restore_host_regs(core_if, reset);
22255 +
22256 + /* The core will be in ON STATE */
22257 + core_if->lx_state = DWC_OTG_L0;
22258 + DWC_PRINTF("Hibernation recovery is complete here\n");
22259 + return 0;
22260 +}
22261 +
22262 +/** Saves some register values into system memory. */
22263 +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
22264 +{
22265 + struct dwc_otg_global_regs_backup *gr;
22266 + int i;
22267 +
22268 + gr = core_if->gr_backup;
22269 + if (!gr) {
22270 + gr = DWC_ALLOC(sizeof(*gr));
22271 + if (!gr) {
22272 + return -DWC_E_NO_MEMORY;
22273 + }
22274 + core_if->gr_backup = gr;
22275 + }
22276 +
22277 + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
22278 + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
22279 + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
22280 + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
22281 + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
22282 + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
22283 + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
22284 +#ifdef CONFIG_USB_DWC_OTG_LPM
22285 + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
22286 +#endif
22287 + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
22288 + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
22289 + gr->gdfifocfg_local =
22290 + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
22291 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
22292 + gr->dtxfsiz_local[i] =
22293 + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
22294 + }
22295 +
22296 + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
22297 + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
22298 + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
22299 + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
22300 + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
22301 + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
22302 + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
22303 + gr->gnptxfsiz_local);
22304 + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
22305 + gr->hptxfsiz_local);
22306 +#ifdef CONFIG_USB_DWC_OTG_LPM
22307 + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
22308 +#endif
22309 + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
22310 + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
22311 + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
22312 +
22313 + return 0;
22314 +}
22315 +
22316 +/** Saves GINTMSK register before setting the msk bits. */
22317 +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
22318 +{
22319 + struct dwc_otg_global_regs_backup *gr;
22320 +
22321 + gr = core_if->gr_backup;
22322 + if (!gr) {
22323 + gr = DWC_ALLOC(sizeof(*gr));
22324 + if (!gr) {
22325 + return -DWC_E_NO_MEMORY;
22326 + }
22327 + core_if->gr_backup = gr;
22328 + }
22329 +
22330 + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
22331 +
22332 + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
22333 + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
22334 +
22335 + return 0;
22336 +}
22337 +
22338 +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
22339 +{
22340 + struct dwc_otg_dev_regs_backup *dr;
22341 + int i;
22342 +
22343 + dr = core_if->dr_backup;
22344 + if (!dr) {
22345 + dr = DWC_ALLOC(sizeof(*dr));
22346 + if (!dr) {
22347 + return -DWC_E_NO_MEMORY;
22348 + }
22349 + core_if->dr_backup = dr;
22350 + }
22351 +
22352 + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
22353 + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
22354 + dr->daintmsk =
22355 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
22356 + dr->diepmsk =
22357 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
22358 + dr->doepmsk =
22359 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
22360 +
22361 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22362 + dr->diepctl[i] =
22363 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
22364 + dr->dieptsiz[i] =
22365 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
22366 + dr->diepdma[i] =
22367 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
22368 + }
22369 +
22370 + DWC_DEBUGPL(DBG_ANY,
22371 + "=============Backing Host registers==============\n");
22372 + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
22373 + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
22374 + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
22375 + dr->daintmsk);
22376 + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
22377 + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
22378 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22379 + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
22380 + dr->diepctl[i]);
22381 + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
22382 + i, dr->dieptsiz[i]);
22383 + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
22384 + dr->diepdma[i]);
22385 + }
22386 +
22387 + return 0;
22388 +}
22389 +
22390 +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
22391 +{
22392 + struct dwc_otg_host_regs_backup *hr;
22393 + int i;
22394 +
22395 + hr = core_if->hr_backup;
22396 + if (!hr) {
22397 + hr = DWC_ALLOC(sizeof(*hr));
22398 + if (!hr) {
22399 + return -DWC_E_NO_MEMORY;
22400 + }
22401 + core_if->hr_backup = hr;
22402 + }
22403 +
22404 + hr->hcfg_local =
22405 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
22406 + hr->haintmsk_local =
22407 + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
22408 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22409 + hr->hcintmsk_local[i] =
22410 + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
22411 + }
22412 + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
22413 + hr->hfir_local =
22414 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
22415 +
22416 + DWC_DEBUGPL(DBG_ANY,
22417 + "=============Backing Host registers===============\n");
22418 + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
22419 + hr->hcfg_local);
22420 + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
22421 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22422 + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
22423 + hr->hcintmsk_local[i]);
22424 + }
22425 + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
22426 + hr->hprt0_local);
22427 + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
22428 + hr->hfir_local);
22429 +
22430 + return 0;
22431 +}
22432 +
22433 +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
22434 +{
22435 + struct dwc_otg_global_regs_backup *gr;
22436 + int i;
22437 +
22438 + gr = core_if->gr_backup;
22439 + if (!gr) {
22440 + return -DWC_E_INVALID;
22441 + }
22442 +
22443 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
22444 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
22445 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
22446 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
22447 + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
22448 + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
22449 + gr->gnptxfsiz_local);
22450 + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
22451 + gr->hptxfsiz_local);
22452 + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
22453 + gr->gdfifocfg_local);
22454 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
22455 + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
22456 + gr->dtxfsiz_local[i]);
22457 + }
22458 +
22459 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22460 + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
22461 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
22462 + (gr->gahbcfg_local));
22463 + return 0;
22464 +}
22465 +
22466 +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
22467 +{
22468 + struct dwc_otg_dev_regs_backup *dr;
22469 + int i;
22470 +
22471 + dr = core_if->dr_backup;
22472 +
22473 + if (!dr) {
22474 + return -DWC_E_INVALID;
22475 + }
22476 +
22477 + if (!rem_wakeup) {
22478 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
22479 + dr->dctl);
22480 + }
22481 +
22482 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
22483 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
22484 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
22485 +
22486 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22487 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
22488 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
22489 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
22490 + }
22491 +
22492 + return 0;
22493 +}
22494 +
22495 +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
22496 +{
22497 + struct dwc_otg_host_regs_backup *hr;
22498 + int i;
22499 + hr = core_if->hr_backup;
22500 +
22501 + if (!hr) {
22502 + return -DWC_E_INVALID;
22503 + }
22504 +
22505 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
22506 + //if (!reset)
22507 + //{
22508 + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
22509 + //}
22510 +
22511 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
22512 + hr->haintmsk_local);
22513 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22514 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
22515 + hr->hcintmsk_local[i]);
22516 + }
22517 +
22518 + return 0;
22519 +}
22520 +
22521 +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
22522 +{
22523 + struct dwc_otg_global_regs_backup *gr;
22524 +
22525 + gr = core_if->gr_backup;
22526 +
22527 + /* Restore values for LPM and I2C */
22528 +#ifdef CONFIG_USB_DWC_OTG_LPM
22529 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
22530 +#endif
22531 + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
22532 +
22533 + return 0;
22534 +}
22535 +
22536 +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
22537 +{
22538 + struct dwc_otg_global_regs_backup *gr;
22539 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
22540 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
22541 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
22542 + gintmsk_data_t gintmsk = {.d32 = 0 };
22543 +
22544 + /* Restore LPM and I2C registers */
22545 + restore_lpm_i2c_regs(core_if);
22546 +
22547 + /* Set PCGCCTL to 0 */
22548 + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
22549 +
22550 + gr = core_if->gr_backup;
22551 + /* Load restore values for [31:14] bits */
22552 + DWC_WRITE_REG32(core_if->pcgcctl,
22553 + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
22554 +
22555 + /* Umnask global Interrupt in GAHBCFG and restore it */
22556 + gahbcfg.d32 = gr->gahbcfg_local;
22557 + gahbcfg.b.glblintrmsk = 1;
22558 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
22559 +
22560 + /* Clear all pending interupts */
22561 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22562 +
22563 + /* Unmask restore done interrupt */
22564 + gintmsk.b.restoredone = 1;
22565 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
22566 +
22567 + /* Restore GUSBCFG and HCFG/DCFG */
22568 + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
22569 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
22570 +
22571 + if (is_host) {
22572 + hcfg_data_t hcfg = {.d32 = 0 };
22573 + hcfg.d32 = core_if->hr_backup->hcfg_local;
22574 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
22575 + hcfg.d32);
22576 +
22577 + /* Load restore values for [31:14] bits */
22578 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22579 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22580 +
22581 + if (rmode)
22582 + pcgcctl.b.restoremode = 1;
22583 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22584 + dwc_udelay(10);
22585 +
22586 + /* Load restore values for [31:14] bits and set EssRegRestored bit */
22587 + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
22588 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22589 + pcgcctl.b.ess_reg_restored = 1;
22590 + if (rmode)
22591 + pcgcctl.b.restoremode = 1;
22592 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22593 + } else {
22594 + dcfg_data_t dcfg = {.d32 = 0 };
22595 + dcfg.d32 = core_if->dr_backup->dcfg;
22596 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
22597 +
22598 + /* Load restore values for [31:14] bits */
22599 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22600 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22601 + if (!rmode) {
22602 + pcgcctl.d32 |= 0x208;
22603 + }
22604 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22605 + dwc_udelay(10);
22606 +
22607 + /* Load restore values for [31:14] bits */
22608 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22609 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22610 + pcgcctl.b.ess_reg_restored = 1;
22611 + if (!rmode)
22612 + pcgcctl.d32 |= 0x208;
22613 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22614 + }
22615 +
22616 + return 0;
22617 +}
22618 +
22619 +/**
22620 + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
22621 + * type.
22622 + */
22623 +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
22624 +{
22625 + uint32_t val;
22626 + hcfg_data_t hcfg;
22627 +
22628 + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
22629 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22630 + (core_if->core_params->ulpi_fs_ls)) ||
22631 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22632 + /* Full speed PHY */
22633 + val = DWC_HCFG_48_MHZ;
22634 + } else {
22635 + /* High speed PHY running at full speed or high speed */
22636 + val = DWC_HCFG_30_60_MHZ;
22637 + }
22638 +
22639 + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
22640 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
22641 + hcfg.b.fslspclksel = val;
22642 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
22643 +}
22644 +
22645 +/**
22646 + * Initializes the DevSpd field of the DCFG register depending on the PHY type
22647 + * and the enumeration speed of the device.
22648 + */
22649 +static void init_devspd(dwc_otg_core_if_t * core_if)
22650 +{
22651 + uint32_t val;
22652 + dcfg_data_t dcfg;
22653 +
22654 + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
22655 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22656 + (core_if->core_params->ulpi_fs_ls)) ||
22657 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22658 + /* Full speed PHY */
22659 + val = 0x3;
22660 + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
22661 + /* High speed PHY running at full speed */
22662 + val = 0x1;
22663 + } else {
22664 + /* High speed PHY running at high speed */
22665 + val = 0x0;
22666 + }
22667 +
22668 + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
22669 +
22670 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
22671 + dcfg.b.devspd = val;
22672 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
22673 +}
22674 +
22675 +/**
22676 + * This function calculates the number of IN EPS
22677 + * using GHWCFG1 and GHWCFG2 registers values
22678 + *
22679 + * @param core_if Programming view of the DWC_otg controller
22680 + */
22681 +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
22682 +{
22683 + uint32_t num_in_eps = 0;
22684 + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
22685 + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
22686 + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
22687 + int i;
22688 +
22689 + for (i = 0; i < num_eps; ++i) {
22690 + if (!(hwcfg1 & 0x1))
22691 + num_in_eps++;
22692 +
22693 + hwcfg1 >>= 2;
22694 + }
22695 +
22696 + if (core_if->hwcfg4.b.ded_fifo_en) {
22697 + num_in_eps =
22698 + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
22699 + }
22700 +
22701 + return num_in_eps;
22702 +}
22703 +
22704 +/**
22705 + * This function calculates the number of OUT EPS
22706 + * using GHWCFG1 and GHWCFG2 registers values
22707 + *
22708 + * @param core_if Programming view of the DWC_otg controller
22709 + */
22710 +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
22711 +{
22712 + uint32_t num_out_eps = 0;
22713 + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
22714 + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
22715 + int i;
22716 +
22717 + for (i = 0; i < num_eps; ++i) {
22718 + if (!(hwcfg1 & 0x1))
22719 + num_out_eps++;
22720 +
22721 + hwcfg1 >>= 2;
22722 + }
22723 + return num_out_eps;
22724 +}
22725 +
22726 +/**
22727 + * This function initializes the DWC_otg controller registers and
22728 + * prepares the core for device mode or host mode operation.
22729 + *
22730 + * @param core_if Programming view of the DWC_otg controller
22731 + *
22732 + */
22733 +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
22734 +{
22735 + int i = 0;
22736 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
22737 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
22738 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
22739 + gusbcfg_data_t usbcfg = {.d32 = 0 };
22740 + gi2cctl_data_t i2cctl = {.d32 = 0 };
22741 +
22742 + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
22743 + core_if, global_regs);
22744 +
22745 + /* Common Initialization */
22746 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22747 +
22748 + /* Program the ULPI External VBUS bit if needed */
22749 + usbcfg.b.ulpi_ext_vbus_drv =
22750 + (core_if->core_params->phy_ulpi_ext_vbus ==
22751 + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
22752 +
22753 + /* Set external TS Dline pulsing */
22754 + usbcfg.b.term_sel_dl_pulse =
22755 + (core_if->core_params->ts_dline == 1) ? 1 : 0;
22756 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22757 +
22758 + /* Reset the Controller */
22759 + dwc_otg_core_reset(core_if);
22760 +
22761 + core_if->adp_enable = core_if->core_params->adp_supp_enable;
22762 + core_if->power_down = core_if->core_params->power_down;
22763 + core_if->otg_sts = 0;
22764 +
22765 + /* Initialize parameters from Hardware configuration registers. */
22766 + dev_if->num_in_eps = calc_num_in_eps(core_if);
22767 + dev_if->num_out_eps = calc_num_out_eps(core_if);
22768 +
22769 + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
22770 + core_if->hwcfg4.b.num_dev_perio_in_ep);
22771 +
22772 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
22773 + dev_if->perio_tx_fifo_size[i] =
22774 + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
22775 + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
22776 + i, dev_if->perio_tx_fifo_size[i]);
22777 + }
22778 +
22779 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
22780 + dev_if->tx_fifo_size[i] =
22781 + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
22782 + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
22783 + i, dev_if->tx_fifo_size[i]);
22784 + }
22785 +
22786 + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
22787 + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
22788 + core_if->nperio_tx_fifo_size =
22789 + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
22790 +
22791 + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
22792 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
22793 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
22794 + core_if->nperio_tx_fifo_size);
22795 +
22796 + /* This programming sequence needs to happen in FS mode before any other
22797 + * programming occurs */
22798 + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
22799 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22800 + /* If FS mode with FS PHY */
22801 +
22802 + /* core_init() is now called on every switch so only call the
22803 + * following for the first time through. */
22804 + if (!core_if->phy_init_done) {
22805 + core_if->phy_init_done = 1;
22806 + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
22807 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22808 + usbcfg.b.physel = 1;
22809 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22810 +
22811 + /* Reset after a PHY select */
22812 + dwc_otg_core_reset(core_if);
22813 + }
22814 +
22815 + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
22816 + * do this on HNP Dev/Host mode switches (done in dev_init and
22817 + * host_init). */
22818 + if (dwc_otg_is_host_mode(core_if)) {
22819 + init_fslspclksel(core_if);
22820 + } else {
22821 + init_devspd(core_if);
22822 + }
22823 +
22824 + if (core_if->core_params->i2c_enable) {
22825 + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
22826 + /* Program GUSBCFG.OtgUtmifsSel to I2C */
22827 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22828 + usbcfg.b.otgutmifssel = 1;
22829 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22830 +
22831 + /* Program GI2CCTL.I2CEn */
22832 + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
22833 + i2cctl.b.i2cdevaddr = 1;
22834 + i2cctl.b.i2cen = 0;
22835 + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
22836 + i2cctl.b.i2cen = 1;
22837 + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
22838 + }
22839 +
22840 + } /* endif speed == DWC_SPEED_PARAM_FULL */
22841 + else {
22842 + /* High speed PHY. */
22843 + if (!core_if->phy_init_done) {
22844 + core_if->phy_init_done = 1;
22845 + /* HS PHY parameters. These parameters are preserved
22846 + * during soft reset so only program the first time. Do
22847 + * a soft reset immediately after setting phyif. */
22848 +
22849 + if (core_if->core_params->phy_type == 2) {
22850 + /* ULPI interface */
22851 + usbcfg.b.ulpi_utmi_sel = 1;
22852 + usbcfg.b.phyif = 0;
22853 + usbcfg.b.ddrsel =
22854 + core_if->core_params->phy_ulpi_ddr;
22855 + } else if (core_if->core_params->phy_type == 1) {
22856 + /* UTMI+ interface */
22857 + usbcfg.b.ulpi_utmi_sel = 0;
22858 + if (core_if->core_params->phy_utmi_width == 16) {
22859 + usbcfg.b.phyif = 1;
22860 +
22861 + } else {
22862 + usbcfg.b.phyif = 0;
22863 + }
22864 + } else {
22865 + DWC_ERROR("FS PHY TYPE\n");
22866 + }
22867 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22868 + /* Reset after setting the PHY parameters */
22869 + dwc_otg_core_reset(core_if);
22870 + }
22871 + }
22872 +
22873 + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
22874 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22875 + (core_if->core_params->ulpi_fs_ls)) {
22876 + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
22877 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22878 + usbcfg.b.ulpi_fsls = 1;
22879 + usbcfg.b.ulpi_clk_sus_m = 1;
22880 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22881 + } else {
22882 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22883 + usbcfg.b.ulpi_fsls = 0;
22884 + usbcfg.b.ulpi_clk_sus_m = 0;
22885 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22886 + }
22887 +
22888 + /* Program the GAHBCFG Register. */
22889 + switch (core_if->hwcfg2.b.architecture) {
22890 +
22891 + case DWC_SLAVE_ONLY_ARCH:
22892 + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
22893 + ahbcfg.b.nptxfemplvl_txfemplvl =
22894 + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
22895 + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
22896 + core_if->dma_enable = 0;
22897 + core_if->dma_desc_enable = 0;
22898 + break;
22899 +
22900 + case DWC_EXT_DMA_ARCH:
22901 + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
22902 + {
22903 + uint8_t brst_sz = core_if->core_params->dma_burst_size;
22904 + ahbcfg.b.hburstlen = 0;
22905 + while (brst_sz > 1) {
22906 + ahbcfg.b.hburstlen++;
22907 + brst_sz >>= 1;
22908 + }
22909 + }
22910 + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
22911 + core_if->dma_desc_enable =
22912 + (core_if->core_params->dma_desc_enable != 0);
22913 + break;
22914 +
22915 + case DWC_INT_DMA_ARCH:
22916 + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
22917 + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
22918 + Host mode ISOC in issue fix - vahrama */
22919 + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
22920 + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
22921 + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
22922 + core_if->dma_desc_enable =
22923 + (core_if->core_params->dma_desc_enable != 0);
22924 + break;
22925 +
22926 + }
22927 + if (core_if->dma_enable) {
22928 + if (core_if->dma_desc_enable) {
22929 + DWC_PRINTF("Using Descriptor DMA mode\n");
22930 + } else {
22931 + DWC_PRINTF("Using Buffer DMA mode\n");
22932 +
22933 + }
22934 + } else {
22935 + DWC_PRINTF("Using Slave mode\n");
22936 + core_if->dma_desc_enable = 0;
22937 + }
22938 +
22939 + if (core_if->core_params->ahb_single) {
22940 + ahbcfg.b.ahbsingle = 1;
22941 + }
22942 +
22943 + ahbcfg.b.dmaenable = core_if->dma_enable;
22944 + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
22945 +
22946 + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
22947 +
22948 + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
22949 + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
22950 + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
22951 + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
22952 + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
22953 + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
22954 +
22955 + /*
22956 + * Program the GUSBCFG register.
22957 + */
22958 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22959 +
22960 + switch (core_if->hwcfg2.b.op_mode) {
22961 + case DWC_MODE_HNP_SRP_CAPABLE:
22962 + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
22963 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
22964 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
22965 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
22966 + break;
22967 +
22968 + case DWC_MODE_SRP_ONLY_CAPABLE:
22969 + usbcfg.b.hnpcap = 0;
22970 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
22971 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
22972 + break;
22973 +
22974 + case DWC_MODE_NO_HNP_SRP_CAPABLE:
22975 + usbcfg.b.hnpcap = 0;
22976 + usbcfg.b.srpcap = 0;
22977 + break;
22978 +
22979 + case DWC_MODE_SRP_CAPABLE_DEVICE:
22980 + usbcfg.b.hnpcap = 0;
22981 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
22982 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
22983 + break;
22984 +
22985 + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
22986 + usbcfg.b.hnpcap = 0;
22987 + usbcfg.b.srpcap = 0;
22988 + break;
22989 +
22990 + case DWC_MODE_SRP_CAPABLE_HOST:
22991 + usbcfg.b.hnpcap = 0;
22992 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
22993 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
22994 + break;
22995 +
22996 + case DWC_MODE_NO_SRP_CAPABLE_HOST:
22997 + usbcfg.b.hnpcap = 0;
22998 + usbcfg.b.srpcap = 0;
22999 + break;
23000 + }
23001 +
23002 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23003 +
23004 +#ifdef CONFIG_USB_DWC_OTG_LPM
23005 + if (core_if->core_params->lpm_enable) {
23006 + glpmcfg_data_t lpmcfg = {.d32 = 0 };
23007 +
23008 + /* To enable LPM support set lpm_cap_en bit */
23009 + lpmcfg.b.lpm_cap_en = 1;
23010 +
23011 + /* Make AppL1Res ACK */
23012 + lpmcfg.b.appl_resp = 1;
23013 +
23014 + /* Retry 3 times */
23015 + lpmcfg.b.retry_count = 3;
23016 +
23017 + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
23018 + 0, lpmcfg.d32);
23019 +
23020 + }
23021 +#endif
23022 + if (core_if->core_params->ic_usb_cap) {
23023 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
23024 + gusbcfg.b.ic_usb_cap = 1;
23025 + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
23026 + 0, gusbcfg.d32);
23027 + }
23028 + {
23029 + gotgctl_data_t gotgctl = {.d32 = 0 };
23030 + gotgctl.b.otgver = core_if->core_params->otg_ver;
23031 + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
23032 + gotgctl.d32);
23033 + /* Set OTG version supported */
23034 + core_if->otg_ver = core_if->core_params->otg_ver;
23035 + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
23036 + core_if->core_params->otg_ver, core_if->otg_ver);
23037 + }
23038 +
23039 +
23040 + /* Enable common interrupts */
23041 + dwc_otg_enable_common_interrupts(core_if);
23042 +
23043 + /* Do device or host intialization based on mode during PCD
23044 + * and HCD initialization */
23045 + if (dwc_otg_is_host_mode(core_if)) {
23046 + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
23047 + core_if->op_state = A_HOST;
23048 + } else {
23049 + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
23050 + core_if->op_state = B_PERIPHERAL;
23051 +#ifdef DWC_DEVICE_ONLY
23052 + dwc_otg_core_dev_init(core_if);
23053 +#endif
23054 + }
23055 +}
23056 +
23057 +/**
23058 + * This function enables the Device mode interrupts.
23059 + *
23060 + * @param core_if Programming view of DWC_otg controller
23061 + */
23062 +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
23063 +{
23064 + gintmsk_data_t intr_mask = {.d32 = 0 };
23065 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23066 +
23067 + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
23068 +
23069 + /* Disable all interrupts. */
23070 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
23071 +
23072 + /* Clear any pending interrupts */
23073 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
23074 +
23075 + /* Enable the common interrupts */
23076 + dwc_otg_enable_common_interrupts(core_if);
23077 +
23078 + /* Enable interrupts */
23079 + intr_mask.b.usbreset = 1;
23080 + intr_mask.b.enumdone = 1;
23081 + /* Disable Disconnect interrupt in Device mode */
23082 + intr_mask.b.disconnect = 0;
23083 +
23084 + if (!core_if->multiproc_int_enable) {
23085 + intr_mask.b.inepintr = 1;
23086 + intr_mask.b.outepintr = 1;
23087 + }
23088 +
23089 + intr_mask.b.erlysuspend = 1;
23090 +
23091 + if (core_if->en_multiple_tx_fifo == 0) {
23092 + intr_mask.b.epmismatch = 1;
23093 + }
23094 +
23095 + //intr_mask.b.incomplisoout = 1;
23096 + intr_mask.b.incomplisoin = 1;
23097 +
23098 +/* Enable the ignore frame number for ISOC xfers - MAS */
23099 +/* Disable to support high bandwith ISOC transfers - manukz */
23100 +#if 0
23101 +#ifdef DWC_UTE_PER_IO
23102 + if (core_if->dma_enable) {
23103 + if (core_if->dma_desc_enable) {
23104 + dctl_data_t dctl1 = {.d32 = 0 };
23105 + dctl1.b.ifrmnum = 1;
23106 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
23107 + dctl, 0, dctl1.d32);
23108 + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
23109 + DWC_READ_REG32(&core_if->dev_if->
23110 + dev_global_regs->dctl));
23111 + }
23112 + }
23113 +#endif
23114 +#endif
23115 +#ifdef DWC_EN_ISOC
23116 + if (core_if->dma_enable) {
23117 + if (core_if->dma_desc_enable == 0) {
23118 + if (core_if->pti_enh_enable) {
23119 + dctl_data_t dctl = {.d32 = 0 };
23120 + dctl.b.ifrmnum = 1;
23121 + DWC_MODIFY_REG32(&core_if->
23122 + dev_if->dev_global_regs->dctl,
23123 + 0, dctl.d32);
23124 + } else {
23125 + intr_mask.b.incomplisoin = 1;
23126 + intr_mask.b.incomplisoout = 1;
23127 + }
23128 + }
23129 + } else {
23130 + intr_mask.b.incomplisoin = 1;
23131 + intr_mask.b.incomplisoout = 1;
23132 + }
23133 +#endif /* DWC_EN_ISOC */
23134 +
23135 + /** @todo NGS: Should this be a module parameter? */
23136 +#ifdef USE_PERIODIC_EP
23137 + intr_mask.b.isooutdrop = 1;
23138 + intr_mask.b.eopframe = 1;
23139 + intr_mask.b.incomplisoin = 1;
23140 + intr_mask.b.incomplisoout = 1;
23141 +#endif
23142 +
23143 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
23144 +
23145 + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
23146 + DWC_READ_REG32(&global_regs->gintmsk));
23147 +}
23148 +
23149 +/**
23150 + * This function initializes the DWC_otg controller registers for
23151 + * device mode.
23152 + *
23153 + * @param core_if Programming view of DWC_otg controller
23154 + *
23155 + */
23156 +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
23157 +{
23158 + int i;
23159 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23160 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
23161 + dwc_otg_core_params_t *params = core_if->core_params;
23162 + dcfg_data_t dcfg = {.d32 = 0 };
23163 + depctl_data_t diepctl = {.d32 = 0 };
23164 + grstctl_t resetctl = {.d32 = 0 };
23165 + uint32_t rx_fifo_size;
23166 + fifosize_data_t nptxfifosize;
23167 + fifosize_data_t txfifosize;
23168 + dthrctl_data_t dthrctl;
23169 + fifosize_data_t ptxfifosize;
23170 + uint16_t rxfsiz, nptxfsiz;
23171 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
23172 + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
23173 +
23174 + /* Restart the Phy Clock */
23175 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
23176 +
23177 + /* Device configuration register */
23178 + init_devspd(core_if);
23179 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
23180 + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
23181 + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
23182 + /* Enable Device OUT NAK in case of DDMA mode*/
23183 + if (core_if->core_params->dev_out_nak) {
23184 + dcfg.b.endevoutnak = 1;
23185 + }
23186 +
23187 + if (core_if->core_params->cont_on_bna) {
23188 + dctl_data_t dctl = {.d32 = 0 };
23189 + dctl.b.encontonbna = 1;
23190 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
23191 + }
23192 +
23193 +
23194 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
23195 +
23196 + /* Configure data FIFO sizes */
23197 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
23198 + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
23199 + core_if->total_fifo_size);
23200 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
23201 + params->dev_rx_fifo_size);
23202 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
23203 + params->dev_nperio_tx_fifo_size);
23204 +
23205 + /* Rx FIFO */
23206 + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
23207 + DWC_READ_REG32(&global_regs->grxfsiz));
23208 +
23209 +#ifdef DWC_UTE_CFI
23210 + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
23211 + core_if->init_rxfsiz = params->dev_rx_fifo_size;
23212 +#endif
23213 + rx_fifo_size = params->dev_rx_fifo_size;
23214 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
23215 +
23216 + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
23217 + DWC_READ_REG32(&global_regs->grxfsiz));
23218 +
23219 + /** Set Periodic Tx FIFO Mask all bits 0 */
23220 + core_if->p_tx_msk = 0;
23221 +
23222 + /** Set Tx FIFO Mask all bits 0 */
23223 + core_if->tx_msk = 0;
23224 +
23225 + if (core_if->en_multiple_tx_fifo == 0) {
23226 + /* Non-periodic Tx FIFO */
23227 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23228 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23229 +
23230 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
23231 + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
23232 +
23233 + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
23234 + nptxfifosize.d32);
23235 +
23236 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23237 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23238 +
23239 + /**@todo NGS: Fix Periodic FIFO Sizing! */
23240 + /*
23241 + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
23242 + * Indexes of the FIFO size module parameters in the
23243 + * dev_perio_tx_fifo_size array and the FIFO size registers in
23244 + * the dptxfsiz array run from 0 to 14.
23245 + */
23246 + /** @todo Finish debug of this */
23247 + ptxfifosize.b.startaddr =
23248 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23249 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
23250 + ptxfifosize.b.depth =
23251 + params->dev_perio_tx_fifo_size[i];
23252 + DWC_DEBUGPL(DBG_CIL,
23253 + "initial dtxfsiz[%d]=%08x\n", i,
23254 + DWC_READ_REG32(&global_regs->dtxfsiz
23255 + [i]));
23256 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
23257 + ptxfifosize.d32);
23258 + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
23259 + i,
23260 + DWC_READ_REG32(&global_regs->dtxfsiz
23261 + [i]));
23262 + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
23263 + }
23264 + } else {
23265 + /*
23266 + * Tx FIFOs These FIFOs are numbered from 1 to 15.
23267 + * Indexes of the FIFO size module parameters in the
23268 + * dev_tx_fifo_size array and the FIFO size registers in
23269 + * the dtxfsiz array run from 0 to 14.
23270 + */
23271 +
23272 + /* Non-periodic Tx FIFO */
23273 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23274 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23275 +
23276 +#ifdef DWC_UTE_CFI
23277 + core_if->pwron_gnptxfsiz =
23278 + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23279 + core_if->init_gnptxfsiz =
23280 + params->dev_nperio_tx_fifo_size;
23281 +#endif
23282 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
23283 + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
23284 +
23285 + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
23286 + nptxfifosize.d32);
23287 +
23288 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23289 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23290 +
23291 + txfifosize.b.startaddr =
23292 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23293 +
23294 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
23295 +
23296 + txfifosize.b.depth =
23297 + params->dev_tx_fifo_size[i];
23298 +
23299 + DWC_DEBUGPL(DBG_CIL,
23300 + "initial dtxfsiz[%d]=%08x\n",
23301 + i,
23302 + DWC_READ_REG32(&global_regs->dtxfsiz
23303 + [i]));
23304 +
23305 +#ifdef DWC_UTE_CFI
23306 + core_if->pwron_txfsiz[i] =
23307 + (DWC_READ_REG32
23308 + (&global_regs->dtxfsiz[i]) >> 16);
23309 + core_if->init_txfsiz[i] =
23310 + params->dev_tx_fifo_size[i];
23311 +#endif
23312 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
23313 + txfifosize.d32);
23314 +
23315 + DWC_DEBUGPL(DBG_CIL,
23316 + "new dtxfsiz[%d]=%08x\n",
23317 + i,
23318 + DWC_READ_REG32(&global_regs->dtxfsiz
23319 + [i]));
23320 +
23321 + txfifosize.b.startaddr += txfifosize.b.depth;
23322 + }
23323 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
23324 + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
23325 + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
23326 + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
23327 + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
23328 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23329 + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
23330 + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23331 + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
23332 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23333 + }
23334 + }
23335 +
23336 + /* Flush the FIFOs */
23337 + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
23338 + dwc_otg_flush_rx_fifo(core_if);
23339 +
23340 + /* Flush the Learning Queue. */
23341 + resetctl.b.intknqflsh = 1;
23342 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
23343 +
23344 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
23345 + core_if->start_predict = 0;
23346 + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
23347 + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
23348 + }
23349 + core_if->nextep_seq[0] = 0;
23350 + core_if->first_in_nextep_seq = 0;
23351 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
23352 + diepctl.b.nextep = 0;
23353 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
23354 +
23355 + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
23356 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
23357 + dcfg.b.epmscnt = 2;
23358 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
23359 +
23360 + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
23361 + __func__, core_if->first_in_nextep_seq);
23362 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
23363 + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
23364 + }
23365 + DWC_DEBUGPL(DBG_CILV,"\n");
23366 + }
23367 +
23368 + /* Clear all pending Device Interrupts */
23369 + /** @todo - if the condition needed to be checked
23370 + * or in any case all pending interrutps should be cleared?
23371 + */
23372 + if (core_if->multiproc_int_enable) {
23373 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
23374 + DWC_WRITE_REG32(&dev_if->
23375 + dev_global_regs->diepeachintmsk[i], 0);
23376 + }
23377 + }
23378 +
23379 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
23380 + DWC_WRITE_REG32(&dev_if->
23381 + dev_global_regs->doepeachintmsk[i], 0);
23382 + }
23383 +
23384 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
23385 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
23386 + } else {
23387 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
23388 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
23389 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
23390 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
23391 + }
23392 +
23393 + for (i = 0; i <= dev_if->num_in_eps; i++) {
23394 + depctl_data_t depctl;
23395 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
23396 + if (depctl.b.epena) {
23397 + depctl.d32 = 0;
23398 + depctl.b.epdis = 1;
23399 + depctl.b.snak = 1;
23400 + } else {
23401 + depctl.d32 = 0;
23402 + }
23403 +
23404 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
23405 +
23406 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
23407 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
23408 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
23409 + }
23410 +
23411 + for (i = 0; i <= dev_if->num_out_eps; i++) {
23412 + depctl_data_t depctl;
23413 + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
23414 + if (depctl.b.epena) {
23415 + dctl_data_t dctl = {.d32 = 0 };
23416 + gintmsk_data_t gintsts = {.d32 = 0 };
23417 + doepint_data_t doepint = {.d32 = 0 };
23418 + dctl.b.sgoutnak = 1;
23419 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
23420 + do {
23421 + dwc_udelay(10);
23422 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
23423 + } while (!gintsts.b.goutnakeff);
23424 + gintsts.d32 = 0;
23425 + gintsts.b.goutnakeff = 1;
23426 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
23427 +
23428 + depctl.d32 = 0;
23429 + depctl.b.epdis = 1;
23430 + depctl.b.snak = 1;
23431 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
23432 + do {
23433 + dwc_udelay(10);
23434 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
23435 + out_ep_regs[i]->doepint);
23436 + } while (!doepint.b.epdisabled);
23437 +
23438 + doepint.b.epdisabled = 1;
23439 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
23440 +
23441 + dctl.d32 = 0;
23442 + dctl.b.cgoutnak = 1;
23443 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
23444 + } else {
23445 + depctl.d32 = 0;
23446 + }
23447 +
23448 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
23449 +
23450 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
23451 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
23452 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
23453 + }
23454 +
23455 + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
23456 + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
23457 + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
23458 + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
23459 +
23460 + dev_if->rx_thr_length = params->rx_thr_length;
23461 + dev_if->tx_thr_length = params->tx_thr_length;
23462 +
23463 + dev_if->setup_desc_index = 0;
23464 +
23465 + dthrctl.d32 = 0;
23466 + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
23467 + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
23468 + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
23469 + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
23470 + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
23471 + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
23472 +
23473 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
23474 + dthrctl.d32);
23475 +
23476 + DWC_DEBUGPL(DBG_CIL,
23477 + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
23478 + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
23479 + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
23480 + dthrctl.b.rx_thr_len);
23481 +
23482 + }
23483 +
23484 + dwc_otg_enable_device_interrupts(core_if);
23485 +
23486 + {
23487 + diepmsk_data_t msk = {.d32 = 0 };
23488 + msk.b.txfifoundrn = 1;
23489 + if (core_if->multiproc_int_enable) {
23490 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
23491 + diepeachintmsk[0], msk.d32, msk.d32);
23492 + } else {
23493 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
23494 + msk.d32, msk.d32);
23495 + }
23496 + }
23497 +
23498 + if (core_if->multiproc_int_enable) {
23499 + /* Set NAK on Babble */
23500 + dctl_data_t dctl = {.d32 = 0 };
23501 + dctl.b.nakonbble = 1;
23502 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
23503 + }
23504 +
23505 + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
23506 + dctl_data_t dctl = {.d32 = 0 };
23507 + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
23508 + dctl.b.sftdiscon = 0;
23509 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
23510 + }
23511 +}
23512 +
23513 +/**
23514 + * This function enables the Host mode interrupts.
23515 + *
23516 + * @param core_if Programming view of DWC_otg controller
23517 + */
23518 +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
23519 +{
23520 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23521 + gintmsk_data_t intr_mask = {.d32 = 0 };
23522 +
23523 + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
23524 +
23525 + /* Disable all interrupts. */
23526 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
23527 +
23528 + /* Clear any pending interrupts. */
23529 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
23530 +
23531 + /* Enable the common interrupts */
23532 + dwc_otg_enable_common_interrupts(core_if);
23533 +
23534 + /*
23535 + * Enable host mode interrupts without disturbing common
23536 + * interrupts.
23537 + */
23538 +
23539 + intr_mask.b.disconnect = 1;
23540 + intr_mask.b.portintr = 1;
23541 + intr_mask.b.hcintr = 1;
23542 +
23543 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
23544 +}
23545 +
23546 +/**
23547 + * This function disables the Host Mode interrupts.
23548 + *
23549 + * @param core_if Programming view of DWC_otg controller
23550 + */
23551 +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
23552 +{
23553 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23554 + gintmsk_data_t intr_mask = {.d32 = 0 };
23555 +
23556 + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
23557 +
23558 + /*
23559 + * Disable host mode interrupts without disturbing common
23560 + * interrupts.
23561 + */
23562 + intr_mask.b.sofintr = 1;
23563 + intr_mask.b.portintr = 1;
23564 + intr_mask.b.hcintr = 1;
23565 + intr_mask.b.ptxfempty = 1;
23566 + intr_mask.b.nptxfempty = 1;
23567 +
23568 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
23569 +}
23570 +
23571 +/**
23572 + * This function initializes the DWC_otg controller registers for
23573 + * host mode.
23574 + *
23575 + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
23576 + * request queues. Host channels are reset to ensure that they are ready for
23577 + * performing transfers.
23578 + *
23579 + * @param core_if Programming view of DWC_otg controller
23580 + *
23581 + */
23582 +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
23583 +{
23584 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23585 + dwc_otg_host_if_t *host_if = core_if->host_if;
23586 + dwc_otg_core_params_t *params = core_if->core_params;
23587 + hprt0_data_t hprt0 = {.d32 = 0 };
23588 + fifosize_data_t nptxfifosize;
23589 + fifosize_data_t ptxfifosize;
23590 + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
23591 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
23592 + int i;
23593 + hcchar_data_t hcchar;
23594 + hcfg_data_t hcfg;
23595 + hfir_data_t hfir;
23596 + dwc_otg_hc_regs_t *hc_regs;
23597 + int num_channels;
23598 + gotgctl_data_t gotgctl = {.d32 = 0 };
23599 +
23600 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
23601 +
23602 + /* Restart the Phy Clock */
23603 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
23604 +
23605 + /* Initialize Host Configuration Register */
23606 + init_fslspclksel(core_if);
23607 + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
23608 + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
23609 + hcfg.b.fslssupp = 1;
23610 + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
23611 +
23612 + }
23613 +
23614 + /* This bit allows dynamic reloading of the HFIR register
23615 + * during runtime. This bit needs to be programmed during
23616 + * initial configuration and its value must not be changed
23617 + * during runtime.*/
23618 + if (core_if->core_params->reload_ctl == 1) {
23619 + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
23620 + hfir.b.hfirrldctrl = 1;
23621 + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
23622 + }
23623 +
23624 + if (core_if->core_params->dma_desc_enable) {
23625 + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
23626 + if (!
23627 + (core_if->hwcfg4.b.desc_dma
23628 + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
23629 + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
23630 + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
23631 + || (op_mode ==
23632 + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
23633 + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
23634 + || (op_mode ==
23635 + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
23636 +
23637 + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
23638 + "Either core version is below 2.90a or "
23639 + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
23640 + "To run the driver in Buffer DMA host mode set dma_desc_enable "
23641 + "module parameter to 0.\n");
23642 + return;
23643 + }
23644 + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
23645 + hcfg.b.descdma = 1;
23646 + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
23647 + }
23648 +
23649 + /* Configure data FIFO sizes */
23650 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
23651 + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
23652 + core_if->total_fifo_size);
23653 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
23654 + params->host_rx_fifo_size);
23655 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
23656 + params->host_nperio_tx_fifo_size);
23657 + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
23658 + params->host_perio_tx_fifo_size);
23659 +
23660 + /* Rx FIFO */
23661 + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
23662 + DWC_READ_REG32(&global_regs->grxfsiz));
23663 + DWC_WRITE_REG32(&global_regs->grxfsiz,
23664 + params->host_rx_fifo_size);
23665 + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
23666 + DWC_READ_REG32(&global_regs->grxfsiz));
23667 +
23668 + /* Non-periodic Tx FIFO */
23669 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23670 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23671 + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
23672 + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
23673 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
23674 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23675 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23676 +
23677 + /* Periodic Tx FIFO */
23678 + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
23679 + DWC_READ_REG32(&global_regs->hptxfsiz));
23680 + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
23681 + ptxfifosize.b.startaddr =
23682 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23683 + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
23684 + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
23685 + DWC_READ_REG32(&global_regs->hptxfsiz));
23686 +
23687 + if (core_if->en_multiple_tx_fifo
23688 + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
23689 + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
23690 + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
23691 + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
23692 + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23693 + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
23694 + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
23695 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23696 + }
23697 + }
23698 +
23699 + /* TODO - check this */
23700 + /* Clear Host Set HNP Enable in the OTG Control Register */
23701 + gotgctl.b.hstsethnpen = 1;
23702 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
23703 + /* Make sure the FIFOs are flushed. */
23704 + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
23705 + dwc_otg_flush_rx_fifo(core_if);
23706 +
23707 + /* Clear Host Set HNP Enable in the OTG Control Register */
23708 + gotgctl.b.hstsethnpen = 1;
23709 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
23710 +
23711 + if (!core_if->core_params->dma_desc_enable) {
23712 + /* Flush out any leftover queued requests. */
23713 + num_channels = core_if->core_params->host_channels;
23714 +
23715 + for (i = 0; i < num_channels; i++) {
23716 + hc_regs = core_if->host_if->hc_regs[i];
23717 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23718 + hcchar.b.chen = 0;
23719 + hcchar.b.chdis = 1;
23720 + hcchar.b.epdir = 0;
23721 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23722 + }
23723 +
23724 + /* Halt all channels to put them into a known state. */
23725 + for (i = 0; i < num_channels; i++) {
23726 + int count = 0;
23727 + hc_regs = core_if->host_if->hc_regs[i];
23728 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23729 + hcchar.b.chen = 1;
23730 + hcchar.b.chdis = 1;
23731 + hcchar.b.epdir = 0;
23732 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23733 + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
23734 + do {
23735 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23736 + if (++count > 1000) {
23737 + DWC_ERROR
23738 + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
23739 + __func__, i, hcchar.d32, &hc_regs->hcchar);
23740 + break;
23741 + }
23742 + dwc_udelay(1);
23743 + } while (hcchar.b.chen);
23744 + }
23745 + }
23746 +
23747 + /* Turn on the vbus power. */
23748 + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
23749 + if (core_if->op_state == A_HOST) {
23750 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
23751 + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
23752 + if (hprt0.b.prtpwr == 0) {
23753 + hprt0.b.prtpwr = 1;
23754 + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
23755 + }
23756 + }
23757 +
23758 + dwc_otg_enable_host_interrupts(core_if);
23759 +}
23760 +
23761 +/**
23762 + * Prepares a host channel for transferring packets to/from a specific
23763 + * endpoint. The HCCHARn register is set up with the characteristics specified
23764 + * in _hc. Host channel interrupts that may need to be serviced while this
23765 + * transfer is in progress are enabled.
23766 + *
23767 + * @param core_if Programming view of DWC_otg controller
23768 + * @param hc Information needed to initialize the host channel
23769 + */
23770 +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
23771 +{
23772 + hcintmsk_data_t hc_intr_mask;
23773 + hcchar_data_t hcchar;
23774 + hcsplt_data_t hcsplt;
23775 +
23776 + uint8_t hc_num = hc->hc_num;
23777 + dwc_otg_host_if_t *host_if = core_if->host_if;
23778 + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
23779 +
23780 + /* Clear old interrupt conditions for this host channel. */
23781 + hc_intr_mask.d32 = 0xFFFFFFFF;
23782 + hc_intr_mask.b.reserved14_31 = 0;
23783 + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
23784 +
23785 + /* Enable channel interrupts required for this transfer. */
23786 + hc_intr_mask.d32 = 0;
23787 + hc_intr_mask.b.chhltd = 1;
23788 + if (core_if->dma_enable) {
23789 + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
23790 + if (!core_if->dma_desc_enable)
23791 + hc_intr_mask.b.ahberr = 1;
23792 + else {
23793 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
23794 + hc_intr_mask.b.xfercompl = 1;
23795 + }
23796 +
23797 + if (hc->error_state && !hc->do_split &&
23798 + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
23799 + hc_intr_mask.b.ack = 1;
23800 + if (hc->ep_is_in) {
23801 + hc_intr_mask.b.datatglerr = 1;
23802 + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
23803 + hc_intr_mask.b.nak = 1;
23804 + }
23805 + }
23806 + }
23807 + } else {
23808 + switch (hc->ep_type) {
23809 + case DWC_OTG_EP_TYPE_CONTROL:
23810 + case DWC_OTG_EP_TYPE_BULK:
23811 + hc_intr_mask.b.xfercompl = 1;
23812 + hc_intr_mask.b.stall = 1;
23813 + hc_intr_mask.b.xacterr = 1;
23814 + hc_intr_mask.b.datatglerr = 1;
23815 + if (hc->ep_is_in) {
23816 + hc_intr_mask.b.bblerr = 1;
23817 + } else {
23818 + hc_intr_mask.b.nak = 1;
23819 + hc_intr_mask.b.nyet = 1;
23820 + if (hc->do_ping) {
23821 + hc_intr_mask.b.ack = 1;
23822 + }
23823 + }
23824 +
23825 + if (hc->do_split) {
23826 + hc_intr_mask.b.nak = 1;
23827 + if (hc->complete_split) {
23828 + hc_intr_mask.b.nyet = 1;
23829 + } else {
23830 + hc_intr_mask.b.ack = 1;
23831 + }
23832 + }
23833 +
23834 + if (hc->error_state) {
23835 + hc_intr_mask.b.ack = 1;
23836 + }
23837 + break;
23838 + case DWC_OTG_EP_TYPE_INTR:
23839 + hc_intr_mask.b.xfercompl = 1;
23840 + hc_intr_mask.b.nak = 1;
23841 + hc_intr_mask.b.stall = 1;
23842 + hc_intr_mask.b.xacterr = 1;
23843 + hc_intr_mask.b.datatglerr = 1;
23844 + hc_intr_mask.b.frmovrun = 1;
23845 +
23846 + if (hc->ep_is_in) {
23847 + hc_intr_mask.b.bblerr = 1;
23848 + }
23849 + if (hc->error_state) {
23850 + hc_intr_mask.b.ack = 1;
23851 + }
23852 + if (hc->do_split) {
23853 + if (hc->complete_split) {
23854 + hc_intr_mask.b.nyet = 1;
23855 + } else {
23856 + hc_intr_mask.b.ack = 1;
23857 + }
23858 + }
23859 + break;
23860 + case DWC_OTG_EP_TYPE_ISOC:
23861 + hc_intr_mask.b.xfercompl = 1;
23862 + hc_intr_mask.b.frmovrun = 1;
23863 + hc_intr_mask.b.ack = 1;
23864 +
23865 + if (hc->ep_is_in) {
23866 + hc_intr_mask.b.xacterr = 1;
23867 + hc_intr_mask.b.bblerr = 1;
23868 + }
23869 + break;
23870 + }
23871 + }
23872 + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
23873 +
23874 + /*
23875 + * Program the HCCHARn register with the endpoint characteristics for
23876 + * the current transfer.
23877 + */
23878 + hcchar.d32 = 0;
23879 + hcchar.b.devaddr = hc->dev_addr;
23880 + hcchar.b.epnum = hc->ep_num;
23881 + hcchar.b.epdir = hc->ep_is_in;
23882 + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
23883 + hcchar.b.eptype = hc->ep_type;
23884 + hcchar.b.mps = hc->max_packet;
23885 +
23886 + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
23887 +
23888 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
23889 + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
23890 + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
23891 + "Max Pkt %d, Multi Cnt %d\n",
23892 + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
23893 + hcchar.b.mps, hcchar.b.multicnt);
23894 +
23895 + /*
23896 + * Program the HCSPLIT register for SPLITs
23897 + */
23898 + hcsplt.d32 = 0;
23899 + if (hc->do_split) {
23900 + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
23901 + hc->hc_num,
23902 + hc->complete_split ? "CSPLIT" : "SSPLIT");
23903 + hcsplt.b.compsplt = hc->complete_split;
23904 + hcsplt.b.xactpos = hc->xact_pos;
23905 + hcsplt.b.hubaddr = hc->hub_addr;
23906 + hcsplt.b.prtaddr = hc->port_addr;
23907 + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
23908 + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
23909 + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
23910 + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
23911 + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
23912 + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
23913 + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
23914 + }
23915 + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
23916 +
23917 +}
23918 +
23919 +/**
23920 + * Attempts to halt a host channel. This function should only be called in
23921 + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
23922 + * normal circumstances in DMA mode, the controller halts the channel when the
23923 + * transfer is complete or a condition occurs that requires application
23924 + * intervention.
23925 + *
23926 + * In slave mode, checks for a free request queue entry, then sets the Channel
23927 + * Enable and Channel Disable bits of the Host Channel Characteristics
23928 + * register of the specified channel to intiate the halt. If there is no free
23929 + * request queue entry, sets only the Channel Disable bit of the HCCHARn
23930 + * register to flush requests for this channel. In the latter case, sets a
23931 + * flag to indicate that the host channel needs to be halted when a request
23932 + * queue slot is open.
23933 + *
23934 + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
23935 + * HCCHARn register. The controller ensures there is space in the request
23936 + * queue before submitting the halt request.
23937 + *
23938 + * Some time may elapse before the core flushes any posted requests for this
23939 + * host channel and halts. The Channel Halted interrupt handler completes the
23940 + * deactivation of the host channel.
23941 + *
23942 + * @param core_if Controller register interface.
23943 + * @param hc Host channel to halt.
23944 + * @param halt_status Reason for halting the channel.
23945 + */
23946 +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
23947 + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
23948 +{
23949 + gnptxsts_data_t nptxsts;
23950 + hptxsts_data_t hptxsts;
23951 + hcchar_data_t hcchar;
23952 + dwc_otg_hc_regs_t *hc_regs;
23953 + dwc_otg_core_global_regs_t *global_regs;
23954 + dwc_otg_host_global_regs_t *host_global_regs;
23955 +
23956 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
23957 + global_regs = core_if->core_global_regs;
23958 + host_global_regs = core_if->host_if->host_global_regs;
23959 +
23960 + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
23961 + "halt_status = %d\n", halt_status);
23962 +
23963 + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
23964 + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
23965 + /*
23966 + * Disable all channel interrupts except Ch Halted. The QTD
23967 + * and QH state associated with this transfer has been cleared
23968 + * (in the case of URB_DEQUEUE), so the channel needs to be
23969 + * shut down carefully to prevent crashes.
23970 + */
23971 + hcintmsk_data_t hcintmsk;
23972 + hcintmsk.d32 = 0;
23973 + hcintmsk.b.chhltd = 1;
23974 + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
23975 +
23976 + /*
23977 + * Make sure no other interrupts besides halt are currently
23978 + * pending. Handling another interrupt could cause a crash due
23979 + * to the QTD and QH state.
23980 + */
23981 + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
23982 +
23983 + /*
23984 + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
23985 + * even if the channel was already halted for some other
23986 + * reason.
23987 + */
23988 + hc->halt_status = halt_status;
23989 +
23990 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23991 + if (hcchar.b.chen == 0) {
23992 + /*
23993 + * The channel is either already halted or it hasn't
23994 + * started yet. In DMA mode, the transfer may halt if
23995 + * it finishes normally or a condition occurs that
23996 + * requires driver intervention. Don't want to halt
23997 + * the channel again. In either Slave or DMA mode,
23998 + * it's possible that the transfer has been assigned
23999 + * to a channel, but not started yet when an URB is
24000 + * dequeued. Don't want to halt a channel that hasn't
24001 + * started yet.
24002 + */
24003 + return;
24004 + }
24005 + }
24006 + if (hc->halt_pending) {
24007 + /*
24008 + * A halt has already been issued for this channel. This might
24009 + * happen when a transfer is aborted by a higher level in
24010 + * the stack.
24011 + */
24012 +#ifdef DEBUG
24013 + DWC_PRINTF
24014 + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
24015 + __func__, hc->hc_num);
24016 +
24017 +#endif
24018 + return;
24019 + }
24020 +
24021 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24022 +
24023 + /* No need to set the bit in DDMA for disabling the channel */
24024 + //TODO check it everywhere channel is disabled
24025 + if (!core_if->core_params->dma_desc_enable)
24026 + hcchar.b.chen = 1;
24027 + hcchar.b.chdis = 1;
24028 +
24029 + if (!core_if->dma_enable) {
24030 + /* Check for space in the request queue to issue the halt. */
24031 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
24032 + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
24033 + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
24034 + if (nptxsts.b.nptxqspcavail == 0) {
24035 + hcchar.b.chen = 0;
24036 + }
24037 + } else {
24038 + hptxsts.d32 =
24039 + DWC_READ_REG32(&host_global_regs->hptxsts);
24040 + if ((hptxsts.b.ptxqspcavail == 0)
24041 + || (core_if->queuing_high_bandwidth)) {
24042 + hcchar.b.chen = 0;
24043 + }
24044 + }
24045 + }
24046 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24047 +
24048 + hc->halt_status = halt_status;
24049 +
24050 + if (hcchar.b.chen) {
24051 + hc->halt_pending = 1;
24052 + hc->halt_on_queue = 0;
24053 + } else {
24054 + hc->halt_on_queue = 1;
24055 + }
24056 +
24057 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24058 + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
24059 + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
24060 + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
24061 + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
24062 +
24063 + return;
24064 +}
24065 +
24066 +/**
24067 + * Clears the transfer state for a host channel. This function is normally
24068 + * called after a transfer is done and the host channel is being released.
24069 + *
24070 + * @param core_if Programming view of DWC_otg controller.
24071 + * @param hc Identifies the host channel to clean up.
24072 + */
24073 +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24074 +{
24075 + dwc_otg_hc_regs_t *hc_regs;
24076 +
24077 + hc->xfer_started = 0;
24078 +
24079 + /*
24080 + * Clear channel interrupt enables and any unhandled channel interrupt
24081 + * conditions.
24082 + */
24083 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24084 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
24085 + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
24086 +#ifdef DEBUG
24087 + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
24088 +#endif
24089 +}
24090 +
24091 +/**
24092 + * Sets the channel property that indicates in which frame a periodic transfer
24093 + * should occur. This is always set to the _next_ frame. This function has no
24094 + * effect on non-periodic transfers.
24095 + *
24096 + * @param core_if Programming view of DWC_otg controller.
24097 + * @param hc Identifies the host channel to set up and its properties.
24098 + * @param hcchar Current value of the HCCHAR register for the specified host
24099 + * channel.
24100 + */
24101 +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
24102 + dwc_hc_t * hc, hcchar_data_t * hcchar)
24103 +{
24104 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24105 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24106 + hfnum_data_t hfnum;
24107 + hfnum.d32 =
24108 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
24109 +
24110 + /* 1 if _next_ frame is odd, 0 if it's even */
24111 + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
24112 +#ifdef DEBUG
24113 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
24114 + && !hc->complete_split) {
24115 + switch (hfnum.b.frnum & 0x7) {
24116 + case 7:
24117 + core_if->hfnum_7_samples++;
24118 + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
24119 + break;
24120 + case 0:
24121 + core_if->hfnum_0_samples++;
24122 + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
24123 + break;
24124 + default:
24125 + core_if->hfnum_other_samples++;
24126 + core_if->hfnum_other_frrem_accum +=
24127 + hfnum.b.frrem;
24128 + break;
24129 + }
24130 + }
24131 +#endif
24132 + }
24133 +}
24134 +
24135 +#ifdef DEBUG
24136 +void hc_xfer_timeout(void *ptr)
24137 +{
24138 + hc_xfer_info_t *xfer_info = NULL;
24139 + int hc_num = 0;
24140 +
24141 + if (ptr)
24142 + xfer_info = (hc_xfer_info_t *) ptr;
24143 +
24144 + if (!xfer_info->hc) {
24145 + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
24146 + return;
24147 + }
24148 +
24149 + hc_num = xfer_info->hc->hc_num;
24150 + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
24151 + DWC_WARN(" start_hcchar_val 0x%08x\n",
24152 + xfer_info->core_if->start_hcchar_val[hc_num]);
24153 +}
24154 +#endif
24155 +
24156 +void ep_xfer_timeout(void *ptr)
24157 +{
24158 + ep_xfer_info_t *xfer_info = NULL;
24159 + int ep_num = 0;
24160 + dctl_data_t dctl = {.d32 = 0 };
24161 + gintsts_data_t gintsts = {.d32 = 0 };
24162 + gintmsk_data_t gintmsk = {.d32 = 0 };
24163 +
24164 + if (ptr)
24165 + xfer_info = (ep_xfer_info_t *) ptr;
24166 +
24167 + if (!xfer_info->ep) {
24168 + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
24169 + return;
24170 + }
24171 +
24172 + ep_num = xfer_info->ep->num;
24173 + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
24174 + /* Put the sate to 2 as it was time outed */
24175 + xfer_info->state = 2;
24176 +
24177 + dctl.d32 =
24178 + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
24179 + gintsts.d32 =
24180 + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
24181 + gintmsk.d32 =
24182 + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
24183 +
24184 + if (!gintmsk.b.goutnakeff) {
24185 + /* Unmask it */
24186 + gintmsk.b.goutnakeff = 1;
24187 + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
24188 + gintmsk.d32);
24189 +
24190 + }
24191 +
24192 + if (!gintsts.b.goutnakeff) {
24193 + dctl.b.sgoutnak = 1;
24194 + }
24195 + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
24196 + dctl.d32);
24197 +
24198 +}
24199 +
24200 +void set_pid_isoc(dwc_hc_t * hc)
24201 +{
24202 + /* Set up the initial PID for the transfer. */
24203 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
24204 + if (hc->ep_is_in) {
24205 + if (hc->multi_count == 1) {
24206 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24207 + } else if (hc->multi_count == 2) {
24208 + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
24209 + } else {
24210 + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
24211 + }
24212 + } else {
24213 + if (hc->multi_count == 1) {
24214 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24215 + } else {
24216 + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
24217 + }
24218 + }
24219 + } else {
24220 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24221 + }
24222 +}
24223 +
24224 +/**
24225 + * This function does the setup for a data transfer for a host channel and
24226 + * starts the transfer. May be called in either Slave mode or DMA mode. In
24227 + * Slave mode, the caller must ensure that there is sufficient space in the
24228 + * request queue and Tx Data FIFO.
24229 + *
24230 + * For an OUT transfer in Slave mode, it loads a data packet into the
24231 + * appropriate FIFO. If necessary, additional data packets will be loaded in
24232 + * the Host ISR.
24233 + *
24234 + * For an IN transfer in Slave mode, a data packet is requested. The data
24235 + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
24236 + * additional data packets are requested in the Host ISR.
24237 + *
24238 + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
24239 + * register along with a packet count of 1 and the channel is enabled. This
24240 + * causes a single PING transaction to occur. Other fields in HCTSIZ are
24241 + * simply set to 0 since no data transfer occurs in this case.
24242 + *
24243 + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
24244 + * all the information required to perform the subsequent data transfer. In
24245 + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
24246 + * controller performs the entire PING protocol, then starts the data
24247 + * transfer.
24248 + *
24249 + * @param core_if Programming view of DWC_otg controller.
24250 + * @param hc Information needed to initialize the host channel. The xfer_len
24251 + * value may be reduced to accommodate the max widths of the XferSize and
24252 + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
24253 + * to reflect the final xfer_len value.
24254 + */
24255 +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24256 +{
24257 + hcchar_data_t hcchar;
24258 + hctsiz_data_t hctsiz;
24259 + uint16_t num_packets;
24260 + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
24261 + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
24262 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24263 +
24264 + hctsiz.d32 = 0;
24265 +
24266 + if (hc->do_ping) {
24267 + if (!core_if->dma_enable) {
24268 + dwc_otg_hc_do_ping(core_if, hc);
24269 + hc->xfer_started = 1;
24270 + return;
24271 + } else {
24272 + hctsiz.b.dopng = 1;
24273 + }
24274 + }
24275 +
24276 + if (hc->do_split) {
24277 + num_packets = 1;
24278 +
24279 + if (hc->complete_split && !hc->ep_is_in) {
24280 + /* For CSPLIT OUT Transfer, set the size to 0 so the
24281 + * core doesn't expect any data written to the FIFO */
24282 + hc->xfer_len = 0;
24283 + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
24284 + hc->xfer_len = hc->max_packet;
24285 + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
24286 + hc->xfer_len = 188;
24287 + }
24288 +
24289 + hctsiz.b.xfersize = hc->xfer_len;
24290 + } else {
24291 + /*
24292 + * Ensure that the transfer length and packet count will fit
24293 + * in the widths allocated for them in the HCTSIZn register.
24294 + */
24295 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24296 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24297 + /*
24298 + * Make sure the transfer size is no larger than one
24299 + * (micro)frame's worth of data. (A check was done
24300 + * when the periodic transfer was accepted to ensure
24301 + * that a (micro)frame's worth of data can be
24302 + * programmed into a channel.)
24303 + */
24304 + uint32_t max_periodic_len =
24305 + hc->multi_count * hc->max_packet;
24306 + if (hc->xfer_len > max_periodic_len) {
24307 + hc->xfer_len = max_periodic_len;
24308 + } else {
24309 + }
24310 + } else if (hc->xfer_len > max_hc_xfer_size) {
24311 + /* Make sure that xfer_len is a multiple of max packet size. */
24312 + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
24313 + }
24314 +
24315 + if (hc->xfer_len > 0) {
24316 + num_packets =
24317 + (hc->xfer_len + hc->max_packet -
24318 + 1) / hc->max_packet;
24319 + if (num_packets > max_hc_pkt_count) {
24320 + num_packets = max_hc_pkt_count;
24321 + hc->xfer_len = num_packets * hc->max_packet;
24322 + }
24323 + } else {
24324 + /* Need 1 packet for transfer length of 0. */
24325 + num_packets = 1;
24326 + }
24327 +
24328 + if (hc->ep_is_in) {
24329 + /* Always program an integral # of max packets for IN transfers. */
24330 + hc->xfer_len = num_packets * hc->max_packet;
24331 + }
24332 +
24333 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24334 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24335 + /*
24336 + * Make sure that the multi_count field matches the
24337 + * actual transfer length.
24338 + */
24339 + hc->multi_count = num_packets;
24340 + }
24341 +
24342 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24343 + set_pid_isoc(hc);
24344 +
24345 + hctsiz.b.xfersize = hc->xfer_len;
24346 + }
24347 +
24348 + hc->start_pkt_count = num_packets;
24349 + hctsiz.b.pktcnt = num_packets;
24350 + hctsiz.b.pid = hc->data_pid_start;
24351 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24352 +
24353 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24354 + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
24355 + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
24356 + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
24357 +
24358 + if (core_if->dma_enable) {
24359 + dwc_dma_t dma_addr;
24360 + if (hc->align_buff) {
24361 + dma_addr = hc->align_buff;
24362 + } else {
24363 + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
24364 + }
24365 + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
24366 + }
24367 +
24368 + /* Start the split */
24369 + if (hc->do_split) {
24370 + hcsplt_data_t hcsplt;
24371 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
24372 + hcsplt.b.spltena = 1;
24373 + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
24374 + }
24375 +
24376 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24377 + hcchar.b.multicnt = hc->multi_count;
24378 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24379 +#ifdef DEBUG
24380 + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
24381 + if (hcchar.b.chdis) {
24382 + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
24383 + __func__, hc->hc_num, hcchar.d32);
24384 + }
24385 +#endif
24386 +
24387 + /* Set host channel enable after all other setup is complete. */
24388 + hcchar.b.chen = 1;
24389 + hcchar.b.chdis = 0;
24390 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24391 +
24392 + hc->xfer_started = 1;
24393 + hc->requests++;
24394 +
24395 + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
24396 + /* Load OUT packet into the appropriate Tx FIFO. */
24397 + dwc_otg_hc_write_packet(core_if, hc);
24398 + }
24399 +#ifdef DEBUG
24400 + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
24401 + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
24402 + hc->hc_num, core_if);//GRAYG
24403 + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
24404 + core_if->hc_xfer_info[hc->hc_num].hc = hc;
24405 +
24406 + /* Start a timer for this transfer. */
24407 + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
24408 + }
24409 +#endif
24410 +}
24411 +
24412 +/**
24413 + * This function does the setup for a data transfer for a host channel
24414 + * and starts the transfer in Descriptor DMA mode.
24415 + *
24416 + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
24417 + * Sets PID and NTD values. For periodic transfers
24418 + * initializes SCHED_INFO field with micro-frame bitmap.
24419 + *
24420 + * Initializes HCDMA register with descriptor list address and CTD value
24421 + * then starts the transfer via enabling the channel.
24422 + *
24423 + * @param core_if Programming view of DWC_otg controller.
24424 + * @param hc Information needed to initialize the host channel.
24425 + */
24426 +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24427 +{
24428 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24429 + hcchar_data_t hcchar;
24430 + hctsiz_data_t hctsiz;
24431 + hcdma_data_t hcdma;
24432 +
24433 + hctsiz.d32 = 0;
24434 +
24435 + if (hc->do_ping)
24436 + hctsiz.b_ddma.dopng = 1;
24437 +
24438 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24439 + set_pid_isoc(hc);
24440 +
24441 + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
24442 + hctsiz.b_ddma.pid = hc->data_pid_start;
24443 + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
24444 + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
24445 +
24446 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24447 + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
24448 + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
24449 +
24450 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24451 +
24452 + hcdma.d32 = 0;
24453 + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
24454 +
24455 + /* Always start from first descriptor. */
24456 + hcdma.b.ctd = 0;
24457 + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
24458 +
24459 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24460 + hcchar.b.multicnt = hc->multi_count;
24461 +
24462 +#ifdef DEBUG
24463 + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
24464 + if (hcchar.b.chdis) {
24465 + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
24466 + __func__, hc->hc_num, hcchar.d32);
24467 + }
24468 +#endif
24469 +
24470 + /* Set host channel enable after all other setup is complete. */
24471 + hcchar.b.chen = 1;
24472 + hcchar.b.chdis = 0;
24473 +
24474 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24475 +
24476 + hc->xfer_started = 1;
24477 + hc->requests++;
24478 +
24479 +#ifdef DEBUG
24480 + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
24481 + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
24482 + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
24483 + hc->hc_num, core_if);//GRAYG
24484 + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
24485 + core_if->hc_xfer_info[hc->hc_num].hc = hc;
24486 + /* Start a timer for this transfer. */
24487 + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
24488 + }
24489 +#endif
24490 +
24491 +}
24492 +
24493 +/**
24494 + * This function continues a data transfer that was started by previous call
24495 + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
24496 + * sufficient space in the request queue and Tx Data FIFO. This function
24497 + * should only be called in Slave mode. In DMA mode, the controller acts
24498 + * autonomously to complete transfers programmed to a host channel.
24499 + *
24500 + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
24501 + * if there is any data remaining to be queued. For an IN transfer, another
24502 + * data packet is always requested. For the SETUP phase of a control transfer,
24503 + * this function does nothing.
24504 + *
24505 + * @return 1 if a new request is queued, 0 if no more requests are required
24506 + * for this transfer.
24507 + */
24508 +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24509 +{
24510 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24511 +
24512 + if (hc->do_split) {
24513 + /* SPLITs always queue just once per channel */
24514 + return 0;
24515 + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
24516 + /* SETUPs are queued only once since they can't be NAKed. */
24517 + return 0;
24518 + } else if (hc->ep_is_in) {
24519 + /*
24520 + * Always queue another request for other IN transfers. If
24521 + * back-to-back INs are issued and NAKs are received for both,
24522 + * the driver may still be processing the first NAK when the
24523 + * second NAK is received. When the interrupt handler clears
24524 + * the NAK interrupt for the first NAK, the second NAK will
24525 + * not be seen. So we can't depend on the NAK interrupt
24526 + * handler to requeue a NAKed request. Instead, IN requests
24527 + * are issued each time this function is called. When the
24528 + * transfer completes, the extra requests for the channel will
24529 + * be flushed.
24530 + */
24531 + hcchar_data_t hcchar;
24532 + dwc_otg_hc_regs_t *hc_regs =
24533 + core_if->host_if->hc_regs[hc->hc_num];
24534 +
24535 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24536 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24537 + hcchar.b.chen = 1;
24538 + hcchar.b.chdis = 0;
24539 + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
24540 + hcchar.d32);
24541 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24542 + hc->requests++;
24543 + return 1;
24544 + } else {
24545 + /* OUT transfers. */
24546 + if (hc->xfer_count < hc->xfer_len) {
24547 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24548 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24549 + hcchar_data_t hcchar;
24550 + dwc_otg_hc_regs_t *hc_regs;
24551 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24552 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24553 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24554 + }
24555 +
24556 + /* Load OUT packet into the appropriate Tx FIFO. */
24557 + dwc_otg_hc_write_packet(core_if, hc);
24558 + hc->requests++;
24559 + return 1;
24560 + } else {
24561 + return 0;
24562 + }
24563 + }
24564 +}
24565 +
24566 +/**
24567 + * Starts a PING transfer. This function should only be called in Slave mode.
24568 + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
24569 + */
24570 +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24571 +{
24572 + hcchar_data_t hcchar;
24573 + hctsiz_data_t hctsiz;
24574 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24575 +
24576 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24577 +
24578 + hctsiz.d32 = 0;
24579 + hctsiz.b.dopng = 1;
24580 + hctsiz.b.pktcnt = 1;
24581 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24582 +
24583 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24584 + hcchar.b.chen = 1;
24585 + hcchar.b.chdis = 0;
24586 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24587 +}
24588 +
24589 +/*
24590 + * This function writes a packet into the Tx FIFO associated with the Host
24591 + * Channel. For a channel associated with a non-periodic EP, the non-periodic
24592 + * Tx FIFO is written. For a channel associated with a periodic EP, the
24593 + * periodic Tx FIFO is written. This function should only be called in Slave
24594 + * mode.
24595 + *
24596 + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
24597 + * then number of bytes written to the Tx FIFO.
24598 + */
24599 +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24600 +{
24601 + uint32_t i;
24602 + uint32_t remaining_count;
24603 + uint32_t byte_count;
24604 + uint32_t dword_count;
24605 +
24606 + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
24607 + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
24608 +
24609 + remaining_count = hc->xfer_len - hc->xfer_count;
24610 + if (remaining_count > hc->max_packet) {
24611 + byte_count = hc->max_packet;
24612 + } else {
24613 + byte_count = remaining_count;
24614 + }
24615 +
24616 + dword_count = (byte_count + 3) / 4;
24617 +
24618 + if ((((unsigned long)data_buff) & 0x3) == 0) {
24619 + /* xfer_buff is DWORD aligned. */
24620 + for (i = 0; i < dword_count; i++, data_buff++) {
24621 + DWC_WRITE_REG32(data_fifo, *data_buff);
24622 + }
24623 + } else {
24624 + /* xfer_buff is not DWORD aligned. */
24625 + for (i = 0; i < dword_count; i++, data_buff++) {
24626 + uint32_t data;
24627 + data =
24628 + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
24629 + 16 | data_buff[3] << 24);
24630 + DWC_WRITE_REG32(data_fifo, data);
24631 + }
24632 + }
24633 +
24634 + hc->xfer_count += byte_count;
24635 + hc->xfer_buff += byte_count;
24636 +}
24637 +
24638 +/**
24639 + * Gets the current USB frame number. This is the frame number from the last
24640 + * SOF packet.
24641 + */
24642 +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
24643 +{
24644 + dsts_data_t dsts;
24645 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
24646 +
24647 + /* read current frame/microframe number from DSTS register */
24648 + return dsts.b.soffn;
24649 +}
24650 +
24651 +/**
24652 + * Calculates and gets the frame Interval value of HFIR register according PHY
24653 + * type and speed.The application can modify a value of HFIR register only after
24654 + * the Port Enable bit of the Host Port Control and Status register
24655 + * (HPRT.PrtEnaPort) has been set.
24656 +*/
24657 +
24658 +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
24659 +{
24660 + gusbcfg_data_t usbcfg;
24661 + hwcfg2_data_t hwcfg2;
24662 + hprt0_data_t hprt0;
24663 + int clock = 60; // default value
24664 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
24665 + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
24666 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
24667 + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
24668 + clock = 60;
24669 + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
24670 + clock = 48;
24671 + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24672 + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
24673 + clock = 30;
24674 + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24675 + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
24676 + clock = 60;
24677 + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24678 + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
24679 + clock = 48;
24680 + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
24681 + clock = 48;
24682 + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
24683 + clock = 48;
24684 + if (hprt0.b.prtspd == 0)
24685 + /* High speed case */
24686 + return 125 * clock - 1;
24687 + else
24688 + /* FS/LS case */
24689 + return 1000 * clock - 1;
24690 +}
24691 +
24692 +/**
24693 + * This function reads a setup packet from the Rx FIFO into the destination
24694 + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
24695 + * Interrupt routine when a SETUP packet has been received in Slave mode.
24696 + *
24697 + * @param core_if Programming view of DWC_otg controller.
24698 + * @param dest Destination buffer for packet data.
24699 + */
24700 +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
24701 +{
24702 + device_grxsts_data_t status;
24703 + /* Get the 8 bytes of a setup transaction data */
24704 +
24705 + /* Pop 2 DWORDS off the receive data FIFO into memory */
24706 + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
24707 + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
24708 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
24709 + status.d32 =
24710 + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
24711 + DWC_DEBUGPL(DBG_ANY,
24712 + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
24713 + status.b.epnum, status.b.bcnt, status.b.pktsts,
24714 + status.b.fn, status.b.fn);
24715 + }
24716 +}
24717 +
24718 +/**
24719 + * This function enables EP0 OUT to receive SETUP packets and configures EP0
24720 + * IN for transmitting packets. It is normally called when the
24721 + * "Enumeration Done" interrupt occurs.
24722 + *
24723 + * @param core_if Programming view of DWC_otg controller.
24724 + * @param ep The EP0 data.
24725 + */
24726 +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24727 +{
24728 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
24729 + dsts_data_t dsts;
24730 + depctl_data_t diepctl;
24731 + depctl_data_t doepctl;
24732 + dctl_data_t dctl = {.d32 = 0 };
24733 +
24734 + ep->stp_rollover = 0;
24735 + /* Read the Device Status and Endpoint 0 Control registers */
24736 + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
24737 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
24738 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
24739 +
24740 + /* Set the MPS of the IN EP based on the enumeration speed */
24741 + switch (dsts.b.enumspd) {
24742 + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
24743 + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
24744 + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
24745 + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
24746 + break;
24747 + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
24748 + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
24749 + break;
24750 + }
24751 +
24752 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
24753 +
24754 + /* Enable OUT EP for receive */
24755 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
24756 + doepctl.b.epena = 1;
24757 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
24758 + }
24759 +#ifdef VERBOSE
24760 + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
24761 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
24762 + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
24763 + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
24764 +#endif
24765 + dctl.b.cgnpinnak = 1;
24766 +
24767 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
24768 + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
24769 + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
24770 +
24771 +}
24772 +
24773 +/**
24774 + * This function activates an EP. The Device EP control register for
24775 + * the EP is configured as defined in the ep structure. Note: This
24776 + * function is not used for EP0.
24777 + *
24778 + * @param core_if Programming view of DWC_otg controller.
24779 + * @param ep The EP to activate.
24780 + */
24781 +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24782 +{
24783 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
24784 + depctl_data_t depctl;
24785 + volatile uint32_t *addr;
24786 + daint_data_t daintmsk = {.d32 = 0 };
24787 + dcfg_data_t dcfg;
24788 + uint8_t i;
24789 +
24790 + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
24791 + (ep->is_in ? "IN" : "OUT"));
24792 +
24793 +#ifdef DWC_UTE_PER_IO
24794 + ep->xiso_frame_num = 0xFFFFFFFF;
24795 + ep->xiso_active_xfers = 0;
24796 + ep->xiso_queued_xfers = 0;
24797 +#endif
24798 + /* Read DEPCTLn register */
24799 + if (ep->is_in == 1) {
24800 + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
24801 + daintmsk.ep.in = 1 << ep->num;
24802 + } else {
24803 + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
24804 + daintmsk.ep.out = 1 << ep->num;
24805 + }
24806 +
24807 + /* If the EP is already active don't change the EP Control
24808 + * register. */
24809 + depctl.d32 = DWC_READ_REG32(addr);
24810 + if (!depctl.b.usbactep) {
24811 + depctl.b.mps = ep->maxpacket;
24812 + depctl.b.eptype = ep->type;
24813 + depctl.b.txfnum = ep->tx_fifo_num;
24814 +
24815 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
24816 + depctl.b.setd0pid = 1; // ???
24817 + } else {
24818 + depctl.b.setd0pid = 1;
24819 + }
24820 + depctl.b.usbactep = 1;
24821 +
24822 + /* Update nextep_seq array and EPMSCNT in DCFG*/
24823 + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
24824 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
24825 + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
24826 + break;
24827 + }
24828 + core_if->nextep_seq[i] = ep->num;
24829 + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
24830 + depctl.b.nextep = core_if->nextep_seq[ep->num];
24831 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
24832 + dcfg.b.epmscnt++;
24833 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
24834 +
24835 + DWC_DEBUGPL(DBG_PCDV,
24836 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
24837 + __func__, core_if->first_in_nextep_seq);
24838 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
24839 + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
24840 + core_if->nextep_seq[i]);
24841 + }
24842 +
24843 + }
24844 +
24845 +
24846 + DWC_WRITE_REG32(addr, depctl.d32);
24847 + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
24848 + }
24849 +
24850 + /* Enable the Interrupt for this EP */
24851 + if (core_if->multiproc_int_enable) {
24852 + if (ep->is_in == 1) {
24853 + diepmsk_data_t diepmsk = {.d32 = 0 };
24854 + diepmsk.b.xfercompl = 1;
24855 + diepmsk.b.timeout = 1;
24856 + diepmsk.b.epdisabled = 1;
24857 + diepmsk.b.ahberr = 1;
24858 + diepmsk.b.intknepmis = 1;
24859 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
24860 + diepmsk.b.intknepmis = 0;
24861 + diepmsk.b.txfifoundrn = 1; //?????
24862 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
24863 + diepmsk.b.nak = 1;
24864 + }
24865 +
24866 +
24867 +
24868 +/*
24869 + if (core_if->dma_desc_enable) {
24870 + diepmsk.b.bna = 1;
24871 + }
24872 +*/
24873 +/*
24874 + if (core_if->dma_enable) {
24875 + doepmsk.b.nak = 1;
24876 + }
24877 +*/
24878 + DWC_WRITE_REG32(&dev_if->dev_global_regs->
24879 + diepeachintmsk[ep->num], diepmsk.d32);
24880 +
24881 + } else {
24882 + doepmsk_data_t doepmsk = {.d32 = 0 };
24883 + doepmsk.b.xfercompl = 1;
24884 + doepmsk.b.ahberr = 1;
24885 + doepmsk.b.epdisabled = 1;
24886 + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
24887 + doepmsk.b.outtknepdis = 1;
24888 +
24889 +/*
24890 +
24891 + if (core_if->dma_desc_enable) {
24892 + doepmsk.b.bna = 1;
24893 + }
24894 +*/
24895 +/*
24896 + doepmsk.b.babble = 1;
24897 + doepmsk.b.nyet = 1;
24898 + doepmsk.b.nak = 1;
24899 +*/
24900 + DWC_WRITE_REG32(&dev_if->dev_global_regs->
24901 + doepeachintmsk[ep->num], doepmsk.d32);
24902 + }
24903 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
24904 + 0, daintmsk.d32);
24905 + } else {
24906 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
24907 + if (ep->is_in) {
24908 + diepmsk_data_t diepmsk = {.d32 = 0 };
24909 + diepmsk.b.nak = 1;
24910 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
24911 + } else {
24912 + doepmsk_data_t doepmsk = {.d32 = 0 };
24913 + doepmsk.b.outtknepdis = 1;
24914 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
24915 + }
24916 + }
24917 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
24918 + 0, daintmsk.d32);
24919 + }
24920 +
24921 + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
24922 + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
24923 +
24924 + ep->stall_clear_flag = 0;
24925 +
24926 + return;
24927 +}
24928 +
24929 +/**
24930 + * This function deactivates an EP. This is done by clearing the USB Active
24931 + * EP bit in the Device EP control register. Note: This function is not used
24932 + * for EP0. EP0 cannot be deactivated.
24933 + *
24934 + * @param core_if Programming view of DWC_otg controller.
24935 + * @param ep The EP to deactivate.
24936 + */
24937 +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24938 +{
24939 + depctl_data_t depctl = {.d32 = 0 };
24940 + volatile uint32_t *addr;
24941 + daint_data_t daintmsk = {.d32 = 0 };
24942 + dcfg_data_t dcfg;
24943 + uint8_t i = 0;
24944 +
24945 +#ifdef DWC_UTE_PER_IO
24946 + ep->xiso_frame_num = 0xFFFFFFFF;
24947 + ep->xiso_active_xfers = 0;
24948 + ep->xiso_queued_xfers = 0;
24949 +#endif
24950 +
24951 + /* Read DEPCTLn register */
24952 + if (ep->is_in == 1) {
24953 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
24954 + daintmsk.ep.in = 1 << ep->num;
24955 + } else {
24956 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
24957 + daintmsk.ep.out = 1 << ep->num;
24958 + }
24959 +
24960 + depctl.d32 = DWC_READ_REG32(addr);
24961 +
24962 + depctl.b.usbactep = 0;
24963 +
24964 + /* Update nextep_seq array and EPMSCNT in DCFG*/
24965 + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
24966 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
24967 + if (core_if->nextep_seq[i] == ep->num)
24968 + break;
24969 + }
24970 + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
24971 + if (core_if->first_in_nextep_seq == ep->num)
24972 + core_if->first_in_nextep_seq = i;
24973 + core_if->nextep_seq[ep->num] = 0xff;
24974 + depctl.b.nextep = 0;
24975 + dcfg.d32 =
24976 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
24977 + dcfg.b.epmscnt--;
24978 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
24979 + dcfg.d32);
24980 +
24981 + DWC_DEBUGPL(DBG_PCDV,
24982 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
24983 + __func__, core_if->first_in_nextep_seq);
24984 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
24985 + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
24986 + }
24987 + }
24988 +
24989 + if (ep->is_in == 1)
24990 + depctl.b.txfnum = 0;
24991 +
24992 + if (core_if->dma_desc_enable)
24993 + depctl.b.epdis = 1;
24994 +
24995 + DWC_WRITE_REG32(addr, depctl.d32);
24996 + depctl.d32 = DWC_READ_REG32(addr);
24997 + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
24998 + && depctl.b.epena) {
24999 + depctl_data_t depctl = {.d32 = 0};
25000 + if (ep->is_in) {
25001 + diepint_data_t diepint = {.d32 = 0};
25002 +
25003 + depctl.b.snak = 1;
25004 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25005 + diepctl, depctl.d32);
25006 + do {
25007 + dwc_udelay(10);
25008 + diepint.d32 =
25009 + DWC_READ_REG32(&core_if->
25010 + dev_if->in_ep_regs[ep->num]->
25011 + diepint);
25012 + } while (!diepint.b.inepnakeff);
25013 + diepint.b.inepnakeff = 1;
25014 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25015 + diepint, diepint.d32);
25016 + depctl.d32 = 0;
25017 + depctl.b.epdis = 1;
25018 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25019 + diepctl, depctl.d32);
25020 + do {
25021 + dwc_udelay(10);
25022 + diepint.d32 =
25023 + DWC_READ_REG32(&core_if->
25024 + dev_if->in_ep_regs[ep->num]->
25025 + diepint);
25026 + } while (!diepint.b.epdisabled);
25027 + diepint.b.epdisabled = 1;
25028 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25029 + diepint, diepint.d32);
25030 + } else {
25031 + dctl_data_t dctl = {.d32 = 0};
25032 + gintmsk_data_t gintsts = {.d32 = 0};
25033 + doepint_data_t doepint = {.d32 = 0};
25034 + dctl.b.sgoutnak = 1;
25035 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
25036 + dctl, 0, dctl.d32);
25037 + do {
25038 + dwc_udelay(10);
25039 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
25040 + } while (!gintsts.b.goutnakeff);
25041 + gintsts.d32 = 0;
25042 + gintsts.b.goutnakeff = 1;
25043 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
25044 +
25045 + depctl.d32 = 0;
25046 + depctl.b.epdis = 1;
25047 + depctl.b.snak = 1;
25048 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
25049 + do
25050 + {
25051 + dwc_udelay(10);
25052 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
25053 + out_ep_regs[ep->num]->doepint);
25054 + } while (!doepint.b.epdisabled);
25055 +
25056 + doepint.b.epdisabled = 1;
25057 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
25058 +
25059 + dctl.d32 = 0;
25060 + dctl.b.cgoutnak = 1;
25061 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
25062 + }
25063 + }
25064 +
25065 + /* Disable the Interrupt for this EP */
25066 + if (core_if->multiproc_int_enable) {
25067 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
25068 + daintmsk.d32, 0);
25069 +
25070 + if (ep->is_in == 1) {
25071 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
25072 + diepeachintmsk[ep->num], 0);
25073 + } else {
25074 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
25075 + doepeachintmsk[ep->num], 0);
25076 + }
25077 + } else {
25078 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
25079 + daintmsk.d32, 0);
25080 + }
25081 +
25082 +}
25083 +
25084 +/**
25085 + * This function initializes dma descriptor chain.
25086 + *
25087 + * @param core_if Programming view of DWC_otg controller.
25088 + * @param ep The EP to start the transfer on.
25089 + */
25090 +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25091 +{
25092 + dwc_otg_dev_dma_desc_t *dma_desc;
25093 + uint32_t offset;
25094 + uint32_t xfer_est;
25095 + int i;
25096 + unsigned maxxfer_local, total_len;
25097 +
25098 + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
25099 + (ep->maxpacket%4)) {
25100 + maxxfer_local = ep->maxpacket;
25101 + total_len = ep->xfer_len;
25102 + } else {
25103 + maxxfer_local = ep->maxxfer;
25104 + total_len = ep->total_len;
25105 + }
25106 +
25107 + ep->desc_cnt = (total_len / maxxfer_local) +
25108 + ((total_len % maxxfer_local) ? 1 : 0);
25109 +
25110 + if (!ep->desc_cnt)
25111 + ep->desc_cnt = 1;
25112 +
25113 + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
25114 + ep->desc_cnt = MAX_DMA_DESC_CNT;
25115 +
25116 + dma_desc = ep->desc_addr;
25117 + if (maxxfer_local == ep->maxpacket) {
25118 + if ((total_len % maxxfer_local) &&
25119 + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
25120 + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
25121 + (total_len % maxxfer_local);
25122 + } else
25123 + xfer_est = ep->desc_cnt * maxxfer_local;
25124 + } else
25125 + xfer_est = total_len;
25126 + offset = 0;
25127 + for (i = 0; i < ep->desc_cnt; ++i) {
25128 + /** DMA Descriptor Setup */
25129 + if (xfer_est > maxxfer_local) {
25130 + dma_desc->status.b.bs = BS_HOST_BUSY;
25131 + dma_desc->status.b.l = 0;
25132 + dma_desc->status.b.ioc = 0;
25133 + dma_desc->status.b.sp = 0;
25134 + dma_desc->status.b.bytes = maxxfer_local;
25135 + dma_desc->buf = ep->dma_addr + offset;
25136 + dma_desc->status.b.sts = 0;
25137 + dma_desc->status.b.bs = BS_HOST_READY;
25138 +
25139 + xfer_est -= maxxfer_local;
25140 + offset += maxxfer_local;
25141 + } else {
25142 + dma_desc->status.b.bs = BS_HOST_BUSY;
25143 + dma_desc->status.b.l = 1;
25144 + dma_desc->status.b.ioc = 1;
25145 + if (ep->is_in) {
25146 + dma_desc->status.b.sp =
25147 + (xfer_est %
25148 + ep->maxpacket) ? 1 : ((ep->
25149 + sent_zlp) ? 1 : 0);
25150 + dma_desc->status.b.bytes = xfer_est;
25151 + } else {
25152 + if (maxxfer_local == ep->maxpacket)
25153 + dma_desc->status.b.bytes = xfer_est;
25154 + else
25155 + dma_desc->status.b.bytes =
25156 + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
25157 + }
25158 +
25159 + dma_desc->buf = ep->dma_addr + offset;
25160 + dma_desc->status.b.sts = 0;
25161 + dma_desc->status.b.bs = BS_HOST_READY;
25162 + }
25163 + dma_desc++;
25164 + }
25165 +}
25166 +/**
25167 + * This function is called when to write ISOC data into appropriate dedicated
25168 + * periodic FIFO.
25169 + */
25170 +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
25171 +{
25172 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
25173 + dwc_otg_dev_in_ep_regs_t *ep_regs;
25174 + dtxfsts_data_t txstatus = {.d32 = 0 };
25175 + uint32_t len = 0;
25176 + int epnum = dwc_ep->num;
25177 + int dwords;
25178 +
25179 + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
25180 +
25181 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
25182 +
25183 + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
25184 +
25185 + if (len > dwc_ep->maxpacket) {
25186 + len = dwc_ep->maxpacket;
25187 + }
25188 +
25189 + dwords = (len + 3) / 4;
25190 +
25191 + /* While there is space in the queue and space in the FIFO and
25192 + * More data to tranfer, Write packets to the Tx FIFO */
25193 + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
25194 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
25195 +
25196 + while (txstatus.b.txfspcavail > dwords &&
25197 + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
25198 + /* Write the FIFO */
25199 + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
25200 +
25201 + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
25202 + if (len > dwc_ep->maxpacket) {
25203 + len = dwc_ep->maxpacket;
25204 + }
25205 +
25206 + dwords = (len + 3) / 4;
25207 + txstatus.d32 =
25208 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
25209 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
25210 + txstatus.d32);
25211 + }
25212 +
25213 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
25214 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
25215 +
25216 + return 1;
25217 +}
25218 +/**
25219 + * This function does the setup for a data transfer for an EP and
25220 + * starts the transfer. For an IN transfer, the packets will be
25221 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
25222 + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
25223 + *
25224 + * @param core_if Programming view of DWC_otg controller.
25225 + * @param ep The EP to start the transfer on.
25226 + */
25227 +
25228 +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25229 +{
25230 + depctl_data_t depctl;
25231 + deptsiz_data_t deptsiz;
25232 + gintmsk_data_t intr_mask = {.d32 = 0 };
25233 +
25234 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
25235 + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
25236 + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
25237 + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
25238 + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
25239 + ep->total_len);
25240 + /* IN endpoint */
25241 + if (ep->is_in == 1) {
25242 + dwc_otg_dev_in_ep_regs_t *in_regs =
25243 + core_if->dev_if->in_ep_regs[ep->num];
25244 +
25245 + gnptxsts_data_t gtxstatus;
25246 +
25247 + gtxstatus.d32 =
25248 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25249 +
25250 + if (core_if->en_multiple_tx_fifo == 0
25251 + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
25252 +#ifdef DEBUG
25253 + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
25254 +#endif
25255 + return;
25256 + }
25257 +
25258 + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
25259 + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
25260 +
25261 + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
25262 + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
25263 + ep->maxxfer : (ep->total_len - ep->xfer_len);
25264 + else
25265 + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
25266 + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
25267 +
25268 +
25269 + /* Zero Length Packet? */
25270 + if ((ep->xfer_len - ep->xfer_count) == 0) {
25271 + deptsiz.b.xfersize = 0;
25272 + deptsiz.b.pktcnt = 1;
25273 + } else {
25274 + /* Program the transfer size and packet count
25275 + * as follows: xfersize = N * maxpacket +
25276 + * short_packet pktcnt = N + (short_packet
25277 + * exist ? 1 : 0)
25278 + */
25279 + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
25280 + deptsiz.b.pktcnt =
25281 + (ep->xfer_len - ep->xfer_count - 1 +
25282 + ep->maxpacket) / ep->maxpacket;
25283 + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
25284 + deptsiz.b.pktcnt = MAX_PKT_CNT;
25285 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
25286 + }
25287 + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
25288 + deptsiz.b.mc = deptsiz.b.pktcnt;
25289 + }
25290 +
25291 + /* Write the DMA register */
25292 + if (core_if->dma_enable) {
25293 + if (core_if->dma_desc_enable == 0) {
25294 + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
25295 + deptsiz.b.mc = 1;
25296 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25297 + deptsiz.d32);
25298 + DWC_WRITE_REG32(&(in_regs->diepdma),
25299 + (uint32_t) ep->dma_addr);
25300 + } else {
25301 +#ifdef DWC_UTE_CFI
25302 + /* The descriptor chain should be already initialized by now */
25303 + if (ep->buff_mode != BM_STANDARD) {
25304 + DWC_WRITE_REG32(&in_regs->diepdma,
25305 + ep->descs_dma_addr);
25306 + } else {
25307 +#endif
25308 + init_dma_desc_chain(core_if, ep);
25309 + /** DIEPDMAn Register write */
25310 + DWC_WRITE_REG32(&in_regs->diepdma,
25311 + ep->dma_desc_addr);
25312 +#ifdef DWC_UTE_CFI
25313 + }
25314 +#endif
25315 + }
25316 + } else {
25317 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25318 + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
25319 + /**
25320 + * Enable the Non-Periodic Tx FIFO empty interrupt,
25321 + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
25322 + * the data will be written into the fifo by the ISR.
25323 + */
25324 + if (core_if->en_multiple_tx_fifo == 0) {
25325 + intr_mask.b.nptxfempty = 1;
25326 + DWC_MODIFY_REG32
25327 + (&core_if->core_global_regs->gintmsk,
25328 + intr_mask.d32, intr_mask.d32);
25329 + } else {
25330 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25331 + if (ep->xfer_len > 0) {
25332 + uint32_t fifoemptymsk = 0;
25333 + fifoemptymsk = 1 << ep->num;
25334 + DWC_MODIFY_REG32
25335 + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25336 + 0, fifoemptymsk);
25337 +
25338 + }
25339 + }
25340 + } else {
25341 + write_isoc_tx_fifo(core_if, ep);
25342 + }
25343 + }
25344 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25345 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25346 +
25347 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25348 + dsts_data_t dsts = {.d32 = 0};
25349 + if (ep->bInterval == 1) {
25350 + dsts.d32 =
25351 + DWC_READ_REG32(&core_if->dev_if->
25352 + dev_global_regs->dsts);
25353 + ep->frame_num = dsts.b.soffn + ep->bInterval;
25354 + if (ep->frame_num > 0x3FFF) {
25355 + ep->frm_overrun = 1;
25356 + ep->frame_num &= 0x3FFF;
25357 + } else
25358 + ep->frm_overrun = 0;
25359 + if (ep->frame_num & 0x1) {
25360 + depctl.b.setd1pid = 1;
25361 + } else {
25362 + depctl.b.setd0pid = 1;
25363 + }
25364 + }
25365 + }
25366 + /* EP enable, IN data in FIFO */
25367 + depctl.b.cnak = 1;
25368 + depctl.b.epena = 1;
25369 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25370 +
25371 + } else {
25372 + /* OUT endpoint */
25373 + dwc_otg_dev_out_ep_regs_t *out_regs =
25374 + core_if->dev_if->out_ep_regs[ep->num];
25375 +
25376 + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
25377 + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
25378 +
25379 + if (!core_if->dma_desc_enable) {
25380 + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
25381 + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
25382 + ep->maxxfer : (ep->total_len - ep->xfer_len);
25383 + else
25384 + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
25385 + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
25386 + }
25387 +
25388 + /* Program the transfer size and packet count as follows:
25389 + *
25390 + * pktcnt = N
25391 + * xfersize = N * maxpacket
25392 + */
25393 + if ((ep->xfer_len - ep->xfer_count) == 0) {
25394 + /* Zero Length Packet */
25395 + deptsiz.b.xfersize = ep->maxpacket;
25396 + deptsiz.b.pktcnt = 1;
25397 + } else {
25398 + deptsiz.b.pktcnt =
25399 + (ep->xfer_len - ep->xfer_count +
25400 + (ep->maxpacket - 1)) / ep->maxpacket;
25401 + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
25402 + deptsiz.b.pktcnt = MAX_PKT_CNT;
25403 + }
25404 + if (!core_if->dma_desc_enable) {
25405 + ep->xfer_len =
25406 + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
25407 + }
25408 + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
25409 + }
25410 +
25411 + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
25412 + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
25413 +
25414 + if (core_if->dma_enable) {
25415 + if (!core_if->dma_desc_enable) {
25416 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25417 + deptsiz.d32);
25418 +
25419 + DWC_WRITE_REG32(&(out_regs->doepdma),
25420 + (uint32_t) ep->dma_addr);
25421 + } else {
25422 +#ifdef DWC_UTE_CFI
25423 + /* The descriptor chain should be already initialized by now */
25424 + if (ep->buff_mode != BM_STANDARD) {
25425 + DWC_WRITE_REG32(&out_regs->doepdma,
25426 + ep->descs_dma_addr);
25427 + } else {
25428 +#endif
25429 + /** This is used for interrupt out transfers*/
25430 + if (!ep->xfer_len)
25431 + ep->xfer_len = ep->total_len;
25432 + init_dma_desc_chain(core_if, ep);
25433 +
25434 + if (core_if->core_params->dev_out_nak) {
25435 + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
25436 + deptsiz.b.pktcnt = (ep->total_len +
25437 + (ep->maxpacket - 1)) / ep->maxpacket;
25438 + deptsiz.b.xfersize = ep->total_len;
25439 + /* Remember initial value of doeptsiz */
25440 + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
25441 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25442 + deptsiz.d32);
25443 + }
25444 + }
25445 + /** DOEPDMAn Register write */
25446 + DWC_WRITE_REG32(&out_regs->doepdma,
25447 + ep->dma_desc_addr);
25448 +#ifdef DWC_UTE_CFI
25449 + }
25450 +#endif
25451 + }
25452 + } else {
25453 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25454 + }
25455 +
25456 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25457 + dsts_data_t dsts = {.d32 = 0};
25458 + if (ep->bInterval == 1) {
25459 + dsts.d32 =
25460 + DWC_READ_REG32(&core_if->dev_if->
25461 + dev_global_regs->dsts);
25462 + ep->frame_num = dsts.b.soffn + ep->bInterval;
25463 + if (ep->frame_num > 0x3FFF) {
25464 + ep->frm_overrun = 1;
25465 + ep->frame_num &= 0x3FFF;
25466 + } else
25467 + ep->frm_overrun = 0;
25468 +
25469 + if (ep->frame_num & 0x1) {
25470 + depctl.b.setd1pid = 1;
25471 + } else {
25472 + depctl.b.setd0pid = 1;
25473 + }
25474 + }
25475 + }
25476 +
25477 + /* EP enable */
25478 + depctl.b.cnak = 1;
25479 + depctl.b.epena = 1;
25480 +
25481 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25482 +
25483 + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
25484 + DWC_READ_REG32(&out_regs->doepctl),
25485 + DWC_READ_REG32(&out_regs->doeptsiz));
25486 + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
25487 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
25488 + daintmsk),
25489 + DWC_READ_REG32(&core_if->core_global_regs->
25490 + gintmsk));
25491 +
25492 + /* Timer is scheduling only for out bulk transfers for
25493 + * "Device DDMA OUT NAK Enhancement" feature to inform user
25494 + * about received data payload in case of timeout
25495 + */
25496 + if (core_if->core_params->dev_out_nak) {
25497 + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
25498 + core_if->ep_xfer_info[ep->num].core_if = core_if;
25499 + core_if->ep_xfer_info[ep->num].ep = ep;
25500 + core_if->ep_xfer_info[ep->num].state = 1;
25501 +
25502 + /* Start a timer for this transfer. */
25503 + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
25504 + }
25505 + }
25506 + }
25507 +}
25508 +
25509 +/**
25510 + * This function setup a zero length transfer in Buffer DMA and
25511 + * Slave modes for usb requests with zero field set
25512 + *
25513 + * @param core_if Programming view of DWC_otg controller.
25514 + * @param ep The EP to start the transfer on.
25515 + *
25516 + */
25517 +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25518 +{
25519 +
25520 + depctl_data_t depctl;
25521 + deptsiz_data_t deptsiz;
25522 + gintmsk_data_t intr_mask = {.d32 = 0 };
25523 +
25524 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
25525 + DWC_PRINTF("zero length transfer is called\n");
25526 +
25527 + /* IN endpoint */
25528 + if (ep->is_in == 1) {
25529 + dwc_otg_dev_in_ep_regs_t *in_regs =
25530 + core_if->dev_if->in_ep_regs[ep->num];
25531 +
25532 + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
25533 + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
25534 +
25535 + deptsiz.b.xfersize = 0;
25536 + deptsiz.b.pktcnt = 1;
25537 +
25538 + /* Write the DMA register */
25539 + if (core_if->dma_enable) {
25540 + if (core_if->dma_desc_enable == 0) {
25541 + deptsiz.b.mc = 1;
25542 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25543 + deptsiz.d32);
25544 + DWC_WRITE_REG32(&(in_regs->diepdma),
25545 + (uint32_t) ep->dma_addr);
25546 + }
25547 + } else {
25548 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25549 + /**
25550 + * Enable the Non-Periodic Tx FIFO empty interrupt,
25551 + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
25552 + * the data will be written into the fifo by the ISR.
25553 + */
25554 + if (core_if->en_multiple_tx_fifo == 0) {
25555 + intr_mask.b.nptxfempty = 1;
25556 + DWC_MODIFY_REG32(&core_if->
25557 + core_global_regs->gintmsk,
25558 + intr_mask.d32, intr_mask.d32);
25559 + } else {
25560 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25561 + if (ep->xfer_len > 0) {
25562 + uint32_t fifoemptymsk = 0;
25563 + fifoemptymsk = 1 << ep->num;
25564 + DWC_MODIFY_REG32(&core_if->
25565 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25566 + 0, fifoemptymsk);
25567 + }
25568 + }
25569 + }
25570 +
25571 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25572 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25573 + /* EP enable, IN data in FIFO */
25574 + depctl.b.cnak = 1;
25575 + depctl.b.epena = 1;
25576 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25577 +
25578 + } else {
25579 + /* OUT endpoint */
25580 + dwc_otg_dev_out_ep_regs_t *out_regs =
25581 + core_if->dev_if->out_ep_regs[ep->num];
25582 +
25583 + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
25584 + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
25585 +
25586 + /* Zero Length Packet */
25587 + deptsiz.b.xfersize = ep->maxpacket;
25588 + deptsiz.b.pktcnt = 1;
25589 +
25590 + if (core_if->dma_enable) {
25591 + if (!core_if->dma_desc_enable) {
25592 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25593 + deptsiz.d32);
25594 +
25595 + DWC_WRITE_REG32(&(out_regs->doepdma),
25596 + (uint32_t) ep->dma_addr);
25597 + }
25598 + } else {
25599 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25600 + }
25601 +
25602 + /* EP enable */
25603 + depctl.b.cnak = 1;
25604 + depctl.b.epena = 1;
25605 +
25606 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25607 +
25608 + }
25609 +}
25610 +
25611 +/**
25612 + * This function does the setup for a data transfer for EP0 and starts
25613 + * the transfer. For an IN transfer, the packets will be loaded into
25614 + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
25615 + * unloaded from the Rx FIFO in the ISR.
25616 + *
25617 + * @param core_if Programming view of DWC_otg controller.
25618 + * @param ep The EP0 data.
25619 + */
25620 +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25621 +{
25622 + depctl_data_t depctl;
25623 + deptsiz0_data_t deptsiz;
25624 + gintmsk_data_t intr_mask = {.d32 = 0 };
25625 + dwc_otg_dev_dma_desc_t *dma_desc;
25626 +
25627 + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
25628 + "xfer_buff=%p start_xfer_buff=%p \n",
25629 + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
25630 + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
25631 +
25632 + ep->total_len = ep->xfer_len;
25633 +
25634 + /* IN endpoint */
25635 + if (ep->is_in == 1) {
25636 + dwc_otg_dev_in_ep_regs_t *in_regs =
25637 + core_if->dev_if->in_ep_regs[0];
25638 +
25639 + gnptxsts_data_t gtxstatus;
25640 +
25641 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
25642 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25643 + if (depctl.b.epena)
25644 + return;
25645 + }
25646 +
25647 + gtxstatus.d32 =
25648 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25649 +
25650 + /* If dedicated FIFO every time flush fifo before enable ep*/
25651 + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
25652 + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
25653 +
25654 + if (core_if->en_multiple_tx_fifo == 0
25655 + && gtxstatus.b.nptxqspcavail == 0
25656 + && !core_if->dma_enable) {
25657 +#ifdef DEBUG
25658 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25659 + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
25660 + DWC_READ_REG32(&in_regs->diepctl));
25661 + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
25662 + deptsiz.d32,
25663 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
25664 + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
25665 + gtxstatus.d32);
25666 +#endif
25667 + return;
25668 + }
25669 +
25670 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25671 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25672 +
25673 + /* Zero Length Packet? */
25674 + if (ep->xfer_len == 0) {
25675 + deptsiz.b.xfersize = 0;
25676 + deptsiz.b.pktcnt = 1;
25677 + } else {
25678 + /* Program the transfer size and packet count
25679 + * as follows: xfersize = N * maxpacket +
25680 + * short_packet pktcnt = N + (short_packet
25681 + * exist ? 1 : 0)
25682 + */
25683 + if (ep->xfer_len > ep->maxpacket) {
25684 + ep->xfer_len = ep->maxpacket;
25685 + deptsiz.b.xfersize = ep->maxpacket;
25686 + } else {
25687 + deptsiz.b.xfersize = ep->xfer_len;
25688 + }
25689 + deptsiz.b.pktcnt = 1;
25690 +
25691 + }
25692 + DWC_DEBUGPL(DBG_PCDV,
25693 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
25694 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
25695 + deptsiz.d32);
25696 +
25697 + /* Write the DMA register */
25698 + if (core_if->dma_enable) {
25699 + if (core_if->dma_desc_enable == 0) {
25700 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25701 + deptsiz.d32);
25702 +
25703 + DWC_WRITE_REG32(&(in_regs->diepdma),
25704 + (uint32_t) ep->dma_addr);
25705 + } else {
25706 + dma_desc = core_if->dev_if->in_desc_addr;
25707 +
25708 + /** DMA Descriptor Setup */
25709 + dma_desc->status.b.bs = BS_HOST_BUSY;
25710 + dma_desc->status.b.l = 1;
25711 + dma_desc->status.b.ioc = 1;
25712 + dma_desc->status.b.sp =
25713 + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
25714 + dma_desc->status.b.bytes = ep->xfer_len;
25715 + dma_desc->buf = ep->dma_addr;
25716 + dma_desc->status.b.sts = 0;
25717 + dma_desc->status.b.bs = BS_HOST_READY;
25718 +
25719 + /** DIEPDMA0 Register write */
25720 + DWC_WRITE_REG32(&in_regs->diepdma,
25721 + core_if->
25722 + dev_if->dma_in_desc_addr);
25723 + }
25724 + } else {
25725 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25726 + }
25727 +
25728 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25729 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25730 + /* EP enable, IN data in FIFO */
25731 + depctl.b.cnak = 1;
25732 + depctl.b.epena = 1;
25733 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25734 +
25735 + /**
25736 + * Enable the Non-Periodic Tx FIFO empty interrupt, the
25737 + * data will be written into the fifo by the ISR.
25738 + */
25739 + if (!core_if->dma_enable) {
25740 + if (core_if->en_multiple_tx_fifo == 0) {
25741 + intr_mask.b.nptxfempty = 1;
25742 + DWC_MODIFY_REG32(&core_if->
25743 + core_global_regs->gintmsk,
25744 + intr_mask.d32, intr_mask.d32);
25745 + } else {
25746 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25747 + if (ep->xfer_len > 0) {
25748 + uint32_t fifoemptymsk = 0;
25749 + fifoemptymsk |= 1 << ep->num;
25750 + DWC_MODIFY_REG32(&core_if->
25751 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25752 + 0, fifoemptymsk);
25753 + }
25754 + }
25755 + }
25756 + } else {
25757 + /* OUT endpoint */
25758 + dwc_otg_dev_out_ep_regs_t *out_regs =
25759 + core_if->dev_if->out_ep_regs[0];
25760 +
25761 + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
25762 + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
25763 +
25764 + /* Program the transfer size and packet count as follows:
25765 + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
25766 + * pktcnt = N */
25767 + /* Zero Length Packet */
25768 + deptsiz.b.xfersize = ep->maxpacket;
25769 + deptsiz.b.pktcnt = 1;
25770 + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
25771 + deptsiz.b.supcnt = 3;
25772 +
25773 + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
25774 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
25775 +
25776 + if (core_if->dma_enable) {
25777 + if (!core_if->dma_desc_enable) {
25778 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25779 + deptsiz.d32);
25780 +
25781 + DWC_WRITE_REG32(&(out_regs->doepdma),
25782 + (uint32_t) ep->dma_addr);
25783 + } else {
25784 + dma_desc = core_if->dev_if->out_desc_addr;
25785 +
25786 + /** DMA Descriptor Setup */
25787 + dma_desc->status.b.bs = BS_HOST_BUSY;
25788 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
25789 + dma_desc->status.b.mtrf = 0;
25790 + dma_desc->status.b.sr = 0;
25791 + }
25792 + dma_desc->status.b.l = 1;
25793 + dma_desc->status.b.ioc = 1;
25794 + dma_desc->status.b.bytes = ep->maxpacket;
25795 + dma_desc->buf = ep->dma_addr;
25796 + dma_desc->status.b.sts = 0;
25797 + dma_desc->status.b.bs = BS_HOST_READY;
25798 +
25799 + /** DOEPDMA0 Register write */
25800 + DWC_WRITE_REG32(&out_regs->doepdma,
25801 + core_if->dev_if->
25802 + dma_out_desc_addr);
25803 + }
25804 + } else {
25805 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25806 + }
25807 +
25808 + /* EP enable */
25809 + depctl.b.cnak = 1;
25810 + depctl.b.epena = 1;
25811 + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
25812 + }
25813 +}
25814 +
25815 +/**
25816 + * This function continues control IN transfers started by
25817 + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
25818 + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
25819 + * bit for the packet count.
25820 + *
25821 + * @param core_if Programming view of DWC_otg controller.
25822 + * @param ep The EP0 data.
25823 + */
25824 +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25825 +{
25826 + depctl_data_t depctl;
25827 + deptsiz0_data_t deptsiz;
25828 + gintmsk_data_t intr_mask = {.d32 = 0 };
25829 + dwc_otg_dev_dma_desc_t *dma_desc;
25830 +
25831 + if (ep->is_in == 1) {
25832 + dwc_otg_dev_in_ep_regs_t *in_regs =
25833 + core_if->dev_if->in_ep_regs[0];
25834 + gnptxsts_data_t tx_status = {.d32 = 0 };
25835 +
25836 + tx_status.d32 =
25837 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25838 + /** @todo Should there be check for room in the Tx
25839 + * Status Queue. If not remove the code above this comment. */
25840 +
25841 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25842 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25843 +
25844 + /* Program the transfer size and packet count
25845 + * as follows: xfersize = N * maxpacket +
25846 + * short_packet pktcnt = N + (short_packet
25847 + * exist ? 1 : 0)
25848 + */
25849 +
25850 + if (core_if->dma_desc_enable == 0) {
25851 + deptsiz.b.xfersize =
25852 + (ep->total_len - ep->xfer_count) >
25853 + ep->maxpacket ? ep->maxpacket : (ep->total_len -
25854 + ep->xfer_count);
25855 + deptsiz.b.pktcnt = 1;
25856 + if (core_if->dma_enable == 0) {
25857 + ep->xfer_len += deptsiz.b.xfersize;
25858 + } else {
25859 + ep->xfer_len = deptsiz.b.xfersize;
25860 + }
25861 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25862 + } else {
25863 + ep->xfer_len =
25864 + (ep->total_len - ep->xfer_count) >
25865 + ep->maxpacket ? ep->maxpacket : (ep->total_len -
25866 + ep->xfer_count);
25867 +
25868 + dma_desc = core_if->dev_if->in_desc_addr;
25869 +
25870 + /** DMA Descriptor Setup */
25871 + dma_desc->status.b.bs = BS_HOST_BUSY;
25872 + dma_desc->status.b.l = 1;
25873 + dma_desc->status.b.ioc = 1;
25874 + dma_desc->status.b.sp =
25875 + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
25876 + dma_desc->status.b.bytes = ep->xfer_len;
25877 + dma_desc->buf = ep->dma_addr;
25878 + dma_desc->status.b.sts = 0;
25879 + dma_desc->status.b.bs = BS_HOST_READY;
25880 +
25881 + /** DIEPDMA0 Register write */
25882 + DWC_WRITE_REG32(&in_regs->diepdma,
25883 + core_if->dev_if->dma_in_desc_addr);
25884 + }
25885 +
25886 + DWC_DEBUGPL(DBG_PCDV,
25887 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
25888 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
25889 + deptsiz.d32);
25890 +
25891 + /* Write the DMA register */
25892 + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
25893 + if (core_if->dma_desc_enable == 0)
25894 + DWC_WRITE_REG32(&(in_regs->diepdma),
25895 + (uint32_t) ep->dma_addr);
25896 + }
25897 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25898 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25899 + /* EP enable, IN data in FIFO */
25900 + depctl.b.cnak = 1;
25901 + depctl.b.epena = 1;
25902 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25903 +
25904 + /**
25905 + * Enable the Non-Periodic Tx FIFO empty interrupt, the
25906 + * data will be written into the fifo by the ISR.
25907 + */
25908 + if (!core_if->dma_enable) {
25909 + if (core_if->en_multiple_tx_fifo == 0) {
25910 + /* First clear it from GINTSTS */
25911 + intr_mask.b.nptxfempty = 1;
25912 + DWC_MODIFY_REG32(&core_if->
25913 + core_global_regs->gintmsk,
25914 + intr_mask.d32, intr_mask.d32);
25915 +
25916 + } else {
25917 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25918 + if (ep->xfer_len > 0) {
25919 + uint32_t fifoemptymsk = 0;
25920 + fifoemptymsk |= 1 << ep->num;
25921 + DWC_MODIFY_REG32(&core_if->
25922 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25923 + 0, fifoemptymsk);
25924 + }
25925 + }
25926 + }
25927 + } else {
25928 + dwc_otg_dev_out_ep_regs_t *out_regs =
25929 + core_if->dev_if->out_ep_regs[0];
25930 +
25931 + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
25932 + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
25933 +
25934 + /* Program the transfer size and packet count
25935 + * as follows: xfersize = N * maxpacket +
25936 + * short_packet pktcnt = N + (short_packet
25937 + * exist ? 1 : 0)
25938 + */
25939 + deptsiz.b.xfersize = ep->maxpacket;
25940 + deptsiz.b.pktcnt = 1;
25941 +
25942 + if (core_if->dma_desc_enable == 0) {
25943 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25944 + } else {
25945 + dma_desc = core_if->dev_if->out_desc_addr;
25946 +
25947 + /** DMA Descriptor Setup */
25948 + dma_desc->status.b.bs = BS_HOST_BUSY;
25949 + dma_desc->status.b.l = 1;
25950 + dma_desc->status.b.ioc = 1;
25951 + dma_desc->status.b.bytes = ep->maxpacket;
25952 + dma_desc->buf = ep->dma_addr;
25953 + dma_desc->status.b.sts = 0;
25954 + dma_desc->status.b.bs = BS_HOST_READY;
25955 +
25956 + /** DOEPDMA0 Register write */
25957 + DWC_WRITE_REG32(&out_regs->doepdma,
25958 + core_if->dev_if->dma_out_desc_addr);
25959 + }
25960 +
25961 + DWC_DEBUGPL(DBG_PCDV,
25962 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
25963 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
25964 + deptsiz.d32);
25965 +
25966 + /* Write the DMA register */
25967 + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
25968 + if (core_if->dma_desc_enable == 0)
25969 + DWC_WRITE_REG32(&(out_regs->doepdma),
25970 + (uint32_t) ep->dma_addr);
25971 +
25972 + }
25973 +
25974 + /* EP enable, IN data in FIFO */
25975 + depctl.b.cnak = 1;
25976 + depctl.b.epena = 1;
25977 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25978 +
25979 + }
25980 +}
25981 +
25982 +#ifdef DEBUG
25983 +void dump_msg(const u8 * buf, unsigned int length)
25984 +{
25985 + unsigned int start, num, i;
25986 + char line[52], *p;
25987 +
25988 + if (length >= 512)
25989 + return;
25990 + start = 0;
25991 + while (length > 0) {
25992 + num = length < 16u ? length : 16u;
25993 + p = line;
25994 + for (i = 0; i < num; ++i) {
25995 + if (i == 8)
25996 + *p++ = ' ';
25997 + DWC_SPRINTF(p, " %02x", buf[i]);
25998 + p += 3;
25999 + }
26000 + *p = 0;
26001 + DWC_PRINTF("%6x: %s\n", start, line);
26002 + buf += num;
26003 + start += num;
26004 + length -= num;
26005 + }
26006 +}
26007 +#else
26008 +static inline void dump_msg(const u8 * buf, unsigned int length)
26009 +{
26010 +}
26011 +#endif
26012 +
26013 +/**
26014 + * This function writes a packet into the Tx FIFO associated with the
26015 + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
26016 + * periodic EPs the periodic Tx FIFO associated with the EP is written
26017 + * with all packets for the next micro-frame.
26018 + *
26019 + * @param core_if Programming view of DWC_otg controller.
26020 + * @param ep The EP to write packet for.
26021 + * @param dma Indicates if DMA is being used.
26022 + */
26023 +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
26024 + int dma)
26025 +{
26026 + /**
26027 + * The buffer is padded to DWORD on a per packet basis in
26028 + * slave/dma mode if the MPS is not DWORD aligned. The last
26029 + * packet, if short, is also padded to a multiple of DWORD.
26030 + *
26031 + * ep->xfer_buff always starts DWORD aligned in memory and is a
26032 + * multiple of DWORD in length
26033 + *
26034 + * ep->xfer_len can be any number of bytes
26035 + *
26036 + * ep->xfer_count is a multiple of ep->maxpacket until the last
26037 + * packet
26038 + *
26039 + * FIFO access is DWORD */
26040 +
26041 + uint32_t i;
26042 + uint32_t byte_count;
26043 + uint32_t dword_count;
26044 + uint32_t *fifo;
26045 + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
26046 +
26047 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
26048 + ep);
26049 + if (ep->xfer_count >= ep->xfer_len) {
26050 + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
26051 + return;
26052 + }
26053 +
26054 + /* Find the byte length of the packet either short packet or MPS */
26055 + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
26056 + byte_count = ep->xfer_len - ep->xfer_count;
26057 + } else {
26058 + byte_count = ep->maxpacket;
26059 + }
26060 +
26061 + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
26062 + * is not a multiple of DWORD */
26063 + dword_count = (byte_count + 3) / 4;
26064 +
26065 +#ifdef VERBOSE
26066 + dump_msg(ep->xfer_buff, byte_count);
26067 +#endif
26068 +
26069 + /**@todo NGS Where are the Periodic Tx FIFO addresses
26070 + * intialized? What should this be? */
26071 +
26072 + fifo = core_if->data_fifo[ep->num];
26073 +
26074 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
26075 + fifo, data_buff, *data_buff, byte_count);
26076 +
26077 + if (!dma) {
26078 + for (i = 0; i < dword_count; i++, data_buff++) {
26079 + DWC_WRITE_REG32(fifo, *data_buff);
26080 + }
26081 + }
26082 +
26083 + ep->xfer_count += byte_count;
26084 + ep->xfer_buff += byte_count;
26085 + ep->dma_addr += byte_count;
26086 +}
26087 +
26088 +/**
26089 + * Set the EP STALL.
26090 + *
26091 + * @param core_if Programming view of DWC_otg controller.
26092 + * @param ep The EP to set the stall on.
26093 + */
26094 +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26095 +{
26096 + depctl_data_t depctl;
26097 + volatile uint32_t *depctl_addr;
26098 +
26099 + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
26100 + (ep->is_in ? "IN" : "OUT"));
26101 +
26102 + if (ep->is_in == 1) {
26103 + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
26104 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26105 +
26106 + /* set the disable and stall bits */
26107 + if (depctl.b.epena) {
26108 + depctl.b.epdis = 1;
26109 + }
26110 + depctl.b.stall = 1;
26111 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26112 + } else {
26113 + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
26114 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26115 +
26116 + /* set the stall bit */
26117 + depctl.b.stall = 1;
26118 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26119 + }
26120 +
26121 + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
26122 +
26123 + return;
26124 +}
26125 +
26126 +/**
26127 + * Clear the EP STALL.
26128 + *
26129 + * @param core_if Programming view of DWC_otg controller.
26130 + * @param ep The EP to clear stall from.
26131 + */
26132 +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26133 +{
26134 + depctl_data_t depctl;
26135 + volatile uint32_t *depctl_addr;
26136 +
26137 + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
26138 + (ep->is_in ? "IN" : "OUT"));
26139 +
26140 + if (ep->is_in == 1) {
26141 + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
26142 + } else {
26143 + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
26144 + }
26145 +
26146 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26147 +
26148 + /* clear the stall bits */
26149 + depctl.b.stall = 0;
26150 +
26151 + /*
26152 + * USB Spec 9.4.5: For endpoints using data toggle, regardless
26153 + * of whether an endpoint has the Halt feature set, a
26154 + * ClearFeature(ENDPOINT_HALT) request always results in the
26155 + * data toggle being reinitialized to DATA0.
26156 + */
26157 + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
26158 + ep->type == DWC_OTG_EP_TYPE_BULK) {
26159 + depctl.b.setd0pid = 1; /* DATA0 */
26160 + }
26161 +
26162 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26163 + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
26164 + return;
26165 +}
26166 +
26167 +/**
26168 + * This function reads a packet from the Rx FIFO into the destination
26169 + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
26170 + *
26171 + * @param core_if Programming view of DWC_otg controller.
26172 + * @param dest Destination buffer for the packet.
26173 + * @param bytes Number of bytes to copy to the destination.
26174 + */
26175 +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
26176 + uint8_t * dest, uint16_t bytes)
26177 +{
26178 + int i;
26179 + int word_count = (bytes + 3) / 4;
26180 +
26181 + volatile uint32_t *fifo = core_if->data_fifo[0];
26182 + uint32_t *data_buff = (uint32_t *) dest;
26183 +
26184 + /**
26185 + * @todo Account for the case where _dest is not dword aligned. This
26186 + * requires reading data from the FIFO into a uint32_t temp buffer,
26187 + * then moving it into the data buffer.
26188 + */
26189 +
26190 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
26191 + core_if, dest, bytes);
26192 +
26193 + for (i = 0; i < word_count; i++, data_buff++) {
26194 + *data_buff = DWC_READ_REG32(fifo);
26195 + }
26196 +
26197 + return;
26198 +}
26199 +
26200 +/**
26201 + * This functions reads the device registers and prints them
26202 + *
26203 + * @param core_if Programming view of DWC_otg controller.
26204 + */
26205 +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
26206 +{
26207 + int i;
26208 + volatile uint32_t *addr;
26209 +
26210 + DWC_PRINTF("Device Global Registers\n");
26211 + addr = &core_if->dev_if->dev_global_regs->dcfg;
26212 + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
26213 + (unsigned long)addr, DWC_READ_REG32(addr));
26214 + addr = &core_if->dev_if->dev_global_regs->dctl;
26215 + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
26216 + (unsigned long)addr, DWC_READ_REG32(addr));
26217 + addr = &core_if->dev_if->dev_global_regs->dsts;
26218 + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
26219 + (unsigned long)addr, DWC_READ_REG32(addr));
26220 + addr = &core_if->dev_if->dev_global_regs->diepmsk;
26221 + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26222 + DWC_READ_REG32(addr));
26223 + addr = &core_if->dev_if->dev_global_regs->doepmsk;
26224 + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26225 + DWC_READ_REG32(addr));
26226 + addr = &core_if->dev_if->dev_global_regs->daint;
26227 + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26228 + DWC_READ_REG32(addr));
26229 + addr = &core_if->dev_if->dev_global_regs->daintmsk;
26230 + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26231 + DWC_READ_REG32(addr));
26232 + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
26233 + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26234 + DWC_READ_REG32(addr));
26235 + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
26236 + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
26237 + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
26238 + (unsigned long)addr, DWC_READ_REG32(addr));
26239 + }
26240 +
26241 + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
26242 + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
26243 + DWC_READ_REG32(addr));
26244 +
26245 + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
26246 + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
26247 + (unsigned long)addr, DWC_READ_REG32(addr));
26248 +
26249 + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
26250 + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
26251 + (unsigned long)addr, DWC_READ_REG32(addr));
26252 +
26253 + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
26254 + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
26255 + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
26256 + (unsigned long)addr, DWC_READ_REG32(addr));
26257 + }
26258 +
26259 + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
26260 + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26261 + DWC_READ_REG32(addr));
26262 +
26263 + if (core_if->hwcfg2.b.multi_proc_int) {
26264 +
26265 + addr = &core_if->dev_if->dev_global_regs->deachint;
26266 + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
26267 + (unsigned long)addr, DWC_READ_REG32(addr));
26268 + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
26269 + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
26270 + (unsigned long)addr, DWC_READ_REG32(addr));
26271 +
26272 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
26273 + addr =
26274 + &core_if->dev_if->
26275 + dev_global_regs->diepeachintmsk[i];
26276 + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
26277 + i, (unsigned long)addr,
26278 + DWC_READ_REG32(addr));
26279 + }
26280 +
26281 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
26282 + addr =
26283 + &core_if->dev_if->
26284 + dev_global_regs->doepeachintmsk[i];
26285 + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
26286 + i, (unsigned long)addr,
26287 + DWC_READ_REG32(addr));
26288 + }
26289 + }
26290 +
26291 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
26292 + DWC_PRINTF("Device IN EP %d Registers\n", i);
26293 + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
26294 + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
26295 + (unsigned long)addr, DWC_READ_REG32(addr));
26296 + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
26297 + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
26298 + (unsigned long)addr, DWC_READ_REG32(addr));
26299 + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
26300 + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
26301 + (unsigned long)addr, DWC_READ_REG32(addr));
26302 + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
26303 + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
26304 + (unsigned long)addr, DWC_READ_REG32(addr));
26305 + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
26306 + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
26307 + (unsigned long)addr, DWC_READ_REG32(addr));
26308 + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
26309 + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
26310 + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
26311 + }
26312 +
26313 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
26314 + DWC_PRINTF("Device OUT EP %d Registers\n", i);
26315 + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
26316 + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
26317 + (unsigned long)addr, DWC_READ_REG32(addr));
26318 + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
26319 + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
26320 + (unsigned long)addr, DWC_READ_REG32(addr));
26321 + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
26322 + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
26323 + (unsigned long)addr, DWC_READ_REG32(addr));
26324 + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
26325 + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
26326 + (unsigned long)addr, DWC_READ_REG32(addr));
26327 + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
26328 + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
26329 + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
26330 + (unsigned long)addr, DWC_READ_REG32(addr));
26331 + }
26332 +
26333 + }
26334 +}
26335 +
26336 +/**
26337 + * This functions reads the SPRAM and prints its content
26338 + *
26339 + * @param core_if Programming view of DWC_otg controller.
26340 + */
26341 +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
26342 +{
26343 + volatile uint8_t *addr, *start_addr, *end_addr;
26344 +
26345 + DWC_PRINTF("SPRAM Data:\n");
26346 + start_addr = (void *)core_if->core_global_regs;
26347 + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
26348 + start_addr += 0x00028000;
26349 + end_addr = (void *)core_if->core_global_regs;
26350 + end_addr += 0x000280e0;
26351 +
26352 + for (addr = start_addr; addr < end_addr; addr += 16) {
26353 + DWC_PRINTF
26354 + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
26355 + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
26356 + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
26357 + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
26358 + );
26359 + }
26360 +
26361 + return;
26362 +}
26363 +
26364 +/**
26365 + * This function reads the host registers and prints them
26366 + *
26367 + * @param core_if Programming view of DWC_otg controller.
26368 + */
26369 +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
26370 +{
26371 + int i;
26372 + volatile uint32_t *addr;
26373 +
26374 + DWC_PRINTF("Host Global Registers\n");
26375 + addr = &core_if->host_if->host_global_regs->hcfg;
26376 + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
26377 + (unsigned long)addr, DWC_READ_REG32(addr));
26378 + addr = &core_if->host_if->host_global_regs->hfir;
26379 + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
26380 + (unsigned long)addr, DWC_READ_REG32(addr));
26381 + addr = &core_if->host_if->host_global_regs->hfnum;
26382 + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
26383 + DWC_READ_REG32(addr));
26384 + addr = &core_if->host_if->host_global_regs->hptxsts;
26385 + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26386 + DWC_READ_REG32(addr));
26387 + addr = &core_if->host_if->host_global_regs->haint;
26388 + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26389 + DWC_READ_REG32(addr));
26390 + addr = &core_if->host_if->host_global_regs->haintmsk;
26391 + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26392 + DWC_READ_REG32(addr));
26393 + if (core_if->dma_desc_enable) {
26394 + addr = &core_if->host_if->host_global_regs->hflbaddr;
26395 + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
26396 + (unsigned long)addr, DWC_READ_REG32(addr));
26397 + }
26398 +
26399 + addr = core_if->host_if->hprt0;
26400 + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26401 + DWC_READ_REG32(addr));
26402 +
26403 + for (i = 0; i < core_if->core_params->host_channels; i++) {
26404 + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
26405 + addr = &core_if->host_if->hc_regs[i]->hcchar;
26406 + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
26407 + (unsigned long)addr, DWC_READ_REG32(addr));
26408 + addr = &core_if->host_if->hc_regs[i]->hcsplt;
26409 + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
26410 + (unsigned long)addr, DWC_READ_REG32(addr));
26411 + addr = &core_if->host_if->hc_regs[i]->hcint;
26412 + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
26413 + (unsigned long)addr, DWC_READ_REG32(addr));
26414 + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
26415 + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
26416 + (unsigned long)addr, DWC_READ_REG32(addr));
26417 + addr = &core_if->host_if->hc_regs[i]->hctsiz;
26418 + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
26419 + (unsigned long)addr, DWC_READ_REG32(addr));
26420 + addr = &core_if->host_if->hc_regs[i]->hcdma;
26421 + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
26422 + (unsigned long)addr, DWC_READ_REG32(addr));
26423 + if (core_if->dma_desc_enable) {
26424 + addr = &core_if->host_if->hc_regs[i]->hcdmab;
26425 + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
26426 + (unsigned long)addr, DWC_READ_REG32(addr));
26427 + }
26428 +
26429 + }
26430 + return;
26431 +}
26432 +
26433 +/**
26434 + * This function reads the core global registers and prints them
26435 + *
26436 + * @param core_if Programming view of DWC_otg controller.
26437 + */
26438 +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
26439 +{
26440 + int i, ep_num;
26441 + volatile uint32_t *addr;
26442 + char *txfsiz;
26443 +
26444 + DWC_PRINTF("Core Global Registers\n");
26445 + addr = &core_if->core_global_regs->gotgctl;
26446 + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26447 + DWC_READ_REG32(addr));
26448 + addr = &core_if->core_global_regs->gotgint;
26449 + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26450 + DWC_READ_REG32(addr));
26451 + addr = &core_if->core_global_regs->gahbcfg;
26452 + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26453 + DWC_READ_REG32(addr));
26454 + addr = &core_if->core_global_regs->gusbcfg;
26455 + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26456 + DWC_READ_REG32(addr));
26457 + addr = &core_if->core_global_regs->grstctl;
26458 + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26459 + DWC_READ_REG32(addr));
26460 + addr = &core_if->core_global_regs->gintsts;
26461 + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26462 + DWC_READ_REG32(addr));
26463 + addr = &core_if->core_global_regs->gintmsk;
26464 + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26465 + DWC_READ_REG32(addr));
26466 + addr = &core_if->core_global_regs->grxstsr;
26467 + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
26468 + DWC_READ_REG32(addr));
26469 + addr = &core_if->core_global_regs->grxfsiz;
26470 + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26471 + DWC_READ_REG32(addr));
26472 + addr = &core_if->core_global_regs->gnptxfsiz;
26473 + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26474 + DWC_READ_REG32(addr));
26475 + addr = &core_if->core_global_regs->gnptxsts;
26476 + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26477 + DWC_READ_REG32(addr));
26478 + addr = &core_if->core_global_regs->gi2cctl;
26479 + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26480 + DWC_READ_REG32(addr));
26481 + addr = &core_if->core_global_regs->gpvndctl;
26482 + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26483 + DWC_READ_REG32(addr));
26484 + addr = &core_if->core_global_regs->ggpio;
26485 + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
26486 + DWC_READ_REG32(addr));
26487 + addr = &core_if->core_global_regs->guid;
26488 + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
26489 + (unsigned long)addr, DWC_READ_REG32(addr));
26490 + addr = &core_if->core_global_regs->gsnpsid;
26491 + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
26492 + DWC_READ_REG32(addr));
26493 + addr = &core_if->core_global_regs->ghwcfg1;
26494 + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26495 + DWC_READ_REG32(addr));
26496 + addr = &core_if->core_global_regs->ghwcfg2;
26497 + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26498 + DWC_READ_REG32(addr));
26499 + addr = &core_if->core_global_regs->ghwcfg3;
26500 + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26501 + DWC_READ_REG32(addr));
26502 + addr = &core_if->core_global_regs->ghwcfg4;
26503 + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26504 + DWC_READ_REG32(addr));
26505 + addr = &core_if->core_global_regs->glpmcfg;
26506 + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26507 + DWC_READ_REG32(addr));
26508 + addr = &core_if->core_global_regs->gpwrdn;
26509 + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
26510 + DWC_READ_REG32(addr));
26511 + addr = &core_if->core_global_regs->gdfifocfg;
26512 + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26513 + DWC_READ_REG32(addr));
26514 + addr = &core_if->core_global_regs->adpctl;
26515 + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26516 + dwc_otg_adp_read_reg(core_if));
26517 + addr = &core_if->core_global_regs->hptxfsiz;
26518 + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26519 + DWC_READ_REG32(addr));
26520 +
26521 + if (core_if->en_multiple_tx_fifo == 0) {
26522 + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
26523 + txfsiz = "DPTXFSIZ";
26524 + } else {
26525 + ep_num = core_if->hwcfg4.b.num_in_eps;
26526 + txfsiz = "DIENPTXF";
26527 + }
26528 + for (i = 0; i < ep_num; i++) {
26529 + addr = &core_if->core_global_regs->dtxfsiz[i];
26530 + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
26531 + (unsigned long)addr, DWC_READ_REG32(addr));
26532 + }
26533 + addr = core_if->pcgcctl;
26534 + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26535 + DWC_READ_REG32(addr));
26536 +}
26537 +
26538 +/**
26539 + * Flush a Tx FIFO.
26540 + *
26541 + * @param core_if Programming view of DWC_otg controller.
26542 + * @param num Tx FIFO to flush.
26543 + */
26544 +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
26545 +{
26546 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26547 + volatile grstctl_t greset = {.d32 = 0 };
26548 + int count = 0;
26549 +
26550 + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
26551 +
26552 + greset.b.txfflsh = 1;
26553 + greset.b.txfnum = num;
26554 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26555 +
26556 + do {
26557 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26558 + if (++count > 10000) {
26559 + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
26560 + __func__, greset.d32,
26561 + DWC_READ_REG32(&global_regs->gnptxsts));
26562 + break;
26563 + }
26564 + dwc_udelay(1);
26565 + } while (greset.b.txfflsh == 1);
26566 +
26567 + /* Wait for 3 PHY Clocks */
26568 + dwc_udelay(1);
26569 +}
26570 +
26571 +/**
26572 + * Flush Rx FIFO.
26573 + *
26574 + * @param core_if Programming view of DWC_otg controller.
26575 + */
26576 +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
26577 +{
26578 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26579 + volatile grstctl_t greset = {.d32 = 0 };
26580 + int count = 0;
26581 +
26582 + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
26583 + /*
26584 + *
26585 + */
26586 + greset.b.rxfflsh = 1;
26587 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26588 +
26589 + do {
26590 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26591 + if (++count > 10000) {
26592 + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
26593 + greset.d32);
26594 + break;
26595 + }
26596 + dwc_udelay(1);
26597 + } while (greset.b.rxfflsh == 1);
26598 +
26599 + /* Wait for 3 PHY Clocks */
26600 + dwc_udelay(1);
26601 +}
26602 +
26603 +/**
26604 + * Do core a soft reset of the core. Be careful with this because it
26605 + * resets all the internal state machines of the core.
26606 + */
26607 +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
26608 +{
26609 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26610 + volatile grstctl_t greset = {.d32 = 0 };
26611 + int count = 0;
26612 +
26613 + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
26614 + /* Wait for AHB master IDLE state. */
26615 + do {
26616 + dwc_udelay(10);
26617 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26618 + if (++count > 100000) {
26619 + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
26620 + greset.d32);
26621 + return;
26622 + }
26623 + }
26624 + while (greset.b.ahbidle == 0);
26625 +
26626 + /* Core Soft Reset */
26627 + count = 0;
26628 + greset.b.csftrst = 1;
26629 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26630 + do {
26631 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26632 + if (++count > 10000) {
26633 + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
26634 + __func__, greset.d32);
26635 + break;
26636 + }
26637 + dwc_udelay(1);
26638 + }
26639 + while (greset.b.csftrst == 1);
26640 +
26641 + /* Wait for 3 PHY Clocks */
26642 + dwc_mdelay(100);
26643 +}
26644 +
26645 +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
26646 +{
26647 + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
26648 +}
26649 +
26650 +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
26651 +{
26652 + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
26653 +}
26654 +
26655 +/**
26656 + * Register HCD callbacks. The callbacks are used to start and stop
26657 + * the HCD for interrupt processing.
26658 + *
26659 + * @param core_if Programming view of DWC_otg controller.
26660 + * @param cb the HCD callback structure.
26661 + * @param p pointer to be passed to callback function (usb_hcd*).
26662 + */
26663 +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
26664 + dwc_otg_cil_callbacks_t * cb, void *p)
26665 +{
26666 + core_if->hcd_cb = cb;
26667 + cb->p = p;
26668 +}
26669 +
26670 +/**
26671 + * Register PCD callbacks. The callbacks are used to start and stop
26672 + * the PCD for interrupt processing.
26673 + *
26674 + * @param core_if Programming view of DWC_otg controller.
26675 + * @param cb the PCD callback structure.
26676 + * @param p pointer to be passed to callback function (pcd*).
26677 + */
26678 +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
26679 + dwc_otg_cil_callbacks_t * cb, void *p)
26680 +{
26681 + core_if->pcd_cb = cb;
26682 + cb->p = p;
26683 +}
26684 +
26685 +#ifdef DWC_EN_ISOC
26686 +
26687 +/**
26688 + * This function writes isoc data per 1 (micro)frame into tx fifo
26689 + *
26690 + * @param core_if Programming view of DWC_otg controller.
26691 + * @param ep The EP to start the transfer on.
26692 + *
26693 + */
26694 +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26695 +{
26696 + dwc_otg_dev_in_ep_regs_t *ep_regs;
26697 + dtxfsts_data_t txstatus = {.d32 = 0 };
26698 + uint32_t len = 0;
26699 + uint32_t dwords;
26700 +
26701 + ep->xfer_len = ep->data_per_frame;
26702 + ep->xfer_count = 0;
26703 +
26704 + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
26705 +
26706 + len = ep->xfer_len - ep->xfer_count;
26707 +
26708 + if (len > ep->maxpacket) {
26709 + len = ep->maxpacket;
26710 + }
26711 +
26712 + dwords = (len + 3) / 4;
26713 +
26714 + /* While there is space in the queue and space in the FIFO and
26715 + * More data to tranfer, Write packets to the Tx FIFO */
26716 + txstatus.d32 =
26717 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
26718 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
26719 +
26720 + while (txstatus.b.txfspcavail > dwords &&
26721 + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
26722 + /* Write the FIFO */
26723 + dwc_otg_ep_write_packet(core_if, ep, 0);
26724 +
26725 + len = ep->xfer_len - ep->xfer_count;
26726 + if (len > ep->maxpacket) {
26727 + len = ep->maxpacket;
26728 + }
26729 +
26730 + dwords = (len + 3) / 4;
26731 + txstatus.d32 =
26732 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
26733 + dtxfsts);
26734 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
26735 + txstatus.d32);
26736 + }
26737 +}
26738 +
26739 +/**
26740 + * This function initializes a descriptor chain for Isochronous transfer
26741 + *
26742 + * @param core_if Programming view of DWC_otg controller.
26743 + * @param ep The EP to start the transfer on.
26744 + *
26745 + */
26746 +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
26747 + dwc_ep_t * ep)
26748 +{
26749 + deptsiz_data_t deptsiz = {.d32 = 0 };
26750 + depctl_data_t depctl = {.d32 = 0 };
26751 + dsts_data_t dsts = {.d32 = 0 };
26752 + volatile uint32_t *addr;
26753 +
26754 + if (ep->is_in) {
26755 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
26756 + } else {
26757 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
26758 + }
26759 +
26760 + ep->xfer_len = ep->data_per_frame;
26761 + ep->xfer_count = 0;
26762 + ep->xfer_buff = ep->cur_pkt_addr;
26763 + ep->dma_addr = ep->cur_pkt_dma_addr;
26764 +
26765 + if (ep->is_in) {
26766 + /* Program the transfer size and packet count
26767 + * as follows: xfersize = N * maxpacket +
26768 + * short_packet pktcnt = N + (short_packet
26769 + * exist ? 1 : 0)
26770 + */
26771 + deptsiz.b.xfersize = ep->xfer_len;
26772 + deptsiz.b.pktcnt =
26773 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
26774 + deptsiz.b.mc = deptsiz.b.pktcnt;
26775 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
26776 + deptsiz.d32);
26777 +
26778 + /* Write the DMA register */
26779 + if (core_if->dma_enable) {
26780 + DWC_WRITE_REG32(&
26781 + (core_if->dev_if->in_ep_regs[ep->num]->
26782 + diepdma), (uint32_t) ep->dma_addr);
26783 + }
26784 + } else {
26785 + deptsiz.b.pktcnt =
26786 + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
26787 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
26788 +
26789 + DWC_WRITE_REG32(&core_if->dev_if->
26790 + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
26791 +
26792 + if (core_if->dma_enable) {
26793 + DWC_WRITE_REG32(&
26794 + (core_if->dev_if->
26795 + out_ep_regs[ep->num]->doepdma),
26796 + (uint32_t) ep->dma_addr);
26797 + }
26798 + }
26799 +
26800 + /** Enable endpoint, clear nak */
26801 +
26802 + depctl.d32 = 0;
26803 + if (ep->bInterval == 1) {
26804 + dsts.d32 =
26805 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
26806 + ep->next_frame = dsts.b.soffn + ep->bInterval;
26807 +
26808 + if (ep->next_frame & 0x1) {
26809 + depctl.b.setd1pid = 1;
26810 + } else {
26811 + depctl.b.setd0pid = 1;
26812 + }
26813 + } else {
26814 + ep->next_frame += ep->bInterval;
26815 +
26816 + if (ep->next_frame & 0x1) {
26817 + depctl.b.setd1pid = 1;
26818 + } else {
26819 + depctl.b.setd0pid = 1;
26820 + }
26821 + }
26822 + depctl.b.epena = 1;
26823 + depctl.b.cnak = 1;
26824 +
26825 + DWC_MODIFY_REG32(addr, 0, depctl.d32);
26826 + depctl.d32 = DWC_READ_REG32(addr);
26827 +
26828 + if (ep->is_in && core_if->dma_enable == 0) {
26829 + write_isoc_frame_data(core_if, ep);
26830 + }
26831 +
26832 +}
26833 +#endif /* DWC_EN_ISOC */
26834 +
26835 +static void dwc_otg_set_uninitialized(int32_t * p, int size)
26836 +{
26837 + int i;
26838 + for (i = 0; i < size; i++) {
26839 + p[i] = -1;
26840 + }
26841 +}
26842 +
26843 +static int dwc_otg_param_initialized(int32_t val)
26844 +{
26845 + return val != -1;
26846 +}
26847 +
26848 +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
26849 +{
26850 + int i;
26851 + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
26852 + if (!core_if->core_params) {
26853 + return -DWC_E_NO_MEMORY;
26854 + }
26855 + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
26856 + sizeof(*core_if->core_params) /
26857 + sizeof(int32_t));
26858 + DWC_PRINTF("Setting default values for core params\n");
26859 + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
26860 + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
26861 + dwc_otg_set_param_dma_desc_enable(core_if,
26862 + dwc_param_dma_desc_enable_default);
26863 + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
26864 + dwc_otg_set_param_dma_burst_size(core_if,
26865 + dwc_param_dma_burst_size_default);
26866 + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
26867 + dwc_param_host_support_fs_ls_low_power_default);
26868 + dwc_otg_set_param_enable_dynamic_fifo(core_if,
26869 + dwc_param_enable_dynamic_fifo_default);
26870 + dwc_otg_set_param_data_fifo_size(core_if,
26871 + dwc_param_data_fifo_size_default);
26872 + dwc_otg_set_param_dev_rx_fifo_size(core_if,
26873 + dwc_param_dev_rx_fifo_size_default);
26874 + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
26875 + dwc_param_dev_nperio_tx_fifo_size_default);
26876 + dwc_otg_set_param_host_rx_fifo_size(core_if,
26877 + dwc_param_host_rx_fifo_size_default);
26878 + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
26879 + dwc_param_host_nperio_tx_fifo_size_default);
26880 + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
26881 + dwc_param_host_perio_tx_fifo_size_default);
26882 + dwc_otg_set_param_max_transfer_size(core_if,
26883 + dwc_param_max_transfer_size_default);
26884 + dwc_otg_set_param_max_packet_count(core_if,
26885 + dwc_param_max_packet_count_default);
26886 + dwc_otg_set_param_host_channels(core_if,
26887 + dwc_param_host_channels_default);
26888 + dwc_otg_set_param_dev_endpoints(core_if,
26889 + dwc_param_dev_endpoints_default);
26890 + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
26891 + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
26892 + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
26893 + dwc_param_host_ls_low_power_phy_clk_default);
26894 + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
26895 + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
26896 + dwc_param_phy_ulpi_ext_vbus_default);
26897 + dwc_otg_set_param_phy_utmi_width(core_if,
26898 + dwc_param_phy_utmi_width_default);
26899 + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
26900 + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
26901 + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
26902 + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
26903 + dwc_param_en_multiple_tx_fifo_default);
26904 + for (i = 0; i < 15; i++) {
26905 + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
26906 + dwc_param_dev_perio_tx_fifo_size_default,
26907 + i);
26908 + }
26909 +
26910 + for (i = 0; i < 15; i++) {
26911 + dwc_otg_set_param_dev_tx_fifo_size(core_if,
26912 + dwc_param_dev_tx_fifo_size_default,
26913 + i);
26914 + }
26915 + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
26916 + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
26917 + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
26918 + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
26919 + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
26920 + dwc_otg_set_param_tx_thr_length(core_if,
26921 + dwc_param_tx_thr_length_default);
26922 + dwc_otg_set_param_rx_thr_length(core_if,
26923 + dwc_param_rx_thr_length_default);
26924 + dwc_otg_set_param_ahb_thr_ratio(core_if,
26925 + dwc_param_ahb_thr_ratio_default);
26926 + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
26927 + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
26928 + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
26929 + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
26930 + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
26931 + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
26932 + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
26933 + DWC_PRINTF("Finished setting default values for core params\n");
26934 +
26935 + return 0;
26936 +}
26937 +
26938 +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
26939 +{
26940 + return core_if->dma_enable;
26941 +}
26942 +
26943 +/* Checks if the parameter is outside of its valid range of values */
26944 +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
26945 + (((_param_) < (_low_)) || \
26946 + ((_param_) > (_high_)))
26947 +
26948 +/* Parameter access functions */
26949 +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
26950 +{
26951 + int valid;
26952 + int retval = 0;
26953 + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
26954 + DWC_WARN("Wrong value for otg_cap parameter\n");
26955 + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
26956 + retval = -DWC_E_INVALID;
26957 + goto out;
26958 + }
26959 +
26960 + valid = 1;
26961 + switch (val) {
26962 + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
26963 + if (core_if->hwcfg2.b.op_mode !=
26964 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
26965 + valid = 0;
26966 + break;
26967 + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
26968 + if ((core_if->hwcfg2.b.op_mode !=
26969 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
26970 + && (core_if->hwcfg2.b.op_mode !=
26971 + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
26972 + && (core_if->hwcfg2.b.op_mode !=
26973 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
26974 + && (core_if->hwcfg2.b.op_mode !=
26975 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
26976 + valid = 0;
26977 + }
26978 + break;
26979 + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
26980 + /* always valid */
26981 + break;
26982 + }
26983 + if (!valid) {
26984 + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
26985 + DWC_ERROR
26986 + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
26987 + val);
26988 + }
26989 + val =
26990 + (((core_if->hwcfg2.b.op_mode ==
26991 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
26992 + || (core_if->hwcfg2.b.op_mode ==
26993 + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
26994 + || (core_if->hwcfg2.b.op_mode ==
26995 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
26996 + || (core_if->hwcfg2.b.op_mode ==
26997 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
26998 + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
26999 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
27000 + retval = -DWC_E_INVALID;
27001 + }
27002 +
27003 + core_if->core_params->otg_cap = val;
27004 +out:
27005 + return retval;
27006 +}
27007 +
27008 +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
27009 +{
27010 + return core_if->core_params->otg_cap;
27011 +}
27012 +
27013 +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
27014 +{
27015 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27016 + DWC_WARN("Wrong value for opt parameter\n");
27017 + return -DWC_E_INVALID;
27018 + }
27019 + core_if->core_params->opt = val;
27020 + return 0;
27021 +}
27022 +
27023 +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
27024 +{
27025 + return core_if->core_params->opt;
27026 +}
27027 +
27028 +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
27029 +{
27030 + int retval = 0;
27031 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27032 + DWC_WARN("Wrong value for dma enable\n");
27033 + return -DWC_E_INVALID;
27034 + }
27035 +
27036 + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
27037 + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
27038 + DWC_ERROR
27039 + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
27040 + val);
27041 + }
27042 + val = 0;
27043 + retval = -DWC_E_INVALID;
27044 + }
27045 +
27046 + core_if->core_params->dma_enable = val;
27047 + if (val == 0) {
27048 + dwc_otg_set_param_dma_desc_enable(core_if, 0);
27049 + }
27050 + return retval;
27051 +}
27052 +
27053 +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
27054 +{
27055 + return core_if->core_params->dma_enable;
27056 +}
27057 +
27058 +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
27059 +{
27060 + int retval = 0;
27061 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27062 + DWC_WARN("Wrong value for dma_enable\n");
27063 + DWC_WARN("dma_desc_enable must be 0 or 1\n");
27064 + return -DWC_E_INVALID;
27065 + }
27066 +
27067 + if ((val == 1)
27068 + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
27069 + || (core_if->hwcfg4.b.desc_dma == 0))) {
27070 + if (dwc_otg_param_initialized
27071 + (core_if->core_params->dma_desc_enable)) {
27072 + DWC_ERROR
27073 + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
27074 + val);
27075 + }
27076 + val = 0;
27077 + retval = -DWC_E_INVALID;
27078 + }
27079 + core_if->core_params->dma_desc_enable = val;
27080 + return retval;
27081 +}
27082 +
27083 +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
27084 +{
27085 + return core_if->core_params->dma_desc_enable;
27086 +}
27087 +
27088 +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
27089 + int32_t val)
27090 +{
27091 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27092 + DWC_WARN("Wrong value for host_support_fs_low_power\n");
27093 + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
27094 + return -DWC_E_INVALID;
27095 + }
27096 + core_if->core_params->host_support_fs_ls_low_power = val;
27097 + return 0;
27098 +}
27099 +
27100 +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
27101 + core_if)
27102 +{
27103 + return core_if->core_params->host_support_fs_ls_low_power;
27104 +}
27105 +
27106 +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
27107 + int32_t val)
27108 +{
27109 + int retval = 0;
27110 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27111 + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
27112 + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
27113 + return -DWC_E_INVALID;
27114 + }
27115 +
27116 + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
27117 + if (dwc_otg_param_initialized
27118 + (core_if->core_params->enable_dynamic_fifo)) {
27119 + DWC_ERROR
27120 + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
27121 + val);
27122 + }
27123 + val = 0;
27124 + retval = -DWC_E_INVALID;
27125 + }
27126 + core_if->core_params->enable_dynamic_fifo = val;
27127 + return retval;
27128 +}
27129 +
27130 +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
27131 +{
27132 + return core_if->core_params->enable_dynamic_fifo;
27133 +}
27134 +
27135 +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
27136 +{
27137 + int retval = 0;
27138 + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
27139 + DWC_WARN("Wrong value for data_fifo_size\n");
27140 + DWC_WARN("data_fifo_size must be 32-32768\n");
27141 + return -DWC_E_INVALID;
27142 + }
27143 +
27144 + if (val > core_if->hwcfg3.b.dfifo_depth) {
27145 + if (dwc_otg_param_initialized
27146 + (core_if->core_params->data_fifo_size)) {
27147 + DWC_ERROR
27148 + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
27149 + val);
27150 + }
27151 + val = core_if->hwcfg3.b.dfifo_depth;
27152 + retval = -DWC_E_INVALID;
27153 + }
27154 +
27155 + core_if->core_params->data_fifo_size = val;
27156 + return retval;
27157 +}
27158 +
27159 +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
27160 +{
27161 + return core_if->core_params->data_fifo_size;
27162 +}
27163 +
27164 +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
27165 +{
27166 + int retval = 0;
27167 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27168 + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
27169 + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
27170 + return -DWC_E_INVALID;
27171 + }
27172 +
27173 + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
27174 + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
27175 + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
27176 + }
27177 + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
27178 + retval = -DWC_E_INVALID;
27179 + }
27180 +
27181 + core_if->core_params->dev_rx_fifo_size = val;
27182 + return retval;
27183 +}
27184 +
27185 +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
27186 +{
27187 + return core_if->core_params->dev_rx_fifo_size;
27188 +}
27189 +
27190 +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27191 + int32_t val)
27192 +{
27193 + int retval = 0;
27194 +
27195 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27196 + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
27197 + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
27198 + return -DWC_E_INVALID;
27199 + }
27200 +
27201 + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
27202 + if (dwc_otg_param_initialized
27203 + (core_if->core_params->dev_nperio_tx_fifo_size)) {
27204 + DWC_ERROR
27205 + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
27206 + val);
27207 + }
27208 + val =
27209 + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
27210 + 16);
27211 + retval = -DWC_E_INVALID;
27212 + }
27213 +
27214 + core_if->core_params->dev_nperio_tx_fifo_size = val;
27215 + return retval;
27216 +}
27217 +
27218 +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27219 +{
27220 + return core_if->core_params->dev_nperio_tx_fifo_size;
27221 +}
27222 +
27223 +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
27224 + int32_t val)
27225 +{
27226 + int retval = 0;
27227 +
27228 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27229 + DWC_WARN("Wrong value for host_rx_fifo_size\n");
27230 + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
27231 + return -DWC_E_INVALID;
27232 + }
27233 +
27234 + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
27235 + if (dwc_otg_param_initialized
27236 + (core_if->core_params->host_rx_fifo_size)) {
27237 + DWC_ERROR
27238 + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
27239 + val);
27240 + }
27241 + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
27242 + retval = -DWC_E_INVALID;
27243 + }
27244 +
27245 + core_if->core_params->host_rx_fifo_size = val;
27246 + return retval;
27247 +
27248 +}
27249 +
27250 +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
27251 +{
27252 + return core_if->core_params->host_rx_fifo_size;
27253 +}
27254 +
27255 +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27256 + int32_t val)
27257 +{
27258 + int retval = 0;
27259 +
27260 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27261 + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
27262 + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
27263 + return -DWC_E_INVALID;
27264 + }
27265 +
27266 + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
27267 + if (dwc_otg_param_initialized
27268 + (core_if->core_params->host_nperio_tx_fifo_size)) {
27269 + DWC_ERROR
27270 + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
27271 + val);
27272 + }
27273 + val =
27274 + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
27275 + 16);
27276 + retval = -DWC_E_INVALID;
27277 + }
27278 +
27279 + core_if->core_params->host_nperio_tx_fifo_size = val;
27280 + return retval;
27281 +}
27282 +
27283 +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27284 +{
27285 + return core_if->core_params->host_nperio_tx_fifo_size;
27286 +}
27287 +
27288 +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27289 + int32_t val)
27290 +{
27291 + int retval = 0;
27292 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27293 + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
27294 + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
27295 + return -DWC_E_INVALID;
27296 + }
27297 +
27298 + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
27299 + if (dwc_otg_param_initialized
27300 + (core_if->core_params->host_perio_tx_fifo_size)) {
27301 + DWC_ERROR
27302 + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
27303 + val);
27304 + }
27305 + val = (core_if->hptxfsiz.d32) >> 16;
27306 + retval = -DWC_E_INVALID;
27307 + }
27308 +
27309 + core_if->core_params->host_perio_tx_fifo_size = val;
27310 + return retval;
27311 +}
27312 +
27313 +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27314 +{
27315 + return core_if->core_params->host_perio_tx_fifo_size;
27316 +}
27317 +
27318 +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
27319 + int32_t val)
27320 +{
27321 + int retval = 0;
27322 +
27323 + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
27324 + DWC_WARN("Wrong value for max_transfer_size\n");
27325 + DWC_WARN("max_transfer_size must be 2047-524288\n");
27326 + return -DWC_E_INVALID;
27327 + }
27328 +
27329 + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
27330 + if (dwc_otg_param_initialized
27331 + (core_if->core_params->max_transfer_size)) {
27332 + DWC_ERROR
27333 + ("%d invalid for max_transfer_size. Check HW configuration.\n",
27334 + val);
27335 + }
27336 + val =
27337 + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
27338 + 1);
27339 + retval = -DWC_E_INVALID;
27340 + }
27341 +
27342 + core_if->core_params->max_transfer_size = val;
27343 + return retval;
27344 +}
27345 +
27346 +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
27347 +{
27348 + return core_if->core_params->max_transfer_size;
27349 +}
27350 +
27351 +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
27352 +{
27353 + int retval = 0;
27354 +
27355 + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
27356 + DWC_WARN("Wrong value for max_packet_count\n");
27357 + DWC_WARN("max_packet_count must be 15-511\n");
27358 + return -DWC_E_INVALID;
27359 + }
27360 +
27361 + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
27362 + if (dwc_otg_param_initialized
27363 + (core_if->core_params->max_packet_count)) {
27364 + DWC_ERROR
27365 + ("%d invalid for max_packet_count. Check HW configuration.\n",
27366 + val);
27367 + }
27368 + val =
27369 + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
27370 + retval = -DWC_E_INVALID;
27371 + }
27372 +
27373 + core_if->core_params->max_packet_count = val;
27374 + return retval;
27375 +}
27376 +
27377 +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
27378 +{
27379 + return core_if->core_params->max_packet_count;
27380 +}
27381 +
27382 +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
27383 +{
27384 + int retval = 0;
27385 +
27386 + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
27387 + DWC_WARN("Wrong value for host_channels\n");
27388 + DWC_WARN("host_channels must be 1-16\n");
27389 + return -DWC_E_INVALID;
27390 + }
27391 +
27392 + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
27393 + if (dwc_otg_param_initialized
27394 + (core_if->core_params->host_channels)) {
27395 + DWC_ERROR
27396 + ("%d invalid for host_channels. Check HW configurations.\n",
27397 + val);
27398 + }
27399 + val = (core_if->hwcfg2.b.num_host_chan + 1);
27400 + retval = -DWC_E_INVALID;
27401 + }
27402 +
27403 + core_if->core_params->host_channels = val;
27404 + return retval;
27405 +}
27406 +
27407 +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
27408 +{
27409 + return core_if->core_params->host_channels;
27410 +}
27411 +
27412 +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
27413 +{
27414 + int retval = 0;
27415 +
27416 + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
27417 + DWC_WARN("Wrong value for dev_endpoints\n");
27418 + DWC_WARN("dev_endpoints must be 1-15\n");
27419 + return -DWC_E_INVALID;
27420 + }
27421 +
27422 + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
27423 + if (dwc_otg_param_initialized
27424 + (core_if->core_params->dev_endpoints)) {
27425 + DWC_ERROR
27426 + ("%d invalid for dev_endpoints. Check HW configurations.\n",
27427 + val);
27428 + }
27429 + val = core_if->hwcfg2.b.num_dev_ep;
27430 + retval = -DWC_E_INVALID;
27431 + }
27432 +
27433 + core_if->core_params->dev_endpoints = val;
27434 + return retval;
27435 +}
27436 +
27437 +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
27438 +{
27439 + return core_if->core_params->dev_endpoints;
27440 +}
27441 +
27442 +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
27443 +{
27444 + int retval = 0;
27445 + int valid = 0;
27446 +
27447 + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
27448 + DWC_WARN("Wrong value for phy_type\n");
27449 + DWC_WARN("phy_type must be 0,1 or 2\n");
27450 + return -DWC_E_INVALID;
27451 + }
27452 +#ifndef NO_FS_PHY_HW_CHECKS
27453 + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
27454 + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
27455 + (core_if->hwcfg2.b.hs_phy_type == 3))) {
27456 + valid = 1;
27457 + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
27458 + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
27459 + (core_if->hwcfg2.b.hs_phy_type == 3))) {
27460 + valid = 1;
27461 + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
27462 + (core_if->hwcfg2.b.fs_phy_type == 1)) {
27463 + valid = 1;
27464 + }
27465 + if (!valid) {
27466 + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
27467 + DWC_ERROR
27468 + ("%d invalid for phy_type. Check HW configurations.\n",
27469 + val);
27470 + }
27471 + if (core_if->hwcfg2.b.hs_phy_type) {
27472 + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
27473 + (core_if->hwcfg2.b.hs_phy_type == 1)) {
27474 + val = DWC_PHY_TYPE_PARAM_UTMI;
27475 + } else {
27476 + val = DWC_PHY_TYPE_PARAM_ULPI;
27477 + }
27478 + }
27479 + retval = -DWC_E_INVALID;
27480 + }
27481 +#endif
27482 + core_if->core_params->phy_type = val;
27483 + return retval;
27484 +}
27485 +
27486 +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
27487 +{
27488 + return core_if->core_params->phy_type;
27489 +}
27490 +
27491 +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
27492 +{
27493 + int retval = 0;
27494 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27495 + DWC_WARN("Wrong value for speed parameter\n");
27496 + DWC_WARN("max_speed parameter must be 0 or 1\n");
27497 + return -DWC_E_INVALID;
27498 + }
27499 + if ((val == 0)
27500 + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
27501 + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
27502 + DWC_ERROR
27503 + ("%d invalid for speed paremter. Check HW configuration.\n",
27504 + val);
27505 + }
27506 + val =
27507 + (dwc_otg_get_param_phy_type(core_if) ==
27508 + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
27509 + retval = -DWC_E_INVALID;
27510 + }
27511 + core_if->core_params->speed = val;
27512 + return retval;
27513 +}
27514 +
27515 +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
27516 +{
27517 + return core_if->core_params->speed;
27518 +}
27519 +
27520 +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
27521 + int32_t val)
27522 +{
27523 + int retval = 0;
27524 +
27525 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27526 + DWC_WARN
27527 + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
27528 + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
27529 + return -DWC_E_INVALID;
27530 + }
27531 +
27532 + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
27533 + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
27534 + if (dwc_otg_param_initialized
27535 + (core_if->core_params->host_ls_low_power_phy_clk)) {
27536 + DWC_ERROR
27537 + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
27538 + val);
27539 + }
27540 + val =
27541 + (dwc_otg_get_param_phy_type(core_if) ==
27542 + DWC_PHY_TYPE_PARAM_FS) ?
27543 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
27544 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
27545 + retval = -DWC_E_INVALID;
27546 + }
27547 +
27548 + core_if->core_params->host_ls_low_power_phy_clk = val;
27549 + return retval;
27550 +}
27551 +
27552 +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
27553 +{
27554 + return core_if->core_params->host_ls_low_power_phy_clk;
27555 +}
27556 +
27557 +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
27558 +{
27559 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27560 + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
27561 + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
27562 + return -DWC_E_INVALID;
27563 + }
27564 +
27565 + core_if->core_params->phy_ulpi_ddr = val;
27566 + return 0;
27567 +}
27568 +
27569 +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
27570 +{
27571 + return core_if->core_params->phy_ulpi_ddr;
27572 +}
27573 +
27574 +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
27575 + int32_t val)
27576 +{
27577 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27578 + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
27579 + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
27580 + return -DWC_E_INVALID;
27581 + }
27582 +
27583 + core_if->core_params->phy_ulpi_ext_vbus = val;
27584 + return 0;
27585 +}
27586 +
27587 +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
27588 +{
27589 + return core_if->core_params->phy_ulpi_ext_vbus;
27590 +}
27591 +
27592 +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
27593 +{
27594 + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
27595 + DWC_WARN("Wrong valaue for phy_utmi_width\n");
27596 + DWC_WARN("phy_utmi_width must be 8 or 16\n");
27597 + return -DWC_E_INVALID;
27598 + }
27599 +
27600 + core_if->core_params->phy_utmi_width = val;
27601 + return 0;
27602 +}
27603 +
27604 +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
27605 +{
27606 + return core_if->core_params->phy_utmi_width;
27607 +}
27608 +
27609 +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
27610 +{
27611 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27612 + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
27613 + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
27614 + return -DWC_E_INVALID;
27615 + }
27616 +
27617 + core_if->core_params->ulpi_fs_ls = val;
27618 + return 0;
27619 +}
27620 +
27621 +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
27622 +{
27623 + return core_if->core_params->ulpi_fs_ls;
27624 +}
27625 +
27626 +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
27627 +{
27628 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27629 + DWC_WARN("Wrong valaue for ts_dline\n");
27630 + DWC_WARN("ts_dline must be 0 or 1\n");
27631 + return -DWC_E_INVALID;
27632 + }
27633 +
27634 + core_if->core_params->ts_dline = val;
27635 + return 0;
27636 +}
27637 +
27638 +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
27639 +{
27640 + return core_if->core_params->ts_dline;
27641 +}
27642 +
27643 +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
27644 +{
27645 + int retval = 0;
27646 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27647 + DWC_WARN("Wrong valaue for i2c_enable\n");
27648 + DWC_WARN("i2c_enable must be 0 or 1\n");
27649 + return -DWC_E_INVALID;
27650 + }
27651 +#ifndef NO_FS_PHY_HW_CHECK
27652 + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
27653 + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
27654 + DWC_ERROR
27655 + ("%d invalid for i2c_enable. Check HW configuration.\n",
27656 + val);
27657 + }
27658 + val = 0;
27659 + retval = -DWC_E_INVALID;
27660 + }
27661 +#endif
27662 +
27663 + core_if->core_params->i2c_enable = val;
27664 + return retval;
27665 +}
27666 +
27667 +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
27668 +{
27669 + return core_if->core_params->i2c_enable;
27670 +}
27671 +
27672 +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27673 + int32_t val, int fifo_num)
27674 +{
27675 + int retval = 0;
27676 +
27677 + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
27678 + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
27679 + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
27680 + return -DWC_E_INVALID;
27681 + }
27682 +
27683 + if (val >
27684 + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
27685 + if (dwc_otg_param_initialized
27686 + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
27687 + DWC_ERROR
27688 + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
27689 + val, fifo_num);
27690 + }
27691 + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
27692 + retval = -DWC_E_INVALID;
27693 + }
27694 +
27695 + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
27696 + return retval;
27697 +}
27698 +
27699 +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27700 + int fifo_num)
27701 +{
27702 + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
27703 +}
27704 +
27705 +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
27706 + int32_t val)
27707 +{
27708 + int retval = 0;
27709 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27710 + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
27711 + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
27712 + return -DWC_E_INVALID;
27713 + }
27714 +
27715 + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
27716 + if (dwc_otg_param_initialized
27717 + (core_if->core_params->en_multiple_tx_fifo)) {
27718 + DWC_ERROR
27719 + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
27720 + val);
27721 + }
27722 + val = 0;
27723 + retval = -DWC_E_INVALID;
27724 + }
27725 +
27726 + core_if->core_params->en_multiple_tx_fifo = val;
27727 + return retval;
27728 +}
27729 +
27730 +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
27731 +{
27732 + return core_if->core_params->en_multiple_tx_fifo;
27733 +}
27734 +
27735 +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
27736 + int fifo_num)
27737 +{
27738 + int retval = 0;
27739 +
27740 + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
27741 + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
27742 + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
27743 + return -DWC_E_INVALID;
27744 + }
27745 +
27746 + if (val >
27747 + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
27748 + if (dwc_otg_param_initialized
27749 + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
27750 + DWC_ERROR
27751 + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
27752 + val, fifo_num);
27753 + }
27754 + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
27755 + retval = -DWC_E_INVALID;
27756 + }
27757 +
27758 + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
27759 + return retval;
27760 +}
27761 +
27762 +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
27763 + int fifo_num)
27764 +{
27765 + return core_if->core_params->dev_tx_fifo_size[fifo_num];
27766 +}
27767 +
27768 +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
27769 +{
27770 + int retval = 0;
27771 +
27772 + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
27773 + DWC_WARN("Wrong value for thr_ctl\n");
27774 + DWC_WARN("thr_ctl must be 0-7\n");
27775 + return -DWC_E_INVALID;
27776 + }
27777 +
27778 + if ((val != 0) &&
27779 + (!dwc_otg_get_param_dma_enable(core_if) ||
27780 + !core_if->hwcfg4.b.ded_fifo_en)) {
27781 + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
27782 + DWC_ERROR
27783 + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
27784 + val);
27785 + }
27786 + val = 0;
27787 + retval = -DWC_E_INVALID;
27788 + }
27789 +
27790 + core_if->core_params->thr_ctl = val;
27791 + return retval;
27792 +}
27793 +
27794 +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
27795 +{
27796 + return core_if->core_params->thr_ctl;
27797 +}
27798 +
27799 +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
27800 +{
27801 + int retval = 0;
27802 +
27803 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27804 + DWC_WARN("Wrong value for lpm_enable\n");
27805 + DWC_WARN("lpm_enable must be 0 or 1\n");
27806 + return -DWC_E_INVALID;
27807 + }
27808 +
27809 + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
27810 + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
27811 + DWC_ERROR
27812 + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
27813 + val);
27814 + }
27815 + val = 0;
27816 + retval = -DWC_E_INVALID;
27817 + }
27818 +
27819 + core_if->core_params->lpm_enable = val;
27820 + return retval;
27821 +}
27822 +
27823 +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
27824 +{
27825 + return core_if->core_params->lpm_enable;
27826 +}
27827 +
27828 +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
27829 +{
27830 + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
27831 + DWC_WARN("Wrong valaue for tx_thr_length\n");
27832 + DWC_WARN("tx_thr_length must be 8 - 128\n");
27833 + return -DWC_E_INVALID;
27834 + }
27835 +
27836 + core_if->core_params->tx_thr_length = val;
27837 + return 0;
27838 +}
27839 +
27840 +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
27841 +{
27842 + return core_if->core_params->tx_thr_length;
27843 +}
27844 +
27845 +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
27846 +{
27847 + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
27848 + DWC_WARN("Wrong valaue for rx_thr_length\n");
27849 + DWC_WARN("rx_thr_length must be 8 - 128\n");
27850 + return -DWC_E_INVALID;
27851 + }
27852 +
27853 + core_if->core_params->rx_thr_length = val;
27854 + return 0;
27855 +}
27856 +
27857 +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
27858 +{
27859 + return core_if->core_params->rx_thr_length;
27860 +}
27861 +
27862 +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
27863 +{
27864 + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
27865 + DWC_OTG_PARAM_TEST(val, 4, 4) &&
27866 + DWC_OTG_PARAM_TEST(val, 8, 8) &&
27867 + DWC_OTG_PARAM_TEST(val, 16, 16) &&
27868 + DWC_OTG_PARAM_TEST(val, 32, 32) &&
27869 + DWC_OTG_PARAM_TEST(val, 64, 64) &&
27870 + DWC_OTG_PARAM_TEST(val, 128, 128) &&
27871 + DWC_OTG_PARAM_TEST(val, 256, 256)) {
27872 + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
27873 + return -DWC_E_INVALID;
27874 + }
27875 + core_if->core_params->dma_burst_size = val;
27876 + return 0;
27877 +}
27878 +
27879 +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
27880 +{
27881 + return core_if->core_params->dma_burst_size;
27882 +}
27883 +
27884 +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
27885 +{
27886 + int retval = 0;
27887 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27888 + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
27889 + return -DWC_E_INVALID;
27890 + }
27891 + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
27892 + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
27893 + DWC_ERROR
27894 + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
27895 + val);
27896 + }
27897 + retval = -DWC_E_INVALID;
27898 + val = 0;
27899 + }
27900 + core_if->core_params->pti_enable = val;
27901 + return retval;
27902 +}
27903 +
27904 +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
27905 +{
27906 + return core_if->core_params->pti_enable;
27907 +}
27908 +
27909 +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
27910 +{
27911 + int retval = 0;
27912 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27913 + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
27914 + return -DWC_E_INVALID;
27915 + }
27916 + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
27917 + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
27918 + DWC_ERROR
27919 + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
27920 + val);
27921 + }
27922 + retval = -DWC_E_INVALID;
27923 + val = 0;
27924 + }
27925 + core_if->core_params->mpi_enable = val;
27926 + return retval;
27927 +}
27928 +
27929 +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
27930 +{
27931 + return core_if->core_params->mpi_enable;
27932 +}
27933 +
27934 +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
27935 +{
27936 + int retval = 0;
27937 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27938 + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
27939 + return -DWC_E_INVALID;
27940 + }
27941 + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
27942 + if (dwc_otg_param_initialized
27943 + (core_if->core_params->adp_supp_enable)) {
27944 + DWC_ERROR
27945 + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
27946 + val);
27947 + }
27948 + retval = -DWC_E_INVALID;
27949 + val = 0;
27950 + }
27951 + core_if->core_params->adp_supp_enable = val;
27952 + /*Set OTG version 2.0 in case of enabling ADP*/
27953 + if (val)
27954 + dwc_otg_set_param_otg_ver(core_if, 1);
27955 +
27956 + return retval;
27957 +}
27958 +
27959 +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
27960 +{
27961 + return core_if->core_params->adp_supp_enable;
27962 +}
27963 +
27964 +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
27965 +{
27966 + int retval = 0;
27967 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27968 + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
27969 + DWC_WARN("ic_usb_cap must be 0 or 1\n");
27970 + return -DWC_E_INVALID;
27971 + }
27972 +
27973 + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
27974 + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
27975 + DWC_ERROR
27976 + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
27977 + val);
27978 + }
27979 + retval = -DWC_E_INVALID;
27980 + val = 0;
27981 + }
27982 + core_if->core_params->ic_usb_cap = val;
27983 + return retval;
27984 +}
27985 +
27986 +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
27987 +{
27988 + return core_if->core_params->ic_usb_cap;
27989 +}
27990 +
27991 +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
27992 +{
27993 + int retval = 0;
27994 + int valid = 1;
27995 +
27996 + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
27997 + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
27998 + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
27999 + return -DWC_E_INVALID;
28000 + }
28001 +
28002 + if (val
28003 + && (core_if->snpsid < OTG_CORE_REV_2_81a
28004 + || !dwc_otg_get_param_thr_ctl(core_if))) {
28005 + valid = 0;
28006 + } else if (val
28007 + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
28008 + 4)) {
28009 + valid = 0;
28010 + }
28011 + if (valid == 0) {
28012 + if (dwc_otg_param_initialized
28013 + (core_if->core_params->ahb_thr_ratio)) {
28014 + DWC_ERROR
28015 + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
28016 + val);
28017 + }
28018 + retval = -DWC_E_INVALID;
28019 + val = 0;
28020 + }
28021 +
28022 + core_if->core_params->ahb_thr_ratio = val;
28023 + return retval;
28024 +}
28025 +
28026 +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
28027 +{
28028 + return core_if->core_params->ahb_thr_ratio;
28029 +}
28030 +
28031 +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
28032 +{
28033 + int retval = 0;
28034 + int valid = 1;
28035 + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
28036 + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
28037 +
28038 + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
28039 + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
28040 + DWC_WARN("power_down must be 0 - 2\n");
28041 + return -DWC_E_INVALID;
28042 + }
28043 +
28044 + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
28045 + valid = 0;
28046 + }
28047 + if ((val == 3)
28048 + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
28049 + || (hwcfg4.b.xhiber == 0))) {
28050 + valid = 0;
28051 + }
28052 + if (valid == 0) {
28053 + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
28054 + DWC_ERROR
28055 + ("%d invalid for parameter power_down. Check HW configuration.\n",
28056 + val);
28057 + }
28058 + retval = -DWC_E_INVALID;
28059 + val = 0;
28060 + }
28061 + core_if->core_params->power_down = val;
28062 + return retval;
28063 +}
28064 +
28065 +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
28066 +{
28067 + return core_if->core_params->power_down;
28068 +}
28069 +
28070 +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
28071 +{
28072 + int retval = 0;
28073 + int valid = 1;
28074 +
28075 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28076 + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
28077 + DWC_WARN("reload_ctl must be 0 or 1\n");
28078 + return -DWC_E_INVALID;
28079 + }
28080 +
28081 + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
28082 + valid = 0;
28083 + }
28084 + if (valid == 0) {
28085 + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
28086 + DWC_ERROR("%d invalid for parameter reload_ctl."
28087 + "Check HW configuration.\n", val);
28088 + }
28089 + retval = -DWC_E_INVALID;
28090 + val = 0;
28091 + }
28092 + core_if->core_params->reload_ctl = val;
28093 + return retval;
28094 +}
28095 +
28096 +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
28097 +{
28098 + return core_if->core_params->reload_ctl;
28099 +}
28100 +
28101 +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
28102 +{
28103 + int retval = 0;
28104 + int valid = 1;
28105 +
28106 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28107 + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
28108 + DWC_WARN("dev_out_nak must be 0 or 1\n");
28109 + return -DWC_E_INVALID;
28110 + }
28111 +
28112 + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
28113 + !(core_if->core_params->dma_desc_enable))) {
28114 + valid = 0;
28115 + }
28116 + if (valid == 0) {
28117 + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
28118 + DWC_ERROR("%d invalid for parameter dev_out_nak."
28119 + "Check HW configuration.\n", val);
28120 + }
28121 + retval = -DWC_E_INVALID;
28122 + val = 0;
28123 + }
28124 + core_if->core_params->dev_out_nak = val;
28125 + return retval;
28126 +}
28127 +
28128 +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
28129 +{
28130 + return core_if->core_params->dev_out_nak;
28131 +}
28132 +
28133 +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
28134 +{
28135 + int retval = 0;
28136 + int valid = 1;
28137 +
28138 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28139 + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
28140 + DWC_WARN("cont_on_bna must be 0 or 1\n");
28141 + return -DWC_E_INVALID;
28142 + }
28143 +
28144 + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
28145 + !(core_if->core_params->dma_desc_enable))) {
28146 + valid = 0;
28147 + }
28148 + if (valid == 0) {
28149 + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
28150 + DWC_ERROR("%d invalid for parameter cont_on_bna."
28151 + "Check HW configuration.\n", val);
28152 + }
28153 + retval = -DWC_E_INVALID;
28154 + val = 0;
28155 + }
28156 + core_if->core_params->cont_on_bna = val;
28157 + return retval;
28158 +}
28159 +
28160 +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
28161 +{
28162 + return core_if->core_params->cont_on_bna;
28163 +}
28164 +
28165 +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
28166 +{
28167 + int retval = 0;
28168 + int valid = 1;
28169 +
28170 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28171 + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
28172 + DWC_WARN("ahb_single must be 0 or 1\n");
28173 + return -DWC_E_INVALID;
28174 + }
28175 +
28176 + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
28177 + valid = 0;
28178 + }
28179 + if (valid == 0) {
28180 + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
28181 + DWC_ERROR("%d invalid for parameter ahb_single."
28182 + "Check HW configuration.\n", val);
28183 + }
28184 + retval = -DWC_E_INVALID;
28185 + val = 0;
28186 + }
28187 + core_if->core_params->ahb_single = val;
28188 + return retval;
28189 +}
28190 +
28191 +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
28192 +{
28193 + return core_if->core_params->ahb_single;
28194 +}
28195 +
28196 +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
28197 +{
28198 + int retval = 0;
28199 +
28200 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28201 + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
28202 + DWC_WARN
28203 + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
28204 + return -DWC_E_INVALID;
28205 + }
28206 +
28207 + core_if->core_params->otg_ver = val;
28208 + return retval;
28209 +}
28210 +
28211 +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
28212 +{
28213 + return core_if->core_params->otg_ver;
28214 +}
28215 +
28216 +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
28217 +{
28218 + gotgctl_data_t otgctl;
28219 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28220 + return otgctl.b.hstnegscs;
28221 +}
28222 +
28223 +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
28224 +{
28225 + gotgctl_data_t otgctl;
28226 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28227 + return otgctl.b.sesreqscs;
28228 +}
28229 +
28230 +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
28231 +{
28232 + if(core_if->otg_ver == 0) {
28233 + gotgctl_data_t otgctl;
28234 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28235 + otgctl.b.hnpreq = val;
28236 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
28237 + } else {
28238 + core_if->otg_sts = val;
28239 + }
28240 +}
28241 +
28242 +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
28243 +{
28244 + return core_if->snpsid;
28245 +}
28246 +
28247 +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
28248 +{
28249 + gintsts_data_t gintsts;
28250 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
28251 + return gintsts.b.curmode;
28252 +}
28253 +
28254 +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
28255 +{
28256 + gusbcfg_data_t usbcfg;
28257 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28258 + return usbcfg.b.hnpcap;
28259 +}
28260 +
28261 +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
28262 +{
28263 + gusbcfg_data_t usbcfg;
28264 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28265 + usbcfg.b.hnpcap = val;
28266 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
28267 +}
28268 +
28269 +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
28270 +{
28271 + gusbcfg_data_t usbcfg;
28272 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28273 + return usbcfg.b.srpcap;
28274 +}
28275 +
28276 +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
28277 +{
28278 + gusbcfg_data_t usbcfg;
28279 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28280 + usbcfg.b.srpcap = val;
28281 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
28282 +}
28283 +
28284 +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
28285 +{
28286 + dcfg_data_t dcfg;
28287 + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
28288 +
28289 + dcfg.d32 = -1; //GRAYG
28290 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
28291 + if (NULL == core_if)
28292 + DWC_ERROR("reg request with NULL core_if\n");
28293 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
28294 + core_if, core_if->dev_if);
28295 + if (NULL == core_if->dev_if)
28296 + DWC_ERROR("reg request with NULL dev_if\n");
28297 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
28298 + "dev_global_regs(%p)\n", __func__,
28299 + core_if, core_if->dev_if,
28300 + core_if->dev_if->dev_global_regs);
28301 + if (NULL == core_if->dev_if->dev_global_regs)
28302 + DWC_ERROR("reg request with NULL dev_global_regs\n");
28303 + else {
28304 + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
28305 + "dev_global_regs(%p)->dcfg = %p\n", __func__,
28306 + core_if, core_if->dev_if,
28307 + core_if->dev_if->dev_global_regs,
28308 + &core_if->dev_if->dev_global_regs->dcfg);
28309 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
28310 + }
28311 + return dcfg.b.devspd;
28312 +}
28313 +
28314 +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
28315 +{
28316 + dcfg_data_t dcfg;
28317 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
28318 + dcfg.b.devspd = val;
28319 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
28320 +}
28321 +
28322 +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
28323 +{
28324 + hprt0_data_t hprt0;
28325 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28326 + return hprt0.b.prtconnsts;
28327 +}
28328 +
28329 +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
28330 +{
28331 + dsts_data_t dsts;
28332 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
28333 + return dsts.b.enumspd;
28334 +}
28335 +
28336 +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
28337 +{
28338 + hprt0_data_t hprt0;
28339 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28340 + return hprt0.b.prtpwr;
28341 +
28342 +}
28343 +
28344 +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
28345 +{
28346 + return core_if->hibernation_suspend;
28347 +}
28348 +
28349 +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
28350 +{
28351 + hprt0_data_t hprt0;
28352 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28353 + hprt0.b.prtpwr = val;
28354 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28355 +}
28356 +
28357 +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
28358 +{
28359 + hprt0_data_t hprt0;
28360 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28361 + return hprt0.b.prtsusp;
28362 +
28363 +}
28364 +
28365 +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
28366 +{
28367 + hprt0_data_t hprt0;
28368 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28369 + hprt0.b.prtsusp = val;
28370 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28371 +}
28372 +
28373 +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
28374 +{
28375 + hfir_data_t hfir;
28376 + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
28377 + return hfir.b.frint;
28378 +
28379 +}
28380 +
28381 +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
28382 +{
28383 + hfir_data_t hfir;
28384 + uint32_t fram_int;
28385 + fram_int = calc_frame_interval(core_if);
28386 + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
28387 + if (!core_if->core_params->reload_ctl) {
28388 + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
28389 + "not set to 1.\nShould load driver with reload_ctl=1"
28390 + " module parameter\n");
28391 + return;
28392 + }
28393 + switch (fram_int) {
28394 + case 3750:
28395 + if ((val < 3350) || (val > 4150)) {
28396 + DWC_WARN("HFIR interval for HS core and 30 MHz"
28397 + "clock freq should be from 3350 to 4150\n");
28398 + return;
28399 + }
28400 + break;
28401 + case 30000:
28402 + if ((val < 26820) || (val > 33180)) {
28403 + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
28404 + "clock freq should be from 26820 to 33180\n");
28405 + return;
28406 + }
28407 + break;
28408 + case 6000:
28409 + if ((val < 5360) || (val > 6640)) {
28410 + DWC_WARN("HFIR interval for HS core and 48 MHz"
28411 + "clock freq should be from 5360 to 6640\n");
28412 + return;
28413 + }
28414 + break;
28415 + case 48000:
28416 + if ((val < 42912) || (val > 53088)) {
28417 + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
28418 + "clock freq should be from 42912 to 53088\n");
28419 + return;
28420 + }
28421 + break;
28422 + case 7500:
28423 + if ((val < 6700) || (val > 8300)) {
28424 + DWC_WARN("HFIR interval for HS core and 60 MHz"
28425 + "clock freq should be from 6700 to 8300\n");
28426 + return;
28427 + }
28428 + break;
28429 + case 60000:
28430 + if ((val < 53640) || (val > 65536)) {
28431 + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
28432 + "clock freq should be from 53640 to 65536\n");
28433 + return;
28434 + }
28435 + break;
28436 + default:
28437 + DWC_WARN("Unknown frame interval\n");
28438 + return;
28439 + break;
28440 +
28441 + }
28442 + hfir.b.frint = val;
28443 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
28444 +}
28445 +
28446 +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
28447 +{
28448 + hcfg_data_t hcfg;
28449 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
28450 + return hcfg.b.modechtimen;
28451 +
28452 +}
28453 +
28454 +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
28455 +{
28456 + hcfg_data_t hcfg;
28457 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
28458 + hcfg.b.modechtimen = val;
28459 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
28460 +}
28461 +
28462 +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
28463 +{
28464 + hprt0_data_t hprt0;
28465 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28466 + hprt0.b.prtres = val;
28467 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28468 +}
28469 +
28470 +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
28471 +{
28472 + dctl_data_t dctl;
28473 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
28474 + return dctl.b.rmtwkupsig;
28475 +}
28476 +
28477 +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
28478 +{
28479 + glpmcfg_data_t lpmcfg;
28480 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28481 +
28482 + DWC_ASSERT(!
28483 + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
28484 + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
28485 + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
28486 +
28487 + return lpmcfg.b.prt_sleep_sts;
28488 +}
28489 +
28490 +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
28491 +{
28492 + glpmcfg_data_t lpmcfg;
28493 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28494 + return lpmcfg.b.rem_wkup_en;
28495 +}
28496 +
28497 +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
28498 +{
28499 + glpmcfg_data_t lpmcfg;
28500 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28501 + return lpmcfg.b.appl_resp;
28502 +}
28503 +
28504 +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
28505 +{
28506 + glpmcfg_data_t lpmcfg;
28507 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28508 + lpmcfg.b.appl_resp = val;
28509 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28510 +}
28511 +
28512 +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
28513 +{
28514 + glpmcfg_data_t lpmcfg;
28515 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28516 + return lpmcfg.b.hsic_connect;
28517 +}
28518 +
28519 +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
28520 +{
28521 + glpmcfg_data_t lpmcfg;
28522 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28523 + lpmcfg.b.hsic_connect = val;
28524 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28525 +}
28526 +
28527 +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
28528 +{
28529 + glpmcfg_data_t lpmcfg;
28530 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28531 + return lpmcfg.b.inv_sel_hsic;
28532 +
28533 +}
28534 +
28535 +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
28536 +{
28537 + glpmcfg_data_t lpmcfg;
28538 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28539 + lpmcfg.b.inv_sel_hsic = val;
28540 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28541 +}
28542 +
28543 +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
28544 +{
28545 + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28546 +}
28547 +
28548 +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
28549 +{
28550 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
28551 +}
28552 +
28553 +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
28554 +{
28555 + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28556 +}
28557 +
28558 +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
28559 +{
28560 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
28561 +}
28562 +
28563 +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
28564 +{
28565 + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
28566 +}
28567 +
28568 +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
28569 +{
28570 + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
28571 +}
28572 +
28573 +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
28574 +{
28575 + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
28576 +}
28577 +
28578 +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
28579 +{
28580 + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
28581 +}
28582 +
28583 +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
28584 +{
28585 + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
28586 +}
28587 +
28588 +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
28589 +{
28590 + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
28591 +}
28592 +
28593 +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
28594 +{
28595 + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
28596 +}
28597 +
28598 +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
28599 +{
28600 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
28601 +}
28602 +
28603 +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
28604 +{
28605 + return DWC_READ_REG32(core_if->host_if->hprt0);
28606 +
28607 +}
28608 +
28609 +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
28610 +{
28611 + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
28612 +}
28613 +
28614 +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
28615 +{
28616 + return DWC_READ_REG32(&core_if->core_global_regs->guid);
28617 +}
28618 +
28619 +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
28620 +{
28621 + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
28622 +}
28623 +
28624 +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
28625 +{
28626 + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
28627 +}
28628 +
28629 +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
28630 +{
28631 + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
28632 +}
28633 +
28634 +/**
28635 + * Start the SRP timer to detect when the SRP does not complete within
28636 + * 6 seconds.
28637 + *
28638 + * @param core_if the pointer to core_if strucure.
28639 + */
28640 +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
28641 +{
28642 + core_if->srp_timer_started = 1;
28643 + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
28644 +}
28645 +
28646 +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
28647 +{
28648 + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
28649 + gotgctl_data_t mem;
28650 + gotgctl_data_t val;
28651 +
28652 + val.d32 = DWC_READ_REG32(addr);
28653 + if (val.b.sesreq) {
28654 + DWC_ERROR("Session Request Already active!\n");
28655 + return;
28656 + }
28657 +
28658 + DWC_INFO("Session Request Initated\n"); //NOTICE
28659 + mem.d32 = DWC_READ_REG32(addr);
28660 + mem.b.sesreq = 1;
28661 + DWC_WRITE_REG32(addr, mem.d32);
28662 +
28663 + /* Start the SRP timer */
28664 + dwc_otg_pcd_start_srp_timer(core_if);
28665 + return;
28666 +}
28667 --- /dev/null
28668 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.h
28669 @@ -0,0 +1,1464 @@
28670 +/* ==========================================================================
28671 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
28672 + * $Revision: #123 $
28673 + * $Date: 2012/08/10 $
28674 + * $Change: 2047372 $
28675 + *
28676 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
28677 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
28678 + * otherwise expressly agreed to in writing between Synopsys and you.
28679 + *
28680 + * The Software IS NOT an item of Licensed Software or Licensed Product under
28681 + * any End User Software License Agreement or Agreement for Licensed Product
28682 + * with Synopsys or any supplement thereto. You are permitted to use and
28683 + * redistribute this Software in source and binary forms, with or without
28684 + * modification, provided that redistributions of source code must retain this
28685 + * notice. You may not view, use, disclose, copy or distribute this file or
28686 + * any information contained herein except pursuant to this license grant from
28687 + * Synopsys. If you do not agree with this notice, including the disclaimer
28688 + * below, then you are not authorized to use the Software.
28689 + *
28690 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
28691 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28692 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28693 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
28694 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28695 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28696 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28697 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28698 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28699 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28700 + * DAMAGE.
28701 + * ========================================================================== */
28702 +
28703 +#if !defined(__DWC_CIL_H__)
28704 +#define __DWC_CIL_H__
28705 +
28706 +#include "dwc_list.h"
28707 +#include "dwc_otg_dbg.h"
28708 +#include "dwc_otg_regs.h"
28709 +
28710 +#include "dwc_otg_core_if.h"
28711 +#include "dwc_otg_adp.h"
28712 +
28713 +/**
28714 + * @file
28715 + * This file contains the interface to the Core Interface Layer.
28716 + */
28717 +
28718 +#ifdef DWC_UTE_CFI
28719 +
28720 +#define MAX_DMA_DESCS_PER_EP 256
28721 +
28722 +/**
28723 + * Enumeration for the data buffer mode
28724 + */
28725 +typedef enum _data_buffer_mode {
28726 + BM_STANDARD = 0, /* data buffer is in normal mode */
28727 + BM_SG = 1, /* data buffer uses the scatter/gather mode */
28728 + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
28729 + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
28730 + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
28731 +} data_buffer_mode_e;
28732 +#endif //DWC_UTE_CFI
28733 +
28734 +/** Macros defined for DWC OTG HW Release version */
28735 +
28736 +#define OTG_CORE_REV_2_60a 0x4F54260A
28737 +#define OTG_CORE_REV_2_71a 0x4F54271A
28738 +#define OTG_CORE_REV_2_72a 0x4F54272A
28739 +#define OTG_CORE_REV_2_80a 0x4F54280A
28740 +#define OTG_CORE_REV_2_81a 0x4F54281A
28741 +#define OTG_CORE_REV_2_90a 0x4F54290A
28742 +#define OTG_CORE_REV_2_91a 0x4F54291A
28743 +#define OTG_CORE_REV_2_92a 0x4F54292A
28744 +#define OTG_CORE_REV_2_93a 0x4F54293A
28745 +#define OTG_CORE_REV_2_94a 0x4F54294A
28746 +#define OTG_CORE_REV_3_00a 0x4F54300A
28747 +
28748 +/**
28749 + * Information for each ISOC packet.
28750 + */
28751 +typedef struct iso_pkt_info {
28752 + uint32_t offset;
28753 + uint32_t length;
28754 + int32_t status;
28755 +} iso_pkt_info_t;
28756 +
28757 +/**
28758 + * The <code>dwc_ep</code> structure represents the state of a single
28759 + * endpoint when acting in device mode. It contains the data items
28760 + * needed for an endpoint to be activated and transfer packets.
28761 + */
28762 +typedef struct dwc_ep {
28763 + /** EP number used for register address lookup */
28764 + uint8_t num;
28765 + /** EP direction 0 = OUT */
28766 + unsigned is_in:1;
28767 + /** EP active. */
28768 + unsigned active:1;
28769 +
28770 + /**
28771 + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
28772 + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
28773 + unsigned tx_fifo_num:4;
28774 + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
28775 + unsigned type:2;
28776 +#define DWC_OTG_EP_TYPE_CONTROL 0
28777 +#define DWC_OTG_EP_TYPE_ISOC 1
28778 +#define DWC_OTG_EP_TYPE_BULK 2
28779 +#define DWC_OTG_EP_TYPE_INTR 3
28780 +
28781 + /** DATA start PID for INTR and BULK EP */
28782 + unsigned data_pid_start:1;
28783 + /** Frame (even/odd) for ISOC EP */
28784 + unsigned even_odd_frame:1;
28785 + /** Max Packet bytes */
28786 + unsigned maxpacket:11;
28787 +
28788 + /** Max Transfer size */
28789 + uint32_t maxxfer;
28790 +
28791 + /** @name Transfer state */
28792 + /** @{ */
28793 +
28794 + /**
28795 + * Pointer to the beginning of the transfer buffer -- do not modify
28796 + * during transfer.
28797 + */
28798 +
28799 + dwc_dma_t dma_addr;
28800 +
28801 + dwc_dma_t dma_desc_addr;
28802 + dwc_otg_dev_dma_desc_t *desc_addr;
28803 +
28804 + uint8_t *start_xfer_buff;
28805 + /** pointer to the transfer buffer */
28806 + uint8_t *xfer_buff;
28807 + /** Number of bytes to transfer */
28808 + unsigned xfer_len:19;
28809 + /** Number of bytes transferred. */
28810 + unsigned xfer_count:19;
28811 + /** Sent ZLP */
28812 + unsigned sent_zlp:1;
28813 + /** Total len for control transfer */
28814 + unsigned total_len:19;
28815 +
28816 + /** stall clear flag */
28817 + unsigned stall_clear_flag:1;
28818 +
28819 + /** SETUP pkt cnt rollover flag for EP0 out*/
28820 + unsigned stp_rollover;
28821 +
28822 +#ifdef DWC_UTE_CFI
28823 + /* The buffer mode */
28824 + data_buffer_mode_e buff_mode;
28825 +
28826 + /* The chain of DMA descriptors.
28827 + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
28828 + */
28829 + dwc_otg_dma_desc_t *descs;
28830 +
28831 + /* The DMA address of the descriptors chain start */
28832 + dma_addr_t descs_dma_addr;
28833 + /** This variable stores the length of the last enqueued request */
28834 + uint32_t cfi_req_len;
28835 +#endif //DWC_UTE_CFI
28836 +
28837 +/** Max DMA Descriptor count for any EP */
28838 +#define MAX_DMA_DESC_CNT 256
28839 + /** Allocated DMA Desc count */
28840 + uint32_t desc_cnt;
28841 +
28842 + /** bInterval */
28843 + uint32_t bInterval;
28844 + /** Next frame num to setup next ISOC transfer */
28845 + uint32_t frame_num;
28846 + /** Indicates SOF number overrun in DSTS */
28847 + uint8_t frm_overrun;
28848 +
28849 +#ifdef DWC_UTE_PER_IO
28850 + /** Next frame num for which will be setup DMA Desc */
28851 + uint32_t xiso_frame_num;
28852 + /** bInterval */
28853 + uint32_t xiso_bInterval;
28854 + /** Count of currently active transfers - shall be either 0 or 1 */
28855 + int xiso_active_xfers;
28856 + int xiso_queued_xfers;
28857 +#endif
28858 +#ifdef DWC_EN_ISOC
28859 + /**
28860 + * Variables specific for ISOC EPs
28861 + *
28862 + */
28863 + /** DMA addresses of ISOC buffers */
28864 + dwc_dma_t dma_addr0;
28865 + dwc_dma_t dma_addr1;
28866 +
28867 + dwc_dma_t iso_dma_desc_addr;
28868 + dwc_otg_dev_dma_desc_t *iso_desc_addr;
28869 +
28870 + /** pointer to the transfer buffers */
28871 + uint8_t *xfer_buff0;
28872 + uint8_t *xfer_buff1;
28873 +
28874 + /** number of ISOC Buffer is processing */
28875 + uint32_t proc_buf_num;
28876 + /** Interval of ISOC Buffer processing */
28877 + uint32_t buf_proc_intrvl;
28878 + /** Data size for regular frame */
28879 + uint32_t data_per_frame;
28880 +
28881 + /* todo - pattern data support is to be implemented in the future */
28882 + /** Data size for pattern frame */
28883 + uint32_t data_pattern_frame;
28884 + /** Frame number of pattern data */
28885 + uint32_t sync_frame;
28886 +
28887 + /** bInterval */
28888 + uint32_t bInterval;
28889 + /** ISO Packet number per frame */
28890 + uint32_t pkt_per_frm;
28891 + /** Next frame num for which will be setup DMA Desc */
28892 + uint32_t next_frame;
28893 + /** Number of packets per buffer processing */
28894 + uint32_t pkt_cnt;
28895 + /** Info for all isoc packets */
28896 + iso_pkt_info_t *pkt_info;
28897 + /** current pkt number */
28898 + uint32_t cur_pkt;
28899 + /** current pkt number */
28900 + uint8_t *cur_pkt_addr;
28901 + /** current pkt number */
28902 + uint32_t cur_pkt_dma_addr;
28903 +#endif /* DWC_EN_ISOC */
28904 +
28905 +/** @} */
28906 +} dwc_ep_t;
28907 +
28908 +/*
28909 + * Reasons for halting a host channel.
28910 + */
28911 +typedef enum dwc_otg_halt_status {
28912 + DWC_OTG_HC_XFER_NO_HALT_STATUS,
28913 + DWC_OTG_HC_XFER_COMPLETE,
28914 + DWC_OTG_HC_XFER_URB_COMPLETE,
28915 + DWC_OTG_HC_XFER_ACK,
28916 + DWC_OTG_HC_XFER_NAK,
28917 + DWC_OTG_HC_XFER_NYET,
28918 + DWC_OTG_HC_XFER_STALL,
28919 + DWC_OTG_HC_XFER_XACT_ERR,
28920 + DWC_OTG_HC_XFER_FRAME_OVERRUN,
28921 + DWC_OTG_HC_XFER_BABBLE_ERR,
28922 + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
28923 + DWC_OTG_HC_XFER_AHB_ERR,
28924 + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
28925 + DWC_OTG_HC_XFER_URB_DEQUEUE
28926 +} dwc_otg_halt_status_e;
28927 +
28928 +/**
28929 + * Host channel descriptor. This structure represents the state of a single
28930 + * host channel when acting in host mode. It contains the data items needed to
28931 + * transfer packets to an endpoint via a host channel.
28932 + */
28933 +typedef struct dwc_hc {
28934 + /** Host channel number used for register address lookup */
28935 + uint8_t hc_num;
28936 +
28937 + /** Device to access */
28938 + unsigned dev_addr:7;
28939 +
28940 + /** EP to access */
28941 + unsigned ep_num:4;
28942 +
28943 + /** EP direction. 0: OUT, 1: IN */
28944 + unsigned ep_is_in:1;
28945 +
28946 + /**
28947 + * EP speed.
28948 + * One of the following values:
28949 + * - DWC_OTG_EP_SPEED_LOW
28950 + * - DWC_OTG_EP_SPEED_FULL
28951 + * - DWC_OTG_EP_SPEED_HIGH
28952 + */
28953 + unsigned speed:2;
28954 +#define DWC_OTG_EP_SPEED_LOW 0
28955 +#define DWC_OTG_EP_SPEED_FULL 1
28956 +#define DWC_OTG_EP_SPEED_HIGH 2
28957 +
28958 + /**
28959 + * Endpoint type.
28960 + * One of the following values:
28961 + * - DWC_OTG_EP_TYPE_CONTROL: 0
28962 + * - DWC_OTG_EP_TYPE_ISOC: 1
28963 + * - DWC_OTG_EP_TYPE_BULK: 2
28964 + * - DWC_OTG_EP_TYPE_INTR: 3
28965 + */
28966 + unsigned ep_type:2;
28967 +
28968 + /** Max packet size in bytes */
28969 + unsigned max_packet:11;
28970 +
28971 + /**
28972 + * PID for initial transaction.
28973 + * 0: DATA0,<br>
28974 + * 1: DATA2,<br>
28975 + * 2: DATA1,<br>
28976 + * 3: MDATA (non-Control EP),
28977 + * SETUP (Control EP)
28978 + */
28979 + unsigned data_pid_start:2;
28980 +#define DWC_OTG_HC_PID_DATA0 0
28981 +#define DWC_OTG_HC_PID_DATA2 1
28982 +#define DWC_OTG_HC_PID_DATA1 2
28983 +#define DWC_OTG_HC_PID_MDATA 3
28984 +#define DWC_OTG_HC_PID_SETUP 3
28985 +
28986 + /** Number of periodic transactions per (micro)frame */
28987 + unsigned multi_count:2;
28988 +
28989 + /** @name Transfer State */
28990 + /** @{ */
28991 +
28992 + /** Pointer to the current transfer buffer position. */
28993 + uint8_t *xfer_buff;
28994 + /**
28995 + * In Buffer DMA mode this buffer will be used
28996 + * if xfer_buff is not DWORD aligned.
28997 + */
28998 + dwc_dma_t align_buff;
28999 + /** Total number of bytes to transfer. */
29000 + uint32_t xfer_len;
29001 + /** Number of bytes transferred so far. */
29002 + uint32_t xfer_count;
29003 + /** Packet count at start of transfer.*/
29004 + uint16_t start_pkt_count;
29005 +
29006 + /**
29007 + * Flag to indicate whether the transfer has been started. Set to 1 if
29008 + * it has been started, 0 otherwise.
29009 + */
29010 + uint8_t xfer_started;
29011 +
29012 + /**
29013 + * Set to 1 to indicate that a PING request should be issued on this
29014 + * channel. If 0, process normally.
29015 + */
29016 + uint8_t do_ping;
29017 +
29018 + /**
29019 + * Set to 1 to indicate that the error count for this transaction is
29020 + * non-zero. Set to 0 if the error count is 0.
29021 + */
29022 + uint8_t error_state;
29023 +
29024 + /**
29025 + * Set to 1 to indicate that this channel should be halted the next
29026 + * time a request is queued for the channel. This is necessary in
29027 + * slave mode if no request queue space is available when an attempt
29028 + * is made to halt the channel.
29029 + */
29030 + uint8_t halt_on_queue;
29031 +
29032 + /**
29033 + * Set to 1 if the host channel has been halted, but the core is not
29034 + * finished flushing queued requests. Otherwise 0.
29035 + */
29036 + uint8_t halt_pending;
29037 +
29038 + /**
29039 + * Reason for halting the host channel.
29040 + */
29041 + dwc_otg_halt_status_e halt_status;
29042 +
29043 + /*
29044 + * Split settings for the host channel
29045 + */
29046 + uint8_t do_split; /**< Enable split for the channel */
29047 + uint8_t complete_split; /**< Enable complete split */
29048 + uint8_t hub_addr; /**< Address of high speed hub */
29049 +
29050 + uint8_t port_addr; /**< Port of the low/full speed device */
29051 + /** Split transaction position
29052 + * One of the following values:
29053 + * - DWC_HCSPLIT_XACTPOS_MID
29054 + * - DWC_HCSPLIT_XACTPOS_BEGIN
29055 + * - DWC_HCSPLIT_XACTPOS_END
29056 + * - DWC_HCSPLIT_XACTPOS_ALL */
29057 + uint8_t xact_pos;
29058 +
29059 + /** Set when the host channel does a short read. */
29060 + uint8_t short_read;
29061 +
29062 + /**
29063 + * Number of requests issued for this channel since it was assigned to
29064 + * the current transfer (not counting PINGs).
29065 + */
29066 + uint8_t requests;
29067 +
29068 + /**
29069 + * Queue Head for the transfer being processed by this channel.
29070 + */
29071 + struct dwc_otg_qh *qh;
29072 +
29073 + /** @} */
29074 +
29075 + /** Entry in list of host channels. */
29076 + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
29077 +
29078 + /** @name Descriptor DMA support */
29079 + /** @{ */
29080 +
29081 + /** Number of Transfer Descriptors */
29082 + uint16_t ntd;
29083 +
29084 + /** Descriptor List DMA address */
29085 + dwc_dma_t desc_list_addr;
29086 +
29087 + /** Scheduling micro-frame bitmap. */
29088 + uint8_t schinfo;
29089 +
29090 + /** @} */
29091 +} dwc_hc_t;
29092 +
29093 +/**
29094 + * The following parameters may be specified when starting the module. These
29095 + * parameters define how the DWC_otg controller should be configured.
29096 + */
29097 +typedef struct dwc_otg_core_params {
29098 + int32_t opt;
29099 +
29100 + /**
29101 + * Specifies the OTG capabilities. The driver will automatically
29102 + * detect the value for this parameter if none is specified.
29103 + * 0 - HNP and SRP capable (default)
29104 + * 1 - SRP Only capable
29105 + * 2 - No HNP/SRP capable
29106 + */
29107 + int32_t otg_cap;
29108 +
29109 + /**
29110 + * Specifies whether to use slave or DMA mode for accessing the data
29111 + * FIFOs. The driver will automatically detect the value for this
29112 + * parameter if none is specified.
29113 + * 0 - Slave
29114 + * 1 - DMA (default, if available)
29115 + */
29116 + int32_t dma_enable;
29117 +
29118 + /**
29119 + * When DMA mode is enabled specifies whether to use address DMA or DMA
29120 + * Descriptor mode for accessing the data FIFOs in device mode. The driver
29121 + * will automatically detect the value for this if none is specified.
29122 + * 0 - address DMA
29123 + * 1 - DMA Descriptor(default, if available)
29124 + */
29125 + int32_t dma_desc_enable;
29126 + /** The DMA Burst size (applicable only for External DMA
29127 + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
29128 + */
29129 + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
29130 +
29131 + /**
29132 + * Specifies the maximum speed of operation in host and device mode.
29133 + * The actual speed depends on the speed of the attached device and
29134 + * the value of phy_type. The actual speed depends on the speed of the
29135 + * attached device.
29136 + * 0 - High Speed (default)
29137 + * 1 - Full Speed
29138 + */
29139 + int32_t speed;
29140 + /** Specifies whether low power mode is supported when attached
29141 + * to a Full Speed or Low Speed device in host mode.
29142 + * 0 - Don't support low power mode (default)
29143 + * 1 - Support low power mode
29144 + */
29145 + int32_t host_support_fs_ls_low_power;
29146 +
29147 + /** Specifies the PHY clock rate in low power mode when connected to a
29148 + * Low Speed device in host mode. This parameter is applicable only if
29149 + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
29150 + * then defaults to 6 MHZ otherwise 48 MHZ.
29151 + *
29152 + * 0 - 48 MHz
29153 + * 1 - 6 MHz
29154 + */
29155 + int32_t host_ls_low_power_phy_clk;
29156 +
29157 + /**
29158 + * 0 - Use cC FIFO size parameters
29159 + * 1 - Allow dynamic FIFO sizing (default)
29160 + */
29161 + int32_t enable_dynamic_fifo;
29162 +
29163 + /** Total number of 4-byte words in the data FIFO memory. This
29164 + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
29165 + * Tx FIFOs.
29166 + * 32 to 32768 (default 8192)
29167 + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
29168 + */
29169 + int32_t data_fifo_size;
29170 +
29171 + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
29172 + * FIFO sizing is enabled.
29173 + * 16 to 32768 (default 1064)
29174 + */
29175 + int32_t dev_rx_fifo_size;
29176 +
29177 + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
29178 + * when dynamic FIFO sizing is enabled.
29179 + * 16 to 32768 (default 1024)
29180 + */
29181 + int32_t dev_nperio_tx_fifo_size;
29182 +
29183 + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
29184 + * mode when dynamic FIFO sizing is enabled.
29185 + * 4 to 768 (default 256)
29186 + */
29187 + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
29188 +
29189 + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
29190 + * FIFO sizing is enabled.
29191 + * 16 to 32768 (default 1024)
29192 + */
29193 + int32_t host_rx_fifo_size;
29194 +
29195 + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
29196 + * when Dynamic FIFO sizing is enabled in the core.
29197 + * 16 to 32768 (default 1024)
29198 + */
29199 + int32_t host_nperio_tx_fifo_size;
29200 +
29201 + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
29202 + * FIFO sizing is enabled.
29203 + * 16 to 32768 (default 1024)
29204 + */
29205 + int32_t host_perio_tx_fifo_size;
29206 +
29207 + /** The maximum transfer size supported in bytes.
29208 + * 2047 to 65,535 (default 65,535)
29209 + */
29210 + int32_t max_transfer_size;
29211 +
29212 + /** The maximum number of packets in a transfer.
29213 + * 15 to 511 (default 511)
29214 + */
29215 + int32_t max_packet_count;
29216 +
29217 + /** The number of host channel registers to use.
29218 + * 1 to 16 (default 12)
29219 + * Note: The FPGA configuration supports a maximum of 12 host channels.
29220 + */
29221 + int32_t host_channels;
29222 +
29223 + /** The number of endpoints in addition to EP0 available for device
29224 + * mode operations.
29225 + * 1 to 15 (default 6 IN and OUT)
29226 + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
29227 + * endpoints in addition to EP0.
29228 + */
29229 + int32_t dev_endpoints;
29230 +
29231 + /**
29232 + * Specifies the type of PHY interface to use. By default, the driver
29233 + * will automatically detect the phy_type.
29234 + *
29235 + * 0 - Full Speed PHY
29236 + * 1 - UTMI+ (default)
29237 + * 2 - ULPI
29238 + */
29239 + int32_t phy_type;
29240 +
29241 + /**
29242 + * Specifies the UTMI+ Data Width. This parameter is
29243 + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
29244 + * PHY_TYPE, this parameter indicates the data width between
29245 + * the MAC and the ULPI Wrapper.) Also, this parameter is
29246 + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
29247 + * to "8 and 16 bits", meaning that the core has been
29248 + * configured to work at either data path width.
29249 + *
29250 + * 8 or 16 bits (default 16)
29251 + */
29252 + int32_t phy_utmi_width;
29253 +
29254 + /**
29255 + * Specifies whether the ULPI operates at double or single
29256 + * data rate. This parameter is only applicable if PHY_TYPE is
29257 + * ULPI.
29258 + *
29259 + * 0 - single data rate ULPI interface with 8 bit wide data
29260 + * bus (default)
29261 + * 1 - double data rate ULPI interface with 4 bit wide data
29262 + * bus
29263 + */
29264 + int32_t phy_ulpi_ddr;
29265 +
29266 + /**
29267 + * Specifies whether to use the internal or external supply to
29268 + * drive the vbus with a ULPI phy.
29269 + */
29270 + int32_t phy_ulpi_ext_vbus;
29271 +
29272 + /**
29273 + * Specifies whether to use the I2Cinterface for full speed PHY. This
29274 + * parameter is only applicable if PHY_TYPE is FS.
29275 + * 0 - No (default)
29276 + * 1 - Yes
29277 + */
29278 + int32_t i2c_enable;
29279 +
29280 + int32_t ulpi_fs_ls;
29281 +
29282 + int32_t ts_dline;
29283 +
29284 + /**
29285 + * Specifies whether dedicated transmit FIFOs are
29286 + * enabled for non periodic IN endpoints in device mode
29287 + * 0 - No
29288 + * 1 - Yes
29289 + */
29290 + int32_t en_multiple_tx_fifo;
29291 +
29292 + /** Number of 4-byte words in each of the Tx FIFOs in device
29293 + * mode when dynamic FIFO sizing is enabled.
29294 + * 4 to 768 (default 256)
29295 + */
29296 + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
29297 +
29298 + /** Thresholding enable flag-
29299 + * bit 0 - enable non-ISO Tx thresholding
29300 + * bit 1 - enable ISO Tx thresholding
29301 + * bit 2 - enable Rx thresholding
29302 + */
29303 + uint32_t thr_ctl;
29304 +
29305 + /** Thresholding length for Tx
29306 + * FIFOs in 32 bit DWORDs
29307 + */
29308 + uint32_t tx_thr_length;
29309 +
29310 + /** Thresholding length for Rx
29311 + * FIFOs in 32 bit DWORDs
29312 + */
29313 + uint32_t rx_thr_length;
29314 +
29315 + /**
29316 + * Specifies whether LPM (Link Power Management) support is enabled
29317 + */
29318 + int32_t lpm_enable;
29319 +
29320 + /** Per Transfer Interrupt
29321 + * mode enable flag
29322 + * 1 - Enabled
29323 + * 0 - Disabled
29324 + */
29325 + int32_t pti_enable;
29326 +
29327 + /** Multi Processor Interrupt
29328 + * mode enable flag
29329 + * 1 - Enabled
29330 + * 0 - Disabled
29331 + */
29332 + int32_t mpi_enable;
29333 +
29334 + /** IS_USB Capability
29335 + * 1 - Enabled
29336 + * 0 - Disabled
29337 + */
29338 + int32_t ic_usb_cap;
29339 +
29340 + /** AHB Threshold Ratio
29341 + * 2'b00 AHB Threshold = MAC Threshold
29342 + * 2'b01 AHB Threshold = 1/2 MAC Threshold
29343 + * 2'b10 AHB Threshold = 1/4 MAC Threshold
29344 + * 2'b11 AHB Threshold = 1/8 MAC Threshold
29345 + */
29346 + int32_t ahb_thr_ratio;
29347 +
29348 + /** ADP Support
29349 + * 1 - Enabled
29350 + * 0 - Disabled
29351 + */
29352 + int32_t adp_supp_enable;
29353 +
29354 + /** HFIR Reload Control
29355 + * 0 - The HFIR cannot be reloaded dynamically.
29356 + * 1 - Allow dynamic reloading of the HFIR register during runtime.
29357 + */
29358 + int32_t reload_ctl;
29359 +
29360 + /** DCFG: Enable device Out NAK
29361 + * 0 - The core does not set NAK after Bulk Out transfer complete.
29362 + * 1 - The core sets NAK after Bulk OUT transfer complete.
29363 + */
29364 + int32_t dev_out_nak;
29365 +
29366 + /** DCFG: Enable Continue on BNA
29367 + * After receiving BNA interrupt the core disables the endpoint,when the
29368 + * endpoint is re-enabled by the application the core starts processing
29369 + * 0 - from the DOEPDMA descriptor
29370 + * 1 - from the descriptor which received the BNA.
29371 + */
29372 + int32_t cont_on_bna;
29373 +
29374 + /** GAHBCFG: AHB Single Support
29375 + * This bit when programmed supports SINGLE transfers for remainder
29376 + * data in a transfer for DMA mode of operation.
29377 + * 0 - in this case the remainder data will be sent using INCR burst size.
29378 + * 1 - in this case the remainder data will be sent using SINGLE burst size.
29379 + */
29380 + int32_t ahb_single;
29381 +
29382 + /** Core Power down mode
29383 + * 0 - No Power Down is enabled
29384 + * 1 - Reserved
29385 + * 2 - Complete Power Down (Hibernation)
29386 + */
29387 + int32_t power_down;
29388 +
29389 + /** OTG revision supported
29390 + * 0 - OTG 1.3 revision
29391 + * 1 - OTG 2.0 revision
29392 + */
29393 + int32_t otg_ver;
29394 +
29395 +} dwc_otg_core_params_t;
29396 +
29397 +#ifdef DEBUG
29398 +struct dwc_otg_core_if;
29399 +typedef struct hc_xfer_info {
29400 + struct dwc_otg_core_if *core_if;
29401 + dwc_hc_t *hc;
29402 +} hc_xfer_info_t;
29403 +#endif
29404 +
29405 +typedef struct ep_xfer_info {
29406 + struct dwc_otg_core_if *core_if;
29407 + dwc_ep_t *ep;
29408 + uint8_t state;
29409 +} ep_xfer_info_t;
29410 +/*
29411 + * Device States
29412 + */
29413 +typedef enum dwc_otg_lx_state {
29414 + /** On state */
29415 + DWC_OTG_L0,
29416 + /** LPM sleep state*/
29417 + DWC_OTG_L1,
29418 + /** USB suspend state*/
29419 + DWC_OTG_L2,
29420 + /** Off state*/
29421 + DWC_OTG_L3
29422 +} dwc_otg_lx_state_e;
29423 +
29424 +struct dwc_otg_global_regs_backup {
29425 + uint32_t gotgctl_local;
29426 + uint32_t gintmsk_local;
29427 + uint32_t gahbcfg_local;
29428 + uint32_t gusbcfg_local;
29429 + uint32_t grxfsiz_local;
29430 + uint32_t gnptxfsiz_local;
29431 +#ifdef CONFIG_USB_DWC_OTG_LPM
29432 + uint32_t glpmcfg_local;
29433 +#endif
29434 + uint32_t gi2cctl_local;
29435 + uint32_t hptxfsiz_local;
29436 + uint32_t pcgcctl_local;
29437 + uint32_t gdfifocfg_local;
29438 + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
29439 + uint32_t gpwrdn_local;
29440 + uint32_t xhib_pcgcctl;
29441 + uint32_t xhib_gpwrdn;
29442 +};
29443 +
29444 +struct dwc_otg_host_regs_backup {
29445 + uint32_t hcfg_local;
29446 + uint32_t haintmsk_local;
29447 + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
29448 + uint32_t hprt0_local;
29449 + uint32_t hfir_local;
29450 +};
29451 +
29452 +struct dwc_otg_dev_regs_backup {
29453 + uint32_t dcfg;
29454 + uint32_t dctl;
29455 + uint32_t daintmsk;
29456 + uint32_t diepmsk;
29457 + uint32_t doepmsk;
29458 + uint32_t diepctl[MAX_EPS_CHANNELS];
29459 + uint32_t dieptsiz[MAX_EPS_CHANNELS];
29460 + uint32_t diepdma[MAX_EPS_CHANNELS];
29461 +};
29462 +/**
29463 + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
29464 + * the DWC_otg controller acting in either host or device mode. It
29465 + * represents the programming view of the controller as a whole.
29466 + */
29467 +struct dwc_otg_core_if {
29468 + /** Parameters that define how the core should be configured.*/
29469 + dwc_otg_core_params_t *core_params;
29470 +
29471 + /** Core Global registers starting at offset 000h. */
29472 + dwc_otg_core_global_regs_t *core_global_regs;
29473 +
29474 + /** Device-specific information */
29475 + dwc_otg_dev_if_t *dev_if;
29476 + /** Host-specific information */
29477 + dwc_otg_host_if_t *host_if;
29478 +
29479 + /** Value from SNPSID register */
29480 + uint32_t snpsid;
29481 +
29482 + /*
29483 + * Set to 1 if the core PHY interface bits in USBCFG have been
29484 + * initialized.
29485 + */
29486 + uint8_t phy_init_done;
29487 +
29488 + /*
29489 + * SRP Success flag, set by srp success interrupt in FS I2C mode
29490 + */
29491 + uint8_t srp_success;
29492 + uint8_t srp_timer_started;
29493 + /** Timer for SRP. If it expires before SRP is successful
29494 + * clear the SRP. */
29495 + dwc_timer_t *srp_timer;
29496 +
29497 +#ifdef DWC_DEV_SRPCAP
29498 + /* This timer is needed to power on the hibernated host core if SRP is not
29499 + * initiated on connected SRP capable device for limited period of time
29500 + */
29501 + uint8_t pwron_timer_started;
29502 + dwc_timer_t *pwron_timer;
29503 +#endif
29504 + /* Common configuration information */
29505 + /** Power and Clock Gating Control Register */
29506 + volatile uint32_t *pcgcctl;
29507 +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
29508 +
29509 + /** Push/pop addresses for endpoints or host channels.*/
29510 + uint32_t *data_fifo[MAX_EPS_CHANNELS];
29511 +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
29512 +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
29513 +
29514 + /** Total RAM for FIFOs (Bytes) */
29515 + uint16_t total_fifo_size;
29516 + /** Size of Rx FIFO (Bytes) */
29517 + uint16_t rx_fifo_size;
29518 + /** Size of Non-periodic Tx FIFO (Bytes) */
29519 + uint16_t nperio_tx_fifo_size;
29520 +
29521 + /** 1 if DMA is enabled, 0 otherwise. */
29522 + uint8_t dma_enable;
29523 +
29524 + /** 1 if DMA descriptor is enabled, 0 otherwise. */
29525 + uint8_t dma_desc_enable;
29526 +
29527 + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
29528 + uint8_t pti_enh_enable;
29529 +
29530 + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
29531 + uint8_t multiproc_int_enable;
29532 +
29533 + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
29534 + uint8_t en_multiple_tx_fifo;
29535 +
29536 + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
29537 + * process of being queued */
29538 + uint8_t queuing_high_bandwidth;
29539 +
29540 + /** Hardware Configuration -- stored here for convenience.*/
29541 + hwcfg1_data_t hwcfg1;
29542 + hwcfg2_data_t hwcfg2;
29543 + hwcfg3_data_t hwcfg3;
29544 + hwcfg4_data_t hwcfg4;
29545 + fifosize_data_t hptxfsiz;
29546 +
29547 + /** Host and Device Configuration -- stored here for convenience.*/
29548 + hcfg_data_t hcfg;
29549 + dcfg_data_t dcfg;
29550 +
29551 + /** The operational State, during transations
29552 + * (a_host>>a_peripherial and b_device=>b_host) this may not
29553 + * match the core but allows the software to determine
29554 + * transitions.
29555 + */
29556 + uint8_t op_state;
29557 +
29558 + /**
29559 + * Set to 1 if the HCD needs to be restarted on a session request
29560 + * interrupt. This is required if no connector ID status change has
29561 + * occurred since the HCD was last disconnected.
29562 + */
29563 + uint8_t restart_hcd_on_session_req;
29564 +
29565 + /** HCD callbacks */
29566 + /** A-Device is a_host */
29567 +#define A_HOST (1)
29568 + /** A-Device is a_suspend */
29569 +#define A_SUSPEND (2)
29570 + /** A-Device is a_peripherial */
29571 +#define A_PERIPHERAL (3)
29572 + /** B-Device is operating as a Peripheral. */
29573 +#define B_PERIPHERAL (4)
29574 + /** B-Device is operating as a Host. */
29575 +#define B_HOST (5)
29576 +
29577 + /** HCD callbacks */
29578 + struct dwc_otg_cil_callbacks *hcd_cb;
29579 + /** PCD callbacks */
29580 + struct dwc_otg_cil_callbacks *pcd_cb;
29581 +
29582 + /** Device mode Periodic Tx FIFO Mask */
29583 + uint32_t p_tx_msk;
29584 + /** Device mode Periodic Tx FIFO Mask */
29585 + uint32_t tx_msk;
29586 +
29587 + /** Workqueue object used for handling several interrupts */
29588 + dwc_workq_t *wq_otg;
29589 +
29590 + /** Timer object used for handling "Wakeup Detected" Interrupt */
29591 + dwc_timer_t *wkp_timer;
29592 + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
29593 + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
29594 + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
29595 + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
29596 +#ifdef DEBUG
29597 + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
29598 +
29599 + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
29600 + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
29601 +
29602 + uint32_t hfnum_7_samples;
29603 + uint64_t hfnum_7_frrem_accum;
29604 + uint32_t hfnum_0_samples;
29605 + uint64_t hfnum_0_frrem_accum;
29606 + uint32_t hfnum_other_samples;
29607 + uint64_t hfnum_other_frrem_accum;
29608 +#endif
29609 +
29610 +#ifdef DWC_UTE_CFI
29611 + uint16_t pwron_rxfsiz;
29612 + uint16_t pwron_gnptxfsiz;
29613 + uint16_t pwron_txfsiz[15];
29614 +
29615 + uint16_t init_rxfsiz;
29616 + uint16_t init_gnptxfsiz;
29617 + uint16_t init_txfsiz[15];
29618 +#endif
29619 +
29620 + /** Lx state of device */
29621 + dwc_otg_lx_state_e lx_state;
29622 +
29623 + /** Saved Core Global registers */
29624 + struct dwc_otg_global_regs_backup *gr_backup;
29625 + /** Saved Host registers */
29626 + struct dwc_otg_host_regs_backup *hr_backup;
29627 + /** Saved Device registers */
29628 + struct dwc_otg_dev_regs_backup *dr_backup;
29629 +
29630 + /** Power Down Enable */
29631 + uint32_t power_down;
29632 +
29633 + /** ADP support Enable */
29634 + uint32_t adp_enable;
29635 +
29636 + /** ADP structure object */
29637 + dwc_otg_adp_t adp;
29638 +
29639 + /** hibernation/suspend flag */
29640 + int hibernation_suspend;
29641 +
29642 + /** Device mode extended hibernation flag */
29643 + int xhib;
29644 +
29645 + /** OTG revision supported */
29646 + uint32_t otg_ver;
29647 +
29648 + /** OTG status flag used for HNP polling */
29649 + uint8_t otg_sts;
29650 +
29651 + /** Pointer to either hcd->lock or pcd->lock */
29652 + dwc_spinlock_t *lock;
29653 +
29654 + /** Start predict NextEP based on Learning Queue if equal 1,
29655 + * also used as counter of disabled NP IN EP's */
29656 + uint8_t start_predict;
29657 +
29658 + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
29659 + * active, 0xff otherwise */
29660 + uint8_t nextep_seq[MAX_EPS_CHANNELS];
29661 +
29662 + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
29663 + uint8_t first_in_nextep_seq;
29664 +
29665 + /** Frame number while entering to ISR - needed for ISOCs **/
29666 + uint32_t frame_num;
29667 +
29668 +};
29669 +
29670 +#ifdef DEBUG
29671 +/*
29672 + * This function is called when transfer is timed out.
29673 + */
29674 +extern void hc_xfer_timeout(void *ptr);
29675 +#endif
29676 +
29677 +/*
29678 + * This function is called when transfer is timed out on endpoint.
29679 + */
29680 +extern void ep_xfer_timeout(void *ptr);
29681 +
29682 +/*
29683 + * The following functions are functions for works
29684 + * using during handling some interrupts
29685 + */
29686 +extern void w_conn_id_status_change(void *p);
29687 +
29688 +extern void w_wakeup_detected(void *p);
29689 +
29690 +/** Saves global register values into system memory. */
29691 +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
29692 +/** Saves device register values into system memory. */
29693 +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
29694 +/** Saves host register values into system memory. */
29695 +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
29696 +/** Restore global register values. */
29697 +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
29698 +/** Restore host register values. */
29699 +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
29700 +/** Restore device register values. */
29701 +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
29702 + int rem_wakeup);
29703 +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
29704 +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
29705 + int is_host);
29706 +
29707 +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
29708 + int restore_mode, int reset);
29709 +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
29710 + int rem_wakeup, int reset);
29711 +
29712 +/*
29713 + * The following functions support initialization of the CIL driver component
29714 + * and the DWC_otg controller.
29715 + */
29716 +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
29717 +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
29718 +
29719 +/** @name Device CIL Functions
29720 + * The following functions support managing the DWC_otg controller in device
29721 + * mode.
29722 + */
29723 +/**@{*/
29724 +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
29725 +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
29726 + uint32_t * _dest);
29727 +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
29728 +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29729 +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29730 +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29731 +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
29732 + dwc_ep_t * _ep);
29733 +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
29734 + dwc_ep_t * _ep);
29735 +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
29736 + dwc_ep_t * _ep);
29737 +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
29738 + dwc_ep_t * _ep);
29739 +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
29740 + dwc_ep_t * _ep, int _dma);
29741 +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29742 +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
29743 + dwc_ep_t * _ep);
29744 +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
29745 +
29746 +#ifdef DWC_EN_ISOC
29747 +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
29748 + dwc_ep_t * ep);
29749 +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
29750 + dwc_ep_t * ep);
29751 +#endif /* DWC_EN_ISOC */
29752 +/**@}*/
29753 +
29754 +/** @name Host CIL Functions
29755 + * The following functions support managing the DWC_otg controller in host
29756 + * mode.
29757 + */
29758 +/**@{*/
29759 +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29760 +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
29761 + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
29762 +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29763 +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
29764 + dwc_hc_t * _hc);
29765 +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
29766 + dwc_hc_t * _hc);
29767 +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29768 +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
29769 + dwc_hc_t * _hc);
29770 +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
29771 +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
29772 +
29773 +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
29774 + dwc_hc_t * hc);
29775 +
29776 +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
29777 +
29778 +/* Macro used to clear one channel interrupt */
29779 +#define clear_hc_int(_hc_regs_, _intr_) \
29780 +do { \
29781 + hcint_data_t hcint_clear = {.d32 = 0}; \
29782 + hcint_clear.b._intr_ = 1; \
29783 + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
29784 +} while (0)
29785 +
29786 +/*
29787 + * Macro used to disable one channel interrupt. Channel interrupts are
29788 + * disabled when the channel is halted or released by the interrupt handler.
29789 + * There is no need to handle further interrupts of that type until the
29790 + * channel is re-assigned. In fact, subsequent handling may cause crashes
29791 + * because the channel structures are cleaned up when the channel is released.
29792 + */
29793 +#define disable_hc_int(_hc_regs_, _intr_) \
29794 +do { \
29795 + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
29796 + hcintmsk.b._intr_ = 1; \
29797 + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
29798 +} while (0)
29799 +
29800 +/**
29801 + * This function Reads HPRT0 in preparation to modify. It keeps the
29802 + * WC bits 0 so that if they are read as 1, they won't clear when you
29803 + * write it back
29804 + */
29805 +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
29806 +{
29807 + hprt0_data_t hprt0;
29808 + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
29809 + hprt0.b.prtena = 0;
29810 + hprt0.b.prtconndet = 0;
29811 + hprt0.b.prtenchng = 0;
29812 + hprt0.b.prtovrcurrchng = 0;
29813 + return hprt0.d32;
29814 +}
29815 +
29816 +/**@}*/
29817 +
29818 +/** @name Common CIL Functions
29819 + * The following functions support managing the DWC_otg controller in either
29820 + * device or host mode.
29821 + */
29822 +/**@{*/
29823 +
29824 +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
29825 + uint8_t * dest, uint16_t bytes);
29826 +
29827 +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
29828 +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
29829 +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
29830 +
29831 +/**
29832 + * This function returns the Core Interrupt register.
29833 + */
29834 +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
29835 +{
29836 + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
29837 + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
29838 +}
29839 +
29840 +/**
29841 + * This function returns the OTG Interrupt register.
29842 + */
29843 +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
29844 +{
29845 + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
29846 +}
29847 +
29848 +/**
29849 + * This function reads the Device All Endpoints Interrupt register and
29850 + * returns the IN endpoint interrupt bits.
29851 + */
29852 +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
29853 + core_if)
29854 +{
29855 +
29856 + uint32_t v;
29857 +
29858 + if (core_if->multiproc_int_enable) {
29859 + v = DWC_READ_REG32(&core_if->dev_if->
29860 + dev_global_regs->deachint) &
29861 + DWC_READ_REG32(&core_if->
29862 + dev_if->dev_global_regs->deachintmsk);
29863 + } else {
29864 + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
29865 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
29866 + }
29867 + return (v & 0xffff);
29868 +}
29869 +
29870 +/**
29871 + * This function reads the Device All Endpoints Interrupt register and
29872 + * returns the OUT endpoint interrupt bits.
29873 + */
29874 +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
29875 + core_if)
29876 +{
29877 + uint32_t v;
29878 +
29879 + if (core_if->multiproc_int_enable) {
29880 + v = DWC_READ_REG32(&core_if->dev_if->
29881 + dev_global_regs->deachint) &
29882 + DWC_READ_REG32(&core_if->
29883 + dev_if->dev_global_regs->deachintmsk);
29884 + } else {
29885 + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
29886 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
29887 + }
29888 +
29889 + return ((v & 0xffff0000) >> 16);
29890 +}
29891 +
29892 +/**
29893 + * This function returns the Device IN EP Interrupt register
29894 + */
29895 +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
29896 + dwc_ep_t * ep)
29897 +{
29898 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
29899 + uint32_t v, msk, emp;
29900 +
29901 + if (core_if->multiproc_int_enable) {
29902 + msk =
29903 + DWC_READ_REG32(&dev_if->
29904 + dev_global_regs->diepeachintmsk[ep->num]);
29905 + emp =
29906 + DWC_READ_REG32(&dev_if->
29907 + dev_global_regs->dtknqr4_fifoemptymsk);
29908 + msk |= ((emp >> ep->num) & 0x1) << 7;
29909 + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
29910 + } else {
29911 + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
29912 + emp =
29913 + DWC_READ_REG32(&dev_if->
29914 + dev_global_regs->dtknqr4_fifoemptymsk);
29915 + msk |= ((emp >> ep->num) & 0x1) << 7;
29916 + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
29917 + }
29918 +
29919 + return v;
29920 +}
29921 +
29922 +/**
29923 + * This function returns the Device OUT EP Interrupt register
29924 + */
29925 +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
29926 + _core_if, dwc_ep_t * _ep)
29927 +{
29928 + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
29929 + uint32_t v;
29930 + doepmsk_data_t msk = {.d32 = 0 };
29931 +
29932 + if (_core_if->multiproc_int_enable) {
29933 + msk.d32 =
29934 + DWC_READ_REG32(&dev_if->
29935 + dev_global_regs->doepeachintmsk[_ep->num]);
29936 + if (_core_if->pti_enh_enable) {
29937 + msk.b.pktdrpsts = 1;
29938 + }
29939 + v = DWC_READ_REG32(&dev_if->
29940 + out_ep_regs[_ep->num]->doepint) & msk.d32;
29941 + } else {
29942 + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
29943 + if (_core_if->pti_enh_enable) {
29944 + msk.b.pktdrpsts = 1;
29945 + }
29946 + v = DWC_READ_REG32(&dev_if->
29947 + out_ep_regs[_ep->num]->doepint) & msk.d32;
29948 + }
29949 + return v;
29950 +}
29951 +
29952 +/**
29953 + * This function returns the Host All Channel Interrupt register
29954 + */
29955 +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
29956 + _core_if)
29957 +{
29958 + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
29959 +}
29960 +
29961 +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
29962 + _core_if, dwc_hc_t * _hc)
29963 +{
29964 + return (DWC_READ_REG32
29965 + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
29966 +}
29967 +
29968 +/**
29969 + * This function returns the mode of the operation, host or device.
29970 + *
29971 + * @return 0 - Device Mode, 1 - Host Mode
29972 + */
29973 +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
29974 +{
29975 + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
29976 +}
29977 +
29978 +/**@}*/
29979 +
29980 +/**
29981 + * DWC_otg CIL callback structure. This structure allows the HCD and
29982 + * PCD to register functions used for starting and stopping the PCD
29983 + * and HCD for role change on for a DRD.
29984 + */
29985 +typedef struct dwc_otg_cil_callbacks {
29986 + /** Start function for role change */
29987 + int (*start) (void *_p);
29988 + /** Stop Function for role change */
29989 + int (*stop) (void *_p);
29990 + /** Disconnect Function for role change */
29991 + int (*disconnect) (void *_p);
29992 + /** Resume/Remote wakeup Function */
29993 + int (*resume_wakeup) (void *_p);
29994 + /** Suspend function */
29995 + int (*suspend) (void *_p);
29996 + /** Session Start (SRP) */
29997 + int (*session_start) (void *_p);
29998 +#ifdef CONFIG_USB_DWC_OTG_LPM
29999 + /** Sleep (switch to L0 state) */
30000 + int (*sleep) (void *_p);
30001 +#endif
30002 + /** Pointer passed to start() and stop() */
30003 + void *p;
30004 +} dwc_otg_cil_callbacks_t;
30005 +
30006 +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
30007 + dwc_otg_cil_callbacks_t * _cb,
30008 + void *_p);
30009 +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
30010 + dwc_otg_cil_callbacks_t * _cb,
30011 + void *_p);
30012 +
30013 +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
30014 +
30015 +//////////////////////////////////////////////////////////////////////
30016 +/** Start the HCD. Helper function for using the HCD callbacks.
30017 + *
30018 + * @param core_if Programming view of DWC_otg controller.
30019 + */
30020 +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
30021 +{
30022 + if (core_if->hcd_cb && core_if->hcd_cb->start) {
30023 + core_if->hcd_cb->start(core_if->hcd_cb->p);
30024 + }
30025 +}
30026 +
30027 +/** Stop the HCD. Helper function for using the HCD callbacks.
30028 + *
30029 + * @param core_if Programming view of DWC_otg controller.
30030 + */
30031 +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
30032 +{
30033 + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
30034 + core_if->hcd_cb->stop(core_if->hcd_cb->p);
30035 + }
30036 +}
30037 +
30038 +/** Disconnect the HCD. Helper function for using the HCD callbacks.
30039 + *
30040 + * @param core_if Programming view of DWC_otg controller.
30041 + */
30042 +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
30043 +{
30044 + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
30045 + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
30046 + }
30047 +}
30048 +
30049 +/** Inform the HCD the a New Session has begun. Helper function for
30050 + * using the HCD callbacks.
30051 + *
30052 + * @param core_if Programming view of DWC_otg controller.
30053 + */
30054 +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
30055 +{
30056 + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
30057 + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
30058 + }
30059 +}
30060 +
30061 +#ifdef CONFIG_USB_DWC_OTG_LPM
30062 +/**
30063 + * Inform the HCD about LPM sleep.
30064 + * Helper function for using the HCD callbacks.
30065 + *
30066 + * @param core_if Programming view of DWC_otg controller.
30067 + */
30068 +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
30069 +{
30070 + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
30071 + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
30072 + }
30073 +}
30074 +#endif
30075 +
30076 +/** Resume the HCD. Helper function for using the HCD callbacks.
30077 + *
30078 + * @param core_if Programming view of DWC_otg controller.
30079 + */
30080 +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
30081 +{
30082 + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
30083 + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
30084 + }
30085 +}
30086 +
30087 +/** Start the PCD. Helper function for using the PCD callbacks.
30088 + *
30089 + * @param core_if Programming view of DWC_otg controller.
30090 + */
30091 +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
30092 +{
30093 + if (core_if->pcd_cb && core_if->pcd_cb->start) {
30094 + core_if->pcd_cb->start(core_if->pcd_cb->p);
30095 + }
30096 +}
30097 +
30098 +/** Stop the PCD. Helper function for using the PCD callbacks.
30099 + *
30100 + * @param core_if Programming view of DWC_otg controller.
30101 + */
30102 +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
30103 +{
30104 + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
30105 + core_if->pcd_cb->stop(core_if->pcd_cb->p);
30106 + }
30107 +}
30108 +
30109 +/** Suspend the PCD. Helper function for using the PCD callbacks.
30110 + *
30111 + * @param core_if Programming view of DWC_otg controller.
30112 + */
30113 +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
30114 +{
30115 + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
30116 + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
30117 + }
30118 +}
30119 +
30120 +/** Resume the PCD. Helper function for using the PCD callbacks.
30121 + *
30122 + * @param core_if Programming view of DWC_otg controller.
30123 + */
30124 +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
30125 +{
30126 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
30127 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
30128 + }
30129 +}
30130 +
30131 +//////////////////////////////////////////////////////////////////////
30132 +
30133 +#endif
30134 --- /dev/null
30135 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
30136 @@ -0,0 +1,1594 @@
30137 +/* ==========================================================================
30138 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
30139 + * $Revision: #32 $
30140 + * $Date: 2012/08/10 $
30141 + * $Change: 2047372 $
30142 + *
30143 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
30144 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
30145 + * otherwise expressly agreed to in writing between Synopsys and you.
30146 + *
30147 + * The Software IS NOT an item of Licensed Software or Licensed Product under
30148 + * any End User Software License Agreement or Agreement for Licensed Product
30149 + * with Synopsys or any supplement thereto. You are permitted to use and
30150 + * redistribute this Software in source and binary forms, with or without
30151 + * modification, provided that redistributions of source code must retain this
30152 + * notice. You may not view, use, disclose, copy or distribute this file or
30153 + * any information contained herein except pursuant to this license grant from
30154 + * Synopsys. If you do not agree with this notice, including the disclaimer
30155 + * below, then you are not authorized to use the Software.
30156 + *
30157 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
30158 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30159 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30160 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
30161 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30162 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30163 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30164 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30165 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30166 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30167 + * DAMAGE.
30168 + * ========================================================================== */
30169 +
30170 +/** @file
30171 + *
30172 + * The Core Interface Layer provides basic services for accessing and
30173 + * managing the DWC_otg hardware. These services are used by both the
30174 + * Host Controller Driver and the Peripheral Controller Driver.
30175 + *
30176 + * This file contains the Common Interrupt handlers.
30177 + */
30178 +#include "dwc_os.h"
30179 +#include "dwc_otg_regs.h"
30180 +#include "dwc_otg_cil.h"
30181 +#include "dwc_otg_driver.h"
30182 +#include "dwc_otg_pcd.h"
30183 +#include "dwc_otg_hcd.h"
30184 +
30185 +#ifdef DEBUG
30186 +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
30187 +{
30188 + return (core_if->op_state == A_HOST ? "a_host" :
30189 + (core_if->op_state == A_SUSPEND ? "a_suspend" :
30190 + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
30191 + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
30192 + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
30193 +}
30194 +#endif
30195 +
30196 +/** This function will log a debug message
30197 + *
30198 + * @param core_if Programming view of DWC_otg controller.
30199 + */
30200 +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
30201 +{
30202 + gintsts_data_t gintsts;
30203 + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
30204 + dwc_otg_mode(core_if) ? "Host" : "Device");
30205 +
30206 + /* Clear interrupt */
30207 + gintsts.d32 = 0;
30208 + gintsts.b.modemismatch = 1;
30209 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30210 + return 1;
30211 +}
30212 +
30213 +/**
30214 + * This function handles the OTG Interrupts. It reads the OTG
30215 + * Interrupt Register (GOTGINT) to determine what interrupt has
30216 + * occurred.
30217 + *
30218 + * @param core_if Programming view of DWC_otg controller.
30219 + */
30220 +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
30221 +{
30222 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
30223 + gotgint_data_t gotgint;
30224 + gotgctl_data_t gotgctl;
30225 + gintmsk_data_t gintmsk;
30226 + gpwrdn_data_t gpwrdn;
30227 +
30228 + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
30229 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30230 + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
30231 + op_state_str(core_if));
30232 +
30233 + if (gotgint.b.sesenddet) {
30234 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30235 + "Session End Detected++ (%s)\n",
30236 + op_state_str(core_if));
30237 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30238 +
30239 + if (core_if->op_state == B_HOST) {
30240 + cil_pcd_start(core_if);
30241 + core_if->op_state = B_PERIPHERAL;
30242 + } else {
30243 + /* If not B_HOST and Device HNP still set. HNP
30244 + * Did not succeed!*/
30245 + if (gotgctl.b.devhnpen) {
30246 + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
30247 + __DWC_ERROR("Device Not Connected/Responding!\n");
30248 + }
30249 +
30250 + /* If Session End Detected the B-Cable has
30251 + * been disconnected. */
30252 + /* Reset PCD and Gadget driver to a
30253 + * clean state. */
30254 + core_if->lx_state = DWC_OTG_L0;
30255 + DWC_SPINUNLOCK(core_if->lock);
30256 + cil_pcd_stop(core_if);
30257 + DWC_SPINLOCK(core_if->lock);
30258 +
30259 + if (core_if->adp_enable) {
30260 + if (core_if->power_down == 2) {
30261 + gpwrdn.d32 = 0;
30262 + gpwrdn.b.pwrdnswtch = 1;
30263 + DWC_MODIFY_REG32(&core_if->
30264 + core_global_regs->
30265 + gpwrdn, gpwrdn.d32, 0);
30266 + }
30267 +
30268 + gpwrdn.d32 = 0;
30269 + gpwrdn.b.pmuintsel = 1;
30270 + gpwrdn.b.pmuactv = 1;
30271 + DWC_MODIFY_REG32(&core_if->core_global_regs->
30272 + gpwrdn, 0, gpwrdn.d32);
30273 +
30274 + dwc_otg_adp_sense_start(core_if);
30275 + }
30276 + }
30277 +
30278 + gotgctl.d32 = 0;
30279 + gotgctl.b.devhnpen = 1;
30280 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
30281 + }
30282 + if (gotgint.b.sesreqsucstschng) {
30283 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30284 + "Session Reqeust Success Status Change++\n");
30285 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30286 + if (gotgctl.b.sesreqscs) {
30287 +
30288 + if ((core_if->core_params->phy_type ==
30289 + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
30290 + core_if->srp_success = 1;
30291 + } else {
30292 + DWC_SPINUNLOCK(core_if->lock);
30293 + cil_pcd_resume(core_if);
30294 + DWC_SPINLOCK(core_if->lock);
30295 + /* Clear Session Request */
30296 + gotgctl.d32 = 0;
30297 + gotgctl.b.sesreq = 1;
30298 + DWC_MODIFY_REG32(&global_regs->gotgctl,
30299 + gotgctl.d32, 0);
30300 + }
30301 + }
30302 + }
30303 + if (gotgint.b.hstnegsucstschng) {
30304 + /* Print statements during the HNP interrupt handling
30305 + * can cause it to fail.*/
30306 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30307 + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
30308 + * this does not help*/
30309 + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
30310 + dwc_udelay(100);
30311 + if (gotgctl.b.hstnegscs) {
30312 + if (dwc_otg_is_host_mode(core_if)) {
30313 + core_if->op_state = B_HOST;
30314 + /*
30315 + * Need to disable SOF interrupt immediately.
30316 + * When switching from device to host, the PCD
30317 + * interrupt handler won't handle the
30318 + * interrupt if host mode is already set. The
30319 + * HCD interrupt handler won't get called if
30320 + * the HCD state is HALT. This means that the
30321 + * interrupt does not get handled and Linux
30322 + * complains loudly.
30323 + */
30324 + gintmsk.d32 = 0;
30325 + gintmsk.b.sofintr = 1;
30326 + DWC_MODIFY_REG32(&global_regs->gintmsk,
30327 + gintmsk.d32, 0);
30328 + /* Call callback function with spin lock released */
30329 + DWC_SPINUNLOCK(core_if->lock);
30330 + cil_pcd_stop(core_if);
30331 + /*
30332 + * Initialize the Core for Host mode.
30333 + */
30334 + cil_hcd_start(core_if);
30335 + DWC_SPINLOCK(core_if->lock);
30336 + core_if->op_state = B_HOST;
30337 + }
30338 + } else {
30339 + gotgctl.d32 = 0;
30340 + gotgctl.b.hnpreq = 1;
30341 + gotgctl.b.devhnpen = 1;
30342 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
30343 + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
30344 + __DWC_ERROR("Device Not Connected/Responding\n");
30345 + }
30346 + }
30347 + if (gotgint.b.hstnegdet) {
30348 + /* The disconnect interrupt is set at the same time as
30349 + * Host Negotiation Detected. During the mode
30350 + * switch all interrupts are cleared so the disconnect
30351 + * interrupt handler will not get executed.
30352 + */
30353 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30354 + "Host Negotiation Detected++ (%s)\n",
30355 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30356 + "Device"));
30357 + if (dwc_otg_is_device_mode(core_if)) {
30358 + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
30359 + core_if->op_state);
30360 + DWC_SPINUNLOCK(core_if->lock);
30361 + cil_hcd_disconnect(core_if);
30362 + cil_pcd_start(core_if);
30363 + DWC_SPINLOCK(core_if->lock);
30364 + core_if->op_state = A_PERIPHERAL;
30365 + } else {
30366 + /*
30367 + * Need to disable SOF interrupt immediately. When
30368 + * switching from device to host, the PCD interrupt
30369 + * handler won't handle the interrupt if host mode is
30370 + * already set. The HCD interrupt handler won't get
30371 + * called if the HCD state is HALT. This means that
30372 + * the interrupt does not get handled and Linux
30373 + * complains loudly.
30374 + */
30375 + gintmsk.d32 = 0;
30376 + gintmsk.b.sofintr = 1;
30377 + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
30378 + DWC_SPINUNLOCK(core_if->lock);
30379 + cil_pcd_stop(core_if);
30380 + cil_hcd_start(core_if);
30381 + DWC_SPINLOCK(core_if->lock);
30382 + core_if->op_state = A_HOST;
30383 + }
30384 + }
30385 + if (gotgint.b.adevtoutchng) {
30386 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30387 + "A-Device Timeout Change++\n");
30388 + }
30389 + if (gotgint.b.debdone) {
30390 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
30391 + }
30392 +
30393 + /* Clear GOTGINT */
30394 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
30395 +
30396 + return 1;
30397 +}
30398 +
30399 +void w_conn_id_status_change(void *p)
30400 +{
30401 + dwc_otg_core_if_t *core_if = p;
30402 + uint32_t count = 0;
30403 + gotgctl_data_t gotgctl = {.d32 = 0 };
30404 +
30405 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
30406 + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
30407 + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
30408 +
30409 + /* B-Device connector (Device Mode) */
30410 + if (gotgctl.b.conidsts) {
30411 + /* Wait for switch to device mode. */
30412 + while (!dwc_otg_is_device_mode(core_if)) {
30413 + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
30414 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30415 + "Peripheral"));
30416 + dwc_mdelay(100);
30417 + if (++count > 10000)
30418 + break;
30419 + }
30420 + DWC_ASSERT(++count < 10000,
30421 + "Connection id status change timed out");
30422 + core_if->op_state = B_PERIPHERAL;
30423 + dwc_otg_core_init(core_if);
30424 + dwc_otg_enable_global_interrupts(core_if);
30425 + cil_pcd_start(core_if);
30426 + } else {
30427 + /* A-Device connector (Host Mode) */
30428 + while (!dwc_otg_is_host_mode(core_if)) {
30429 + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
30430 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30431 + "Peripheral"));
30432 + dwc_mdelay(100);
30433 + if (++count > 10000)
30434 + break;
30435 + }
30436 + DWC_ASSERT(++count < 10000,
30437 + "Connection id status change timed out");
30438 + core_if->op_state = A_HOST;
30439 + /*
30440 + * Initialize the Core for Host mode.
30441 + */
30442 + dwc_otg_core_init(core_if);
30443 + dwc_otg_enable_global_interrupts(core_if);
30444 + cil_hcd_start(core_if);
30445 + }
30446 +}
30447 +
30448 +/**
30449 + * This function handles the Connector ID Status Change Interrupt. It
30450 + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
30451 + * is a Device to Host Mode transition or a Host Mode to Device
30452 + * Transition.
30453 + *
30454 + * This only occurs when the cable is connected/removed from the PHY
30455 + * connector.
30456 + *
30457 + * @param core_if Programming view of DWC_otg controller.
30458 + */
30459 +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
30460 +{
30461 +
30462 + /*
30463 + * Need to disable SOF interrupt immediately. If switching from device
30464 + * to host, the PCD interrupt handler won't handle the interrupt if
30465 + * host mode is already set. The HCD interrupt handler won't get
30466 + * called if the HCD state is HALT. This means that the interrupt does
30467 + * not get handled and Linux complains loudly.
30468 + */
30469 + gintmsk_data_t gintmsk = {.d32 = 0 };
30470 + gintsts_data_t gintsts = {.d32 = 0 };
30471 +
30472 + gintmsk.b.sofintr = 1;
30473 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
30474 +
30475 + DWC_DEBUGPL(DBG_CIL,
30476 + " ++Connector ID Status Change Interrupt++ (%s)\n",
30477 + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
30478 +
30479 + DWC_SPINUNLOCK(core_if->lock);
30480 +
30481 + /*
30482 + * Need to schedule a work, as there are possible DELAY function calls
30483 + * Release lock before scheduling workq as it holds spinlock during scheduling
30484 + */
30485 +
30486 + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
30487 + core_if, "connection id status change");
30488 + DWC_SPINLOCK(core_if->lock);
30489 +
30490 + /* Set flag and clear interrupt */
30491 + gintsts.b.conidstschng = 1;
30492 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30493 +
30494 + return 1;
30495 +}
30496 +
30497 +/**
30498 + * This interrupt indicates that a device is initiating the Session
30499 + * Request Protocol to request the host to turn on bus power so a new
30500 + * session can begin. The handler responds by turning on bus power. If
30501 + * the DWC_otg controller is in low power mode, the handler brings the
30502 + * controller out of low power mode before turning on bus power.
30503 + *
30504 + * @param core_if Programming view of DWC_otg controller.
30505 + */
30506 +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
30507 +{
30508 + gintsts_data_t gintsts;
30509 +
30510 +#ifndef DWC_HOST_ONLY
30511 + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
30512 +
30513 + if (dwc_otg_is_device_mode(core_if)) {
30514 + DWC_PRINTF("SRP: Device mode\n");
30515 + } else {
30516 + hprt0_data_t hprt0;
30517 + DWC_PRINTF("SRP: Host mode\n");
30518 +
30519 + /* Turn on the port power bit. */
30520 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
30521 + hprt0.b.prtpwr = 1;
30522 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
30523 +
30524 + /* Start the Connection timer. So a message can be displayed
30525 + * if connect does not occur within 10 seconds. */
30526 + cil_hcd_session_start(core_if);
30527 + }
30528 +#endif
30529 +
30530 + /* Clear interrupt */
30531 + gintsts.d32 = 0;
30532 + gintsts.b.sessreqintr = 1;
30533 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30534 +
30535 + return 1;
30536 +}
30537 +
30538 +void w_wakeup_detected(void *p)
30539 +{
30540 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
30541 + /*
30542 + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
30543 + * so that OPT tests pass with all PHYs).
30544 + */
30545 + hprt0_data_t hprt0 = {.d32 = 0 };
30546 +#if 0
30547 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
30548 + /* Restart the Phy Clock */
30549 + pcgcctl.b.stoppclk = 1;
30550 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
30551 + dwc_udelay(10);
30552 +#endif //0
30553 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
30554 + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
30555 +// dwc_mdelay(70);
30556 + hprt0.b.prtres = 0; /* Resume */
30557 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
30558 + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
30559 + DWC_READ_REG32(core_if->host_if->hprt0));
30560 +
30561 + cil_hcd_resume(core_if);
30562 +
30563 + /** Change to L0 state*/
30564 + core_if->lx_state = DWC_OTG_L0;
30565 +}
30566 +
30567 +/**
30568 + * This interrupt indicates that the DWC_otg controller has detected a
30569 + * resume or remote wakeup sequence. If the DWC_otg controller is in
30570 + * low power mode, the handler must brings the controller out of low
30571 + * power mode. The controller automatically begins resume
30572 + * signaling. The handler schedules a time to stop resume signaling.
30573 + */
30574 +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
30575 +{
30576 + gintsts_data_t gintsts;
30577 +
30578 + DWC_DEBUGPL(DBG_ANY,
30579 + "++Resume and Remote Wakeup Detected Interrupt++\n");
30580 +
30581 + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
30582 +
30583 + if (dwc_otg_is_device_mode(core_if)) {
30584 + dctl_data_t dctl = {.d32 = 0 };
30585 + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
30586 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
30587 + dsts));
30588 + if (core_if->lx_state == DWC_OTG_L2) {
30589 +#ifdef PARTIAL_POWER_DOWN
30590 + if (core_if->hwcfg4.b.power_optimiz) {
30591 + pcgcctl_data_t power = {.d32 = 0 };
30592 +
30593 + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
30594 + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
30595 + power.d32);
30596 +
30597 + power.b.stoppclk = 0;
30598 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30599 +
30600 + power.b.pwrclmp = 0;
30601 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30602 +
30603 + power.b.rstpdwnmodule = 0;
30604 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30605 + }
30606 +#endif
30607 + /* Clear the Remote Wakeup Signaling */
30608 + dctl.b.rmtwkupsig = 1;
30609 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
30610 + dctl, dctl.d32, 0);
30611 +
30612 + DWC_SPINUNLOCK(core_if->lock);
30613 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
30614 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
30615 + }
30616 + DWC_SPINLOCK(core_if->lock);
30617 + } else {
30618 + glpmcfg_data_t lpmcfg;
30619 + lpmcfg.d32 =
30620 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
30621 + lpmcfg.b.hird_thres &= (~(1 << 4));
30622 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
30623 + lpmcfg.d32);
30624 + }
30625 + /** Change to L0 state*/
30626 + core_if->lx_state = DWC_OTG_L0;
30627 + } else {
30628 + if (core_if->lx_state != DWC_OTG_L1) {
30629 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
30630 +
30631 + /* Restart the Phy Clock */
30632 + pcgcctl.b.stoppclk = 1;
30633 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
30634 + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
30635 + } else {
30636 + /** Change to L0 state*/
30637 + core_if->lx_state = DWC_OTG_L0;
30638 + }
30639 + }
30640 +
30641 + /* Clear interrupt */
30642 + gintsts.d32 = 0;
30643 + gintsts.b.wkupintr = 1;
30644 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30645 +
30646 + return 1;
30647 +}
30648 +
30649 +/**
30650 + * This interrupt indicates that the Wakeup Logic has detected a
30651 + * Device disconnect.
30652 + */
30653 +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
30654 +{
30655 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
30656 + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
30657 + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30658 +
30659 + DWC_PRINTF("%s called\n", __FUNCTION__);
30660 +
30661 + if (!core_if->hibernation_suspend) {
30662 + DWC_PRINTF("Already exited from Hibernation\n");
30663 + return 1;
30664 + }
30665 +
30666 + /* Switch on the voltage to the core */
30667 + gpwrdn.b.pwrdnswtch = 1;
30668 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30669 + dwc_udelay(10);
30670 +
30671 + /* Reset the core */
30672 + gpwrdn.d32 = 0;
30673 + gpwrdn.b.pwrdnrstn = 1;
30674 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30675 + dwc_udelay(10);
30676 +
30677 + /* Disable power clamps*/
30678 + gpwrdn.d32 = 0;
30679 + gpwrdn.b.pwrdnclmp = 1;
30680 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30681 +
30682 + /* Remove reset the core signal */
30683 + gpwrdn.d32 = 0;
30684 + gpwrdn.b.pwrdnrstn = 1;
30685 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30686 + dwc_udelay(10);
30687 +
30688 + /* Disable PMU interrupt */
30689 + gpwrdn.d32 = 0;
30690 + gpwrdn.b.pmuintsel = 1;
30691 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30692 +
30693 + core_if->hibernation_suspend = 0;
30694 +
30695 + /* Disable PMU */
30696 + gpwrdn.d32 = 0;
30697 + gpwrdn.b.pmuactv = 1;
30698 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30699 + dwc_udelay(10);
30700 +
30701 + if (gpwrdn_temp.b.idsts) {
30702 + core_if->op_state = B_PERIPHERAL;
30703 + dwc_otg_core_init(core_if);
30704 + dwc_otg_enable_global_interrupts(core_if);
30705 + cil_pcd_start(core_if);
30706 + } else {
30707 + core_if->op_state = A_HOST;
30708 + dwc_otg_core_init(core_if);
30709 + dwc_otg_enable_global_interrupts(core_if);
30710 + cil_hcd_start(core_if);
30711 + }
30712 +
30713 + return 1;
30714 +}
30715 +
30716 +/**
30717 + * This interrupt indicates that the Wakeup Logic has detected a
30718 + * remote wakeup sequence.
30719 + */
30720 +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
30721 +{
30722 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30723 + DWC_DEBUGPL(DBG_ANY,
30724 + "++Powerdown Remote Wakeup Detected Interrupt++\n");
30725 +
30726 + if (!core_if->hibernation_suspend) {
30727 + DWC_PRINTF("Already exited from Hibernation\n");
30728 + return 1;
30729 + }
30730 +
30731 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30732 + if (gpwrdn.b.idsts) { // Device Mode
30733 + if ((core_if->power_down == 2)
30734 + && (core_if->hibernation_suspend == 1)) {
30735 + dwc_otg_device_hibernation_restore(core_if, 0, 0);
30736 + }
30737 + } else {
30738 + if ((core_if->power_down == 2)
30739 + && (core_if->hibernation_suspend == 1)) {
30740 + dwc_otg_host_hibernation_restore(core_if, 1, 0);
30741 + }
30742 + }
30743 + return 1;
30744 +}
30745 +
30746 +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
30747 +{
30748 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30749 + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
30750 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
30751 +
30752 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
30753 + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30754 + if (core_if->power_down == 2) {
30755 + if (!core_if->hibernation_suspend) {
30756 + DWC_PRINTF("Already exited from Hibernation\n");
30757 + return 1;
30758 + }
30759 + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
30760 + /* Switch on the voltage to the core */
30761 + gpwrdn.b.pwrdnswtch = 1;
30762 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30763 + dwc_udelay(10);
30764 +
30765 + /* Reset the core */
30766 + gpwrdn.d32 = 0;
30767 + gpwrdn.b.pwrdnrstn = 1;
30768 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30769 + dwc_udelay(10);
30770 +
30771 + /* Disable power clamps */
30772 + gpwrdn.d32 = 0;
30773 + gpwrdn.b.pwrdnclmp = 1;
30774 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30775 +
30776 + /* Remove reset the core signal */
30777 + gpwrdn.d32 = 0;
30778 + gpwrdn.b.pwrdnrstn = 1;
30779 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30780 + dwc_udelay(10);
30781 +
30782 + /* Disable PMU interrupt */
30783 + gpwrdn.d32 = 0;
30784 + gpwrdn.b.pmuintsel = 1;
30785 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30786 +
30787 + /*Indicates that we are exiting from hibernation */
30788 + core_if->hibernation_suspend = 0;
30789 +
30790 + /* Disable PMU */
30791 + gpwrdn.d32 = 0;
30792 + gpwrdn.b.pmuactv = 1;
30793 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30794 + dwc_udelay(10);
30795 +
30796 + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
30797 + if (gpwrdn.b.dis_vbus == 1) {
30798 + gpwrdn.d32 = 0;
30799 + gpwrdn.b.dis_vbus = 1;
30800 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30801 + }
30802 +
30803 + if (gpwrdn_temp.b.idsts) {
30804 + core_if->op_state = B_PERIPHERAL;
30805 + dwc_otg_core_init(core_if);
30806 + dwc_otg_enable_global_interrupts(core_if);
30807 + cil_pcd_start(core_if);
30808 + } else {
30809 + core_if->op_state = A_HOST;
30810 + dwc_otg_core_init(core_if);
30811 + dwc_otg_enable_global_interrupts(core_if);
30812 + cil_hcd_start(core_if);
30813 + }
30814 + }
30815 +
30816 + if (core_if->adp_enable) {
30817 + uint8_t is_host = 0;
30818 + DWC_SPINUNLOCK(core_if->lock);
30819 + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
30820 +#ifndef DWC_HOST_ONLY
30821 + if (gpwrdn_temp.b.idsts)
30822 + core_if->lock = otg_dev->pcd->lock;
30823 +#endif
30824 +#ifndef DWC_DEVICE_ONLY
30825 + if (!gpwrdn_temp.b.idsts) {
30826 + core_if->lock = otg_dev->hcd->lock;
30827 + is_host = 1;
30828 + }
30829 +#endif
30830 + DWC_PRINTF("RESTART ADP\n");
30831 + if (core_if->adp.probe_enabled)
30832 + dwc_otg_adp_probe_stop(core_if);
30833 + if (core_if->adp.sense_enabled)
30834 + dwc_otg_adp_sense_stop(core_if);
30835 + if (core_if->adp.sense_timer_started)
30836 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
30837 + if (core_if->adp.vbuson_timer_started)
30838 + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
30839 + core_if->adp.probe_timer_values[0] = -1;
30840 + core_if->adp.probe_timer_values[1] = -1;
30841 + core_if->adp.sense_timer_started = 0;
30842 + core_if->adp.vbuson_timer_started = 0;
30843 + core_if->adp.probe_counter = 0;
30844 + core_if->adp.gpwrdn = 0;
30845 +
30846 + /* Disable PMU and restart ADP */
30847 + gpwrdn_temp.d32 = 0;
30848 + gpwrdn_temp.b.pmuactv = 1;
30849 + gpwrdn_temp.b.pmuintsel = 1;
30850 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30851 + DWC_PRINTF("Check point 1\n");
30852 + dwc_mdelay(110);
30853 + dwc_otg_adp_start(core_if, is_host);
30854 + DWC_SPINLOCK(core_if->lock);
30855 + }
30856 +
30857 +
30858 + return 1;
30859 +}
30860 +
30861 +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
30862 +{
30863 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30864 + int32_t otg_cap_param = core_if->core_params->otg_cap;
30865 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
30866 +
30867 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30868 + if (core_if->power_down == 2) {
30869 + if (!core_if->hibernation_suspend) {
30870 + DWC_PRINTF("Already exited from Hibernation\n");
30871 + return 1;
30872 + }
30873 +
30874 + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
30875 + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
30876 + gpwrdn.b.bsessvld == 0) {
30877 + /* Save gpwrdn register for further usage if stschng interrupt */
30878 + core_if->gr_backup->gpwrdn_local =
30879 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30880 + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
30881 + return 1;
30882 + }
30883 +
30884 + /* Switch on the voltage to the core */
30885 + gpwrdn.d32 = 0;
30886 + gpwrdn.b.pwrdnswtch = 1;
30887 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30888 + dwc_udelay(10);
30889 +
30890 + /* Reset the core */
30891 + gpwrdn.d32 = 0;
30892 + gpwrdn.b.pwrdnrstn = 1;
30893 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30894 + dwc_udelay(10);
30895 +
30896 + /* Disable power clamps */
30897 + gpwrdn.d32 = 0;
30898 + gpwrdn.b.pwrdnclmp = 1;
30899 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30900 +
30901 + /* Remove reset the core signal */
30902 + gpwrdn.d32 = 0;
30903 + gpwrdn.b.pwrdnrstn = 1;
30904 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30905 + dwc_udelay(10);
30906 +
30907 + /* Disable PMU interrupt */
30908 + gpwrdn.d32 = 0;
30909 + gpwrdn.b.pmuintsel = 1;
30910 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30911 + dwc_udelay(10);
30912 +
30913 + /*Indicates that we are exiting from hibernation */
30914 + core_if->hibernation_suspend = 0;
30915 +
30916 + /* Disable PMU */
30917 + gpwrdn.d32 = 0;
30918 + gpwrdn.b.pmuactv = 1;
30919 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30920 + dwc_udelay(10);
30921 +
30922 + core_if->op_state = B_PERIPHERAL;
30923 + dwc_otg_core_init(core_if);
30924 + dwc_otg_enable_global_interrupts(core_if);
30925 + cil_pcd_start(core_if);
30926 +
30927 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
30928 + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
30929 + /*
30930 + * Initiate SRP after initial ADP probe.
30931 + */
30932 + dwc_otg_initiate_srp(core_if);
30933 + }
30934 + }
30935 +
30936 + return 1;
30937 +}
30938 +/**
30939 + * This interrupt indicates that the Wakeup Logic has detected a
30940 + * status change either on IDDIG or BSessVld.
30941 + */
30942 +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
30943 +{
30944 + int retval;
30945 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30946 + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
30947 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
30948 +
30949 + DWC_PRINTF("%s called\n", __FUNCTION__);
30950 +
30951 + if (core_if->power_down == 2) {
30952 + if (core_if->hibernation_suspend <= 0) {
30953 + DWC_PRINTF("Already exited from Hibernation\n");
30954 + return 1;
30955 + } else
30956 + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
30957 +
30958 + } else {
30959 + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
30960 + }
30961 +
30962 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30963 +
30964 + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
30965 + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
30966 + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
30967 + retval = dwc_otg_handle_pwrdn_session_change(core_if);
30968 + }
30969 +
30970 + return retval;
30971 +}
30972 +
30973 +/**
30974 + * This interrupt indicates that the Wakeup Logic has detected a
30975 + * SRP.
30976 + */
30977 +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
30978 +{
30979 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30980 +
30981 + DWC_PRINTF("%s called\n", __FUNCTION__);
30982 +
30983 + if (!core_if->hibernation_suspend) {
30984 + DWC_PRINTF("Already exited from Hibernation\n");
30985 + return 1;
30986 + }
30987 +#ifdef DWC_DEV_SRPCAP
30988 + if (core_if->pwron_timer_started) {
30989 + core_if->pwron_timer_started = 0;
30990 + DWC_TIMER_CANCEL(core_if->pwron_timer);
30991 + }
30992 +#endif
30993 +
30994 + /* Switch on the voltage to the core */
30995 + gpwrdn.b.pwrdnswtch = 1;
30996 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30997 + dwc_udelay(10);
30998 +
30999 + /* Reset the core */
31000 + gpwrdn.d32 = 0;
31001 + gpwrdn.b.pwrdnrstn = 1;
31002 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31003 + dwc_udelay(10);
31004 +
31005 + /* Disable power clamps */
31006 + gpwrdn.d32 = 0;
31007 + gpwrdn.b.pwrdnclmp = 1;
31008 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31009 +
31010 + /* Remove reset the core signal */
31011 + gpwrdn.d32 = 0;
31012 + gpwrdn.b.pwrdnrstn = 1;
31013 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
31014 + dwc_udelay(10);
31015 +
31016 + /* Disable PMU interrupt */
31017 + gpwrdn.d32 = 0;
31018 + gpwrdn.b.pmuintsel = 1;
31019 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31020 +
31021 + /* Indicates that we are exiting from hibernation */
31022 + core_if->hibernation_suspend = 0;
31023 +
31024 + /* Disable PMU */
31025 + gpwrdn.d32 = 0;
31026 + gpwrdn.b.pmuactv = 1;
31027 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31028 + dwc_udelay(10);
31029 +
31030 + /* Programm Disable VBUS to 0 */
31031 + gpwrdn.d32 = 0;
31032 + gpwrdn.b.dis_vbus = 1;
31033 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31034 +
31035 + /*Initialize the core as Host */
31036 + core_if->op_state = A_HOST;
31037 + dwc_otg_core_init(core_if);
31038 + dwc_otg_enable_global_interrupts(core_if);
31039 + cil_hcd_start(core_if);
31040 +
31041 + return 1;
31042 +}
31043 +
31044 +/** This interrupt indicates that restore command after Hibernation
31045 + * was completed by the core. */
31046 +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
31047 +{
31048 + pcgcctl_data_t pcgcctl;
31049 + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
31050 +
31051 + //TODO De-assert restore signal. 8.a
31052 + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
31053 + if (pcgcctl.b.restoremode == 1) {
31054 + gintmsk_data_t gintmsk = {.d32 = 0 };
31055 + /*
31056 + * If restore mode is Remote Wakeup,
31057 + * unmask Remote Wakeup interrupt.
31058 + */
31059 + gintmsk.b.wkupintr = 1;
31060 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
31061 + 0, gintmsk.d32);
31062 + }
31063 +
31064 + return 1;
31065 +}
31066 +
31067 +/**
31068 + * This interrupt indicates that a device has been disconnected from
31069 + * the root port.
31070 + */
31071 +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
31072 +{
31073 + gintsts_data_t gintsts;
31074 +
31075 + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
31076 + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
31077 + op_state_str(core_if));
31078 +
31079 +/** @todo Consolidate this if statement. */
31080 +#ifndef DWC_HOST_ONLY
31081 + if (core_if->op_state == B_HOST) {
31082 + /* If in device mode Disconnect and stop the HCD, then
31083 + * start the PCD. */
31084 + DWC_SPINUNLOCK(core_if->lock);
31085 + cil_hcd_disconnect(core_if);
31086 + cil_pcd_start(core_if);
31087 + DWC_SPINLOCK(core_if->lock);
31088 + core_if->op_state = B_PERIPHERAL;
31089 + } else if (dwc_otg_is_device_mode(core_if)) {
31090 + gotgctl_data_t gotgctl = {.d32 = 0 };
31091 + gotgctl.d32 =
31092 + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
31093 + if (gotgctl.b.hstsethnpen == 1) {
31094 + /* Do nothing, if HNP in process the OTG
31095 + * interrupt "Host Negotiation Detected"
31096 + * interrupt will do the mode switch.
31097 + */
31098 + } else if (gotgctl.b.devhnpen == 0) {
31099 + /* If in device mode Disconnect and stop the HCD, then
31100 + * start the PCD. */
31101 + DWC_SPINUNLOCK(core_if->lock);
31102 + cil_hcd_disconnect(core_if);
31103 + cil_pcd_start(core_if);
31104 + DWC_SPINLOCK(core_if->lock);
31105 + core_if->op_state = B_PERIPHERAL;
31106 + } else {
31107 + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
31108 + }
31109 + } else {
31110 + if (core_if->op_state == A_HOST) {
31111 + /* A-Cable still connected but device disconnected. */
31112 + cil_hcd_disconnect(core_if);
31113 + if (core_if->adp_enable) {
31114 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
31115 + cil_hcd_stop(core_if);
31116 + /* Enable Power Down Logic */
31117 + gpwrdn.b.pmuintsel = 1;
31118 + gpwrdn.b.pmuactv = 1;
31119 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31120 + gpwrdn, 0, gpwrdn.d32);
31121 + dwc_otg_adp_probe_start(core_if);
31122 +
31123 + /* Power off the core */
31124 + if (core_if->power_down == 2) {
31125 + gpwrdn.d32 = 0;
31126 + gpwrdn.b.pwrdnswtch = 1;
31127 + DWC_MODIFY_REG32
31128 + (&core_if->core_global_regs->gpwrdn,
31129 + gpwrdn.d32, 0);
31130 + }
31131 + }
31132 + }
31133 + }
31134 +#endif
31135 + /* Change to L3(OFF) state */
31136 + core_if->lx_state = DWC_OTG_L3;
31137 +
31138 + gintsts.d32 = 0;
31139 + gintsts.b.disconnect = 1;
31140 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31141 + return 1;
31142 +}
31143 +
31144 +/**
31145 + * This interrupt indicates that SUSPEND state has been detected on
31146 + * the USB.
31147 + *
31148 + * For HNP the USB Suspend interrupt signals the change from
31149 + * "a_peripheral" to "a_host".
31150 + *
31151 + * When power management is enabled the core will be put in low power
31152 + * mode.
31153 + */
31154 +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
31155 +{
31156 + dsts_data_t dsts;
31157 + gintsts_data_t gintsts;
31158 + dcfg_data_t dcfg;
31159 +
31160 + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
31161 +
31162 + if (dwc_otg_is_device_mode(core_if)) {
31163 + /* Check the Device status register to determine if the Suspend
31164 + * state is active. */
31165 + dsts.d32 =
31166 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
31167 + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
31168 + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
31169 + "HWCFG4.power Optimize=%d\n",
31170 + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
31171 +
31172 +#ifdef PARTIAL_POWER_DOWN
31173 +/** @todo Add a module parameter for power management. */
31174 +
31175 + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
31176 + pcgcctl_data_t power = {.d32 = 0 };
31177 + DWC_DEBUGPL(DBG_CIL, "suspend\n");
31178 +
31179 + power.b.pwrclmp = 1;
31180 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
31181 +
31182 + power.b.rstpdwnmodule = 1;
31183 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
31184 +
31185 + power.b.stoppclk = 1;
31186 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
31187 +
31188 + } else {
31189 + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
31190 + }
31191 +#endif
31192 + /* PCD callback for suspend. Release the lock inside of callback function */
31193 + cil_pcd_suspend(core_if);
31194 + if (core_if->power_down == 2)
31195 + {
31196 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
31197 + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
31198 + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
31199 +
31200 + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
31201 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31202 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31203 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
31204 +
31205 + /* Change to L2(suspend) state */
31206 + core_if->lx_state = DWC_OTG_L2;
31207 +
31208 + /* Clear interrupt in gintsts */
31209 + gintsts.d32 = 0;
31210 + gintsts.b.usbsuspend = 1;
31211 + DWC_WRITE_REG32(&core_if->core_global_regs->
31212 + gintsts, gintsts.d32);
31213 + DWC_PRINTF("Start of hibernation completed\n");
31214 + dwc_otg_save_global_regs(core_if);
31215 + dwc_otg_save_dev_regs(core_if);
31216 +
31217 + gusbcfg.d32 =
31218 + DWC_READ_REG32(&core_if->core_global_regs->
31219 + gusbcfg);
31220 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
31221 + /* ULPI interface */
31222 + /* Suspend the Phy Clock */
31223 + pcgcctl.d32 = 0;
31224 + pcgcctl.b.stoppclk = 1;
31225 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
31226 + pcgcctl.d32);
31227 + dwc_udelay(10);
31228 + gpwrdn.b.pmuactv = 1;
31229 + DWC_MODIFY_REG32(&core_if->
31230 + core_global_regs->
31231 + gpwrdn, 0, gpwrdn.d32);
31232 + } else {
31233 + /* UTMI+ Interface */
31234 + gpwrdn.b.pmuactv = 1;
31235 + DWC_MODIFY_REG32(&core_if->
31236 + core_global_regs->
31237 + gpwrdn, 0, gpwrdn.d32);
31238 + dwc_udelay(10);
31239 + pcgcctl.b.stoppclk = 1;
31240 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
31241 + pcgcctl.d32);
31242 + dwc_udelay(10);
31243 + }
31244 +
31245 + /* Set flag to indicate that we are in hibernation */
31246 + core_if->hibernation_suspend = 1;
31247 + /* Enable interrupts from wake up logic */
31248 + gpwrdn.d32 = 0;
31249 + gpwrdn.b.pmuintsel = 1;
31250 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31251 + gpwrdn, 0, gpwrdn.d32);
31252 + dwc_udelay(10);
31253 +
31254 + /* Unmask device mode interrupts in GPWRDN */
31255 + gpwrdn.d32 = 0;
31256 + gpwrdn.b.rst_det_msk = 1;
31257 + gpwrdn.b.lnstchng_msk = 1;
31258 + gpwrdn.b.sts_chngint_msk = 1;
31259 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31260 + gpwrdn, 0, gpwrdn.d32);
31261 + dwc_udelay(10);
31262 +
31263 + /* Enable Power Down Clamp */
31264 + gpwrdn.d32 = 0;
31265 + gpwrdn.b.pwrdnclmp = 1;
31266 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31267 + gpwrdn, 0, gpwrdn.d32);
31268 + dwc_udelay(10);
31269 +
31270 + /* Switch off VDD */
31271 + gpwrdn.d32 = 0;
31272 + gpwrdn.b.pwrdnswtch = 1;
31273 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31274 + gpwrdn, 0, gpwrdn.d32);
31275 +
31276 + /* Save gpwrdn register for further usage if stschng interrupt */
31277 + core_if->gr_backup->gpwrdn_local =
31278 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31279 + DWC_PRINTF("Hibernation completed\n");
31280 +
31281 + return 1;
31282 + }
31283 + } else if (core_if->power_down == 3) {
31284 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31285 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
31286 + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
31287 + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
31288 +
31289 + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
31290 + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
31291 + core_if->xhib = 1;
31292 +
31293 + /* Clear interrupt in gintsts */
31294 + gintsts.d32 = 0;
31295 + gintsts.b.usbsuspend = 1;
31296 + DWC_WRITE_REG32(&core_if->core_global_regs->
31297 + gintsts, gintsts.d32);
31298 +
31299 + dwc_otg_save_global_regs(core_if);
31300 + dwc_otg_save_dev_regs(core_if);
31301 +
31302 + /* Wait for 10 PHY clocks */
31303 + dwc_udelay(10);
31304 +
31305 + /* Program GPIO register while entering to xHib */
31306 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
31307 +
31308 + pcgcctl.b.enbl_extnd_hiber = 1;
31309 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31310 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31311 +
31312 + pcgcctl.d32 = 0;
31313 + pcgcctl.b.extnd_hiber_pwrclmp = 1;
31314 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31315 +
31316 + pcgcctl.d32 = 0;
31317 + pcgcctl.b.extnd_hiber_switch = 1;
31318 + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31319 + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
31320 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31321 +
31322 + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
31323 +
31324 + return 1;
31325 + }
31326 + }
31327 + } else {
31328 + if (core_if->op_state == A_PERIPHERAL) {
31329 + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
31330 + /* Clear the a_peripheral flag, back to a_host. */
31331 + DWC_SPINUNLOCK(core_if->lock);
31332 + cil_pcd_stop(core_if);
31333 + cil_hcd_start(core_if);
31334 + DWC_SPINLOCK(core_if->lock);
31335 + core_if->op_state = A_HOST;
31336 + }
31337 + }
31338 +
31339 + /* Change to L2(suspend) state */
31340 + core_if->lx_state = DWC_OTG_L2;
31341 +
31342 + /* Clear interrupt */
31343 + gintsts.d32 = 0;
31344 + gintsts.b.usbsuspend = 1;
31345 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31346 +
31347 + return 1;
31348 +}
31349 +
31350 +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
31351 +{
31352 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31353 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31354 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
31355 +
31356 + dwc_udelay(10);
31357 +
31358 + /* Program GPIO register while entering to xHib */
31359 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
31360 +
31361 + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
31362 + pcgcctl.b.extnd_hiber_pwrclmp = 0;
31363 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31364 + dwc_udelay(10);
31365 +
31366 + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
31367 + gpwrdn.b.restore = 1;
31368 + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
31369 + dwc_udelay(10);
31370 +
31371 + restore_lpm_i2c_regs(core_if);
31372 +
31373 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31374 + pcgcctl.b.max_xcvrselect = 1;
31375 + pcgcctl.b.ess_reg_restored = 0;
31376 + pcgcctl.b.extnd_hiber_switch = 0;
31377 + pcgcctl.b.extnd_hiber_pwrclmp = 0;
31378 + pcgcctl.b.enbl_extnd_hiber = 1;
31379 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31380 +
31381 + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
31382 + gahbcfg.b.glblintrmsk = 1;
31383 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
31384 +
31385 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
31386 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
31387 +
31388 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
31389 + core_if->gr_backup->gusbcfg_local);
31390 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
31391 + core_if->dr_backup->dcfg);
31392 +
31393 + pcgcctl.d32 = 0;
31394 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31395 + pcgcctl.b.max_xcvrselect = 1;
31396 + pcgcctl.d32 |= 0x608;
31397 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31398 + dwc_udelay(10);
31399 +
31400 + pcgcctl.d32 = 0;
31401 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31402 + pcgcctl.b.max_xcvrselect = 1;
31403 + pcgcctl.b.ess_reg_restored = 1;
31404 + pcgcctl.b.enbl_extnd_hiber = 1;
31405 + pcgcctl.b.rstpdwnmodule = 1;
31406 + pcgcctl.b.restoremode = 1;
31407 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31408 +
31409 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
31410 +
31411 + return 1;
31412 +}
31413 +
31414 +#ifdef CONFIG_USB_DWC_OTG_LPM
31415 +/**
31416 + * This function hadles LPM transaction received interrupt.
31417 + */
31418 +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
31419 +{
31420 + glpmcfg_data_t lpmcfg;
31421 + gintsts_data_t gintsts;
31422 +
31423 + if (!core_if->core_params->lpm_enable) {
31424 + DWC_PRINTF("Unexpected LPM interrupt\n");
31425 + }
31426 +
31427 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
31428 + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
31429 +
31430 + if (dwc_otg_is_host_mode(core_if)) {
31431 + cil_hcd_sleep(core_if);
31432 + } else {
31433 + lpmcfg.b.hird_thres |= (1 << 4);
31434 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
31435 + lpmcfg.d32);
31436 + }
31437 +
31438 + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
31439 + dwc_udelay(10);
31440 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
31441 + if (lpmcfg.b.prt_sleep_sts) {
31442 + /* Save the current state */
31443 + core_if->lx_state = DWC_OTG_L1;
31444 + }
31445 +
31446 + /* Clear interrupt */
31447 + gintsts.d32 = 0;
31448 + gintsts.b.lpmtranrcvd = 1;
31449 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31450 + return 1;
31451 +}
31452 +#endif /* CONFIG_USB_DWC_OTG_LPM */
31453 +
31454 +/**
31455 + * This function returns the Core Interrupt register.
31456 + */
31457 +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if, gintmsk_data_t *reenable_gintmsk, dwc_otg_hcd_t *hcd)
31458 +{
31459 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
31460 + gintsts_data_t gintsts;
31461 + gintmsk_data_t gintmsk;
31462 + gintmsk_data_t gintmsk_common = {.d32 = 0 };
31463 + gintmsk_common.b.wkupintr = 1;
31464 + gintmsk_common.b.sessreqintr = 1;
31465 + gintmsk_common.b.conidstschng = 1;
31466 + gintmsk_common.b.otgintr = 1;
31467 + gintmsk_common.b.modemismatch = 1;
31468 + gintmsk_common.b.disconnect = 1;
31469 + gintmsk_common.b.usbsuspend = 1;
31470 +#ifdef CONFIG_USB_DWC_OTG_LPM
31471 + gintmsk_common.b.lpmtranrcvd = 1;
31472 +#endif
31473 + gintmsk_common.b.restoredone = 1;
31474 + if(dwc_otg_is_device_mode(core_if))
31475 + {
31476 + /** @todo: The port interrupt occurs while in device
31477 + * mode. Added code to CIL to clear the interrupt for now!
31478 + */
31479 + gintmsk_common.b.portintr = 1;
31480 + }
31481 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
31482 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
31483 + if(fiq_enable) {
31484 + local_fiq_disable();
31485 + /* Pull in the interrupts that the FIQ has masked */
31486 + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
31487 + gintmsk.d32 |= gintmsk_common.d32;
31488 + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
31489 + reenable_gintmsk->d32 = gintmsk.d32;
31490 + local_fiq_enable();
31491 + }
31492 +
31493 + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
31494 +
31495 +#ifdef DEBUG
31496 + /* if any common interrupts set */
31497 + if (gintsts.d32 & gintmsk_common.d32) {
31498 + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
31499 + gintsts.d32, gintmsk.d32);
31500 + }
31501 +#endif
31502 + if (!fiq_enable){
31503 + if (gahbcfg.b.glblintrmsk)
31504 + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
31505 + else
31506 + return 0;
31507 + } else {
31508 + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
31509 + * Can't trust the global interrupt mask bit in this case.
31510 + */
31511 + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
31512 + }
31513 +
31514 +}
31515 +
31516 +/* MACRO for clearing interupt bits in GPWRDN register */
31517 +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
31518 +do { \
31519 + gpwrdn_data_t gpwrdn = {.d32=0}; \
31520 + gpwrdn.b.__intr = 1; \
31521 + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
31522 + 0, gpwrdn.d32); \
31523 +} while (0)
31524 +
31525 +/**
31526 + * Common interrupt handler.
31527 + *
31528 + * The common interrupts are those that occur in both Host and Device mode.
31529 + * This handler handles the following interrupts:
31530 + * - Mode Mismatch Interrupt
31531 + * - Disconnect Interrupt
31532 + * - OTG Interrupt
31533 + * - Connector ID Status Change Interrupt
31534 + * - Session Request Interrupt.
31535 + * - Resume / Remote Wakeup Detected Interrupt.
31536 + * - LPM Transaction Received Interrupt
31537 + * - ADP Transaction Received Interrupt
31538 + *
31539 + */
31540 +int32_t dwc_otg_handle_common_intr(void *dev)
31541 +{
31542 + int retval = 0;
31543 + gintsts_data_t gintsts;
31544 + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
31545 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31546 + dwc_otg_device_t *otg_dev = dev;
31547 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
31548 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31549 + if (dwc_otg_is_device_mode(core_if))
31550 + core_if->frame_num = dwc_otg_get_frame_number(core_if);
31551 +
31552 + if (core_if->lock)
31553 + DWC_SPINLOCK(core_if->lock);
31554 +
31555 + if (core_if->power_down == 3 && core_if->xhib == 1) {
31556 + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
31557 + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
31558 + core_if->xhib = 2;
31559 + if (core_if->lock)
31560 + DWC_SPINUNLOCK(core_if->lock);
31561 +
31562 + return retval;
31563 + }
31564 +
31565 + if (core_if->hibernation_suspend <= 0) {
31566 + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
31567 + * of this handler - god only knows why it's done like this
31568 + */
31569 + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
31570 +
31571 + if (gintsts.b.modemismatch) {
31572 + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
31573 + }
31574 + if (gintsts.b.otgintr) {
31575 + retval |= dwc_otg_handle_otg_intr(core_if);
31576 + }
31577 + if (gintsts.b.conidstschng) {
31578 + retval |=
31579 + dwc_otg_handle_conn_id_status_change_intr(core_if);
31580 + }
31581 + if (gintsts.b.disconnect) {
31582 + retval |= dwc_otg_handle_disconnect_intr(core_if);
31583 + }
31584 + if (gintsts.b.sessreqintr) {
31585 + retval |= dwc_otg_handle_session_req_intr(core_if);
31586 + }
31587 + if (gintsts.b.wkupintr) {
31588 + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
31589 + }
31590 + if (gintsts.b.usbsuspend) {
31591 + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
31592 + }
31593 +#ifdef CONFIG_USB_DWC_OTG_LPM
31594 + if (gintsts.b.lpmtranrcvd) {
31595 + retval |= dwc_otg_handle_lpm_intr(core_if);
31596 + }
31597 +#endif
31598 + if (gintsts.b.restoredone) {
31599 + gintsts.d32 = 0;
31600 + if (core_if->power_down == 2)
31601 + core_if->hibernation_suspend = -1;
31602 + else if (core_if->power_down == 3 && core_if->xhib == 2) {
31603 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31604 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31605 + dctl_data_t dctl = {.d32 = 0 };
31606 +
31607 + DWC_WRITE_REG32(&core_if->core_global_regs->
31608 + gintsts, 0xFFFFFFFF);
31609 +
31610 + DWC_DEBUGPL(DBG_ANY,
31611 + "RESTORE DONE generated\n");
31612 +
31613 + gpwrdn.b.restore = 1;
31614 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31615 + dwc_udelay(10);
31616 +
31617 + pcgcctl.b.rstpdwnmodule = 1;
31618 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
31619 +
31620 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
31621 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
31622 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
31623 + dwc_udelay(50);
31624 +
31625 + dctl.b.pwronprgdone = 1;
31626 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
31627 + dwc_udelay(10);
31628 +
31629 + dwc_otg_restore_global_regs(core_if);
31630 + dwc_otg_restore_dev_regs(core_if, 0);
31631 +
31632 + dctl.d32 = 0;
31633 + dctl.b.pwronprgdone = 1;
31634 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
31635 + dwc_udelay(10);
31636 +
31637 + pcgcctl.d32 = 0;
31638 + pcgcctl.b.enbl_extnd_hiber = 1;
31639 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
31640 +
31641 + /* The core will be in ON STATE */
31642 + core_if->lx_state = DWC_OTG_L0;
31643 + core_if->xhib = 0;
31644 +
31645 + DWC_SPINUNLOCK(core_if->lock);
31646 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
31647 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
31648 + }
31649 + DWC_SPINLOCK(core_if->lock);
31650 +
31651 + }
31652 +
31653 + gintsts.b.restoredone = 1;
31654 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
31655 + DWC_PRINTF(" --Restore done interrupt received-- \n");
31656 + retval |= 1;
31657 + }
31658 + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
31659 + /* The port interrupt occurs while in device mode with HPRT0
31660 + * Port Enable/Disable.
31661 + */
31662 + gintsts.d32 = 0;
31663 + gintsts.b.portintr = 1;
31664 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
31665 + retval |= 1;
31666 + gintmsk_reenable.b.portintr = 1;
31667 +
31668 + }
31669 + /* Did we actually handle anything? if so, unmask the interrupt */
31670 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
31671 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
31672 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
31673 + if (retval && fiq_enable) {
31674 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
31675 + }
31676 +
31677 + } else {
31678 + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
31679 +
31680 + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
31681 + CLEAR_GPWRDN_INTR(core_if, disconn_det);
31682 + if (gpwrdn.b.linestate == 0) {
31683 + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
31684 + } else {
31685 + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
31686 + }
31687 +
31688 + retval |= 1;
31689 + }
31690 + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
31691 + CLEAR_GPWRDN_INTR(core_if, lnstschng);
31692 + /* remote wakeup from hibernation */
31693 + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
31694 + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
31695 + } else {
31696 + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
31697 + }
31698 + retval |= 1;
31699 + }
31700 + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
31701 + CLEAR_GPWRDN_INTR(core_if, rst_det);
31702 + if (gpwrdn.b.linestate == 0) {
31703 + DWC_PRINTF("Reset detected\n");
31704 + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
31705 + }
31706 + }
31707 + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
31708 + CLEAR_GPWRDN_INTR(core_if, srp_det);
31709 + dwc_otg_handle_pwrdn_srp_intr(core_if);
31710 + retval |= 1;
31711 + }
31712 + }
31713 + /* Handle ADP interrupt here */
31714 + if (gpwrdn.b.adp_int) {
31715 + DWC_PRINTF("ADP interrupt\n");
31716 + CLEAR_GPWRDN_INTR(core_if, adp_int);
31717 + dwc_otg_adp_handle_intr(core_if);
31718 + retval |= 1;
31719 + }
31720 + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
31721 + DWC_PRINTF("STS CHNG interrupt asserted\n");
31722 + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
31723 + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
31724 +
31725 + retval |= 1;
31726 + }
31727 + if (core_if->lock)
31728 + DWC_SPINUNLOCK(core_if->lock);
31729 + return retval;
31730 +}
31731 --- /dev/null
31732 +++ b/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
31733 @@ -0,0 +1,705 @@
31734 +/* ==========================================================================
31735 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
31736 + * $Revision: #13 $
31737 + * $Date: 2012/08/10 $
31738 + * $Change: 2047372 $
31739 + *
31740 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
31741 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
31742 + * otherwise expressly agreed to in writing between Synopsys and you.
31743 + *
31744 + * The Software IS NOT an item of Licensed Software or Licensed Product under
31745 + * any End User Software License Agreement or Agreement for Licensed Product
31746 + * with Synopsys or any supplement thereto. You are permitted to use and
31747 + * redistribute this Software in source and binary forms, with or without
31748 + * modification, provided that redistributions of source code must retain this
31749 + * notice. You may not view, use, disclose, copy or distribute this file or
31750 + * any information contained herein except pursuant to this license grant from
31751 + * Synopsys. If you do not agree with this notice, including the disclaimer
31752 + * below, then you are not authorized to use the Software.
31753 + *
31754 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
31755 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31756 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31757 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
31758 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31759 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31760 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31761 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31762 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31763 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31764 + * DAMAGE.
31765 + * ========================================================================== */
31766 +#if !defined(__DWC_CORE_IF_H__)
31767 +#define __DWC_CORE_IF_H__
31768 +
31769 +#include "dwc_os.h"
31770 +
31771 +/** @file
31772 + * This file defines DWC_OTG Core API
31773 + */
31774 +
31775 +struct dwc_otg_core_if;
31776 +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
31777 +
31778 +/** Maximum number of Periodic FIFOs */
31779 +#define MAX_PERIO_FIFOS 15
31780 +/** Maximum number of Periodic FIFOs */
31781 +#define MAX_TX_FIFOS 15
31782 +
31783 +/** Maximum number of Endpoints/HostChannels */
31784 +#define MAX_EPS_CHANNELS 16
31785 +
31786 +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
31787 +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
31788 +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
31789 +
31790 +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
31791 +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
31792 +
31793 +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
31794 +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
31795 +
31796 +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
31797 +
31798 +/** This function should be called on every hardware interrupt. */
31799 +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
31800 +
31801 +/** @name OTG Core Parameters */
31802 +/** @{ */
31803 +
31804 +/**
31805 + * Specifies the OTG capabilities. The driver will automatically
31806 + * detect the value for this parameter if none is specified.
31807 + * 0 - HNP and SRP capable (default)
31808 + * 1 - SRP Only capable
31809 + * 2 - No HNP/SRP capable
31810 + */
31811 +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
31812 +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
31813 +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
31814 +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
31815 +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
31816 +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
31817 +
31818 +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
31819 +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
31820 +#define dwc_param_opt_default 1
31821 +
31822 +/**
31823 + * Specifies whether to use slave or DMA mode for accessing the data
31824 + * FIFOs. The driver will automatically detect the value for this
31825 + * parameter if none is specified.
31826 + * 0 - Slave
31827 + * 1 - DMA (default, if available)
31828 + */
31829 +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
31830 + int32_t val);
31831 +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
31832 +#define dwc_param_dma_enable_default 1
31833 +
31834 +/**
31835 + * When DMA mode is enabled specifies whether to use
31836 + * address DMA or DMA Descritor mode for accessing the data
31837 + * FIFOs in device mode. The driver will automatically detect
31838 + * the value for this parameter if none is specified.
31839 + * 0 - address DMA
31840 + * 1 - DMA Descriptor(default, if available)
31841 + */
31842 +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
31843 + int32_t val);
31844 +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
31845 +//#define dwc_param_dma_desc_enable_default 1
31846 +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
31847 +
31848 +/** The DMA Burst size (applicable only for External DMA
31849 + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
31850 + */
31851 +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
31852 + int32_t val);
31853 +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
31854 +#define dwc_param_dma_burst_size_default 32
31855 +
31856 +/**
31857 + * Specifies the maximum speed of operation in host and device mode.
31858 + * The actual speed depends on the speed of the attached device and
31859 + * the value of phy_type. The actual speed depends on the speed of the
31860 + * attached device.
31861 + * 0 - High Speed (default)
31862 + * 1 - Full Speed
31863 + */
31864 +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
31865 +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
31866 +#define dwc_param_speed_default 0
31867 +#define DWC_SPEED_PARAM_HIGH 0
31868 +#define DWC_SPEED_PARAM_FULL 1
31869 +
31870 +/** Specifies whether low power mode is supported when attached
31871 + * to a Full Speed or Low Speed device in host mode.
31872 + * 0 - Don't support low power mode (default)
31873 + * 1 - Support low power mode
31874 + */
31875 +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
31876 + core_if, int32_t val);
31877 +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
31878 + * core_if);
31879 +#define dwc_param_host_support_fs_ls_low_power_default 0
31880 +
31881 +/** Specifies the PHY clock rate in low power mode when connected to a
31882 + * Low Speed device in host mode. This parameter is applicable only if
31883 + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
31884 + * then defaults to 6 MHZ otherwise 48 MHZ.
31885 + *
31886 + * 0 - 48 MHz
31887 + * 1 - 6 MHz
31888 + */
31889 +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
31890 + core_if, int32_t val);
31891 +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
31892 + core_if);
31893 +#define dwc_param_host_ls_low_power_phy_clk_default 0
31894 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
31895 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
31896 +
31897 +/**
31898 + * 0 - Use cC FIFO size parameters
31899 + * 1 - Allow dynamic FIFO sizing (default)
31900 + */
31901 +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
31902 + int32_t val);
31903 +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
31904 + core_if);
31905 +#define dwc_param_enable_dynamic_fifo_default 1
31906 +
31907 +/** Total number of 4-byte words in the data FIFO memory. This
31908 + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
31909 + * Tx FIFOs.
31910 + * 32 to 32768 (default 8192)
31911 + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
31912 + */
31913 +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
31914 + int32_t val);
31915 +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
31916 +//#define dwc_param_data_fifo_size_default 8192
31917 +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
31918 +
31919 +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
31920 + * FIFO sizing is enabled.
31921 + * 16 to 32768 (default 1064)
31922 + */
31923 +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
31924 + int32_t val);
31925 +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
31926 +#define dwc_param_dev_rx_fifo_size_default 1064
31927 +
31928 +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
31929 + * when dynamic FIFO sizing is enabled.
31930 + * 16 to 32768 (default 1024)
31931 + */
31932 +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
31933 + core_if, int32_t val);
31934 +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
31935 + core_if);
31936 +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
31937 +
31938 +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
31939 + * mode when dynamic FIFO sizing is enabled.
31940 + * 4 to 768 (default 256)
31941 + */
31942 +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
31943 + int32_t val, int fifo_num);
31944 +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
31945 + core_if, int fifo_num);
31946 +#define dwc_param_dev_perio_tx_fifo_size_default 256
31947 +
31948 +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
31949 + * FIFO sizing is enabled.
31950 + * 16 to 32768 (default 1024)
31951 + */
31952 +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
31953 + int32_t val);
31954 +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
31955 +//#define dwc_param_host_rx_fifo_size_default 1024
31956 +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
31957 +
31958 +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
31959 + * when Dynamic FIFO sizing is enabled in the core.
31960 + * 16 to 32768 (default 1024)
31961 + */
31962 +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
31963 + core_if, int32_t val);
31964 +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
31965 + core_if);
31966 +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
31967 +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
31968 +
31969 +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
31970 + * FIFO sizing is enabled.
31971 + * 16 to 32768 (default 1024)
31972 + */
31973 +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
31974 + core_if, int32_t val);
31975 +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
31976 + core_if);
31977 +//#define dwc_param_host_perio_tx_fifo_size_default 1024
31978 +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
31979 +
31980 +/** The maximum transfer size supported in bytes.
31981 + * 2047 to 65,535 (default 65,535)
31982 + */
31983 +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
31984 + int32_t val);
31985 +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
31986 +#define dwc_param_max_transfer_size_default 65535
31987 +
31988 +/** The maximum number of packets in a transfer.
31989 + * 15 to 511 (default 511)
31990 + */
31991 +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
31992 + int32_t val);
31993 +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
31994 +#define dwc_param_max_packet_count_default 511
31995 +
31996 +/** The number of host channel registers to use.
31997 + * 1 to 16 (default 12)
31998 + * Note: The FPGA configuration supports a maximum of 12 host channels.
31999 + */
32000 +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
32001 + int32_t val);
32002 +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
32003 +//#define dwc_param_host_channels_default 12
32004 +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
32005 +
32006 +/** The number of endpoints in addition to EP0 available for device
32007 + * mode operations.
32008 + * 1 to 15 (default 6 IN and OUT)
32009 + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
32010 + * endpoints in addition to EP0.
32011 + */
32012 +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
32013 + int32_t val);
32014 +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
32015 +#define dwc_param_dev_endpoints_default 6
32016 +
32017 +/**
32018 + * Specifies the type of PHY interface to use. By default, the driver
32019 + * will automatically detect the phy_type.
32020 + *
32021 + * 0 - Full Speed PHY
32022 + * 1 - UTMI+ (default)
32023 + * 2 - ULPI
32024 + */
32025 +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
32026 +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
32027 +#define DWC_PHY_TYPE_PARAM_FS 0
32028 +#define DWC_PHY_TYPE_PARAM_UTMI 1
32029 +#define DWC_PHY_TYPE_PARAM_ULPI 2
32030 +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
32031 +
32032 +/**
32033 + * Specifies the UTMI+ Data Width. This parameter is
32034 + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
32035 + * PHY_TYPE, this parameter indicates the data width between
32036 + * the MAC and the ULPI Wrapper.) Also, this parameter is
32037 + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
32038 + * to "8 and 16 bits", meaning that the core has been
32039 + * configured to work at either data path width.
32040 + *
32041 + * 8 or 16 bits (default 16)
32042 + */
32043 +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
32044 + int32_t val);
32045 +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
32046 +//#define dwc_param_phy_utmi_width_default 16
32047 +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
32048 +
32049 +/**
32050 + * Specifies whether the ULPI operates at double or single
32051 + * data rate. This parameter is only applicable if PHY_TYPE is
32052 + * ULPI.
32053 + *
32054 + * 0 - single data rate ULPI interface with 8 bit wide data
32055 + * bus (default)
32056 + * 1 - double data rate ULPI interface with 4 bit wide data
32057 + * bus
32058 + */
32059 +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
32060 + int32_t val);
32061 +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
32062 +#define dwc_param_phy_ulpi_ddr_default 0
32063 +
32064 +/**
32065 + * Specifies whether to use the internal or external supply to
32066 + * drive the vbus with a ULPI phy.
32067 + */
32068 +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
32069 + int32_t val);
32070 +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
32071 +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
32072 +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
32073 +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
32074 +
32075 +/**
32076 + * Specifies whether to use the I2Cinterface for full speed PHY. This
32077 + * parameter is only applicable if PHY_TYPE is FS.
32078 + * 0 - No (default)
32079 + * 1 - Yes
32080 + */
32081 +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
32082 + int32_t val);
32083 +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
32084 +#define dwc_param_i2c_enable_default 0
32085 +
32086 +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
32087 + int32_t val);
32088 +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
32089 +#define dwc_param_ulpi_fs_ls_default 0
32090 +
32091 +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
32092 +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
32093 +#define dwc_param_ts_dline_default 0
32094 +
32095 +/**
32096 + * Specifies whether dedicated transmit FIFOs are
32097 + * enabled for non periodic IN endpoints in device mode
32098 + * 0 - No
32099 + * 1 - Yes
32100 + */
32101 +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
32102 + int32_t val);
32103 +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
32104 + core_if);
32105 +#define dwc_param_en_multiple_tx_fifo_default 1
32106 +
32107 +/** Number of 4-byte words in each of the Tx FIFOs in device
32108 + * mode when dynamic FIFO sizing is enabled.
32109 + * 4 to 768 (default 256)
32110 + */
32111 +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
32112 + int fifo_num, int32_t val);
32113 +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
32114 + int fifo_num);
32115 +#define dwc_param_dev_tx_fifo_size_default 768
32116 +
32117 +/** Thresholding enable flag-
32118 + * bit 0 - enable non-ISO Tx thresholding
32119 + * bit 1 - enable ISO Tx thresholding
32120 + * bit 2 - enable Rx thresholding
32121 + */
32122 +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
32123 +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
32124 +#define dwc_param_thr_ctl_default 0
32125 +
32126 +/** Thresholding length for Tx
32127 + * FIFOs in 32 bit DWORDs
32128 + */
32129 +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
32130 + int32_t val);
32131 +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
32132 +#define dwc_param_tx_thr_length_default 64
32133 +
32134 +/** Thresholding length for Rx
32135 + * FIFOs in 32 bit DWORDs
32136 + */
32137 +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
32138 + int32_t val);
32139 +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
32140 +#define dwc_param_rx_thr_length_default 64
32141 +
32142 +/**
32143 + * Specifies whether LPM (Link Power Management) support is enabled
32144 + */
32145 +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
32146 + int32_t val);
32147 +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
32148 +#define dwc_param_lpm_enable_default 1
32149 +
32150 +/**
32151 + * Specifies whether PTI enhancement is enabled
32152 + */
32153 +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
32154 + int32_t val);
32155 +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
32156 +#define dwc_param_pti_enable_default 0
32157 +
32158 +/**
32159 + * Specifies whether MPI enhancement is enabled
32160 + */
32161 +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
32162 + int32_t val);
32163 +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
32164 +#define dwc_param_mpi_enable_default 0
32165 +
32166 +/**
32167 + * Specifies whether ADP capability is enabled
32168 + */
32169 +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
32170 + int32_t val);
32171 +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
32172 +#define dwc_param_adp_enable_default 0
32173 +
32174 +/**
32175 + * Specifies whether IC_USB capability is enabled
32176 + */
32177 +
32178 +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
32179 + int32_t val);
32180 +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
32181 +#define dwc_param_ic_usb_cap_default 0
32182 +
32183 +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
32184 + int32_t val);
32185 +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
32186 +#define dwc_param_ahb_thr_ratio_default 0
32187 +
32188 +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
32189 + int32_t val);
32190 +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
32191 +#define dwc_param_power_down_default 0
32192 +
32193 +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
32194 + int32_t val);
32195 +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
32196 +#define dwc_param_reload_ctl_default 0
32197 +
32198 +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
32199 + int32_t val);
32200 +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
32201 +#define dwc_param_dev_out_nak_default 0
32202 +
32203 +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
32204 + int32_t val);
32205 +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
32206 +#define dwc_param_cont_on_bna_default 0
32207 +
32208 +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
32209 + int32_t val);
32210 +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
32211 +#define dwc_param_ahb_single_default 0
32212 +
32213 +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
32214 +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
32215 +#define dwc_param_otg_ver_default 0
32216 +
32217 +/** @} */
32218 +
32219 +/** @name Access to registers and bit-fields */
32220 +
32221 +/**
32222 + * Dump core registers and SPRAM
32223 + */
32224 +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
32225 +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
32226 +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
32227 +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
32228 +
32229 +/**
32230 + * Get host negotiation status.
32231 + */
32232 +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
32233 +
32234 +/**
32235 + * Get srp status
32236 + */
32237 +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
32238 +
32239 +/**
32240 + * Set hnpreq bit in the GOTGCTL register.
32241 + */
32242 +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
32243 +
32244 +/**
32245 + * Get Content of SNPSID register.
32246 + */
32247 +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
32248 +
32249 +/**
32250 + * Get current mode.
32251 + * Returns 0 if in device mode, and 1 if in host mode.
32252 + */
32253 +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
32254 +
32255 +/**
32256 + * Get value of hnpcapable field in the GUSBCFG register
32257 + */
32258 +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
32259 +/**
32260 + * Set value of hnpcapable field in the GUSBCFG register
32261 + */
32262 +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
32263 +
32264 +/**
32265 + * Get value of srpcapable field in the GUSBCFG register
32266 + */
32267 +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
32268 +/**
32269 + * Set value of srpcapable field in the GUSBCFG register
32270 + */
32271 +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
32272 +
32273 +/**
32274 + * Get value of devspeed field in the DCFG register
32275 + */
32276 +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
32277 +/**
32278 + * Set value of devspeed field in the DCFG register
32279 + */
32280 +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
32281 +
32282 +/**
32283 + * Get the value of busconnected field from the HPRT0 register
32284 + */
32285 +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
32286 +
32287 +/**
32288 + * Gets the device enumeration Speed.
32289 + */
32290 +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
32291 +
32292 +/**
32293 + * Get value of prtpwr field from the HPRT0 register
32294 + */
32295 +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
32296 +
32297 +/**
32298 + * Get value of flag indicating core state - hibernated or not
32299 + */
32300 +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
32301 +
32302 +/**
32303 + * Set value of prtpwr field from the HPRT0 register
32304 + */
32305 +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
32306 +
32307 +/**
32308 + * Get value of prtsusp field from the HPRT0 regsiter
32309 + */
32310 +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
32311 +/**
32312 + * Set value of prtpwr field from the HPRT0 register
32313 + */
32314 +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
32315 +
32316 +/**
32317 + * Get value of ModeChTimEn field from the HCFG regsiter
32318 + */
32319 +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
32320 +/**
32321 + * Set value of ModeChTimEn field from the HCFG regsiter
32322 + */
32323 +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
32324 +
32325 +/**
32326 + * Get value of Fram Interval field from the HFIR regsiter
32327 + */
32328 +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
32329 +/**
32330 + * Set value of Frame Interval field from the HFIR regsiter
32331 + */
32332 +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
32333 +
32334 +/**
32335 + * Set value of prtres field from the HPRT0 register
32336 + *FIXME Remove?
32337 + */
32338 +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
32339 +
32340 +/**
32341 + * Get value of rmtwkupsig bit in DCTL register
32342 + */
32343 +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
32344 +
32345 +/**
32346 + * Get value of prt_sleep_sts field from the GLPMCFG register
32347 + */
32348 +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
32349 +
32350 +/**
32351 + * Get value of rem_wkup_en field from the GLPMCFG register
32352 + */
32353 +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
32354 +
32355 +/**
32356 + * Get value of appl_resp field from the GLPMCFG register
32357 + */
32358 +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
32359 +/**
32360 + * Set value of appl_resp field from the GLPMCFG register
32361 + */
32362 +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
32363 +
32364 +/**
32365 + * Get value of hsic_connect field from the GLPMCFG register
32366 + */
32367 +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
32368 +/**
32369 + * Set value of hsic_connect field from the GLPMCFG register
32370 + */
32371 +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
32372 +
32373 +/**
32374 + * Get value of inv_sel_hsic field from the GLPMCFG register.
32375 + */
32376 +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
32377 +/**
32378 + * Set value of inv_sel_hsic field from the GLPMFG register.
32379 + */
32380 +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
32381 +
32382 +/*
32383 + * Some functions for accessing registers
32384 + */
32385 +
32386 +/**
32387 + * GOTGCTL register
32388 + */
32389 +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
32390 +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
32391 +
32392 +/**
32393 + * GUSBCFG register
32394 + */
32395 +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
32396 +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
32397 +
32398 +/**
32399 + * GRXFSIZ register
32400 + */
32401 +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
32402 +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
32403 +
32404 +/**
32405 + * GNPTXFSIZ register
32406 + */
32407 +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
32408 +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
32409 +
32410 +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
32411 +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
32412 +
32413 +/**
32414 + * GGPIO register
32415 + */
32416 +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
32417 +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
32418 +
32419 +/**
32420 + * GUID register
32421 + */
32422 +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
32423 +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
32424 +
32425 +/**
32426 + * HPRT0 register
32427 + */
32428 +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
32429 +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
32430 +
32431 +/**
32432 + * GHPTXFSIZE
32433 + */
32434 +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
32435 +
32436 +/** @} */
32437 +
32438 +#endif /* __DWC_CORE_IF_H__ */
32439 --- /dev/null
32440 +++ b/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
32441 @@ -0,0 +1,117 @@
32442 +/* ==========================================================================
32443 + *
32444 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
32445 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
32446 + * otherwise expressly agreed to in writing between Synopsys and you.
32447 + *
32448 + * The Software IS NOT an item of Licensed Software or Licensed Product under
32449 + * any End User Software License Agreement or Agreement for Licensed Product
32450 + * with Synopsys or any supplement thereto. You are permitted to use and
32451 + * redistribute this Software in source and binary forms, with or without
32452 + * modification, provided that redistributions of source code must retain this
32453 + * notice. You may not view, use, disclose, copy or distribute this file or
32454 + * any information contained herein except pursuant to this license grant from
32455 + * Synopsys. If you do not agree with this notice, including the disclaimer
32456 + * below, then you are not authorized to use the Software.
32457 + *
32458 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
32459 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32460 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32461 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
32462 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32463 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32464 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32465 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32466 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32467 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32468 + * DAMAGE.
32469 + * ========================================================================== */
32470 +
32471 +#ifndef __DWC_OTG_DBG_H__
32472 +#define __DWC_OTG_DBG_H__
32473 +
32474 +/** @file
32475 + * This file defines debug levels.
32476 + * Debugging support vanishes in non-debug builds.
32477 + */
32478 +
32479 +/**
32480 + * The Debug Level bit-mask variable.
32481 + */
32482 +extern uint32_t g_dbg_lvl;
32483 +/**
32484 + * Set the Debug Level variable.
32485 + */
32486 +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
32487 +{
32488 + uint32_t old = g_dbg_lvl;
32489 + g_dbg_lvl = new;
32490 + return old;
32491 +}
32492 +
32493 +#define DBG_USER (0x1)
32494 +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
32495 +#define DBG_CIL (0x2)
32496 +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
32497 + * messages */
32498 +#define DBG_CILV (0x20)
32499 +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
32500 + * messages */
32501 +#define DBG_PCD (0x4)
32502 +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
32503 + * messages */
32504 +#define DBG_PCDV (0x40)
32505 +/** When debug level has the DBG_HCD bit set, display Host debug messages */
32506 +#define DBG_HCD (0x8)
32507 +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
32508 + * messages */
32509 +#define DBG_HCDV (0x80)
32510 +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
32511 + * mode. */
32512 +#define DBG_HCD_URB (0x800)
32513 +/** When debug level has the DBG_HCDI bit set, display host interrupt
32514 + * messages. */
32515 +#define DBG_HCDI (0x1000)
32516 +
32517 +/** When debug level has any bit set, display debug messages */
32518 +#define DBG_ANY (0xFF)
32519 +
32520 +/** All debug messages off */
32521 +#define DBG_OFF 0
32522 +
32523 +/** Prefix string for DWC_DEBUG print macros. */
32524 +#define USB_DWC "DWC_otg: "
32525 +
32526 +/**
32527 + * Print a debug message when the Global debug level variable contains
32528 + * the bit defined in <code>lvl</code>.
32529 + *
32530 + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
32531 + * @param[in] x - like printf
32532 + *
32533 + * Example:<p>
32534 + * <code>
32535 + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
32536 + * </code>
32537 + * <br>
32538 + * results in:<br>
32539 + * <code>
32540 + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
32541 + * </code>
32542 + */
32543 +#ifdef DEBUG
32544 +
32545 +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
32546 +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
32547 +
32548 +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
32549 +
32550 +#else
32551 +
32552 +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
32553 +# define DWC_DEBUGP(x...)
32554 +
32555 +# define CHK_DEBUG_LEVEL(level) (0)
32556 +
32557 +#endif /*DEBUG*/
32558 +#endif
32559 --- /dev/null
32560 +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.c
32561 @@ -0,0 +1,1757 @@
32562 +/* ==========================================================================
32563 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
32564 + * $Revision: #92 $
32565 + * $Date: 2012/08/10 $
32566 + * $Change: 2047372 $
32567 + *
32568 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
32569 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
32570 + * otherwise expressly agreed to in writing between Synopsys and you.
32571 + *
32572 + * The Software IS NOT an item of Licensed Software or Licensed Product under
32573 + * any End User Software License Agreement or Agreement for Licensed Product
32574 + * with Synopsys or any supplement thereto. You are permitted to use and
32575 + * redistribute this Software in source and binary forms, with or without
32576 + * modification, provided that redistributions of source code must retain this
32577 + * notice. You may not view, use, disclose, copy or distribute this file or
32578 + * any information contained herein except pursuant to this license grant from
32579 + * Synopsys. If you do not agree with this notice, including the disclaimer
32580 + * below, then you are not authorized to use the Software.
32581 + *
32582 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
32583 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32584 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32585 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
32586 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32587 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32588 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32589 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32590 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32591 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32592 + * DAMAGE.
32593 + * ========================================================================== */
32594 +
32595 +/** @file
32596 + * The dwc_otg_driver module provides the initialization and cleanup entry
32597 + * points for the DWC_otg driver. This module will be dynamically installed
32598 + * after Linux is booted using the insmod command. When the module is
32599 + * installed, the dwc_otg_driver_init function is called. When the module is
32600 + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
32601 + *
32602 + * This module also defines a data structure for the dwc_otg_driver, which is
32603 + * used in conjunction with the standard ARM lm_device structure. These
32604 + * structures allow the OTG driver to comply with the standard Linux driver
32605 + * model in which devices and drivers are registered with a bus driver. This
32606 + * has the benefit that Linux can expose attributes of the driver and device
32607 + * in its special sysfs file system. Users can then read or write files in
32608 + * this file system to perform diagnostics on the driver components or the
32609 + * device.
32610 + */
32611 +
32612 +#include "dwc_otg_os_dep.h"
32613 +#include "dwc_os.h"
32614 +#include "dwc_otg_dbg.h"
32615 +#include "dwc_otg_driver.h"
32616 +#include "dwc_otg_attr.h"
32617 +#include "dwc_otg_core_if.h"
32618 +#include "dwc_otg_pcd_if.h"
32619 +#include "dwc_otg_hcd_if.h"
32620 +#include "dwc_otg_fiq_fsm.h"
32621 +
32622 +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
32623 +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
32624 +
32625 +bool microframe_schedule=true;
32626 +
32627 +static const char dwc_driver_name[] = "dwc_otg";
32628 +
32629 +
32630 +extern int pcd_init(
32631 +#ifdef LM_INTERFACE
32632 + struct lm_device *_dev
32633 +#elif defined(PCI_INTERFACE)
32634 + struct pci_dev *_dev
32635 +#elif defined(PLATFORM_INTERFACE)
32636 + struct platform_device *dev
32637 +#endif
32638 + );
32639 +extern int hcd_init(
32640 +#ifdef LM_INTERFACE
32641 + struct lm_device *_dev
32642 +#elif defined(PCI_INTERFACE)
32643 + struct pci_dev *_dev
32644 +#elif defined(PLATFORM_INTERFACE)
32645 + struct platform_device *dev
32646 +#endif
32647 + );
32648 +
32649 +extern int pcd_remove(
32650 +#ifdef LM_INTERFACE
32651 + struct lm_device *_dev
32652 +#elif defined(PCI_INTERFACE)
32653 + struct pci_dev *_dev
32654 +#elif defined(PLATFORM_INTERFACE)
32655 + struct platform_device *_dev
32656 +#endif
32657 + );
32658 +
32659 +extern void hcd_remove(
32660 +#ifdef LM_INTERFACE
32661 + struct lm_device *_dev
32662 +#elif defined(PCI_INTERFACE)
32663 + struct pci_dev *_dev
32664 +#elif defined(PLATFORM_INTERFACE)
32665 + struct platform_device *_dev
32666 +#endif
32667 + );
32668 +
32669 +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
32670 +
32671 +/*-------------------------------------------------------------------------*/
32672 +/* Encapsulate the module parameter settings */
32673 +
32674 +struct dwc_otg_driver_module_params {
32675 + int32_t opt;
32676 + int32_t otg_cap;
32677 + int32_t dma_enable;
32678 + int32_t dma_desc_enable;
32679 + int32_t dma_burst_size;
32680 + int32_t speed;
32681 + int32_t host_support_fs_ls_low_power;
32682 + int32_t host_ls_low_power_phy_clk;
32683 + int32_t enable_dynamic_fifo;
32684 + int32_t data_fifo_size;
32685 + int32_t dev_rx_fifo_size;
32686 + int32_t dev_nperio_tx_fifo_size;
32687 + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
32688 + int32_t host_rx_fifo_size;
32689 + int32_t host_nperio_tx_fifo_size;
32690 + int32_t host_perio_tx_fifo_size;
32691 + int32_t max_transfer_size;
32692 + int32_t max_packet_count;
32693 + int32_t host_channels;
32694 + int32_t dev_endpoints;
32695 + int32_t phy_type;
32696 + int32_t phy_utmi_width;
32697 + int32_t phy_ulpi_ddr;
32698 + int32_t phy_ulpi_ext_vbus;
32699 + int32_t i2c_enable;
32700 + int32_t ulpi_fs_ls;
32701 + int32_t ts_dline;
32702 + int32_t en_multiple_tx_fifo;
32703 + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
32704 + uint32_t thr_ctl;
32705 + uint32_t tx_thr_length;
32706 + uint32_t rx_thr_length;
32707 + int32_t pti_enable;
32708 + int32_t mpi_enable;
32709 + int32_t lpm_enable;
32710 + int32_t ic_usb_cap;
32711 + int32_t ahb_thr_ratio;
32712 + int32_t power_down;
32713 + int32_t reload_ctl;
32714 + int32_t dev_out_nak;
32715 + int32_t cont_on_bna;
32716 + int32_t ahb_single;
32717 + int32_t otg_ver;
32718 + int32_t adp_enable;
32719 +};
32720 +
32721 +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
32722 + .opt = -1,
32723 + .otg_cap = -1,
32724 + .dma_enable = -1,
32725 + .dma_desc_enable = -1,
32726 + .dma_burst_size = -1,
32727 + .speed = -1,
32728 + .host_support_fs_ls_low_power = -1,
32729 + .host_ls_low_power_phy_clk = -1,
32730 + .enable_dynamic_fifo = -1,
32731 + .data_fifo_size = -1,
32732 + .dev_rx_fifo_size = -1,
32733 + .dev_nperio_tx_fifo_size = -1,
32734 + .dev_perio_tx_fifo_size = {
32735 + /* dev_perio_tx_fifo_size_1 */
32736 + -1,
32737 + -1,
32738 + -1,
32739 + -1,
32740 + -1,
32741 + -1,
32742 + -1,
32743 + -1,
32744 + -1,
32745 + -1,
32746 + -1,
32747 + -1,
32748 + -1,
32749 + -1,
32750 + -1
32751 + /* 15 */
32752 + },
32753 + .host_rx_fifo_size = -1,
32754 + .host_nperio_tx_fifo_size = -1,
32755 + .host_perio_tx_fifo_size = -1,
32756 + .max_transfer_size = -1,
32757 + .max_packet_count = -1,
32758 + .host_channels = -1,
32759 + .dev_endpoints = -1,
32760 + .phy_type = -1,
32761 + .phy_utmi_width = -1,
32762 + .phy_ulpi_ddr = -1,
32763 + .phy_ulpi_ext_vbus = -1,
32764 + .i2c_enable = -1,
32765 + .ulpi_fs_ls = -1,
32766 + .ts_dline = -1,
32767 + .en_multiple_tx_fifo = -1,
32768 + .dev_tx_fifo_size = {
32769 + /* dev_tx_fifo_size */
32770 + -1,
32771 + -1,
32772 + -1,
32773 + -1,
32774 + -1,
32775 + -1,
32776 + -1,
32777 + -1,
32778 + -1,
32779 + -1,
32780 + -1,
32781 + -1,
32782 + -1,
32783 + -1,
32784 + -1
32785 + /* 15 */
32786 + },
32787 + .thr_ctl = -1,
32788 + .tx_thr_length = -1,
32789 + .rx_thr_length = -1,
32790 + .pti_enable = -1,
32791 + .mpi_enable = -1,
32792 + .lpm_enable = 0,
32793 + .ic_usb_cap = -1,
32794 + .ahb_thr_ratio = -1,
32795 + .power_down = -1,
32796 + .reload_ctl = -1,
32797 + .dev_out_nak = -1,
32798 + .cont_on_bna = -1,
32799 + .ahb_single = -1,
32800 + .otg_ver = -1,
32801 + .adp_enable = -1,
32802 +};
32803 +
32804 +//Global variable to switch the fiq fix on or off
32805 +bool fiq_enable = 1;
32806 +// Global variable to enable the split transaction fix
32807 +bool fiq_fsm_enable = true;
32808 +//Bulk split-transaction NAK holdoff in microframes
32809 +uint16_t nak_holdoff = 8;
32810 +
32811 +unsigned short fiq_fsm_mask = 0x0F;
32812 +
32813 +/**
32814 + * This function shows the Driver Version.
32815 + */
32816 +static ssize_t version_show(struct device_driver *dev, char *buf)
32817 +{
32818 + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
32819 + DWC_DRIVER_VERSION);
32820 +}
32821 +
32822 +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
32823 +
32824 +/**
32825 + * Global Debug Level Mask.
32826 + */
32827 +uint32_t g_dbg_lvl = 0; /* OFF */
32828 +
32829 +/**
32830 + * This function shows the driver Debug Level.
32831 + */
32832 +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
32833 +{
32834 + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
32835 +}
32836 +
32837 +/**
32838 + * This function stores the driver Debug Level.
32839 + */
32840 +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
32841 + size_t count)
32842 +{
32843 + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
32844 + return count;
32845 +}
32846 +
32847 +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
32848 + dbg_level_store);
32849 +
32850 +/**
32851 + * This function is called during module intialization
32852 + * to pass module parameters to the DWC_OTG CORE.
32853 + */
32854 +static int set_parameters(dwc_otg_core_if_t * core_if)
32855 +{
32856 + int retval = 0;
32857 + int i;
32858 +
32859 + if (dwc_otg_module_params.otg_cap != -1) {
32860 + retval +=
32861 + dwc_otg_set_param_otg_cap(core_if,
32862 + dwc_otg_module_params.otg_cap);
32863 + }
32864 + if (dwc_otg_module_params.dma_enable != -1) {
32865 + retval +=
32866 + dwc_otg_set_param_dma_enable(core_if,
32867 + dwc_otg_module_params.
32868 + dma_enable);
32869 + }
32870 + if (dwc_otg_module_params.dma_desc_enable != -1) {
32871 + retval +=
32872 + dwc_otg_set_param_dma_desc_enable(core_if,
32873 + dwc_otg_module_params.
32874 + dma_desc_enable);
32875 + }
32876 + if (dwc_otg_module_params.opt != -1) {
32877 + retval +=
32878 + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
32879 + }
32880 + if (dwc_otg_module_params.dma_burst_size != -1) {
32881 + retval +=
32882 + dwc_otg_set_param_dma_burst_size(core_if,
32883 + dwc_otg_module_params.
32884 + dma_burst_size);
32885 + }
32886 + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
32887 + retval +=
32888 + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
32889 + dwc_otg_module_params.
32890 + host_support_fs_ls_low_power);
32891 + }
32892 + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
32893 + retval +=
32894 + dwc_otg_set_param_enable_dynamic_fifo(core_if,
32895 + dwc_otg_module_params.
32896 + enable_dynamic_fifo);
32897 + }
32898 + if (dwc_otg_module_params.data_fifo_size != -1) {
32899 + retval +=
32900 + dwc_otg_set_param_data_fifo_size(core_if,
32901 + dwc_otg_module_params.
32902 + data_fifo_size);
32903 + }
32904 + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
32905 + retval +=
32906 + dwc_otg_set_param_dev_rx_fifo_size(core_if,
32907 + dwc_otg_module_params.
32908 + dev_rx_fifo_size);
32909 + }
32910 + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
32911 + retval +=
32912 + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
32913 + dwc_otg_module_params.
32914 + dev_nperio_tx_fifo_size);
32915 + }
32916 + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
32917 + retval +=
32918 + dwc_otg_set_param_host_rx_fifo_size(core_if,
32919 + dwc_otg_module_params.host_rx_fifo_size);
32920 + }
32921 + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
32922 + retval +=
32923 + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
32924 + dwc_otg_module_params.
32925 + host_nperio_tx_fifo_size);
32926 + }
32927 + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
32928 + retval +=
32929 + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
32930 + dwc_otg_module_params.
32931 + host_perio_tx_fifo_size);
32932 + }
32933 + if (dwc_otg_module_params.max_transfer_size != -1) {
32934 + retval +=
32935 + dwc_otg_set_param_max_transfer_size(core_if,
32936 + dwc_otg_module_params.
32937 + max_transfer_size);
32938 + }
32939 + if (dwc_otg_module_params.max_packet_count != -1) {
32940 + retval +=
32941 + dwc_otg_set_param_max_packet_count(core_if,
32942 + dwc_otg_module_params.
32943 + max_packet_count);
32944 + }
32945 + if (dwc_otg_module_params.host_channels != -1) {
32946 + retval +=
32947 + dwc_otg_set_param_host_channels(core_if,
32948 + dwc_otg_module_params.
32949 + host_channels);
32950 + }
32951 + if (dwc_otg_module_params.dev_endpoints != -1) {
32952 + retval +=
32953 + dwc_otg_set_param_dev_endpoints(core_if,
32954 + dwc_otg_module_params.
32955 + dev_endpoints);
32956 + }
32957 + if (dwc_otg_module_params.phy_type != -1) {
32958 + retval +=
32959 + dwc_otg_set_param_phy_type(core_if,
32960 + dwc_otg_module_params.phy_type);
32961 + }
32962 + if (dwc_otg_module_params.speed != -1) {
32963 + retval +=
32964 + dwc_otg_set_param_speed(core_if,
32965 + dwc_otg_module_params.speed);
32966 + }
32967 + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
32968 + retval +=
32969 + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
32970 + dwc_otg_module_params.
32971 + host_ls_low_power_phy_clk);
32972 + }
32973 + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
32974 + retval +=
32975 + dwc_otg_set_param_phy_ulpi_ddr(core_if,
32976 + dwc_otg_module_params.
32977 + phy_ulpi_ddr);
32978 + }
32979 + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
32980 + retval +=
32981 + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
32982 + dwc_otg_module_params.
32983 + phy_ulpi_ext_vbus);
32984 + }
32985 + if (dwc_otg_module_params.phy_utmi_width != -1) {
32986 + retval +=
32987 + dwc_otg_set_param_phy_utmi_width(core_if,
32988 + dwc_otg_module_params.
32989 + phy_utmi_width);
32990 + }
32991 + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
32992 + retval +=
32993 + dwc_otg_set_param_ulpi_fs_ls(core_if,
32994 + dwc_otg_module_params.ulpi_fs_ls);
32995 + }
32996 + if (dwc_otg_module_params.ts_dline != -1) {
32997 + retval +=
32998 + dwc_otg_set_param_ts_dline(core_if,
32999 + dwc_otg_module_params.ts_dline);
33000 + }
33001 + if (dwc_otg_module_params.i2c_enable != -1) {
33002 + retval +=
33003 + dwc_otg_set_param_i2c_enable(core_if,
33004 + dwc_otg_module_params.
33005 + i2c_enable);
33006 + }
33007 + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
33008 + retval +=
33009 + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
33010 + dwc_otg_module_params.
33011 + en_multiple_tx_fifo);
33012 + }
33013 + for (i = 0; i < 15; i++) {
33014 + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
33015 + retval +=
33016 + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
33017 + dwc_otg_module_params.
33018 + dev_perio_tx_fifo_size
33019 + [i], i);
33020 + }
33021 + }
33022 +
33023 + for (i = 0; i < 15; i++) {
33024 + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
33025 + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
33026 + dwc_otg_module_params.
33027 + dev_tx_fifo_size
33028 + [i], i);
33029 + }
33030 + }
33031 + if (dwc_otg_module_params.thr_ctl != -1) {
33032 + retval +=
33033 + dwc_otg_set_param_thr_ctl(core_if,
33034 + dwc_otg_module_params.thr_ctl);
33035 + }
33036 + if (dwc_otg_module_params.mpi_enable != -1) {
33037 + retval +=
33038 + dwc_otg_set_param_mpi_enable(core_if,
33039 + dwc_otg_module_params.
33040 + mpi_enable);
33041 + }
33042 + if (dwc_otg_module_params.pti_enable != -1) {
33043 + retval +=
33044 + dwc_otg_set_param_pti_enable(core_if,
33045 + dwc_otg_module_params.
33046 + pti_enable);
33047 + }
33048 + if (dwc_otg_module_params.lpm_enable != -1) {
33049 + retval +=
33050 + dwc_otg_set_param_lpm_enable(core_if,
33051 + dwc_otg_module_params.
33052 + lpm_enable);
33053 + }
33054 + if (dwc_otg_module_params.ic_usb_cap != -1) {
33055 + retval +=
33056 + dwc_otg_set_param_ic_usb_cap(core_if,
33057 + dwc_otg_module_params.
33058 + ic_usb_cap);
33059 + }
33060 + if (dwc_otg_module_params.tx_thr_length != -1) {
33061 + retval +=
33062 + dwc_otg_set_param_tx_thr_length(core_if,
33063 + dwc_otg_module_params.tx_thr_length);
33064 + }
33065 + if (dwc_otg_module_params.rx_thr_length != -1) {
33066 + retval +=
33067 + dwc_otg_set_param_rx_thr_length(core_if,
33068 + dwc_otg_module_params.
33069 + rx_thr_length);
33070 + }
33071 + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
33072 + retval +=
33073 + dwc_otg_set_param_ahb_thr_ratio(core_if,
33074 + dwc_otg_module_params.ahb_thr_ratio);
33075 + }
33076 + if (dwc_otg_module_params.power_down != -1) {
33077 + retval +=
33078 + dwc_otg_set_param_power_down(core_if,
33079 + dwc_otg_module_params.power_down);
33080 + }
33081 + if (dwc_otg_module_params.reload_ctl != -1) {
33082 + retval +=
33083 + dwc_otg_set_param_reload_ctl(core_if,
33084 + dwc_otg_module_params.reload_ctl);
33085 + }
33086 +
33087 + if (dwc_otg_module_params.dev_out_nak != -1) {
33088 + retval +=
33089 + dwc_otg_set_param_dev_out_nak(core_if,
33090 + dwc_otg_module_params.dev_out_nak);
33091 + }
33092 +
33093 + if (dwc_otg_module_params.cont_on_bna != -1) {
33094 + retval +=
33095 + dwc_otg_set_param_cont_on_bna(core_if,
33096 + dwc_otg_module_params.cont_on_bna);
33097 + }
33098 +
33099 + if (dwc_otg_module_params.ahb_single != -1) {
33100 + retval +=
33101 + dwc_otg_set_param_ahb_single(core_if,
33102 + dwc_otg_module_params.ahb_single);
33103 + }
33104 +
33105 + if (dwc_otg_module_params.otg_ver != -1) {
33106 + retval +=
33107 + dwc_otg_set_param_otg_ver(core_if,
33108 + dwc_otg_module_params.otg_ver);
33109 + }
33110 + if (dwc_otg_module_params.adp_enable != -1) {
33111 + retval +=
33112 + dwc_otg_set_param_adp_enable(core_if,
33113 + dwc_otg_module_params.
33114 + adp_enable);
33115 + }
33116 + return retval;
33117 +}
33118 +
33119 +/**
33120 + * This function is the top level interrupt handler for the Common
33121 + * (Device and host modes) interrupts.
33122 + */
33123 +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
33124 +{
33125 + int32_t retval = IRQ_NONE;
33126 +
33127 + retval = dwc_otg_handle_common_intr(dev);
33128 + if (retval != 0) {
33129 + S3C2410X_CLEAR_EINTPEND();
33130 + }
33131 + return IRQ_RETVAL(retval);
33132 +}
33133 +
33134 +/**
33135 + * This function is called when a lm_device is unregistered with the
33136 + * dwc_otg_driver. This happens, for example, when the rmmod command is
33137 + * executed. The device may or may not be electrically present. If it is
33138 + * present, the driver stops device processing. Any resources used on behalf
33139 + * of this device are freed.
33140 + *
33141 + * @param _dev
33142 + */
33143 +#ifdef LM_INTERFACE
33144 +#define REM_RETVAL(n)
33145 +static void dwc_otg_driver_remove( struct lm_device *_dev )
33146 +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
33147 +#elif defined(PCI_INTERFACE)
33148 +#define REM_RETVAL(n)
33149 +static void dwc_otg_driver_remove( struct pci_dev *_dev )
33150 +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
33151 +#elif defined(PLATFORM_INTERFACE)
33152 +#define REM_RETVAL(n) n
33153 +static int dwc_otg_driver_remove( struct platform_device *_dev )
33154 +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
33155 +#endif
33156 +
33157 + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
33158 +
33159 + if (!otg_dev) {
33160 + /* Memory allocation for the dwc_otg_device failed. */
33161 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
33162 + return REM_RETVAL(-ENOMEM);
33163 + }
33164 +#ifndef DWC_DEVICE_ONLY
33165 + if (otg_dev->hcd) {
33166 + hcd_remove(_dev);
33167 + } else {
33168 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
33169 + return REM_RETVAL(-EINVAL);
33170 + }
33171 +#endif
33172 +
33173 +#ifndef DWC_HOST_ONLY
33174 + if (otg_dev->pcd) {
33175 + pcd_remove(_dev);
33176 + } else {
33177 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
33178 + return REM_RETVAL(-EINVAL);
33179 + }
33180 +#endif
33181 + /*
33182 + * Free the IRQ
33183 + */
33184 + if (otg_dev->common_irq_installed) {
33185 +#ifdef PLATFORM_INTERFACE
33186 + free_irq(platform_get_irq(_dev, 0), otg_dev);
33187 +#else
33188 + free_irq(_dev->irq, otg_dev);
33189 +#endif
33190 + } else {
33191 + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
33192 + return REM_RETVAL(-ENXIO);
33193 + }
33194 +
33195 + if (otg_dev->core_if) {
33196 + dwc_otg_cil_remove(otg_dev->core_if);
33197 + } else {
33198 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
33199 + return REM_RETVAL(-ENXIO);
33200 + }
33201 +
33202 + /*
33203 + * Remove the device attributes
33204 + */
33205 + dwc_otg_attr_remove(_dev);
33206 +
33207 + /*
33208 + * Return the memory.
33209 + */
33210 + if (otg_dev->os_dep.base) {
33211 + iounmap(otg_dev->os_dep.base);
33212 + }
33213 + DWC_FREE(otg_dev);
33214 +
33215 + /*
33216 + * Clear the drvdata pointer.
33217 + */
33218 +#ifdef LM_INTERFACE
33219 + lm_set_drvdata(_dev, 0);
33220 +#elif defined(PCI_INTERFACE)
33221 + release_mem_region(otg_dev->os_dep.rsrc_start,
33222 + otg_dev->os_dep.rsrc_len);
33223 + pci_set_drvdata(_dev, 0);
33224 +#elif defined(PLATFORM_INTERFACE)
33225 + platform_set_drvdata(_dev, 0);
33226 +#endif
33227 + return REM_RETVAL(0);
33228 +}
33229 +
33230 +/**
33231 + * This function is called when an lm_device is bound to a
33232 + * dwc_otg_driver. It creates the driver components required to
33233 + * control the device (CIL, HCD, and PCD) and it initializes the
33234 + * device. The driver components are stored in a dwc_otg_device
33235 + * structure. A reference to the dwc_otg_device is saved in the
33236 + * lm_device. This allows the driver to access the dwc_otg_device
33237 + * structure on subsequent calls to driver methods for this device.
33238 + *
33239 + * @param _dev Bus device
33240 + */
33241 +static int dwc_otg_driver_probe(
33242 +#ifdef LM_INTERFACE
33243 + struct lm_device *_dev
33244 +#elif defined(PCI_INTERFACE)
33245 + struct pci_dev *_dev,
33246 + const struct pci_device_id *id
33247 +#elif defined(PLATFORM_INTERFACE)
33248 + struct platform_device *_dev
33249 +#endif
33250 + )
33251 +{
33252 + int retval = 0;
33253 + dwc_otg_device_t *dwc_otg_device;
33254 + int devirq;
33255 +
33256 + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
33257 +#ifdef LM_INTERFACE
33258 + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
33259 +#elif defined(PCI_INTERFACE)
33260 + if (!id) {
33261 + DWC_ERROR("Invalid pci_device_id %p", id);
33262 + return -EINVAL;
33263 + }
33264 +
33265 + if (!_dev || (pci_enable_device(_dev) < 0)) {
33266 + DWC_ERROR("Invalid pci_device %p", _dev);
33267 + return -ENODEV;
33268 + }
33269 + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
33270 + /* other stuff needed as well? */
33271 +
33272 +#elif defined(PLATFORM_INTERFACE)
33273 + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
33274 + (unsigned)_dev->resource->start,
33275 + (unsigned)(_dev->resource->end - _dev->resource->start));
33276 +#endif
33277 +
33278 + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
33279 +
33280 + if (!dwc_otg_device) {
33281 + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
33282 + return -ENOMEM;
33283 + }
33284 +
33285 + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
33286 + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
33287 + dwc_otg_device->os_dep.platformdev = _dev;
33288 +
33289 + /*
33290 + * Map the DWC_otg Core memory into virtual address space.
33291 + */
33292 +#ifdef LM_INTERFACE
33293 + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
33294 +
33295 + if (!dwc_otg_device->os_dep.base) {
33296 + dev_err(&_dev->dev, "ioremap() failed\n");
33297 + DWC_FREE(dwc_otg_device);
33298 + return -ENOMEM;
33299 + }
33300 + dev_dbg(&_dev->dev, "base=0x%08x\n",
33301 + (unsigned)dwc_otg_device->os_dep.base);
33302 +#elif defined(PCI_INTERFACE)
33303 + _dev->current_state = PCI_D0;
33304 + _dev->dev.power.power_state = PMSG_ON;
33305 +
33306 + if (!_dev->irq) {
33307 + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
33308 + pci_name(_dev));
33309 + iounmap(dwc_otg_device->os_dep.base);
33310 + DWC_FREE(dwc_otg_device);
33311 + return -ENODEV;
33312 + }
33313 +
33314 + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
33315 + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
33316 + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
33317 + (unsigned)dwc_otg_device->os_dep.rsrc_start,
33318 + (unsigned)dwc_otg_device->os_dep.rsrc_len);
33319 + if (!request_mem_region
33320 + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
33321 + "dwc_otg")) {
33322 + dev_dbg(&_dev->dev, "error requesting memory\n");
33323 + iounmap(dwc_otg_device->os_dep.base);
33324 + DWC_FREE(dwc_otg_device);
33325 + return -EFAULT;
33326 + }
33327 +
33328 + dwc_otg_device->os_dep.base =
33329 + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
33330 + dwc_otg_device->os_dep.rsrc_len);
33331 + if (dwc_otg_device->os_dep.base == NULL) {
33332 + dev_dbg(&_dev->dev, "error mapping memory\n");
33333 + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
33334 + dwc_otg_device->os_dep.rsrc_len);
33335 + iounmap(dwc_otg_device->os_dep.base);
33336 + DWC_FREE(dwc_otg_device);
33337 + return -EFAULT;
33338 + }
33339 + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
33340 + dwc_otg_device->os_dep.base);
33341 + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
33342 + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
33343 + dwc_otg_device->os_dep.base);
33344 + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
33345 + (unsigned)dwc_otg_device->os_dep.rsrc_start,
33346 + dwc_otg_device->os_dep.base);
33347 +
33348 + pci_set_master(_dev);
33349 + pci_set_drvdata(_dev, dwc_otg_device);
33350 +#elif defined(PLATFORM_INTERFACE)
33351 + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
33352 + _dev->resource->start,
33353 + _dev->resource->end - _dev->resource->start + 1);
33354 +#if 1
33355 + if (!request_mem_region(_dev->resource[0].start,
33356 + _dev->resource[0].end - _dev->resource[0].start + 1,
33357 + "dwc_otg")) {
33358 + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
33359 + retval = -EFAULT;
33360 + goto fail;
33361 + }
33362 +
33363 + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
33364 + _dev->resource[0].end -
33365 + _dev->resource[0].start+1);
33366 + if (fiq_enable)
33367 + {
33368 + if (!request_mem_region(_dev->resource[1].start,
33369 + _dev->resource[1].end - _dev->resource[1].start + 1,
33370 + "dwc_otg")) {
33371 + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
33372 + retval = -EFAULT;
33373 + goto fail;
33374 + }
33375 +
33376 + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
33377 + _dev->resource[1].end -
33378 + _dev->resource[1].start + 1);
33379 + }
33380 +
33381 +#else
33382 + {
33383 + struct map_desc desc = {
33384 + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
33385 + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
33386 + .length = SZ_128K,
33387 + .type = MT_DEVICE
33388 + };
33389 + iotable_init(&desc, 1);
33390 + dwc_otg_device->os_dep.base = (void *)desc.virtual;
33391 + }
33392 +#endif
33393 + if (!dwc_otg_device->os_dep.base) {
33394 + dev_err(&_dev->dev, "ioremap() failed\n");
33395 + retval = -ENOMEM;
33396 + goto fail;
33397 + }
33398 + dev_dbg(&_dev->dev, "base=0x%08x\n",
33399 + (unsigned)dwc_otg_device->os_dep.base);
33400 +#endif
33401 +
33402 + /*
33403 + * Initialize driver data to point to the global DWC_otg
33404 + * Device structure.
33405 + */
33406 +#ifdef LM_INTERFACE
33407 + lm_set_drvdata(_dev, dwc_otg_device);
33408 +#elif defined(PLATFORM_INTERFACE)
33409 + platform_set_drvdata(_dev, dwc_otg_device);
33410 +#endif
33411 + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
33412 +
33413 + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
33414 + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
33415 + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
33416 +
33417 + if (!dwc_otg_device->core_if) {
33418 + dev_err(&_dev->dev, "CIL initialization failed!\n");
33419 + retval = -ENOMEM;
33420 + goto fail;
33421 + }
33422 +
33423 + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
33424 + /*
33425 + * Attempt to ensure this device is really a DWC_otg Controller.
33426 + * Read and verify the SNPSID register contents. The value should be
33427 + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
33428 + * as in "OTG version 2.XX" or "OTG version 3.XX".
33429 + */
33430 +
33431 + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
33432 + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
33433 + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
33434 + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
33435 + retval = -EINVAL;
33436 + goto fail;
33437 + }
33438 +
33439 + /*
33440 + * Validate parameter values.
33441 + */
33442 + dev_dbg(&_dev->dev, "Calling set_parameters\n");
33443 + if (set_parameters(dwc_otg_device->core_if)) {
33444 + retval = -EINVAL;
33445 + goto fail;
33446 + }
33447 +
33448 + /*
33449 + * Create Device Attributes in sysfs
33450 + */
33451 + dev_dbg(&_dev->dev, "Calling attr_create\n");
33452 + dwc_otg_attr_create(_dev);
33453 +
33454 + /*
33455 + * Disable the global interrupt until all the interrupt
33456 + * handlers are installed.
33457 + */
33458 + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
33459 + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
33460 +
33461 + /*
33462 + * Install the interrupt handler for the common interrupts before
33463 + * enabling common interrupts in core_init below.
33464 + */
33465 +
33466 +#if defined(PLATFORM_INTERFACE)
33467 + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
33468 +#else
33469 + devirq = _dev->irq;
33470 +#endif
33471 + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
33472 + devirq);
33473 + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
33474 + retval = request_irq(devirq, dwc_otg_common_irq,
33475 + IRQF_SHARED,
33476 + "dwc_otg", dwc_otg_device);
33477 + if (retval) {
33478 + DWC_ERROR("request of irq%d failed\n", devirq);
33479 + retval = -EBUSY;
33480 + goto fail;
33481 + } else {
33482 + dwc_otg_device->common_irq_installed = 1;
33483 + }
33484 +
33485 +#ifndef IRQF_TRIGGER_LOW
33486 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
33487 + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
33488 + set_irq_type(devirq,
33489 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
33490 + IRQT_LOW
33491 +#else
33492 + IRQ_TYPE_LEVEL_LOW
33493 +#endif
33494 + );
33495 +#endif
33496 +#endif /*IRQF_TRIGGER_LOW*/
33497 +
33498 + /*
33499 + * Initialize the DWC_otg core.
33500 + */
33501 + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
33502 + dwc_otg_core_init(dwc_otg_device->core_if);
33503 +
33504 +#ifndef DWC_HOST_ONLY
33505 + /*
33506 + * Initialize the PCD
33507 + */
33508 + dev_dbg(&_dev->dev, "Calling pcd_init\n");
33509 + retval = pcd_init(_dev);
33510 + if (retval != 0) {
33511 + DWC_ERROR("pcd_init failed\n");
33512 + dwc_otg_device->pcd = NULL;
33513 + goto fail;
33514 + }
33515 +#endif
33516 +#ifndef DWC_DEVICE_ONLY
33517 + /*
33518 + * Initialize the HCD
33519 + */
33520 + dev_dbg(&_dev->dev, "Calling hcd_init\n");
33521 + retval = hcd_init(_dev);
33522 + if (retval != 0) {
33523 + DWC_ERROR("hcd_init failed\n");
33524 + dwc_otg_device->hcd = NULL;
33525 + goto fail;
33526 + }
33527 +#endif
33528 + /* Recover from drvdata having been overwritten by hcd_init() */
33529 +#ifdef LM_INTERFACE
33530 + lm_set_drvdata(_dev, dwc_otg_device);
33531 +#elif defined(PLATFORM_INTERFACE)
33532 + platform_set_drvdata(_dev, dwc_otg_device);
33533 +#elif defined(PCI_INTERFACE)
33534 + pci_set_drvdata(_dev, dwc_otg_device);
33535 + dwc_otg_device->os_dep.pcidev = _dev;
33536 +#endif
33537 +
33538 + /*
33539 + * Enable the global interrupt after all the interrupt
33540 + * handlers are installed if there is no ADP support else
33541 + * perform initial actions required for Internal ADP logic.
33542 + */
33543 + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
33544 + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
33545 + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
33546 + dev_dbg(&_dev->dev, "Done\n");
33547 + } else
33548 + dwc_otg_adp_start(dwc_otg_device->core_if,
33549 + dwc_otg_is_host_mode(dwc_otg_device->core_if));
33550 +
33551 + return 0;
33552 +
33553 +fail:
33554 + dwc_otg_driver_remove(_dev);
33555 + return retval;
33556 +}
33557 +
33558 +/**
33559 + * This structure defines the methods to be called by a bus driver
33560 + * during the lifecycle of a device on that bus. Both drivers and
33561 + * devices are registered with a bus driver. The bus driver matches
33562 + * devices to drivers based on information in the device and driver
33563 + * structures.
33564 + *
33565 + * The probe function is called when the bus driver matches a device
33566 + * to this driver. The remove function is called when a device is
33567 + * unregistered with the bus driver.
33568 + */
33569 +#ifdef LM_INTERFACE
33570 +static struct lm_driver dwc_otg_driver = {
33571 + .drv = {.name = (char *)dwc_driver_name,},
33572 + .probe = dwc_otg_driver_probe,
33573 + .remove = dwc_otg_driver_remove,
33574 + // 'suspend' and 'resume' absent
33575 +};
33576 +#elif defined(PCI_INTERFACE)
33577 +static const struct pci_device_id pci_ids[] = { {
33578 + PCI_DEVICE(0x16c3, 0xabcd),
33579 + .driver_data =
33580 + (unsigned long)0xdeadbeef,
33581 + }, { /* end: all zeroes */ }
33582 +};
33583 +
33584 +MODULE_DEVICE_TABLE(pci, pci_ids);
33585 +
33586 +/* pci driver glue; this is a "new style" PCI driver module */
33587 +static struct pci_driver dwc_otg_driver = {
33588 + .name = "dwc_otg",
33589 + .id_table = pci_ids,
33590 +
33591 + .probe = dwc_otg_driver_probe,
33592 + .remove = dwc_otg_driver_remove,
33593 +
33594 + .driver = {
33595 + .name = (char *)dwc_driver_name,
33596 + },
33597 +};
33598 +#elif defined(PLATFORM_INTERFACE)
33599 +static struct platform_device_id platform_ids[] = {
33600 + {
33601 + .name = "bcm2708_usb",
33602 + .driver_data = (kernel_ulong_t) 0xdeadbeef,
33603 + },
33604 + { /* end: all zeroes */ }
33605 +};
33606 +MODULE_DEVICE_TABLE(platform, platform_ids);
33607 +
33608 +static const struct of_device_id dwc_otg_of_match_table[] = {
33609 + { .compatible = "brcm,bcm2708-usb", },
33610 + {},
33611 +};
33612 +MODULE_DEVICE_TABLE(of, dwc_otg_of_match_table);
33613 +
33614 +static struct platform_driver dwc_otg_driver = {
33615 + .driver = {
33616 + .name = (char *)dwc_driver_name,
33617 + .of_match_table = dwc_otg_of_match_table,
33618 + },
33619 + .id_table = platform_ids,
33620 +
33621 + .probe = dwc_otg_driver_probe,
33622 + .remove = dwc_otg_driver_remove,
33623 + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
33624 +};
33625 +#endif
33626 +
33627 +/**
33628 + * This function is called when the dwc_otg_driver is installed with the
33629 + * insmod command. It registers the dwc_otg_driver structure with the
33630 + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
33631 + * to be called. In addition, the bus driver will automatically expose
33632 + * attributes defined for the device and driver in the special sysfs file
33633 + * system.
33634 + *
33635 + * @return
33636 + */
33637 +static int __init dwc_otg_driver_init(void)
33638 +{
33639 + int retval = 0;
33640 + int error;
33641 + struct device_driver *drv;
33642 +
33643 + if(fiq_fsm_enable && !fiq_enable) {
33644 + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
33645 + fiq_enable = 1;
33646 + }
33647 +
33648 + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
33649 + DWC_DRIVER_VERSION,
33650 +#ifdef LM_INTERFACE
33651 + "logicmodule");
33652 + retval = lm_driver_register(&dwc_otg_driver);
33653 + drv = &dwc_otg_driver.drv;
33654 +#elif defined(PCI_INTERFACE)
33655 + "pci");
33656 + retval = pci_register_driver(&dwc_otg_driver);
33657 + drv = &dwc_otg_driver.driver;
33658 +#elif defined(PLATFORM_INTERFACE)
33659 + "platform");
33660 + retval = platform_driver_register(&dwc_otg_driver);
33661 + drv = &dwc_otg_driver.driver;
33662 +#endif
33663 + if (retval < 0) {
33664 + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
33665 + return retval;
33666 + }
33667 + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
33668 + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
33669 + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
33670 +
33671 + error = driver_create_file(drv, &driver_attr_version);
33672 +#ifdef DEBUG
33673 + error = driver_create_file(drv, &driver_attr_debuglevel);
33674 +#endif
33675 + return retval;
33676 +}
33677 +
33678 +module_init(dwc_otg_driver_init);
33679 +
33680 +/**
33681 + * This function is called when the driver is removed from the kernel
33682 + * with the rmmod command. The driver unregisters itself with its bus
33683 + * driver.
33684 + *
33685 + */
33686 +static void __exit dwc_otg_driver_cleanup(void)
33687 +{
33688 + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
33689 +
33690 +#ifdef LM_INTERFACE
33691 + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
33692 + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
33693 + lm_driver_unregister(&dwc_otg_driver);
33694 +#elif defined(PCI_INTERFACE)
33695 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
33696 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
33697 + pci_unregister_driver(&dwc_otg_driver);
33698 +#elif defined(PLATFORM_INTERFACE)
33699 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
33700 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
33701 + platform_driver_unregister(&dwc_otg_driver);
33702 +#endif
33703 +
33704 + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
33705 +}
33706 +
33707 +module_exit(dwc_otg_driver_cleanup);
33708 +
33709 +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
33710 +MODULE_AUTHOR("Synopsys Inc.");
33711 +MODULE_LICENSE("GPL");
33712 +
33713 +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
33714 +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
33715 +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
33716 +MODULE_PARM_DESC(opt, "OPT Mode");
33717 +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
33718 +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
33719 +
33720 +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
33721 + 0444);
33722 +MODULE_PARM_DESC(dma_desc_enable,
33723 + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
33724 +
33725 +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
33726 + 0444);
33727 +MODULE_PARM_DESC(dma_burst_size,
33728 + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
33729 +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
33730 +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
33731 +module_param_named(host_support_fs_ls_low_power,
33732 + dwc_otg_module_params.host_support_fs_ls_low_power, int,
33733 + 0444);
33734 +MODULE_PARM_DESC(host_support_fs_ls_low_power,
33735 + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
33736 +module_param_named(host_ls_low_power_phy_clk,
33737 + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
33738 +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
33739 + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
33740 +module_param_named(enable_dynamic_fifo,
33741 + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
33742 +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
33743 +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
33744 + 0444);
33745 +MODULE_PARM_DESC(data_fifo_size,
33746 + "Total number of words in the data FIFO memory 32-32768");
33747 +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
33748 + int, 0444);
33749 +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
33750 +module_param_named(dev_nperio_tx_fifo_size,
33751 + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
33752 +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
33753 + "Number of words in the non-periodic Tx FIFO 16-32768");
33754 +module_param_named(dev_perio_tx_fifo_size_1,
33755 + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
33756 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
33757 + "Number of words in the periodic Tx FIFO 4-768");
33758 +module_param_named(dev_perio_tx_fifo_size_2,
33759 + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
33760 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
33761 + "Number of words in the periodic Tx FIFO 4-768");
33762 +module_param_named(dev_perio_tx_fifo_size_3,
33763 + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
33764 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
33765 + "Number of words in the periodic Tx FIFO 4-768");
33766 +module_param_named(dev_perio_tx_fifo_size_4,
33767 + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
33768 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
33769 + "Number of words in the periodic Tx FIFO 4-768");
33770 +module_param_named(dev_perio_tx_fifo_size_5,
33771 + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
33772 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
33773 + "Number of words in the periodic Tx FIFO 4-768");
33774 +module_param_named(dev_perio_tx_fifo_size_6,
33775 + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
33776 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
33777 + "Number of words in the periodic Tx FIFO 4-768");
33778 +module_param_named(dev_perio_tx_fifo_size_7,
33779 + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
33780 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
33781 + "Number of words in the periodic Tx FIFO 4-768");
33782 +module_param_named(dev_perio_tx_fifo_size_8,
33783 + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
33784 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
33785 + "Number of words in the periodic Tx FIFO 4-768");
33786 +module_param_named(dev_perio_tx_fifo_size_9,
33787 + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
33788 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
33789 + "Number of words in the periodic Tx FIFO 4-768");
33790 +module_param_named(dev_perio_tx_fifo_size_10,
33791 + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
33792 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
33793 + "Number of words in the periodic Tx FIFO 4-768");
33794 +module_param_named(dev_perio_tx_fifo_size_11,
33795 + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
33796 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
33797 + "Number of words in the periodic Tx FIFO 4-768");
33798 +module_param_named(dev_perio_tx_fifo_size_12,
33799 + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
33800 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
33801 + "Number of words in the periodic Tx FIFO 4-768");
33802 +module_param_named(dev_perio_tx_fifo_size_13,
33803 + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
33804 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
33805 + "Number of words in the periodic Tx FIFO 4-768");
33806 +module_param_named(dev_perio_tx_fifo_size_14,
33807 + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
33808 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
33809 + "Number of words in the periodic Tx FIFO 4-768");
33810 +module_param_named(dev_perio_tx_fifo_size_15,
33811 + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
33812 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
33813 + "Number of words in the periodic Tx FIFO 4-768");
33814 +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
33815 + int, 0444);
33816 +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
33817 +module_param_named(host_nperio_tx_fifo_size,
33818 + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
33819 +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
33820 + "Number of words in the non-periodic Tx FIFO 16-32768");
33821 +module_param_named(host_perio_tx_fifo_size,
33822 + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
33823 +MODULE_PARM_DESC(host_perio_tx_fifo_size,
33824 + "Number of words in the host periodic Tx FIFO 16-32768");
33825 +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
33826 + int, 0444);
33827 +/** @todo Set the max to 512K, modify checks */
33828 +MODULE_PARM_DESC(max_transfer_size,
33829 + "The maximum transfer size supported in bytes 2047-65535");
33830 +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
33831 + int, 0444);
33832 +MODULE_PARM_DESC(max_packet_count,
33833 + "The maximum number of packets in a transfer 15-511");
33834 +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
33835 + 0444);
33836 +MODULE_PARM_DESC(host_channels,
33837 + "The number of host channel registers to use 1-16");
33838 +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
33839 + 0444);
33840 +MODULE_PARM_DESC(dev_endpoints,
33841 + "The number of endpoints in addition to EP0 available for device mode 1-15");
33842 +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
33843 +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
33844 +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
33845 + 0444);
33846 +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
33847 +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
33848 +MODULE_PARM_DESC(phy_ulpi_ddr,
33849 + "ULPI at double or single data rate 0=Single 1=Double");
33850 +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
33851 + int, 0444);
33852 +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
33853 + "ULPI PHY using internal or external vbus 0=Internal");
33854 +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
33855 +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
33856 +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
33857 +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
33858 +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
33859 +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
33860 +module_param_named(debug, g_dbg_lvl, int, 0444);
33861 +MODULE_PARM_DESC(debug, "");
33862 +
33863 +module_param_named(en_multiple_tx_fifo,
33864 + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
33865 +MODULE_PARM_DESC(en_multiple_tx_fifo,
33866 + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
33867 +module_param_named(dev_tx_fifo_size_1,
33868 + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
33869 +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
33870 +module_param_named(dev_tx_fifo_size_2,
33871 + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
33872 +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
33873 +module_param_named(dev_tx_fifo_size_3,
33874 + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
33875 +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
33876 +module_param_named(dev_tx_fifo_size_4,
33877 + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
33878 +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
33879 +module_param_named(dev_tx_fifo_size_5,
33880 + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
33881 +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
33882 +module_param_named(dev_tx_fifo_size_6,
33883 + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
33884 +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
33885 +module_param_named(dev_tx_fifo_size_7,
33886 + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
33887 +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
33888 +module_param_named(dev_tx_fifo_size_8,
33889 + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
33890 +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
33891 +module_param_named(dev_tx_fifo_size_9,
33892 + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
33893 +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
33894 +module_param_named(dev_tx_fifo_size_10,
33895 + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
33896 +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
33897 +module_param_named(dev_tx_fifo_size_11,
33898 + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
33899 +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
33900 +module_param_named(dev_tx_fifo_size_12,
33901 + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
33902 +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
33903 +module_param_named(dev_tx_fifo_size_13,
33904 + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
33905 +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
33906 +module_param_named(dev_tx_fifo_size_14,
33907 + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
33908 +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
33909 +module_param_named(dev_tx_fifo_size_15,
33910 + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
33911 +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
33912 +
33913 +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
33914 +MODULE_PARM_DESC(thr_ctl,
33915 + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
33916 +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
33917 + 0444);
33918 +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
33919 +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
33920 + 0444);
33921 +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
33922 +
33923 +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
33924 +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
33925 +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
33926 +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
33927 +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
33928 +MODULE_PARM_DESC(ic_usb_cap,
33929 + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
33930 +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
33931 + 0444);
33932 +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
33933 +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
33934 +MODULE_PARM_DESC(power_down, "Power Down Mode");
33935 +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
33936 +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
33937 +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
33938 +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
33939 +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
33940 +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
33941 +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
33942 +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
33943 +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
33944 +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
33945 +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
33946 +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
33947 +module_param(microframe_schedule, bool, 0444);
33948 +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
33949 +
33950 +module_param(fiq_enable, bool, 0444);
33951 +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
33952 +module_param(nak_holdoff, ushort, 0644);
33953 +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
33954 +module_param(fiq_fsm_enable, bool, 0444);
33955 +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
33956 +module_param(fiq_fsm_mask, ushort, 0444);
33957 +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
33958 + "Bit 0 : Non-periodic split transactions\n"
33959 + "Bit 1 : Periodic split transactions\n"
33960 + "Bit 2 : High-speed multi-transfer isochronous\n"
33961 + "All other bits should be set 0.");
33962 +
33963 +
33964 +/** @page "Module Parameters"
33965 + *
33966 + * The following parameters may be specified when starting the module.
33967 + * These parameters define how the DWC_otg controller should be
33968 + * configured. Parameter values are passed to the CIL initialization
33969 + * function dwc_otg_cil_init
33970 + *
33971 + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
33972 + *
33973 +
33974 + <table>
33975 + <tr><td>Parameter Name</td><td>Meaning</td></tr>
33976 +
33977 + <tr>
33978 + <td>otg_cap</td>
33979 + <td>Specifies the OTG capabilities. The driver will automatically detect the
33980 + value for this parameter if none is specified.
33981 + - 0: HNP and SRP capable (default, if available)
33982 + - 1: SRP Only capable
33983 + - 2: No HNP/SRP capable
33984 + </td></tr>
33985 +
33986 + <tr>
33987 + <td>dma_enable</td>
33988 + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
33989 + The driver will automatically detect the value for this parameter if none is
33990 + specified.
33991 + - 0: Slave
33992 + - 1: DMA (default, if available)
33993 + </td></tr>
33994 +
33995 + <tr>
33996 + <td>dma_burst_size</td>
33997 + <td>The DMA Burst size (applicable only for External DMA Mode).
33998 + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
33999 + </td></tr>
34000 +
34001 + <tr>
34002 + <td>speed</td>
34003 + <td>Specifies the maximum speed of operation in host and device mode. The
34004 + actual speed depends on the speed of the attached device and the value of
34005 + phy_type.
34006 + - 0: High Speed (default)
34007 + - 1: Full Speed
34008 + </td></tr>
34009 +
34010 + <tr>
34011 + <td>host_support_fs_ls_low_power</td>
34012 + <td>Specifies whether low power mode is supported when attached to a Full
34013 + Speed or Low Speed device in host mode.
34014 + - 0: Don't support low power mode (default)
34015 + - 1: Support low power mode
34016 + </td></tr>
34017 +
34018 + <tr>
34019 + <td>host_ls_low_power_phy_clk</td>
34020 + <td>Specifies the PHY clock rate in low power mode when connected to a Low
34021 + Speed device in host mode. This parameter is applicable only if
34022 + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
34023 + - 0: 48 MHz (default)
34024 + - 1: 6 MHz
34025 + </td></tr>
34026 +
34027 + <tr>
34028 + <td>enable_dynamic_fifo</td>
34029 + <td> Specifies whether FIFOs may be resized by the driver software.
34030 + - 0: Use cC FIFO size parameters
34031 + - 1: Allow dynamic FIFO sizing (default)
34032 + </td></tr>
34033 +
34034 + <tr>
34035 + <td>data_fifo_size</td>
34036 + <td>Total number of 4-byte words in the data FIFO memory. This memory
34037 + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
34038 + - Values: 32 to 32768 (default 8192)
34039 +
34040 + Note: The total FIFO memory depth in the FPGA configuration is 8192.
34041 + </td></tr>
34042 +
34043 + <tr>
34044 + <td>dev_rx_fifo_size</td>
34045 + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
34046 + FIFO sizing is enabled.
34047 + - Values: 16 to 32768 (default 1064)
34048 + </td></tr>
34049 +
34050 + <tr>
34051 + <td>dev_nperio_tx_fifo_size</td>
34052 + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
34053 + dynamic FIFO sizing is enabled.
34054 + - Values: 16 to 32768 (default 1024)
34055 + </td></tr>
34056 +
34057 + <tr>
34058 + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
34059 + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
34060 + when dynamic FIFO sizing is enabled.
34061 + - Values: 4 to 768 (default 256)
34062 + </td></tr>
34063 +
34064 + <tr>
34065 + <td>host_rx_fifo_size</td>
34066 + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
34067 + sizing is enabled.
34068 + - Values: 16 to 32768 (default 1024)
34069 + </td></tr>
34070 +
34071 + <tr>
34072 + <td>host_nperio_tx_fifo_size</td>
34073 + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
34074 + dynamic FIFO sizing is enabled in the core.
34075 + - Values: 16 to 32768 (default 1024)
34076 + </td></tr>
34077 +
34078 + <tr>
34079 + <td>host_perio_tx_fifo_size</td>
34080 + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
34081 + sizing is enabled.
34082 + - Values: 16 to 32768 (default 1024)
34083 + </td></tr>
34084 +
34085 + <tr>
34086 + <td>max_transfer_size</td>
34087 + <td>The maximum transfer size supported in bytes.
34088 + - Values: 2047 to 65,535 (default 65,535)
34089 + </td></tr>
34090 +
34091 + <tr>
34092 + <td>max_packet_count</td>
34093 + <td>The maximum number of packets in a transfer.
34094 + - Values: 15 to 511 (default 511)
34095 + </td></tr>
34096 +
34097 + <tr>
34098 + <td>host_channels</td>
34099 + <td>The number of host channel registers to use.
34100 + - Values: 1 to 16 (default 12)
34101 +
34102 + Note: The FPGA configuration supports a maximum of 12 host channels.
34103 + </td></tr>
34104 +
34105 + <tr>
34106 + <td>dev_endpoints</td>
34107 + <td>The number of endpoints in addition to EP0 available for device mode
34108 + operations.
34109 + - Values: 1 to 15 (default 6 IN and OUT)
34110 +
34111 + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
34112 + addition to EP0.
34113 + </td></tr>
34114 +
34115 + <tr>
34116 + <td>phy_type</td>
34117 + <td>Specifies the type of PHY interface to use. By default, the driver will
34118 + automatically detect the phy_type.
34119 + - 0: Full Speed
34120 + - 1: UTMI+ (default, if available)
34121 + - 2: ULPI
34122 + </td></tr>
34123 +
34124 + <tr>
34125 + <td>phy_utmi_width</td>
34126 + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
34127 + phy_type of UTMI+. Also, this parameter is applicable only if the
34128 + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
34129 + core has been configured to work at either data path width.
34130 + - Values: 8 or 16 bits (default 16)
34131 + </td></tr>
34132 +
34133 + <tr>
34134 + <td>phy_ulpi_ddr</td>
34135 + <td>Specifies whether the ULPI operates at double or single data rate. This
34136 + parameter is only applicable if phy_type is ULPI.
34137 + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
34138 + - 1: double data rate ULPI interface with 4 bit wide data bus
34139 + </td></tr>
34140 +
34141 + <tr>
34142 + <td>i2c_enable</td>
34143 + <td>Specifies whether to use the I2C interface for full speed PHY. This
34144 + parameter is only applicable if PHY_TYPE is FS.
34145 + - 0: Disabled (default)
34146 + - 1: Enabled
34147 + </td></tr>
34148 +
34149 + <tr>
34150 + <td>ulpi_fs_ls</td>
34151 + <td>Specifies whether to use ULPI FS/LS mode only.
34152 + - 0: Disabled (default)
34153 + - 1: Enabled
34154 + </td></tr>
34155 +
34156 + <tr>
34157 + <td>ts_dline</td>
34158 + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
34159 + - 0: Disabled (default)
34160 + - 1: Enabled
34161 + </td></tr>
34162 +
34163 + <tr>
34164 + <td>en_multiple_tx_fifo</td>
34165 + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
34166 + The driver will automatically detect the value for this parameter if none is
34167 + specified.
34168 + - 0: Disabled
34169 + - 1: Enabled (default, if available)
34170 + </td></tr>
34171 +
34172 + <tr>
34173 + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
34174 + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
34175 + when dynamic FIFO sizing is enabled.
34176 + - Values: 4 to 768 (default 256)
34177 + </td></tr>
34178 +
34179 + <tr>
34180 + <td>tx_thr_length</td>
34181 + <td>Transmit Threshold length in 32 bit double words
34182 + - Values: 8 to 128 (default 64)
34183 + </td></tr>
34184 +
34185 + <tr>
34186 + <td>rx_thr_length</td>
34187 + <td>Receive Threshold length in 32 bit double words
34188 + - Values: 8 to 128 (default 64)
34189 + </td></tr>
34190 +
34191 +<tr>
34192 + <td>thr_ctl</td>
34193 + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
34194 + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
34195 + Rx transfers accordingly.
34196 + The driver will automatically detect the value for this parameter if none is
34197 + specified.
34198 + - Values: 0 to 7 (default 0)
34199 + Bit values indicate:
34200 + - 0: Thresholding disabled
34201 + - 1: Thresholding enabled
34202 + </td></tr>
34203 +
34204 +<tr>
34205 + <td>dma_desc_enable</td>
34206 + <td>Specifies whether to enable Descriptor DMA mode.
34207 + The driver will automatically detect the value for this parameter if none is
34208 + specified.
34209 + - 0: Descriptor DMA disabled
34210 + - 1: Descriptor DMA (default, if available)
34211 + </td></tr>
34212 +
34213 +<tr>
34214 + <td>mpi_enable</td>
34215 + <td>Specifies whether to enable MPI enhancement mode.
34216 + The driver will automatically detect the value for this parameter if none is
34217 + specified.
34218 + - 0: MPI disabled (default)
34219 + - 1: MPI enable
34220 + </td></tr>
34221 +
34222 +<tr>
34223 + <td>pti_enable</td>
34224 + <td>Specifies whether to enable PTI enhancement support.
34225 + The driver will automatically detect the value for this parameter if none is
34226 + specified.
34227 + - 0: PTI disabled (default)
34228 + - 1: PTI enable
34229 + </td></tr>
34230 +
34231 +<tr>
34232 + <td>lpm_enable</td>
34233 + <td>Specifies whether to enable LPM support.
34234 + The driver will automatically detect the value for this parameter if none is
34235 + specified.
34236 + - 0: LPM disabled
34237 + - 1: LPM enable (default, if available)
34238 + </td></tr>
34239 +
34240 +<tr>
34241 + <td>ic_usb_cap</td>
34242 + <td>Specifies whether to enable IC_USB capability.
34243 + The driver will automatically detect the value for this parameter if none is
34244 + specified.
34245 + - 0: IC_USB disabled (default, if available)
34246 + - 1: IC_USB enable
34247 + </td></tr>
34248 +
34249 +<tr>
34250 + <td>ahb_thr_ratio</td>
34251 + <td>Specifies AHB Threshold ratio.
34252 + - Values: 0 to 3 (default 0)
34253 + </td></tr>
34254 +
34255 +<tr>
34256 + <td>power_down</td>
34257 + <td>Specifies Power Down(Hibernation) Mode.
34258 + The driver will automatically detect the value for this parameter if none is
34259 + specified.
34260 + - 0: Power Down disabled (default)
34261 + - 2: Power Down enabled
34262 + </td></tr>
34263 +
34264 + <tr>
34265 + <td>reload_ctl</td>
34266 + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
34267 + run time. The driver will automatically detect the value for this parameter if
34268 + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
34269 + the core might misbehave.
34270 + - 0: Reload Control disabled (default)
34271 + - 1: Reload Control enabled
34272 + </td></tr>
34273 +
34274 + <tr>
34275 + <td>dev_out_nak</td>
34276 + <td>Specifies whether Device OUT NAK enhancement enabled or no.
34277 + The driver will automatically detect the value for this parameter if
34278 + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
34279 + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
34280 + - 1: The core sets NAK after Bulk OUT transfer complete
34281 + </td></tr>
34282 +
34283 + <tr>
34284 + <td>cont_on_bna</td>
34285 + <td>Specifies whether Enable Continue on BNA enabled or no.
34286 + After receiving BNA interrupt the core disables the endpoint,when the
34287 + endpoint is re-enabled by the application the
34288 + - 0: Core starts processing from the DOEPDMA descriptor (default)
34289 + - 1: Core starts processing from the descriptor which received the BNA.
34290 + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
34291 + </td></tr>
34292 +
34293 + <tr>
34294 + <td>ahb_single</td>
34295 + <td>This bit when programmed supports SINGLE transfers for remainder data
34296 + in a transfer for DMA mode of operation.
34297 + - 0: The remainder data will be sent using INCR burst size (default)
34298 + - 1: The remainder data will be sent using SINGLE burst size.
34299 + </td></tr>
34300 +
34301 +<tr>
34302 + <td>adp_enable</td>
34303 + <td>Specifies whether ADP feature is enabled.
34304 + The driver will automatically detect the value for this parameter if none is
34305 + specified.
34306 + - 0: ADP feature disabled (default)
34307 + - 1: ADP feature enabled
34308 + </td></tr>
34309 +
34310 + <tr>
34311 + <td>otg_ver</td>
34312 + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
34313 + USB OTG device.
34314 + - 0: OTG 2.0 support disabled (default)
34315 + - 1: OTG 2.0 support enabled
34316 + </td></tr>
34317 +
34318 +*/
34319 --- /dev/null
34320 +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.h
34321 @@ -0,0 +1,86 @@
34322 +/* ==========================================================================
34323 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
34324 + * $Revision: #19 $
34325 + * $Date: 2010/11/15 $
34326 + * $Change: 1627671 $
34327 + *
34328 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
34329 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
34330 + * otherwise expressly agreed to in writing between Synopsys and you.
34331 + *
34332 + * The Software IS NOT an item of Licensed Software or Licensed Product under
34333 + * any End User Software License Agreement or Agreement for Licensed Product
34334 + * with Synopsys or any supplement thereto. You are permitted to use and
34335 + * redistribute this Software in source and binary forms, with or without
34336 + * modification, provided that redistributions of source code must retain this
34337 + * notice. You may not view, use, disclose, copy or distribute this file or
34338 + * any information contained herein except pursuant to this license grant from
34339 + * Synopsys. If you do not agree with this notice, including the disclaimer
34340 + * below, then you are not authorized to use the Software.
34341 + *
34342 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
34343 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34344 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34345 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
34346 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34347 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34348 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34349 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34350 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34351 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
34352 + * DAMAGE.
34353 + * ========================================================================== */
34354 +
34355 +#ifndef __DWC_OTG_DRIVER_H__
34356 +#define __DWC_OTG_DRIVER_H__
34357 +
34358 +/** @file
34359 + * This file contains the interface to the Linux driver.
34360 + */
34361 +#include "dwc_otg_os_dep.h"
34362 +#include "dwc_otg_core_if.h"
34363 +
34364 +/* Type declarations */
34365 +struct dwc_otg_pcd;
34366 +struct dwc_otg_hcd;
34367 +
34368 +/**
34369 + * This structure is a wrapper that encapsulates the driver components used to
34370 + * manage a single DWC_otg controller.
34371 + */
34372 +typedef struct dwc_otg_device {
34373 + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
34374 + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
34375 + * require this. */
34376 + struct os_dependent os_dep;
34377 +
34378 + /** Pointer to the core interface structure. */
34379 + dwc_otg_core_if_t *core_if;
34380 +
34381 + /** Pointer to the PCD structure. */
34382 + struct dwc_otg_pcd *pcd;
34383 +
34384 + /** Pointer to the HCD structure. */
34385 + struct dwc_otg_hcd *hcd;
34386 +
34387 + /** Flag to indicate whether the common IRQ handler is installed. */
34388 + uint8_t common_irq_installed;
34389 +
34390 +} dwc_otg_device_t;
34391 +
34392 +/*We must clear S3C24XX_EINTPEND external interrupt register
34393 + * because after clearing in this register trigerred IRQ from
34394 + * H/W core in kernel interrupt can be occured again before OTG
34395 + * handlers clear all IRQ sources of Core registers because of
34396 + * timing latencies and Low Level IRQ Type.
34397 + */
34398 +#ifdef CONFIG_MACH_IPMATE
34399 +#define S3C2410X_CLEAR_EINTPEND() \
34400 +do { \
34401 + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
34402 +} while (0)
34403 +#else
34404 +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
34405 +#endif
34406 +
34407 +#endif
34408 --- /dev/null
34409 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
34410 @@ -0,0 +1,1355 @@
34411 +/*
34412 + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
34413 + *
34414 + * Copyright (c) 2013 Raspberry Pi Foundation
34415 + *
34416 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
34417 + * All rights reserved.
34418 + *
34419 + * Redistribution and use in source and binary forms, with or without
34420 + * modification, are permitted provided that the following conditions are met:
34421 + * * Redistributions of source code must retain the above copyright
34422 + * notice, this list of conditions and the following disclaimer.
34423 + * * Redistributions in binary form must reproduce the above copyright
34424 + * notice, this list of conditions and the following disclaimer in the
34425 + * documentation and/or other materials provided with the distribution.
34426 + * * Neither the name of Raspberry Pi nor the
34427 + * names of its contributors may be used to endorse or promote products
34428 + * derived from this software without specific prior written permission.
34429 + *
34430 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34431 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34432 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34433 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
34434 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34435 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34436 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34437 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34438 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34439 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34440 + *
34441 + * This FIQ implements functionality that performs split transactions on
34442 + * the dwc_otg hardware without any outside intervention. A split transaction
34443 + * is "queued" by nominating a specific host channel to perform the entirety
34444 + * of a split transaction. This FIQ will then perform the microframe-precise
34445 + * scheduling required in each phase of the transaction until completion.
34446 + *
34447 + * The FIQ functionality is glued into the Synopsys driver via the entry point
34448 + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
34449 + * for a FSM-enabled channel.
34450 + *
34451 + * NB: Large parts of this implementation have architecture-specific code.
34452 + * For porting this functionality to other ARM machines, the minimum is required:
34453 + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
34454 + * to the FIQ
34455 + * - A method of forcing a software generated interrupt from FIQ mode that then
34456 + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
34457 + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
34458 + * processor core - there is no locking between the FIQ and IRQ (aside from
34459 + * local_fiq_disable)
34460 + *
34461 + */
34462 +
34463 +#include "dwc_otg_fiq_fsm.h"
34464 +
34465 +
34466 +char buffer[1000*16];
34467 +int wptr;
34468 +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
34469 +{
34470 + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
34471 + va_list args;
34472 + char text[17];
34473 + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
34474 +
34475 + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
34476 + {
34477 + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
34478 + va_start(args, fmt);
34479 + vsnprintf(text+8, 9, fmt, args);
34480 + va_end(args);
34481 +
34482 + memcpy(buffer + wptr, text, 16);
34483 + wptr = (wptr + 16) % sizeof(buffer);
34484 + }
34485 +}
34486 +
34487 +/**
34488 + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
34489 + * Must be called with local interrupts and FIQ disabled.
34490 + */
34491 +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
34492 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
34493 +{
34494 + unsigned long tmp;
34495 + uint32_t newval;
34496 + fiq_lock_t lockval;
34497 + smp_mb__before_spinlock();
34498 + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
34499 + * will be sufficient. If we are on a different CPU, then the lock protects us. */
34500 + prefetchw(&lock->slock);
34501 + asm volatile (
34502 + "1: ldrex %0, [%3]\n"
34503 + " add %1, %0, %4\n"
34504 + " strex %2, %1, [%3]\n"
34505 + " teq %2, #0\n"
34506 + " bne 1b"
34507 + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
34508 + : "r" (&lock->slock), "I" (1 << 16)
34509 + : "cc");
34510 +
34511 + while (lockval.tickets.next != lockval.tickets.owner) {
34512 + wfe();
34513 + lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
34514 + }
34515 + smp_mb();
34516 +}
34517 +#else
34518 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock) { }
34519 +#endif
34520 +
34521 +/**
34522 + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
34523 + */
34524 +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
34525 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
34526 +{
34527 + smp_mb();
34528 + lock->tickets.owner++;
34529 + dsb_sev();
34530 +}
34531 +#else
34532 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock) { }
34533 +#endif
34534 +
34535 +/**
34536 + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
34537 + * @channel: channel to re-enable
34538 + */
34539 +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
34540 +{
34541 + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
34542 +
34543 + hcchar.b.chen = 0;
34544 + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
34545 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
34546 + /* Hardware bug workaround: update the ssplit index */
34547 + if (st->channel[n].hcsplt_copy.b.spltena)
34548 + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
34549 +
34550 + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
34551 + }
34552 +
34553 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
34554 + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
34555 + hcchar.b.chen = 1;
34556 +
34557 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
34558 + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
34559 +}
34560 +
34561 +/**
34562 + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
34563 + * @st: Pointer to the channel's state
34564 + * @n : channel number
34565 + *
34566 + * Change host channel registers to perform a complete-split transaction. Being mindful of the
34567 + * endpoint direction, set control regs up correctly.
34568 + */
34569 +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
34570 +{
34571 + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
34572 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34573 +
34574 + hcsplt.b.compsplt = 1;
34575 + if (st->channel[n].hcchar_copy.b.epdir == 1) {
34576 + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
34577 + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
34578 + } else {
34579 + // If OUT, the CSPLIT result contains handshake only.
34580 + hctsiz.b.xfersize = 0;
34581 + }
34582 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
34583 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34584 + mb();
34585 +}
34586 +
34587 +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
34588 +{
34589 + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
34590 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34591 +
34592 + if (st->channel[n].hcchar_copy.b.epdir == 0) {
34593 + return st->channel[n].hctsiz_copy.b.xfersize;
34594 + } else {
34595 + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
34596 + }
34597 +
34598 +}
34599 +
34600 +
34601 +/**
34602 + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
34603 + *
34604 + * Of use only for IN periodic transfers.
34605 + */
34606 +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
34607 +{
34608 + hcdma_data_t hcdma;
34609 + int i = st->channel[n].dma_info.index;
34610 + int len;
34611 + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
34612 +
34613 + len = fiq_get_xfer_len(st, n);
34614 + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
34615 + st->channel[n].dma_info.slot_len[i] = len;
34616 + i++;
34617 + if (i > 6)
34618 + BUG();
34619 +
34620 + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
34621 + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
34622 + st->channel[n].dma_info.index = i;
34623 + return 0;
34624 +}
34625 +
34626 +/**
34627 + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
34628 + */
34629 +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
34630 +{
34631 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34632 + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
34633 + hctsiz.b.pktcnt = 1;
34634 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34635 +}
34636 +
34637 +/**
34638 + * fiq_iso_out_advance() - update DMA address and split position bits
34639 + * for isochronous OUT transactions.
34640 + *
34641 + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
34642 + * Split-BEGIN states are not handled - this is done when the transaction was queued.
34643 + *
34644 + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
34645 + */
34646 +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
34647 +{
34648 + hcsplt_data_t hcsplt;
34649 + hctsiz_data_t hctsiz;
34650 + hcdma_data_t hcdma;
34651 + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
34652 + int last = 0;
34653 + int i = st->channel[n].dma_info.index;
34654 +
34655 + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
34656 + i++;
34657 + if (i == 4)
34658 + last = 1;
34659 + if (st->channel[n].dma_info.slot_len[i+1] == 255)
34660 + last = 1;
34661 +
34662 + /* New DMA address - address of bounce buffer referred to in index */
34663 + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
34664 + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n));
34665 + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
34666 + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
34667 + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
34668 + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
34669 + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
34670 + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
34671 + /* Set up new packet length */
34672 + hctsiz.b.pktcnt = 1;
34673 + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
34674 + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
34675 +
34676 + st->channel[n].dma_info.index++;
34677 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
34678 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34679 + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
34680 + return last;
34681 +}
34682 +
34683 +/**
34684 + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
34685 + *
34686 + * Despite the limitations of the DWC core, we can force a microframe pipeline of
34687 + * isochronous OUT start-split transactions while waiting for a corresponding other-type
34688 + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
34689 + * is very unlikely that filling the start-split FIFO will cause data loss.
34690 + * This allows much better interleaving of transactions in an order-independent way-
34691 + * there is no requirement to prioritise isochronous, just a state-space search has
34692 + * to be performed on each periodic start-split complete interrupt.
34693 + */
34694 +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
34695 +{
34696 + int hub_addr = st->channel[n].hub_addr;
34697 + int port_addr = st->channel[n].port_addr;
34698 + int i, poked = 0;
34699 + for (i = 0; i < num_channels; i++) {
34700 + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
34701 + continue;
34702 + if (st->channel[i].hub_addr == hub_addr &&
34703 + st->channel[i].port_addr == port_addr) {
34704 + switch (st->channel[i].fsm) {
34705 + case FIQ_PER_ISO_OUT_PENDING:
34706 + if (st->channel[i].nrpackets == 1) {
34707 + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
34708 + } else {
34709 + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
34710 + }
34711 + fiq_fsm_restart_channel(st, i, 0);
34712 + poked = 1;
34713 + break;
34714 +
34715 + default:
34716 + break;
34717 + }
34718 + }
34719 + if (poked)
34720 + break;
34721 + }
34722 + return poked;
34723 +}
34724 +
34725 +/**
34726 + * fiq_fsm_tt_in_use() - search for host channels using this TT
34727 + * @n: Channel to use as reference
34728 + *
34729 + */
34730 +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
34731 +{
34732 + int hub_addr = st->channel[n].hub_addr;
34733 + int port_addr = st->channel[n].port_addr;
34734 + int i, in_use = 0;
34735 + for (i = 0; i < num_channels; i++) {
34736 + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
34737 + continue;
34738 + switch (st->channel[i].fsm) {
34739 + /* TT is reserved for channels that are in the middle of a periodic
34740 + * split transaction.
34741 + */
34742 + case FIQ_PER_SSPLIT_STARTED:
34743 + case FIQ_PER_CSPLIT_WAIT:
34744 + case FIQ_PER_CSPLIT_NYET1:
34745 + //case FIQ_PER_CSPLIT_POLL:
34746 + case FIQ_PER_ISO_OUT_ACTIVE:
34747 + case FIQ_PER_ISO_OUT_LAST:
34748 + if (st->channel[i].hub_addr == hub_addr &&
34749 + st->channel[i].port_addr == port_addr) {
34750 + in_use = 1;
34751 + }
34752 + break;
34753 + default:
34754 + break;
34755 + }
34756 + if (in_use)
34757 + break;
34758 + }
34759 + return in_use;
34760 +}
34761 +
34762 +/**
34763 + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
34764 + * to be issued for this IN transaction.
34765 + *
34766 + * We cannot tell the inbound PID of a data packet due to hardware limitations.
34767 + * we need to make an educated guess as to whether we need to queue another CSPLIT
34768 + * or not. A no-brainer is when we have received enough data to fill the endpoint
34769 + * size, but for endpoints that give variable-length data then we have to resort
34770 + * to heuristics.
34771 + *
34772 + * We also return whether this is the last CSPLIT to be queued, again based on
34773 + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
34774 + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
34775 + */
34776 +
34777 +/*
34778 + * We need some way of guaranteeing if a returned periodic packet of size X
34779 + * has a DATA0 PID.
34780 + * The heuristic value of 144 bytes assumes that the received data has maximal
34781 + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
34782 + * permissible limit. If the transfer length results in a final packet size
34783 + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
34784 + * Also used to ensure that an endpoint will nominally only return a single
34785 + * complete-split worth of data.
34786 + */
34787 +#define DATA0_PID_HEURISTIC 144
34788 +
34789 +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
34790 +{
34791 +
34792 + int i;
34793 + int total_len = 0;
34794 + int more_needed = 1;
34795 + struct fiq_channel_state *st = &state->channel[n];
34796 +
34797 + for (i = 0; i < st->dma_info.index; i++) {
34798 + total_len += st->dma_info.slot_len[i];
34799 + }
34800 +
34801 + *probably_last = 0;
34802 +
34803 + if (st->hcchar_copy.b.eptype == 0x3) {
34804 + /*
34805 + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
34806 + * then this is definitely the last CSPLIT.
34807 + */
34808 + *probably_last = 1;
34809 + } else {
34810 + /* Isoc IN. This is a bit risky if we are the first transaction:
34811 + * we may have been held off slightly. */
34812 + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
34813 + more_needed = 0;
34814 + }
34815 + /* If in the next uframe we will receive enough data to fill the endpoint,
34816 + * then only issue 1 more csplit.
34817 + */
34818 + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
34819 + *probably_last = 1;
34820 + }
34821 +
34822 + if (total_len >= st->hctsiz_copy.b.xfersize ||
34823 + i == 6 || total_len == 0)
34824 + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
34825 + * a single endpoint. Accepting more would completely break our scheduling mechanism though
34826 + * - in these extreme cases we will pass through a truncated packet.
34827 + */
34828 + more_needed = 0;
34829 +
34830 + return more_needed;
34831 +}
34832 +
34833 +/**
34834 + * fiq_fsm_too_late() - Test transaction for lateness
34835 + *
34836 + * If a SSPLIT for a large IN transaction is issued too late in a frame,
34837 + * the hub will disable the port to the device and respond with ERR handshakes.
34838 + * The hub status endpoint will not reflect this change.
34839 + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
34840 + */
34841 +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
34842 +{
34843 + int uframe;
34844 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
34845 + uframe = hfnum.b.frnum & 0x7;
34846 + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
34847 + return 1;
34848 + } else {
34849 + return 0;
34850 + }
34851 +}
34852 +
34853 +
34854 +/**
34855 + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
34856 + *
34857 + * Search pending transactions in the start-split pending state and queue them.
34858 + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
34859 + * Note: we specifically don't do isochronous OUT transactions first because better
34860 + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
34861 + */
34862 +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
34863 +{
34864 + int n;
34865 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
34866 + if ((hfnum.b.frnum & 0x7) == 5)
34867 + return;
34868 + for (n = 0; n < num_channels; n++) {
34869 + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
34870 + /* Check to see if any other transactions are using this TT */
34871 + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
34872 + if (!fiq_fsm_too_late(st, n)) {
34873 + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
34874 + fiq_print(FIQDBG_INT, st, "NEXTPER ");
34875 + fiq_fsm_restart_channel(st, n, 0);
34876 + } else {
34877 + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
34878 + }
34879 + break;
34880 + }
34881 + }
34882 + }
34883 + for (n = 0; n < num_channels; n++) {
34884 + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
34885 + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
34886 + fiq_print(FIQDBG_INT, st, "NEXTISO ");
34887 + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
34888 + fiq_fsm_restart_channel(st, n, 0);
34889 + break;
34890 + }
34891 + }
34892 + }
34893 +}
34894 +
34895 +/**
34896 + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
34897 + * @state: Pointer to fiq_state
34898 + * @n: Channel transaction is active on
34899 + * @hcint: Copy of host channel interrupt register
34900 + *
34901 + * Returns 0 if there are no more transactions for this HC to do, 1
34902 + * otherwise.
34903 + */
34904 +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
34905 +{
34906 + struct fiq_channel_state *st = &state->channel[n];
34907 + int xfer_len = 0, nrpackets = 0;
34908 + hcdma_data_t hcdma;
34909 + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
34910 +
34911 + xfer_len = fiq_get_xfer_len(state, n);
34912 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
34913 +
34914 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
34915 +
34916 + st->hs_isoc_info.index++;
34917 + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
34918 + return 0;
34919 + }
34920 +
34921 + /* grab the next DMA address offset from the array */
34922 + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
34923 + FIQ_WRITE(state->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
34924 +
34925 + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
34926 + * the core needs to be told to send the correct number. Caution: for IN transfers,
34927 + * this is always set to the maximum size of the endpoint. */
34928 + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
34929 + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
34930 + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
34931 + if (nrpackets == 0)
34932 + nrpackets = 1;
34933 + st->hcchar_copy.b.multicnt = nrpackets;
34934 + st->hctsiz_copy.b.pktcnt = nrpackets;
34935 +
34936 + /* Initial PID also needs to be set */
34937 + if (st->hcchar_copy.b.epdir == 0) {
34938 + st->hctsiz_copy.b.xfersize = xfer_len;
34939 + switch (st->hcchar_copy.b.multicnt) {
34940 + case 1:
34941 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
34942 + break;
34943 + case 2:
34944 + case 3:
34945 + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
34946 + break;
34947 + }
34948 +
34949 + } else {
34950 + switch (st->hcchar_copy.b.multicnt) {
34951 + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
34952 + case 1:
34953 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
34954 + break;
34955 + case 2:
34956 + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
34957 + break;
34958 + case 3:
34959 + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
34960 + break;
34961 + }
34962 + }
34963 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
34964 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
34965 + /* Channel is enabled on hcint handler exit */
34966 + fiq_print(FIQDBG_INT, state, "HSISOOUT");
34967 + return 1;
34968 +}
34969 +
34970 +
34971 +/**
34972 + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
34973 + * @state: Pointer to the state struct passed from banked FIQ mode registers.
34974 + * @num_channels: set according to the DWC hardware configuration
34975 + *
34976 + * The SOF handler in FSM mode has two functions
34977 + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
34978 + * nothing to do
34979 + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
34980 + * of holdoff.
34981 + *
34982 + * The second part is architecture-specific to mach-bcm2835 -
34983 + * a sane interrupt controller would have a mask register for ARM interrupt sources
34984 + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
34985 + * number (USB) can be enabled. This means that certain parts of the USB specification
34986 + * that require "wait a little while, then issue another packet" cannot be fulfilled with
34987 + * the timing granularity required to achieve optimal throughout. The workaround is to use
34988 + * the SOF "timer" (125uS) to perform this task.
34989 + */
34990 +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
34991 +{
34992 + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
34993 + int n;
34994 + int kick_irq = 0;
34995 +
34996 + if ((hfnum.b.frnum & 0x7) == 1) {
34997 + /* We cannot issue csplits for transactions in the last frame past (n+1).1
34998 + * Check to see if there are any transactions that are stale.
34999 + * Boot them out.
35000 + */
35001 + for (n = 0; n < num_channels; n++) {
35002 + switch (state->channel[n].fsm) {
35003 + case FIQ_PER_CSPLIT_WAIT:
35004 + case FIQ_PER_CSPLIT_NYET1:
35005 + case FIQ_PER_CSPLIT_POLL:
35006 + case FIQ_PER_CSPLIT_LAST:
35007 + /* Check if we are no longer in the same full-speed frame. */
35008 + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
35009 + (hfnum.b.frnum & ~0x7))
35010 + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
35011 + break;
35012 + default:
35013 + break;
35014 + }
35015 + }
35016 + }
35017 +
35018 + for (n = 0; n < num_channels; n++) {
35019 + switch (state->channel[n].fsm) {
35020 +
35021 + case FIQ_NP_SSPLIT_RETRY:
35022 + case FIQ_NP_IN_CSPLIT_RETRY:
35023 + case FIQ_NP_OUT_CSPLIT_RETRY:
35024 + fiq_fsm_restart_channel(state, n, 0);
35025 + break;
35026 +
35027 + case FIQ_HS_ISOC_SLEEPING:
35028 + /* Is it time to wake this channel yet? */
35029 + if (--state->channel[n].uframe_sleeps == 0) {
35030 + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
35031 + fiq_fsm_restart_channel(state, n, 0);
35032 + }
35033 + break;
35034 +
35035 + case FIQ_PER_SSPLIT_QUEUED:
35036 + if ((hfnum.b.frnum & 0x7) == 5)
35037 + break;
35038 + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
35039 + if (!fiq_fsm_too_late(state, n)) {
35040 + fiq_print(FIQDBG_INT, state, "SOF GO %01d", n);
35041 + fiq_fsm_restart_channel(state, n, 0);
35042 + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
35043 + } else {
35044 + /* Transaction cannot be started without risking a device babble error */
35045 + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
35046 + state->haintmsk_saved.b2.chint &= ~(1 << n);
35047 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
35048 + kick_irq |= 1;
35049 + }
35050 + }
35051 + break;
35052 +
35053 + case FIQ_PER_ISO_OUT_PENDING:
35054 + /* Ordinarily, this should be poked after the SSPLIT
35055 + * complete interrupt for a competing transfer on the same
35056 + * TT. Doesn't happen for aborted transactions though.
35057 + */
35058 + if ((hfnum.b.frnum & 0x7) >= 5)
35059 + break;
35060 + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
35061 + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
35062 + * that caused this.
35063 + */
35064 + fiq_fsm_restart_channel(state, n, 0);
35065 + fiq_print(FIQDBG_INT, state, "SOF ISOC");
35066 + if (state->channel[n].nrpackets == 1) {
35067 + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
35068 + } else {
35069 + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
35070 + }
35071 + }
35072 + break;
35073 +
35074 + case FIQ_PER_CSPLIT_WAIT:
35075 + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
35076 + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
35077 + * will utterly bugger this up though.
35078 + */
35079 + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
35080 + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
35081 + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
35082 + fiq_fsm_restart_channel(state, n, 0);
35083 + fiq_fsm_start_next_periodic(state, num_channels);
35084 +
35085 + }
35086 + break;
35087 +
35088 + case FIQ_PER_SPLIT_TIMEOUT:
35089 + case FIQ_DEQUEUE_ISSUED:
35090 + /* Ugly: we have to force a HCD interrupt.
35091 + * Poke the mask for the channel in question.
35092 + * We will take a fake SOF because of this, but
35093 + * that's OK.
35094 + */
35095 + state->haintmsk_saved.b2.chint &= ~(1 << n);
35096 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
35097 + kick_irq |= 1;
35098 + break;
35099 +
35100 + default:
35101 + break;
35102 + }
35103 + }
35104 +
35105 + if (state->kick_np_queues ||
35106 + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
35107 + kick_irq |= 1;
35108 +
35109 + return !kick_irq;
35110 +}
35111 +
35112 +
35113 +/**
35114 + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
35115 + * @state: Pointer to the FIQ state struct
35116 + * @num_channels: Number of channels as per hardware config
35117 + * @n: channel for which HAINT(i) was raised
35118 + *
35119 + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
35120 + */
35121 +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
35122 +{
35123 + hcint_data_t hcint;
35124 + hcintmsk_data_t hcintmsk;
35125 + hcint_data_t hcint_probe;
35126 + hcchar_data_t hcchar;
35127 + int handled = 0;
35128 + int restart = 0;
35129 + int last_csplit = 0;
35130 + int start_next_periodic = 0;
35131 + struct fiq_channel_state *st = &state->channel[n];
35132 + hfnum_data_t hfnum;
35133 +
35134 + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
35135 + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
35136 + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
35137 +
35138 + if (st->fsm != FIQ_PASSTHROUGH) {
35139 + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
35140 + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
35141 + }
35142 +
35143 + switch (st->fsm) {
35144 +
35145 + case FIQ_PASSTHROUGH:
35146 + case FIQ_DEQUEUE_ISSUED:
35147 + /* doesn't belong to us, kick it upstairs */
35148 + break;
35149 +
35150 + case FIQ_PASSTHROUGH_ERRORSTATE:
35151 + /* We are here to emulate the error recovery mechanism of the dwc HCD.
35152 + * Several interrupts are unmasked if a previous transaction failed - it's
35153 + * death for the FIQ to attempt to handle them as the channel isn't halted.
35154 + * Emulate what the HCD does in this situation: mask and continue.
35155 + * The FSM has no other state setup so this has to be handled out-of-band.
35156 + */
35157 + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
35158 + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
35159 + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
35160 + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
35161 + * interrupt, after the device has disappeared.
35162 + */
35163 + if (!hcint.b.xacterr)
35164 + st->nr_errors = 0;
35165 + hcintmsk.b.nak = 0;
35166 + hcintmsk.b.ack = 0;
35167 + hcintmsk.b.datatglerr = 0;
35168 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
35169 + return 1;
35170 + }
35171 + if (hcint_probe.b.chhltd) {
35172 + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
35173 + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
35174 + return 0;
35175 + }
35176 + break;
35177 +
35178 + /* Non-periodic state groups */
35179 + case FIQ_NP_SSPLIT_STARTED:
35180 + case FIQ_NP_SSPLIT_RETRY:
35181 + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
35182 + if (hcint.b.ack) {
35183 + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
35184 + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
35185 + */
35186 + if(st->hcchar_copy.b.epdir == 1)
35187 + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
35188 + else
35189 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35190 + st->nr_errors = 0;
35191 + handled = 1;
35192 + fiq_fsm_setup_csplit(state, n);
35193 + } else if (hcint.b.nak) {
35194 + // No buffer space in TT. Retry on a uframe boundary.
35195 + st->fsm = FIQ_NP_SSPLIT_RETRY;
35196 + handled = 1;
35197 + } else if (hcint.b.xacterr) {
35198 + // The only other one we care about is xacterr. This implies HS bus error - retry.
35199 + st->nr_errors++;
35200 + st->fsm = FIQ_NP_SSPLIT_RETRY;
35201 + if (st->nr_errors >= 3) {
35202 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35203 + } else {
35204 + handled = 1;
35205 + restart = 1;
35206 + }
35207 + } else {
35208 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35209 + handled = 0;
35210 + restart = 0;
35211 + }
35212 + break;
35213 +
35214 + case FIQ_NP_IN_CSPLIT_RETRY:
35215 + /* Received a CSPLIT done interrupt.
35216 + * Expected Data/NAK/STALL/NYET for IN.
35217 + */
35218 + if (hcint.b.xfercomp) {
35219 + /* For IN, data is present. */
35220 + st->fsm = FIQ_NP_SPLIT_DONE;
35221 + } else if (hcint.b.nak) {
35222 + /* no endpoint data. Punt it upstairs */
35223 + st->fsm = FIQ_NP_SPLIT_DONE;
35224 + } else if (hcint.b.nyet) {
35225 + /* CSPLIT NYET - retry on a uframe boundary. */
35226 + handled = 1;
35227 + st->nr_errors = 0;
35228 + } else if (hcint.b.datatglerr) {
35229 + /* data toggle errors do not set the xfercomp bit. */
35230 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35231 + } else if (hcint.b.xacterr) {
35232 + /* HS error. Retry immediate */
35233 + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
35234 + st->nr_errors++;
35235 + if (st->nr_errors >= 3) {
35236 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35237 + } else {
35238 + handled = 1;
35239 + restart = 1;
35240 + }
35241 + } else if (hcint.b.stall || hcint.b.bblerr) {
35242 + /* A STALL implies either a LS bus error or a genuine STALL. */
35243 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35244 + } else {
35245 + /* Hardware bug. It's possible in some cases to
35246 + * get a channel halt with nothing else set when
35247 + * the response was a NYET. Treat as local 3-strikes retry.
35248 + */
35249 + hcint_data_t hcint_test = hcint;
35250 + hcint_test.b.chhltd = 0;
35251 + if (!hcint_test.d32) {
35252 + st->nr_errors++;
35253 + if (st->nr_errors >= 3) {
35254 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35255 + } else {
35256 + handled = 1;
35257 + }
35258 + } else {
35259 + /* Bail out if something unexpected happened */
35260 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35261 + }
35262 + }
35263 + break;
35264 +
35265 + case FIQ_NP_OUT_CSPLIT_RETRY:
35266 + /* Received a CSPLIT done interrupt.
35267 + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
35268 + if (hcint.b.xfercomp) {
35269 + st->fsm = FIQ_NP_SPLIT_DONE;
35270 + } else if (hcint.b.nak) {
35271 + // The HCD will implement the holdoff on frame boundaries.
35272 + st->fsm = FIQ_NP_SPLIT_DONE;
35273 + } else if (hcint.b.nyet) {
35274 + // Hub still processing.
35275 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35276 + handled = 1;
35277 + st->nr_errors = 0;
35278 + //restart = 1;
35279 + } else if (hcint.b.xacterr) {
35280 + /* HS error. retry immediate */
35281 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35282 + st->nr_errors++;
35283 + if (st->nr_errors >= 3) {
35284 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35285 + } else {
35286 + handled = 1;
35287 + restart = 1;
35288 + }
35289 + } else if (hcint.b.stall) {
35290 + /* LS bus error or genuine stall */
35291 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35292 + } else {
35293 + /*
35294 + * Hardware bug. It's possible in some cases to get a
35295 + * channel halt with nothing else set when the response was a NYET.
35296 + * Treat as local 3-strikes retry.
35297 + */
35298 + hcint_data_t hcint_test = hcint;
35299 + hcint_test.b.chhltd = 0;
35300 + if (!hcint_test.d32) {
35301 + st->nr_errors++;
35302 + if (st->nr_errors >= 3) {
35303 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35304 + } else {
35305 + handled = 1;
35306 + }
35307 + } else {
35308 + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
35309 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35310 + }
35311 + }
35312 + break;
35313 +
35314 + /* Periodic split states (except isoc out) */
35315 + case FIQ_PER_SSPLIT_STARTED:
35316 + /* Expect an ACK or failure for SSPLIT */
35317 + if (hcint.b.ack) {
35318 + /*
35319 + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
35320 + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
35321 + * point for microframe n-3, the packet will not appear on the bus until microframe n.
35322 + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
35323 + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
35324 + * coincident with SOF for n+1.
35325 + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
35326 + * These appear to be caused by timing/clock crossing bugs within the core itself.
35327 + * State machine workaround.
35328 + */
35329 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35330 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35331 + fiq_fsm_setup_csplit(state, n);
35332 + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
35333 + * time. If not, then we're in the next SOF.
35334 + */
35335 + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
35336 + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
35337 + st->expected_uframe = hfnum.b.frnum;
35338 + st->fsm = FIQ_PER_CSPLIT_WAIT;
35339 + } else {
35340 + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
35341 + /* For isochronous IN endpoints,
35342 + * we need to hold off if we are expecting a lot of data */
35343 + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
35344 + start_next_periodic = 1;
35345 + }
35346 + /* Danger will robinson: we are in a broken state. If our first interrupt after
35347 + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
35348 + * lag. Unmask the NYET interrupt.
35349 + */
35350 + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
35351 + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
35352 + restart = 1;
35353 + }
35354 + handled = 1;
35355 + } else if (hcint.b.xacterr) {
35356 + /* 3-strikes retry is enabled, we have hit our max nr_errors */
35357 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35358 + start_next_periodic = 1;
35359 + } else {
35360 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35361 + start_next_periodic = 1;
35362 + }
35363 + /* We can now queue the next isochronous OUT transaction, if one is pending. */
35364 + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
35365 + fiq_print(FIQDBG_INT, state, "NEXTISO ");
35366 + }
35367 + break;
35368 +
35369 + case FIQ_PER_CSPLIT_NYET1:
35370 + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
35371 + * we are too late and the TT has dropped its CSPLIT fifo.
35372 + */
35373 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35374 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35375 + start_next_periodic = 1;
35376 + if (hcint.b.nak) {
35377 + st->fsm = FIQ_PER_SPLIT_DONE;
35378 + } else if (hcint.b.xfercomp) {
35379 + fiq_increment_dma_buf(state, num_channels, n);
35380 + st->fsm = FIQ_PER_CSPLIT_POLL;
35381 + st->nr_errors = 0;
35382 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35383 + handled = 1;
35384 + restart = 1;
35385 + if (!last_csplit)
35386 + start_next_periodic = 0;
35387 + } else {
35388 + st->fsm = FIQ_PER_SPLIT_DONE;
35389 + }
35390 + } else if (hcint.b.nyet) {
35391 + /* Doh. Data lost. */
35392 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35393 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35394 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35395 + } else {
35396 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35397 + }
35398 + break;
35399 +
35400 + case FIQ_PER_CSPLIT_BROKEN_NYET1:
35401 + /*
35402 + * we got here because our host channel is in the delayed-interrupt
35403 + * state and we cannot take a NYET interrupt any later than when it
35404 + * occurred. Disable then re-enable the channel if this happens to force
35405 + * CSPLITs to occur at the right time.
35406 + */
35407 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35408 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35409 + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
35410 + if (hcint.b.nak) {
35411 + st->fsm = FIQ_PER_SPLIT_DONE;
35412 + start_next_periodic = 1;
35413 + } else if (hcint.b.xfercomp) {
35414 + fiq_increment_dma_buf(state, num_channels, n);
35415 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35416 + st->fsm = FIQ_PER_CSPLIT_POLL;
35417 + handled = 1;
35418 + restart = 1;
35419 + start_next_periodic = 1;
35420 + /* Reload HCTSIZ for the next transfer */
35421 + fiq_fsm_reload_hctsiz(state, n);
35422 + if (!last_csplit)
35423 + start_next_periodic = 0;
35424 + } else {
35425 + st->fsm = FIQ_PER_SPLIT_DONE;
35426 + }
35427 + } else if (hcint.b.nyet) {
35428 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35429 + start_next_periodic = 1;
35430 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35431 + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
35432 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35433 + } else {
35434 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35435 + }
35436 + break;
35437 +
35438 + case FIQ_PER_CSPLIT_POLL:
35439 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35440 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35441 + start_next_periodic = 1;
35442 + if (hcint.b.nak) {
35443 + st->fsm = FIQ_PER_SPLIT_DONE;
35444 + } else if (hcint.b.xfercomp) {
35445 + fiq_increment_dma_buf(state, num_channels, n);
35446 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35447 + handled = 1;
35448 + restart = 1;
35449 + /* Reload HCTSIZ for the next transfer */
35450 + fiq_fsm_reload_hctsiz(state, n);
35451 + if (!last_csplit)
35452 + start_next_periodic = 0;
35453 + } else {
35454 + st->fsm = FIQ_PER_SPLIT_DONE;
35455 + }
35456 + } else if (hcint.b.nyet) {
35457 + /* Are we a NYET after the first data packet? */
35458 + if (st->nrpackets == 0) {
35459 + st->fsm = FIQ_PER_CSPLIT_NYET1;
35460 + handled = 1;
35461 + restart = 1;
35462 + } else {
35463 + /* We got a NYET when polling CSPLITs. Can happen
35464 + * if our heuristic fails, or if someone disables us
35465 + * for any significant length of time.
35466 + */
35467 + if (st->nr_errors >= 3) {
35468 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35469 + } else {
35470 + st->fsm = FIQ_PER_SPLIT_DONE;
35471 + }
35472 + }
35473 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35474 + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
35475 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35476 + } else {
35477 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35478 + }
35479 + break;
35480 +
35481 + case FIQ_HS_ISOC_TURBO:
35482 + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
35483 + /* more transactions to come */
35484 + handled = 1;
35485 + fiq_print(FIQDBG_INT, state, "HSISO M ");
35486 + /* For strided transfers, put ourselves to sleep */
35487 + if (st->hs_isoc_info.stride > 1) {
35488 + st->uframe_sleeps = st->hs_isoc_info.stride - 1;
35489 + st->fsm = FIQ_HS_ISOC_SLEEPING;
35490 + } else {
35491 + restart = 1;
35492 + }
35493 + } else {
35494 + st->fsm = FIQ_HS_ISOC_DONE;
35495 + fiq_print(FIQDBG_INT, state, "HSISO F ");
35496 + }
35497 + break;
35498 +
35499 + case FIQ_HS_ISOC_ABORTED:
35500 + /* This abort is called by the driver rewriting the state mid-transaction
35501 + * which allows the dequeue mechanism to work more effectively.
35502 + */
35503 + break;
35504 +
35505 + case FIQ_PER_ISO_OUT_ACTIVE:
35506 + if (hcint.b.ack) {
35507 + if(fiq_iso_out_advance(state, num_channels, n)) {
35508 + /* last OUT transfer */
35509 + st->fsm = FIQ_PER_ISO_OUT_LAST;
35510 + /*
35511 + * Assuming the periodic FIFO in the dwc core
35512 + * actually does its job properly, we can queue
35513 + * the next ssplit now and in theory, the wire
35514 + * transactions will be in-order.
35515 + */
35516 + // No it doesn't. It appears to process requests in host channel order.
35517 + //start_next_periodic = 1;
35518 + }
35519 + handled = 1;
35520 + restart = 1;
35521 + } else {
35522 + /*
35523 + * Isochronous transactions carry on regardless. Log the error
35524 + * and continue.
35525 + */
35526 + //explode += 1;
35527 + st->nr_errors++;
35528 + if(fiq_iso_out_advance(state, num_channels, n)) {
35529 + st->fsm = FIQ_PER_ISO_OUT_LAST;
35530 + //start_next_periodic = 1;
35531 + }
35532 + handled = 1;
35533 + restart = 1;
35534 + }
35535 + break;
35536 +
35537 + case FIQ_PER_ISO_OUT_LAST:
35538 + if (hcint.b.ack) {
35539 + /* All done here */
35540 + st->fsm = FIQ_PER_ISO_OUT_DONE;
35541 + } else {
35542 + st->fsm = FIQ_PER_ISO_OUT_DONE;
35543 + st->nr_errors++;
35544 + }
35545 + start_next_periodic = 1;
35546 + break;
35547 +
35548 + case FIQ_PER_SPLIT_TIMEOUT:
35549 + /* SOF kicked us because we overran. */
35550 + start_next_periodic = 1;
35551 + break;
35552 +
35553 + default:
35554 + break;
35555 + }
35556 +
35557 + if (handled) {
35558 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
35559 + } else {
35560 + /* Copy the regs into the state so the IRQ knows what to do */
35561 + st->hcint_copy.d32 = hcint.d32;
35562 + }
35563 +
35564 + if (restart) {
35565 + /* Restart always implies handled. */
35566 + if (restart == 2) {
35567 + /* For complete-split INs, the show must go on.
35568 + * Force a channel restart */
35569 + fiq_fsm_restart_channel(state, n, 1);
35570 + } else {
35571 + fiq_fsm_restart_channel(state, n, 0);
35572 + }
35573 + }
35574 + if (start_next_periodic) {
35575 + fiq_fsm_start_next_periodic(state, num_channels);
35576 + }
35577 + if (st->fsm != FIQ_PASSTHROUGH)
35578 + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
35579 +
35580 + return handled;
35581 +}
35582 +
35583 +
35584 +/**
35585 + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
35586 + * @state: pointer to state struct passed from the banked FIQ mode registers.
35587 + * @num_channels: set according to the DWC hardware configuration
35588 + * @dma: pointer to DMA bounce buffers for split transaction slots
35589 + *
35590 + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
35591 + * inside an EHCI or similar host controller regarding split transactions. The DWC core
35592 + * interrupts each and every time a split transaction packet is received or sent successfully.
35593 + * This results in either an interrupt storm when everything is working "properly", or
35594 + * the interrupt latency of the system in general breaks time-sensitive periodic split
35595 + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
35596 + * solves these problems.
35597 + *
35598 + * Return: void
35599 + */
35600 +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
35601 +{
35602 + gintsts_data_t gintsts, gintsts_handled;
35603 + gintmsk_data_t gintmsk;
35604 + //hfnum_data_t hfnum;
35605 + haint_data_t haint, haint_handled;
35606 + haintmsk_data_t haintmsk;
35607 + int kick_irq = 0;
35608 +
35609 + gintsts_handled.d32 = 0;
35610 + haint_handled.d32 = 0;
35611 +
35612 + fiq_fsm_spin_lock(&state->lock);
35613 + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
35614 + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
35615 + gintsts.d32 &= gintmsk.d32;
35616 +
35617 + if (gintsts.b.sofintr) {
35618 + /* For FSM mode, SOF is required to keep the state machine advance for
35619 + * certain stages of the periodic pipeline. It's death to mask this
35620 + * interrupt in that case.
35621 + */
35622 +
35623 + if (!fiq_fsm_do_sof(state, num_channels)) {
35624 + /* Kick IRQ once. Queue advancement means that all pending transactions
35625 + * will get serviced when the IRQ finally executes.
35626 + */
35627 + if (state->gintmsk_saved.b.sofintr == 1)
35628 + kick_irq |= 1;
35629 + state->gintmsk_saved.b.sofintr = 0;
35630 + }
35631 + gintsts_handled.b.sofintr = 1;
35632 + }
35633 +
35634 + if (gintsts.b.hcintr) {
35635 + int i;
35636 + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
35637 + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
35638 + haint.d32 &= haintmsk.d32;
35639 + haint_handled.d32 = 0;
35640 + for (i=0; i<num_channels; i++) {
35641 + if (haint.b2.chint & (1 << i)) {
35642 + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
35643 + /* HCINT was not handled in FIQ
35644 + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
35645 + * Mask HAINT(i) but keep top-level hcint unmasked.
35646 + */
35647 + state->haintmsk_saved.b2.chint &= ~(1 << i);
35648 + } else {
35649 + /* do_hcintr cleaned up after itself, but clear haint */
35650 + haint_handled.b2.chint |= (1 << i);
35651 + }
35652 + }
35653 + }
35654 +
35655 + if (haint_handled.b2.chint) {
35656 + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
35657 + }
35658 +
35659 + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
35660 + /*
35661 + * This is necessary to avoid multiple retriggers of the MPHI in the case
35662 + * where interrupts are held off and HCINTs start to pile up.
35663 + * Only wake up the IRQ if a new interrupt came in, was not handled and was
35664 + * masked.
35665 + */
35666 + haintmsk.d32 &= state->haintmsk_saved.d32;
35667 + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
35668 + kick_irq |= 1;
35669 + }
35670 + /* Top-Level interrupt - always handled because it's level-sensitive */
35671 + gintsts_handled.b.hcintr = 1;
35672 + }
35673 +
35674 +
35675 + /* Clear the bits in the saved register that were not handled but were triggered. */
35676 + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
35677 +
35678 + /* FIQ didn't handle something - mask has changed - write new mask */
35679 + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
35680 + gintmsk.d32 &= state->gintmsk_saved.d32;
35681 + gintmsk.b.sofintr = 1;
35682 + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
35683 +// fiq_print(FIQDBG_INT, state, "KICKGINT");
35684 +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
35685 +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
35686 + kick_irq |= 1;
35687 + }
35688 +
35689 + if (gintsts_handled.d32) {
35690 + /* Only applies to edge-sensitive bits in GINTSTS */
35691 + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
35692 + }
35693 +
35694 + /* We got an interrupt, didn't handle it. */
35695 + if (kick_irq) {
35696 + state->mphi_int_count++;
35697 + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
35698 + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
35699 +
35700 + }
35701 + state->fiq_done++;
35702 + mb();
35703 + fiq_fsm_spin_unlock(&state->lock);
35704 +}
35705 +
35706 +
35707 +/**
35708 + * dwc_otg_fiq_nop() - FIQ "lite"
35709 + * @state: pointer to state struct passed from the banked FIQ mode registers.
35710 + *
35711 + * The "nop" handler does not intervene on any interrupts other than SOF.
35712 + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
35713 + * with non-periodic/periodic queues) needs to be kicked.
35714 + *
35715 + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
35716 + *
35717 + * Return: void
35718 + */
35719 +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
35720 +{
35721 + gintsts_data_t gintsts, gintsts_handled;
35722 + gintmsk_data_t gintmsk;
35723 + hfnum_data_t hfnum;
35724 +
35725 + fiq_fsm_spin_lock(&state->lock);
35726 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35727 + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
35728 + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
35729 + gintsts.d32 &= gintmsk.d32;
35730 + gintsts_handled.d32 = 0;
35731 +
35732 + if (gintsts.b.sofintr) {
35733 + if (!state->kick_np_queues &&
35734 + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
35735 + /* SOF handled, no work to do, just ACK interrupt */
35736 + gintsts_handled.b.sofintr = 1;
35737 + } else {
35738 + /* Kick IRQ */
35739 + state->gintmsk_saved.b.sofintr = 0;
35740 + }
35741 + }
35742 +
35743 + /* Reset handled interrupts */
35744 + if(gintsts_handled.d32) {
35745 + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
35746 + }
35747 +
35748 + /* Clear the bits in the saved register that were not handled but were triggered. */
35749 + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
35750 +
35751 + /* We got an interrupt, didn't handle it and want to mask it */
35752 + if (~(state->gintmsk_saved.d32)) {
35753 + state->mphi_int_count++;
35754 + gintmsk.d32 &= state->gintmsk_saved.d32;
35755 + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
35756 + /* Force a clear before another dummy send */
35757 + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
35758 + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
35759 + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
35760 +
35761 + }
35762 + state->fiq_done++;
35763 + mb();
35764 + fiq_fsm_spin_unlock(&state->lock);
35765 +}
35766 --- /dev/null
35767 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
35768 @@ -0,0 +1,370 @@
35769 +/*
35770 + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
35771 + *
35772 + * Copyright (c) 2013 Raspberry Pi Foundation
35773 + *
35774 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
35775 + * All rights reserved.
35776 + *
35777 + * Redistribution and use in source and binary forms, with or without
35778 + * modification, are permitted provided that the following conditions are met:
35779 + * * Redistributions of source code must retain the above copyright
35780 + * notice, this list of conditions and the following disclaimer.
35781 + * * Redistributions in binary form must reproduce the above copyright
35782 + * notice, this list of conditions and the following disclaimer in the
35783 + * documentation and/or other materials provided with the distribution.
35784 + * * Neither the name of Raspberry Pi nor the
35785 + * names of its contributors may be used to endorse or promote products
35786 + * derived from this software without specific prior written permission.
35787 + *
35788 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
35789 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35790 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35791 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
35792 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35793 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35794 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35795 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35796 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35797 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35798 + *
35799 + * This FIQ implements functionality that performs split transactions on
35800 + * the dwc_otg hardware without any outside intervention. A split transaction
35801 + * is "queued" by nominating a specific host channel to perform the entirety
35802 + * of a split transaction. This FIQ will then perform the microframe-precise
35803 + * scheduling required in each phase of the transaction until completion.
35804 + *
35805 + * The FIQ functionality has been surgically implanted into the Synopsys
35806 + * vendor-provided driver.
35807 + *
35808 + */
35809 +
35810 +#ifndef DWC_OTG_FIQ_FSM_H_
35811 +#define DWC_OTG_FIQ_FSM_H_
35812 +
35813 +#include "dwc_otg_regs.h"
35814 +#include "dwc_otg_cil.h"
35815 +#include "dwc_otg_hcd.h"
35816 +#include <linux/kernel.h>
35817 +#include <linux/irqflags.h>
35818 +#include <linux/string.h>
35819 +#include <asm/barrier.h>
35820 +
35821 +#if 0
35822 +#define FLAME_ON(x) \
35823 +do { \
35824 + int gpioreg; \
35825 + \
35826 + gpioreg = readl(__io_address(0x20200000+0x8)); \
35827 + gpioreg &= ~(7 << (x-20)*3); \
35828 + gpioreg |= 0x1 << (x-20)*3; \
35829 + writel(gpioreg, __io_address(0x20200000+0x8)); \
35830 + \
35831 + writel(1<<x, __io_address(0x20200000+(0x1C))); \
35832 +} while (0)
35833 +
35834 +#define FLAME_OFF(x) \
35835 +do { \
35836 + writel(1<<x, __io_address(0x20200000+(0x28))); \
35837 +} while (0)
35838 +#else
35839 +#define FLAME_ON(x) do { } while (0)
35840 +#define FLAME_OFF(X) do { } while (0)
35841 +#endif
35842 +
35843 +/* This is a quick-and-dirty arch-specific register read/write. We know that
35844 + * writes to a peripheral on BCM2835 will always arrive in-order, also that
35845 + * reads and writes are executed in-order therefore the need for memory barriers
35846 + * is obviated if we're only talking to USB.
35847 + */
35848 +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
35849 +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
35850 +
35851 +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
35852 +#define GINTSTS 0x014
35853 +#define GINTMSK 0x018
35854 +/* Debug register. Poll the top of the received packets FIFO. */
35855 +#define GRXSTSR 0x01C
35856 +#define HFNUM 0x408
35857 +#define HAINT 0x414
35858 +#define HAINTMSK 0x418
35859 +#define HPRT0 0x440
35860 +
35861 +/* HC_regs start from an offset of 0x500 */
35862 +#define HC_START 0x500
35863 +#define HC_OFFSET 0x020
35864 +
35865 +#define HC_DMA 0x514
35866 +
35867 +#define HCCHAR 0x00
35868 +#define HCSPLT 0x04
35869 +#define HCINT 0x08
35870 +#define HCINTMSK 0x0C
35871 +#define HCTSIZ 0x10
35872 +
35873 +#define ISOC_XACTPOS_ALL 0b11
35874 +#define ISOC_XACTPOS_BEGIN 0b10
35875 +#define ISOC_XACTPOS_MID 0b00
35876 +#define ISOC_XACTPOS_END 0b01
35877 +
35878 +#define DWC_PID_DATA2 0b01
35879 +#define DWC_PID_MDATA 0b11
35880 +#define DWC_PID_DATA1 0b10
35881 +#define DWC_PID_DATA0 0b00
35882 +
35883 +typedef struct {
35884 + volatile void* base;
35885 + volatile void* ctrl;
35886 + volatile void* outdda;
35887 + volatile void* outddb;
35888 + volatile void* intstat;
35889 +} mphi_regs_t;
35890 +
35891 +enum fiq_debug_level {
35892 + FIQDBG_SCHED = (1 << 0),
35893 + FIQDBG_INT = (1 << 1),
35894 + FIQDBG_ERR = (1 << 2),
35895 + FIQDBG_PORTHUB = (1 << 3),
35896 +};
35897 +
35898 +typedef struct {
35899 + union {
35900 + uint32_t slock;
35901 + struct _tickets {
35902 + uint16_t owner;
35903 + uint16_t next;
35904 + } tickets;
35905 + };
35906 +} fiq_lock_t;
35907 +
35908 +struct fiq_state;
35909 +
35910 +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
35911 +#if 0
35912 +#define fiq_print _fiq_print
35913 +#else
35914 +#define fiq_print(x, y, ...)
35915 +#endif
35916 +
35917 +extern bool fiq_enable, fiq_fsm_enable;
35918 +extern ushort nak_holdoff;
35919 +
35920 +/**
35921 + * enum fiq_fsm_state - The FIQ FSM states.
35922 + *
35923 + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
35924 + * USB2.0 specification for host responses to various transaction states.
35925 + * There are modifications to this host state machine because of a variety of
35926 + * quirks and limitations in the dwc_otg hardware.
35927 + *
35928 + * The fsm state is also used to communicate back to the driver on completion of
35929 + * a split transaction. The end states are used in conjunction with the interrupts
35930 + * raised by the final transaction.
35931 + */
35932 +enum fiq_fsm_state {
35933 + /* FIQ isn't enabled for this host channel */
35934 + FIQ_PASSTHROUGH = 0,
35935 + /* For the first interrupt received for this channel,
35936 + * the FIQ has to ack any interrupts indicating success. */
35937 + FIQ_PASSTHROUGH_ERRORSTATE = 31,
35938 + /* Nonperiodic state groups */
35939 + FIQ_NP_SSPLIT_STARTED = 1,
35940 + FIQ_NP_SSPLIT_RETRY = 2,
35941 + FIQ_NP_OUT_CSPLIT_RETRY = 3,
35942 + FIQ_NP_IN_CSPLIT_RETRY = 4,
35943 + FIQ_NP_SPLIT_DONE = 5,
35944 + FIQ_NP_SPLIT_LS_ABORTED = 6,
35945 + /* This differentiates a HS transaction error from a LS one
35946 + * (handling the hub state is different) */
35947 + FIQ_NP_SPLIT_HS_ABORTED = 7,
35948 +
35949 + /* Periodic state groups */
35950 + /* Periodic transactions are either started directly by the IRQ handler
35951 + * or deferred if the TT is already in use.
35952 + */
35953 + FIQ_PER_SSPLIT_QUEUED = 8,
35954 + FIQ_PER_SSPLIT_STARTED = 9,
35955 + FIQ_PER_SSPLIT_LAST = 10,
35956 +
35957 +
35958 + FIQ_PER_ISO_OUT_PENDING = 11,
35959 + FIQ_PER_ISO_OUT_ACTIVE = 12,
35960 + FIQ_PER_ISO_OUT_LAST = 13,
35961 + FIQ_PER_ISO_OUT_DONE = 27,
35962 +
35963 + FIQ_PER_CSPLIT_WAIT = 14,
35964 + FIQ_PER_CSPLIT_NYET1 = 15,
35965 + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
35966 + FIQ_PER_CSPLIT_NYET_FAFF = 29,
35967 + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
35968 + FIQ_PER_CSPLIT_POLL = 16,
35969 + /* The last CSPLIT for a transaction has been issued, differentiates
35970 + * for the state machine to queue the next packet.
35971 + */
35972 + FIQ_PER_CSPLIT_LAST = 17,
35973 +
35974 + FIQ_PER_SPLIT_DONE = 18,
35975 + FIQ_PER_SPLIT_LS_ABORTED = 19,
35976 + FIQ_PER_SPLIT_HS_ABORTED = 20,
35977 + FIQ_PER_SPLIT_NYET_ABORTED = 21,
35978 + /* Frame rollover has occurred without the transaction finishing. */
35979 + FIQ_PER_SPLIT_TIMEOUT = 22,
35980 +
35981 + /* FIQ-accelerated HS Isochronous state groups */
35982 + FIQ_HS_ISOC_TURBO = 23,
35983 + /* For interval > 1, SOF wakes up the isochronous FSM */
35984 + FIQ_HS_ISOC_SLEEPING = 24,
35985 + FIQ_HS_ISOC_DONE = 25,
35986 + FIQ_HS_ISOC_ABORTED = 26,
35987 + FIQ_DEQUEUE_ISSUED = 30,
35988 + FIQ_TEST = 32,
35989 +};
35990 +
35991 +struct fiq_stack {
35992 + int magic1;
35993 + uint8_t stack[2048];
35994 + int magic2;
35995 +};
35996 +
35997 +
35998 +/**
35999 + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
36000 + * @index: Number of slots reported used for IN transactions / number of slots
36001 + * transmitted for an OUT transaction
36002 + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
36003 + *
36004 + * Split transaction transfers can have variable length depending on other bus
36005 + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
36006 + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
36007 + * can happen per-frame.
36008 + */
36009 +struct fiq_dma_info {
36010 + u8 index;
36011 + u8 slot_len[6];
36012 +};
36013 +
36014 +struct __attribute__((packed)) fiq_split_dma_slot {
36015 + u8 buf[188];
36016 +};
36017 +
36018 +struct fiq_dma_channel {
36019 + struct __attribute__((packed)) fiq_split_dma_slot index[6];
36020 +};
36021 +
36022 +struct fiq_dma_blob {
36023 + struct __attribute__((packed)) fiq_dma_channel channel[0];
36024 +};
36025 +
36026 +/**
36027 + * struct fiq_hs_isoc_info - USB2.0 isochronous data
36028 + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
36029 + * @nrframes: Total length of iso_frame_desc array
36030 + * @index: Current index (FIQ-maintained)
36031 + * @stride: Interval in uframes between HS isoc transactions
36032 + */
36033 +struct fiq_hs_isoc_info {
36034 + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
36035 + unsigned int nrframes;
36036 + unsigned int index;
36037 + unsigned int stride;
36038 +};
36039 +
36040 +/**
36041 + * struct fiq_channel_state - FIQ state machine storage
36042 + * @fsm: Current state of the channel as understood by the FIQ
36043 + * @nr_errors: Number of transaction errors on this split-transaction
36044 + * @hub_addr: SSPLIT/CSPLIT destination hub
36045 + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
36046 + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
36047 + * split-IN, number of CSPLIT data packets that were received.
36048 + * @hcchar_copy:
36049 + * @hcsplt_copy:
36050 + * @hcintmsk_copy:
36051 + * @hctsiz_copy: Copies of the host channel registers.
36052 + * For use as scratch, or for returning state.
36053 + *
36054 + * The fiq_channel_state is state storage between interrupts for a host channel. The
36055 + * FSM state is stored here. Members of this structure must only be set up by the
36056 + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
36057 + * has updated the state to either a COMPLETE state group or ABORT state group.
36058 + */
36059 +
36060 +struct fiq_channel_state {
36061 + enum fiq_fsm_state fsm;
36062 + unsigned int nr_errors;
36063 + unsigned int hub_addr;
36064 + unsigned int port_addr;
36065 + /* Hardware bug workaround: sometimes channel halt interrupts are
36066 + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
36067 + unsigned int expected_uframe;
36068 + /* number of uframes remaining (for interval > 1 HS isoc transfers) before next transfer */
36069 + unsigned int uframe_sleeps;
36070 + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
36071 + unsigned int nrpackets;
36072 + struct fiq_dma_info dma_info;
36073 + struct fiq_hs_isoc_info hs_isoc_info;
36074 + /* Copies of HC registers - in/out communication from/to IRQ handler
36075 + * and for ease of channel setup. A bit of mungeing is performed - for
36076 + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
36077 + */
36078 + hcchar_data_t hcchar_copy;
36079 + hcsplt_data_t hcsplt_copy;
36080 + hcint_data_t hcint_copy;
36081 + hcintmsk_data_t hcintmsk_copy;
36082 + hctsiz_data_t hctsiz_copy;
36083 + hcdma_data_t hcdma_copy;
36084 +};
36085 +
36086 +/**
36087 + * struct fiq_state - top-level FIQ state machine storage
36088 + * @mphi_regs: virtual address of the MPHI peripheral register file
36089 + * @dwc_regs_base: virtual address of the base of the DWC core register file
36090 + * @dma_base: physical address for the base of the DMA bounce buffers
36091 + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
36092 + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
36093 + * Used for determining which interrupts fired to set off the IRQ handler.
36094 + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
36095 + * @np_count: Non-periodic transactions in the active queue
36096 + * @np_sent: Count of non-periodic transactions that have completed
36097 + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
36098 + * this is the next frame on which a SOF interrupt is required. Used to hold off
36099 + * passing SOF through to the driver until necessary.
36100 + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
36101 + * channels configured into the core logic.
36102 + *
36103 + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
36104 + * It contains top-level state information.
36105 + */
36106 +struct fiq_state {
36107 + fiq_lock_t lock;
36108 + mphi_regs_t mphi_regs;
36109 + void *dwc_regs_base;
36110 + dma_addr_t dma_base;
36111 + struct fiq_dma_blob *fiq_dmab;
36112 + void *dummy_send;
36113 + gintmsk_data_t gintmsk_saved;
36114 + haintmsk_data_t haintmsk_saved;
36115 + int mphi_int_count;
36116 + unsigned int fiq_done;
36117 + unsigned int kick_np_queues;
36118 + unsigned int next_sched_frame;
36119 +#ifdef FIQ_DEBUG
36120 + char * buffer;
36121 + unsigned int bufsiz;
36122 +#endif
36123 + struct fiq_channel_state channel[0];
36124 +};
36125 +
36126 +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
36127 +
36128 +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
36129 +
36130 +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
36131 +
36132 +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
36133 +
36134 +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
36135 +
36136 +extern void dwc_otg_fiq_nop(struct fiq_state *state);
36137 +
36138 +#endif /* DWC_OTG_FIQ_FSM_H_ */
36139 --- /dev/null
36140 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
36141 @@ -0,0 +1,80 @@
36142 +/*
36143 + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
36144 + *
36145 + * Copyright (c) 2013 Raspberry Pi Foundation
36146 + *
36147 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
36148 + * All rights reserved.
36149 + *
36150 + * Redistribution and use in source and binary forms, with or without
36151 + * modification, are permitted provided that the following conditions are met:
36152 + * * Redistributions of source code must retain the above copyright
36153 + * notice, this list of conditions and the following disclaimer.
36154 + * * Redistributions in binary form must reproduce the above copyright
36155 + * notice, this list of conditions and the following disclaimer in the
36156 + * documentation and/or other materials provided with the distribution.
36157 + * * Neither the name of Raspberry Pi nor the
36158 + * names of its contributors may be used to endorse or promote products
36159 + * derived from this software without specific prior written permission.
36160 + *
36161 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
36162 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36163 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36164 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
36165 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36166 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36167 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36168 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36169 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36170 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36171 + */
36172 +
36173 +
36174 +#include <asm/assembler.h>
36175 +#include <linux/linkage.h>
36176 +
36177 +
36178 +.text
36179 +
36180 +.global _dwc_otg_fiq_stub_end;
36181 +
36182 +/**
36183 + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
36184 + * a C-style function call with arguments from the FIQ banked registers.
36185 + * r0 = &hcd->fiq_state
36186 + * r1 = &hcd->num_channels
36187 + * r2 = &hcd->dma_buffers
36188 + * Tramples: r0, r1, r2, r4, fp, ip
36189 + */
36190 +
36191 +ENTRY(_dwc_otg_fiq_stub)
36192 + /* Stash unbanked regs - SP will have been set up for us */
36193 + mov ip, sp;
36194 + stmdb sp!, {r0-r12, lr};
36195 +#ifdef FIQ_DEBUG
36196 + // Cycle profiling - read cycle counter at start
36197 + mrc p15, 0, r5, c15, c12, 1;
36198 +#endif
36199 + /* r11 = fp, don't trample it */
36200 + mov r4, fp;
36201 + /* set EABI frame size */
36202 + sub fp, ip, #512;
36203 +
36204 + /* for fiq NOP mode - just need state */
36205 + mov r0, r8;
36206 + /* r9 = num_channels */
36207 + mov r1, r9;
36208 + /* r10 = struct *dma_bufs */
36209 +// mov r2, r10;
36210 +
36211 + /* r4 = &fiq_c_function */
36212 + blx r4;
36213 +#ifdef FIQ_DEBUG
36214 + mrc p15, 0, r4, c15, c12, 1;
36215 + subs r5, r5, r4;
36216 + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
36217 +#endif
36218 + ldmia sp!, {r0-r12, lr};
36219 + subs pc, lr, #4;
36220 +_dwc_otg_fiq_stub_end:
36221 +END(_dwc_otg_fiq_stub)
36222 --- /dev/null
36223 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
36224 @@ -0,0 +1,4260 @@
36225 +
36226 +/* ==========================================================================
36227 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
36228 + * $Revision: #104 $
36229 + * $Date: 2011/10/24 $
36230 + * $Change: 1871159 $
36231 + *
36232 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
36233 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
36234 + * otherwise expressly agreed to in writing between Synopsys and you.
36235 + *
36236 + * The Software IS NOT an item of Licensed Software or Licensed Product under
36237 + * any End User Software License Agreement or Agreement for Licensed Product
36238 + * with Synopsys or any supplement thereto. You are permitted to use and
36239 + * redistribute this Software in source and binary forms, with or without
36240 + * modification, provided that redistributions of source code must retain this
36241 + * notice. You may not view, use, disclose, copy or distribute this file or
36242 + * any information contained herein except pursuant to this license grant from
36243 + * Synopsys. If you do not agree with this notice, including the disclaimer
36244 + * below, then you are not authorized to use the Software.
36245 + *
36246 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
36247 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36248 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36249 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
36250 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36251 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36252 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36253 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36254 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36255 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
36256 + * DAMAGE.
36257 + * ========================================================================== */
36258 +#ifndef DWC_DEVICE_ONLY
36259 +
36260 +/** @file
36261 + * This file implements HCD Core. All code in this file is portable and doesn't
36262 + * use any OS specific functions.
36263 + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
36264 + * header file.
36265 + */
36266 +
36267 +#include <linux/usb.h>
36268 +#include <linux/usb/hcd.h>
36269 +
36270 +#include "dwc_otg_hcd.h"
36271 +#include "dwc_otg_regs.h"
36272 +#include "dwc_otg_fiq_fsm.h"
36273 +
36274 +extern bool microframe_schedule;
36275 +extern uint16_t fiq_fsm_mask, nak_holdoff;
36276 +
36277 +//#define DEBUG_HOST_CHANNELS
36278 +#ifdef DEBUG_HOST_CHANNELS
36279 +static int last_sel_trans_num_per_scheduled = 0;
36280 +static int last_sel_trans_num_nonper_scheduled = 0;
36281 +static int last_sel_trans_num_avail_hc_at_start = 0;
36282 +static int last_sel_trans_num_avail_hc_at_end = 0;
36283 +#endif /* DEBUG_HOST_CHANNELS */
36284 +
36285 +
36286 +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
36287 +{
36288 + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
36289 +}
36290 +
36291 +/**
36292 + * Connection timeout function. An OTG host is required to display a
36293 + * message if the device does not connect within 10 seconds.
36294 + */
36295 +void dwc_otg_hcd_connect_timeout(void *ptr)
36296 +{
36297 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
36298 + DWC_PRINTF("Connect Timeout\n");
36299 + __DWC_ERROR("Device Not Connected/Responding\n");
36300 +}
36301 +
36302 +#if defined(DEBUG)
36303 +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
36304 +{
36305 + if (qh->channel != NULL) {
36306 + dwc_hc_t *hc = qh->channel;
36307 + dwc_list_link_t *item;
36308 + dwc_otg_qh_t *qh_item;
36309 + int num_channels = hcd->core_if->core_params->host_channels;
36310 + int i;
36311 +
36312 + dwc_otg_hc_regs_t *hc_regs;
36313 + hcchar_data_t hcchar;
36314 + hcsplt_data_t hcsplt;
36315 + hctsiz_data_t hctsiz;
36316 + uint32_t hcdma;
36317 +
36318 + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
36319 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
36320 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
36321 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
36322 + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
36323 +
36324 + DWC_PRINTF(" Assigned to channel %p:\n", hc);
36325 + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
36326 + hcsplt.d32);
36327 + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
36328 + hcdma);
36329 + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
36330 + hc->dev_addr, hc->ep_num, hc->ep_is_in);
36331 + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
36332 + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
36333 + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
36334 + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
36335 + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
36336 + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
36337 + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
36338 + DWC_PRINTF(" qh: %p\n", hc->qh);
36339 + DWC_PRINTF(" NP inactive sched:\n");
36340 + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
36341 + qh_item =
36342 + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36343 + DWC_PRINTF(" %p\n", qh_item);
36344 + }
36345 + DWC_PRINTF(" NP active sched:\n");
36346 + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
36347 + qh_item =
36348 + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36349 + DWC_PRINTF(" %p\n", qh_item);
36350 + }
36351 + DWC_PRINTF(" Channels: \n");
36352 + for (i = 0; i < num_channels; i++) {
36353 + dwc_hc_t *hc = hcd->hc_ptr_array[i];
36354 + DWC_PRINTF(" %2d: %p\n", i, hc);
36355 + }
36356 + }
36357 +}
36358 +#else
36359 +#define dump_channel_info(hcd, qh)
36360 +#endif /* DEBUG */
36361 +
36362 +/**
36363 + * Work queue function for starting the HCD when A-Cable is connected.
36364 + * The hcd_start() must be called in a process context.
36365 + */
36366 +static void hcd_start_func(void *_vp)
36367 +{
36368 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
36369 +
36370 + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
36371 + if (hcd) {
36372 + hcd->fops->start(hcd);
36373 + }
36374 +}
36375 +
36376 +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
36377 +{
36378 +#ifdef DEBUG
36379 + int i;
36380 + int num_channels = hcd->core_if->core_params->host_channels;
36381 + for (i = 0; i < num_channels; i++) {
36382 + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
36383 + }
36384 +#endif
36385 +}
36386 +
36387 +static void del_timers(dwc_otg_hcd_t * hcd)
36388 +{
36389 + del_xfer_timers(hcd);
36390 + DWC_TIMER_CANCEL(hcd->conn_timer);
36391 +}
36392 +
36393 +/**
36394 + * Processes all the URBs in a single list of QHs. Completes them with
36395 + * -ESHUTDOWN and frees the QTD.
36396 + */
36397 +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
36398 +{
36399 + dwc_list_link_t *qh_item, *qh_tmp;
36400 + dwc_otg_qh_t *qh;
36401 + dwc_otg_qtd_t *qtd, *qtd_tmp;
36402 +
36403 + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
36404 + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
36405 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
36406 + &qh->qtd_list, qtd_list_entry) {
36407 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
36408 + if (qtd->urb != NULL) {
36409 + hcd->fops->complete(hcd, qtd->urb->priv,
36410 + qtd->urb, -DWC_E_SHUTDOWN);
36411 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
36412 + }
36413 +
36414 + }
36415 + if(qh->channel) {
36416 + /* Using hcchar.chen == 1 is not a reliable test.
36417 + * It is possible that the channel has already halted
36418 + * but not yet been through the IRQ handler.
36419 + */
36420 + dwc_otg_hc_halt(hcd->core_if, qh->channel,
36421 + DWC_OTG_HC_XFER_URB_DEQUEUE);
36422 + if(microframe_schedule)
36423 + hcd->available_host_channels++;
36424 + qh->channel = NULL;
36425 + }
36426 + dwc_otg_hcd_qh_remove(hcd, qh);
36427 + }
36428 +}
36429 +
36430 +/**
36431 + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
36432 + * and periodic schedules. The QTD associated with each URB is removed from
36433 + * the schedule and freed. This function may be called when a disconnect is
36434 + * detected or when the HCD is being stopped.
36435 + */
36436 +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
36437 +{
36438 + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
36439 + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
36440 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
36441 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
36442 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
36443 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
36444 +}
36445 +
36446 +/**
36447 + * Start the connection timer. An OTG host is required to display a
36448 + * message if the device does not connect within 10 seconds. The
36449 + * timer is deleted if a port connect interrupt occurs before the
36450 + * timer expires.
36451 + */
36452 +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
36453 +{
36454 + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
36455 +}
36456 +
36457 +/**
36458 + * HCD Callback function for disconnect of the HCD.
36459 + *
36460 + * @param p void pointer to the <code>struct usb_hcd</code>
36461 + */
36462 +static int32_t dwc_otg_hcd_session_start_cb(void *p)
36463 +{
36464 + dwc_otg_hcd_t *dwc_otg_hcd;
36465 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
36466 + dwc_otg_hcd = p;
36467 + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
36468 + return 1;
36469 +}
36470 +
36471 +/**
36472 + * HCD Callback function for starting the HCD when A-Cable is
36473 + * connected.
36474 + *
36475 + * @param p void pointer to the <code>struct usb_hcd</code>
36476 + */
36477 +static int32_t dwc_otg_hcd_start_cb(void *p)
36478 +{
36479 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36480 + dwc_otg_core_if_t *core_if;
36481 + hprt0_data_t hprt0;
36482 +
36483 + core_if = dwc_otg_hcd->core_if;
36484 +
36485 + if (core_if->op_state == B_HOST) {
36486 + /*
36487 + * Reset the port. During a HNP mode switch the reset
36488 + * needs to occur within 1ms and have a duration of at
36489 + * least 50ms.
36490 + */
36491 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
36492 + hprt0.b.prtrst = 1;
36493 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
36494 + }
36495 + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
36496 + hcd_start_func, dwc_otg_hcd, 50,
36497 + "start hcd");
36498 +
36499 + return 1;
36500 +}
36501 +
36502 +/**
36503 + * HCD Callback function for disconnect of the HCD.
36504 + *
36505 + * @param p void pointer to the <code>struct usb_hcd</code>
36506 + */
36507 +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
36508 +{
36509 + gintsts_data_t intr;
36510 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36511 +
36512 + /*
36513 + * Set status flags for the hub driver.
36514 + */
36515 + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
36516 + dwc_otg_hcd->flags.b.port_connect_status = 0;
36517 + if(fiq_enable)
36518 + local_fiq_disable();
36519 + /*
36520 + * Shutdown any transfers in process by clearing the Tx FIFO Empty
36521 + * interrupt mask and status bits and disabling subsequent host
36522 + * channel interrupts.
36523 + */
36524 + intr.d32 = 0;
36525 + intr.b.nptxfempty = 1;
36526 + intr.b.ptxfempty = 1;
36527 + intr.b.hcintr = 1;
36528 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
36529 + intr.d32, 0);
36530 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
36531 + intr.d32, 0);
36532 +
36533 + del_timers(dwc_otg_hcd);
36534 +
36535 + /*
36536 + * Turn off the vbus power only if the core has transitioned to device
36537 + * mode. If still in host mode, need to keep power on to detect a
36538 + * reconnection.
36539 + */
36540 + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
36541 + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
36542 + hprt0_data_t hprt0 = {.d32 = 0 };
36543 + DWC_PRINTF("Disconnect: PortPower off\n");
36544 + hprt0.b.prtpwr = 0;
36545 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
36546 + hprt0.d32);
36547 + }
36548 +
36549 + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
36550 + }
36551 +
36552 + /* Respond with an error status to all URBs in the schedule. */
36553 + kill_all_urbs(dwc_otg_hcd);
36554 +
36555 + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
36556 + /* Clean up any host channels that were in use. */
36557 + int num_channels;
36558 + int i;
36559 + dwc_hc_t *channel;
36560 + dwc_otg_hc_regs_t *hc_regs;
36561 + hcchar_data_t hcchar;
36562 +
36563 + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
36564 +
36565 + if (!dwc_otg_hcd->core_if->dma_enable) {
36566 + /* Flush out any channel requests in slave mode. */
36567 + for (i = 0; i < num_channels; i++) {
36568 + channel = dwc_otg_hcd->hc_ptr_array[i];
36569 + if (DWC_CIRCLEQ_EMPTY_ENTRY
36570 + (channel, hc_list_entry)) {
36571 + hc_regs =
36572 + dwc_otg_hcd->core_if->
36573 + host_if->hc_regs[i];
36574 + hcchar.d32 =
36575 + DWC_READ_REG32(&hc_regs->hcchar);
36576 + if (hcchar.b.chen) {
36577 + hcchar.b.chen = 0;
36578 + hcchar.b.chdis = 1;
36579 + hcchar.b.epdir = 0;
36580 + DWC_WRITE_REG32
36581 + (&hc_regs->hcchar,
36582 + hcchar.d32);
36583 + }
36584 + }
36585 + }
36586 + }
36587 +
36588 + for (i = 0; i < num_channels; i++) {
36589 + channel = dwc_otg_hcd->hc_ptr_array[i];
36590 + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
36591 + hc_regs =
36592 + dwc_otg_hcd->core_if->host_if->hc_regs[i];
36593 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
36594 + if (hcchar.b.chen) {
36595 + /* Halt the channel. */
36596 + hcchar.b.chdis = 1;
36597 + DWC_WRITE_REG32(&hc_regs->hcchar,
36598 + hcchar.d32);
36599 + }
36600 +
36601 + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
36602 + channel);
36603 + DWC_CIRCLEQ_INSERT_TAIL
36604 + (&dwc_otg_hcd->free_hc_list, channel,
36605 + hc_list_entry);
36606 + /*
36607 + * Added for Descriptor DMA to prevent channel double cleanup
36608 + * in release_channel_ddma(). Which called from ep_disable
36609 + * when device disconnect.
36610 + */
36611 + channel->qh = NULL;
36612 + }
36613 + }
36614 + if(fiq_fsm_enable) {
36615 + for(i=0; i < 128; i++) {
36616 + dwc_otg_hcd->hub_port[i] = 0;
36617 + }
36618 + }
36619 +
36620 + }
36621 +
36622 + if(fiq_enable)
36623 + local_fiq_enable();
36624 +
36625 + if (dwc_otg_hcd->fops->disconnect) {
36626 + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
36627 + }
36628 +
36629 + return 1;
36630 +}
36631 +
36632 +/**
36633 + * HCD Callback function for stopping the HCD.
36634 + *
36635 + * @param p void pointer to the <code>struct usb_hcd</code>
36636 + */
36637 +static int32_t dwc_otg_hcd_stop_cb(void *p)
36638 +{
36639 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36640 +
36641 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
36642 + dwc_otg_hcd_stop(dwc_otg_hcd);
36643 + return 1;
36644 +}
36645 +
36646 +#ifdef CONFIG_USB_DWC_OTG_LPM
36647 +/**
36648 + * HCD Callback function for sleep of HCD.
36649 + *
36650 + * @param p void pointer to the <code>struct usb_hcd</code>
36651 + */
36652 +static int dwc_otg_hcd_sleep_cb(void *p)
36653 +{
36654 + dwc_otg_hcd_t *hcd = p;
36655 +
36656 + dwc_otg_hcd_free_hc_from_lpm(hcd);
36657 +
36658 + return 0;
36659 +}
36660 +#endif
36661 +
36662 +
36663 +/**
36664 + * HCD Callback function for Remote Wakeup.
36665 + *
36666 + * @param p void pointer to the <code>struct usb_hcd</code>
36667 + */
36668 +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
36669 +{
36670 + dwc_otg_hcd_t *hcd = p;
36671 +
36672 + if (hcd->core_if->lx_state == DWC_OTG_L2) {
36673 + hcd->flags.b.port_suspend_change = 1;
36674 + }
36675 +#ifdef CONFIG_USB_DWC_OTG_LPM
36676 + else {
36677 + hcd->flags.b.port_l1_change = 1;
36678 + }
36679 +#endif
36680 + return 0;
36681 +}
36682 +
36683 +/**
36684 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
36685 + * stopped.
36686 + */
36687 +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
36688 +{
36689 + hprt0_data_t hprt0 = {.d32 = 0 };
36690 +
36691 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
36692 +
36693 + /*
36694 + * The root hub should be disconnected before this function is called.
36695 + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
36696 + * and the QH lists (via ..._hcd_endpoint_disable).
36697 + */
36698 +
36699 + /* Turn off all host-specific interrupts. */
36700 + dwc_otg_disable_host_interrupts(hcd->core_if);
36701 +
36702 + /* Turn off the vbus power */
36703 + DWC_PRINTF("PortPower off\n");
36704 + hprt0.b.prtpwr = 0;
36705 + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
36706 + dwc_mdelay(1);
36707 +}
36708 +
36709 +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
36710 + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
36711 + int atomic_alloc)
36712 +{
36713 + int retval = 0;
36714 + uint8_t needs_scheduling = 0;
36715 + dwc_otg_transaction_type_e tr_type;
36716 + dwc_otg_qtd_t *qtd;
36717 + gintmsk_data_t intr_mask = {.d32 = 0 };
36718 + hprt0_data_t hprt0 = { .d32 = 0 };
36719 +
36720 +#ifdef DEBUG /* integrity checks (Broadcom) */
36721 + if (NULL == hcd->core_if) {
36722 + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
36723 + /* No longer connected. */
36724 + return -DWC_E_INVALID;
36725 + }
36726 +#endif
36727 + if (!hcd->flags.b.port_connect_status) {
36728 + /* No longer connected. */
36729 + DWC_ERROR("Not connected\n");
36730 + return -DWC_E_NO_DEVICE;
36731 + }
36732 +
36733 + /* Some core configurations cannot support LS traffic on a FS root port */
36734 + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
36735 + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
36736 + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
36737 + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
36738 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
36739 + return -DWC_E_NO_DEVICE;
36740 + }
36741 + }
36742 +
36743 + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
36744 + if (qtd == NULL) {
36745 + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
36746 + return -DWC_E_NO_MEMORY;
36747 + }
36748 +#ifdef DEBUG /* integrity checks (Broadcom) */
36749 + if (qtd->urb == NULL) {
36750 + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
36751 + return -DWC_E_NO_MEMORY;
36752 + }
36753 + if (qtd->urb->priv == NULL) {
36754 + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
36755 + return -DWC_E_NO_MEMORY;
36756 + }
36757 +#endif
36758 + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
36759 + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
36760 + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
36761 + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
36762 + needs_scheduling = 0;
36763 +
36764 + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
36765 + // creates a new queue in ep_handle if it doesn't exist already
36766 + if (retval < 0) {
36767 + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
36768 + "Error status %d\n", retval);
36769 + dwc_otg_hcd_qtd_free(qtd);
36770 + return retval;
36771 + }
36772 +
36773 + if(needs_scheduling) {
36774 + tr_type = dwc_otg_hcd_select_transactions(hcd);
36775 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
36776 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
36777 + }
36778 + }
36779 + return retval;
36780 +}
36781 +
36782 +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
36783 + dwc_otg_hcd_urb_t * dwc_otg_urb)
36784 +{
36785 + dwc_otg_qh_t *qh;
36786 + dwc_otg_qtd_t *urb_qtd;
36787 + BUG_ON(!hcd);
36788 + BUG_ON(!dwc_otg_urb);
36789 +
36790 +#ifdef DEBUG /* integrity checks (Broadcom) */
36791 +
36792 + if (hcd == NULL) {
36793 + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
36794 + return -DWC_E_INVALID;
36795 + }
36796 + if (dwc_otg_urb == NULL) {
36797 + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
36798 + return -DWC_E_INVALID;
36799 + }
36800 + if (dwc_otg_urb->qtd == NULL) {
36801 + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
36802 + return -DWC_E_INVALID;
36803 + }
36804 + urb_qtd = dwc_otg_urb->qtd;
36805 + BUG_ON(!urb_qtd);
36806 + if (urb_qtd->qh == NULL) {
36807 + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
36808 + return -DWC_E_INVALID;
36809 + }
36810 +#else
36811 + urb_qtd = dwc_otg_urb->qtd;
36812 + BUG_ON(!urb_qtd);
36813 +#endif
36814 + qh = urb_qtd->qh;
36815 + BUG_ON(!qh);
36816 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
36817 + if (urb_qtd->in_process) {
36818 + dump_channel_info(hcd, qh);
36819 + }
36820 + }
36821 +#ifdef DEBUG /* integrity checks (Broadcom) */
36822 + if (hcd->core_if == NULL) {
36823 + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
36824 + return -DWC_E_INVALID;
36825 + }
36826 +#endif
36827 + if (urb_qtd->in_process && qh->channel) {
36828 + /* The QTD is in process (it has been assigned to a channel). */
36829 + if (hcd->flags.b.port_connect_status) {
36830 + int n = qh->channel->hc_num;
36831 + /*
36832 + * If still connected (i.e. in host mode), halt the
36833 + * channel so it can be used for other transfers. If
36834 + * no longer connected, the host registers can't be
36835 + * written to halt the channel since the core is in
36836 + * device mode.
36837 + */
36838 + /* In FIQ FSM mode, we need to shut down carefully.
36839 + * The FIQ may attempt to restart a disabled channel */
36840 + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
36841 + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
36842 + qh->channel->halt_pending = 1;
36843 + hcd->fiq_state->channel[n].fsm = FIQ_DEQUEUE_ISSUED;
36844 + } else {
36845 + dwc_otg_hc_halt(hcd->core_if, qh->channel,
36846 + DWC_OTG_HC_XFER_URB_DEQUEUE);
36847 + }
36848 + }
36849 + }
36850 +
36851 + /*
36852 + * Free the QTD and clean up the associated QH. Leave the QH in the
36853 + * schedule if it has any remaining QTDs.
36854 + */
36855 +
36856 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
36857 + "delete %sQueue handler\n",
36858 + hcd->core_if->dma_desc_enable?"DMA ":"");
36859 + if (!hcd->core_if->dma_desc_enable) {
36860 + uint8_t b = urb_qtd->in_process;
36861 + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
36862 + if (b) {
36863 + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
36864 + qh->channel = NULL;
36865 + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
36866 + dwc_otg_hcd_qh_remove(hcd, qh);
36867 + }
36868 + } else {
36869 + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
36870 + }
36871 + return 0;
36872 +}
36873 +
36874 +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
36875 + int retry)
36876 +{
36877 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
36878 + int retval = 0;
36879 + dwc_irqflags_t flags;
36880 +
36881 + if (retry < 0) {
36882 + retval = -DWC_E_INVALID;
36883 + goto done;
36884 + }
36885 +
36886 + if (!qh) {
36887 + retval = -DWC_E_INVALID;
36888 + goto done;
36889 + }
36890 +
36891 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36892 +
36893 + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
36894 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36895 + retry--;
36896 + dwc_msleep(5);
36897 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36898 + }
36899 +
36900 + dwc_otg_hcd_qh_remove(hcd, qh);
36901 +
36902 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36903 + /*
36904 + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
36905 + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
36906 + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
36907 + * and dwc_otg_hcd_frame_list_alloc().
36908 + */
36909 + dwc_otg_hcd_qh_free(hcd, qh);
36910 +
36911 +done:
36912 + return retval;
36913 +}
36914 +
36915 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
36916 +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
36917 +{
36918 + int retval = 0;
36919 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
36920 + if (!qh)
36921 + return -DWC_E_INVALID;
36922 +
36923 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
36924 + return retval;
36925 +}
36926 +#endif
36927 +
36928 +/**
36929 + * HCD Callback structure for handling mode switching.
36930 + */
36931 +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
36932 + .start = dwc_otg_hcd_start_cb,
36933 + .stop = dwc_otg_hcd_stop_cb,
36934 + .disconnect = dwc_otg_hcd_disconnect_cb,
36935 + .session_start = dwc_otg_hcd_session_start_cb,
36936 + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
36937 +#ifdef CONFIG_USB_DWC_OTG_LPM
36938 + .sleep = dwc_otg_hcd_sleep_cb,
36939 +#endif
36940 + .p = 0,
36941 +};
36942 +
36943 +/**
36944 + * Reset tasklet function
36945 + */
36946 +static void reset_tasklet_func(void *data)
36947 +{
36948 + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
36949 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
36950 + hprt0_data_t hprt0;
36951 +
36952 + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
36953 +
36954 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
36955 + hprt0.b.prtrst = 1;
36956 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
36957 + dwc_mdelay(60);
36958 +
36959 + hprt0.b.prtrst = 0;
36960 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
36961 + dwc_otg_hcd->flags.b.port_reset_change = 1;
36962 +}
36963 +
36964 +static void completion_tasklet_func(void *ptr)
36965 +{
36966 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
36967 + struct urb *urb;
36968 + urb_tq_entry_t *item;
36969 + dwc_irqflags_t flags;
36970 +
36971 + /* This could just be spin_lock_irq */
36972 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36973 + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
36974 + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
36975 + urb = item->urb;
36976 + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
36977 + urb_tq_entries);
36978 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36979 + DWC_FREE(item);
36980 +
36981 + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
36982 +
36983 +
36984 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36985 + }
36986 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36987 + return;
36988 +}
36989 +
36990 +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
36991 +{
36992 + dwc_list_link_t *item;
36993 + dwc_otg_qh_t *qh;
36994 + dwc_irqflags_t flags;
36995 +
36996 + if (!qh_list->next) {
36997 + /* The list hasn't been initialized yet. */
36998 + return;
36999 + }
37000 + /*
37001 + * Hold spinlock here. Not needed in that case if bellow
37002 + * function is being called from ISR
37003 + */
37004 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37005 + /* Ensure there are no QTDs or URBs left. */
37006 + kill_urbs_in_qh_list(hcd, qh_list);
37007 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37008 +
37009 + DWC_LIST_FOREACH(item, qh_list) {
37010 + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
37011 + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
37012 + }
37013 +}
37014 +
37015 +/**
37016 + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
37017 + * Device during SRP time by host power up.
37018 + */
37019 +void dwc_otg_hcd_power_up(void *ptr)
37020 +{
37021 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
37022 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
37023 +
37024 + DWC_PRINTF("%s called\n", __FUNCTION__);
37025 +
37026 + if (!core_if->hibernation_suspend) {
37027 + DWC_PRINTF("Already exited from Hibernation\n");
37028 + return;
37029 + }
37030 +
37031 + /* Switch on the voltage to the core */
37032 + gpwrdn.b.pwrdnswtch = 1;
37033 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37034 + dwc_udelay(10);
37035 +
37036 + /* Reset the core */
37037 + gpwrdn.d32 = 0;
37038 + gpwrdn.b.pwrdnrstn = 1;
37039 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37040 + dwc_udelay(10);
37041 +
37042 + /* Disable power clamps */
37043 + gpwrdn.d32 = 0;
37044 + gpwrdn.b.pwrdnclmp = 1;
37045 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37046 +
37047 + /* Remove reset the core signal */
37048 + gpwrdn.d32 = 0;
37049 + gpwrdn.b.pwrdnrstn = 1;
37050 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
37051 + dwc_udelay(10);
37052 +
37053 + /* Disable PMU interrupt */
37054 + gpwrdn.d32 = 0;
37055 + gpwrdn.b.pmuintsel = 1;
37056 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37057 +
37058 + core_if->hibernation_suspend = 0;
37059 +
37060 + /* Disable PMU */
37061 + gpwrdn.d32 = 0;
37062 + gpwrdn.b.pmuactv = 1;
37063 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37064 + dwc_udelay(10);
37065 +
37066 + /* Enable VBUS */
37067 + gpwrdn.d32 = 0;
37068 + gpwrdn.b.dis_vbus = 1;
37069 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37070 +
37071 + core_if->op_state = A_HOST;
37072 + dwc_otg_core_init(core_if);
37073 + dwc_otg_enable_global_interrupts(core_if);
37074 + cil_hcd_start(core_if);
37075 +}
37076 +
37077 +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
37078 +{
37079 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
37080 + struct fiq_dma_blob *blob = hcd->fiq_dmab;
37081 + int i;
37082 +
37083 + st->fsm = FIQ_PASSTHROUGH;
37084 + st->hcchar_copy.d32 = 0;
37085 + st->hcsplt_copy.d32 = 0;
37086 + st->hcint_copy.d32 = 0;
37087 + st->hcintmsk_copy.d32 = 0;
37088 + st->hctsiz_copy.d32 = 0;
37089 + st->hcdma_copy.d32 = 0;
37090 + st->nr_errors = 0;
37091 + st->hub_addr = 0;
37092 + st->port_addr = 0;
37093 + st->expected_uframe = 0;
37094 + st->nrpackets = 0;
37095 + st->dma_info.index = 0;
37096 + for (i = 0; i < 6; i++)
37097 + st->dma_info.slot_len[i] = 255;
37098 + st->hs_isoc_info.index = 0;
37099 + st->hs_isoc_info.iso_desc = NULL;
37100 + st->hs_isoc_info.nrframes = 0;
37101 +
37102 + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
37103 +}
37104 +
37105 +/**
37106 + * Frees secondary storage associated with the dwc_otg_hcd structure contained
37107 + * in the struct usb_hcd field.
37108 + */
37109 +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
37110 +{
37111 + struct device *dev = dwc_otg_hcd_to_dev(dwc_otg_hcd);
37112 + int i;
37113 +
37114 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
37115 +
37116 + del_timers(dwc_otg_hcd);
37117 +
37118 + /* Free memory for QH/QTD lists */
37119 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
37120 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
37121 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
37122 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
37123 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
37124 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
37125 +
37126 + /* Free memory for the host channels. */
37127 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
37128 + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
37129 +
37130 +#ifdef DEBUG
37131 + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
37132 + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
37133 + }
37134 +#endif
37135 + if (hc != NULL) {
37136 + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
37137 + i, hc);
37138 + DWC_FREE(hc);
37139 + }
37140 + }
37141 +
37142 + if (dwc_otg_hcd->core_if->dma_enable) {
37143 + if (dwc_otg_hcd->status_buf_dma) {
37144 + DWC_DMA_FREE(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
37145 + dwc_otg_hcd->status_buf,
37146 + dwc_otg_hcd->status_buf_dma);
37147 + }
37148 + } else if (dwc_otg_hcd->status_buf != NULL) {
37149 + DWC_FREE(dwc_otg_hcd->status_buf);
37150 + }
37151 + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
37152 + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
37153 + /* Set core_if's lock pointer to NULL */
37154 + dwc_otg_hcd->core_if->lock = NULL;
37155 +
37156 + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
37157 + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
37158 + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
37159 + DWC_FREE(dwc_otg_hcd->fiq_state);
37160 +
37161 +#ifdef DWC_DEV_SRPCAP
37162 + if (dwc_otg_hcd->core_if->power_down == 2 &&
37163 + dwc_otg_hcd->core_if->pwron_timer) {
37164 + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
37165 + }
37166 +#endif
37167 + DWC_FREE(dwc_otg_hcd);
37168 +}
37169 +
37170 +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
37171 +
37172 +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
37173 +{
37174 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
37175 + int retval = 0;
37176 + int num_channels;
37177 + int i;
37178 + dwc_hc_t *channel;
37179 +
37180 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
37181 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
37182 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->channel_lock);
37183 +#else
37184 + hcd->lock = DWC_SPINLOCK_ALLOC();
37185 + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
37186 +#endif
37187 + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
37188 + hcd, core_if);
37189 + if (!hcd->lock) {
37190 + DWC_ERROR("Could not allocate lock for pcd");
37191 + DWC_FREE(hcd);
37192 + retval = -DWC_E_NO_MEMORY;
37193 + goto out;
37194 + }
37195 + hcd->core_if = core_if;
37196 +
37197 + /* Register the HCD CIL Callbacks */
37198 + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
37199 + &hcd_cil_callbacks, hcd);
37200 +
37201 + /* Initialize the non-periodic schedule. */
37202 + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
37203 + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
37204 +
37205 + /* Initialize the periodic schedule. */
37206 + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
37207 + DWC_LIST_INIT(&hcd->periodic_sched_ready);
37208 + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
37209 + DWC_LIST_INIT(&hcd->periodic_sched_queued);
37210 + DWC_TAILQ_INIT(&hcd->completed_urb_list);
37211 + /*
37212 + * Create a host channel descriptor for each host channel implemented
37213 + * in the controller. Initialize the channel descriptor array.
37214 + */
37215 + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
37216 + num_channels = hcd->core_if->core_params->host_channels;
37217 + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
37218 + for (i = 0; i < num_channels; i++) {
37219 + channel = DWC_ALLOC(sizeof(dwc_hc_t));
37220 + if (channel == NULL) {
37221 + retval = -DWC_E_NO_MEMORY;
37222 + DWC_ERROR("%s: host channel allocation failed\n",
37223 + __func__);
37224 + dwc_otg_hcd_free(hcd);
37225 + goto out;
37226 + }
37227 + channel->hc_num = i;
37228 + hcd->hc_ptr_array[i] = channel;
37229 +#ifdef DEBUG
37230 + hcd->core_if->hc_xfer_timer[i] =
37231 + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
37232 + &hcd->core_if->hc_xfer_info[i]);
37233 +#endif
37234 + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
37235 + channel);
37236 + }
37237 +
37238 + if (fiq_enable) {
37239 + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
37240 + if (!hcd->fiq_state) {
37241 + retval = -DWC_E_NO_MEMORY;
37242 + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
37243 + dwc_otg_hcd_free(hcd);
37244 + goto out;
37245 + }
37246 + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
37247 +
37248 + for (i = 0; i < num_channels; i++) {
37249 + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
37250 + }
37251 + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
37252 +
37253 + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
37254 + if (!hcd->fiq_stack) {
37255 + retval = -DWC_E_NO_MEMORY;
37256 + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
37257 + dwc_otg_hcd_free(hcd);
37258 + goto out;
37259 + }
37260 + hcd->fiq_stack->magic1 = 0xDEADBEEF;
37261 + hcd->fiq_stack->magic2 = 0xD00DFEED;
37262 + hcd->fiq_state->gintmsk_saved.d32 = ~0;
37263 + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
37264 +
37265 + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
37266 + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
37267 + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
37268 + * moderately readable array casts.
37269 + */
37270 + hcd->fiq_dmab = DWC_DMA_ALLOC(dev, (sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
37271 + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
37272 + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
37273 + sizeof(struct fiq_dma_channel) * num_channels);
37274 +
37275 + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
37276 +
37277 + /* pointer for debug in fiq_print */
37278 + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
37279 + if (fiq_fsm_enable) {
37280 + int i;
37281 + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
37282 + dwc_otg_cleanup_fiq_channel(hcd, i);
37283 + }
37284 + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
37285 + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
37286 + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
37287 + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
37288 + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
37289 + }
37290 + }
37291 +
37292 + /* Initialize the Connection timeout timer. */
37293 + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
37294 + dwc_otg_hcd_connect_timeout, 0);
37295 +
37296 + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
37297 + if (microframe_schedule)
37298 + init_hcd_usecs(hcd);
37299 +
37300 + /* Initialize reset tasklet. */
37301 + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
37302 +
37303 + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
37304 + completion_tasklet_func, hcd);
37305 +#ifdef DWC_DEV_SRPCAP
37306 + if (hcd->core_if->power_down == 2) {
37307 + /* Initialize Power on timer for Host power up in case hibernation */
37308 + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
37309 + dwc_otg_hcd_power_up, core_if);
37310 + }
37311 +#endif
37312 +
37313 + /*
37314 + * Allocate space for storing data on status transactions. Normally no
37315 + * data is sent, but this space acts as a bit bucket. This must be
37316 + * done after usb_add_hcd since that function allocates the DMA buffer
37317 + * pool.
37318 + */
37319 + if (hcd->core_if->dma_enable) {
37320 + hcd->status_buf =
37321 + DWC_DMA_ALLOC(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
37322 + &hcd->status_buf_dma);
37323 + } else {
37324 + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
37325 + }
37326 + if (!hcd->status_buf) {
37327 + retval = -DWC_E_NO_MEMORY;
37328 + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
37329 + dwc_otg_hcd_free(hcd);
37330 + goto out;
37331 + }
37332 +
37333 + hcd->otg_port = 1;
37334 + hcd->frame_list = NULL;
37335 + hcd->frame_list_dma = 0;
37336 + hcd->periodic_qh_count = 0;
37337 +
37338 + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
37339 +#ifdef FIQ_DEBUG
37340 + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
37341 +#endif
37342 +
37343 +out:
37344 + return retval;
37345 +}
37346 +
37347 +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
37348 +{
37349 + /* Turn off all host-specific interrupts. */
37350 + dwc_otg_disable_host_interrupts(hcd->core_if);
37351 +
37352 + dwc_otg_hcd_free(hcd);
37353 +}
37354 +
37355 +/**
37356 + * Initializes dynamic portions of the DWC_otg HCD state.
37357 + */
37358 +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
37359 +{
37360 + int num_channels;
37361 + int i;
37362 + dwc_hc_t *channel;
37363 + dwc_hc_t *channel_tmp;
37364 +
37365 + hcd->flags.d32 = 0;
37366 +
37367 + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
37368 + if (!microframe_schedule) {
37369 + hcd->non_periodic_channels = 0;
37370 + hcd->periodic_channels = 0;
37371 + } else {
37372 + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
37373 + }
37374 + /*
37375 + * Put all channels in the free channel list and clean up channel
37376 + * states.
37377 + */
37378 + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
37379 + &hcd->free_hc_list, hc_list_entry) {
37380 + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
37381 + }
37382 +
37383 + num_channels = hcd->core_if->core_params->host_channels;
37384 + for (i = 0; i < num_channels; i++) {
37385 + channel = hcd->hc_ptr_array[i];
37386 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
37387 + hc_list_entry);
37388 + dwc_otg_hc_cleanup(hcd->core_if, channel);
37389 + }
37390 +
37391 + /* Initialize the DWC core for host mode operation. */
37392 + dwc_otg_core_host_init(hcd->core_if);
37393 +
37394 + /* Set core_if's lock pointer to the hcd->lock */
37395 + hcd->core_if->lock = hcd->lock;
37396 +}
37397 +
37398 +/**
37399 + * Assigns transactions from a QTD to a free host channel and initializes the
37400 + * host channel to perform the transactions. The host channel is removed from
37401 + * the free list.
37402 + *
37403 + * @param hcd The HCD state structure.
37404 + * @param qh Transactions from the first QTD for this QH are selected and
37405 + * assigned to a free host channel.
37406 + */
37407 +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
37408 +{
37409 + dwc_hc_t *hc;
37410 + dwc_otg_qtd_t *qtd;
37411 + dwc_otg_hcd_urb_t *urb;
37412 + void* ptr = NULL;
37413 + uint32_t intr_enable;
37414 + unsigned long flags;
37415 + gintmsk_data_t gintmsk = { .d32 = 0, };
37416 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
37417 +
37418 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37419 +
37420 + urb = qtd->urb;
37421 +
37422 + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
37423 +
37424 + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
37425 + urb->actual_length = urb->length;
37426 +
37427 +
37428 + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
37429 +
37430 + /* Remove the host channel from the free list. */
37431 + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
37432 +
37433 + qh->channel = hc;
37434 +
37435 + qtd->in_process = 1;
37436 +
37437 + /*
37438 + * Use usb_pipedevice to determine device address. This address is
37439 + * 0 before the SET_ADDRESS command and the correct address afterward.
37440 + */
37441 + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
37442 + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
37443 + hc->speed = qh->dev_speed;
37444 + hc->max_packet = dwc_max_packet(qh->maxp);
37445 +
37446 + hc->xfer_started = 0;
37447 + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
37448 + hc->error_state = (qtd->error_count > 0);
37449 + hc->halt_on_queue = 0;
37450 + hc->halt_pending = 0;
37451 + hc->requests = 0;
37452 +
37453 + /*
37454 + * The following values may be modified in the transfer type section
37455 + * below. The xfer_len value may be reduced when the transfer is
37456 + * started to accommodate the max widths of the XferSize and PktCnt
37457 + * fields in the HCTSIZn register.
37458 + */
37459 +
37460 + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
37461 + if (hc->ep_is_in) {
37462 + hc->do_ping = 0;
37463 + } else {
37464 + hc->do_ping = qh->ping_state;
37465 + }
37466 +
37467 + hc->data_pid_start = qh->data_toggle;
37468 + hc->multi_count = 1;
37469 +
37470 + if (hcd->core_if->dma_enable) {
37471 + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
37472 +
37473 + /* For non-dword aligned case */
37474 + if (((unsigned long)hc->xfer_buff & 0x3)
37475 + && !hcd->core_if->dma_desc_enable) {
37476 + ptr = (uint8_t *) urb->buf + urb->actual_length;
37477 + }
37478 + } else {
37479 + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
37480 + }
37481 + hc->xfer_len = urb->length - urb->actual_length;
37482 + hc->xfer_count = 0;
37483 +
37484 + /*
37485 + * Set the split attributes
37486 + */
37487 + hc->do_split = 0;
37488 + if (qh->do_split) {
37489 + uint32_t hub_addr, port_addr;
37490 + hc->do_split = 1;
37491 + hc->xact_pos = qtd->isoc_split_pos;
37492 + /* We don't need to do complete splits anymore */
37493 +// if(fiq_fsm_enable)
37494 + if (0)
37495 + hc->complete_split = qtd->complete_split = 0;
37496 + else
37497 + hc->complete_split = qtd->complete_split;
37498 +
37499 + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
37500 + hc->hub_addr = (uint8_t) hub_addr;
37501 + hc->port_addr = (uint8_t) port_addr;
37502 + }
37503 +
37504 + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
37505 + case UE_CONTROL:
37506 + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
37507 + switch (qtd->control_phase) {
37508 + case DWC_OTG_CONTROL_SETUP:
37509 + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
37510 + hc->do_ping = 0;
37511 + hc->ep_is_in = 0;
37512 + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
37513 + if (hcd->core_if->dma_enable) {
37514 + hc->xfer_buff = (uint8_t *) urb->setup_dma;
37515 + } else {
37516 + hc->xfer_buff = (uint8_t *) urb->setup_packet;
37517 + }
37518 + hc->xfer_len = 8;
37519 + ptr = NULL;
37520 + break;
37521 + case DWC_OTG_CONTROL_DATA:
37522 + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
37523 + hc->data_pid_start = qtd->data_toggle;
37524 + break;
37525 + case DWC_OTG_CONTROL_STATUS:
37526 + /*
37527 + * Direction is opposite of data direction or IN if no
37528 + * data.
37529 + */
37530 + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
37531 + if (urb->length == 0) {
37532 + hc->ep_is_in = 1;
37533 + } else {
37534 + hc->ep_is_in =
37535 + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
37536 + }
37537 + if (hc->ep_is_in) {
37538 + hc->do_ping = 0;
37539 + }
37540 +
37541 + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
37542 +
37543 + hc->xfer_len = 0;
37544 + if (hcd->core_if->dma_enable) {
37545 + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
37546 + } else {
37547 + hc->xfer_buff = (uint8_t *) hcd->status_buf;
37548 + }
37549 + ptr = NULL;
37550 + break;
37551 + }
37552 + break;
37553 + case UE_BULK:
37554 + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
37555 + break;
37556 + case UE_INTERRUPT:
37557 + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
37558 + break;
37559 + case UE_ISOCHRONOUS:
37560 + {
37561 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
37562 +
37563 + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
37564 +
37565 + if (hcd->core_if->dma_desc_enable)
37566 + break;
37567 +
37568 + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
37569 +
37570 + frame_desc->status = 0;
37571 +
37572 + if (hcd->core_if->dma_enable) {
37573 + hc->xfer_buff = (uint8_t *) urb->dma;
37574 + } else {
37575 + hc->xfer_buff = (uint8_t *) urb->buf;
37576 + }
37577 + hc->xfer_buff +=
37578 + frame_desc->offset + qtd->isoc_split_offset;
37579 + hc->xfer_len =
37580 + frame_desc->length - qtd->isoc_split_offset;
37581 +
37582 + /* For non-dword aligned buffers */
37583 + if (((unsigned long)hc->xfer_buff & 0x3)
37584 + && hcd->core_if->dma_enable) {
37585 + ptr =
37586 + (uint8_t *) urb->buf + frame_desc->offset +
37587 + qtd->isoc_split_offset;
37588 + } else
37589 + ptr = NULL;
37590 +
37591 + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
37592 + if (hc->xfer_len <= 188) {
37593 + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
37594 + } else {
37595 + hc->xact_pos =
37596 + DWC_HCSPLIT_XACTPOS_BEGIN;
37597 + }
37598 + }
37599 + }
37600 + break;
37601 + }
37602 + /* non DWORD-aligned buffer case */
37603 + if (ptr) {
37604 + uint32_t buf_size;
37605 + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
37606 + buf_size = hcd->core_if->core_params->max_transfer_size;
37607 + } else {
37608 + buf_size = 4096;
37609 + }
37610 + if (!qh->dw_align_buf) {
37611 + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(dev, buf_size,
37612 + &qh->dw_align_buf_dma);
37613 + if (!qh->dw_align_buf) {
37614 + DWC_ERROR
37615 + ("%s: Failed to allocate memory to handle "
37616 + "non-dword aligned buffer case\n",
37617 + __func__);
37618 + return;
37619 + }
37620 + }
37621 + if (!hc->ep_is_in) {
37622 + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
37623 + }
37624 + hc->align_buff = qh->dw_align_buf_dma;
37625 + } else {
37626 + hc->align_buff = 0;
37627 + }
37628 +
37629 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
37630 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
37631 + /*
37632 + * This value may be modified when the transfer is started to
37633 + * reflect the actual transfer length.
37634 + */
37635 + hc->multi_count = dwc_hb_mult(qh->maxp);
37636 + }
37637 +
37638 + if (hcd->core_if->dma_desc_enable)
37639 + hc->desc_list_addr = qh->desc_list_dma;
37640 +
37641 + dwc_otg_hc_init(hcd->core_if, hc);
37642 +
37643 + local_irq_save(flags);
37644 +
37645 + if (fiq_enable) {
37646 + local_fiq_disable();
37647 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
37648 + }
37649 +
37650 + /* Enable the top level host channel interrupt. */
37651 + intr_enable = (1 << hc->hc_num);
37652 + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
37653 +
37654 + /* Make sure host channel interrupts are enabled. */
37655 + gintmsk.b.hcintr = 1;
37656 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
37657 +
37658 + if (fiq_enable) {
37659 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
37660 + local_fiq_enable();
37661 + }
37662 +
37663 + local_irq_restore(flags);
37664 + hc->qh = qh;
37665 +}
37666 +
37667 +
37668 +/**
37669 + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
37670 + * @qh: pointer to the endpoint's queue head
37671 + *
37672 + * Transaction start/end control flow is grafted onto the existing dwc_otg
37673 + * mechanisms, to avoid spaghettifying the functions more than they already are.
37674 + * This function's eligibility check is altered by debug parameter.
37675 + *
37676 + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
37677 + */
37678 +
37679 +int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh)
37680 +{
37681 + if (qh->do_split) {
37682 + switch (qh->ep_type) {
37683 + case UE_CONTROL:
37684 + case UE_BULK:
37685 + if (fiq_fsm_mask & (1 << 0))
37686 + return 1;
37687 + break;
37688 + case UE_INTERRUPT:
37689 + case UE_ISOCHRONOUS:
37690 + if (fiq_fsm_mask & (1 << 1))
37691 + return 1;
37692 + break;
37693 + default:
37694 + break;
37695 + }
37696 + } else if (qh->ep_type == UE_ISOCHRONOUS) {
37697 + if (fiq_fsm_mask & (1 << 2)) {
37698 + /* HS ISOCH support. We test for compatibility:
37699 + * - DWORD aligned buffers
37700 + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
37701 + * If yes, then the fsm enqueue function will handle the state machine setup.
37702 + */
37703 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37704 + dwc_otg_hcd_urb_t *urb = qtd->urb;
37705 + struct dwc_otg_hcd_iso_packet_desc (*iso_descs)[0] = &urb->iso_descs;
37706 + int nr_iso_frames = urb->packet_count;
37707 + int i;
37708 + uint32_t ptr;
37709 +
37710 + if (nr_iso_frames < 2)
37711 + return 0;
37712 + for (i = 0; i < nr_iso_frames; i++) {
37713 + ptr = urb->dma + iso_descs[i]->offset;
37714 + if (ptr & 0x3) {
37715 + printk_ratelimited("%s: Non-Dword aligned isochronous frame offset."
37716 + " Cannot queue FIQ-accelerated transfer to device %d endpoint %d\n",
37717 + __FUNCTION__, qh->channel->dev_addr, qh->channel->ep_num);
37718 + return 0;
37719 + }
37720 + }
37721 + return 1;
37722 + }
37723 + }
37724 + return 0;
37725 +}
37726 +
37727 +/**
37728 + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
37729 + * @hcd: Pointer to the dwc_otg_hcd struct
37730 + * @qh: Pointer to the endpoint's queue head
37731 + *
37732 + * Periodic split transactions are transmitted modulo 188 bytes.
37733 + * This necessitates slicing data up into buckets for isochronous out
37734 + * and fixing up the DMA address for all IN transfers.
37735 + *
37736 + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
37737 + * HC buffer has been used.
37738 + */
37739 +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
37740 + {
37741 + int frame_length, i = 0;
37742 + uint8_t *ptr = NULL;
37743 + dwc_hc_t *hc = qh->channel;
37744 + struct fiq_dma_blob *blob;
37745 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
37746 +
37747 + for (i = 0; i < 6; i++) {
37748 + st->dma_info.slot_len[i] = 255;
37749 + }
37750 + st->dma_info.index = 0;
37751 + i = 0;
37752 + if (hc->ep_is_in) {
37753 + /*
37754 + * Set dma_regs to bounce buffer. FIQ will update the
37755 + * state depending on transaction progress.
37756 + */
37757 + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
37758 + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
37759 + /* Calculate the max number of CSPLITS such that the FIQ can time out
37760 + * a transaction if it fails.
37761 + */
37762 + frame_length = st->hcchar_copy.b.mps;
37763 + do {
37764 + i++;
37765 + frame_length -= 188;
37766 + } while (frame_length >= 0);
37767 + st->nrpackets = i;
37768 + return 1;
37769 + } else {
37770 + if (qh->ep_type == UE_ISOCHRONOUS) {
37771 +
37772 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37773 +
37774 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
37775 + frame_length = frame_desc->length;
37776 +
37777 + /* Virtual address for bounce buffers */
37778 + blob = hcd->fiq_dmab;
37779 +
37780 + ptr = qtd->urb->buf + frame_desc->offset;
37781 + if (frame_length == 0) {
37782 + /*
37783 + * for isochronous transactions, we must still transmit a packet
37784 + * even if the length is zero.
37785 + */
37786 + st->dma_info.slot_len[0] = 0;
37787 + st->nrpackets = 1;
37788 + } else {
37789 + do {
37790 + if (frame_length <= 188) {
37791 + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
37792 + st->dma_info.slot_len[i] = frame_length;
37793 + ptr += frame_length;
37794 + } else {
37795 + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
37796 + st->dma_info.slot_len[i] = 188;
37797 + ptr += 188;
37798 + }
37799 + i++;
37800 + frame_length -= 188;
37801 + } while (frame_length > 0);
37802 + st->nrpackets = i;
37803 + }
37804 + ptr = qtd->urb->buf + frame_desc->offset;
37805 + /* Point the HC at the DMA address of the bounce buffers */
37806 + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
37807 + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
37808 +
37809 + /* fixup xfersize to the actual packet size */
37810 + st->hctsiz_copy.b.pid = 0;
37811 + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
37812 + return 1;
37813 + } else {
37814 + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
37815 + return 0;
37816 + }
37817 + }
37818 +}
37819 +
37820 +/*
37821 + * Pushing a periodic request into the queue near the EOF1 point
37822 + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
37823 + * Usually, the request goes out on the bus causing a transfer but
37824 + * the core does not transfer the data to memory.
37825 + * This guard interval (in number of 60MHz clocks) is required which
37826 + * must cater for CPU latency between reading the value and enabling
37827 + * the channel.
37828 + */
37829 +#define PERIODIC_FRREM_BACKOFF 1000
37830 +
37831 +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37832 +{
37833 + dwc_hc_t *hc = qh->channel;
37834 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
37835 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37836 + int frame;
37837 + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
37838 + int xfer_len, nrpackets;
37839 + hcdma_data_t hcdma;
37840 + hfnum_data_t hfnum;
37841 +
37842 + if (st->fsm != FIQ_PASSTHROUGH)
37843 + return 0;
37844 +
37845 + st->nr_errors = 0;
37846 +
37847 + st->hcchar_copy.d32 = 0;
37848 + st->hcchar_copy.b.mps = hc->max_packet;
37849 + st->hcchar_copy.b.epdir = hc->ep_is_in;
37850 + st->hcchar_copy.b.devaddr = hc->dev_addr;
37851 + st->hcchar_copy.b.epnum = hc->ep_num;
37852 + st->hcchar_copy.b.eptype = hc->ep_type;
37853 +
37854 + st->hcintmsk_copy.b.chhltd = 1;
37855 +
37856 + frame = dwc_otg_hcd_get_frame_number(hcd);
37857 + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
37858 +
37859 + st->hcchar_copy.b.lspddev = 0;
37860 + /* Enable the channel later as a final register write. */
37861 +
37862 + st->hcsplt_copy.d32 = 0;
37863 +
37864 + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
37865 + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
37866 + /* grab the next DMA address offset from the array */
37867 + st->hcdma_copy.d32 = qtd->urb->dma;
37868 + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
37869 +
37870 + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
37871 + * the core needs to be told to send the correct number. Caution: for IN transfers,
37872 + * this is always set to the maximum size of the endpoint. */
37873 + xfer_len = st->hs_isoc_info.iso_desc[0].length;
37874 + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
37875 + if (nrpackets == 0)
37876 + nrpackets = 1;
37877 + st->hcchar_copy.b.multicnt = nrpackets;
37878 + st->hctsiz_copy.b.pktcnt = nrpackets;
37879 +
37880 + /* Initial PID also needs to be set */
37881 + if (st->hcchar_copy.b.epdir == 0) {
37882 + st->hctsiz_copy.b.xfersize = xfer_len;
37883 + switch (st->hcchar_copy.b.multicnt) {
37884 + case 1:
37885 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
37886 + break;
37887 + case 2:
37888 + case 3:
37889 + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
37890 + break;
37891 + }
37892 +
37893 + } else {
37894 + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
37895 + switch (st->hcchar_copy.b.multicnt) {
37896 + case 1:
37897 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
37898 + break;
37899 + case 2:
37900 + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
37901 + break;
37902 + case 3:
37903 + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
37904 + break;
37905 + }
37906 + }
37907 +
37908 + st->hs_isoc_info.stride = qh->interval;
37909 + st->uframe_sleeps = 0;
37910 +
37911 + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
37912 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
37913 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
37914 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
37915 + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
37916 + local_fiq_disable();
37917 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
37918 + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
37919 + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
37920 + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
37921 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
37922 + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
37923 + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
37924 + /* Prevent queueing near EOF1. Bad things happen if a periodic
37925 + * split transaction is queued very close to EOF. SOF interrupt handler
37926 + * will wake this channel at the next interrupt.
37927 + */
37928 + st->fsm = FIQ_HS_ISOC_SLEEPING;
37929 + st->uframe_sleeps = 1;
37930 + } else {
37931 + st->fsm = FIQ_HS_ISOC_TURBO;
37932 + st->hcchar_copy.b.chen = 1;
37933 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
37934 + }
37935 + mb();
37936 + st->hcchar_copy.b.chen = 0;
37937 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
37938 + local_fiq_enable();
37939 + return 0;
37940 +}
37941 +
37942 +
37943 +/**
37944 + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
37945 + * @hcd: Pointer to the dwc_otg_hcd struct
37946 + * @qh: Pointer to the endpoint's queue head
37947 + *
37948 + * This overrides the dwc_otg driver's normal method of queueing a transaction.
37949 + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
37950 + * for the nominated host channel.
37951 + *
37952 + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
37953 + * start is possible. If not, then the FIQ is left to start the transfer.
37954 + */
37955 +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37956 +{
37957 + int start_immediate = 1, i;
37958 + hfnum_data_t hfnum;
37959 + dwc_hc_t *hc = qh->channel;
37960 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
37961 + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
37962 + int hub_addr, port_addr, frame, uframe;
37963 + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
37964 +
37965 + if (st->fsm != FIQ_PASSTHROUGH)
37966 + return 0;
37967 + st->nr_errors = 0;
37968 +
37969 + st->hcchar_copy.d32 = 0;
37970 + st->hcchar_copy.b.mps = hc->max_packet;
37971 + st->hcchar_copy.b.epdir = hc->ep_is_in;
37972 + st->hcchar_copy.b.devaddr = hc->dev_addr;
37973 + st->hcchar_copy.b.epnum = hc->ep_num;
37974 + st->hcchar_copy.b.eptype = hc->ep_type;
37975 + if (hc->ep_type & 0x1) {
37976 + if (hc->ep_is_in)
37977 + st->hcchar_copy.b.multicnt = 3;
37978 + else
37979 + /* Docs say set this to 1, but driver sets to 0! */
37980 + st->hcchar_copy.b.multicnt = 0;
37981 + } else {
37982 + st->hcchar_copy.b.multicnt = 1;
37983 + st->hcchar_copy.b.oddfrm = 0;
37984 + }
37985 + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
37986 + /* Enable the channel later as a final register write. */
37987 +
37988 + st->hcsplt_copy.d32 = 0;
37989 + if(qh->do_split) {
37990 + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
37991 + st->hcsplt_copy.b.compsplt = 0;
37992 + st->hcsplt_copy.b.spltena = 1;
37993 + // XACTPOS is for isoc-out only but needs initialising anyway.
37994 + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
37995 + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
37996 + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
37997 + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
37998 + * will update as necessary.
37999 + */
38000 + if (hc->xfer_len > 188) {
38001 + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
38002 + }
38003 + }
38004 + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
38005 + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
38006 + st->hub_addr = hub_addr;
38007 + st->port_addr = port_addr;
38008 + }
38009 +
38010 + st->hctsiz_copy.d32 = 0;
38011 + st->hctsiz_copy.b.dopng = 0;
38012 + st->hctsiz_copy.b.pid = hc->data_pid_start;
38013 +
38014 + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
38015 + hc->xfer_len = hc->max_packet;
38016 + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
38017 + hc->xfer_len = 188;
38018 + }
38019 + st->hctsiz_copy.b.xfersize = hc->xfer_len;
38020 +
38021 + st->hctsiz_copy.b.pktcnt = 1;
38022 +
38023 + if (hc->ep_type & 0x1) {
38024 + /*
38025 + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
38026 + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
38027 + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
38028 + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
38029 + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
38030 + * must not touch internal driver state.
38031 + */
38032 + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
38033 + if (hc->align_buff) {
38034 + st->hcdma_copy.d32 = hc->align_buff;
38035 + } else {
38036 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38037 + }
38038 + }
38039 + } else {
38040 + if (hc->align_buff) {
38041 + st->hcdma_copy.d32 = hc->align_buff;
38042 + } else {
38043 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38044 + }
38045 + }
38046 + /* The FIQ depends upon no other interrupts being enabled except channel halt.
38047 + * Fixup channel interrupt mask. */
38048 + st->hcintmsk_copy.d32 = 0;
38049 + st->hcintmsk_copy.b.chhltd = 1;
38050 + st->hcintmsk_copy.b.ahberr = 1;
38051 +
38052 + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
38053 + * as Control puts the transfer into the non-periodic request queue and the
38054 + * non-periodic handler in the hub. Makes things lots easier.
38055 + */
38056 + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
38057 + st->hcchar_copy.b.multicnt = 0;
38058 + st->hcchar_copy.b.oddfrm = 0;
38059 + st->hcchar_copy.b.eptype = UE_CONTROL;
38060 + if (hc->align_buff) {
38061 + st->hcdma_copy.d32 = hc->align_buff;
38062 + } else {
38063 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38064 + }
38065 + }
38066 + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
38067 + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
38068 + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
38069 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38070 + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
38071 +
38072 + local_fiq_disable();
38073 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38074 +
38075 + if (hc->ep_type & 0x1) {
38076 + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
38077 + frame = (hfnum.b.frnum & ~0x7) >> 3;
38078 + uframe = hfnum.b.frnum & 0x7;
38079 + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
38080 + /* Prevent queueing near EOF1. Bad things happen if a periodic
38081 + * split transaction is queued very close to EOF.
38082 + */
38083 + start_immediate = 0;
38084 + } else if (uframe == 5) {
38085 + start_immediate = 0;
38086 + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
38087 + start_immediate = 0;
38088 + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
38089 + start_immediate = 0;
38090 + } else {
38091 + /* Search through all host channels to determine if a transaction
38092 + * is currently in progress */
38093 + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
38094 + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
38095 + continue;
38096 + switch (hcd->fiq_state->channel[i].fsm) {
38097 + /* TT is reserved for channels that are in the middle of a periodic
38098 + * split transaction.
38099 + */
38100 + case FIQ_PER_SSPLIT_STARTED:
38101 + case FIQ_PER_CSPLIT_WAIT:
38102 + case FIQ_PER_CSPLIT_NYET1:
38103 + case FIQ_PER_CSPLIT_POLL:
38104 + case FIQ_PER_ISO_OUT_ACTIVE:
38105 + case FIQ_PER_ISO_OUT_LAST:
38106 + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
38107 + hcd->fiq_state->channel[i].port_addr == port_addr) {
38108 + start_immediate = 0;
38109 + }
38110 + break;
38111 + default:
38112 + break;
38113 + }
38114 + if (!start_immediate)
38115 + break;
38116 + }
38117 + }
38118 + }
38119 + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
38120 + start_immediate = 1;
38121 +
38122 + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
38123 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
38124 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
38125 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
38126 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
38127 + switch (hc->ep_type) {
38128 + case UE_CONTROL:
38129 + case UE_BULK:
38130 + st->fsm = FIQ_NP_SSPLIT_STARTED;
38131 + break;
38132 + case UE_ISOCHRONOUS:
38133 + if (hc->ep_is_in) {
38134 + if (start_immediate) {
38135 + st->fsm = FIQ_PER_SSPLIT_STARTED;
38136 + } else {
38137 + st->fsm = FIQ_PER_SSPLIT_QUEUED;
38138 + }
38139 + } else {
38140 + if (start_immediate) {
38141 + /* Single-isoc OUT packets don't require FIQ involvement */
38142 + if (st->nrpackets == 1) {
38143 + st->fsm = FIQ_PER_ISO_OUT_LAST;
38144 + } else {
38145 + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
38146 + }
38147 + } else {
38148 + st->fsm = FIQ_PER_ISO_OUT_PENDING;
38149 + }
38150 + }
38151 + break;
38152 + case UE_INTERRUPT:
38153 + if (fiq_fsm_mask & 0x8) {
38154 + st->fsm = FIQ_NP_SSPLIT_STARTED;
38155 + } else if (start_immediate) {
38156 + st->fsm = FIQ_PER_SSPLIT_STARTED;
38157 + } else {
38158 + st->fsm = FIQ_PER_SSPLIT_QUEUED;
38159 + }
38160 + default:
38161 + break;
38162 + }
38163 + if (start_immediate) {
38164 + /* Set the oddfrm bit as close as possible to actual queueing */
38165 + frame = dwc_otg_hcd_get_frame_number(hcd);
38166 + st->expected_uframe = (frame + 1) & 0x3FFF;
38167 + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
38168 + st->hcchar_copy.b.chen = 1;
38169 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38170 + }
38171 + mb();
38172 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38173 + local_fiq_enable();
38174 + return 0;
38175 +}
38176 +
38177 +
38178 +/**
38179 + * This function selects transactions from the HCD transfer schedule and
38180 + * assigns them to available host channels. It is called from HCD interrupt
38181 + * handler functions.
38182 + *
38183 + * @param hcd The HCD state structure.
38184 + *
38185 + * @return The types of new transactions that were assigned to host channels.
38186 + */
38187 +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
38188 +{
38189 + dwc_list_link_t *qh_ptr;
38190 + dwc_otg_qh_t *qh;
38191 + int num_channels;
38192 + dwc_irqflags_t flags;
38193 + dwc_spinlock_t *channel_lock = hcd->channel_lock;
38194 + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
38195 +
38196 +#ifdef DEBUG_HOST_CHANNELS
38197 + last_sel_trans_num_per_scheduled = 0;
38198 + last_sel_trans_num_nonper_scheduled = 0;
38199 + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
38200 +#endif /* DEBUG_HOST_CHANNELS */
38201 +
38202 + /* Process entries in the periodic ready list. */
38203 + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
38204 +
38205 + while (qh_ptr != &hcd->periodic_sched_ready &&
38206 + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
38207 +
38208 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38209 +
38210 + if (microframe_schedule) {
38211 + // Make sure we leave one channel for non periodic transactions.
38212 + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
38213 + if (hcd->available_host_channels <= 1) {
38214 + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
38215 + break;
38216 + }
38217 + hcd->available_host_channels--;
38218 + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
38219 +#ifdef DEBUG_HOST_CHANNELS
38220 + last_sel_trans_num_per_scheduled++;
38221 +#endif /* DEBUG_HOST_CHANNELS */
38222 + }
38223 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38224 + assign_and_init_hc(hcd, qh);
38225 +
38226 + /*
38227 + * Move the QH from the periodic ready schedule to the
38228 + * periodic assigned schedule.
38229 + */
38230 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38231 + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
38232 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
38233 + &qh->qh_list_entry);
38234 + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
38235 + }
38236 +
38237 + /*
38238 + * Process entries in the inactive portion of the non-periodic
38239 + * schedule. Some free host channels may not be used if they are
38240 + * reserved for periodic transfers.
38241 + */
38242 + qh_ptr = hcd->non_periodic_sched_inactive.next;
38243 + num_channels = hcd->core_if->core_params->host_channels;
38244 + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
38245 + (microframe_schedule || hcd->non_periodic_channels <
38246 + num_channels - hcd->periodic_channels) &&
38247 + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
38248 +
38249 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38250 + /*
38251 + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
38252 + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
38253 + * cheeky devices that just hold off using NAKs
38254 + */
38255 + if (fiq_enable && nak_holdoff && qh->do_split) {
38256 + if (qh->nak_frame != 0xffff) {
38257 + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
38258 + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
38259 + if (dwc_frame_num_le(frame, next_frame)) {
38260 + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
38261 + hcd->fiq_state->next_sched_frame = next_frame;
38262 + }
38263 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38264 + continue;
38265 + } else {
38266 + qh->nak_frame = 0xFFFF;
38267 + }
38268 + }
38269 + }
38270 +
38271 + if (microframe_schedule) {
38272 + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
38273 + if (hcd->available_host_channels < 1) {
38274 + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
38275 + break;
38276 + }
38277 + hcd->available_host_channels--;
38278 + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
38279 +#ifdef DEBUG_HOST_CHANNELS
38280 + last_sel_trans_num_nonper_scheduled++;
38281 +#endif /* DEBUG_HOST_CHANNELS */
38282 + }
38283 +
38284 + assign_and_init_hc(hcd, qh);
38285 +
38286 + /*
38287 + * Move the QH from the non-periodic inactive schedule to the
38288 + * non-periodic active schedule.
38289 + */
38290 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38291 + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
38292 + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
38293 + &qh->qh_list_entry);
38294 + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
38295 +
38296 +
38297 + if (!microframe_schedule)
38298 + hcd->non_periodic_channels++;
38299 + }
38300 + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
38301 + * stop the FIQ from kicking us. We could potentially still have elements here if we
38302 + * ran out of host channels.
38303 + */
38304 + if (fiq_enable) {
38305 + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
38306 + hcd->fiq_state->kick_np_queues = 0;
38307 + } else {
38308 + /* For each entry remaining in the NP inactive queue,
38309 + * if this a NAK'd retransmit then don't set the kick flag.
38310 + */
38311 + if(nak_holdoff) {
38312 + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
38313 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38314 + if (qh->nak_frame == 0xFFFF) {
38315 + hcd->fiq_state->kick_np_queues = 1;
38316 + }
38317 + }
38318 + }
38319 + }
38320 + }
38321 + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
38322 + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
38323 +
38324 + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
38325 + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
38326 +
38327 +
38328 +#ifdef DEBUG_HOST_CHANNELS
38329 + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
38330 +#endif /* DEBUG_HOST_CHANNELS */
38331 + return ret_val;
38332 +}
38333 +
38334 +/**
38335 + * Attempts to queue a single transaction request for a host channel
38336 + * associated with either a periodic or non-periodic transfer. This function
38337 + * assumes that there is space available in the appropriate request queue. For
38338 + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
38339 + * is available in the appropriate Tx FIFO.
38340 + *
38341 + * @param hcd The HCD state structure.
38342 + * @param hc Host channel descriptor associated with either a periodic or
38343 + * non-periodic transfer.
38344 + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
38345 + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
38346 + * transfers.
38347 + *
38348 + * @return 1 if a request is queued and more requests may be needed to
38349 + * complete the transfer, 0 if no more requests are required for this
38350 + * transfer, -1 if there is insufficient space in the Tx FIFO.
38351 + */
38352 +static int queue_transaction(dwc_otg_hcd_t * hcd,
38353 + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
38354 +{
38355 + int retval;
38356 +
38357 + if (hcd->core_if->dma_enable) {
38358 + if (hcd->core_if->dma_desc_enable) {
38359 + if (!hc->xfer_started
38360 + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
38361 + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
38362 + hc->qh->ping_state = 0;
38363 + }
38364 + } else if (!hc->xfer_started) {
38365 + if (fiq_fsm_enable && hc->error_state) {
38366 + hcd->fiq_state->channel[hc->hc_num].nr_errors =
38367 + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
38368 + hcd->fiq_state->channel[hc->hc_num].fsm =
38369 + FIQ_PASSTHROUGH_ERRORSTATE;
38370 + }
38371 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38372 + hc->qh->ping_state = 0;
38373 + }
38374 + retval = 0;
38375 + } else if (hc->halt_pending) {
38376 + /* Don't queue a request if the channel has been halted. */
38377 + retval = 0;
38378 + } else if (hc->halt_on_queue) {
38379 + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
38380 + retval = 0;
38381 + } else if (hc->do_ping) {
38382 + if (!hc->xfer_started) {
38383 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38384 + }
38385 + retval = 0;
38386 + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
38387 + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
38388 + if (!hc->xfer_started) {
38389 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38390 + retval = 1;
38391 + } else {
38392 + retval =
38393 + dwc_otg_hc_continue_transfer(hcd->core_if,
38394 + hc);
38395 + }
38396 + } else {
38397 + retval = -1;
38398 + }
38399 + } else {
38400 + if (!hc->xfer_started) {
38401 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38402 + retval = 1;
38403 + } else {
38404 + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
38405 + }
38406 + }
38407 +
38408 + return retval;
38409 +}
38410 +
38411 +/**
38412 + * Processes periodic channels for the next frame and queues transactions for
38413 + * these channels to the DWC_otg controller. After queueing transactions, the
38414 + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
38415 + * to queue as Periodic Tx FIFO or request queue space becomes available.
38416 + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
38417 + */
38418 +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
38419 +{
38420 + hptxsts_data_t tx_status;
38421 + dwc_list_link_t *qh_ptr;
38422 + dwc_otg_qh_t *qh;
38423 + int status = 0;
38424 + int no_queue_space = 0;
38425 + int no_fifo_space = 0;
38426 +
38427 + dwc_otg_host_global_regs_t *host_regs;
38428 + host_regs = hcd->core_if->host_if->host_global_regs;
38429 +
38430 + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
38431 +#ifdef DEBUG
38432 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38433 + DWC_DEBUGPL(DBG_HCDV,
38434 + " P Tx Req Queue Space Avail (before queue): %d\n",
38435 + tx_status.b.ptxqspcavail);
38436 + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
38437 + tx_status.b.ptxfspcavail);
38438 +#endif
38439 +
38440 + qh_ptr = hcd->periodic_sched_assigned.next;
38441 + while (qh_ptr != &hcd->periodic_sched_assigned) {
38442 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38443 + if (tx_status.b.ptxqspcavail == 0) {
38444 + no_queue_space = 1;
38445 + break;
38446 + }
38447 +
38448 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38449 +
38450 + // Do not send a split start transaction any later than frame .6
38451 + // Note, we have to schedule a periodic in .5 to make it go in .6
38452 + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
38453 + {
38454 + qh_ptr = qh_ptr->next;
38455 + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
38456 + continue;
38457 + }
38458 +
38459 + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
38460 + if (qh->do_split)
38461 + fiq_fsm_queue_split_transaction(hcd, qh);
38462 + else
38463 + fiq_fsm_queue_isoc_transaction(hcd, qh);
38464 + } else {
38465 +
38466 + /*
38467 + * Set a flag if we're queueing high-bandwidth in slave mode.
38468 + * The flag prevents any halts to get into the request queue in
38469 + * the middle of multiple high-bandwidth packets getting queued.
38470 + */
38471 + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
38472 + hcd->core_if->queuing_high_bandwidth = 1;
38473 + }
38474 + status = queue_transaction(hcd, qh->channel,
38475 + tx_status.b.ptxfspcavail);
38476 + if (status < 0) {
38477 + no_fifo_space = 1;
38478 + break;
38479 + }
38480 + }
38481 +
38482 + /*
38483 + * In Slave mode, stay on the current transfer until there is
38484 + * nothing more to do or the high-bandwidth request count is
38485 + * reached. In DMA mode, only need to queue one request. The
38486 + * controller automatically handles multiple packets for
38487 + * high-bandwidth transfers.
38488 + */
38489 + if (hcd->core_if->dma_enable || status == 0 ||
38490 + qh->channel->requests == qh->channel->multi_count) {
38491 + qh_ptr = qh_ptr->next;
38492 + /*
38493 + * Move the QH from the periodic assigned schedule to
38494 + * the periodic queued schedule.
38495 + */
38496 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
38497 + &qh->qh_list_entry);
38498 +
38499 + /* done queuing high bandwidth */
38500 + hcd->core_if->queuing_high_bandwidth = 0;
38501 + }
38502 + }
38503 +
38504 + if (!hcd->core_if->dma_enable) {
38505 + dwc_otg_core_global_regs_t *global_regs;
38506 + gintmsk_data_t intr_mask = {.d32 = 0 };
38507 +
38508 + global_regs = hcd->core_if->core_global_regs;
38509 + intr_mask.b.ptxfempty = 1;
38510 +#ifdef DEBUG
38511 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38512 + DWC_DEBUGPL(DBG_HCDV,
38513 + " P Tx Req Queue Space Avail (after queue): %d\n",
38514 + tx_status.b.ptxqspcavail);
38515 + DWC_DEBUGPL(DBG_HCDV,
38516 + " P Tx FIFO Space Avail (after queue): %d\n",
38517 + tx_status.b.ptxfspcavail);
38518 +#endif
38519 + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
38520 + no_queue_space || no_fifo_space) {
38521 + /*
38522 + * May need to queue more transactions as the request
38523 + * queue or Tx FIFO empties. Enable the periodic Tx
38524 + * FIFO empty interrupt. (Always use the half-empty
38525 + * level to ensure that new requests are loaded as
38526 + * soon as possible.)
38527 + */
38528 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
38529 + intr_mask.d32);
38530 + } else {
38531 + /*
38532 + * Disable the Tx FIFO empty interrupt since there are
38533 + * no more transactions that need to be queued right
38534 + * now. This function is called from interrupt
38535 + * handlers to queue more transactions as transfer
38536 + * states change.
38537 + */
38538 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
38539 + 0);
38540 + }
38541 + }
38542 +}
38543 +
38544 +/**
38545 + * Processes active non-periodic channels and queues transactions for these
38546 + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
38547 + * FIFO Empty interrupt is enabled if there are more transactions to queue as
38548 + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
38549 + * FIFO Empty interrupt is disabled.
38550 + */
38551 +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
38552 +{
38553 + gnptxsts_data_t tx_status;
38554 + dwc_list_link_t *orig_qh_ptr;
38555 + dwc_otg_qh_t *qh;
38556 + int status;
38557 + int no_queue_space = 0;
38558 + int no_fifo_space = 0;
38559 + int more_to_do = 0;
38560 +
38561 + dwc_otg_core_global_regs_t *global_regs =
38562 + hcd->core_if->core_global_regs;
38563 +
38564 + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
38565 +#ifdef DEBUG
38566 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38567 + DWC_DEBUGPL(DBG_HCDV,
38568 + " NP Tx Req Queue Space Avail (before queue): %d\n",
38569 + tx_status.b.nptxqspcavail);
38570 + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
38571 + tx_status.b.nptxfspcavail);
38572 +#endif
38573 + /*
38574 + * Keep track of the starting point. Skip over the start-of-list
38575 + * entry.
38576 + */
38577 + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
38578 + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
38579 + }
38580 + orig_qh_ptr = hcd->non_periodic_qh_ptr;
38581 +
38582 + /*
38583 + * Process once through the active list or until no more space is
38584 + * available in the request queue or the Tx FIFO.
38585 + */
38586 + do {
38587 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38588 + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
38589 + no_queue_space = 1;
38590 + break;
38591 + }
38592 +
38593 + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
38594 + qh_list_entry);
38595 +
38596 + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
38597 + fiq_fsm_queue_split_transaction(hcd, qh);
38598 + } else {
38599 + status = queue_transaction(hcd, qh->channel,
38600 + tx_status.b.nptxfspcavail);
38601 +
38602 + if (status > 0) {
38603 + more_to_do = 1;
38604 + } else if (status < 0) {
38605 + no_fifo_space = 1;
38606 + break;
38607 + }
38608 + }
38609 + /* Advance to next QH, skipping start-of-list entry. */
38610 + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
38611 + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
38612 + hcd->non_periodic_qh_ptr =
38613 + hcd->non_periodic_qh_ptr->next;
38614 + }
38615 +
38616 + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
38617 +
38618 + if (!hcd->core_if->dma_enable) {
38619 + gintmsk_data_t intr_mask = {.d32 = 0 };
38620 + intr_mask.b.nptxfempty = 1;
38621 +
38622 +#ifdef DEBUG
38623 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38624 + DWC_DEBUGPL(DBG_HCDV,
38625 + " NP Tx Req Queue Space Avail (after queue): %d\n",
38626 + tx_status.b.nptxqspcavail);
38627 + DWC_DEBUGPL(DBG_HCDV,
38628 + " NP Tx FIFO Space Avail (after queue): %d\n",
38629 + tx_status.b.nptxfspcavail);
38630 +#endif
38631 + if (more_to_do || no_queue_space || no_fifo_space) {
38632 + /*
38633 + * May need to queue more transactions as the request
38634 + * queue or Tx FIFO empties. Enable the non-periodic
38635 + * Tx FIFO empty interrupt. (Always use the half-empty
38636 + * level to ensure that new requests are loaded as
38637 + * soon as possible.)
38638 + */
38639 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
38640 + intr_mask.d32);
38641 + } else {
38642 + /*
38643 + * Disable the Tx FIFO empty interrupt since there are
38644 + * no more transactions that need to be queued right
38645 + * now. This function is called from interrupt
38646 + * handlers to queue more transactions as transfer
38647 + * states change.
38648 + */
38649 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
38650 + 0);
38651 + }
38652 + }
38653 +}
38654 +
38655 +/**
38656 + * This function processes the currently active host channels and queues
38657 + * transactions for these channels to the DWC_otg controller. It is called
38658 + * from HCD interrupt handler functions.
38659 + *
38660 + * @param hcd The HCD state structure.
38661 + * @param tr_type The type(s) of transactions to queue (non-periodic,
38662 + * periodic, or both).
38663 + */
38664 +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
38665 + dwc_otg_transaction_type_e tr_type)
38666 +{
38667 +#ifdef DEBUG_SOF
38668 + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
38669 +#endif
38670 + /* Process host channels associated with periodic transfers. */
38671 + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
38672 + tr_type == DWC_OTG_TRANSACTION_ALL) &&
38673 + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
38674 +
38675 + process_periodic_channels(hcd);
38676 + }
38677 +
38678 + /* Process host channels associated with non-periodic transfers. */
38679 + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
38680 + tr_type == DWC_OTG_TRANSACTION_ALL) {
38681 + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
38682 + process_non_periodic_channels(hcd);
38683 + } else {
38684 + /*
38685 + * Ensure NP Tx FIFO empty interrupt is disabled when
38686 + * there are no non-periodic transfers to process.
38687 + */
38688 + gintmsk_data_t gintmsk = {.d32 = 0 };
38689 + gintmsk.b.nptxfempty = 1;
38690 +
38691 + if (fiq_enable) {
38692 + local_fiq_disable();
38693 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38694 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
38695 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38696 + local_fiq_enable();
38697 + } else {
38698 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
38699 + }
38700 + }
38701 + }
38702 +}
38703 +
38704 +#ifdef DWC_HS_ELECT_TST
38705 +/*
38706 + * Quick and dirty hack to implement the HS Electrical Test
38707 + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
38708 + *
38709 + * This code was copied from our userspace app "hset". It sends a
38710 + * Get Device Descriptor control sequence in two parts, first the
38711 + * Setup packet by itself, followed some time later by the In and
38712 + * Ack packets. Rather than trying to figure out how to add this
38713 + * functionality to the normal driver code, we just hijack the
38714 + * hardware, using these two function to drive the hardware
38715 + * directly.
38716 + */
38717 +
38718 +static dwc_otg_core_global_regs_t *global_regs;
38719 +static dwc_otg_host_global_regs_t *hc_global_regs;
38720 +static dwc_otg_hc_regs_t *hc_regs;
38721 +static uint32_t *data_fifo;
38722 +
38723 +static void do_setup(void)
38724 +{
38725 + gintsts_data_t gintsts;
38726 + hctsiz_data_t hctsiz;
38727 + hcchar_data_t hcchar;
38728 + haint_data_t haint;
38729 + hcint_data_t hcint;
38730 +
38731 + /* Enable HAINTs */
38732 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
38733 +
38734 + /* Enable HCINTs */
38735 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
38736 +
38737 + /* Read GINTSTS */
38738 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38739 +
38740 + /* Read HAINT */
38741 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38742 +
38743 + /* Read HCINT */
38744 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38745 +
38746 + /* Read HCCHAR */
38747 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38748 +
38749 + /* Clear HCINT */
38750 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38751 +
38752 + /* Clear HAINT */
38753 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38754 +
38755 + /* Clear GINTSTS */
38756 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38757 +
38758 + /* Read GINTSTS */
38759 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38760 +
38761 + /*
38762 + * Send Setup packet (Get Device Descriptor)
38763 + */
38764 +
38765 + /* Make sure channel is disabled */
38766 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38767 + if (hcchar.b.chen) {
38768 + hcchar.b.chdis = 1;
38769 +// hcchar.b.chen = 1;
38770 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38771 + //sleep(1);
38772 + dwc_mdelay(1000);
38773 +
38774 + /* Read GINTSTS */
38775 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38776 +
38777 + /* Read HAINT */
38778 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38779 +
38780 + /* Read HCINT */
38781 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38782 +
38783 + /* Read HCCHAR */
38784 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38785 +
38786 + /* Clear HCINT */
38787 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38788 +
38789 + /* Clear HAINT */
38790 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38791 +
38792 + /* Clear GINTSTS */
38793 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38794 +
38795 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38796 + }
38797 +
38798 + /* Set HCTSIZ */
38799 + hctsiz.d32 = 0;
38800 + hctsiz.b.xfersize = 8;
38801 + hctsiz.b.pktcnt = 1;
38802 + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
38803 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
38804 +
38805 + /* Set HCCHAR */
38806 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38807 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
38808 + hcchar.b.epdir = 0;
38809 + hcchar.b.epnum = 0;
38810 + hcchar.b.mps = 8;
38811 + hcchar.b.chen = 1;
38812 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38813 +
38814 + /* Fill FIFO with Setup data for Get Device Descriptor */
38815 + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
38816 + DWC_WRITE_REG32(data_fifo++, 0x01000680);
38817 + DWC_WRITE_REG32(data_fifo++, 0x00080000);
38818 +
38819 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38820 +
38821 + /* Wait for host channel interrupt */
38822 + do {
38823 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38824 + } while (gintsts.b.hcintr == 0);
38825 +
38826 + /* Disable HCINTs */
38827 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
38828 +
38829 + /* Disable HAINTs */
38830 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
38831 +
38832 + /* Read HAINT */
38833 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38834 +
38835 + /* Read HCINT */
38836 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38837 +
38838 + /* Read HCCHAR */
38839 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38840 +
38841 + /* Clear HCINT */
38842 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38843 +
38844 + /* Clear HAINT */
38845 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38846 +
38847 + /* Clear GINTSTS */
38848 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38849 +
38850 + /* Read GINTSTS */
38851 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38852 +}
38853 +
38854 +static void do_in_ack(void)
38855 +{
38856 + gintsts_data_t gintsts;
38857 + hctsiz_data_t hctsiz;
38858 + hcchar_data_t hcchar;
38859 + haint_data_t haint;
38860 + hcint_data_t hcint;
38861 + host_grxsts_data_t grxsts;
38862 +
38863 + /* Enable HAINTs */
38864 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
38865 +
38866 + /* Enable HCINTs */
38867 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
38868 +
38869 + /* Read GINTSTS */
38870 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38871 +
38872 + /* Read HAINT */
38873 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38874 +
38875 + /* Read HCINT */
38876 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38877 +
38878 + /* Read HCCHAR */
38879 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38880 +
38881 + /* Clear HCINT */
38882 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38883 +
38884 + /* Clear HAINT */
38885 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38886 +
38887 + /* Clear GINTSTS */
38888 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38889 +
38890 + /* Read GINTSTS */
38891 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38892 +
38893 + /*
38894 + * Receive Control In packet
38895 + */
38896 +
38897 + /* Make sure channel is disabled */
38898 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38899 + if (hcchar.b.chen) {
38900 + hcchar.b.chdis = 1;
38901 + hcchar.b.chen = 1;
38902 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38903 + //sleep(1);
38904 + dwc_mdelay(1000);
38905 +
38906 + /* Read GINTSTS */
38907 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38908 +
38909 + /* Read HAINT */
38910 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38911 +
38912 + /* Read HCINT */
38913 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38914 +
38915 + /* Read HCCHAR */
38916 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38917 +
38918 + /* Clear HCINT */
38919 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38920 +
38921 + /* Clear HAINT */
38922 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38923 +
38924 + /* Clear GINTSTS */
38925 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38926 +
38927 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38928 + }
38929 +
38930 + /* Set HCTSIZ */
38931 + hctsiz.d32 = 0;
38932 + hctsiz.b.xfersize = 8;
38933 + hctsiz.b.pktcnt = 1;
38934 + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
38935 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
38936 +
38937 + /* Set HCCHAR */
38938 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38939 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
38940 + hcchar.b.epdir = 1;
38941 + hcchar.b.epnum = 0;
38942 + hcchar.b.mps = 8;
38943 + hcchar.b.chen = 1;
38944 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38945 +
38946 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38947 +
38948 + /* Wait for receive status queue interrupt */
38949 + do {
38950 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38951 + } while (gintsts.b.rxstsqlvl == 0);
38952 +
38953 + /* Read RXSTS */
38954 + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
38955 +
38956 + /* Clear RXSTSQLVL in GINTSTS */
38957 + gintsts.d32 = 0;
38958 + gintsts.b.rxstsqlvl = 1;
38959 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38960 +
38961 + switch (grxsts.b.pktsts) {
38962 + case DWC_GRXSTS_PKTSTS_IN:
38963 + /* Read the data into the host buffer */
38964 + if (grxsts.b.bcnt > 0) {
38965 + int i;
38966 + int word_count = (grxsts.b.bcnt + 3) / 4;
38967 +
38968 + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
38969 +
38970 + for (i = 0; i < word_count; i++) {
38971 + (void)DWC_READ_REG32(data_fifo++);
38972 + }
38973 + }
38974 + break;
38975 +
38976 + default:
38977 + break;
38978 + }
38979 +
38980 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38981 +
38982 + /* Wait for receive status queue interrupt */
38983 + do {
38984 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38985 + } while (gintsts.b.rxstsqlvl == 0);
38986 +
38987 + /* Read RXSTS */
38988 + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
38989 +
38990 + /* Clear RXSTSQLVL in GINTSTS */
38991 + gintsts.d32 = 0;
38992 + gintsts.b.rxstsqlvl = 1;
38993 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38994 +
38995 + switch (grxsts.b.pktsts) {
38996 + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
38997 + break;
38998 +
38999 + default:
39000 + break;
39001 + }
39002 +
39003 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39004 +
39005 + /* Wait for host channel interrupt */
39006 + do {
39007 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39008 + } while (gintsts.b.hcintr == 0);
39009 +
39010 + /* Read HAINT */
39011 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39012 +
39013 + /* Read HCINT */
39014 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39015 +
39016 + /* Read HCCHAR */
39017 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39018 +
39019 + /* Clear HCINT */
39020 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39021 +
39022 + /* Clear HAINT */
39023 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39024 +
39025 + /* Clear GINTSTS */
39026 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39027 +
39028 + /* Read GINTSTS */
39029 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39030 +
39031 +// usleep(100000);
39032 +// mdelay(100);
39033 + dwc_mdelay(1);
39034 +
39035 + /*
39036 + * Send handshake packet
39037 + */
39038 +
39039 + /* Read HAINT */
39040 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39041 +
39042 + /* Read HCINT */
39043 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39044 +
39045 + /* Read HCCHAR */
39046 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39047 +
39048 + /* Clear HCINT */
39049 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39050 +
39051 + /* Clear HAINT */
39052 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39053 +
39054 + /* Clear GINTSTS */
39055 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39056 +
39057 + /* Read GINTSTS */
39058 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39059 +
39060 + /* Make sure channel is disabled */
39061 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39062 + if (hcchar.b.chen) {
39063 + hcchar.b.chdis = 1;
39064 + hcchar.b.chen = 1;
39065 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39066 + //sleep(1);
39067 + dwc_mdelay(1000);
39068 +
39069 + /* Read GINTSTS */
39070 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39071 +
39072 + /* Read HAINT */
39073 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39074 +
39075 + /* Read HCINT */
39076 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39077 +
39078 + /* Read HCCHAR */
39079 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39080 +
39081 + /* Clear HCINT */
39082 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39083 +
39084 + /* Clear HAINT */
39085 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39086 +
39087 + /* Clear GINTSTS */
39088 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39089 +
39090 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39091 + }
39092 +
39093 + /* Set HCTSIZ */
39094 + hctsiz.d32 = 0;
39095 + hctsiz.b.xfersize = 0;
39096 + hctsiz.b.pktcnt = 1;
39097 + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
39098 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
39099 +
39100 + /* Set HCCHAR */
39101 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39102 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39103 + hcchar.b.epdir = 0;
39104 + hcchar.b.epnum = 0;
39105 + hcchar.b.mps = 8;
39106 + hcchar.b.chen = 1;
39107 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39108 +
39109 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39110 +
39111 + /* Wait for host channel interrupt */
39112 + do {
39113 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39114 + } while (gintsts.b.hcintr == 0);
39115 +
39116 + /* Disable HCINTs */
39117 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
39118 +
39119 + /* Disable HAINTs */
39120 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
39121 +
39122 + /* Read HAINT */
39123 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39124 +
39125 + /* Read HCINT */
39126 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39127 +
39128 + /* Read HCCHAR */
39129 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39130 +
39131 + /* Clear HCINT */
39132 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39133 +
39134 + /* Clear HAINT */
39135 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39136 +
39137 + /* Clear GINTSTS */
39138 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39139 +
39140 + /* Read GINTSTS */
39141 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39142 +}
39143 +#endif
39144 +
39145 +/** Handles hub class-specific requests. */
39146 +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
39147 + uint16_t typeReq,
39148 + uint16_t wValue,
39149 + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
39150 +{
39151 + int retval = 0;
39152 +
39153 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
39154 + usb_hub_descriptor_t *hub_desc;
39155 + hprt0_data_t hprt0 = {.d32 = 0 };
39156 +
39157 + uint32_t port_status;
39158 +
39159 + switch (typeReq) {
39160 + case UCR_CLEAR_HUB_FEATURE:
39161 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39162 + "ClearHubFeature 0x%x\n", wValue);
39163 + switch (wValue) {
39164 + case UHF_C_HUB_LOCAL_POWER:
39165 + case UHF_C_HUB_OVER_CURRENT:
39166 + /* Nothing required here */
39167 + break;
39168 + default:
39169 + retval = -DWC_E_INVALID;
39170 + DWC_ERROR("DWC OTG HCD - "
39171 + "ClearHubFeature request %xh unknown\n",
39172 + wValue);
39173 + }
39174 + break;
39175 + case UCR_CLEAR_PORT_FEATURE:
39176 +#ifdef CONFIG_USB_DWC_OTG_LPM
39177 + if (wValue != UHF_PORT_L1)
39178 +#endif
39179 + if (!wIndex || wIndex > 1)
39180 + goto error;
39181 +
39182 + switch (wValue) {
39183 + case UHF_PORT_ENABLE:
39184 + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
39185 + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
39186 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39187 + hprt0.b.prtena = 1;
39188 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39189 + break;
39190 + case UHF_PORT_SUSPEND:
39191 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39192 + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
39193 +
39194 + if (core_if->power_down == 2) {
39195 + dwc_otg_host_hibernation_restore(core_if, 0, 0);
39196 + } else {
39197 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
39198 + dwc_mdelay(5);
39199 +
39200 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39201 + hprt0.b.prtres = 1;
39202 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39203 + hprt0.b.prtsusp = 0;
39204 + /* Clear Resume bit */
39205 + dwc_mdelay(100);
39206 + hprt0.b.prtres = 0;
39207 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39208 + }
39209 + break;
39210 +#ifdef CONFIG_USB_DWC_OTG_LPM
39211 + case UHF_PORT_L1:
39212 + {
39213 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39214 + glpmcfg_data_t lpmcfg = {.d32 = 0 };
39215 +
39216 + lpmcfg.d32 =
39217 + DWC_READ_REG32(&core_if->
39218 + core_global_regs->glpmcfg);
39219 + lpmcfg.b.en_utmi_sleep = 0;
39220 + lpmcfg.b.hird_thres &= (~(1 << 4));
39221 + lpmcfg.b.prt_sleep_sts = 1;
39222 + DWC_WRITE_REG32(&core_if->
39223 + core_global_regs->glpmcfg,
39224 + lpmcfg.d32);
39225 +
39226 + /* Clear Enbl_L1Gating bit. */
39227 + pcgcctl.b.enbl_sleep_gating = 1;
39228 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
39229 + 0);
39230 +
39231 + dwc_mdelay(5);
39232 +
39233 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39234 + hprt0.b.prtres = 1;
39235 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39236 + hprt0.d32);
39237 + /* This bit will be cleared in wakeup interrupt handle */
39238 + break;
39239 + }
39240 +#endif
39241 + case UHF_PORT_POWER:
39242 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39243 + "ClearPortFeature USB_PORT_FEAT_POWER\n");
39244 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39245 + hprt0.b.prtpwr = 0;
39246 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39247 + break;
39248 + case UHF_PORT_INDICATOR:
39249 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39250 + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
39251 + /* Port inidicator not supported */
39252 + break;
39253 + case UHF_C_PORT_CONNECTION:
39254 + /* Clears drivers internal connect status change
39255 + * flag */
39256 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39257 + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
39258 + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
39259 + break;
39260 + case UHF_C_PORT_RESET:
39261 + /* Clears the driver's internal Port Reset Change
39262 + * flag */
39263 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39264 + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
39265 + dwc_otg_hcd->flags.b.port_reset_change = 0;
39266 + break;
39267 + case UHF_C_PORT_ENABLE:
39268 + /* Clears the driver's internal Port
39269 + * Enable/Disable Change flag */
39270 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39271 + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
39272 + dwc_otg_hcd->flags.b.port_enable_change = 0;
39273 + break;
39274 + case UHF_C_PORT_SUSPEND:
39275 + /* Clears the driver's internal Port Suspend
39276 + * Change flag, which is set when resume signaling on
39277 + * the host port is complete */
39278 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39279 + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
39280 + dwc_otg_hcd->flags.b.port_suspend_change = 0;
39281 + break;
39282 +#ifdef CONFIG_USB_DWC_OTG_LPM
39283 + case UHF_C_PORT_L1:
39284 + dwc_otg_hcd->flags.b.port_l1_change = 0;
39285 + break;
39286 +#endif
39287 + case UHF_C_PORT_OVER_CURRENT:
39288 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39289 + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
39290 + dwc_otg_hcd->flags.b.port_over_current_change = 0;
39291 + break;
39292 + default:
39293 + retval = -DWC_E_INVALID;
39294 + DWC_ERROR("DWC OTG HCD - "
39295 + "ClearPortFeature request %xh "
39296 + "unknown or unsupported\n", wValue);
39297 + }
39298 + break;
39299 + case UCR_GET_HUB_DESCRIPTOR:
39300 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39301 + "GetHubDescriptor\n");
39302 + hub_desc = (usb_hub_descriptor_t *) buf;
39303 + hub_desc->bDescLength = 9;
39304 + hub_desc->bDescriptorType = 0x29;
39305 + hub_desc->bNbrPorts = 1;
39306 + USETW(hub_desc->wHubCharacteristics, 0x08);
39307 + hub_desc->bPwrOn2PwrGood = 1;
39308 + hub_desc->bHubContrCurrent = 0;
39309 + hub_desc->DeviceRemovable[0] = 0;
39310 + hub_desc->DeviceRemovable[1] = 0xff;
39311 + break;
39312 + case UCR_GET_HUB_STATUS:
39313 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39314 + "GetHubStatus\n");
39315 + DWC_MEMSET(buf, 0, 4);
39316 + break;
39317 + case UCR_GET_PORT_STATUS:
39318 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39319 + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
39320 + wIndex, dwc_otg_hcd->flags.d32);
39321 + if (!wIndex || wIndex > 1)
39322 + goto error;
39323 +
39324 + port_status = 0;
39325 +
39326 + if (dwc_otg_hcd->flags.b.port_connect_status_change)
39327 + port_status |= (1 << UHF_C_PORT_CONNECTION);
39328 +
39329 + if (dwc_otg_hcd->flags.b.port_enable_change)
39330 + port_status |= (1 << UHF_C_PORT_ENABLE);
39331 +
39332 + if (dwc_otg_hcd->flags.b.port_suspend_change)
39333 + port_status |= (1 << UHF_C_PORT_SUSPEND);
39334 +
39335 + if (dwc_otg_hcd->flags.b.port_l1_change)
39336 + port_status |= (1 << UHF_C_PORT_L1);
39337 +
39338 + if (dwc_otg_hcd->flags.b.port_reset_change) {
39339 + port_status |= (1 << UHF_C_PORT_RESET);
39340 + }
39341 +
39342 + if (dwc_otg_hcd->flags.b.port_over_current_change) {
39343 + DWC_WARN("Overcurrent change detected\n");
39344 + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
39345 + }
39346 +
39347 + if (!dwc_otg_hcd->flags.b.port_connect_status) {
39348 + /*
39349 + * The port is disconnected, which means the core is
39350 + * either in device mode or it soon will be. Just
39351 + * return 0's for the remainder of the port status
39352 + * since the port register can't be read if the core
39353 + * is in device mode.
39354 + */
39355 + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
39356 + break;
39357 + }
39358 +
39359 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
39360 + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
39361 +
39362 + if (hprt0.b.prtconnsts)
39363 + port_status |= (1 << UHF_PORT_CONNECTION);
39364 +
39365 + if (hprt0.b.prtena)
39366 + port_status |= (1 << UHF_PORT_ENABLE);
39367 +
39368 + if (hprt0.b.prtsusp)
39369 + port_status |= (1 << UHF_PORT_SUSPEND);
39370 +
39371 + if (hprt0.b.prtovrcurract)
39372 + port_status |= (1 << UHF_PORT_OVER_CURRENT);
39373 +
39374 + if (hprt0.b.prtrst)
39375 + port_status |= (1 << UHF_PORT_RESET);
39376 +
39377 + if (hprt0.b.prtpwr)
39378 + port_status |= (1 << UHF_PORT_POWER);
39379 +
39380 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
39381 + port_status |= (1 << UHF_PORT_HIGH_SPEED);
39382 + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
39383 + port_status |= (1 << UHF_PORT_LOW_SPEED);
39384 +
39385 + if (hprt0.b.prttstctl)
39386 + port_status |= (1 << UHF_PORT_TEST);
39387 + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
39388 + port_status |= (1 << UHF_PORT_L1);
39389 + }
39390 + /*
39391 + For Synopsys HW emulation of Power down wkup_control asserts the
39392 + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
39393 + We intentionally tell the software that port is in L2Suspend state.
39394 + Only for STE.
39395 + */
39396 + if ((core_if->power_down == 2)
39397 + && (core_if->hibernation_suspend == 1)) {
39398 + port_status |= (1 << UHF_PORT_SUSPEND);
39399 + }
39400 + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
39401 +
39402 + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
39403 +
39404 + break;
39405 + case UCR_SET_HUB_FEATURE:
39406 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39407 + "SetHubFeature\n");
39408 + /* No HUB features supported */
39409 + break;
39410 + case UCR_SET_PORT_FEATURE:
39411 + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
39412 + goto error;
39413 +
39414 + if (!dwc_otg_hcd->flags.b.port_connect_status) {
39415 + /*
39416 + * The port is disconnected, which means the core is
39417 + * either in device mode or it soon will be. Just
39418 + * return without doing anything since the port
39419 + * register can't be written if the core is in device
39420 + * mode.
39421 + */
39422 + break;
39423 + }
39424 +
39425 + switch (wValue) {
39426 + case UHF_PORT_SUSPEND:
39427 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39428 + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
39429 + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
39430 + goto error;
39431 + }
39432 + if (core_if->power_down == 2) {
39433 + int timeout = 300;
39434 + dwc_irqflags_t flags;
39435 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39436 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
39437 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
39438 +#ifdef DWC_DEV_SRPCAP
39439 + int32_t otg_cap_param = core_if->core_params->otg_cap;
39440 +#endif
39441 + DWC_PRINTF("Preparing for complete power-off\n");
39442 +
39443 + /* Save registers before hibernation */
39444 + dwc_otg_save_global_regs(core_if);
39445 + dwc_otg_save_host_regs(core_if);
39446 +
39447 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39448 + hprt0.b.prtsusp = 1;
39449 + hprt0.b.prtena = 0;
39450 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39451 + /* Spin hprt0.b.prtsusp to became 1 */
39452 + do {
39453 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39454 + if (hprt0.b.prtsusp) {
39455 + break;
39456 + }
39457 + dwc_mdelay(1);
39458 + } while (--timeout);
39459 + if (!timeout) {
39460 + DWC_WARN("Suspend wasn't genereted\n");
39461 + }
39462 + dwc_udelay(10);
39463 +
39464 + /*
39465 + * We need to disable interrupts to prevent servicing of any IRQ
39466 + * during going to hibernation
39467 + */
39468 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
39469 + core_if->lx_state = DWC_OTG_L2;
39470 +#ifdef DWC_DEV_SRPCAP
39471 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39472 + hprt0.b.prtpwr = 0;
39473 + hprt0.b.prtena = 0;
39474 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39475 + hprt0.d32);
39476 +#endif
39477 + gusbcfg.d32 =
39478 + DWC_READ_REG32(&core_if->core_global_regs->
39479 + gusbcfg);
39480 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
39481 + /* ULPI interface */
39482 + /* Suspend the Phy Clock */
39483 + pcgcctl.d32 = 0;
39484 + pcgcctl.b.stoppclk = 1;
39485 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
39486 + pcgcctl.d32);
39487 + dwc_udelay(10);
39488 + gpwrdn.b.pmuactv = 1;
39489 + DWC_MODIFY_REG32(&core_if->
39490 + core_global_regs->
39491 + gpwrdn, 0, gpwrdn.d32);
39492 + } else {
39493 + /* UTMI+ Interface */
39494 + gpwrdn.b.pmuactv = 1;
39495 + DWC_MODIFY_REG32(&core_if->
39496 + core_global_regs->
39497 + gpwrdn, 0, gpwrdn.d32);
39498 + dwc_udelay(10);
39499 + pcgcctl.b.stoppclk = 1;
39500 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
39501 + dwc_udelay(10);
39502 + }
39503 +#ifdef DWC_DEV_SRPCAP
39504 + gpwrdn.d32 = 0;
39505 + gpwrdn.b.dis_vbus = 1;
39506 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39507 + gpwrdn, 0, gpwrdn.d32);
39508 +#endif
39509 + gpwrdn.d32 = 0;
39510 + gpwrdn.b.pmuintsel = 1;
39511 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39512 + gpwrdn, 0, gpwrdn.d32);
39513 + dwc_udelay(10);
39514 +
39515 + gpwrdn.d32 = 0;
39516 +#ifdef DWC_DEV_SRPCAP
39517 + gpwrdn.b.srp_det_msk = 1;
39518 +#endif
39519 + gpwrdn.b.disconn_det_msk = 1;
39520 + gpwrdn.b.lnstchng_msk = 1;
39521 + gpwrdn.b.sts_chngint_msk = 1;
39522 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39523 + gpwrdn, 0, gpwrdn.d32);
39524 + dwc_udelay(10);
39525 +
39526 + /* Enable Power Down Clamp and all interrupts in GPWRDN */
39527 + gpwrdn.d32 = 0;
39528 + gpwrdn.b.pwrdnclmp = 1;
39529 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39530 + gpwrdn, 0, gpwrdn.d32);
39531 + dwc_udelay(10);
39532 +
39533 + /* Switch off VDD */
39534 + gpwrdn.d32 = 0;
39535 + gpwrdn.b.pwrdnswtch = 1;
39536 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39537 + gpwrdn, 0, gpwrdn.d32);
39538 +
39539 +#ifdef DWC_DEV_SRPCAP
39540 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
39541 + {
39542 + core_if->pwron_timer_started = 1;
39543 + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
39544 + }
39545 +#endif
39546 + /* Save gpwrdn register for further usage if stschng interrupt */
39547 + core_if->gr_backup->gpwrdn_local =
39548 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
39549 +
39550 + /* Set flag to indicate that we are in hibernation */
39551 + core_if->hibernation_suspend = 1;
39552 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
39553 +
39554 + DWC_PRINTF("Host hibernation completed\n");
39555 + // Exit from case statement
39556 + break;
39557 +
39558 + }
39559 + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
39560 + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
39561 + gotgctl_data_t gotgctl = {.d32 = 0 };
39562 + gotgctl.b.hstsethnpen = 1;
39563 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39564 + gotgctl, 0, gotgctl.d32);
39565 + core_if->op_state = A_SUSPEND;
39566 + }
39567 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39568 + hprt0.b.prtsusp = 1;
39569 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39570 + {
39571 + dwc_irqflags_t flags;
39572 + /* Update lx_state */
39573 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
39574 + core_if->lx_state = DWC_OTG_L2;
39575 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
39576 + }
39577 + /* Suspend the Phy Clock */
39578 + {
39579 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39580 + pcgcctl.b.stoppclk = 1;
39581 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
39582 + pcgcctl.d32);
39583 + dwc_udelay(10);
39584 + }
39585 +
39586 + /* For HNP the bus must be suspended for at least 200ms. */
39587 + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
39588 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39589 + pcgcctl.b.stoppclk = 1;
39590 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
39591 + dwc_mdelay(200);
39592 + }
39593 +
39594 + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
39595 +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
39596 + if (core_if->adp_enable) {
39597 + gotgctl_data_t gotgctl = {.d32 = 0 };
39598 + gpwrdn_data_t gpwrdn;
39599 +
39600 + while (gotgctl.b.asesvld == 1) {
39601 + gotgctl.d32 =
39602 + DWC_READ_REG32(&core_if->
39603 + core_global_regs->
39604 + gotgctl);
39605 + dwc_mdelay(100);
39606 + }
39607 +
39608 + /* Enable Power Down Logic */
39609 + gpwrdn.d32 = 0;
39610 + gpwrdn.b.pmuactv = 1;
39611 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39612 + gpwrdn, 0, gpwrdn.d32);
39613 +
39614 + /* Unmask SRP detected interrupt from Power Down Logic */
39615 + gpwrdn.d32 = 0;
39616 + gpwrdn.b.srp_det_msk = 1;
39617 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39618 + gpwrdn, 0, gpwrdn.d32);
39619 +
39620 + dwc_otg_adp_probe_start(core_if);
39621 + }
39622 +#endif
39623 + break;
39624 + case UHF_PORT_POWER:
39625 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39626 + "SetPortFeature - USB_PORT_FEAT_POWER\n");
39627 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39628 + hprt0.b.prtpwr = 1;
39629 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39630 + break;
39631 + case UHF_PORT_RESET:
39632 + if ((core_if->power_down == 2)
39633 + && (core_if->hibernation_suspend == 1)) {
39634 + /* If we are going to exit from Hibernated
39635 + * state via USB RESET.
39636 + */
39637 + dwc_otg_host_hibernation_restore(core_if, 0, 1);
39638 + } else {
39639 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39640 +
39641 + DWC_DEBUGPL(DBG_HCD,
39642 + "DWC OTG HCD HUB CONTROL - "
39643 + "SetPortFeature - USB_PORT_FEAT_RESET\n");
39644 + {
39645 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39646 + pcgcctl.b.enbl_sleep_gating = 1;
39647 + pcgcctl.b.stoppclk = 1;
39648 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
39649 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
39650 + }
39651 +#ifdef CONFIG_USB_DWC_OTG_LPM
39652 + {
39653 + glpmcfg_data_t lpmcfg;
39654 + lpmcfg.d32 =
39655 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
39656 + if (lpmcfg.b.prt_sleep_sts) {
39657 + lpmcfg.b.en_utmi_sleep = 0;
39658 + lpmcfg.b.hird_thres &= (~(1 << 4));
39659 + DWC_WRITE_REG32
39660 + (&core_if->core_global_regs->glpmcfg,
39661 + lpmcfg.d32);
39662 + dwc_mdelay(1);
39663 + }
39664 + }
39665 +#endif
39666 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39667 + /* Clear suspend bit if resetting from suspended state. */
39668 + hprt0.b.prtsusp = 0;
39669 + /* When B-Host the Port reset bit is set in
39670 + * the Start HCD Callback function, so that
39671 + * the reset is started within 1ms of the HNP
39672 + * success interrupt. */
39673 + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
39674 + hprt0.b.prtpwr = 1;
39675 + hprt0.b.prtrst = 1;
39676 + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
39677 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39678 + hprt0.d32);
39679 + }
39680 + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
39681 + dwc_mdelay(60);
39682 + hprt0.b.prtrst = 0;
39683 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39684 + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
39685 + }
39686 + break;
39687 +#ifdef DWC_HS_ELECT_TST
39688 + case UHF_PORT_TEST:
39689 + {
39690 + uint32_t t;
39691 + gintmsk_data_t gintmsk;
39692 +
39693 + t = (wIndex >> 8); /* MSB wIndex USB */
39694 + DWC_DEBUGPL(DBG_HCD,
39695 + "DWC OTG HCD HUB CONTROL - "
39696 + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
39697 + t);
39698 + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
39699 + if (t < 6) {
39700 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39701 + hprt0.b.prttstctl = t;
39702 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39703 + hprt0.d32);
39704 + } else {
39705 + /* Setup global vars with reg addresses (quick and
39706 + * dirty hack, should be cleaned up)
39707 + */
39708 + global_regs = core_if->core_global_regs;
39709 + hc_global_regs =
39710 + core_if->host_if->host_global_regs;
39711 + hc_regs =
39712 + (dwc_otg_hc_regs_t *) ((char *)
39713 + global_regs +
39714 + 0x500);
39715 + data_fifo =
39716 + (uint32_t *) ((char *)global_regs +
39717 + 0x1000);
39718 +
39719 + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
39720 + /* Save current interrupt mask */
39721 + gintmsk.d32 =
39722 + DWC_READ_REG32
39723 + (&global_regs->gintmsk);
39724 +
39725 + /* Disable all interrupts while we muck with
39726 + * the hardware directly
39727 + */
39728 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39729 +
39730 + /* 15 second delay per the test spec */
39731 + dwc_mdelay(15000);
39732 +
39733 + /* Drive suspend on the root port */
39734 + hprt0.d32 =
39735 + dwc_otg_read_hprt0(core_if);
39736 + hprt0.b.prtsusp = 1;
39737 + hprt0.b.prtres = 0;
39738 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39739 +
39740 + /* 15 second delay per the test spec */
39741 + dwc_mdelay(15000);
39742 +
39743 + /* Drive resume on the root port */
39744 + hprt0.d32 =
39745 + dwc_otg_read_hprt0(core_if);
39746 + hprt0.b.prtsusp = 0;
39747 + hprt0.b.prtres = 1;
39748 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39749 + dwc_mdelay(100);
39750 +
39751 + /* Clear the resume bit */
39752 + hprt0.b.prtres = 0;
39753 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39754 +
39755 + /* Restore interrupts */
39756 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
39757 + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
39758 + /* Save current interrupt mask */
39759 + gintmsk.d32 =
39760 + DWC_READ_REG32
39761 + (&global_regs->gintmsk);
39762 +
39763 + /* Disable all interrupts while we muck with
39764 + * the hardware directly
39765 + */
39766 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39767 +
39768 + /* 15 second delay per the test spec */
39769 + dwc_mdelay(15000);
39770 +
39771 + /* Send the Setup packet */
39772 + do_setup();
39773 +
39774 + /* 15 second delay so nothing else happens for awhile */
39775 + dwc_mdelay(15000);
39776 +
39777 + /* Restore interrupts */
39778 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
39779 + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
39780 + /* Save current interrupt mask */
39781 + gintmsk.d32 =
39782 + DWC_READ_REG32
39783 + (&global_regs->gintmsk);
39784 +
39785 + /* Disable all interrupts while we muck with
39786 + * the hardware directly
39787 + */
39788 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39789 +
39790 + /* Send the Setup packet */
39791 + do_setup();
39792 +
39793 + /* 15 second delay so nothing else happens for awhile */
39794 + dwc_mdelay(15000);
39795 +
39796 + /* Send the In and Ack packets */
39797 + do_in_ack();
39798 +
39799 + /* 15 second delay so nothing else happens for awhile */
39800 + dwc_mdelay(15000);
39801 +
39802 + /* Restore interrupts */
39803 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
39804 + }
39805 + }
39806 + break;
39807 + }
39808 +#endif /* DWC_HS_ELECT_TST */
39809 +
39810 + case UHF_PORT_INDICATOR:
39811 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39812 + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
39813 + /* Not supported */
39814 + break;
39815 + default:
39816 + retval = -DWC_E_INVALID;
39817 + DWC_ERROR("DWC OTG HCD - "
39818 + "SetPortFeature request %xh "
39819 + "unknown or unsupported\n", wValue);
39820 + break;
39821 + }
39822 + break;
39823 +#ifdef CONFIG_USB_DWC_OTG_LPM
39824 + case UCR_SET_AND_TEST_PORT_FEATURE:
39825 + if (wValue != UHF_PORT_L1) {
39826 + goto error;
39827 + }
39828 + {
39829 + int portnum, hird, devaddr, remwake;
39830 + glpmcfg_data_t lpmcfg;
39831 + uint32_t time_usecs;
39832 + gintsts_data_t gintsts;
39833 + gintmsk_data_t gintmsk;
39834 +
39835 + if (!dwc_otg_get_param_lpm_enable(core_if)) {
39836 + goto error;
39837 + }
39838 + if (wValue != UHF_PORT_L1 || wLength != 1) {
39839 + goto error;
39840 + }
39841 + /* Check if the port currently is in SLEEP state */
39842 + lpmcfg.d32 =
39843 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
39844 + if (lpmcfg.b.prt_sleep_sts) {
39845 + DWC_INFO("Port is already in sleep mode\n");
39846 + buf[0] = 0; /* Return success */
39847 + break;
39848 + }
39849 +
39850 + portnum = wIndex & 0xf;
39851 + hird = (wIndex >> 4) & 0xf;
39852 + devaddr = (wIndex >> 8) & 0x7f;
39853 + remwake = (wIndex >> 15);
39854 +
39855 + if (portnum != 1) {
39856 + retval = -DWC_E_INVALID;
39857 + DWC_WARN
39858 + ("Wrong port number(%d) in SetandTestPortFeature request\n",
39859 + portnum);
39860 + break;
39861 + }
39862 +
39863 + DWC_PRINTF
39864 + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
39865 + portnum, hird, devaddr, remwake);
39866 + /* Disable LPM interrupt */
39867 + gintmsk.d32 = 0;
39868 + gintmsk.b.lpmtranrcvd = 1;
39869 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
39870 + gintmsk.d32, 0);
39871 +
39872 + if (dwc_otg_hcd_send_lpm
39873 + (dwc_otg_hcd, devaddr, hird, remwake)) {
39874 + retval = -DWC_E_INVALID;
39875 + break;
39876 + }
39877 +
39878 + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
39879 + /* We will consider timeout if time_usecs microseconds pass,
39880 + * and we don't receive LPM transaction status.
39881 + * After receiving non-error responce(ACK/NYET/STALL) from device,
39882 + * core will set lpmtranrcvd bit.
39883 + */
39884 + do {
39885 + gintsts.d32 =
39886 + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
39887 + if (gintsts.b.lpmtranrcvd) {
39888 + break;
39889 + }
39890 + dwc_udelay(1);
39891 + } while (--time_usecs);
39892 + /* lpm_int bit will be cleared in LPM interrupt handler */
39893 +
39894 + /* Now fill status
39895 + * 0x00 - Success
39896 + * 0x10 - NYET
39897 + * 0x11 - Timeout
39898 + */
39899 + if (!gintsts.b.lpmtranrcvd) {
39900 + buf[0] = 0x3; /* Completion code is Timeout */
39901 + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
39902 + } else {
39903 + lpmcfg.d32 =
39904 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
39905 + if (lpmcfg.b.lpm_resp == 0x3) {
39906 + /* ACK responce from the device */
39907 + buf[0] = 0x00; /* Success */
39908 + } else if (lpmcfg.b.lpm_resp == 0x2) {
39909 + /* NYET responce from the device */
39910 + buf[0] = 0x2;
39911 + } else {
39912 + /* Otherwise responce with Timeout */
39913 + buf[0] = 0x3;
39914 + }
39915 + }
39916 + DWC_PRINTF("Device responce to LPM trans is %x\n",
39917 + lpmcfg.b.lpm_resp);
39918 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
39919 + gintmsk.d32);
39920 +
39921 + break;
39922 + }
39923 +#endif /* CONFIG_USB_DWC_OTG_LPM */
39924 + default:
39925 +error:
39926 + retval = -DWC_E_INVALID;
39927 + DWC_WARN("DWC OTG HCD - "
39928 + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
39929 + typeReq, wIndex, wValue);
39930 + break;
39931 + }
39932 +
39933 + return retval;
39934 +}
39935 +
39936 +#ifdef CONFIG_USB_DWC_OTG_LPM
39937 +/** Returns index of host channel to perform LPM transaction. */
39938 +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
39939 +{
39940 + dwc_otg_core_if_t *core_if = hcd->core_if;
39941 + dwc_hc_t *hc;
39942 + hcchar_data_t hcchar;
39943 + gintmsk_data_t gintmsk = {.d32 = 0 };
39944 +
39945 + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
39946 + DWC_PRINTF("No free channel to select for LPM transaction\n");
39947 + return -1;
39948 + }
39949 +
39950 + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
39951 +
39952 + /* Mask host channel interrupts. */
39953 + gintmsk.b.hcintr = 1;
39954 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
39955 +
39956 + /* Fill fields that core needs for LPM transaction */
39957 + hcchar.b.devaddr = devaddr;
39958 + hcchar.b.epnum = 0;
39959 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39960 + hcchar.b.mps = 64;
39961 + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
39962 + hcchar.b.epdir = 0; /* OUT */
39963 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
39964 + hcchar.d32);
39965 +
39966 + /* Remove the host channel from the free list. */
39967 + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
39968 +
39969 + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
39970 +
39971 + return hc->hc_num;
39972 +}
39973 +
39974 +/** Release hc after performing LPM transaction */
39975 +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
39976 +{
39977 + dwc_hc_t *hc;
39978 + glpmcfg_data_t lpmcfg;
39979 + uint8_t hc_num;
39980 +
39981 + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
39982 + hc_num = lpmcfg.b.lpm_chan_index;
39983 +
39984 + hc = hcd->hc_ptr_array[hc_num];
39985 +
39986 + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
39987 + /* Return host channel to free list */
39988 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
39989 +}
39990 +
39991 +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
39992 + uint8_t bRemoteWake)
39993 +{
39994 + glpmcfg_data_t lpmcfg;
39995 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39996 + int channel;
39997 +
39998 + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
39999 + if (channel < 0) {
40000 + return channel;
40001 + }
40002 +
40003 + pcgcctl.b.enbl_sleep_gating = 1;
40004 + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
40005 +
40006 + /* Read LPM config register */
40007 + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
40008 +
40009 + /* Program LPM transaction fields */
40010 + lpmcfg.b.rem_wkup_en = bRemoteWake;
40011 + lpmcfg.b.hird = hird;
40012 + lpmcfg.b.hird_thres = 0x1c;
40013 + lpmcfg.b.lpm_chan_index = channel;
40014 + lpmcfg.b.en_utmi_sleep = 1;
40015 + /* Program LPM config register */
40016 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
40017 +
40018 + /* Send LPM transaction */
40019 + lpmcfg.b.send_lpm = 1;
40020 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
40021 +
40022 + return 0;
40023 +}
40024 +
40025 +#endif /* CONFIG_USB_DWC_OTG_LPM */
40026 +
40027 +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
40028 +{
40029 + int retval;
40030 +
40031 + if (port != 1) {
40032 + return -DWC_E_INVALID;
40033 + }
40034 +
40035 + retval = (hcd->flags.b.port_connect_status_change ||
40036 + hcd->flags.b.port_reset_change ||
40037 + hcd->flags.b.port_enable_change ||
40038 + hcd->flags.b.port_suspend_change ||
40039 + hcd->flags.b.port_over_current_change);
40040 +#ifdef DEBUG
40041 + if (retval) {
40042 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
40043 + " Root port status changed\n");
40044 + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
40045 + hcd->flags.b.port_connect_status_change);
40046 + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
40047 + hcd->flags.b.port_reset_change);
40048 + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
40049 + hcd->flags.b.port_enable_change);
40050 + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
40051 + hcd->flags.b.port_suspend_change);
40052 + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
40053 + hcd->flags.b.port_over_current_change);
40054 + }
40055 +#endif
40056 + return retval;
40057 +}
40058 +
40059 +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
40060 +{
40061 + hfnum_data_t hfnum;
40062 + hfnum.d32 =
40063 + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
40064 + hfnum);
40065 +
40066 +#ifdef DEBUG_SOF
40067 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
40068 + hfnum.b.frnum);
40069 +#endif
40070 + return hfnum.b.frnum;
40071 +}
40072 +
40073 +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
40074 + struct dwc_otg_hcd_function_ops *fops)
40075 +{
40076 + int retval = 0;
40077 +
40078 + hcd->fops = fops;
40079 + if (!dwc_otg_is_device_mode(hcd->core_if) &&
40080 + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
40081 + dwc_otg_hcd_reinit(hcd);
40082 + } else {
40083 + retval = -DWC_E_NO_DEVICE;
40084 + }
40085 +
40086 + return retval;
40087 +}
40088 +
40089 +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
40090 +{
40091 + return hcd->priv;
40092 +}
40093 +
40094 +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
40095 +{
40096 + hcd->priv = priv_data;
40097 +}
40098 +
40099 +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
40100 +{
40101 + return hcd->otg_port;
40102 +}
40103 +
40104 +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
40105 +{
40106 + uint32_t is_b_host;
40107 + if (hcd->core_if->op_state == B_HOST) {
40108 + is_b_host = 1;
40109 + } else {
40110 + is_b_host = 0;
40111 + }
40112 +
40113 + return is_b_host;
40114 +}
40115 +
40116 +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
40117 + int iso_desc_count, int atomic_alloc)
40118 +{
40119 + dwc_otg_hcd_urb_t *dwc_otg_urb;
40120 + uint32_t size;
40121 +
40122 + size =
40123 + sizeof(*dwc_otg_urb) +
40124 + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
40125 + if (atomic_alloc)
40126 + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
40127 + else
40128 + dwc_otg_urb = DWC_ALLOC(size);
40129 +
40130 + if (dwc_otg_urb)
40131 + dwc_otg_urb->packet_count = iso_desc_count;
40132 + else {
40133 + DWC_ERROR("**** DWC OTG HCD URB alloc - "
40134 + "%salloc of %db failed\n",
40135 + atomic_alloc?"atomic ":"", size);
40136 + }
40137 + return dwc_otg_urb;
40138 +}
40139 +
40140 +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
40141 + uint8_t dev_addr, uint8_t ep_num,
40142 + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
40143 +{
40144 + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
40145 + ep_type, ep_dir, mps);
40146 +#if 0
40147 + DWC_PRINTF
40148 + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
40149 + dev_addr, ep_num, ep_dir, ep_type, mps);
40150 +#endif
40151 +}
40152 +
40153 +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
40154 + void *urb_handle, void *buf, dwc_dma_t dma,
40155 + uint32_t buflen, void *setup_packet,
40156 + dwc_dma_t setup_dma, uint32_t flags,
40157 + uint16_t interval)
40158 +{
40159 + dwc_otg_urb->priv = urb_handle;
40160 + dwc_otg_urb->buf = buf;
40161 + dwc_otg_urb->dma = dma;
40162 + dwc_otg_urb->length = buflen;
40163 + dwc_otg_urb->setup_packet = setup_packet;
40164 + dwc_otg_urb->setup_dma = setup_dma;
40165 + dwc_otg_urb->flags = flags;
40166 + dwc_otg_urb->interval = interval;
40167 + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
40168 +}
40169 +
40170 +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
40171 +{
40172 + return dwc_otg_urb->status;
40173 +}
40174 +
40175 +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
40176 +{
40177 + return dwc_otg_urb->actual_length;
40178 +}
40179 +
40180 +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
40181 +{
40182 + return dwc_otg_urb->error_count;
40183 +}
40184 +
40185 +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
40186 + int desc_num, uint32_t offset,
40187 + uint32_t length)
40188 +{
40189 + dwc_otg_urb->iso_descs[desc_num].offset = offset;
40190 + dwc_otg_urb->iso_descs[desc_num].length = length;
40191 +}
40192 +
40193 +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
40194 + int desc_num)
40195 +{
40196 + return dwc_otg_urb->iso_descs[desc_num].status;
40197 +}
40198 +
40199 +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
40200 + dwc_otg_urb, int desc_num)
40201 +{
40202 + return dwc_otg_urb->iso_descs[desc_num].actual_length;
40203 +}
40204 +
40205 +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
40206 +{
40207 + int allocated = 0;
40208 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40209 +
40210 + if (qh) {
40211 + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
40212 + allocated = 1;
40213 + }
40214 + }
40215 + return allocated;
40216 +}
40217 +
40218 +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
40219 +{
40220 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40221 + int freed = 0;
40222 + DWC_ASSERT(qh, "qh is not allocated\n");
40223 +
40224 + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
40225 + freed = 1;
40226 + }
40227 +
40228 + return freed;
40229 +}
40230 +
40231 +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
40232 +{
40233 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40234 + DWC_ASSERT(qh, "qh is not allocated\n");
40235 + return qh->usecs;
40236 +}
40237 +
40238 +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
40239 +{
40240 +#ifdef DEBUG
40241 + int num_channels;
40242 + int i;
40243 + gnptxsts_data_t np_tx_status;
40244 + hptxsts_data_t p_tx_status;
40245 +
40246 + num_channels = hcd->core_if->core_params->host_channels;
40247 + DWC_PRINTF("\n");
40248 + DWC_PRINTF
40249 + ("************************************************************\n");
40250 + DWC_PRINTF("HCD State:\n");
40251 + DWC_PRINTF(" Num channels: %d\n", num_channels);
40252 + for (i = 0; i < num_channels; i++) {
40253 + dwc_hc_t *hc = hcd->hc_ptr_array[i];
40254 + DWC_PRINTF(" Channel %d:\n", i);
40255 + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
40256 + hc->dev_addr, hc->ep_num, hc->ep_is_in);
40257 + DWC_PRINTF(" speed: %d\n", hc->speed);
40258 + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
40259 + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
40260 + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
40261 + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
40262 + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
40263 + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
40264 + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
40265 + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
40266 + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
40267 + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
40268 + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
40269 + DWC_PRINTF(" do_split: %d\n", hc->do_split);
40270 + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
40271 + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
40272 + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
40273 + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
40274 + DWC_PRINTF(" requests: %d\n", hc->requests);
40275 + DWC_PRINTF(" qh: %p\n", hc->qh);
40276 + if (hc->xfer_started) {
40277 + hfnum_data_t hfnum;
40278 + hcchar_data_t hcchar;
40279 + hctsiz_data_t hctsiz;
40280 + hcint_data_t hcint;
40281 + hcintmsk_data_t hcintmsk;
40282 + hfnum.d32 =
40283 + DWC_READ_REG32(&hcd->core_if->
40284 + host_if->host_global_regs->hfnum);
40285 + hcchar.d32 =
40286 + DWC_READ_REG32(&hcd->core_if->host_if->
40287 + hc_regs[i]->hcchar);
40288 + hctsiz.d32 =
40289 + DWC_READ_REG32(&hcd->core_if->host_if->
40290 + hc_regs[i]->hctsiz);
40291 + hcint.d32 =
40292 + DWC_READ_REG32(&hcd->core_if->host_if->
40293 + hc_regs[i]->hcint);
40294 + hcintmsk.d32 =
40295 + DWC_READ_REG32(&hcd->core_if->host_if->
40296 + hc_regs[i]->hcintmsk);
40297 + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
40298 + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
40299 + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
40300 + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
40301 + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
40302 + }
40303 + if (hc->xfer_started && hc->qh) {
40304 + dwc_otg_qtd_t *qtd;
40305 + dwc_otg_hcd_urb_t *urb;
40306 +
40307 + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
40308 + if (!qtd->in_process)
40309 + break;
40310 +
40311 + urb = qtd->urb;
40312 + DWC_PRINTF(" URB Info:\n");
40313 + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
40314 + if (urb) {
40315 + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
40316 + dwc_otg_hcd_get_dev_addr(&urb->
40317 + pipe_info),
40318 + dwc_otg_hcd_get_ep_num(&urb->
40319 + pipe_info),
40320 + dwc_otg_hcd_is_pipe_in(&urb->
40321 + pipe_info) ?
40322 + "IN" : "OUT");
40323 + DWC_PRINTF(" Max packet size: %d\n",
40324 + dwc_otg_hcd_get_mps(&urb->
40325 + pipe_info));
40326 + DWC_PRINTF(" transfer_buffer: %p\n",
40327 + urb->buf);
40328 + DWC_PRINTF(" transfer_dma: %p\n",
40329 + (void *)urb->dma);
40330 + DWC_PRINTF(" transfer_buffer_length: %d\n",
40331 + urb->length);
40332 + DWC_PRINTF(" actual_length: %d\n",
40333 + urb->actual_length);
40334 + }
40335 + }
40336 + }
40337 + }
40338 + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
40339 + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
40340 + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
40341 + np_tx_status.d32 =
40342 + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
40343 + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
40344 + np_tx_status.b.nptxqspcavail);
40345 + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
40346 + np_tx_status.b.nptxfspcavail);
40347 + p_tx_status.d32 =
40348 + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
40349 + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
40350 + p_tx_status.b.ptxqspcavail);
40351 + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
40352 + dwc_otg_hcd_dump_frrem(hcd);
40353 + dwc_otg_dump_global_registers(hcd->core_if);
40354 + dwc_otg_dump_host_registers(hcd->core_if);
40355 + DWC_PRINTF
40356 + ("************************************************************\n");
40357 + DWC_PRINTF("\n");
40358 +#endif
40359 +}
40360 +
40361 +#ifdef DEBUG
40362 +void dwc_print_setup_data(uint8_t * setup)
40363 +{
40364 + int i;
40365 + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
40366 + DWC_PRINTF("Setup Data = MSB ");
40367 + for (i = 7; i >= 0; i--)
40368 + DWC_PRINTF("%02x ", setup[i]);
40369 + DWC_PRINTF("\n");
40370 + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
40371 + (setup[0] & 0x80) ? "Device-to-Host" :
40372 + "Host-to-Device");
40373 + DWC_PRINTF(" bmRequestType Type = ");
40374 + switch ((setup[0] & 0x60) >> 5) {
40375 + case 0:
40376 + DWC_PRINTF("Standard\n");
40377 + break;
40378 + case 1:
40379 + DWC_PRINTF("Class\n");
40380 + break;
40381 + case 2:
40382 + DWC_PRINTF("Vendor\n");
40383 + break;
40384 + case 3:
40385 + DWC_PRINTF("Reserved\n");
40386 + break;
40387 + }
40388 + DWC_PRINTF(" bmRequestType Recipient = ");
40389 + switch (setup[0] & 0x1f) {
40390 + case 0:
40391 + DWC_PRINTF("Device\n");
40392 + break;
40393 + case 1:
40394 + DWC_PRINTF("Interface\n");
40395 + break;
40396 + case 2:
40397 + DWC_PRINTF("Endpoint\n");
40398 + break;
40399 + case 3:
40400 + DWC_PRINTF("Other\n");
40401 + break;
40402 + default:
40403 + DWC_PRINTF("Reserved\n");
40404 + break;
40405 + }
40406 + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
40407 + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
40408 + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
40409 + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
40410 + }
40411 +}
40412 +#endif
40413 +
40414 +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
40415 +{
40416 +#if 0
40417 + DWC_PRINTF("Frame remaining at SOF:\n");
40418 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40419 + hcd->frrem_samples, hcd->frrem_accum,
40420 + (hcd->frrem_samples > 0) ?
40421 + hcd->frrem_accum / hcd->frrem_samples : 0);
40422 +
40423 + DWC_PRINTF("\n");
40424 + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
40425 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40426 + hcd->core_if->hfnum_7_samples,
40427 + hcd->core_if->hfnum_7_frrem_accum,
40428 + (hcd->core_if->hfnum_7_samples >
40429 + 0) ? hcd->core_if->hfnum_7_frrem_accum /
40430 + hcd->core_if->hfnum_7_samples : 0);
40431 + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
40432 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40433 + hcd->core_if->hfnum_0_samples,
40434 + hcd->core_if->hfnum_0_frrem_accum,
40435 + (hcd->core_if->hfnum_0_samples >
40436 + 0) ? hcd->core_if->hfnum_0_frrem_accum /
40437 + hcd->core_if->hfnum_0_samples : 0);
40438 + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
40439 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40440 + hcd->core_if->hfnum_other_samples,
40441 + hcd->core_if->hfnum_other_frrem_accum,
40442 + (hcd->core_if->hfnum_other_samples >
40443 + 0) ? hcd->core_if->hfnum_other_frrem_accum /
40444 + hcd->core_if->hfnum_other_samples : 0);
40445 +
40446 + DWC_PRINTF("\n");
40447 + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
40448 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40449 + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
40450 + (hcd->hfnum_7_samples_a > 0) ?
40451 + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
40452 + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
40453 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40454 + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
40455 + (hcd->hfnum_0_samples_a > 0) ?
40456 + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
40457 + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
40458 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40459 + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
40460 + (hcd->hfnum_other_samples_a > 0) ?
40461 + hcd->hfnum_other_frrem_accum_a /
40462 + hcd->hfnum_other_samples_a : 0);
40463 +
40464 + DWC_PRINTF("\n");
40465 + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
40466 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40467 + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
40468 + (hcd->hfnum_7_samples_b > 0) ?
40469 + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
40470 + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
40471 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40472 + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
40473 + (hcd->hfnum_0_samples_b > 0) ?
40474 + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
40475 + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
40476 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40477 + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
40478 + (hcd->hfnum_other_samples_b > 0) ?
40479 + hcd->hfnum_other_frrem_accum_b /
40480 + hcd->hfnum_other_samples_b : 0);
40481 +#endif
40482 +}
40483 +
40484 +#endif /* DWC_DEVICE_ONLY */
40485 --- /dev/null
40486 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
40487 @@ -0,0 +1,868 @@
40488 +/* ==========================================================================
40489 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
40490 + * $Revision: #58 $
40491 + * $Date: 2011/09/15 $
40492 + * $Change: 1846647 $
40493 + *
40494 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
40495 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
40496 + * otherwise expressly agreed to in writing between Synopsys and you.
40497 + *
40498 + * The Software IS NOT an item of Licensed Software or Licensed Product under
40499 + * any End User Software License Agreement or Agreement for Licensed Product
40500 + * with Synopsys or any supplement thereto. You are permitted to use and
40501 + * redistribute this Software in source and binary forms, with or without
40502 + * modification, provided that redistributions of source code must retain this
40503 + * notice. You may not view, use, disclose, copy or distribute this file or
40504 + * any information contained herein except pursuant to this license grant from
40505 + * Synopsys. If you do not agree with this notice, including the disclaimer
40506 + * below, then you are not authorized to use the Software.
40507 + *
40508 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
40509 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40510 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40511 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
40512 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40513 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40514 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40515 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40516 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40517 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40518 + * DAMAGE.
40519 + * ========================================================================== */
40520 +#ifndef DWC_DEVICE_ONLY
40521 +#ifndef __DWC_HCD_H__
40522 +#define __DWC_HCD_H__
40523 +
40524 +#include "dwc_otg_os_dep.h"
40525 +#include "usb.h"
40526 +#include "dwc_otg_hcd_if.h"
40527 +#include "dwc_otg_core_if.h"
40528 +#include "dwc_list.h"
40529 +#include "dwc_otg_cil.h"
40530 +#include "dwc_otg_fiq_fsm.h"
40531 +#include "dwc_otg_driver.h"
40532 +
40533 +
40534 +/**
40535 + * @file
40536 + *
40537 + * This file contains the structures, constants, and interfaces for
40538 + * the Host Contoller Driver (HCD).
40539 + *
40540 + * The Host Controller Driver (HCD) is responsible for translating requests
40541 + * from the USB Driver into the appropriate actions on the DWC_otg controller.
40542 + * It isolates the USBD from the specifics of the controller by providing an
40543 + * API to the USBD.
40544 + */
40545 +
40546 +struct dwc_otg_hcd_pipe_info {
40547 + uint8_t dev_addr;
40548 + uint8_t ep_num;
40549 + uint8_t pipe_type;
40550 + uint8_t pipe_dir;
40551 + uint16_t mps;
40552 +};
40553 +
40554 +struct dwc_otg_hcd_iso_packet_desc {
40555 + uint32_t offset;
40556 + uint32_t length;
40557 + uint32_t actual_length;
40558 + uint32_t status;
40559 +};
40560 +
40561 +struct dwc_otg_qtd;
40562 +
40563 +struct dwc_otg_hcd_urb {
40564 + void *priv;
40565 + struct dwc_otg_qtd *qtd;
40566 + void *buf;
40567 + dwc_dma_t dma;
40568 + void *setup_packet;
40569 + dwc_dma_t setup_dma;
40570 + uint32_t length;
40571 + uint32_t actual_length;
40572 + uint32_t status;
40573 + uint32_t error_count;
40574 + uint32_t packet_count;
40575 + uint32_t flags;
40576 + uint16_t interval;
40577 + struct dwc_otg_hcd_pipe_info pipe_info;
40578 + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
40579 +};
40580 +
40581 +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
40582 +{
40583 + return pipe->ep_num;
40584 +}
40585 +
40586 +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
40587 + *pipe)
40588 +{
40589 + return pipe->pipe_type;
40590 +}
40591 +
40592 +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
40593 +{
40594 + return pipe->mps;
40595 +}
40596 +
40597 +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
40598 + *pipe)
40599 +{
40600 + return pipe->dev_addr;
40601 +}
40602 +
40603 +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
40604 + *pipe)
40605 +{
40606 + return (pipe->pipe_type == UE_ISOCHRONOUS);
40607 +}
40608 +
40609 +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
40610 + *pipe)
40611 +{
40612 + return (pipe->pipe_type == UE_INTERRUPT);
40613 +}
40614 +
40615 +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
40616 + *pipe)
40617 +{
40618 + return (pipe->pipe_type == UE_BULK);
40619 +}
40620 +
40621 +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
40622 + *pipe)
40623 +{
40624 + return (pipe->pipe_type == UE_CONTROL);
40625 +}
40626 +
40627 +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
40628 +{
40629 + return (pipe->pipe_dir == UE_DIR_IN);
40630 +}
40631 +
40632 +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
40633 + *pipe)
40634 +{
40635 + return (!dwc_otg_hcd_is_pipe_in(pipe));
40636 +}
40637 +
40638 +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
40639 + uint8_t devaddr, uint8_t ep_num,
40640 + uint8_t pipe_type, uint8_t pipe_dir,
40641 + uint16_t mps)
40642 +{
40643 + pipe->dev_addr = devaddr;
40644 + pipe->ep_num = ep_num;
40645 + pipe->pipe_type = pipe_type;
40646 + pipe->pipe_dir = pipe_dir;
40647 + pipe->mps = mps;
40648 +}
40649 +
40650 +/**
40651 + * Phases for control transfers.
40652 + */
40653 +typedef enum dwc_otg_control_phase {
40654 + DWC_OTG_CONTROL_SETUP,
40655 + DWC_OTG_CONTROL_DATA,
40656 + DWC_OTG_CONTROL_STATUS
40657 +} dwc_otg_control_phase_e;
40658 +
40659 +/** Transaction types. */
40660 +typedef enum dwc_otg_transaction_type {
40661 + DWC_OTG_TRANSACTION_NONE = 0,
40662 + DWC_OTG_TRANSACTION_PERIODIC = 1,
40663 + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
40664 + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
40665 +} dwc_otg_transaction_type_e;
40666 +
40667 +struct dwc_otg_qh;
40668 +
40669 +/**
40670 + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
40671 + * interrupt, or isochronous transfer. A single QTD is created for each URB
40672 + * (of one of these types) submitted to the HCD. The transfer associated with
40673 + * a QTD may require one or multiple transactions.
40674 + *
40675 + * A QTD is linked to a Queue Head, which is entered in either the
40676 + * non-periodic or periodic schedule for execution. When a QTD is chosen for
40677 + * execution, some or all of its transactions may be executed. After
40678 + * execution, the state of the QTD is updated. The QTD may be retired if all
40679 + * its transactions are complete or if an error occurred. Otherwise, it
40680 + * remains in the schedule so more transactions can be executed later.
40681 + */
40682 +typedef struct dwc_otg_qtd {
40683 + /**
40684 + * Determines the PID of the next data packet for the data phase of
40685 + * control transfers. Ignored for other transfer types.<br>
40686 + * One of the following values:
40687 + * - DWC_OTG_HC_PID_DATA0
40688 + * - DWC_OTG_HC_PID_DATA1
40689 + */
40690 + uint8_t data_toggle;
40691 +
40692 + /** Current phase for control transfers (Setup, Data, or Status). */
40693 + dwc_otg_control_phase_e control_phase;
40694 +
40695 + /** Keep track of the current split type
40696 + * for FS/LS endpoints on a HS Hub */
40697 + uint8_t complete_split;
40698 +
40699 + /** How many bytes transferred during SSPLIT OUT */
40700 + uint32_t ssplit_out_xfer_count;
40701 +
40702 + /**
40703 + * Holds the number of bus errors that have occurred for a transaction
40704 + * within this transfer.
40705 + */
40706 + uint8_t error_count;
40707 +
40708 + /**
40709 + * Index of the next frame descriptor for an isochronous transfer. A
40710 + * frame descriptor describes the buffer position and length of the
40711 + * data to be transferred in the next scheduled (micro)frame of an
40712 + * isochronous transfer. It also holds status for that transaction.
40713 + * The frame index starts at 0.
40714 + */
40715 + uint16_t isoc_frame_index;
40716 +
40717 + /** Position of the ISOC split on full/low speed */
40718 + uint8_t isoc_split_pos;
40719 +
40720 + /** Position of the ISOC split in the buffer for the current frame */
40721 + uint16_t isoc_split_offset;
40722 +
40723 + /** URB for this transfer */
40724 + struct dwc_otg_hcd_urb *urb;
40725 +
40726 + struct dwc_otg_qh *qh;
40727 +
40728 + /** This list of QTDs */
40729 + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
40730 +
40731 + /** Indicates if this QTD is currently processed by HW. */
40732 + uint8_t in_process;
40733 +
40734 + /** Number of DMA descriptors for this QTD */
40735 + uint8_t n_desc;
40736 +
40737 + /**
40738 + * Last activated frame(packet) index.
40739 + * Used in Descriptor DMA mode only.
40740 + */
40741 + uint16_t isoc_frame_index_last;
40742 +
40743 +} dwc_otg_qtd_t;
40744 +
40745 +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
40746 +
40747 +/**
40748 + * A Queue Head (QH) holds the static characteristics of an endpoint and
40749 + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
40750 + * be entered in either the non-periodic or periodic schedule.
40751 + */
40752 +typedef struct dwc_otg_qh {
40753 + /**
40754 + * Endpoint type.
40755 + * One of the following values:
40756 + * - UE_CONTROL
40757 + * - UE_BULK
40758 + * - UE_INTERRUPT
40759 + * - UE_ISOCHRONOUS
40760 + */
40761 + uint8_t ep_type;
40762 + uint8_t ep_is_in;
40763 +
40764 + /** wMaxPacketSize Field of Endpoint Descriptor. */
40765 + uint16_t maxp;
40766 +
40767 + /**
40768 + * Device speed.
40769 + * One of the following values:
40770 + * - DWC_OTG_EP_SPEED_LOW
40771 + * - DWC_OTG_EP_SPEED_FULL
40772 + * - DWC_OTG_EP_SPEED_HIGH
40773 + */
40774 + uint8_t dev_speed;
40775 +
40776 + /**
40777 + * Determines the PID of the next data packet for non-control
40778 + * transfers. Ignored for control transfers.<br>
40779 + * One of the following values:
40780 + * - DWC_OTG_HC_PID_DATA0
40781 + * - DWC_OTG_HC_PID_DATA1
40782 + */
40783 + uint8_t data_toggle;
40784 +
40785 + /** Ping state if 1. */
40786 + uint8_t ping_state;
40787 +
40788 + /**
40789 + * List of QTDs for this QH.
40790 + */
40791 + struct dwc_otg_qtd_list qtd_list;
40792 +
40793 + /** Host channel currently processing transfers for this QH. */
40794 + struct dwc_hc *channel;
40795 +
40796 + /** Full/low speed endpoint on high-speed hub requires split. */
40797 + uint8_t do_split;
40798 +
40799 + /** @name Periodic schedule information */
40800 + /** @{ */
40801 +
40802 + /** Bandwidth in microseconds per (micro)frame. */
40803 + uint16_t usecs;
40804 +
40805 + /** Interval between transfers in (micro)frames. */
40806 + uint16_t interval;
40807 +
40808 + /**
40809 + * (micro)frame to initialize a periodic transfer. The transfer
40810 + * executes in the following (micro)frame.
40811 + */
40812 + uint16_t sched_frame;
40813 +
40814 + /*
40815 + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
40816 + */
40817 + uint16_t nak_frame;
40818 +
40819 + /** (micro)frame at which last start split was initialized. */
40820 + uint16_t start_split_frame;
40821 +
40822 + /** @} */
40823 +
40824 + /**
40825 + * Used instead of original buffer if
40826 + * it(physical address) is not dword-aligned.
40827 + */
40828 + uint8_t *dw_align_buf;
40829 + dwc_dma_t dw_align_buf_dma;
40830 +
40831 + /** Entry for QH in either the periodic or non-periodic schedule. */
40832 + dwc_list_link_t qh_list_entry;
40833 +
40834 + /** @name Descriptor DMA support */
40835 + /** @{ */
40836 +
40837 + /** Descriptor List. */
40838 + dwc_otg_host_dma_desc_t *desc_list;
40839 +
40840 + /** Descriptor List physical address. */
40841 + dwc_dma_t desc_list_dma;
40842 +
40843 + /**
40844 + * Xfer Bytes array.
40845 + * Each element corresponds to a descriptor and indicates
40846 + * original XferSize size value for the descriptor.
40847 + */
40848 + uint32_t *n_bytes;
40849 +
40850 + /** Actual number of transfer descriptors in a list. */
40851 + uint16_t ntd;
40852 +
40853 + /** First activated isochronous transfer descriptor index. */
40854 + uint8_t td_first;
40855 + /** Last activated isochronous transfer descriptor index. */
40856 + uint8_t td_last;
40857 +
40858 + /** @} */
40859 +
40860 +
40861 + uint16_t speed;
40862 + uint16_t frame_usecs[8];
40863 +
40864 + uint32_t skip_count;
40865 +} dwc_otg_qh_t;
40866 +
40867 +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
40868 +
40869 +typedef struct urb_tq_entry {
40870 + struct urb *urb;
40871 + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
40872 +} urb_tq_entry_t;
40873 +
40874 +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
40875 +
40876 +/**
40877 + * This structure holds the state of the HCD, including the non-periodic and
40878 + * periodic schedules.
40879 + */
40880 +struct dwc_otg_hcd {
40881 + /** The DWC otg device pointer */
40882 + struct dwc_otg_device *otg_dev;
40883 + /** DWC OTG Core Interface Layer */
40884 + dwc_otg_core_if_t *core_if;
40885 +
40886 + /** Function HCD driver callbacks */
40887 + struct dwc_otg_hcd_function_ops *fops;
40888 +
40889 + /** Internal DWC HCD Flags */
40890 + volatile union dwc_otg_hcd_internal_flags {
40891 + uint32_t d32;
40892 + struct {
40893 + unsigned port_connect_status_change:1;
40894 + unsigned port_connect_status:1;
40895 + unsigned port_reset_change:1;
40896 + unsigned port_enable_change:1;
40897 + unsigned port_suspend_change:1;
40898 + unsigned port_over_current_change:1;
40899 + unsigned port_l1_change:1;
40900 + unsigned reserved:26;
40901 + } b;
40902 + } flags;
40903 +
40904 + /**
40905 + * Inactive items in the non-periodic schedule. This is a list of
40906 + * Queue Heads. Transfers associated with these Queue Heads are not
40907 + * currently assigned to a host channel.
40908 + */
40909 + dwc_list_link_t non_periodic_sched_inactive;
40910 +
40911 + /**
40912 + * Active items in the non-periodic schedule. This is a list of
40913 + * Queue Heads. Transfers associated with these Queue Heads are
40914 + * currently assigned to a host channel.
40915 + */
40916 + dwc_list_link_t non_periodic_sched_active;
40917 +
40918 + /**
40919 + * Pointer to the next Queue Head to process in the active
40920 + * non-periodic schedule.
40921 + */
40922 + dwc_list_link_t *non_periodic_qh_ptr;
40923 +
40924 + /**
40925 + * Inactive items in the periodic schedule. This is a list of QHs for
40926 + * periodic transfers that are _not_ scheduled for the next frame.
40927 + * Each QH in the list has an interval counter that determines when it
40928 + * needs to be scheduled for execution. This scheduling mechanism
40929 + * allows only a simple calculation for periodic bandwidth used (i.e.
40930 + * must assume that all periodic transfers may need to execute in the
40931 + * same frame). However, it greatly simplifies scheduling and should
40932 + * be sufficient for the vast majority of OTG hosts, which need to
40933 + * connect to a small number of peripherals at one time.
40934 + *
40935 + * Items move from this list to periodic_sched_ready when the QH
40936 + * interval counter is 0 at SOF.
40937 + */
40938 + dwc_list_link_t periodic_sched_inactive;
40939 +
40940 + /**
40941 + * List of periodic QHs that are ready for execution in the next
40942 + * frame, but have not yet been assigned to host channels.
40943 + *
40944 + * Items move from this list to periodic_sched_assigned as host
40945 + * channels become available during the current frame.
40946 + */
40947 + dwc_list_link_t periodic_sched_ready;
40948 +
40949 + /**
40950 + * List of periodic QHs to be executed in the next frame that are
40951 + * assigned to host channels.
40952 + *
40953 + * Items move from this list to periodic_sched_queued as the
40954 + * transactions for the QH are queued to the DWC_otg controller.
40955 + */
40956 + dwc_list_link_t periodic_sched_assigned;
40957 +
40958 + /**
40959 + * List of periodic QHs that have been queued for execution.
40960 + *
40961 + * Items move from this list to either periodic_sched_inactive or
40962 + * periodic_sched_ready when the channel associated with the transfer
40963 + * is released. If the interval for the QH is 1, the item moves to
40964 + * periodic_sched_ready because it must be rescheduled for the next
40965 + * frame. Otherwise, the item moves to periodic_sched_inactive.
40966 + */
40967 + dwc_list_link_t periodic_sched_queued;
40968 +
40969 + /**
40970 + * Total bandwidth claimed so far for periodic transfers. This value
40971 + * is in microseconds per (micro)frame. The assumption is that all
40972 + * periodic transfers may occur in the same (micro)frame.
40973 + */
40974 + uint16_t periodic_usecs;
40975 +
40976 + /**
40977 + * Total bandwidth claimed so far for all periodic transfers
40978 + * in a frame.
40979 + * This will include a mixture of HS and FS transfers.
40980 + * Units are microseconds per (micro)frame.
40981 + * We have a budget per frame and have to schedule
40982 + * transactions accordingly.
40983 + * Watch out for the fact that things are actually scheduled for the
40984 + * "next frame".
40985 + */
40986 + uint16_t frame_usecs[8];
40987 +
40988 +
40989 + /**
40990 + * Frame number read from the core at SOF. The value ranges from 0 to
40991 + * DWC_HFNUM_MAX_FRNUM.
40992 + */
40993 + uint16_t frame_number;
40994 +
40995 + /**
40996 + * Count of periodic QHs, if using several eps. For SOF enable/disable.
40997 + */
40998 + uint16_t periodic_qh_count;
40999 +
41000 + /**
41001 + * Free host channels in the controller. This is a list of
41002 + * dwc_hc_t items.
41003 + */
41004 + struct hc_list free_hc_list;
41005 + /**
41006 + * Number of host channels assigned to periodic transfers. Currently
41007 + * assuming that there is a dedicated host channel for each periodic
41008 + * transaction and at least one host channel available for
41009 + * non-periodic transactions.
41010 + */
41011 + int periodic_channels; /* microframe_schedule==0 */
41012 +
41013 + /**
41014 + * Number of host channels assigned to non-periodic transfers.
41015 + */
41016 + int non_periodic_channels; /* microframe_schedule==0 */
41017 +
41018 + /**
41019 + * Number of host channels assigned to non-periodic transfers.
41020 + */
41021 + int available_host_channels;
41022 +
41023 + /**
41024 + * Array of pointers to the host channel descriptors. Allows accessing
41025 + * a host channel descriptor given the host channel number. This is
41026 + * useful in interrupt handlers.
41027 + */
41028 + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
41029 +
41030 + /**
41031 + * Buffer to use for any data received during the status phase of a
41032 + * control transfer. Normally no data is transferred during the status
41033 + * phase. This buffer is used as a bit bucket.
41034 + */
41035 + uint8_t *status_buf;
41036 +
41037 + /**
41038 + * DMA address for status_buf.
41039 + */
41040 + dma_addr_t status_buf_dma;
41041 +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
41042 +
41043 + /**
41044 + * Connection timer. An OTG host must display a message if the device
41045 + * does not connect. Started when the VBus power is turned on via
41046 + * sysfs attribute "buspower".
41047 + */
41048 + dwc_timer_t *conn_timer;
41049 +
41050 + /* Tasket to do a reset */
41051 + dwc_tasklet_t *reset_tasklet;
41052 +
41053 + dwc_tasklet_t *completion_tasklet;
41054 + struct urb_list completed_urb_list;
41055 +
41056 + /* */
41057 + dwc_spinlock_t *lock;
41058 + dwc_spinlock_t *channel_lock;
41059 + /**
41060 + * Private data that could be used by OS wrapper.
41061 + */
41062 + void *priv;
41063 +
41064 + uint8_t otg_port;
41065 +
41066 + /** Frame List */
41067 + uint32_t *frame_list;
41068 +
41069 + /** Hub - Port assignment */
41070 + int hub_port[128];
41071 +#ifdef FIQ_DEBUG
41072 + int hub_port_alloc[2048];
41073 +#endif
41074 +
41075 + /** Frame List DMA address */
41076 + dma_addr_t frame_list_dma;
41077 +
41078 + struct fiq_stack *fiq_stack;
41079 + struct fiq_state *fiq_state;
41080 +
41081 + /** Virtual address for split transaction DMA bounce buffers */
41082 + struct fiq_dma_blob *fiq_dmab;
41083 +
41084 +#ifdef DEBUG
41085 + uint32_t frrem_samples;
41086 + uint64_t frrem_accum;
41087 +
41088 + uint32_t hfnum_7_samples_a;
41089 + uint64_t hfnum_7_frrem_accum_a;
41090 + uint32_t hfnum_0_samples_a;
41091 + uint64_t hfnum_0_frrem_accum_a;
41092 + uint32_t hfnum_other_samples_a;
41093 + uint64_t hfnum_other_frrem_accum_a;
41094 +
41095 + uint32_t hfnum_7_samples_b;
41096 + uint64_t hfnum_7_frrem_accum_b;
41097 + uint32_t hfnum_0_samples_b;
41098 + uint64_t hfnum_0_frrem_accum_b;
41099 + uint32_t hfnum_other_samples_b;
41100 + uint64_t hfnum_other_frrem_accum_b;
41101 +#endif
41102 +};
41103 +
41104 +static inline struct device *dwc_otg_hcd_to_dev(struct dwc_otg_hcd *hcd)
41105 +{
41106 + return &hcd->otg_dev->os_dep.platformdev->dev;
41107 +}
41108 +
41109 +/** @name Transaction Execution Functions */
41110 +/** @{ */
41111 +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
41112 + * hcd);
41113 +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
41114 + dwc_otg_transaction_type_e tr_type);
41115 +
41116 +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
41117 +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
41118 +
41119 +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
41120 +extern int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh);
41121 +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
41122 +
41123 +/** @} */
41124 +
41125 +/** @name Interrupt Handler Functions */
41126 +/** @{ */
41127 +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41128 +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41129 +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
41130 + dwc_otg_hcd);
41131 +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
41132 + dwc_otg_hcd);
41133 +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
41134 + dwc_otg_hcd);
41135 +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
41136 + dwc_otg_hcd);
41137 +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41138 +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
41139 + dwc_otg_hcd);
41140 +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41141 +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41142 +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
41143 + uint32_t num);
41144 +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41145 +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
41146 + dwc_otg_hcd);
41147 +/** @} */
41148 +
41149 +/** @name Schedule Queue Functions */
41150 +/** @{ */
41151 +
41152 +/* Implemented in dwc_otg_hcd_queue.c */
41153 +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
41154 + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
41155 +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41156 +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41157 +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41158 +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41159 + int sched_csplit);
41160 +
41161 +/** Remove and free a QH */
41162 +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
41163 + dwc_otg_qh_t * qh)
41164 +{
41165 + dwc_irqflags_t flags;
41166 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
41167 + dwc_otg_hcd_qh_remove(hcd, qh);
41168 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
41169 + dwc_otg_hcd_qh_free(hcd, qh);
41170 +}
41171 +
41172 +/** Allocates memory for a QH structure.
41173 + * @return Returns the memory allocate or NULL on error. */
41174 +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
41175 +{
41176 + if (atomic_alloc)
41177 + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
41178 + else
41179 + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
41180 +}
41181 +
41182 +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
41183 + int atomic_alloc);
41184 +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
41185 +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
41186 + dwc_otg_qh_t ** qh, int atomic_alloc);
41187 +
41188 +/** Allocates memory for a QTD structure.
41189 + * @return Returns the memory allocate or NULL on error. */
41190 +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
41191 +{
41192 + if (atomic_alloc)
41193 + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
41194 + else
41195 + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
41196 +}
41197 +
41198 +/** Frees the memory for a QTD structure. QTD should already be removed from
41199 + * list.
41200 + * @param qtd QTD to free.*/
41201 +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
41202 +{
41203 + DWC_FREE(qtd);
41204 +}
41205 +
41206 +/** Removes a QTD from list.
41207 + * @param hcd HCD instance.
41208 + * @param qtd QTD to remove from list.
41209 + * @param qh QTD belongs to.
41210 + */
41211 +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
41212 + dwc_otg_qtd_t * qtd,
41213 + dwc_otg_qh_t * qh)
41214 +{
41215 + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
41216 +}
41217 +
41218 +/** Remove and free a QTD
41219 + * Need to disable IRQ and hold hcd lock while calling this function out of
41220 + * interrupt servicing chain */
41221 +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
41222 + dwc_otg_qtd_t * qtd,
41223 + dwc_otg_qh_t * qh)
41224 +{
41225 + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
41226 + dwc_otg_hcd_qtd_free(qtd);
41227 +}
41228 +
41229 +/** @} */
41230 +
41231 +/** @name Descriptor DMA Supporting Functions */
41232 +/** @{ */
41233 +
41234 +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41235 +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
41236 + dwc_hc_t * hc,
41237 + dwc_otg_hc_regs_t * hc_regs,
41238 + dwc_otg_halt_status_e halt_status);
41239 +
41240 +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41241 +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41242 +
41243 +/** @} */
41244 +
41245 +/** @name Internal Functions */
41246 +/** @{ */
41247 +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
41248 +/** @} */
41249 +
41250 +#ifdef CONFIG_USB_DWC_OTG_LPM
41251 +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
41252 + uint8_t devaddr);
41253 +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
41254 +#endif
41255 +
41256 +/** Gets the QH that contains the list_head */
41257 +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
41258 +
41259 +/** Gets the QTD that contains the list_head */
41260 +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
41261 +
41262 +/** Check if QH is non-periodic */
41263 +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
41264 + (_qh_ptr_->ep_type == UE_CONTROL))
41265 +
41266 +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
41267 +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
41268 +
41269 +/** Packet size for any kind of endpoint descriptor */
41270 +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
41271 +
41272 +/**
41273 + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
41274 + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
41275 + * frame number when the max frame number is reached.
41276 + */
41277 +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
41278 +{
41279 + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
41280 + (DWC_HFNUM_MAX_FRNUM >> 1);
41281 +}
41282 +
41283 +/**
41284 + * Returns true if _frame1 is greater than _frame2. The comparison is done
41285 + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
41286 + * number when the max frame number is reached.
41287 + */
41288 +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
41289 +{
41290 + return (frame1 != frame2) &&
41291 + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
41292 + (DWC_HFNUM_MAX_FRNUM >> 1));
41293 +}
41294 +
41295 +/**
41296 + * Increments _frame by the amount specified by _inc. The addition is done
41297 + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
41298 + */
41299 +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
41300 +{
41301 + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
41302 +}
41303 +
41304 +static inline uint16_t dwc_full_frame_num(uint16_t frame)
41305 +{
41306 + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
41307 +}
41308 +
41309 +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
41310 +{
41311 + return frame & 0x7;
41312 +}
41313 +
41314 +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
41315 + dwc_otg_hc_regs_t * hc_regs,
41316 + dwc_otg_qtd_t * qtd);
41317 +
41318 +#ifdef DEBUG
41319 +/**
41320 + * Macro to sample the remaining PHY clocks left in the current frame. This
41321 + * may be used during debugging to determine the average time it takes to
41322 + * execute sections of code. There are two possible sample points, "a" and
41323 + * "b", so the _letter argument must be one of these values.
41324 + *
41325 + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
41326 + * example, "cat /sys/devices/lm0/hcd_frrem".
41327 + */
41328 +#define dwc_sample_frrem(_hcd, _qh, _letter) \
41329 +{ \
41330 + hfnum_data_t hfnum; \
41331 + dwc_otg_qtd_t *qtd; \
41332 + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
41333 + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
41334 + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
41335 + switch (hfnum.b.frnum & 0x7) { \
41336 + case 7: \
41337 + _hcd->hfnum_7_samples_##_letter++; \
41338 + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
41339 + break; \
41340 + case 0: \
41341 + _hcd->hfnum_0_samples_##_letter++; \
41342 + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
41343 + break; \
41344 + default: \
41345 + _hcd->hfnum_other_samples_##_letter++; \
41346 + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
41347 + break; \
41348 + } \
41349 + } \
41350 +}
41351 +#else
41352 +#define dwc_sample_frrem(_hcd, _qh, _letter)
41353 +#endif
41354 +#endif
41355 +#endif /* DWC_DEVICE_ONLY */
41356 --- /dev/null
41357 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
41358 @@ -0,0 +1,1139 @@
41359 +/*==========================================================================
41360 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
41361 + * $Revision: #10 $
41362 + * $Date: 2011/10/20 $
41363 + * $Change: 1869464 $
41364 + *
41365 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
41366 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
41367 + * otherwise expressly agreed to in writing between Synopsys and you.
41368 + *
41369 + * The Software IS NOT an item of Licensed Software or Licensed Product under
41370 + * any End User Software License Agreement or Agreement for Licensed Product
41371 + * with Synopsys or any supplement thereto. You are permitted to use and
41372 + * redistribute this Software in source and binary forms, with or without
41373 + * modification, provided that redistributions of source code must retain this
41374 + * notice. You may not view, use, disclose, copy or distribute this file or
41375 + * any information contained herein except pursuant to this license grant from
41376 + * Synopsys. If you do not agree with this notice, including the disclaimer
41377 + * below, then you are not authorized to use the Software.
41378 + *
41379 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
41380 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41381 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41382 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
41383 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41384 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41385 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41386 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41387 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41388 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41389 + * DAMAGE.
41390 + * ========================================================================== */
41391 +#ifndef DWC_DEVICE_ONLY
41392 +
41393 +/** @file
41394 + * This file contains Descriptor DMA support implementation for host mode.
41395 + */
41396 +
41397 +#include "dwc_otg_hcd.h"
41398 +#include "dwc_otg_regs.h"
41399 +
41400 +extern bool microframe_schedule;
41401 +
41402 +static inline uint8_t frame_list_idx(uint16_t frame)
41403 +{
41404 + return (frame & (MAX_FRLIST_EN_NUM - 1));
41405 +}
41406 +
41407 +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
41408 +{
41409 + return (idx + inc) &
41410 + (((speed ==
41411 + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
41412 + MAX_DMA_DESC_NUM_GENERIC) - 1);
41413 +}
41414 +
41415 +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
41416 +{
41417 + return (idx - inc) &
41418 + (((speed ==
41419 + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
41420 + MAX_DMA_DESC_NUM_GENERIC) - 1);
41421 +}
41422 +
41423 +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
41424 +{
41425 + return (((qh->ep_type == UE_ISOCHRONOUS)
41426 + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
41427 + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
41428 +}
41429 +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
41430 +{
41431 + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
41432 + ? ((qh->interval + 8 - 1) / 8)
41433 + : qh->interval);
41434 +}
41435 +
41436 +static int desc_list_alloc(struct device *dev, dwc_otg_qh_t * qh)
41437 +{
41438 + int retval = 0;
41439 +
41440 + qh->desc_list = (dwc_otg_host_dma_desc_t *)
41441 + DWC_DMA_ALLOC(dev, sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
41442 + &qh->desc_list_dma);
41443 +
41444 + if (!qh->desc_list) {
41445 + retval = -DWC_E_NO_MEMORY;
41446 + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
41447 +
41448 + }
41449 +
41450 + dwc_memset(qh->desc_list, 0x00,
41451 + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
41452 +
41453 + qh->n_bytes =
41454 + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
41455 +
41456 + if (!qh->n_bytes) {
41457 + retval = -DWC_E_NO_MEMORY;
41458 + DWC_ERROR
41459 + ("%s: Failed to allocate array for descriptors' size actual values\n",
41460 + __func__);
41461 +
41462 + }
41463 + return retval;
41464 +
41465 +}
41466 +
41467 +static void desc_list_free(struct device *dev, dwc_otg_qh_t * qh)
41468 +{
41469 + if (qh->desc_list) {
41470 + DWC_DMA_FREE(dev, max_desc_num(qh), qh->desc_list,
41471 + qh->desc_list_dma);
41472 + qh->desc_list = NULL;
41473 + }
41474 +
41475 + if (qh->n_bytes) {
41476 + DWC_FREE(qh->n_bytes);
41477 + qh->n_bytes = NULL;
41478 + }
41479 +}
41480 +
41481 +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
41482 +{
41483 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41484 + int retval = 0;
41485 +
41486 + if (hcd->frame_list)
41487 + return 0;
41488 +
41489 + hcd->frame_list = DWC_DMA_ALLOC(dev, 4 * MAX_FRLIST_EN_NUM,
41490 + &hcd->frame_list_dma);
41491 + if (!hcd->frame_list) {
41492 + retval = -DWC_E_NO_MEMORY;
41493 + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
41494 + }
41495 +
41496 + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
41497 +
41498 + return retval;
41499 +}
41500 +
41501 +static void frame_list_free(dwc_otg_hcd_t * hcd)
41502 +{
41503 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41504 +
41505 + if (!hcd->frame_list)
41506 + return;
41507 +
41508 + DWC_DMA_FREE(dev, 4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
41509 + hcd->frame_list = NULL;
41510 +}
41511 +
41512 +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
41513 +{
41514 +
41515 + hcfg_data_t hcfg;
41516 +
41517 + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
41518 +
41519 + if (hcfg.b.perschedena) {
41520 + /* already enabled */
41521 + return;
41522 + }
41523 +
41524 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
41525 + hcd->frame_list_dma);
41526 +
41527 + switch (fr_list_en) {
41528 + case 64:
41529 + hcfg.b.frlisten = 3;
41530 + break;
41531 + case 32:
41532 + hcfg.b.frlisten = 2;
41533 + break;
41534 + case 16:
41535 + hcfg.b.frlisten = 1;
41536 + break;
41537 + case 8:
41538 + hcfg.b.frlisten = 0;
41539 + break;
41540 + default:
41541 + break;
41542 + }
41543 +
41544 + hcfg.b.perschedena = 1;
41545 +
41546 + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
41547 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
41548 +
41549 +}
41550 +
41551 +static void per_sched_disable(dwc_otg_hcd_t * hcd)
41552 +{
41553 + hcfg_data_t hcfg;
41554 +
41555 + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
41556 +
41557 + if (!hcfg.b.perschedena) {
41558 + /* already disabled */
41559 + return;
41560 + }
41561 + hcfg.b.perschedena = 0;
41562 +
41563 + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
41564 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
41565 +}
41566 +
41567 +/*
41568 + * Activates/Deactivates FrameList entries for the channel
41569 + * based on endpoint servicing period.
41570 + */
41571 +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
41572 +{
41573 + uint16_t i, j, inc;
41574 + dwc_hc_t *hc = NULL;
41575 +
41576 + if (!qh->channel) {
41577 + DWC_ERROR("qh->channel = %p", qh->channel);
41578 + return;
41579 + }
41580 +
41581 + if (!hcd) {
41582 + DWC_ERROR("------hcd = %p", hcd);
41583 + return;
41584 + }
41585 +
41586 + if (!hcd->frame_list) {
41587 + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
41588 + return;
41589 + }
41590 +
41591 + hc = qh->channel;
41592 + inc = frame_incr_val(qh);
41593 + if (qh->ep_type == UE_ISOCHRONOUS)
41594 + i = frame_list_idx(qh->sched_frame);
41595 + else
41596 + i = 0;
41597 +
41598 + j = i;
41599 + do {
41600 + if (enable)
41601 + hcd->frame_list[j] |= (1 << hc->hc_num);
41602 + else
41603 + hcd->frame_list[j] &= ~(1 << hc->hc_num);
41604 + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
41605 + }
41606 + while (j != i);
41607 + if (!enable)
41608 + return;
41609 + hc->schinfo = 0;
41610 + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
41611 + j = 1;
41612 + /* TODO - check this */
41613 + inc = (8 + qh->interval - 1) / qh->interval;
41614 + for (i = 0; i < inc; i++) {
41615 + hc->schinfo |= j;
41616 + j = j << qh->interval;
41617 + }
41618 + } else {
41619 + hc->schinfo = 0xff;
41620 + }
41621 +}
41622 +
41623 +#if 1
41624 +void dump_frame_list(dwc_otg_hcd_t * hcd)
41625 +{
41626 + int i = 0;
41627 + DWC_PRINTF("--FRAME LIST (hex) --\n");
41628 + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
41629 + DWC_PRINTF("%x\t", hcd->frame_list[i]);
41630 + if (!(i % 8) && i)
41631 + DWC_PRINTF("\n");
41632 + }
41633 + DWC_PRINTF("\n----\n");
41634 +
41635 +}
41636 +#endif
41637 +
41638 +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41639 +{
41640 + dwc_irqflags_t flags;
41641 + dwc_spinlock_t *channel_lock = hcd->channel_lock;
41642 +
41643 + dwc_hc_t *hc = qh->channel;
41644 + if (dwc_qh_is_non_per(qh)) {
41645 + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
41646 + if (!microframe_schedule)
41647 + hcd->non_periodic_channels--;
41648 + else
41649 + hcd->available_host_channels++;
41650 + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
41651 + } else
41652 + update_frame_list(hcd, qh, 0);
41653 +
41654 + /*
41655 + * The condition is added to prevent double cleanup try in case of device
41656 + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
41657 + */
41658 + if (hc->qh) {
41659 + dwc_otg_hc_cleanup(hcd->core_if, hc);
41660 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
41661 + hc->qh = NULL;
41662 + }
41663 +
41664 + qh->channel = NULL;
41665 + qh->ntd = 0;
41666 +
41667 + if (qh->desc_list) {
41668 + dwc_memset(qh->desc_list, 0x00,
41669 + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
41670 + }
41671 +}
41672 +
41673 +/**
41674 + * Initializes a QH structure's Descriptor DMA related members.
41675 + * Allocates memory for descriptor list.
41676 + * On first periodic QH, allocates memory for FrameList
41677 + * and enables periodic scheduling.
41678 + *
41679 + * @param hcd The HCD state structure for the DWC OTG controller.
41680 + * @param qh The QH to init.
41681 + *
41682 + * @return 0 if successful, negative error code otherwise.
41683 + */
41684 +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41685 +{
41686 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41687 + int retval = 0;
41688 +
41689 + if (qh->do_split) {
41690 + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
41691 + return -1;
41692 + }
41693 +
41694 + retval = desc_list_alloc(dev, qh);
41695 +
41696 + if ((retval == 0)
41697 + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
41698 + if (!hcd->frame_list) {
41699 + retval = frame_list_alloc(hcd);
41700 + /* Enable periodic schedule on first periodic QH */
41701 + if (retval == 0)
41702 + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
41703 + }
41704 + }
41705 +
41706 + qh->ntd = 0;
41707 +
41708 + return retval;
41709 +}
41710 +
41711 +/**
41712 + * Frees descriptor list memory associated with the QH.
41713 + * If QH is periodic and the last, frees FrameList memory
41714 + * and disables periodic scheduling.
41715 + *
41716 + * @param hcd The HCD state structure for the DWC OTG controller.
41717 + * @param qh The QH to init.
41718 + */
41719 +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41720 +{
41721 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41722 +
41723 + desc_list_free(dev, qh);
41724 +
41725 + /*
41726 + * Channel still assigned due to some reasons.
41727 + * Seen on Isoc URB dequeue. Channel halted but no subsequent
41728 + * ChHalted interrupt to release the channel. Afterwards
41729 + * when it comes here from endpoint disable routine
41730 + * channel remains assigned.
41731 + */
41732 + if (qh->channel)
41733 + release_channel_ddma(hcd, qh);
41734 +
41735 + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
41736 + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
41737 +
41738 + per_sched_disable(hcd);
41739 + frame_list_free(hcd);
41740 + }
41741 +}
41742 +
41743 +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
41744 +{
41745 + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
41746 + /*
41747 + * Descriptor set(8 descriptors) index
41748 + * which is 8-aligned.
41749 + */
41750 + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
41751 + } else {
41752 + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
41753 + }
41754 +}
41755 +
41756 +/*
41757 + * Determine starting frame for Isochronous transfer.
41758 + * Few frames skipped to prevent race condition with HC.
41759 + */
41760 +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41761 + uint8_t * skip_frames)
41762 +{
41763 + uint16_t frame = 0;
41764 + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
41765 +
41766 + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
41767 +
41768 + /*
41769 + * skip_frames is used to limit activated descriptors number
41770 + * to avoid the situation when HC services the last activated
41771 + * descriptor firstly.
41772 + * Example for FS:
41773 + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
41774 + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
41775 + * will be fetched. If the number of descriptors is max=64 (or greather) the
41776 + * list will be fully programmed with Active descriptors and it is possible
41777 + * case(rare) that the latest descriptor(considering rollback) corresponding
41778 + * to frame 2 will be serviced first. HS case is more probable because, in fact,
41779 + * up to 11 uframes(16 in the code) may be skipped.
41780 + */
41781 + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
41782 + /*
41783 + * Consider uframe counter also, to start xfer asap.
41784 + * If half of the frame elapsed skip 2 frames otherwise
41785 + * just 1 frame.
41786 + * Starting descriptor index must be 8-aligned, so
41787 + * if the current frame is near to complete the next one
41788 + * is skipped as well.
41789 + */
41790 +
41791 + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
41792 + *skip_frames = 2 * 8;
41793 + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
41794 + } else {
41795 + *skip_frames = 1 * 8;
41796 + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
41797 + }
41798 +
41799 + frame = dwc_full_frame_num(frame);
41800 + } else {
41801 + /*
41802 + * Two frames are skipped for FS - the current and the next.
41803 + * But for descriptor programming, 1 frame(descriptor) is enough,
41804 + * see example above.
41805 + */
41806 + *skip_frames = 1;
41807 + frame = dwc_frame_num_inc(hcd->frame_number, 2);
41808 + }
41809 +
41810 + return frame;
41811 +}
41812 +
41813 +/*
41814 + * Calculate initial descriptor index for isochronous transfer
41815 + * based on scheduled frame.
41816 + */
41817 +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41818 +{
41819 + uint16_t frame = 0, fr_idx, fr_idx_tmp;
41820 + uint8_t skip_frames = 0;
41821 + /*
41822 + * With current ISOC processing algorithm the channel is being
41823 + * released when no more QTDs in the list(qh->ntd == 0).
41824 + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
41825 + *
41826 + * So qh->channel != NULL branch is not used and just not removed from the
41827 + * source file. It is required for another possible approach which is,
41828 + * do not disable and release the channel when ISOC session completed,
41829 + * just move QH to inactive schedule until new QTD arrives.
41830 + * On new QTD, the QH moved back to 'ready' schedule,
41831 + * starting frame and therefore starting desc_index are recalculated.
41832 + * In this case channel is released only on ep_disable.
41833 + */
41834 +
41835 + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
41836 + if (qh->channel) {
41837 + frame = calc_starting_frame(hcd, qh, &skip_frames);
41838 + /*
41839 + * Calculate initial descriptor index based on FrameList current bitmap
41840 + * and servicing period.
41841 + */
41842 + fr_idx_tmp = frame_list_idx(frame);
41843 + fr_idx =
41844 + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
41845 + fr_idx_tmp)
41846 + % frame_incr_val(qh);
41847 + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
41848 + } else {
41849 + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
41850 + fr_idx = frame_list_idx(qh->sched_frame);
41851 + }
41852 +
41853 + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
41854 +
41855 + return skip_frames;
41856 +}
41857 +
41858 +#define ISOC_URB_GIVEBACK_ASAP
41859 +
41860 +#define MAX_ISOC_XFER_SIZE_FS 1023
41861 +#define MAX_ISOC_XFER_SIZE_HS 3072
41862 +#define DESCNUM_THRESHOLD 4
41863 +
41864 +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41865 + uint8_t skip_frames)
41866 +{
41867 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
41868 + dwc_otg_qtd_t *qtd;
41869 + dwc_otg_host_dma_desc_t *dma_desc;
41870 + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
41871 +
41872 + idx = qh->td_last;
41873 + inc = qh->interval;
41874 + n_desc = 0;
41875 +
41876 + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
41877 + if (skip_frames && !qh->channel)
41878 + ntd_max = ntd_max - skip_frames / qh->interval;
41879 +
41880 + max_xfer_size =
41881 + (qh->dev_speed ==
41882 + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
41883 + MAX_ISOC_XFER_SIZE_FS;
41884 +
41885 + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
41886 + while ((qh->ntd < ntd_max)
41887 + && (qtd->isoc_frame_index_last <
41888 + qtd->urb->packet_count)) {
41889 +
41890 + dma_desc = &qh->desc_list[idx];
41891 + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
41892 +
41893 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
41894 +
41895 + if (frame_desc->length > max_xfer_size)
41896 + qh->n_bytes[idx] = max_xfer_size;
41897 + else
41898 + qh->n_bytes[idx] = frame_desc->length;
41899 + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
41900 + dma_desc->status.b_isoc.a = 1;
41901 + dma_desc->status.b_isoc.sts = 0;
41902 +
41903 + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
41904 +
41905 + qh->ntd++;
41906 +
41907 + qtd->isoc_frame_index_last++;
41908 +
41909 +#ifdef ISOC_URB_GIVEBACK_ASAP
41910 + /*
41911 + * Set IOC for each descriptor corresponding to the
41912 + * last frame of the URB.
41913 + */
41914 + if (qtd->isoc_frame_index_last ==
41915 + qtd->urb->packet_count)
41916 + dma_desc->status.b_isoc.ioc = 1;
41917 +
41918 +#endif
41919 + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
41920 + n_desc++;
41921 +
41922 + }
41923 + qtd->in_process = 1;
41924 + }
41925 +
41926 + qh->td_last = idx;
41927 +
41928 +#ifdef ISOC_URB_GIVEBACK_ASAP
41929 + /* Set IOC for the last descriptor if descriptor list is full */
41930 + if (qh->ntd == ntd_max) {
41931 + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
41932 + qh->desc_list[idx].status.b_isoc.ioc = 1;
41933 + }
41934 +#else
41935 + /*
41936 + * Set IOC bit only for one descriptor.
41937 + * Always try to be ahead of HW processing,
41938 + * i.e. on IOC generation driver activates next descriptors but
41939 + * core continues to process descriptors followed the one with IOC set.
41940 + */
41941 +
41942 + if (n_desc > DESCNUM_THRESHOLD) {
41943 + /*
41944 + * Move IOC "up". Required even if there is only one QTD
41945 + * in the list, cause QTDs migth continue to be queued,
41946 + * but during the activation it was only one queued.
41947 + * Actually more than one QTD might be in the list if this function called
41948 + * from XferCompletion - QTDs was queued during HW processing of the previous
41949 + * descriptor chunk.
41950 + */
41951 + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
41952 + } else {
41953 + /*
41954 + * Set the IOC for the latest descriptor
41955 + * if either number of descriptor is not greather than threshold
41956 + * or no more new descriptors activated.
41957 + */
41958 + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
41959 + }
41960 +
41961 + qh->desc_list[idx].status.b_isoc.ioc = 1;
41962 +#endif
41963 +}
41964 +
41965 +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41966 +{
41967 +
41968 + dwc_hc_t *hc;
41969 + dwc_otg_host_dma_desc_t *dma_desc;
41970 + dwc_otg_qtd_t *qtd;
41971 + int num_packets, len, n_desc = 0;
41972 +
41973 + hc = qh->channel;
41974 +
41975 + /*
41976 + * Start with hc->xfer_buff initialized in
41977 + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
41978 + * this pointer re-assigned to the buffer of the currently processed QTD.
41979 + * For non-SG request there is always one QTD active.
41980 + */
41981 +
41982 + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
41983 +
41984 + if (n_desc) {
41985 + /* SG request - more than 1 QTDs */
41986 + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
41987 + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
41988 + }
41989 +
41990 + qtd->n_desc = 0;
41991 +
41992 + do {
41993 + dma_desc = &qh->desc_list[n_desc];
41994 + len = hc->xfer_len;
41995 +
41996 + if (len > MAX_DMA_DESC_SIZE)
41997 + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
41998 +
41999 + if (hc->ep_is_in) {
42000 + if (len > 0) {
42001 + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
42002 + } else {
42003 + /* Need 1 packet for transfer length of 0. */
42004 + num_packets = 1;
42005 + }
42006 + /* Always program an integral # of max packets for IN transfers. */
42007 + len = num_packets * hc->max_packet;
42008 + }
42009 +
42010 + dma_desc->status.b.n_bytes = len;
42011 +
42012 + qh->n_bytes[n_desc] = len;
42013 +
42014 + if ((qh->ep_type == UE_CONTROL)
42015 + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
42016 + dma_desc->status.b.sup = 1; /* Setup Packet */
42017 +
42018 + dma_desc->status.b.a = 1; /* Active descriptor */
42019 + dma_desc->status.b.sts = 0;
42020 +
42021 + dma_desc->buf =
42022 + ((unsigned long)hc->xfer_buff & 0xffffffff);
42023 +
42024 + /*
42025 + * Last descriptor(or single) of IN transfer
42026 + * with actual size less than MaxPacket.
42027 + */
42028 + if (len > hc->xfer_len) {
42029 + hc->xfer_len = 0;
42030 + } else {
42031 + hc->xfer_buff += len;
42032 + hc->xfer_len -= len;
42033 + }
42034 +
42035 + qtd->n_desc++;
42036 + n_desc++;
42037 + }
42038 + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
42039 +
42040 +
42041 + qtd->in_process = 1;
42042 +
42043 + if (qh->ep_type == UE_CONTROL)
42044 + break;
42045 +
42046 + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
42047 + break;
42048 + }
42049 +
42050 + if (n_desc) {
42051 + /* Request Transfer Complete interrupt for the last descriptor */
42052 + qh->desc_list[n_desc - 1].status.b.ioc = 1;
42053 + /* End of List indicator */
42054 + qh->desc_list[n_desc - 1].status.b.eol = 1;
42055 +
42056 + hc->ntd = n_desc;
42057 + }
42058 +}
42059 +
42060 +/**
42061 + * For Control and Bulk endpoints initializes descriptor list
42062 + * and starts the transfer.
42063 + *
42064 + * For Interrupt and Isochronous endpoints initializes descriptor list
42065 + * then updates FrameList, marking appropriate entries as active.
42066 + * In case of Isochronous, the starting descriptor index is calculated based
42067 + * on the scheduled frame, but only on the first transfer descriptor within a session.
42068 + * Then starts the transfer via enabling the channel.
42069 + * For Isochronous endpoint the channel is not halted on XferComplete
42070 + * interrupt so remains assigned to the endpoint(QH) until session is done.
42071 + *
42072 + * @param hcd The HCD state structure for the DWC OTG controller.
42073 + * @param qh The QH to init.
42074 + *
42075 + * @return 0 if successful, negative error code otherwise.
42076 + */
42077 +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42078 +{
42079 + /* Channel is already assigned */
42080 + dwc_hc_t *hc = qh->channel;
42081 + uint8_t skip_frames = 0;
42082 +
42083 + switch (hc->ep_type) {
42084 + case DWC_OTG_EP_TYPE_CONTROL:
42085 + case DWC_OTG_EP_TYPE_BULK:
42086 + init_non_isoc_dma_desc(hcd, qh);
42087 +
42088 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42089 + break;
42090 + case DWC_OTG_EP_TYPE_INTR:
42091 + init_non_isoc_dma_desc(hcd, qh);
42092 +
42093 + update_frame_list(hcd, qh, 1);
42094 +
42095 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42096 + break;
42097 + case DWC_OTG_EP_TYPE_ISOC:
42098 +
42099 + if (!qh->ntd)
42100 + skip_frames = recalc_initial_desc_idx(hcd, qh);
42101 +
42102 + init_isoc_dma_desc(hcd, qh, skip_frames);
42103 +
42104 + if (!hc->xfer_started) {
42105 +
42106 + update_frame_list(hcd, qh, 1);
42107 +
42108 + /*
42109 + * Always set to max, instead of actual size.
42110 + * Otherwise ntd will be changed with
42111 + * channel being enabled. Not recommended.
42112 + *
42113 + */
42114 + hc->ntd = max_desc_num(qh);
42115 + /* Enable channel only once for ISOC */
42116 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42117 + }
42118 +
42119 + break;
42120 + default:
42121 +
42122 + break;
42123 + }
42124 +}
42125 +
42126 +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
42127 + dwc_hc_t * hc,
42128 + dwc_otg_hc_regs_t * hc_regs,
42129 + dwc_otg_halt_status_e halt_status)
42130 +{
42131 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
42132 + dwc_otg_qtd_t *qtd, *qtd_tmp;
42133 + dwc_otg_qh_t *qh;
42134 + dwc_otg_host_dma_desc_t *dma_desc;
42135 + uint16_t idx, remain;
42136 + uint8_t urb_compl;
42137 +
42138 + qh = hc->qh;
42139 + idx = qh->td_first;
42140 +
42141 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42142 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
42143 + qtd->in_process = 0;
42144 + return;
42145 + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
42146 + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
42147 + /*
42148 + * Channel is halted in these error cases.
42149 + * Considered as serious issues.
42150 + * Complete all URBs marking all frames as failed,
42151 + * irrespective whether some of the descriptors(frames) succeeded or no.
42152 + * Pass error code to completion routine as well, to
42153 + * update urb->status, some of class drivers might use it to stop
42154 + * queing transfer requests.
42155 + */
42156 + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
42157 + ? (-DWC_E_IO)
42158 + : (-DWC_E_OVERFLOW);
42159 +
42160 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42161 + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
42162 + frame_desc = &qtd->urb->iso_descs[idx];
42163 + frame_desc->status = err;
42164 + }
42165 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
42166 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42167 + }
42168 + return;
42169 + }
42170 +
42171 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42172 +
42173 + if (!qtd->in_process)
42174 + break;
42175 +
42176 + urb_compl = 0;
42177 +
42178 + do {
42179 +
42180 + dma_desc = &qh->desc_list[idx];
42181 +
42182 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
42183 + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
42184 +
42185 + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
42186 + /*
42187 + * XactError or, unable to complete all the transactions
42188 + * in the scheduled micro-frame/frame,
42189 + * both indicated by DMA_DESC_STS_PKTERR.
42190 + */
42191 + qtd->urb->error_count++;
42192 + frame_desc->actual_length = qh->n_bytes[idx] - remain;
42193 + frame_desc->status = -DWC_E_PROTOCOL;
42194 + } else {
42195 + /* Success */
42196 +
42197 + frame_desc->actual_length = qh->n_bytes[idx] - remain;
42198 + frame_desc->status = 0;
42199 + }
42200 +
42201 + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
42202 + /*
42203 + * urb->status is not used for isoc transfers here.
42204 + * The individual frame_desc status are used instead.
42205 + */
42206 +
42207 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
42208 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42209 +
42210 + /*
42211 + * This check is necessary because urb_dequeue can be called
42212 + * from urb complete callback(sound driver example).
42213 + * All pending URBs are dequeued there, so no need for
42214 + * further processing.
42215 + */
42216 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42217 + return;
42218 + }
42219 +
42220 + urb_compl = 1;
42221 +
42222 + }
42223 +
42224 + qh->ntd--;
42225 +
42226 + /* Stop if IOC requested descriptor reached */
42227 + if (dma_desc->status.b_isoc.ioc) {
42228 + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
42229 + goto stop_scan;
42230 + }
42231 +
42232 + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
42233 +
42234 + if (urb_compl)
42235 + break;
42236 + }
42237 + while (idx != qh->td_first);
42238 + }
42239 +stop_scan:
42240 + qh->td_first = idx;
42241 +}
42242 +
42243 +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
42244 + dwc_hc_t * hc,
42245 + dwc_otg_qtd_t * qtd,
42246 + dwc_otg_host_dma_desc_t * dma_desc,
42247 + dwc_otg_halt_status_e halt_status,
42248 + uint32_t n_bytes, uint8_t * xfer_done)
42249 +{
42250 +
42251 + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
42252 + dwc_otg_hcd_urb_t *urb = qtd->urb;
42253 +
42254 + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
42255 + urb->status = -DWC_E_IO;
42256 + return 1;
42257 + }
42258 + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
42259 + switch (halt_status) {
42260 + case DWC_OTG_HC_XFER_STALL:
42261 + urb->status = -DWC_E_PIPE;
42262 + break;
42263 + case DWC_OTG_HC_XFER_BABBLE_ERR:
42264 + urb->status = -DWC_E_OVERFLOW;
42265 + break;
42266 + case DWC_OTG_HC_XFER_XACT_ERR:
42267 + urb->status = -DWC_E_PROTOCOL;
42268 + break;
42269 + default:
42270 + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
42271 + halt_status);
42272 + break;
42273 + }
42274 + return 1;
42275 + }
42276 +
42277 + if (dma_desc->status.b.a == 1) {
42278 + DWC_DEBUGPL(DBG_HCDV,
42279 + "Active descriptor encountered on channel %d\n",
42280 + hc->hc_num);
42281 + return 0;
42282 + }
42283 +
42284 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
42285 + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
42286 + urb->actual_length += n_bytes - remain;
42287 + if (remain || urb->actual_length == urb->length) {
42288 + /*
42289 + * For Control Data stage do not set urb->status=0 to prevent
42290 + * URB callback. Set it when Status phase done. See below.
42291 + */
42292 + *xfer_done = 1;
42293 + }
42294 +
42295 + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
42296 + urb->status = 0;
42297 + *xfer_done = 1;
42298 + }
42299 + /* No handling for SETUP stage */
42300 + } else {
42301 + /* BULK and INTR */
42302 + urb->actual_length += n_bytes - remain;
42303 + if (remain || urb->actual_length == urb->length) {
42304 + urb->status = 0;
42305 + *xfer_done = 1;
42306 + }
42307 + }
42308 +
42309 + return 0;
42310 +}
42311 +
42312 +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
42313 + dwc_hc_t * hc,
42314 + dwc_otg_hc_regs_t * hc_regs,
42315 + dwc_otg_halt_status_e halt_status)
42316 +{
42317 + dwc_otg_hcd_urb_t *urb = NULL;
42318 + dwc_otg_qtd_t *qtd, *qtd_tmp;
42319 + dwc_otg_qh_t *qh;
42320 + dwc_otg_host_dma_desc_t *dma_desc;
42321 + uint32_t n_bytes, n_desc, i;
42322 + uint8_t failed = 0, xfer_done;
42323 +
42324 + n_desc = 0;
42325 +
42326 + qh = hc->qh;
42327 +
42328 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42329 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42330 + qtd->in_process = 0;
42331 + }
42332 + return;
42333 + }
42334 +
42335 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
42336 +
42337 + urb = qtd->urb;
42338 +
42339 + n_bytes = 0;
42340 + xfer_done = 0;
42341 +
42342 + for (i = 0; i < qtd->n_desc; i++) {
42343 + dma_desc = &qh->desc_list[n_desc];
42344 +
42345 + n_bytes = qh->n_bytes[n_desc];
42346 +
42347 + failed =
42348 + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
42349 + dma_desc,
42350 + halt_status, n_bytes,
42351 + &xfer_done);
42352 +
42353 + if (failed
42354 + || (xfer_done
42355 + && (urb->status != -DWC_E_IN_PROGRESS))) {
42356 +
42357 + hcd->fops->complete(hcd, urb->priv, urb,
42358 + urb->status);
42359 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42360 +
42361 + if (failed)
42362 + goto stop_scan;
42363 + } else if (qh->ep_type == UE_CONTROL) {
42364 + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
42365 + if (urb->length > 0) {
42366 + qtd->control_phase = DWC_OTG_CONTROL_DATA;
42367 + } else {
42368 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
42369 + }
42370 + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
42371 + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
42372 + if (xfer_done) {
42373 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
42374 + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
42375 + } else if (i + 1 == qtd->n_desc) {
42376 + /*
42377 + * Last descriptor for Control data stage which is
42378 + * not completed yet.
42379 + */
42380 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
42381 + }
42382 + }
42383 + }
42384 +
42385 + n_desc++;
42386 + }
42387 +
42388 + }
42389 +
42390 +stop_scan:
42391 +
42392 + if (qh->ep_type != UE_CONTROL) {
42393 + /*
42394 + * Resetting the data toggle for bulk
42395 + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
42396 + */
42397 + if (halt_status == DWC_OTG_HC_XFER_STALL)
42398 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
42399 + else
42400 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
42401 + }
42402 +
42403 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
42404 + hcint_data_t hcint;
42405 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
42406 + if (hcint.b.nyet) {
42407 + /*
42408 + * Got a NYET on the last transaction of the transfer. It
42409 + * means that the endpoint should be in the PING state at the
42410 + * beginning of the next transfer.
42411 + */
42412 + qh->ping_state = 1;
42413 + clear_hc_int(hc_regs, nyet);
42414 + }
42415 +
42416 + }
42417 +
42418 +}
42419 +
42420 +/**
42421 + * This function is called from interrupt handlers.
42422 + * Scans the descriptor list, updates URB's status and
42423 + * calls completion routine for the URB if it's done.
42424 + * Releases the channel to be used by other transfers.
42425 + * In case of Isochronous endpoint the channel is not halted until
42426 + * the end of the session, i.e. QTD list is empty.
42427 + * If periodic channel released the FrameList is updated accordingly.
42428 + *
42429 + * Calls transaction selection routines to activate pending transfers.
42430 + *
42431 + * @param hcd The HCD state structure for the DWC OTG controller.
42432 + * @param hc Host channel, the transfer is completed on.
42433 + * @param hc_regs Host channel registers.
42434 + * @param halt_status Reason the channel is being halted,
42435 + * or just XferComplete for isochronous transfer
42436 + */
42437 +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
42438 + dwc_hc_t * hc,
42439 + dwc_otg_hc_regs_t * hc_regs,
42440 + dwc_otg_halt_status_e halt_status)
42441 +{
42442 + uint8_t continue_isoc_xfer = 0;
42443 + dwc_otg_transaction_type_e tr_type;
42444 + dwc_otg_qh_t *qh = hc->qh;
42445 +
42446 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
42447 +
42448 + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
42449 +
42450 + /* Release the channel if halted or session completed */
42451 + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
42452 + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
42453 +
42454 + /* Halt the channel if session completed */
42455 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
42456 + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
42457 + }
42458 +
42459 + release_channel_ddma(hcd, qh);
42460 + dwc_otg_hcd_qh_remove(hcd, qh);
42461 + } else {
42462 + /* Keep in assigned schedule to continue transfer */
42463 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
42464 + &qh->qh_list_entry);
42465 + continue_isoc_xfer = 1;
42466 +
42467 + }
42468 + /** @todo Consider the case when period exceeds FrameList size.
42469 + * Frame Rollover interrupt should be used.
42470 + */
42471 + } else {
42472 + /* Scan descriptor list to complete the URB(s), then release the channel */
42473 + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
42474 +
42475 + release_channel_ddma(hcd, qh);
42476 + dwc_otg_hcd_qh_remove(hcd, qh);
42477 +
42478 + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
42479 + /* Add back to inactive non-periodic schedule on normal completion */
42480 + dwc_otg_hcd_qh_add(hcd, qh);
42481 + }
42482 +
42483 + }
42484 + tr_type = dwc_otg_hcd_select_transactions(hcd);
42485 + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
42486 + if (continue_isoc_xfer) {
42487 + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
42488 + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
42489 + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
42490 + tr_type = DWC_OTG_TRANSACTION_ALL;
42491 + }
42492 + }
42493 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
42494 + }
42495 +}
42496 +
42497 +#endif /* DWC_DEVICE_ONLY */
42498 --- /dev/null
42499 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
42500 @@ -0,0 +1,417 @@
42501 +/* ==========================================================================
42502 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
42503 + * $Revision: #12 $
42504 + * $Date: 2011/10/26 $
42505 + * $Change: 1873028 $
42506 + *
42507 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
42508 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
42509 + * otherwise expressly agreed to in writing between Synopsys and you.
42510 + *
42511 + * The Software IS NOT an item of Licensed Software or Licensed Product under
42512 + * any End User Software License Agreement or Agreement for Licensed Product
42513 + * with Synopsys or any supplement thereto. You are permitted to use and
42514 + * redistribute this Software in source and binary forms, with or without
42515 + * modification, provided that redistributions of source code must retain this
42516 + * notice. You may not view, use, disclose, copy or distribute this file or
42517 + * any information contained herein except pursuant to this license grant from
42518 + * Synopsys. If you do not agree with this notice, including the disclaimer
42519 + * below, then you are not authorized to use the Software.
42520 + *
42521 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
42522 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42523 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42524 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
42525 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42526 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42527 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42528 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42529 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42530 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42531 + * DAMAGE.
42532 + * ========================================================================== */
42533 +#ifndef DWC_DEVICE_ONLY
42534 +#ifndef __DWC_HCD_IF_H__
42535 +#define __DWC_HCD_IF_H__
42536 +
42537 +#include "dwc_otg_core_if.h"
42538 +
42539 +/** @file
42540 + * This file defines DWC_OTG HCD Core API.
42541 + */
42542 +
42543 +struct dwc_otg_hcd;
42544 +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
42545 +
42546 +struct dwc_otg_hcd_urb;
42547 +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
42548 +
42549 +/** @name HCD Function Driver Callbacks */
42550 +/** @{ */
42551 +
42552 +/** This function is called whenever core switches to host mode. */
42553 +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
42554 +
42555 +/** This function is called when device has been disconnected */
42556 +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
42557 +
42558 +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
42559 +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
42560 + void *urb_handle,
42561 + uint32_t * hub_addr,
42562 + uint32_t * port_addr);
42563 +/** Via this function HCD core gets device speed */
42564 +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
42565 + void *urb_handle);
42566 +
42567 +/** This function is called when urb is completed */
42568 +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
42569 + void *urb_handle,
42570 + dwc_otg_hcd_urb_t * dwc_otg_urb,
42571 + int32_t status);
42572 +
42573 +/** Via this function HCD core gets b_hnp_enable parameter */
42574 +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
42575 +
42576 +struct dwc_otg_hcd_function_ops {
42577 + dwc_otg_hcd_start_cb_t start;
42578 + dwc_otg_hcd_disconnect_cb_t disconnect;
42579 + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
42580 + dwc_otg_hcd_speed_from_urb_cb_t speed;
42581 + dwc_otg_hcd_complete_urb_cb_t complete;
42582 + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
42583 +};
42584 +/** @} */
42585 +
42586 +/** @name HCD Core API */
42587 +/** @{ */
42588 +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
42589 +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
42590 +
42591 +/** This function should be called to initiate HCD Core.
42592 + *
42593 + * @param hcd The HCD
42594 + * @param core_if The DWC_OTG Core
42595 + *
42596 + * Returns -DWC_E_NO_MEMORY if no enough memory.
42597 + * Returns 0 on success
42598 + */
42599 +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
42600 +
42601 +/** Frees HCD
42602 + *
42603 + * @param hcd The HCD
42604 + */
42605 +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
42606 +
42607 +/** This function should be called on every hardware interrupt.
42608 + *
42609 + * @param dwc_otg_hcd The HCD
42610 + *
42611 + * Returns non zero if interrupt is handled
42612 + * Return 0 if interrupt is not handled
42613 + */
42614 +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
42615 +
42616 +/** This function is used to handle the fast interrupt
42617 + *
42618 + */
42619 +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
42620 +
42621 +/**
42622 + * Returns private data set by
42623 + * dwc_otg_hcd_set_priv_data function.
42624 + *
42625 + * @param hcd The HCD
42626 + */
42627 +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
42628 +
42629 +/**
42630 + * Set private data.
42631 + *
42632 + * @param hcd The HCD
42633 + * @param priv_data pointer to be stored in private data
42634 + */
42635 +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
42636 +
42637 +/**
42638 + * This function initializes the HCD Core.
42639 + *
42640 + * @param hcd The HCD
42641 + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
42642 + *
42643 + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
42644 + * Returns 0 on success
42645 + */
42646 +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
42647 + struct dwc_otg_hcd_function_ops *fops);
42648 +
42649 +/**
42650 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
42651 + * stopped.
42652 + *
42653 + * @param hcd The HCD
42654 + */
42655 +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
42656 +
42657 +/**
42658 + * Handles hub class-specific requests.
42659 + *
42660 + * @param dwc_otg_hcd The HCD
42661 + * @param typeReq Request Type
42662 + * @param wValue wValue from control request
42663 + * @param wIndex wIndex from control request
42664 + * @param buf data buffer
42665 + * @param wLength data buffer length
42666 + *
42667 + * Returns -DWC_E_INVALID if invalid argument is passed
42668 + * Returns 0 on success
42669 + */
42670 +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
42671 + uint16_t typeReq, uint16_t wValue,
42672 + uint16_t wIndex, uint8_t * buf,
42673 + uint16_t wLength);
42674 +
42675 +/**
42676 + * Returns otg port number.
42677 + *
42678 + * @param hcd The HCD
42679 + */
42680 +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
42681 +
42682 +/**
42683 + * Returns OTG version - either 1.3 or 2.0.
42684 + *
42685 + * @param core_if The core_if structure pointer
42686 + */
42687 +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
42688 +
42689 +/**
42690 + * Returns 1 if currently core is acting as B host, and 0 otherwise.
42691 + *
42692 + * @param hcd The HCD
42693 + */
42694 +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
42695 +
42696 +/**
42697 + * Returns current frame number.
42698 + *
42699 + * @param hcd The HCD
42700 + */
42701 +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
42702 +
42703 +/**
42704 + * Dumps hcd state.
42705 + *
42706 + * @param hcd The HCD
42707 + */
42708 +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
42709 +
42710 +/**
42711 + * Dump the average frame remaining at SOF. This can be used to
42712 + * determine average interrupt latency. Frame remaining is also shown for
42713 + * start transfer and two additional sample points.
42714 + * Currently this function is not implemented.
42715 + *
42716 + * @param hcd The HCD
42717 + */
42718 +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
42719 +
42720 +/**
42721 + * Sends LPM transaction to the local device.
42722 + *
42723 + * @param hcd The HCD
42724 + * @param devaddr Device Address
42725 + * @param hird Host initiated resume duration
42726 + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
42727 + *
42728 + * Returns negative value if sending LPM transaction was not succeeded.
42729 + * Returns 0 on success.
42730 + */
42731 +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
42732 + uint8_t hird, uint8_t bRemoteWake);
42733 +
42734 +/* URB interface */
42735 +
42736 +/**
42737 + * Allocates memory for dwc_otg_hcd_urb structure.
42738 + * Allocated memory should be freed by call of DWC_FREE.
42739 + *
42740 + * @param hcd The HCD
42741 + * @param iso_desc_count Count of ISOC descriptors
42742 + * @param atomic_alloc Specefies whether to perform atomic allocation.
42743 + */
42744 +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
42745 + int iso_desc_count,
42746 + int atomic_alloc);
42747 +
42748 +/**
42749 + * Set pipe information in URB.
42750 + *
42751 + * @param hcd_urb DWC_OTG URB
42752 + * @param devaddr Device Address
42753 + * @param ep_num Endpoint Number
42754 + * @param ep_type Endpoint Type
42755 + * @param ep_dir Endpoint Direction
42756 + * @param mps Max Packet Size
42757 + */
42758 +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
42759 + uint8_t devaddr, uint8_t ep_num,
42760 + uint8_t ep_type, uint8_t ep_dir,
42761 + uint16_t mps);
42762 +
42763 +/* Transfer flags */
42764 +#define URB_GIVEBACK_ASAP 0x1
42765 +#define URB_SEND_ZERO_PACKET 0x2
42766 +
42767 +/**
42768 + * Sets dwc_otg_hcd_urb parameters.
42769 + *
42770 + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
42771 + * @param urb_handle Unique handle for request, this will be passed back
42772 + * to function driver in completion callback.
42773 + * @param buf The buffer for the data
42774 + * @param dma The DMA buffer for the data
42775 + * @param buflen Transfer length
42776 + * @param sp Buffer for setup data
42777 + * @param sp_dma DMA address of setup data buffer
42778 + * @param flags Transfer flags
42779 + * @param interval Polling interval for interrupt or isochronous transfers.
42780 + */
42781 +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
42782 + void *urb_handle, void *buf,
42783 + dwc_dma_t dma, uint32_t buflen, void *sp,
42784 + dwc_dma_t sp_dma, uint32_t flags,
42785 + uint16_t interval);
42786 +
42787 +/** Gets status from dwc_otg_hcd_urb
42788 + *
42789 + * @param dwc_otg_urb DWC_OTG URB
42790 + */
42791 +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
42792 +
42793 +/** Gets actual length from dwc_otg_hcd_urb
42794 + *
42795 + * @param dwc_otg_urb DWC_OTG URB
42796 + */
42797 +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
42798 + dwc_otg_urb);
42799 +
42800 +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
42801 + *
42802 + * @param dwc_otg_urb DWC_OTG URB
42803 + */
42804 +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
42805 + dwc_otg_urb);
42806 +
42807 +/** Set ISOC descriptor offset and length
42808 + *
42809 + * @param dwc_otg_urb DWC_OTG URB
42810 + * @param desc_num ISOC descriptor number
42811 + * @param offset Offset from beginig of buffer.
42812 + * @param length Transaction length
42813 + */
42814 +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
42815 + int desc_num, uint32_t offset,
42816 + uint32_t length);
42817 +
42818 +/** Get status of ISOC descriptor, specified by desc_num
42819 + *
42820 + * @param dwc_otg_urb DWC_OTG URB
42821 + * @param desc_num ISOC descriptor number
42822 + */
42823 +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
42824 + dwc_otg_urb, int desc_num);
42825 +
42826 +/** Get actual length of ISOC descriptor, specified by desc_num
42827 + *
42828 + * @param dwc_otg_urb DWC_OTG URB
42829 + * @param desc_num ISOC descriptor number
42830 + */
42831 +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
42832 + dwc_otg_urb,
42833 + int desc_num);
42834 +
42835 +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
42836 + *
42837 + * @param dwc_otg_hcd The HCD
42838 + * @param dwc_otg_urb DWC_OTG URB
42839 + * @param ep_handle Out parameter for returning endpoint handle
42840 + * @param atomic_alloc Flag to do atomic allocation if needed
42841 + *
42842 + * Returns -DWC_E_NO_DEVICE if no device is connected.
42843 + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
42844 + * Returns 0 on success.
42845 + */
42846 +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
42847 + dwc_otg_hcd_urb_t * dwc_otg_urb,
42848 + void **ep_handle, int atomic_alloc);
42849 +
42850 +/** De-queue the specified URB
42851 + *
42852 + * @param dwc_otg_hcd The HCD
42853 + * @param dwc_otg_urb DWC_OTG URB
42854 + */
42855 +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
42856 + dwc_otg_hcd_urb_t * dwc_otg_urb);
42857 +
42858 +/** Frees resources in the DWC_otg controller related to a given endpoint.
42859 + * Any URBs for the endpoint must already be dequeued.
42860 + *
42861 + * @param hcd The HCD
42862 + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
42863 + * @param retry Number of retries if there are queued transfers.
42864 + *
42865 + * Returns -DWC_E_INVALID if invalid arguments are passed.
42866 + * Returns 0 on success
42867 + */
42868 +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
42869 + int retry);
42870 +
42871 +/* Resets the data toggle in qh structure. This function can be called from
42872 + * usb_clear_halt routine.
42873 + *
42874 + * @param hcd The HCD
42875 + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
42876 + *
42877 + * Returns -DWC_E_INVALID if invalid arguments are passed.
42878 + * Returns 0 on success
42879 + */
42880 +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
42881 +
42882 +/** Returns 1 if status of specified port is changed and 0 otherwise.
42883 + *
42884 + * @param hcd The HCD
42885 + * @param port Port number
42886 + */
42887 +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
42888 +
42889 +/** Call this function to check if bandwidth was allocated for specified endpoint.
42890 + * Only for ISOC and INTERRUPT endpoints.
42891 + *
42892 + * @param hcd The HCD
42893 + * @param ep_handle Endpoint handle
42894 + */
42895 +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
42896 + void *ep_handle);
42897 +
42898 +/** Call this function to check if bandwidth was freed for specified endpoint.
42899 + *
42900 + * @param hcd The HCD
42901 + * @param ep_handle Endpoint handle
42902 + */
42903 +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
42904 +
42905 +/** Returns bandwidth allocated for specified endpoint in microseconds.
42906 + * Only for ISOC and INTERRUPT endpoints.
42907 + *
42908 + * @param hcd The HCD
42909 + * @param ep_handle Endpoint handle
42910 + */
42911 +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
42912 + void *ep_handle);
42913 +
42914 +/** @} */
42915 +
42916 +#endif /* __DWC_HCD_IF_H__ */
42917 +#endif /* DWC_DEVICE_ONLY */
42918 --- /dev/null
42919 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
42920 @@ -0,0 +1,2727 @@
42921 +/* ==========================================================================
42922 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
42923 + * $Revision: #89 $
42924 + * $Date: 2011/10/20 $
42925 + * $Change: 1869487 $
42926 + *
42927 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
42928 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
42929 + * otherwise expressly agreed to in writing between Synopsys and you.
42930 + *
42931 + * The Software IS NOT an item of Licensed Software or Licensed Product under
42932 + * any End User Software License Agreement or Agreement for Licensed Product
42933 + * with Synopsys or any supplement thereto. You are permitted to use and
42934 + * redistribute this Software in source and binary forms, with or without
42935 + * modification, provided that redistributions of source code must retain this
42936 + * notice. You may not view, use, disclose, copy or distribute this file or
42937 + * any information contained herein except pursuant to this license grant from
42938 + * Synopsys. If you do not agree with this notice, including the disclaimer
42939 + * below, then you are not authorized to use the Software.
42940 + *
42941 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
42942 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42943 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42944 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
42945 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42946 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42947 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42948 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42949 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42950 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42951 + * DAMAGE.
42952 + * ========================================================================== */
42953 +#ifndef DWC_DEVICE_ONLY
42954 +
42955 +#include "dwc_otg_hcd.h"
42956 +#include "dwc_otg_regs.h"
42957 +
42958 +#include <linux/jiffies.h>
42959 +#include <asm/fiq.h>
42960 +
42961 +
42962 +extern bool microframe_schedule;
42963 +
42964 +/** @file
42965 + * This file contains the implementation of the HCD Interrupt handlers.
42966 + */
42967 +
42968 +int fiq_done, int_done;
42969 +
42970 +#ifdef FIQ_DEBUG
42971 +char buffer[1000*16];
42972 +int wptr;
42973 +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
42974 +{
42975 + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
42976 + va_list args;
42977 + char text[17];
42978 + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
42979 +
42980 + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
42981 + {
42982 + local_fiq_disable();
42983 + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
42984 + va_start(args, fmt);
42985 + vsnprintf(text+8, 9, fmt, args);
42986 + va_end(args);
42987 +
42988 + memcpy(buffer + wptr, text, 16);
42989 + wptr = (wptr + 16) % sizeof(buffer);
42990 + local_fiq_enable();
42991 + }
42992 +}
42993 +#endif
42994 +
42995 +/** This function handles interrupts for the HCD. */
42996 +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
42997 +{
42998 + int retval = 0;
42999 + static int last_time;
43000 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
43001 + gintsts_data_t gintsts;
43002 + gintmsk_data_t gintmsk;
43003 + hfnum_data_t hfnum;
43004 + haintmsk_data_t haintmsk;
43005 +
43006 +#ifdef DEBUG
43007 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
43008 +
43009 +#endif
43010 +
43011 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
43012 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
43013 +
43014 + /* Exit from ISR if core is hibernated */
43015 + if (core_if->hibernation_suspend == 1) {
43016 + goto exit_handler_routine;
43017 + }
43018 + DWC_SPINLOCK(dwc_otg_hcd->lock);
43019 + /* Check if HOST Mode */
43020 + if (dwc_otg_is_host_mode(core_if)) {
43021 + if (fiq_enable) {
43022 + local_fiq_disable();
43023 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43024 + /* Pull in from the FIQ's disabled mask */
43025 + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
43026 + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
43027 + }
43028 +
43029 + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
43030 + gintsts.b.hcintr = 1;
43031 + }
43032 +
43033 + /* Danger will robinson: fake a SOF if necessary */
43034 + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
43035 + gintsts.b.sofintr = 1;
43036 + }
43037 + gintsts.d32 &= gintmsk.d32;
43038 +
43039 + if (fiq_enable) {
43040 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43041 + local_fiq_enable();
43042 + }
43043 +
43044 + if (!gintsts.d32) {
43045 + goto exit_handler_routine;
43046 + }
43047 +
43048 +#ifdef DEBUG
43049 + // We should be OK doing this because the common interrupts should already have been serviced
43050 + /* Don't print debug message in the interrupt handler on SOF */
43051 +#ifndef DEBUG_SOF
43052 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43053 +#endif
43054 + DWC_DEBUGPL(DBG_HCDI, "\n");
43055 +#endif
43056 +
43057 +#ifdef DEBUG
43058 +#ifndef DEBUG_SOF
43059 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43060 +#endif
43061 + DWC_DEBUGPL(DBG_HCDI,
43062 + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
43063 + gintsts.d32, core_if);
43064 +#endif
43065 + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
43066 + if (gintsts.b.sofintr) {
43067 + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
43068 + }
43069 +
43070 + if (gintsts.b.rxstsqlvl) {
43071 + retval |=
43072 + dwc_otg_hcd_handle_rx_status_q_level_intr
43073 + (dwc_otg_hcd);
43074 + }
43075 + if (gintsts.b.nptxfempty) {
43076 + retval |=
43077 + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
43078 + (dwc_otg_hcd);
43079 + }
43080 + if (gintsts.b.i2cintr) {
43081 + /** @todo Implement i2cintr handler. */
43082 + }
43083 + if (gintsts.b.portintr) {
43084 +
43085 + gintmsk_data_t gintmsk = { .b.portintr = 1};
43086 + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
43087 + if (fiq_enable) {
43088 + local_fiq_disable();
43089 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43090 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
43091 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43092 + local_fiq_enable();
43093 + } else {
43094 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
43095 + }
43096 + }
43097 + if (gintsts.b.hcintr) {
43098 + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
43099 + }
43100 + if (gintsts.b.ptxfempty) {
43101 + retval |=
43102 + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
43103 + (dwc_otg_hcd);
43104 + }
43105 +#ifdef DEBUG
43106 +#ifndef DEBUG_SOF
43107 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43108 +#endif
43109 + {
43110 + DWC_DEBUGPL(DBG_HCDI,
43111 + "DWC OTG HCD Finished Servicing Interrupts\n");
43112 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
43113 + DWC_READ_REG32(&global_regs->gintsts));
43114 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
43115 + DWC_READ_REG32(&global_regs->gintmsk));
43116 + }
43117 +#endif
43118 +
43119 +#ifdef DEBUG
43120 +#ifndef DEBUG_SOF
43121 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43122 +#endif
43123 + DWC_DEBUGPL(DBG_HCDI, "\n");
43124 +#endif
43125 +
43126 + }
43127 +
43128 +exit_handler_routine:
43129 + if (fiq_enable) {
43130 + gintmsk_data_t gintmsk_new;
43131 + haintmsk_data_t haintmsk_new;
43132 + local_fiq_disable();
43133 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43134 + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
43135 + if(fiq_fsm_enable)
43136 + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
43137 + else
43138 + haintmsk_new.d32 = 0x0000FFFF;
43139 +
43140 + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
43141 + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
43142 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
43143 + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
43144 + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
43145 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
43146 + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
43147 + ;
43148 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
43149 + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
43150 + }
43151 + int_done++;
43152 + }
43153 + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
43154 + /* Re-enable interrupts that the FIQ masked (first time round) */
43155 + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
43156 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43157 + local_fiq_enable();
43158 +
43159 + if ((jiffies / HZ) > last_time) {
43160 + //dwc_otg_qh_t *qh;
43161 + //dwc_list_link_t *cur;
43162 + /* Once a second output the fiq and irq numbers, useful for debug */
43163 + last_time = jiffies / HZ;
43164 + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
43165 + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
43166 + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
43167 + //printk(KERN_WARNING "Periodic queues:\n");
43168 + }
43169 + }
43170 +
43171 + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
43172 + return retval;
43173 +}
43174 +
43175 +#ifdef DWC_TRACK_MISSED_SOFS
43176 +
43177 +#warning Compiling code to track missed SOFs
43178 +#define FRAME_NUM_ARRAY_SIZE 1000
43179 +/**
43180 + * This function is for debug only.
43181 + */
43182 +static inline void track_missed_sofs(uint16_t curr_frame_number)
43183 +{
43184 + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
43185 + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
43186 + static int frame_num_idx = 0;
43187 + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
43188 + static int dumped_frame_num_array = 0;
43189 +
43190 + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
43191 + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
43192 + curr_frame_number) {
43193 + frame_num_array[frame_num_idx] = curr_frame_number;
43194 + last_frame_num_array[frame_num_idx++] = last_frame_num;
43195 + }
43196 + } else if (!dumped_frame_num_array) {
43197 + int i;
43198 + DWC_PRINTF("Frame Last Frame\n");
43199 + DWC_PRINTF("----- ----------\n");
43200 + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
43201 + DWC_PRINTF("0x%04x 0x%04x\n",
43202 + frame_num_array[i], last_frame_num_array[i]);
43203 + }
43204 + dumped_frame_num_array = 1;
43205 + }
43206 + last_frame_num = curr_frame_number;
43207 +}
43208 +#endif
43209 +
43210 +/**
43211 + * Handles the start-of-frame interrupt in host mode. Non-periodic
43212 + * transactions may be queued to the DWC_otg controller for the current
43213 + * (micro)frame. Periodic transactions may be queued to the controller for the
43214 + * next (micro)frame.
43215 + */
43216 +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
43217 +{
43218 + hfnum_data_t hfnum;
43219 + gintsts_data_t gintsts = { .d32 = 0 };
43220 + dwc_list_link_t *qh_entry;
43221 + dwc_otg_qh_t *qh;
43222 + dwc_otg_transaction_type_e tr_type;
43223 + int did_something = 0;
43224 + int32_t next_sched_frame = -1;
43225 +
43226 + hfnum.d32 =
43227 + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
43228 +
43229 +#ifdef DEBUG_SOF
43230 + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
43231 +#endif
43232 + hcd->frame_number = hfnum.b.frnum;
43233 +
43234 +#ifdef DEBUG
43235 + hcd->frrem_accum += hfnum.b.frrem;
43236 + hcd->frrem_samples++;
43237 +#endif
43238 +
43239 +#ifdef DWC_TRACK_MISSED_SOFS
43240 + track_missed_sofs(hcd->frame_number);
43241 +#endif
43242 + /* Determine whether any periodic QHs should be executed. */
43243 + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
43244 + while (qh_entry != &hcd->periodic_sched_inactive) {
43245 + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
43246 + qh_entry = qh_entry->next;
43247 + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
43248 +
43249 + /*
43250 + * Move QH to the ready list to be executed next
43251 + * (micro)frame.
43252 + */
43253 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
43254 + &qh->qh_list_entry);
43255 +
43256 + did_something = 1;
43257 + }
43258 + else
43259 + {
43260 + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
43261 + {
43262 + next_sched_frame = qh->sched_frame;
43263 + }
43264 + }
43265 + }
43266 + if (fiq_enable)
43267 + hcd->fiq_state->next_sched_frame = next_sched_frame;
43268 +
43269 + tr_type = dwc_otg_hcd_select_transactions(hcd);
43270 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
43271 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
43272 + did_something = 1;
43273 + }
43274 +
43275 + /* Clear interrupt - but do not trample on the FIQ sof */
43276 + if (!fiq_fsm_enable) {
43277 + gintsts.b.sofintr = 1;
43278 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
43279 + }
43280 + return 1;
43281 +}
43282 +
43283 +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
43284 + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
43285 + * memory if the DWC_otg controller is operating in Slave mode. */
43286 +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43287 +{
43288 + host_grxsts_data_t grxsts;
43289 + dwc_hc_t *hc = NULL;
43290 +
43291 + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
43292 +
43293 + grxsts.d32 =
43294 + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
43295 +
43296 + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
43297 + if (!hc) {
43298 + DWC_ERROR("Unable to get corresponding channel\n");
43299 + return 0;
43300 + }
43301 +
43302 + /* Packet Status */
43303 + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
43304 + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
43305 + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
43306 + hc->data_pid_start);
43307 + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
43308 +
43309 + switch (grxsts.b.pktsts) {
43310 + case DWC_GRXSTS_PKTSTS_IN:
43311 + /* Read the data into the host buffer. */
43312 + if (grxsts.b.bcnt > 0) {
43313 + dwc_otg_read_packet(dwc_otg_hcd->core_if,
43314 + hc->xfer_buff, grxsts.b.bcnt);
43315 +
43316 + /* Update the HC fields for the next packet received. */
43317 + hc->xfer_count += grxsts.b.bcnt;
43318 + hc->xfer_buff += grxsts.b.bcnt;
43319 + }
43320 +
43321 + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
43322 + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
43323 + case DWC_GRXSTS_PKTSTS_CH_HALTED:
43324 + /* Handled in interrupt, just ignore data */
43325 + break;
43326 + default:
43327 + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
43328 + grxsts.b.pktsts);
43329 + break;
43330 + }
43331 +
43332 + return 1;
43333 +}
43334 +
43335 +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
43336 + * data packets may be written to the FIFO for OUT transfers. More requests
43337 + * may be written to the non-periodic request queue for IN transfers. This
43338 + * interrupt is enabled only in Slave mode. */
43339 +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43340 +{
43341 + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
43342 + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
43343 + DWC_OTG_TRANSACTION_NON_PERIODIC);
43344 + return 1;
43345 +}
43346 +
43347 +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
43348 + * packets may be written to the FIFO for OUT transfers. More requests may be
43349 + * written to the periodic request queue for IN transfers. This interrupt is
43350 + * enabled only in Slave mode. */
43351 +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43352 +{
43353 + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
43354 + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
43355 + DWC_OTG_TRANSACTION_PERIODIC);
43356 + return 1;
43357 +}
43358 +
43359 +/** There are multiple conditions that can cause a port interrupt. This function
43360 + * determines which interrupt conditions have occurred and handles them
43361 + * appropriately. */
43362 +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43363 +{
43364 + int retval = 0;
43365 + hprt0_data_t hprt0;
43366 + hprt0_data_t hprt0_modify;
43367 +
43368 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43369 + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43370 +
43371 + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
43372 + * GINTSTS */
43373 +
43374 + hprt0_modify.b.prtena = 0;
43375 + hprt0_modify.b.prtconndet = 0;
43376 + hprt0_modify.b.prtenchng = 0;
43377 + hprt0_modify.b.prtovrcurrchng = 0;
43378 +
43379 + /* Port Connect Detected
43380 + * Set flag and clear if detected */
43381 + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
43382 + // Dont modify port status if we are in hibernation state
43383 + hprt0_modify.b.prtconndet = 1;
43384 + hprt0_modify.b.prtenchng = 1;
43385 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
43386 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43387 + return retval;
43388 + }
43389 +
43390 + if (hprt0.b.prtconndet) {
43391 + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
43392 + if (dwc_otg_hcd->core_if->adp_enable &&
43393 + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
43394 + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
43395 + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
43396 + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
43397 + /* TODO - check if this is required, as
43398 + * host initialization was already performed
43399 + * after initial ADP probing
43400 + */
43401 + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
43402 + dwc_otg_core_init(dwc_otg_hcd->core_if);
43403 + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
43404 + cil_hcd_start(dwc_otg_hcd->core_if);*/
43405 + } else {
43406 +
43407 + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
43408 + "Port Connect Detected--\n", hprt0.d32);
43409 + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
43410 + dwc_otg_hcd->flags.b.port_connect_status = 1;
43411 + hprt0_modify.b.prtconndet = 1;
43412 +
43413 + /* B-Device has connected, Delete the connection timer. */
43414 + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
43415 + }
43416 + /* The Hub driver asserts a reset when it sees port connect
43417 + * status change flag */
43418 + retval |= 1;
43419 + }
43420 +
43421 + /* Port Enable Changed
43422 + * Clear if detected - Set internal flag if disabled */
43423 + if (hprt0.b.prtenchng) {
43424 + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
43425 + "Port Enable Changed--\n", hprt0.d32);
43426 + hprt0_modify.b.prtenchng = 1;
43427 + if (hprt0.b.prtena == 1) {
43428 + hfir_data_t hfir;
43429 + int do_reset = 0;
43430 + dwc_otg_core_params_t *params =
43431 + dwc_otg_hcd->core_if->core_params;
43432 + dwc_otg_core_global_regs_t *global_regs =
43433 + dwc_otg_hcd->core_if->core_global_regs;
43434 + dwc_otg_host_if_t *host_if =
43435 + dwc_otg_hcd->core_if->host_if;
43436 +
43437 + /* Every time when port enables calculate
43438 + * HFIR.FrInterval
43439 + */
43440 + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
43441 + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
43442 + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
43443 +
43444 + /* Check if we need to adjust the PHY clock speed for
43445 + * low power and adjust it */
43446 + if (params->host_support_fs_ls_low_power) {
43447 + gusbcfg_data_t usbcfg;
43448 +
43449 + usbcfg.d32 =
43450 + DWC_READ_REG32(&global_regs->gusbcfg);
43451 +
43452 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
43453 + || hprt0.b.prtspd ==
43454 + DWC_HPRT0_PRTSPD_FULL_SPEED) {
43455 + /*
43456 + * Low power
43457 + */
43458 + hcfg_data_t hcfg;
43459 + if (usbcfg.b.phylpwrclksel == 0) {
43460 + /* Set PHY low power clock select for FS/LS devices */
43461 + usbcfg.b.phylpwrclksel = 1;
43462 + DWC_WRITE_REG32
43463 + (&global_regs->gusbcfg,
43464 + usbcfg.d32);
43465 + do_reset = 1;
43466 + }
43467 +
43468 + hcfg.d32 =
43469 + DWC_READ_REG32
43470 + (&host_if->host_global_regs->hcfg);
43471 +
43472 + if (hprt0.b.prtspd ==
43473 + DWC_HPRT0_PRTSPD_LOW_SPEED
43474 + && params->host_ls_low_power_phy_clk
43475 + ==
43476 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
43477 + {
43478 + /* 6 MHZ */
43479 + DWC_DEBUGPL(DBG_CIL,
43480 + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
43481 + if (hcfg.b.fslspclksel !=
43482 + DWC_HCFG_6_MHZ) {
43483 + hcfg.b.fslspclksel =
43484 + DWC_HCFG_6_MHZ;
43485 + DWC_WRITE_REG32
43486 + (&host_if->host_global_regs->hcfg,
43487 + hcfg.d32);
43488 + do_reset = 1;
43489 + }
43490 + } else {
43491 + /* 48 MHZ */
43492 + DWC_DEBUGPL(DBG_CIL,
43493 + "FS_PHY programming HCFG to 48 MHz ()\n");
43494 + if (hcfg.b.fslspclksel !=
43495 + DWC_HCFG_48_MHZ) {
43496 + hcfg.b.fslspclksel =
43497 + DWC_HCFG_48_MHZ;
43498 + DWC_WRITE_REG32
43499 + (&host_if->host_global_regs->hcfg,
43500 + hcfg.d32);
43501 + do_reset = 1;
43502 + }
43503 + }
43504 + } else {
43505 + /*
43506 + * Not low power
43507 + */
43508 + if (usbcfg.b.phylpwrclksel == 1) {
43509 + usbcfg.b.phylpwrclksel = 0;
43510 + DWC_WRITE_REG32
43511 + (&global_regs->gusbcfg,
43512 + usbcfg.d32);
43513 + do_reset = 1;
43514 + }
43515 + }
43516 +
43517 + if (do_reset) {
43518 + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
43519 + }
43520 + }
43521 +
43522 + if (!do_reset) {
43523 + /* Port has been enabled set the reset change flag */
43524 + dwc_otg_hcd->flags.b.port_reset_change = 1;
43525 + }
43526 + } else {
43527 + dwc_otg_hcd->flags.b.port_enable_change = 1;
43528 + }
43529 + retval |= 1;
43530 + }
43531 +
43532 + /** Overcurrent Change Interrupt */
43533 + if (hprt0.b.prtovrcurrchng) {
43534 + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
43535 + "Port Overcurrent Changed--\n", hprt0.d32);
43536 + dwc_otg_hcd->flags.b.port_over_current_change = 1;
43537 + hprt0_modify.b.prtovrcurrchng = 1;
43538 + retval |= 1;
43539 + }
43540 +
43541 + /* Clear Port Interrupts */
43542 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
43543 +
43544 + return retval;
43545 +}
43546 +
43547 +/** This interrupt indicates that one or more host channels has a pending
43548 + * interrupt. There are multiple conditions that can cause each host channel
43549 + * interrupt. This function determines which conditions have occurred for each
43550 + * host channel interrupt and handles them appropriately. */
43551 +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43552 +{
43553 + int i;
43554 + int retval = 0;
43555 + haint_data_t haint = { .d32 = 0 } ;
43556 +
43557 + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
43558 + * GINTSTS */
43559 +
43560 + if (!fiq_fsm_enable)
43561 + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
43562 +
43563 + // Overwrite with saved interrupts from fiq handler
43564 + if(fiq_fsm_enable)
43565 + {
43566 + /* check the mask? */
43567 + local_fiq_disable();
43568 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43569 + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
43570 + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
43571 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43572 + local_fiq_enable();
43573 + }
43574 +
43575 + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
43576 + if (haint.b2.chint & (1 << i)) {
43577 + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
43578 + }
43579 + }
43580 +
43581 + return retval;
43582 +}
43583 +
43584 +/**
43585 + * Gets the actual length of a transfer after the transfer halts. _halt_status
43586 + * holds the reason for the halt.
43587 + *
43588 + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
43589 + * *short_read is set to 1 upon return if less than the requested
43590 + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
43591 + * return. short_read may also be NULL on entry, in which case it remains
43592 + * unchanged.
43593 + */
43594 +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
43595 + dwc_otg_hc_regs_t * hc_regs,
43596 + dwc_otg_qtd_t * qtd,
43597 + dwc_otg_halt_status_e halt_status,
43598 + int *short_read)
43599 +{
43600 + hctsiz_data_t hctsiz;
43601 + uint32_t length;
43602 +
43603 + if (short_read != NULL) {
43604 + *short_read = 0;
43605 + }
43606 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43607 +
43608 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
43609 + if (hc->ep_is_in) {
43610 + length = hc->xfer_len - hctsiz.b.xfersize;
43611 + if (short_read != NULL) {
43612 + *short_read = (hctsiz.b.xfersize != 0);
43613 + }
43614 + } else if (hc->qh->do_split) {
43615 + //length = split_out_xfersize[hc->hc_num];
43616 + length = qtd->ssplit_out_xfer_count;
43617 + } else {
43618 + length = hc->xfer_len;
43619 + }
43620 + } else {
43621 + /*
43622 + * Must use the hctsiz.pktcnt field to determine how much data
43623 + * has been transferred. This field reflects the number of
43624 + * packets that have been transferred via the USB. This is
43625 + * always an integral number of packets if the transfer was
43626 + * halted before its normal completion. (Can't use the
43627 + * hctsiz.xfersize field because that reflects the number of
43628 + * bytes transferred via the AHB, not the USB).
43629 + */
43630 + length =
43631 + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
43632 + }
43633 +
43634 + return length;
43635 +}
43636 +
43637 +/**
43638 + * Updates the state of the URB after a Transfer Complete interrupt on the
43639 + * host channel. Updates the actual_length field of the URB based on the
43640 + * number of bytes transferred via the host channel. Sets the URB status
43641 + * if the data transfer is finished.
43642 + *
43643 + * @return 1 if the data transfer specified by the URB is completely finished,
43644 + * 0 otherwise.
43645 + */
43646 +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
43647 + dwc_otg_hc_regs_t * hc_regs,
43648 + dwc_otg_hcd_urb_t * urb,
43649 + dwc_otg_qtd_t * qtd)
43650 +{
43651 + int xfer_done = 0;
43652 + int short_read = 0;
43653 +
43654 + int xfer_length;
43655 +
43656 + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
43657 + DWC_OTG_HC_XFER_COMPLETE,
43658 + &short_read);
43659 +
43660 + if (urb->actual_length + xfer_length > urb->length) {
43661 + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
43662 + hc->dev_addr, __func__, __LINE__);
43663 + xfer_length = urb->length - urb->actual_length;
43664 + }
43665 +
43666 + /* non DWORD-aligned buffer case handling. */
43667 + if (hc->align_buff && xfer_length && hc->ep_is_in) {
43668 + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
43669 + xfer_length);
43670 + }
43671 +
43672 + urb->actual_length += xfer_length;
43673 +
43674 + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
43675 + (urb->flags & URB_SEND_ZERO_PACKET)
43676 + && (urb->actual_length == urb->length)
43677 + && !(urb->length % hc->max_packet)) {
43678 + xfer_done = 0;
43679 + } else if (short_read || urb->actual_length >= urb->length) {
43680 + xfer_done = 1;
43681 + urb->status = 0;
43682 + }
43683 +
43684 +#ifdef DEBUG
43685 + {
43686 + hctsiz_data_t hctsiz;
43687 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43688 + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
43689 + __func__, (hc->ep_is_in ? "IN" : "OUT"),
43690 + hc->hc_num);
43691 + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
43692 + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
43693 + hctsiz.b.xfersize);
43694 + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
43695 + urb->length);
43696 + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
43697 + urb->actual_length);
43698 + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
43699 + short_read, xfer_done);
43700 + }
43701 +#endif
43702 +
43703 + return xfer_done;
43704 +}
43705 +
43706 +/*
43707 + * Save the starting data toggle for the next transfer. The data toggle is
43708 + * saved in the QH for non-control transfers and it's saved in the QTD for
43709 + * control transfers.
43710 + */
43711 +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
43712 + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
43713 +{
43714 + hctsiz_data_t hctsiz;
43715 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43716 +
43717 + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
43718 + dwc_otg_qh_t *qh = hc->qh;
43719 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
43720 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
43721 + } else {
43722 + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
43723 + }
43724 + } else {
43725 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
43726 + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
43727 + } else {
43728 + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
43729 + }
43730 + }
43731 +}
43732 +
43733 +/**
43734 + * Updates the state of an Isochronous URB when the transfer is stopped for
43735 + * any reason. The fields of the current entry in the frame descriptor array
43736 + * are set based on the transfer state and the input _halt_status. Completes
43737 + * the Isochronous URB if all the URB frames have been completed.
43738 + *
43739 + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
43740 + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
43741 + */
43742 +static dwc_otg_halt_status_e
43743 +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
43744 + dwc_hc_t * hc,
43745 + dwc_otg_hc_regs_t * hc_regs,
43746 + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
43747 +{
43748 + dwc_otg_hcd_urb_t *urb = qtd->urb;
43749 + dwc_otg_halt_status_e ret_val = halt_status;
43750 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
43751 +
43752 + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
43753 + switch (halt_status) {
43754 + case DWC_OTG_HC_XFER_COMPLETE:
43755 + frame_desc->status = 0;
43756 + frame_desc->actual_length =
43757 + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
43758 +
43759 + /* non DWORD-aligned buffer case handling. */
43760 + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
43761 + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
43762 + hc->qh->dw_align_buf, frame_desc->actual_length);
43763 + }
43764 +
43765 + break;
43766 + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
43767 + urb->error_count++;
43768 + if (hc->ep_is_in) {
43769 + frame_desc->status = -DWC_E_NO_STREAM_RES;
43770 + } else {
43771 + frame_desc->status = -DWC_E_COMMUNICATION;
43772 + }
43773 + frame_desc->actual_length = 0;
43774 + break;
43775 + case DWC_OTG_HC_XFER_BABBLE_ERR:
43776 + urb->error_count++;
43777 + frame_desc->status = -DWC_E_OVERFLOW;
43778 + /* Don't need to update actual_length in this case. */
43779 + break;
43780 + case DWC_OTG_HC_XFER_XACT_ERR:
43781 + urb->error_count++;
43782 + frame_desc->status = -DWC_E_PROTOCOL;
43783 + frame_desc->actual_length =
43784 + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
43785 +
43786 + /* non DWORD-aligned buffer case handling. */
43787 + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
43788 + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
43789 + hc->qh->dw_align_buf, frame_desc->actual_length);
43790 + }
43791 + /* Skip whole frame */
43792 + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
43793 + hc->ep_is_in && hcd->core_if->dma_enable) {
43794 + qtd->complete_split = 0;
43795 + qtd->isoc_split_offset = 0;
43796 + }
43797 +
43798 + break;
43799 + default:
43800 + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
43801 + break;
43802 + }
43803 + if (++qtd->isoc_frame_index == urb->packet_count) {
43804 + /*
43805 + * urb->status is not used for isoc transfers.
43806 + * The individual frame_desc statuses are used instead.
43807 + */
43808 + hcd->fops->complete(hcd, urb->priv, urb, 0);
43809 + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
43810 + } else {
43811 + ret_val = DWC_OTG_HC_XFER_COMPLETE;
43812 + }
43813 + return ret_val;
43814 +}
43815 +
43816 +/**
43817 + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
43818 + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
43819 + * still linked to the QH, the QH is added to the end of the inactive
43820 + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
43821 + * schedule if no more QTDs are linked to the QH.
43822 + */
43823 +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
43824 +{
43825 + int continue_split = 0;
43826 + dwc_otg_qtd_t *qtd;
43827 +
43828 + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
43829 +
43830 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
43831 +
43832 + if (qtd->complete_split) {
43833 + continue_split = 1;
43834 + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
43835 + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
43836 + continue_split = 1;
43837 + }
43838 +
43839 + if (free_qtd) {
43840 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
43841 + continue_split = 0;
43842 + }
43843 +
43844 + qh->channel = NULL;
43845 + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
43846 +}
43847 +
43848 +/**
43849 + * Releases a host channel for use by other transfers. Attempts to select and
43850 + * queue more transactions since at least one host channel is available.
43851 + *
43852 + * @param hcd The HCD state structure.
43853 + * @param hc The host channel to release.
43854 + * @param qtd The QTD associated with the host channel. This QTD may be freed
43855 + * if the transfer is complete or an error has occurred.
43856 + * @param halt_status Reason the channel is being released. This status
43857 + * determines the actions taken by this function.
43858 + */
43859 +static void release_channel(dwc_otg_hcd_t * hcd,
43860 + dwc_hc_t * hc,
43861 + dwc_otg_qtd_t * qtd,
43862 + dwc_otg_halt_status_e halt_status)
43863 +{
43864 + dwc_otg_transaction_type_e tr_type;
43865 + int free_qtd;
43866 + dwc_irqflags_t flags;
43867 + dwc_spinlock_t *channel_lock = hcd->channel_lock;
43868 +
43869 + int hog_port = 0;
43870 +
43871 + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
43872 + __func__, hc->hc_num, halt_status, hc->xfer_len);
43873 +
43874 + if(fiq_fsm_enable && hc->do_split) {
43875 + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
43876 + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
43877 + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
43878 + hog_port = 0;
43879 + }
43880 + }
43881 + }
43882 +
43883 + switch (halt_status) {
43884 + case DWC_OTG_HC_XFER_URB_COMPLETE:
43885 + free_qtd = 1;
43886 + break;
43887 + case DWC_OTG_HC_XFER_AHB_ERR:
43888 + case DWC_OTG_HC_XFER_STALL:
43889 + case DWC_OTG_HC_XFER_BABBLE_ERR:
43890 + free_qtd = 1;
43891 + break;
43892 + case DWC_OTG_HC_XFER_XACT_ERR:
43893 + if (qtd->error_count >= 3) {
43894 + DWC_DEBUGPL(DBG_HCDV,
43895 + " Complete URB with transaction error\n");
43896 + free_qtd = 1;
43897 + qtd->urb->status = -DWC_E_PROTOCOL;
43898 + hcd->fops->complete(hcd, qtd->urb->priv,
43899 + qtd->urb, -DWC_E_PROTOCOL);
43900 + } else {
43901 + free_qtd = 0;
43902 + }
43903 + break;
43904 + case DWC_OTG_HC_XFER_URB_DEQUEUE:
43905 + /*
43906 + * The QTD has already been removed and the QH has been
43907 + * deactivated. Don't want to do anything except release the
43908 + * host channel and try to queue more transfers.
43909 + */
43910 + goto cleanup;
43911 + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
43912 + free_qtd = 0;
43913 + break;
43914 + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
43915 + DWC_DEBUGPL(DBG_HCDV,
43916 + " Complete URB with I/O error\n");
43917 + free_qtd = 1;
43918 + qtd->urb->status = -DWC_E_IO;
43919 + hcd->fops->complete(hcd, qtd->urb->priv,
43920 + qtd->urb, -DWC_E_IO);
43921 + break;
43922 + default:
43923 + free_qtd = 0;
43924 + break;
43925 + }
43926 +
43927 + deactivate_qh(hcd, hc->qh, free_qtd);
43928 +
43929 +cleanup:
43930 + /*
43931 + * Release the host channel for use by other transfers. The cleanup
43932 + * function clears the channel interrupt enables and conditions, so
43933 + * there's no need to clear the Channel Halted interrupt separately.
43934 + */
43935 + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
43936 + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
43937 + dwc_otg_hc_cleanup(hcd->core_if, hc);
43938 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
43939 +
43940 + if (!microframe_schedule) {
43941 + switch (hc->ep_type) {
43942 + case DWC_OTG_EP_TYPE_CONTROL:
43943 + case DWC_OTG_EP_TYPE_BULK:
43944 + hcd->non_periodic_channels--;
43945 + break;
43946 +
43947 + default:
43948 + /*
43949 + * Don't release reservations for periodic channels here.
43950 + * That's done when a periodic transfer is descheduled (i.e.
43951 + * when the QH is removed from the periodic schedule).
43952 + */
43953 + break;
43954 + }
43955 + } else {
43956 +
43957 + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
43958 + hcd->available_host_channels++;
43959 + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
43960 + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
43961 + }
43962 +
43963 + /* Try to queue more transfers now that there's a free channel. */
43964 + tr_type = dwc_otg_hcd_select_transactions(hcd);
43965 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
43966 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
43967 + }
43968 +}
43969 +
43970 +/**
43971 + * Halts a host channel. If the channel cannot be halted immediately because
43972 + * the request queue is full, this function ensures that the FIFO empty
43973 + * interrupt for the appropriate queue is enabled so that the halt request can
43974 + * be queued when there is space in the request queue.
43975 + *
43976 + * This function may also be called in DMA mode. In that case, the channel is
43977 + * simply released since the core always halts the channel automatically in
43978 + * DMA mode.
43979 + */
43980 +static void halt_channel(dwc_otg_hcd_t * hcd,
43981 + dwc_hc_t * hc,
43982 + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
43983 +{
43984 + if (hcd->core_if->dma_enable) {
43985 + release_channel(hcd, hc, qtd, halt_status);
43986 + return;
43987 + }
43988 +
43989 + /* Slave mode processing... */
43990 + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
43991 +
43992 + if (hc->halt_on_queue) {
43993 + gintmsk_data_t gintmsk = {.d32 = 0 };
43994 + dwc_otg_core_global_regs_t *global_regs;
43995 + global_regs = hcd->core_if->core_global_regs;
43996 +
43997 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
43998 + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
43999 + /*
44000 + * Make sure the Non-periodic Tx FIFO empty interrupt
44001 + * is enabled so that the non-periodic schedule will
44002 + * be processed.
44003 + */
44004 + gintmsk.b.nptxfempty = 1;
44005 + if (fiq_enable) {
44006 + local_fiq_disable();
44007 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
44008 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44009 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
44010 + local_fiq_enable();
44011 + } else {
44012 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44013 + }
44014 + } else {
44015 + /*
44016 + * Move the QH from the periodic queued schedule to
44017 + * the periodic assigned schedule. This allows the
44018 + * halt to be queued when the periodic schedule is
44019 + * processed.
44020 + */
44021 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
44022 + &hc->qh->qh_list_entry);
44023 +
44024 + /*
44025 + * Make sure the Periodic Tx FIFO Empty interrupt is
44026 + * enabled so that the periodic schedule will be
44027 + * processed.
44028 + */
44029 + gintmsk.b.ptxfempty = 1;
44030 + if (fiq_enable) {
44031 + local_fiq_disable();
44032 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
44033 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44034 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
44035 + local_fiq_enable();
44036 + } else {
44037 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44038 + }
44039 + }
44040 + }
44041 +}
44042 +
44043 +/**
44044 + * Performs common cleanup for non-periodic transfers after a Transfer
44045 + * Complete interrupt. This function should be called after any endpoint type
44046 + * specific handling is finished to release the host channel.
44047 + */
44048 +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
44049 + dwc_hc_t * hc,
44050 + dwc_otg_hc_regs_t * hc_regs,
44051 + dwc_otg_qtd_t * qtd,
44052 + dwc_otg_halt_status_e halt_status)
44053 +{
44054 + hcint_data_t hcint;
44055 +
44056 + qtd->error_count = 0;
44057 +
44058 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
44059 + if (hcint.b.nyet) {
44060 + /*
44061 + * Got a NYET on the last transaction of the transfer. This
44062 + * means that the endpoint should be in the PING state at the
44063 + * beginning of the next transfer.
44064 + */
44065 + hc->qh->ping_state = 1;
44066 + clear_hc_int(hc_regs, nyet);
44067 + }
44068 +
44069 + /*
44070 + * Always halt and release the host channel to make it available for
44071 + * more transfers. There may still be more phases for a control
44072 + * transfer or more data packets for a bulk transfer at this point,
44073 + * but the host channel is still halted. A channel will be reassigned
44074 + * to the transfer when the non-periodic schedule is processed after
44075 + * the channel is released. This allows transactions to be queued
44076 + * properly via dwc_otg_hcd_queue_transactions, which also enables the
44077 + * Tx FIFO Empty interrupt if necessary.
44078 + */
44079 + if (hc->ep_is_in) {
44080 + /*
44081 + * IN transfers in Slave mode require an explicit disable to
44082 + * halt the channel. (In DMA mode, this call simply releases
44083 + * the channel.)
44084 + */
44085 + halt_channel(hcd, hc, qtd, halt_status);
44086 + } else {
44087 + /*
44088 + * The channel is automatically disabled by the core for OUT
44089 + * transfers in Slave mode.
44090 + */
44091 + release_channel(hcd, hc, qtd, halt_status);
44092 + }
44093 +}
44094 +
44095 +/**
44096 + * Performs common cleanup for periodic transfers after a Transfer Complete
44097 + * interrupt. This function should be called after any endpoint type specific
44098 + * handling is finished to release the host channel.
44099 + */
44100 +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
44101 + dwc_hc_t * hc,
44102 + dwc_otg_hc_regs_t * hc_regs,
44103 + dwc_otg_qtd_t * qtd,
44104 + dwc_otg_halt_status_e halt_status)
44105 +{
44106 + hctsiz_data_t hctsiz;
44107 + qtd->error_count = 0;
44108 +
44109 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44110 + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
44111 + /* Core halts channel in these cases. */
44112 + release_channel(hcd, hc, qtd, halt_status);
44113 + } else {
44114 + /* Flush any outstanding requests from the Tx queue. */
44115 + halt_channel(hcd, hc, qtd, halt_status);
44116 + }
44117 +}
44118 +
44119 +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
44120 + dwc_hc_t * hc,
44121 + dwc_otg_hc_regs_t * hc_regs,
44122 + dwc_otg_qtd_t * qtd)
44123 +{
44124 + uint32_t len;
44125 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
44126 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
44127 +
44128 + len = get_actual_xfer_length(hc, hc_regs, qtd,
44129 + DWC_OTG_HC_XFER_COMPLETE, NULL);
44130 +
44131 + if (!len) {
44132 + qtd->complete_split = 0;
44133 + qtd->isoc_split_offset = 0;
44134 + return 0;
44135 + }
44136 + frame_desc->actual_length += len;
44137 +
44138 + if (hc->align_buff && len)
44139 + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
44140 + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
44141 + qtd->isoc_split_offset += len;
44142 +
44143 + if (frame_desc->length == frame_desc->actual_length) {
44144 + frame_desc->status = 0;
44145 + qtd->isoc_frame_index++;
44146 + qtd->complete_split = 0;
44147 + qtd->isoc_split_offset = 0;
44148 + }
44149 +
44150 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
44151 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
44152 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
44153 + } else {
44154 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
44155 + }
44156 +
44157 + return 1; /* Indicates that channel released */
44158 +}
44159 +
44160 +/**
44161 + * Handles a host channel Transfer Complete interrupt. This handler may be
44162 + * called in either DMA mode or Slave mode.
44163 + */
44164 +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
44165 + dwc_hc_t * hc,
44166 + dwc_otg_hc_regs_t * hc_regs,
44167 + dwc_otg_qtd_t * qtd)
44168 +{
44169 + int urb_xfer_done;
44170 + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
44171 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44172 + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
44173 +
44174 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44175 + "Transfer Complete--\n", hc->hc_num);
44176 +
44177 + if (hcd->core_if->dma_desc_enable) {
44178 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
44179 + if (pipe_type == UE_ISOCHRONOUS) {
44180 + /* Do not disable the interrupt, just clear it */
44181 + clear_hc_int(hc_regs, xfercomp);
44182 + return 1;
44183 + }
44184 + goto handle_xfercomp_done;
44185 + }
44186 +
44187 + /*
44188 + * Handle xfer complete on CSPLIT.
44189 + */
44190 +
44191 + if (hc->qh->do_split) {
44192 + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
44193 + && hcd->core_if->dma_enable) {
44194 + if (qtd->complete_split
44195 + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
44196 + qtd))
44197 + goto handle_xfercomp_done;
44198 + } else {
44199 + qtd->complete_split = 0;
44200 + }
44201 + }
44202 +
44203 + /* Update the QTD and URB states. */
44204 + switch (pipe_type) {
44205 + case UE_CONTROL:
44206 + switch (qtd->control_phase) {
44207 + case DWC_OTG_CONTROL_SETUP:
44208 + if (urb->length > 0) {
44209 + qtd->control_phase = DWC_OTG_CONTROL_DATA;
44210 + } else {
44211 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
44212 + }
44213 + DWC_DEBUGPL(DBG_HCDV,
44214 + " Control setup transaction done\n");
44215 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44216 + break;
44217 + case DWC_OTG_CONTROL_DATA:{
44218 + urb_xfer_done =
44219 + update_urb_state_xfer_comp(hc, hc_regs, urb,
44220 + qtd);
44221 + if (urb_xfer_done) {
44222 + qtd->control_phase =
44223 + DWC_OTG_CONTROL_STATUS;
44224 + DWC_DEBUGPL(DBG_HCDV,
44225 + " Control data transfer done\n");
44226 + } else {
44227 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44228 + }
44229 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44230 + break;
44231 + }
44232 + case DWC_OTG_CONTROL_STATUS:
44233 + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
44234 + if (urb->status == -DWC_E_IN_PROGRESS) {
44235 + urb->status = 0;
44236 + }
44237 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44238 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44239 + break;
44240 + }
44241 +
44242 + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44243 + break;
44244 + case UE_BULK:
44245 + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
44246 + urb_xfer_done =
44247 + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
44248 + if (urb_xfer_done) {
44249 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44250 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44251 + } else {
44252 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44253 + }
44254 +
44255 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44256 + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44257 + break;
44258 + case UE_INTERRUPT:
44259 + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
44260 + urb_xfer_done =
44261 + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
44262 +
44263 + /*
44264 + * Interrupt URB is done on the first transfer complete
44265 + * interrupt.
44266 + */
44267 + if (urb_xfer_done) {
44268 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44269 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44270 + } else {
44271 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44272 + }
44273 +
44274 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44275 + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44276 + break;
44277 + case UE_ISOCHRONOUS:
44278 + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
44279 + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
44280 + halt_status =
44281 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44282 + DWC_OTG_HC_XFER_COMPLETE);
44283 + }
44284 + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44285 + break;
44286 + }
44287 +
44288 +handle_xfercomp_done:
44289 + disable_hc_int(hc_regs, xfercompl);
44290 +
44291 + return 1;
44292 +}
44293 +
44294 +/**
44295 + * Handles a host channel STALL interrupt. This handler may be called in
44296 + * either DMA mode or Slave mode.
44297 + */
44298 +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
44299 + dwc_hc_t * hc,
44300 + dwc_otg_hc_regs_t * hc_regs,
44301 + dwc_otg_qtd_t * qtd)
44302 +{
44303 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44304 + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
44305 +
44306 + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
44307 + "STALL Received--\n", hc->hc_num);
44308 +
44309 + if (hcd->core_if->dma_desc_enable) {
44310 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
44311 + goto handle_stall_done;
44312 + }
44313 +
44314 + if (pipe_type == UE_CONTROL) {
44315 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
44316 + }
44317 +
44318 + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
44319 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
44320 + /*
44321 + * USB protocol requires resetting the data toggle for bulk
44322 + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
44323 + * setup command is issued to the endpoint. Anticipate the
44324 + * CLEAR_FEATURE command since a STALL has occurred and reset
44325 + * the data toggle now.
44326 + */
44327 + hc->qh->data_toggle = 0;
44328 + }
44329 +
44330 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
44331 +
44332 +handle_stall_done:
44333 + disable_hc_int(hc_regs, stall);
44334 +
44335 + return 1;
44336 +}
44337 +
44338 +/*
44339 + * Updates the state of the URB when a transfer has been stopped due to an
44340 + * abnormal condition before the transfer completes. Modifies the
44341 + * actual_length field of the URB to reflect the number of bytes that have
44342 + * actually been transferred via the host channel.
44343 + */
44344 +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
44345 + dwc_otg_hc_regs_t * hc_regs,
44346 + dwc_otg_hcd_urb_t * urb,
44347 + dwc_otg_qtd_t * qtd,
44348 + dwc_otg_halt_status_e halt_status)
44349 +{
44350 + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
44351 + halt_status, NULL);
44352 +
44353 + if (urb->actual_length + bytes_transferred > urb->length) {
44354 + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
44355 + hc->dev_addr, __func__, __LINE__);
44356 + bytes_transferred = urb->length - urb->actual_length;
44357 + }
44358 +
44359 + /* non DWORD-aligned buffer case handling. */
44360 + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
44361 + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
44362 + bytes_transferred);
44363 + }
44364 +
44365 + urb->actual_length += bytes_transferred;
44366 +
44367 +#ifdef DEBUG
44368 + {
44369 + hctsiz_data_t hctsiz;
44370 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44371 + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
44372 + __func__, (hc->ep_is_in ? "IN" : "OUT"),
44373 + hc->hc_num);
44374 + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
44375 + hc->start_pkt_count);
44376 + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
44377 + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
44378 + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
44379 + bytes_transferred);
44380 + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
44381 + urb->actual_length);
44382 + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
44383 + urb->length);
44384 + }
44385 +#endif
44386 +}
44387 +
44388 +/**
44389 + * Handles a host channel NAK interrupt. This handler may be called in either
44390 + * DMA mode or Slave mode.
44391 + */
44392 +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
44393 + dwc_hc_t * hc,
44394 + dwc_otg_hc_regs_t * hc_regs,
44395 + dwc_otg_qtd_t * qtd)
44396 +{
44397 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44398 + "NAK Received--\n", hc->hc_num);
44399 +
44400 + /*
44401 + * When we get bulk NAKs then remember this so we holdoff on this qh until
44402 + * the beginning of the next frame
44403 + */
44404 + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44405 + case UE_BULK:
44406 + case UE_CONTROL:
44407 + if (nak_holdoff && qtd->qh->do_split)
44408 + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
44409 + }
44410 +
44411 + /*
44412 + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
44413 + * interrupt. Re-start the SSPLIT transfer.
44414 + */
44415 + if (hc->do_split) {
44416 + if (hc->complete_split) {
44417 + qtd->error_count = 0;
44418 + }
44419 + qtd->complete_split = 0;
44420 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44421 + goto handle_nak_done;
44422 + }
44423 +
44424 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44425 + case UE_CONTROL:
44426 + case UE_BULK:
44427 + if (hcd->core_if->dma_enable && hc->ep_is_in) {
44428 + /*
44429 + * NAK interrupts are enabled on bulk/control IN
44430 + * transfers in DMA mode for the sole purpose of
44431 + * resetting the error count after a transaction error
44432 + * occurs. The core will continue transferring data.
44433 + * Disable other interrupts unmasked for the same
44434 + * reason.
44435 + */
44436 + disable_hc_int(hc_regs, datatglerr);
44437 + disable_hc_int(hc_regs, ack);
44438 + qtd->error_count = 0;
44439 + goto handle_nak_done;
44440 + }
44441 +
44442 + /*
44443 + * NAK interrupts normally occur during OUT transfers in DMA
44444 + * or Slave mode. For IN transfers, more requests will be
44445 + * queued as request queue space is available.
44446 + */
44447 + qtd->error_count = 0;
44448 +
44449 + if (!hc->qh->ping_state) {
44450 + update_urb_state_xfer_intr(hc, hc_regs,
44451 + qtd->urb, qtd,
44452 + DWC_OTG_HC_XFER_NAK);
44453 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44454 +
44455 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
44456 + hc->qh->ping_state = 1;
44457 + }
44458 +
44459 + /*
44460 + * Halt the channel so the transfer can be re-started from
44461 + * the appropriate point or the PING protocol will
44462 + * start/continue.
44463 + */
44464 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44465 + break;
44466 + case UE_INTERRUPT:
44467 + qtd->error_count = 0;
44468 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44469 + break;
44470 + case UE_ISOCHRONOUS:
44471 + /* Should never get called for isochronous transfers. */
44472 + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
44473 + break;
44474 + }
44475 +
44476 +handle_nak_done:
44477 + disable_hc_int(hc_regs, nak);
44478 +
44479 + return 1;
44480 +}
44481 +
44482 +/**
44483 + * Handles a host channel ACK interrupt. This interrupt is enabled when
44484 + * performing the PING protocol in Slave mode, when errors occur during
44485 + * either Slave mode or DMA mode, and during Start Split transactions.
44486 + */
44487 +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
44488 + dwc_hc_t * hc,
44489 + dwc_otg_hc_regs_t * hc_regs,
44490 + dwc_otg_qtd_t * qtd)
44491 +{
44492 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44493 + "ACK Received--\n", hc->hc_num);
44494 +
44495 + if (hc->do_split) {
44496 + /*
44497 + * Handle ACK on SSPLIT.
44498 + * ACK should not occur in CSPLIT.
44499 + */
44500 + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
44501 + qtd->ssplit_out_xfer_count = hc->xfer_len;
44502 + }
44503 + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
44504 + /* Don't need complete for isochronous out transfers. */
44505 + qtd->complete_split = 1;
44506 + }
44507 +
44508 + /* ISOC OUT */
44509 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
44510 + switch (hc->xact_pos) {
44511 + case DWC_HCSPLIT_XACTPOS_ALL:
44512 + break;
44513 + case DWC_HCSPLIT_XACTPOS_END:
44514 + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
44515 + qtd->isoc_split_offset = 0;
44516 + break;
44517 + case DWC_HCSPLIT_XACTPOS_BEGIN:
44518 + case DWC_HCSPLIT_XACTPOS_MID:
44519 + /*
44520 + * For BEGIN or MID, calculate the length for
44521 + * the next microframe to determine the correct
44522 + * SSPLIT token, either MID or END.
44523 + */
44524 + {
44525 + struct dwc_otg_hcd_iso_packet_desc
44526 + *frame_desc;
44527 +
44528 + frame_desc =
44529 + &qtd->urb->
44530 + iso_descs[qtd->isoc_frame_index];
44531 + qtd->isoc_split_offset += 188;
44532 +
44533 + if ((frame_desc->length -
44534 + qtd->isoc_split_offset) <= 188) {
44535 + qtd->isoc_split_pos =
44536 + DWC_HCSPLIT_XACTPOS_END;
44537 + } else {
44538 + qtd->isoc_split_pos =
44539 + DWC_HCSPLIT_XACTPOS_MID;
44540 + }
44541 +
44542 + }
44543 + break;
44544 + }
44545 + } else {
44546 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
44547 + }
44548 + } else {
44549 + /*
44550 + * An unmasked ACK on a non-split DMA transaction is
44551 + * for the sole purpose of resetting error counts. Disable other
44552 + * interrupts unmasked for the same reason.
44553 + */
44554 + if(hcd->core_if->dma_enable) {
44555 + disable_hc_int(hc_regs, datatglerr);
44556 + disable_hc_int(hc_regs, nak);
44557 + }
44558 + qtd->error_count = 0;
44559 +
44560 + if (hc->qh->ping_state) {
44561 + hc->qh->ping_state = 0;
44562 + /*
44563 + * Halt the channel so the transfer can be re-started
44564 + * from the appropriate point. This only happens in
44565 + * Slave mode. In DMA mode, the ping_state is cleared
44566 + * when the transfer is started because the core
44567 + * automatically executes the PING, then the transfer.
44568 + */
44569 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
44570 + }
44571 + }
44572 +
44573 + /*
44574 + * If the ACK occurred when _not_ in the PING state, let the channel
44575 + * continue transferring data after clearing the error count.
44576 + */
44577 +
44578 + disable_hc_int(hc_regs, ack);
44579 +
44580 + return 1;
44581 +}
44582 +
44583 +/**
44584 + * Handles a host channel NYET interrupt. This interrupt should only occur on
44585 + * Bulk and Control OUT endpoints and for complete split transactions. If a
44586 + * NYET occurs at the same time as a Transfer Complete interrupt, it is
44587 + * handled in the xfercomp interrupt handler, not here. This handler may be
44588 + * called in either DMA mode or Slave mode.
44589 + */
44590 +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
44591 + dwc_hc_t * hc,
44592 + dwc_otg_hc_regs_t * hc_regs,
44593 + dwc_otg_qtd_t * qtd)
44594 +{
44595 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44596 + "NYET Received--\n", hc->hc_num);
44597 +
44598 + /*
44599 + * NYET on CSPLIT
44600 + * re-do the CSPLIT immediately on non-periodic
44601 + */
44602 + if (hc->do_split && hc->complete_split) {
44603 + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
44604 + && hcd->core_if->dma_enable) {
44605 + qtd->complete_split = 0;
44606 + qtd->isoc_split_offset = 0;
44607 + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
44608 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
44609 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
44610 + }
44611 + else
44612 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
44613 + goto handle_nyet_done;
44614 + }
44615 +
44616 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
44617 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
44618 + int frnum = dwc_otg_hcd_get_frame_number(hcd);
44619 +
44620 + // With the FIQ running we only ever see the failed NYET
44621 + if (dwc_full_frame_num(frnum) !=
44622 + dwc_full_frame_num(hc->qh->sched_frame) ||
44623 + fiq_fsm_enable) {
44624 + /*
44625 + * No longer in the same full speed frame.
44626 + * Treat this as a transaction error.
44627 + */
44628 +#if 0
44629 + /** @todo Fix system performance so this can
44630 + * be treated as an error. Right now complete
44631 + * splits cannot be scheduled precisely enough
44632 + * due to other system activity, so this error
44633 + * occurs regularly in Slave mode.
44634 + */
44635 + qtd->error_count++;
44636 +#endif
44637 + qtd->complete_split = 0;
44638 + halt_channel(hcd, hc, qtd,
44639 + DWC_OTG_HC_XFER_XACT_ERR);
44640 + /** @todo add support for isoc release */
44641 + goto handle_nyet_done;
44642 + }
44643 + }
44644 +
44645 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
44646 + goto handle_nyet_done;
44647 + }
44648 +
44649 + hc->qh->ping_state = 1;
44650 + qtd->error_count = 0;
44651 +
44652 + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
44653 + DWC_OTG_HC_XFER_NYET);
44654 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44655 +
44656 + /*
44657 + * Halt the channel and re-start the transfer so the PING
44658 + * protocol will start.
44659 + */
44660 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
44661 +
44662 +handle_nyet_done:
44663 + disable_hc_int(hc_regs, nyet);
44664 + return 1;
44665 +}
44666 +
44667 +/**
44668 + * Handles a host channel babble interrupt. This handler may be called in
44669 + * either DMA mode or Slave mode.
44670 + */
44671 +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
44672 + dwc_hc_t * hc,
44673 + dwc_otg_hc_regs_t * hc_regs,
44674 + dwc_otg_qtd_t * qtd)
44675 +{
44676 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44677 + "Babble Error--\n", hc->hc_num);
44678 +
44679 + if (hcd->core_if->dma_desc_enable) {
44680 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44681 + DWC_OTG_HC_XFER_BABBLE_ERR);
44682 + goto handle_babble_done;
44683 + }
44684 +
44685 + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
44686 + hcd->fops->complete(hcd, qtd->urb->priv,
44687 + qtd->urb, -DWC_E_OVERFLOW);
44688 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
44689 + } else {
44690 + dwc_otg_halt_status_e halt_status;
44691 + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44692 + DWC_OTG_HC_XFER_BABBLE_ERR);
44693 + halt_channel(hcd, hc, qtd, halt_status);
44694 + }
44695 +
44696 +handle_babble_done:
44697 + disable_hc_int(hc_regs, bblerr);
44698 + return 1;
44699 +}
44700 +
44701 +/**
44702 + * Handles a host channel AHB error interrupt. This handler is only called in
44703 + * DMA mode.
44704 + */
44705 +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
44706 + dwc_hc_t * hc,
44707 + dwc_otg_hc_regs_t * hc_regs,
44708 + dwc_otg_qtd_t * qtd)
44709 +{
44710 + hcchar_data_t hcchar;
44711 + hcsplt_data_t hcsplt;
44712 + hctsiz_data_t hctsiz;
44713 + uint32_t hcdma;
44714 + char *pipetype, *speed;
44715 +
44716 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44717 +
44718 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44719 + "AHB Error--\n", hc->hc_num);
44720 +
44721 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
44722 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
44723 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44724 + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
44725 +
44726 + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
44727 + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
44728 + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
44729 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
44730 + DWC_ERROR(" Device address: %d\n",
44731 + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
44732 + DWC_ERROR(" Endpoint: %d, %s\n",
44733 + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
44734 + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
44735 +
44736 + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
44737 + case UE_CONTROL:
44738 + pipetype = "CONTROL";
44739 + break;
44740 + case UE_BULK:
44741 + pipetype = "BULK";
44742 + break;
44743 + case UE_INTERRUPT:
44744 + pipetype = "INTERRUPT";
44745 + break;
44746 + case UE_ISOCHRONOUS:
44747 + pipetype = "ISOCHRONOUS";
44748 + break;
44749 + default:
44750 + pipetype = "UNKNOWN";
44751 + break;
44752 + }
44753 +
44754 + DWC_ERROR(" Endpoint type: %s\n", pipetype);
44755 +
44756 + switch (hc->speed) {
44757 + case DWC_OTG_EP_SPEED_HIGH:
44758 + speed = "HIGH";
44759 + break;
44760 + case DWC_OTG_EP_SPEED_FULL:
44761 + speed = "FULL";
44762 + break;
44763 + case DWC_OTG_EP_SPEED_LOW:
44764 + speed = "LOW";
44765 + break;
44766 + default:
44767 + speed = "UNKNOWN";
44768 + break;
44769 + };
44770 +
44771 + DWC_ERROR(" Speed: %s\n", speed);
44772 +
44773 + DWC_ERROR(" Max packet size: %d\n",
44774 + dwc_otg_hcd_get_mps(&urb->pipe_info));
44775 + DWC_ERROR(" Data buffer length: %d\n", urb->length);
44776 + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
44777 + urb->buf, (void *)urb->dma);
44778 + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
44779 + urb->setup_packet, (void *)urb->setup_dma);
44780 + DWC_ERROR(" Interval: %d\n", urb->interval);
44781 +
44782 + /* Core haltes the channel for Descriptor DMA mode */
44783 + if (hcd->core_if->dma_desc_enable) {
44784 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44785 + DWC_OTG_HC_XFER_AHB_ERR);
44786 + goto handle_ahberr_done;
44787 + }
44788 +
44789 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
44790 +
44791 + /*
44792 + * Force a channel halt. Don't call halt_channel because that won't
44793 + * write to the HCCHARn register in DMA mode to force the halt.
44794 + */
44795 + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
44796 +handle_ahberr_done:
44797 + disable_hc_int(hc_regs, ahberr);
44798 + return 1;
44799 +}
44800 +
44801 +/**
44802 + * Handles a host channel transaction error interrupt. This handler may be
44803 + * called in either DMA mode or Slave mode.
44804 + */
44805 +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
44806 + dwc_hc_t * hc,
44807 + dwc_otg_hc_regs_t * hc_regs,
44808 + dwc_otg_qtd_t * qtd)
44809 +{
44810 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44811 + "Transaction Error--\n", hc->hc_num);
44812 +
44813 + if (hcd->core_if->dma_desc_enable) {
44814 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44815 + DWC_OTG_HC_XFER_XACT_ERR);
44816 + goto handle_xacterr_done;
44817 + }
44818 +
44819 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44820 + case UE_CONTROL:
44821 + case UE_BULK:
44822 + qtd->error_count++;
44823 + if (!hc->qh->ping_state) {
44824 +
44825 + update_urb_state_xfer_intr(hc, hc_regs,
44826 + qtd->urb, qtd,
44827 + DWC_OTG_HC_XFER_XACT_ERR);
44828 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44829 + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
44830 + hc->qh->ping_state = 1;
44831 + }
44832 + }
44833 +
44834 + /*
44835 + * Halt the channel so the transfer can be re-started from
44836 + * the appropriate point or the PING protocol will start.
44837 + */
44838 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
44839 + break;
44840 + case UE_INTERRUPT:
44841 + qtd->error_count++;
44842 + if (hc->do_split && hc->complete_split) {
44843 + qtd->complete_split = 0;
44844 + }
44845 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
44846 + break;
44847 + case UE_ISOCHRONOUS:
44848 + {
44849 + dwc_otg_halt_status_e halt_status;
44850 + halt_status =
44851 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44852 + DWC_OTG_HC_XFER_XACT_ERR);
44853 +
44854 + halt_channel(hcd, hc, qtd, halt_status);
44855 + }
44856 + break;
44857 + }
44858 +handle_xacterr_done:
44859 + disable_hc_int(hc_regs, xacterr);
44860 +
44861 + return 1;
44862 +}
44863 +
44864 +/**
44865 + * Handles a host channel frame overrun interrupt. This handler may be called
44866 + * in either DMA mode or Slave mode.
44867 + */
44868 +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
44869 + dwc_hc_t * hc,
44870 + dwc_otg_hc_regs_t * hc_regs,
44871 + dwc_otg_qtd_t * qtd)
44872 +{
44873 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44874 + "Frame Overrun--\n", hc->hc_num);
44875 +
44876 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44877 + case UE_CONTROL:
44878 + case UE_BULK:
44879 + break;
44880 + case UE_INTERRUPT:
44881 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
44882 + break;
44883 + case UE_ISOCHRONOUS:
44884 + {
44885 + dwc_otg_halt_status_e halt_status;
44886 + halt_status =
44887 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44888 + DWC_OTG_HC_XFER_FRAME_OVERRUN);
44889 +
44890 + halt_channel(hcd, hc, qtd, halt_status);
44891 + }
44892 + break;
44893 + }
44894 +
44895 + disable_hc_int(hc_regs, frmovrun);
44896 +
44897 + return 1;
44898 +}
44899 +
44900 +/**
44901 + * Handles a host channel data toggle error interrupt. This handler may be
44902 + * called in either DMA mode or Slave mode.
44903 + */
44904 +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
44905 + dwc_hc_t * hc,
44906 + dwc_otg_hc_regs_t * hc_regs,
44907 + dwc_otg_qtd_t * qtd)
44908 +{
44909 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44910 + "Data Toggle Error on %s transfer--\n",
44911 + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
44912 +
44913 + /* Data toggles on split transactions cause the hc to halt.
44914 + * restart transfer */
44915 + if(hc->qh->do_split)
44916 + {
44917 + qtd->error_count++;
44918 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44919 + update_urb_state_xfer_intr(hc, hc_regs,
44920 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
44921 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
44922 + } else if (hc->ep_is_in) {
44923 + /* An unmasked data toggle error on a non-split DMA transaction is
44924 + * for the sole purpose of resetting error counts. Disable other
44925 + * interrupts unmasked for the same reason.
44926 + */
44927 + if(hcd->core_if->dma_enable) {
44928 + disable_hc_int(hc_regs, ack);
44929 + disable_hc_int(hc_regs, nak);
44930 + }
44931 + qtd->error_count = 0;
44932 + }
44933 +
44934 + disable_hc_int(hc_regs, datatglerr);
44935 +
44936 + return 1;
44937 +}
44938 +
44939 +#ifdef DEBUG
44940 +/**
44941 + * This function is for debug only. It checks that a valid halt status is set
44942 + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
44943 + * taken and a warning is issued.
44944 + * @return 1 if halt status is ok, 0 otherwise.
44945 + */
44946 +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
44947 + dwc_hc_t * hc,
44948 + dwc_otg_hc_regs_t * hc_regs,
44949 + dwc_otg_qtd_t * qtd)
44950 +{
44951 + hcchar_data_t hcchar;
44952 + hctsiz_data_t hctsiz;
44953 + hcint_data_t hcint;
44954 + hcintmsk_data_t hcintmsk;
44955 + hcsplt_data_t hcsplt;
44956 +
44957 + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
44958 + /*
44959 + * This code is here only as a check. This condition should
44960 + * never happen. Ignore the halt if it does occur.
44961 + */
44962 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
44963 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44964 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
44965 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
44966 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
44967 + DWC_WARN
44968 + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
44969 + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
44970 + "hcint 0x%08x, hcintmsk 0x%08x, "
44971 + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
44972 + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
44973 + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
44974 +
44975 + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
44976 + __func__, hc->hc_num);
44977 + DWC_WARN("\n");
44978 + clear_hc_int(hc_regs, chhltd);
44979 + return 0;
44980 + }
44981 +
44982 + /*
44983 + * This code is here only as a check. hcchar.chdis should
44984 + * never be set when the halt interrupt occurs. Halt the
44985 + * channel again if it does occur.
44986 + */
44987 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
44988 + if (hcchar.b.chdis) {
44989 + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
44990 + "hcchar 0x%08x, trying to halt again\n",
44991 + __func__, hcchar.d32);
44992 + clear_hc_int(hc_regs, chhltd);
44993 + hc->halt_pending = 0;
44994 + halt_channel(hcd, hc, qtd, hc->halt_status);
44995 + return 0;
44996 + }
44997 +
44998 + return 1;
44999 +}
45000 +#endif
45001 +
45002 +/**
45003 + * Handles a host Channel Halted interrupt in DMA mode. This handler
45004 + * determines the reason the channel halted and proceeds accordingly.
45005 + */
45006 +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
45007 + dwc_hc_t * hc,
45008 + dwc_otg_hc_regs_t * hc_regs,
45009 + dwc_otg_qtd_t * qtd)
45010 +{
45011 + int out_nak_enh = 0;
45012 + hcint_data_t hcint;
45013 + hcintmsk_data_t hcintmsk;
45014 + /* For core with OUT NAK enhancement, the flow for high-
45015 + * speed CONTROL/BULK OUT is handled a little differently.
45016 + */
45017 + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
45018 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
45019 + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
45020 + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
45021 + out_nak_enh = 1;
45022 + }
45023 + }
45024 +
45025 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
45026 + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
45027 + && !hcd->core_if->dma_desc_enable)) {
45028 + /*
45029 + * Just release the channel. A dequeue can happen on a
45030 + * transfer timeout. In the case of an AHB Error, the channel
45031 + * was forced to halt because there's no way to gracefully
45032 + * recover.
45033 + */
45034 + if (hcd->core_if->dma_desc_enable)
45035 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
45036 + hc->halt_status);
45037 + else
45038 + release_channel(hcd, hc, qtd, hc->halt_status);
45039 + return;
45040 + }
45041 +
45042 + /* Read the HCINTn register to determine the cause for the halt. */
45043 +
45044 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45045 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45046 +
45047 + if (hcint.b.xfercomp) {
45048 + /** @todo This is here because of a possible hardware bug. Spec
45049 + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
45050 + * interrupt w/ACK bit set should occur, but I only see the
45051 + * XFERCOMP bit, even with it masked out. This is a workaround
45052 + * for that behavior. Should fix this when hardware is fixed.
45053 + */
45054 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
45055 + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
45056 + }
45057 + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
45058 + } else if (hcint.b.stall) {
45059 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45060 + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
45061 + if (out_nak_enh) {
45062 + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
45063 + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
45064 + qtd->error_count = 0;
45065 + } else {
45066 + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
45067 + }
45068 + }
45069 +
45070 + /*
45071 + * Must handle xacterr before nak or ack. Could get a xacterr
45072 + * at the same time as either of these on a BULK/CONTROL OUT
45073 + * that started with a PING. The xacterr takes precedence.
45074 + */
45075 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45076 + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
45077 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45078 + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
45079 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45080 + } else if (hcint.b.bblerr) {
45081 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45082 + } else if (hcint.b.frmovrun) {
45083 + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
45084 + } else if (hcint.b.datatglerr) {
45085 + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
45086 + } else if (!out_nak_enh) {
45087 + if (hcint.b.nyet) {
45088 + /*
45089 + * Must handle nyet before nak or ack. Could get a nyet at the
45090 + * same time as either of those on a BULK/CONTROL OUT that
45091 + * started with a PING. The nyet takes precedence.
45092 + */
45093 + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
45094 + } else if (hcint.b.nak && !hcintmsk.b.nak) {
45095 + /*
45096 + * If nak is not masked, it's because a non-split IN transfer
45097 + * is in an error state. In that case, the nak is handled by
45098 + * the nak interrupt handler, not here. Handle nak here for
45099 + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
45100 + * rewinding the buffer pointer.
45101 + */
45102 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45103 + } else if (hcint.b.ack && !hcintmsk.b.ack) {
45104 + /*
45105 + * If ack is not masked, it's because a non-split IN transfer
45106 + * is in an error state. In that case, the ack is handled by
45107 + * the ack interrupt handler, not here. Handle ack here for
45108 + * split transfers. Start splits halt on ACK.
45109 + */
45110 + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
45111 + } else {
45112 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
45113 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
45114 + /*
45115 + * A periodic transfer halted with no other channel
45116 + * interrupts set. Assume it was halted by the core
45117 + * because it could not be completed in its scheduled
45118 + * (micro)frame.
45119 + */
45120 +#ifdef DEBUG
45121 + DWC_PRINTF
45122 + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
45123 + __func__, hc->hc_num);
45124 +#endif
45125 + halt_channel(hcd, hc, qtd,
45126 + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
45127 + } else {
45128 + DWC_ERROR
45129 + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
45130 + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
45131 + __func__, hc->hc_num, hcint.d32,
45132 + DWC_READ_REG32(&hcd->
45133 + core_if->core_global_regs->
45134 + gintsts));
45135 + /* Failthrough: use 3-strikes rule */
45136 + qtd->error_count++;
45137 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45138 + update_urb_state_xfer_intr(hc, hc_regs,
45139 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45140 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45141 + }
45142 +
45143 + }
45144 + } else {
45145 + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
45146 + hcint.d32);
45147 + /* Failthrough: use 3-strikes rule */
45148 + qtd->error_count++;
45149 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45150 + update_urb_state_xfer_intr(hc, hc_regs,
45151 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45152 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45153 + }
45154 +}
45155 +
45156 +/**
45157 + * Handles a host channel Channel Halted interrupt.
45158 + *
45159 + * In slave mode, this handler is called only when the driver specifically
45160 + * requests a halt. This occurs during handling other host channel interrupts
45161 + * (e.g. nak, xacterr, stall, nyet, etc.).
45162 + *
45163 + * In DMA mode, this is the interrupt that occurs when the core has finished
45164 + * processing a transfer on a channel. Other host channel interrupts (except
45165 + * ahberr) are disabled in DMA mode.
45166 + */
45167 +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
45168 + dwc_hc_t * hc,
45169 + dwc_otg_hc_regs_t * hc_regs,
45170 + dwc_otg_qtd_t * qtd)
45171 +{
45172 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45173 + "Channel Halted--\n", hc->hc_num);
45174 +
45175 + if (hcd->core_if->dma_enable) {
45176 + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
45177 + } else {
45178 +#ifdef DEBUG
45179 + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
45180 + return 1;
45181 + }
45182 +#endif
45183 + release_channel(hcd, hc, qtd, hc->halt_status);
45184 + }
45185 +
45186 + return 1;
45187 +}
45188 +
45189 +
45190 +/**
45191 + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
45192 + * FIQ transfer completion
45193 + * @hcd: Pointer to dwc_otg_hcd struct
45194 + * @num: Host channel number
45195 + *
45196 + * 1. Un-mangle the status as recorded in each iso_frame_desc status
45197 + * 2. Copy it from the dwc_otg_urb into the real URB
45198 + */
45199 +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
45200 +{
45201 + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
45202 + int nr_frames = dwc_urb->packet_count;
45203 + int i;
45204 + hcint_data_t frame_hcint;
45205 +
45206 + for (i = 0; i < nr_frames; i++) {
45207 + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
45208 + if (frame_hcint.b.xfercomp) {
45209 + dwc_urb->iso_descs[i].status = 0;
45210 + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
45211 + } else if (frame_hcint.b.frmovrun) {
45212 + if (qh->ep_is_in)
45213 + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
45214 + else
45215 + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
45216 + dwc_urb->error_count++;
45217 + dwc_urb->iso_descs[i].actual_length = 0;
45218 + } else if (frame_hcint.b.xacterr) {
45219 + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
45220 + dwc_urb->error_count++;
45221 + dwc_urb->iso_descs[i].actual_length = 0;
45222 + } else if (frame_hcint.b.bblerr) {
45223 + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
45224 + dwc_urb->error_count++;
45225 + dwc_urb->iso_descs[i].actual_length = 0;
45226 + } else {
45227 + /* Something went wrong */
45228 + dwc_urb->iso_descs[i].status = -1;
45229 + dwc_urb->iso_descs[i].actual_length = 0;
45230 + dwc_urb->error_count++;
45231 + }
45232 + }
45233 + qh->sched_frame = dwc_frame_num_inc(qh->sched_frame, qh->interval * (nr_frames - 1));
45234 +
45235 + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
45236 + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
45237 +}
45238 +
45239 +/**
45240 + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
45241 + * @hcd: Pointer to dwc_otg_hcd struct
45242 + * @num: Host channel number
45243 + *
45244 + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
45245 + * Returns total length of data or -1 if the buffers were not used.
45246 + *
45247 + */
45248 +int dwc_otg_fiq_unsetup_per_dma(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
45249 +{
45250 + dwc_hc_t *hc = qh->channel;
45251 + struct fiq_dma_blob *blob = hcd->fiq_dmab;
45252 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
45253 + uint8_t *ptr = NULL;
45254 + int index = 0, len = 0;
45255 + int i = 0;
45256 + if (hc->ep_is_in) {
45257 + /* Copy data out of the DMA bounce buffers to the URB's buffer.
45258 + * The align_buf is ignored as this is ignored on FSM enqueue. */
45259 + ptr = qtd->urb->buf;
45260 + if (qh->ep_type == UE_ISOCHRONOUS) {
45261 + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
45262 + index = qtd->isoc_frame_index;
45263 + ptr += qtd->urb->iso_descs[index].offset;
45264 + } else {
45265 + /* Need to increment by actual_length for interrupt IN */
45266 + ptr += qtd->urb->actual_length;
45267 + }
45268 +
45269 + for (i = 0; i < st->dma_info.index; i++) {
45270 + len += st->dma_info.slot_len[i];
45271 + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
45272 + ptr += st->dma_info.slot_len[i];
45273 + }
45274 + return len;
45275 + } else {
45276 + /* OUT endpoints - nothing to do. */
45277 + return -1;
45278 + }
45279 +
45280 +}
45281 +/**
45282 + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
45283 + * from a channel handled in the FIQ
45284 + * @hcd: Pointer to dwc_otg_hcd struct
45285 + * @num: Host channel number
45286 + *
45287 + * If a host channel interrupt was received by the IRQ and this was a channel
45288 + * used by the FIQ, the execution flow for transfer completion is substantially
45289 + * different from the normal (messy) path. This function and its friends handles
45290 + * channel cleanup and transaction completion from a FIQ transaction.
45291 + */
45292 +void dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
45293 +{
45294 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
45295 + dwc_hc_t *hc = hcd->hc_ptr_array[num];
45296 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
45297 + dwc_otg_qh_t *qh = hc->qh;
45298 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
45299 + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
45300 + int hostchannels = 0;
45301 + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
45302 +
45303 + hostchannels = hcd->available_host_channels;
45304 + switch (st->fsm) {
45305 + case FIQ_TEST:
45306 + break;
45307 +
45308 + case FIQ_DEQUEUE_ISSUED:
45309 + /* hc_halt was called. QTD no longer exists. */
45310 + /* TODO: for a nonperiodic split transaction, need to issue a
45311 + * CLEAR_TT_BUFFER hub command if we were in the start-split phase.
45312 + */
45313 + release_channel(hcd, hc, NULL, hc->halt_status);
45314 + break;
45315 +
45316 + case FIQ_NP_SPLIT_DONE:
45317 + /* Nonperiodic transaction complete. */
45318 + if (!hc->ep_is_in) {
45319 + qtd->ssplit_out_xfer_count = hc->xfer_len;
45320 + }
45321 + if (hcint.b.xfercomp) {
45322 + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
45323 + } else if (hcint.b.nak) {
45324 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45325 + }
45326 + break;
45327 +
45328 + case FIQ_NP_SPLIT_HS_ABORTED:
45329 + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
45330 + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
45331 + * because there's no guarantee which order a non-periodic split happened in.
45332 + * We could end up clearing a perfectly good transaction out of the buffer.
45333 + */
45334 + if (hcint.b.xacterr) {
45335 + qtd->error_count += st->nr_errors;
45336 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45337 + } else if (hcint.b.ahberr) {
45338 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45339 + } else {
45340 + local_fiq_disable();
45341 + BUG();
45342 + }
45343 + break;
45344 +
45345 + case FIQ_NP_SPLIT_LS_ABORTED:
45346 + /* A few cases can cause this - either an unknown state on a SSPLIT or
45347 + * STALL/data toggle error response on a CSPLIT */
45348 + if (hcint.b.stall) {
45349 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45350 + } else if (hcint.b.datatglerr) {
45351 + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
45352 + } else if (hcint.b.bblerr) {
45353 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45354 + } else if (hcint.b.ahberr) {
45355 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45356 + } else {
45357 + local_fiq_disable();
45358 + BUG();
45359 + }
45360 + break;
45361 +
45362 + case FIQ_PER_SPLIT_DONE:
45363 + /* Isoc IN or Interrupt IN/OUT */
45364 +
45365 + /* Flow control here is different from the normal execution by the driver.
45366 + * We need to completely ignore most of the driver's method of handling
45367 + * split transactions and do it ourselves.
45368 + */
45369 + if (hc->ep_type == UE_INTERRUPT) {
45370 + if (hcint.b.nak) {
45371 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45372 + } else if (hc->ep_is_in) {
45373 + int len;
45374 + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
45375 + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
45376 + qtd->urb->actual_length += len;
45377 + if (qtd->urb->actual_length >= qtd->urb->length) {
45378 + qtd->urb->status = 0;
45379 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45380 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45381 + } else {
45382 + /* Interrupt transfer not complete yet - is it a short read? */
45383 + if (len < hc->max_packet) {
45384 + /* Interrupt transaction complete */
45385 + qtd->urb->status = 0;
45386 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45387 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45388 + } else {
45389 + /* Further transactions required */
45390 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45391 + }
45392 + }
45393 + } else {
45394 + /* Interrupt OUT complete. */
45395 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45396 + qtd->urb->actual_length += hc->xfer_len;
45397 + if (qtd->urb->actual_length >= qtd->urb->length) {
45398 + qtd->urb->status = 0;
45399 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45400 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45401 + } else {
45402 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45403 + }
45404 + }
45405 + } else {
45406 + /* ISOC IN complete. */
45407 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45408 + int len = 0;
45409 + /* Record errors, update qtd. */
45410 + if (st->nr_errors) {
45411 + frame_desc->actual_length = 0;
45412 + frame_desc->status = -DWC_E_PROTOCOL;
45413 + } else {
45414 + frame_desc->status = 0;
45415 + /* Unswizzle dma */
45416 + len = dwc_otg_fiq_unsetup_per_dma(hcd, qh, qtd, num);
45417 + frame_desc->actual_length = len;
45418 + }
45419 + qtd->isoc_frame_index++;
45420 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45421 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45422 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45423 + } else {
45424 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45425 + }
45426 + }
45427 + break;
45428 +
45429 + case FIQ_PER_ISO_OUT_DONE: {
45430 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45431 + /* Record errors, update qtd. */
45432 + if (st->nr_errors) {
45433 + frame_desc->actual_length = 0;
45434 + frame_desc->status = -DWC_E_PROTOCOL;
45435 + } else {
45436 + frame_desc->status = 0;
45437 + frame_desc->actual_length = frame_desc->length;
45438 + }
45439 + qtd->isoc_frame_index++;
45440 + qtd->isoc_split_offset = 0;
45441 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45442 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45443 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45444 + } else {
45445 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45446 + }
45447 + }
45448 + break;
45449 +
45450 + case FIQ_PER_SPLIT_NYET_ABORTED:
45451 + /* Doh. lost the data. */
45452 + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
45453 + "- FIQ reported NYET. Data may have been lost.\n",
45454 + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
45455 + if (hc->ep_type == UE_ISOCHRONOUS) {
45456 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45457 + /* Record errors, update qtd. */
45458 + frame_desc->actual_length = 0;
45459 + frame_desc->status = -DWC_E_PROTOCOL;
45460 + qtd->isoc_frame_index++;
45461 + qtd->isoc_split_offset = 0;
45462 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45463 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45464 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45465 + } else {
45466 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45467 + }
45468 + } else {
45469 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45470 + }
45471 + break;
45472 +
45473 + case FIQ_HS_ISOC_DONE:
45474 + /* The FIQ has performed a whole pile of isochronous transactions.
45475 + * The status is recorded as the interrupt state should the transaction
45476 + * fail.
45477 + */
45478 + dwc_otg_fiq_unmangle_isoc(hcd, qh, qtd, num);
45479 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45480 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45481 + break;
45482 +
45483 + case FIQ_PER_SPLIT_LS_ABORTED:
45484 + if (hcint.b.xacterr) {
45485 + /* Hub has responded with an ERR packet. Device
45486 + * has been unplugged or the port has been disabled.
45487 + * TODO: need to issue a reset to the hub port. */
45488 + qtd->error_count += 3;
45489 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45490 + } else if (hcint.b.stall) {
45491 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45492 + } else if (hcint.b.bblerr) {
45493 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45494 + } else {
45495 + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
45496 + "- FIQ reported FSM=%d. Data may have been lost.\n",
45497 + st->fsm, hc->dev_addr, hc->ep_num);
45498 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45499 + }
45500 + break;
45501 +
45502 + case FIQ_PER_SPLIT_HS_ABORTED:
45503 + /* Either the SSPLIT phase suffered transaction errors or something
45504 + * unexpected happened.
45505 + */
45506 + qtd->error_count += 3;
45507 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45508 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45509 + break;
45510 +
45511 + case FIQ_PER_SPLIT_TIMEOUT:
45512 + /* Couldn't complete in the nominated frame */
45513 + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
45514 + "- FIQ timed out. Data may have been lost.\n",
45515 + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
45516 + if (hc->ep_type == UE_ISOCHRONOUS) {
45517 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45518 + /* Record errors, update qtd. */
45519 + frame_desc->actual_length = 0;
45520 + if (hc->ep_is_in) {
45521 + frame_desc->status = -DWC_E_NO_STREAM_RES;
45522 + } else {
45523 + frame_desc->status = -DWC_E_COMMUNICATION;
45524 + }
45525 + qtd->isoc_frame_index++;
45526 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45527 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45528 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45529 + } else {
45530 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45531 + }
45532 + } else {
45533 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45534 + }
45535 + break;
45536 +
45537 + default:
45538 + DWC_WARN("Unexpected state received on hc=%d fsm=%d on transfer to device %d ep 0x%x",
45539 + hc->hc_num, st->fsm, hc->dev_addr, hc->ep_num);
45540 + qtd->error_count++;
45541 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45542 + }
45543 + return;
45544 +}
45545 +
45546 +/** Handles interrupt for a specific Host Channel */
45547 +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
45548 +{
45549 + int retval = 0;
45550 + hcint_data_t hcint;
45551 + hcintmsk_data_t hcintmsk;
45552 + dwc_hc_t *hc;
45553 + dwc_otg_hc_regs_t *hc_regs;
45554 + dwc_otg_qtd_t *qtd;
45555 +
45556 + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
45557 +
45558 + hc = dwc_otg_hcd->hc_ptr_array[num];
45559 + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
45560 + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
45561 + /* We are responding to a channel disable. Driver
45562 + * state is cleared - our qtd has gone away.
45563 + */
45564 + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
45565 + return 1;
45566 + }
45567 + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
45568 +
45569 + /*
45570 + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
45571 + * Execution path is fundamentally different for the channels after a FIQ has completed
45572 + * a split transaction.
45573 + */
45574 + if (fiq_fsm_enable) {
45575 + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
45576 + case FIQ_PASSTHROUGH:
45577 + break;
45578 + case FIQ_PASSTHROUGH_ERRORSTATE:
45579 + /* Hook into the error count */
45580 + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
45581 + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
45582 + qtd->error_count = 0;
45583 + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
45584 + }
45585 + break;
45586 + default:
45587 + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
45588 + return 1;
45589 + }
45590 + }
45591 +
45592 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45593 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45594 + hcint.d32 = hcint.d32 & hcintmsk.d32;
45595 + if (!dwc_otg_hcd->core_if->dma_enable) {
45596 + if (hcint.b.chhltd && hcint.d32 != 0x2) {
45597 + hcint.b.chhltd = 0;
45598 + }
45599 + }
45600 +
45601 + if (hcint.b.xfercomp) {
45602 + retval |=
45603 + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45604 + /*
45605 + * If NYET occurred at same time as Xfer Complete, the NYET is
45606 + * handled by the Xfer Complete interrupt handler. Don't want
45607 + * to call the NYET interrupt handler in this case.
45608 + */
45609 + hcint.b.nyet = 0;
45610 + }
45611 + if (hcint.b.chhltd) {
45612 + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45613 + }
45614 + if (hcint.b.ahberr) {
45615 + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45616 + }
45617 + if (hcint.b.stall) {
45618 + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45619 + }
45620 + if (hcint.b.nak) {
45621 + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45622 + }
45623 + if (hcint.b.ack) {
45624 + if(!hcint.b.chhltd)
45625 + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45626 + }
45627 + if (hcint.b.nyet) {
45628 + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45629 + }
45630 + if (hcint.b.xacterr) {
45631 + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45632 + }
45633 + if (hcint.b.bblerr) {
45634 + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45635 + }
45636 + if (hcint.b.frmovrun) {
45637 + retval |=
45638 + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45639 + }
45640 + if (hcint.b.datatglerr) {
45641 + retval |=
45642 + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45643 + }
45644 +
45645 + return retval;
45646 +}
45647 +#endif /* DWC_DEVICE_ONLY */
45648 --- /dev/null
45649 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
45650 @@ -0,0 +1,1005 @@
45651 +
45652 +/* ==========================================================================
45653 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
45654 + * $Revision: #20 $
45655 + * $Date: 2011/10/26 $
45656 + * $Change: 1872981 $
45657 + *
45658 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
45659 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
45660 + * otherwise expressly agreed to in writing between Synopsys and you.
45661 + *
45662 + * The Software IS NOT an item of Licensed Software or Licensed Product under
45663 + * any End User Software License Agreement or Agreement for Licensed Product
45664 + * with Synopsys or any supplement thereto. You are permitted to use and
45665 + * redistribute this Software in source and binary forms, with or without
45666 + * modification, provided that redistributions of source code must retain this
45667 + * notice. You may not view, use, disclose, copy or distribute this file or
45668 + * any information contained herein except pursuant to this license grant from
45669 + * Synopsys. If you do not agree with this notice, including the disclaimer
45670 + * below, then you are not authorized to use the Software.
45671 + *
45672 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
45673 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45674 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45675 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
45676 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45677 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45678 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
45679 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45680 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45681 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45682 + * DAMAGE.
45683 + * ========================================================================== */
45684 +#ifndef DWC_DEVICE_ONLY
45685 +
45686 +/**
45687 + * @file
45688 + *
45689 + * This file contains the implementation of the HCD. In Linux, the HCD
45690 + * implements the hc_driver API.
45691 + */
45692 +#include <linux/kernel.h>
45693 +#include <linux/module.h>
45694 +#include <linux/moduleparam.h>
45695 +#include <linux/init.h>
45696 +#include <linux/device.h>
45697 +#include <linux/errno.h>
45698 +#include <linux/list.h>
45699 +#include <linux/interrupt.h>
45700 +#include <linux/string.h>
45701 +#include <linux/dma-mapping.h>
45702 +#include <linux/version.h>
45703 +#include <asm/io.h>
45704 +#include <asm/fiq.h>
45705 +#include <linux/usb.h>
45706 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
45707 +#include <../drivers/usb/core/hcd.h>
45708 +#else
45709 +#include <linux/usb/hcd.h>
45710 +#endif
45711 +#include <asm/bug.h>
45712 +
45713 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
45714 +#define USB_URB_EP_LINKING 1
45715 +#else
45716 +#define USB_URB_EP_LINKING 0
45717 +#endif
45718 +
45719 +#include "dwc_otg_hcd_if.h"
45720 +#include "dwc_otg_dbg.h"
45721 +#include "dwc_otg_driver.h"
45722 +#include "dwc_otg_hcd.h"
45723 +
45724 +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
45725 +
45726 +/**
45727 + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
45728 + * qualified with its direction (possible 32 endpoints per device).
45729 + */
45730 +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
45731 + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
45732 +
45733 +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
45734 +
45735 +extern bool fiq_enable;
45736 +
45737 +/** @name Linux HC Driver API Functions */
45738 +/** @{ */
45739 +/* manage i/o requests, device state */
45740 +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
45741 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
45742 + struct usb_host_endpoint *ep,
45743 +#endif
45744 + struct urb *urb, gfp_t mem_flags);
45745 +
45746 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
45747 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
45748 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
45749 +#endif
45750 +#else /* kernels at or post 2.6.30 */
45751 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
45752 + struct urb *urb, int status);
45753 +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
45754 +
45755 +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
45756 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
45757 +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
45758 +#endif
45759 +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
45760 +extern int hcd_start(struct usb_hcd *hcd);
45761 +extern void hcd_stop(struct usb_hcd *hcd);
45762 +static int get_frame_number(struct usb_hcd *hcd);
45763 +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
45764 +extern int hub_control(struct usb_hcd *hcd,
45765 + u16 typeReq,
45766 + u16 wValue, u16 wIndex, char *buf, u16 wLength);
45767 +
45768 +struct wrapper_priv_data {
45769 + dwc_otg_hcd_t *dwc_otg_hcd;
45770 +};
45771 +
45772 +/** @} */
45773 +
45774 +static struct hc_driver dwc_otg_hc_driver = {
45775 +
45776 + .description = dwc_otg_hcd_name,
45777 + .product_desc = "DWC OTG Controller",
45778 + .hcd_priv_size = sizeof(struct wrapper_priv_data),
45779 +
45780 + .irq = dwc_otg_hcd_irq,
45781 +
45782 + .flags = HCD_MEMORY | HCD_USB2,
45783 +
45784 + //.reset =
45785 + .start = hcd_start,
45786 + //.suspend =
45787 + //.resume =
45788 + .stop = hcd_stop,
45789 +
45790 + .urb_enqueue = dwc_otg_urb_enqueue,
45791 + .urb_dequeue = dwc_otg_urb_dequeue,
45792 + .endpoint_disable = endpoint_disable,
45793 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
45794 + .endpoint_reset = endpoint_reset,
45795 +#endif
45796 + .get_frame_number = get_frame_number,
45797 +
45798 + .hub_status_data = hub_status_data,
45799 + .hub_control = hub_control,
45800 + //.bus_suspend =
45801 + //.bus_resume =
45802 +};
45803 +
45804 +/** Gets the dwc_otg_hcd from a struct usb_hcd */
45805 +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
45806 +{
45807 + struct wrapper_priv_data *p;
45808 + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
45809 + return p->dwc_otg_hcd;
45810 +}
45811 +
45812 +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
45813 +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
45814 +{
45815 + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
45816 +}
45817 +
45818 +/** Gets the usb_host_endpoint associated with an URB. */
45819 +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
45820 +{
45821 + struct usb_device *dev = urb->dev;
45822 + int ep_num = usb_pipeendpoint(urb->pipe);
45823 +
45824 + if (usb_pipein(urb->pipe))
45825 + return dev->ep_in[ep_num];
45826 + else
45827 + return dev->ep_out[ep_num];
45828 +}
45829 +
45830 +static int _disconnect(dwc_otg_hcd_t * hcd)
45831 +{
45832 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
45833 +
45834 + usb_hcd->self.is_b_host = 0;
45835 + return 0;
45836 +}
45837 +
45838 +static int _start(dwc_otg_hcd_t * hcd)
45839 +{
45840 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
45841 +
45842 + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
45843 + hcd_start(usb_hcd);
45844 +
45845 + return 0;
45846 +}
45847 +
45848 +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
45849 + uint32_t * port_addr)
45850 +{
45851 + struct urb *urb = (struct urb *)urb_handle;
45852 + struct usb_bus *bus;
45853 +#if 1 //GRAYG - temporary
45854 + if (NULL == urb_handle)
45855 + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
45856 + if (NULL == urb->dev)
45857 + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
45858 + if (NULL == port_addr)
45859 + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
45860 +#endif
45861 + if (urb->dev->tt) {
45862 + if (NULL == urb->dev->tt->hub) {
45863 + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
45864 + __func__); //GRAYG
45865 + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
45866 + *hub_addr = 0; //GRAYG
45867 + // we probably shouldn't have a transaction translator if
45868 + // there's no associated hub?
45869 + } else {
45870 + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
45871 + if (urb->dev->tt->hub == bus->root_hub)
45872 + *hub_addr = 0;
45873 + else
45874 + *hub_addr = urb->dev->tt->hub->devnum;
45875 + }
45876 + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
45877 + } else {
45878 + *hub_addr = 0;
45879 + *port_addr = urb->dev->ttport;
45880 + }
45881 + return 0;
45882 +}
45883 +
45884 +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
45885 +{
45886 + struct urb *urb = (struct urb *)urb_handle;
45887 + return urb->dev->speed;
45888 +}
45889 +
45890 +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
45891 +{
45892 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
45893 + return usb_hcd->self.b_hnp_enable;
45894 +}
45895 +
45896 +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
45897 + struct urb *urb)
45898 +{
45899 + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
45900 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
45901 + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
45902 + } else {
45903 + hcd_to_bus(hcd)->bandwidth_int_reqs++;
45904 + }
45905 +}
45906 +
45907 +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
45908 + struct urb *urb)
45909 +{
45910 + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
45911 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
45912 + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
45913 + } else {
45914 + hcd_to_bus(hcd)->bandwidth_int_reqs--;
45915 + }
45916 +}
45917 +
45918 +/**
45919 + * Sets the final status of an URB and returns it to the device driver. Any
45920 + * required cleanup of the URB is performed. The HCD lock should be held on
45921 + * entry.
45922 + */
45923 +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
45924 + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
45925 +{
45926 + struct urb *urb = (struct urb *)urb_handle;
45927 + urb_tq_entry_t *new_entry;
45928 + int rc = 0;
45929 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
45930 + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
45931 + __func__, urb, usb_pipedevice(urb->pipe),
45932 + usb_pipeendpoint(urb->pipe),
45933 + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
45934 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
45935 + int i;
45936 + for (i = 0; i < urb->number_of_packets; i++) {
45937 + DWC_PRINTF(" ISO Desc %d status: %d\n",
45938 + i, urb->iso_frame_desc[i].status);
45939 + }
45940 + }
45941 + }
45942 + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
45943 + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
45944 + /* Convert status value. */
45945 + switch (status) {
45946 + case -DWC_E_PROTOCOL:
45947 + status = -EPROTO;
45948 + break;
45949 + case -DWC_E_IN_PROGRESS:
45950 + status = -EINPROGRESS;
45951 + break;
45952 + case -DWC_E_PIPE:
45953 + status = -EPIPE;
45954 + break;
45955 + case -DWC_E_IO:
45956 + status = -EIO;
45957 + break;
45958 + case -DWC_E_TIMEOUT:
45959 + status = -ETIMEDOUT;
45960 + break;
45961 + case -DWC_E_OVERFLOW:
45962 + status = -EOVERFLOW;
45963 + break;
45964 + case -DWC_E_SHUTDOWN:
45965 + status = -ESHUTDOWN;
45966 + break;
45967 + default:
45968 + if (status) {
45969 + DWC_PRINTF("Uknown urb status %d\n", status);
45970 +
45971 + }
45972 + }
45973 +
45974 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
45975 + int i;
45976 +
45977 + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
45978 + for (i = 0; i < urb->number_of_packets; ++i) {
45979 + urb->iso_frame_desc[i].actual_length =
45980 + dwc_otg_hcd_urb_get_iso_desc_actual_length
45981 + (dwc_otg_urb, i);
45982 + urb->iso_frame_desc[i].status =
45983 + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
45984 + }
45985 + }
45986 +
45987 + urb->status = status;
45988 + urb->hcpriv = NULL;
45989 + if (!status) {
45990 + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
45991 + (urb->actual_length < urb->transfer_buffer_length)) {
45992 + urb->status = -EREMOTEIO;
45993 + }
45994 + }
45995 +
45996 + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
45997 + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
45998 + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
45999 + if (ep) {
46000 + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
46001 + dwc_otg_hcd_get_ep_bandwidth(hcd,
46002 + ep->hcpriv),
46003 + urb);
46004 + }
46005 + }
46006 + DWC_FREE(dwc_otg_urb);
46007 + if (!new_entry) {
46008 + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
46009 + urb->status = -EPROTO;
46010 + /* don't schedule the tasklet -
46011 + * directly return the packet here with error. */
46012 +#if USB_URB_EP_LINKING
46013 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
46014 +#endif
46015 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46016 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
46017 +#else
46018 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
46019 +#endif
46020 + } else {
46021 + new_entry->urb = urb;
46022 +#if USB_URB_EP_LINKING
46023 + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
46024 + if(0 == rc) {
46025 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
46026 + }
46027 +#endif
46028 + if(0 == rc) {
46029 + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
46030 + urb_tq_entries);
46031 + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
46032 + }
46033 + }
46034 + return 0;
46035 +}
46036 +
46037 +static struct dwc_otg_hcd_function_ops hcd_fops = {
46038 + .start = _start,
46039 + .disconnect = _disconnect,
46040 + .hub_info = _hub_info,
46041 + .speed = _speed,
46042 + .complete = _complete,
46043 + .get_b_hnp_enable = _get_b_hnp_enable,
46044 +};
46045 +
46046 +static struct fiq_handler fh = {
46047 + .name = "usb_fiq",
46048 +};
46049 +
46050 +static void hcd_init_fiq(void *cookie)
46051 +{
46052 + dwc_otg_device_t *otg_dev = cookie;
46053 + dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
46054 + struct pt_regs regs;
46055 + int irq;
46056 +
46057 + if (claim_fiq(&fh)) {
46058 + DWC_ERROR("Can't claim FIQ");
46059 + BUG();
46060 + }
46061 + DWC_WARN("FIQ on core %d at 0x%08x",
46062 + smp_processor_id(),
46063 + (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
46064 + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
46065 + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
46066 + memset(&regs,0,sizeof(regs));
46067 +
46068 + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
46069 + if (fiq_fsm_enable) {
46070 + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
46071 + //regs.ARM_r10 = dwc_otg_hcd->dma;
46072 + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
46073 + } else {
46074 + regs.ARM_fp = (long) dwc_otg_fiq_nop;
46075 + }
46076 +
46077 + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
46078 +
46079 +// __show_regs(&regs);
46080 + set_fiq_regs(&regs);
46081 +
46082 + //Set the mphi periph to the required registers
46083 + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
46084 + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
46085 + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
46086 + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
46087 + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
46088 + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
46089 + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
46090 + //Enable mphi peripheral
46091 + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
46092 +#ifdef DEBUG
46093 + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
46094 + DWC_WARN("MPHI periph has been enabled");
46095 + else
46096 + DWC_WARN("MPHI periph has NOT been enabled");
46097 +#endif
46098 + // Enable FIQ interrupt from USB peripheral
46099 +#ifdef CONFIG_MULTI_IRQ_HANDLER
46100 + irq = platform_get_irq(otg_dev->os_dep.platformdev, 1);
46101 +#else
46102 + irq = INTERRUPT_VC_USB;
46103 +#endif
46104 + if (irq < 0) {
46105 + DWC_ERROR("Can't get FIQ irq");
46106 + return;
46107 + }
46108 + enable_fiq(irq);
46109 + local_fiq_enable();
46110 +}
46111 +
46112 +/**
46113 + * Initializes the HCD. This function allocates memory for and initializes the
46114 + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
46115 + * USB bus with the core and calls the hc_driver->start() function. It returns
46116 + * a negative error on failure.
46117 + */
46118 +int hcd_init(dwc_bus_dev_t *_dev)
46119 +{
46120 + struct usb_hcd *hcd = NULL;
46121 + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
46122 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
46123 + int retval = 0;
46124 + u64 dmamask;
46125 +
46126 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
46127 +
46128 + /* Set device flags indicating whether the HCD supports DMA. */
46129 + if (dwc_otg_is_dma_enable(otg_dev->core_if))
46130 + dmamask = DMA_BIT_MASK(32);
46131 + else
46132 + dmamask = 0;
46133 +
46134 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
46135 + dma_set_mask(&_dev->dev, dmamask);
46136 + dma_set_coherent_mask(&_dev->dev, dmamask);
46137 +#elif defined(PCI_INTERFACE)
46138 + pci_set_dma_mask(_dev, dmamask);
46139 + pci_set_consistent_dma_mask(_dev, dmamask);
46140 +#endif
46141 +
46142 + /*
46143 + * Allocate memory for the base HCD plus the DWC OTG HCD.
46144 + * Initialize the base HCD.
46145 + */
46146 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
46147 + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
46148 +#else
46149 + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
46150 + hcd->has_tt = 1;
46151 +// hcd->uses_new_polling = 1;
46152 +// hcd->poll_rh = 0;
46153 +#endif
46154 + if (!hcd) {
46155 + retval = -ENOMEM;
46156 + goto error1;
46157 + }
46158 +
46159 + hcd->regs = otg_dev->os_dep.base;
46160 +
46161 +
46162 + /* Initialize the DWC OTG HCD. */
46163 + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
46164 + if (!dwc_otg_hcd) {
46165 + goto error2;
46166 + }
46167 + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
46168 + dwc_otg_hcd;
46169 + otg_dev->hcd = dwc_otg_hcd;
46170 + otg_dev->hcd->otg_dev = otg_dev;
46171 +
46172 + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
46173 + goto error2;
46174 + }
46175 +
46176 + if (fiq_enable) {
46177 + if (num_online_cpus() > 1) {
46178 + /* bcm2709: can run the FIQ on a separate core to IRQs */
46179 + smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
46180 + } else {
46181 + smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
46182 + }
46183 + }
46184 +
46185 + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
46186 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
46187 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
46188 + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
46189 +#endif
46190 + /* Don't support SG list at this point */
46191 + hcd->self.sg_tablesize = 0;
46192 +#endif
46193 + /*
46194 + * Finish generic HCD initialization and start the HCD. This function
46195 + * allocates the DMA buffer pool, registers the USB bus, requests the
46196 + * IRQ line, and calls hcd_start method.
46197 + */
46198 +#ifdef PLATFORM_INTERFACE
46199 + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED);
46200 +#else
46201 + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED);
46202 +#endif
46203 + if (retval < 0) {
46204 + goto error2;
46205 + }
46206 +
46207 + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
46208 + return 0;
46209 +
46210 +error2:
46211 + usb_put_hcd(hcd);
46212 +error1:
46213 + return retval;
46214 +}
46215 +
46216 +/**
46217 + * Removes the HCD.
46218 + * Frees memory and resources associated with the HCD and deregisters the bus.
46219 + */
46220 +void hcd_remove(dwc_bus_dev_t *_dev)
46221 +{
46222 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
46223 + dwc_otg_hcd_t *dwc_otg_hcd;
46224 + struct usb_hcd *hcd;
46225 +
46226 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
46227 +
46228 + if (!otg_dev) {
46229 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
46230 + return;
46231 + }
46232 +
46233 + dwc_otg_hcd = otg_dev->hcd;
46234 +
46235 + if (!dwc_otg_hcd) {
46236 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
46237 + return;
46238 + }
46239 +
46240 + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
46241 +
46242 + if (!hcd) {
46243 + DWC_DEBUGPL(DBG_ANY,
46244 + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
46245 + __func__);
46246 + return;
46247 + }
46248 + usb_remove_hcd(hcd);
46249 + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
46250 + dwc_otg_hcd_remove(dwc_otg_hcd);
46251 + usb_put_hcd(hcd);
46252 +}
46253 +
46254 +/* =========================================================================
46255 + * Linux HC Driver Functions
46256 + * ========================================================================= */
46257 +
46258 +/** Initializes the DWC_otg controller and its root hub and prepares it for host
46259 + * mode operation. Activates the root port. Returns 0 on success and a negative
46260 + * error code on failure. */
46261 +int hcd_start(struct usb_hcd *hcd)
46262 +{
46263 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46264 + struct usb_bus *bus;
46265 +
46266 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
46267 + bus = hcd_to_bus(hcd);
46268 +
46269 + hcd->state = HC_STATE_RUNNING;
46270 + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
46271 + return 0;
46272 + }
46273 +
46274 + /* Initialize and connect root hub if one is not already attached */
46275 + if (bus->root_hub) {
46276 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
46277 + /* Inform the HUB driver to resume. */
46278 + usb_hcd_resume_root_hub(hcd);
46279 + }
46280 +
46281 + return 0;
46282 +}
46283 +
46284 +/**
46285 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
46286 + * stopped.
46287 + */
46288 +void hcd_stop(struct usb_hcd *hcd)
46289 +{
46290 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46291 +
46292 + dwc_otg_hcd_stop(dwc_otg_hcd);
46293 +}
46294 +
46295 +/** Returns the current frame number. */
46296 +static int get_frame_number(struct usb_hcd *hcd)
46297 +{
46298 + hprt0_data_t hprt0;
46299 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46300 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
46301 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
46302 + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
46303 + else
46304 + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
46305 +}
46306 +
46307 +#ifdef DEBUG
46308 +static void dump_urb_info(struct urb *urb, char *fn_name)
46309 +{
46310 + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
46311 + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
46312 + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
46313 + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
46314 + DWC_PRINTF(" Endpoint type: %s\n", ( {
46315 + char *pipetype;
46316 + switch (usb_pipetype(urb->pipe)) {
46317 +case PIPE_CONTROL:
46318 +pipetype = "CONTROL"; break; case PIPE_BULK:
46319 +pipetype = "BULK"; break; case PIPE_INTERRUPT:
46320 +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
46321 +pipetype = "ISOCHRONOUS"; break; default:
46322 + pipetype = "UNKNOWN"; break;};
46323 + pipetype;}
46324 + )) ;
46325 + DWC_PRINTF(" Speed: %s\n", ( {
46326 + char *speed; switch (urb->dev->speed) {
46327 +case USB_SPEED_HIGH:
46328 +speed = "HIGH"; break; case USB_SPEED_FULL:
46329 +speed = "FULL"; break; case USB_SPEED_LOW:
46330 +speed = "LOW"; break; default:
46331 + speed = "UNKNOWN"; break;};
46332 + speed;}
46333 + )) ;
46334 + DWC_PRINTF(" Max packet size: %d\n",
46335 + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
46336 + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
46337 + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
46338 + urb->transfer_buffer, (void *)urb->transfer_dma);
46339 + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
46340 + urb->setup_packet, (void *)urb->setup_dma);
46341 + DWC_PRINTF(" Interval: %d\n", urb->interval);
46342 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46343 + int i;
46344 + for (i = 0; i < urb->number_of_packets; i++) {
46345 + DWC_PRINTF(" ISO Desc %d:\n", i);
46346 + DWC_PRINTF(" offset: %d, length %d\n",
46347 + urb->iso_frame_desc[i].offset,
46348 + urb->iso_frame_desc[i].length);
46349 + }
46350 + }
46351 +}
46352 +#endif
46353 +
46354 +/** Starts processing a USB transfer request specified by a USB Request Block
46355 + * (URB). mem_flags indicates the type of memory allocation to use while
46356 + * processing this URB. */
46357 +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
46358 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46359 + struct usb_host_endpoint *ep,
46360 +#endif
46361 + struct urb *urb, gfp_t mem_flags)
46362 +{
46363 + int retval = 0;
46364 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
46365 + struct usb_host_endpoint *ep = urb->ep;
46366 +#endif
46367 + dwc_irqflags_t irqflags;
46368 + void **ref_ep_hcpriv = &ep->hcpriv;
46369 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46370 + dwc_otg_hcd_urb_t *dwc_otg_urb;
46371 + int i;
46372 + int alloc_bandwidth = 0;
46373 + uint8_t ep_type = 0;
46374 + uint32_t flags = 0;
46375 + void *buf;
46376 +
46377 +#ifdef DEBUG
46378 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46379 + dump_urb_info(urb, "dwc_otg_urb_enqueue");
46380 + }
46381 +#endif
46382 +
46383 + if (!urb->transfer_buffer && urb->transfer_buffer_length)
46384 + return -EINVAL;
46385 +
46386 + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
46387 + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
46388 + if (!dwc_otg_hcd_is_bandwidth_allocated
46389 + (dwc_otg_hcd, ref_ep_hcpriv)) {
46390 + alloc_bandwidth = 1;
46391 + }
46392 + }
46393 +
46394 + switch (usb_pipetype(urb->pipe)) {
46395 + case PIPE_CONTROL:
46396 + ep_type = USB_ENDPOINT_XFER_CONTROL;
46397 + break;
46398 + case PIPE_ISOCHRONOUS:
46399 + ep_type = USB_ENDPOINT_XFER_ISOC;
46400 + break;
46401 + case PIPE_BULK:
46402 + ep_type = USB_ENDPOINT_XFER_BULK;
46403 + break;
46404 + case PIPE_INTERRUPT:
46405 + ep_type = USB_ENDPOINT_XFER_INT;
46406 + break;
46407 + default:
46408 + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
46409 + }
46410 +
46411 + /* # of packets is often 0 - do we really need to call this then? */
46412 + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
46413 + urb->number_of_packets,
46414 + mem_flags == GFP_ATOMIC ? 1 : 0);
46415 +
46416 + if(dwc_otg_urb == NULL)
46417 + return -ENOMEM;
46418 +
46419 + if (!dwc_otg_urb && urb->number_of_packets)
46420 + return -ENOMEM;
46421 +
46422 + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
46423 + usb_pipeendpoint(urb->pipe), ep_type,
46424 + usb_pipein(urb->pipe),
46425 + usb_maxpacket(urb->dev, urb->pipe,
46426 + !(usb_pipein(urb->pipe))));
46427 +
46428 + buf = urb->transfer_buffer;
46429 + if (hcd->self.uses_dma && !buf && urb->transfer_buffer_length) {
46430 + /*
46431 + * Calculate virtual address from physical address,
46432 + * because some class driver may not fill transfer_buffer.
46433 + * In Buffer DMA mode virual address is used,
46434 + * when handling non DWORD aligned buffers.
46435 + */
46436 + buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
46437 + dev_warn_once(&urb->dev->dev,
46438 + "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
46439 + &urb->transfer_dma, buf);
46440 + }
46441 +
46442 + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
46443 + flags |= URB_GIVEBACK_ASAP;
46444 + if (urb->transfer_flags & URB_ZERO_PACKET)
46445 + flags |= URB_SEND_ZERO_PACKET;
46446 +
46447 + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
46448 + urb->transfer_dma,
46449 + urb->transfer_buffer_length,
46450 + urb->setup_packet,
46451 + urb->setup_dma, flags, urb->interval);
46452 +
46453 + for (i = 0; i < urb->number_of_packets; ++i) {
46454 + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
46455 + urb->
46456 + iso_frame_desc[i].offset,
46457 + urb->
46458 + iso_frame_desc[i].length);
46459 + }
46460 +
46461 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
46462 + urb->hcpriv = dwc_otg_urb;
46463 +#if USB_URB_EP_LINKING
46464 + retval = usb_hcd_link_urb_to_ep(hcd, urb);
46465 + if (0 == retval)
46466 +#endif
46467 + {
46468 + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
46469 + /*(dwc_otg_qh_t **)*/
46470 + ref_ep_hcpriv, 1);
46471 + if (0 == retval) {
46472 + if (alloc_bandwidth) {
46473 + allocate_bus_bandwidth(hcd,
46474 + dwc_otg_hcd_get_ep_bandwidth(
46475 + dwc_otg_hcd, *ref_ep_hcpriv),
46476 + urb);
46477 + }
46478 + } else {
46479 + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
46480 +#if USB_URB_EP_LINKING
46481 + usb_hcd_unlink_urb_from_ep(hcd, urb);
46482 +#endif
46483 + DWC_FREE(dwc_otg_urb);
46484 + urb->hcpriv = NULL;
46485 + if (retval == -DWC_E_NO_DEVICE)
46486 + retval = -ENODEV;
46487 + }
46488 + }
46489 +#if USB_URB_EP_LINKING
46490 + else
46491 + {
46492 + DWC_FREE(dwc_otg_urb);
46493 + urb->hcpriv = NULL;
46494 + }
46495 +#endif
46496 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
46497 + return retval;
46498 +}
46499 +
46500 +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
46501 + * success. */
46502 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46503 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
46504 +#else
46505 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
46506 +#endif
46507 +{
46508 + dwc_irqflags_t flags;
46509 + dwc_otg_hcd_t *dwc_otg_hcd;
46510 + int rc;
46511 +
46512 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
46513 +
46514 + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46515 +
46516 +#ifdef DEBUG
46517 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46518 + dump_urb_info(urb, "dwc_otg_urb_dequeue");
46519 + }
46520 +#endif
46521 +
46522 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
46523 + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
46524 + if (0 == rc) {
46525 + if(urb->hcpriv != NULL) {
46526 + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
46527 + (dwc_otg_hcd_urb_t *)urb->hcpriv);
46528 +
46529 + DWC_FREE(urb->hcpriv);
46530 + urb->hcpriv = NULL;
46531 + }
46532 + }
46533 +
46534 + if (0 == rc) {
46535 + /* Higher layer software sets URB status. */
46536 +#if USB_URB_EP_LINKING
46537 + usb_hcd_unlink_urb_from_ep(hcd, urb);
46538 +#endif
46539 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46540 +
46541 +
46542 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46543 + usb_hcd_giveback_urb(hcd, urb);
46544 +#else
46545 + usb_hcd_giveback_urb(hcd, urb, status);
46546 +#endif
46547 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46548 + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
46549 + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
46550 + }
46551 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
46552 + } else {
46553 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46554 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
46555 + rc);
46556 + }
46557 +
46558 + return rc;
46559 +}
46560 +
46561 +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
46562 + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
46563 + * must already be dequeued. */
46564 +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
46565 +{
46566 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46567 +
46568 + DWC_DEBUGPL(DBG_HCD,
46569 + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
46570 + "endpoint=%d\n", ep->desc.bEndpointAddress,
46571 + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
46572 + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
46573 + ep->hcpriv = NULL;
46574 +}
46575 +
46576 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
46577 +/* Resets endpoint specific parameter values, in current version used to reset
46578 + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
46579 +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
46580 +{
46581 + dwc_irqflags_t flags;
46582 + struct usb_device *udev = NULL;
46583 + int epnum = usb_endpoint_num(&ep->desc);
46584 + int is_out = usb_endpoint_dir_out(&ep->desc);
46585 + int is_control = usb_endpoint_xfer_control(&ep->desc);
46586 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46587 + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
46588 +
46589 + if (dev)
46590 + udev = to_usb_device(dev);
46591 + else
46592 + return;
46593 +
46594 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
46595 +
46596 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
46597 + usb_settoggle(udev, epnum, is_out, 0);
46598 + if (is_control)
46599 + usb_settoggle(udev, epnum, !is_out, 0);
46600 +
46601 + if (ep->hcpriv) {
46602 + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
46603 + }
46604 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46605 +}
46606 +#endif
46607 +
46608 +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
46609 + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
46610 + * interrupt.
46611 + *
46612 + * This function is called by the USB core when an interrupt occurs */
46613 +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
46614 +{
46615 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46616 + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
46617 + if (retval != 0) {
46618 + S3C2410X_CLEAR_EINTPEND();
46619 + }
46620 + return IRQ_RETVAL(retval);
46621 +}
46622 +
46623 +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
46624 + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
46625 + * is the status change indicator for the single root port. Returns 1 if either
46626 + * change indicator is 1, otherwise returns 0. */
46627 +int hub_status_data(struct usb_hcd *hcd, char *buf)
46628 +{
46629 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46630 +
46631 + buf[0] = 0;
46632 + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
46633 +
46634 + return (buf[0] != 0);
46635 +}
46636 +
46637 +/** Handles hub class-specific requests. */
46638 +int hub_control(struct usb_hcd *hcd,
46639 + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
46640 +{
46641 + int retval;
46642 +
46643 + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
46644 + typeReq, wValue, wIndex, buf, wLength);
46645 +
46646 + switch (retval) {
46647 + case -DWC_E_INVALID:
46648 + retval = -EINVAL;
46649 + break;
46650 + }
46651 +
46652 + return retval;
46653 +}
46654 +
46655 +#endif /* DWC_DEVICE_ONLY */
46656 --- /dev/null
46657 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
46658 @@ -0,0 +1,963 @@
46659 +/* ==========================================================================
46660 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
46661 + * $Revision: #44 $
46662 + * $Date: 2011/10/26 $
46663 + * $Change: 1873028 $
46664 + *
46665 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
46666 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
46667 + * otherwise expressly agreed to in writing between Synopsys and you.
46668 + *
46669 + * The Software IS NOT an item of Licensed Software or Licensed Product under
46670 + * any End User Software License Agreement or Agreement for Licensed Product
46671 + * with Synopsys or any supplement thereto. You are permitted to use and
46672 + * redistribute this Software in source and binary forms, with or without
46673 + * modification, provided that redistributions of source code must retain this
46674 + * notice. You may not view, use, disclose, copy or distribute this file or
46675 + * any information contained herein except pursuant to this license grant from
46676 + * Synopsys. If you do not agree with this notice, including the disclaimer
46677 + * below, then you are not authorized to use the Software.
46678 + *
46679 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
46680 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46681 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46682 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
46683 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46684 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46685 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46686 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46687 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46688 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46689 + * DAMAGE.
46690 + * ========================================================================== */
46691 +#ifndef DWC_DEVICE_ONLY
46692 +
46693 +/**
46694 + * @file
46695 + *
46696 + * This file contains the functions to manage Queue Heads and Queue
46697 + * Transfer Descriptors.
46698 + */
46699 +
46700 +#include "dwc_otg_hcd.h"
46701 +#include "dwc_otg_regs.h"
46702 +
46703 +extern bool microframe_schedule;
46704 +
46705 +/**
46706 + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
46707 + * removed from a list. QTD list should already be empty if called from URB
46708 + * Dequeue.
46709 + *
46710 + * @param hcd HCD instance.
46711 + * @param qh The QH to free.
46712 + */
46713 +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
46714 +{
46715 + dwc_otg_qtd_t *qtd, *qtd_tmp;
46716 + dwc_irqflags_t flags;
46717 + uint32_t buf_size = 0;
46718 + uint8_t *align_buf_virt = NULL;
46719 + dwc_dma_t align_buf_dma;
46720 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
46721 +
46722 + /* Free each QTD in the QTD list */
46723 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
46724 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
46725 + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
46726 + dwc_otg_hcd_qtd_free(qtd);
46727 + }
46728 +
46729 + if (hcd->core_if->dma_desc_enable) {
46730 + dwc_otg_hcd_qh_free_ddma(hcd, qh);
46731 + } else if (qh->dw_align_buf) {
46732 + if (qh->ep_type == UE_ISOCHRONOUS) {
46733 + buf_size = 4096;
46734 + } else {
46735 + buf_size = hcd->core_if->core_params->max_transfer_size;
46736 + }
46737 + align_buf_virt = qh->dw_align_buf;
46738 + align_buf_dma = qh->dw_align_buf_dma;
46739 + }
46740 +
46741 + DWC_FREE(qh);
46742 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
46743 + if (align_buf_virt)
46744 + DWC_DMA_FREE(dev, buf_size, align_buf_virt, align_buf_dma);
46745 + return;
46746 +}
46747 +
46748 +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
46749 +#define HS_HOST_DELAY 5 /* nanoseconds */
46750 +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
46751 +#define HUB_LS_SETUP 333 /* nanoseconds */
46752 +#define NS_TO_US(ns) ((ns + 500) / 1000)
46753 + /* convert & round nanoseconds to microseconds */
46754 +
46755 +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
46756 +{
46757 + unsigned long retval;
46758 +
46759 + switch (speed) {
46760 + case USB_SPEED_HIGH:
46761 + if (is_isoc) {
46762 + retval =
46763 + ((38 * 8 * 2083) +
46764 + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
46765 + HS_HOST_DELAY;
46766 + } else {
46767 + retval =
46768 + ((55 * 8 * 2083) +
46769 + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
46770 + HS_HOST_DELAY;
46771 + }
46772 + break;
46773 + case USB_SPEED_FULL:
46774 + if (is_isoc) {
46775 + retval =
46776 + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
46777 + if (is_in) {
46778 + retval = 7268 + FS_LS_HOST_DELAY + retval;
46779 + } else {
46780 + retval = 6265 + FS_LS_HOST_DELAY + retval;
46781 + }
46782 + } else {
46783 + retval =
46784 + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
46785 + retval = 9107 + FS_LS_HOST_DELAY + retval;
46786 + }
46787 + break;
46788 + case USB_SPEED_LOW:
46789 + if (is_in) {
46790 + retval =
46791 + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
46792 + 1000;
46793 + retval =
46794 + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
46795 + retval;
46796 + } else {
46797 + retval =
46798 + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
46799 + 1000;
46800 + retval =
46801 + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
46802 + retval;
46803 + }
46804 + break;
46805 + default:
46806 + DWC_WARN("Unknown device speed\n");
46807 + retval = -1;
46808 + }
46809 +
46810 + return NS_TO_US(retval);
46811 +}
46812 +
46813 +/**
46814 + * Initializes a QH structure.
46815 + *
46816 + * @param hcd The HCD state structure for the DWC OTG controller.
46817 + * @param qh The QH to init.
46818 + * @param urb Holds the information about the device/endpoint that we need
46819 + * to initialize the QH.
46820 + */
46821 +#define SCHEDULE_SLOP 10
46822 +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
46823 +{
46824 + char *speed, *type;
46825 + int dev_speed;
46826 + uint32_t hub_addr, hub_port;
46827 +
46828 + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
46829 +
46830 + /* Initialize QH */
46831 + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
46832 + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
46833 +
46834 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
46835 + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
46836 + DWC_CIRCLEQ_INIT(&qh->qtd_list);
46837 + DWC_LIST_INIT(&qh->qh_list_entry);
46838 + qh->channel = NULL;
46839 +
46840 + /* FS/LS Enpoint on HS Hub
46841 + * NOT virtual root hub */
46842 + dev_speed = hcd->fops->speed(hcd, urb->priv);
46843 +
46844 + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
46845 + qh->do_split = 0;
46846 + if (microframe_schedule)
46847 + qh->speed = dev_speed;
46848 +
46849 + qh->nak_frame = 0xffff;
46850 +
46851 + if (((dev_speed == USB_SPEED_LOW) ||
46852 + (dev_speed == USB_SPEED_FULL)) &&
46853 + (hub_addr != 0 && hub_addr != 1)) {
46854 + DWC_DEBUGPL(DBG_HCD,
46855 + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
46856 + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
46857 + hub_port);
46858 + qh->do_split = 1;
46859 + qh->skip_count = 0;
46860 + }
46861 +
46862 + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
46863 + /* Compute scheduling parameters once and save them. */
46864 + hprt0_data_t hprt;
46865 +
46866 + /** @todo Account for split transfers in the bus time. */
46867 + int bytecount =
46868 + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
46869 +
46870 + qh->usecs =
46871 + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
46872 + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
46873 + bytecount);
46874 + /* Start in a slightly future (micro)frame. */
46875 + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
46876 + SCHEDULE_SLOP);
46877 + qh->interval = urb->interval;
46878 +
46879 +#if 0
46880 + /* Increase interrupt polling rate for debugging. */
46881 + if (qh->ep_type == UE_INTERRUPT) {
46882 + qh->interval = 8;
46883 + }
46884 +#endif
46885 + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
46886 + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
46887 + ((dev_speed == USB_SPEED_LOW) ||
46888 + (dev_speed == USB_SPEED_FULL))) {
46889 + qh->interval *= 8;
46890 + qh->sched_frame |= 0x7;
46891 + qh->start_split_frame = qh->sched_frame;
46892 + }
46893 +
46894 + }
46895 +
46896 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
46897 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
46898 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
46899 + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
46900 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
46901 + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
46902 + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
46903 + switch (dev_speed) {
46904 + case USB_SPEED_LOW:
46905 + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
46906 + speed = "low";
46907 + break;
46908 + case USB_SPEED_FULL:
46909 + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
46910 + speed = "full";
46911 + break;
46912 + case USB_SPEED_HIGH:
46913 + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
46914 + speed = "high";
46915 + break;
46916 + default:
46917 + speed = "?";
46918 + break;
46919 + }
46920 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
46921 +
46922 + switch (qh->ep_type) {
46923 + case UE_ISOCHRONOUS:
46924 + type = "isochronous";
46925 + break;
46926 + case UE_INTERRUPT:
46927 + type = "interrupt";
46928 + break;
46929 + case UE_CONTROL:
46930 + type = "control";
46931 + break;
46932 + case UE_BULK:
46933 + type = "bulk";
46934 + break;
46935 + default:
46936 + type = "?";
46937 + break;
46938 + }
46939 +
46940 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
46941 +
46942 +#ifdef DEBUG
46943 + if (qh->ep_type == UE_INTERRUPT) {
46944 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
46945 + qh->usecs);
46946 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
46947 + qh->interval);
46948 + }
46949 +#endif
46950 +
46951 +}
46952 +
46953 +/**
46954 + * This function allocates and initializes a QH.
46955 + *
46956 + * @param hcd The HCD state structure for the DWC OTG controller.
46957 + * @param urb Holds the information about the device/endpoint that we need
46958 + * to initialize the QH.
46959 + * @param atomic_alloc Flag to do atomic allocation if needed
46960 + *
46961 + * @return Returns pointer to the newly allocated QH, or NULL on error. */
46962 +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
46963 + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
46964 +{
46965 + dwc_otg_qh_t *qh;
46966 +
46967 + /* Allocate memory */
46968 + /** @todo add memflags argument */
46969 + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
46970 + if (qh == NULL) {
46971 + DWC_ERROR("qh allocation failed");
46972 + return NULL;
46973 + }
46974 +
46975 + qh_init(hcd, qh, urb);
46976 +
46977 + if (hcd->core_if->dma_desc_enable
46978 + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
46979 + dwc_otg_hcd_qh_free(hcd, qh);
46980 + return NULL;
46981 + }
46982 +
46983 + return qh;
46984 +}
46985 +
46986 +/* microframe_schedule=0 start */
46987 +
46988 +/**
46989 + * Checks that a channel is available for a periodic transfer.
46990 + *
46991 + * @return 0 if successful, negative error code otherise.
46992 + */
46993 +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
46994 +{
46995 + /*
46996 + * Currently assuming that there is a dedicated host channnel for each
46997 + * periodic transaction plus at least one host channel for
46998 + * non-periodic transactions.
46999 + */
47000 + int status;
47001 + int num_channels;
47002 +
47003 + num_channels = hcd->core_if->core_params->host_channels;
47004 + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
47005 + && (hcd->periodic_channels < num_channels - 1)) {
47006 + status = 0;
47007 + } else {
47008 + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
47009 + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
47010 + status = -DWC_E_NO_SPACE;
47011 + }
47012 +
47013 + return status;
47014 +}
47015 +
47016 +/**
47017 + * Checks that there is sufficient bandwidth for the specified QH in the
47018 + * periodic schedule. For simplicity, this calculation assumes that all the
47019 + * transfers in the periodic schedule may occur in the same (micro)frame.
47020 + *
47021 + * @param hcd The HCD state structure for the DWC OTG controller.
47022 + * @param qh QH containing periodic bandwidth required.
47023 + *
47024 + * @return 0 if successful, negative error code otherwise.
47025 + */
47026 +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47027 +{
47028 + int status;
47029 + int16_t max_claimed_usecs;
47030 +
47031 + status = 0;
47032 +
47033 + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
47034 + /*
47035 + * High speed mode.
47036 + * Max periodic usecs is 80% x 125 usec = 100 usec.
47037 + */
47038 +
47039 + max_claimed_usecs = 100 - qh->usecs;
47040 + } else {
47041 + /*
47042 + * Full speed mode.
47043 + * Max periodic usecs is 90% x 1000 usec = 900 usec.
47044 + */
47045 + max_claimed_usecs = 900 - qh->usecs;
47046 + }
47047 +
47048 + if (hcd->periodic_usecs > max_claimed_usecs) {
47049 + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
47050 + status = -DWC_E_NO_SPACE;
47051 + }
47052 +
47053 + return status;
47054 +}
47055 +
47056 +/* microframe_schedule=0 end */
47057 +
47058 +/**
47059 + * Microframe scheduler
47060 + * track the total use in hcd->frame_usecs
47061 + * keep each qh use in qh->frame_usecs
47062 + * when surrendering the qh then donate the time back
47063 + */
47064 +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
47065 +
47066 +/*
47067 + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
47068 + */
47069 +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
47070 +{
47071 + int i;
47072 + for (i=0; i<8; i++) {
47073 + _hcd->frame_usecs[i] = max_uframe_usecs[i];
47074 + }
47075 + return 0;
47076 +}
47077 +
47078 +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47079 +{
47080 + int i;
47081 + unsigned short utime;
47082 + int t_left;
47083 + int ret;
47084 + int done;
47085 +
47086 + ret = -1;
47087 + utime = _qh->usecs;
47088 + t_left = utime;
47089 + i = 0;
47090 + done = 0;
47091 + while (done == 0) {
47092 + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
47093 + if (utime <= _hcd->frame_usecs[i]) {
47094 + _hcd->frame_usecs[i] -= utime;
47095 + _qh->frame_usecs[i] += utime;
47096 + t_left -= utime;
47097 + ret = i;
47098 + done = 1;
47099 + return ret;
47100 + } else {
47101 + i++;
47102 + if (i == 8) {
47103 + done = 1;
47104 + ret = -1;
47105 + }
47106 + }
47107 + }
47108 + return ret;
47109 + }
47110 +
47111 +/*
47112 + * use this for FS apps that can span multiple uframes
47113 + */
47114 +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47115 +{
47116 + int i;
47117 + int j;
47118 + unsigned short utime;
47119 + int t_left;
47120 + int ret;
47121 + int done;
47122 + unsigned short xtime;
47123 +
47124 + ret = -1;
47125 + utime = _qh->usecs;
47126 + t_left = utime;
47127 + i = 0;
47128 + done = 0;
47129 +loop:
47130 + while (done == 0) {
47131 + if(_hcd->frame_usecs[i] <= 0) {
47132 + i++;
47133 + if (i == 8) {
47134 + done = 1;
47135 + ret = -1;
47136 + }
47137 + goto loop;
47138 + }
47139 +
47140 + /*
47141 + * we need n consecutive slots
47142 + * so use j as a start slot j plus j+1 must be enough time (for now)
47143 + */
47144 + xtime= _hcd->frame_usecs[i];
47145 + for (j = i+1 ; j < 8 ; j++ ) {
47146 + /*
47147 + * if we add this frame remaining time to xtime we may
47148 + * be OK, if not we need to test j for a complete frame
47149 + */
47150 + if ((xtime+_hcd->frame_usecs[j]) < utime) {
47151 + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
47152 + j = 8;
47153 + ret = -1;
47154 + continue;
47155 + }
47156 + }
47157 + if (xtime >= utime) {
47158 + ret = i;
47159 + j = 8; /* stop loop with a good value ret */
47160 + continue;
47161 + }
47162 + /* add the frame time to x time */
47163 + xtime += _hcd->frame_usecs[j];
47164 + /* we must have a fully available next frame or break */
47165 + if ((xtime < utime)
47166 + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
47167 + ret = -1;
47168 + j = 8; /* stop loop with a bad value ret */
47169 + continue;
47170 + }
47171 + }
47172 + if (ret >= 0) {
47173 + t_left = utime;
47174 + for (j = i; (t_left>0) && (j < 8); j++ ) {
47175 + t_left -= _hcd->frame_usecs[j];
47176 + if ( t_left <= 0 ) {
47177 + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
47178 + _hcd->frame_usecs[j]= -t_left;
47179 + ret = i;
47180 + done = 1;
47181 + } else {
47182 + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
47183 + _hcd->frame_usecs[j] = 0;
47184 + }
47185 + }
47186 + } else {
47187 + i++;
47188 + if (i == 8) {
47189 + done = 1;
47190 + ret = -1;
47191 + }
47192 + }
47193 + }
47194 + return ret;
47195 +}
47196 +
47197 +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47198 +{
47199 + int ret;
47200 + ret = -1;
47201 +
47202 + if (_qh->speed == USB_SPEED_HIGH) {
47203 + /* if this is a hs transaction we need a full frame */
47204 + ret = find_single_uframe(_hcd, _qh);
47205 + } else {
47206 + /* if this is a fs transaction we may need a sequence of frames */
47207 + ret = find_multi_uframe(_hcd, _qh);
47208 + }
47209 + return ret;
47210 +}
47211 +
47212 +/**
47213 + * Checks that the max transfer size allowed in a host channel is large enough
47214 + * to handle the maximum data transfer in a single (micro)frame for a periodic
47215 + * transfer.
47216 + *
47217 + * @param hcd The HCD state structure for the DWC OTG controller.
47218 + * @param qh QH for a periodic endpoint.
47219 + *
47220 + * @return 0 if successful, negative error code otherwise.
47221 + */
47222 +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47223 +{
47224 + int status;
47225 + uint32_t max_xfer_size;
47226 + uint32_t max_channel_xfer_size;
47227 +
47228 + status = 0;
47229 +
47230 + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
47231 + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
47232 +
47233 + if (max_xfer_size > max_channel_xfer_size) {
47234 + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
47235 + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
47236 + status = -DWC_E_NO_SPACE;
47237 + }
47238 +
47239 + return status;
47240 +}
47241 +
47242 +
47243 +
47244 +/**
47245 + * Schedules an interrupt or isochronous transfer in the periodic schedule.
47246 + *
47247 + * @param hcd The HCD state structure for the DWC OTG controller.
47248 + * @param qh QH for the periodic transfer. The QH should already contain the
47249 + * scheduling information.
47250 + *
47251 + * @return 0 if successful, negative error code otherwise.
47252 + */
47253 +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47254 +{
47255 + int status = 0;
47256 +
47257 + if (microframe_schedule) {
47258 + int frame;
47259 + status = find_uframe(hcd, qh);
47260 + frame = -1;
47261 + if (status == 0) {
47262 + frame = 7;
47263 + } else {
47264 + if (status > 0 )
47265 + frame = status-1;
47266 + }
47267 +
47268 + /* Set the new frame up */
47269 + if (frame > -1) {
47270 + qh->sched_frame &= ~0x7;
47271 + qh->sched_frame |= (frame & 7);
47272 + }
47273 +
47274 + if (status != -1)
47275 + status = 0;
47276 + } else {
47277 + status = periodic_channel_available(hcd);
47278 + if (status) {
47279 + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
47280 + return status;
47281 + }
47282 +
47283 + status = check_periodic_bandwidth(hcd, qh);
47284 + }
47285 + if (status) {
47286 + DWC_INFO("%s: Insufficient periodic bandwidth for "
47287 + "periodic transfer.\n", __func__);
47288 + return status;
47289 + }
47290 + status = check_max_xfer_size(hcd, qh);
47291 + if (status) {
47292 + DWC_INFO("%s: Channel max transfer size too small "
47293 + "for periodic transfer.\n", __func__);
47294 + return status;
47295 + }
47296 +
47297 + if (hcd->core_if->dma_desc_enable) {
47298 + /* Don't rely on SOF and start in ready schedule */
47299 + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
47300 + }
47301 + else {
47302 + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
47303 + {
47304 + hcd->fiq_state->next_sched_frame = qh->sched_frame;
47305 +
47306 + }
47307 + /* Always start in the inactive schedule. */
47308 + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
47309 + }
47310 +
47311 + if (!microframe_schedule) {
47312 + /* Reserve the periodic channel. */
47313 + hcd->periodic_channels++;
47314 + }
47315 +
47316 + /* Update claimed usecs per (micro)frame. */
47317 + hcd->periodic_usecs += qh->usecs;
47318 +
47319 + return status;
47320 +}
47321 +
47322 +
47323 +/**
47324 + * This function adds a QH to either the non periodic or periodic schedule if
47325 + * it is not already in the schedule. If the QH is already in the schedule, no
47326 + * action is taken.
47327 + *
47328 + * @return 0 if successful, negative error code otherwise.
47329 + */
47330 +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47331 +{
47332 + int status = 0;
47333 + gintmsk_data_t intr_mask = {.d32 = 0 };
47334 +
47335 + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
47336 + /* QH already in a schedule. */
47337 + return status;
47338 + }
47339 +
47340 + /* Add the new QH to the appropriate schedule */
47341 + if (dwc_qh_is_non_per(qh)) {
47342 + /* Always start in the inactive schedule. */
47343 + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
47344 + &qh->qh_list_entry);
47345 + //hcd->fiq_state->kick_np_queues = 1;
47346 + } else {
47347 + status = schedule_periodic(hcd, qh);
47348 + if ( !hcd->periodic_qh_count ) {
47349 + intr_mask.b.sofintr = 1;
47350 + if (fiq_enable) {
47351 + local_fiq_disable();
47352 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
47353 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
47354 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
47355 + local_fiq_enable();
47356 + } else {
47357 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
47358 + }
47359 + }
47360 + hcd->periodic_qh_count++;
47361 + }
47362 +
47363 + return status;
47364 +}
47365 +
47366 +/**
47367 + * Removes an interrupt or isochronous transfer from the periodic schedule.
47368 + *
47369 + * @param hcd The HCD state structure for the DWC OTG controller.
47370 + * @param qh QH for the periodic transfer.
47371 + */
47372 +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47373 +{
47374 + int i;
47375 + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
47376 +
47377 + /* Update claimed usecs per (micro)frame. */
47378 + hcd->periodic_usecs -= qh->usecs;
47379 +
47380 + if (!microframe_schedule) {
47381 + /* Release the periodic channel reservation. */
47382 + hcd->periodic_channels--;
47383 + } else {
47384 + for (i = 0; i < 8; i++) {
47385 + hcd->frame_usecs[i] += qh->frame_usecs[i];
47386 + qh->frame_usecs[i] = 0;
47387 + }
47388 + }
47389 +}
47390 +
47391 +/**
47392 + * Removes a QH from either the non-periodic or periodic schedule. Memory is
47393 + * not freed.
47394 + *
47395 + * @param hcd The HCD state structure.
47396 + * @param qh QH to remove from schedule. */
47397 +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47398 +{
47399 + gintmsk_data_t intr_mask = {.d32 = 0 };
47400 +
47401 + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
47402 + /* QH is not in a schedule. */
47403 + return;
47404 + }
47405 +
47406 + if (dwc_qh_is_non_per(qh)) {
47407 + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
47408 + hcd->non_periodic_qh_ptr =
47409 + hcd->non_periodic_qh_ptr->next;
47410 + }
47411 + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
47412 + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
47413 + // hcd->fiq_state->kick_np_queues = 1;
47414 + } else {
47415 + deschedule_periodic(hcd, qh);
47416 + hcd->periodic_qh_count--;
47417 + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
47418 + intr_mask.b.sofintr = 1;
47419 + if (fiq_enable) {
47420 + local_fiq_disable();
47421 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
47422 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
47423 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
47424 + local_fiq_enable();
47425 + } else {
47426 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
47427 + }
47428 + }
47429 + }
47430 +}
47431 +
47432 +/**
47433 + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
47434 + * non-periodic schedule. The QH is added to the inactive non-periodic
47435 + * schedule if any QTDs are still attached to the QH.
47436 + *
47437 + * For periodic QHs, the QH is removed from the periodic queued schedule. If
47438 + * there are any QTDs still attached to the QH, the QH is added to either the
47439 + * periodic inactive schedule or the periodic ready schedule and its next
47440 + * scheduled frame is calculated. The QH is placed in the ready schedule if
47441 + * the scheduled frame has been reached already. Otherwise it's placed in the
47442 + * inactive schedule. If there are no QTDs attached to the QH, the QH is
47443 + * completely removed from the periodic schedule.
47444 + */
47445 +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
47446 + int sched_next_periodic_split)
47447 +{
47448 + if (dwc_qh_is_non_per(qh)) {
47449 + dwc_otg_hcd_qh_remove(hcd, qh);
47450 + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
47451 + /* Add back to inactive non-periodic schedule. */
47452 + dwc_otg_hcd_qh_add(hcd, qh);
47453 + //hcd->fiq_state->kick_np_queues = 1;
47454 + }
47455 + } else {
47456 + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
47457 +
47458 + if (qh->do_split) {
47459 + /* Schedule the next continuing periodic split transfer */
47460 + if (sched_next_periodic_split) {
47461 +
47462 + qh->sched_frame = frame_number;
47463 +
47464 + if (dwc_frame_num_le(frame_number,
47465 + dwc_frame_num_inc
47466 + (qh->start_split_frame,
47467 + 1))) {
47468 + /*
47469 + * Allow one frame to elapse after start
47470 + * split microframe before scheduling
47471 + * complete split, but DONT if we are
47472 + * doing the next start split in the
47473 + * same frame for an ISOC out.
47474 + */
47475 + if ((qh->ep_type != UE_ISOCHRONOUS) ||
47476 + (qh->ep_is_in != 0)) {
47477 + qh->sched_frame =
47478 + dwc_frame_num_inc(qh->sched_frame, 1);
47479 + }
47480 + }
47481 + } else {
47482 + qh->sched_frame =
47483 + dwc_frame_num_inc(qh->start_split_frame,
47484 + qh->interval);
47485 + if (dwc_frame_num_le
47486 + (qh->sched_frame, frame_number)) {
47487 + qh->sched_frame = frame_number;
47488 + }
47489 + qh->sched_frame |= 0x7;
47490 + qh->start_split_frame = qh->sched_frame;
47491 + }
47492 + } else {
47493 + qh->sched_frame =
47494 + dwc_frame_num_inc(qh->sched_frame, qh->interval);
47495 + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
47496 + qh->sched_frame = frame_number;
47497 + }
47498 + }
47499 +
47500 + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
47501 + dwc_otg_hcd_qh_remove(hcd, qh);
47502 + } else {
47503 + /*
47504 + * Remove from periodic_sched_queued and move to
47505 + * appropriate queue.
47506 + */
47507 + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
47508 + (!microframe_schedule && qh->sched_frame == frame_number)) {
47509 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
47510 + &qh->qh_list_entry);
47511 + } else {
47512 + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
47513 + {
47514 + hcd->fiq_state->next_sched_frame = qh->sched_frame;
47515 + }
47516 +
47517 + DWC_LIST_MOVE_HEAD
47518 + (&hcd->periodic_sched_inactive,
47519 + &qh->qh_list_entry);
47520 + }
47521 + }
47522 + }
47523 +}
47524 +
47525 +/**
47526 + * This function allocates and initializes a QTD.
47527 + *
47528 + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
47529 + * pointing to each other so each pair should have a unique correlation.
47530 + * @param atomic_alloc Flag to do atomic alloc if needed
47531 + *
47532 + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
47533 +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
47534 +{
47535 + dwc_otg_qtd_t *qtd;
47536 +
47537 + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
47538 + if (qtd == NULL) {
47539 + return NULL;
47540 + }
47541 +
47542 + dwc_otg_hcd_qtd_init(qtd, urb);
47543 + return qtd;
47544 +}
47545 +
47546 +/**
47547 + * Initializes a QTD structure.
47548 + *
47549 + * @param qtd The QTD to initialize.
47550 + * @param urb The URB to use for initialization. */
47551 +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
47552 +{
47553 + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
47554 + qtd->urb = urb;
47555 + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
47556 + /*
47557 + * The only time the QTD data toggle is used is on the data
47558 + * phase of control transfers. This phase always starts with
47559 + * DATA1.
47560 + */
47561 + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
47562 + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
47563 + }
47564 +
47565 + /* start split */
47566 + qtd->complete_split = 0;
47567 + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
47568 + qtd->isoc_split_offset = 0;
47569 + qtd->in_process = 0;
47570 +
47571 + /* Store the qtd ptr in the urb to reference what QTD. */
47572 + urb->qtd = qtd;
47573 + return;
47574 +}
47575 +
47576 +/**
47577 + * This function adds a QTD to the QTD-list of a QH. It will find the correct
47578 + * QH to place the QTD into. If it does not find a QH, then it will create a
47579 + * new QH. If the QH to which the QTD is added is not currently scheduled, it
47580 + * is placed into the proper schedule based on its EP type.
47581 + * HCD lock must be held and interrupts must be disabled on entry
47582 + *
47583 + * @param[in] qtd The QTD to add
47584 + * @param[in] hcd The DWC HCD structure
47585 + * @param[out] qh out parameter to return queue head
47586 + * @param atomic_alloc Flag to do atomic alloc if needed
47587 + *
47588 + * @return 0 if successful, negative error code otherwise.
47589 + */
47590 +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
47591 + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
47592 +{
47593 + int retval = 0;
47594 + dwc_otg_hcd_urb_t *urb = qtd->urb;
47595 +
47596 + /*
47597 + * Get the QH which holds the QTD-list to insert to. Create QH if it
47598 + * doesn't exist.
47599 + */
47600 + if (*qh == NULL) {
47601 + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
47602 + if (*qh == NULL) {
47603 + retval = -DWC_E_NO_MEMORY;
47604 + goto done;
47605 + } else {
47606 + if (fiq_enable)
47607 + hcd->fiq_state->kick_np_queues = 1;
47608 + }
47609 + }
47610 + retval = dwc_otg_hcd_qh_add(hcd, *qh);
47611 + if (retval == 0) {
47612 + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
47613 + qtd_list_entry);
47614 + qtd->qh = *qh;
47615 + }
47616 +done:
47617 +
47618 + return retval;
47619 +}
47620 +
47621 +#endif /* DWC_DEVICE_ONLY */
47622 --- /dev/null
47623 +++ b/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
47624 @@ -0,0 +1,188 @@
47625 +#ifndef _DWC_OS_DEP_H_
47626 +#define _DWC_OS_DEP_H_
47627 +
47628 +/**
47629 + * @file
47630 + *
47631 + * This file contains OS dependent structures.
47632 + *
47633 + */
47634 +
47635 +#include <linux/kernel.h>
47636 +#include <linux/module.h>
47637 +#include <linux/moduleparam.h>
47638 +#include <linux/init.h>
47639 +#include <linux/device.h>
47640 +#include <linux/errno.h>
47641 +#include <linux/types.h>
47642 +#include <linux/slab.h>
47643 +#include <linux/list.h>
47644 +#include <linux/interrupt.h>
47645 +#include <linux/ctype.h>
47646 +#include <linux/string.h>
47647 +#include <linux/dma-mapping.h>
47648 +#include <linux/jiffies.h>
47649 +#include <linux/delay.h>
47650 +#include <linux/timer.h>
47651 +#include <linux/workqueue.h>
47652 +#include <linux/stat.h>
47653 +#include <linux/pci.h>
47654 +
47655 +#include <linux/version.h>
47656 +
47657 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
47658 +# include <linux/irq.h>
47659 +#endif
47660 +
47661 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
47662 +# include <linux/usb/ch9.h>
47663 +#else
47664 +# include <linux/usb_ch9.h>
47665 +#endif
47666 +
47667 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
47668 +# include <linux/usb/gadget.h>
47669 +#else
47670 +# include <linux/usb_gadget.h>
47671 +#endif
47672 +
47673 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
47674 +# include <asm/irq.h>
47675 +#endif
47676 +
47677 +#ifdef PCI_INTERFACE
47678 +# include <asm/io.h>
47679 +#endif
47680 +
47681 +#ifdef LM_INTERFACE
47682 +# include <asm/unaligned.h>
47683 +# include <asm/sizes.h>
47684 +# include <asm/param.h>
47685 +# include <asm/io.h>
47686 +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
47687 +# include <asm/arch/hardware.h>
47688 +# include <asm/arch/lm.h>
47689 +# include <asm/arch/irqs.h>
47690 +# include <asm/arch/regs-irq.h>
47691 +# else
47692 +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
47693 + here we assume that the machine architecture provides definitions
47694 + in its own header
47695 +*/
47696 +# include <mach/lm.h>
47697 +# include <mach/hardware.h>
47698 +# endif
47699 +#endif
47700 +
47701 +#ifdef PLATFORM_INTERFACE
47702 +#include <linux/platform_device.h>
47703 +#include <asm/mach/map.h>
47704 +#endif
47705 +
47706 +/** The OS page size */
47707 +#define DWC_OS_PAGE_SIZE PAGE_SIZE
47708 +
47709 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
47710 +typedef int gfp_t;
47711 +#endif
47712 +
47713 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
47714 +# define IRQF_SHARED SA_SHIRQ
47715 +#endif
47716 +
47717 +typedef struct os_dependent {
47718 + /** Base address returned from ioremap() */
47719 + void *base;
47720 +
47721 + /** Register offset for Diagnostic API */
47722 + uint32_t reg_offset;
47723 +
47724 + /** Base address for MPHI peripheral */
47725 + void *mphi_base;
47726 +
47727 +#ifdef LM_INTERFACE
47728 + struct lm_device *lmdev;
47729 +#elif defined(PCI_INTERFACE)
47730 + struct pci_dev *pcidev;
47731 +
47732 + /** Start address of a PCI region */
47733 + resource_size_t rsrc_start;
47734 +
47735 + /** Length address of a PCI region */
47736 + resource_size_t rsrc_len;
47737 +#elif defined(PLATFORM_INTERFACE)
47738 + struct platform_device *platformdev;
47739 +#endif
47740 +
47741 +} os_dependent_t;
47742 +
47743 +#ifdef __cplusplus
47744 +}
47745 +#endif
47746 +
47747 +
47748 +
47749 +/* Type for the our device on the chosen bus */
47750 +#if defined(LM_INTERFACE)
47751 +typedef struct lm_device dwc_bus_dev_t;
47752 +#elif defined(PCI_INTERFACE)
47753 +typedef struct pci_dev dwc_bus_dev_t;
47754 +#elif defined(PLATFORM_INTERFACE)
47755 +typedef struct platform_device dwc_bus_dev_t;
47756 +#endif
47757 +
47758 +/* Helper macro to retrieve drvdata from the device on the chosen bus */
47759 +#if defined(LM_INTERFACE)
47760 +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
47761 +#elif defined(PCI_INTERFACE)
47762 +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
47763 +#elif defined(PLATFORM_INTERFACE)
47764 +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
47765 +#endif
47766 +
47767 +/**
47768 + * Helper macro returning the otg_device structure of a given struct device
47769 + *
47770 + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
47771 + */
47772 +#ifdef LM_INTERFACE
47773 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
47774 + struct lm_device *lm_dev = \
47775 + container_of(_dev, struct lm_device, dev); \
47776 + _var = lm_get_drvdata(lm_dev); \
47777 + } while (0)
47778 +
47779 +#elif defined(PCI_INTERFACE)
47780 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
47781 + _var = dev_get_drvdata(_dev); \
47782 + } while (0)
47783 +
47784 +#elif defined(PLATFORM_INTERFACE)
47785 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
47786 + struct platform_device *platform_dev = \
47787 + container_of(_dev, struct platform_device, dev); \
47788 + _var = platform_get_drvdata(platform_dev); \
47789 + } while (0)
47790 +#endif
47791 +
47792 +
47793 +/**
47794 + * Helper macro returning the struct dev of the given struct os_dependent
47795 + *
47796 + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
47797 + */
47798 +#ifdef LM_INTERFACE
47799 +#define DWC_OTG_OS_GETDEV(_osdep) \
47800 + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
47801 +#elif defined(PCI_INTERFACE)
47802 +#define DWC_OTG_OS_GETDEV(_osdep) \
47803 + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
47804 +#elif defined(PLATFORM_INTERFACE)
47805 +#define DWC_OTG_OS_GETDEV(_osdep) \
47806 + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
47807 +#endif
47808 +
47809 +
47810 +
47811 +
47812 +#endif /* _DWC_OS_DEP_H_ */
47813 --- /dev/null
47814 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
47815 @@ -0,0 +1,2725 @@
47816 +/* ==========================================================================
47817 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
47818 + * $Revision: #101 $
47819 + * $Date: 2012/08/10 $
47820 + * $Change: 2047372 $
47821 + *
47822 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
47823 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
47824 + * otherwise expressly agreed to in writing between Synopsys and you.
47825 + *
47826 + * The Software IS NOT an item of Licensed Software or Licensed Product under
47827 + * any End User Software License Agreement or Agreement for Licensed Product
47828 + * with Synopsys or any supplement thereto. You are permitted to use and
47829 + * redistribute this Software in source and binary forms, with or without
47830 + * modification, provided that redistributions of source code must retain this
47831 + * notice. You may not view, use, disclose, copy or distribute this file or
47832 + * any information contained herein except pursuant to this license grant from
47833 + * Synopsys. If you do not agree with this notice, including the disclaimer
47834 + * below, then you are not authorized to use the Software.
47835 + *
47836 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
47837 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47838 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47839 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
47840 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47841 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47842 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47843 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47844 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47845 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
47846 + * DAMAGE.
47847 + * ========================================================================== */
47848 +#ifndef DWC_HOST_ONLY
47849 +
47850 +/** @file
47851 + * This file implements PCD Core. All code in this file is portable and doesn't
47852 + * use any OS specific functions.
47853 + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
47854 + * header file, which can be used to implement OS specific PCD interface.
47855 + *
47856 + * An important function of the PCD is managing interrupts generated
47857 + * by the DWC_otg controller. The implementation of the DWC_otg device
47858 + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
47859 + *
47860 + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
47861 + * @todo Does it work when the request size is greater than DEPTSIZ
47862 + * transfer size
47863 + *
47864 + */
47865 +
47866 +#include "dwc_otg_pcd.h"
47867 +
47868 +#ifdef DWC_UTE_CFI
47869 +#include "dwc_otg_cfi.h"
47870 +
47871 +extern int init_cfi(cfiobject_t * cfiobj);
47872 +#endif
47873 +
47874 +/**
47875 + * Choose endpoint from ep arrays using usb_ep structure.
47876 + */
47877 +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
47878 +{
47879 + int i;
47880 + if (pcd->ep0.priv == handle) {
47881 + return &pcd->ep0;
47882 + }
47883 + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
47884 + if (pcd->in_ep[i].priv == handle)
47885 + return &pcd->in_ep[i];
47886 + if (pcd->out_ep[i].priv == handle)
47887 + return &pcd->out_ep[i];
47888 + }
47889 +
47890 + return NULL;
47891 +}
47892 +
47893 +/**
47894 + * This function completes a request. It call's the request call back.
47895 + */
47896 +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
47897 + int32_t status)
47898 +{
47899 + unsigned stopped = ep->stopped;
47900 +
47901 + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
47902 + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
47903 +
47904 + /* don't modify queue heads during completion callback */
47905 + ep->stopped = 1;
47906 + /* spin_unlock/spin_lock now done in fops->complete() */
47907 + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
47908 + req->actual);
47909 +
47910 + if (ep->pcd->request_pending > 0) {
47911 + --ep->pcd->request_pending;
47912 + }
47913 +
47914 + ep->stopped = stopped;
47915 + DWC_FREE(req);
47916 +}
47917 +
47918 +/**
47919 + * This function terminates all the requsts in the EP request queue.
47920 + */
47921 +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
47922 +{
47923 + dwc_otg_pcd_request_t *req;
47924 +
47925 + ep->stopped = 1;
47926 +
47927 + /* called with irqs blocked?? */
47928 + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
47929 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
47930 + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
47931 + }
47932 +}
47933 +
47934 +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
47935 + const struct dwc_otg_pcd_function_ops *fops)
47936 +{
47937 + pcd->fops = fops;
47938 +}
47939 +
47940 +/**
47941 + * PCD Callback function for initializing the PCD when switching to
47942 + * device mode.
47943 + *
47944 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
47945 + */
47946 +static int32_t dwc_otg_pcd_start_cb(void *p)
47947 +{
47948 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
47949 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
47950 +
47951 + /*
47952 + * Initialized the Core for Device mode.
47953 + */
47954 + if (dwc_otg_is_device_mode(core_if)) {
47955 + dwc_otg_core_dev_init(core_if);
47956 + /* Set core_if's lock pointer to the pcd->lock */
47957 + core_if->lock = pcd->lock;
47958 + }
47959 + return 1;
47960 +}
47961 +
47962 +/** CFI-specific buffer allocation function for EP */
47963 +#ifdef DWC_UTE_CFI
47964 +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
47965 + size_t buflen, int flags)
47966 +{
47967 + dwc_otg_pcd_ep_t *ep;
47968 + ep = get_ep_from_handle(pcd, pep);
47969 + if (!ep) {
47970 + DWC_WARN("bad ep\n");
47971 + return -DWC_E_INVALID;
47972 + }
47973 +
47974 + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
47975 + flags);
47976 +}
47977 +#else
47978 +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
47979 + size_t buflen, int flags);
47980 +#endif
47981 +
47982 +/**
47983 + * PCD Callback function for notifying the PCD when resuming from
47984 + * suspend.
47985 + *
47986 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
47987 + */
47988 +static int32_t dwc_otg_pcd_resume_cb(void *p)
47989 +{
47990 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
47991 +
47992 + if (pcd->fops->resume) {
47993 + pcd->fops->resume(pcd);
47994 + }
47995 +
47996 + /* Stop the SRP timeout timer. */
47997 + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
47998 + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
47999 + if (GET_CORE_IF(pcd)->srp_timer_started) {
48000 + GET_CORE_IF(pcd)->srp_timer_started = 0;
48001 + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
48002 + }
48003 + }
48004 + return 1;
48005 +}
48006 +
48007 +/**
48008 + * PCD Callback function for notifying the PCD device is suspended.
48009 + *
48010 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48011 + */
48012 +static int32_t dwc_otg_pcd_suspend_cb(void *p)
48013 +{
48014 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48015 +
48016 + if (pcd->fops->suspend) {
48017 + DWC_SPINUNLOCK(pcd->lock);
48018 + pcd->fops->suspend(pcd);
48019 + DWC_SPINLOCK(pcd->lock);
48020 + }
48021 +
48022 + return 1;
48023 +}
48024 +
48025 +/**
48026 + * PCD Callback function for stopping the PCD when switching to Host
48027 + * mode.
48028 + *
48029 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48030 + */
48031 +static int32_t dwc_otg_pcd_stop_cb(void *p)
48032 +{
48033 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48034 + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
48035 +
48036 + dwc_otg_pcd_stop(pcd);
48037 + return 1;
48038 +}
48039 +
48040 +/**
48041 + * PCD Callback structure for handling mode switching.
48042 + */
48043 +static dwc_otg_cil_callbacks_t pcd_callbacks = {
48044 + .start = dwc_otg_pcd_start_cb,
48045 + .stop = dwc_otg_pcd_stop_cb,
48046 + .suspend = dwc_otg_pcd_suspend_cb,
48047 + .resume_wakeup = dwc_otg_pcd_resume_cb,
48048 + .p = 0, /* Set at registration */
48049 +};
48050 +
48051 +/**
48052 + * This function allocates a DMA Descriptor chain for the Endpoint
48053 + * buffer to be used for a transfer to/from the specified endpoint.
48054 + */
48055 +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(struct device *dev,
48056 + dwc_dma_t * dma_desc_addr,
48057 + uint32_t count)
48058 +{
48059 + return DWC_DMA_ALLOC_ATOMIC(dev, count * sizeof(dwc_otg_dev_dma_desc_t),
48060 + dma_desc_addr);
48061 +}
48062 +
48063 +/**
48064 + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
48065 + */
48066 +void dwc_otg_ep_free_desc_chain(struct device *dev,
48067 + dwc_otg_dev_dma_desc_t * desc_addr,
48068 + uint32_t dma_desc_addr, uint32_t count)
48069 +{
48070 + DWC_DMA_FREE(dev, count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
48071 + dma_desc_addr);
48072 +}
48073 +
48074 +#ifdef DWC_EN_ISOC
48075 +
48076 +/**
48077 + * This function initializes a descriptor chain for Isochronous transfer
48078 + *
48079 + * @param core_if Programming view of DWC_otg controller.
48080 + * @param dwc_ep The EP to start the transfer on.
48081 + *
48082 + */
48083 +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
48084 + dwc_ep_t * dwc_ep)
48085 +{
48086 +
48087 + dsts_data_t dsts = {.d32 = 0 };
48088 + depctl_data_t depctl = {.d32 = 0 };
48089 + volatile uint32_t *addr;
48090 + int i, j;
48091 + uint32_t len;
48092 +
48093 + if (dwc_ep->is_in)
48094 + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
48095 + else
48096 + dwc_ep->desc_cnt =
48097 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48098 + dwc_ep->bInterval;
48099 +
48100 + /** Allocate descriptors for double buffering */
48101 + dwc_ep->iso_desc_addr =
48102 + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
48103 + dwc_ep->desc_cnt * 2);
48104 + if (dwc_ep->desc_addr) {
48105 + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
48106 + return;
48107 + }
48108 +
48109 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48110 +
48111 + /** ISO OUT EP */
48112 + if (dwc_ep->is_in == 0) {
48113 + dev_dma_desc_sts_t sts = {.d32 = 0 };
48114 + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
48115 + dma_addr_t dma_ad;
48116 + uint32_t data_per_desc;
48117 + dwc_otg_dev_out_ep_regs_t *out_regs =
48118 + core_if->dev_if->out_ep_regs[dwc_ep->num];
48119 + int offset;
48120 +
48121 + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
48122 + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
48123 +
48124 + /** Buffer 0 descriptors setup */
48125 + dma_ad = dwc_ep->dma_addr0;
48126 +
48127 + sts.b_iso_out.bs = BS_HOST_READY;
48128 + sts.b_iso_out.rxsts = 0;
48129 + sts.b_iso_out.l = 0;
48130 + sts.b_iso_out.sp = 0;
48131 + sts.b_iso_out.ioc = 0;
48132 + sts.b_iso_out.pid = 0;
48133 + sts.b_iso_out.framenum = 0;
48134 +
48135 + offset = 0;
48136 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48137 + i += dwc_ep->pkt_per_frm) {
48138 +
48139 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
48140 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48141 + if (len > dwc_ep->data_per_frame)
48142 + data_per_desc =
48143 + dwc_ep->data_per_frame -
48144 + j * dwc_ep->maxpacket;
48145 + else
48146 + data_per_desc = dwc_ep->maxpacket;
48147 + len = data_per_desc % 4;
48148 + if (len)
48149 + data_per_desc += 4 - len;
48150 +
48151 + sts.b_iso_out.rxbytes = data_per_desc;
48152 + dma_desc->buf = dma_ad;
48153 + dma_desc->status.d32 = sts.d32;
48154 +
48155 + offset += data_per_desc;
48156 + dma_desc++;
48157 + dma_ad += data_per_desc;
48158 + }
48159 + }
48160 +
48161 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
48162 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48163 + if (len > dwc_ep->data_per_frame)
48164 + data_per_desc =
48165 + dwc_ep->data_per_frame -
48166 + j * dwc_ep->maxpacket;
48167 + else
48168 + data_per_desc = dwc_ep->maxpacket;
48169 + len = data_per_desc % 4;
48170 + if (len)
48171 + data_per_desc += 4 - len;
48172 + sts.b_iso_out.rxbytes = data_per_desc;
48173 + dma_desc->buf = dma_ad;
48174 + dma_desc->status.d32 = sts.d32;
48175 +
48176 + offset += data_per_desc;
48177 + dma_desc++;
48178 + dma_ad += data_per_desc;
48179 + }
48180 +
48181 + sts.b_iso_out.ioc = 1;
48182 + len = (j + 1) * dwc_ep->maxpacket;
48183 + if (len > dwc_ep->data_per_frame)
48184 + data_per_desc =
48185 + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
48186 + else
48187 + data_per_desc = dwc_ep->maxpacket;
48188 + len = data_per_desc % 4;
48189 + if (len)
48190 + data_per_desc += 4 - len;
48191 + sts.b_iso_out.rxbytes = data_per_desc;
48192 +
48193 + dma_desc->buf = dma_ad;
48194 + dma_desc->status.d32 = sts.d32;
48195 + dma_desc++;
48196 +
48197 + /** Buffer 1 descriptors setup */
48198 + sts.b_iso_out.ioc = 0;
48199 + dma_ad = dwc_ep->dma_addr1;
48200 +
48201 + offset = 0;
48202 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48203 + i += dwc_ep->pkt_per_frm) {
48204 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
48205 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48206 + if (len > dwc_ep->data_per_frame)
48207 + data_per_desc =
48208 + dwc_ep->data_per_frame -
48209 + j * dwc_ep->maxpacket;
48210 + else
48211 + data_per_desc = dwc_ep->maxpacket;
48212 + len = data_per_desc % 4;
48213 + if (len)
48214 + data_per_desc += 4 - len;
48215 +
48216 + data_per_desc =
48217 + sts.b_iso_out.rxbytes = data_per_desc;
48218 + dma_desc->buf = dma_ad;
48219 + dma_desc->status.d32 = sts.d32;
48220 +
48221 + offset += data_per_desc;
48222 + dma_desc++;
48223 + dma_ad += data_per_desc;
48224 + }
48225 + }
48226 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
48227 + data_per_desc =
48228 + ((j + 1) * dwc_ep->maxpacket >
48229 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
48230 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
48231 + data_per_desc +=
48232 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
48233 + sts.b_iso_out.rxbytes = data_per_desc;
48234 + dma_desc->buf = dma_ad;
48235 + dma_desc->status.d32 = sts.d32;
48236 +
48237 + offset += data_per_desc;
48238 + dma_desc++;
48239 + dma_ad += data_per_desc;
48240 + }
48241 +
48242 + sts.b_iso_out.ioc = 1;
48243 + sts.b_iso_out.l = 1;
48244 + data_per_desc =
48245 + ((j + 1) * dwc_ep->maxpacket >
48246 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
48247 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
48248 + data_per_desc +=
48249 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
48250 + sts.b_iso_out.rxbytes = data_per_desc;
48251 +
48252 + dma_desc->buf = dma_ad;
48253 + dma_desc->status.d32 = sts.d32;
48254 +
48255 + dwc_ep->next_frame = 0;
48256 +
48257 + /** Write dma_ad into DOEPDMA register */
48258 + DWC_WRITE_REG32(&(out_regs->doepdma),
48259 + (uint32_t) dwc_ep->iso_dma_desc_addr);
48260 +
48261 + }
48262 + /** ISO IN EP */
48263 + else {
48264 + dev_dma_desc_sts_t sts = {.d32 = 0 };
48265 + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
48266 + dma_addr_t dma_ad;
48267 + dwc_otg_dev_in_ep_regs_t *in_regs =
48268 + core_if->dev_if->in_ep_regs[dwc_ep->num];
48269 + unsigned int frmnumber;
48270 + fifosize_data_t txfifosize, rxfifosize;
48271 +
48272 + txfifosize.d32 =
48273 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
48274 + dtxfsts);
48275 + rxfifosize.d32 =
48276 + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
48277 +
48278 + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
48279 +
48280 + dma_ad = dwc_ep->dma_addr0;
48281 +
48282 + dsts.d32 =
48283 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48284 +
48285 + sts.b_iso_in.bs = BS_HOST_READY;
48286 + sts.b_iso_in.txsts = 0;
48287 + sts.b_iso_in.sp =
48288 + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
48289 + sts.b_iso_in.ioc = 0;
48290 + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
48291 +
48292 + frmnumber = dwc_ep->next_frame;
48293 +
48294 + sts.b_iso_in.framenum = frmnumber;
48295 + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
48296 + sts.b_iso_in.l = 0;
48297 +
48298 + /** Buffer 0 descriptors setup */
48299 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
48300 + dma_desc->buf = dma_ad;
48301 + dma_desc->status.d32 = sts.d32;
48302 + dma_desc++;
48303 +
48304 + dma_ad += dwc_ep->data_per_frame;
48305 + sts.b_iso_in.framenum += dwc_ep->bInterval;
48306 + }
48307 +
48308 + sts.b_iso_in.ioc = 1;
48309 + dma_desc->buf = dma_ad;
48310 + dma_desc->status.d32 = sts.d32;
48311 + ++dma_desc;
48312 +
48313 + /** Buffer 1 descriptors setup */
48314 + sts.b_iso_in.ioc = 0;
48315 + dma_ad = dwc_ep->dma_addr1;
48316 +
48317 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48318 + i += dwc_ep->pkt_per_frm) {
48319 + dma_desc->buf = dma_ad;
48320 + dma_desc->status.d32 = sts.d32;
48321 + dma_desc++;
48322 +
48323 + dma_ad += dwc_ep->data_per_frame;
48324 + sts.b_iso_in.framenum += dwc_ep->bInterval;
48325 +
48326 + sts.b_iso_in.ioc = 0;
48327 + }
48328 + sts.b_iso_in.ioc = 1;
48329 + sts.b_iso_in.l = 1;
48330 +
48331 + dma_desc->buf = dma_ad;
48332 + dma_desc->status.d32 = sts.d32;
48333 +
48334 + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
48335 +
48336 + /** Write dma_ad into diepdma register */
48337 + DWC_WRITE_REG32(&(in_regs->diepdma),
48338 + (uint32_t) dwc_ep->iso_dma_desc_addr);
48339 + }
48340 + /** Enable endpoint, clear nak */
48341 + depctl.d32 = 0;
48342 + depctl.b.epena = 1;
48343 + depctl.b.usbactep = 1;
48344 + depctl.b.cnak = 1;
48345 +
48346 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
48347 + depctl.d32 = DWC_READ_REG32(addr);
48348 +}
48349 +
48350 +/**
48351 + * This function initializes a descriptor chain for Isochronous transfer
48352 + *
48353 + * @param core_if Programming view of DWC_otg controller.
48354 + * @param ep The EP to start the transfer on.
48355 + *
48356 + */
48357 +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
48358 + dwc_ep_t * ep)
48359 +{
48360 + depctl_data_t depctl = {.d32 = 0 };
48361 + volatile uint32_t *addr;
48362 +
48363 + if (ep->is_in) {
48364 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
48365 + } else {
48366 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
48367 + }
48368 +
48369 + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
48370 + return;
48371 + } else {
48372 + deptsiz_data_t deptsiz = {.d32 = 0 };
48373 +
48374 + ep->xfer_len =
48375 + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
48376 + ep->pkt_cnt =
48377 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
48378 + ep->xfer_count = 0;
48379 + ep->xfer_buff =
48380 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
48381 + ep->dma_addr =
48382 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
48383 +
48384 + if (ep->is_in) {
48385 + /* Program the transfer size and packet count
48386 + * as follows: xfersize = N * maxpacket +
48387 + * short_packet pktcnt = N + (short_packet
48388 + * exist ? 1 : 0)
48389 + */
48390 + deptsiz.b.mc = ep->pkt_per_frm;
48391 + deptsiz.b.xfersize = ep->xfer_len;
48392 + deptsiz.b.pktcnt =
48393 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
48394 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
48395 + dieptsiz, deptsiz.d32);
48396 +
48397 + /* Write the DMA register */
48398 + DWC_WRITE_REG32(&
48399 + (core_if->dev_if->in_ep_regs[ep->num]->
48400 + diepdma), (uint32_t) ep->dma_addr);
48401 +
48402 + } else {
48403 + deptsiz.b.pktcnt =
48404 + (ep->xfer_len + (ep->maxpacket - 1)) /
48405 + ep->maxpacket;
48406 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
48407 +
48408 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
48409 + doeptsiz, deptsiz.d32);
48410 +
48411 + /* Write the DMA register */
48412 + DWC_WRITE_REG32(&
48413 + (core_if->dev_if->out_ep_regs[ep->num]->
48414 + doepdma), (uint32_t) ep->dma_addr);
48415 +
48416 + }
48417 + /** Enable endpoint, clear nak */
48418 + depctl.d32 = 0;
48419 + depctl.b.epena = 1;
48420 + depctl.b.cnak = 1;
48421 +
48422 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
48423 + }
48424 +}
48425 +
48426 +/**
48427 + * This function does the setup for a data transfer for an EP and
48428 + * starts the transfer. For an IN transfer, the packets will be
48429 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
48430 + * the packets are unloaded from the Rx FIFO in the ISR.
48431 + *
48432 + * @param core_if Programming view of DWC_otg controller.
48433 + * @param ep The EP to start the transfer on.
48434 + */
48435 +
48436 +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
48437 + dwc_ep_t * ep)
48438 +{
48439 + if (core_if->dma_enable) {
48440 + if (core_if->dma_desc_enable) {
48441 + if (ep->is_in) {
48442 + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
48443 + } else {
48444 + ep->desc_cnt = ep->pkt_cnt;
48445 + }
48446 + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
48447 + } else {
48448 + if (core_if->pti_enh_enable) {
48449 + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
48450 + } else {
48451 + ep->cur_pkt_addr =
48452 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
48453 + xfer_buff0;
48454 + ep->cur_pkt_dma_addr =
48455 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
48456 + dma_addr0;
48457 + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
48458 + }
48459 + }
48460 + } else {
48461 + ep->cur_pkt_addr =
48462 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
48463 + ep->cur_pkt_dma_addr =
48464 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
48465 + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
48466 + }
48467 +}
48468 +
48469 +/**
48470 + * This function stops transfer for an EP and
48471 + * resets the ep's variables.
48472 + *
48473 + * @param core_if Programming view of DWC_otg controller.
48474 + * @param ep The EP to start the transfer on.
48475 + */
48476 +
48477 +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
48478 +{
48479 + depctl_data_t depctl = {.d32 = 0 };
48480 + volatile uint32_t *addr;
48481 +
48482 + if (ep->is_in == 1) {
48483 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
48484 + } else {
48485 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
48486 + }
48487 +
48488 + /* disable the ep */
48489 + depctl.d32 = DWC_READ_REG32(addr);
48490 +
48491 + depctl.b.epdis = 1;
48492 + depctl.b.snak = 1;
48493 +
48494 + DWC_WRITE_REG32(addr, depctl.d32);
48495 +
48496 + if (core_if->dma_desc_enable &&
48497 + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
48498 + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
48499 + ep->iso_dma_desc_addr,
48500 + ep->desc_cnt * 2);
48501 + }
48502 +
48503 + /* reset varibales */
48504 + ep->dma_addr0 = 0;
48505 + ep->dma_addr1 = 0;
48506 + ep->xfer_buff0 = 0;
48507 + ep->xfer_buff1 = 0;
48508 + ep->data_per_frame = 0;
48509 + ep->data_pattern_frame = 0;
48510 + ep->sync_frame = 0;
48511 + ep->buf_proc_intrvl = 0;
48512 + ep->bInterval = 0;
48513 + ep->proc_buf_num = 0;
48514 + ep->pkt_per_frm = 0;
48515 + ep->pkt_per_frm = 0;
48516 + ep->desc_cnt = 0;
48517 + ep->iso_desc_addr = 0;
48518 + ep->iso_dma_desc_addr = 0;
48519 +}
48520 +
48521 +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
48522 + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
48523 + dwc_dma_t dma1, int sync_frame, int dp_frame,
48524 + int data_per_frame, int start_frame,
48525 + int buf_proc_intrvl, void *req_handle,
48526 + int atomic_alloc)
48527 +{
48528 + dwc_otg_pcd_ep_t *ep;
48529 + dwc_irqflags_t flags = 0;
48530 + dwc_ep_t *dwc_ep;
48531 + int32_t frm_data;
48532 + dsts_data_t dsts;
48533 + dwc_otg_core_if_t *core_if;
48534 +
48535 + ep = get_ep_from_handle(pcd, ep_handle);
48536 +
48537 + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
48538 + DWC_WARN("bad ep\n");
48539 + return -DWC_E_INVALID;
48540 + }
48541 +
48542 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
48543 + core_if = GET_CORE_IF(pcd);
48544 + dwc_ep = &ep->dwc_ep;
48545 +
48546 + if (ep->iso_req_handle) {
48547 + DWC_WARN("ISO request in progress\n");
48548 + }
48549 +
48550 + dwc_ep->dma_addr0 = dma0;
48551 + dwc_ep->dma_addr1 = dma1;
48552 +
48553 + dwc_ep->xfer_buff0 = buf0;
48554 + dwc_ep->xfer_buff1 = buf1;
48555 +
48556 + dwc_ep->data_per_frame = data_per_frame;
48557 +
48558 + /** @todo - pattern data support is to be implemented in the future */
48559 + dwc_ep->data_pattern_frame = dp_frame;
48560 + dwc_ep->sync_frame = sync_frame;
48561 +
48562 + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
48563 +
48564 + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
48565 +
48566 + dwc_ep->proc_buf_num = 0;
48567 +
48568 + dwc_ep->pkt_per_frm = 0;
48569 + frm_data = ep->dwc_ep.data_per_frame;
48570 + while (frm_data > 0) {
48571 + dwc_ep->pkt_per_frm++;
48572 + frm_data -= ep->dwc_ep.maxpacket;
48573 + }
48574 +
48575 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48576 +
48577 + if (start_frame == -1) {
48578 + dwc_ep->next_frame = dsts.b.soffn + 1;
48579 + if (dwc_ep->bInterval != 1) {
48580 + dwc_ep->next_frame =
48581 + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
48582 + dwc_ep->next_frame %
48583 + dwc_ep->bInterval);
48584 + }
48585 + } else {
48586 + dwc_ep->next_frame = start_frame;
48587 + }
48588 +
48589 + if (!core_if->pti_enh_enable) {
48590 + dwc_ep->pkt_cnt =
48591 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48592 + dwc_ep->bInterval;
48593 + } else {
48594 + dwc_ep->pkt_cnt =
48595 + (dwc_ep->data_per_frame *
48596 + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
48597 + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
48598 + }
48599 +
48600 + if (core_if->dma_desc_enable) {
48601 + dwc_ep->desc_cnt =
48602 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48603 + dwc_ep->bInterval;
48604 + }
48605 +
48606 + if (atomic_alloc) {
48607 + dwc_ep->pkt_info =
48608 + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48609 + } else {
48610 + dwc_ep->pkt_info =
48611 + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48612 + }
48613 + if (!dwc_ep->pkt_info) {
48614 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48615 + return -DWC_E_NO_MEMORY;
48616 + }
48617 + if (core_if->pti_enh_enable) {
48618 + dwc_memset(dwc_ep->pkt_info, 0,
48619 + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48620 + }
48621 +
48622 + dwc_ep->cur_pkt = 0;
48623 + ep->iso_req_handle = req_handle;
48624 +
48625 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48626 + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
48627 + return 0;
48628 +}
48629 +
48630 +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
48631 + void *req_handle)
48632 +{
48633 + dwc_irqflags_t flags = 0;
48634 + dwc_otg_pcd_ep_t *ep;
48635 + dwc_ep_t *dwc_ep;
48636 +
48637 + ep = get_ep_from_handle(pcd, ep_handle);
48638 + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
48639 + DWC_WARN("bad ep\n");
48640 + return -DWC_E_INVALID;
48641 + }
48642 + dwc_ep = &ep->dwc_ep;
48643 +
48644 + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
48645 +
48646 + DWC_FREE(dwc_ep->pkt_info);
48647 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
48648 + if (ep->iso_req_handle != req_handle) {
48649 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48650 + return -DWC_E_INVALID;
48651 + }
48652 +
48653 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48654 +
48655 + ep->iso_req_handle = 0;
48656 + return 0;
48657 +}
48658 +
48659 +/**
48660 + * This function is used for perodical data exchnage between PCD and gadget drivers.
48661 + * for Isochronous EPs
48662 + *
48663 + * - Every time a sync period completes this function is called to
48664 + * perform data exchange between PCD and gadget
48665 + */
48666 +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
48667 + void *req_handle)
48668 +{
48669 + int i;
48670 + dwc_ep_t *dwc_ep;
48671 +
48672 + dwc_ep = &ep->dwc_ep;
48673 +
48674 + DWC_SPINUNLOCK(ep->pcd->lock);
48675 + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
48676 + dwc_ep->proc_buf_num ^ 0x1);
48677 + DWC_SPINLOCK(ep->pcd->lock);
48678 +
48679 + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
48680 + dwc_ep->pkt_info[i].status = 0;
48681 + dwc_ep->pkt_info[i].offset = 0;
48682 + dwc_ep->pkt_info[i].length = 0;
48683 + }
48684 +}
48685 +
48686 +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
48687 + void *iso_req_handle)
48688 +{
48689 + dwc_otg_pcd_ep_t *ep;
48690 + dwc_ep_t *dwc_ep;
48691 +
48692 + ep = get_ep_from_handle(pcd, ep_handle);
48693 + if (!ep->desc || ep->dwc_ep.num == 0) {
48694 + DWC_WARN("bad ep\n");
48695 + return -DWC_E_INVALID;
48696 + }
48697 + dwc_ep = &ep->dwc_ep;
48698 +
48699 + return dwc_ep->pkt_cnt;
48700 +}
48701 +
48702 +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
48703 + void *iso_req_handle, int packet,
48704 + int *status, int *actual, int *offset)
48705 +{
48706 + dwc_otg_pcd_ep_t *ep;
48707 + dwc_ep_t *dwc_ep;
48708 +
48709 + ep = get_ep_from_handle(pcd, ep_handle);
48710 + if (!ep)
48711 + DWC_WARN("bad ep\n");
48712 +
48713 + dwc_ep = &ep->dwc_ep;
48714 +
48715 + *status = dwc_ep->pkt_info[packet].status;
48716 + *actual = dwc_ep->pkt_info[packet].length;
48717 + *offset = dwc_ep->pkt_info[packet].offset;
48718 +}
48719 +
48720 +#endif /* DWC_EN_ISOC */
48721 +
48722 +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
48723 + uint32_t is_in, uint32_t ep_num)
48724 +{
48725 + /* Init EP structure */
48726 + pcd_ep->desc = 0;
48727 + pcd_ep->pcd = pcd;
48728 + pcd_ep->stopped = 1;
48729 + pcd_ep->queue_sof = 0;
48730 +
48731 + /* Init DWC ep structure */
48732 + pcd_ep->dwc_ep.is_in = is_in;
48733 + pcd_ep->dwc_ep.num = ep_num;
48734 + pcd_ep->dwc_ep.active = 0;
48735 + pcd_ep->dwc_ep.tx_fifo_num = 0;
48736 + /* Control until ep is actvated */
48737 + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
48738 + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
48739 + pcd_ep->dwc_ep.dma_addr = 0;
48740 + pcd_ep->dwc_ep.start_xfer_buff = 0;
48741 + pcd_ep->dwc_ep.xfer_buff = 0;
48742 + pcd_ep->dwc_ep.xfer_len = 0;
48743 + pcd_ep->dwc_ep.xfer_count = 0;
48744 + pcd_ep->dwc_ep.sent_zlp = 0;
48745 + pcd_ep->dwc_ep.total_len = 0;
48746 + pcd_ep->dwc_ep.desc_addr = 0;
48747 + pcd_ep->dwc_ep.dma_desc_addr = 0;
48748 + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
48749 +}
48750 +
48751 +/**
48752 + * Initialize ep's
48753 + */
48754 +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
48755 +{
48756 + int i;
48757 + uint32_t hwcfg1;
48758 + dwc_otg_pcd_ep_t *ep;
48759 + int in_ep_cntr, out_ep_cntr;
48760 + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
48761 + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
48762 +
48763 + /**
48764 + * Initialize the EP0 structure.
48765 + */
48766 + ep = &pcd->ep0;
48767 + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
48768 +
48769 + in_ep_cntr = 0;
48770 + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
48771 + for (i = 1; in_ep_cntr < num_in_eps; i++) {
48772 + if ((hwcfg1 & 0x1) == 0) {
48773 + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
48774 + in_ep_cntr++;
48775 + /**
48776 + * @todo NGS: Add direction to EP, based on contents
48777 + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
48778 + * sprintf(";r
48779 + */
48780 + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
48781 +
48782 + DWC_CIRCLEQ_INIT(&ep->queue);
48783 + }
48784 + hwcfg1 >>= 2;
48785 + }
48786 +
48787 + out_ep_cntr = 0;
48788 + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
48789 + for (i = 1; out_ep_cntr < num_out_eps; i++) {
48790 + if ((hwcfg1 & 0x1) == 0) {
48791 + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
48792 + out_ep_cntr++;
48793 + /**
48794 + * @todo NGS: Add direction to EP, based on contents
48795 + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
48796 + * sprintf(";r
48797 + */
48798 + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
48799 + DWC_CIRCLEQ_INIT(&ep->queue);
48800 + }
48801 + hwcfg1 >>= 2;
48802 + }
48803 +
48804 + pcd->ep0state = EP0_DISCONNECT;
48805 + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
48806 + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
48807 +}
48808 +
48809 +/**
48810 + * This function is called when the SRP timer expires. The SRP should
48811 + * complete within 6 seconds.
48812 + */
48813 +static void srp_timeout(void *ptr)
48814 +{
48815 + gotgctl_data_t gotgctl;
48816 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
48817 + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
48818 +
48819 + gotgctl.d32 = DWC_READ_REG32(addr);
48820 +
48821 + core_if->srp_timer_started = 0;
48822 +
48823 + if (core_if->adp_enable) {
48824 + if (gotgctl.b.bsesvld == 0) {
48825 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
48826 + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
48827 + /* Power off the core */
48828 + if (core_if->power_down == 2) {
48829 + gpwrdn.b.pwrdnswtch = 1;
48830 + DWC_MODIFY_REG32(&core_if->
48831 + core_global_regs->gpwrdn,
48832 + gpwrdn.d32, 0);
48833 + }
48834 +
48835 + gpwrdn.d32 = 0;
48836 + gpwrdn.b.pmuintsel = 1;
48837 + gpwrdn.b.pmuactv = 1;
48838 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
48839 + gpwrdn.d32);
48840 + dwc_otg_adp_probe_start(core_if);
48841 + } else {
48842 + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
48843 + core_if->op_state = B_PERIPHERAL;
48844 + dwc_otg_core_init(core_if);
48845 + dwc_otg_enable_global_interrupts(core_if);
48846 + cil_pcd_start(core_if);
48847 + }
48848 + }
48849 +
48850 + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
48851 + (core_if->core_params->i2c_enable)) {
48852 + DWC_PRINTF("SRP Timeout\n");
48853 +
48854 + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
48855 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
48856 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
48857 + }
48858 +
48859 + /* Clear Session Request */
48860 + gotgctl.d32 = 0;
48861 + gotgctl.b.sesreq = 1;
48862 + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
48863 + gotgctl.d32, 0);
48864 +
48865 + core_if->srp_success = 0;
48866 + } else {
48867 + __DWC_ERROR("Device not connected/responding\n");
48868 + gotgctl.b.sesreq = 0;
48869 + DWC_WRITE_REG32(addr, gotgctl.d32);
48870 + }
48871 + } else if (gotgctl.b.sesreq) {
48872 + DWC_PRINTF("SRP Timeout\n");
48873 +
48874 + __DWC_ERROR("Device not connected/responding\n");
48875 + gotgctl.b.sesreq = 0;
48876 + DWC_WRITE_REG32(addr, gotgctl.d32);
48877 + } else {
48878 + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
48879 + }
48880 +}
48881 +
48882 +/**
48883 + * Tasklet
48884 + *
48885 + */
48886 +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
48887 +
48888 +static void start_xfer_tasklet_func(void *data)
48889 +{
48890 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
48891 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
48892 +
48893 + int i;
48894 + depctl_data_t diepctl;
48895 +
48896 + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
48897 +
48898 + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
48899 +
48900 + if (pcd->ep0.queue_sof) {
48901 + pcd->ep0.queue_sof = 0;
48902 + start_next_request(&pcd->ep0);
48903 + // break;
48904 + }
48905 +
48906 + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
48907 + depctl_data_t diepctl;
48908 + diepctl.d32 =
48909 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
48910 +
48911 + if (pcd->in_ep[i].queue_sof) {
48912 + pcd->in_ep[i].queue_sof = 0;
48913 + start_next_request(&pcd->in_ep[i]);
48914 + // break;
48915 + }
48916 + }
48917 +
48918 + return;
48919 +}
48920 +
48921 +/**
48922 + * This function initialized the PCD portion of the driver.
48923 + *
48924 + */
48925 +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev)
48926 +{
48927 + struct device *dev = &otg_dev->os_dep.platformdev->dev;
48928 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
48929 + dwc_otg_pcd_t *pcd = NULL;
48930 + dwc_otg_dev_if_t *dev_if;
48931 + int i;
48932 +
48933 + /*
48934 + * Allocate PCD structure
48935 + */
48936 + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
48937 +
48938 + if (pcd == NULL) {
48939 + return NULL;
48940 + }
48941 +
48942 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
48943 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
48944 +#else
48945 + pcd->lock = DWC_SPINLOCK_ALLOC();
48946 +#endif
48947 + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
48948 + pcd, core_if);//GRAYG
48949 + if (!pcd->lock) {
48950 + DWC_ERROR("Could not allocate lock for pcd");
48951 + DWC_FREE(pcd);
48952 + return NULL;
48953 + }
48954 + /* Set core_if's lock pointer to hcd->lock */
48955 + core_if->lock = pcd->lock;
48956 + pcd->core_if = core_if;
48957 +
48958 + dev_if = core_if->dev_if;
48959 + dev_if->isoc_ep = NULL;
48960 +
48961 + if (core_if->hwcfg4.b.ded_fifo_en) {
48962 + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
48963 + } else {
48964 + DWC_PRINTF("Shared Tx FIFO mode\n");
48965 + }
48966 +
48967 + /*
48968 + * Initialized the Core for Device mode here if there is nod ADP support.
48969 + * Otherwise it will be done later in dwc_otg_adp_start routine.
48970 + */
48971 + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
48972 + dwc_otg_core_dev_init(core_if);
48973 + }
48974 +
48975 + /*
48976 + * Register the PCD Callbacks.
48977 + */
48978 + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
48979 +
48980 + /*
48981 + * Initialize the DMA buffer for SETUP packets
48982 + */
48983 + if (GET_CORE_IF(pcd)->dma_enable) {
48984 + pcd->setup_pkt =
48985 + DWC_DMA_ALLOC(dev, sizeof(*pcd->setup_pkt) * 5,
48986 + &pcd->setup_pkt_dma_handle);
48987 + if (pcd->setup_pkt == NULL) {
48988 + DWC_FREE(pcd);
48989 + return NULL;
48990 + }
48991 +
48992 + pcd->status_buf =
48993 + DWC_DMA_ALLOC(dev, sizeof(uint16_t),
48994 + &pcd->status_buf_dma_handle);
48995 + if (pcd->status_buf == NULL) {
48996 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
48997 + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
48998 + DWC_FREE(pcd);
48999 + return NULL;
49000 + }
49001 +
49002 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49003 + dev_if->setup_desc_addr[0] =
49004 + dwc_otg_ep_alloc_desc_chain(dev,
49005 + &dev_if->dma_setup_desc_addr[0], 1);
49006 + dev_if->setup_desc_addr[1] =
49007 + dwc_otg_ep_alloc_desc_chain(dev,
49008 + &dev_if->dma_setup_desc_addr[1], 1);
49009 + dev_if->in_desc_addr =
49010 + dwc_otg_ep_alloc_desc_chain(dev,
49011 + &dev_if->dma_in_desc_addr, 1);
49012 + dev_if->out_desc_addr =
49013 + dwc_otg_ep_alloc_desc_chain(dev,
49014 + &dev_if->dma_out_desc_addr, 1);
49015 + pcd->data_terminated = 0;
49016 +
49017 + if (dev_if->setup_desc_addr[0] == 0
49018 + || dev_if->setup_desc_addr[1] == 0
49019 + || dev_if->in_desc_addr == 0
49020 + || dev_if->out_desc_addr == 0) {
49021 +
49022 + if (dev_if->out_desc_addr)
49023 + dwc_otg_ep_free_desc_chain(dev,
49024 + dev_if->out_desc_addr,
49025 + dev_if->dma_out_desc_addr, 1);
49026 + if (dev_if->in_desc_addr)
49027 + dwc_otg_ep_free_desc_chain(dev,
49028 + dev_if->in_desc_addr,
49029 + dev_if->dma_in_desc_addr, 1);
49030 + if (dev_if->setup_desc_addr[1])
49031 + dwc_otg_ep_free_desc_chain(dev,
49032 + dev_if->setup_desc_addr[1],
49033 + dev_if->dma_setup_desc_addr[1], 1);
49034 + if (dev_if->setup_desc_addr[0])
49035 + dwc_otg_ep_free_desc_chain(dev,
49036 + dev_if->setup_desc_addr[0],
49037 + dev_if->dma_setup_desc_addr[0], 1);
49038 +
49039 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
49040 + pcd->setup_pkt,
49041 + pcd->setup_pkt_dma_handle);
49042 + DWC_DMA_FREE(dev, sizeof(*pcd->status_buf),
49043 + pcd->status_buf,
49044 + pcd->status_buf_dma_handle);
49045 +
49046 + DWC_FREE(pcd);
49047 +
49048 + return NULL;
49049 + }
49050 + }
49051 + } else {
49052 + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
49053 + if (pcd->setup_pkt == NULL) {
49054 + DWC_FREE(pcd);
49055 + return NULL;
49056 + }
49057 +
49058 + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
49059 + if (pcd->status_buf == NULL) {
49060 + DWC_FREE(pcd->setup_pkt);
49061 + DWC_FREE(pcd);
49062 + return NULL;
49063 + }
49064 + }
49065 +
49066 + dwc_otg_pcd_reinit(pcd);
49067 +
49068 + /* Allocate the cfi object for the PCD */
49069 +#ifdef DWC_UTE_CFI
49070 + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
49071 + if (NULL == pcd->cfi)
49072 + goto fail;
49073 + if (init_cfi(pcd->cfi)) {
49074 + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
49075 + goto fail;
49076 + }
49077 +#endif
49078 +
49079 + /* Initialize tasklets */
49080 + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
49081 + start_xfer_tasklet_func, pcd);
49082 + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
49083 + do_test_mode, pcd);
49084 +
49085 + /* Initialize SRP timer */
49086 + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
49087 +
49088 + if (core_if->core_params->dev_out_nak) {
49089 + /**
49090 + * Initialize xfer timeout timer. Implemented for
49091 + * 2.93a feature "Device DDMA OUT NAK Enhancement"
49092 + */
49093 + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
49094 + pcd->core_if->ep_xfer_timer[i] =
49095 + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
49096 + &pcd->core_if->ep_xfer_info[i]);
49097 + }
49098 + }
49099 +
49100 + return pcd;
49101 +#ifdef DWC_UTE_CFI
49102 +fail:
49103 +#endif
49104 + if (pcd->setup_pkt)
49105 + DWC_FREE(pcd->setup_pkt);
49106 + if (pcd->status_buf)
49107 + DWC_FREE(pcd->status_buf);
49108 +#ifdef DWC_UTE_CFI
49109 + if (pcd->cfi)
49110 + DWC_FREE(pcd->cfi);
49111 +#endif
49112 + if (pcd)
49113 + DWC_FREE(pcd);
49114 + return NULL;
49115 +
49116 +}
49117 +
49118 +/**
49119 + * Remove PCD specific data
49120 + */
49121 +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
49122 +{
49123 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
49124 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49125 + int i;
49126 +
49127 + if (pcd->core_if->core_params->dev_out_nak) {
49128 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49129 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
49130 + pcd->core_if->ep_xfer_info[i].state = 0;
49131 + }
49132 + }
49133 +
49134 + if (GET_CORE_IF(pcd)->dma_enable) {
49135 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
49136 + pcd->setup_pkt_dma_handle);
49137 + DWC_DMA_FREE(dev, sizeof(uint16_t), pcd->status_buf,
49138 + pcd->status_buf_dma_handle);
49139 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49140 + dwc_otg_ep_free_desc_chain(dev,
49141 + dev_if->setup_desc_addr[0],
49142 + dev_if->dma_setup_desc_addr
49143 + [0], 1);
49144 + dwc_otg_ep_free_desc_chain(dev,
49145 + dev_if->setup_desc_addr[1],
49146 + dev_if->dma_setup_desc_addr
49147 + [1], 1);
49148 + dwc_otg_ep_free_desc_chain(dev,
49149 + dev_if->in_desc_addr,
49150 + dev_if->dma_in_desc_addr, 1);
49151 + dwc_otg_ep_free_desc_chain(dev,
49152 + dev_if->out_desc_addr,
49153 + dev_if->dma_out_desc_addr,
49154 + 1);
49155 + }
49156 + } else {
49157 + DWC_FREE(pcd->setup_pkt);
49158 + DWC_FREE(pcd->status_buf);
49159 + }
49160 + DWC_SPINLOCK_FREE(pcd->lock);
49161 + /* Set core_if's lock pointer to NULL */
49162 + pcd->core_if->lock = NULL;
49163 +
49164 + DWC_TASK_FREE(pcd->start_xfer_tasklet);
49165 + DWC_TASK_FREE(pcd->test_mode_tasklet);
49166 + if (pcd->core_if->core_params->dev_out_nak) {
49167 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49168 + if (pcd->core_if->ep_xfer_timer[i]) {
49169 + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
49170 + }
49171 + }
49172 + }
49173 +
49174 +/* Release the CFI object's dynamic memory */
49175 +#ifdef DWC_UTE_CFI
49176 + if (pcd->cfi->ops.release) {
49177 + pcd->cfi->ops.release(pcd->cfi);
49178 + }
49179 +#endif
49180 +
49181 + DWC_FREE(pcd);
49182 +}
49183 +
49184 +/**
49185 + * Returns whether registered pcd is dual speed or not
49186 + */
49187 +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
49188 +{
49189 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49190 +
49191 + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
49192 + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
49193 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
49194 + (core_if->core_params->ulpi_fs_ls))) {
49195 + return 0;
49196 + }
49197 +
49198 + return 1;
49199 +}
49200 +
49201 +/**
49202 + * Returns whether registered pcd is OTG capable or not
49203 + */
49204 +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
49205 +{
49206 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49207 + gusbcfg_data_t usbcfg = {.d32 = 0 };
49208 +
49209 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
49210 + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
49211 + return 0;
49212 + }
49213 +
49214 + return 1;
49215 +}
49216 +
49217 +/**
49218 + * This function assigns periodic Tx FIFO to an periodic EP
49219 + * in shared Tx FIFO mode
49220 + */
49221 +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
49222 +{
49223 + uint32_t TxMsk = 1;
49224 + int i;
49225 +
49226 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
49227 + if ((TxMsk & core_if->tx_msk) == 0) {
49228 + core_if->tx_msk |= TxMsk;
49229 + return i + 1;
49230 + }
49231 + TxMsk <<= 1;
49232 + }
49233 + return 0;
49234 +}
49235 +
49236 +/**
49237 + * This function assigns periodic Tx FIFO to an periodic EP
49238 + * in shared Tx FIFO mode
49239 + */
49240 +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
49241 +{
49242 + uint32_t PerTxMsk = 1;
49243 + int i;
49244 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
49245 + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
49246 + core_if->p_tx_msk |= PerTxMsk;
49247 + return i + 1;
49248 + }
49249 + PerTxMsk <<= 1;
49250 + }
49251 + return 0;
49252 +}
49253 +
49254 +/**
49255 + * This function releases periodic Tx FIFO
49256 + * in shared Tx FIFO mode
49257 + */
49258 +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
49259 + uint32_t fifo_num)
49260 +{
49261 + core_if->p_tx_msk =
49262 + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
49263 +}
49264 +
49265 +/**
49266 + * This function releases periodic Tx FIFO
49267 + * in shared Tx FIFO mode
49268 + */
49269 +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
49270 +{
49271 + core_if->tx_msk =
49272 + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
49273 +}
49274 +
49275 +/**
49276 + * This function is being called from gadget
49277 + * to enable PCD endpoint.
49278 + */
49279 +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
49280 + const uint8_t * ep_desc, void *usb_ep)
49281 +{
49282 + int num, dir;
49283 + dwc_otg_pcd_ep_t *ep = NULL;
49284 + const usb_endpoint_descriptor_t *desc;
49285 + dwc_irqflags_t flags;
49286 + fifosize_data_t dptxfsiz = {.d32 = 0 };
49287 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
49288 + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
49289 + int retval = 0;
49290 + int i, epcount;
49291 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49292 +
49293 + desc = (const usb_endpoint_descriptor_t *)ep_desc;
49294 +
49295 + if (!desc) {
49296 + pcd->ep0.priv = usb_ep;
49297 + ep = &pcd->ep0;
49298 + retval = -DWC_E_INVALID;
49299 + goto out;
49300 + }
49301 +
49302 + num = UE_GET_ADDR(desc->bEndpointAddress);
49303 + dir = UE_GET_DIR(desc->bEndpointAddress);
49304 +
49305 + if (!desc->wMaxPacketSize) {
49306 + DWC_WARN("bad maxpacketsize\n");
49307 + retval = -DWC_E_INVALID;
49308 + goto out;
49309 + }
49310 +
49311 + if (dir == UE_DIR_IN) {
49312 + epcount = pcd->core_if->dev_if->num_in_eps;
49313 + for (i = 0; i < epcount; i++) {
49314 + if (num == pcd->in_ep[i].dwc_ep.num) {
49315 + ep = &pcd->in_ep[i];
49316 + break;
49317 + }
49318 + }
49319 + } else {
49320 + epcount = pcd->core_if->dev_if->num_out_eps;
49321 + for (i = 0; i < epcount; i++) {
49322 + if (num == pcd->out_ep[i].dwc_ep.num) {
49323 + ep = &pcd->out_ep[i];
49324 + break;
49325 + }
49326 + }
49327 + }
49328 +
49329 + if (!ep) {
49330 + DWC_WARN("bad address\n");
49331 + retval = -DWC_E_INVALID;
49332 + goto out;
49333 + }
49334 +
49335 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49336 +
49337 + ep->desc = desc;
49338 + ep->priv = usb_ep;
49339 +
49340 + /*
49341 + * Activate the EP
49342 + */
49343 + ep->stopped = 0;
49344 +
49345 + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
49346 + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
49347 +
49348 + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
49349 +
49350 + if (ep->dwc_ep.is_in) {
49351 + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49352 + ep->dwc_ep.tx_fifo_num = 0;
49353 +
49354 + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
49355 + /*
49356 + * if ISOC EP then assign a Periodic Tx FIFO.
49357 + */
49358 + ep->dwc_ep.tx_fifo_num =
49359 + assign_perio_tx_fifo(GET_CORE_IF(pcd));
49360 + }
49361 + } else {
49362 + /*
49363 + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
49364 + */
49365 + ep->dwc_ep.tx_fifo_num =
49366 + assign_tx_fifo(GET_CORE_IF(pcd));
49367 + }
49368 +
49369 + /* Calculating EP info controller base address */
49370 + if (ep->dwc_ep.tx_fifo_num
49371 + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49372 + gdfifocfg.d32 =
49373 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
49374 + core_global_regs->gdfifocfg);
49375 + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
49376 + dptxfsiz.d32 =
49377 + (DWC_READ_REG32
49378 + (&GET_CORE_IF(pcd)->core_global_regs->
49379 + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
49380 + gdfifocfg.b.epinfobase =
49381 + gdfifocfgbase.d32 + dptxfsiz.d32;
49382 + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
49383 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
49384 + core_global_regs->gdfifocfg,
49385 + gdfifocfg.d32);
49386 + }
49387 + }
49388 + }
49389 + /* Set initial data PID. */
49390 + if (ep->dwc_ep.type == UE_BULK) {
49391 + ep->dwc_ep.data_pid_start = 0;
49392 + }
49393 +
49394 + /* Alloc DMA Descriptors */
49395 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49396 +#ifndef DWC_UTE_PER_IO
49397 + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
49398 +#endif
49399 + ep->dwc_ep.desc_addr =
49400 + dwc_otg_ep_alloc_desc_chain(dev,
49401 + &ep->dwc_ep.dma_desc_addr,
49402 + MAX_DMA_DESC_CNT);
49403 + if (!ep->dwc_ep.desc_addr) {
49404 + DWC_WARN("%s, can't allocate DMA descriptor\n",
49405 + __func__);
49406 + retval = -DWC_E_SHUTDOWN;
49407 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49408 + goto out;
49409 + }
49410 +#ifndef DWC_UTE_PER_IO
49411 + }
49412 +#endif
49413 + }
49414 +
49415 + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
49416 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
49417 + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
49418 +#ifdef DWC_UTE_PER_IO
49419 + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
49420 +#endif
49421 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
49422 + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
49423 + ep->dwc_ep.frame_num = 0xFFFFFFFF;
49424 + }
49425 +
49426 + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
49427 +
49428 +#ifdef DWC_UTE_CFI
49429 + if (pcd->cfi->ops.ep_enable) {
49430 + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
49431 + }
49432 +#endif
49433 +
49434 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49435 +
49436 +out:
49437 + return retval;
49438 +}
49439 +
49440 +/**
49441 + * This function is being called from gadget
49442 + * to disable PCD endpoint.
49443 + */
49444 +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
49445 +{
49446 + dwc_otg_pcd_ep_t *ep;
49447 + dwc_irqflags_t flags;
49448 + dwc_otg_dev_dma_desc_t *desc_addr;
49449 + dwc_dma_t dma_desc_addr;
49450 + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
49451 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
49452 + fifosize_data_t dptxfsiz = {.d32 = 0 };
49453 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49454 +
49455 + ep = get_ep_from_handle(pcd, ep_handle);
49456 +
49457 + if (!ep || !ep->desc) {
49458 + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
49459 + return -DWC_E_INVALID;
49460 + }
49461 +
49462 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49463 +
49464 + dwc_otg_request_nuke(ep);
49465 +
49466 + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
49467 + if (pcd->core_if->core_params->dev_out_nak) {
49468 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
49469 + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
49470 + }
49471 + ep->desc = NULL;
49472 + ep->stopped = 1;
49473 +
49474 + gdfifocfg.d32 =
49475 + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
49476 + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
49477 +
49478 + if (ep->dwc_ep.is_in) {
49479 + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49480 + /* Flush the Tx FIFO */
49481 + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
49482 + ep->dwc_ep.tx_fifo_num);
49483 + }
49484 + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
49485 + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
49486 + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49487 + /* Decreasing EPinfo Base Addr */
49488 + dptxfsiz.d32 =
49489 + (DWC_READ_REG32
49490 + (&GET_CORE_IF(pcd)->
49491 + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
49492 + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
49493 + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
49494 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
49495 + gdfifocfg.d32);
49496 + }
49497 + }
49498 + }
49499 +
49500 + /* Free DMA Descriptors */
49501 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49502 + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
49503 + desc_addr = ep->dwc_ep.desc_addr;
49504 + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
49505 +
49506 + /* Cannot call dma_free_coherent() with IRQs disabled */
49507 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49508 + dwc_otg_ep_free_desc_chain(dev, desc_addr, dma_desc_addr,
49509 + MAX_DMA_DESC_CNT);
49510 +
49511 + goto out_unlocked;
49512 + }
49513 + }
49514 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49515 +
49516 +out_unlocked:
49517 + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
49518 + ep->dwc_ep.is_in ? "IN" : "OUT");
49519 + return 0;
49520 +
49521 +}
49522 +
49523 +/******************************************************************************/
49524 +#ifdef DWC_UTE_PER_IO
49525 +
49526 +/**
49527 + * Free the request and its extended parts
49528 + *
49529 + */
49530 +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
49531 +{
49532 + DWC_FREE(req->ext_req.per_io_frame_descs);
49533 + DWC_FREE(req);
49534 +}
49535 +
49536 +/**
49537 + * Start the next request in the endpoint's queue.
49538 + *
49539 + */
49540 +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
49541 + dwc_otg_pcd_ep_t * ep)
49542 +{
49543 + int i;
49544 + dwc_otg_pcd_request_t *req = NULL;
49545 + dwc_ep_t *dwcep = NULL;
49546 + struct dwc_iso_xreq_port *ereq = NULL;
49547 + struct dwc_iso_pkt_desc_port *ddesc_iso;
49548 + uint16_t nat;
49549 + depctl_data_t diepctl;
49550 +
49551 + dwcep = &ep->dwc_ep;
49552 +
49553 + if (dwcep->xiso_active_xfers > 0) {
49554 +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
49555 + DWC_WARN("There are currently active transfers for EP%d \
49556 + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
49557 + dwcep->xiso_queued_xfers);
49558 +#endif
49559 + return 0;
49560 + }
49561 +
49562 + nat = UGETW(ep->desc->wMaxPacketSize);
49563 + nat = (nat >> 11) & 0x03;
49564 +
49565 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
49566 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
49567 + ereq = &req->ext_req;
49568 + ep->stopped = 0;
49569 +
49570 + /* Get the frame number */
49571 + dwcep->xiso_frame_num =
49572 + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
49573 + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
49574 +
49575 + ddesc_iso = ereq->per_io_frame_descs;
49576 +
49577 + if (dwcep->is_in) {
49578 + /* Setup DMA Descriptor chain for IN Isoc request */
49579 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49580 + //if ((i % (nat + 1)) == 0)
49581 + if ( i > 0 )
49582 + dwcep->xiso_frame_num =
49583 + (dwcep->xiso_bInterval +
49584 + dwcep->xiso_frame_num) & 0x3FFF;
49585 + dwcep->desc_addr[i].buf =
49586 + req->dma + ddesc_iso[i].offset;
49587 + dwcep->desc_addr[i].status.b_iso_in.txbytes =
49588 + ddesc_iso[i].length;
49589 + dwcep->desc_addr[i].status.b_iso_in.framenum =
49590 + dwcep->xiso_frame_num;
49591 + dwcep->desc_addr[i].status.b_iso_in.bs =
49592 + BS_HOST_READY;
49593 + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
49594 + dwcep->desc_addr[i].status.b_iso_in.sp =
49595 + (ddesc_iso[i].length %
49596 + dwcep->maxpacket) ? 1 : 0;
49597 + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
49598 + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
49599 + dwcep->desc_addr[i].status.b_iso_in.l = 0;
49600 +
49601 + /* Process the last descriptor */
49602 + if (i == ereq->pio_pkt_count - 1) {
49603 + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
49604 + dwcep->desc_addr[i].status.b_iso_in.l = 1;
49605 + }
49606 + }
49607 +
49608 + /* Setup and start the transfer for this endpoint */
49609 + dwcep->xiso_active_xfers++;
49610 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
49611 + in_ep_regs[dwcep->num]->diepdma,
49612 + dwcep->dma_desc_addr);
49613 + diepctl.d32 = 0;
49614 + diepctl.b.epena = 1;
49615 + diepctl.b.cnak = 1;
49616 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
49617 + in_ep_regs[dwcep->num]->diepctl, 0,
49618 + diepctl.d32);
49619 + } else {
49620 + /* Setup DMA Descriptor chain for OUT Isoc request */
49621 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49622 + //if ((i % (nat + 1)) == 0)
49623 + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
49624 + dwcep->xiso_frame_num) & 0x3FFF;
49625 + dwcep->desc_addr[i].buf =
49626 + req->dma + ddesc_iso[i].offset;
49627 + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
49628 + ddesc_iso[i].length;
49629 + dwcep->desc_addr[i].status.b_iso_out.framenum =
49630 + dwcep->xiso_frame_num;
49631 + dwcep->desc_addr[i].status.b_iso_out.bs =
49632 + BS_HOST_READY;
49633 + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
49634 + dwcep->desc_addr[i].status.b_iso_out.sp =
49635 + (ddesc_iso[i].length %
49636 + dwcep->maxpacket) ? 1 : 0;
49637 + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
49638 + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
49639 + dwcep->desc_addr[i].status.b_iso_out.l = 0;
49640 +
49641 + /* Process the last descriptor */
49642 + if (i == ereq->pio_pkt_count - 1) {
49643 + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
49644 + dwcep->desc_addr[i].status.b_iso_out.l = 1;
49645 + }
49646 + }
49647 +
49648 + /* Setup and start the transfer for this endpoint */
49649 + dwcep->xiso_active_xfers++;
49650 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
49651 + dev_if->out_ep_regs[dwcep->num]->
49652 + doepdma, dwcep->dma_desc_addr);
49653 + diepctl.d32 = 0;
49654 + diepctl.b.epena = 1;
49655 + diepctl.b.cnak = 1;
49656 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
49657 + dev_if->out_ep_regs[dwcep->num]->
49658 + doepctl, 0, diepctl.d32);
49659 + }
49660 +
49661 + } else {
49662 + ep->stopped = 1;
49663 + }
49664 +
49665 + return 0;
49666 +}
49667 +
49668 +/**
49669 + * - Remove the request from the queue
49670 + */
49671 +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
49672 +{
49673 + dwc_otg_pcd_request_t *req = NULL;
49674 + struct dwc_iso_xreq_port *ereq = NULL;
49675 + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
49676 + dwc_ep_t *dwcep = NULL;
49677 + int i;
49678 +
49679 + //DWC_DEBUG();
49680 + dwcep = &ep->dwc_ep;
49681 +
49682 + /* Get the first pending request from the queue */
49683 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
49684 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
49685 + if (!req) {
49686 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
49687 + return;
49688 + }
49689 + dwcep->xiso_active_xfers--;
49690 + dwcep->xiso_queued_xfers--;
49691 + /* Remove this request from the queue */
49692 + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
49693 + } else {
49694 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
49695 + return;
49696 + }
49697 +
49698 + ep->stopped = 1;
49699 + ereq = &req->ext_req;
49700 + ddesc_iso = ereq->per_io_frame_descs;
49701 +
49702 + if (dwcep->xiso_active_xfers < 0) {
49703 + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
49704 + dwcep->xiso_active_xfers);
49705 + }
49706 +
49707 + /* Fill the Isoc descs of portable extended req from dma descriptors */
49708 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49709 + if (dwcep->is_in) { /* IN endpoints */
49710 + ddesc_iso[i].actual_length = ddesc_iso[i].length -
49711 + dwcep->desc_addr[i].status.b_iso_in.txbytes;
49712 + ddesc_iso[i].status =
49713 + dwcep->desc_addr[i].status.b_iso_in.txsts;
49714 + } else { /* OUT endpoints */
49715 + ddesc_iso[i].actual_length = ddesc_iso[i].length -
49716 + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
49717 + ddesc_iso[i].status =
49718 + dwcep->desc_addr[i].status.b_iso_out.rxsts;
49719 + }
49720 + }
49721 +
49722 + DWC_SPINUNLOCK(ep->pcd->lock);
49723 +
49724 + /* Call the completion function in the non-portable logic */
49725 + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
49726 + &req->ext_req);
49727 +
49728 + DWC_SPINLOCK(ep->pcd->lock);
49729 +
49730 + /* Free the request - specific freeing needed for extended request object */
49731 + dwc_pcd_xiso_ereq_free(ep, req);
49732 +
49733 + /* Start the next request */
49734 + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
49735 +
49736 + return;
49737 +}
49738 +
49739 +/**
49740 + * Create and initialize the Isoc pkt descriptors of the extended request.
49741 + *
49742 + */
49743 +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
49744 + void *ereq_nonport,
49745 + int atomic_alloc)
49746 +{
49747 + struct dwc_iso_xreq_port *ereq = NULL;
49748 + struct dwc_iso_xreq_port *req_mapped = NULL;
49749 + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
49750 + uint32_t pkt_count;
49751 + int i;
49752 +
49753 + ereq = &req->ext_req;
49754 + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
49755 + pkt_count = req_mapped->pio_pkt_count;
49756 +
49757 + /* Create the isoc descs */
49758 + if (atomic_alloc) {
49759 + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
49760 + } else {
49761 + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
49762 + }
49763 +
49764 + if (!ipds) {
49765 + DWC_ERROR("Failed to allocate isoc descriptors");
49766 + return -DWC_E_NO_MEMORY;
49767 + }
49768 +
49769 + /* Initialize the extended request fields */
49770 + ereq->per_io_frame_descs = ipds;
49771 + ereq->error_count = 0;
49772 + ereq->pio_alloc_pkt_count = pkt_count;
49773 + ereq->pio_pkt_count = pkt_count;
49774 + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
49775 +
49776 + /* Init the Isoc descriptors */
49777 + for (i = 0; i < pkt_count; i++) {
49778 + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
49779 + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
49780 + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
49781 + ipds[i].actual_length =
49782 + req_mapped->per_io_frame_descs[i].actual_length;
49783 + }
49784 +
49785 + return 0;
49786 +}
49787 +
49788 +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
49789 +{
49790 + struct dwc_iso_pkt_desc_port *xfd = NULL;
49791 + int i;
49792 +
49793 + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
49794 + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
49795 + DWC_DEBUG("error_count=%d", ereq->error_count);
49796 + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
49797 + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
49798 + DWC_DEBUG("res=%d", ereq->res);
49799 +
49800 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49801 + xfd = &ereq->per_io_frame_descs[0];
49802 + DWC_DEBUG("FD #%d", i);
49803 +
49804 + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
49805 + DWC_DEBUG("xfd->length=%d", xfd->length);
49806 + DWC_DEBUG("xfd->offset=%d", xfd->offset);
49807 + DWC_DEBUG("xfd->status=%d", xfd->status);
49808 + }
49809 +}
49810 +
49811 +/**
49812 + *
49813 + */
49814 +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
49815 + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
49816 + int zero, void *req_handle, int atomic_alloc,
49817 + void *ereq_nonport)
49818 +{
49819 + dwc_otg_pcd_request_t *req = NULL;
49820 + dwc_otg_pcd_ep_t *ep;
49821 + dwc_irqflags_t flags;
49822 + int res;
49823 +
49824 + ep = get_ep_from_handle(pcd, ep_handle);
49825 + if (!ep) {
49826 + DWC_WARN("bad ep\n");
49827 + return -DWC_E_INVALID;
49828 + }
49829 +
49830 + /* We support this extension only for DDMA mode */
49831 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
49832 + if (!GET_CORE_IF(pcd)->dma_desc_enable)
49833 + return -DWC_E_INVALID;
49834 +
49835 + /* Create a dwc_otg_pcd_request_t object */
49836 + if (atomic_alloc) {
49837 + req = DWC_ALLOC_ATOMIC(sizeof(*req));
49838 + } else {
49839 + req = DWC_ALLOC(sizeof(*req));
49840 + }
49841 +
49842 + if (!req) {
49843 + return -DWC_E_NO_MEMORY;
49844 + }
49845 +
49846 + /* Create the Isoc descs for this request which shall be the exact match
49847 + * of the structure sent to us from the non-portable logic */
49848 + res =
49849 + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
49850 + if (res) {
49851 + DWC_WARN("Failed to init the Isoc descriptors");
49852 + DWC_FREE(req);
49853 + return res;
49854 + }
49855 +
49856 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49857 +
49858 + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
49859 + req->buf = buf;
49860 + req->dma = dma_buf;
49861 + req->length = buflen;
49862 + req->sent_zlp = zero;
49863 + req->priv = req_handle;
49864 +
49865 + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49866 + ep->dwc_ep.dma_addr = dma_buf;
49867 + ep->dwc_ep.start_xfer_buff = buf;
49868 + ep->dwc_ep.xfer_buff = buf;
49869 + ep->dwc_ep.xfer_len = 0;
49870 + ep->dwc_ep.xfer_count = 0;
49871 + ep->dwc_ep.sent_zlp = 0;
49872 + ep->dwc_ep.total_len = buflen;
49873 +
49874 + /* Add this request to the tail */
49875 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
49876 + ep->dwc_ep.xiso_queued_xfers++;
49877 +
49878 +//DWC_DEBUG("CP_0");
49879 +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
49880 +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
49881 +//prn_ext_request(&req->ext_req);
49882 +
49883 + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49884 +
49885 + /* If the req->status == ASAP then check if there is any active transfer
49886 + * for this endpoint. If no active transfers, then get the first entry
49887 + * from the queue and start that transfer
49888 + */
49889 + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
49890 + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
49891 + if (res) {
49892 + DWC_WARN("Failed to start the next Isoc transfer");
49893 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49894 + DWC_FREE(req);
49895 + return res;
49896 + }
49897 + }
49898 +
49899 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49900 + return 0;
49901 +}
49902 +
49903 +#endif
49904 +/* END ifdef DWC_UTE_PER_IO ***************************************************/
49905 +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
49906 + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
49907 + int zero, void *req_handle, int atomic_alloc)
49908 +{
49909 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49910 + dwc_irqflags_t flags;
49911 + dwc_otg_pcd_request_t *req;
49912 + dwc_otg_pcd_ep_t *ep;
49913 + uint32_t max_transfer;
49914 +
49915 + ep = get_ep_from_handle(pcd, ep_handle);
49916 + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
49917 + DWC_WARN("bad ep\n");
49918 + return -DWC_E_INVALID;
49919 + }
49920 +
49921 + if (atomic_alloc) {
49922 + req = DWC_ALLOC_ATOMIC(sizeof(*req));
49923 + } else {
49924 + req = DWC_ALLOC(sizeof(*req));
49925 + }
49926 +
49927 + if (!req) {
49928 + return -DWC_E_NO_MEMORY;
49929 + }
49930 + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
49931 + if (!GET_CORE_IF(pcd)->core_params->opt) {
49932 + if (ep->dwc_ep.num != 0) {
49933 + DWC_ERROR("queue req %p, len %d buf %p\n",
49934 + req_handle, buflen, buf);
49935 + }
49936 + }
49937 +
49938 + req->buf = buf;
49939 + req->dma = dma_buf;
49940 + req->length = buflen;
49941 + req->sent_zlp = zero;
49942 + req->priv = req_handle;
49943 + req->dw_align_buf = NULL;
49944 + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
49945 + && !GET_CORE_IF(pcd)->dma_desc_enable)
49946 + req->dw_align_buf = DWC_DMA_ALLOC(dev, buflen,
49947 + &req->dw_align_buf_dma);
49948 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49949 +
49950 + /*
49951 + * After adding request to the queue for IN ISOC wait for In Token Received
49952 + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
49953 + * Received when EP is disabled interrupt to obtain starting microframe
49954 + * (odd/even) start transfer
49955 + */
49956 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
49957 + if (req != 0) {
49958 + depctl_data_t depctl = {.d32 =
49959 + DWC_READ_REG32(&pcd->core_if->dev_if->
49960 + in_ep_regs[ep->dwc_ep.num]->
49961 + diepctl) };
49962 + ++pcd->request_pending;
49963 +
49964 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
49965 + if (ep->dwc_ep.is_in) {
49966 + depctl.b.cnak = 1;
49967 + DWC_WRITE_REG32(&pcd->core_if->dev_if->
49968 + in_ep_regs[ep->dwc_ep.num]->
49969 + diepctl, depctl.d32);
49970 + }
49971 +
49972 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49973 + }
49974 + return 0;
49975 + }
49976 +
49977 + /*
49978 + * For EP0 IN without premature status, zlp is required?
49979 + */
49980 + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
49981 + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
49982 + //_req->zero = 1;
49983 + }
49984 +
49985 + /* Start the transfer */
49986 + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
49987 + /* EP0 Transfer? */
49988 + if (ep->dwc_ep.num == 0) {
49989 + switch (pcd->ep0state) {
49990 + case EP0_IN_DATA_PHASE:
49991 + DWC_DEBUGPL(DBG_PCD,
49992 + "%s ep0: EP0_IN_DATA_PHASE\n",
49993 + __func__);
49994 + break;
49995 +
49996 + case EP0_OUT_DATA_PHASE:
49997 + DWC_DEBUGPL(DBG_PCD,
49998 + "%s ep0: EP0_OUT_DATA_PHASE\n",
49999 + __func__);
50000 + if (pcd->request_config) {
50001 + /* Complete STATUS PHASE */
50002 + ep->dwc_ep.is_in = 1;
50003 + pcd->ep0state = EP0_IN_STATUS_PHASE;
50004 + }
50005 + break;
50006 +
50007 + case EP0_IN_STATUS_PHASE:
50008 + DWC_DEBUGPL(DBG_PCD,
50009 + "%s ep0: EP0_IN_STATUS_PHASE\n",
50010 + __func__);
50011 + break;
50012 +
50013 + default:
50014 + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
50015 + pcd->ep0state);
50016 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50017 + return -DWC_E_SHUTDOWN;
50018 + }
50019 +
50020 + ep->dwc_ep.dma_addr = dma_buf;
50021 + ep->dwc_ep.start_xfer_buff = buf;
50022 + ep->dwc_ep.xfer_buff = buf;
50023 + ep->dwc_ep.xfer_len = buflen;
50024 + ep->dwc_ep.xfer_count = 0;
50025 + ep->dwc_ep.sent_zlp = 0;
50026 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
50027 +
50028 + if (zero) {
50029 + if ((ep->dwc_ep.xfer_len %
50030 + ep->dwc_ep.maxpacket == 0)
50031 + && (ep->dwc_ep.xfer_len != 0)) {
50032 + ep->dwc_ep.sent_zlp = 1;
50033 + }
50034 +
50035 + }
50036 +
50037 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
50038 + &ep->dwc_ep);
50039 + } // non-ep0 endpoints
50040 + else {
50041 +#ifdef DWC_UTE_CFI
50042 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
50043 + /* store the request length */
50044 + ep->dwc_ep.cfi_req_len = buflen;
50045 + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
50046 + ep, req);
50047 + } else {
50048 +#endif
50049 + max_transfer =
50050 + GET_CORE_IF(ep->pcd)->core_params->
50051 + max_transfer_size;
50052 +
50053 + /* Setup and start the Transfer */
50054 + if (req->dw_align_buf){
50055 + if (ep->dwc_ep.is_in)
50056 + dwc_memcpy(req->dw_align_buf,
50057 + buf, buflen);
50058 + ep->dwc_ep.dma_addr =
50059 + req->dw_align_buf_dma;
50060 + ep->dwc_ep.start_xfer_buff =
50061 + req->dw_align_buf;
50062 + ep->dwc_ep.xfer_buff =
50063 + req->dw_align_buf;
50064 + } else {
50065 + ep->dwc_ep.dma_addr = dma_buf;
50066 + ep->dwc_ep.start_xfer_buff = buf;
50067 + ep->dwc_ep.xfer_buff = buf;
50068 + }
50069 + ep->dwc_ep.xfer_len = 0;
50070 + ep->dwc_ep.xfer_count = 0;
50071 + ep->dwc_ep.sent_zlp = 0;
50072 + ep->dwc_ep.total_len = buflen;
50073 +
50074 + ep->dwc_ep.maxxfer = max_transfer;
50075 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
50076 + uint32_t out_max_xfer =
50077 + DDMA_MAX_TRANSFER_SIZE -
50078 + (DDMA_MAX_TRANSFER_SIZE % 4);
50079 + if (ep->dwc_ep.is_in) {
50080 + if (ep->dwc_ep.maxxfer >
50081 + DDMA_MAX_TRANSFER_SIZE) {
50082 + ep->dwc_ep.maxxfer =
50083 + DDMA_MAX_TRANSFER_SIZE;
50084 + }
50085 + } else {
50086 + if (ep->dwc_ep.maxxfer >
50087 + out_max_xfer) {
50088 + ep->dwc_ep.maxxfer =
50089 + out_max_xfer;
50090 + }
50091 + }
50092 + }
50093 + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
50094 + ep->dwc_ep.maxxfer -=
50095 + (ep->dwc_ep.maxxfer %
50096 + ep->dwc_ep.maxpacket);
50097 + }
50098 +
50099 + if (zero) {
50100 + if ((ep->dwc_ep.total_len %
50101 + ep->dwc_ep.maxpacket == 0)
50102 + && (ep->dwc_ep.total_len != 0)) {
50103 + ep->dwc_ep.sent_zlp = 1;
50104 + }
50105 + }
50106 +#ifdef DWC_UTE_CFI
50107 + }
50108 +#endif
50109 + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
50110 + &ep->dwc_ep);
50111 + }
50112 + }
50113 +
50114 + if (req != 0) {
50115 + ++pcd->request_pending;
50116 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50117 + if (ep->dwc_ep.is_in && ep->stopped
50118 + && !(GET_CORE_IF(pcd)->dma_enable)) {
50119 + /** @todo NGS Create a function for this. */
50120 + diepmsk_data_t diepmsk = {.d32 = 0 };
50121 + diepmsk.b.intktxfemp = 1;
50122 + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
50123 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
50124 + dev_if->dev_global_regs->diepeachintmsk
50125 + [ep->dwc_ep.num], 0,
50126 + diepmsk.d32);
50127 + } else {
50128 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
50129 + dev_if->dev_global_regs->
50130 + diepmsk, 0, diepmsk.d32);
50131 + }
50132 +
50133 + }
50134 + }
50135 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50136 +
50137 + return 0;
50138 +}
50139 +
50140 +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
50141 + void *req_handle)
50142 +{
50143 + dwc_irqflags_t flags;
50144 + dwc_otg_pcd_request_t *req;
50145 + dwc_otg_pcd_ep_t *ep;
50146 +
50147 + ep = get_ep_from_handle(pcd, ep_handle);
50148 + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
50149 + DWC_WARN("bad argument\n");
50150 + return -DWC_E_INVALID;
50151 + }
50152 +
50153 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50154 +
50155 + /* make sure it's actually queued on this endpoint */
50156 + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
50157 + if (req->priv == (void *)req_handle) {
50158 + break;
50159 + }
50160 + }
50161 +
50162 + if (req->priv != (void *)req_handle) {
50163 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50164 + return -DWC_E_INVALID;
50165 + }
50166 +
50167 + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
50168 + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
50169 + } else {
50170 + req = NULL;
50171 + }
50172 +
50173 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50174 +
50175 + return req ? 0 : -DWC_E_SHUTDOWN;
50176 +
50177 +}
50178 +
50179 +/**
50180 + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
50181 + *
50182 + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
50183 + * requests. If the gadget driver clears the halt status, it will
50184 + * automatically unwedge the endpoint.
50185 + *
50186 + * Returns zero on success, else negative DWC error code.
50187 + */
50188 +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
50189 +{
50190 + dwc_otg_pcd_ep_t *ep;
50191 + dwc_irqflags_t flags;
50192 + int retval = 0;
50193 +
50194 + ep = get_ep_from_handle(pcd, ep_handle);
50195 +
50196 + if ((!ep->desc && ep != &pcd->ep0) ||
50197 + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
50198 + DWC_WARN("%s, bad ep\n", __func__);
50199 + return -DWC_E_INVALID;
50200 + }
50201 +
50202 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50203 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50204 + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
50205 + ep->dwc_ep.is_in ? "IN" : "OUT");
50206 + retval = -DWC_E_AGAIN;
50207 + } else {
50208 + /* This code needs to be reviewed */
50209 + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
50210 + dtxfsts_data_t txstatus;
50211 + fifosize_data_t txfifosize;
50212 +
50213 + txfifosize.d32 =
50214 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
50215 + core_global_regs->dtxfsiz[ep->dwc_ep.
50216 + tx_fifo_num]);
50217 + txstatus.d32 =
50218 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
50219 + dev_if->in_ep_regs[ep->dwc_ep.num]->
50220 + dtxfsts);
50221 +
50222 + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
50223 + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
50224 + retval = -DWC_E_AGAIN;
50225 + } else {
50226 + if (ep->dwc_ep.num == 0) {
50227 + pcd->ep0state = EP0_STALL;
50228 + }
50229 +
50230 + ep->stopped = 1;
50231 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
50232 + &ep->dwc_ep);
50233 + }
50234 + } else {
50235 + if (ep->dwc_ep.num == 0) {
50236 + pcd->ep0state = EP0_STALL;
50237 + }
50238 +
50239 + ep->stopped = 1;
50240 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50241 + }
50242 + }
50243 +
50244 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50245 +
50246 + return retval;
50247 +}
50248 +
50249 +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
50250 +{
50251 + dwc_otg_pcd_ep_t *ep;
50252 + dwc_irqflags_t flags;
50253 + int retval = 0;
50254 +
50255 + ep = get_ep_from_handle(pcd, ep_handle);
50256 +
50257 + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
50258 + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
50259 + DWC_WARN("%s, bad ep\n", __func__);
50260 + return -DWC_E_INVALID;
50261 + }
50262 +
50263 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50264 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50265 + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
50266 + ep->dwc_ep.is_in ? "IN" : "OUT");
50267 + retval = -DWC_E_AGAIN;
50268 + } else if (value == 0) {
50269 + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50270 + } else if (value == 1) {
50271 + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
50272 + dtxfsts_data_t txstatus;
50273 + fifosize_data_t txfifosize;
50274 +
50275 + txfifosize.d32 =
50276 + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
50277 + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
50278 + txstatus.d32 =
50279 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
50280 + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
50281 +
50282 + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
50283 + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
50284 + retval = -DWC_E_AGAIN;
50285 + } else {
50286 + if (ep->dwc_ep.num == 0) {
50287 + pcd->ep0state = EP0_STALL;
50288 + }
50289 +
50290 + ep->stopped = 1;
50291 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
50292 + &ep->dwc_ep);
50293 + }
50294 + } else {
50295 + if (ep->dwc_ep.num == 0) {
50296 + pcd->ep0state = EP0_STALL;
50297 + }
50298 +
50299 + ep->stopped = 1;
50300 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50301 + }
50302 + } else if (value == 2) {
50303 + ep->dwc_ep.stall_clear_flag = 0;
50304 + } else if (value == 3) {
50305 + ep->dwc_ep.stall_clear_flag = 1;
50306 + }
50307 +
50308 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50309 +
50310 + return retval;
50311 +}
50312 +
50313 +/**
50314 + * This function initiates remote wakeup of the host from suspend state.
50315 + */
50316 +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
50317 +{
50318 + dctl_data_t dctl = { 0 };
50319 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50320 + dsts_data_t dsts;
50321 +
50322 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
50323 + if (!dsts.b.suspsts) {
50324 + DWC_WARN("Remote wakeup while is not in suspend state\n");
50325 + }
50326 + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
50327 + if (pcd->remote_wakeup_enable) {
50328 + if (set) {
50329 +
50330 + if (core_if->adp_enable) {
50331 + gpwrdn_data_t gpwrdn;
50332 +
50333 + dwc_otg_adp_probe_stop(core_if);
50334 +
50335 + /* Mask SRP detected interrupt from Power Down Logic */
50336 + gpwrdn.d32 = 0;
50337 + gpwrdn.b.srp_det_msk = 1;
50338 + DWC_MODIFY_REG32(&core_if->
50339 + core_global_regs->gpwrdn,
50340 + gpwrdn.d32, 0);
50341 +
50342 + /* Disable Power Down Logic */
50343 + gpwrdn.d32 = 0;
50344 + gpwrdn.b.pmuactv = 1;
50345 + DWC_MODIFY_REG32(&core_if->
50346 + core_global_regs->gpwrdn,
50347 + gpwrdn.d32, 0);
50348 +
50349 + /*
50350 + * Initialize the Core for Device mode.
50351 + */
50352 + core_if->op_state = B_PERIPHERAL;
50353 + dwc_otg_core_init(core_if);
50354 + dwc_otg_enable_global_interrupts(core_if);
50355 + cil_pcd_start(core_if);
50356 +
50357 + dwc_otg_initiate_srp(core_if);
50358 + }
50359 +
50360 + dctl.b.rmtwkupsig = 1;
50361 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
50362 + dctl, 0, dctl.d32);
50363 + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
50364 +
50365 + dwc_mdelay(2);
50366 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
50367 + dctl, dctl.d32, 0);
50368 + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
50369 + }
50370 + } else {
50371 + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
50372 + }
50373 +}
50374 +
50375 +#ifdef CONFIG_USB_DWC_OTG_LPM
50376 +/**
50377 + * This function initiates remote wakeup of the host from L1 sleep state.
50378 + */
50379 +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
50380 +{
50381 + glpmcfg_data_t lpmcfg;
50382 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50383 +
50384 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
50385 +
50386 + /* Check if we are in L1 state */
50387 + if (!lpmcfg.b.prt_sleep_sts) {
50388 + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
50389 + return;
50390 + }
50391 +
50392 + /* Check if host allows remote wakeup */
50393 + if (!lpmcfg.b.rem_wkup_en) {
50394 + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
50395 + return;
50396 + }
50397 +
50398 + /* Check if Resume OK */
50399 + if (!lpmcfg.b.sleep_state_resumeok) {
50400 + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
50401 + return;
50402 + }
50403 +
50404 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
50405 + lpmcfg.b.en_utmi_sleep = 0;
50406 + lpmcfg.b.hird_thres &= (~(1 << 4));
50407 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
50408 +
50409 + if (set) {
50410 + dctl_data_t dctl = {.d32 = 0 };
50411 + dctl.b.rmtwkupsig = 1;
50412 + /* Set RmtWkUpSig bit to start remote wakup signaling.
50413 + * Hardware will automatically clear this bit.
50414 + */
50415 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
50416 + 0, dctl.d32);
50417 + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
50418 + }
50419 +
50420 +}
50421 +#endif
50422 +
50423 +/**
50424 + * Performs remote wakeup.
50425 + */
50426 +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
50427 +{
50428 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50429 + dwc_irqflags_t flags;
50430 + if (dwc_otg_is_device_mode(core_if)) {
50431 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50432 +#ifdef CONFIG_USB_DWC_OTG_LPM
50433 + if (core_if->lx_state == DWC_OTG_L1) {
50434 + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
50435 + } else {
50436 +#endif
50437 + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
50438 +#ifdef CONFIG_USB_DWC_OTG_LPM
50439 + }
50440 +#endif
50441 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50442 + }
50443 + return;
50444 +}
50445 +
50446 +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
50447 +{
50448 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50449 + dctl_data_t dctl = { 0 };
50450 +
50451 + if (dwc_otg_is_device_mode(core_if)) {
50452 + dctl.b.sftdiscon = 1;
50453 + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
50454 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
50455 + dwc_udelay(no_of_usecs);
50456 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
50457 +
50458 + } else{
50459 + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
50460 + }
50461 + return;
50462 +
50463 +}
50464 +
50465 +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
50466 +{
50467 + dsts_data_t dsts;
50468 + gotgctl_data_t gotgctl;
50469 +
50470 + /*
50471 + * This function starts the Protocol if no session is in progress. If
50472 + * a session is already in progress, but the device is suspended,
50473 + * remote wakeup signaling is started.
50474 + */
50475 +
50476 + /* Check if valid session */
50477 + gotgctl.d32 =
50478 + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
50479 + if (gotgctl.b.bsesvld) {
50480 + /* Check if suspend state */
50481 + dsts.d32 =
50482 + DWC_READ_REG32(&
50483 + (GET_CORE_IF(pcd)->dev_if->
50484 + dev_global_regs->dsts));
50485 + if (dsts.b.suspsts) {
50486 + dwc_otg_pcd_remote_wakeup(pcd, 1);
50487 + }
50488 + } else {
50489 + dwc_otg_pcd_initiate_srp(pcd);
50490 + }
50491 +
50492 + return 0;
50493 +
50494 +}
50495 +
50496 +/**
50497 + * Start the SRP timer to detect when the SRP does not complete within
50498 + * 6 seconds.
50499 + *
50500 + * @param pcd the pcd structure.
50501 + */
50502 +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
50503 +{
50504 + dwc_irqflags_t flags;
50505 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50506 + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
50507 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50508 +}
50509 +
50510 +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
50511 +{
50512 + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
50513 +}
50514 +
50515 +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
50516 +{
50517 + return GET_CORE_IF(pcd)->core_params->lpm_enable;
50518 +}
50519 +
50520 +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
50521 +{
50522 + return pcd->b_hnp_enable;
50523 +}
50524 +
50525 +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
50526 +{
50527 + return pcd->a_hnp_support;
50528 +}
50529 +
50530 +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
50531 +{
50532 + return pcd->a_alt_hnp_support;
50533 +}
50534 +
50535 +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
50536 +{
50537 + return pcd->remote_wakeup_enable;
50538 +}
50539 +
50540 +#endif /* DWC_HOST_ONLY */
50541 --- /dev/null
50542 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
50543 @@ -0,0 +1,273 @@
50544 +/* ==========================================================================
50545 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
50546 + * $Revision: #48 $
50547 + * $Date: 2012/08/10 $
50548 + * $Change: 2047372 $
50549 + *
50550 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
50551 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
50552 + * otherwise expressly agreed to in writing between Synopsys and you.
50553 + *
50554 + * The Software IS NOT an item of Licensed Software or Licensed Product under
50555 + * any End User Software License Agreement or Agreement for Licensed Product
50556 + * with Synopsys or any supplement thereto. You are permitted to use and
50557 + * redistribute this Software in source and binary forms, with or without
50558 + * modification, provided that redistributions of source code must retain this
50559 + * notice. You may not view, use, disclose, copy or distribute this file or
50560 + * any information contained herein except pursuant to this license grant from
50561 + * Synopsys. If you do not agree with this notice, including the disclaimer
50562 + * below, then you are not authorized to use the Software.
50563 + *
50564 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
50565 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50566 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50567 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
50568 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50569 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50570 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50571 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50572 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50573 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
50574 + * DAMAGE.
50575 + * ========================================================================== */
50576 +#ifndef DWC_HOST_ONLY
50577 +#if !defined(__DWC_PCD_H__)
50578 +#define __DWC_PCD_H__
50579 +
50580 +#include "dwc_otg_os_dep.h"
50581 +#include "usb.h"
50582 +#include "dwc_otg_cil.h"
50583 +#include "dwc_otg_pcd_if.h"
50584 +#include "dwc_otg_driver.h"
50585 +
50586 +struct cfiobject;
50587 +
50588 +/**
50589 + * @file
50590 + *
50591 + * This file contains the structures, constants, and interfaces for
50592 + * the Perpherial Contoller Driver (PCD).
50593 + *
50594 + * The Peripheral Controller Driver (PCD) for Linux will implement the
50595 + * Gadget API, so that the existing Gadget drivers can be used. For
50596 + * the Mass Storage Function driver the File-backed USB Storage Gadget
50597 + * (FBS) driver will be used. The FBS driver supports the
50598 + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
50599 + * transports.
50600 + *
50601 + */
50602 +
50603 +/** Invalid DMA Address */
50604 +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
50605 +
50606 +/** Max Transfer size for any EP */
50607 +#define DDMA_MAX_TRANSFER_SIZE 65535
50608 +
50609 +/**
50610 + * Get the pointer to the core_if from the pcd pointer.
50611 + */
50612 +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
50613 +
50614 +/**
50615 + * States of EP0.
50616 + */
50617 +typedef enum ep0_state {
50618 + EP0_DISCONNECT, /* no host */
50619 + EP0_IDLE,
50620 + EP0_IN_DATA_PHASE,
50621 + EP0_OUT_DATA_PHASE,
50622 + EP0_IN_STATUS_PHASE,
50623 + EP0_OUT_STATUS_PHASE,
50624 + EP0_STALL,
50625 +} ep0state_e;
50626 +
50627 +/** Fordward declaration.*/
50628 +struct dwc_otg_pcd;
50629 +
50630 +/** DWC_otg iso request structure.
50631 + *
50632 + */
50633 +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
50634 +
50635 +#ifdef DWC_UTE_PER_IO
50636 +
50637 +/**
50638 + * This shall be the exact analogy of the same type structure defined in the
50639 + * usb_gadget.h. Each descriptor contains
50640 + */
50641 +struct dwc_iso_pkt_desc_port {
50642 + uint32_t offset;
50643 + uint32_t length; /* expected length */
50644 + uint32_t actual_length;
50645 + uint32_t status;
50646 +};
50647 +
50648 +struct dwc_iso_xreq_port {
50649 + /** transfer/submission flag */
50650 + uint32_t tr_sub_flags;
50651 + /** Start the request ASAP */
50652 +#define DWC_EREQ_TF_ASAP 0x00000002
50653 + /** Just enqueue the request w/o initiating a transfer */
50654 +#define DWC_EREQ_TF_ENQUEUE 0x00000004
50655 +
50656 + /**
50657 + * count of ISO packets attached to this request - shall
50658 + * not exceed the pio_alloc_pkt_count
50659 + */
50660 + uint32_t pio_pkt_count;
50661 + /** count of ISO packets allocated for this request */
50662 + uint32_t pio_alloc_pkt_count;
50663 + /** number of ISO packet errors */
50664 + uint32_t error_count;
50665 + /** reserved for future extension */
50666 + uint32_t res;
50667 + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
50668 + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
50669 +};
50670 +#endif
50671 +/** DWC_otg request structure.
50672 + * This structure is a list of requests.
50673 + */
50674 +typedef struct dwc_otg_pcd_request {
50675 + void *priv;
50676 + void *buf;
50677 + dwc_dma_t dma;
50678 + uint32_t length;
50679 + uint32_t actual;
50680 + unsigned sent_zlp:1;
50681 + /**
50682 + * Used instead of original buffer if
50683 + * it(physical address) is not dword-aligned.
50684 + **/
50685 + uint8_t *dw_align_buf;
50686 + dwc_dma_t dw_align_buf_dma;
50687 +
50688 + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
50689 +#ifdef DWC_UTE_PER_IO
50690 + struct dwc_iso_xreq_port ext_req;
50691 + //void *priv_ereq_nport; /* */
50692 +#endif
50693 +} dwc_otg_pcd_request_t;
50694 +
50695 +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
50696 +
50697 +/** PCD EP structure.
50698 + * This structure describes an EP, there is an array of EPs in the PCD
50699 + * structure.
50700 + */
50701 +typedef struct dwc_otg_pcd_ep {
50702 + /** USB EP Descriptor */
50703 + const usb_endpoint_descriptor_t *desc;
50704 +
50705 + /** queue of dwc_otg_pcd_requests. */
50706 + struct req_list queue;
50707 + unsigned stopped:1;
50708 + unsigned disabling:1;
50709 + unsigned dma:1;
50710 + unsigned queue_sof:1;
50711 +
50712 +#ifdef DWC_EN_ISOC
50713 + /** ISOC req handle passed */
50714 + void *iso_req_handle;
50715 +#endif //_EN_ISOC_
50716 +
50717 + /** DWC_otg ep data. */
50718 + dwc_ep_t dwc_ep;
50719 +
50720 + /** Pointer to PCD */
50721 + struct dwc_otg_pcd *pcd;
50722 +
50723 + void *priv;
50724 +} dwc_otg_pcd_ep_t;
50725 +
50726 +/** DWC_otg PCD Structure.
50727 + * This structure encapsulates the data for the dwc_otg PCD.
50728 + */
50729 +struct dwc_otg_pcd {
50730 + const struct dwc_otg_pcd_function_ops *fops;
50731 + /** The DWC otg device pointer */
50732 + struct dwc_otg_device *otg_dev;
50733 + /** Core Interface */
50734 + dwc_otg_core_if_t *core_if;
50735 + /** State of EP0 */
50736 + ep0state_e ep0state;
50737 + /** EP0 Request is pending */
50738 + unsigned ep0_pending:1;
50739 + /** Indicates when SET CONFIGURATION Request is in process */
50740 + unsigned request_config:1;
50741 + /** The state of the Remote Wakeup Enable. */
50742 + unsigned remote_wakeup_enable:1;
50743 + /** The state of the B-Device HNP Enable. */
50744 + unsigned b_hnp_enable:1;
50745 + /** The state of A-Device HNP Support. */
50746 + unsigned a_hnp_support:1;
50747 + /** The state of the A-Device Alt HNP support. */
50748 + unsigned a_alt_hnp_support:1;
50749 + /** Count of pending Requests */
50750 + unsigned request_pending;
50751 +
50752 + /** SETUP packet for EP0
50753 + * This structure is allocated as a DMA buffer on PCD initialization
50754 + * with enough space for up to 3 setup packets.
50755 + */
50756 + union {
50757 + usb_device_request_t req;
50758 + uint32_t d32[2];
50759 + } *setup_pkt;
50760 +
50761 + dwc_dma_t setup_pkt_dma_handle;
50762 +
50763 + /* Additional buffer and flag for CTRL_WR premature case */
50764 + uint8_t *backup_buf;
50765 + unsigned data_terminated;
50766 +
50767 + /** 2-byte dma buffer used to return status from GET_STATUS */
50768 + uint16_t *status_buf;
50769 + dwc_dma_t status_buf_dma_handle;
50770 +
50771 + /** EP0 */
50772 + dwc_otg_pcd_ep_t ep0;
50773 +
50774 + /** Array of IN EPs. */
50775 + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
50776 + /** Array of OUT EPs. */
50777 + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
50778 + /** number of valid EPs in the above array. */
50779 +// unsigned num_eps : 4;
50780 + dwc_spinlock_t *lock;
50781 +
50782 + /** Tasklet to defer starting of TEST mode transmissions until
50783 + * Status Phase has been completed.
50784 + */
50785 + dwc_tasklet_t *test_mode_tasklet;
50786 +
50787 + /** Tasklet to delay starting of xfer in DMA mode */
50788 + dwc_tasklet_t *start_xfer_tasklet;
50789 +
50790 + /** The test mode to enter when the tasklet is executed. */
50791 + unsigned test_mode;
50792 + /** The cfi_api structure that implements most of the CFI API
50793 + * and OTG specific core configuration functionality
50794 + */
50795 +#ifdef DWC_UTE_CFI
50796 + struct cfiobject *cfi;
50797 +#endif
50798 +
50799 +};
50800 +
50801 +static inline struct device *dwc_otg_pcd_to_dev(struct dwc_otg_pcd *pcd)
50802 +{
50803 + return &pcd->otg_dev->os_dep.platformdev->dev;
50804 +}
50805 +
50806 +//FIXME this functions should be static, and this prototypes should be removed
50807 +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
50808 +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
50809 + dwc_otg_pcd_request_t * req, int32_t status);
50810 +
50811 +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
50812 + void *req_handle);
50813 +
50814 +extern void do_test_mode(void *data);
50815 +#endif
50816 +#endif /* DWC_HOST_ONLY */
50817 --- /dev/null
50818 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
50819 @@ -0,0 +1,361 @@
50820 +/* ==========================================================================
50821 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
50822 + * $Revision: #11 $
50823 + * $Date: 2011/10/26 $
50824 + * $Change: 1873028 $
50825 + *
50826 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
50827 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
50828 + * otherwise expressly agreed to in writing between Synopsys and you.
50829 + *
50830 + * The Software IS NOT an item of Licensed Software or Licensed Product under
50831 + * any End User Software License Agreement or Agreement for Licensed Product
50832 + * with Synopsys or any supplement thereto. You are permitted to use and
50833 + * redistribute this Software in source and binary forms, with or without
50834 + * modification, provided that redistributions of source code must retain this
50835 + * notice. You may not view, use, disclose, copy or distribute this file or
50836 + * any information contained herein except pursuant to this license grant from
50837 + * Synopsys. If you do not agree with this notice, including the disclaimer
50838 + * below, then you are not authorized to use the Software.
50839 + *
50840 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
50841 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50842 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50843 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
50844 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50845 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50846 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50847 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50848 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50849 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
50850 + * DAMAGE.
50851 + * ========================================================================== */
50852 +#ifndef DWC_HOST_ONLY
50853 +
50854 +#if !defined(__DWC_PCD_IF_H__)
50855 +#define __DWC_PCD_IF_H__
50856 +
50857 +//#include "dwc_os.h"
50858 +#include "dwc_otg_core_if.h"
50859 +#include "dwc_otg_driver.h"
50860 +
50861 +/** @file
50862 + * This file defines DWC_OTG PCD Core API.
50863 + */
50864 +
50865 +struct dwc_otg_pcd;
50866 +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
50867 +
50868 +/** Maxpacket size for EP0 */
50869 +#define MAX_EP0_SIZE 64
50870 +/** Maxpacket size for any EP */
50871 +#define MAX_PACKET_SIZE 1024
50872 +
50873 +/** @name Function Driver Callbacks */
50874 +/** @{ */
50875 +
50876 +/** This function will be called whenever a previously queued request has
50877 + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
50878 + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
50879 + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
50880 + * parameters. */
50881 +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
50882 + void *req_handle, int32_t status,
50883 + uint32_t actual);
50884 +/**
50885 + * This function will be called whenever a previousle queued ISOC request has
50886 + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
50887 + * function.
50888 + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
50889 + * functions.
50890 + */
50891 +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
50892 + void *req_handle, int proc_buf_num);
50893 +/** This function should handle any SETUP request that cannot be handled by the
50894 + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
50895 + * class-specific requests, etc. The function must non-blocking.
50896 + *
50897 + * Returns 0 on success.
50898 + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
50899 + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
50900 + * Returns -DWC_E_SHUTDOWN on any other error. */
50901 +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
50902 +/** This is called whenever the device has been disconnected. The function
50903 + * driver should take appropriate action to clean up all pending requests in the
50904 + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
50905 + * state. */
50906 +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
50907 +/** This function is called when device has been connected. */
50908 +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
50909 +/** This function is called when device has been suspended */
50910 +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
50911 +/** This function is called when device has received LPM tokens, i.e.
50912 + * device has been sent to sleep state. */
50913 +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
50914 +/** This function is called when device has been resumed
50915 + * from suspend(L2) or L1 sleep state. */
50916 +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
50917 +/** This function is called whenever hnp params has been changed.
50918 + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
50919 + * to get hnp parameters. */
50920 +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
50921 +/** This function is called whenever USB RESET is detected. */
50922 +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
50923 +
50924 +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
50925 +
50926 +/**
50927 + *
50928 + * @param ep_handle Void pointer to the usb_ep structure
50929 + * @param ereq_port Pointer to the extended request structure created in the
50930 + * portable part.
50931 + */
50932 +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
50933 + void *req_handle, int32_t status,
50934 + void *ereq_port);
50935 +/** Function Driver Ops Data Structure */
50936 +struct dwc_otg_pcd_function_ops {
50937 + dwc_connect_cb_t connect;
50938 + dwc_disconnect_cb_t disconnect;
50939 + dwc_setup_cb_t setup;
50940 + dwc_completion_cb_t complete;
50941 + dwc_isoc_completion_cb_t isoc_complete;
50942 + dwc_suspend_cb_t suspend;
50943 + dwc_sleep_cb_t sleep;
50944 + dwc_resume_cb_t resume;
50945 + dwc_reset_cb_t reset;
50946 + dwc_hnp_params_changed_cb_t hnp_changed;
50947 + cfi_setup_cb_t cfi_setup;
50948 +#ifdef DWC_UTE_PER_IO
50949 + xiso_completion_cb_t xisoc_complete;
50950 +#endif
50951 +};
50952 +/** @} */
50953 +
50954 +/** @name Function Driver Functions */
50955 +/** @{ */
50956 +
50957 +/** Call this function to get pointer on dwc_otg_pcd_t,
50958 + * this pointer will be used for all PCD API functions.
50959 + *
50960 + * @param core_if The DWC_OTG Core
50961 + */
50962 +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev);
50963 +
50964 +/** Frees PCD allocated by dwc_otg_pcd_init
50965 + *
50966 + * @param pcd The PCD
50967 + */
50968 +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
50969 +
50970 +/** Call this to bind the function driver to the PCD Core.
50971 + *
50972 + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
50973 + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
50974 + */
50975 +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
50976 + const struct dwc_otg_pcd_function_ops *fops);
50977 +
50978 +/** Enables an endpoint for use. This function enables an endpoint in
50979 + * the PCD. The endpoint is described by the ep_desc which has the
50980 + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
50981 + * to the endpoint from other API functions and in callbacks. Normally this
50982 + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
50983 + * core for that interface.
50984 + *
50985 + * Returns -DWC_E_INVALID if invalid parameters were passed.
50986 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
50987 + * Returns 0 on success.
50988 + *
50989 + * @param pcd The PCD
50990 + * @param ep_desc Endpoint descriptor
50991 + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
50992 + */
50993 +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
50994 + const uint8_t * ep_desc, void *usb_ep);
50995 +
50996 +/** Disable the endpoint referenced by ep_handle.
50997 + *
50998 + * Returns -DWC_E_INVALID if invalid parameters were passed.
50999 + * Returns -DWC_E_SHUTDOWN if any other error occurred.
51000 + * Returns 0 on success. */
51001 +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
51002 +
51003 +/** Queue a data transfer request on the endpoint referenced by ep_handle.
51004 + * After the transfer is completes, the complete callback will be called with
51005 + * the request status.
51006 + *
51007 + * @param pcd The PCD
51008 + * @param ep_handle The handle of the endpoint
51009 + * @param buf The buffer for the data
51010 + * @param dma_buf The DMA buffer for the data
51011 + * @param buflen The length of the data transfer
51012 + * @param zero Specifies whether to send zero length last packet.
51013 + * @param req_handle Set this handle to any value to use to reference this
51014 + * request in the ep_dequeue function or from the complete callback
51015 + * @param atomic_alloc If driver need to perform atomic allocations
51016 + * for internal data structures.
51017 + *
51018 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51019 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51020 + * Returns 0 on success. */
51021 +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
51022 + uint8_t * buf, dwc_dma_t dma_buf,
51023 + uint32_t buflen, int zero, void *req_handle,
51024 + int atomic_alloc);
51025 +#ifdef DWC_UTE_PER_IO
51026 +/**
51027 + *
51028 + * @param ereq_nonport Pointer to the extended request part of the
51029 + * usb_request structure defined in usb_gadget.h file.
51030 + */
51031 +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
51032 + uint8_t * buf, dwc_dma_t dma_buf,
51033 + uint32_t buflen, int zero,
51034 + void *req_handle, int atomic_alloc,
51035 + void *ereq_nonport);
51036 +
51037 +#endif
51038 +
51039 +/** De-queue the specified data transfer that has not yet completed.
51040 + *
51041 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51042 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51043 + * Returns 0 on success. */
51044 +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
51045 + void *req_handle);
51046 +
51047 +/** Halt (STALL) an endpoint or clear it.
51048 + *
51049 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51050 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51051 + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
51052 + * Returns 0 on success. */
51053 +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
51054 +
51055 +/** This function */
51056 +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
51057 +
51058 +/** This function should be called on every hardware interrupt */
51059 +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
51060 +
51061 +/** This function returns current frame number */
51062 +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
51063 +
51064 +/**
51065 + * Start isochronous transfers on the endpoint referenced by ep_handle.
51066 + * For isochronous transfers duble buffering is used.
51067 + * After processing each of buffers comlete callback will be called with
51068 + * status for each transaction.
51069 + *
51070 + * @param pcd The PCD
51071 + * @param ep_handle The handle of the endpoint
51072 + * @param buf0 The virtual address of first data buffer
51073 + * @param buf1 The virtual address of second data buffer
51074 + * @param dma0 The DMA address of first data buffer
51075 + * @param dma1 The DMA address of second data buffer
51076 + * @param sync_frame Data pattern frame number
51077 + * @param dp_frame Data size for pattern frame
51078 + * @param data_per_frame Data size for regular frame
51079 + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
51080 + * @param buf_proc_intrvl Interval of ISOC Buffer processing
51081 + * @param req_handle Handle of ISOC request
51082 + * @param atomic_alloc Specefies whether to perform atomic allocation for
51083 + * internal data structures.
51084 + *
51085 + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
51086 + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
51087 + * Returns -DW_E_SHUTDOWN for any other error.
51088 + * Returns 0 on success
51089 + */
51090 +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
51091 + uint8_t * buf0, uint8_t * buf1,
51092 + dwc_dma_t dma0, dwc_dma_t dma1,
51093 + int sync_frame, int dp_frame,
51094 + int data_per_frame, int start_frame,
51095 + int buf_proc_intrvl, void *req_handle,
51096 + int atomic_alloc);
51097 +
51098 +/** Stop ISOC transfers on endpoint referenced by ep_handle.
51099 + *
51100 + * @param pcd The PCD
51101 + * @param ep_handle The handle of the endpoint
51102 + * @param req_handle Handle of ISOC request
51103 + *
51104 + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
51105 + * Returns 0 on success
51106 + */
51107 +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
51108 + void *req_handle);
51109 +
51110 +/** Get ISOC packet status.
51111 + *
51112 + * @param pcd The PCD
51113 + * @param ep_handle The handle of the endpoint
51114 + * @param iso_req_handle Isochronoush request handle
51115 + * @param packet Number of packet
51116 + * @param status Out parameter for returning status
51117 + * @param actual Out parameter for returning actual length
51118 + * @param offset Out parameter for returning offset
51119 + *
51120 + */
51121 +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
51122 + void *ep_handle,
51123 + void *iso_req_handle, int packet,
51124 + int *status, int *actual,
51125 + int *offset);
51126 +
51127 +/** Get ISOC packet count.
51128 + *
51129 + * @param pcd The PCD
51130 + * @param ep_handle The handle of the endpoint
51131 + * @param iso_req_handle
51132 + */
51133 +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
51134 + void *ep_handle,
51135 + void *iso_req_handle);
51136 +
51137 +/** This function starts the SRP Protocol if no session is in progress. If
51138 + * a session is already in progress, but the device is suspended,
51139 + * remote wakeup signaling is started.
51140 + */
51141 +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
51142 +
51143 +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
51144 +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
51145 +
51146 +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
51147 +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
51148 +
51149 +/** Initiate SRP */
51150 +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
51151 +
51152 +/** Starts remote wakeup signaling. */
51153 +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
51154 +
51155 +/** Starts micorsecond soft disconnect. */
51156 +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
51157 +/** This function returns whether device is dualspeed.*/
51158 +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
51159 +
51160 +/** This function returns whether device is otg. */
51161 +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
51162 +
51163 +/** These functions allow to get hnp parameters */
51164 +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
51165 +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
51166 +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
51167 +
51168 +/** CFI specific Interface functions */
51169 +/** Allocate a cfi buffer */
51170 +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
51171 + dwc_dma_t * addr, size_t buflen,
51172 + int flags);
51173 +
51174 +/******************************************************************************/
51175 +
51176 +/** @} */
51177 +
51178 +#endif /* __DWC_PCD_IF_H__ */
51179 +
51180 +#endif /* DWC_HOST_ONLY */
51181 --- /dev/null
51182 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
51183 @@ -0,0 +1,5148 @@
51184 +/* ==========================================================================
51185 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
51186 + * $Revision: #116 $
51187 + * $Date: 2012/08/10 $
51188 + * $Change: 2047372 $
51189 + *
51190 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
51191 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
51192 + * otherwise expressly agreed to in writing between Synopsys and you.
51193 + *
51194 + * The Software IS NOT an item of Licensed Software or Licensed Product under
51195 + * any End User Software License Agreement or Agreement for Licensed Product
51196 + * with Synopsys or any supplement thereto. You are permitted to use and
51197 + * redistribute this Software in source and binary forms, with or without
51198 + * modification, provided that redistributions of source code must retain this
51199 + * notice. You may not view, use, disclose, copy or distribute this file or
51200 + * any information contained herein except pursuant to this license grant from
51201 + * Synopsys. If you do not agree with this notice, including the disclaimer
51202 + * below, then you are not authorized to use the Software.
51203 + *
51204 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
51205 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51206 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51207 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
51208 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51209 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51210 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51211 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51212 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51213 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51214 + * DAMAGE.
51215 + * ========================================================================== */
51216 +#ifndef DWC_HOST_ONLY
51217 +
51218 +#include "dwc_otg_pcd.h"
51219 +
51220 +#ifdef DWC_UTE_CFI
51221 +#include "dwc_otg_cfi.h"
51222 +#endif
51223 +
51224 +#ifdef DWC_UTE_PER_IO
51225 +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
51226 +#endif
51227 +//#define PRINT_CFI_DMA_DESCS
51228 +
51229 +#define DEBUG_EP0
51230 +
51231 +/**
51232 + * This function updates OTG.
51233 + */
51234 +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
51235 +{
51236 +
51237 + if (reset) {
51238 + pcd->b_hnp_enable = 0;
51239 + pcd->a_hnp_support = 0;
51240 + pcd->a_alt_hnp_support = 0;
51241 + }
51242 +
51243 + if (pcd->fops->hnp_changed) {
51244 + pcd->fops->hnp_changed(pcd);
51245 + }
51246 +}
51247 +
51248 +/** @file
51249 + * This file contains the implementation of the PCD Interrupt handlers.
51250 + *
51251 + * The PCD handles the device interrupts. Many conditions can cause a
51252 + * device interrupt. When an interrupt occurs, the device interrupt
51253 + * service routine determines the cause of the interrupt and
51254 + * dispatches handling to the appropriate function. These interrupt
51255 + * handling functions are described below.
51256 + * All interrupt registers are processed from LSB to MSB.
51257 + */
51258 +
51259 +/**
51260 + * This function prints the ep0 state for debug purposes.
51261 + */
51262 +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
51263 +{
51264 +#ifdef DEBUG
51265 + char str[40];
51266 +
51267 + switch (pcd->ep0state) {
51268 + case EP0_DISCONNECT:
51269 + dwc_strcpy(str, "EP0_DISCONNECT");
51270 + break;
51271 + case EP0_IDLE:
51272 + dwc_strcpy(str, "EP0_IDLE");
51273 + break;
51274 + case EP0_IN_DATA_PHASE:
51275 + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
51276 + break;
51277 + case EP0_OUT_DATA_PHASE:
51278 + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
51279 + break;
51280 + case EP0_IN_STATUS_PHASE:
51281 + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
51282 + break;
51283 + case EP0_OUT_STATUS_PHASE:
51284 + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
51285 + break;
51286 + case EP0_STALL:
51287 + dwc_strcpy(str, "EP0_STALL");
51288 + break;
51289 + default:
51290 + dwc_strcpy(str, "EP0_INVALID");
51291 + }
51292 +
51293 + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
51294 +#endif
51295 +}
51296 +
51297 +/**
51298 + * This function calculate the size of the payload in the memory
51299 + * for out endpoints and prints size for debug purposes(used in
51300 + * 2.93a DevOutNak feature).
51301 + */
51302 +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
51303 +{
51304 +#ifdef DEBUG
51305 + deptsiz_data_t deptsiz_init = {.d32 = 0 };
51306 + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
51307 + int pack_num;
51308 + unsigned payload;
51309 +
51310 + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
51311 + deptsiz_updt.d32 =
51312 + DWC_READ_REG32(&pcd->core_if->dev_if->
51313 + out_ep_regs[ep->num]->doeptsiz);
51314 + /* Payload will be */
51315 + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
51316 + /* Packet count is decremented every time a packet
51317 + * is written to the RxFIFO not in to the external memory
51318 + * So, if payload == 0, then it means no packet was sent to ext memory*/
51319 + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
51320 + DWC_DEBUGPL(DBG_PCDV,
51321 + "Payload for EP%d-%s\n",
51322 + ep->num, (ep->is_in ? "IN" : "OUT"));
51323 + DWC_DEBUGPL(DBG_PCDV,
51324 + "Number of transfered bytes = 0x%08x\n", payload);
51325 + DWC_DEBUGPL(DBG_PCDV,
51326 + "Number of transfered packets = %d\n", pack_num);
51327 +#endif
51328 +}
51329 +
51330 +
51331 +#ifdef DWC_UTE_CFI
51332 +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
51333 + const uint8_t * epname, int descnum)
51334 +{
51335 + CFI_INFO
51336 + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
51337 + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
51338 + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
51339 + ddesc->status.b.bs);
51340 +}
51341 +#endif
51342 +
51343 +/**
51344 + * This function returns pointer to in ep struct with number ep_num
51345 + */
51346 +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
51347 +{
51348 + int i;
51349 + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
51350 + if (ep_num == 0) {
51351 + return &pcd->ep0;
51352 + } else {
51353 + for (i = 0; i < num_in_eps; ++i) {
51354 + if (pcd->in_ep[i].dwc_ep.num == ep_num)
51355 + return &pcd->in_ep[i];
51356 + }
51357 + return 0;
51358 + }
51359 +}
51360 +
51361 +/**
51362 + * This function returns pointer to out ep struct with number ep_num
51363 + */
51364 +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
51365 +{
51366 + int i;
51367 + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
51368 + if (ep_num == 0) {
51369 + return &pcd->ep0;
51370 + } else {
51371 + for (i = 0; i < num_out_eps; ++i) {
51372 + if (pcd->out_ep[i].dwc_ep.num == ep_num)
51373 + return &pcd->out_ep[i];
51374 + }
51375 + return 0;
51376 + }
51377 +}
51378 +
51379 +/**
51380 + * This functions gets a pointer to an EP from the wIndex address
51381 + * value of the control request.
51382 + */
51383 +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
51384 +{
51385 + dwc_otg_pcd_ep_t *ep;
51386 + uint32_t ep_num = UE_GET_ADDR(wIndex);
51387 +
51388 + if (ep_num == 0) {
51389 + ep = &pcd->ep0;
51390 + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
51391 + ep = &pcd->in_ep[ep_num - 1];
51392 + } else {
51393 + ep = &pcd->out_ep[ep_num - 1];
51394 + }
51395 +
51396 + return ep;
51397 +}
51398 +
51399 +/**
51400 + * This function checks the EP request queue, if the queue is not
51401 + * empty the next request is started.
51402 + */
51403 +void start_next_request(dwc_otg_pcd_ep_t * ep)
51404 +{
51405 + dwc_otg_pcd_request_t *req = 0;
51406 + uint32_t max_transfer =
51407 + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
51408 +
51409 +#ifdef DWC_UTE_CFI
51410 + struct dwc_otg_pcd *pcd;
51411 + pcd = ep->pcd;
51412 +#endif
51413 +
51414 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
51415 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
51416 +
51417 +#ifdef DWC_UTE_CFI
51418 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
51419 + ep->dwc_ep.cfi_req_len = req->length;
51420 + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
51421 + } else {
51422 +#endif
51423 + /* Setup and start the Transfer */
51424 + if (req->dw_align_buf) {
51425 + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
51426 + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
51427 + ep->dwc_ep.xfer_buff = req->dw_align_buf;
51428 + } else {
51429 + ep->dwc_ep.dma_addr = req->dma;
51430 + ep->dwc_ep.start_xfer_buff = req->buf;
51431 + ep->dwc_ep.xfer_buff = req->buf;
51432 + }
51433 + ep->dwc_ep.sent_zlp = 0;
51434 + ep->dwc_ep.total_len = req->length;
51435 + ep->dwc_ep.xfer_len = 0;
51436 + ep->dwc_ep.xfer_count = 0;
51437 +
51438 + ep->dwc_ep.maxxfer = max_transfer;
51439 + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
51440 + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
51441 + - (DDMA_MAX_TRANSFER_SIZE % 4);
51442 + if (ep->dwc_ep.is_in) {
51443 + if (ep->dwc_ep.maxxfer >
51444 + DDMA_MAX_TRANSFER_SIZE) {
51445 + ep->dwc_ep.maxxfer =
51446 + DDMA_MAX_TRANSFER_SIZE;
51447 + }
51448 + } else {
51449 + if (ep->dwc_ep.maxxfer > out_max_xfer) {
51450 + ep->dwc_ep.maxxfer =
51451 + out_max_xfer;
51452 + }
51453 + }
51454 + }
51455 + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
51456 + ep->dwc_ep.maxxfer -=
51457 + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
51458 + }
51459 + if (req->sent_zlp) {
51460 + if ((ep->dwc_ep.total_len %
51461 + ep->dwc_ep.maxpacket == 0)
51462 + && (ep->dwc_ep.total_len != 0)) {
51463 + ep->dwc_ep.sent_zlp = 1;
51464 + }
51465 +
51466 + }
51467 +#ifdef DWC_UTE_CFI
51468 + }
51469 +#endif
51470 + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
51471 + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
51472 + DWC_PRINTF("There are no more ISOC requests \n");
51473 + ep->dwc_ep.frame_num = 0xFFFFFFFF;
51474 + }
51475 +}
51476 +
51477 +/**
51478 + * This function handles the SOF Interrupts. At this time the SOF
51479 + * Interrupt is disabled.
51480 + */
51481 +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
51482 +{
51483 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51484 +
51485 + gintsts_data_t gintsts;
51486 +
51487 + DWC_DEBUGPL(DBG_PCD, "SOF\n");
51488 +
51489 + /* Clear interrupt */
51490 + gintsts.d32 = 0;
51491 + gintsts.b.sofintr = 1;
51492 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
51493 +
51494 + return 1;
51495 +}
51496 +
51497 +/**
51498 + * This function handles the Rx Status Queue Level Interrupt, which
51499 + * indicates that there is a least one packet in the Rx FIFO. The
51500 + * packets are moved from the FIFO to memory, where they will be
51501 + * processed when the Endpoint Interrupt Register indicates Transfer
51502 + * Complete or SETUP Phase Done.
51503 + *
51504 + * Repeat the following until the Rx Status Queue is empty:
51505 + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
51506 + * info
51507 + * -# If Receive FIFO is empty then skip to step Clear the interrupt
51508 + * and exit
51509 + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
51510 + * SETUP data to the buffer
51511 + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
51512 + * to the destination buffer
51513 + */
51514 +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
51515 +{
51516 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51517 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
51518 + gintmsk_data_t gintmask = {.d32 = 0 };
51519 + device_grxsts_data_t status;
51520 + dwc_otg_pcd_ep_t *ep;
51521 + gintsts_data_t gintsts;
51522 +#ifdef DEBUG
51523 + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
51524 +#endif
51525 +
51526 + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
51527 + /* Disable the Rx Status Queue Level interrupt */
51528 + gintmask.b.rxstsqlvl = 1;
51529 + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
51530 +
51531 + /* Get the Status from the top of the FIFO */
51532 + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
51533 +
51534 + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
51535 + "pktsts:%x Frame:%d(0x%0x)\n",
51536 + status.b.epnum, status.b.bcnt,
51537 + dpid_str[status.b.dpid],
51538 + status.b.pktsts, status.b.fn, status.b.fn);
51539 + /* Get pointer to EP structure */
51540 + ep = get_out_ep(pcd, status.b.epnum);
51541 +
51542 + switch (status.b.pktsts) {
51543 + case DWC_DSTS_GOUT_NAK:
51544 + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
51545 + break;
51546 + case DWC_STS_DATA_UPDT:
51547 + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
51548 + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
51549 + /** @todo NGS Check for buffer overflow? */
51550 + dwc_otg_read_packet(core_if,
51551 + ep->dwc_ep.xfer_buff,
51552 + status.b.bcnt);
51553 + ep->dwc_ep.xfer_count += status.b.bcnt;
51554 + ep->dwc_ep.xfer_buff += status.b.bcnt;
51555 + }
51556 + break;
51557 + case DWC_STS_XFER_COMP:
51558 + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
51559 + break;
51560 + case DWC_DSTS_SETUP_COMP:
51561 +#ifdef DEBUG_EP0
51562 + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
51563 +#endif
51564 + break;
51565 + case DWC_DSTS_SETUP_UPDT:
51566 + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
51567 +#ifdef DEBUG_EP0
51568 + DWC_DEBUGPL(DBG_PCD,
51569 + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
51570 + pcd->setup_pkt->req.bmRequestType,
51571 + pcd->setup_pkt->req.bRequest,
51572 + UGETW(pcd->setup_pkt->req.wValue),
51573 + UGETW(pcd->setup_pkt->req.wIndex),
51574 + UGETW(pcd->setup_pkt->req.wLength));
51575 +#endif
51576 + ep->dwc_ep.xfer_count += status.b.bcnt;
51577 + break;
51578 + default:
51579 + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
51580 + status.b.pktsts);
51581 + break;
51582 + }
51583 +
51584 + /* Enable the Rx Status Queue Level interrupt */
51585 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
51586 + /* Clear interrupt */
51587 + gintsts.d32 = 0;
51588 + gintsts.b.rxstsqlvl = 1;
51589 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
51590 +
51591 + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
51592 + return 1;
51593 +}
51594 +
51595 +/**
51596 + * This function examines the Device IN Token Learning Queue to
51597 + * determine the EP number of the last IN token received. This
51598 + * implementation is for the Mass Storage device where there are only
51599 + * 2 IN EPs (Control-IN and BULK-IN).
51600 + *
51601 + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
51602 + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
51603 + *
51604 + * @param core_if Programming view of DWC_otg controller.
51605 + *
51606 + */
51607 +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
51608 +{
51609 + dwc_otg_device_global_regs_t *dev_global_regs =
51610 + core_if->dev_if->dev_global_regs;
51611 + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
51612 + /* Number of Token Queue Registers */
51613 + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
51614 + dtknq1_data_t dtknqr1;
51615 + uint32_t in_tkn_epnums[4];
51616 + int ndx = 0;
51617 + int i = 0;
51618 + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
51619 + int epnum = 0;
51620 +
51621 + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
51622 +
51623 + /* Read the DTKNQ Registers */
51624 + for (i = 0; i < DTKNQ_REG_CNT; i++) {
51625 + in_tkn_epnums[i] = DWC_READ_REG32(addr);
51626 + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
51627 + in_tkn_epnums[i]);
51628 + if (addr == &dev_global_regs->dvbusdis) {
51629 + addr = &dev_global_regs->dtknqr3_dthrctl;
51630 + } else {
51631 + ++addr;
51632 + }
51633 +
51634 + }
51635 +
51636 + /* Copy the DTKNQR1 data to the bit field. */
51637 + dtknqr1.d32 = in_tkn_epnums[0];
51638 + /* Get the EP numbers */
51639 + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
51640 + ndx = dtknqr1.b.intknwptr - 1;
51641 +
51642 + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
51643 + if (ndx == -1) {
51644 + /** @todo Find a simpler way to calculate the max
51645 + * queue position.*/
51646 + int cnt = TOKEN_Q_DEPTH;
51647 + if (TOKEN_Q_DEPTH <= 6) {
51648 + cnt = TOKEN_Q_DEPTH - 1;
51649 + } else if (TOKEN_Q_DEPTH <= 14) {
51650 + cnt = TOKEN_Q_DEPTH - 7;
51651 + } else if (TOKEN_Q_DEPTH <= 22) {
51652 + cnt = TOKEN_Q_DEPTH - 15;
51653 + } else {
51654 + cnt = TOKEN_Q_DEPTH - 23;
51655 + }
51656 + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
51657 + } else {
51658 + if (ndx <= 5) {
51659 + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
51660 + } else if (ndx <= 13) {
51661 + ndx -= 6;
51662 + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
51663 + } else if (ndx <= 21) {
51664 + ndx -= 14;
51665 + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
51666 + } else if (ndx <= 29) {
51667 + ndx -= 22;
51668 + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
51669 + }
51670 + }
51671 + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
51672 + return epnum;
51673 +}
51674 +
51675 +/**
51676 + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
51677 + * The active request is checked for the next packet to be loaded into
51678 + * the non-periodic Tx FIFO.
51679 + */
51680 +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
51681 +{
51682 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51683 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
51684 + dwc_otg_dev_in_ep_regs_t *ep_regs;
51685 + gnptxsts_data_t txstatus = {.d32 = 0 };
51686 + gintsts_data_t gintsts;
51687 +
51688 + int epnum = 0;
51689 + dwc_otg_pcd_ep_t *ep = 0;
51690 + uint32_t len = 0;
51691 + int dwords;
51692 +
51693 + /* Get the epnum from the IN Token Learning Queue. */
51694 + epnum = get_ep_of_last_in_token(core_if);
51695 + ep = get_in_ep(pcd, epnum);
51696 +
51697 + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
51698 +
51699 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
51700 +
51701 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51702 + if (len > ep->dwc_ep.maxpacket) {
51703 + len = ep->dwc_ep.maxpacket;
51704 + }
51705 + dwords = (len + 3) / 4;
51706 +
51707 + /* While there is space in the queue and space in the FIFO and
51708 + * More data to tranfer, Write packets to the Tx FIFO */
51709 + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
51710 + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
51711 +
51712 + while (txstatus.b.nptxqspcavail > 0 &&
51713 + txstatus.b.nptxfspcavail > dwords &&
51714 + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
51715 + /* Write the FIFO */
51716 + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
51717 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51718 +
51719 + if (len > ep->dwc_ep.maxpacket) {
51720 + len = ep->dwc_ep.maxpacket;
51721 + }
51722 +
51723 + dwords = (len + 3) / 4;
51724 + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
51725 + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
51726 + }
51727 +
51728 + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
51729 + DWC_READ_REG32(&global_regs->gnptxsts));
51730 +
51731 + /* Clear interrupt */
51732 + gintsts.d32 = 0;
51733 + gintsts.b.nptxfempty = 1;
51734 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
51735 +
51736 + return 1;
51737 +}
51738 +
51739 +/**
51740 + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
51741 + * The active request is checked for the next packet to be loaded into
51742 + * apropriate Tx FIFO.
51743 + */
51744 +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
51745 +{
51746 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51747 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
51748 + dwc_otg_dev_in_ep_regs_t *ep_regs;
51749 + dtxfsts_data_t txstatus = {.d32 = 0 };
51750 + dwc_otg_pcd_ep_t *ep = 0;
51751 + uint32_t len = 0;
51752 + int dwords;
51753 +
51754 + ep = get_in_ep(pcd, epnum);
51755 +
51756 + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
51757 +
51758 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
51759 +
51760 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51761 +
51762 + if (len > ep->dwc_ep.maxpacket) {
51763 + len = ep->dwc_ep.maxpacket;
51764 + }
51765 +
51766 + dwords = (len + 3) / 4;
51767 +
51768 + /* While there is space in the queue and space in the FIFO and
51769 + * More data to tranfer, Write packets to the Tx FIFO */
51770 + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
51771 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
51772 +
51773 + while (txstatus.b.txfspcavail > dwords &&
51774 + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
51775 + ep->dwc_ep.xfer_len != 0) {
51776 + /* Write the FIFO */
51777 + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
51778 +
51779 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51780 + if (len > ep->dwc_ep.maxpacket) {
51781 + len = ep->dwc_ep.maxpacket;
51782 + }
51783 +
51784 + dwords = (len + 3) / 4;
51785 + txstatus.d32 =
51786 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
51787 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
51788 + txstatus.d32);
51789 + }
51790 +
51791 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
51792 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
51793 +
51794 + return 1;
51795 +}
51796 +
51797 +/**
51798 + * This function is called when the Device is disconnected. It stops
51799 + * any active requests and informs the Gadget driver of the
51800 + * disconnect.
51801 + */
51802 +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
51803 +{
51804 + int i, num_in_eps, num_out_eps;
51805 + dwc_otg_pcd_ep_t *ep;
51806 +
51807 + gintmsk_data_t intr_mask = {.d32 = 0 };
51808 +
51809 + DWC_SPINLOCK(pcd->lock);
51810 +
51811 + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
51812 + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
51813 +
51814 + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
51815 + /* don't disconnect drivers more than once */
51816 + if (pcd->ep0state == EP0_DISCONNECT) {
51817 + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
51818 + DWC_SPINUNLOCK(pcd->lock);
51819 + return;
51820 + }
51821 + pcd->ep0state = EP0_DISCONNECT;
51822 +
51823 + /* Reset the OTG state. */
51824 + dwc_otg_pcd_update_otg(pcd, 1);
51825 +
51826 + /* Disable the NP Tx Fifo Empty Interrupt. */
51827 + intr_mask.b.nptxfempty = 1;
51828 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
51829 + intr_mask.d32, 0);
51830 +
51831 + /* Flush the FIFOs */
51832 + /**@todo NGS Flush Periodic FIFOs */
51833 + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
51834 + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
51835 +
51836 + /* prevent new request submissions, kill any outstanding requests */
51837 + ep = &pcd->ep0;
51838 + dwc_otg_request_nuke(ep);
51839 + /* prevent new request submissions, kill any outstanding requests */
51840 + for (i = 0; i < num_in_eps; i++) {
51841 + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
51842 + dwc_otg_request_nuke(ep);
51843 + }
51844 + /* prevent new request submissions, kill any outstanding requests */
51845 + for (i = 0; i < num_out_eps; i++) {
51846 + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
51847 + dwc_otg_request_nuke(ep);
51848 + }
51849 +
51850 + /* report disconnect; the driver is already quiesced */
51851 + if (pcd->fops->disconnect) {
51852 + DWC_SPINUNLOCK(pcd->lock);
51853 + pcd->fops->disconnect(pcd);
51854 + DWC_SPINLOCK(pcd->lock);
51855 + }
51856 + DWC_SPINUNLOCK(pcd->lock);
51857 +}
51858 +
51859 +/**
51860 + * This interrupt indicates that ...
51861 + */
51862 +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
51863 +{
51864 + gintmsk_data_t intr_mask = {.d32 = 0 };
51865 + gintsts_data_t gintsts;
51866 +
51867 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
51868 + intr_mask.b.i2cintr = 1;
51869 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
51870 + intr_mask.d32, 0);
51871 +
51872 + /* Clear interrupt */
51873 + gintsts.d32 = 0;
51874 + gintsts.b.i2cintr = 1;
51875 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
51876 + gintsts.d32);
51877 + return 1;
51878 +}
51879 +
51880 +/**
51881 + * This interrupt indicates that ...
51882 + */
51883 +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
51884 +{
51885 + gintsts_data_t gintsts;
51886 +#if defined(VERBOSE)
51887 + DWC_PRINTF("Early Suspend Detected\n");
51888 +#endif
51889 +
51890 + /* Clear interrupt */
51891 + gintsts.d32 = 0;
51892 + gintsts.b.erlysuspend = 1;
51893 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
51894 + gintsts.d32);
51895 + return 1;
51896 +}
51897 +
51898 +/**
51899 + * This function configures EPO to receive SETUP packets.
51900 + *
51901 + * @todo NGS: Update the comments from the HW FS.
51902 + *
51903 + * -# Program the following fields in the endpoint specific registers
51904 + * for Control OUT EP 0, in order to receive a setup packet
51905 + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
51906 + * setup packets)
51907 + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
51908 + * to back setup packets)
51909 + * - In DMA mode, DOEPDMA0 Register with a memory address to
51910 + * store any setup packets received
51911 + *
51912 + * @param core_if Programming view of DWC_otg controller.
51913 + * @param pcd Programming view of the PCD.
51914 + */
51915 +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
51916 + dwc_otg_pcd_t * pcd)
51917 +{
51918 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
51919 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
51920 + dwc_otg_dev_dma_desc_t *dma_desc;
51921 + depctl_data_t doepctl = {.d32 = 0 };
51922 +
51923 +#ifdef VERBOSE
51924 + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
51925 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
51926 +#endif
51927 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
51928 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
51929 + if (doepctl.b.epena) {
51930 + return;
51931 + }
51932 + }
51933 +
51934 + doeptsize0.b.supcnt = 3;
51935 + doeptsize0.b.pktcnt = 1;
51936 + doeptsize0.b.xfersize = 8 * 3;
51937 +
51938 + if (core_if->dma_enable) {
51939 + if (!core_if->dma_desc_enable) {
51940 + /** put here as for Hermes mode deptisz register should not be written */
51941 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
51942 + doeptsize0.d32);
51943 +
51944 + /** @todo dma needs to handle multiple setup packets (up to 3) */
51945 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
51946 + pcd->setup_pkt_dma_handle);
51947 + } else {
51948 + dev_if->setup_desc_index =
51949 + (dev_if->setup_desc_index + 1) & 1;
51950 + dma_desc =
51951 + dev_if->setup_desc_addr[dev_if->setup_desc_index];
51952 +
51953 + /** DMA Descriptor Setup */
51954 + dma_desc->status.b.bs = BS_HOST_BUSY;
51955 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
51956 + dma_desc->status.b.sr = 0;
51957 + dma_desc->status.b.mtrf = 0;
51958 + }
51959 + dma_desc->status.b.l = 1;
51960 + dma_desc->status.b.ioc = 1;
51961 + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
51962 + dma_desc->buf = pcd->setup_pkt_dma_handle;
51963 + dma_desc->status.b.sts = 0;
51964 + dma_desc->status.b.bs = BS_HOST_READY;
51965 +
51966 + /** DOEPDMA0 Register write */
51967 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
51968 + dev_if->dma_setup_desc_addr
51969 + [dev_if->setup_desc_index]);
51970 + }
51971 +
51972 + } else {
51973 + /** put here as for Hermes mode deptisz register should not be written */
51974 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
51975 + doeptsize0.d32);
51976 + }
51977 +
51978 + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
51979 + doepctl.d32 = 0;
51980 + doepctl.b.epena = 1;
51981 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
51982 + doepctl.b.cnak = 1;
51983 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
51984 + } else {
51985 + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
51986 + }
51987 +
51988 +#ifdef VERBOSE
51989 + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
51990 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
51991 + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
51992 + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
51993 +#endif
51994 +}
51995 +
51996 +/**
51997 + * This interrupt occurs when a USB Reset is detected. When the USB
51998 + * Reset Interrupt occurs the device state is set to DEFAULT and the
51999 + * EP0 state is set to IDLE.
52000 + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
52001 + * -# Unmask the following interrupt bits
52002 + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
52003 + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
52004 + * - DOEPMSK.SETUP = 1
52005 + * - DOEPMSK.XferCompl = 1
52006 + * - DIEPMSK.XferCompl = 1
52007 + * - DIEPMSK.TimeOut = 1
52008 + * -# Program the following fields in the endpoint specific registers
52009 + * for Control OUT EP 0, in order to receive a setup packet
52010 + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
52011 + * setup packets)
52012 + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
52013 + * to back setup packets)
52014 + * - In DMA mode, DOEPDMA0 Register with a memory address to
52015 + * store any setup packets received
52016 + * At this point, all the required initialization, except for enabling
52017 + * the control 0 OUT endpoint is done, for receiving SETUP packets.
52018 + */
52019 +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
52020 +{
52021 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52022 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52023 + depctl_data_t doepctl = {.d32 = 0 };
52024 + depctl_data_t diepctl = {.d32 = 0 };
52025 + daint_data_t daintmsk = {.d32 = 0 };
52026 + doepmsk_data_t doepmsk = {.d32 = 0 };
52027 + diepmsk_data_t diepmsk = {.d32 = 0 };
52028 + dcfg_data_t dcfg = {.d32 = 0 };
52029 + grstctl_t resetctl = {.d32 = 0 };
52030 + dctl_data_t dctl = {.d32 = 0 };
52031 + int i = 0;
52032 + gintsts_data_t gintsts;
52033 + pcgcctl_data_t power = {.d32 = 0 };
52034 +
52035 + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
52036 + if (power.b.stoppclk) {
52037 + power.d32 = 0;
52038 + power.b.stoppclk = 1;
52039 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52040 +
52041 + power.b.pwrclmp = 1;
52042 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52043 +
52044 + power.b.rstpdwnmodule = 1;
52045 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52046 + }
52047 +
52048 + core_if->lx_state = DWC_OTG_L0;
52049 +
52050 + DWC_PRINTF("USB RESET\n");
52051 +#ifdef DWC_EN_ISOC
52052 + for (i = 1; i < 16; ++i) {
52053 + dwc_otg_pcd_ep_t *ep;
52054 + dwc_ep_t *dwc_ep;
52055 + ep = get_in_ep(pcd, i);
52056 + if (ep != 0) {
52057 + dwc_ep = &ep->dwc_ep;
52058 + dwc_ep->next_frame = 0xffffffff;
52059 + }
52060 + }
52061 +#endif /* DWC_EN_ISOC */
52062 +
52063 + /* reset the HNP settings */
52064 + dwc_otg_pcd_update_otg(pcd, 1);
52065 +
52066 + /* Clear the Remote Wakeup Signalling */
52067 + dctl.b.rmtwkupsig = 1;
52068 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
52069 +
52070 + /* Set NAK for all OUT EPs */
52071 + doepctl.b.snak = 1;
52072 + for (i = 0; i <= dev_if->num_out_eps; i++) {
52073 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
52074 + }
52075 +
52076 + /* Flush the NP Tx FIFO */
52077 + dwc_otg_flush_tx_fifo(core_if, 0x10);
52078 + /* Flush the Learning Queue */
52079 + resetctl.b.intknqflsh = 1;
52080 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
52081 +
52082 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
52083 + core_if->start_predict = 0;
52084 + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
52085 + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
52086 + }
52087 + core_if->nextep_seq[0] = 0;
52088 + core_if->first_in_nextep_seq = 0;
52089 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
52090 + diepctl.b.nextep = 0;
52091 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
52092 +
52093 + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
52094 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
52095 + dcfg.b.epmscnt = 2;
52096 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
52097 +
52098 + DWC_DEBUGPL(DBG_PCDV,
52099 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
52100 + __func__, core_if->first_in_nextep_seq);
52101 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
52102 + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
52103 + }
52104 + }
52105 +
52106 + if (core_if->multiproc_int_enable) {
52107 + daintmsk.b.inep0 = 1;
52108 + daintmsk.b.outep0 = 1;
52109 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
52110 + daintmsk.d32);
52111 +
52112 + doepmsk.b.setup = 1;
52113 + doepmsk.b.xfercompl = 1;
52114 + doepmsk.b.ahberr = 1;
52115 + doepmsk.b.epdisabled = 1;
52116 +
52117 + if ((core_if->dma_desc_enable) ||
52118 + (core_if->dma_enable
52119 + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
52120 + doepmsk.b.stsphsercvd = 1;
52121 + }
52122 + if (core_if->dma_desc_enable)
52123 + doepmsk.b.bna = 1;
52124 +/*
52125 + doepmsk.b.babble = 1;
52126 + doepmsk.b.nyet = 1;
52127 +
52128 + if (core_if->dma_enable) {
52129 + doepmsk.b.nak = 1;
52130 + }
52131 +*/
52132 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
52133 + doepmsk.d32);
52134 +
52135 + diepmsk.b.xfercompl = 1;
52136 + diepmsk.b.timeout = 1;
52137 + diepmsk.b.epdisabled = 1;
52138 + diepmsk.b.ahberr = 1;
52139 + diepmsk.b.intknepmis = 1;
52140 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
52141 + diepmsk.b.intknepmis = 0;
52142 +
52143 +/* if (core_if->dma_desc_enable) {
52144 + diepmsk.b.bna = 1;
52145 + }
52146 +*/
52147 +/*
52148 + if (core_if->dma_enable) {
52149 + diepmsk.b.nak = 1;
52150 + }
52151 +*/
52152 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
52153 + diepmsk.d32);
52154 + } else {
52155 + daintmsk.b.inep0 = 1;
52156 + daintmsk.b.outep0 = 1;
52157 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
52158 + daintmsk.d32);
52159 +
52160 + doepmsk.b.setup = 1;
52161 + doepmsk.b.xfercompl = 1;
52162 + doepmsk.b.ahberr = 1;
52163 + doepmsk.b.epdisabled = 1;
52164 +
52165 + if ((core_if->dma_desc_enable) ||
52166 + (core_if->dma_enable
52167 + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
52168 + doepmsk.b.stsphsercvd = 1;
52169 + }
52170 + if (core_if->dma_desc_enable)
52171 + doepmsk.b.bna = 1;
52172 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
52173 +
52174 + diepmsk.b.xfercompl = 1;
52175 + diepmsk.b.timeout = 1;
52176 + diepmsk.b.epdisabled = 1;
52177 + diepmsk.b.ahberr = 1;
52178 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
52179 + diepmsk.b.intknepmis = 0;
52180 +/*
52181 + if (core_if->dma_desc_enable) {
52182 + diepmsk.b.bna = 1;
52183 + }
52184 +*/
52185 +
52186 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
52187 + }
52188 +
52189 + /* Reset Device Address */
52190 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
52191 + dcfg.b.devaddr = 0;
52192 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
52193 +
52194 + /* setup EP0 to receive SETUP packets */
52195 + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
52196 + ep0_out_start(core_if, pcd);
52197 +
52198 + /* Clear interrupt */
52199 + gintsts.d32 = 0;
52200 + gintsts.b.usbreset = 1;
52201 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52202 +
52203 + return 1;
52204 +}
52205 +
52206 +/**
52207 + * Get the device speed from the device status register and convert it
52208 + * to USB speed constant.
52209 + *
52210 + * @param core_if Programming view of DWC_otg controller.
52211 + */
52212 +static int get_device_speed(dwc_otg_core_if_t * core_if)
52213 +{
52214 + dsts_data_t dsts;
52215 + int speed = 0;
52216 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
52217 +
52218 + switch (dsts.b.enumspd) {
52219 + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
52220 + speed = USB_SPEED_HIGH;
52221 + break;
52222 + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
52223 + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
52224 + speed = USB_SPEED_FULL;
52225 + break;
52226 +
52227 + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
52228 + speed = USB_SPEED_LOW;
52229 + break;
52230 + }
52231 +
52232 + return speed;
52233 +}
52234 +
52235 +/**
52236 + * Read the device status register and set the device speed in the
52237 + * data structure.
52238 + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
52239 + */
52240 +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
52241 +{
52242 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52243 + gintsts_data_t gintsts;
52244 + gusbcfg_data_t gusbcfg;
52245 + dwc_otg_core_global_regs_t *global_regs =
52246 + GET_CORE_IF(pcd)->core_global_regs;
52247 + uint8_t utmi16b, utmi8b;
52248 + int speed;
52249 + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
52250 +
52251 + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
52252 + utmi16b = 6; //vahrama old value was 6;
52253 + utmi8b = 9;
52254 + } else {
52255 + utmi16b = 4;
52256 + utmi8b = 8;
52257 + }
52258 + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
52259 + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
52260 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52261 + }
52262 +
52263 +#ifdef DEBUG_EP0
52264 + print_ep0_state(pcd);
52265 +#endif
52266 +
52267 + if (pcd->ep0state == EP0_DISCONNECT) {
52268 + pcd->ep0state = EP0_IDLE;
52269 + } else if (pcd->ep0state == EP0_STALL) {
52270 + pcd->ep0state = EP0_IDLE;
52271 + }
52272 +
52273 + pcd->ep0state = EP0_IDLE;
52274 +
52275 + ep0->stopped = 0;
52276 +
52277 + speed = get_device_speed(GET_CORE_IF(pcd));
52278 + pcd->fops->connect(pcd, speed);
52279 +
52280 + /* Set USB turnaround time based on device speed and PHY interface. */
52281 + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
52282 + if (speed == USB_SPEED_HIGH) {
52283 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52284 + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
52285 + /* ULPI interface */
52286 + gusbcfg.b.usbtrdtim = 9;
52287 + }
52288 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52289 + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
52290 + /* UTMI+ interface */
52291 + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
52292 + gusbcfg.b.usbtrdtim = utmi8b;
52293 + } else if (GET_CORE_IF(pcd)->hwcfg4.
52294 + b.utmi_phy_data_width == 1) {
52295 + gusbcfg.b.usbtrdtim = utmi16b;
52296 + } else if (GET_CORE_IF(pcd)->
52297 + core_params->phy_utmi_width == 8) {
52298 + gusbcfg.b.usbtrdtim = utmi8b;
52299 + } else {
52300 + gusbcfg.b.usbtrdtim = utmi16b;
52301 + }
52302 + }
52303 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52304 + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
52305 + /* UTMI+ OR ULPI interface */
52306 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
52307 + /* ULPI interface */
52308 + gusbcfg.b.usbtrdtim = 9;
52309 + } else {
52310 + /* UTMI+ interface */
52311 + if (GET_CORE_IF(pcd)->
52312 + core_params->phy_utmi_width == 16) {
52313 + gusbcfg.b.usbtrdtim = utmi16b;
52314 + } else {
52315 + gusbcfg.b.usbtrdtim = utmi8b;
52316 + }
52317 + }
52318 + }
52319 + } else {
52320 + /* Full or low speed */
52321 + gusbcfg.b.usbtrdtim = 9;
52322 + }
52323 + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
52324 +
52325 + /* Clear interrupt */
52326 + gintsts.d32 = 0;
52327 + gintsts.b.enumdone = 1;
52328 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52329 + gintsts.d32);
52330 + return 1;
52331 +}
52332 +
52333 +/**
52334 + * This interrupt indicates that the ISO OUT Packet was dropped due to
52335 + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
52336 + * read all the data from the Rx FIFO.
52337 + */
52338 +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
52339 +{
52340 + gintmsk_data_t intr_mask = {.d32 = 0 };
52341 + gintsts_data_t gintsts;
52342 +
52343 + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
52344 + "ISOC Out Dropped");
52345 +
52346 + intr_mask.b.isooutdrop = 1;
52347 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52348 + intr_mask.d32, 0);
52349 +
52350 + /* Clear interrupt */
52351 + gintsts.d32 = 0;
52352 + gintsts.b.isooutdrop = 1;
52353 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52354 + gintsts.d32);
52355 +
52356 + return 1;
52357 +}
52358 +
52359 +/**
52360 + * This interrupt indicates the end of the portion of the micro-frame
52361 + * for periodic transactions. If there is a periodic transaction for
52362 + * the next frame, load the packets into the EP periodic Tx FIFO.
52363 + */
52364 +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
52365 +{
52366 + gintmsk_data_t intr_mask = {.d32 = 0 };
52367 + gintsts_data_t gintsts;
52368 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
52369 +
52370 + intr_mask.b.eopframe = 1;
52371 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52372 + intr_mask.d32, 0);
52373 +
52374 + /* Clear interrupt */
52375 + gintsts.d32 = 0;
52376 + gintsts.b.eopframe = 1;
52377 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52378 + gintsts.d32);
52379 +
52380 + return 1;
52381 +}
52382 +
52383 +/**
52384 + * This interrupt indicates that EP of the packet on the top of the
52385 + * non-periodic Tx FIFO does not match EP of the IN Token received.
52386 + *
52387 + * The "Device IN Token Queue" Registers are read to determine the
52388 + * order the IN Tokens have been received. The non-periodic Tx FIFO
52389 + * is flushed, so it can be reloaded in the order seen in the IN Token
52390 + * Queue.
52391 + */
52392 +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
52393 +{
52394 + gintsts_data_t gintsts;
52395 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52396 + dctl_data_t dctl;
52397 + gintmsk_data_t intr_mask = {.d32 = 0 };
52398 +
52399 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
52400 + core_if->start_predict = 1;
52401 +
52402 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
52403 +
52404 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
52405 + if (!gintsts.b.ginnakeff) {
52406 + /* Disable EP Mismatch interrupt */
52407 + intr_mask.d32 = 0;
52408 + intr_mask.b.epmismatch = 1;
52409 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
52410 + /* Enable the Global IN NAK Effective Interrupt */
52411 + intr_mask.d32 = 0;
52412 + intr_mask.b.ginnakeff = 1;
52413 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
52414 + /* Set the global non-periodic IN NAK handshake */
52415 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
52416 + dctl.b.sgnpinnak = 1;
52417 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
52418 + } else {
52419 + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
52420 + }
52421 + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
52422 + * handler after Global IN NAK Effective interrupt will be asserted */
52423 + }
52424 + /* Clear interrupt */
52425 + gintsts.d32 = 0;
52426 + gintsts.b.epmismatch = 1;
52427 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52428 +
52429 + return 1;
52430 +}
52431 +
52432 +/**
52433 + * This interrupt is valid only in DMA mode. This interrupt indicates that the
52434 + * core has stopped fetching data for IN endpoints due to the unavailability of
52435 + * TxFIFO space or Request Queue space. This interrupt is used by the
52436 + * application for an endpoint mismatch algorithm.
52437 + *
52438 + * @param pcd The PCD
52439 + */
52440 +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
52441 +{
52442 + gintsts_data_t gintsts;
52443 + gintmsk_data_t gintmsk_data;
52444 + dctl_data_t dctl;
52445 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52446 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
52447 +
52448 + /* Clear the global non-periodic IN NAK handshake */
52449 + dctl.d32 = 0;
52450 + dctl.b.cgnpinnak = 1;
52451 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
52452 +
52453 + /* Mask GINTSTS.FETSUSP interrupt */
52454 + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
52455 + gintmsk_data.b.fetsusp = 0;
52456 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
52457 +
52458 + /* Clear interrupt */
52459 + gintsts.d32 = 0;
52460 + gintsts.b.fetsusp = 1;
52461 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52462 +
52463 + return 1;
52464 +}
52465 +/**
52466 + * This funcion stalls EP0.
52467 + */
52468 +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
52469 +{
52470 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52471 + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
52472 + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
52473 + ctrl->bmRequestType, ctrl->bRequest, err_val);
52474 +
52475 + ep0->dwc_ep.is_in = 1;
52476 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
52477 + pcd->ep0.stopped = 1;
52478 + pcd->ep0state = EP0_IDLE;
52479 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52480 +}
52481 +
52482 +/**
52483 + * This functions delegates the setup command to the gadget driver.
52484 + */
52485 +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
52486 + usb_device_request_t * ctrl)
52487 +{
52488 + int ret = 0;
52489 + DWC_SPINUNLOCK(pcd->lock);
52490 + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
52491 + DWC_SPINLOCK(pcd->lock);
52492 + if (ret < 0) {
52493 + ep0_do_stall(pcd, ret);
52494 + }
52495 +
52496 + /** @todo This is a g_file_storage gadget driver specific
52497 + * workaround: a DELAYED_STATUS result from the fsg_setup
52498 + * routine will result in the gadget queueing a EP0 IN status
52499 + * phase for a two-stage control transfer. Exactly the same as
52500 + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
52501 + * specific request. Need a generic way to know when the gadget
52502 + * driver will queue the status phase. Can we assume when we
52503 + * call the gadget driver setup() function that it will always
52504 + * queue and require the following flag? Need to look into
52505 + * this.
52506 + */
52507 +
52508 + if (ret == 256 + 999) {
52509 + pcd->request_config = 1;
52510 + }
52511 +}
52512 +
52513 +#ifdef DWC_UTE_CFI
52514 +/**
52515 + * This functions delegates the CFI setup commands to the gadget driver.
52516 + * This function will return a negative value to indicate a failure.
52517 + */
52518 +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
52519 + struct cfi_usb_ctrlrequest *ctrl_req)
52520 +{
52521 + int ret = 0;
52522 +
52523 + if (pcd->fops && pcd->fops->cfi_setup) {
52524 + DWC_SPINUNLOCK(pcd->lock);
52525 + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
52526 + DWC_SPINLOCK(pcd->lock);
52527 + if (ret < 0) {
52528 + ep0_do_stall(pcd, ret);
52529 + return ret;
52530 + }
52531 + }
52532 +
52533 + return ret;
52534 +}
52535 +#endif
52536 +
52537 +/**
52538 + * This function starts the Zero-Length Packet for the IN status phase
52539 + * of a 2 stage control transfer.
52540 + */
52541 +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
52542 +{
52543 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52544 + if (pcd->ep0state == EP0_STALL) {
52545 + return;
52546 + }
52547 +
52548 + pcd->ep0state = EP0_IN_STATUS_PHASE;
52549 +
52550 + /* Prepare for more SETUP Packets */
52551 + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
52552 + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
52553 + && (pcd->core_if->dma_desc_enable)
52554 + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
52555 + DWC_DEBUGPL(DBG_PCDV,
52556 + "Data terminated wait next packet in out_desc_addr\n");
52557 + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
52558 + pcd->data_terminated = 1;
52559 + }
52560 + ep0->dwc_ep.xfer_len = 0;
52561 + ep0->dwc_ep.xfer_count = 0;
52562 + ep0->dwc_ep.is_in = 1;
52563 + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
52564 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52565 +
52566 + /* Prepare for more SETUP Packets */
52567 + //ep0_out_start(GET_CORE_IF(pcd), pcd);
52568 +}
52569 +
52570 +/**
52571 + * This function starts the Zero-Length Packet for the OUT status phase
52572 + * of a 2 stage control transfer.
52573 + */
52574 +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
52575 +{
52576 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52577 + if (pcd->ep0state == EP0_STALL) {
52578 + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
52579 + return;
52580 + }
52581 + pcd->ep0state = EP0_OUT_STATUS_PHASE;
52582 +
52583 + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
52584 + ep0->dwc_ep.xfer_len = 0;
52585 + ep0->dwc_ep.xfer_count = 0;
52586 + ep0->dwc_ep.is_in = 0;
52587 + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
52588 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52589 +
52590 + /* Prepare for more SETUP Packets */
52591 + if (GET_CORE_IF(pcd)->dma_enable == 0) {
52592 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52593 + }
52594 +}
52595 +
52596 +/**
52597 + * Clear the EP halt (STALL) and if pending requests start the
52598 + * transfer.
52599 + */
52600 +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
52601 +{
52602 + if (ep->dwc_ep.stall_clear_flag == 0)
52603 + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
52604 +
52605 + /* Reactive the EP */
52606 + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
52607 + if (ep->stopped) {
52608 + ep->stopped = 0;
52609 + /* If there is a request in the EP queue start it */
52610 +
52611 + /** @todo FIXME: this causes an EP mismatch in DMA mode.
52612 + * epmismatch not yet implemented. */
52613 +
52614 + /*
52615 + * Above fixme is solved by implmenting a tasklet to call the
52616 + * start_next_request(), outside of interrupt context at some
52617 + * time after the current time, after a clear-halt setup packet.
52618 + * Still need to implement ep mismatch in the future if a gadget
52619 + * ever uses more than one endpoint at once
52620 + */
52621 + ep->queue_sof = 1;
52622 + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
52623 + }
52624 + /* Start Control Status Phase */
52625 + do_setup_in_status_phase(pcd);
52626 +}
52627 +
52628 +/**
52629 + * This function is called when the SET_FEATURE TEST_MODE Setup packet
52630 + * is sent from the host. The Device Control register is written with
52631 + * the Test Mode bits set to the specified Test Mode. This is done as
52632 + * a tasklet so that the "Status" phase of the control transfer
52633 + * completes before transmitting the TEST packets.
52634 + *
52635 + * @todo This has not been tested since the tasklet struct was put
52636 + * into the PCD struct!
52637 + *
52638 + */
52639 +void do_test_mode(void *data)
52640 +{
52641 + dctl_data_t dctl;
52642 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
52643 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52644 + int test_mode = pcd->test_mode;
52645 +
52646 +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
52647 +
52648 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
52649 + switch (test_mode) {
52650 + case 1: // TEST_J
52651 + dctl.b.tstctl = 1;
52652 + break;
52653 +
52654 + case 2: // TEST_K
52655 + dctl.b.tstctl = 2;
52656 + break;
52657 +
52658 + case 3: // TEST_SE0_NAK
52659 + dctl.b.tstctl = 3;
52660 + break;
52661 +
52662 + case 4: // TEST_PACKET
52663 + dctl.b.tstctl = 4;
52664 + break;
52665 +
52666 + case 5: // TEST_FORCE_ENABLE
52667 + dctl.b.tstctl = 5;
52668 + break;
52669 + }
52670 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
52671 +}
52672 +
52673 +/**
52674 + * This function process the GET_STATUS Setup Commands.
52675 + */
52676 +static inline void do_get_status(dwc_otg_pcd_t * pcd)
52677 +{
52678 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52679 + dwc_otg_pcd_ep_t *ep;
52680 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52681 + uint16_t *status = pcd->status_buf;
52682 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52683 +
52684 +#ifdef DEBUG_EP0
52685 + DWC_DEBUGPL(DBG_PCD,
52686 + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
52687 + ctrl.bmRequestType, ctrl.bRequest,
52688 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
52689 + UGETW(ctrl.wLength));
52690 +#endif
52691 +
52692 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
52693 + case UT_DEVICE:
52694 + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
52695 + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
52696 + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
52697 + DWC_PRINTF("OTG CAP - %d, %d\n",
52698 + core_if->core_params->otg_cap,
52699 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
52700 + if (core_if->otg_ver == 1
52701 + && core_if->core_params->otg_cap ==
52702 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
52703 + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
52704 + *otgsts = (core_if->otg_sts & 0x1);
52705 + pcd->ep0_pending = 1;
52706 + ep0->dwc_ep.start_xfer_buff =
52707 + (uint8_t *) otgsts;
52708 + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
52709 + ep0->dwc_ep.dma_addr =
52710 + pcd->status_buf_dma_handle;
52711 + ep0->dwc_ep.xfer_len = 1;
52712 + ep0->dwc_ep.xfer_count = 0;
52713 + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
52714 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
52715 + &ep0->dwc_ep);
52716 + return;
52717 + } else {
52718 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52719 + return;
52720 + }
52721 + break;
52722 + } else {
52723 + *status = 0x1; /* Self powered */
52724 + *status |= pcd->remote_wakeup_enable << 1;
52725 + break;
52726 + }
52727 + case UT_INTERFACE:
52728 + *status = 0;
52729 + break;
52730 +
52731 + case UT_ENDPOINT:
52732 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
52733 + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
52734 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52735 + return;
52736 + }
52737 + /** @todo check for EP stall */
52738 + *status = ep->stopped;
52739 + break;
52740 + }
52741 + pcd->ep0_pending = 1;
52742 + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
52743 + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
52744 + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
52745 + ep0->dwc_ep.xfer_len = 2;
52746 + ep0->dwc_ep.xfer_count = 0;
52747 + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
52748 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52749 +}
52750 +
52751 +/**
52752 + * This function process the SET_FEATURE Setup Commands.
52753 + */
52754 +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
52755 +{
52756 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52757 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
52758 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52759 + dwc_otg_pcd_ep_t *ep = 0;
52760 + int32_t otg_cap_param = core_if->core_params->otg_cap;
52761 + gotgctl_data_t gotgctl = {.d32 = 0 };
52762 +
52763 + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
52764 + ctrl.bmRequestType, ctrl.bRequest,
52765 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
52766 + UGETW(ctrl.wLength));
52767 + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
52768 +
52769 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
52770 + case UT_DEVICE:
52771 + switch (UGETW(ctrl.wValue)) {
52772 + case UF_DEVICE_REMOTE_WAKEUP:
52773 + pcd->remote_wakeup_enable = 1;
52774 + break;
52775 +
52776 + case UF_TEST_MODE:
52777 + /* Setup the Test Mode tasklet to do the Test
52778 + * Packet generation after the SETUP Status
52779 + * phase has completed. */
52780 +
52781 + /** @todo This has not been tested since the
52782 + * tasklet struct was put into the PCD
52783 + * struct! */
52784 + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
52785 + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
52786 + break;
52787 +
52788 + case UF_DEVICE_B_HNP_ENABLE:
52789 + DWC_DEBUGPL(DBG_PCDV,
52790 + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
52791 +
52792 + /* dev may initiate HNP */
52793 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
52794 + pcd->b_hnp_enable = 1;
52795 + dwc_otg_pcd_update_otg(pcd, 0);
52796 + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
52797 + /**@todo Is the gotgctl.devhnpen cleared
52798 + * by a USB Reset? */
52799 + gotgctl.b.devhnpen = 1;
52800 + gotgctl.b.hnpreq = 1;
52801 + DWC_WRITE_REG32(&global_regs->gotgctl,
52802 + gotgctl.d32);
52803 + } else {
52804 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52805 + return;
52806 + }
52807 + break;
52808 +
52809 + case UF_DEVICE_A_HNP_SUPPORT:
52810 + /* RH port supports HNP */
52811 + DWC_DEBUGPL(DBG_PCDV,
52812 + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
52813 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
52814 + pcd->a_hnp_support = 1;
52815 + dwc_otg_pcd_update_otg(pcd, 0);
52816 + } else {
52817 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52818 + return;
52819 + }
52820 + break;
52821 +
52822 + case UF_DEVICE_A_ALT_HNP_SUPPORT:
52823 + /* other RH port does */
52824 + DWC_DEBUGPL(DBG_PCDV,
52825 + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
52826 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
52827 + pcd->a_alt_hnp_support = 1;
52828 + dwc_otg_pcd_update_otg(pcd, 0);
52829 + } else {
52830 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52831 + return;
52832 + }
52833 + break;
52834 +
52835 + default:
52836 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52837 + return;
52838 +
52839 + }
52840 + do_setup_in_status_phase(pcd);
52841 + break;
52842 +
52843 + case UT_INTERFACE:
52844 + do_gadget_setup(pcd, &ctrl);
52845 + break;
52846 +
52847 + case UT_ENDPOINT:
52848 + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
52849 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
52850 + if (ep == 0) {
52851 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52852 + return;
52853 + }
52854 + ep->stopped = 1;
52855 + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
52856 + }
52857 + do_setup_in_status_phase(pcd);
52858 + break;
52859 + }
52860 +}
52861 +
52862 +/**
52863 + * This function process the CLEAR_FEATURE Setup Commands.
52864 + */
52865 +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
52866 +{
52867 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52868 + dwc_otg_pcd_ep_t *ep = 0;
52869 +
52870 + DWC_DEBUGPL(DBG_PCD,
52871 + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
52872 + ctrl.bmRequestType, ctrl.bRequest,
52873 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
52874 + UGETW(ctrl.wLength));
52875 +
52876 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
52877 + case UT_DEVICE:
52878 + switch (UGETW(ctrl.wValue)) {
52879 + case UF_DEVICE_REMOTE_WAKEUP:
52880 + pcd->remote_wakeup_enable = 0;
52881 + break;
52882 +
52883 + case UF_TEST_MODE:
52884 + /** @todo Add CLEAR_FEATURE for TEST modes. */
52885 + break;
52886 +
52887 + default:
52888 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52889 + return;
52890 + }
52891 + do_setup_in_status_phase(pcd);
52892 + break;
52893 +
52894 + case UT_ENDPOINT:
52895 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
52896 + if (ep == 0) {
52897 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52898 + return;
52899 + }
52900 +
52901 + pcd_clear_halt(pcd, ep);
52902 +
52903 + break;
52904 + }
52905 +}
52906 +
52907 +/**
52908 + * This function process the SET_ADDRESS Setup Commands.
52909 + */
52910 +static inline void do_set_address(dwc_otg_pcd_t * pcd)
52911 +{
52912 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
52913 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52914 +
52915 + if (ctrl.bmRequestType == UT_DEVICE) {
52916 + dcfg_data_t dcfg = {.d32 = 0 };
52917 +
52918 +#ifdef DEBUG_EP0
52919 +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
52920 +#endif
52921 + dcfg.b.devaddr = UGETW(ctrl.wValue);
52922 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
52923 + do_setup_in_status_phase(pcd);
52924 + }
52925 +}
52926 +
52927 +/**
52928 + * This function processes SETUP commands. In Linux, the USB Command
52929 + * processing is done in two places - the first being the PCD and the
52930 + * second in the Gadget Driver (for example, the File-Backed Storage
52931 + * Gadget Driver).
52932 + *
52933 + * <table>
52934 + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
52935 + *
52936 + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
52937 + * defined in chapter 9 of the USB 2.0 Specification chapter 9
52938 + * </td></tr>
52939 + *
52940 + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
52941 + * requests are the ENDPOINT_HALT feature is procesed, all others the
52942 + * interface requests are ignored.</td></tr>
52943 + *
52944 + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
52945 + * requests are processed by the PCD. Interface requests are passed
52946 + * to the Gadget Driver.</td></tr>
52947 + *
52948 + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
52949 + * with device address received </td></tr>
52950 + *
52951 + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
52952 + * requested descriptor</td></tr>
52953 + *
52954 + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
52955 + * not implemented by any of the existing Gadget Drivers.</td></tr>
52956 + *
52957 + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
52958 + * all EPs and enable EPs for new configuration.</td></tr>
52959 + *
52960 + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
52961 + * the current configuration</td></tr>
52962 + *
52963 + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
52964 + * EPs and enable EPs for new configuration.</td></tr>
52965 + *
52966 + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
52967 + * current interface.</td></tr>
52968 + *
52969 + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
52970 + * message.</td></tr>
52971 + * </table>
52972 + *
52973 + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
52974 + * processed by pcd_setup. Calling the Function Driver's setup function from
52975 + * pcd_setup processes the gadget SETUP commands.
52976 + */
52977 +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
52978 +{
52979 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52980 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52981 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52982 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52983 +
52984 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
52985 +
52986 +#ifdef DWC_UTE_CFI
52987 + int retval = 0;
52988 + struct cfi_usb_ctrlrequest cfi_req;
52989 +#endif
52990 +
52991 + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
52992 +
52993 + /** In BDMA more then 1 setup packet is not supported till 3.00a */
52994 + if (core_if->dma_enable && core_if->dma_desc_enable == 0
52995 + && (doeptsize0.b.supcnt < 2)
52996 + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
52997 + DWC_ERROR
52998 + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
52999 + }
53000 + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
53001 + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
53002 + ctrl =
53003 + (pcd->setup_pkt +
53004 + (3 - doeptsize0.b.supcnt - 1 +
53005 + ep0->dwc_ep.stp_rollover))->req;
53006 + }
53007 +#ifdef DEBUG_EP0
53008 + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
53009 + ctrl.bmRequestType, ctrl.bRequest,
53010 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53011 + UGETW(ctrl.wLength));
53012 +#endif
53013 +
53014 + /* Clean up the request queue */
53015 + dwc_otg_request_nuke(ep0);
53016 + ep0->stopped = 0;
53017 +
53018 + if (ctrl.bmRequestType & UE_DIR_IN) {
53019 + ep0->dwc_ep.is_in = 1;
53020 + pcd->ep0state = EP0_IN_DATA_PHASE;
53021 + } else {
53022 + ep0->dwc_ep.is_in = 0;
53023 + pcd->ep0state = EP0_OUT_DATA_PHASE;
53024 + }
53025 +
53026 + if (UGETW(ctrl.wLength) == 0) {
53027 + ep0->dwc_ep.is_in = 1;
53028 + pcd->ep0state = EP0_IN_STATUS_PHASE;
53029 + }
53030 +
53031 + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
53032 +
53033 +#ifdef DWC_UTE_CFI
53034 + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
53035 +
53036 + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
53037 + ctrl.bRequestType, ctrl.bRequest);
53038 + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
53039 + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
53040 + retval = cfi_setup(pcd, &cfi_req);
53041 + if (retval < 0) {
53042 + ep0_do_stall(pcd, retval);
53043 + pcd->ep0_pending = 0;
53044 + return;
53045 + }
53046 +
53047 + /* if need gadget setup then call it and check the retval */
53048 + if (pcd->cfi->need_gadget_att) {
53049 + retval =
53050 + cfi_gadget_setup(pcd,
53051 + &pcd->
53052 + cfi->ctrl_req);
53053 + if (retval < 0) {
53054 + pcd->ep0_pending = 0;
53055 + return;
53056 + }
53057 + }
53058 +
53059 + if (pcd->cfi->need_status_in_complete) {
53060 + do_setup_in_status_phase(pcd);
53061 + }
53062 + return;
53063 + }
53064 + }
53065 +#endif
53066 +
53067 + /* handle non-standard (class/vendor) requests in the gadget driver */
53068 + do_gadget_setup(pcd, &ctrl);
53069 + return;
53070 + }
53071 +
53072 + /** @todo NGS: Handle bad setup packet? */
53073 +
53074 +///////////////////////////////////////////
53075 +//// --- Standard Request handling --- ////
53076 +
53077 + switch (ctrl.bRequest) {
53078 + case UR_GET_STATUS:
53079 + do_get_status(pcd);
53080 + break;
53081 +
53082 + case UR_CLEAR_FEATURE:
53083 + do_clear_feature(pcd);
53084 + break;
53085 +
53086 + case UR_SET_FEATURE:
53087 + do_set_feature(pcd);
53088 + break;
53089 +
53090 + case UR_SET_ADDRESS:
53091 + do_set_address(pcd);
53092 + break;
53093 +
53094 + case UR_SET_INTERFACE:
53095 + case UR_SET_CONFIG:
53096 +// _pcd->request_config = 1; /* Configuration changed */
53097 + do_gadget_setup(pcd, &ctrl);
53098 + break;
53099 +
53100 + case UR_SYNCH_FRAME:
53101 + do_gadget_setup(pcd, &ctrl);
53102 + break;
53103 +
53104 + default:
53105 + /* Call the Gadget Driver's setup functions */
53106 + do_gadget_setup(pcd, &ctrl);
53107 + break;
53108 + }
53109 +}
53110 +
53111 +/**
53112 + * This function completes the ep0 control transfer.
53113 + */
53114 +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
53115 +{
53116 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
53117 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53118 + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
53119 + dev_if->in_ep_regs[ep->dwc_ep.num];
53120 +#ifdef DEBUG_EP0
53121 + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
53122 + dev_if->out_ep_regs[ep->dwc_ep.num];
53123 +#endif
53124 + deptsiz0_data_t deptsiz;
53125 + dev_dma_desc_sts_t desc_sts;
53126 + dwc_otg_pcd_request_t *req;
53127 + int is_last = 0;
53128 + dwc_otg_pcd_t *pcd = ep->pcd;
53129 +
53130 +#ifdef DWC_UTE_CFI
53131 + struct cfi_usb_ctrlrequest *ctrlreq;
53132 + int retval = -DWC_E_NOT_SUPPORTED;
53133 +#endif
53134 +
53135 + desc_sts.b.bytes = 0;
53136 +
53137 + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53138 + if (ep->dwc_ep.is_in) {
53139 +#ifdef DEBUG_EP0
53140 + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
53141 +#endif
53142 + do_setup_out_status_phase(pcd);
53143 + } else {
53144 +#ifdef DEBUG_EP0
53145 + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
53146 +#endif
53147 +
53148 +#ifdef DWC_UTE_CFI
53149 + ctrlreq = &pcd->cfi->ctrl_req;
53150 +
53151 + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
53152 + if (ctrlreq->bRequest > 0xB0
53153 + && ctrlreq->bRequest < 0xBF) {
53154 +
53155 + /* Return if the PCD failed to handle the request */
53156 + if ((retval =
53157 + pcd->cfi->ops.
53158 + ctrl_write_complete(pcd->cfi,
53159 + pcd)) < 0) {
53160 + CFI_INFO
53161 + ("ERROR setting a new value in the PCD(%d)\n",
53162 + retval);
53163 + ep0_do_stall(pcd, retval);
53164 + pcd->ep0_pending = 0;
53165 + return 0;
53166 + }
53167 +
53168 + /* If the gadget needs to be notified on the request */
53169 + if (pcd->cfi->need_gadget_att == 1) {
53170 + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
53171 + retval =
53172 + cfi_gadget_setup(pcd,
53173 + &pcd->cfi->
53174 + ctrl_req);
53175 +
53176 + /* Return from the function if the gadget failed to process
53177 + * the request properly - this should never happen !!!
53178 + */
53179 + if (retval < 0) {
53180 + CFI_INFO
53181 + ("ERROR setting a new value in the gadget(%d)\n",
53182 + retval);
53183 + pcd->ep0_pending = 0;
53184 + return 0;
53185 + }
53186 + }
53187 +
53188 + CFI_INFO("%s: RETVAL=%d\n", __func__,
53189 + retval);
53190 + /* If we hit here then the PCD and the gadget has properly
53191 + * handled the request - so send the ZLP IN to the host.
53192 + */
53193 + /* @todo: MAS - decide whether we need to start the setup
53194 + * stage based on the need_setup value of the cfi object
53195 + */
53196 + do_setup_in_status_phase(pcd);
53197 + pcd->ep0_pending = 0;
53198 + return 1;
53199 + }
53200 + }
53201 +#endif
53202 +
53203 + do_setup_in_status_phase(pcd);
53204 + }
53205 + pcd->ep0_pending = 0;
53206 + return 1;
53207 + }
53208 +
53209 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53210 + return 0;
53211 + }
53212 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
53213 +
53214 + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
53215 + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
53216 + is_last = 1;
53217 + } else if (ep->dwc_ep.is_in) {
53218 + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
53219 + if (core_if->dma_desc_enable != 0)
53220 + desc_sts = dev_if->in_desc_addr->status;
53221 +#ifdef DEBUG_EP0
53222 + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
53223 + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
53224 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53225 +#endif
53226 +
53227 + if (((core_if->dma_desc_enable == 0)
53228 + && (deptsiz.b.xfersize == 0))
53229 + || ((core_if->dma_desc_enable != 0)
53230 + && (desc_sts.b.bytes == 0))) {
53231 + req->actual = ep->dwc_ep.xfer_count;
53232 + /* Is a Zero Len Packet needed? */
53233 + if (req->sent_zlp) {
53234 +#ifdef DEBUG_EP0
53235 + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
53236 +#endif
53237 + req->sent_zlp = 0;
53238 + }
53239 + do_setup_out_status_phase(pcd);
53240 + }
53241 + } else {
53242 + /* ep0-OUT */
53243 +#ifdef DEBUG_EP0
53244 + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
53245 + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
53246 + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
53247 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53248 +#endif
53249 + req->actual = ep->dwc_ep.xfer_count;
53250 +
53251 + /* Is a Zero Len Packet needed? */
53252 + if (req->sent_zlp) {
53253 +#ifdef DEBUG_EP0
53254 + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
53255 +#endif
53256 + req->sent_zlp = 0;
53257 + }
53258 + /* For older cores do setup in status phase in Slave/BDMA modes,
53259 + * starting from 3.00 do that only in slave, and for DMA modes
53260 + * just re-enable ep 0 OUT here*/
53261 + if (core_if->dma_enable == 0
53262 + || (core_if->dma_desc_enable == 0
53263 + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
53264 + do_setup_in_status_phase(pcd);
53265 + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
53266 + DWC_DEBUGPL(DBG_PCDV,
53267 + "Enable out ep before in status phase\n");
53268 + ep0_out_start(core_if, pcd);
53269 + }
53270 + }
53271 +
53272 + /* Complete the request */
53273 + if (is_last) {
53274 + dwc_otg_request_done(ep, req, 0);
53275 + ep->dwc_ep.start_xfer_buff = 0;
53276 + ep->dwc_ep.xfer_buff = 0;
53277 + ep->dwc_ep.xfer_len = 0;
53278 + return 1;
53279 + }
53280 + return 0;
53281 +}
53282 +
53283 +#ifdef DWC_UTE_CFI
53284 +/**
53285 + * This function calculates traverses all the CFI DMA descriptors and
53286 + * and accumulates the bytes that are left to be transfered.
53287 + *
53288 + * @return The total bytes left to transfered, or a negative value as failure
53289 + */
53290 +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
53291 +{
53292 + int32_t ret = 0;
53293 + int i;
53294 + struct dwc_otg_dma_desc *ddesc = NULL;
53295 + struct cfi_ep *cfiep;
53296 +
53297 + /* See if the pcd_ep has its respective cfi_ep mapped */
53298 + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
53299 + if (!cfiep) {
53300 + CFI_INFO("%s: Failed to find ep\n", __func__);
53301 + return -1;
53302 + }
53303 +
53304 + ddesc = ep->dwc_ep.descs;
53305 +
53306 + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
53307 +
53308 +#if defined(PRINT_CFI_DMA_DESCS)
53309 + print_desc(ddesc, ep->ep.name, i);
53310 +#endif
53311 + ret += ddesc->status.b.bytes;
53312 + ddesc++;
53313 + }
53314 +
53315 + if (ret)
53316 + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
53317 + ret);
53318 +
53319 + return ret;
53320 +}
53321 +#endif
53322 +
53323 +/**
53324 + * This function completes the request for the EP. If there are
53325 + * additional requests for the EP in the queue they will be started.
53326 + */
53327 +static void complete_ep(dwc_otg_pcd_ep_t * ep)
53328 +{
53329 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
53330 + struct device *dev = dwc_otg_pcd_to_dev(ep->pcd);
53331 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53332 + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
53333 + dev_if->in_ep_regs[ep->dwc_ep.num];
53334 + deptsiz_data_t deptsiz;
53335 + dev_dma_desc_sts_t desc_sts;
53336 + dwc_otg_pcd_request_t *req = 0;
53337 + dwc_otg_dev_dma_desc_t *dma_desc;
53338 + uint32_t byte_count = 0;
53339 + int is_last = 0;
53340 + int i;
53341 +
53342 + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
53343 + (ep->dwc_ep.is_in ? "IN" : "OUT"));
53344 +
53345 + /* Get any pending requests */
53346 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53347 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
53348 + if (!req) {
53349 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
53350 + return;
53351 + }
53352 + } else {
53353 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
53354 + return;
53355 + }
53356 +
53357 + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
53358 +
53359 + if (ep->dwc_ep.is_in) {
53360 + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
53361 +
53362 + if (core_if->dma_enable) {
53363 + if (core_if->dma_desc_enable == 0) {
53364 + if (deptsiz.b.xfersize == 0
53365 + && deptsiz.b.pktcnt == 0) {
53366 + byte_count =
53367 + ep->dwc_ep.xfer_len -
53368 + ep->dwc_ep.xfer_count;
53369 +
53370 + ep->dwc_ep.xfer_buff += byte_count;
53371 + ep->dwc_ep.dma_addr += byte_count;
53372 + ep->dwc_ep.xfer_count += byte_count;
53373 +
53374 + DWC_DEBUGPL(DBG_PCDV,
53375 + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
53376 + ep->dwc_ep.num,
53377 + (ep->dwc_ep.
53378 + is_in ? "IN" : "OUT"),
53379 + ep->dwc_ep.xfer_len,
53380 + deptsiz.b.xfersize,
53381 + deptsiz.b.pktcnt);
53382 +
53383 + if (ep->dwc_ep.xfer_len <
53384 + ep->dwc_ep.total_len) {
53385 + dwc_otg_ep_start_transfer
53386 + (core_if, &ep->dwc_ep);
53387 + } else if (ep->dwc_ep.sent_zlp) {
53388 + /*
53389 + * This fragment of code should initiate 0
53390 + * length transfer in case if it is queued
53391 + * a transfer with size divisible to EPs max
53392 + * packet size and with usb_request zero field
53393 + * is set, which means that after data is transfered,
53394 + * it is also should be transfered
53395 + * a 0 length packet at the end. For Slave and
53396 + * Buffer DMA modes in this case SW has
53397 + * to initiate 2 transfers one with transfer size,
53398 + * and the second with 0 size. For Descriptor
53399 + * DMA mode SW is able to initiate a transfer,
53400 + * which will handle all the packets including
53401 + * the last 0 length.
53402 + */
53403 + ep->dwc_ep.sent_zlp = 0;
53404 + dwc_otg_ep_start_zl_transfer
53405 + (core_if, &ep->dwc_ep);
53406 + } else {
53407 + is_last = 1;
53408 + }
53409 + } else {
53410 + if (ep->dwc_ep.type ==
53411 + DWC_OTG_EP_TYPE_ISOC) {
53412 + req->actual = 0;
53413 + dwc_otg_request_done(ep, req, 0);
53414 +
53415 + ep->dwc_ep.start_xfer_buff = 0;
53416 + ep->dwc_ep.xfer_buff = 0;
53417 + ep->dwc_ep.xfer_len = 0;
53418 +
53419 + /* If there is a request in the queue start it. */
53420 + start_next_request(ep);
53421 + } else
53422 + DWC_WARN
53423 + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
53424 + ep->dwc_ep.num,
53425 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
53426 + deptsiz.b.xfersize,
53427 + deptsiz.b.pktcnt);
53428 + }
53429 + } else {
53430 + dma_desc = ep->dwc_ep.desc_addr;
53431 + byte_count = 0;
53432 + ep->dwc_ep.sent_zlp = 0;
53433 +
53434 +#ifdef DWC_UTE_CFI
53435 + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
53436 + ep->dwc_ep.buff_mode);
53437 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53438 + int residue;
53439 +
53440 + residue = cfi_calc_desc_residue(ep);
53441 + if (residue < 0)
53442 + return;
53443 +
53444 + byte_count = residue;
53445 + } else {
53446 +#endif
53447 + for (i = 0; i < ep->dwc_ep.desc_cnt;
53448 + ++i) {
53449 + desc_sts = dma_desc->status;
53450 + byte_count += desc_sts.b.bytes;
53451 + dma_desc++;
53452 + }
53453 +#ifdef DWC_UTE_CFI
53454 + }
53455 +#endif
53456 + if (byte_count == 0) {
53457 + ep->dwc_ep.xfer_count =
53458 + ep->dwc_ep.total_len;
53459 + is_last = 1;
53460 + } else {
53461 + DWC_WARN("Incomplete transfer\n");
53462 + }
53463 + }
53464 + } else {
53465 + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
53466 + DWC_DEBUGPL(DBG_PCDV,
53467 + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
53468 + ep->dwc_ep.num,
53469 + ep->dwc_ep.is_in ? "IN" : "OUT",
53470 + ep->dwc_ep.xfer_len,
53471 + deptsiz.b.xfersize,
53472 + deptsiz.b.pktcnt);
53473 +
53474 + /* Check if the whole transfer was completed,
53475 + * if no, setup transfer for next portion of data
53476 + */
53477 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53478 + dwc_otg_ep_start_transfer(core_if,
53479 + &ep->dwc_ep);
53480 + } else if (ep->dwc_ep.sent_zlp) {
53481 + /*
53482 + * This fragment of code should initiate 0
53483 + * length trasfer in case if it is queued
53484 + * a trasfer with size divisible to EPs max
53485 + * packet size and with usb_request zero field
53486 + * is set, which means that after data is transfered,
53487 + * it is also should be transfered
53488 + * a 0 length packet at the end. For Slave and
53489 + * Buffer DMA modes in this case SW has
53490 + * to initiate 2 transfers one with transfer size,
53491 + * and the second with 0 size. For Desriptor
53492 + * DMA mode SW is able to initiate a transfer,
53493 + * which will handle all the packets including
53494 + * the last 0 legth.
53495 + */
53496 + ep->dwc_ep.sent_zlp = 0;
53497 + dwc_otg_ep_start_zl_transfer(core_if,
53498 + &ep->dwc_ep);
53499 + } else {
53500 + is_last = 1;
53501 + }
53502 + } else {
53503 + DWC_WARN
53504 + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
53505 + ep->dwc_ep.num,
53506 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
53507 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53508 + }
53509 + }
53510 + } else {
53511 + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
53512 + dev_if->out_ep_regs[ep->dwc_ep.num];
53513 + desc_sts.d32 = 0;
53514 + if (core_if->dma_enable) {
53515 + if (core_if->dma_desc_enable) {
53516 + dma_desc = ep->dwc_ep.desc_addr;
53517 + byte_count = 0;
53518 + ep->dwc_ep.sent_zlp = 0;
53519 +
53520 +#ifdef DWC_UTE_CFI
53521 + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
53522 + ep->dwc_ep.buff_mode);
53523 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53524 + int residue;
53525 + residue = cfi_calc_desc_residue(ep);
53526 + if (residue < 0)
53527 + return;
53528 + byte_count = residue;
53529 + } else {
53530 +#endif
53531 +
53532 + for (i = 0; i < ep->dwc_ep.desc_cnt;
53533 + ++i) {
53534 + desc_sts = dma_desc->status;
53535 + byte_count += desc_sts.b.bytes;
53536 + dma_desc++;
53537 + }
53538 +
53539 +#ifdef DWC_UTE_CFI
53540 + }
53541 +#endif
53542 + /* Checking for interrupt Out transfers with not
53543 + * dword aligned mps sizes
53544 + */
53545 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
53546 + (ep->dwc_ep.maxpacket%4)) {
53547 + ep->dwc_ep.xfer_count =
53548 + ep->dwc_ep.total_len - byte_count;
53549 + if ((ep->dwc_ep.xfer_len %
53550 + ep->dwc_ep.maxpacket)
53551 + && (ep->dwc_ep.xfer_len /
53552 + ep->dwc_ep.maxpacket <
53553 + MAX_DMA_DESC_CNT))
53554 + ep->dwc_ep.xfer_len -=
53555 + (ep->dwc_ep.desc_cnt -
53556 + 1) * ep->dwc_ep.maxpacket +
53557 + ep->dwc_ep.xfer_len %
53558 + ep->dwc_ep.maxpacket;
53559 + else
53560 + ep->dwc_ep.xfer_len -=
53561 + ep->dwc_ep.desc_cnt *
53562 + ep->dwc_ep.maxpacket;
53563 + if (ep->dwc_ep.xfer_len > 0) {
53564 + dwc_otg_ep_start_transfer
53565 + (core_if, &ep->dwc_ep);
53566 + } else {
53567 + is_last = 1;
53568 + }
53569 + } else {
53570 + ep->dwc_ep.xfer_count =
53571 + ep->dwc_ep.total_len - byte_count +
53572 + ((4 -
53573 + (ep->dwc_ep.
53574 + total_len & 0x3)) & 0x3);
53575 + is_last = 1;
53576 + }
53577 + } else {
53578 + deptsiz.d32 = 0;
53579 + deptsiz.d32 =
53580 + DWC_READ_REG32(&out_ep_regs->doeptsiz);
53581 +
53582 + byte_count = (ep->dwc_ep.xfer_len -
53583 + ep->dwc_ep.xfer_count -
53584 + deptsiz.b.xfersize);
53585 + ep->dwc_ep.xfer_buff += byte_count;
53586 + ep->dwc_ep.dma_addr += byte_count;
53587 + ep->dwc_ep.xfer_count += byte_count;
53588 +
53589 + /* Check if the whole transfer was completed,
53590 + * if no, setup transfer for next portion of data
53591 + */
53592 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53593 + dwc_otg_ep_start_transfer(core_if,
53594 + &ep->dwc_ep);
53595 + } else if (ep->dwc_ep.sent_zlp) {
53596 + /*
53597 + * This fragment of code should initiate 0
53598 + * length trasfer in case if it is queued
53599 + * a trasfer with size divisible to EPs max
53600 + * packet size and with usb_request zero field
53601 + * is set, which means that after data is transfered,
53602 + * it is also should be transfered
53603 + * a 0 length packet at the end. For Slave and
53604 + * Buffer DMA modes in this case SW has
53605 + * to initiate 2 transfers one with transfer size,
53606 + * and the second with 0 size. For Desriptor
53607 + * DMA mode SW is able to initiate a transfer,
53608 + * which will handle all the packets including
53609 + * the last 0 legth.
53610 + */
53611 + ep->dwc_ep.sent_zlp = 0;
53612 + dwc_otg_ep_start_zl_transfer(core_if,
53613 + &ep->dwc_ep);
53614 + } else {
53615 + is_last = 1;
53616 + }
53617 + }
53618 + } else {
53619 + /* Check if the whole transfer was completed,
53620 + * if no, setup transfer for next portion of data
53621 + */
53622 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53623 + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
53624 + } else if (ep->dwc_ep.sent_zlp) {
53625 + /*
53626 + * This fragment of code should initiate 0
53627 + * length transfer in case if it is queued
53628 + * a transfer with size divisible to EPs max
53629 + * packet size and with usb_request zero field
53630 + * is set, which means that after data is transfered,
53631 + * it is also should be transfered
53632 + * a 0 length packet at the end. For Slave and
53633 + * Buffer DMA modes in this case SW has
53634 + * to initiate 2 transfers one with transfer size,
53635 + * and the second with 0 size. For Descriptor
53636 + * DMA mode SW is able to initiate a transfer,
53637 + * which will handle all the packets including
53638 + * the last 0 length.
53639 + */
53640 + ep->dwc_ep.sent_zlp = 0;
53641 + dwc_otg_ep_start_zl_transfer(core_if,
53642 + &ep->dwc_ep);
53643 + } else {
53644 + is_last = 1;
53645 + }
53646 + }
53647 +
53648 + DWC_DEBUGPL(DBG_PCDV,
53649 + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
53650 + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
53651 + ep->dwc_ep.is_in ? "IN" : "OUT",
53652 + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
53653 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53654 + }
53655 +
53656 + /* Complete the request */
53657 + if (is_last) {
53658 +#ifdef DWC_UTE_CFI
53659 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53660 + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
53661 + } else {
53662 +#endif
53663 + req->actual = ep->dwc_ep.xfer_count;
53664 +#ifdef DWC_UTE_CFI
53665 + }
53666 +#endif
53667 + if (req->dw_align_buf) {
53668 + if (!ep->dwc_ep.is_in) {
53669 + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
53670 + }
53671 + DWC_DMA_FREE(dev, req->length, req->dw_align_buf,
53672 + req->dw_align_buf_dma);
53673 + }
53674 +
53675 + dwc_otg_request_done(ep, req, 0);
53676 +
53677 + ep->dwc_ep.start_xfer_buff = 0;
53678 + ep->dwc_ep.xfer_buff = 0;
53679 + ep->dwc_ep.xfer_len = 0;
53680 +
53681 + /* If there is a request in the queue start it. */
53682 + start_next_request(ep);
53683 + }
53684 +}
53685 +
53686 +#ifdef DWC_EN_ISOC
53687 +
53688 +/**
53689 + * This function BNA interrupt for Isochronous EPs
53690 + *
53691 + */
53692 +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
53693 +{
53694 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
53695 + volatile uint32_t *addr;
53696 + depctl_data_t depctl = {.d32 = 0 };
53697 + dwc_otg_pcd_t *pcd = ep->pcd;
53698 + dwc_otg_dev_dma_desc_t *dma_desc;
53699 + int i;
53700 +
53701 + dma_desc =
53702 + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
53703 +
53704 + if (dwc_ep->is_in) {
53705 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53706 + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
53707 + sts.d32 = dma_desc->status.d32;
53708 + sts.b_iso_in.bs = BS_HOST_READY;
53709 + dma_desc->status.d32 = sts.d32;
53710 + }
53711 + } else {
53712 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53713 + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
53714 + sts.d32 = dma_desc->status.d32;
53715 + sts.b_iso_out.bs = BS_HOST_READY;
53716 + dma_desc->status.d32 = sts.d32;
53717 + }
53718 + }
53719 +
53720 + if (dwc_ep->is_in == 0) {
53721 + addr =
53722 + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
53723 + num]->doepctl;
53724 + } else {
53725 + addr =
53726 + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
53727 + }
53728 + depctl.b.epena = 1;
53729 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
53730 +}
53731 +
53732 +/**
53733 + * This function sets latest iso packet information(non-PTI mode)
53734 + *
53735 + * @param core_if Programming view of DWC_otg controller.
53736 + * @param ep The EP to start the transfer on.
53737 + *
53738 + */
53739 +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
53740 +{
53741 + deptsiz_data_t deptsiz = {.d32 = 0 };
53742 + dma_addr_t dma_addr;
53743 + uint32_t offset;
53744 +
53745 + if (ep->proc_buf_num)
53746 + dma_addr = ep->dma_addr1;
53747 + else
53748 + dma_addr = ep->dma_addr0;
53749 +
53750 + if (ep->is_in) {
53751 + deptsiz.d32 =
53752 + DWC_READ_REG32(&core_if->dev_if->
53753 + in_ep_regs[ep->num]->dieptsiz);
53754 + offset = ep->data_per_frame;
53755 + } else {
53756 + deptsiz.d32 =
53757 + DWC_READ_REG32(&core_if->dev_if->
53758 + out_ep_regs[ep->num]->doeptsiz);
53759 + offset =
53760 + ep->data_per_frame +
53761 + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
53762 + }
53763 +
53764 + if (!deptsiz.b.xfersize) {
53765 + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
53766 + ep->pkt_info[ep->cur_pkt].offset =
53767 + ep->cur_pkt_dma_addr - dma_addr;
53768 + ep->pkt_info[ep->cur_pkt].status = 0;
53769 + } else {
53770 + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
53771 + ep->pkt_info[ep->cur_pkt].offset =
53772 + ep->cur_pkt_dma_addr - dma_addr;
53773 + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
53774 + }
53775 + ep->cur_pkt_addr += offset;
53776 + ep->cur_pkt_dma_addr += offset;
53777 + ep->cur_pkt++;
53778 +}
53779 +
53780 +/**
53781 + * This function sets latest iso packet information(DDMA mode)
53782 + *
53783 + * @param core_if Programming view of DWC_otg controller.
53784 + * @param dwc_ep The EP to start the transfer on.
53785 + *
53786 + */
53787 +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
53788 + dwc_ep_t * dwc_ep)
53789 +{
53790 + dwc_otg_dev_dma_desc_t *dma_desc;
53791 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53792 + iso_pkt_info_t *iso_packet;
53793 + uint32_t data_per_desc;
53794 + uint32_t offset;
53795 + int i, j;
53796 +
53797 + iso_packet = dwc_ep->pkt_info;
53798 +
53799 + /** Reinit closed DMA Descriptors*/
53800 + /** ISO OUT EP */
53801 + if (dwc_ep->is_in == 0) {
53802 + dma_desc =
53803 + dwc_ep->iso_desc_addr +
53804 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
53805 + offset = 0;
53806 +
53807 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
53808 + i += dwc_ep->pkt_per_frm) {
53809 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
53810 + data_per_desc =
53811 + ((j + 1) * dwc_ep->maxpacket >
53812 + dwc_ep->
53813 + data_per_frame) ? dwc_ep->data_per_frame -
53814 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
53815 + data_per_desc +=
53816 + (data_per_desc % 4) ? (4 -
53817 + data_per_desc %
53818 + 4) : 0;
53819 +
53820 + sts.d32 = dma_desc->status.d32;
53821 +
53822 + /* Write status in iso_packet_decsriptor */
53823 + iso_packet->status =
53824 + sts.b_iso_out.rxsts +
53825 + (sts.b_iso_out.bs ^ BS_DMA_DONE);
53826 + if (iso_packet->status) {
53827 + iso_packet->status = -DWC_E_NO_DATA;
53828 + }
53829 +
53830 + /* Received data length */
53831 + if (!sts.b_iso_out.rxbytes) {
53832 + iso_packet->length =
53833 + data_per_desc -
53834 + sts.b_iso_out.rxbytes;
53835 + } else {
53836 + iso_packet->length =
53837 + data_per_desc -
53838 + sts.b_iso_out.rxbytes + (4 -
53839 + dwc_ep->data_per_frame
53840 + % 4);
53841 + }
53842 +
53843 + iso_packet->offset = offset;
53844 +
53845 + offset += data_per_desc;
53846 + dma_desc++;
53847 + iso_packet++;
53848 + }
53849 + }
53850 +
53851 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
53852 + data_per_desc =
53853 + ((j + 1) * dwc_ep->maxpacket >
53854 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
53855 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
53856 + data_per_desc +=
53857 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
53858 +
53859 + sts.d32 = dma_desc->status.d32;
53860 +
53861 + /* Write status in iso_packet_decsriptor */
53862 + iso_packet->status =
53863 + sts.b_iso_out.rxsts +
53864 + (sts.b_iso_out.bs ^ BS_DMA_DONE);
53865 + if (iso_packet->status) {
53866 + iso_packet->status = -DWC_E_NO_DATA;
53867 + }
53868 +
53869 + /* Received data length */
53870 + iso_packet->length =
53871 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
53872 +
53873 + iso_packet->offset = offset;
53874 +
53875 + offset += data_per_desc;
53876 + iso_packet++;
53877 + dma_desc++;
53878 + }
53879 +
53880 + sts.d32 = dma_desc->status.d32;
53881 +
53882 + /* Write status in iso_packet_decsriptor */
53883 + iso_packet->status =
53884 + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
53885 + if (iso_packet->status) {
53886 + iso_packet->status = -DWC_E_NO_DATA;
53887 + }
53888 + /* Received data length */
53889 + if (!sts.b_iso_out.rxbytes) {
53890 + iso_packet->length =
53891 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
53892 + } else {
53893 + iso_packet->length =
53894 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
53895 + (4 - dwc_ep->data_per_frame % 4);
53896 + }
53897 +
53898 + iso_packet->offset = offset;
53899 + } else {
53900 +/** ISO IN EP */
53901 +
53902 + dma_desc =
53903 + dwc_ep->iso_desc_addr +
53904 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
53905 +
53906 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
53907 + sts.d32 = dma_desc->status.d32;
53908 +
53909 + /* Write status in iso packet descriptor */
53910 + iso_packet->status =
53911 + sts.b_iso_in.txsts +
53912 + (sts.b_iso_in.bs ^ BS_DMA_DONE);
53913 + if (iso_packet->status != 0) {
53914 + iso_packet->status = -DWC_E_NO_DATA;
53915 +
53916 + }
53917 + /* Bytes has been transfered */
53918 + iso_packet->length =
53919 + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
53920 +
53921 + dma_desc++;
53922 + iso_packet++;
53923 + }
53924 +
53925 + sts.d32 = dma_desc->status.d32;
53926 + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
53927 + sts.d32 = dma_desc->status.d32;
53928 + }
53929 +
53930 + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
53931 + iso_packet->status =
53932 + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
53933 + if (iso_packet->status != 0) {
53934 + iso_packet->status = -DWC_E_NO_DATA;
53935 + }
53936 +
53937 + /* Bytes has been transfered */
53938 + iso_packet->length =
53939 + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
53940 + }
53941 +}
53942 +
53943 +/**
53944 + * This function reinitialize DMA Descriptors for Isochronous transfer
53945 + *
53946 + * @param core_if Programming view of DWC_otg controller.
53947 + * @param dwc_ep The EP to start the transfer on.
53948 + *
53949 + */
53950 +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
53951 +{
53952 + int i, j;
53953 + dwc_otg_dev_dma_desc_t *dma_desc;
53954 + dma_addr_t dma_ad;
53955 + volatile uint32_t *addr;
53956 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53957 + uint32_t data_per_desc;
53958 +
53959 + if (dwc_ep->is_in == 0) {
53960 + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
53961 + } else {
53962 + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
53963 + }
53964 +
53965 + if (dwc_ep->proc_buf_num == 0) {
53966 + /** Buffer 0 descriptors setup */
53967 + dma_ad = dwc_ep->dma_addr0;
53968 + } else {
53969 + /** Buffer 1 descriptors setup */
53970 + dma_ad = dwc_ep->dma_addr1;
53971 + }
53972 +
53973 + /** Reinit closed DMA Descriptors*/
53974 + /** ISO OUT EP */
53975 + if (dwc_ep->is_in == 0) {
53976 + dma_desc =
53977 + dwc_ep->iso_desc_addr +
53978 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
53979 +
53980 + sts.b_iso_out.bs = BS_HOST_READY;
53981 + sts.b_iso_out.rxsts = 0;
53982 + sts.b_iso_out.l = 0;
53983 + sts.b_iso_out.sp = 0;
53984 + sts.b_iso_out.ioc = 0;
53985 + sts.b_iso_out.pid = 0;
53986 + sts.b_iso_out.framenum = 0;
53987 +
53988 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
53989 + i += dwc_ep->pkt_per_frm) {
53990 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
53991 + data_per_desc =
53992 + ((j + 1) * dwc_ep->maxpacket >
53993 + dwc_ep->
53994 + data_per_frame) ? dwc_ep->data_per_frame -
53995 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
53996 + data_per_desc +=
53997 + (data_per_desc % 4) ? (4 -
53998 + data_per_desc %
53999 + 4) : 0;
54000 + sts.b_iso_out.rxbytes = data_per_desc;
54001 + dma_desc->buf = dma_ad;
54002 + dma_desc->status.d32 = sts.d32;
54003 +
54004 + dma_ad += data_per_desc;
54005 + dma_desc++;
54006 + }
54007 + }
54008 +
54009 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
54010 +
54011 + data_per_desc =
54012 + ((j + 1) * dwc_ep->maxpacket >
54013 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54014 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54015 + data_per_desc +=
54016 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54017 + sts.b_iso_out.rxbytes = data_per_desc;
54018 +
54019 + dma_desc->buf = dma_ad;
54020 + dma_desc->status.d32 = sts.d32;
54021 +
54022 + dma_desc++;
54023 + dma_ad += data_per_desc;
54024 + }
54025 +
54026 + sts.b_iso_out.ioc = 1;
54027 + sts.b_iso_out.l = dwc_ep->proc_buf_num;
54028 +
54029 + data_per_desc =
54030 + ((j + 1) * dwc_ep->maxpacket >
54031 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54032 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54033 + data_per_desc +=
54034 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54035 + sts.b_iso_out.rxbytes = data_per_desc;
54036 +
54037 + dma_desc->buf = dma_ad;
54038 + dma_desc->status.d32 = sts.d32;
54039 + } else {
54040 +/** ISO IN EP */
54041 +
54042 + dma_desc =
54043 + dwc_ep->iso_desc_addr +
54044 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54045 +
54046 + sts.b_iso_in.bs = BS_HOST_READY;
54047 + sts.b_iso_in.txsts = 0;
54048 + sts.b_iso_in.sp = 0;
54049 + sts.b_iso_in.ioc = 0;
54050 + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
54051 + sts.b_iso_in.framenum = dwc_ep->next_frame;
54052 + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
54053 + sts.b_iso_in.l = 0;
54054 +
54055 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
54056 + dma_desc->buf = dma_ad;
54057 + dma_desc->status.d32 = sts.d32;
54058 +
54059 + sts.b_iso_in.framenum += dwc_ep->bInterval;
54060 + dma_ad += dwc_ep->data_per_frame;
54061 + dma_desc++;
54062 + }
54063 +
54064 + sts.b_iso_in.ioc = 1;
54065 + sts.b_iso_in.l = dwc_ep->proc_buf_num;
54066 +
54067 + dma_desc->buf = dma_ad;
54068 + dma_desc->status.d32 = sts.d32;
54069 +
54070 + dwc_ep->next_frame =
54071 + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
54072 + }
54073 + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
54074 +}
54075 +
54076 +/**
54077 + * This function is to handle Iso EP transfer complete interrupt
54078 + * in case Iso out packet was dropped
54079 + *
54080 + * @param core_if Programming view of DWC_otg controller.
54081 + * @param dwc_ep The EP for wihich transfer complete was asserted
54082 + *
54083 + */
54084 +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
54085 + dwc_ep_t * dwc_ep)
54086 +{
54087 + uint32_t dma_addr;
54088 + uint32_t drp_pkt;
54089 + uint32_t drp_pkt_cnt;
54090 + deptsiz_data_t deptsiz = {.d32 = 0 };
54091 + depctl_data_t depctl = {.d32 = 0 };
54092 + int i;
54093 +
54094 + deptsiz.d32 =
54095 + DWC_READ_REG32(&core_if->dev_if->
54096 + out_ep_regs[dwc_ep->num]->doeptsiz);
54097 +
54098 + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
54099 + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
54100 +
54101 + /* Setting dropped packets status */
54102 + for (i = 0; i < drp_pkt_cnt; ++i) {
54103 + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
54104 + drp_pkt++;
54105 + deptsiz.b.pktcnt--;
54106 + }
54107 +
54108 + if (deptsiz.b.pktcnt > 0) {
54109 + deptsiz.b.xfersize =
54110 + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
54111 + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
54112 + } else {
54113 + deptsiz.b.xfersize = 0;
54114 + deptsiz.b.pktcnt = 0;
54115 + }
54116 +
54117 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
54118 + deptsiz.d32);
54119 +
54120 + if (deptsiz.b.pktcnt > 0) {
54121 + if (dwc_ep->proc_buf_num) {
54122 + dma_addr =
54123 + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
54124 + deptsiz.b.xfersize;
54125 + } else {
54126 + dma_addr =
54127 + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
54128 + deptsiz.b.xfersize;;
54129 + }
54130 +
54131 + DWC_WRITE_REG32(&core_if->dev_if->
54132 + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
54133 +
54134 + /** Re-enable endpoint, clear nak */
54135 + depctl.d32 = 0;
54136 + depctl.b.epena = 1;
54137 + depctl.b.cnak = 1;
54138 +
54139 + DWC_MODIFY_REG32(&core_if->dev_if->
54140 + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
54141 + depctl.d32);
54142 + return 0;
54143 + } else {
54144 + return 1;
54145 + }
54146 +}
54147 +
54148 +/**
54149 + * This function sets iso packets information(PTI mode)
54150 + *
54151 + * @param core_if Programming view of DWC_otg controller.
54152 + * @param ep The EP to start the transfer on.
54153 + *
54154 + */
54155 +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
54156 +{
54157 + int i, j;
54158 + dma_addr_t dma_ad;
54159 + iso_pkt_info_t *packet_info = ep->pkt_info;
54160 + uint32_t offset;
54161 + uint32_t frame_data;
54162 + deptsiz_data_t deptsiz;
54163 +
54164 + if (ep->proc_buf_num == 0) {
54165 + /** Buffer 0 descriptors setup */
54166 + dma_ad = ep->dma_addr0;
54167 + } else {
54168 + /** Buffer 1 descriptors setup */
54169 + dma_ad = ep->dma_addr1;
54170 + }
54171 +
54172 + if (ep->is_in) {
54173 + deptsiz.d32 =
54174 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
54175 + dieptsiz);
54176 + } else {
54177 + deptsiz.d32 =
54178 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
54179 + doeptsiz);
54180 + }
54181 +
54182 + if (!deptsiz.b.xfersize) {
54183 + offset = 0;
54184 + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
54185 + frame_data = ep->data_per_frame;
54186 + for (j = 0; j < ep->pkt_per_frm; ++j) {
54187 +
54188 + /* Packet status - is not set as initially
54189 + * it is set to 0 and if packet was sent
54190 + successfully, status field will remain 0*/
54191 +
54192 + /* Bytes has been transfered */
54193 + packet_info->length =
54194 + (ep->maxpacket <
54195 + frame_data) ? ep->maxpacket : frame_data;
54196 +
54197 + /* Received packet offset */
54198 + packet_info->offset = offset;
54199 + offset += packet_info->length;
54200 + frame_data -= packet_info->length;
54201 +
54202 + packet_info++;
54203 + }
54204 + }
54205 + return 1;
54206 + } else {
54207 + /* This is a workaround for in case of Transfer Complete with
54208 + * PktDrpSts interrupts merging - in this case Transfer complete
54209 + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
54210 + * set and with DOEPTSIZ register non zero. Investigations showed,
54211 + * that this happens when Out packet is dropped, but because of
54212 + * interrupts merging during first interrupt handling PktDrpSts
54213 + * bit is cleared and for next merged interrupts it is not reset.
54214 + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
54215 + */
54216 + if (ep->is_in) {
54217 + return 1;
54218 + } else {
54219 + return handle_iso_out_pkt_dropped(core_if, ep);
54220 + }
54221 + }
54222 +}
54223 +
54224 +/**
54225 + * This function is to handle Iso EP transfer complete interrupt
54226 + *
54227 + * @param pcd The PCD
54228 + * @param ep The EP for which transfer complete was asserted
54229 + *
54230 + */
54231 +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
54232 +{
54233 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
54234 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54235 + uint8_t is_last = 0;
54236 +
54237 + if (ep->dwc_ep.next_frame == 0xffffffff) {
54238 + DWC_WARN("Next frame is not set!\n");
54239 + return;
54240 + }
54241 +
54242 + if (core_if->dma_enable) {
54243 + if (core_if->dma_desc_enable) {
54244 + set_ddma_iso_pkts_info(core_if, dwc_ep);
54245 + reinit_ddma_iso_xfer(core_if, dwc_ep);
54246 + is_last = 1;
54247 + } else {
54248 + if (core_if->pti_enh_enable) {
54249 + if (set_iso_pkts_info(core_if, dwc_ep)) {
54250 + dwc_ep->proc_buf_num =
54251 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
54252 + dwc_otg_iso_ep_start_buf_transfer
54253 + (core_if, dwc_ep);
54254 + is_last = 1;
54255 + }
54256 + } else {
54257 + set_current_pkt_info(core_if, dwc_ep);
54258 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
54259 + is_last = 1;
54260 + dwc_ep->cur_pkt = 0;
54261 + dwc_ep->proc_buf_num =
54262 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
54263 + if (dwc_ep->proc_buf_num) {
54264 + dwc_ep->cur_pkt_addr =
54265 + dwc_ep->xfer_buff1;
54266 + dwc_ep->cur_pkt_dma_addr =
54267 + dwc_ep->dma_addr1;
54268 + } else {
54269 + dwc_ep->cur_pkt_addr =
54270 + dwc_ep->xfer_buff0;
54271 + dwc_ep->cur_pkt_dma_addr =
54272 + dwc_ep->dma_addr0;
54273 + }
54274 +
54275 + }
54276 + dwc_otg_iso_ep_start_frm_transfer(core_if,
54277 + dwc_ep);
54278 + }
54279 + }
54280 + } else {
54281 + set_current_pkt_info(core_if, dwc_ep);
54282 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
54283 + is_last = 1;
54284 + dwc_ep->cur_pkt = 0;
54285 + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
54286 + if (dwc_ep->proc_buf_num) {
54287 + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
54288 + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
54289 + } else {
54290 + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
54291 + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
54292 + }
54293 +
54294 + }
54295 + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
54296 + }
54297 + if (is_last)
54298 + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
54299 +}
54300 +#endif /* DWC_EN_ISOC */
54301 +
54302 +/**
54303 + * This function handle BNA interrupt for Non Isochronous EPs
54304 + *
54305 + */
54306 +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
54307 +{
54308 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54309 + volatile uint32_t *addr;
54310 + depctl_data_t depctl = {.d32 = 0 };
54311 + dwc_otg_pcd_t *pcd = ep->pcd;
54312 + dwc_otg_dev_dma_desc_t *dma_desc;
54313 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54314 + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
54315 + int i, start;
54316 +
54317 + if (!dwc_ep->desc_cnt)
54318 + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
54319 + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
54320 +
54321 + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
54322 + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
54323 + uint32_t doepdma;
54324 + dwc_otg_dev_out_ep_regs_t *out_regs =
54325 + core_if->dev_if->out_ep_regs[dwc_ep->num];
54326 + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
54327 + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
54328 + dma_desc = &(dwc_ep->desc_addr[start]);
54329 + } else {
54330 + start = 0;
54331 + dma_desc = dwc_ep->desc_addr;
54332 + }
54333 +
54334 +
54335 + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
54336 + sts.d32 = dma_desc->status.d32;
54337 + sts.b.bs = BS_HOST_READY;
54338 + dma_desc->status.d32 = sts.d32;
54339 + }
54340 +
54341 + if (dwc_ep->is_in == 0) {
54342 + addr =
54343 + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
54344 + doepctl;
54345 + } else {
54346 + addr =
54347 + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
54348 + }
54349 + depctl.b.epena = 1;
54350 + depctl.b.cnak = 1;
54351 + DWC_MODIFY_REG32(addr, 0, depctl.d32);
54352 +}
54353 +
54354 +/**
54355 + * This function handles EP0 Control transfers.
54356 + *
54357 + * The state of the control transfers are tracked in
54358 + * <code>ep0state</code>.
54359 + */
54360 +static void handle_ep0(dwc_otg_pcd_t * pcd)
54361 +{
54362 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
54363 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
54364 + dev_dma_desc_sts_t desc_sts;
54365 + deptsiz0_data_t deptsiz;
54366 + uint32_t byte_count;
54367 +
54368 +#ifdef DEBUG_EP0
54369 + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
54370 + print_ep0_state(pcd);
54371 +#endif
54372 +
54373 +// DWC_PRINTF("HANDLE EP0\n");
54374 +
54375 + switch (pcd->ep0state) {
54376 + case EP0_DISCONNECT:
54377 + break;
54378 +
54379 + case EP0_IDLE:
54380 + pcd->request_config = 0;
54381 +
54382 + pcd_setup(pcd);
54383 + break;
54384 +
54385 + case EP0_IN_DATA_PHASE:
54386 +#ifdef DEBUG_EP0
54387 + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
54388 + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
54389 + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
54390 +#endif
54391 +
54392 + if (core_if->dma_enable != 0) {
54393 + /*
54394 + * For EP0 we can only program 1 packet at a time so we
54395 + * need to do the make calculations after each complete.
54396 + * Call write_packet to make the calculations, as in
54397 + * slave mode, and use those values to determine if we
54398 + * can complete.
54399 + */
54400 + if (core_if->dma_desc_enable == 0) {
54401 + deptsiz.d32 =
54402 + DWC_READ_REG32(&core_if->
54403 + dev_if->in_ep_regs[0]->
54404 + dieptsiz);
54405 + byte_count =
54406 + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
54407 + } else {
54408 + desc_sts =
54409 + core_if->dev_if->in_desc_addr->status;
54410 + byte_count =
54411 + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
54412 + }
54413 + ep0->dwc_ep.xfer_count += byte_count;
54414 + ep0->dwc_ep.xfer_buff += byte_count;
54415 + ep0->dwc_ep.dma_addr += byte_count;
54416 + }
54417 + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
54418 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54419 + &ep0->dwc_ep);
54420 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
54421 + } else if (ep0->dwc_ep.sent_zlp) {
54422 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54423 + &ep0->dwc_ep);
54424 + ep0->dwc_ep.sent_zlp = 0;
54425 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
54426 + } else {
54427 + ep0_complete_request(ep0);
54428 + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
54429 + }
54430 + break;
54431 + case EP0_OUT_DATA_PHASE:
54432 +#ifdef DEBUG_EP0
54433 + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
54434 + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
54435 + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
54436 +#endif
54437 + if (core_if->dma_enable != 0) {
54438 + if (core_if->dma_desc_enable == 0) {
54439 + deptsiz.d32 =
54440 + DWC_READ_REG32(&core_if->
54441 + dev_if->out_ep_regs[0]->
54442 + doeptsiz);
54443 + byte_count =
54444 + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
54445 + } else {
54446 + desc_sts =
54447 + core_if->dev_if->out_desc_addr->status;
54448 + byte_count =
54449 + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
54450 + }
54451 + ep0->dwc_ep.xfer_count += byte_count;
54452 + ep0->dwc_ep.xfer_buff += byte_count;
54453 + ep0->dwc_ep.dma_addr += byte_count;
54454 + }
54455 + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
54456 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54457 + &ep0->dwc_ep);
54458 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
54459 + } else if (ep0->dwc_ep.sent_zlp) {
54460 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54461 + &ep0->dwc_ep);
54462 + ep0->dwc_ep.sent_zlp = 0;
54463 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
54464 + } else {
54465 + ep0_complete_request(ep0);
54466 + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
54467 + }
54468 + break;
54469 +
54470 + case EP0_IN_STATUS_PHASE:
54471 + case EP0_OUT_STATUS_PHASE:
54472 + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
54473 + ep0_complete_request(ep0);
54474 + pcd->ep0state = EP0_IDLE;
54475 + ep0->stopped = 1;
54476 + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
54477 +
54478 + /* Prepare for more SETUP Packets */
54479 + if (core_if->dma_enable) {
54480 + ep0_out_start(core_if, pcd);
54481 + }
54482 + break;
54483 +
54484 + case EP0_STALL:
54485 + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
54486 + break;
54487 + }
54488 +#ifdef DEBUG_EP0
54489 + print_ep0_state(pcd);
54490 +#endif
54491 +}
54492 +
54493 +/**
54494 + * Restart transfer
54495 + */
54496 +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
54497 +{
54498 + dwc_otg_core_if_t *core_if;
54499 + dwc_otg_dev_if_t *dev_if;
54500 + deptsiz_data_t dieptsiz = {.d32 = 0 };
54501 + dwc_otg_pcd_ep_t *ep;
54502 +
54503 + ep = get_in_ep(pcd, epnum);
54504 +
54505 +#ifdef DWC_EN_ISOC
54506 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
54507 + return;
54508 + }
54509 +#endif /* DWC_EN_ISOC */
54510 +
54511 + core_if = GET_CORE_IF(pcd);
54512 + dev_if = core_if->dev_if;
54513 +
54514 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
54515 +
54516 + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
54517 + " stopped=%d\n", ep->dwc_ep.xfer_buff,
54518 + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
54519 + /*
54520 + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
54521 + */
54522 + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
54523 + ep->dwc_ep.start_xfer_buff != 0) {
54524 + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
54525 + ep->dwc_ep.xfer_count = 0;
54526 + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
54527 + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
54528 + } else {
54529 + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
54530 + /* convert packet size to dwords. */
54531 + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
54532 + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
54533 + }
54534 + ep->stopped = 0;
54535 + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
54536 + "xfer_len=%0x stopped=%d\n",
54537 + ep->dwc_ep.xfer_buff,
54538 + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
54539 + ep->stopped);
54540 + if (epnum == 0) {
54541 + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
54542 + } else {
54543 + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
54544 + }
54545 + }
54546 +}
54547 +
54548 +/*
54549 + * This function create new nextep sequnce based on Learn Queue.
54550 + *
54551 + * @param core_if Programming view of DWC_otg controller
54552 + */
54553 +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
54554 +{
54555 + dwc_otg_device_global_regs_t *dev_global_regs =
54556 + core_if->dev_if->dev_global_regs;
54557 + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
54558 + /* Number of Token Queue Registers */
54559 + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
54560 + dtknq1_data_t dtknqr1;
54561 + uint32_t in_tkn_epnums[4];
54562 + uint8_t seqnum[MAX_EPS_CHANNELS];
54563 + uint8_t intkn_seq[TOKEN_Q_DEPTH];
54564 + grstctl_t resetctl = {.d32 = 0 };
54565 + uint8_t temp;
54566 + int ndx = 0;
54567 + int start = 0;
54568 + int end = 0;
54569 + int sort_done = 0;
54570 + int i = 0;
54571 + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
54572 +
54573 +
54574 + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
54575 +
54576 + /* Read the DTKNQ Registers */
54577 + for (i = 0; i < DTKNQ_REG_CNT; i++) {
54578 + in_tkn_epnums[i] = DWC_READ_REG32(addr);
54579 + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
54580 + in_tkn_epnums[i]);
54581 + if (addr == &dev_global_regs->dvbusdis) {
54582 + addr = &dev_global_regs->dtknqr3_dthrctl;
54583 + } else {
54584 + ++addr;
54585 + }
54586 +
54587 + }
54588 +
54589 + /* Copy the DTKNQR1 data to the bit field. */
54590 + dtknqr1.d32 = in_tkn_epnums[0];
54591 + if (dtknqr1.b.wrap_bit) {
54592 + ndx = dtknqr1.b.intknwptr;
54593 + end = ndx -1;
54594 + if (end < 0)
54595 + end = TOKEN_Q_DEPTH -1;
54596 + } else {
54597 + ndx = 0;
54598 + end = dtknqr1.b.intknwptr -1;
54599 + if (end < 0)
54600 + end = 0;
54601 + }
54602 + start = ndx;
54603 +
54604 + /* Fill seqnum[] by initial values: EP number + 31 */
54605 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
54606 + seqnum[i] = i +31;
54607 + }
54608 +
54609 + /* Fill intkn_seq[] from in_tkn_epnums[0] */
54610 + for (i=0; i < 6; i++)
54611 + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
54612 +
54613 + if (TOKEN_Q_DEPTH > 6) {
54614 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54615 + for (i=6; i < 14; i++)
54616 + intkn_seq[i] =
54617 + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
54618 + }
54619 +
54620 + if (TOKEN_Q_DEPTH > 14) {
54621 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54622 + for (i=14; i < 22; i++)
54623 + intkn_seq[i] =
54624 + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
54625 + }
54626 +
54627 + if (TOKEN_Q_DEPTH > 22) {
54628 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54629 + for (i=22; i < 30; i++)
54630 + intkn_seq[i] =
54631 + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
54632 + }
54633 +
54634 + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
54635 + start, end);
54636 + for (i=0; i<TOKEN_Q_DEPTH; i++)
54637 + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
54638 +
54639 + /* Update seqnum based on intkn_seq[] */
54640 + i = 0;
54641 + do {
54642 + seqnum[intkn_seq[ndx]] = i;
54643 + ndx++;
54644 + i++;
54645 + if (ndx == TOKEN_Q_DEPTH)
54646 + ndx = 0;
54647 + } while ( i < TOKEN_Q_DEPTH );
54648 +
54649 + /* Mark non active EP's in seqnum[] by 0xff */
54650 + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
54651 + if (core_if->nextep_seq[i] == 0xff )
54652 + seqnum[i] = 0xff;
54653 + }
54654 +
54655 + /* Sort seqnum[] */
54656 + sort_done = 0;
54657 + while (!sort_done) {
54658 + sort_done = 1;
54659 + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
54660 + if (seqnum[i] > seqnum[i+1]) {
54661 + temp = seqnum[i];
54662 + seqnum[i] = seqnum[i+1];
54663 + seqnum[i+1] = temp;
54664 + sort_done = 0;
54665 + }
54666 + }
54667 + }
54668 +
54669 + ndx = start + seqnum[0];
54670 + if (ndx >= TOKEN_Q_DEPTH)
54671 + ndx = ndx % TOKEN_Q_DEPTH;
54672 + core_if->first_in_nextep_seq = intkn_seq[ndx];
54673 +
54674 + /* Update seqnum[] by EP numbers */
54675 + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
54676 + ndx = start + i;
54677 + if (seqnum[i] < 31) {
54678 + ndx = start + seqnum[i];
54679 + if (ndx >= TOKEN_Q_DEPTH)
54680 + ndx = ndx % TOKEN_Q_DEPTH;
54681 + seqnum[i] = intkn_seq[ndx];
54682 + } else {
54683 + if (seqnum[i] < 0xff) {
54684 + seqnum[i] = seqnum[i] - 31;
54685 + } else {
54686 + break;
54687 + }
54688 + }
54689 + }
54690 +
54691 + /* Update nextep_seq[] based on seqnum[] */
54692 + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
54693 + if (seqnum[i] != 0xff) {
54694 + if (seqnum[i+1] != 0xff) {
54695 + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
54696 + } else {
54697 + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
54698 + break;
54699 + }
54700 + } else {
54701 + break;
54702 + }
54703 + }
54704 +
54705 + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
54706 + __func__, core_if->first_in_nextep_seq);
54707 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
54708 + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
54709 + }
54710 +
54711 + /* Flush the Learning Queue */
54712 + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
54713 + resetctl.b.intknqflsh = 1;
54714 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
54715 +
54716 +
54717 +}
54718 +
54719 +/**
54720 + * handle the IN EP disable interrupt.
54721 + */
54722 +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
54723 + const uint32_t epnum)
54724 +{
54725 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
54726 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
54727 + deptsiz_data_t dieptsiz = {.d32 = 0 };
54728 + dctl_data_t dctl = {.d32 = 0 };
54729 + dwc_otg_pcd_ep_t *ep;
54730 + dwc_ep_t *dwc_ep;
54731 + gintmsk_data_t gintmsk_data;
54732 + depctl_data_t depctl;
54733 + uint32_t diepdma;
54734 + uint32_t remain_to_transfer = 0;
54735 + uint8_t i;
54736 + uint32_t xfer_size;
54737 +
54738 + ep = get_in_ep(pcd, epnum);
54739 + dwc_ep = &ep->dwc_ep;
54740 +
54741 + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
54742 + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
54743 + complete_ep(ep);
54744 + return;
54745 + }
54746 +
54747 + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
54748 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
54749 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
54750 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
54751 +
54752 + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
54753 + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
54754 +
54755 + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
54756 + if (ep->stopped) {
54757 + if (core_if->en_multiple_tx_fifo)
54758 + /* Flush the Tx FIFO */
54759 + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
54760 + /* Clear the Global IN NP NAK */
54761 + dctl.d32 = 0;
54762 + dctl.b.cgnpinnak = 1;
54763 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
54764 + /* Restart the transaction */
54765 + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
54766 + restart_transfer(pcd, epnum);
54767 + }
54768 + } else {
54769 + /* Restart the transaction */
54770 + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
54771 + restart_transfer(pcd, epnum);
54772 + }
54773 + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
54774 + }
54775 + return;
54776 + }
54777 +
54778 + if (core_if->start_predict > 2) { // NP IN EP
54779 + core_if->start_predict--;
54780 + return;
54781 + }
54782 +
54783 + core_if->start_predict--;
54784 +
54785 + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
54786 +
54787 + predict_nextep_seq(core_if);
54788 +
54789 + /* Update all active IN EP's NextEP field based of nextep_seq[] */
54790 + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
54791 + depctl.d32 =
54792 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
54793 + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
54794 + depctl.b.nextep = core_if->nextep_seq[i];
54795 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
54796 + }
54797 + }
54798 + /* Flush Shared NP TxFIFO */
54799 + dwc_otg_flush_tx_fifo(core_if, 0);
54800 + /* Rewind buffers */
54801 + if (!core_if->dma_desc_enable) {
54802 + i = core_if->first_in_nextep_seq;
54803 + do {
54804 + ep = get_in_ep(pcd, i);
54805 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
54806 + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
54807 + if (xfer_size > ep->dwc_ep.maxxfer)
54808 + xfer_size = ep->dwc_ep.maxxfer;
54809 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
54810 + if (dieptsiz.b.pktcnt != 0) {
54811 + if (xfer_size == 0) {
54812 + remain_to_transfer = 0;
54813 + } else {
54814 + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
54815 + remain_to_transfer =
54816 + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
54817 + } else {
54818 + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
54819 + + (xfer_size % ep->dwc_ep.maxpacket);
54820 + }
54821 + }
54822 + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
54823 + dieptsiz.b.xfersize = remain_to_transfer;
54824 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
54825 + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
54826 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
54827 + }
54828 + i = core_if->nextep_seq[i];
54829 + } while (i != core_if->first_in_nextep_seq);
54830 + } else { // dma_desc_enable
54831 + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
54832 + }
54833 +
54834 + /* Restart transfers in predicted sequences */
54835 + i = core_if->first_in_nextep_seq;
54836 + do {
54837 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
54838 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
54839 + if (dieptsiz.b.pktcnt != 0) {
54840 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
54841 + depctl.b.epena = 1;
54842 + depctl.b.cnak = 1;
54843 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
54844 + }
54845 + i = core_if->nextep_seq[i];
54846 + } while (i != core_if->first_in_nextep_seq);
54847 +
54848 + /* Clear the global non-periodic IN NAK handshake */
54849 + dctl.d32 = 0;
54850 + dctl.b.cgnpinnak = 1;
54851 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
54852 +
54853 + /* Unmask EP Mismatch interrupt */
54854 + gintmsk_data.d32 = 0;
54855 + gintmsk_data.b.epmismatch = 1;
54856 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
54857 +
54858 + core_if->start_predict = 0;
54859 +
54860 + }
54861 +}
54862 +
54863 +/**
54864 + * Handler for the IN EP timeout handshake interrupt.
54865 + */
54866 +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
54867 + const uint32_t epnum)
54868 +{
54869 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
54870 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
54871 +
54872 +#ifdef DEBUG
54873 + deptsiz_data_t dieptsiz = {.d32 = 0 };
54874 + uint32_t num = 0;
54875 +#endif
54876 + dctl_data_t dctl = {.d32 = 0 };
54877 + dwc_otg_pcd_ep_t *ep;
54878 +
54879 + gintmsk_data_t intr_mask = {.d32 = 0 };
54880 +
54881 + ep = get_in_ep(pcd, epnum);
54882 +
54883 + /* Disable the NP Tx Fifo Empty Interrrupt */
54884 + if (!core_if->dma_enable) {
54885 + intr_mask.b.nptxfempty = 1;
54886 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
54887 + intr_mask.d32, 0);
54888 + }
54889 + /** @todo NGS Check EP type.
54890 + * Implement for Periodic EPs */
54891 + /*
54892 + * Non-periodic EP
54893 + */
54894 + /* Enable the Global IN NAK Effective Interrupt */
54895 + intr_mask.b.ginnakeff = 1;
54896 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
54897 +
54898 + /* Set Global IN NAK */
54899 + dctl.b.sgnpinnak = 1;
54900 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
54901 +
54902 + ep->stopped = 1;
54903 +
54904 +#ifdef DEBUG
54905 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
54906 + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
54907 + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
54908 +#endif
54909 +
54910 +#ifdef DISABLE_PERIODIC_EP
54911 + /*
54912 + * Set the NAK bit for this EP to
54913 + * start the disable process.
54914 + */
54915 + diepctl.d32 = 0;
54916 + diepctl.b.snak = 1;
54917 + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
54918 + diepctl.d32);
54919 + ep->disabling = 1;
54920 + ep->stopped = 1;
54921 +#endif
54922 +}
54923 +
54924 +/**
54925 + * Handler for the IN EP NAK interrupt.
54926 + */
54927 +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
54928 + const uint32_t epnum)
54929 +{
54930 + /** @todo implement ISR */
54931 + dwc_otg_core_if_t *core_if;
54932 + diepmsk_data_t intr_mask = {.d32 = 0 };
54933 +
54934 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
54935 + core_if = GET_CORE_IF(pcd);
54936 + intr_mask.b.nak = 1;
54937 +
54938 + if (core_if->multiproc_int_enable) {
54939 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
54940 + diepeachintmsk[epnum], intr_mask.d32, 0);
54941 + } else {
54942 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
54943 + intr_mask.d32, 0);
54944 + }
54945 +
54946 + return 1;
54947 +}
54948 +
54949 +/**
54950 + * Handler for the OUT EP Babble interrupt.
54951 + */
54952 +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
54953 + const uint32_t epnum)
54954 +{
54955 + /** @todo implement ISR */
54956 + dwc_otg_core_if_t *core_if;
54957 + doepmsk_data_t intr_mask = {.d32 = 0 };
54958 +
54959 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
54960 + "OUT EP Babble");
54961 + core_if = GET_CORE_IF(pcd);
54962 + intr_mask.b.babble = 1;
54963 +
54964 + if (core_if->multiproc_int_enable) {
54965 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
54966 + doepeachintmsk[epnum], intr_mask.d32, 0);
54967 + } else {
54968 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
54969 + intr_mask.d32, 0);
54970 + }
54971 +
54972 + return 1;
54973 +}
54974 +
54975 +/**
54976 + * Handler for the OUT EP NAK interrupt.
54977 + */
54978 +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
54979 + const uint32_t epnum)
54980 +{
54981 + /** @todo implement ISR */
54982 + dwc_otg_core_if_t *core_if;
54983 + doepmsk_data_t intr_mask = {.d32 = 0 };
54984 +
54985 + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
54986 + core_if = GET_CORE_IF(pcd);
54987 + intr_mask.b.nak = 1;
54988 +
54989 + if (core_if->multiproc_int_enable) {
54990 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
54991 + doepeachintmsk[epnum], intr_mask.d32, 0);
54992 + } else {
54993 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
54994 + intr_mask.d32, 0);
54995 + }
54996 +
54997 + return 1;
54998 +}
54999 +
55000 +/**
55001 + * Handler for the OUT EP NYET interrupt.
55002 + */
55003 +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
55004 + const uint32_t epnum)
55005 +{
55006 + /** @todo implement ISR */
55007 + dwc_otg_core_if_t *core_if;
55008 + doepmsk_data_t intr_mask = {.d32 = 0 };
55009 +
55010 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
55011 + core_if = GET_CORE_IF(pcd);
55012 + intr_mask.b.nyet = 1;
55013 +
55014 + if (core_if->multiproc_int_enable) {
55015 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55016 + doepeachintmsk[epnum], intr_mask.d32, 0);
55017 + } else {
55018 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55019 + intr_mask.d32, 0);
55020 + }
55021 +
55022 + return 1;
55023 +}
55024 +
55025 +/**
55026 + * This interrupt indicates that an IN EP has a pending Interrupt.
55027 + * The sequence for handling the IN EP interrupt is shown below:
55028 + * -# Read the Device All Endpoint Interrupt register
55029 + * -# Repeat the following for each IN EP interrupt bit set (from
55030 + * LSB to MSB).
55031 + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
55032 + * -# If "Transfer Complete" call the request complete function
55033 + * -# If "Endpoint Disabled" complete the EP disable procedure.
55034 + * -# If "AHB Error Interrupt" log error
55035 + * -# If "Time-out Handshake" log error
55036 + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
55037 + * FIFO.
55038 + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
55039 + * Mismatch Interrupt)
55040 + */
55041 +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
55042 +{
55043 +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
55044 +do { \
55045 + diepint_data_t diepint = {.d32=0}; \
55046 + diepint.b.__intr = 1; \
55047 + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
55048 + diepint.d32); \
55049 +} while (0)
55050 +
55051 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55052 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
55053 + diepint_data_t diepint = {.d32 = 0 };
55054 + depctl_data_t depctl = {.d32 = 0 };
55055 + uint32_t ep_intr;
55056 + uint32_t epnum = 0;
55057 + dwc_otg_pcd_ep_t *ep;
55058 + dwc_ep_t *dwc_ep;
55059 + gintmsk_data_t intr_mask = {.d32 = 0 };
55060 +
55061 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
55062 +
55063 + /* Read in the device interrupt bits */
55064 + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
55065 +
55066 + /* Service the Device IN interrupts for each endpoint */
55067 + while (ep_intr) {
55068 + if (ep_intr & 0x1) {
55069 + uint32_t empty_msk;
55070 + /* Get EP pointer */
55071 + ep = get_in_ep(pcd, epnum);
55072 + dwc_ep = &ep->dwc_ep;
55073 +
55074 + depctl.d32 =
55075 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55076 + empty_msk =
55077 + DWC_READ_REG32(&dev_if->
55078 + dev_global_regs->dtknqr4_fifoemptymsk);
55079 +
55080 + DWC_DEBUGPL(DBG_PCDV,
55081 + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
55082 + epnum, empty_msk, depctl.d32);
55083 +
55084 + DWC_DEBUGPL(DBG_PCD,
55085 + "EP%d-%s: type=%d, mps=%d\n",
55086 + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
55087 + dwc_ep->type, dwc_ep->maxpacket);
55088 +
55089 + diepint.d32 =
55090 + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
55091 +
55092 + DWC_DEBUGPL(DBG_PCDV,
55093 + "EP %d Interrupt Register - 0x%x\n", epnum,
55094 + diepint.d32);
55095 + /* Transfer complete */
55096 + if (diepint.b.xfercompl) {
55097 + /* Disable the NP Tx FIFO Empty
55098 + * Interrupt */
55099 + if (core_if->en_multiple_tx_fifo == 0) {
55100 + intr_mask.b.nptxfempty = 1;
55101 + DWC_MODIFY_REG32
55102 + (&core_if->core_global_regs->gintmsk,
55103 + intr_mask.d32, 0);
55104 + } else {
55105 + /* Disable the Tx FIFO Empty Interrupt for this EP */
55106 + uint32_t fifoemptymsk =
55107 + 0x1 << dwc_ep->num;
55108 + DWC_MODIFY_REG32(&core_if->
55109 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
55110 + fifoemptymsk, 0);
55111 + }
55112 + /* Clear the bit in DIEPINTn for this interrupt */
55113 + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
55114 +
55115 + /* Complete the transfer */
55116 + if (epnum == 0) {
55117 + handle_ep0(pcd);
55118 + }
55119 +#ifdef DWC_EN_ISOC
55120 + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55121 + if (!ep->stopped)
55122 + complete_iso_ep(pcd, ep);
55123 + }
55124 +#endif /* DWC_EN_ISOC */
55125 +#ifdef DWC_UTE_PER_IO
55126 + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55127 + if (!ep->stopped)
55128 + complete_xiso_ep(ep);
55129 + }
55130 +#endif /* DWC_UTE_PER_IO */
55131 + else {
55132 + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
55133 + dwc_ep->bInterval > 1) {
55134 + dwc_ep->frame_num += dwc_ep->bInterval;
55135 + if (dwc_ep->frame_num > 0x3FFF)
55136 + {
55137 + dwc_ep->frm_overrun = 1;
55138 + dwc_ep->frame_num &= 0x3FFF;
55139 + } else
55140 + dwc_ep->frm_overrun = 0;
55141 + }
55142 + complete_ep(ep);
55143 + if(diepint.b.nak)
55144 + CLEAR_IN_EP_INTR(core_if, epnum, nak);
55145 + }
55146 + }
55147 + /* Endpoint disable */
55148 + if (diepint.b.epdisabled) {
55149 + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
55150 + epnum);
55151 + handle_in_ep_disable_intr(pcd, epnum);
55152 +
55153 + /* Clear the bit in DIEPINTn for this interrupt */
55154 + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
55155 + }
55156 + /* AHB Error */
55157 + if (diepint.b.ahberr) {
55158 + DWC_ERROR("EP%d IN AHB Error\n", epnum);
55159 + /* Clear the bit in DIEPINTn for this interrupt */
55160 + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
55161 + }
55162 + /* TimeOUT Handshake (non-ISOC IN EPs) */
55163 + if (diepint.b.timeout) {
55164 + DWC_ERROR("EP%d IN Time-out\n", epnum);
55165 + handle_in_ep_timeout_intr(pcd, epnum);
55166 +
55167 + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
55168 + }
55169 + /** IN Token received with TxF Empty */
55170 + if (diepint.b.intktxfemp) {
55171 + DWC_DEBUGPL(DBG_ANY,
55172 + "EP%d IN TKN TxFifo Empty\n",
55173 + epnum);
55174 + if (!ep->stopped && epnum != 0) {
55175 +
55176 + diepmsk_data_t diepmsk = {.d32 = 0 };
55177 + diepmsk.b.intktxfemp = 1;
55178 +
55179 + if (core_if->multiproc_int_enable) {
55180 + DWC_MODIFY_REG32
55181 + (&dev_if->dev_global_regs->diepeachintmsk
55182 + [epnum], diepmsk.d32, 0);
55183 + } else {
55184 + DWC_MODIFY_REG32
55185 + (&dev_if->dev_global_regs->diepmsk,
55186 + diepmsk.d32, 0);
55187 + }
55188 + } else if (core_if->dma_desc_enable
55189 + && epnum == 0
55190 + && pcd->ep0state ==
55191 + EP0_OUT_STATUS_PHASE) {
55192 + // EP0 IN set STALL
55193 + depctl.d32 =
55194 + DWC_READ_REG32(&dev_if->in_ep_regs
55195 + [epnum]->diepctl);
55196 +
55197 + /* set the disable and stall bits */
55198 + if (depctl.b.epena) {
55199 + depctl.b.epdis = 1;
55200 + }
55201 + depctl.b.stall = 1;
55202 + DWC_WRITE_REG32(&dev_if->in_ep_regs
55203 + [epnum]->diepctl,
55204 + depctl.d32);
55205 + }
55206 + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
55207 + }
55208 + /** IN Token Received with EP mismatch */
55209 + if (diepint.b.intknepmis) {
55210 + DWC_DEBUGPL(DBG_ANY,
55211 + "EP%d IN TKN EP Mismatch\n", epnum);
55212 + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
55213 + }
55214 + /** IN Endpoint NAK Effective */
55215 + if (diepint.b.inepnakeff) {
55216 + DWC_DEBUGPL(DBG_ANY,
55217 + "EP%d IN EP NAK Effective\n",
55218 + epnum);
55219 + /* Periodic EP */
55220 + if (ep->disabling) {
55221 + depctl.d32 = 0;
55222 + depctl.b.snak = 1;
55223 + depctl.b.epdis = 1;
55224 + DWC_MODIFY_REG32(&dev_if->in_ep_regs
55225 + [epnum]->diepctl,
55226 + depctl.d32,
55227 + depctl.d32);
55228 + }
55229 + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
55230 +
55231 + }
55232 +
55233 + /** IN EP Tx FIFO Empty Intr */
55234 + if (diepint.b.emptyintr) {
55235 + DWC_DEBUGPL(DBG_ANY,
55236 + "EP%d Tx FIFO Empty Intr \n",
55237 + epnum);
55238 + write_empty_tx_fifo(pcd, epnum);
55239 +
55240 + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
55241 +
55242 + }
55243 +
55244 + /** IN EP BNA Intr */
55245 + if (diepint.b.bna) {
55246 + CLEAR_IN_EP_INTR(core_if, epnum, bna);
55247 + if (core_if->dma_desc_enable) {
55248 +#ifdef DWC_EN_ISOC
55249 + if (dwc_ep->type ==
55250 + DWC_OTG_EP_TYPE_ISOC) {
55251 + /*
55252 + * This checking is performed to prevent first "false" BNA
55253 + * handling occuring right after reconnect
55254 + */
55255 + if (dwc_ep->next_frame !=
55256 + 0xffffffff)
55257 + dwc_otg_pcd_handle_iso_bna(ep);
55258 + } else
55259 +#endif /* DWC_EN_ISOC */
55260 + {
55261 + dwc_otg_pcd_handle_noniso_bna(ep);
55262 + }
55263 + }
55264 + }
55265 + /* NAK Interrutp */
55266 + if (diepint.b.nak) {
55267 + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
55268 + epnum);
55269 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
55270 + depctl_data_t depctl;
55271 + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
55272 + ep->dwc_ep.frame_num = core_if->frame_num;
55273 + if (ep->dwc_ep.bInterval > 1) {
55274 + depctl.d32 = 0;
55275 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55276 + if (ep->dwc_ep.frame_num & 0x1) {
55277 + depctl.b.setd1pid = 1;
55278 + depctl.b.setd0pid = 0;
55279 + } else {
55280 + depctl.b.setd0pid = 1;
55281 + depctl.b.setd1pid = 0;
55282 + }
55283 + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
55284 + }
55285 + start_next_request(ep);
55286 + }
55287 + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
55288 + if (dwc_ep->frame_num > 0x3FFF) {
55289 + dwc_ep->frm_overrun = 1;
55290 + dwc_ep->frame_num &= 0x3FFF;
55291 + } else
55292 + dwc_ep->frm_overrun = 0;
55293 + }
55294 +
55295 + CLEAR_IN_EP_INTR(core_if, epnum, nak);
55296 + }
55297 + }
55298 + epnum++;
55299 + ep_intr >>= 1;
55300 + }
55301 +
55302 + return 1;
55303 +#undef CLEAR_IN_EP_INTR
55304 +}
55305 +
55306 +/**
55307 + * This interrupt indicates that an OUT EP has a pending Interrupt.
55308 + * The sequence for handling the OUT EP interrupt is shown below:
55309 + * -# Read the Device All Endpoint Interrupt register
55310 + * -# Repeat the following for each OUT EP interrupt bit set (from
55311 + * LSB to MSB).
55312 + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
55313 + * -# If "Transfer Complete" call the request complete function
55314 + * -# If "Endpoint Disabled" complete the EP disable procedure.
55315 + * -# If "AHB Error Interrupt" log error
55316 + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
55317 + * Command Processing)
55318 + */
55319 +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
55320 +{
55321 +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
55322 +do { \
55323 + doepint_data_t doepint = {.d32=0}; \
55324 + doepint.b.__intr = 1; \
55325 + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
55326 + doepint.d32); \
55327 +} while (0)
55328 +
55329 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55330 + uint32_t ep_intr;
55331 + doepint_data_t doepint = {.d32 = 0 };
55332 + uint32_t epnum = 0;
55333 + dwc_otg_pcd_ep_t *ep;
55334 + dwc_ep_t *dwc_ep;
55335 + dctl_data_t dctl = {.d32 = 0 };
55336 + gintmsk_data_t gintmsk = {.d32 = 0 };
55337 +
55338 +
55339 + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
55340 +
55341 + /* Read in the device interrupt bits */
55342 + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
55343 +
55344 + while (ep_intr) {
55345 + if (ep_intr & 0x1) {
55346 + /* Get EP pointer */
55347 + ep = get_out_ep(pcd, epnum);
55348 + dwc_ep = &ep->dwc_ep;
55349 +
55350 +#ifdef VERBOSE
55351 + DWC_DEBUGPL(DBG_PCDV,
55352 + "EP%d-%s: type=%d, mps=%d\n",
55353 + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
55354 + dwc_ep->type, dwc_ep->maxpacket);
55355 +#endif
55356 + doepint.d32 =
55357 + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
55358 + /* Moved this interrupt upper due to core deffect of asserting
55359 + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
55360 + if (doepint.b.stsphsercvd) {
55361 + deptsiz0_data_t deptsiz;
55362 + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
55363 + deptsiz.d32 =
55364 + DWC_READ_REG32(&core_if->dev_if->
55365 + out_ep_regs[0]->doeptsiz);
55366 + if (core_if->snpsid >= OTG_CORE_REV_3_00a
55367 + && core_if->dma_enable
55368 + && core_if->dma_desc_enable == 0
55369 + && doepint.b.xfercompl
55370 + && deptsiz.b.xfersize == 24) {
55371 + CLEAR_OUT_EP_INTR(core_if, epnum,
55372 + xfercompl);
55373 + doepint.b.xfercompl = 0;
55374 + ep0_out_start(core_if, pcd);
55375 + }
55376 + if ((core_if->dma_desc_enable) ||
55377 + (core_if->dma_enable
55378 + && core_if->snpsid >=
55379 + OTG_CORE_REV_3_00a)) {
55380 + do_setup_in_status_phase(pcd);
55381 + }
55382 + }
55383 + /* Transfer complete */
55384 + if (doepint.b.xfercompl) {
55385 +
55386 + if (epnum == 0) {
55387 + /* Clear the bit in DOEPINTn for this interrupt */
55388 + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
55389 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
55390 + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
55391 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
55392 + doepint.d32);
55393 + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
55394 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
55395 +
55396 + if (core_if->snpsid >= OTG_CORE_REV_3_00a
55397 + && core_if->dma_enable == 0) {
55398 + doepint_data_t doepint;
55399 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55400 + out_ep_regs[0]->doepint);
55401 + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
55402 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55403 + goto exit_xfercompl;
55404 + }
55405 + }
55406 + /* In case of DDMA look at SR bit to go to the Data Stage */
55407 + if (core_if->dma_desc_enable) {
55408 + dev_dma_desc_sts_t status = {.d32 = 0};
55409 + if (pcd->ep0state == EP0_IDLE) {
55410 + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
55411 + dev_if->setup_desc_index]->status.d32;
55412 + if(pcd->data_terminated) {
55413 + pcd->data_terminated = 0;
55414 + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
55415 + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
55416 + }
55417 + if (status.b.sr) {
55418 + if (doepint.b.setup) {
55419 + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
55420 + /* Already started data stage, clear setup */
55421 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55422 + doepint.b.setup = 0;
55423 + handle_ep0(pcd);
55424 + /* Prepare for more setup packets */
55425 + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
55426 + pcd->ep0state == EP0_IN_DATA_PHASE) {
55427 + ep0_out_start(core_if, pcd);
55428 + }
55429 +
55430 + goto exit_xfercompl;
55431 + } else {
55432 + /* Prepare for more setup packets */
55433 + DWC_DEBUGPL(DBG_PCDV,
55434 + "EP0_IDLE SR=1 setup=0 new setup comes\n");
55435 + ep0_out_start(core_if, pcd);
55436 + }
55437 + }
55438 + } else {
55439 + dwc_otg_pcd_request_t *req;
55440 + dev_dma_desc_sts_t status = {.d32 = 0};
55441 + diepint_data_t diepint0;
55442 + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
55443 + in_ep_regs[0]->diepint);
55444 +
55445 + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
55446 + DWC_ERROR("EP0 is stalled/disconnected\n");
55447 + }
55448 +
55449 + /* Clear IN xfercompl if set */
55450 + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
55451 + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
55452 + DWC_WRITE_REG32(&core_if->dev_if->
55453 + in_ep_regs[0]->diepint, diepint0.d32);
55454 + }
55455 +
55456 + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
55457 + dev_if->setup_desc_index]->status.d32;
55458 +
55459 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
55460 + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
55461 + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
55462 + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
55463 + status.d32 = core_if->dev_if->
55464 + out_desc_addr->status.d32;
55465 +
55466 + if (status.b.sr) {
55467 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55468 + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
55469 + } else {
55470 + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
55471 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55472 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
55473 + pcd->ep0state == EP0_OUT_DATA_PHASE) {
55474 + /* Read arrived setup packet from req->buf */
55475 + dwc_memcpy(&pcd->setup_pkt->req,
55476 + req->buf + ep->dwc_ep.xfer_count, 8);
55477 + }
55478 + req->actual = ep->dwc_ep.xfer_count;
55479 + dwc_otg_request_done(ep, req, -ECONNRESET);
55480 + ep->dwc_ep.start_xfer_buff = 0;
55481 + ep->dwc_ep.xfer_buff = 0;
55482 + ep->dwc_ep.xfer_len = 0;
55483 + }
55484 + pcd->ep0state = EP0_IDLE;
55485 + if (doepint.b.setup) {
55486 + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
55487 + /* Data stage started, clear setup */
55488 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55489 + doepint.b.setup = 0;
55490 + handle_ep0(pcd);
55491 + /* Prepare for setup packets if ep0in was enabled*/
55492 + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
55493 + ep0_out_start(core_if, pcd);
55494 + }
55495 +
55496 + goto exit_xfercompl;
55497 + } else {
55498 + /* Prepare for more setup packets */
55499 + DWC_DEBUGPL(DBG_PCDV,
55500 + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
55501 + ep0_out_start(core_if, pcd);
55502 + }
55503 + }
55504 + }
55505 + }
55506 + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
55507 + && core_if->dma_desc_enable == 0) {
55508 + doepint_data_t doepint_temp = {.d32 = 0};
55509 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
55510 + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
55511 + out_ep_regs[ep->dwc_ep.num]->doepint);
55512 + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
55513 + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
55514 + if (pcd->ep0state == EP0_IDLE) {
55515 + if (doepint_temp.b.sr) {
55516 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55517 + }
55518 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55519 + out_ep_regs[0]->doepint);
55520 + if (doeptsize0.b.supcnt == 3) {
55521 + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
55522 + ep->dwc_ep.stp_rollover = 1;
55523 + }
55524 + if (doepint.b.setup) {
55525 +retry:
55526 + /* Already started data stage, clear setup */
55527 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55528 + doepint.b.setup = 0;
55529 + handle_ep0(pcd);
55530 + ep->dwc_ep.stp_rollover = 0;
55531 + /* Prepare for more setup packets */
55532 + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
55533 + pcd->ep0state == EP0_IN_DATA_PHASE) {
55534 + ep0_out_start(core_if, pcd);
55535 + }
55536 + goto exit_xfercompl;
55537 + } else {
55538 + /* Prepare for more setup packets */
55539 + DWC_DEBUGPL(DBG_ANY,
55540 + "EP0_IDLE SR=1 setup=0 new setup comes\n");
55541 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55542 + out_ep_regs[0]->doepint);
55543 + if(doepint.b.setup)
55544 + goto retry;
55545 + ep0_out_start(core_if, pcd);
55546 + }
55547 + } else {
55548 + dwc_otg_pcd_request_t *req;
55549 + diepint_data_t diepint0 = {.d32 = 0};
55550 + doepint_data_t doepint_temp = {.d32 = 0};
55551 + depctl_data_t diepctl0;
55552 + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
55553 + in_ep_regs[0]->diepint);
55554 + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
55555 + in_ep_regs[0]->diepctl);
55556 +
55557 + if (pcd->ep0state == EP0_IN_DATA_PHASE
55558 + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
55559 + if (diepint0.b.xfercompl) {
55560 + DWC_WRITE_REG32(&core_if->dev_if->
55561 + in_ep_regs[0]->diepint, diepint0.d32);
55562 + }
55563 + if (diepctl0.b.epena) {
55564 + diepint_data_t diepint = {.d32 = 0};
55565 + diepctl0.b.snak = 1;
55566 + DWC_WRITE_REG32(&core_if->dev_if->
55567 + in_ep_regs[0]->diepctl, diepctl0.d32);
55568 + do {
55569 + dwc_udelay(10);
55570 + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55571 + in_ep_regs[0]->diepint);
55572 + } while (!diepint.b.inepnakeff);
55573 + diepint.b.inepnakeff = 1;
55574 + DWC_WRITE_REG32(&core_if->dev_if->
55575 + in_ep_regs[0]->diepint, diepint.d32);
55576 + diepctl0.d32 = 0;
55577 + diepctl0.b.epdis = 1;
55578 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
55579 + diepctl0.d32);
55580 + do {
55581 + dwc_udelay(10);
55582 + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55583 + in_ep_regs[0]->diepint);
55584 + } while (!diepint.b.epdisabled);
55585 + diepint.b.epdisabled = 1;
55586 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
55587 + diepint.d32);
55588 + }
55589 + }
55590 + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
55591 + out_ep_regs[ep->dwc_ep.num]->doepint);
55592 + if (doepint_temp.b.sr) {
55593 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55594 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55595 + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
55596 + } else {
55597 + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
55598 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55599 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
55600 + pcd->ep0state == EP0_OUT_DATA_PHASE) {
55601 + /* Read arrived setup packet from req->buf */
55602 + dwc_memcpy(&pcd->setup_pkt->req,
55603 + req->buf + ep->dwc_ep.xfer_count, 8);
55604 + }
55605 + req->actual = ep->dwc_ep.xfer_count;
55606 + dwc_otg_request_done(ep, req, -ECONNRESET);
55607 + ep->dwc_ep.start_xfer_buff = 0;
55608 + ep->dwc_ep.xfer_buff = 0;
55609 + ep->dwc_ep.xfer_len = 0;
55610 + }
55611 + pcd->ep0state = EP0_IDLE;
55612 + if (doepint.b.setup) {
55613 + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
55614 + /* Data stage started, clear setup */
55615 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55616 + doepint.b.setup = 0;
55617 + handle_ep0(pcd);
55618 + /* Prepare for setup packets if ep0in was enabled*/
55619 + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
55620 + ep0_out_start(core_if, pcd);
55621 + }
55622 + goto exit_xfercompl;
55623 + } else {
55624 + /* Prepare for more setup packets */
55625 + DWC_DEBUGPL(DBG_PCDV,
55626 + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
55627 + ep0_out_start(core_if, pcd);
55628 + }
55629 + }
55630 + }
55631 + }
55632 + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
55633 + handle_ep0(pcd);
55634 +exit_xfercompl:
55635 + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
55636 + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
55637 + } else {
55638 + if (core_if->dma_desc_enable == 0
55639 + || pcd->ep0state != EP0_IDLE)
55640 + handle_ep0(pcd);
55641 + }
55642 +#ifdef DWC_EN_ISOC
55643 + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55644 + if (doepint.b.pktdrpsts == 0) {
55645 + /* Clear the bit in DOEPINTn for this interrupt */
55646 + CLEAR_OUT_EP_INTR(core_if,
55647 + epnum,
55648 + xfercompl);
55649 + complete_iso_ep(pcd, ep);
55650 + } else {
55651 +
55652 + doepint_data_t doepint = {.d32 = 0 };
55653 + doepint.b.xfercompl = 1;
55654 + doepint.b.pktdrpsts = 1;
55655 + DWC_WRITE_REG32
55656 + (&core_if->dev_if->out_ep_regs
55657 + [epnum]->doepint,
55658 + doepint.d32);
55659 + if (handle_iso_out_pkt_dropped
55660 + (core_if, dwc_ep)) {
55661 + complete_iso_ep(pcd,
55662 + ep);
55663 + }
55664 + }
55665 +#endif /* DWC_EN_ISOC */
55666 +#ifdef DWC_UTE_PER_IO
55667 + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55668 + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
55669 + if (!ep->stopped)
55670 + complete_xiso_ep(ep);
55671 +#endif /* DWC_UTE_PER_IO */
55672 + } else {
55673 + /* Clear the bit in DOEPINTn for this interrupt */
55674 + CLEAR_OUT_EP_INTR(core_if, epnum,
55675 + xfercompl);
55676 +
55677 + if (core_if->core_params->dev_out_nak) {
55678 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
55679 + pcd->core_if->ep_xfer_info[epnum].state = 0;
55680 +#ifdef DEBUG
55681 + print_memory_payload(pcd, dwc_ep);
55682 +#endif
55683 + }
55684 + complete_ep(ep);
55685 + }
55686 +
55687 + }
55688 +
55689 + /* Endpoint disable */
55690 + if (doepint.b.epdisabled) {
55691 +
55692 + /* Clear the bit in DOEPINTn for this interrupt */
55693 + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
55694 + if (core_if->core_params->dev_out_nak) {
55695 +#ifdef DEBUG
55696 + print_memory_payload(pcd, dwc_ep);
55697 +#endif
55698 + /* In case of timeout condition */
55699 + if (core_if->ep_xfer_info[epnum].state == 2) {
55700 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
55701 + dev_global_regs->dctl);
55702 + dctl.b.cgoutnak = 1;
55703 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
55704 + dctl.d32);
55705 + /* Unmask goutnakeff interrupt which was masked
55706 + * during handle nak out interrupt */
55707 + gintmsk.b.goutnakeff = 1;
55708 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
55709 + 0, gintmsk.d32);
55710 +
55711 + complete_ep(ep);
55712 + }
55713 + }
55714 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
55715 + {
55716 + dctl_data_t dctl;
55717 + gintmsk_data_t intr_mask = {.d32 = 0};
55718 + dwc_otg_pcd_request_t *req = 0;
55719 +
55720 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
55721 + dev_global_regs->dctl);
55722 + dctl.b.cgoutnak = 1;
55723 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
55724 + dctl.d32);
55725 +
55726 + intr_mask.d32 = 0;
55727 + intr_mask.b.incomplisoout = 1;
55728 +
55729 + /* Get any pending requests */
55730 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55731 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55732 + if (!req) {
55733 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
55734 + } else {
55735 + dwc_otg_request_done(ep, req, 0);
55736 + start_next_request(ep);
55737 + }
55738 + } else {
55739 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
55740 + }
55741 + }
55742 + }
55743 + /* AHB Error */
55744 + if (doepint.b.ahberr) {
55745 + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
55746 + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
55747 + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
55748 + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
55749 + }
55750 + /* Setup Phase Done (contorl EPs) */
55751 + if (doepint.b.setup) {
55752 +#ifdef DEBUG_EP0
55753 + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
55754 +#endif
55755 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55756 +
55757 + handle_ep0(pcd);
55758 + }
55759 +
55760 + /** OUT EP BNA Intr */
55761 + if (doepint.b.bna) {
55762 + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
55763 + if (core_if->dma_desc_enable) {
55764 +#ifdef DWC_EN_ISOC
55765 + if (dwc_ep->type ==
55766 + DWC_OTG_EP_TYPE_ISOC) {
55767 + /*
55768 + * This checking is performed to prevent first "false" BNA
55769 + * handling occuring right after reconnect
55770 + */
55771 + if (dwc_ep->next_frame !=
55772 + 0xffffffff)
55773 + dwc_otg_pcd_handle_iso_bna(ep);
55774 + } else
55775 +#endif /* DWC_EN_ISOC */
55776 + {
55777 + dwc_otg_pcd_handle_noniso_bna(ep);
55778 + }
55779 + }
55780 + }
55781 + /* Babble Interrupt */
55782 + if (doepint.b.babble) {
55783 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
55784 + epnum);
55785 + handle_out_ep_babble_intr(pcd, epnum);
55786 +
55787 + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
55788 + }
55789 + if (doepint.b.outtknepdis) {
55790 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
55791 + disabled\n",epnum);
55792 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
55793 + doepmsk_data_t doepmsk = {.d32 = 0};
55794 + ep->dwc_ep.frame_num = core_if->frame_num;
55795 + if (ep->dwc_ep.bInterval > 1) {
55796 + depctl_data_t depctl;
55797 + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
55798 + out_ep_regs[epnum]->doepctl);
55799 + if (ep->dwc_ep.frame_num & 0x1) {
55800 + depctl.b.setd1pid = 1;
55801 + depctl.b.setd0pid = 0;
55802 + } else {
55803 + depctl.b.setd0pid = 1;
55804 + depctl.b.setd1pid = 0;
55805 + }
55806 + DWC_WRITE_REG32(&core_if->dev_if->
55807 + out_ep_regs[epnum]->doepctl, depctl.d32);
55808 + }
55809 + start_next_request(ep);
55810 + doepmsk.b.outtknepdis = 1;
55811 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55812 + doepmsk.d32, 0);
55813 + }
55814 + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
55815 + }
55816 +
55817 + /* NAK Interrutp */
55818 + if (doepint.b.nak) {
55819 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
55820 + handle_out_ep_nak_intr(pcd, epnum);
55821 +
55822 + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
55823 + }
55824 + /* NYET Interrutp */
55825 + if (doepint.b.nyet) {
55826 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
55827 + handle_out_ep_nyet_intr(pcd, epnum);
55828 +
55829 + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
55830 + }
55831 + }
55832 +
55833 + epnum++;
55834 + ep_intr >>= 1;
55835 + }
55836 +
55837 + return 1;
55838 +
55839 +#undef CLEAR_OUT_EP_INTR
55840 +}
55841 +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
55842 +{
55843 + int retval = 0;
55844 + if(!frm_overrun && curr_fr >= trgt_fr)
55845 + retval = 1;
55846 + else if (frm_overrun
55847 + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
55848 + retval = 1;
55849 + return retval;
55850 +}
55851 +/**
55852 + * Incomplete ISO IN Transfer Interrupt.
55853 + * This interrupt indicates one of the following conditions occurred
55854 + * while transmitting an ISOC transaction.
55855 + * - Corrupted IN Token for ISOC EP.
55856 + * - Packet not complete in FIFO.
55857 + * The follow actions will be taken:
55858 + * -# Determine the EP
55859 + * -# Set incomplete flag in dwc_ep structure
55860 + * -# Disable EP; when "Endpoint Disabled" interrupt is received
55861 + * Flush FIFO
55862 + */
55863 +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
55864 +{
55865 + gintsts_data_t gintsts;
55866 +
55867 +#ifdef DWC_EN_ISOC
55868 + dwc_otg_dev_if_t *dev_if;
55869 + deptsiz_data_t deptsiz = {.d32 = 0 };
55870 + depctl_data_t depctl = {.d32 = 0 };
55871 + dsts_data_t dsts = {.d32 = 0 };
55872 + dwc_ep_t *dwc_ep;
55873 + int i;
55874 +
55875 + dev_if = GET_CORE_IF(pcd)->dev_if;
55876 +
55877 + for (i = 1; i <= dev_if->num_in_eps; ++i) {
55878 + dwc_ep = &pcd->in_ep[i].dwc_ep;
55879 + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55880 + deptsiz.d32 =
55881 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
55882 + depctl.d32 =
55883 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55884 +
55885 + if (depctl.b.epdis && deptsiz.d32) {
55886 + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
55887 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
55888 + dwc_ep->cur_pkt = 0;
55889 + dwc_ep->proc_buf_num =
55890 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
55891 +
55892 + if (dwc_ep->proc_buf_num) {
55893 + dwc_ep->cur_pkt_addr =
55894 + dwc_ep->xfer_buff1;
55895 + dwc_ep->cur_pkt_dma_addr =
55896 + dwc_ep->dma_addr1;
55897 + } else {
55898 + dwc_ep->cur_pkt_addr =
55899 + dwc_ep->xfer_buff0;
55900 + dwc_ep->cur_pkt_dma_addr =
55901 + dwc_ep->dma_addr0;
55902 + }
55903 +
55904 + }
55905 +
55906 + dsts.d32 =
55907 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
55908 + dev_global_regs->dsts);
55909 + dwc_ep->next_frame = dsts.b.soffn;
55910 +
55911 + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
55912 + (pcd),
55913 + dwc_ep);
55914 + }
55915 + }
55916 + }
55917 +
55918 +#else
55919 + depctl_data_t depctl = {.d32 = 0 };
55920 + dwc_ep_t *dwc_ep;
55921 + dwc_otg_dev_if_t *dev_if;
55922 + int i;
55923 + dev_if = GET_CORE_IF(pcd)->dev_if;
55924 +
55925 + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
55926 +
55927 + for (i = 1; i <= dev_if->num_in_eps; ++i) {
55928 + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
55929 + depctl.d32 =
55930 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55931 + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55932 + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
55933 + dwc_ep->frm_overrun))
55934 + {
55935 + depctl.d32 =
55936 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55937 + depctl.b.snak = 1;
55938 + depctl.b.epdis = 1;
55939 + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
55940 + }
55941 + }
55942 + }
55943 +
55944 + /*intr_mask.b.incomplisoin = 1;
55945 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
55946 + intr_mask.d32, 0); */
55947 +#endif //DWC_EN_ISOC
55948 +
55949 + /* Clear interrupt */
55950 + gintsts.d32 = 0;
55951 + gintsts.b.incomplisoin = 1;
55952 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
55953 + gintsts.d32);
55954 +
55955 + return 1;
55956 +}
55957 +
55958 +/**
55959 + * Incomplete ISO OUT Transfer Interrupt.
55960 + *
55961 + * This interrupt indicates that the core has dropped an ISO OUT
55962 + * packet. The following conditions can be the cause:
55963 + * - FIFO Full, the entire packet would not fit in the FIFO.
55964 + * - CRC Error
55965 + * - Corrupted Token
55966 + * The follow actions will be taken:
55967 + * -# Determine the EP
55968 + * -# Set incomplete flag in dwc_ep structure
55969 + * -# Read any data from the FIFO
55970 + * -# Disable EP. When "Endpoint Disabled" interrupt is received
55971 + * re-enable EP.
55972 + */
55973 +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
55974 +{
55975 +
55976 + gintsts_data_t gintsts;
55977 +
55978 +#ifdef DWC_EN_ISOC
55979 + dwc_otg_dev_if_t *dev_if;
55980 + deptsiz_data_t deptsiz = {.d32 = 0 };
55981 + depctl_data_t depctl = {.d32 = 0 };
55982 + dsts_data_t dsts = {.d32 = 0 };
55983 + dwc_ep_t *dwc_ep;
55984 + int i;
55985 +
55986 + dev_if = GET_CORE_IF(pcd)->dev_if;
55987 +
55988 + for (i = 1; i <= dev_if->num_out_eps; ++i) {
55989 + dwc_ep = &pcd->in_ep[i].dwc_ep;
55990 + if (pcd->out_ep[i].dwc_ep.active &&
55991 + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
55992 + deptsiz.d32 =
55993 + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
55994 + depctl.d32 =
55995 + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
55996 +
55997 + if (depctl.b.epdis && deptsiz.d32) {
55998 + set_current_pkt_info(GET_CORE_IF(pcd),
55999 + &pcd->out_ep[i].dwc_ep);
56000 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
56001 + dwc_ep->cur_pkt = 0;
56002 + dwc_ep->proc_buf_num =
56003 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
56004 +
56005 + if (dwc_ep->proc_buf_num) {
56006 + dwc_ep->cur_pkt_addr =
56007 + dwc_ep->xfer_buff1;
56008 + dwc_ep->cur_pkt_dma_addr =
56009 + dwc_ep->dma_addr1;
56010 + } else {
56011 + dwc_ep->cur_pkt_addr =
56012 + dwc_ep->xfer_buff0;
56013 + dwc_ep->cur_pkt_dma_addr =
56014 + dwc_ep->dma_addr0;
56015 + }
56016 +
56017 + }
56018 +
56019 + dsts.d32 =
56020 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
56021 + dev_global_regs->dsts);
56022 + dwc_ep->next_frame = dsts.b.soffn;
56023 +
56024 + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
56025 + (pcd),
56026 + dwc_ep);
56027 + }
56028 + }
56029 + }
56030 +#else
56031 + /** @todo implement ISR */
56032 + gintmsk_data_t intr_mask = {.d32 = 0 };
56033 + dwc_otg_core_if_t *core_if;
56034 + deptsiz_data_t deptsiz = {.d32 = 0 };
56035 + depctl_data_t depctl = {.d32 = 0 };
56036 + dctl_data_t dctl = {.d32 = 0 };
56037 + dwc_ep_t *dwc_ep = NULL;
56038 + int i;
56039 + core_if = GET_CORE_IF(pcd);
56040 +
56041 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
56042 + dwc_ep = &pcd->out_ep[i].dwc_ep;
56043 + depctl.d32 =
56044 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
56045 + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
56046 + core_if->dev_if->isoc_ep = dwc_ep;
56047 + deptsiz.d32 =
56048 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
56049 + break;
56050 + }
56051 + }
56052 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
56053 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
56054 + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
56055 +
56056 + if (!intr_mask.b.goutnakeff) {
56057 + /* Unmask it */
56058 + intr_mask.b.goutnakeff = 1;
56059 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
56060 + }
56061 + if (!gintsts.b.goutnakeff) {
56062 + dctl.b.sgoutnak = 1;
56063 + }
56064 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
56065 +
56066 + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
56067 + if (depctl.b.epena) {
56068 + depctl.b.epdis = 1;
56069 + depctl.b.snak = 1;
56070 + }
56071 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
56072 +
56073 + intr_mask.d32 = 0;
56074 + intr_mask.b.incomplisoout = 1;
56075 +
56076 +#endif /* DWC_EN_ISOC */
56077 +
56078 + /* Clear interrupt */
56079 + gintsts.d32 = 0;
56080 + gintsts.b.incomplisoout = 1;
56081 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56082 + gintsts.d32);
56083 +
56084 + return 1;
56085 +}
56086 +
56087 +/**
56088 + * This function handles the Global IN NAK Effective interrupt.
56089 + *
56090 + */
56091 +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
56092 +{
56093 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
56094 + depctl_data_t diepctl = {.d32 = 0 };
56095 + gintmsk_data_t intr_mask = {.d32 = 0 };
56096 + gintsts_data_t gintsts;
56097 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
56098 + int i;
56099 +
56100 + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
56101 +
56102 + /* Disable all active IN EPs */
56103 + for (i = 0; i <= dev_if->num_in_eps; i++) {
56104 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56105 + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
56106 + if (core_if->start_predict > 0)
56107 + core_if->start_predict++;
56108 + diepctl.b.epdis = 1;
56109 + diepctl.b.snak = 1;
56110 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
56111 + }
56112 + }
56113 +
56114 +
56115 + /* Disable the Global IN NAK Effective Interrupt */
56116 + intr_mask.b.ginnakeff = 1;
56117 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56118 + intr_mask.d32, 0);
56119 +
56120 + /* Clear interrupt */
56121 + gintsts.d32 = 0;
56122 + gintsts.b.ginnakeff = 1;
56123 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56124 + gintsts.d32);
56125 +
56126 + return 1;
56127 +}
56128 +
56129 +/**
56130 + * OUT NAK Effective.
56131 + *
56132 + */
56133 +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
56134 +{
56135 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
56136 + gintmsk_data_t intr_mask = {.d32 = 0 };
56137 + gintsts_data_t gintsts;
56138 + depctl_data_t doepctl;
56139 + int i;
56140 +
56141 + /* Disable the Global OUT NAK Effective Interrupt */
56142 + intr_mask.b.goutnakeff = 1;
56143 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56144 + intr_mask.d32, 0);
56145 +
56146 + /* If DEV OUT NAK enabled*/
56147 + if (pcd->core_if->core_params->dev_out_nak) {
56148 + /* Run over all out endpoints to determine the ep number on
56149 + * which the timeout has happened
56150 + */
56151 + for (i = 0; i <= dev_if->num_out_eps; i++) {
56152 + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
56153 + break;
56154 + }
56155 + if (i > dev_if->num_out_eps) {
56156 + dctl_data_t dctl;
56157 + dctl.d32 =
56158 + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
56159 + dctl.b.cgoutnak = 1;
56160 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
56161 + dctl.d32);
56162 + goto out;
56163 + }
56164 +
56165 + /* Disable the endpoint */
56166 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
56167 + if (doepctl.b.epena) {
56168 + doepctl.b.epdis = 1;
56169 + doepctl.b.snak = 1;
56170 + }
56171 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
56172 + return 1;
56173 + }
56174 + /* We come here from Incomplete ISO OUT handler */
56175 + if (dev_if->isoc_ep) {
56176 + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
56177 + uint32_t epnum = dwc_ep->num;
56178 + doepint_data_t doepint;
56179 + doepint.d32 =
56180 + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
56181 + dev_if->isoc_ep = NULL;
56182 + doepctl.d32 =
56183 + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
56184 + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
56185 + if (doepctl.b.epena) {
56186 + doepctl.b.epdis = 1;
56187 + doepctl.b.snak = 1;
56188 + }
56189 + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
56190 + doepctl.d32);
56191 + return 1;
56192 + } else
56193 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
56194 + "Global OUT NAK Effective\n");
56195 +
56196 +out:
56197 + /* Clear interrupt */
56198 + gintsts.d32 = 0;
56199 + gintsts.b.goutnakeff = 1;
56200 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56201 + gintsts.d32);
56202 +
56203 + return 1;
56204 +}
56205 +
56206 +/**
56207 + * PCD interrupt handler.
56208 + *
56209 + * The PCD handles the device interrupts. Many conditions can cause a
56210 + * device interrupt. When an interrupt occurs, the device interrupt
56211 + * service routine determines the cause of the interrupt and
56212 + * dispatches handling to the appropriate function. These interrupt
56213 + * handling functions are described below.
56214 + *
56215 + * All interrupt registers are processed from LSB to MSB.
56216 + *
56217 + */
56218 +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
56219 +{
56220 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
56221 +#ifdef VERBOSE
56222 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
56223 +#endif
56224 + gintsts_data_t gintr_status;
56225 + int32_t retval = 0;
56226 +
56227 + /* Exit from ISR if core is hibernated */
56228 + if (core_if->hibernation_suspend == 1) {
56229 + return retval;
56230 + }
56231 +#ifdef VERBOSE
56232 + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
56233 + __func__,
56234 + DWC_READ_REG32(&global_regs->gintsts),
56235 + DWC_READ_REG32(&global_regs->gintmsk));
56236 +#endif
56237 +
56238 + if (dwc_otg_is_device_mode(core_if)) {
56239 + DWC_SPINLOCK(pcd->lock);
56240 +#ifdef VERBOSE
56241 + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
56242 + __func__,
56243 + DWC_READ_REG32(&global_regs->gintsts),
56244 + DWC_READ_REG32(&global_regs->gintmsk));
56245 +#endif
56246 +
56247 + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
56248 +
56249 + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
56250 + __func__, gintr_status.d32);
56251 +
56252 + if (gintr_status.b.sofintr) {
56253 + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
56254 + }
56255 + if (gintr_status.b.rxstsqlvl) {
56256 + retval |=
56257 + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
56258 + }
56259 + if (gintr_status.b.nptxfempty) {
56260 + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
56261 + }
56262 + if (gintr_status.b.goutnakeff) {
56263 + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
56264 + }
56265 + if (gintr_status.b.i2cintr) {
56266 + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
56267 + }
56268 + if (gintr_status.b.erlysuspend) {
56269 + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
56270 + }
56271 + if (gintr_status.b.usbreset) {
56272 + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
56273 + }
56274 + if (gintr_status.b.enumdone) {
56275 + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
56276 + }
56277 + if (gintr_status.b.isooutdrop) {
56278 + retval |=
56279 + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
56280 + (pcd);
56281 + }
56282 + if (gintr_status.b.eopframe) {
56283 + retval |=
56284 + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
56285 + }
56286 + if (gintr_status.b.inepint) {
56287 + if (!core_if->multiproc_int_enable) {
56288 + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
56289 + }
56290 + }
56291 + if (gintr_status.b.outepintr) {
56292 + if (!core_if->multiproc_int_enable) {
56293 + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
56294 + }
56295 + }
56296 + if (gintr_status.b.epmismatch) {
56297 + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
56298 + }
56299 + if (gintr_status.b.fetsusp) {
56300 + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
56301 + }
56302 + if (gintr_status.b.ginnakeff) {
56303 + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
56304 + }
56305 + if (gintr_status.b.incomplisoin) {
56306 + retval |=
56307 + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
56308 + }
56309 + if (gintr_status.b.incomplisoout) {
56310 + retval |=
56311 + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
56312 + }
56313 +
56314 + /* In MPI mode Device Endpoints interrupts are asserted
56315 + * without setting outepintr and inepint bits set, so these
56316 + * Interrupt handlers are called without checking these bit-fields
56317 + */
56318 + if (core_if->multiproc_int_enable) {
56319 + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
56320 + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
56321 + }
56322 +#ifdef VERBOSE
56323 + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
56324 + DWC_READ_REG32(&global_regs->gintsts));
56325 +#endif
56326 + DWC_SPINUNLOCK(pcd->lock);
56327 + }
56328 + return retval;
56329 +}
56330 +
56331 +#endif /* DWC_HOST_ONLY */
56332 --- /dev/null
56333 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
56334 @@ -0,0 +1,1280 @@
56335 + /* ==========================================================================
56336 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
56337 + * $Revision: #21 $
56338 + * $Date: 2012/08/10 $
56339 + * $Change: 2047372 $
56340 + *
56341 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
56342 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
56343 + * otherwise expressly agreed to in writing between Synopsys and you.
56344 + *
56345 + * The Software IS NOT an item of Licensed Software or Licensed Product under
56346 + * any End User Software License Agreement or Agreement for Licensed Product
56347 + * with Synopsys or any supplement thereto. You are permitted to use and
56348 + * redistribute this Software in source and binary forms, with or without
56349 + * modification, provided that redistributions of source code must retain this
56350 + * notice. You may not view, use, disclose, copy or distribute this file or
56351 + * any information contained herein except pursuant to this license grant from
56352 + * Synopsys. If you do not agree with this notice, including the disclaimer
56353 + * below, then you are not authorized to use the Software.
56354 + *
56355 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
56356 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56357 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56358 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
56359 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56360 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56361 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56362 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56363 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56364 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
56365 + * DAMAGE.
56366 + * ========================================================================== */
56367 +#ifndef DWC_HOST_ONLY
56368 +
56369 +/** @file
56370 + * This file implements the Peripheral Controller Driver.
56371 + *
56372 + * The Peripheral Controller Driver (PCD) is responsible for
56373 + * translating requests from the Function Driver into the appropriate
56374 + * actions on the DWC_otg controller. It isolates the Function Driver
56375 + * from the specifics of the controller by providing an API to the
56376 + * Function Driver.
56377 + *
56378 + * The Peripheral Controller Driver for Linux will implement the
56379 + * Gadget API, so that the existing Gadget drivers can be used.
56380 + * (Gadget Driver is the Linux terminology for a Function Driver.)
56381 + *
56382 + * The Linux Gadget API is defined in the header file
56383 + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
56384 + * defined in the structure <code>usb_ep_ops</code> and the USB
56385 + * Controller API is defined in the structure
56386 + * <code>usb_gadget_ops</code>.
56387 + *
56388 + */
56389 +
56390 +#include "dwc_otg_os_dep.h"
56391 +#include "dwc_otg_pcd_if.h"
56392 +#include "dwc_otg_pcd.h"
56393 +#include "dwc_otg_driver.h"
56394 +#include "dwc_otg_dbg.h"
56395 +
56396 +extern bool fiq_enable;
56397 +
56398 +static struct gadget_wrapper {
56399 + dwc_otg_pcd_t *pcd;
56400 +
56401 + struct usb_gadget gadget;
56402 + struct usb_gadget_driver *driver;
56403 +
56404 + struct usb_ep ep0;
56405 + struct usb_ep in_ep[16];
56406 + struct usb_ep out_ep[16];
56407 +
56408 +} *gadget_wrapper;
56409 +
56410 +/* Display the contents of the buffer */
56411 +extern void dump_msg(const u8 * buf, unsigned int length);
56412 +/**
56413 + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
56414 + * if the endpoint is not found
56415 + */
56416 +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
56417 +{
56418 + int i;
56419 + if (pcd->ep0.priv == handle) {
56420 + return &pcd->ep0;
56421 + }
56422 +
56423 + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
56424 + if (pcd->in_ep[i].priv == handle)
56425 + return &pcd->in_ep[i];
56426 + if (pcd->out_ep[i].priv == handle)
56427 + return &pcd->out_ep[i];
56428 + }
56429 +
56430 + return NULL;
56431 +}
56432 +
56433 +/* USB Endpoint Operations */
56434 +/*
56435 + * The following sections briefly describe the behavior of the Gadget
56436 + * API endpoint operations implemented in the DWC_otg driver
56437 + * software. Detailed descriptions of the generic behavior of each of
56438 + * these functions can be found in the Linux header file
56439 + * include/linux/usb_gadget.h.
56440 + *
56441 + * The Gadget API provides wrapper functions for each of the function
56442 + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
56443 + * function, which then calls the underlying PCD function. The
56444 + * following sections are named according to the wrapper
56445 + * functions. Within each section, the corresponding DWC_otg PCD
56446 + * function name is specified.
56447 + *
56448 + */
56449 +
56450 +/**
56451 + * This function is called by the Gadget Driver for each EP to be
56452 + * configured for the current configuration (SET_CONFIGURATION).
56453 + *
56454 + * This function initializes the dwc_otg_ep_t data structure, and then
56455 + * calls dwc_otg_ep_activate.
56456 + */
56457 +static int ep_enable(struct usb_ep *usb_ep,
56458 + const struct usb_endpoint_descriptor *ep_desc)
56459 +{
56460 + int retval;
56461 +
56462 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
56463 +
56464 + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
56465 + DWC_WARN("%s, bad ep or descriptor\n", __func__);
56466 + return -EINVAL;
56467 + }
56468 + if (usb_ep == &gadget_wrapper->ep0) {
56469 + DWC_WARN("%s, bad ep(0)\n", __func__);
56470 + return -EINVAL;
56471 + }
56472 +
56473 + /* Check FIFO size? */
56474 + if (!ep_desc->wMaxPacketSize) {
56475 + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
56476 + return -ERANGE;
56477 + }
56478 +
56479 + if (!gadget_wrapper->driver ||
56480 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56481 + DWC_WARN("%s, bogus device state\n", __func__);
56482 + return -ESHUTDOWN;
56483 + }
56484 +
56485 + /* Delete after check - MAS */
56486 +#if 0
56487 + nat = (uint32_t) ep_desc->wMaxPacketSize;
56488 + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
56489 + nat = (nat >> 11) & 0x03;
56490 + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
56491 +#endif
56492 + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
56493 + (const uint8_t *)ep_desc,
56494 + (void *)usb_ep);
56495 + if (retval) {
56496 + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
56497 + return -EINVAL;
56498 + }
56499 +
56500 + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
56501 +
56502 + return 0;
56503 +}
56504 +
56505 +/**
56506 + * This function is called when an EP is disabled due to disconnect or
56507 + * change in configuration. Any pending requests will terminate with a
56508 + * status of -ESHUTDOWN.
56509 + *
56510 + * This function modifies the dwc_otg_ep_t data structure for this EP,
56511 + * and then calls dwc_otg_ep_deactivate.
56512 + */
56513 +static int ep_disable(struct usb_ep *usb_ep)
56514 +{
56515 + int retval;
56516 +
56517 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
56518 + if (!usb_ep) {
56519 + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
56520 + usb_ep ? usb_ep->name : NULL);
56521 + return -EINVAL;
56522 + }
56523 +
56524 + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
56525 + if (retval) {
56526 + retval = -EINVAL;
56527 + }
56528 +
56529 + return retval;
56530 +}
56531 +
56532 +/**
56533 + * This function allocates a request object to use with the specified
56534 + * endpoint.
56535 + *
56536 + * @param ep The endpoint to be used with with the request
56537 + * @param gfp_flags the GFP_* flags to use.
56538 + */
56539 +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
56540 + gfp_t gfp_flags)
56541 +{
56542 + struct usb_request *usb_req;
56543 +
56544 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
56545 + if (0 == ep) {
56546 + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
56547 + return 0;
56548 + }
56549 + usb_req = kzalloc(sizeof(*usb_req), gfp_flags);
56550 + if (0 == usb_req) {
56551 + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
56552 + return 0;
56553 + }
56554 + usb_req->dma = DWC_DMA_ADDR_INVALID;
56555 +
56556 + return usb_req;
56557 +}
56558 +
56559 +/**
56560 + * This function frees a request object.
56561 + *
56562 + * @param ep The endpoint associated with the request
56563 + * @param req The request being freed
56564 + */
56565 +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
56566 +{
56567 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
56568 +
56569 + if (0 == ep || 0 == req) {
56570 + DWC_WARN("%s() %s\n", __func__,
56571 + "Invalid ep or req argument!\n");
56572 + return;
56573 + }
56574 +
56575 + kfree(req);
56576 +}
56577 +
56578 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56579 +/**
56580 + * This function allocates an I/O buffer to be used for a transfer
56581 + * to/from the specified endpoint.
56582 + *
56583 + * @param usb_ep The endpoint to be used with with the request
56584 + * @param bytes The desired number of bytes for the buffer
56585 + * @param dma Pointer to the buffer's DMA address; must be valid
56586 + * @param gfp_flags the GFP_* flags to use.
56587 + * @return address of a new buffer or null is buffer could not be allocated.
56588 + */
56589 +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
56590 + dma_addr_t * dma, gfp_t gfp_flags)
56591 +{
56592 + void *buf;
56593 + dwc_otg_pcd_t *pcd = 0;
56594 +
56595 + pcd = gadget_wrapper->pcd;
56596 +
56597 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
56598 + dma, gfp_flags);
56599 +
56600 + /* Check dword alignment */
56601 + if ((bytes & 0x3UL) != 0) {
56602 + DWC_WARN("%s() Buffer size is not a multiple of"
56603 + "DWORD size (%d)", __func__, bytes);
56604 + }
56605 +
56606 + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
56607 + WARN_ON(!buf);
56608 +
56609 + /* Check dword alignment */
56610 + if (((int)buf & 0x3UL) != 0) {
56611 + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
56612 + __func__, buf);
56613 + }
56614 +
56615 + return buf;
56616 +}
56617 +
56618 +/**
56619 + * This function frees an I/O buffer that was allocated by alloc_buffer.
56620 + *
56621 + * @param usb_ep the endpoint associated with the buffer
56622 + * @param buf address of the buffer
56623 + * @param dma The buffer's DMA address
56624 + * @param bytes The number of bytes of the buffer
56625 + */
56626 +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
56627 + dma_addr_t dma, unsigned bytes)
56628 +{
56629 + dwc_otg_pcd_t *pcd = 0;
56630 +
56631 + pcd = gadget_wrapper->pcd;
56632 +
56633 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
56634 +
56635 + dma_free_coherent(NULL, bytes, buf, dma);
56636 +}
56637 +#endif
56638 +
56639 +/**
56640 + * This function is used to submit an I/O Request to an EP.
56641 + *
56642 + * - When the request completes the request's completion callback
56643 + * is called to return the request to the driver.
56644 + * - An EP, except control EPs, may have multiple requests
56645 + * pending.
56646 + * - Once submitted the request cannot be examined or modified.
56647 + * - Each request is turned into one or more packets.
56648 + * - A BULK EP can queue any amount of data; the transfer is
56649 + * packetized.
56650 + * - Zero length Packets are specified with the request 'zero'
56651 + * flag.
56652 + */
56653 +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
56654 + gfp_t gfp_flags)
56655 +{
56656 + dwc_otg_pcd_t *pcd;
56657 + struct dwc_otg_pcd_ep *ep = NULL;
56658 + int retval = 0, is_isoc_ep = 0;
56659 + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
56660 +
56661 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
56662 + __func__, usb_ep, usb_req, gfp_flags);
56663 +
56664 + if (!usb_req || !usb_req->complete || !usb_req->buf) {
56665 + DWC_WARN("bad params\n");
56666 + return -EINVAL;
56667 + }
56668 +
56669 + if (!usb_ep) {
56670 + DWC_WARN("bad ep\n");
56671 + return -EINVAL;
56672 + }
56673 +
56674 + pcd = gadget_wrapper->pcd;
56675 + if (!gadget_wrapper->driver ||
56676 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56677 + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
56678 + gadget_wrapper->gadget.speed);
56679 + DWC_WARN("bogus device state\n");
56680 + return -ESHUTDOWN;
56681 + }
56682 +
56683 + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
56684 + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
56685 +
56686 + usb_req->status = -EINPROGRESS;
56687 + usb_req->actual = 0;
56688 +
56689 + ep = ep_from_handle(pcd, usb_ep);
56690 + if (ep == NULL)
56691 + is_isoc_ep = 0;
56692 + else
56693 + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
56694 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56695 + dma_addr = usb_req->dma;
56696 +#else
56697 + if (GET_CORE_IF(pcd)->dma_enable) {
56698 + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
56699 + struct device *dev = NULL;
56700 +
56701 + if (otg_dev != NULL)
56702 + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
56703 +
56704 + if (usb_req->length != 0 &&
56705 + usb_req->dma == DWC_DMA_ADDR_INVALID) {
56706 + dma_addr = dma_map_single(dev, usb_req->buf,
56707 + usb_req->length,
56708 + ep->dwc_ep.is_in ?
56709 + DMA_TO_DEVICE:
56710 + DMA_FROM_DEVICE);
56711 + }
56712 + }
56713 +#endif
56714 +
56715 +#ifdef DWC_UTE_PER_IO
56716 + if (is_isoc_ep == 1) {
56717 + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
56718 + usb_req->length, usb_req->zero, usb_req,
56719 + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
56720 + if (retval)
56721 + return -EINVAL;
56722 +
56723 + return 0;
56724 + }
56725 +#endif
56726 + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
56727 + usb_req->length, usb_req->zero, usb_req,
56728 + gfp_flags == GFP_ATOMIC ? 1 : 0);
56729 + if (retval) {
56730 + return -EINVAL;
56731 + }
56732 +
56733 + return 0;
56734 +}
56735 +
56736 +/**
56737 + * This function cancels an I/O request from an EP.
56738 + */
56739 +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
56740 +{
56741 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
56742 +
56743 + if (!usb_ep || !usb_req) {
56744 + DWC_WARN("bad argument\n");
56745 + return -EINVAL;
56746 + }
56747 + if (!gadget_wrapper->driver ||
56748 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56749 + DWC_WARN("bogus device state\n");
56750 + return -ESHUTDOWN;
56751 + }
56752 + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
56753 + return -EINVAL;
56754 + }
56755 +
56756 + return 0;
56757 +}
56758 +
56759 +/**
56760 + * usb_ep_set_halt stalls an endpoint.
56761 + *
56762 + * usb_ep_clear_halt clears an endpoint halt and resets its data
56763 + * toggle.
56764 + *
56765 + * Both of these functions are implemented with the same underlying
56766 + * function. The behavior depends on the value argument.
56767 + *
56768 + * @param[in] usb_ep the Endpoint to halt or clear halt.
56769 + * @param[in] value
56770 + * - 0 means clear_halt.
56771 + * - 1 means set_halt,
56772 + * - 2 means clear stall lock flag.
56773 + * - 3 means set stall lock flag.
56774 + */
56775 +static int ep_halt(struct usb_ep *usb_ep, int value)
56776 +{
56777 + int retval = 0;
56778 +
56779 + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
56780 +
56781 + if (!usb_ep) {
56782 + DWC_WARN("bad ep\n");
56783 + return -EINVAL;
56784 + }
56785 +
56786 + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
56787 + if (retval == -DWC_E_AGAIN) {
56788 + return -EAGAIN;
56789 + } else if (retval) {
56790 + retval = -EINVAL;
56791 + }
56792 +
56793 + return retval;
56794 +}
56795 +
56796 +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
56797 +#if 0
56798 +/**
56799 + * ep_wedge: sets the halt feature and ignores clear requests
56800 + *
56801 + * @usb_ep: the endpoint being wedged
56802 + *
56803 + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
56804 + * requests. If the gadget driver clears the halt status, it will
56805 + * automatically unwedge the endpoint.
56806 + *
56807 + * Returns zero on success, else negative errno. *
56808 + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
56809 + */
56810 +static int ep_wedge(struct usb_ep *usb_ep)
56811 +{
56812 + int retval = 0;
56813 +
56814 + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
56815 +
56816 + if (!usb_ep) {
56817 + DWC_WARN("bad ep\n");
56818 + return -EINVAL;
56819 + }
56820 +
56821 + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
56822 + if (retval == -DWC_E_AGAIN) {
56823 + retval = -EAGAIN;
56824 + } else if (retval) {
56825 + retval = -EINVAL;
56826 + }
56827 +
56828 + return retval;
56829 +}
56830 +#endif
56831 +
56832 +#ifdef DWC_EN_ISOC
56833 +/**
56834 + * This function is used to submit an ISOC Transfer Request to an EP.
56835 + *
56836 + * - Every time a sync period completes the request's completion callback
56837 + * is called to provide data to the gadget driver.
56838 + * - Once submitted the request cannot be modified.
56839 + * - Each request is turned into periodic data packets untill ISO
56840 + * Transfer is stopped..
56841 + */
56842 +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
56843 + gfp_t gfp_flags)
56844 +{
56845 + int retval = 0;
56846 +
56847 + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
56848 + DWC_WARN("bad params\n");
56849 + return -EINVAL;
56850 + }
56851 +
56852 + if (!usb_ep) {
56853 + DWC_PRINTF("bad params\n");
56854 + return -EINVAL;
56855 + }
56856 +
56857 + req->status = -EINPROGRESS;
56858 +
56859 + retval =
56860 + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
56861 + req->buf1, req->dma0, req->dma1,
56862 + req->sync_frame, req->data_pattern_frame,
56863 + req->data_per_frame,
56864 + req->
56865 + flags & USB_REQ_ISO_ASAP ? -1 :
56866 + req->start_frame, req->buf_proc_intrvl,
56867 + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
56868 +
56869 + if (retval) {
56870 + return -EINVAL;
56871 + }
56872 +
56873 + return retval;
56874 +}
56875 +
56876 +/**
56877 + * This function stops ISO EP Periodic Data Transfer.
56878 + */
56879 +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
56880 +{
56881 + int retval = 0;
56882 + if (!usb_ep) {
56883 + DWC_WARN("bad ep\n");
56884 + }
56885 +
56886 + if (!gadget_wrapper->driver ||
56887 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56888 + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
56889 + gadget_wrapper->gadget.speed);
56890 + DWC_WARN("bogus device state\n");
56891 + }
56892 +
56893 + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
56894 + if (retval) {
56895 + retval = -EINVAL;
56896 + }
56897 +
56898 + return retval;
56899 +}
56900 +
56901 +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
56902 + int packets, gfp_t gfp_flags)
56903 +{
56904 + struct usb_iso_request *pReq = NULL;
56905 + uint32_t req_size;
56906 +
56907 + req_size = sizeof(struct usb_iso_request);
56908 + req_size +=
56909 + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
56910 +
56911 + pReq = kmalloc(req_size, gfp_flags);
56912 + if (!pReq) {
56913 + DWC_WARN("Can't allocate Iso Request\n");
56914 + return 0;
56915 + }
56916 + pReq->iso_packet_desc0 = (void *)(pReq + 1);
56917 +
56918 + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
56919 +
56920 + return pReq;
56921 +}
56922 +
56923 +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
56924 +{
56925 + kfree(req);
56926 +}
56927 +
56928 +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
56929 + .ep_ops = {
56930 + .enable = ep_enable,
56931 + .disable = ep_disable,
56932 +
56933 + .alloc_request = dwc_otg_pcd_alloc_request,
56934 + .free_request = dwc_otg_pcd_free_request,
56935 +
56936 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56937 + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
56938 + .free_buffer = dwc_otg_pcd_free_buffer,
56939 +#endif
56940 +
56941 + .queue = ep_queue,
56942 + .dequeue = ep_dequeue,
56943 +
56944 + .set_halt = ep_halt,
56945 + .fifo_status = 0,
56946 + .fifo_flush = 0,
56947 + },
56948 + .iso_ep_start = iso_ep_start,
56949 + .iso_ep_stop = iso_ep_stop,
56950 + .alloc_iso_request = alloc_iso_request,
56951 + .free_iso_request = free_iso_request,
56952 +};
56953 +
56954 +#else
56955 +
56956 + int (*enable) (struct usb_ep *ep,
56957 + const struct usb_endpoint_descriptor *desc);
56958 + int (*disable) (struct usb_ep *ep);
56959 +
56960 + struct usb_request *(*alloc_request) (struct usb_ep *ep,
56961 + gfp_t gfp_flags);
56962 + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
56963 +
56964 + int (*queue) (struct usb_ep *ep, struct usb_request *req,
56965 + gfp_t gfp_flags);
56966 + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
56967 +
56968 + int (*set_halt) (struct usb_ep *ep, int value);
56969 + int (*set_wedge) (struct usb_ep *ep);
56970 +
56971 + int (*fifo_status) (struct usb_ep *ep);
56972 + void (*fifo_flush) (struct usb_ep *ep);
56973 +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
56974 + .enable = ep_enable,
56975 + .disable = ep_disable,
56976 +
56977 + .alloc_request = dwc_otg_pcd_alloc_request,
56978 + .free_request = dwc_otg_pcd_free_request,
56979 +
56980 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56981 + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
56982 + .free_buffer = dwc_otg_pcd_free_buffer,
56983 +#else
56984 + /* .set_wedge = ep_wedge, */
56985 + .set_wedge = NULL, /* uses set_halt instead */
56986 +#endif
56987 +
56988 + .queue = ep_queue,
56989 + .dequeue = ep_dequeue,
56990 +
56991 + .set_halt = ep_halt,
56992 + .fifo_status = 0,
56993 + .fifo_flush = 0,
56994 +
56995 +};
56996 +
56997 +#endif /* _EN_ISOC_ */
56998 +/* Gadget Operations */
56999 +/**
57000 + * The following gadget operations will be implemented in the DWC_otg
57001 + * PCD. Functions in the API that are not described below are not
57002 + * implemented.
57003 + *
57004 + * The Gadget API provides wrapper functions for each of the function
57005 + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
57006 + * wrapper function, which then calls the underlying PCD function. The
57007 + * following sections are named according to the wrapper functions
57008 + * (except for ioctl, which doesn't have a wrapper function). Within
57009 + * each section, the corresponding DWC_otg PCD function name is
57010 + * specified.
57011 + *
57012 + */
57013 +
57014 +/**
57015 + *Gets the USB Frame number of the last SOF.
57016 + */
57017 +static int get_frame_number(struct usb_gadget *gadget)
57018 +{
57019 + struct gadget_wrapper *d;
57020 +
57021 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
57022 +
57023 + if (gadget == 0) {
57024 + return -ENODEV;
57025 + }
57026 +
57027 + d = container_of(gadget, struct gadget_wrapper, gadget);
57028 + return dwc_otg_pcd_get_frame_number(d->pcd);
57029 +}
57030 +
57031 +#ifdef CONFIG_USB_DWC_OTG_LPM
57032 +static int test_lpm_enabled(struct usb_gadget *gadget)
57033 +{
57034 + struct gadget_wrapper *d;
57035 +
57036 + d = container_of(gadget, struct gadget_wrapper, gadget);
57037 +
57038 + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
57039 +}
57040 +#endif
57041 +
57042 +/**
57043 + * Initiates Session Request Protocol (SRP) to wakeup the host if no
57044 + * session is in progress. If a session is already in progress, but
57045 + * the device is suspended, remote wakeup signaling is started.
57046 + *
57047 + */
57048 +static int wakeup(struct usb_gadget *gadget)
57049 +{
57050 + struct gadget_wrapper *d;
57051 +
57052 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
57053 +
57054 + if (gadget == 0) {
57055 + return -ENODEV;
57056 + } else {
57057 + d = container_of(gadget, struct gadget_wrapper, gadget);
57058 + }
57059 + dwc_otg_pcd_wakeup(d->pcd);
57060 + return 0;
57061 +}
57062 +
57063 +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
57064 + .get_frame = get_frame_number,
57065 + .wakeup = wakeup,
57066 +#ifdef CONFIG_USB_DWC_OTG_LPM
57067 + .lpm_support = test_lpm_enabled,
57068 +#endif
57069 + // current versions must always be self-powered
57070 +};
57071 +
57072 +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
57073 +{
57074 + int retval = -DWC_E_NOT_SUPPORTED;
57075 + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
57076 + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
57077 + (struct usb_ctrlrequest
57078 + *)bytes);
57079 + }
57080 +
57081 + if (retval == -ENOTSUPP) {
57082 + retval = -DWC_E_NOT_SUPPORTED;
57083 + } else if (retval < 0) {
57084 + retval = -DWC_E_INVALID;
57085 + }
57086 +
57087 + return retval;
57088 +}
57089 +
57090 +#ifdef DWC_EN_ISOC
57091 +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57092 + void *req_handle, int proc_buf_num)
57093 +{
57094 + int i, packet_count;
57095 + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
57096 + struct usb_iso_request *iso_req = req_handle;
57097 +
57098 + if (proc_buf_num) {
57099 + iso_packet = iso_req->iso_packet_desc1;
57100 + } else {
57101 + iso_packet = iso_req->iso_packet_desc0;
57102 + }
57103 + packet_count =
57104 + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
57105 + for (i = 0; i < packet_count; ++i) {
57106 + int status;
57107 + int actual;
57108 + int offset;
57109 + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
57110 + i, &status, &actual, &offset);
57111 + switch (status) {
57112 + case -DWC_E_NO_DATA:
57113 + status = -ENODATA;
57114 + break;
57115 + default:
57116 + if (status) {
57117 + DWC_PRINTF("unknown status in isoc packet\n");
57118 + }
57119 +
57120 + }
57121 + iso_packet[i].status = status;
57122 + iso_packet[i].offset = offset;
57123 + iso_packet[i].actual_length = actual;
57124 + }
57125 +
57126 + iso_req->status = 0;
57127 + iso_req->process_buffer(ep_handle, iso_req);
57128 +
57129 + return 0;
57130 +}
57131 +#endif /* DWC_EN_ISOC */
57132 +
57133 +#ifdef DWC_UTE_PER_IO
57134 +/**
57135 + * Copy the contents of the extended request to the Linux usb_request's
57136 + * extended part and call the gadget's completion.
57137 + *
57138 + * @param pcd Pointer to the pcd structure
57139 + * @param ep_handle Void pointer to the usb_ep structure
57140 + * @param req_handle Void pointer to the usb_request structure
57141 + * @param status Request status returned from the portable logic
57142 + * @param ereq_port Void pointer to the extended request structure
57143 + * created in the the portable part that contains the
57144 + * results of the processed iso packets.
57145 + */
57146 +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57147 + void *req_handle, int32_t status, void *ereq_port)
57148 +{
57149 + struct dwc_ute_iso_req_ext *ereqorg = NULL;
57150 + struct dwc_iso_xreq_port *ereqport = NULL;
57151 + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
57152 + int i;
57153 + struct usb_request *req;
57154 + //struct dwc_ute_iso_packet_descriptor *
57155 + //int status = 0;
57156 +
57157 + req = (struct usb_request *)req_handle;
57158 + ereqorg = &req->ext_req;
57159 + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
57160 + desc_org = ereqorg->per_io_frame_descs;
57161 +
57162 + if (req && req->complete) {
57163 + /* Copy the request data from the portable logic to our request */
57164 + for (i = 0; i < ereqport->pio_pkt_count; i++) {
57165 + desc_org[i].actual_length =
57166 + ereqport->per_io_frame_descs[i].actual_length;
57167 + desc_org[i].status =
57168 + ereqport->per_io_frame_descs[i].status;
57169 + }
57170 +
57171 + switch (status) {
57172 + case -DWC_E_SHUTDOWN:
57173 + req->status = -ESHUTDOWN;
57174 + break;
57175 + case -DWC_E_RESTART:
57176 + req->status = -ECONNRESET;
57177 + break;
57178 + case -DWC_E_INVALID:
57179 + req->status = -EINVAL;
57180 + break;
57181 + case -DWC_E_TIMEOUT:
57182 + req->status = -ETIMEDOUT;
57183 + break;
57184 + default:
57185 + req->status = status;
57186 + }
57187 +
57188 + /* And call the gadget's completion */
57189 + req->complete(ep_handle, req);
57190 + }
57191 +
57192 + return 0;
57193 +}
57194 +#endif /* DWC_UTE_PER_IO */
57195 +
57196 +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57197 + void *req_handle, int32_t status, uint32_t actual)
57198 +{
57199 + struct usb_request *req = (struct usb_request *)req_handle;
57200 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
57201 + struct dwc_otg_pcd_ep *ep = NULL;
57202 +#endif
57203 +
57204 + if (req && req->complete) {
57205 + switch (status) {
57206 + case -DWC_E_SHUTDOWN:
57207 + req->status = -ESHUTDOWN;
57208 + break;
57209 + case -DWC_E_RESTART:
57210 + req->status = -ECONNRESET;
57211 + break;
57212 + case -DWC_E_INVALID:
57213 + req->status = -EINVAL;
57214 + break;
57215 + case -DWC_E_TIMEOUT:
57216 + req->status = -ETIMEDOUT;
57217 + break;
57218 + default:
57219 + req->status = status;
57220 +
57221 + }
57222 +
57223 + req->actual = actual;
57224 + DWC_SPINUNLOCK(pcd->lock);
57225 + req->complete(ep_handle, req);
57226 + DWC_SPINLOCK(pcd->lock);
57227 + }
57228 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
57229 + ep = ep_from_handle(pcd, ep_handle);
57230 + if (GET_CORE_IF(pcd)->dma_enable) {
57231 + if (req->length != 0) {
57232 + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
57233 + struct device *dev = NULL;
57234 +
57235 + if (otg_dev != NULL)
57236 + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
57237 +
57238 + dma_unmap_single(dev, req->dma, req->length,
57239 + ep->dwc_ep.is_in ?
57240 + DMA_TO_DEVICE: DMA_FROM_DEVICE);
57241 + }
57242 + }
57243 +#endif
57244 +
57245 + return 0;
57246 +}
57247 +
57248 +static int _connect(dwc_otg_pcd_t * pcd, int speed)
57249 +{
57250 + gadget_wrapper->gadget.speed = speed;
57251 + return 0;
57252 +}
57253 +
57254 +static int _disconnect(dwc_otg_pcd_t * pcd)
57255 +{
57256 + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
57257 + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
57258 + }
57259 + return 0;
57260 +}
57261 +
57262 +static int _resume(dwc_otg_pcd_t * pcd)
57263 +{
57264 + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
57265 + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
57266 + }
57267 +
57268 + return 0;
57269 +}
57270 +
57271 +static int _suspend(dwc_otg_pcd_t * pcd)
57272 +{
57273 + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
57274 + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
57275 + }
57276 + return 0;
57277 +}
57278 +
57279 +/**
57280 + * This function updates the otg values in the gadget structure.
57281 + */
57282 +static int _hnp_changed(dwc_otg_pcd_t * pcd)
57283 +{
57284 +
57285 + if (!gadget_wrapper->gadget.is_otg)
57286 + return 0;
57287 +
57288 + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
57289 + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
57290 + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
57291 + return 0;
57292 +}
57293 +
57294 +static int _reset(dwc_otg_pcd_t * pcd)
57295 +{
57296 + return 0;
57297 +}
57298 +
57299 +#ifdef DWC_UTE_CFI
57300 +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
57301 +{
57302 + int retval = -DWC_E_INVALID;
57303 + if (gadget_wrapper->driver->cfi_feature_setup) {
57304 + retval =
57305 + gadget_wrapper->driver->
57306 + cfi_feature_setup(&gadget_wrapper->gadget,
57307 + (struct cfi_usb_ctrlrequest *)cfi_req);
57308 + }
57309 +
57310 + return retval;
57311 +}
57312 +#endif
57313 +
57314 +static const struct dwc_otg_pcd_function_ops fops = {
57315 + .complete = _complete,
57316 +#ifdef DWC_EN_ISOC
57317 + .isoc_complete = _isoc_complete,
57318 +#endif
57319 + .setup = _setup,
57320 + .disconnect = _disconnect,
57321 + .connect = _connect,
57322 + .resume = _resume,
57323 + .suspend = _suspend,
57324 + .hnp_changed = _hnp_changed,
57325 + .reset = _reset,
57326 +#ifdef DWC_UTE_CFI
57327 + .cfi_setup = _cfi_setup,
57328 +#endif
57329 +#ifdef DWC_UTE_PER_IO
57330 + .xisoc_complete = _xisoc_complete,
57331 +#endif
57332 +};
57333 +
57334 +/**
57335 + * This function is the top level PCD interrupt handler.
57336 + */
57337 +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
57338 +{
57339 + dwc_otg_pcd_t *pcd = dev;
57340 + int32_t retval = IRQ_NONE;
57341 +
57342 + retval = dwc_otg_pcd_handle_intr(pcd);
57343 + if (retval != 0) {
57344 + S3C2410X_CLEAR_EINTPEND();
57345 + }
57346 + return IRQ_RETVAL(retval);
57347 +}
57348 +
57349 +/**
57350 + * This function initialized the usb_ep structures to there default
57351 + * state.
57352 + *
57353 + * @param d Pointer on gadget_wrapper.
57354 + */
57355 +void gadget_add_eps(struct gadget_wrapper *d)
57356 +{
57357 + static const char *names[] = {
57358 +
57359 + "ep0",
57360 + "ep1in",
57361 + "ep2in",
57362 + "ep3in",
57363 + "ep4in",
57364 + "ep5in",
57365 + "ep6in",
57366 + "ep7in",
57367 + "ep8in",
57368 + "ep9in",
57369 + "ep10in",
57370 + "ep11in",
57371 + "ep12in",
57372 + "ep13in",
57373 + "ep14in",
57374 + "ep15in",
57375 + "ep1out",
57376 + "ep2out",
57377 + "ep3out",
57378 + "ep4out",
57379 + "ep5out",
57380 + "ep6out",
57381 + "ep7out",
57382 + "ep8out",
57383 + "ep9out",
57384 + "ep10out",
57385 + "ep11out",
57386 + "ep12out",
57387 + "ep13out",
57388 + "ep14out",
57389 + "ep15out"
57390 + };
57391 +
57392 + int i;
57393 + struct usb_ep *ep;
57394 + int8_t dev_endpoints;
57395 +
57396 + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
57397 +
57398 + INIT_LIST_HEAD(&d->gadget.ep_list);
57399 + d->gadget.ep0 = &d->ep0;
57400 + d->gadget.speed = USB_SPEED_UNKNOWN;
57401 +
57402 + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
57403 +
57404 + /**
57405 + * Initialize the EP0 structure.
57406 + */
57407 + ep = &d->ep0;
57408 +
57409 + /* Init the usb_ep structure. */
57410 + ep->name = names[0];
57411 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57412 +
57413 + /**
57414 + * @todo NGS: What should the max packet size be set to
57415 + * here? Before EP type is set?
57416 + */
57417 + ep->maxpacket = MAX_PACKET_SIZE;
57418 + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
57419 +
57420 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57421 +
57422 + /**
57423 + * Initialize the EP structures.
57424 + */
57425 + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
57426 +
57427 + for (i = 0; i < dev_endpoints; i++) {
57428 + ep = &d->in_ep[i];
57429 +
57430 + /* Init the usb_ep structure. */
57431 + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
57432 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57433 +
57434 + /**
57435 + * @todo NGS: What should the max packet size be set to
57436 + * here? Before EP type is set?
57437 + */
57438 + ep->maxpacket = MAX_PACKET_SIZE;
57439 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57440 + }
57441 +
57442 + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
57443 +
57444 + for (i = 0; i < dev_endpoints; i++) {
57445 + ep = &d->out_ep[i];
57446 +
57447 + /* Init the usb_ep structure. */
57448 + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
57449 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57450 +
57451 + /**
57452 + * @todo NGS: What should the max packet size be set to
57453 + * here? Before EP type is set?
57454 + */
57455 + ep->maxpacket = MAX_PACKET_SIZE;
57456 +
57457 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57458 + }
57459 +
57460 + /* remove ep0 from the list. There is a ep0 pointer. */
57461 + list_del_init(&d->ep0.ep_list);
57462 +
57463 + d->ep0.maxpacket = MAX_EP0_SIZE;
57464 +}
57465 +
57466 +/**
57467 + * This function releases the Gadget device.
57468 + * required by device_unregister().
57469 + *
57470 + * @todo Should this do something? Should it free the PCD?
57471 + */
57472 +static void dwc_otg_pcd_gadget_release(struct device *dev)
57473 +{
57474 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
57475 +}
57476 +
57477 +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
57478 +{
57479 + static char pcd_name[] = "dwc_otg_pcd";
57480 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57481 + struct gadget_wrapper *d;
57482 + int retval;
57483 +
57484 + d = DWC_ALLOC(sizeof(*d));
57485 + if (d == NULL) {
57486 + return NULL;
57487 + }
57488 +
57489 + memset(d, 0, sizeof(*d));
57490 +
57491 + d->gadget.name = pcd_name;
57492 + d->pcd = otg_dev->pcd;
57493 +
57494 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
57495 + strcpy(d->gadget.dev.bus_id, "gadget");
57496 +#else
57497 + dev_set_name(&d->gadget.dev, "%s", "gadget");
57498 +#endif
57499 +
57500 + d->gadget.dev.parent = &_dev->dev;
57501 + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
57502 + d->gadget.ops = &dwc_otg_pcd_ops;
57503 + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
57504 + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
57505 +
57506 + d->driver = 0;
57507 + /* Register the gadget device */
57508 + retval = device_register(&d->gadget.dev);
57509 + if (retval != 0) {
57510 + DWC_ERROR("device_register failed\n");
57511 + DWC_FREE(d);
57512 + return NULL;
57513 + }
57514 +
57515 + return d;
57516 +}
57517 +
57518 +static void free_wrapper(struct gadget_wrapper *d)
57519 +{
57520 + if (d->driver) {
57521 + /* should have been done already by driver model core */
57522 + DWC_WARN("driver '%s' is still registered\n",
57523 + d->driver->driver.name);
57524 +#ifdef CONFIG_USB_GADGET
57525 + usb_gadget_unregister_driver(d->driver);
57526 +#endif
57527 + }
57528 +
57529 + device_unregister(&d->gadget.dev);
57530 + DWC_FREE(d);
57531 +}
57532 +
57533 +/**
57534 + * This function initialized the PCD portion of the driver.
57535 + *
57536 + */
57537 +int pcd_init(dwc_bus_dev_t *_dev)
57538 +{
57539 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57540 + int retval = 0;
57541 +
57542 + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
57543 +
57544 + otg_dev->pcd = dwc_otg_pcd_init(otg_dev);
57545 +
57546 + if (!otg_dev->pcd) {
57547 + DWC_ERROR("dwc_otg_pcd_init failed\n");
57548 + return -ENOMEM;
57549 + }
57550 +
57551 + otg_dev->pcd->otg_dev = otg_dev;
57552 + gadget_wrapper = alloc_wrapper(_dev);
57553 +
57554 + /*
57555 + * Initialize EP structures
57556 + */
57557 + gadget_add_eps(gadget_wrapper);
57558 + /*
57559 + * Setup interupt handler
57560 + */
57561 +#ifdef PLATFORM_INTERFACE
57562 + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
57563 + platform_get_irq(_dev, fiq_enable ? 0 : 1));
57564 + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
57565 + IRQF_SHARED, gadget_wrapper->gadget.name,
57566 + otg_dev->pcd);
57567 + if (retval != 0) {
57568 + DWC_ERROR("request of irq%d failed\n",
57569 + platform_get_irq(_dev, fiq_enable ? 0 : 1));
57570 + free_wrapper(gadget_wrapper);
57571 + return -EBUSY;
57572 + }
57573 +#else
57574 + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
57575 + _dev->irq);
57576 + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
57577 + IRQF_SHARED | IRQF_DISABLED,
57578 + gadget_wrapper->gadget.name, otg_dev->pcd);
57579 + if (retval != 0) {
57580 + DWC_ERROR("request of irq%d failed\n", _dev->irq);
57581 + free_wrapper(gadget_wrapper);
57582 + return -EBUSY;
57583 + }
57584 +#endif
57585 +
57586 + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
57587 +
57588 + return retval;
57589 +}
57590 +
57591 +/**
57592 + * Cleanup the PCD.
57593 + */
57594 +void pcd_remove(dwc_bus_dev_t *_dev)
57595 +{
57596 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57597 + dwc_otg_pcd_t *pcd = otg_dev->pcd;
57598 +
57599 + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
57600 +
57601 + /*
57602 + * Free the IRQ
57603 + */
57604 +#ifdef PLATFORM_INTERFACE
57605 + free_irq(platform_get_irq(_dev, 0), pcd);
57606 +#else
57607 + free_irq(_dev->irq, pcd);
57608 +#endif
57609 + dwc_otg_pcd_remove(otg_dev->pcd);
57610 + free_wrapper(gadget_wrapper);
57611 + otg_dev->pcd = 0;
57612 +}
57613 +
57614 +#endif /* DWC_HOST_ONLY */
57615 --- /dev/null
57616 +++ b/drivers/usb/host/dwc_otg/dwc_otg_regs.h
57617 @@ -0,0 +1,2550 @@
57618 +/* ==========================================================================
57619 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
57620 + * $Revision: #98 $
57621 + * $Date: 2012/08/10 $
57622 + * $Change: 2047372 $
57623 + *
57624 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
57625 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
57626 + * otherwise expressly agreed to in writing between Synopsys and you.
57627 + *
57628 + * The Software IS NOT an item of Licensed Software or Licensed Product under
57629 + * any End User Software License Agreement or Agreement for Licensed Product
57630 + * with Synopsys or any supplement thereto. You are permitted to use and
57631 + * redistribute this Software in source and binary forms, with or without
57632 + * modification, provided that redistributions of source code must retain this
57633 + * notice. You may not view, use, disclose, copy or distribute this file or
57634 + * any information contained herein except pursuant to this license grant from
57635 + * Synopsys. If you do not agree with this notice, including the disclaimer
57636 + * below, then you are not authorized to use the Software.
57637 + *
57638 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
57639 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57640 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57641 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
57642 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57643 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57644 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57645 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57646 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57647 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
57648 + * DAMAGE.
57649 + * ========================================================================== */
57650 +
57651 +#ifndef __DWC_OTG_REGS_H__
57652 +#define __DWC_OTG_REGS_H__
57653 +
57654 +#include "dwc_otg_core_if.h"
57655 +
57656 +/**
57657 + * @file
57658 + *
57659 + * This file contains the data structures for accessing the DWC_otg core registers.
57660 + *
57661 + * The application interfaces with the HS OTG core by reading from and
57662 + * writing to the Control and Status Register (CSR) space through the
57663 + * AHB Slave interface. These registers are 32 bits wide, and the
57664 + * addresses are 32-bit-block aligned.
57665 + * CSRs are classified as follows:
57666 + * - Core Global Registers
57667 + * - Device Mode Registers
57668 + * - Device Global Registers
57669 + * - Device Endpoint Specific Registers
57670 + * - Host Mode Registers
57671 + * - Host Global Registers
57672 + * - Host Port CSRs
57673 + * - Host Channel Specific Registers
57674 + *
57675 + * Only the Core Global registers can be accessed in both Device and
57676 + * Host modes. When the HS OTG core is operating in one mode, either
57677 + * Device or Host, the application must not access registers from the
57678 + * other mode. When the core switches from one mode to another, the
57679 + * registers in the new mode of operation must be reprogrammed as they
57680 + * would be after a power-on reset.
57681 + */
57682 +
57683 +/****************************************************************************/
57684 +/** DWC_otg Core registers .
57685 + * The dwc_otg_core_global_regs structure defines the size
57686 + * and relative field offsets for the Core Global registers.
57687 + */
57688 +typedef struct dwc_otg_core_global_regs {
57689 + /** OTG Control and Status Register. <i>Offset: 000h</i> */
57690 + volatile uint32_t gotgctl;
57691 + /** OTG Interrupt Register. <i>Offset: 004h</i> */
57692 + volatile uint32_t gotgint;
57693 + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
57694 + volatile uint32_t gahbcfg;
57695 +
57696 +#define DWC_GLBINTRMASK 0x0001
57697 +#define DWC_DMAENABLE 0x0020
57698 +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
57699 +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
57700 +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
57701 +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
57702 +
57703 + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
57704 + volatile uint32_t gusbcfg;
57705 + /**Core Reset Register. <i>Offset: 010h</i> */
57706 + volatile uint32_t grstctl;
57707 + /**Core Interrupt Register. <i>Offset: 014h</i> */
57708 + volatile uint32_t gintsts;
57709 + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
57710 + volatile uint32_t gintmsk;
57711 + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
57712 + volatile uint32_t grxstsr;
57713 + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
57714 + volatile uint32_t grxstsp;
57715 + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
57716 + volatile uint32_t grxfsiz;
57717 + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
57718 + volatile uint32_t gnptxfsiz;
57719 + /**Non Periodic Transmit FIFO/Queue Status Register (Read
57720 + * Only). <i>Offset: 02Ch</i> */
57721 + volatile uint32_t gnptxsts;
57722 + /**I2C Access Register. <i>Offset: 030h</i> */
57723 + volatile uint32_t gi2cctl;
57724 + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
57725 + volatile uint32_t gpvndctl;
57726 + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
57727 + volatile uint32_t ggpio;
57728 + /**User ID Register. <i>Offset: 03Ch</i> */
57729 + volatile uint32_t guid;
57730 + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
57731 + volatile uint32_t gsnpsid;
57732 + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
57733 + volatile uint32_t ghwcfg1;
57734 + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
57735 + volatile uint32_t ghwcfg2;
57736 +#define DWC_SLAVE_ONLY_ARCH 0
57737 +#define DWC_EXT_DMA_ARCH 1
57738 +#define DWC_INT_DMA_ARCH 2
57739 +
57740 +#define DWC_MODE_HNP_SRP_CAPABLE 0
57741 +#define DWC_MODE_SRP_ONLY_CAPABLE 1
57742 +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
57743 +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
57744 +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
57745 +#define DWC_MODE_SRP_CAPABLE_HOST 5
57746 +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
57747 +
57748 + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
57749 + volatile uint32_t ghwcfg3;
57750 + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
57751 + volatile uint32_t ghwcfg4;
57752 + /** Core LPM Configuration register <i>Offset: 054h</i>*/
57753 + volatile uint32_t glpmcfg;
57754 + /** Global PowerDn Register <i>Offset: 058h</i> */
57755 + volatile uint32_t gpwrdn;
57756 + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
57757 + volatile uint32_t gdfifocfg;
57758 + /** ADP Control Register <i>Offset: 060h</i> */
57759 + volatile uint32_t adpctl;
57760 + /** Reserved <i>Offset: 064h-0FFh</i> */
57761 + volatile uint32_t reserved39[39];
57762 + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
57763 + volatile uint32_t hptxfsiz;
57764 + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
57765 + otherwise Device Transmit FIFO#n Register.
57766 + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
57767 + volatile uint32_t dtxfsiz[15];
57768 +} dwc_otg_core_global_regs_t;
57769 +
57770 +/**
57771 + * This union represents the bit fields of the Core OTG Control
57772 + * and Status Register (GOTGCTL). Set the bits using the bit
57773 + * fields then write the <i>d32</i> value to the register.
57774 + */
57775 +typedef union gotgctl_data {
57776 + /** raw register data */
57777 + uint32_t d32;
57778 + /** register bits */
57779 + struct {
57780 + unsigned sesreqscs:1;
57781 + unsigned sesreq:1;
57782 + unsigned vbvalidoven:1;
57783 + unsigned vbvalidovval:1;
57784 + unsigned avalidoven:1;
57785 + unsigned avalidovval:1;
57786 + unsigned bvalidoven:1;
57787 + unsigned bvalidovval:1;
57788 + unsigned hstnegscs:1;
57789 + unsigned hnpreq:1;
57790 + unsigned hstsethnpen:1;
57791 + unsigned devhnpen:1;
57792 + unsigned reserved12_15:4;
57793 + unsigned conidsts:1;
57794 + unsigned dbnctime:1;
57795 + unsigned asesvld:1;
57796 + unsigned bsesvld:1;
57797 + unsigned otgver:1;
57798 + unsigned reserved1:1;
57799 + unsigned multvalidbc:5;
57800 + unsigned chirpen:1;
57801 + unsigned reserved28_31:4;
57802 + } b;
57803 +} gotgctl_data_t;
57804 +
57805 +/**
57806 + * This union represents the bit fields of the Core OTG Interrupt Register
57807 + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
57808 + * value to the register.
57809 + */
57810 +typedef union gotgint_data {
57811 + /** raw register data */
57812 + uint32_t d32;
57813 + /** register bits */
57814 + struct {
57815 + /** Current Mode */
57816 + unsigned reserved0_1:2;
57817 +
57818 + /** Session End Detected */
57819 + unsigned sesenddet:1;
57820 +
57821 + unsigned reserved3_7:5;
57822 +
57823 + /** Session Request Success Status Change */
57824 + unsigned sesreqsucstschng:1;
57825 + /** Host Negotiation Success Status Change */
57826 + unsigned hstnegsucstschng:1;
57827 +
57828 + unsigned reserved10_16:7;
57829 +
57830 + /** Host Negotiation Detected */
57831 + unsigned hstnegdet:1;
57832 + /** A-Device Timeout Change */
57833 + unsigned adevtoutchng:1;
57834 + /** Debounce Done */
57835 + unsigned debdone:1;
57836 + /** Multi-Valued input changed */
57837 + unsigned mvic:1;
57838 +
57839 + unsigned reserved31_21:11;
57840 +
57841 + } b;
57842 +} gotgint_data_t;
57843 +
57844 +/**
57845 + * This union represents the bit fields of the Core AHB Configuration
57846 + * Register (GAHBCFG). Set/clear the bits using the bit fields then
57847 + * write the <i>d32</i> value to the register.
57848 + */
57849 +typedef union gahbcfg_data {
57850 + /** raw register data */
57851 + uint32_t d32;
57852 + /** register bits */
57853 + struct {
57854 + unsigned glblintrmsk:1;
57855 +#define DWC_GAHBCFG_GLBINT_ENABLE 1
57856 +
57857 + unsigned hburstlen:4;
57858 +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
57859 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
57860 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
57861 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
57862 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
57863 +
57864 + unsigned dmaenable:1;
57865 +#define DWC_GAHBCFG_DMAENABLE 1
57866 + unsigned reserved:1;
57867 + unsigned nptxfemplvl_txfemplvl:1;
57868 + unsigned ptxfemplvl:1;
57869 +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
57870 +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
57871 + unsigned reserved9_20:12;
57872 + unsigned remmemsupp:1;
57873 + unsigned notialldmawrit:1;
57874 + unsigned ahbsingle:1;
57875 + unsigned reserved24_31:8;
57876 + } b;
57877 +} gahbcfg_data_t;
57878 +
57879 +/**
57880 + * This union represents the bit fields of the Core USB Configuration
57881 + * Register (GUSBCFG). Set the bits using the bit fields then write
57882 + * the <i>d32</i> value to the register.
57883 + */
57884 +typedef union gusbcfg_data {
57885 + /** raw register data */
57886 + uint32_t d32;
57887 + /** register bits */
57888 + struct {
57889 + unsigned toutcal:3;
57890 + unsigned phyif:1;
57891 + unsigned ulpi_utmi_sel:1;
57892 + unsigned fsintf:1;
57893 + unsigned physel:1;
57894 + unsigned ddrsel:1;
57895 + unsigned srpcap:1;
57896 + unsigned hnpcap:1;
57897 + unsigned usbtrdtim:4;
57898 + unsigned reserved1:1;
57899 + unsigned phylpwrclksel:1;
57900 + unsigned otgutmifssel:1;
57901 + unsigned ulpi_fsls:1;
57902 + unsigned ulpi_auto_res:1;
57903 + unsigned ulpi_clk_sus_m:1;
57904 + unsigned ulpi_ext_vbus_drv:1;
57905 + unsigned ulpi_int_vbus_indicator:1;
57906 + unsigned term_sel_dl_pulse:1;
57907 + unsigned indicator_complement:1;
57908 + unsigned indicator_pass_through:1;
57909 + unsigned ulpi_int_prot_dis:1;
57910 + unsigned ic_usb_cap:1;
57911 + unsigned ic_traffic_pull_remove:1;
57912 + unsigned tx_end_delay:1;
57913 + unsigned force_host_mode:1;
57914 + unsigned force_dev_mode:1;
57915 + unsigned reserved31:1;
57916 + } b;
57917 +} gusbcfg_data_t;
57918 +
57919 +/**
57920 + * This union represents the bit fields of the Core Reset Register
57921 + * (GRSTCTL). Set/clear the bits using the bit fields then write the
57922 + * <i>d32</i> value to the register.
57923 + */
57924 +typedef union grstctl_data {
57925 + /** raw register data */
57926 + uint32_t d32;
57927 + /** register bits */
57928 + struct {
57929 + /** Core Soft Reset (CSftRst) (Device and Host)
57930 + *
57931 + * The application can flush the control logic in the
57932 + * entire core using this bit. This bit resets the
57933 + * pipelines in the AHB Clock domain as well as the
57934 + * PHY Clock domain.
57935 + *
57936 + * The state machines are reset to an IDLE state, the
57937 + * control bits in the CSRs are cleared, all the
57938 + * transmit FIFOs and the receive FIFO are flushed.
57939 + *
57940 + * The status mask bits that control the generation of
57941 + * the interrupt, are cleared, to clear the
57942 + * interrupt. The interrupt status bits are not
57943 + * cleared, so the application can get the status of
57944 + * any events that occurred in the core after it has
57945 + * set this bit.
57946 + *
57947 + * Any transactions on the AHB are terminated as soon
57948 + * as possible following the protocol. Any
57949 + * transactions on the USB are terminated immediately.
57950 + *
57951 + * The configuration settings in the CSRs are
57952 + * unchanged, so the software doesn't have to
57953 + * reprogram these registers (Device
57954 + * Configuration/Host Configuration/Core System
57955 + * Configuration/Core PHY Configuration).
57956 + *
57957 + * The application can write to this bit, any time it
57958 + * wants to reset the core. This is a self clearing
57959 + * bit and the core clears this bit after all the
57960 + * necessary logic is reset in the core, which may
57961 + * take several clocks, depending on the current state
57962 + * of the core.
57963 + */
57964 + unsigned csftrst:1;
57965 + /** Hclk Soft Reset
57966 + *
57967 + * The application uses this bit to reset the control logic in
57968 + * the AHB clock domain. Only AHB clock domain pipelines are
57969 + * reset.
57970 + */
57971 + unsigned hsftrst:1;
57972 + /** Host Frame Counter Reset (Host Only)<br>
57973 + *
57974 + * The application can reset the (micro)frame number
57975 + * counter inside the core, using this bit. When the
57976 + * (micro)frame counter is reset, the subsequent SOF
57977 + * sent out by the core, will have a (micro)frame
57978 + * number of 0.
57979 + */
57980 + unsigned hstfrm:1;
57981 + /** In Token Sequence Learning Queue Flush
57982 + * (INTknQFlsh) (Device Only)
57983 + */
57984 + unsigned intknqflsh:1;
57985 + /** RxFIFO Flush (RxFFlsh) (Device and Host)
57986 + *
57987 + * The application can flush the entire Receive FIFO
57988 + * using this bit. The application must first
57989 + * ensure that the core is not in the middle of a
57990 + * transaction. The application should write into
57991 + * this bit, only after making sure that neither the
57992 + * DMA engine is reading from the RxFIFO nor the MAC
57993 + * is writing the data in to the FIFO. The
57994 + * application should wait until the bit is cleared
57995 + * before performing any other operations. This bit
57996 + * will takes 8 clocks (slowest of PHY or AHB clock)
57997 + * to clear.
57998 + */
57999 + unsigned rxfflsh:1;
58000 + /** TxFIFO Flush (TxFFlsh) (Device and Host).
58001 + *
58002 + * This bit is used to selectively flush a single or
58003 + * all transmit FIFOs. The application must first
58004 + * ensure that the core is not in the middle of a
58005 + * transaction. The application should write into
58006 + * this bit, only after making sure that neither the
58007 + * DMA engine is writing into the TxFIFO nor the MAC
58008 + * is reading the data out of the FIFO. The
58009 + * application should wait until the core clears this
58010 + * bit, before performing any operations. This bit
58011 + * will takes 8 clocks (slowest of PHY or AHB clock)
58012 + * to clear.
58013 + */
58014 + unsigned txfflsh:1;
58015 +
58016 + /** TxFIFO Number (TxFNum) (Device and Host).
58017 + *
58018 + * This is the FIFO number which needs to be flushed,
58019 + * using the TxFIFO Flush bit. This field should not
58020 + * be changed until the TxFIFO Flush bit is cleared by
58021 + * the core.
58022 + * - 0x0 : Non Periodic TxFIFO Flush
58023 + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
58024 + * or Periodic TxFIFO in host mode
58025 + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
58026 + * - ...
58027 + * - 0xF : Periodic TxFIFO #15 Flush in device mode
58028 + * - 0x10: Flush all the Transmit NonPeriodic and
58029 + * Transmit Periodic FIFOs in the core
58030 + */
58031 + unsigned txfnum:5;
58032 + /** Reserved */
58033 + unsigned reserved11_29:19;
58034 + /** DMA Request Signal. Indicated DMA request is in
58035 + * probress. Used for debug purpose. */
58036 + unsigned dmareq:1;
58037 + /** AHB Master Idle. Indicates the AHB Master State
58038 + * Machine is in IDLE condition. */
58039 + unsigned ahbidle:1;
58040 + } b;
58041 +} grstctl_t;
58042 +
58043 +/**
58044 + * This union represents the bit fields of the Core Interrupt Mask
58045 + * Register (GINTMSK). Set/clear the bits using the bit fields then
58046 + * write the <i>d32</i> value to the register.
58047 + */
58048 +typedef union gintmsk_data {
58049 + /** raw register data */
58050 + uint32_t d32;
58051 + /** register bits */
58052 + struct {
58053 + unsigned reserved0:1;
58054 + unsigned modemismatch:1;
58055 + unsigned otgintr:1;
58056 + unsigned sofintr:1;
58057 + unsigned rxstsqlvl:1;
58058 + unsigned nptxfempty:1;
58059 + unsigned ginnakeff:1;
58060 + unsigned goutnakeff:1;
58061 + unsigned ulpickint:1;
58062 + unsigned i2cintr:1;
58063 + unsigned erlysuspend:1;
58064 + unsigned usbsuspend:1;
58065 + unsigned usbreset:1;
58066 + unsigned enumdone:1;
58067 + unsigned isooutdrop:1;
58068 + unsigned eopframe:1;
58069 + unsigned restoredone:1;
58070 + unsigned epmismatch:1;
58071 + unsigned inepintr:1;
58072 + unsigned outepintr:1;
58073 + unsigned incomplisoin:1;
58074 + unsigned incomplisoout:1;
58075 + unsigned fetsusp:1;
58076 + unsigned resetdet:1;
58077 + unsigned portintr:1;
58078 + unsigned hcintr:1;
58079 + unsigned ptxfempty:1;
58080 + unsigned lpmtranrcvd:1;
58081 + unsigned conidstschng:1;
58082 + unsigned disconnect:1;
58083 + unsigned sessreqintr:1;
58084 + unsigned wkupintr:1;
58085 + } b;
58086 +} gintmsk_data_t;
58087 +/**
58088 + * This union represents the bit fields of the Core Interrupt Register
58089 + * (GINTSTS). Set/clear the bits using the bit fields then write the
58090 + * <i>d32</i> value to the register.
58091 + */
58092 +typedef union gintsts_data {
58093 + /** raw register data */
58094 + uint32_t d32;
58095 +#define DWC_SOF_INTR_MASK 0x0008
58096 + /** register bits */
58097 + struct {
58098 +#define DWC_HOST_MODE 1
58099 + unsigned curmode:1;
58100 + unsigned modemismatch:1;
58101 + unsigned otgintr:1;
58102 + unsigned sofintr:1;
58103 + unsigned rxstsqlvl:1;
58104 + unsigned nptxfempty:1;
58105 + unsigned ginnakeff:1;
58106 + unsigned goutnakeff:1;
58107 + unsigned ulpickint:1;
58108 + unsigned i2cintr:1;
58109 + unsigned erlysuspend:1;
58110 + unsigned usbsuspend:1;
58111 + unsigned usbreset:1;
58112 + unsigned enumdone:1;
58113 + unsigned isooutdrop:1;
58114 + unsigned eopframe:1;
58115 + unsigned restoredone:1;
58116 + unsigned epmismatch:1;
58117 + unsigned inepint:1;
58118 + unsigned outepintr:1;
58119 + unsigned incomplisoin:1;
58120 + unsigned incomplisoout:1;
58121 + unsigned fetsusp:1;
58122 + unsigned resetdet:1;
58123 + unsigned portintr:1;
58124 + unsigned hcintr:1;
58125 + unsigned ptxfempty:1;
58126 + unsigned lpmtranrcvd:1;
58127 + unsigned conidstschng:1;
58128 + unsigned disconnect:1;
58129 + unsigned sessreqintr:1;
58130 + unsigned wkupintr:1;
58131 + } b;
58132 +} gintsts_data_t;
58133 +
58134 +/**
58135 + * This union represents the bit fields in the Device Receive Status Read and
58136 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
58137 + * element then read out the bits using the <i>b</i>it elements.
58138 + */
58139 +typedef union device_grxsts_data {
58140 + /** raw register data */
58141 + uint32_t d32;
58142 + /** register bits */
58143 + struct {
58144 + unsigned epnum:4;
58145 + unsigned bcnt:11;
58146 + unsigned dpid:2;
58147 +
58148 +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
58149 +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
58150 +
58151 +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
58152 +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
58153 +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
58154 + unsigned pktsts:4;
58155 + unsigned fn:4;
58156 + unsigned reserved25_31:7;
58157 + } b;
58158 +} device_grxsts_data_t;
58159 +
58160 +/**
58161 + * This union represents the bit fields in the Host Receive Status Read and
58162 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
58163 + * element then read out the bits using the <i>b</i>it elements.
58164 + */
58165 +typedef union host_grxsts_data {
58166 + /** raw register data */
58167 + uint32_t d32;
58168 + /** register bits */
58169 + struct {
58170 + unsigned chnum:4;
58171 + unsigned bcnt:11;
58172 + unsigned dpid:2;
58173 +
58174 + unsigned pktsts:4;
58175 +#define DWC_GRXSTS_PKTSTS_IN 0x2
58176 +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
58177 +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
58178 +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
58179 +
58180 + unsigned reserved21_31:11;
58181 + } b;
58182 +} host_grxsts_data_t;
58183 +
58184 +/**
58185 + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
58186 + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
58187 + * then read out the bits using the <i>b</i>it elements.
58188 + */
58189 +typedef union fifosize_data {
58190 + /** raw register data */
58191 + uint32_t d32;
58192 + /** register bits */
58193 + struct {
58194 + unsigned startaddr:16;
58195 + unsigned depth:16;
58196 + } b;
58197 +} fifosize_data_t;
58198 +
58199 +/**
58200 + * This union represents the bit fields in the Non-Periodic Transmit
58201 + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
58202 + * <i>d32</i> element then read out the bits using the <i>b</i>it
58203 + * elements.
58204 + */
58205 +typedef union gnptxsts_data {
58206 + /** raw register data */
58207 + uint32_t d32;
58208 + /** register bits */
58209 + struct {
58210 + unsigned nptxfspcavail:16;
58211 + unsigned nptxqspcavail:8;
58212 + /** Top of the Non-Periodic Transmit Request Queue
58213 + * - bit 24 - Terminate (Last entry for the selected
58214 + * channel/EP)
58215 + * - bits 26:25 - Token Type
58216 + * - 2'b00 - IN/OUT
58217 + * - 2'b01 - Zero Length OUT
58218 + * - 2'b10 - PING/Complete Split
58219 + * - 2'b11 - Channel Halt
58220 + * - bits 30:27 - Channel/EP Number
58221 + */
58222 + unsigned nptxqtop_terminate:1;
58223 + unsigned nptxqtop_token:2;
58224 + unsigned nptxqtop_chnep:4;
58225 + unsigned reserved:1;
58226 + } b;
58227 +} gnptxsts_data_t;
58228 +
58229 +/**
58230 + * This union represents the bit fields in the Transmit
58231 + * FIFO Status Register (DTXFSTS). Read the register into the
58232 + * <i>d32</i> element then read out the bits using the <i>b</i>it
58233 + * elements.
58234 + */
58235 +typedef union dtxfsts_data {
58236 + /** raw register data */
58237 + uint32_t d32;
58238 + /** register bits */
58239 + struct {
58240 + unsigned txfspcavail:16;
58241 + unsigned reserved:16;
58242 + } b;
58243 +} dtxfsts_data_t;
58244 +
58245 +/**
58246 + * This union represents the bit fields in the I2C Control Register
58247 + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
58248 + * bits using the <i>b</i>it elements.
58249 + */
58250 +typedef union gi2cctl_data {
58251 + /** raw register data */
58252 + uint32_t d32;
58253 + /** register bits */
58254 + struct {
58255 + unsigned rwdata:8;
58256 + unsigned regaddr:8;
58257 + unsigned addr:7;
58258 + unsigned i2cen:1;
58259 + unsigned ack:1;
58260 + unsigned i2csuspctl:1;
58261 + unsigned i2cdevaddr:2;
58262 + unsigned i2cdatse0:1;
58263 + unsigned reserved:1;
58264 + unsigned rw:1;
58265 + unsigned bsydne:1;
58266 + } b;
58267 +} gi2cctl_data_t;
58268 +
58269 +/**
58270 + * This union represents the bit fields in the PHY Vendor Control Register
58271 + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
58272 + * bits using the <i>b</i>it elements.
58273 + */
58274 +typedef union gpvndctl_data {
58275 + /** raw register data */
58276 + uint32_t d32;
58277 + /** register bits */
58278 + struct {
58279 + unsigned regdata:8;
58280 + unsigned vctrl:8;
58281 + unsigned regaddr16_21:6;
58282 + unsigned regwr:1;
58283 + unsigned reserved23_24:2;
58284 + unsigned newregreq:1;
58285 + unsigned vstsbsy:1;
58286 + unsigned vstsdone:1;
58287 + unsigned reserved28_30:3;
58288 + unsigned disulpidrvr:1;
58289 + } b;
58290 +} gpvndctl_data_t;
58291 +
58292 +/**
58293 + * This union represents the bit fields in the General Purpose
58294 + * Input/Output Register (GGPIO).
58295 + * Read the register into the <i>d32</i> element then read out the
58296 + * bits using the <i>b</i>it elements.
58297 + */
58298 +typedef union ggpio_data {
58299 + /** raw register data */
58300 + uint32_t d32;
58301 + /** register bits */
58302 + struct {
58303 + unsigned gpi:16;
58304 + unsigned gpo:16;
58305 + } b;
58306 +} ggpio_data_t;
58307 +
58308 +/**
58309 + * This union represents the bit fields in the User ID Register
58310 + * (GUID). Read the register into the <i>d32</i> element then read out the
58311 + * bits using the <i>b</i>it elements.
58312 + */
58313 +typedef union guid_data {
58314 + /** raw register data */
58315 + uint32_t d32;
58316 + /** register bits */
58317 + struct {
58318 + unsigned rwdata:32;
58319 + } b;
58320 +} guid_data_t;
58321 +
58322 +/**
58323 + * This union represents the bit fields in the Synopsys ID Register
58324 + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
58325 + * bits using the <i>b</i>it elements.
58326 + */
58327 +typedef union gsnpsid_data {
58328 + /** raw register data */
58329 + uint32_t d32;
58330 + /** register bits */
58331 + struct {
58332 + unsigned rwdata:32;
58333 + } b;
58334 +} gsnpsid_data_t;
58335 +
58336 +/**
58337 + * This union represents the bit fields in the User HW Config1
58338 + * Register. Read the register into the <i>d32</i> element then read
58339 + * out the bits using the <i>b</i>it elements.
58340 + */
58341 +typedef union hwcfg1_data {
58342 + /** raw register data */
58343 + uint32_t d32;
58344 + /** register bits */
58345 + struct {
58346 + unsigned ep_dir0:2;
58347 + unsigned ep_dir1:2;
58348 + unsigned ep_dir2:2;
58349 + unsigned ep_dir3:2;
58350 + unsigned ep_dir4:2;
58351 + unsigned ep_dir5:2;
58352 + unsigned ep_dir6:2;
58353 + unsigned ep_dir7:2;
58354 + unsigned ep_dir8:2;
58355 + unsigned ep_dir9:2;
58356 + unsigned ep_dir10:2;
58357 + unsigned ep_dir11:2;
58358 + unsigned ep_dir12:2;
58359 + unsigned ep_dir13:2;
58360 + unsigned ep_dir14:2;
58361 + unsigned ep_dir15:2;
58362 + } b;
58363 +} hwcfg1_data_t;
58364 +
58365 +/**
58366 + * This union represents the bit fields in the User HW Config2
58367 + * Register. Read the register into the <i>d32</i> element then read
58368 + * out the bits using the <i>b</i>it elements.
58369 + */
58370 +typedef union hwcfg2_data {
58371 + /** raw register data */
58372 + uint32_t d32;
58373 + /** register bits */
58374 + struct {
58375 + /* GHWCFG2 */
58376 + unsigned op_mode:3;
58377 +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
58378 +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
58379 +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
58380 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
58381 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
58382 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
58383 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
58384 +
58385 + unsigned architecture:2;
58386 + unsigned point2point:1;
58387 + unsigned hs_phy_type:2;
58388 +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
58389 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
58390 +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
58391 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
58392 +
58393 + unsigned fs_phy_type:2;
58394 + unsigned num_dev_ep:4;
58395 + unsigned num_host_chan:4;
58396 + unsigned perio_ep_supported:1;
58397 + unsigned dynamic_fifo:1;
58398 + unsigned multi_proc_int:1;
58399 + unsigned reserved21:1;
58400 + unsigned nonperio_tx_q_depth:2;
58401 + unsigned host_perio_tx_q_depth:2;
58402 + unsigned dev_token_q_depth:5;
58403 + unsigned otg_enable_ic_usb:1;
58404 + } b;
58405 +} hwcfg2_data_t;
58406 +
58407 +/**
58408 + * This union represents the bit fields in the User HW Config3
58409 + * Register. Read the register into the <i>d32</i> element then read
58410 + * out the bits using the <i>b</i>it elements.
58411 + */
58412 +typedef union hwcfg3_data {
58413 + /** raw register data */
58414 + uint32_t d32;
58415 + /** register bits */
58416 + struct {
58417 + /* GHWCFG3 */
58418 + unsigned xfer_size_cntr_width:4;
58419 + unsigned packet_size_cntr_width:3;
58420 + unsigned otg_func:1;
58421 + unsigned i2c:1;
58422 + unsigned vendor_ctrl_if:1;
58423 + unsigned optional_features:1;
58424 + unsigned synch_reset_type:1;
58425 + unsigned adp_supp:1;
58426 + unsigned otg_enable_hsic:1;
58427 + unsigned bc_support:1;
58428 + unsigned otg_lpm_en:1;
58429 + unsigned dfifo_depth:16;
58430 + } b;
58431 +} hwcfg3_data_t;
58432 +
58433 +/**
58434 + * This union represents the bit fields in the User HW Config4
58435 + * Register. Read the register into the <i>d32</i> element then read
58436 + * out the bits using the <i>b</i>it elements.
58437 + */
58438 +typedef union hwcfg4_data {
58439 + /** raw register data */
58440 + uint32_t d32;
58441 + /** register bits */
58442 + struct {
58443 + unsigned num_dev_perio_in_ep:4;
58444 + unsigned power_optimiz:1;
58445 + unsigned min_ahb_freq:1;
58446 + unsigned hiber:1;
58447 + unsigned xhiber:1;
58448 + unsigned reserved:6;
58449 + unsigned utmi_phy_data_width:2;
58450 + unsigned num_dev_mode_ctrl_ep:4;
58451 + unsigned iddig_filt_en:1;
58452 + unsigned vbus_valid_filt_en:1;
58453 + unsigned a_valid_filt_en:1;
58454 + unsigned b_valid_filt_en:1;
58455 + unsigned session_end_filt_en:1;
58456 + unsigned ded_fifo_en:1;
58457 + unsigned num_in_eps:4;
58458 + unsigned desc_dma:1;
58459 + unsigned desc_dma_dyn:1;
58460 + } b;
58461 +} hwcfg4_data_t;
58462 +
58463 +/**
58464 + * This union represents the bit fields of the Core LPM Configuration
58465 + * Register (GLPMCFG). Set the bits using bit fields then write
58466 + * the <i>d32</i> value to the register.
58467 + */
58468 +typedef union glpmctl_data {
58469 + /** raw register data */
58470 + uint32_t d32;
58471 + /** register bits */
58472 + struct {
58473 + /** LPM-Capable (LPMCap) (Device and Host)
58474 + * The application uses this bit to control
58475 + * the DWC_otg core LPM capabilities.
58476 + */
58477 + unsigned lpm_cap_en:1;
58478 + /** LPM response programmed by application (AppL1Res) (Device)
58479 + * Handshake response to LPM token pre-programmed
58480 + * by device application software.
58481 + */
58482 + unsigned appl_resp:1;
58483 + /** Host Initiated Resume Duration (HIRD) (Device and Host)
58484 + * In Host mode this field indicates the value of HIRD
58485 + * to be sent in an LPM transaction.
58486 + * In Device mode this field is updated with the
58487 + * Received LPM Token HIRD bmAttribute
58488 + * when an ACK/NYET/STALL response is sent
58489 + * to an LPM transaction.
58490 + */
58491 + unsigned hird:4;
58492 + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
58493 + * In Host mode this bit indicates the value of remote
58494 + * wake up to be sent in wIndex field of LPM transaction.
58495 + * In Device mode this field is updated with the
58496 + * Received LPM Token bRemoteWake bmAttribute
58497 + * when an ACK/NYET/STALL response is sent
58498 + * to an LPM transaction.
58499 + */
58500 + unsigned rem_wkup_en:1;
58501 + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
58502 + * The application uses this bit to control
58503 + * the utmi_sleep_n assertion to the PHY when in L1 state.
58504 + */
58505 + unsigned en_utmi_sleep:1;
58506 + /** HIRD Threshold (HIRD_Thres) (Device and Host)
58507 + */
58508 + unsigned hird_thres:5;
58509 + /** LPM Response (CoreL1Res) (Device and Host)
58510 + * In Host mode this bit contains handsake response to
58511 + * LPM transaction.
58512 + * In Device mode the response of the core to
58513 + * LPM transaction received is reflected in these two bits.
58514 + - 0x0 : ERROR (No handshake response)
58515 + - 0x1 : STALL
58516 + - 0x2 : NYET
58517 + - 0x3 : ACK
58518 + */
58519 + unsigned lpm_resp:2;
58520 + /** Port Sleep Status (SlpSts) (Device and Host)
58521 + * This bit is set as long as a Sleep condition
58522 + * is present on the USB bus.
58523 + */
58524 + unsigned prt_sleep_sts:1;
58525 + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
58526 + * Indicates that the application or host
58527 + * can start resume from Sleep state.
58528 + */
58529 + unsigned sleep_state_resumeok:1;
58530 + /** LPM channel Index (LPM_Chnl_Indx) (Host)
58531 + * The channel number on which the LPM transaction
58532 + * has to be applied while sending
58533 + * an LPM transaction to the local device.
58534 + */
58535 + unsigned lpm_chan_index:4;
58536 + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
58537 + * Number host retries that would be performed
58538 + * if the device response was not valid response.
58539 + */
58540 + unsigned retry_count:3;
58541 + /** Send LPM Transaction (SndLPM) (Host)
58542 + * When set by application software,
58543 + * an LPM transaction containing two tokens
58544 + * is sent.
58545 + */
58546 + unsigned send_lpm:1;
58547 + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
58548 + * Number of LPM Host Retries still remaining
58549 + * to be transmitted for the current LPM sequence
58550 + */
58551 + unsigned retry_count_sts:3;
58552 + unsigned reserved28_29:2;
58553 + /** In host mode once this bit is set, the host
58554 + * configures to drive the HSIC Idle state on the bus.
58555 + * It then waits for the device to initiate the Connect sequence.
58556 + * In device mode once this bit is set, the device waits for
58557 + * the HSIC Idle line state on the bus. Upon receving the Idle
58558 + * line state, it initiates the HSIC Connect sequence.
58559 + */
58560 + unsigned hsic_connect:1;
58561 + /** This bit overrides and functionally inverts
58562 + * the if_select_hsic input port signal.
58563 + */
58564 + unsigned inv_sel_hsic:1;
58565 + } b;
58566 +} glpmcfg_data_t;
58567 +
58568 +/**
58569 + * This union represents the bit fields of the Core ADP Timer, Control and
58570 + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
58571 + * the <i>d32</i> value to the register.
58572 + */
58573 +typedef union adpctl_data {
58574 + /** raw register data */
58575 + uint32_t d32;
58576 + /** register bits */
58577 + struct {
58578 + /** Probe Discharge (PRB_DSCHG)
58579 + * These bits set the times for TADP_DSCHG.
58580 + * These bits are defined as follows:
58581 + * 2'b00 - 4 msec
58582 + * 2'b01 - 8 msec
58583 + * 2'b10 - 16 msec
58584 + * 2'b11 - 32 msec
58585 + */
58586 + unsigned prb_dschg:2;
58587 + /** Probe Delta (PRB_DELTA)
58588 + * These bits set the resolution for RTIM value.
58589 + * The bits are defined in units of 32 kHz clock cycles as follows:
58590 + * 2'b00 - 1 cycles
58591 + * 2'b01 - 2 cycles
58592 + * 2'b10 - 3 cycles
58593 + * 2'b11 - 4 cycles
58594 + * For example if this value is chosen to 2'b01, it means that RTIM
58595 + * increments for every 3(three) 32Khz clock cycles.
58596 + */
58597 + unsigned prb_delta:2;
58598 + /** Probe Period (PRB_PER)
58599 + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
58600 + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
58601 + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
58602 + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
58603 + * 2'b11 - Reserved
58604 + */
58605 + unsigned prb_per:2;
58606 + /** These bits capture the latest time it took for VBUS to ramp from
58607 + * VADP_SINK to VADP_PRB.
58608 + * 0x000 - 1 cycles
58609 + * 0x001 - 2 cycles
58610 + * 0x002 - 3 cycles
58611 + * etc
58612 + * 0x7FF - 2048 cycles
58613 + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
58614 + */
58615 + unsigned rtim:11;
58616 + /** Enable Probe (EnaPrb)
58617 + * When programmed to 1'b1, the core performs a probe operation.
58618 + * This bit is valid only if OTG_Ver = 1'b1.
58619 + */
58620 + unsigned enaprb:1;
58621 + /** Enable Sense (EnaSns)
58622 + * When programmed to 1'b1, the core performs a Sense operation.
58623 + * This bit is valid only if OTG_Ver = 1'b1.
58624 + */
58625 + unsigned enasns:1;
58626 + /** ADP Reset (ADPRes)
58627 + * When set, ADP controller is reset.
58628 + * This bit is valid only if OTG_Ver = 1'b1.
58629 + */
58630 + unsigned adpres:1;
58631 + /** ADP Enable (ADPEn)
58632 + * When set, the core performs either ADP probing or sensing
58633 + * based on EnaPrb or EnaSns.
58634 + * This bit is valid only if OTG_Ver = 1'b1.
58635 + */
58636 + unsigned adpen:1;
58637 + /** ADP Probe Interrupt (ADP_PRB_INT)
58638 + * When this bit is set, it means that the VBUS
58639 + * voltage is greater than VADP_PRB or VADP_PRB is reached.
58640 + * This bit is valid only if OTG_Ver = 1'b1.
58641 + */
58642 + unsigned adp_prb_int:1;
58643 + /**
58644 + * ADP Sense Interrupt (ADP_SNS_INT)
58645 + * When this bit is set, it means that the VBUS voltage is greater than
58646 + * VADP_SNS value or VADP_SNS is reached.
58647 + * This bit is valid only if OTG_Ver = 1'b1.
58648 + */
58649 + unsigned adp_sns_int:1;
58650 + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
58651 + * This bit is relevant only for an ADP probe.
58652 + * When this bit is set, it means that the ramp time has
58653 + * completed ie ADPCTL.RTIM has reached its terminal value
58654 + * of 0x7FF. This is a debug feature that allows software
58655 + * to read the ramp time after each cycle.
58656 + * This bit is valid only if OTG_Ver = 1'b1.
58657 + */
58658 + unsigned adp_tmout_int:1;
58659 + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
58660 + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
58661 + * This bit is valid only if OTG_Ver = 1'b1.
58662 + */
58663 + unsigned adp_prb_int_msk:1;
58664 + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
58665 + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
58666 + * This bit is valid only if OTG_Ver = 1'b1.
58667 + */
58668 + unsigned adp_sns_int_msk:1;
58669 + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
58670 + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
58671 + * This bit is valid only if OTG_Ver = 1'b1.
58672 + */
58673 + unsigned adp_tmout_int_msk:1;
58674 + /** Access Request
58675 + * 2'b00 - Read/Write Valid (updated by the core)
58676 + * 2'b01 - Read
58677 + * 2'b00 - Write
58678 + * 2'b00 - Reserved
58679 + */
58680 + unsigned ar:2;
58681 + /** Reserved */
58682 + unsigned reserved29_31:3;
58683 + } b;
58684 +} adpctl_data_t;
58685 +
58686 +////////////////////////////////////////////
58687 +// Device Registers
58688 +/**
58689 + * Device Global Registers. <i>Offsets 800h-BFFh</i>
58690 + *
58691 + * The following structures define the size and relative field offsets
58692 + * for the Device Mode Registers.
58693 + *
58694 + * <i>These registers are visible only in Device mode and must not be
58695 + * accessed in Host mode, as the results are unknown.</i>
58696 + */
58697 +typedef struct dwc_otg_dev_global_regs {
58698 + /** Device Configuration Register. <i>Offset 800h</i> */
58699 + volatile uint32_t dcfg;
58700 + /** Device Control Register. <i>Offset: 804h</i> */
58701 + volatile uint32_t dctl;
58702 + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
58703 + volatile uint32_t dsts;
58704 + /** Reserved. <i>Offset: 80Ch</i> */
58705 + uint32_t unused;
58706 + /** Device IN Endpoint Common Interrupt Mask
58707 + * Register. <i>Offset: 810h</i> */
58708 + volatile uint32_t diepmsk;
58709 + /** Device OUT Endpoint Common Interrupt Mask
58710 + * Register. <i>Offset: 814h</i> */
58711 + volatile uint32_t doepmsk;
58712 + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
58713 + volatile uint32_t daint;
58714 + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
58715 + * 81Ch</i> */
58716 + volatile uint32_t daintmsk;
58717 + /** Device IN Token Queue Read Register-1 (Read Only).
58718 + * <i>Offset: 820h</i> */
58719 + volatile uint32_t dtknqr1;
58720 + /** Device IN Token Queue Read Register-2 (Read Only).
58721 + * <i>Offset: 824h</i> */
58722 + volatile uint32_t dtknqr2;
58723 + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
58724 + volatile uint32_t dvbusdis;
58725 + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
58726 + volatile uint32_t dvbuspulse;
58727 + /** Device IN Token Queue Read Register-3 (Read Only). /
58728 + * Device Thresholding control register (Read/Write)
58729 + * <i>Offset: 830h</i> */
58730 + volatile uint32_t dtknqr3_dthrctl;
58731 + /** Device IN Token Queue Read Register-4 (Read Only). /
58732 + * Device IN EPs empty Inr. Mask Register (Read/Write)
58733 + * <i>Offset: 834h</i> */
58734 + volatile uint32_t dtknqr4_fifoemptymsk;
58735 + /** Device Each Endpoint Interrupt Register (Read Only). /
58736 + * <i>Offset: 838h</i> */
58737 + volatile uint32_t deachint;
58738 + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
58739 + * <i>Offset: 83Ch</i> */
58740 + volatile uint32_t deachintmsk;
58741 + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
58742 + * <i>Offset: 840h</i> */
58743 + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
58744 + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
58745 + * <i>Offset: 880h</i> */
58746 + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
58747 +} dwc_otg_device_global_regs_t;
58748 +
58749 +/**
58750 + * This union represents the bit fields in the Device Configuration
58751 + * Register. Read the register into the <i>d32</i> member then
58752 + * set/clear the bits using the <i>b</i>it elements. Write the
58753 + * <i>d32</i> member to the dcfg register.
58754 + */
58755 +typedef union dcfg_data {
58756 + /** raw register data */
58757 + uint32_t d32;
58758 + /** register bits */
58759 + struct {
58760 + /** Device Speed */
58761 + unsigned devspd:2;
58762 + /** Non Zero Length Status OUT Handshake */
58763 + unsigned nzstsouthshk:1;
58764 +#define DWC_DCFG_SEND_STALL 1
58765 +
58766 + unsigned ena32khzs:1;
58767 + /** Device Addresses */
58768 + unsigned devaddr:7;
58769 + /** Periodic Frame Interval */
58770 + unsigned perfrint:2;
58771 +#define DWC_DCFG_FRAME_INTERVAL_80 0
58772 +#define DWC_DCFG_FRAME_INTERVAL_85 1
58773 +#define DWC_DCFG_FRAME_INTERVAL_90 2
58774 +#define DWC_DCFG_FRAME_INTERVAL_95 3
58775 +
58776 + /** Enable Device OUT NAK for bulk in DDMA mode */
58777 + unsigned endevoutnak:1;
58778 +
58779 + unsigned reserved14_17:4;
58780 + /** In Endpoint Mis-match count */
58781 + unsigned epmscnt:5;
58782 + /** Enable Descriptor DMA in Device mode */
58783 + unsigned descdma:1;
58784 + unsigned perschintvl:2;
58785 + unsigned resvalid:6;
58786 + } b;
58787 +} dcfg_data_t;
58788 +
58789 +/**
58790 + * This union represents the bit fields in the Device Control
58791 + * Register. Read the register into the <i>d32</i> member then
58792 + * set/clear the bits using the <i>b</i>it elements.
58793 + */
58794 +typedef union dctl_data {
58795 + /** raw register data */
58796 + uint32_t d32;
58797 + /** register bits */
58798 + struct {
58799 + /** Remote Wakeup */
58800 + unsigned rmtwkupsig:1;
58801 + /** Soft Disconnect */
58802 + unsigned sftdiscon:1;
58803 + /** Global Non-Periodic IN NAK Status */
58804 + unsigned gnpinnaksts:1;
58805 + /** Global OUT NAK Status */
58806 + unsigned goutnaksts:1;
58807 + /** Test Control */
58808 + unsigned tstctl:3;
58809 + /** Set Global Non-Periodic IN NAK */
58810 + unsigned sgnpinnak:1;
58811 + /** Clear Global Non-Periodic IN NAK */
58812 + unsigned cgnpinnak:1;
58813 + /** Set Global OUT NAK */
58814 + unsigned sgoutnak:1;
58815 + /** Clear Global OUT NAK */
58816 + unsigned cgoutnak:1;
58817 + /** Power-On Programming Done */
58818 + unsigned pwronprgdone:1;
58819 + /** Reserved */
58820 + unsigned reserved:1;
58821 + /** Global Multi Count */
58822 + unsigned gmc:2;
58823 + /** Ignore Frame Number for ISOC EPs */
58824 + unsigned ifrmnum:1;
58825 + /** NAK on Babble */
58826 + unsigned nakonbble:1;
58827 + /** Enable Continue on BNA */
58828 + unsigned encontonbna:1;
58829 +
58830 + unsigned reserved18_31:14;
58831 + } b;
58832 +} dctl_data_t;
58833 +
58834 +/**
58835 + * This union represents the bit fields in the Device Status
58836 + * Register. Read the register into the <i>d32</i> member then
58837 + * set/clear the bits using the <i>b</i>it elements.
58838 + */
58839 +typedef union dsts_data {
58840 + /** raw register data */
58841 + uint32_t d32;
58842 + /** register bits */
58843 + struct {
58844 + /** Suspend Status */
58845 + unsigned suspsts:1;
58846 + /** Enumerated Speed */
58847 + unsigned enumspd:2;
58848 +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
58849 +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
58850 +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
58851 +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
58852 + /** Erratic Error */
58853 + unsigned errticerr:1;
58854 + unsigned reserved4_7:4;
58855 + /** Frame or Microframe Number of the received SOF */
58856 + unsigned soffn:14;
58857 + unsigned reserved22_31:10;
58858 + } b;
58859 +} dsts_data_t;
58860 +
58861 +/**
58862 + * This union represents the bit fields in the Device IN EP Interrupt
58863 + * Register and the Device IN EP Common Mask Register.
58864 + *
58865 + * - Read the register into the <i>d32</i> member then set/clear the
58866 + * bits using the <i>b</i>it elements.
58867 + */
58868 +typedef union diepint_data {
58869 + /** raw register data */
58870 + uint32_t d32;
58871 + /** register bits */
58872 + struct {
58873 + /** Transfer complete mask */
58874 + unsigned xfercompl:1;
58875 + /** Endpoint disable mask */
58876 + unsigned epdisabled:1;
58877 + /** AHB Error mask */
58878 + unsigned ahberr:1;
58879 + /** TimeOUT Handshake mask (non-ISOC EPs) */
58880 + unsigned timeout:1;
58881 + /** IN Token received with TxF Empty mask */
58882 + unsigned intktxfemp:1;
58883 + /** IN Token Received with EP mismatch mask */
58884 + unsigned intknepmis:1;
58885 + /** IN Endpoint NAK Effective mask */
58886 + unsigned inepnakeff:1;
58887 + /** Reserved */
58888 + unsigned emptyintr:1;
58889 +
58890 + unsigned txfifoundrn:1;
58891 +
58892 + /** BNA Interrupt mask */
58893 + unsigned bna:1;
58894 +
58895 + unsigned reserved10_12:3;
58896 + /** BNA Interrupt mask */
58897 + unsigned nak:1;
58898 +
58899 + unsigned reserved14_31:18;
58900 + } b;
58901 +} diepint_data_t;
58902 +
58903 +/**
58904 + * This union represents the bit fields in the Device IN EP
58905 + * Common/Dedicated Interrupt Mask Register.
58906 + */
58907 +typedef union diepint_data diepmsk_data_t;
58908 +
58909 +/**
58910 + * This union represents the bit fields in the Device OUT EP Interrupt
58911 + * Registerand Device OUT EP Common Interrupt Mask Register.
58912 + *
58913 + * - Read the register into the <i>d32</i> member then set/clear the
58914 + * bits using the <i>b</i>it elements.
58915 + */
58916 +typedef union doepint_data {
58917 + /** raw register data */
58918 + uint32_t d32;
58919 + /** register bits */
58920 + struct {
58921 + /** Transfer complete */
58922 + unsigned xfercompl:1;
58923 + /** Endpoint disable */
58924 + unsigned epdisabled:1;
58925 + /** AHB Error */
58926 + unsigned ahberr:1;
58927 + /** Setup Phase Done (contorl EPs) */
58928 + unsigned setup:1;
58929 + /** OUT Token Received when Endpoint Disabled */
58930 + unsigned outtknepdis:1;
58931 +
58932 + unsigned stsphsercvd:1;
58933 + /** Back-to-Back SETUP Packets Received */
58934 + unsigned back2backsetup:1;
58935 +
58936 + unsigned reserved7:1;
58937 + /** OUT packet Error */
58938 + unsigned outpkterr:1;
58939 + /** BNA Interrupt */
58940 + unsigned bna:1;
58941 +
58942 + unsigned reserved10:1;
58943 + /** Packet Drop Status */
58944 + unsigned pktdrpsts:1;
58945 + /** Babble Interrupt */
58946 + unsigned babble:1;
58947 + /** NAK Interrupt */
58948 + unsigned nak:1;
58949 + /** NYET Interrupt */
58950 + unsigned nyet:1;
58951 + /** Bit indicating setup packet received */
58952 + unsigned sr:1;
58953 +
58954 + unsigned reserved16_31:16;
58955 + } b;
58956 +} doepint_data_t;
58957 +
58958 +/**
58959 + * This union represents the bit fields in the Device OUT EP
58960 + * Common/Dedicated Interrupt Mask Register.
58961 + */
58962 +typedef union doepint_data doepmsk_data_t;
58963 +
58964 +/**
58965 + * This union represents the bit fields in the Device All EP Interrupt
58966 + * and Mask Registers.
58967 + * - Read the register into the <i>d32</i> member then set/clear the
58968 + * bits using the <i>b</i>it elements.
58969 + */
58970 +typedef union daint_data {
58971 + /** raw register data */
58972 + uint32_t d32;
58973 + /** register bits */
58974 + struct {
58975 + /** IN Endpoint bits */
58976 + unsigned in:16;
58977 + /** OUT Endpoint bits */
58978 + unsigned out:16;
58979 + } ep;
58980 + struct {
58981 + /** IN Endpoint bits */
58982 + unsigned inep0:1;
58983 + unsigned inep1:1;
58984 + unsigned inep2:1;
58985 + unsigned inep3:1;
58986 + unsigned inep4:1;
58987 + unsigned inep5:1;
58988 + unsigned inep6:1;
58989 + unsigned inep7:1;
58990 + unsigned inep8:1;
58991 + unsigned inep9:1;
58992 + unsigned inep10:1;
58993 + unsigned inep11:1;
58994 + unsigned inep12:1;
58995 + unsigned inep13:1;
58996 + unsigned inep14:1;
58997 + unsigned inep15:1;
58998 + /** OUT Endpoint bits */
58999 + unsigned outep0:1;
59000 + unsigned outep1:1;
59001 + unsigned outep2:1;
59002 + unsigned outep3:1;
59003 + unsigned outep4:1;
59004 + unsigned outep5:1;
59005 + unsigned outep6:1;
59006 + unsigned outep7:1;
59007 + unsigned outep8:1;
59008 + unsigned outep9:1;
59009 + unsigned outep10:1;
59010 + unsigned outep11:1;
59011 + unsigned outep12:1;
59012 + unsigned outep13:1;
59013 + unsigned outep14:1;
59014 + unsigned outep15:1;
59015 + } b;
59016 +} daint_data_t;
59017 +
59018 +/**
59019 + * This union represents the bit fields in the Device IN Token Queue
59020 + * Read Registers.
59021 + * - Read the register into the <i>d32</i> member.
59022 + * - READ-ONLY Register
59023 + */
59024 +typedef union dtknq1_data {
59025 + /** raw register data */
59026 + uint32_t d32;
59027 + /** register bits */
59028 + struct {
59029 + /** In Token Queue Write Pointer */
59030 + unsigned intknwptr:5;
59031 + /** Reserved */
59032 + unsigned reserved05_06:2;
59033 + /** write pointer has wrapped. */
59034 + unsigned wrap_bit:1;
59035 + /** EP Numbers of IN Tokens 0 ... 4 */
59036 + unsigned epnums0_5:24;
59037 + } b;
59038 +} dtknq1_data_t;
59039 +
59040 +/**
59041 + * This union represents Threshold control Register
59042 + * - Read and write the register into the <i>d32</i> member.
59043 + * - READ-WRITABLE Register
59044 + */
59045 +typedef union dthrctl_data {
59046 + /** raw register data */
59047 + uint32_t d32;
59048 + /** register bits */
59049 + struct {
59050 + /** non ISO Tx Thr. Enable */
59051 + unsigned non_iso_thr_en:1;
59052 + /** ISO Tx Thr. Enable */
59053 + unsigned iso_thr_en:1;
59054 + /** Tx Thr. Length */
59055 + unsigned tx_thr_len:9;
59056 + /** AHB Threshold ratio */
59057 + unsigned ahb_thr_ratio:2;
59058 + /** Reserved */
59059 + unsigned reserved13_15:3;
59060 + /** Rx Thr. Enable */
59061 + unsigned rx_thr_en:1;
59062 + /** Rx Thr. Length */
59063 + unsigned rx_thr_len:9;
59064 + unsigned reserved26:1;
59065 + /** Arbiter Parking Enable*/
59066 + unsigned arbprken:1;
59067 + /** Reserved */
59068 + unsigned reserved28_31:4;
59069 + } b;
59070 +} dthrctl_data_t;
59071 +
59072 +/**
59073 + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
59074 + * 900h-AFCh</i>
59075 + *
59076 + * There will be one set of endpoint registers per logical endpoint
59077 + * implemented.
59078 + *
59079 + * <i>These registers are visible only in Device mode and must not be
59080 + * accessed in Host mode, as the results are unknown.</i>
59081 + */
59082 +typedef struct dwc_otg_dev_in_ep_regs {
59083 + /** Device IN Endpoint Control Register. <i>Offset:900h +
59084 + * (ep_num * 20h) + 00h</i> */
59085 + volatile uint32_t diepctl;
59086 + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
59087 + uint32_t reserved04;
59088 + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
59089 + * (ep_num * 20h) + 08h</i> */
59090 + volatile uint32_t diepint;
59091 + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
59092 + uint32_t reserved0C;
59093 + /** Device IN Endpoint Transfer Size
59094 + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
59095 + volatile uint32_t dieptsiz;
59096 + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
59097 + * (ep_num * 20h) + 14h</i> */
59098 + volatile uint32_t diepdma;
59099 + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
59100 + * (ep_num * 20h) + 18h</i> */
59101 + volatile uint32_t dtxfsts;
59102 + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
59103 + * (ep_num * 20h) + 1Ch</i> */
59104 + volatile uint32_t diepdmab;
59105 +} dwc_otg_dev_in_ep_regs_t;
59106 +
59107 +/**
59108 + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
59109 + * B00h-CFCh</i>
59110 + *
59111 + * There will be one set of endpoint registers per logical endpoint
59112 + * implemented.
59113 + *
59114 + * <i>These registers are visible only in Device mode and must not be
59115 + * accessed in Host mode, as the results are unknown.</i>
59116 + */
59117 +typedef struct dwc_otg_dev_out_ep_regs {
59118 + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
59119 + * (ep_num * 20h) + 00h</i> */
59120 + volatile uint32_t doepctl;
59121 + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
59122 + uint32_t reserved04;
59123 + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
59124 + * (ep_num * 20h) + 08h</i> */
59125 + volatile uint32_t doepint;
59126 + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
59127 + uint32_t reserved0C;
59128 + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
59129 + * B00h + (ep_num * 20h) + 10h</i> */
59130 + volatile uint32_t doeptsiz;
59131 + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
59132 + * + (ep_num * 20h) + 14h</i> */
59133 + volatile uint32_t doepdma;
59134 + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
59135 + uint32_t unused;
59136 + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
59137 + * + (ep_num * 20h) + 1Ch</i> */
59138 + uint32_t doepdmab;
59139 +} dwc_otg_dev_out_ep_regs_t;
59140 +
59141 +/**
59142 + * This union represents the bit fields in the Device EP Control
59143 + * Register. Read the register into the <i>d32</i> member then
59144 + * set/clear the bits using the <i>b</i>it elements.
59145 + */
59146 +typedef union depctl_data {
59147 + /** raw register data */
59148 + uint32_t d32;
59149 + /** register bits */
59150 + struct {
59151 + /** Maximum Packet Size
59152 + * IN/OUT EPn
59153 + * IN/OUT EP0 - 2 bits
59154 + * 2'b00: 64 Bytes
59155 + * 2'b01: 32
59156 + * 2'b10: 16
59157 + * 2'b11: 8 */
59158 + unsigned mps:11;
59159 +#define DWC_DEP0CTL_MPS_64 0
59160 +#define DWC_DEP0CTL_MPS_32 1
59161 +#define DWC_DEP0CTL_MPS_16 2
59162 +#define DWC_DEP0CTL_MPS_8 3
59163 +
59164 + /** Next Endpoint
59165 + * IN EPn/IN EP0
59166 + * OUT EPn/OUT EP0 - reserved */
59167 + unsigned nextep:4;
59168 +
59169 + /** USB Active Endpoint */
59170 + unsigned usbactep:1;
59171 +
59172 + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
59173 + * This field contains the PID of the packet going to
59174 + * be received or transmitted on this endpoint. The
59175 + * application should program the PID of the first
59176 + * packet going to be received or transmitted on this
59177 + * endpoint , after the endpoint is
59178 + * activated. Application use the SetD1PID and
59179 + * SetD0PID fields of this register to program either
59180 + * D0 or D1 PID.
59181 + *
59182 + * The encoding for this field is
59183 + * - 0: D0
59184 + * - 1: D1
59185 + */
59186 + unsigned dpid:1;
59187 +
59188 + /** NAK Status */
59189 + unsigned naksts:1;
59190 +
59191 + /** Endpoint Type
59192 + * 2'b00: Control
59193 + * 2'b01: Isochronous
59194 + * 2'b10: Bulk
59195 + * 2'b11: Interrupt */
59196 + unsigned eptype:2;
59197 +
59198 + /** Snoop Mode
59199 + * OUT EPn/OUT EP0
59200 + * IN EPn/IN EP0 - reserved */
59201 + unsigned snp:1;
59202 +
59203 + /** Stall Handshake */
59204 + unsigned stall:1;
59205 +
59206 + /** Tx Fifo Number
59207 + * IN EPn/IN EP0
59208 + * OUT EPn/OUT EP0 - reserved */
59209 + unsigned txfnum:4;
59210 +
59211 + /** Clear NAK */
59212 + unsigned cnak:1;
59213 + /** Set NAK */
59214 + unsigned snak:1;
59215 + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
59216 + * Writing to this field sets the Endpoint DPID (DPID)
59217 + * field in this register to DATA0. Set Even
59218 + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
59219 + * Writing to this field sets the Even/Odd
59220 + * (micro)frame (EO_FrNum) field to even (micro)
59221 + * frame.
59222 + */
59223 + unsigned setd0pid:1;
59224 + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
59225 + * Writing to this field sets the Endpoint DPID (DPID)
59226 + * field in this register to DATA1 Set Odd
59227 + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
59228 + * Writing to this field sets the Even/Odd
59229 + * (micro)frame (EO_FrNum) field to odd (micro) frame.
59230 + */
59231 + unsigned setd1pid:1;
59232 +
59233 + /** Endpoint Disable */
59234 + unsigned epdis:1;
59235 + /** Endpoint Enable */
59236 + unsigned epena:1;
59237 + } b;
59238 +} depctl_data_t;
59239 +
59240 +/**
59241 + * This union represents the bit fields in the Device EP Transfer
59242 + * Size Register. Read the register into the <i>d32</i> member then
59243 + * set/clear the bits using the <i>b</i>it elements.
59244 + */
59245 +typedef union deptsiz_data {
59246 + /** raw register data */
59247 + uint32_t d32;
59248 + /** register bits */
59249 + struct {
59250 + /** Transfer size */
59251 + unsigned xfersize:19;
59252 +/** Max packet count for EP (pow(2,10)-1) */
59253 +#define MAX_PKT_CNT 1023
59254 + /** Packet Count */
59255 + unsigned pktcnt:10;
59256 + /** Multi Count - Periodic IN endpoints */
59257 + unsigned mc:2;
59258 + unsigned reserved:1;
59259 + } b;
59260 +} deptsiz_data_t;
59261 +
59262 +/**
59263 + * This union represents the bit fields in the Device EP 0 Transfer
59264 + * Size Register. Read the register into the <i>d32</i> member then
59265 + * set/clear the bits using the <i>b</i>it elements.
59266 + */
59267 +typedef union deptsiz0_data {
59268 + /** raw register data */
59269 + uint32_t d32;
59270 + /** register bits */
59271 + struct {
59272 + /** Transfer size */
59273 + unsigned xfersize:7;
59274 + /** Reserved */
59275 + unsigned reserved7_18:12;
59276 + /** Packet Count */
59277 + unsigned pktcnt:2;
59278 + /** Reserved */
59279 + unsigned reserved21_28:8;
59280 + /**Setup Packet Count (DOEPTSIZ0 Only) */
59281 + unsigned supcnt:2;
59282 + unsigned reserved31;
59283 + } b;
59284 +} deptsiz0_data_t;
59285 +
59286 +/////////////////////////////////////////////////
59287 +// DMA Descriptor Specific Structures
59288 +//
59289 +
59290 +/** Buffer status definitions */
59291 +
59292 +#define BS_HOST_READY 0x0
59293 +#define BS_DMA_BUSY 0x1
59294 +#define BS_DMA_DONE 0x2
59295 +#define BS_HOST_BUSY 0x3
59296 +
59297 +/** Receive/Transmit status definitions */
59298 +
59299 +#define RTS_SUCCESS 0x0
59300 +#define RTS_BUFFLUSH 0x1
59301 +#define RTS_RESERVED 0x2
59302 +#define RTS_BUFERR 0x3
59303 +
59304 +/**
59305 + * This union represents the bit fields in the DMA Descriptor
59306 + * status quadlet. Read the quadlet into the <i>d32</i> member then
59307 + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
59308 + * <i>b_iso_in</i> elements.
59309 + */
59310 +typedef union dev_dma_desc_sts {
59311 + /** raw register data */
59312 + uint32_t d32;
59313 + /** quadlet bits */
59314 + struct {
59315 + /** Received number of bytes */
59316 + unsigned bytes:16;
59317 + /** NAK bit - only for OUT EPs */
59318 + unsigned nak:1;
59319 + unsigned reserved17_22:6;
59320 + /** Multiple Transfer - only for OUT EPs */
59321 + unsigned mtrf:1;
59322 + /** Setup Packet received - only for OUT EPs */
59323 + unsigned sr:1;
59324 + /** Interrupt On Complete */
59325 + unsigned ioc:1;
59326 + /** Short Packet */
59327 + unsigned sp:1;
59328 + /** Last */
59329 + unsigned l:1;
59330 + /** Receive Status */
59331 + unsigned sts:2;
59332 + /** Buffer Status */
59333 + unsigned bs:2;
59334 + } b;
59335 +
59336 +//#ifdef DWC_EN_ISOC
59337 + /** iso out quadlet bits */
59338 + struct {
59339 + /** Received number of bytes */
59340 + unsigned rxbytes:11;
59341 +
59342 + unsigned reserved11:1;
59343 + /** Frame Number */
59344 + unsigned framenum:11;
59345 + /** Received ISO Data PID */
59346 + unsigned pid:2;
59347 + /** Interrupt On Complete */
59348 + unsigned ioc:1;
59349 + /** Short Packet */
59350 + unsigned sp:1;
59351 + /** Last */
59352 + unsigned l:1;
59353 + /** Receive Status */
59354 + unsigned rxsts:2;
59355 + /** Buffer Status */
59356 + unsigned bs:2;
59357 + } b_iso_out;
59358 +
59359 + /** iso in quadlet bits */
59360 + struct {
59361 + /** Transmited number of bytes */
59362 + unsigned txbytes:12;
59363 + /** Frame Number */
59364 + unsigned framenum:11;
59365 + /** Transmited ISO Data PID */
59366 + unsigned pid:2;
59367 + /** Interrupt On Complete */
59368 + unsigned ioc:1;
59369 + /** Short Packet */
59370 + unsigned sp:1;
59371 + /** Last */
59372 + unsigned l:1;
59373 + /** Transmit Status */
59374 + unsigned txsts:2;
59375 + /** Buffer Status */
59376 + unsigned bs:2;
59377 + } b_iso_in;
59378 +//#endif /* DWC_EN_ISOC */
59379 +} dev_dma_desc_sts_t;
59380 +
59381 +/**
59382 + * DMA Descriptor structure
59383 + *
59384 + * DMA Descriptor structure contains two quadlets:
59385 + * Status quadlet and Data buffer pointer.
59386 + */
59387 +typedef struct dwc_otg_dev_dma_desc {
59388 + /** DMA Descriptor status quadlet */
59389 + dev_dma_desc_sts_t status;
59390 + /** DMA Descriptor data buffer pointer */
59391 + uint32_t buf;
59392 +} dwc_otg_dev_dma_desc_t;
59393 +
59394 +/**
59395 + * The dwc_otg_dev_if structure contains information needed to manage
59396 + * the DWC_otg controller acting in device mode. It represents the
59397 + * programming view of the device-specific aspects of the controller.
59398 + */
59399 +typedef struct dwc_otg_dev_if {
59400 + /** Pointer to device Global registers.
59401 + * Device Global Registers starting at offset 800h
59402 + */
59403 + dwc_otg_device_global_regs_t *dev_global_regs;
59404 +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
59405 +
59406 + /**
59407 + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
59408 + */
59409 + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
59410 +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
59411 +#define DWC_EP_REG_OFFSET 0x20
59412 +
59413 + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
59414 + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
59415 +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
59416 +
59417 + /* Device configuration information */
59418 + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
59419 + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
59420 + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
59421 +
59422 + /** Size of periodic FIFOs (Bytes) */
59423 + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
59424 +
59425 + /** Size of Tx FIFOs (Bytes) */
59426 + uint16_t tx_fifo_size[MAX_TX_FIFOS];
59427 +
59428 + /** Thresholding enable flags and length varaiables **/
59429 + uint16_t rx_thr_en;
59430 + uint16_t iso_tx_thr_en;
59431 + uint16_t non_iso_tx_thr_en;
59432 +
59433 + uint16_t rx_thr_length;
59434 + uint16_t tx_thr_length;
59435 +
59436 + /**
59437 + * Pointers to the DMA Descriptors for EP0 Control
59438 + * transfers (virtual and physical)
59439 + */
59440 +
59441 + /** 2 descriptors for SETUP packets */
59442 + dwc_dma_t dma_setup_desc_addr[2];
59443 + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
59444 +
59445 + /** Pointer to Descriptor with latest SETUP packet */
59446 + dwc_otg_dev_dma_desc_t *psetup;
59447 +
59448 + /** Index of current SETUP handler descriptor */
59449 + uint32_t setup_desc_index;
59450 +
59451 + /** Descriptor for Data In or Status In phases */
59452 + dwc_dma_t dma_in_desc_addr;
59453 + dwc_otg_dev_dma_desc_t *in_desc_addr;
59454 +
59455 + /** Descriptor for Data Out or Status Out phases */
59456 + dwc_dma_t dma_out_desc_addr;
59457 + dwc_otg_dev_dma_desc_t *out_desc_addr;
59458 +
59459 + /** Setup Packet Detected - if set clear NAK when queueing */
59460 + uint32_t spd;
59461 + /** Isoc ep pointer on which incomplete happens */
59462 + void *isoc_ep;
59463 +
59464 +} dwc_otg_dev_if_t;
59465 +
59466 +/////////////////////////////////////////////////
59467 +// Host Mode Register Structures
59468 +//
59469 +/**
59470 + * The Host Global Registers structure defines the size and relative
59471 + * field offsets for the Host Mode Global Registers. Host Global
59472 + * Registers offsets 400h-7FFh.
59473 +*/
59474 +typedef struct dwc_otg_host_global_regs {
59475 + /** Host Configuration Register. <i>Offset: 400h</i> */
59476 + volatile uint32_t hcfg;
59477 + /** Host Frame Interval Register. <i>Offset: 404h</i> */
59478 + volatile uint32_t hfir;
59479 + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
59480 + volatile uint32_t hfnum;
59481 + /** Reserved. <i>Offset: 40Ch</i> */
59482 + uint32_t reserved40C;
59483 + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
59484 + volatile uint32_t hptxsts;
59485 + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
59486 + volatile uint32_t haint;
59487 + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
59488 + volatile uint32_t haintmsk;
59489 + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
59490 + volatile uint32_t hflbaddr;
59491 +} dwc_otg_host_global_regs_t;
59492 +
59493 +/**
59494 + * This union represents the bit fields in the Host Configuration Register.
59495 + * Read the register into the <i>d32</i> member then set/clear the bits using
59496 + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
59497 + */
59498 +typedef union hcfg_data {
59499 + /** raw register data */
59500 + uint32_t d32;
59501 +
59502 + /** register bits */
59503 + struct {
59504 + /** FS/LS Phy Clock Select */
59505 + unsigned fslspclksel:2;
59506 +#define DWC_HCFG_30_60_MHZ 0
59507 +#define DWC_HCFG_48_MHZ 1
59508 +#define DWC_HCFG_6_MHZ 2
59509 +
59510 + /** FS/LS Only Support */
59511 + unsigned fslssupp:1;
59512 + unsigned reserved3_6:4;
59513 + /** Enable 32-KHz Suspend Mode */
59514 + unsigned ena32khzs:1;
59515 + /** Resume Validation Periiod */
59516 + unsigned resvalid:8;
59517 + unsigned reserved16_22:7;
59518 + /** Enable Scatter/gather DMA in Host mode */
59519 + unsigned descdma:1;
59520 + /** Frame List Entries */
59521 + unsigned frlisten:2;
59522 + /** Enable Periodic Scheduling */
59523 + unsigned perschedena:1;
59524 + unsigned reserved27_30:4;
59525 + unsigned modechtimen:1;
59526 + } b;
59527 +} hcfg_data_t;
59528 +
59529 +/**
59530 + * This union represents the bit fields in the Host Frame Remaing/Number
59531 + * Register.
59532 + */
59533 +typedef union hfir_data {
59534 + /** raw register data */
59535 + uint32_t d32;
59536 +
59537 + /** register bits */
59538 + struct {
59539 + unsigned frint:16;
59540 + unsigned hfirrldctrl:1;
59541 + unsigned reserved:15;
59542 + } b;
59543 +} hfir_data_t;
59544 +
59545 +/**
59546 + * This union represents the bit fields in the Host Frame Remaing/Number
59547 + * Register.
59548 + */
59549 +typedef union hfnum_data {
59550 + /** raw register data */
59551 + uint32_t d32;
59552 +
59553 + /** register bits */
59554 + struct {
59555 + unsigned frnum:16;
59556 +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
59557 + unsigned frrem:16;
59558 + } b;
59559 +} hfnum_data_t;
59560 +
59561 +typedef union hptxsts_data {
59562 + /** raw register data */
59563 + uint32_t d32;
59564 +
59565 + /** register bits */
59566 + struct {
59567 + unsigned ptxfspcavail:16;
59568 + unsigned ptxqspcavail:8;
59569 + /** Top of the Periodic Transmit Request Queue
59570 + * - bit 24 - Terminate (last entry for the selected channel)
59571 + * - bits 26:25 - Token Type
59572 + * - 2'b00 - Zero length
59573 + * - 2'b01 - Ping
59574 + * - 2'b10 - Disable
59575 + * - bits 30:27 - Channel Number
59576 + * - bit 31 - Odd/even microframe
59577 + */
59578 + unsigned ptxqtop_terminate:1;
59579 + unsigned ptxqtop_token:2;
59580 + unsigned ptxqtop_chnum:4;
59581 + unsigned ptxqtop_odd:1;
59582 + } b;
59583 +} hptxsts_data_t;
59584 +
59585 +/**
59586 + * This union represents the bit fields in the Host Port Control and Status
59587 + * Register. Read the register into the <i>d32</i> member then set/clear the
59588 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59589 + * hprt0 register.
59590 + */
59591 +typedef union hprt0_data {
59592 + /** raw register data */
59593 + uint32_t d32;
59594 + /** register bits */
59595 + struct {
59596 + unsigned prtconnsts:1;
59597 + unsigned prtconndet:1;
59598 + unsigned prtena:1;
59599 + unsigned prtenchng:1;
59600 + unsigned prtovrcurract:1;
59601 + unsigned prtovrcurrchng:1;
59602 + unsigned prtres:1;
59603 + unsigned prtsusp:1;
59604 + unsigned prtrst:1;
59605 + unsigned reserved9:1;
59606 + unsigned prtlnsts:2;
59607 + unsigned prtpwr:1;
59608 + unsigned prttstctl:4;
59609 + unsigned prtspd:2;
59610 +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
59611 +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
59612 +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
59613 + unsigned reserved19_31:13;
59614 + } b;
59615 +} hprt0_data_t;
59616 +
59617 +/**
59618 + * This union represents the bit fields in the Host All Interrupt
59619 + * Register.
59620 + */
59621 +typedef union haint_data {
59622 + /** raw register data */
59623 + uint32_t d32;
59624 + /** register bits */
59625 + struct {
59626 + unsigned ch0:1;
59627 + unsigned ch1:1;
59628 + unsigned ch2:1;
59629 + unsigned ch3:1;
59630 + unsigned ch4:1;
59631 + unsigned ch5:1;
59632 + unsigned ch6:1;
59633 + unsigned ch7:1;
59634 + unsigned ch8:1;
59635 + unsigned ch9:1;
59636 + unsigned ch10:1;
59637 + unsigned ch11:1;
59638 + unsigned ch12:1;
59639 + unsigned ch13:1;
59640 + unsigned ch14:1;
59641 + unsigned ch15:1;
59642 + unsigned reserved:16;
59643 + } b;
59644 +
59645 + struct {
59646 + unsigned chint:16;
59647 + unsigned reserved:16;
59648 + } b2;
59649 +} haint_data_t;
59650 +
59651 +/**
59652 + * This union represents the bit fields in the Host All Interrupt
59653 + * Register.
59654 + */
59655 +typedef union haintmsk_data {
59656 + /** raw register data */
59657 + uint32_t d32;
59658 + /** register bits */
59659 + struct {
59660 + unsigned ch0:1;
59661 + unsigned ch1:1;
59662 + unsigned ch2:1;
59663 + unsigned ch3:1;
59664 + unsigned ch4:1;
59665 + unsigned ch5:1;
59666 + unsigned ch6:1;
59667 + unsigned ch7:1;
59668 + unsigned ch8:1;
59669 + unsigned ch9:1;
59670 + unsigned ch10:1;
59671 + unsigned ch11:1;
59672 + unsigned ch12:1;
59673 + unsigned ch13:1;
59674 + unsigned ch14:1;
59675 + unsigned ch15:1;
59676 + unsigned reserved:16;
59677 + } b;
59678 +
59679 + struct {
59680 + unsigned chint:16;
59681 + unsigned reserved:16;
59682 + } b2;
59683 +} haintmsk_data_t;
59684 +
59685 +/**
59686 + * Host Channel Specific Registers. <i>500h-5FCh</i>
59687 + */
59688 +typedef struct dwc_otg_hc_regs {
59689 + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
59690 + volatile uint32_t hcchar;
59691 + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
59692 + volatile uint32_t hcsplt;
59693 + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
59694 + volatile uint32_t hcint;
59695 + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
59696 + volatile uint32_t hcintmsk;
59697 + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
59698 + volatile uint32_t hctsiz;
59699 + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
59700 + volatile uint32_t hcdma;
59701 + volatile uint32_t reserved;
59702 + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
59703 + volatile uint32_t hcdmab;
59704 +} dwc_otg_hc_regs_t;
59705 +
59706 +/**
59707 + * This union represents the bit fields in the Host Channel Characteristics
59708 + * Register. Read the register into the <i>d32</i> member then set/clear the
59709 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59710 + * hcchar register.
59711 + */
59712 +typedef union hcchar_data {
59713 + /** raw register data */
59714 + uint32_t d32;
59715 +
59716 + /** register bits */
59717 + struct {
59718 + /** Maximum packet size in bytes */
59719 + unsigned mps:11;
59720 +
59721 + /** Endpoint number */
59722 + unsigned epnum:4;
59723 +
59724 + /** 0: OUT, 1: IN */
59725 + unsigned epdir:1;
59726 +
59727 + unsigned reserved:1;
59728 +
59729 + /** 0: Full/high speed device, 1: Low speed device */
59730 + unsigned lspddev:1;
59731 +
59732 + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
59733 + unsigned eptype:2;
59734 +
59735 + /** Packets per frame for periodic transfers. 0 is reserved. */
59736 + unsigned multicnt:2;
59737 +
59738 + /** Device address */
59739 + unsigned devaddr:7;
59740 +
59741 + /**
59742 + * Frame to transmit periodic transaction.
59743 + * 0: even, 1: odd
59744 + */
59745 + unsigned oddfrm:1;
59746 +
59747 + /** Channel disable */
59748 + unsigned chdis:1;
59749 +
59750 + /** Channel enable */
59751 + unsigned chen:1;
59752 + } b;
59753 +} hcchar_data_t;
59754 +
59755 +typedef union hcsplt_data {
59756 + /** raw register data */
59757 + uint32_t d32;
59758 +
59759 + /** register bits */
59760 + struct {
59761 + /** Port Address */
59762 + unsigned prtaddr:7;
59763 +
59764 + /** Hub Address */
59765 + unsigned hubaddr:7;
59766 +
59767 + /** Transaction Position */
59768 + unsigned xactpos:2;
59769 +#define DWC_HCSPLIT_XACTPOS_MID 0
59770 +#define DWC_HCSPLIT_XACTPOS_END 1
59771 +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
59772 +#define DWC_HCSPLIT_XACTPOS_ALL 3
59773 +
59774 + /** Do Complete Split */
59775 + unsigned compsplt:1;
59776 +
59777 + /** Reserved */
59778 + unsigned reserved:14;
59779 +
59780 + /** Split Enble */
59781 + unsigned spltena:1;
59782 + } b;
59783 +} hcsplt_data_t;
59784 +
59785 +/**
59786 + * This union represents the bit fields in the Host All Interrupt
59787 + * Register.
59788 + */
59789 +typedef union hcint_data {
59790 + /** raw register data */
59791 + uint32_t d32;
59792 + /** register bits */
59793 + struct {
59794 + /** Transfer Complete */
59795 + unsigned xfercomp:1;
59796 + /** Channel Halted */
59797 + unsigned chhltd:1;
59798 + /** AHB Error */
59799 + unsigned ahberr:1;
59800 + /** STALL Response Received */
59801 + unsigned stall:1;
59802 + /** NAK Response Received */
59803 + unsigned nak:1;
59804 + /** ACK Response Received */
59805 + unsigned ack:1;
59806 + /** NYET Response Received */
59807 + unsigned nyet:1;
59808 + /** Transaction Err */
59809 + unsigned xacterr:1;
59810 + /** Babble Error */
59811 + unsigned bblerr:1;
59812 + /** Frame Overrun */
59813 + unsigned frmovrun:1;
59814 + /** Data Toggle Error */
59815 + unsigned datatglerr:1;
59816 + /** Buffer Not Available (only for DDMA mode) */
59817 + unsigned bna:1;
59818 + /** Exessive transaction error (only for DDMA mode) */
59819 + unsigned xcs_xact:1;
59820 + /** Frame List Rollover interrupt */
59821 + unsigned frm_list_roll:1;
59822 + /** Reserved */
59823 + unsigned reserved14_31:18;
59824 + } b;
59825 +} hcint_data_t;
59826 +
59827 +/**
59828 + * This union represents the bit fields in the Host Channel Interrupt Mask
59829 + * Register. Read the register into the <i>d32</i> member then set/clear the
59830 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59831 + * hcintmsk register.
59832 + */
59833 +typedef union hcintmsk_data {
59834 + /** raw register data */
59835 + uint32_t d32;
59836 +
59837 + /** register bits */
59838 + struct {
59839 + unsigned xfercompl:1;
59840 + unsigned chhltd:1;
59841 + unsigned ahberr:1;
59842 + unsigned stall:1;
59843 + unsigned nak:1;
59844 + unsigned ack:1;
59845 + unsigned nyet:1;
59846 + unsigned xacterr:1;
59847 + unsigned bblerr:1;
59848 + unsigned frmovrun:1;
59849 + unsigned datatglerr:1;
59850 + unsigned bna:1;
59851 + unsigned xcs_xact:1;
59852 + unsigned frm_list_roll:1;
59853 + unsigned reserved14_31:18;
59854 + } b;
59855 +} hcintmsk_data_t;
59856 +
59857 +/**
59858 + * This union represents the bit fields in the Host Channel Transfer Size
59859 + * Register. Read the register into the <i>d32</i> member then set/clear the
59860 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59861 + * hcchar register.
59862 + */
59863 +
59864 +typedef union hctsiz_data {
59865 + /** raw register data */
59866 + uint32_t d32;
59867 +
59868 + /** register bits */
59869 + struct {
59870 + /** Total transfer size in bytes */
59871 + unsigned xfersize:19;
59872 +
59873 + /** Data packets to transfer */
59874 + unsigned pktcnt:10;
59875 +
59876 + /**
59877 + * Packet ID for next data packet
59878 + * 0: DATA0
59879 + * 1: DATA2
59880 + * 2: DATA1
59881 + * 3: MDATA (non-Control), SETUP (Control)
59882 + */
59883 + unsigned pid:2;
59884 +#define DWC_HCTSIZ_DATA0 0
59885 +#define DWC_HCTSIZ_DATA1 2
59886 +#define DWC_HCTSIZ_DATA2 1
59887 +#define DWC_HCTSIZ_MDATA 3
59888 +#define DWC_HCTSIZ_SETUP 3
59889 +
59890 + /** Do PING protocol when 1 */
59891 + unsigned dopng:1;
59892 + } b;
59893 +
59894 + /** register bits */
59895 + struct {
59896 + /** Scheduling information */
59897 + unsigned schinfo:8;
59898 +
59899 + /** Number of transfer descriptors.
59900 + * Max value:
59901 + * 64 in general,
59902 + * 256 only for HS isochronous endpoint.
59903 + */
59904 + unsigned ntd:8;
59905 +
59906 + /** Data packets to transfer */
59907 + unsigned reserved16_28:13;
59908 +
59909 + /**
59910 + * Packet ID for next data packet
59911 + * 0: DATA0
59912 + * 1: DATA2
59913 + * 2: DATA1
59914 + * 3: MDATA (non-Control)
59915 + */
59916 + unsigned pid:2;
59917 +
59918 + /** Do PING protocol when 1 */
59919 + unsigned dopng:1;
59920 + } b_ddma;
59921 +} hctsiz_data_t;
59922 +
59923 +/**
59924 + * This union represents the bit fields in the Host DMA Address
59925 + * Register used in Descriptor DMA mode.
59926 + */
59927 +typedef union hcdma_data {
59928 + /** raw register data */
59929 + uint32_t d32;
59930 + /** register bits */
59931 + struct {
59932 + unsigned reserved0_2:3;
59933 + /** Current Transfer Descriptor. Not used for ISOC */
59934 + unsigned ctd:8;
59935 + /** Start Address of Descriptor List */
59936 + unsigned dma_addr:21;
59937 + } b;
59938 +} hcdma_data_t;
59939 +
59940 +/**
59941 + * This union represents the bit fields in the DMA Descriptor
59942 + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
59943 + * set/clear the bits using the <i>b</i>it elements.
59944 + */
59945 +typedef union host_dma_desc_sts {
59946 + /** raw register data */
59947 + uint32_t d32;
59948 + /** quadlet bits */
59949 +
59950 + /* for non-isochronous */
59951 + struct {
59952 + /** Number of bytes */
59953 + unsigned n_bytes:17;
59954 + /** QTD offset to jump when Short Packet received - only for IN EPs */
59955 + unsigned qtd_offset:6;
59956 + /**
59957 + * Set to request the core to jump to alternate QTD if
59958 + * Short Packet received - only for IN EPs
59959 + */
59960 + unsigned a_qtd:1;
59961 + /**
59962 + * Setup Packet bit. When set indicates that buffer contains
59963 + * setup packet.
59964 + */
59965 + unsigned sup:1;
59966 + /** Interrupt On Complete */
59967 + unsigned ioc:1;
59968 + /** End of List */
59969 + unsigned eol:1;
59970 + unsigned reserved27:1;
59971 + /** Rx/Tx Status */
59972 + unsigned sts:2;
59973 +#define DMA_DESC_STS_PKTERR 1
59974 + unsigned reserved30:1;
59975 + /** Active Bit */
59976 + unsigned a:1;
59977 + } b;
59978 + /* for isochronous */
59979 + struct {
59980 + /** Number of bytes */
59981 + unsigned n_bytes:12;
59982 + unsigned reserved12_24:13;
59983 + /** Interrupt On Complete */
59984 + unsigned ioc:1;
59985 + unsigned reserved26_27:2;
59986 + /** Rx/Tx Status */
59987 + unsigned sts:2;
59988 + unsigned reserved30:1;
59989 + /** Active Bit */
59990 + unsigned a:1;
59991 + } b_isoc;
59992 +} host_dma_desc_sts_t;
59993 +
59994 +#define MAX_DMA_DESC_SIZE 131071
59995 +#define MAX_DMA_DESC_NUM_GENERIC 64
59996 +#define MAX_DMA_DESC_NUM_HS_ISOC 256
59997 +#define MAX_FRLIST_EN_NUM 64
59998 +/**
59999 + * Host-mode DMA Descriptor structure
60000 + *
60001 + * DMA Descriptor structure contains two quadlets:
60002 + * Status quadlet and Data buffer pointer.
60003 + */
60004 +typedef struct dwc_otg_host_dma_desc {
60005 + /** DMA Descriptor status quadlet */
60006 + host_dma_desc_sts_t status;
60007 + /** DMA Descriptor data buffer pointer */
60008 + uint32_t buf;
60009 +} dwc_otg_host_dma_desc_t;
60010 +
60011 +/** OTG Host Interface Structure.
60012 + *
60013 + * The OTG Host Interface Structure structure contains information
60014 + * needed to manage the DWC_otg controller acting in host mode. It
60015 + * represents the programming view of the host-specific aspects of the
60016 + * controller.
60017 + */
60018 +typedef struct dwc_otg_host_if {
60019 + /** Host Global Registers starting at offset 400h.*/
60020 + dwc_otg_host_global_regs_t *host_global_regs;
60021 +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
60022 +
60023 + /** Host Port 0 Control and Status Register */
60024 + volatile uint32_t *hprt0;
60025 +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
60026 +
60027 + /** Host Channel Specific Registers at offsets 500h-5FCh. */
60028 + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
60029 +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
60030 +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
60031 +
60032 + /* Host configuration information */
60033 + /** Number of Host Channels (range: 1-16) */
60034 + uint8_t num_host_channels;
60035 + /** Periodic EPs supported (0: no, 1: yes) */
60036 + uint8_t perio_eps_supported;
60037 + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
60038 + uint16_t perio_tx_fifo_size;
60039 +
60040 +} dwc_otg_host_if_t;
60041 +
60042 +/**
60043 + * This union represents the bit fields in the Power and Clock Gating Control
60044 + * Register. Read the register into the <i>d32</i> member then set/clear the
60045 + * bits using the <i>b</i>it elements.
60046 + */
60047 +typedef union pcgcctl_data {
60048 + /** raw register data */
60049 + uint32_t d32;
60050 +
60051 + /** register bits */
60052 + struct {
60053 + /** Stop Pclk */
60054 + unsigned stoppclk:1;
60055 + /** Gate Hclk */
60056 + unsigned gatehclk:1;
60057 + /** Power Clamp */
60058 + unsigned pwrclmp:1;
60059 + /** Reset Power Down Modules */
60060 + unsigned rstpdwnmodule:1;
60061 + /** Reserved */
60062 + unsigned reserved:1;
60063 + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
60064 + unsigned enbl_sleep_gating:1;
60065 + /** PHY In Sleep (PhySleep) */
60066 + unsigned phy_in_sleep:1;
60067 + /** Deep Sleep*/
60068 + unsigned deep_sleep:1;
60069 + unsigned resetaftsusp:1;
60070 + unsigned restoremode:1;
60071 + unsigned enbl_extnd_hiber:1;
60072 + unsigned extnd_hiber_pwrclmp:1;
60073 + unsigned extnd_hiber_switch:1;
60074 + unsigned ess_reg_restored:1;
60075 + unsigned prt_clk_sel:2;
60076 + unsigned port_power:1;
60077 + unsigned max_xcvrselect:2;
60078 + unsigned max_termsel:1;
60079 + unsigned mac_dev_addr:7;
60080 + unsigned p2hd_dev_enum_spd:2;
60081 + unsigned p2hd_prt_spd:2;
60082 + unsigned if_dev_mode:1;
60083 + } b;
60084 +} pcgcctl_data_t;
60085 +
60086 +/**
60087 + * This union represents the bit fields in the Global Data FIFO Software
60088 + * Configuration Register. Read the register into the <i>d32</i> member then
60089 + * set/clear the bits using the <i>b</i>it elements.
60090 + */
60091 +typedef union gdfifocfg_data {
60092 + /* raw register data */
60093 + uint32_t d32;
60094 + /** register bits */
60095 + struct {
60096 + /** OTG Data FIFO depth */
60097 + unsigned gdfifocfg:16;
60098 + /** Start address of EP info controller */
60099 + unsigned epinfobase:16;
60100 + } b;
60101 +} gdfifocfg_data_t;
60102 +
60103 +/**
60104 + * This union represents the bit fields in the Global Power Down Register
60105 + * Register. Read the register into the <i>d32</i> member then set/clear the
60106 + * bits using the <i>b</i>it elements.
60107 + */
60108 +typedef union gpwrdn_data {
60109 + /* raw register data */
60110 + uint32_t d32;
60111 +
60112 + /** register bits */
60113 + struct {
60114 + /** PMU Interrupt Select */
60115 + unsigned pmuintsel:1;
60116 + /** PMU Active */
60117 + unsigned pmuactv:1;
60118 + /** Restore */
60119 + unsigned restore:1;
60120 + /** Power Down Clamp */
60121 + unsigned pwrdnclmp:1;
60122 + /** Power Down Reset */
60123 + unsigned pwrdnrstn:1;
60124 + /** Power Down Switch */
60125 + unsigned pwrdnswtch:1;
60126 + /** Disable VBUS */
60127 + unsigned dis_vbus:1;
60128 + /** Line State Change */
60129 + unsigned lnstschng:1;
60130 + /** Line state change mask */
60131 + unsigned lnstchng_msk:1;
60132 + /** Reset Detected */
60133 + unsigned rst_det:1;
60134 + /** Reset Detect mask */
60135 + unsigned rst_det_msk:1;
60136 + /** Disconnect Detected */
60137 + unsigned disconn_det:1;
60138 + /** Disconnect Detect mask */
60139 + unsigned disconn_det_msk:1;
60140 + /** Connect Detected*/
60141 + unsigned connect_det:1;
60142 + /** Connect Detected Mask*/
60143 + unsigned connect_det_msk:1;
60144 + /** SRP Detected */
60145 + unsigned srp_det:1;
60146 + /** SRP Detect mask */
60147 + unsigned srp_det_msk:1;
60148 + /** Status Change Interrupt */
60149 + unsigned sts_chngint:1;
60150 + /** Status Change Interrupt Mask */
60151 + unsigned sts_chngint_msk:1;
60152 + /** Line State */
60153 + unsigned linestate:2;
60154 + /** Indicates current mode(status of IDDIG signal) */
60155 + unsigned idsts:1;
60156 + /** B Session Valid signal status*/
60157 + unsigned bsessvld:1;
60158 + /** ADP Event Detected */
60159 + unsigned adp_int:1;
60160 + /** Multi Valued ID pin */
60161 + unsigned mult_val_id_bc:5;
60162 + /** Reserved 24_31 */
60163 + unsigned reserved29_31:3;
60164 + } b;
60165 +} gpwrdn_data_t;
60166 +
60167 +#endif
60168 --- /dev/null
60169 +++ b/drivers/usb/host/dwc_otg/test/Makefile
60170 @@ -0,0 +1,16 @@
60171 +
60172 +PERL=/usr/bin/perl
60173 +PL_TESTS=test_sysfs.pl test_mod_param.pl
60174 +
60175 +.PHONY : test
60176 +test : perl_tests
60177 +
60178 +perl_tests :
60179 + @echo
60180 + @echo Running perl tests
60181 + @for test in $(PL_TESTS); do \
60182 + if $(PERL) ./$$test ; then \
60183 + echo "=======> $$test, PASSED" ; \
60184 + else echo "=======> $$test, FAILED" ; \
60185 + fi \
60186 + done
60187 --- /dev/null
60188 +++ b/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
60189 @@ -0,0 +1,337 @@
60190 +package dwc_otg_test;
60191 +
60192 +use strict;
60193 +use Exporter ();
60194 +
60195 +use vars qw(@ISA @EXPORT
60196 +$sysfsdir $paramdir $errors $params
60197 +);
60198 +
60199 +@ISA = qw(Exporter);
60200 +
60201 +#
60202 +# Globals
60203 +#
60204 +$sysfsdir = "/sys/devices/lm0";
60205 +$paramdir = "/sys/module/dwc_otg";
60206 +$errors = 0;
60207 +
60208 +$params = [
60209 + {
60210 + NAME => "otg_cap",
60211 + DEFAULT => 0,
60212 + ENUM => [],
60213 + LOW => 0,
60214 + HIGH => 2
60215 + },
60216 + {
60217 + NAME => "dma_enable",
60218 + DEFAULT => 0,
60219 + ENUM => [],
60220 + LOW => 0,
60221 + HIGH => 1
60222 + },
60223 + {
60224 + NAME => "dma_burst_size",
60225 + DEFAULT => 32,
60226 + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
60227 + LOW => 1,
60228 + HIGH => 256
60229 + },
60230 + {
60231 + NAME => "host_speed",
60232 + DEFAULT => 0,
60233 + ENUM => [],
60234 + LOW => 0,
60235 + HIGH => 1
60236 + },
60237 + {
60238 + NAME => "host_support_fs_ls_low_power",
60239 + DEFAULT => 0,
60240 + ENUM => [],
60241 + LOW => 0,
60242 + HIGH => 1
60243 + },
60244 + {
60245 + NAME => "host_ls_low_power_phy_clk",
60246 + DEFAULT => 0,
60247 + ENUM => [],
60248 + LOW => 0,
60249 + HIGH => 1
60250 + },
60251 + {
60252 + NAME => "dev_speed",
60253 + DEFAULT => 0,
60254 + ENUM => [],
60255 + LOW => 0,
60256 + HIGH => 1
60257 + },
60258 + {
60259 + NAME => "enable_dynamic_fifo",
60260 + DEFAULT => 1,
60261 + ENUM => [],
60262 + LOW => 0,
60263 + HIGH => 1
60264 + },
60265 + {
60266 + NAME => "data_fifo_size",
60267 + DEFAULT => 8192,
60268 + ENUM => [],
60269 + LOW => 32,
60270 + HIGH => 32768
60271 + },
60272 + {
60273 + NAME => "dev_rx_fifo_size",
60274 + DEFAULT => 1064,
60275 + ENUM => [],
60276 + LOW => 16,
60277 + HIGH => 32768
60278 + },
60279 + {
60280 + NAME => "dev_nperio_tx_fifo_size",
60281 + DEFAULT => 1024,
60282 + ENUM => [],
60283 + LOW => 16,
60284 + HIGH => 32768
60285 + },
60286 + {
60287 + NAME => "dev_perio_tx_fifo_size_1",
60288 + DEFAULT => 256,
60289 + ENUM => [],
60290 + LOW => 4,
60291 + HIGH => 768
60292 + },
60293 + {
60294 + NAME => "dev_perio_tx_fifo_size_2",
60295 + DEFAULT => 256,
60296 + ENUM => [],
60297 + LOW => 4,
60298 + HIGH => 768
60299 + },
60300 + {
60301 + NAME => "dev_perio_tx_fifo_size_3",
60302 + DEFAULT => 256,
60303 + ENUM => [],
60304 + LOW => 4,
60305 + HIGH => 768
60306 + },
60307 + {
60308 + NAME => "dev_perio_tx_fifo_size_4",
60309 + DEFAULT => 256,
60310 + ENUM => [],
60311 + LOW => 4,
60312 + HIGH => 768
60313 + },
60314 + {
60315 + NAME => "dev_perio_tx_fifo_size_5",
60316 + DEFAULT => 256,
60317 + ENUM => [],
60318 + LOW => 4,
60319 + HIGH => 768
60320 + },
60321 + {
60322 + NAME => "dev_perio_tx_fifo_size_6",
60323 + DEFAULT => 256,
60324 + ENUM => [],
60325 + LOW => 4,
60326 + HIGH => 768
60327 + },
60328 + {
60329 + NAME => "dev_perio_tx_fifo_size_7",
60330 + DEFAULT => 256,
60331 + ENUM => [],
60332 + LOW => 4,
60333 + HIGH => 768
60334 + },
60335 + {
60336 + NAME => "dev_perio_tx_fifo_size_8",
60337 + DEFAULT => 256,
60338 + ENUM => [],
60339 + LOW => 4,
60340 + HIGH => 768
60341 + },
60342 + {
60343 + NAME => "dev_perio_tx_fifo_size_9",
60344 + DEFAULT => 256,
60345 + ENUM => [],
60346 + LOW => 4,
60347 + HIGH => 768
60348 + },
60349 + {
60350 + NAME => "dev_perio_tx_fifo_size_10",
60351 + DEFAULT => 256,
60352 + ENUM => [],
60353 + LOW => 4,
60354 + HIGH => 768
60355 + },
60356 + {
60357 + NAME => "dev_perio_tx_fifo_size_11",
60358 + DEFAULT => 256,
60359 + ENUM => [],
60360 + LOW => 4,
60361 + HIGH => 768
60362 + },
60363 + {
60364 + NAME => "dev_perio_tx_fifo_size_12",
60365 + DEFAULT => 256,
60366 + ENUM => [],
60367 + LOW => 4,
60368 + HIGH => 768
60369 + },
60370 + {
60371 + NAME => "dev_perio_tx_fifo_size_13",
60372 + DEFAULT => 256,
60373 + ENUM => [],
60374 + LOW => 4,
60375 + HIGH => 768
60376 + },
60377 + {
60378 + NAME => "dev_perio_tx_fifo_size_14",
60379 + DEFAULT => 256,
60380 + ENUM => [],
60381 + LOW => 4,
60382 + HIGH => 768
60383 + },
60384 + {
60385 + NAME => "dev_perio_tx_fifo_size_15",
60386 + DEFAULT => 256,
60387 + ENUM => [],
60388 + LOW => 4,
60389 + HIGH => 768
60390 + },
60391 + {
60392 + NAME => "host_rx_fifo_size",
60393 + DEFAULT => 1024,
60394 + ENUM => [],
60395 + LOW => 16,
60396 + HIGH => 32768
60397 + },
60398 + {
60399 + NAME => "host_nperio_tx_fifo_size",
60400 + DEFAULT => 1024,
60401 + ENUM => [],
60402 + LOW => 16,
60403 + HIGH => 32768
60404 + },
60405 + {
60406 + NAME => "host_perio_tx_fifo_size",
60407 + DEFAULT => 1024,
60408 + ENUM => [],
60409 + LOW => 16,
60410 + HIGH => 32768
60411 + },
60412 + {
60413 + NAME => "max_transfer_size",
60414 + DEFAULT => 65535,
60415 + ENUM => [],
60416 + LOW => 2047,
60417 + HIGH => 65535
60418 + },
60419 + {
60420 + NAME => "max_packet_count",
60421 + DEFAULT => 511,
60422 + ENUM => [],
60423 + LOW => 15,
60424 + HIGH => 511
60425 + },
60426 + {
60427 + NAME => "host_channels",
60428 + DEFAULT => 12,
60429 + ENUM => [],
60430 + LOW => 1,
60431 + HIGH => 16
60432 + },
60433 + {
60434 + NAME => "dev_endpoints",
60435 + DEFAULT => 6,
60436 + ENUM => [],
60437 + LOW => 1,
60438 + HIGH => 15
60439 + },
60440 + {
60441 + NAME => "phy_type",
60442 + DEFAULT => 1,
60443 + ENUM => [],
60444 + LOW => 0,
60445 + HIGH => 2
60446 + },
60447 + {
60448 + NAME => "phy_utmi_width",
60449 + DEFAULT => 16,
60450 + ENUM => [8, 16],
60451 + LOW => 8,
60452 + HIGH => 16
60453 + },
60454 + {
60455 + NAME => "phy_ulpi_ddr",
60456 + DEFAULT => 0,
60457 + ENUM => [],
60458 + LOW => 0,
60459 + HIGH => 1
60460 + },
60461 + ];
60462 +
60463 +
60464 +#
60465 +#
60466 +sub check_arch {
60467 + $_ = `uname -m`;
60468 + chomp;
60469 + unless (m/armv4tl/) {
60470 + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
60471 + return 0;
60472 + }
60473 + return 1;
60474 +}
60475 +
60476 +#
60477 +#
60478 +sub load_module {
60479 + my $params = shift;
60480 + print "\nRemoving Module\n";
60481 + system "rmmod dwc_otg";
60482 + print "Loading Module\n";
60483 + if ($params ne "") {
60484 + print "Module Parameters: $params\n";
60485 + }
60486 + if (system("modprobe dwc_otg $params")) {
60487 + warn "Unable to load module\n";
60488 + return 0;
60489 + }
60490 + return 1;
60491 +}
60492 +
60493 +#
60494 +#
60495 +sub test_status {
60496 + my $arg = shift;
60497 +
60498 + print "\n";
60499 +
60500 + if (defined $arg) {
60501 + warn "WARNING: $arg\n";
60502 + }
60503 +
60504 + if ($errors > 0) {
60505 + warn "TEST FAILED with $errors errors\n";
60506 + return 0;
60507 + } else {
60508 + print "TEST PASSED\n";
60509 + return 0 if (defined $arg);
60510 + }
60511 + return 1;
60512 +}
60513 +
60514 +#
60515 +#
60516 +@EXPORT = qw(
60517 +$sysfsdir
60518 +$paramdir
60519 +$params
60520 +$errors
60521 +check_arch
60522 +load_module
60523 +test_status
60524 +);
60525 +
60526 +1;
60527 --- /dev/null
60528 +++ b/drivers/usb/host/dwc_otg/test/test_mod_param.pl
60529 @@ -0,0 +1,133 @@
60530 +#!/usr/bin/perl -w
60531 +#
60532 +# Run this program on the integrator.
60533 +#
60534 +# - Tests module parameter default values.
60535 +# - Tests setting of valid module parameter values via modprobe.
60536 +# - Tests invalid module parameter values.
60537 +# -----------------------------------------------------------------------------
60538 +use strict;
60539 +use dwc_otg_test;
60540 +
60541 +check_arch() or die;
60542 +
60543 +#
60544 +#
60545 +sub test {
60546 + my ($param,$expected) = @_;
60547 + my $value = get($param);
60548 +
60549 + if ($value == $expected) {
60550 + print "$param = $value, okay\n";
60551 + }
60552 +
60553 + else {
60554 + warn "ERROR: value of $param != $expected, $value\n";
60555 + $errors ++;
60556 + }
60557 +}
60558 +
60559 +#
60560 +#
60561 +sub get {
60562 + my $param = shift;
60563 + my $tmp = `cat $paramdir/$param`;
60564 + chomp $tmp;
60565 + return $tmp;
60566 +}
60567 +
60568 +#
60569 +#
60570 +sub test_main {
60571 +
60572 + print "\nTesting Module Parameters\n";
60573 +
60574 + load_module("") or die;
60575 +
60576 + # Test initial values
60577 + print "\nTesting Default Values\n";
60578 + foreach (@{$params}) {
60579 + test ($_->{NAME}, $_->{DEFAULT});
60580 + }
60581 +
60582 + # Test low value
60583 + print "\nTesting Low Value\n";
60584 + my $cmd_params = "";
60585 + foreach (@{$params}) {
60586 + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
60587 + }
60588 + load_module($cmd_params) or die;
60589 +
60590 + foreach (@{$params}) {
60591 + test ($_->{NAME}, $_->{LOW});
60592 + }
60593 +
60594 + # Test high value
60595 + print "\nTesting High Value\n";
60596 + $cmd_params = "";
60597 + foreach (@{$params}) {
60598 + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
60599 + }
60600 + load_module($cmd_params) or die;
60601 +
60602 + foreach (@{$params}) {
60603 + test ($_->{NAME}, $_->{HIGH});
60604 + }
60605 +
60606 + # Test Enum
60607 + print "\nTesting Enumerated\n";
60608 + foreach (@{$params}) {
60609 + if (defined $_->{ENUM}) {
60610 + my $value;
60611 + foreach $value (@{$_->{ENUM}}) {
60612 + $cmd_params = "$_->{NAME}=$value";
60613 + load_module($cmd_params) or die;
60614 + test ($_->{NAME}, $value);
60615 + }
60616 + }
60617 + }
60618 +
60619 + # Test Invalid Values
60620 + print "\nTesting Invalid Values\n";
60621 + $cmd_params = "";
60622 + foreach (@{$params}) {
60623 + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
60624 + }
60625 + load_module($cmd_params) or die;
60626 +
60627 + foreach (@{$params}) {
60628 + test ($_->{NAME}, $_->{DEFAULT});
60629 + }
60630 +
60631 + $cmd_params = "";
60632 + foreach (@{$params}) {
60633 + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
60634 + }
60635 + load_module($cmd_params) or die;
60636 +
60637 + foreach (@{$params}) {
60638 + test ($_->{NAME}, $_->{DEFAULT});
60639 + }
60640 +
60641 + print "\nTesting Enumerated\n";
60642 + foreach (@{$params}) {
60643 + if (defined $_->{ENUM}) {
60644 + my $value;
60645 + foreach $value (@{$_->{ENUM}}) {
60646 + $value = $value + 1;
60647 + $cmd_params = "$_->{NAME}=$value";
60648 + load_module($cmd_params) or die;
60649 + test ($_->{NAME}, $_->{DEFAULT});
60650 + $value = $value - 2;
60651 + $cmd_params = "$_->{NAME}=$value";
60652 + load_module($cmd_params) or die;
60653 + test ($_->{NAME}, $_->{DEFAULT});
60654 + }
60655 + }
60656 + }
60657 +
60658 + test_status() or die;
60659 +}
60660 +
60661 +test_main();
60662 +0;
60663 --- /dev/null
60664 +++ b/drivers/usb/host/dwc_otg/test/test_sysfs.pl
60665 @@ -0,0 +1,193 @@
60666 +#!/usr/bin/perl -w
60667 +#
60668 +# Run this program on the integrator
60669 +# - Tests select sysfs attributes.
60670 +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
60671 +# -----------------------------------------------------------------------------
60672 +use strict;
60673 +use dwc_otg_test;
60674 +
60675 +check_arch() or die;
60676 +
60677 +#
60678 +#
60679 +sub test {
60680 + my ($attr,$expected) = @_;
60681 + my $string = get($attr);
60682 +
60683 + if ($string eq $expected) {
60684 + printf("$attr = $string, okay\n");
60685 + }
60686 + else {
60687 + warn "ERROR: value of $attr != $expected, $string\n";
60688 + $errors ++;
60689 + }
60690 +}
60691 +
60692 +#
60693 +#
60694 +sub set {
60695 + my ($reg, $value) = @_;
60696 + system "echo $value > $sysfsdir/$reg";
60697 +}
60698 +
60699 +#
60700 +#
60701 +sub get {
60702 + my $attr = shift;
60703 + my $string = `cat $sysfsdir/$attr`;
60704 + chomp $string;
60705 + if ($string =~ m/\s\=\s/) {
60706 + my $tmp;
60707 + ($tmp, $string) = split /\s=\s/, $string;
60708 + }
60709 + return $string;
60710 +}
60711 +
60712 +#
60713 +#
60714 +sub test_main {
60715 + print("\nTesting Sysfs Attributes\n");
60716 +
60717 + load_module("") or die;
60718 +
60719 + # Test initial values of regoffset/regvalue/guid/gsnpsid
60720 + print("\nTesting Default Values\n");
60721 +
60722 + test("regoffset", "0xffffffff");
60723 + test("regvalue", "invalid offset");
60724 + test("guid", "0x12345678"); # this will fail if it has been changed
60725 + test("gsnpsid", "0x4f54200a");
60726 +
60727 + # Test operation of regoffset/regvalue
60728 + print("\nTesting regoffset\n");
60729 + set('regoffset', '5a5a5a5a');
60730 + test("regoffset", "0xffffffff");
60731 +
60732 + set('regoffset', '0');
60733 + test("regoffset", "0x00000000");
60734 +
60735 + set('regoffset', '40000');
60736 + test("regoffset", "0x00000000");
60737 +
60738 + set('regoffset', '3ffff');
60739 + test("regoffset", "0x0003ffff");
60740 +
60741 + set('regoffset', '1');
60742 + test("regoffset", "0x00000001");
60743 +
60744 + print("\nTesting regvalue\n");
60745 + set('regoffset', '3c');
60746 + test("regvalue", "0x12345678");
60747 + set('regvalue', '5a5a5a5a');
60748 + test("regvalue", "0x5a5a5a5a");
60749 + set('regvalue','a5a5a5a5');
60750 + test("regvalue", "0xa5a5a5a5");
60751 + set('guid','12345678');
60752 +
60753 + # Test HNP Capable
60754 + print("\nTesting HNP Capable bit\n");
60755 + set('hnpcapable', '1');
60756 + test("hnpcapable", "0x1");
60757 + set('hnpcapable','0');
60758 + test("hnpcapable", "0x0");
60759 +
60760 + set('regoffset','0c');
60761 +
60762 + my $old = get('gusbcfg');
60763 + print("setting hnpcapable\n");
60764 + set('hnpcapable', '1');
60765 + test("hnpcapable", "0x1");
60766 + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
60767 + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
60768 +
60769 + $old = get('gusbcfg');
60770 + print("clearing hnpcapable\n");
60771 + set('hnpcapable', '0');
60772 + test("hnpcapable", "0x0");
60773 + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
60774 + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
60775 +
60776 + # Test SRP Capable
60777 + print("\nTesting SRP Capable bit\n");
60778 + set('srpcapable', '1');
60779 + test("srpcapable", "0x1");
60780 + set('srpcapable','0');
60781 + test("srpcapable", "0x0");
60782 +
60783 + set('regoffset','0c');
60784 +
60785 + $old = get('gusbcfg');
60786 + print("setting srpcapable\n");
60787 + set('srpcapable', '1');
60788 + test("srpcapable", "0x1");
60789 + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
60790 + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
60791 +
60792 + $old = get('gusbcfg');
60793 + print("clearing srpcapable\n");
60794 + set('srpcapable', '0');
60795 + test("srpcapable", "0x0");
60796 + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
60797 + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
60798 +
60799 + # Test GGPIO
60800 + print("\nTesting GGPIO\n");
60801 + set('ggpio','5a5a5a5a');
60802 + test('ggpio','0x5a5a0000');
60803 + set('ggpio','a5a5a5a5');
60804 + test('ggpio','0xa5a50000');
60805 + set('ggpio','11110000');
60806 + test('ggpio','0x11110000');
60807 + set('ggpio','00001111');
60808 + test('ggpio','0x00000000');
60809 +
60810 + # Test DEVSPEED
60811 + print("\nTesting DEVSPEED\n");
60812 + set('regoffset','800');
60813 + $old = get('regvalue');
60814 + set('devspeed','0');
60815 + test('devspeed','0x0');
60816 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
60817 + set('devspeed','1');
60818 + test('devspeed','0x1');
60819 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
60820 + set('devspeed','2');
60821 + test('devspeed','0x2');
60822 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
60823 + set('devspeed','3');
60824 + test('devspeed','0x3');
60825 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
60826 + set('devspeed','4');
60827 + test('devspeed','0x0');
60828 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
60829 + set('devspeed','5');
60830 + test('devspeed','0x1');
60831 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
60832 +
60833 +
60834 + # mode Returns the current mode:0 for device mode1 for host mode Read
60835 + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
60836 + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
60837 + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
60838 + # bussuspend Suspend the USB bus. Read/Write
60839 + # busconnected Get the connection status of the bus Read
60840 +
60841 + # gotgctl Get or set the Core Control Status Register. Read/Write
60842 + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
60843 + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
60844 + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
60845 + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
60846 + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
60847 + ## guid Get or set the value of the User ID Register Read/Write
60848 + ## gsnpsid Get the value of the Synopsys ID Regester Read
60849 + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
60850 + # enumspeed Gets the device enumeration Speed. Read
60851 + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
60852 + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
60853 +
60854 + test_status("TEST NYI") or die;
60855 +}
60856 +
60857 +test_main();
60858 +0;