kernel: bump 6.1 to 6.1.77
[openwrt/staging/stintel.git] / target / linux / bcm27xx / patches-6.1 / 950-0106-Add-dwc_otg-driver.patch
1 From fe24a2249a07c6c70767d6030b4b467a5dc6717f 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 dwc_otg: fix summarize urb->actual_length for isochronous transfers
506
507 Kernel does not copy input data of ISO transfers to userspace
508 if actual_length is set only in ISO transfers and not summarized
509 in urb->actual_length. Fixes raspberrypi/linux#903
510
511 fiq_fsm: Use correct states when starting isoc OUT transfers
512
513 In fiq_fsm_start_next_periodic() if an isochronous OUT transfer
514 was selected, no regard was given as to whether this was a single-packet
515 transfer or a multi-packet staged transfer.
516
517 For single-packet transfers, this had the effect of repeatedly sending
518 OUT packets with bogus data and lengths.
519
520 Eventually if the channel was repeatedly enabled enough times, this
521 would lock up the OTG core and no further bus transfers would happen.
522
523 Set the FSM state up properly if we select a single-packet transfer.
524
525 Fixes https://github.com/raspberrypi/linux/issues/1842
526
527 dwc_otg: make nak_holdoff work as intended with empty queues
528
529 If URBs reading from non-periodic split endpoints were dequeued and
530 the last transfer from the endpoint was a NAK handshake, the resulting
531 qh->nak_frame value was stale which would result in unnecessarily long
532 polling intervals for the first subsequent transfer with a fresh URB.
533
534 Fixup qh->nak_frame in dwc_otg_hcd_urb_dequeue and also guard against
535 a case where a single URB is submitted to the endpoint, a NAK was
536 received on the transfer immediately prior to receiving data and the
537 device subsequently resubmits another URB past the qh->nak_frame interval.
538
539 Fixes https://github.com/raspberrypi/linux/issues/1709
540
541 dwc_otg: fix split transaction data toggle handling around dequeues
542
543 See https://github.com/raspberrypi/linux/issues/1709
544
545 Fix several issues regarding endpoint state when URBs are dequeued
546 - If the HCD is disconnected, flush FIQ-enabled channels properly
547 - Save the data toggle state for bulk endpoints if the last transfer
548 from an endpoint where URBs were dequeued returned a data packet
549 - Reset hc->start_pkt_count properly in assign_and_init_hc()
550
551 dwc_otg: fix several potential crash sources
552
553 On root port disconnect events, the host driver state is cleared and
554 in-progress host channels are forcibly stopped. This doesn't play
555 well with the FIQ running in the background, so:
556 - Guard the disconnect callback with both the host spinlock and FIQ
557 spinlock
558 - Move qtd dereference in dwc_otg_handle_hc_fsm() after the early-out
559 so we don't dereference a qtd that has gone away
560 - Turn catch-all BUG()s in dwc_otg_handle_hc_fsm() into warnings.
561
562 dwc_otg: delete hcd->channel_lock
563
564 The lock serves no purpose as it is only held while the HCD spinlock
565 is already being held.
566
567 dwc_otg: remove unnecessary dma-mode channel halts on disconnect interrupt
568
569 Host channels are already halted in kill_urbs_in_qh_list() with the
570 subsequent interrupt processing behaving as if the URB was dequeued
571 via HCD callback.
572
573 There's no need to clobber the host channel registers a second time
574 as this exposes races between the driver and host channel resulting
575 in hcd->free_hc_list becoming corrupted.
576
577 dwcotg: Allow to build without FIQ on ARM64
578
579 Signed-off-by: popcornmix <popcornmix@gmail.com>
580
581 dwc_otg: make periodic scheduling behave properly for FS buses
582
583 If the root port is in full-speed mode, transfer times at 12mbit/s
584 would be calculated but matched against high-speed quotas.
585
586 Reinitialise hcd->frame_usecs[i] on each port enable event so that
587 full-speed bandwidth can be tracked sensibly.
588
589 Also, don't bother using the FIQ for transfers when in full-speed
590 mode - at the slower bus speed, interrupt frequency is reduced by
591 an order of magnitude.
592
593 Related issue: https://github.com/raspberrypi/linux/issues/2020
594
595 dwc_otg: fiq_fsm: Make isochronous compatibility checks work properly
596
597 Get rid of the spammy printk and local pointer mangling.
598 Also, there is a nominal benefit for using fiq_fsm for isochronous
599 transfers in FS mode (~1.1k IRQs per second vs 2.1k IRQs per second)
600 so remove the root port speed check.
601
602 dwc_otg: add module parameter int_ep_interval_min
603
604 Add a module parameter (defaulting to ignored) that clamps the polling rate
605 of high-speed Interrupt endpoints to a minimum microframe interval.
606
607 The parameter is modifiable at runtime as it is used when activating new
608 endpoints (such as on device connect).
609
610 dwc_otg: fiq_fsm: Add non-periodic TT exclusivity constraints
611
612 Certain hub types do not discriminate between pipe direction (IN or OUT)
613 when considering non-periodic transfers. Therefore these hubs get confused
614 if multiple transfers are issued in different directions with the same
615 device address and endpoint number.
616
617 Constrain queuing non-periodic split transactions so they are performed
618 serially in such cases.
619
620 Related: https://github.com/raspberrypi/linux/issues/2024
621
622 dwc_otg: Fixup change to DRIVER_ATTR interface
623
624 dwc_otg: Fix compilation warnings
625
626 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
627
628 USB_DWCOTG: Disable building dwc_otg as a module (#2265)
629
630 When dwc_otg is built as a module, build will fail with the following
631 error:
632
633 ERROR: "DWC_TASK_HI_SCHEDULE" [drivers/usb/host/dwc_otg/dwc_otg.ko] undefined!
634 scripts/Makefile.modpost:91: recipe for target '__modpost' failed
635 make[1]: *** [__modpost] Error 1
636 Makefile:1199: recipe for target 'modules' failed
637 make: *** [modules] Error 2
638
639 Even if the error is solved by including the missing
640 DWC_TASK_HI_SCHEDULE function, the kernel will panic when loading
641 dwc_otg.
642
643 As a workaround, simply prevent user from building dwc_otg as a module
644 as the current kernel does not support it.
645
646 See: https://github.com/raspberrypi/linux/issues/2258
647
648 Signed-off-by: Malik Olivier Boussejra <malik@boussejra.com>
649
650 dwc_otg: New timer API
651
652 dwc_otg: Fix removed ACCESS_ONCE->READ_ONCE
653
654 dwc_otg: don't unconditionally force host mode in dwc_otg_cil_init()
655
656 Add the ability to disable force_host_mode for those that want to use
657 dwc_otg in both device and host modes.
658
659 dwc_otg: Fix a regression when dequeueing isochronous transfers
660
661 In 282bed95 (dwc_otg: make nak_holdoff work as intended with empty queues)
662 the dequeue mechanism was changed to leave FIQ-enabled transfers to run
663 to completion - to avoid leaving hub TT buffers with stale packets lying
664 around.
665
666 This broke FIQ-accelerated isochronous transfers, as this then meant that
667 dozens of transfers were performed after the dequeue function returned.
668
669 Restore the state machine fence for isochronous transfers.
670
671 fiq_fsm: rewind DMA pointer for OUT transactions that fail (#2288)
672
673 See: https://github.com/raspberrypi/linux/issues/2140
674
675 dwc_otg: add smp_mb() to prevent driver state corruption on boot
676
677 Occasional crashes have been seen where the FIQ code dereferences
678 invalid/random pointers immediately after being set up, leading to
679 panic on boot.
680
681 The crash occurs as the FIQ code races against hcd_init_fiq() and
682 the hcd_init_fiq() code races against the outstanding memory stores
683 from dwc_otg_hcd_init(). Use explicit barriers after touching
684 driver state.
685
686 usb: dwc_otg: fix memory corruption in dwc_otg driver
687
688 [Upstream commit 51b1b6491752ac066ee8d32cc66042fcc955fef6]
689
690 The move from the staging tree to the main tree exposed a
691 longstanding memory corruption bug in the dwc2 driver. The
692 reordering of the driver initialization caused the dwc2 driver
693 to corrupt the initialization data of the sdhci driver on the
694 Raspberry Pi platform, which made the bug show up.
695
696 The error is in calling to_usb_device(hsotg->dev), since ->dev
697 is not a member of struct usb_device. The easiest fix is to
698 just remove the offending code, since it is not really needed.
699
700 Thanks to Stephen Warren for tracking down the cause of this.
701
702 Reported-by: Andre Heider <a.heider@gmail.com>
703 Tested-by: Stephen Warren <swarren@wwwdotorg.org>
704 Signed-off-by: Paul Zimmerman <paulz@synopsys.com>
705 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
706 [lukas: port from upstream dwc2 to out-of-tree dwc_otg driver]
707 Signed-off-by: Lukas Wunner <lukas@wunner.de>
708
709 usb: dwb_otg: Fix unreachable switch statement warning
710
711 This warning appears with GCC 7.3.0 from toolchains.bootlin.com:
712
713 ../drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c: In function ‘fiq_fsm_update_hs_isoc’:
714 ../drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c:595:61: warning: statement will never be executed [-Wswitch-unreachable]
715 st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
716 ~~~~~~~~~~~~~~~~~^~~~
717
718 Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
719
720 dwc_otg: fiq_fsm: fix incorrect DMA register offset calculation
721
722 Rationalise the offset and update all call sites.
723
724 Fixes https://github.com/raspberrypi/linux/issues/2408
725
726 dwc_otg: fix bug with port_addr assignment for single-TT hubs
727
728 See https://github.com/raspberrypi/linux/issues/2734
729
730 The "Hub Port" field in the split transaction packet was always set
731 to 1 for single-TT hubs. The majority of single-TT hub products
732 apparently ignore this field and broadcast to all downstream enabled
733 ports, which masked the issue. A subset of hub devices apparently
734 need the port number to be exact or split transactions will fail.
735
736 usb: dwc_otg: Clean up build warnings on 64bit kernels
737
738 No functional changes. Almost all are changes to logging lines.
739
740 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
741
742 usb: dwc_otg: Use dma allocation for mphi dummy_send buffer
743
744 The FIQ driver used a kzalloc'ed buffer for dummy_send,
745 passing a kernel virtual address to the hardware block.
746 The buffer is only ever used for a dummy read, so it
747 should be harmless, but there is the chance that it will
748 cause exceptions.
749
750 Use a dma allocation so that we have a genuine bus address,
751 and read from that.
752 Free the allocation when done for good measure.
753
754 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
755
756 dwc_otg: only do_split when we actually need to do a split
757
758 The previous test would fail if the root port was in fullspeed mode
759 and there was a hub between the FS device and the root port. While
760 the transfer worked, the schedule mangling performed for high-speed
761 split transfers would break leading to an 8ms polling interval.
762
763 dwc_otg: fix locking around dequeueing and killing URBs
764
765 kill_urbs_in_qh_list() is practically only ever called with the fiq lock
766 already held, so don't spinlock twice in the case where we need to cancel
767 an isochronous transfer.
768
769 Also fix up a case where the global interrupt register could be read with
770 the fiq lock not held.
771
772 Fixes the deadlock seen in https://github.com/raspberrypi/linux/issues/2907
773
774 ARM64/DWC_OTG: Port dwc_otg driver to ARM64
775
776 In ARM64, the FIQ mechanism used by this driver is not current
777 implemented. As a workaround, reqular IRQ is used instead
778 of FIQ.
779
780 In a separate change, the IRQ-CPU mapping is round robined
781 on ARM64 to increase concurrency and allow multiple interrupts
782 to be serviced at a time. This reduces the need for FIQ.
783
784 Tests Run:
785
786 This mechanism is most likely to break when multiple USB devices
787 are attached at the same time. So the system was tested under
788 stress.
789
790 Devices:
791
792 1. USB Speakers playing back a FLAC audio through VLC
793 at 96KHz.(Higher then typically, but supported on my speakers).
794
795 2. sftp transferring large files through the buildin ethernet
796 connection which is connected through USB.
797
798 3. Keyboard and mouse attached and being used.
799
800 Although I do occasionally hear some glitches, the music seems to
801 play quite well.
802
803 Signed-off-by: Michael Zoran <mzoran@crowfest.net>
804
805 usb: dwc_otg: Clean up interrupt claiming code
806
807 The FIQ/IRQ interrupt number identification code is scattered through
808 the dwc_otg driver. Rationalise it, simplifying the code and solving
809 an existing issue.
810
811 See: https://github.com/raspberrypi/linux/issues/2612
812
813 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
814
815 dwc_otg: Choose appropriate IRQ handover strategy
816
817 2711 has no MPHI peripheral, but the ARM Control block can fake
818 interrupts. Use the size of the DTB "mphi" reg block to determine
819 which is required.
820
821 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
822
823 usb: host: dwc_otg: fix compiling in separate directory
824
825 The dwc_otg Makefile does not respect the O=path argument correctly:
826 include paths in CFLAGS are given relatively to object path, not source
827 path. Compiling in a separate directory yields #include errors.
828
829 Signed-off-by: Marek Behún <marek.behun@nic.cz>
830
831 dwc_otg: use align_buf for small IN control transfers (#3150)
832
833 The hardware will do a 4-byte write to memory on any IN packet received
834 that is between 1 and 3 bytes long. This tramples memory in the uvcvideo
835 driver, as it uses a sequence of 1- and 2-byte control transfers to
836 query the min/max/range/step of each individual camera control and
837 gives us buffers that are offsets into a struct.
838
839 Catch small control transfers in the data phase and use the align_buf
840 to bounce the correct number of bytes into the URB's buffer.
841
842 In general, short packets on non-control endpoints should be OK as URBs
843 should have enough buffer space for a wMaxPacket size transfer.
844
845 See: https://github.com/raspberrypi/linux/issues/3148
846
847 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
848
849 dwc_otg: Declare DMA capability with HCD_DMA flag
850
851 Following [1], USB controllers have to declare DMA capabilities in
852 order for them to be used by adding the HCD_DMA flag to their hc_driver
853 struct.
854
855 [1] 7b81cb6bddd2 ("usb: add a HCD_DMA flag instead of guestimating DMA capabilities")
856
857 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
858
859 dwc_otg: checking the urb->transfer_buffer too early (#3332)
860
861 After enable the HIGHMEM and VMSPLIT_3G, the dwc_otg driver doesn't
862 work well on Pi2/3 boards with 1G physical ram. Users experience
863 the failure when copying a file of 600M size to the USB stick. And
864 at the same time, the dmesg shows:
865 usb 1-1.1.2: reset high-speed USB device number 8 using dwc_otg
866 sd 0:0:0:0: [sda] tag#0 FAILED Result: hostbyte=DID_ERROR driverbyte=DRIVER_OK
867 blk_update_request: I/O error, dev sda, sector 3024048 op 0x1:(WRITE) flags 0x4000 phys_seg 15 prio class 0
868
869 When this happens, the sg_buf sent to the driver is located in the
870 highmem region, the usb_sg_init() in the core/message.c will leave
871 transfer_buffer to NULL if the sg_buf is in highmem, but in the
872 dwc_otg driver, it returns -EINVAL unconditionally if transfer_buffer
873 is NULL.
874
875 The driver can handle the situation of buffer to be NULL, if it is in
876 DMA mode, it will convert an address from transfer_dma.
877
878 But if the conversion fails or it is in the PIO mode, we should check
879 buffer and return -EINVAL if it is NULL.
880
881 BugLink: https://bugs.launchpad.net/bugs/1852510
882 Signed-off-by: Hui Wang <hui.wang@canonical.com>
883
884 dwc_otg: constrain endpoint max packet and transfer size on split IN
885
886 The hcd would unconditionally set the transfer length to the endpoint
887 packet size for non-isoc IN transfers. If the remaining buffer length
888 was less than the length of returned data, random memory would get
889 scribbled over, with bad effects if it crossed a page boundary.
890
891 Force a babble error if this happens by limiting the max transfer size
892 to the available buffer space. DMA will stop writing to memory on a
893 babble condition.
894
895 The hardware expects xfersize to be an integer multiple of maxpacket
896 size, so override hcchar.b.mps as well.
897
898 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
899
900 dwc_otg: fiq_fsm: pause when cancelling split transactions
901
902 Non-periodic splits will DMA to/from the driver-provided transfer_buffer,
903 which may be freed immediately after the dequeue call returns. Block until
904 we know the transfer is complete.
905
906 A similar delay is needed when cleaning up disconnects, as the FIQ could
907 have started a periodic transfer in the previous microframe to the one
908 that triggered a disconnect.
909
910 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
911
912 dwc_otg: fiq_fsm: add a barrier on entry into FIQ handler(s)
913
914 On BCM2835, there is no hardware guarantee that multiple outstanding
915 reads to different peripherals will complete in-order. The FIQ code
916 uses peripheral reads without barriers for performance, so in the case
917 where a read to a slow peripheral was issued immediately prior to FIQ
918 entry, the first peripheral read that the FIQ did could end up with
919 wrong read data returned.
920
921 Add dsb(sy) on entry so that all outstanding reads are retired.
922
923 The FIQ only issues reads to the dwc_otg core, so per-read barriers
924 in the handler itself are not required.
925
926 On BCM2836 and BCM2837 the barrier is not strictly required due to
927 differences in how the peripheral bus is implemented, but having
928 arch-specific handlers that introduce different latencies is risky.
929
930 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
931
932 dwc_otg: whitelist_table is now productlist_table
933
934 dwc_otg: initialise sched_frame for periodic QHs that were parked
935
936 If a periodic QH has no remaining QTDs, then it is removed from all
937 periodic schedules. When re-adding, initialise the sched_frame and
938 start_split_frame from the current value of the frame counter.
939
940 See https://bugs.launchpad.net/raspbian/+bug/1819560
941 and
942 https://github.com/raspberrypi/linux/issues/3883
943
944 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
945
946 dwc_otg: Minimise header and fix build warnings
947
948 Delete a large amount of unused declaration from "usb.h", some of which
949 were causing build warnings, and get the module building cleanly.
950
951 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
952
953 dwc-otg: fix clang -Wignored-attributes warning
954
955 warning: attribute declaration must precede definition
956
957 dwc-otg: fix clang -Wsometimes-uninitialized warning
958
959 warning: variable 'retval' is used uninitialized whenever 'if' condition is false
960
961 dwc-otg: fix clang -Wpointer-bool-conversion warning
962
963 warning: address of array 'desc->wMaxPacketSize' will always evaluate to 'true'
964
965 The wMaxPacketSize field is actually a two element array which content should
966 be accessed via the UGETW macro.
967
968 dwc_otg: fix an undeclared variable
969 Replace an undeclared variable used by DWC_DEBUGPL with the real endpoint address. DWC_DEBUGPL does nothing with DEBUG undefined so it did not go wrong before.
970 Signed-off-by: Zixuan Wang <wangzixuan@sjtu.edu.cn>
971
972 dwc_otg: Update NetBSD usb.h header licence
973
974 NetBSD have changed their licensing requirements such that the 2-clause
975 licence is preferred. Update usb.h in the downstream dwc_otg code
976 accordingly.
977
978 See https://www.netbsd.org/about/redistribution.html for more
979 information.
980
981 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
982
983 dwc_otg: pay attention to qh->interval when rescheduling periodic queues
984
985 A regression introduced in https://github.com/raspberrypi/linux/pull/3887
986 meant that if the newly scheduled transfer immediately returned data, and
987 the driver resubmitted a single URB after every transfer, then the effective
988 polling interval would end up being approx 1ms.
989
990 Use the larger of SCHEDULE_SLOP or the configured endpoint interval.
991
992 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
993 ---
994 arch/arm/include/asm/irqflags.h | 16 +-
995 arch/arm/kernel/fiqasm.S | 4 +
996 drivers/usb/Makefile | 1 +
997 drivers/usb/core/generic.c | 1 +
998 drivers/usb/core/hub.c | 2 +-
999 drivers/usb/core/message.c | 79 +
1000 drivers/usb/core/otg_productlist.h | 114 +-
1001 drivers/usb/gadget/file_storage.c | 3676 +++++++++
1002 drivers/usb/host/Kconfig | 10 +
1003 drivers/usb/host/Makefile | 1 +
1004 drivers/usb/host/dwc_common_port/Makefile | 58 +
1005 .../usb/host/dwc_common_port/Makefile.fbsd | 17 +
1006 .../usb/host/dwc_common_port/Makefile.linux | 49 +
1007 drivers/usb/host/dwc_common_port/changes.txt | 174 +
1008 .../usb/host/dwc_common_port/doc/doxygen.cfg | 270 +
1009 drivers/usb/host/dwc_common_port/dwc_cc.c | 532 ++
1010 drivers/usb/host/dwc_common_port/dwc_cc.h | 224 +
1011 .../host/dwc_common_port/dwc_common_fbsd.c | 1308 +++
1012 .../host/dwc_common_port/dwc_common_linux.c | 1409 ++++
1013 .../host/dwc_common_port/dwc_common_nbsd.c | 1275 +++
1014 drivers/usb/host/dwc_common_port/dwc_crypto.c | 308 +
1015 drivers/usb/host/dwc_common_port/dwc_crypto.h | 111 +
1016 drivers/usb/host/dwc_common_port/dwc_dh.c | 291 +
1017 drivers/usb/host/dwc_common_port/dwc_dh.h | 106 +
1018 drivers/usb/host/dwc_common_port/dwc_list.h | 594 ++
1019 drivers/usb/host/dwc_common_port/dwc_mem.c | 245 +
1020 drivers/usb/host/dwc_common_port/dwc_modpow.c | 636 ++
1021 drivers/usb/host/dwc_common_port/dwc_modpow.h | 34 +
1022 .../usb/host/dwc_common_port/dwc_notifier.c | 319 +
1023 .../usb/host/dwc_common_port/dwc_notifier.h | 122 +
1024 drivers/usb/host/dwc_common_port/dwc_os.h | 1275 +++
1025 drivers/usb/host/dwc_common_port/usb.h | 275 +
1026 drivers/usb/host/dwc_otg/Makefile | 85 +
1027 drivers/usb/host/dwc_otg/doc/doxygen.cfg | 224 +
1028 drivers/usb/host/dwc_otg/dummy_audio.c | 1574 ++++
1029 drivers/usb/host/dwc_otg/dwc_cfi_common.h | 142 +
1030 drivers/usb/host/dwc_otg/dwc_otg_adp.c | 854 ++
1031 drivers/usb/host/dwc_otg/dwc_otg_adp.h | 80 +
1032 drivers/usb/host/dwc_otg/dwc_otg_attr.c | 1212 +++
1033 drivers/usb/host/dwc_otg/dwc_otg_attr.h | 89 +
1034 drivers/usb/host/dwc_otg/dwc_otg_cfi.c | 1876 +++++
1035 drivers/usb/host/dwc_otg/dwc_otg_cfi.h | 320 +
1036 drivers/usb/host/dwc_otg/dwc_otg_cil.c | 7146 +++++++++++++++++
1037 drivers/usb/host/dwc_otg/dwc_otg_cil.h | 1464 ++++
1038 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c | 1601 ++++
1039 drivers/usb/host/dwc_otg/dwc_otg_core_if.h | 705 ++
1040 drivers/usb/host/dwc_otg/dwc_otg_dbg.h | 117 +
1041 drivers/usb/host/dwc_otg/dwc_otg_driver.c | 1772 ++++
1042 drivers/usb/host/dwc_otg/dwc_otg_driver.h | 86 +
1043 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 1433 ++++
1044 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 399 +
1045 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S | 80 +
1046 drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 4363 ++++++++++
1047 drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 870 ++
1048 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c | 1135 +++
1049 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h | 421 +
1050 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 2757 +++++++
1051 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 1086 +++
1052 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c | 974 +++
1053 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h | 200 +
1054 drivers/usb/host/dwc_otg/dwc_otg_pcd.c | 2725 +++++++
1055 drivers/usb/host/dwc_otg/dwc_otg_pcd.h | 273 +
1056 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h | 361 +
1057 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c | 5148 ++++++++++++
1058 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c | 1262 +++
1059 drivers/usb/host/dwc_otg/dwc_otg_regs.h | 2550 ++++++
1060 drivers/usb/host/dwc_otg/test/Makefile | 16 +
1061 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm | 337 +
1062 .../usb/host/dwc_otg/test/test_mod_param.pl | 133 +
1063 drivers/usb/host/dwc_otg/test/test_sysfs.pl | 193 +
1064 70 files changed, 59583 insertions(+), 16 deletions(-)
1065 create mode 100644 drivers/usb/gadget/file_storage.c
1066 create mode 100644 drivers/usb/host/dwc_common_port/Makefile
1067 create mode 100644 drivers/usb/host/dwc_common_port/Makefile.fbsd
1068 create mode 100644 drivers/usb/host/dwc_common_port/Makefile.linux
1069 create mode 100644 drivers/usb/host/dwc_common_port/changes.txt
1070 create mode 100644 drivers/usb/host/dwc_common_port/doc/doxygen.cfg
1071 create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.c
1072 create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.h
1073 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
1074 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_linux.c
1075 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
1076 create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.c
1077 create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.h
1078 create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.c
1079 create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.h
1080 create mode 100644 drivers/usb/host/dwc_common_port/dwc_list.h
1081 create mode 100644 drivers/usb/host/dwc_common_port/dwc_mem.c
1082 create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.c
1083 create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.h
1084 create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.c
1085 create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.h
1086 create mode 100644 drivers/usb/host/dwc_common_port/dwc_os.h
1087 create mode 100644 drivers/usb/host/dwc_common_port/usb.h
1088 create mode 100644 drivers/usb/host/dwc_otg/Makefile
1089 create mode 100644 drivers/usb/host/dwc_otg/doc/doxygen.cfg
1090 create mode 100644 drivers/usb/host/dwc_otg/dummy_audio.c
1091 create mode 100644 drivers/usb/host/dwc_otg/dwc_cfi_common.h
1092 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.c
1093 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.h
1094 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.c
1095 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.h
1096 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.c
1097 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.h
1098 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.c
1099 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.h
1100 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
1101 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_core_if.h
1102 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_dbg.h
1103 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.c
1104 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.h
1105 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
1106 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
1107 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
1108 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.c
1109 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.h
1110 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
1111 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
1112 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
1113 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
1114 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
1115 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
1116 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.c
1117 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.h
1118 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
1119 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
1120 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
1121 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_regs.h
1122 create mode 100644 drivers/usb/host/dwc_otg/test/Makefile
1123 create mode 100644 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
1124 create mode 100644 drivers/usb/host/dwc_otg/test/test_mod_param.pl
1125 create mode 100644 drivers/usb/host/dwc_otg/test/test_sysfs.pl
1126
1127 --- a/arch/arm/include/asm/irqflags.h
1128 +++ b/arch/arm/include/asm/irqflags.h
1129 @@ -163,13 +163,23 @@ static inline unsigned long arch_local_s
1130 }
1131
1132 /*
1133 - * restore saved IRQ & FIQ state
1134 + * restore saved IRQ state
1135 */
1136 #define arch_local_irq_restore arch_local_irq_restore
1137 static inline void arch_local_irq_restore(unsigned long flags)
1138 {
1139 - asm volatile(
1140 - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
1141 + unsigned long temp = 0;
1142 + flags &= ~(1 << 6);
1143 + asm volatile (
1144 + " mrs %0, cpsr"
1145 + : "=r" (temp)
1146 + :
1147 + : "memory", "cc");
1148 + /* Preserve FIQ bit */
1149 + temp &= (1 << 6);
1150 + flags = flags | temp;
1151 + asm volatile (
1152 + " msr cpsr_c, %0 @ local_irq_restore"
1153 :
1154 : "r" (flags)
1155 : "memory", "cc");
1156 --- a/arch/arm/kernel/fiqasm.S
1157 +++ b/arch/arm/kernel/fiqasm.S
1158 @@ -47,3 +47,7 @@ ENTRY(__get_fiq_regs)
1159 mov r0, r0 @ avoid hazard prior to ARMv4
1160 ret lr
1161 ENDPROC(__get_fiq_regs)
1162 +
1163 +ENTRY(__FIQ_Branch)
1164 + mov pc, r8
1165 +ENDPROC(__FIQ_Branch)
1166 --- a/drivers/usb/Makefile
1167 +++ b/drivers/usb/Makefile
1168 @@ -9,6 +9,7 @@ obj-$(CONFIG_USB_COMMON) += common/
1169 obj-$(CONFIG_USB) += core/
1170 obj-$(CONFIG_USB_SUPPORT) += phy/
1171
1172 +obj-$(CONFIG_USB_DWCOTG) += host/
1173 obj-$(CONFIG_USB_DWC3) += dwc3/
1174 obj-$(CONFIG_USB_DWC2) += dwc2/
1175 obj-$(CONFIG_USB_ISP1760) += isp1760/
1176 --- a/drivers/usb/core/generic.c
1177 +++ b/drivers/usb/core/generic.c
1178 @@ -190,6 +190,7 @@ int usb_choose_configuration(struct usb_
1179 dev_warn(&udev->dev,
1180 "no configuration chosen from %d choice%s\n",
1181 num_configs, plural(num_configs));
1182 + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
1183 }
1184 return i;
1185 }
1186 --- a/drivers/usb/core/hub.c
1187 +++ b/drivers/usb/core/hub.c
1188 @@ -5669,7 +5669,7 @@ static void port_event(struct usb_hub *h
1189 port_dev->over_current_count++;
1190 port_over_current_notify(port_dev);
1191
1192 - dev_dbg(&port_dev->dev, "over-current change #%u\n",
1193 + dev_notice(&port_dev->dev, "over-current change #%u\n",
1194 port_dev->over_current_count);
1195 usb_clear_port_feature(hdev, port1,
1196 USB_PORT_FEAT_C_OVER_CURRENT);
1197 --- a/drivers/usb/core/message.c
1198 +++ b/drivers/usb/core/message.c
1199 @@ -2135,6 +2135,85 @@ free_interfaces:
1200 if (cp->string == NULL &&
1201 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
1202 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
1203 +/* Uncomment this define to enable the HS Electrical Test support */
1204 +#define DWC_HS_ELECT_TST 1
1205 +#ifdef DWC_HS_ELECT_TST
1206 + /* Here we implement the HS Electrical Test support. The
1207 + * tester uses a vendor ID of 0x1A0A to indicate we should
1208 + * run a special test sequence. The product ID tells us
1209 + * which sequence to run. We invoke the test sequence by
1210 + * sending a non-standard SetFeature command to our root
1211 + * hub port. Our dwc_otg_hcd_hub_control() routine will
1212 + * recognize the command and perform the desired test
1213 + * sequence.
1214 + */
1215 + if (dev->descriptor.idVendor == 0x1A0A) {
1216 + /* HSOTG Electrical Test */
1217 + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
1218 +
1219 + if (dev->bus && dev->bus->root_hub) {
1220 + struct usb_device *hdev = dev->bus->root_hub;
1221 + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
1222 +
1223 + switch (dev->descriptor.idProduct) {
1224 + case 0x0101: /* TEST_SE0_NAK */
1225 + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
1226 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1227 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1228 + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
1229 + break;
1230 +
1231 + case 0x0102: /* TEST_J */
1232 + dev_warn(&dev->dev, "TEST_J\n");
1233 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1234 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1235 + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
1236 + break;
1237 +
1238 + case 0x0103: /* TEST_K */
1239 + dev_warn(&dev->dev, "TEST_K\n");
1240 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1241 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1242 + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
1243 + break;
1244 +
1245 + case 0x0104: /* TEST_PACKET */
1246 + dev_warn(&dev->dev, "TEST_PACKET\n");
1247 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1248 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1249 + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
1250 + break;
1251 +
1252 + case 0x0105: /* TEST_FORCE_ENABLE */
1253 + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
1254 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1255 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1256 + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
1257 + break;
1258 +
1259 + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
1260 + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
1261 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1262 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1263 + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
1264 + break;
1265 +
1266 + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
1267 + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
1268 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1269 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1270 + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
1271 + break;
1272 +
1273 + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
1274 + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
1275 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1276 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1277 + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
1278 + }
1279 + }
1280 + }
1281 +#endif /* DWC_HS_ELECT_TST */
1282
1283 /* Now that the interfaces are installed, re-enable LPM. */
1284 usb_unlocked_enable_lpm(dev);
1285 --- a/drivers/usb/core/otg_productlist.h
1286 +++ b/drivers/usb/core/otg_productlist.h
1287 @@ -11,33 +11,82 @@
1288 static struct usb_device_id productlist_table[] = {
1289
1290 /* hubs are optional in OTG, but very handy ... */
1291 +#define CERT_WITHOUT_HUBS
1292 +#if defined(CERT_WITHOUT_HUBS)
1293 +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
1294 +#else
1295 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
1296 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
1297 +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
1298 +#endif
1299
1300 #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
1301 /* FIXME actually, printers are NOT supposed to use device classes;
1302 * they're supposed to use interface classes...
1303 */
1304 -{ USB_DEVICE_INFO(7, 1, 1) },
1305 -{ USB_DEVICE_INFO(7, 1, 2) },
1306 -{ USB_DEVICE_INFO(7, 1, 3) },
1307 +//{ USB_DEVICE_INFO(7, 1, 1) },
1308 +//{ USB_DEVICE_INFO(7, 1, 2) },
1309 +//{ USB_DEVICE_INFO(7, 1, 3) },
1310 #endif
1311
1312 #ifdef CONFIG_USB_NET_CDCETHER
1313 /* Linux-USB CDC Ethernet gadget */
1314 -{ USB_DEVICE(0x0525, 0xa4a1), },
1315 +//{ USB_DEVICE(0x0525, 0xa4a1), },
1316 /* Linux-USB CDC Ethernet + RNDIS gadget */
1317 -{ USB_DEVICE(0x0525, 0xa4a2), },
1318 +//{ USB_DEVICE(0x0525, 0xa4a2), },
1319 #endif
1320
1321 #if IS_ENABLED(CONFIG_USB_TEST)
1322 /* gadget zero, for testing */
1323 -{ USB_DEVICE(0x0525, 0xa4a0), },
1324 +//{ USB_DEVICE(0x0525, 0xa4a0), },
1325 #endif
1326
1327 +/* OPT Tester */
1328 +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
1329 +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
1330 +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
1331 +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
1332 +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
1333 +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
1334 +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
1335 +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
1336 +
1337 +/* Sony cameras */
1338 +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
1339 +
1340 +/* Memory Devices */
1341 +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
1342 +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
1343 +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
1344 +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
1345 +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
1346 +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
1347 +
1348 +/* HP Printers */
1349 +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
1350 +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
1351 +
1352 +/* Speakers */
1353 +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
1354 +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
1355 +
1356 { } /* Terminating entry */
1357 };
1358
1359 +static inline void report_errors(struct usb_device *dev)
1360 +{
1361 + /* OTG MESSAGE: report errors here, customize to match your product */
1362 + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
1363 + le16_to_cpu(dev->descriptor.idVendor),
1364 + le16_to_cpu(dev->descriptor.idProduct));
1365 + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
1366 + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
1367 + } else {
1368 + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
1369 + }
1370 +}
1371 +
1372 +
1373 static int is_targeted(struct usb_device *dev)
1374 {
1375 struct usb_device_id *id = productlist_table;
1376 @@ -87,16 +136,57 @@ static int is_targeted(struct usb_device
1377 continue;
1378
1379 return 1;
1380 - }
1381 + /* NOTE: can't use usb_match_id() since interface caches
1382 + * aren't set up yet. this is cut/paste from that code.
1383 + */
1384 + for (id = productlist_table; id->match_flags; id++) {
1385 +#ifdef DEBUG
1386 + dev_dbg(&dev->dev,
1387 + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
1388 + id->idVendor,
1389 + id->idProduct,
1390 + id->bDeviceClass,
1391 + id->bDeviceSubClass,
1392 + id->bDeviceProtocol);
1393 +#endif
1394
1395 - /* add other match criteria here ... */
1396 + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
1397 + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
1398 + continue;
1399 +
1400 + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
1401 + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
1402 + continue;
1403 +
1404 + /* No need to test id->bcdDevice_lo != 0, since 0 is never
1405 + greater than any unsigned number. */
1406 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
1407 + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
1408 + continue;
1409 +
1410 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
1411 + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
1412 + continue;
1413 +
1414 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
1415 + (id->bDeviceClass != dev->descriptor.bDeviceClass))
1416 + continue;
1417 +
1418 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
1419 + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
1420 + continue;
1421 +
1422 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
1423 + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
1424 + continue;
1425
1426 + return 1;
1427 + }
1428 + }
1429
1430 - /* OTG MESSAGE: report errors here, customize to match your product */
1431 - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
1432 - le16_to_cpu(dev->descriptor.idVendor),
1433 - le16_to_cpu(dev->descriptor.idProduct));
1434 + /* add other match criteria here ... */
1435
1436 + report_errors(dev);
1437 return 0;
1438 }
1439
1440 --- /dev/null
1441 +++ b/drivers/usb/gadget/file_storage.c
1442 @@ -0,0 +1,3676 @@
1443 +/*
1444 + * file_storage.c -- File-backed USB Storage Gadget, for USB development
1445 + *
1446 + * Copyright (C) 2003-2008 Alan Stern
1447 + * All rights reserved.
1448 + *
1449 + * Redistribution and use in source and binary forms, with or without
1450 + * modification, are permitted provided that the following conditions
1451 + * are met:
1452 + * 1. Redistributions of source code must retain the above copyright
1453 + * notice, this list of conditions, and the following disclaimer,
1454 + * without modification.
1455 + * 2. Redistributions in binary form must reproduce the above copyright
1456 + * notice, this list of conditions and the following disclaimer in the
1457 + * documentation and/or other materials provided with the distribution.
1458 + * 3. The names of the above-listed copyright holders may not be used
1459 + * to endorse or promote products derived from this software without
1460 + * specific prior written permission.
1461 + *
1462 + * ALTERNATIVELY, this software may be distributed under the terms of the
1463 + * GNU General Public License ("GPL") as published by the Free Software
1464 + * Foundation, either version 2 of that License or (at your option) any
1465 + * later version.
1466 + *
1467 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1468 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1469 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1470 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1471 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1472 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1473 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1474 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1475 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1476 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1477 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1478 + */
1479 +
1480 +
1481 +/*
1482 + * The File-backed Storage Gadget acts as a USB Mass Storage device,
1483 + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
1484 + * to providing an example of a genuinely useful gadget driver for a USB
1485 + * device, it also illustrates a technique of double-buffering for increased
1486 + * throughput. Last but not least, it gives an easy way to probe the
1487 + * behavior of the Mass Storage drivers in a USB host.
1488 + *
1489 + * Backing storage is provided by a regular file or a block device, specified
1490 + * by the "file" module parameter. Access can be limited to read-only by
1491 + * setting the optional "ro" module parameter. (For CD-ROM emulation,
1492 + * access is always read-only.) The gadget will indicate that it has
1493 + * removable media if the optional "removable" module parameter is set.
1494 + *
1495 + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
1496 + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
1497 + * by the optional "transport" module parameter. It also supports the
1498 + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
1499 + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
1500 + * the optional "protocol" module parameter. In addition, the default
1501 + * Vendor ID, Product ID, release number and serial number can be overridden.
1502 + *
1503 + * There is support for multiple logical units (LUNs), each of which has
1504 + * its own backing file. The number of LUNs can be set using the optional
1505 + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
1506 + * files are specified using comma-separated lists for "file" and "ro".
1507 + * The default number of LUNs is taken from the number of "file" elements;
1508 + * it is 1 if "file" is not given. If "removable" is not set then a backing
1509 + * file must be specified for each LUN. If it is set, then an unspecified
1510 + * or empty backing filename means the LUN's medium is not loaded. Ideally
1511 + * each LUN would be settable independently as a disk drive or a CD-ROM
1512 + * drive, but currently all LUNs have to be the same type. The CD-ROM
1513 + * emulation includes a single data track and no audio tracks; hence there
1514 + * need be only one backing file per LUN.
1515 + *
1516 + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
1517 + * needed (an interrupt-out endpoint is also needed for CBI). The memory
1518 + * requirement amounts to two 16K buffers, size configurable by a parameter.
1519 + * Support is included for both full-speed and high-speed operation.
1520 + *
1521 + * Note that the driver is slightly non-portable in that it assumes a
1522 + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
1523 + * interrupt-in endpoints. With most device controllers this isn't an
1524 + * issue, but there may be some with hardware restrictions that prevent
1525 + * a buffer from being used by more than one endpoint.
1526 + *
1527 + * Module options:
1528 + *
1529 + * file=filename[,filename...]
1530 + * Required if "removable" is not set, names of
1531 + * the files or block devices used for
1532 + * backing storage
1533 + * serial=HHHH... Required serial number (string of hex chars)
1534 + * ro=b[,b...] Default false, booleans for read-only access
1535 + * removable Default false, boolean for removable media
1536 + * luns=N Default N = number of filenames, number of
1537 + * LUNs to support
1538 + * nofua=b[,b...] Default false, booleans for ignore FUA flag
1539 + * in SCSI WRITE(10,12) commands
1540 + * stall Default determined according to the type of
1541 + * USB device controller (usually true),
1542 + * boolean to permit the driver to halt
1543 + * bulk endpoints
1544 + * cdrom Default false, boolean for whether to emulate
1545 + * a CD-ROM drive
1546 + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
1547 + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
1548 + * ATAPI, QIC, UFI, 8070, or SCSI;
1549 + * also 1 - 6)
1550 + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
1551 + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
1552 + * release=0xRRRR Override the USB release number (bcdDevice)
1553 + * buflen=N Default N=16384, buffer size used (will be
1554 + * rounded down to a multiple of
1555 + * PAGE_CACHE_SIZE)
1556 + *
1557 + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
1558 + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
1559 + * default values are used for everything else.
1560 + *
1561 + * The pathnames of the backing files and the ro settings are available in
1562 + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
1563 + * the gadget's sysfs directory. If the "removable" option is set, writing to
1564 + * these files will simulate ejecting/loading the medium (writing an empty
1565 + * line means eject) and adjusting a write-enable tab. Changes to the ro
1566 + * setting are not allowed when the medium is loaded or if CD-ROM emulation
1567 + * is being used.
1568 + *
1569 + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
1570 + * The driver's SCSI command interface was based on the "Information
1571 + * technology - Small Computer System Interface - 2" document from
1572 + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
1573 + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
1574 + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
1575 + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
1576 + * document, Revision 1.0, December 14, 1998, available at
1577 + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
1578 + */
1579 +
1580 +
1581 +/*
1582 + * Driver Design
1583 + *
1584 + * The FSG driver is fairly straightforward. There is a main kernel
1585 + * thread that handles most of the work. Interrupt routines field
1586 + * callbacks from the controller driver: bulk- and interrupt-request
1587 + * completion notifications, endpoint-0 events, and disconnect events.
1588 + * Completion events are passed to the main thread by wakeup calls. Many
1589 + * ep0 requests are handled at interrupt time, but SetInterface,
1590 + * SetConfiguration, and device reset requests are forwarded to the
1591 + * thread in the form of "exceptions" using SIGUSR1 signals (since they
1592 + * should interrupt any ongoing file I/O operations).
1593 + *
1594 + * The thread's main routine implements the standard command/data/status
1595 + * parts of a SCSI interaction. It and its subroutines are full of tests
1596 + * for pending signals/exceptions -- all this polling is necessary since
1597 + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
1598 + * indication that the driver really wants to be running in userspace.)
1599 + * An important point is that so long as the thread is alive it keeps an
1600 + * open reference to the backing file. This will prevent unmounting
1601 + * the backing file's underlying filesystem and could cause problems
1602 + * during system shutdown, for example. To prevent such problems, the
1603 + * thread catches INT, TERM, and KILL signals and converts them into
1604 + * an EXIT exception.
1605 + *
1606 + * In normal operation the main thread is started during the gadget's
1607 + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
1608 + * exit when it receives a signal, and there's no point leaving the
1609 + * gadget running when the thread is dead. So just before the thread
1610 + * exits, it deregisters the gadget driver. This makes things a little
1611 + * tricky: The driver is deregistered at two places, and the exiting
1612 + * thread can indirectly call fsg_unbind() which in turn can tell the
1613 + * thread to exit. The first problem is resolved through the use of the
1614 + * REGISTERED atomic bitflag; the driver will only be deregistered once.
1615 + * The second problem is resolved by having fsg_unbind() check
1616 + * fsg->state; it won't try to stop the thread if the state is already
1617 + * FSG_STATE_TERMINATED.
1618 + *
1619 + * To provide maximum throughput, the driver uses a circular pipeline of
1620 + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
1621 + * arbitrarily long; in practice the benefits don't justify having more
1622 + * than 2 stages (i.e., double buffering). But it helps to think of the
1623 + * pipeline as being a long one. Each buffer head contains a bulk-in and
1624 + * a bulk-out request pointer (since the buffer can be used for both
1625 + * output and input -- directions always are given from the host's
1626 + * point of view) as well as a pointer to the buffer and various state
1627 + * variables.
1628 + *
1629 + * Use of the pipeline follows a simple protocol. There is a variable
1630 + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
1631 + * At any time that buffer head may still be in use from an earlier
1632 + * request, so each buffer head has a state variable indicating whether
1633 + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
1634 + * buffer head to be EMPTY, filling the buffer either by file I/O or by
1635 + * USB I/O (during which the buffer head is BUSY), and marking the buffer
1636 + * head FULL when the I/O is complete. Then the buffer will be emptied
1637 + * (again possibly by USB I/O, during which it is marked BUSY) and
1638 + * finally marked EMPTY again (possibly by a completion routine).
1639 + *
1640 + * A module parameter tells the driver to avoid stalling the bulk
1641 + * endpoints wherever the transport specification allows. This is
1642 + * necessary for some UDCs like the SuperH, which cannot reliably clear a
1643 + * halt on a bulk endpoint. However, under certain circumstances the
1644 + * Bulk-only specification requires a stall. In such cases the driver
1645 + * will halt the endpoint and set a flag indicating that it should clear
1646 + * the halt in software during the next device reset. Hopefully this
1647 + * will permit everything to work correctly. Furthermore, although the
1648 + * specification allows the bulk-out endpoint to halt when the host sends
1649 + * too much data, implementing this would cause an unavoidable race.
1650 + * The driver will always use the "no-stall" approach for OUT transfers.
1651 + *
1652 + * One subtle point concerns sending status-stage responses for ep0
1653 + * requests. Some of these requests, such as device reset, can involve
1654 + * interrupting an ongoing file I/O operation, which might take an
1655 + * arbitrarily long time. During that delay the host might give up on
1656 + * the original ep0 request and issue a new one. When that happens the
1657 + * driver should not notify the host about completion of the original
1658 + * request, as the host will no longer be waiting for it. So the driver
1659 + * assigns to each ep0 request a unique tag, and it keeps track of the
1660 + * tag value of the request associated with a long-running exception
1661 + * (device-reset, interface-change, or configuration-change). When the
1662 + * exception handler is finished, the status-stage response is submitted
1663 + * only if the current ep0 request tag is equal to the exception request
1664 + * tag. Thus only the most recently received ep0 request will get a
1665 + * status-stage response.
1666 + *
1667 + * Warning: This driver source file is too long. It ought to be split up
1668 + * into a header file plus about 3 separate .c files, to handle the details
1669 + * of the Gadget, USB Mass Storage, and SCSI protocols.
1670 + */
1671 +
1672 +
1673 +/* #define VERBOSE_DEBUG */
1674 +/* #define DUMP_MSGS */
1675 +
1676 +
1677 +#include <linux/blkdev.h>
1678 +#include <linux/completion.h>
1679 +#include <linux/dcache.h>
1680 +#include <linux/delay.h>
1681 +#include <linux/device.h>
1682 +#include <linux/fcntl.h>
1683 +#include <linux/file.h>
1684 +#include <linux/fs.h>
1685 +#include <linux/kref.h>
1686 +#include <linux/kthread.h>
1687 +#include <linux/limits.h>
1688 +#include <linux/module.h>
1689 +#include <linux/rwsem.h>
1690 +#include <linux/slab.h>
1691 +#include <linux/spinlock.h>
1692 +#include <linux/string.h>
1693 +#include <linux/freezer.h>
1694 +#include <linux/utsname.h>
1695 +
1696 +#include <linux/usb/ch9.h>
1697 +#include <linux/usb/gadget.h>
1698 +
1699 +#include "gadget_chips.h"
1700 +
1701 +
1702 +
1703 +/*
1704 + * Kbuild is not very cooperative with respect to linking separately
1705 + * compiled library objects into one module. So for now we won't use
1706 + * separate compilation ... ensuring init/exit sections work to shrink
1707 + * the runtime footprint, and giving us at least some parts of what
1708 + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
1709 + */
1710 +#include "usbstring.c"
1711 +#include "config.c"
1712 +#include "epautoconf.c"
1713 +
1714 +/*-------------------------------------------------------------------------*/
1715 +
1716 +#define DRIVER_DESC "File-backed Storage Gadget"
1717 +#define DRIVER_NAME "g_file_storage"
1718 +#define DRIVER_VERSION "1 September 2010"
1719 +
1720 +static char fsg_string_manufacturer[64];
1721 +static const char fsg_string_product[] = DRIVER_DESC;
1722 +static const char fsg_string_config[] = "Self-powered";
1723 +static const char fsg_string_interface[] = "Mass Storage";
1724 +
1725 +
1726 +#include "storage_common.c"
1727 +
1728 +
1729 +MODULE_DESCRIPTION(DRIVER_DESC);
1730 +MODULE_AUTHOR("Alan Stern");
1731 +MODULE_LICENSE("Dual BSD/GPL");
1732 +
1733 +/*
1734 + * This driver assumes self-powered hardware and has no way for users to
1735 + * trigger remote wakeup. It uses autoconfiguration to select endpoints
1736 + * and endpoint addresses.
1737 + */
1738 +
1739 +
1740 +/*-------------------------------------------------------------------------*/
1741 +
1742 +
1743 +/* Encapsulate the module parameter settings */
1744 +
1745 +static struct {
1746 + char *file[FSG_MAX_LUNS];
1747 + char *serial;
1748 + bool ro[FSG_MAX_LUNS];
1749 + bool nofua[FSG_MAX_LUNS];
1750 + unsigned int num_filenames;
1751 + unsigned int num_ros;
1752 + unsigned int num_nofuas;
1753 + unsigned int nluns;
1754 +
1755 + bool removable;
1756 + bool can_stall;
1757 + bool cdrom;
1758 +
1759 + char *transport_parm;
1760 + char *protocol_parm;
1761 + unsigned short vendor;
1762 + unsigned short product;
1763 + unsigned short release;
1764 + unsigned int buflen;
1765 +
1766 + int transport_type;
1767 + char *transport_name;
1768 + int protocol_type;
1769 + char *protocol_name;
1770 +
1771 +} mod_data = { // Default values
1772 + .transport_parm = "BBB",
1773 + .protocol_parm = "SCSI",
1774 + .removable = 0,
1775 + .can_stall = 1,
1776 + .cdrom = 0,
1777 + .vendor = FSG_VENDOR_ID,
1778 + .product = FSG_PRODUCT_ID,
1779 + .release = 0xffff, // Use controller chip type
1780 + .buflen = 16384,
1781 + };
1782 +
1783 +
1784 +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
1785 + S_IRUGO);
1786 +MODULE_PARM_DESC(file, "names of backing files or devices");
1787 +
1788 +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
1789 +MODULE_PARM_DESC(serial, "USB serial number");
1790 +
1791 +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
1792 +MODULE_PARM_DESC(ro, "true to force read-only");
1793 +
1794 +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
1795 + S_IRUGO);
1796 +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
1797 +
1798 +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
1799 +MODULE_PARM_DESC(luns, "number of LUNs");
1800 +
1801 +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
1802 +MODULE_PARM_DESC(removable, "true to simulate removable media");
1803 +
1804 +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
1805 +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
1806 +
1807 +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
1808 +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
1809 +
1810 +/* In the non-TEST version, only the module parameters listed above
1811 + * are available. */
1812 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1813 +
1814 +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
1815 +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
1816 +
1817 +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
1818 +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
1819 + "8070, or SCSI)");
1820 +
1821 +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
1822 +MODULE_PARM_DESC(vendor, "USB Vendor ID");
1823 +
1824 +module_param_named(product, mod_data.product, ushort, S_IRUGO);
1825 +MODULE_PARM_DESC(product, "USB Product ID");
1826 +
1827 +module_param_named(release, mod_data.release, ushort, S_IRUGO);
1828 +MODULE_PARM_DESC(release, "USB release number");
1829 +
1830 +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
1831 +MODULE_PARM_DESC(buflen, "I/O buffer size");
1832 +
1833 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1834 +
1835 +
1836 +/*
1837 + * These definitions will permit the compiler to avoid generating code for
1838 + * parts of the driver that aren't used in the non-TEST version. Even gcc
1839 + * can recognize when a test of a constant expression yields a dead code
1840 + * path.
1841 + */
1842 +
1843 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1844 +
1845 +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
1846 +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
1847 +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
1848 +
1849 +#else
1850 +
1851 +#define transport_is_bbb() 1
1852 +#define transport_is_cbi() 0
1853 +#define protocol_is_scsi() 1
1854 +
1855 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1856 +
1857 +
1858 +/*-------------------------------------------------------------------------*/
1859 +
1860 +
1861 +struct fsg_dev {
1862 + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
1863 + spinlock_t lock;
1864 + struct usb_gadget *gadget;
1865 +
1866 + /* filesem protects: backing files in use */
1867 + struct rw_semaphore filesem;
1868 +
1869 + /* reference counting: wait until all LUNs are released */
1870 + struct kref ref;
1871 +
1872 + struct usb_ep *ep0; // Handy copy of gadget->ep0
1873 + struct usb_request *ep0req; // For control responses
1874 + unsigned int ep0_req_tag;
1875 + const char *ep0req_name;
1876 +
1877 + struct usb_request *intreq; // For interrupt responses
1878 + int intreq_busy;
1879 + struct fsg_buffhd *intr_buffhd;
1880 +
1881 + unsigned int bulk_out_maxpacket;
1882 + enum fsg_state state; // For exception handling
1883 + unsigned int exception_req_tag;
1884 +
1885 + u8 config, new_config;
1886 +
1887 + unsigned int running : 1;
1888 + unsigned int bulk_in_enabled : 1;
1889 + unsigned int bulk_out_enabled : 1;
1890 + unsigned int intr_in_enabled : 1;
1891 + unsigned int phase_error : 1;
1892 + unsigned int short_packet_received : 1;
1893 + unsigned int bad_lun_okay : 1;
1894 +
1895 + unsigned long atomic_bitflags;
1896 +#define REGISTERED 0
1897 +#define IGNORE_BULK_OUT 1
1898 +#define SUSPENDED 2
1899 +
1900 + struct usb_ep *bulk_in;
1901 + struct usb_ep *bulk_out;
1902 + struct usb_ep *intr_in;
1903 +
1904 + struct fsg_buffhd *next_buffhd_to_fill;
1905 + struct fsg_buffhd *next_buffhd_to_drain;
1906 +
1907 + int thread_wakeup_needed;
1908 + struct completion thread_notifier;
1909 + struct task_struct *thread_task;
1910 +
1911 + int cmnd_size;
1912 + u8 cmnd[MAX_COMMAND_SIZE];
1913 + enum data_direction data_dir;
1914 + u32 data_size;
1915 + u32 data_size_from_cmnd;
1916 + u32 tag;
1917 + unsigned int lun;
1918 + u32 residue;
1919 + u32 usb_amount_left;
1920 +
1921 + /* The CB protocol offers no way for a host to know when a command
1922 + * has completed. As a result the next command may arrive early,
1923 + * and we will still have to handle it. For that reason we need
1924 + * a buffer to store new commands when using CB (or CBI, which
1925 + * does not oblige a host to wait for command completion either). */
1926 + int cbbuf_cmnd_size;
1927 + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
1928 +
1929 + unsigned int nluns;
1930 + struct fsg_lun *luns;
1931 + struct fsg_lun *curlun;
1932 + /* Must be the last entry */
1933 + struct fsg_buffhd buffhds[];
1934 +};
1935 +
1936 +typedef void (*fsg_routine_t)(struct fsg_dev *);
1937 +
1938 +static int exception_in_progress(struct fsg_dev *fsg)
1939 +{
1940 + return (fsg->state > FSG_STATE_IDLE);
1941 +}
1942 +
1943 +/* Make bulk-out requests be divisible by the maxpacket size */
1944 +static void set_bulk_out_req_length(struct fsg_dev *fsg,
1945 + struct fsg_buffhd *bh, unsigned int length)
1946 +{
1947 + unsigned int rem;
1948 +
1949 + bh->bulk_out_intended_length = length;
1950 + rem = length % fsg->bulk_out_maxpacket;
1951 + if (rem > 0)
1952 + length += fsg->bulk_out_maxpacket - rem;
1953 + bh->outreq->length = length;
1954 +}
1955 +
1956 +static struct fsg_dev *the_fsg;
1957 +static struct usb_gadget_driver fsg_driver;
1958 +
1959 +
1960 +/*-------------------------------------------------------------------------*/
1961 +
1962 +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
1963 +{
1964 + const char *name;
1965 +
1966 + if (ep == fsg->bulk_in)
1967 + name = "bulk-in";
1968 + else if (ep == fsg->bulk_out)
1969 + name = "bulk-out";
1970 + else
1971 + name = ep->name;
1972 + DBG(fsg, "%s set halt\n", name);
1973 + return usb_ep_set_halt(ep);
1974 +}
1975 +
1976 +
1977 +/*-------------------------------------------------------------------------*/
1978 +
1979 +/*
1980 + * DESCRIPTORS ... most are static, but strings and (full) configuration
1981 + * descriptors are built on demand. Also the (static) config and interface
1982 + * descriptors are adjusted during fsg_bind().
1983 + */
1984 +
1985 +/* There is only one configuration. */
1986 +#define CONFIG_VALUE 1
1987 +
1988 +static struct usb_device_descriptor
1989 +device_desc = {
1990 + .bLength = sizeof device_desc,
1991 + .bDescriptorType = USB_DT_DEVICE,
1992 +
1993 + .bcdUSB = cpu_to_le16(0x0200),
1994 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
1995 +
1996 + /* The next three values can be overridden by module parameters */
1997 + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
1998 + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
1999 + .bcdDevice = cpu_to_le16(0xffff),
2000 +
2001 + .iManufacturer = FSG_STRING_MANUFACTURER,
2002 + .iProduct = FSG_STRING_PRODUCT,
2003 + .iSerialNumber = FSG_STRING_SERIAL,
2004 + .bNumConfigurations = 1,
2005 +};
2006 +
2007 +static struct usb_config_descriptor
2008 +config_desc = {
2009 + .bLength = sizeof config_desc,
2010 + .bDescriptorType = USB_DT_CONFIG,
2011 +
2012 + /* wTotalLength computed by usb_gadget_config_buf() */
2013 + .bNumInterfaces = 1,
2014 + .bConfigurationValue = CONFIG_VALUE,
2015 + .iConfiguration = FSG_STRING_CONFIG,
2016 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
2017 + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
2018 +};
2019 +
2020 +
2021 +static struct usb_qualifier_descriptor
2022 +dev_qualifier = {
2023 + .bLength = sizeof dev_qualifier,
2024 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
2025 +
2026 + .bcdUSB = cpu_to_le16(0x0200),
2027 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
2028 +
2029 + .bNumConfigurations = 1,
2030 +};
2031 +
2032 +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
2033 +{
2034 + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
2035 + buf += USB_DT_BOS_SIZE;
2036 +
2037 + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
2038 + buf += USB_DT_USB_EXT_CAP_SIZE;
2039 +
2040 + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
2041 +
2042 + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
2043 + + USB_DT_USB_EXT_CAP_SIZE;
2044 +}
2045 +
2046 +/*
2047 + * Config descriptors must agree with the code that sets configurations
2048 + * and with code managing interfaces and their altsettings. They must
2049 + * also handle different speeds and other-speed requests.
2050 + */
2051 +static int populate_config_buf(struct usb_gadget *gadget,
2052 + u8 *buf, u8 type, unsigned index)
2053 +{
2054 + enum usb_device_speed speed = gadget->speed;
2055 + int len;
2056 + const struct usb_descriptor_header **function;
2057 +
2058 + if (index > 0)
2059 + return -EINVAL;
2060 +
2061 + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
2062 + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
2063 + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
2064 + ? (const struct usb_descriptor_header **)fsg_hs_function
2065 + : (const struct usb_descriptor_header **)fsg_fs_function;
2066 +
2067 + /* for now, don't advertise srp-only devices */
2068 + if (!gadget_is_otg(gadget))
2069 + function++;
2070 +
2071 + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
2072 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
2073 + return len;
2074 +}
2075 +
2076 +
2077 +/*-------------------------------------------------------------------------*/
2078 +
2079 +/* These routines may be called in process context or in_irq */
2080 +
2081 +/* Caller must hold fsg->lock */
2082 +static void wakeup_thread(struct fsg_dev *fsg)
2083 +{
2084 + /* Tell the main thread that something has happened */
2085 + fsg->thread_wakeup_needed = 1;
2086 + if (fsg->thread_task)
2087 + wake_up_process(fsg->thread_task);
2088 +}
2089 +
2090 +
2091 +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
2092 +{
2093 + unsigned long flags;
2094 +
2095 + /* Do nothing if a higher-priority exception is already in progress.
2096 + * If a lower-or-equal priority exception is in progress, preempt it
2097 + * and notify the main thread by sending it a signal. */
2098 + spin_lock_irqsave(&fsg->lock, flags);
2099 + if (fsg->state <= new_state) {
2100 + fsg->exception_req_tag = fsg->ep0_req_tag;
2101 + fsg->state = new_state;
2102 + if (fsg->thread_task)
2103 + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
2104 + fsg->thread_task);
2105 + }
2106 + spin_unlock_irqrestore(&fsg->lock, flags);
2107 +}
2108 +
2109 +
2110 +/*-------------------------------------------------------------------------*/
2111 +
2112 +/* The disconnect callback and ep0 routines. These always run in_irq,
2113 + * except that ep0_queue() is called in the main thread to acknowledge
2114 + * completion of various requests: set config, set interface, and
2115 + * Bulk-only device reset. */
2116 +
2117 +static void fsg_disconnect(struct usb_gadget *gadget)
2118 +{
2119 + struct fsg_dev *fsg = get_gadget_data(gadget);
2120 +
2121 + DBG(fsg, "disconnect or port reset\n");
2122 + raise_exception(fsg, FSG_STATE_DISCONNECT);
2123 +}
2124 +
2125 +
2126 +static int ep0_queue(struct fsg_dev *fsg)
2127 +{
2128 + int rc;
2129 +
2130 + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
2131 + if (rc != 0 && rc != -ESHUTDOWN) {
2132 +
2133 + /* We can't do much more than wait for a reset */
2134 + WARNING(fsg, "error in submission: %s --> %d\n",
2135 + fsg->ep0->name, rc);
2136 + }
2137 + return rc;
2138 +}
2139 +
2140 +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
2141 +{
2142 + struct fsg_dev *fsg = ep->driver_data;
2143 +
2144 + if (req->actual > 0)
2145 + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
2146 + if (req->status || req->actual != req->length)
2147 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
2148 + req->status, req->actual, req->length);
2149 + if (req->status == -ECONNRESET) // Request was cancelled
2150 + usb_ep_fifo_flush(ep);
2151 +
2152 + if (req->status == 0 && req->context)
2153 + ((fsg_routine_t) (req->context))(fsg);
2154 +}
2155 +
2156 +
2157 +/*-------------------------------------------------------------------------*/
2158 +
2159 +/* Bulk and interrupt endpoint completion handlers.
2160 + * These always run in_irq. */
2161 +
2162 +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
2163 +{
2164 + struct fsg_dev *fsg = ep->driver_data;
2165 + struct fsg_buffhd *bh = req->context;
2166 +
2167 + if (req->status || req->actual != req->length)
2168 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
2169 + req->status, req->actual, req->length);
2170 + if (req->status == -ECONNRESET) // Request was cancelled
2171 + usb_ep_fifo_flush(ep);
2172 +
2173 + /* Hold the lock while we update the request and buffer states */
2174 + smp_wmb();
2175 + spin_lock(&fsg->lock);
2176 + bh->inreq_busy = 0;
2177 + bh->state = BUF_STATE_EMPTY;
2178 + wakeup_thread(fsg);
2179 + spin_unlock(&fsg->lock);
2180 +}
2181 +
2182 +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
2183 +{
2184 + struct fsg_dev *fsg = ep->driver_data;
2185 + struct fsg_buffhd *bh = req->context;
2186 +
2187 + dump_msg(fsg, "bulk-out", req->buf, req->actual);
2188 + if (req->status || req->actual != bh->bulk_out_intended_length)
2189 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
2190 + req->status, req->actual,
2191 + bh->bulk_out_intended_length);
2192 + if (req->status == -ECONNRESET) // Request was cancelled
2193 + usb_ep_fifo_flush(ep);
2194 +
2195 + /* Hold the lock while we update the request and buffer states */
2196 + smp_wmb();
2197 + spin_lock(&fsg->lock);
2198 + bh->outreq_busy = 0;
2199 + bh->state = BUF_STATE_FULL;
2200 + wakeup_thread(fsg);
2201 + spin_unlock(&fsg->lock);
2202 +}
2203 +
2204 +
2205 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
2206 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
2207 +{
2208 + struct fsg_dev *fsg = ep->driver_data;
2209 + struct fsg_buffhd *bh = req->context;
2210 +
2211 + if (req->status || req->actual != req->length)
2212 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
2213 + req->status, req->actual, req->length);
2214 + if (req->status == -ECONNRESET) // Request was cancelled
2215 + usb_ep_fifo_flush(ep);
2216 +
2217 + /* Hold the lock while we update the request and buffer states */
2218 + smp_wmb();
2219 + spin_lock(&fsg->lock);
2220 + fsg->intreq_busy = 0;
2221 + bh->state = BUF_STATE_EMPTY;
2222 + wakeup_thread(fsg);
2223 + spin_unlock(&fsg->lock);
2224 +}
2225 +
2226 +#else
2227 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
2228 +{}
2229 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
2230 +
2231 +
2232 +/*-------------------------------------------------------------------------*/
2233 +
2234 +/* Ep0 class-specific handlers. These always run in_irq. */
2235 +
2236 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
2237 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2238 +{
2239 + struct usb_request *req = fsg->ep0req;
2240 + static u8 cbi_reset_cmnd[6] = {
2241 + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
2242 +
2243 + /* Error in command transfer? */
2244 + if (req->status || req->length != req->actual ||
2245 + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
2246 +
2247 + /* Not all controllers allow a protocol stall after
2248 + * receiving control-out data, but we'll try anyway. */
2249 + fsg_set_halt(fsg, fsg->ep0);
2250 + return; // Wait for reset
2251 + }
2252 +
2253 + /* Is it the special reset command? */
2254 + if (req->actual >= sizeof cbi_reset_cmnd &&
2255 + memcmp(req->buf, cbi_reset_cmnd,
2256 + sizeof cbi_reset_cmnd) == 0) {
2257 +
2258 + /* Raise an exception to stop the current operation
2259 + * and reinitialize our state. */
2260 + DBG(fsg, "cbi reset request\n");
2261 + raise_exception(fsg, FSG_STATE_RESET);
2262 + return;
2263 + }
2264 +
2265 + VDBG(fsg, "CB[I] accept device-specific command\n");
2266 + spin_lock(&fsg->lock);
2267 +
2268 + /* Save the command for later */
2269 + if (fsg->cbbuf_cmnd_size)
2270 + WARNING(fsg, "CB[I] overwriting previous command\n");
2271 + fsg->cbbuf_cmnd_size = req->actual;
2272 + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
2273 +
2274 + wakeup_thread(fsg);
2275 + spin_unlock(&fsg->lock);
2276 +}
2277 +
2278 +#else
2279 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2280 +{}
2281 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
2282 +
2283 +
2284 +static int class_setup_req(struct fsg_dev *fsg,
2285 + const struct usb_ctrlrequest *ctrl)
2286 +{
2287 + struct usb_request *req = fsg->ep0req;
2288 + int value = -EOPNOTSUPP;
2289 + u16 w_index = le16_to_cpu(ctrl->wIndex);
2290 + u16 w_value = le16_to_cpu(ctrl->wValue);
2291 + u16 w_length = le16_to_cpu(ctrl->wLength);
2292 +
2293 + if (!fsg->config)
2294 + return value;
2295 +
2296 + /* Handle Bulk-only class-specific requests */
2297 + if (transport_is_bbb()) {
2298 + switch (ctrl->bRequest) {
2299 +
2300 + case US_BULK_RESET_REQUEST:
2301 + if (ctrl->bRequestType != (USB_DIR_OUT |
2302 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
2303 + break;
2304 + if (w_index != 0 || w_value != 0 || w_length != 0) {
2305 + value = -EDOM;
2306 + break;
2307 + }
2308 +
2309 + /* Raise an exception to stop the current operation
2310 + * and reinitialize our state. */
2311 + DBG(fsg, "bulk reset request\n");
2312 + raise_exception(fsg, FSG_STATE_RESET);
2313 + value = DELAYED_STATUS;
2314 + break;
2315 +
2316 + case US_BULK_GET_MAX_LUN:
2317 + if (ctrl->bRequestType != (USB_DIR_IN |
2318 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
2319 + break;
2320 + if (w_index != 0 || w_value != 0 || w_length != 1) {
2321 + value = -EDOM;
2322 + break;
2323 + }
2324 + VDBG(fsg, "get max LUN\n");
2325 + *(u8 *) req->buf = fsg->nluns - 1;
2326 + value = 1;
2327 + break;
2328 + }
2329 + }
2330 +
2331 + /* Handle CBI class-specific requests */
2332 + else {
2333 + switch (ctrl->bRequest) {
2334 +
2335 + case USB_CBI_ADSC_REQUEST:
2336 + if (ctrl->bRequestType != (USB_DIR_OUT |
2337 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
2338 + break;
2339 + if (w_index != 0 || w_value != 0) {
2340 + value = -EDOM;
2341 + break;
2342 + }
2343 + if (w_length > MAX_COMMAND_SIZE) {
2344 + value = -EOVERFLOW;
2345 + break;
2346 + }
2347 + value = w_length;
2348 + fsg->ep0req->context = received_cbi_adsc;
2349 + break;
2350 + }
2351 + }
2352 +
2353 + if (value == -EOPNOTSUPP)
2354 + VDBG(fsg,
2355 + "unknown class-specific control req "
2356 + "%02x.%02x v%04x i%04x l%u\n",
2357 + ctrl->bRequestType, ctrl->bRequest,
2358 + le16_to_cpu(ctrl->wValue), w_index, w_length);
2359 + return value;
2360 +}
2361 +
2362 +
2363 +/*-------------------------------------------------------------------------*/
2364 +
2365 +/* Ep0 standard request handlers. These always run in_irq. */
2366 +
2367 +static int standard_setup_req(struct fsg_dev *fsg,
2368 + const struct usb_ctrlrequest *ctrl)
2369 +{
2370 + struct usb_request *req = fsg->ep0req;
2371 + int value = -EOPNOTSUPP;
2372 + u16 w_index = le16_to_cpu(ctrl->wIndex);
2373 + u16 w_value = le16_to_cpu(ctrl->wValue);
2374 +
2375 + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
2376 + * but config change events will also reconfigure hardware. */
2377 + switch (ctrl->bRequest) {
2378 +
2379 + case USB_REQ_GET_DESCRIPTOR:
2380 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2381 + USB_RECIP_DEVICE))
2382 + break;
2383 + switch (w_value >> 8) {
2384 +
2385 + case USB_DT_DEVICE:
2386 + VDBG(fsg, "get device descriptor\n");
2387 + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
2388 + value = sizeof device_desc;
2389 + memcpy(req->buf, &device_desc, value);
2390 + break;
2391 + case USB_DT_DEVICE_QUALIFIER:
2392 + VDBG(fsg, "get device qualifier\n");
2393 + if (!gadget_is_dualspeed(fsg->gadget) ||
2394 + fsg->gadget->speed == USB_SPEED_SUPER)
2395 + break;
2396 + /*
2397 + * Assume ep0 uses the same maxpacket value for both
2398 + * speeds
2399 + */
2400 + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
2401 + value = sizeof dev_qualifier;
2402 + memcpy(req->buf, &dev_qualifier, value);
2403 + break;
2404 +
2405 + case USB_DT_OTHER_SPEED_CONFIG:
2406 + VDBG(fsg, "get other-speed config descriptor\n");
2407 + if (!gadget_is_dualspeed(fsg->gadget) ||
2408 + fsg->gadget->speed == USB_SPEED_SUPER)
2409 + break;
2410 + goto get_config;
2411 + case USB_DT_CONFIG:
2412 + VDBG(fsg, "get configuration descriptor\n");
2413 +get_config:
2414 + value = populate_config_buf(fsg->gadget,
2415 + req->buf,
2416 + w_value >> 8,
2417 + w_value & 0xff);
2418 + break;
2419 +
2420 + case USB_DT_STRING:
2421 + VDBG(fsg, "get string descriptor\n");
2422 +
2423 + /* wIndex == language code */
2424 + value = usb_gadget_get_string(&fsg_stringtab,
2425 + w_value & 0xff, req->buf);
2426 + break;
2427 +
2428 + case USB_DT_BOS:
2429 + VDBG(fsg, "get bos descriptor\n");
2430 +
2431 + if (gadget_is_superspeed(fsg->gadget))
2432 + value = populate_bos(fsg, req->buf);
2433 + break;
2434 + }
2435 +
2436 + break;
2437 +
2438 + /* One config, two speeds */
2439 + case USB_REQ_SET_CONFIGURATION:
2440 + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
2441 + USB_RECIP_DEVICE))
2442 + break;
2443 + VDBG(fsg, "set configuration\n");
2444 + if (w_value == CONFIG_VALUE || w_value == 0) {
2445 + fsg->new_config = w_value;
2446 +
2447 + /* Raise an exception to wipe out previous transaction
2448 + * state (queued bufs, etc) and set the new config. */
2449 + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
2450 + value = DELAYED_STATUS;
2451 + }
2452 + break;
2453 + case USB_REQ_GET_CONFIGURATION:
2454 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2455 + USB_RECIP_DEVICE))
2456 + break;
2457 + VDBG(fsg, "get configuration\n");
2458 + *(u8 *) req->buf = fsg->config;
2459 + value = 1;
2460 + break;
2461 +
2462 + case USB_REQ_SET_INTERFACE:
2463 + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
2464 + USB_RECIP_INTERFACE))
2465 + break;
2466 + if (fsg->config && w_index == 0) {
2467 +
2468 + /* Raise an exception to wipe out previous transaction
2469 + * state (queued bufs, etc) and install the new
2470 + * interface altsetting. */
2471 + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
2472 + value = DELAYED_STATUS;
2473 + }
2474 + break;
2475 + case USB_REQ_GET_INTERFACE:
2476 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2477 + USB_RECIP_INTERFACE))
2478 + break;
2479 + if (!fsg->config)
2480 + break;
2481 + if (w_index != 0) {
2482 + value = -EDOM;
2483 + break;
2484 + }
2485 + VDBG(fsg, "get interface\n");
2486 + *(u8 *) req->buf = 0;
2487 + value = 1;
2488 + break;
2489 +
2490 + default:
2491 + VDBG(fsg,
2492 + "unknown control req %02x.%02x v%04x i%04x l%u\n",
2493 + ctrl->bRequestType, ctrl->bRequest,
2494 + w_value, w_index, le16_to_cpu(ctrl->wLength));
2495 + }
2496 +
2497 + return value;
2498 +}
2499 +
2500 +
2501 +static int fsg_setup(struct usb_gadget *gadget,
2502 + const struct usb_ctrlrequest *ctrl)
2503 +{
2504 + struct fsg_dev *fsg = get_gadget_data(gadget);
2505 + int rc;
2506 + int w_length = le16_to_cpu(ctrl->wLength);
2507 +
2508 + ++fsg->ep0_req_tag; // Record arrival of a new request
2509 + fsg->ep0req->context = NULL;
2510 + fsg->ep0req->length = 0;
2511 + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
2512 +
2513 + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
2514 + rc = class_setup_req(fsg, ctrl);
2515 + else
2516 + rc = standard_setup_req(fsg, ctrl);
2517 +
2518 + /* Respond with data/status or defer until later? */
2519 + if (rc >= 0 && rc != DELAYED_STATUS) {
2520 + rc = min(rc, w_length);
2521 + fsg->ep0req->length = rc;
2522 + fsg->ep0req->zero = rc < w_length;
2523 + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
2524 + "ep0-in" : "ep0-out");
2525 + rc = ep0_queue(fsg);
2526 + }
2527 +
2528 + /* Device either stalls (rc < 0) or reports success */
2529 + return rc;
2530 +}
2531 +
2532 +
2533 +/*-------------------------------------------------------------------------*/
2534 +
2535 +/* All the following routines run in process context */
2536 +
2537 +
2538 +/* Use this for bulk or interrupt transfers, not ep0 */
2539 +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
2540 + struct usb_request *req, int *pbusy,
2541 + enum fsg_buffer_state *state)
2542 +{
2543 + int rc;
2544 +
2545 + if (ep == fsg->bulk_in)
2546 + dump_msg(fsg, "bulk-in", req->buf, req->length);
2547 + else if (ep == fsg->intr_in)
2548 + dump_msg(fsg, "intr-in", req->buf, req->length);
2549 +
2550 + spin_lock_irq(&fsg->lock);
2551 + *pbusy = 1;
2552 + *state = BUF_STATE_BUSY;
2553 + spin_unlock_irq(&fsg->lock);
2554 + rc = usb_ep_queue(ep, req, GFP_KERNEL);
2555 + if (rc != 0) {
2556 + *pbusy = 0;
2557 + *state = BUF_STATE_EMPTY;
2558 +
2559 + /* We can't do much more than wait for a reset */
2560 +
2561 + /* Note: currently the net2280 driver fails zero-length
2562 + * submissions if DMA is enabled. */
2563 + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
2564 + req->length == 0))
2565 + WARNING(fsg, "error in submission: %s --> %d\n",
2566 + ep->name, rc);
2567 + }
2568 +}
2569 +
2570 +
2571 +static int sleep_thread(struct fsg_dev *fsg)
2572 +{
2573 + int rc = 0;
2574 +
2575 + /* Wait until a signal arrives or we are woken up */
2576 + for (;;) {
2577 + try_to_freeze();
2578 + set_current_state(TASK_INTERRUPTIBLE);
2579 + if (signal_pending(current)) {
2580 + rc = -EINTR;
2581 + break;
2582 + }
2583 + if (fsg->thread_wakeup_needed)
2584 + break;
2585 + schedule();
2586 + }
2587 + __set_current_state(TASK_RUNNING);
2588 + fsg->thread_wakeup_needed = 0;
2589 + return rc;
2590 +}
2591 +
2592 +
2593 +/*-------------------------------------------------------------------------*/
2594 +
2595 +static int do_read(struct fsg_dev *fsg)
2596 +{
2597 + struct fsg_lun *curlun = fsg->curlun;
2598 + u32 lba;
2599 + struct fsg_buffhd *bh;
2600 + int rc;
2601 + u32 amount_left;
2602 + loff_t file_offset, file_offset_tmp;
2603 + unsigned int amount;
2604 + ssize_t nread;
2605 +
2606 + /* Get the starting Logical Block Address and check that it's
2607 + * not too big */
2608 + if (fsg->cmnd[0] == READ_6)
2609 + lba = get_unaligned_be24(&fsg->cmnd[1]);
2610 + else {
2611 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2612 +
2613 + /* We allow DPO (Disable Page Out = don't save data in the
2614 + * cache) and FUA (Force Unit Access = don't read from the
2615 + * cache), but we don't implement them. */
2616 + if ((fsg->cmnd[1] & ~0x18) != 0) {
2617 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2618 + return -EINVAL;
2619 + }
2620 + }
2621 + if (lba >= curlun->num_sectors) {
2622 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2623 + return -EINVAL;
2624 + }
2625 + file_offset = ((loff_t) lba) << curlun->blkbits;
2626 +
2627 + /* Carry out the file reads */
2628 + amount_left = fsg->data_size_from_cmnd;
2629 + if (unlikely(amount_left == 0))
2630 + return -EIO; // No default reply
2631 +
2632 + for (;;) {
2633 +
2634 + /* Figure out how much we need to read:
2635 + * Try to read the remaining amount.
2636 + * But don't read more than the buffer size.
2637 + * And don't try to read past the end of the file.
2638 + */
2639 + amount = min((unsigned int) amount_left, mod_data.buflen);
2640 + amount = min((loff_t) amount,
2641 + curlun->file_length - file_offset);
2642 +
2643 + /* Wait for the next buffer to become available */
2644 + bh = fsg->next_buffhd_to_fill;
2645 + while (bh->state != BUF_STATE_EMPTY) {
2646 + rc = sleep_thread(fsg);
2647 + if (rc)
2648 + return rc;
2649 + }
2650 +
2651 + /* If we were asked to read past the end of file,
2652 + * end with an empty buffer. */
2653 + if (amount == 0) {
2654 + curlun->sense_data =
2655 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2656 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2657 + curlun->info_valid = 1;
2658 + bh->inreq->length = 0;
2659 + bh->state = BUF_STATE_FULL;
2660 + break;
2661 + }
2662 +
2663 + /* Perform the read */
2664 + file_offset_tmp = file_offset;
2665 + nread = vfs_read(curlun->filp,
2666 + (char __user *) bh->buf,
2667 + amount, &file_offset_tmp);
2668 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
2669 + (unsigned long long) file_offset,
2670 + (int) nread);
2671 + if (signal_pending(current))
2672 + return -EINTR;
2673 +
2674 + if (nread < 0) {
2675 + LDBG(curlun, "error in file read: %d\n",
2676 + (int) nread);
2677 + nread = 0;
2678 + } else if (nread < amount) {
2679 + LDBG(curlun, "partial file read: %d/%u\n",
2680 + (int) nread, amount);
2681 + nread = round_down(nread, curlun->blksize);
2682 + }
2683 + file_offset += nread;
2684 + amount_left -= nread;
2685 + fsg->residue -= nread;
2686 +
2687 + /* Except at the end of the transfer, nread will be
2688 + * equal to the buffer size, which is divisible by the
2689 + * bulk-in maxpacket size.
2690 + */
2691 + bh->inreq->length = nread;
2692 + bh->state = BUF_STATE_FULL;
2693 +
2694 + /* If an error occurred, report it and its position */
2695 + if (nread < amount) {
2696 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
2697 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2698 + curlun->info_valid = 1;
2699 + break;
2700 + }
2701 +
2702 + if (amount_left == 0)
2703 + break; // No more left to read
2704 +
2705 + /* Send this buffer and go read some more */
2706 + bh->inreq->zero = 0;
2707 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
2708 + &bh->inreq_busy, &bh->state);
2709 + fsg->next_buffhd_to_fill = bh->next;
2710 + }
2711 +
2712 + return -EIO; // No default reply
2713 +}
2714 +
2715 +
2716 +/*-------------------------------------------------------------------------*/
2717 +
2718 +static int do_write(struct fsg_dev *fsg)
2719 +{
2720 + struct fsg_lun *curlun = fsg->curlun;
2721 + u32 lba;
2722 + struct fsg_buffhd *bh;
2723 + int get_some_more;
2724 + u32 amount_left_to_req, amount_left_to_write;
2725 + loff_t usb_offset, file_offset, file_offset_tmp;
2726 + unsigned int amount;
2727 + ssize_t nwritten;
2728 + int rc;
2729 +
2730 + if (curlun->ro) {
2731 + curlun->sense_data = SS_WRITE_PROTECTED;
2732 + return -EINVAL;
2733 + }
2734 + spin_lock(&curlun->filp->f_lock);
2735 + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
2736 + spin_unlock(&curlun->filp->f_lock);
2737 +
2738 + /* Get the starting Logical Block Address and check that it's
2739 + * not too big */
2740 + if (fsg->cmnd[0] == WRITE_6)
2741 + lba = get_unaligned_be24(&fsg->cmnd[1]);
2742 + else {
2743 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2744 +
2745 + /* We allow DPO (Disable Page Out = don't save data in the
2746 + * cache) and FUA (Force Unit Access = write directly to the
2747 + * medium). We don't implement DPO; we implement FUA by
2748 + * performing synchronous output. */
2749 + if ((fsg->cmnd[1] & ~0x18) != 0) {
2750 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2751 + return -EINVAL;
2752 + }
2753 + /* FUA */
2754 + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
2755 + spin_lock(&curlun->filp->f_lock);
2756 + curlun->filp->f_flags |= O_DSYNC;
2757 + spin_unlock(&curlun->filp->f_lock);
2758 + }
2759 + }
2760 + if (lba >= curlun->num_sectors) {
2761 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2762 + return -EINVAL;
2763 + }
2764 +
2765 + /* Carry out the file writes */
2766 + get_some_more = 1;
2767 + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
2768 + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
2769 +
2770 + while (amount_left_to_write > 0) {
2771 +
2772 + /* Queue a request for more data from the host */
2773 + bh = fsg->next_buffhd_to_fill;
2774 + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
2775 +
2776 + /* Figure out how much we want to get:
2777 + * Try to get the remaining amount,
2778 + * but not more than the buffer size.
2779 + */
2780 + amount = min(amount_left_to_req, mod_data.buflen);
2781 +
2782 + /* Beyond the end of the backing file? */
2783 + if (usb_offset >= curlun->file_length) {
2784 + get_some_more = 0;
2785 + curlun->sense_data =
2786 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2787 + curlun->sense_data_info = usb_offset >> curlun->blkbits;
2788 + curlun->info_valid = 1;
2789 + continue;
2790 + }
2791 +
2792 + /* Get the next buffer */
2793 + usb_offset += amount;
2794 + fsg->usb_amount_left -= amount;
2795 + amount_left_to_req -= amount;
2796 + if (amount_left_to_req == 0)
2797 + get_some_more = 0;
2798 +
2799 + /* Except at the end of the transfer, amount will be
2800 + * equal to the buffer size, which is divisible by
2801 + * the bulk-out maxpacket size.
2802 + */
2803 + set_bulk_out_req_length(fsg, bh, amount);
2804 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
2805 + &bh->outreq_busy, &bh->state);
2806 + fsg->next_buffhd_to_fill = bh->next;
2807 + continue;
2808 + }
2809 +
2810 + /* Write the received data to the backing file */
2811 + bh = fsg->next_buffhd_to_drain;
2812 + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
2813 + break; // We stopped early
2814 + if (bh->state == BUF_STATE_FULL) {
2815 + smp_rmb();
2816 + fsg->next_buffhd_to_drain = bh->next;
2817 + bh->state = BUF_STATE_EMPTY;
2818 +
2819 + /* Did something go wrong with the transfer? */
2820 + if (bh->outreq->status != 0) {
2821 + curlun->sense_data = SS_COMMUNICATION_FAILURE;
2822 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2823 + curlun->info_valid = 1;
2824 + break;
2825 + }
2826 +
2827 + amount = bh->outreq->actual;
2828 + if (curlun->file_length - file_offset < amount) {
2829 + LERROR(curlun,
2830 + "write %u @ %llu beyond end %llu\n",
2831 + amount, (unsigned long long) file_offset,
2832 + (unsigned long long) curlun->file_length);
2833 + amount = curlun->file_length - file_offset;
2834 + }
2835 +
2836 + /* Don't accept excess data. The spec doesn't say
2837 + * what to do in this case. We'll ignore the error.
2838 + */
2839 + amount = min(amount, bh->bulk_out_intended_length);
2840 +
2841 + /* Don't write a partial block */
2842 + amount = round_down(amount, curlun->blksize);
2843 + if (amount == 0)
2844 + goto empty_write;
2845 +
2846 + /* Perform the write */
2847 + file_offset_tmp = file_offset;
2848 + nwritten = vfs_write(curlun->filp,
2849 + (char __user *) bh->buf,
2850 + amount, &file_offset_tmp);
2851 + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
2852 + (unsigned long long) file_offset,
2853 + (int) nwritten);
2854 + if (signal_pending(current))
2855 + return -EINTR; // Interrupted!
2856 +
2857 + if (nwritten < 0) {
2858 + LDBG(curlun, "error in file write: %d\n",
2859 + (int) nwritten);
2860 + nwritten = 0;
2861 + } else if (nwritten < amount) {
2862 + LDBG(curlun, "partial file write: %d/%u\n",
2863 + (int) nwritten, amount);
2864 + nwritten = round_down(nwritten, curlun->blksize);
2865 + }
2866 + file_offset += nwritten;
2867 + amount_left_to_write -= nwritten;
2868 + fsg->residue -= nwritten;
2869 +
2870 + /* If an error occurred, report it and its position */
2871 + if (nwritten < amount) {
2872 + curlun->sense_data = SS_WRITE_ERROR;
2873 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2874 + curlun->info_valid = 1;
2875 + break;
2876 + }
2877 +
2878 + empty_write:
2879 + /* Did the host decide to stop early? */
2880 + if (bh->outreq->actual < bh->bulk_out_intended_length) {
2881 + fsg->short_packet_received = 1;
2882 + break;
2883 + }
2884 + continue;
2885 + }
2886 +
2887 + /* Wait for something to happen */
2888 + rc = sleep_thread(fsg);
2889 + if (rc)
2890 + return rc;
2891 + }
2892 +
2893 + return -EIO; // No default reply
2894 +}
2895 +
2896 +
2897 +/*-------------------------------------------------------------------------*/
2898 +
2899 +static int do_synchronize_cache(struct fsg_dev *fsg)
2900 +{
2901 + struct fsg_lun *curlun = fsg->curlun;
2902 + int rc;
2903 +
2904 + /* We ignore the requested LBA and write out all file's
2905 + * dirty data buffers. */
2906 + rc = fsg_lun_fsync_sub(curlun);
2907 + if (rc)
2908 + curlun->sense_data = SS_WRITE_ERROR;
2909 + return 0;
2910 +}
2911 +
2912 +
2913 +/*-------------------------------------------------------------------------*/
2914 +
2915 +static void invalidate_sub(struct fsg_lun *curlun)
2916 +{
2917 + struct file *filp = curlun->filp;
2918 + struct inode *inode = filp->f_path.dentry->d_inode;
2919 + unsigned long rc;
2920 +
2921 + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
2922 + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
2923 +}
2924 +
2925 +static int do_verify(struct fsg_dev *fsg)
2926 +{
2927 + struct fsg_lun *curlun = fsg->curlun;
2928 + u32 lba;
2929 + u32 verification_length;
2930 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
2931 + loff_t file_offset, file_offset_tmp;
2932 + u32 amount_left;
2933 + unsigned int amount;
2934 + ssize_t nread;
2935 +
2936 + /* Get the starting Logical Block Address and check that it's
2937 + * not too big */
2938 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2939 + if (lba >= curlun->num_sectors) {
2940 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2941 + return -EINVAL;
2942 + }
2943 +
2944 + /* We allow DPO (Disable Page Out = don't save data in the
2945 + * cache) but we don't implement it. */
2946 + if ((fsg->cmnd[1] & ~0x10) != 0) {
2947 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2948 + return -EINVAL;
2949 + }
2950 +
2951 + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
2952 + if (unlikely(verification_length == 0))
2953 + return -EIO; // No default reply
2954 +
2955 + /* Prepare to carry out the file verify */
2956 + amount_left = verification_length << curlun->blkbits;
2957 + file_offset = ((loff_t) lba) << curlun->blkbits;
2958 +
2959 + /* Write out all the dirty buffers before invalidating them */
2960 + fsg_lun_fsync_sub(curlun);
2961 + if (signal_pending(current))
2962 + return -EINTR;
2963 +
2964 + invalidate_sub(curlun);
2965 + if (signal_pending(current))
2966 + return -EINTR;
2967 +
2968 + /* Just try to read the requested blocks */
2969 + while (amount_left > 0) {
2970 +
2971 + /* Figure out how much we need to read:
2972 + * Try to read the remaining amount, but not more than
2973 + * the buffer size.
2974 + * And don't try to read past the end of the file.
2975 + */
2976 + amount = min((unsigned int) amount_left, mod_data.buflen);
2977 + amount = min((loff_t) amount,
2978 + curlun->file_length - file_offset);
2979 + if (amount == 0) {
2980 + curlun->sense_data =
2981 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2982 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2983 + curlun->info_valid = 1;
2984 + break;
2985 + }
2986 +
2987 + /* Perform the read */
2988 + file_offset_tmp = file_offset;
2989 + nread = vfs_read(curlun->filp,
2990 + (char __user *) bh->buf,
2991 + amount, &file_offset_tmp);
2992 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
2993 + (unsigned long long) file_offset,
2994 + (int) nread);
2995 + if (signal_pending(current))
2996 + return -EINTR;
2997 +
2998 + if (nread < 0) {
2999 + LDBG(curlun, "error in file verify: %d\n",
3000 + (int) nread);
3001 + nread = 0;
3002 + } else if (nread < amount) {
3003 + LDBG(curlun, "partial file verify: %d/%u\n",
3004 + (int) nread, amount);
3005 + nread = round_down(nread, curlun->blksize);
3006 + }
3007 + if (nread == 0) {
3008 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
3009 + curlun->sense_data_info = file_offset >> curlun->blkbits;
3010 + curlun->info_valid = 1;
3011 + break;
3012 + }
3013 + file_offset += nread;
3014 + amount_left -= nread;
3015 + }
3016 + return 0;
3017 +}
3018 +
3019 +
3020 +/*-------------------------------------------------------------------------*/
3021 +
3022 +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3023 +{
3024 + u8 *buf = (u8 *) bh->buf;
3025 +
3026 + static char vendor_id[] = "Linux ";
3027 + static char product_disk_id[] = "File-Stor Gadget";
3028 + static char product_cdrom_id[] = "File-CD Gadget ";
3029 +
3030 + if (!fsg->curlun) { // Unsupported LUNs are okay
3031 + fsg->bad_lun_okay = 1;
3032 + memset(buf, 0, 36);
3033 + buf[0] = 0x7f; // Unsupported, no device-type
3034 + buf[4] = 31; // Additional length
3035 + return 36;
3036 + }
3037 +
3038 + memset(buf, 0, 8);
3039 + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
3040 + if (mod_data.removable)
3041 + buf[1] = 0x80;
3042 + buf[2] = 2; // ANSI SCSI level 2
3043 + buf[3] = 2; // SCSI-2 INQUIRY data format
3044 + buf[4] = 31; // Additional length
3045 + // No special options
3046 + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
3047 + (mod_data.cdrom ? product_cdrom_id :
3048 + product_disk_id),
3049 + mod_data.release);
3050 + return 36;
3051 +}
3052 +
3053 +
3054 +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3055 +{
3056 + struct fsg_lun *curlun = fsg->curlun;
3057 + u8 *buf = (u8 *) bh->buf;
3058 + u32 sd, sdinfo;
3059 + int valid;
3060 +
3061 + /*
3062 + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
3063 + *
3064 + * If a REQUEST SENSE command is received from an initiator
3065 + * with a pending unit attention condition (before the target
3066 + * generates the contingent allegiance condition), then the
3067 + * target shall either:
3068 + * a) report any pending sense data and preserve the unit
3069 + * attention condition on the logical unit, or,
3070 + * b) report the unit attention condition, may discard any
3071 + * pending sense data, and clear the unit attention
3072 + * condition on the logical unit for that initiator.
3073 + *
3074 + * FSG normally uses option a); enable this code to use option b).
3075 + */
3076 +#if 0
3077 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
3078 + curlun->sense_data = curlun->unit_attention_data;
3079 + curlun->unit_attention_data = SS_NO_SENSE;
3080 + }
3081 +#endif
3082 +
3083 + if (!curlun) { // Unsupported LUNs are okay
3084 + fsg->bad_lun_okay = 1;
3085 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
3086 + sdinfo = 0;
3087 + valid = 0;
3088 + } else {
3089 + sd = curlun->sense_data;
3090 + sdinfo = curlun->sense_data_info;
3091 + valid = curlun->info_valid << 7;
3092 + curlun->sense_data = SS_NO_SENSE;
3093 + curlun->sense_data_info = 0;
3094 + curlun->info_valid = 0;
3095 + }
3096 +
3097 + memset(buf, 0, 18);
3098 + buf[0] = valid | 0x70; // Valid, current error
3099 + buf[2] = SK(sd);
3100 + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
3101 + buf[7] = 18 - 8; // Additional sense length
3102 + buf[12] = ASC(sd);
3103 + buf[13] = ASCQ(sd);
3104 + return 18;
3105 +}
3106 +
3107 +
3108 +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3109 +{
3110 + struct fsg_lun *curlun = fsg->curlun;
3111 + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
3112 + int pmi = fsg->cmnd[8];
3113 + u8 *buf = (u8 *) bh->buf;
3114 +
3115 + /* Check the PMI and LBA fields */
3116 + if (pmi > 1 || (pmi == 0 && lba != 0)) {
3117 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3118 + return -EINVAL;
3119 + }
3120 +
3121 + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
3122 + /* Max logical block */
3123 + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
3124 + return 8;
3125 +}
3126 +
3127 +
3128 +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3129 +{
3130 + struct fsg_lun *curlun = fsg->curlun;
3131 + int msf = fsg->cmnd[1] & 0x02;
3132 + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
3133 + u8 *buf = (u8 *) bh->buf;
3134 +
3135 + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
3136 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3137 + return -EINVAL;
3138 + }
3139 + if (lba >= curlun->num_sectors) {
3140 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
3141 + return -EINVAL;
3142 + }
3143 +
3144 + memset(buf, 0, 8);
3145 + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
3146 + store_cdrom_address(&buf[4], msf, lba);
3147 + return 8;
3148 +}
3149 +
3150 +
3151 +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3152 +{
3153 + struct fsg_lun *curlun = fsg->curlun;
3154 + int msf = fsg->cmnd[1] & 0x02;
3155 + int start_track = fsg->cmnd[6];
3156 + u8 *buf = (u8 *) bh->buf;
3157 +
3158 + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
3159 + start_track > 1) {
3160 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3161 + return -EINVAL;
3162 + }
3163 +
3164 + memset(buf, 0, 20);
3165 + buf[1] = (20-2); /* TOC data length */
3166 + buf[2] = 1; /* First track number */
3167 + buf[3] = 1; /* Last track number */
3168 + buf[5] = 0x16; /* Data track, copying allowed */
3169 + buf[6] = 0x01; /* Only track is number 1 */
3170 + store_cdrom_address(&buf[8], msf, 0);
3171 +
3172 + buf[13] = 0x16; /* Lead-out track is data */
3173 + buf[14] = 0xAA; /* Lead-out track number */
3174 + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
3175 + return 20;
3176 +}
3177 +
3178 +
3179 +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3180 +{
3181 + struct fsg_lun *curlun = fsg->curlun;
3182 + int mscmnd = fsg->cmnd[0];
3183 + u8 *buf = (u8 *) bh->buf;
3184 + u8 *buf0 = buf;
3185 + int pc, page_code;
3186 + int changeable_values, all_pages;
3187 + int valid_page = 0;
3188 + int len, limit;
3189 +
3190 + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
3191 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3192 + return -EINVAL;
3193 + }
3194 + pc = fsg->cmnd[2] >> 6;
3195 + page_code = fsg->cmnd[2] & 0x3f;
3196 + if (pc == 3) {
3197 + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
3198 + return -EINVAL;
3199 + }
3200 + changeable_values = (pc == 1);
3201 + all_pages = (page_code == 0x3f);
3202 +
3203 + /* Write the mode parameter header. Fixed values are: default
3204 + * medium type, no cache control (DPOFUA), and no block descriptors.
3205 + * The only variable value is the WriteProtect bit. We will fill in
3206 + * the mode data length later. */
3207 + memset(buf, 0, 8);
3208 + if (mscmnd == MODE_SENSE) {
3209 + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
3210 + buf += 4;
3211 + limit = 255;
3212 + } else { // MODE_SENSE_10
3213 + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
3214 + buf += 8;
3215 + limit = 65535; // Should really be mod_data.buflen
3216 + }
3217 +
3218 + /* No block descriptors */
3219 +
3220 + /* The mode pages, in numerical order. The only page we support
3221 + * is the Caching page. */
3222 + if (page_code == 0x08 || all_pages) {
3223 + valid_page = 1;
3224 + buf[0] = 0x08; // Page code
3225 + buf[1] = 10; // Page length
3226 + memset(buf+2, 0, 10); // None of the fields are changeable
3227 +
3228 + if (!changeable_values) {
3229 + buf[2] = 0x04; // Write cache enable,
3230 + // Read cache not disabled
3231 + // No cache retention priorities
3232 + put_unaligned_be16(0xffff, &buf[4]);
3233 + /* Don't disable prefetch */
3234 + /* Minimum prefetch = 0 */
3235 + put_unaligned_be16(0xffff, &buf[8]);
3236 + /* Maximum prefetch */
3237 + put_unaligned_be16(0xffff, &buf[10]);
3238 + /* Maximum prefetch ceiling */
3239 + }
3240 + buf += 12;
3241 + }
3242 +
3243 + /* Check that a valid page was requested and the mode data length
3244 + * isn't too long. */
3245 + len = buf - buf0;
3246 + if (!valid_page || len > limit) {
3247 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3248 + return -EINVAL;
3249 + }
3250 +
3251 + /* Store the mode data length */
3252 + if (mscmnd == MODE_SENSE)
3253 + buf0[0] = len - 1;
3254 + else
3255 + put_unaligned_be16(len - 2, buf0);
3256 + return len;
3257 +}
3258 +
3259 +
3260 +static int do_start_stop(struct fsg_dev *fsg)
3261 +{
3262 + struct fsg_lun *curlun = fsg->curlun;
3263 + int loej, start;
3264 +
3265 + if (!mod_data.removable) {
3266 + curlun->sense_data = SS_INVALID_COMMAND;
3267 + return -EINVAL;
3268 + }
3269 +
3270 + // int immed = fsg->cmnd[1] & 0x01;
3271 + loej = fsg->cmnd[4] & 0x02;
3272 + start = fsg->cmnd[4] & 0x01;
3273 +
3274 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
3275 + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
3276 + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
3277 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3278 + return -EINVAL;
3279 + }
3280 +
3281 + if (!start) {
3282 +
3283 + /* Are we allowed to unload the media? */
3284 + if (curlun->prevent_medium_removal) {
3285 + LDBG(curlun, "unload attempt prevented\n");
3286 + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
3287 + return -EINVAL;
3288 + }
3289 + if (loej) { // Simulate an unload/eject
3290 + up_read(&fsg->filesem);
3291 + down_write(&fsg->filesem);
3292 + fsg_lun_close(curlun);
3293 + up_write(&fsg->filesem);
3294 + down_read(&fsg->filesem);
3295 + }
3296 + } else {
3297 +
3298 + /* Our emulation doesn't support mounting; the medium is
3299 + * available for use as soon as it is loaded. */
3300 + if (!fsg_lun_is_open(curlun)) {
3301 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
3302 + return -EINVAL;
3303 + }
3304 + }
3305 +#endif
3306 + return 0;
3307 +}
3308 +
3309 +
3310 +static int do_prevent_allow(struct fsg_dev *fsg)
3311 +{
3312 + struct fsg_lun *curlun = fsg->curlun;
3313 + int prevent;
3314 +
3315 + if (!mod_data.removable) {
3316 + curlun->sense_data = SS_INVALID_COMMAND;
3317 + return -EINVAL;
3318 + }
3319 +
3320 + prevent = fsg->cmnd[4] & 0x01;
3321 + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
3322 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3323 + return -EINVAL;
3324 + }
3325 +
3326 + if (curlun->prevent_medium_removal && !prevent)
3327 + fsg_lun_fsync_sub(curlun);
3328 + curlun->prevent_medium_removal = prevent;
3329 + return 0;
3330 +}
3331 +
3332 +
3333 +static int do_read_format_capacities(struct fsg_dev *fsg,
3334 + struct fsg_buffhd *bh)
3335 +{
3336 + struct fsg_lun *curlun = fsg->curlun;
3337 + u8 *buf = (u8 *) bh->buf;
3338 +
3339 + buf[0] = buf[1] = buf[2] = 0;
3340 + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
3341 + buf += 4;
3342 +
3343 + put_unaligned_be32(curlun->num_sectors, &buf[0]);
3344 + /* Number of blocks */
3345 + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
3346 + buf[4] = 0x02; /* Current capacity */
3347 + return 12;
3348 +}
3349 +
3350 +
3351 +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3352 +{
3353 + struct fsg_lun *curlun = fsg->curlun;
3354 +
3355 + /* We don't support MODE SELECT */
3356 + curlun->sense_data = SS_INVALID_COMMAND;
3357 + return -EINVAL;
3358 +}
3359 +
3360 +
3361 +/*-------------------------------------------------------------------------*/
3362 +
3363 +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
3364 +{
3365 + int rc;
3366 +
3367 + rc = fsg_set_halt(fsg, fsg->bulk_in);
3368 + if (rc == -EAGAIN)
3369 + VDBG(fsg, "delayed bulk-in endpoint halt\n");
3370 + while (rc != 0) {
3371 + if (rc != -EAGAIN) {
3372 + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
3373 + rc = 0;
3374 + break;
3375 + }
3376 +
3377 + /* Wait for a short time and then try again */
3378 + if (msleep_interruptible(100) != 0)
3379 + return -EINTR;
3380 + rc = usb_ep_set_halt(fsg->bulk_in);
3381 + }
3382 + return rc;
3383 +}
3384 +
3385 +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
3386 +{
3387 + int rc;
3388 +
3389 + DBG(fsg, "bulk-in set wedge\n");
3390 + rc = usb_ep_set_wedge(fsg->bulk_in);
3391 + if (rc == -EAGAIN)
3392 + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
3393 + while (rc != 0) {
3394 + if (rc != -EAGAIN) {
3395 + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
3396 + rc = 0;
3397 + break;
3398 + }
3399 +
3400 + /* Wait for a short time and then try again */
3401 + if (msleep_interruptible(100) != 0)
3402 + return -EINTR;
3403 + rc = usb_ep_set_wedge(fsg->bulk_in);
3404 + }
3405 + return rc;
3406 +}
3407 +
3408 +static int throw_away_data(struct fsg_dev *fsg)
3409 +{
3410 + struct fsg_buffhd *bh;
3411 + u32 amount;
3412 + int rc;
3413 +
3414 + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
3415 + fsg->usb_amount_left > 0) {
3416 +
3417 + /* Throw away the data in a filled buffer */
3418 + if (bh->state == BUF_STATE_FULL) {
3419 + smp_rmb();
3420 + bh->state = BUF_STATE_EMPTY;
3421 + fsg->next_buffhd_to_drain = bh->next;
3422 +
3423 + /* A short packet or an error ends everything */
3424 + if (bh->outreq->actual < bh->bulk_out_intended_length ||
3425 + bh->outreq->status != 0) {
3426 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3427 + return -EINTR;
3428 + }
3429 + continue;
3430 + }
3431 +
3432 + /* Try to submit another request if we need one */
3433 + bh = fsg->next_buffhd_to_fill;
3434 + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
3435 + amount = min(fsg->usb_amount_left,
3436 + (u32) mod_data.buflen);
3437 +
3438 + /* Except at the end of the transfer, amount will be
3439 + * equal to the buffer size, which is divisible by
3440 + * the bulk-out maxpacket size.
3441 + */
3442 + set_bulk_out_req_length(fsg, bh, amount);
3443 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
3444 + &bh->outreq_busy, &bh->state);
3445 + fsg->next_buffhd_to_fill = bh->next;
3446 + fsg->usb_amount_left -= amount;
3447 + continue;
3448 + }
3449 +
3450 + /* Otherwise wait for something to happen */
3451 + rc = sleep_thread(fsg);
3452 + if (rc)
3453 + return rc;
3454 + }
3455 + return 0;
3456 +}
3457 +
3458 +
3459 +static int finish_reply(struct fsg_dev *fsg)
3460 +{
3461 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
3462 + int rc = 0;
3463 +
3464 + switch (fsg->data_dir) {
3465 + case DATA_DIR_NONE:
3466 + break; // Nothing to send
3467 +
3468 + /* If we don't know whether the host wants to read or write,
3469 + * this must be CB or CBI with an unknown command. We mustn't
3470 + * try to send or receive any data. So stall both bulk pipes
3471 + * if we can and wait for a reset. */
3472 + case DATA_DIR_UNKNOWN:
3473 + if (mod_data.can_stall) {
3474 + fsg_set_halt(fsg, fsg->bulk_out);
3475 + rc = halt_bulk_in_endpoint(fsg);
3476 + }
3477 + break;
3478 +
3479 + /* All but the last buffer of data must have already been sent */
3480 + case DATA_DIR_TO_HOST:
3481 + if (fsg->data_size == 0)
3482 + ; // Nothing to send
3483 +
3484 + /* If there's no residue, simply send the last buffer */
3485 + else if (fsg->residue == 0) {
3486 + bh->inreq->zero = 0;
3487 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3488 + &bh->inreq_busy, &bh->state);
3489 + fsg->next_buffhd_to_fill = bh->next;
3490 + }
3491 +
3492 + /* There is a residue. For CB and CBI, simply mark the end
3493 + * of the data with a short packet. However, if we are
3494 + * allowed to stall, there was no data at all (residue ==
3495 + * data_size), and the command failed (invalid LUN or
3496 + * sense data is set), then halt the bulk-in endpoint
3497 + * instead. */
3498 + else if (!transport_is_bbb()) {
3499 + if (mod_data.can_stall &&
3500 + fsg->residue == fsg->data_size &&
3501 + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
3502 + bh->state = BUF_STATE_EMPTY;
3503 + rc = halt_bulk_in_endpoint(fsg);
3504 + } else {
3505 + bh->inreq->zero = 1;
3506 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3507 + &bh->inreq_busy, &bh->state);
3508 + fsg->next_buffhd_to_fill = bh->next;
3509 + }
3510 + }
3511 +
3512 + /*
3513 + * For Bulk-only, mark the end of the data with a short
3514 + * packet. If we are allowed to stall, halt the bulk-in
3515 + * endpoint. (Note: This violates the Bulk-Only Transport
3516 + * specification, which requires us to pad the data if we
3517 + * don't halt the endpoint. Presumably nobody will mind.)
3518 + */
3519 + else {
3520 + bh->inreq->zero = 1;
3521 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3522 + &bh->inreq_busy, &bh->state);
3523 + fsg->next_buffhd_to_fill = bh->next;
3524 + if (mod_data.can_stall)
3525 + rc = halt_bulk_in_endpoint(fsg);
3526 + }
3527 + break;
3528 +
3529 + /* We have processed all we want from the data the host has sent.
3530 + * There may still be outstanding bulk-out requests. */
3531 + case DATA_DIR_FROM_HOST:
3532 + if (fsg->residue == 0)
3533 + ; // Nothing to receive
3534 +
3535 + /* Did the host stop sending unexpectedly early? */
3536 + else if (fsg->short_packet_received) {
3537 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3538 + rc = -EINTR;
3539 + }
3540 +
3541 + /* We haven't processed all the incoming data. Even though
3542 + * we may be allowed to stall, doing so would cause a race.
3543 + * The controller may already have ACK'ed all the remaining
3544 + * bulk-out packets, in which case the host wouldn't see a
3545 + * STALL. Not realizing the endpoint was halted, it wouldn't
3546 + * clear the halt -- leading to problems later on. */
3547 +#if 0
3548 + else if (mod_data.can_stall) {
3549 + fsg_set_halt(fsg, fsg->bulk_out);
3550 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3551 + rc = -EINTR;
3552 + }
3553 +#endif
3554 +
3555 + /* We can't stall. Read in the excess data and throw it
3556 + * all away. */
3557 + else
3558 + rc = throw_away_data(fsg);
3559 + break;
3560 + }
3561 + return rc;
3562 +}
3563 +
3564 +
3565 +static int send_status(struct fsg_dev *fsg)
3566 +{
3567 + struct fsg_lun *curlun = fsg->curlun;
3568 + struct fsg_buffhd *bh;
3569 + int rc;
3570 + u8 status = US_BULK_STAT_OK;
3571 + u32 sd, sdinfo = 0;
3572 +
3573 + /* Wait for the next buffer to become available */
3574 + bh = fsg->next_buffhd_to_fill;
3575 + while (bh->state != BUF_STATE_EMPTY) {
3576 + rc = sleep_thread(fsg);
3577 + if (rc)
3578 + return rc;
3579 + }
3580 +
3581 + if (curlun) {
3582 + sd = curlun->sense_data;
3583 + sdinfo = curlun->sense_data_info;
3584 + } else if (fsg->bad_lun_okay)
3585 + sd = SS_NO_SENSE;
3586 + else
3587 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
3588 +
3589 + if (fsg->phase_error) {
3590 + DBG(fsg, "sending phase-error status\n");
3591 + status = US_BULK_STAT_PHASE;
3592 + sd = SS_INVALID_COMMAND;
3593 + } else if (sd != SS_NO_SENSE) {
3594 + DBG(fsg, "sending command-failure status\n");
3595 + status = US_BULK_STAT_FAIL;
3596 + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
3597 + " info x%x\n",
3598 + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
3599 + }
3600 +
3601 + if (transport_is_bbb()) {
3602 + struct bulk_cs_wrap *csw = bh->buf;
3603 +
3604 + /* Store and send the Bulk-only CSW */
3605 + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
3606 + csw->Tag = fsg->tag;
3607 + csw->Residue = cpu_to_le32(fsg->residue);
3608 + csw->Status = status;
3609 +
3610 + bh->inreq->length = US_BULK_CS_WRAP_LEN;
3611 + bh->inreq->zero = 0;
3612 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3613 + &bh->inreq_busy, &bh->state);
3614 +
3615 + } else if (mod_data.transport_type == USB_PR_CB) {
3616 +
3617 + /* Control-Bulk transport has no status phase! */
3618 + return 0;
3619 +
3620 + } else { // USB_PR_CBI
3621 + struct interrupt_data *buf = bh->buf;
3622 +
3623 + /* Store and send the Interrupt data. UFI sends the ASC
3624 + * and ASCQ bytes. Everything else sends a Type (which
3625 + * is always 0) and the status Value. */
3626 + if (mod_data.protocol_type == USB_SC_UFI) {
3627 + buf->bType = ASC(sd);
3628 + buf->bValue = ASCQ(sd);
3629 + } else {
3630 + buf->bType = 0;
3631 + buf->bValue = status;
3632 + }
3633 + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
3634 +
3635 + fsg->intr_buffhd = bh; // Point to the right buffhd
3636 + fsg->intreq->buf = bh->inreq->buf;
3637 + fsg->intreq->context = bh;
3638 + start_transfer(fsg, fsg->intr_in, fsg->intreq,
3639 + &fsg->intreq_busy, &bh->state);
3640 + }
3641 +
3642 + fsg->next_buffhd_to_fill = bh->next;
3643 + return 0;
3644 +}
3645 +
3646 +
3647 +/*-------------------------------------------------------------------------*/
3648 +
3649 +/* Check whether the command is properly formed and whether its data size
3650 + * and direction agree with the values we already have. */
3651 +static int check_command(struct fsg_dev *fsg, int cmnd_size,
3652 + enum data_direction data_dir, unsigned int mask,
3653 + int needs_medium, const char *name)
3654 +{
3655 + int i;
3656 + int lun = fsg->cmnd[1] >> 5;
3657 + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
3658 + char hdlen[20];
3659 + struct fsg_lun *curlun;
3660 +
3661 + /* Adjust the expected cmnd_size for protocol encapsulation padding.
3662 + * Transparent SCSI doesn't pad. */
3663 + if (protocol_is_scsi())
3664 + ;
3665 +
3666 + /* There's some disagreement as to whether RBC pads commands or not.
3667 + * We'll play it safe and accept either form. */
3668 + else if (mod_data.protocol_type == USB_SC_RBC) {
3669 + if (fsg->cmnd_size == 12)
3670 + cmnd_size = 12;
3671 +
3672 + /* All the other protocols pad to 12 bytes */
3673 + } else
3674 + cmnd_size = 12;
3675 +
3676 + hdlen[0] = 0;
3677 + if (fsg->data_dir != DATA_DIR_UNKNOWN)
3678 + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
3679 + fsg->data_size);
3680 + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
3681 + name, cmnd_size, dirletter[(int) data_dir],
3682 + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
3683 +
3684 + /* We can't reply at all until we know the correct data direction
3685 + * and size. */
3686 + if (fsg->data_size_from_cmnd == 0)
3687 + data_dir = DATA_DIR_NONE;
3688 + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
3689 + fsg->data_dir = data_dir;
3690 + fsg->data_size = fsg->data_size_from_cmnd;
3691 +
3692 + } else { // Bulk-only
3693 + if (fsg->data_size < fsg->data_size_from_cmnd) {
3694 +
3695 + /* Host data size < Device data size is a phase error.
3696 + * Carry out the command, but only transfer as much
3697 + * as we are allowed. */
3698 + fsg->data_size_from_cmnd = fsg->data_size;
3699 + fsg->phase_error = 1;
3700 + }
3701 + }
3702 + fsg->residue = fsg->usb_amount_left = fsg->data_size;
3703 +
3704 + /* Conflicting data directions is a phase error */
3705 + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
3706 + fsg->phase_error = 1;
3707 + return -EINVAL;
3708 + }
3709 +
3710 + /* Verify the length of the command itself */
3711 + if (cmnd_size != fsg->cmnd_size) {
3712 +
3713 + /* Special case workaround: There are plenty of buggy SCSI
3714 + * implementations. Many have issues with cbw->Length
3715 + * field passing a wrong command size. For those cases we
3716 + * always try to work around the problem by using the length
3717 + * sent by the host side provided it is at least as large
3718 + * as the correct command length.
3719 + * Examples of such cases would be MS-Windows, which issues
3720 + * REQUEST SENSE with cbw->Length == 12 where it should
3721 + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
3722 + * REQUEST SENSE with cbw->Length == 10 where it should
3723 + * be 6 as well.
3724 + */
3725 + if (cmnd_size <= fsg->cmnd_size) {
3726 + DBG(fsg, "%s is buggy! Expected length %d "
3727 + "but we got %d\n", name,
3728 + cmnd_size, fsg->cmnd_size);
3729 + cmnd_size = fsg->cmnd_size;
3730 + } else {
3731 + fsg->phase_error = 1;
3732 + return -EINVAL;
3733 + }
3734 + }
3735 +
3736 + /* Check that the LUN values are consistent */
3737 + if (transport_is_bbb()) {
3738 + if (fsg->lun != lun)
3739 + DBG(fsg, "using LUN %d from CBW, "
3740 + "not LUN %d from CDB\n",
3741 + fsg->lun, lun);
3742 + }
3743 +
3744 + /* Check the LUN */
3745 + curlun = fsg->curlun;
3746 + if (curlun) {
3747 + if (fsg->cmnd[0] != REQUEST_SENSE) {
3748 + curlun->sense_data = SS_NO_SENSE;
3749 + curlun->sense_data_info = 0;
3750 + curlun->info_valid = 0;
3751 + }
3752 + } else {
3753 + fsg->bad_lun_okay = 0;
3754 +
3755 + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
3756 + * to use unsupported LUNs; all others may not. */
3757 + if (fsg->cmnd[0] != INQUIRY &&
3758 + fsg->cmnd[0] != REQUEST_SENSE) {
3759 + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
3760 + return -EINVAL;
3761 + }
3762 + }
3763 +
3764 + /* If a unit attention condition exists, only INQUIRY and
3765 + * REQUEST SENSE commands are allowed; anything else must fail. */
3766 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
3767 + fsg->cmnd[0] != INQUIRY &&
3768 + fsg->cmnd[0] != REQUEST_SENSE) {
3769 + curlun->sense_data = curlun->unit_attention_data;
3770 + curlun->unit_attention_data = SS_NO_SENSE;
3771 + return -EINVAL;
3772 + }
3773 +
3774 + /* Check that only command bytes listed in the mask are non-zero */
3775 + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
3776 + for (i = 1; i < cmnd_size; ++i) {
3777 + if (fsg->cmnd[i] && !(mask & (1 << i))) {
3778 + if (curlun)
3779 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3780 + return -EINVAL;
3781 + }
3782 + }
3783 +
3784 + /* If the medium isn't mounted and the command needs to access
3785 + * it, return an error. */
3786 + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
3787 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
3788 + return -EINVAL;
3789 + }
3790 +
3791 + return 0;
3792 +}
3793 +
3794 +/* wrapper of check_command for data size in blocks handling */
3795 +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
3796 + enum data_direction data_dir, unsigned int mask,
3797 + int needs_medium, const char *name)
3798 +{
3799 + if (fsg->curlun)
3800 + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
3801 + return check_command(fsg, cmnd_size, data_dir,
3802 + mask, needs_medium, name);
3803 +}
3804 +
3805 +static int do_scsi_command(struct fsg_dev *fsg)
3806 +{
3807 + struct fsg_buffhd *bh;
3808 + int rc;
3809 + int reply = -EINVAL;
3810 + int i;
3811 + static char unknown[16];
3812 +
3813 + dump_cdb(fsg);
3814 +
3815 + /* Wait for the next buffer to become available for data or status */
3816 + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
3817 + while (bh->state != BUF_STATE_EMPTY) {
3818 + rc = sleep_thread(fsg);
3819 + if (rc)
3820 + return rc;
3821 + }
3822 + fsg->phase_error = 0;
3823 + fsg->short_packet_received = 0;
3824 +
3825 + down_read(&fsg->filesem); // We're using the backing file
3826 + switch (fsg->cmnd[0]) {
3827 +
3828 + case INQUIRY:
3829 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3830 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3831 + (1<<4), 0,
3832 + "INQUIRY")) == 0)
3833 + reply = do_inquiry(fsg, bh);
3834 + break;
3835 +
3836 + case MODE_SELECT:
3837 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3838 + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
3839 + (1<<1) | (1<<4), 0,
3840 + "MODE SELECT(6)")) == 0)
3841 + reply = do_mode_select(fsg, bh);
3842 + break;
3843 +
3844 + case MODE_SELECT_10:
3845 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3846 + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
3847 + (1<<1) | (3<<7), 0,
3848 + "MODE SELECT(10)")) == 0)
3849 + reply = do_mode_select(fsg, bh);
3850 + break;
3851 +
3852 + case MODE_SENSE:
3853 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3854 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3855 + (1<<1) | (1<<2) | (1<<4), 0,
3856 + "MODE SENSE(6)")) == 0)
3857 + reply = do_mode_sense(fsg, bh);
3858 + break;
3859 +
3860 + case MODE_SENSE_10:
3861 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3862 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3863 + (1<<1) | (1<<2) | (3<<7), 0,
3864 + "MODE SENSE(10)")) == 0)
3865 + reply = do_mode_sense(fsg, bh);
3866 + break;
3867 +
3868 + case ALLOW_MEDIUM_REMOVAL:
3869 + fsg->data_size_from_cmnd = 0;
3870 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
3871 + (1<<4), 0,
3872 + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
3873 + reply = do_prevent_allow(fsg);
3874 + break;
3875 +
3876 + case READ_6:
3877 + i = fsg->cmnd[4];
3878 + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
3879 + if ((reply = check_command_size_in_blocks(fsg, 6,
3880 + DATA_DIR_TO_HOST,
3881 + (7<<1) | (1<<4), 1,
3882 + "READ(6)")) == 0)
3883 + reply = do_read(fsg);
3884 + break;
3885 +
3886 + case READ_10:
3887 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3888 + if ((reply = check_command_size_in_blocks(fsg, 10,
3889 + DATA_DIR_TO_HOST,
3890 + (1<<1) | (0xf<<2) | (3<<7), 1,
3891 + "READ(10)")) == 0)
3892 + reply = do_read(fsg);
3893 + break;
3894 +
3895 + case READ_12:
3896 + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
3897 + if ((reply = check_command_size_in_blocks(fsg, 12,
3898 + DATA_DIR_TO_HOST,
3899 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
3900 + "READ(12)")) == 0)
3901 + reply = do_read(fsg);
3902 + break;
3903 +
3904 + case READ_CAPACITY:
3905 + fsg->data_size_from_cmnd = 8;
3906 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3907 + (0xf<<2) | (1<<8), 1,
3908 + "READ CAPACITY")) == 0)
3909 + reply = do_read_capacity(fsg, bh);
3910 + break;
3911 +
3912 + case READ_HEADER:
3913 + if (!mod_data.cdrom)
3914 + goto unknown_cmnd;
3915 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3916 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3917 + (3<<7) | (0x1f<<1), 1,
3918 + "READ HEADER")) == 0)
3919 + reply = do_read_header(fsg, bh);
3920 + break;
3921 +
3922 + case READ_TOC:
3923 + if (!mod_data.cdrom)
3924 + goto unknown_cmnd;
3925 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3926 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3927 + (7<<6) | (1<<1), 1,
3928 + "READ TOC")) == 0)
3929 + reply = do_read_toc(fsg, bh);
3930 + break;
3931 +
3932 + case READ_FORMAT_CAPACITIES:
3933 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3934 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3935 + (3<<7), 1,
3936 + "READ FORMAT CAPACITIES")) == 0)
3937 + reply = do_read_format_capacities(fsg, bh);
3938 + break;
3939 +
3940 + case REQUEST_SENSE:
3941 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3942 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3943 + (1<<4), 0,
3944 + "REQUEST SENSE")) == 0)
3945 + reply = do_request_sense(fsg, bh);
3946 + break;
3947 +
3948 + case START_STOP:
3949 + fsg->data_size_from_cmnd = 0;
3950 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
3951 + (1<<1) | (1<<4), 0,
3952 + "START-STOP UNIT")) == 0)
3953 + reply = do_start_stop(fsg);
3954 + break;
3955 +
3956 + case SYNCHRONIZE_CACHE:
3957 + fsg->data_size_from_cmnd = 0;
3958 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3959 + (0xf<<2) | (3<<7), 1,
3960 + "SYNCHRONIZE CACHE")) == 0)
3961 + reply = do_synchronize_cache(fsg);
3962 + break;
3963 +
3964 + case TEST_UNIT_READY:
3965 + fsg->data_size_from_cmnd = 0;
3966 + reply = check_command(fsg, 6, DATA_DIR_NONE,
3967 + 0, 1,
3968 + "TEST UNIT READY");
3969 + break;
3970 +
3971 + /* Although optional, this command is used by MS-Windows. We
3972 + * support a minimal version: BytChk must be 0. */
3973 + case VERIFY:
3974 + fsg->data_size_from_cmnd = 0;
3975 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3976 + (1<<1) | (0xf<<2) | (3<<7), 1,
3977 + "VERIFY")) == 0)
3978 + reply = do_verify(fsg);
3979 + break;
3980 +
3981 + case WRITE_6:
3982 + i = fsg->cmnd[4];
3983 + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
3984 + if ((reply = check_command_size_in_blocks(fsg, 6,
3985 + DATA_DIR_FROM_HOST,
3986 + (7<<1) | (1<<4), 1,
3987 + "WRITE(6)")) == 0)
3988 + reply = do_write(fsg);
3989 + break;
3990 +
3991 + case WRITE_10:
3992 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3993 + if ((reply = check_command_size_in_blocks(fsg, 10,
3994 + DATA_DIR_FROM_HOST,
3995 + (1<<1) | (0xf<<2) | (3<<7), 1,
3996 + "WRITE(10)")) == 0)
3997 + reply = do_write(fsg);
3998 + break;
3999 +
4000 + case WRITE_12:
4001 + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
4002 + if ((reply = check_command_size_in_blocks(fsg, 12,
4003 + DATA_DIR_FROM_HOST,
4004 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
4005 + "WRITE(12)")) == 0)
4006 + reply = do_write(fsg);
4007 + break;
4008 +
4009 + /* Some mandatory commands that we recognize but don't implement.
4010 + * They don't mean much in this setting. It's left as an exercise
4011 + * for anyone interested to implement RESERVE and RELEASE in terms
4012 + * of Posix locks. */
4013 + case FORMAT_UNIT:
4014 + case RELEASE:
4015 + case RESERVE:
4016 + case SEND_DIAGNOSTIC:
4017 + // Fall through
4018 +
4019 + default:
4020 + unknown_cmnd:
4021 + fsg->data_size_from_cmnd = 0;
4022 + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
4023 + if ((reply = check_command(fsg, fsg->cmnd_size,
4024 + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
4025 + fsg->curlun->sense_data = SS_INVALID_COMMAND;
4026 + reply = -EINVAL;
4027 + }
4028 + break;
4029 + }
4030 + up_read(&fsg->filesem);
4031 +
4032 + if (reply == -EINTR || signal_pending(current))
4033 + return -EINTR;
4034 +
4035 + /* Set up the single reply buffer for finish_reply() */
4036 + if (reply == -EINVAL)
4037 + reply = 0; // Error reply length
4038 + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
4039 + reply = min((u32) reply, fsg->data_size_from_cmnd);
4040 + bh->inreq->length = reply;
4041 + bh->state = BUF_STATE_FULL;
4042 + fsg->residue -= reply;
4043 + } // Otherwise it's already set
4044 +
4045 + return 0;
4046 +}
4047 +
4048 +
4049 +/*-------------------------------------------------------------------------*/
4050 +
4051 +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
4052 +{
4053 + struct usb_request *req = bh->outreq;
4054 + struct bulk_cb_wrap *cbw = req->buf;
4055 +
4056 + /* Was this a real packet? Should it be ignored? */
4057 + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
4058 + return -EINVAL;
4059 +
4060 + /* Is the CBW valid? */
4061 + if (req->actual != US_BULK_CB_WRAP_LEN ||
4062 + cbw->Signature != cpu_to_le32(
4063 + US_BULK_CB_SIGN)) {
4064 + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
4065 + req->actual,
4066 + le32_to_cpu(cbw->Signature));
4067 +
4068 + /* The Bulk-only spec says we MUST stall the IN endpoint
4069 + * (6.6.1), so it's unavoidable. It also says we must
4070 + * retain this state until the next reset, but there's
4071 + * no way to tell the controller driver it should ignore
4072 + * Clear-Feature(HALT) requests.
4073 + *
4074 + * We aren't required to halt the OUT endpoint; instead
4075 + * we can simply accept and discard any data received
4076 + * until the next reset. */
4077 + wedge_bulk_in_endpoint(fsg);
4078 + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
4079 + return -EINVAL;
4080 + }
4081 +
4082 + /* Is the CBW meaningful? */
4083 + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
4084 + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
4085 + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
4086 + "cmdlen %u\n",
4087 + cbw->Lun, cbw->Flags, cbw->Length);
4088 +
4089 + /* We can do anything we want here, so let's stall the
4090 + * bulk pipes if we are allowed to. */
4091 + if (mod_data.can_stall) {
4092 + fsg_set_halt(fsg, fsg->bulk_out);
4093 + halt_bulk_in_endpoint(fsg);
4094 + }
4095 + return -EINVAL;
4096 + }
4097 +
4098 + /* Save the command for later */
4099 + fsg->cmnd_size = cbw->Length;
4100 + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
4101 + if (cbw->Flags & US_BULK_FLAG_IN)
4102 + fsg->data_dir = DATA_DIR_TO_HOST;
4103 + else
4104 + fsg->data_dir = DATA_DIR_FROM_HOST;
4105 + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
4106 + if (fsg->data_size == 0)
4107 + fsg->data_dir = DATA_DIR_NONE;
4108 + fsg->lun = cbw->Lun;
4109 + fsg->tag = cbw->Tag;
4110 + return 0;
4111 +}
4112 +
4113 +
4114 +static int get_next_command(struct fsg_dev *fsg)
4115 +{
4116 + struct fsg_buffhd *bh;
4117 + int rc = 0;
4118 +
4119 + if (transport_is_bbb()) {
4120 +
4121 + /* Wait for the next buffer to become available */
4122 + bh = fsg->next_buffhd_to_fill;
4123 + while (bh->state != BUF_STATE_EMPTY) {
4124 + rc = sleep_thread(fsg);
4125 + if (rc)
4126 + return rc;
4127 + }
4128 +
4129 + /* Queue a request to read a Bulk-only CBW */
4130 + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
4131 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
4132 + &bh->outreq_busy, &bh->state);
4133 +
4134 + /* We will drain the buffer in software, which means we
4135 + * can reuse it for the next filling. No need to advance
4136 + * next_buffhd_to_fill. */
4137 +
4138 + /* Wait for the CBW to arrive */
4139 + while (bh->state != BUF_STATE_FULL) {
4140 + rc = sleep_thread(fsg);
4141 + if (rc)
4142 + return rc;
4143 + }
4144 + smp_rmb();
4145 + rc = received_cbw(fsg, bh);
4146 + bh->state = BUF_STATE_EMPTY;
4147 +
4148 + } else { // USB_PR_CB or USB_PR_CBI
4149 +
4150 + /* Wait for the next command to arrive */
4151 + while (fsg->cbbuf_cmnd_size == 0) {
4152 + rc = sleep_thread(fsg);
4153 + if (rc)
4154 + return rc;
4155 + }
4156 +
4157 + /* Is the previous status interrupt request still busy?
4158 + * The host is allowed to skip reading the status,
4159 + * so we must cancel it. */
4160 + if (fsg->intreq_busy)
4161 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
4162 +
4163 + /* Copy the command and mark the buffer empty */
4164 + fsg->data_dir = DATA_DIR_UNKNOWN;
4165 + spin_lock_irq(&fsg->lock);
4166 + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
4167 + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
4168 + fsg->cbbuf_cmnd_size = 0;
4169 + spin_unlock_irq(&fsg->lock);
4170 +
4171 + /* Use LUN from the command */
4172 + fsg->lun = fsg->cmnd[1] >> 5;
4173 + }
4174 +
4175 + /* Update current lun */
4176 + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
4177 + fsg->curlun = &fsg->luns[fsg->lun];
4178 + else
4179 + fsg->curlun = NULL;
4180 +
4181 + return rc;
4182 +}
4183 +
4184 +
4185 +/*-------------------------------------------------------------------------*/
4186 +
4187 +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
4188 + const struct usb_endpoint_descriptor *d)
4189 +{
4190 + int rc;
4191 +
4192 + ep->driver_data = fsg;
4193 + ep->desc = d;
4194 + rc = usb_ep_enable(ep);
4195 + if (rc)
4196 + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
4197 + return rc;
4198 +}
4199 +
4200 +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
4201 + struct usb_request **preq)
4202 +{
4203 + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
4204 + if (*preq)
4205 + return 0;
4206 + ERROR(fsg, "can't allocate request for %s\n", ep->name);
4207 + return -ENOMEM;
4208 +}
4209 +
4210 +/*
4211 + * Reset interface setting and re-init endpoint state (toggle etc).
4212 + * Call with altsetting < 0 to disable the interface. The only other
4213 + * available altsetting is 0, which enables the interface.
4214 + */
4215 +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
4216 +{
4217 + int rc = 0;
4218 + int i;
4219 + const struct usb_endpoint_descriptor *d;
4220 +
4221 + if (fsg->running)
4222 + DBG(fsg, "reset interface\n");
4223 +
4224 +reset:
4225 + /* Deallocate the requests */
4226 + for (i = 0; i < fsg_num_buffers; ++i) {
4227 + struct fsg_buffhd *bh = &fsg->buffhds[i];
4228 +
4229 + if (bh->inreq) {
4230 + usb_ep_free_request(fsg->bulk_in, bh->inreq);
4231 + bh->inreq = NULL;
4232 + }
4233 + if (bh->outreq) {
4234 + usb_ep_free_request(fsg->bulk_out, bh->outreq);
4235 + bh->outreq = NULL;
4236 + }
4237 + }
4238 + if (fsg->intreq) {
4239 + usb_ep_free_request(fsg->intr_in, fsg->intreq);
4240 + fsg->intreq = NULL;
4241 + }
4242 +
4243 + /* Disable the endpoints */
4244 + if (fsg->bulk_in_enabled) {
4245 + usb_ep_disable(fsg->bulk_in);
4246 + fsg->bulk_in_enabled = 0;
4247 + }
4248 + if (fsg->bulk_out_enabled) {
4249 + usb_ep_disable(fsg->bulk_out);
4250 + fsg->bulk_out_enabled = 0;
4251 + }
4252 + if (fsg->intr_in_enabled) {
4253 + usb_ep_disable(fsg->intr_in);
4254 + fsg->intr_in_enabled = 0;
4255 + }
4256 +
4257 + fsg->running = 0;
4258 + if (altsetting < 0 || rc != 0)
4259 + return rc;
4260 +
4261 + DBG(fsg, "set interface %d\n", altsetting);
4262 +
4263 + /* Enable the endpoints */
4264 + d = fsg_ep_desc(fsg->gadget,
4265 + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
4266 + &fsg_ss_bulk_in_desc);
4267 + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
4268 + goto reset;
4269 + fsg->bulk_in_enabled = 1;
4270 +
4271 + d = fsg_ep_desc(fsg->gadget,
4272 + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
4273 + &fsg_ss_bulk_out_desc);
4274 + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
4275 + goto reset;
4276 + fsg->bulk_out_enabled = 1;
4277 + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
4278 + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
4279 +
4280 + if (transport_is_cbi()) {
4281 + d = fsg_ep_desc(fsg->gadget,
4282 + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
4283 + &fsg_ss_intr_in_desc);
4284 + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
4285 + goto reset;
4286 + fsg->intr_in_enabled = 1;
4287 + }
4288 +
4289 + /* Allocate the requests */
4290 + for (i = 0; i < fsg_num_buffers; ++i) {
4291 + struct fsg_buffhd *bh = &fsg->buffhds[i];
4292 +
4293 + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
4294 + goto reset;
4295 + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
4296 + goto reset;
4297 + bh->inreq->buf = bh->outreq->buf = bh->buf;
4298 + bh->inreq->context = bh->outreq->context = bh;
4299 + bh->inreq->complete = bulk_in_complete;
4300 + bh->outreq->complete = bulk_out_complete;
4301 + }
4302 + if (transport_is_cbi()) {
4303 + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
4304 + goto reset;
4305 + fsg->intreq->complete = intr_in_complete;
4306 + }
4307 +
4308 + fsg->running = 1;
4309 + for (i = 0; i < fsg->nluns; ++i)
4310 + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
4311 + return rc;
4312 +}
4313 +
4314 +
4315 +/*
4316 + * Change our operational configuration. This code must agree with the code
4317 + * that returns config descriptors, and with interface altsetting code.
4318 + *
4319 + * It's also responsible for power management interactions. Some
4320 + * configurations might not work with our current power sources.
4321 + * For now we just assume the gadget is always self-powered.
4322 + */
4323 +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
4324 +{
4325 + int rc = 0;
4326 +
4327 + /* Disable the single interface */
4328 + if (fsg->config != 0) {
4329 + DBG(fsg, "reset config\n");
4330 + fsg->config = 0;
4331 + rc = do_set_interface(fsg, -1);
4332 + }
4333 +
4334 + /* Enable the interface */
4335 + if (new_config != 0) {
4336 + fsg->config = new_config;
4337 + if ((rc = do_set_interface(fsg, 0)) != 0)
4338 + fsg->config = 0; // Reset on errors
4339 + else
4340 + INFO(fsg, "%s config #%d\n",
4341 + usb_speed_string(fsg->gadget->speed),
4342 + fsg->config);
4343 + }
4344 + return rc;
4345 +}
4346 +
4347 +
4348 +/*-------------------------------------------------------------------------*/
4349 +
4350 +static void handle_exception(struct fsg_dev *fsg)
4351 +{
4352 + siginfo_t info;
4353 + int sig;
4354 + int i;
4355 + int num_active;
4356 + struct fsg_buffhd *bh;
4357 + enum fsg_state old_state;
4358 + u8 new_config;
4359 + struct fsg_lun *curlun;
4360 + unsigned int exception_req_tag;
4361 + int rc;
4362 +
4363 + /* Clear the existing signals. Anything but SIGUSR1 is converted
4364 + * into a high-priority EXIT exception. */
4365 + for (;;) {
4366 + sig = dequeue_signal_lock(current, &current->blocked, &info);
4367 + if (!sig)
4368 + break;
4369 + if (sig != SIGUSR1) {
4370 + if (fsg->state < FSG_STATE_EXIT)
4371 + DBG(fsg, "Main thread exiting on signal\n");
4372 + raise_exception(fsg, FSG_STATE_EXIT);
4373 + }
4374 + }
4375 +
4376 + /* Cancel all the pending transfers */
4377 + if (fsg->intreq_busy)
4378 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
4379 + for (i = 0; i < fsg_num_buffers; ++i) {
4380 + bh = &fsg->buffhds[i];
4381 + if (bh->inreq_busy)
4382 + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
4383 + if (bh->outreq_busy)
4384 + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
4385 + }
4386 +
4387 + /* Wait until everything is idle */
4388 + for (;;) {
4389 + num_active = fsg->intreq_busy;
4390 + for (i = 0; i < fsg_num_buffers; ++i) {
4391 + bh = &fsg->buffhds[i];
4392 + num_active += bh->inreq_busy + bh->outreq_busy;
4393 + }
4394 + if (num_active == 0)
4395 + break;
4396 + if (sleep_thread(fsg))
4397 + return;
4398 + }
4399 +
4400 + /* Clear out the controller's fifos */
4401 + if (fsg->bulk_in_enabled)
4402 + usb_ep_fifo_flush(fsg->bulk_in);
4403 + if (fsg->bulk_out_enabled)
4404 + usb_ep_fifo_flush(fsg->bulk_out);
4405 + if (fsg->intr_in_enabled)
4406 + usb_ep_fifo_flush(fsg->intr_in);
4407 +
4408 + /* Reset the I/O buffer states and pointers, the SCSI
4409 + * state, and the exception. Then invoke the handler. */
4410 + spin_lock_irq(&fsg->lock);
4411 +
4412 + for (i = 0; i < fsg_num_buffers; ++i) {
4413 + bh = &fsg->buffhds[i];
4414 + bh->state = BUF_STATE_EMPTY;
4415 + }
4416 + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
4417 + &fsg->buffhds[0];
4418 +
4419 + exception_req_tag = fsg->exception_req_tag;
4420 + new_config = fsg->new_config;
4421 + old_state = fsg->state;
4422 +
4423 + if (old_state == FSG_STATE_ABORT_BULK_OUT)
4424 + fsg->state = FSG_STATE_STATUS_PHASE;
4425 + else {
4426 + for (i = 0; i < fsg->nluns; ++i) {
4427 + curlun = &fsg->luns[i];
4428 + curlun->prevent_medium_removal = 0;
4429 + curlun->sense_data = curlun->unit_attention_data =
4430 + SS_NO_SENSE;
4431 + curlun->sense_data_info = 0;
4432 + curlun->info_valid = 0;
4433 + }
4434 + fsg->state = FSG_STATE_IDLE;
4435 + }
4436 + spin_unlock_irq(&fsg->lock);
4437 +
4438 + /* Carry out any extra actions required for the exception */
4439 + switch (old_state) {
4440 + default:
4441 + break;
4442 +
4443 + case FSG_STATE_ABORT_BULK_OUT:
4444 + send_status(fsg);
4445 + spin_lock_irq(&fsg->lock);
4446 + if (fsg->state == FSG_STATE_STATUS_PHASE)
4447 + fsg->state = FSG_STATE_IDLE;
4448 + spin_unlock_irq(&fsg->lock);
4449 + break;
4450 +
4451 + case FSG_STATE_RESET:
4452 + /* In case we were forced against our will to halt a
4453 + * bulk endpoint, clear the halt now. (The SuperH UDC
4454 + * requires this.) */
4455 + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
4456 + usb_ep_clear_halt(fsg->bulk_in);
4457 +
4458 + if (transport_is_bbb()) {
4459 + if (fsg->ep0_req_tag == exception_req_tag)
4460 + ep0_queue(fsg); // Complete the status stage
4461 +
4462 + } else if (transport_is_cbi())
4463 + send_status(fsg); // Status by interrupt pipe
4464 +
4465 + /* Technically this should go here, but it would only be
4466 + * a waste of time. Ditto for the INTERFACE_CHANGE and
4467 + * CONFIG_CHANGE cases. */
4468 + // for (i = 0; i < fsg->nluns; ++i)
4469 + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
4470 + break;
4471 +
4472 + case FSG_STATE_INTERFACE_CHANGE:
4473 + rc = do_set_interface(fsg, 0);
4474 + if (fsg->ep0_req_tag != exception_req_tag)
4475 + break;
4476 + if (rc != 0) // STALL on errors
4477 + fsg_set_halt(fsg, fsg->ep0);
4478 + else // Complete the status stage
4479 + ep0_queue(fsg);
4480 + break;
4481 +
4482 + case FSG_STATE_CONFIG_CHANGE:
4483 + rc = do_set_config(fsg, new_config);
4484 + if (fsg->ep0_req_tag != exception_req_tag)
4485 + break;
4486 + if (rc != 0) // STALL on errors
4487 + fsg_set_halt(fsg, fsg->ep0);
4488 + else // Complete the status stage
4489 + ep0_queue(fsg);
4490 + break;
4491 +
4492 + case FSG_STATE_DISCONNECT:
4493 + for (i = 0; i < fsg->nluns; ++i)
4494 + fsg_lun_fsync_sub(fsg->luns + i);
4495 + do_set_config(fsg, 0); // Unconfigured state
4496 + break;
4497 +
4498 + case FSG_STATE_EXIT:
4499 + case FSG_STATE_TERMINATED:
4500 + do_set_config(fsg, 0); // Free resources
4501 + spin_lock_irq(&fsg->lock);
4502 + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
4503 + spin_unlock_irq(&fsg->lock);
4504 + break;
4505 + }
4506 +}
4507 +
4508 +
4509 +/*-------------------------------------------------------------------------*/
4510 +
4511 +static int fsg_main_thread(void *fsg_)
4512 +{
4513 + struct fsg_dev *fsg = fsg_;
4514 +
4515 + /* Allow the thread to be killed by a signal, but set the signal mask
4516 + * to block everything but INT, TERM, KILL, and USR1. */
4517 + allow_signal(SIGINT);
4518 + allow_signal(SIGTERM);
4519 + allow_signal(SIGKILL);
4520 + allow_signal(SIGUSR1);
4521 +
4522 + /* Allow the thread to be frozen */
4523 + set_freezable();
4524 +
4525 + /* Arrange for userspace references to be interpreted as kernel
4526 + * pointers. That way we can pass a kernel pointer to a routine
4527 + * that expects a __user pointer and it will work okay. */
4528 + set_fs(get_ds());
4529 +
4530 + /* The main loop */
4531 + while (fsg->state != FSG_STATE_TERMINATED) {
4532 + if (exception_in_progress(fsg) || signal_pending(current)) {
4533 + handle_exception(fsg);
4534 + continue;
4535 + }
4536 +
4537 + if (!fsg->running) {
4538 + sleep_thread(fsg);
4539 + continue;
4540 + }
4541 +
4542 + if (get_next_command(fsg))
4543 + continue;
4544 +
4545 + spin_lock_irq(&fsg->lock);
4546 + if (!exception_in_progress(fsg))
4547 + fsg->state = FSG_STATE_DATA_PHASE;
4548 + spin_unlock_irq(&fsg->lock);
4549 +
4550 + if (do_scsi_command(fsg) || finish_reply(fsg))
4551 + continue;
4552 +
4553 + spin_lock_irq(&fsg->lock);
4554 + if (!exception_in_progress(fsg))
4555 + fsg->state = FSG_STATE_STATUS_PHASE;
4556 + spin_unlock_irq(&fsg->lock);
4557 +
4558 + if (send_status(fsg))
4559 + continue;
4560 +
4561 + spin_lock_irq(&fsg->lock);
4562 + if (!exception_in_progress(fsg))
4563 + fsg->state = FSG_STATE_IDLE;
4564 + spin_unlock_irq(&fsg->lock);
4565 + }
4566 +
4567 + spin_lock_irq(&fsg->lock);
4568 + fsg->thread_task = NULL;
4569 + spin_unlock_irq(&fsg->lock);
4570 +
4571 + /* If we are exiting because of a signal, unregister the
4572 + * gadget driver. */
4573 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
4574 + usb_gadget_unregister_driver(&fsg_driver);
4575 +
4576 + /* Let the unbind and cleanup routines know the thread has exited */
4577 + kthread_complete_and_exit(&fsg->thread_notifier, 0);
4578 +}
4579 +
4580 +
4581 +/*-------------------------------------------------------------------------*/
4582 +
4583 +
4584 +/* The write permissions and store_xxx pointers are set in fsg_bind() */
4585 +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
4586 +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
4587 +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
4588 +
4589 +
4590 +/*-------------------------------------------------------------------------*/
4591 +
4592 +static void fsg_release(struct kref *ref)
4593 +{
4594 + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
4595 +
4596 + kfree(fsg->luns);
4597 + kfree(fsg);
4598 +}
4599 +
4600 +static void lun_release(struct device *dev)
4601 +{
4602 + struct rw_semaphore *filesem = dev_get_drvdata(dev);
4603 + struct fsg_dev *fsg =
4604 + container_of(filesem, struct fsg_dev, filesem);
4605 +
4606 + kref_put(&fsg->ref, fsg_release);
4607 +}
4608 +
4609 +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
4610 +{
4611 + struct fsg_dev *fsg = get_gadget_data(gadget);
4612 + int i;
4613 + struct fsg_lun *curlun;
4614 + struct usb_request *req = fsg->ep0req;
4615 +
4616 + DBG(fsg, "unbind\n");
4617 + clear_bit(REGISTERED, &fsg->atomic_bitflags);
4618 +
4619 + /* If the thread isn't already dead, tell it to exit now */
4620 + if (fsg->state != FSG_STATE_TERMINATED) {
4621 + raise_exception(fsg, FSG_STATE_EXIT);
4622 + wait_for_completion(&fsg->thread_notifier);
4623 +
4624 + /* The cleanup routine waits for this completion also */
4625 + complete(&fsg->thread_notifier);
4626 + }
4627 +
4628 + /* Unregister the sysfs attribute files and the LUNs */
4629 + for (i = 0; i < fsg->nluns; ++i) {
4630 + curlun = &fsg->luns[i];
4631 + if (curlun->registered) {
4632 + device_remove_file(&curlun->dev, &dev_attr_nofua);
4633 + device_remove_file(&curlun->dev, &dev_attr_ro);
4634 + device_remove_file(&curlun->dev, &dev_attr_file);
4635 + fsg_lun_close(curlun);
4636 + device_unregister(&curlun->dev);
4637 + curlun->registered = 0;
4638 + }
4639 + }
4640 +
4641 + /* Free the data buffers */
4642 + for (i = 0; i < fsg_num_buffers; ++i)
4643 + kfree(fsg->buffhds[i].buf);
4644 +
4645 + /* Free the request and buffer for endpoint 0 */
4646 + if (req) {
4647 + kfree(req->buf);
4648 + usb_ep_free_request(fsg->ep0, req);
4649 + }
4650 +
4651 + set_gadget_data(gadget, NULL);
4652 +}
4653 +
4654 +
4655 +static int __init check_parameters(struct fsg_dev *fsg)
4656 +{
4657 + int prot;
4658 + int gcnum;
4659 +
4660 + /* Store the default values */
4661 + mod_data.transport_type = USB_PR_BULK;
4662 + mod_data.transport_name = "Bulk-only";
4663 + mod_data.protocol_type = USB_SC_SCSI;
4664 + mod_data.protocol_name = "Transparent SCSI";
4665 +
4666 + /* Some peripheral controllers are known not to be able to
4667 + * halt bulk endpoints correctly. If one of them is present,
4668 + * disable stalls.
4669 + */
4670 + if (gadget_is_at91(fsg->gadget))
4671 + mod_data.can_stall = 0;
4672 +
4673 + if (mod_data.release == 0xffff) { // Parameter wasn't set
4674 + gcnum = usb_gadget_controller_number(fsg->gadget);
4675 + if (gcnum >= 0)
4676 + mod_data.release = 0x0300 + gcnum;
4677 + else {
4678 + WARNING(fsg, "controller '%s' not recognized\n",
4679 + fsg->gadget->name);
4680 + mod_data.release = 0x0399;
4681 + }
4682 + }
4683 +
4684 + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
4685 +
4686 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
4687 + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
4688 + ; // Use default setting
4689 + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
4690 + mod_data.transport_type = USB_PR_CB;
4691 + mod_data.transport_name = "Control-Bulk";
4692 + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
4693 + mod_data.transport_type = USB_PR_CBI;
4694 + mod_data.transport_name = "Control-Bulk-Interrupt";
4695 + } else {
4696 + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
4697 + return -EINVAL;
4698 + }
4699 +
4700 + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
4701 + prot == USB_SC_SCSI) {
4702 + ; // Use default setting
4703 + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
4704 + prot == USB_SC_RBC) {
4705 + mod_data.protocol_type = USB_SC_RBC;
4706 + mod_data.protocol_name = "RBC";
4707 + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
4708 + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
4709 + prot == USB_SC_8020) {
4710 + mod_data.protocol_type = USB_SC_8020;
4711 + mod_data.protocol_name = "8020i (ATAPI)";
4712 + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
4713 + prot == USB_SC_QIC) {
4714 + mod_data.protocol_type = USB_SC_QIC;
4715 + mod_data.protocol_name = "QIC-157";
4716 + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
4717 + prot == USB_SC_UFI) {
4718 + mod_data.protocol_type = USB_SC_UFI;
4719 + mod_data.protocol_name = "UFI";
4720 + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
4721 + prot == USB_SC_8070) {
4722 + mod_data.protocol_type = USB_SC_8070;
4723 + mod_data.protocol_name = "8070i";
4724 + } else {
4725 + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
4726 + return -EINVAL;
4727 + }
4728 +
4729 + mod_data.buflen &= PAGE_CACHE_MASK;
4730 + if (mod_data.buflen <= 0) {
4731 + ERROR(fsg, "invalid buflen\n");
4732 + return -ETOOSMALL;
4733 + }
4734 +
4735 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
4736 +
4737 + /* Serial string handling.
4738 + * On a real device, the serial string would be loaded
4739 + * from permanent storage. */
4740 + if (mod_data.serial) {
4741 + const char *ch;
4742 + unsigned len = 0;
4743 +
4744 + /* Sanity check :
4745 + * The CB[I] specification limits the serial string to
4746 + * 12 uppercase hexadecimal characters.
4747 + * BBB need at least 12 uppercase hexadecimal characters,
4748 + * with a maximum of 126. */
4749 + for (ch = mod_data.serial; *ch; ++ch) {
4750 + ++len;
4751 + if ((*ch < '0' || *ch > '9') &&
4752 + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
4753 + WARNING(fsg,
4754 + "Invalid serial string character: %c\n",
4755 + *ch);
4756 + goto no_serial;
4757 + }
4758 + }
4759 + if (len > 126 ||
4760 + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
4761 + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
4762 + WARNING(fsg, "Invalid serial string length!\n");
4763 + goto no_serial;
4764 + }
4765 + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
4766 + } else {
4767 + WARNING(fsg, "No serial-number string provided!\n");
4768 + no_serial:
4769 + device_desc.iSerialNumber = 0;
4770 + }
4771 +
4772 + return 0;
4773 +}
4774 +
4775 +
4776 +static int __init fsg_bind(struct usb_gadget *gadget)
4777 +{
4778 + struct fsg_dev *fsg = the_fsg;
4779 + int rc;
4780 + int i;
4781 + struct fsg_lun *curlun;
4782 + struct usb_ep *ep;
4783 + struct usb_request *req;
4784 + char *pathbuf, *p;
4785 +
4786 + fsg->gadget = gadget;
4787 + set_gadget_data(gadget, fsg);
4788 + fsg->ep0 = gadget->ep0;
4789 + fsg->ep0->driver_data = fsg;
4790 +
4791 + if ((rc = check_parameters(fsg)) != 0)
4792 + goto out;
4793 +
4794 + if (mod_data.removable) { // Enable the store_xxx attributes
4795 + dev_attr_file.attr.mode = 0644;
4796 + dev_attr_file.store = fsg_store_file;
4797 + if (!mod_data.cdrom) {
4798 + dev_attr_ro.attr.mode = 0644;
4799 + dev_attr_ro.store = fsg_store_ro;
4800 + }
4801 + }
4802 +
4803 + /* Only for removable media? */
4804 + dev_attr_nofua.attr.mode = 0644;
4805 + dev_attr_nofua.store = fsg_store_nofua;
4806 +
4807 + /* Find out how many LUNs there should be */
4808 + i = mod_data.nluns;
4809 + if (i == 0)
4810 + i = max(mod_data.num_filenames, 1u);
4811 + if (i > FSG_MAX_LUNS) {
4812 + ERROR(fsg, "invalid number of LUNs: %d\n", i);
4813 + rc = -EINVAL;
4814 + goto out;
4815 + }
4816 +
4817 + /* Create the LUNs, open their backing files, and register the
4818 + * LUN devices in sysfs. */
4819 + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
4820 + if (!fsg->luns) {
4821 + rc = -ENOMEM;
4822 + goto out;
4823 + }
4824 + fsg->nluns = i;
4825 +
4826 + for (i = 0; i < fsg->nluns; ++i) {
4827 + curlun = &fsg->luns[i];
4828 + curlun->cdrom = !!mod_data.cdrom;
4829 + curlun->ro = mod_data.cdrom || mod_data.ro[i];
4830 + curlun->initially_ro = curlun->ro;
4831 + curlun->removable = mod_data.removable;
4832 + curlun->nofua = mod_data.nofua[i];
4833 + curlun->dev.release = lun_release;
4834 + curlun->dev.parent = &gadget->dev;
4835 + curlun->dev.driver = &fsg_driver.driver;
4836 + dev_set_drvdata(&curlun->dev, &fsg->filesem);
4837 + dev_set_name(&curlun->dev,"%s-lun%d",
4838 + dev_name(&gadget->dev), i);
4839 +
4840 + kref_get(&fsg->ref);
4841 + rc = device_register(&curlun->dev);
4842 + if (rc) {
4843 + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
4844 + put_device(&curlun->dev);
4845 + goto out;
4846 + }
4847 + curlun->registered = 1;
4848 +
4849 + rc = device_create_file(&curlun->dev, &dev_attr_ro);
4850 + if (rc)
4851 + goto out;
4852 + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
4853 + if (rc)
4854 + goto out;
4855 + rc = device_create_file(&curlun->dev, &dev_attr_file);
4856 + if (rc)
4857 + goto out;
4858 +
4859 + if (mod_data.file[i] && *mod_data.file[i]) {
4860 + rc = fsg_lun_open(curlun, mod_data.file[i]);
4861 + if (rc)
4862 + goto out;
4863 + } else if (!mod_data.removable) {
4864 + ERROR(fsg, "no file given for LUN%d\n", i);
4865 + rc = -EINVAL;
4866 + goto out;
4867 + }
4868 + }
4869 +
4870 + /* Find all the endpoints we will use */
4871 + usb_ep_autoconfig_reset(gadget);
4872 + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
4873 + if (!ep)
4874 + goto autoconf_fail;
4875 + ep->driver_data = fsg; // claim the endpoint
4876 + fsg->bulk_in = ep;
4877 +
4878 + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
4879 + if (!ep)
4880 + goto autoconf_fail;
4881 + ep->driver_data = fsg; // claim the endpoint
4882 + fsg->bulk_out = ep;
4883 +
4884 + if (transport_is_cbi()) {
4885 + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
4886 + if (!ep)
4887 + goto autoconf_fail;
4888 + ep->driver_data = fsg; // claim the endpoint
4889 + fsg->intr_in = ep;
4890 + }
4891 +
4892 + /* Fix up the descriptors */
4893 + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
4894 + device_desc.idProduct = cpu_to_le16(mod_data.product);
4895 + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
4896 +
4897 + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
4898 + fsg_intf_desc.bNumEndpoints = i;
4899 + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
4900 + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
4901 + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4902 +
4903 + if (gadget_is_dualspeed(gadget)) {
4904 + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4905 +
4906 + /* Assume endpoint addresses are the same for both speeds */
4907 + fsg_hs_bulk_in_desc.bEndpointAddress =
4908 + fsg_fs_bulk_in_desc.bEndpointAddress;
4909 + fsg_hs_bulk_out_desc.bEndpointAddress =
4910 + fsg_fs_bulk_out_desc.bEndpointAddress;
4911 + fsg_hs_intr_in_desc.bEndpointAddress =
4912 + fsg_fs_intr_in_desc.bEndpointAddress;
4913 + }
4914 +
4915 + if (gadget_is_superspeed(gadget)) {
4916 + unsigned max_burst;
4917 +
4918 + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4919 +
4920 + /* Calculate bMaxBurst, we know packet size is 1024 */
4921 + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
4922 +
4923 + /* Assume endpoint addresses are the same for both speeds */
4924 + fsg_ss_bulk_in_desc.bEndpointAddress =
4925 + fsg_fs_bulk_in_desc.bEndpointAddress;
4926 + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
4927 +
4928 + fsg_ss_bulk_out_desc.bEndpointAddress =
4929 + fsg_fs_bulk_out_desc.bEndpointAddress;
4930 + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
4931 + }
4932 +
4933 + if (gadget_is_otg(gadget))
4934 + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
4935 +
4936 + rc = -ENOMEM;
4937 +
4938 + /* Allocate the request and buffer for endpoint 0 */
4939 + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
4940 + if (!req)
4941 + goto out;
4942 + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
4943 + if (!req->buf)
4944 + goto out;
4945 + req->complete = ep0_complete;
4946 +
4947 + /* Allocate the data buffers */
4948 + for (i = 0; i < fsg_num_buffers; ++i) {
4949 + struct fsg_buffhd *bh = &fsg->buffhds[i];
4950 +
4951 + /* Allocate for the bulk-in endpoint. We assume that
4952 + * the buffer will also work with the bulk-out (and
4953 + * interrupt-in) endpoint. */
4954 + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
4955 + if (!bh->buf)
4956 + goto out;
4957 + bh->next = bh + 1;
4958 + }
4959 + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
4960 +
4961 + /* This should reflect the actual gadget power source */
4962 + usb_gadget_set_selfpowered(gadget);
4963 +
4964 + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
4965 + "%s %s with %s",
4966 + init_utsname()->sysname, init_utsname()->release,
4967 + gadget->name);
4968 +
4969 + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
4970 + "file-storage-gadget");
4971 + if (IS_ERR(fsg->thread_task)) {
4972 + rc = PTR_ERR(fsg->thread_task);
4973 + goto out;
4974 + }
4975 +
4976 + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
4977 + INFO(fsg, "NOTE: This driver is deprecated. "
4978 + "Consider using g_mass_storage instead.\n");
4979 + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
4980 +
4981 + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
4982 + for (i = 0; i < fsg->nluns; ++i) {
4983 + curlun = &fsg->luns[i];
4984 + if (fsg_lun_is_open(curlun)) {
4985 + p = NULL;
4986 + if (pathbuf) {
4987 + p = d_path(&curlun->filp->f_path,
4988 + pathbuf, PATH_MAX);
4989 + if (IS_ERR(p))
4990 + p = NULL;
4991 + }
4992 + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
4993 + curlun->ro, curlun->nofua, (p ? p : "(error)"));
4994 + }
4995 + }
4996 + kfree(pathbuf);
4997 +
4998 + DBG(fsg, "transport=%s (x%02x)\n",
4999 + mod_data.transport_name, mod_data.transport_type);
5000 + DBG(fsg, "protocol=%s (x%02x)\n",
5001 + mod_data.protocol_name, mod_data.protocol_type);
5002 + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
5003 + mod_data.vendor, mod_data.product, mod_data.release);
5004 + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
5005 + mod_data.removable, mod_data.can_stall,
5006 + mod_data.cdrom, mod_data.buflen);
5007 + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
5008 +
5009 + set_bit(REGISTERED, &fsg->atomic_bitflags);
5010 +
5011 + /* Tell the thread to start working */
5012 + wake_up_process(fsg->thread_task);
5013 + return 0;
5014 +
5015 +autoconf_fail:
5016 + ERROR(fsg, "unable to autoconfigure all endpoints\n");
5017 + rc = -ENOTSUPP;
5018 +
5019 +out:
5020 + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
5021 + fsg_unbind(gadget);
5022 + complete(&fsg->thread_notifier);
5023 + return rc;
5024 +}
5025 +
5026 +
5027 +/*-------------------------------------------------------------------------*/
5028 +
5029 +static void fsg_suspend(struct usb_gadget *gadget)
5030 +{
5031 + struct fsg_dev *fsg = get_gadget_data(gadget);
5032 +
5033 + DBG(fsg, "suspend\n");
5034 + set_bit(SUSPENDED, &fsg->atomic_bitflags);
5035 +}
5036 +
5037 +static void fsg_resume(struct usb_gadget *gadget)
5038 +{
5039 + struct fsg_dev *fsg = get_gadget_data(gadget);
5040 +
5041 + DBG(fsg, "resume\n");
5042 + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
5043 +}
5044 +
5045 +
5046 +/*-------------------------------------------------------------------------*/
5047 +
5048 +static struct usb_gadget_driver fsg_driver = {
5049 + .max_speed = USB_SPEED_SUPER,
5050 + .function = (char *) fsg_string_product,
5051 + .unbind = fsg_unbind,
5052 + .disconnect = fsg_disconnect,
5053 + .setup = fsg_setup,
5054 + .suspend = fsg_suspend,
5055 + .resume = fsg_resume,
5056 +
5057 + .driver = {
5058 + .name = DRIVER_NAME,
5059 + .owner = THIS_MODULE,
5060 + // .release = ...
5061 + // .suspend = ...
5062 + // .resume = ...
5063 + },
5064 +};
5065 +
5066 +
5067 +static int __init fsg_alloc(void)
5068 +{
5069 + struct fsg_dev *fsg;
5070 +
5071 + fsg = kzalloc(sizeof *fsg +
5072 + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
5073 +
5074 + if (!fsg)
5075 + return -ENOMEM;
5076 + spin_lock_init(&fsg->lock);
5077 + init_rwsem(&fsg->filesem);
5078 + kref_init(&fsg->ref);
5079 + init_completion(&fsg->thread_notifier);
5080 +
5081 + the_fsg = fsg;
5082 + return 0;
5083 +}
5084 +
5085 +
5086 +static int __init fsg_init(void)
5087 +{
5088 + int rc;
5089 + struct fsg_dev *fsg;
5090 +
5091 + rc = fsg_num_buffers_validate();
5092 + if (rc != 0)
5093 + return rc;
5094 +
5095 + if ((rc = fsg_alloc()) != 0)
5096 + return rc;
5097 + fsg = the_fsg;
5098 + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
5099 + kref_put(&fsg->ref, fsg_release);
5100 + return rc;
5101 +}
5102 +module_init(fsg_init);
5103 +
5104 +
5105 +static void __exit fsg_cleanup(void)
5106 +{
5107 + struct fsg_dev *fsg = the_fsg;
5108 +
5109 + /* Unregister the driver iff the thread hasn't already done so */
5110 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
5111 + usb_gadget_unregister_driver(&fsg_driver);
5112 +
5113 + /* Wait for the thread to finish up */
5114 + wait_for_completion(&fsg->thread_notifier);
5115 +
5116 + kref_put(&fsg->ref, fsg_release);
5117 +}
5118 +module_exit(fsg_cleanup);
5119 --- a/drivers/usb/host/Kconfig
5120 +++ b/drivers/usb/host/Kconfig
5121 @@ -742,6 +742,16 @@ config USB_RENESAS_USBHS_HCD
5122 To compile this driver as a module, choose M here: the
5123 module will be called renesas-usbhs.
5124
5125 +config USB_DWCOTG
5126 + bool "Synopsis DWC host support"
5127 + depends on USB && (FIQ || ARM64)
5128 + help
5129 + The Synopsis DWC controller is a dual-role
5130 + host/peripheral/OTG ("On The Go") USB controllers.
5131 +
5132 + Enable this option to support this IP in host controller mode.
5133 + If unsure, say N.
5134 +
5135 config USB_HCD_BCMA
5136 tristate "BCMA usb host driver"
5137 depends on BCMA
5138 --- a/drivers/usb/host/Makefile
5139 +++ b/drivers/usb/host/Makefile
5140 @@ -78,6 +78,7 @@ obj-$(CONFIG_USB_SL811_HCD) += sl811-hcd
5141 obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
5142 obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
5143 obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
5144 +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
5145 obj-$(CONFIG_USB_FSL_USB2) += fsl-mph-dr-of.o
5146 obj-$(CONFIG_USB_EHCI_FSL) += fsl-mph-dr-of.o
5147 obj-$(CONFIG_USB_EHCI_FSL) += ehci-fsl.o
5148 --- /dev/null
5149 +++ b/drivers/usb/host/dwc_common_port/Makefile
5150 @@ -0,0 +1,58 @@
5151 +#
5152 +# Makefile for DWC_common library
5153 +#
5154 +
5155 +ifneq ($(KERNELRELEASE),)
5156 +
5157 +ccflags-y += -DDWC_LINUX
5158 +#ccflags-y += -DDEBUG
5159 +#ccflags-y += -DDWC_DEBUG_REGS
5160 +#ccflags-y += -DDWC_DEBUG_MEMORY
5161 +
5162 +ccflags-y += -DDWC_LIBMODULE
5163 +ccflags-y += -DDWC_CCLIB
5164 +#ccflags-y += -DDWC_CRYPTOLIB
5165 +ccflags-y += -DDWC_NOTIFYLIB
5166 +ccflags-y += -DDWC_UTFLIB
5167 +
5168 +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
5169 +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
5170 + dwc_crypto.o dwc_notifier.o \
5171 + dwc_common_linux.o dwc_mem.o
5172 +
5173 +kernrelwd := $(subst ., ,$(KERNELRELEASE))
5174 +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
5175 +
5176 +ifneq ($(kernrel3),2.6.20)
5177 +# grayg - I only know that we use ccflags-y in 2.6.31 actually
5178 +ccflags-y += $(CPPFLAGS)
5179 +endif
5180 +
5181 +else
5182 +
5183 +#ifeq ($(KDIR),)
5184 +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
5185 +#endif
5186 +
5187 +ifeq ($(ARCH),)
5188 +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
5189 + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
5190 +endif
5191 +
5192 +ifeq ($(DOXYGEN),)
5193 +DOXYGEN := doxygen
5194 +endif
5195 +
5196 +default:
5197 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
5198 +
5199 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
5200 + $(DOXYGEN) doc/doxygen.cfg
5201 +
5202 +tags: $(wildcard *.[hc])
5203 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
5204 +
5205 +endif
5206 +
5207 +clean:
5208 + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
5209 --- /dev/null
5210 +++ b/drivers/usb/host/dwc_common_port/Makefile.fbsd
5211 @@ -0,0 +1,17 @@
5212 +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
5213 +CFLAGS += -DDWC_FREEBSD
5214 +CFLAGS += -DDEBUG
5215 +#CFLAGS += -DDWC_DEBUG_REGS
5216 +#CFLAGS += -DDWC_DEBUG_MEMORY
5217 +
5218 +#CFLAGS += -DDWC_LIBMODULE
5219 +#CFLAGS += -DDWC_CCLIB
5220 +#CFLAGS += -DDWC_CRYPTOLIB
5221 +#CFLAGS += -DDWC_NOTIFYLIB
5222 +#CFLAGS += -DDWC_UTFLIB
5223 +
5224 +KMOD = dwc_common_port_lib
5225 +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
5226 + dwc_common_fbsd.c dwc_mem.c
5227 +
5228 +.include <bsd.kmod.mk>
5229 --- /dev/null
5230 +++ b/drivers/usb/host/dwc_common_port/Makefile.linux
5231 @@ -0,0 +1,49 @@
5232 +#
5233 +# Makefile for DWC_common library
5234 +#
5235 +ifneq ($(KERNELRELEASE),)
5236 +
5237 +ccflags-y += -DDWC_LINUX
5238 +#ccflags-y += -DDEBUG
5239 +#ccflags-y += -DDWC_DEBUG_REGS
5240 +#ccflags-y += -DDWC_DEBUG_MEMORY
5241 +
5242 +ccflags-y += -DDWC_LIBMODULE
5243 +ccflags-y += -DDWC_CCLIB
5244 +ccflags-y += -DDWC_CRYPTOLIB
5245 +ccflags-y += -DDWC_NOTIFYLIB
5246 +ccflags-y += -DDWC_UTFLIB
5247 +
5248 +obj-m := dwc_common_port_lib.o
5249 +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
5250 + dwc_crypto.o dwc_notifier.o \
5251 + dwc_common_linux.o dwc_mem.o
5252 +
5253 +else
5254 +
5255 +ifeq ($(KDIR),)
5256 +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
5257 +endif
5258 +
5259 +ifeq ($(ARCH),)
5260 +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
5261 + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
5262 +endif
5263 +
5264 +ifeq ($(DOXYGEN),)
5265 +DOXYGEN := doxygen
5266 +endif
5267 +
5268 +default:
5269 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
5270 +
5271 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
5272 + $(DOXYGEN) doc/doxygen.cfg
5273 +
5274 +tags: $(wildcard *.[hc])
5275 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
5276 +
5277 +endif
5278 +
5279 +clean:
5280 + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
5281 --- /dev/null
5282 +++ b/drivers/usb/host/dwc_common_port/changes.txt
5283 @@ -0,0 +1,174 @@
5284 +
5285 +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
5286 +IO context struct. The IO context struct should live in an os-dependent struct
5287 +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
5288 +named 'os_dep' embedded in the main device struct. So there these calls look
5289 +like this:
5290 +
5291 + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
5292 +
5293 + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
5294 + &pcd->dev_global_regs->dcfg, 0);
5295 +
5296 +Note that for the existing Linux driver ports, it is not necessary to actually
5297 +define the 'ioctx' member in the os-dependent struct. Since Linux does not
5298 +require an IO context, its macros for dwc_read_reg32() and friends do not
5299 +use the context pointer, so it is optimized away by the compiler. But it is
5300 +necessary to add the pointer parameter to all of the call sites, to be ready
5301 +for any future ports (such as FreeBSD) which do require an IO context.
5302 +
5303 +
5304 +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
5305 +take an additional parameter, a pointer to a memory context. Examples:
5306 +
5307 + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
5308 +
5309 + dwc_free(&usb3_dev->os_dep.memctx, addr);
5310 +
5311 +Again, for the Linux ports, it is not necessary to actually define the memctx
5312 +member, but it is necessary to add the pointer parameter to all of the call
5313 +sites.
5314 +
5315 +
5316 +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
5317 +
5318 + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
5319 +
5320 + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
5321 +
5322 +
5323 +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
5324 +
5325 + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
5326 +
5327 + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
5328 +
5329 +
5330 +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
5331 +
5332 + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
5333 +
5334 + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
5335 +
5336 +
5337 +Same for dwc_timer_alloc(). Example:
5338 +
5339 + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
5340 + cb_func, cb_data);
5341 +
5342 +
5343 +Same for dwc_waitq_alloc(). Example:
5344 +
5345 + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
5346 +
5347 +
5348 +Same for dwc_thread_run(). Example:
5349 +
5350 + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
5351 + "dwc_usb3_thd1", data);
5352 +
5353 +
5354 +Same for dwc_workq_alloc(). Example:
5355 +
5356 + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
5357 +
5358 +
5359 +Same for dwc_task_alloc(). Example:
5360 +
5361 + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
5362 + cb_func, cb_data);
5363 +
5364 +
5365 +In addition to the context pointer additions, a few core functions have had
5366 +other changes made to their parameters:
5367 +
5368 +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
5369 +has been changed from a uint64_t to a dwc_irqflags_t.
5370 +
5371 +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
5372 +FreeBSD equivalent of that function requires it.
5373 +
5374 +And, in addition to the context pointer, dwc_task_alloc() also adds a
5375 +'char *name' parameter, to be consistent with dwc_thread_run() and
5376 +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
5377 +requires a unique name.
5378 +
5379 +
5380 +Here is a complete list of the core functions that now take a pointer to a
5381 +context as their first parameter:
5382 +
5383 + dwc_read_reg32
5384 + dwc_read_reg64
5385 + dwc_write_reg32
5386 + dwc_write_reg64
5387 + dwc_modify_reg32
5388 + dwc_modify_reg64
5389 + dwc_alloc
5390 + dwc_alloc_atomic
5391 + dwc_strdup
5392 + dwc_free
5393 + dwc_dma_alloc
5394 + dwc_dma_free
5395 + dwc_mutex_alloc
5396 + dwc_mutex_free
5397 + dwc_spinlock_alloc
5398 + dwc_spinlock_free
5399 + dwc_timer_alloc
5400 + dwc_waitq_alloc
5401 + dwc_thread_run
5402 + dwc_workq_alloc
5403 + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
5404 +
5405 +And here are the core functions that have other changes to their parameters:
5406 +
5407 + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
5408 + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
5409 + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
5410 +
5411 +
5412 +
5413 +The changes to the core functions also require some of the other library
5414 +functions to change:
5415 +
5416 + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
5417 + (for memory allocation) as the 1st param and a 'void *mtxctx'
5418 + (for mutex allocation) as the 2nd param.
5419 +
5420 + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
5421 + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
5422 + 'void *memctx' as the 1st param.
5423 +
5424 + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
5425 + 'void *memctx' as the 1st param.
5426 +
5427 + dwc_modpow() now takes a 'void *memctx' as the 1st param.
5428 +
5429 + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
5430 + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
5431 + param, and also now returns an integer value that is non-zero if
5432 + allocation of its data structures or work queue fails.
5433 +
5434 + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
5435 +
5436 + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
5437 + param, and also now returns an integer value that is non-zero if
5438 + allocation of its data structures fails.
5439 +
5440 +
5441 +
5442 +Other miscellaneous changes:
5443 +
5444 +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
5445 +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
5446 +
5447 +The following #define's have been added to allow selectively compiling library
5448 +features:
5449 +
5450 + DWC_CCLIB
5451 + DWC_CRYPTOLIB
5452 + DWC_NOTIFYLIB
5453 + DWC_UTFLIB
5454 +
5455 +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
5456 +module code in dwc_common_linux.c is not compiled in. This allows linking the
5457 +library code directly into a driver module, instead of as a standalone module.
5458 --- /dev/null
5459 +++ b/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
5460 @@ -0,0 +1,270 @@
5461 +# Doxyfile 1.4.5
5462 +
5463 +#---------------------------------------------------------------------------
5464 +# Project related configuration options
5465 +#---------------------------------------------------------------------------
5466 +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
5467 +PROJECT_NUMBER =
5468 +OUTPUT_DIRECTORY = doc
5469 +CREATE_SUBDIRS = NO
5470 +OUTPUT_LANGUAGE = English
5471 +BRIEF_MEMBER_DESC = YES
5472 +REPEAT_BRIEF = YES
5473 +ABBREVIATE_BRIEF = "The $name class" \
5474 + "The $name widget" \
5475 + "The $name file" \
5476 + is \
5477 + provides \
5478 + specifies \
5479 + contains \
5480 + represents \
5481 + a \
5482 + an \
5483 + the
5484 +ALWAYS_DETAILED_SEC = YES
5485 +INLINE_INHERITED_MEMB = NO
5486 +FULL_PATH_NAMES = NO
5487 +STRIP_FROM_PATH = ..
5488 +STRIP_FROM_INC_PATH =
5489 +SHORT_NAMES = NO
5490 +JAVADOC_AUTOBRIEF = YES
5491 +MULTILINE_CPP_IS_BRIEF = NO
5492 +DETAILS_AT_TOP = YES
5493 +INHERIT_DOCS = YES
5494 +SEPARATE_MEMBER_PAGES = NO
5495 +TAB_SIZE = 8
5496 +ALIASES =
5497 +OPTIMIZE_OUTPUT_FOR_C = YES
5498 +OPTIMIZE_OUTPUT_JAVA = NO
5499 +BUILTIN_STL_SUPPORT = NO
5500 +DISTRIBUTE_GROUP_DOC = NO
5501 +SUBGROUPING = NO
5502 +#---------------------------------------------------------------------------
5503 +# Build related configuration options
5504 +#---------------------------------------------------------------------------
5505 +EXTRACT_ALL = NO
5506 +EXTRACT_PRIVATE = NO
5507 +EXTRACT_STATIC = YES
5508 +EXTRACT_LOCAL_CLASSES = NO
5509 +EXTRACT_LOCAL_METHODS = NO
5510 +HIDE_UNDOC_MEMBERS = NO
5511 +HIDE_UNDOC_CLASSES = NO
5512 +HIDE_FRIEND_COMPOUNDS = NO
5513 +HIDE_IN_BODY_DOCS = NO
5514 +INTERNAL_DOCS = NO
5515 +CASE_SENSE_NAMES = YES
5516 +HIDE_SCOPE_NAMES = NO
5517 +SHOW_INCLUDE_FILES = NO
5518 +INLINE_INFO = YES
5519 +SORT_MEMBER_DOCS = NO
5520 +SORT_BRIEF_DOCS = NO
5521 +SORT_BY_SCOPE_NAME = NO
5522 +GENERATE_TODOLIST = YES
5523 +GENERATE_TESTLIST = YES
5524 +GENERATE_BUGLIST = YES
5525 +GENERATE_DEPRECATEDLIST= YES
5526 +ENABLED_SECTIONS =
5527 +MAX_INITIALIZER_LINES = 30
5528 +SHOW_USED_FILES = YES
5529 +SHOW_DIRECTORIES = YES
5530 +FILE_VERSION_FILTER =
5531 +#---------------------------------------------------------------------------
5532 +# configuration options related to warning and progress messages
5533 +#---------------------------------------------------------------------------
5534 +QUIET = YES
5535 +WARNINGS = YES
5536 +WARN_IF_UNDOCUMENTED = NO
5537 +WARN_IF_DOC_ERROR = YES
5538 +WARN_NO_PARAMDOC = YES
5539 +WARN_FORMAT = "$file:$line: $text"
5540 +WARN_LOGFILE =
5541 +#---------------------------------------------------------------------------
5542 +# configuration options related to the input files
5543 +#---------------------------------------------------------------------------
5544 +INPUT = .
5545 +FILE_PATTERNS = *.c \
5546 + *.cc \
5547 + *.cxx \
5548 + *.cpp \
5549 + *.c++ \
5550 + *.d \
5551 + *.java \
5552 + *.ii \
5553 + *.ixx \
5554 + *.ipp \
5555 + *.i++ \
5556 + *.inl \
5557 + *.h \
5558 + *.hh \
5559 + *.hxx \
5560 + *.hpp \
5561 + *.h++ \
5562 + *.idl \
5563 + *.odl \
5564 + *.cs \
5565 + *.php \
5566 + *.php3 \
5567 + *.inc \
5568 + *.m \
5569 + *.mm \
5570 + *.dox \
5571 + *.py \
5572 + *.C \
5573 + *.CC \
5574 + *.C++ \
5575 + *.II \
5576 + *.I++ \
5577 + *.H \
5578 + *.HH \
5579 + *.H++ \
5580 + *.CS \
5581 + *.PHP \
5582 + *.PHP3 \
5583 + *.M \
5584 + *.MM \
5585 + *.PY
5586 +RECURSIVE = NO
5587 +EXCLUDE =
5588 +EXCLUDE_SYMLINKS = NO
5589 +EXCLUDE_PATTERNS =
5590 +EXAMPLE_PATH =
5591 +EXAMPLE_PATTERNS = *
5592 +EXAMPLE_RECURSIVE = NO
5593 +IMAGE_PATH =
5594 +INPUT_FILTER =
5595 +FILTER_PATTERNS =
5596 +FILTER_SOURCE_FILES = NO
5597 +#---------------------------------------------------------------------------
5598 +# configuration options related to source browsing
5599 +#---------------------------------------------------------------------------
5600 +SOURCE_BROWSER = NO
5601 +INLINE_SOURCES = NO
5602 +STRIP_CODE_COMMENTS = YES
5603 +REFERENCED_BY_RELATION = YES
5604 +REFERENCES_RELATION = YES
5605 +USE_HTAGS = NO
5606 +VERBATIM_HEADERS = NO
5607 +#---------------------------------------------------------------------------
5608 +# configuration options related to the alphabetical class index
5609 +#---------------------------------------------------------------------------
5610 +ALPHABETICAL_INDEX = NO
5611 +COLS_IN_ALPHA_INDEX = 5
5612 +IGNORE_PREFIX =
5613 +#---------------------------------------------------------------------------
5614 +# configuration options related to the HTML output
5615 +#---------------------------------------------------------------------------
5616 +GENERATE_HTML = YES
5617 +HTML_OUTPUT = html
5618 +HTML_FILE_EXTENSION = .html
5619 +HTML_HEADER =
5620 +HTML_FOOTER =
5621 +HTML_STYLESHEET =
5622 +HTML_ALIGN_MEMBERS = YES
5623 +GENERATE_HTMLHELP = NO
5624 +CHM_FILE =
5625 +HHC_LOCATION =
5626 +GENERATE_CHI = NO
5627 +BINARY_TOC = NO
5628 +TOC_EXPAND = NO
5629 +DISABLE_INDEX = NO
5630 +ENUM_VALUES_PER_LINE = 4
5631 +GENERATE_TREEVIEW = YES
5632 +TREEVIEW_WIDTH = 250
5633 +#---------------------------------------------------------------------------
5634 +# configuration options related to the LaTeX output
5635 +#---------------------------------------------------------------------------
5636 +GENERATE_LATEX = NO
5637 +LATEX_OUTPUT = latex
5638 +LATEX_CMD_NAME = latex
5639 +MAKEINDEX_CMD_NAME = makeindex
5640 +COMPACT_LATEX = NO
5641 +PAPER_TYPE = a4wide
5642 +EXTRA_PACKAGES =
5643 +LATEX_HEADER =
5644 +PDF_HYPERLINKS = NO
5645 +USE_PDFLATEX = NO
5646 +LATEX_BATCHMODE = NO
5647 +LATEX_HIDE_INDICES = NO
5648 +#---------------------------------------------------------------------------
5649 +# configuration options related to the RTF output
5650 +#---------------------------------------------------------------------------
5651 +GENERATE_RTF = NO
5652 +RTF_OUTPUT = rtf
5653 +COMPACT_RTF = NO
5654 +RTF_HYPERLINKS = NO
5655 +RTF_STYLESHEET_FILE =
5656 +RTF_EXTENSIONS_FILE =
5657 +#---------------------------------------------------------------------------
5658 +# configuration options related to the man page output
5659 +#---------------------------------------------------------------------------
5660 +GENERATE_MAN = NO
5661 +MAN_OUTPUT = man
5662 +MAN_EXTENSION = .3
5663 +MAN_LINKS = NO
5664 +#---------------------------------------------------------------------------
5665 +# configuration options related to the XML output
5666 +#---------------------------------------------------------------------------
5667 +GENERATE_XML = NO
5668 +XML_OUTPUT = xml
5669 +XML_SCHEMA =
5670 +XML_DTD =
5671 +XML_PROGRAMLISTING = YES
5672 +#---------------------------------------------------------------------------
5673 +# configuration options for the AutoGen Definitions output
5674 +#---------------------------------------------------------------------------
5675 +GENERATE_AUTOGEN_DEF = NO
5676 +#---------------------------------------------------------------------------
5677 +# configuration options related to the Perl module output
5678 +#---------------------------------------------------------------------------
5679 +GENERATE_PERLMOD = NO
5680 +PERLMOD_LATEX = NO
5681 +PERLMOD_PRETTY = YES
5682 +PERLMOD_MAKEVAR_PREFIX =
5683 +#---------------------------------------------------------------------------
5684 +# Configuration options related to the preprocessor
5685 +#---------------------------------------------------------------------------
5686 +ENABLE_PREPROCESSING = YES
5687 +MACRO_EXPANSION = NO
5688 +EXPAND_ONLY_PREDEF = NO
5689 +SEARCH_INCLUDES = YES
5690 +INCLUDE_PATH =
5691 +INCLUDE_FILE_PATTERNS =
5692 +PREDEFINED = DEBUG DEBUG_MEMORY
5693 +EXPAND_AS_DEFINED =
5694 +SKIP_FUNCTION_MACROS = YES
5695 +#---------------------------------------------------------------------------
5696 +# Configuration::additions related to external references
5697 +#---------------------------------------------------------------------------
5698 +TAGFILES =
5699 +GENERATE_TAGFILE =
5700 +ALLEXTERNALS = NO
5701 +EXTERNAL_GROUPS = YES
5702 +PERL_PATH = /usr/bin/perl
5703 +#---------------------------------------------------------------------------
5704 +# Configuration options related to the dot tool
5705 +#---------------------------------------------------------------------------
5706 +CLASS_DIAGRAMS = YES
5707 +HIDE_UNDOC_RELATIONS = YES
5708 +HAVE_DOT = NO
5709 +CLASS_GRAPH = YES
5710 +COLLABORATION_GRAPH = YES
5711 +GROUP_GRAPHS = YES
5712 +UML_LOOK = NO
5713 +TEMPLATE_RELATIONS = NO
5714 +INCLUDE_GRAPH = NO
5715 +INCLUDED_BY_GRAPH = YES
5716 +CALL_GRAPH = NO
5717 +GRAPHICAL_HIERARCHY = YES
5718 +DIRECTORY_GRAPH = YES
5719 +DOT_IMAGE_FORMAT = png
5720 +DOT_PATH =
5721 +DOTFILE_DIRS =
5722 +MAX_DOT_GRAPH_DEPTH = 1000
5723 +DOT_TRANSPARENT = NO
5724 +DOT_MULTI_TARGETS = NO
5725 +GENERATE_LEGEND = YES
5726 +DOT_CLEANUP = YES
5727 +#---------------------------------------------------------------------------
5728 +# Configuration::additions related to the search engine
5729 +#---------------------------------------------------------------------------
5730 +SEARCHENGINE = NO
5731 --- /dev/null
5732 +++ b/drivers/usb/host/dwc_common_port/dwc_cc.c
5733 @@ -0,0 +1,532 @@
5734 +/* =========================================================================
5735 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
5736 + * $Revision: #4 $
5737 + * $Date: 2010/11/04 $
5738 + * $Change: 1621692 $
5739 + *
5740 + * Synopsys Portability Library Software and documentation
5741 + * (hereinafter, "Software") is an Unsupported proprietary work of
5742 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
5743 + * between Synopsys and you.
5744 + *
5745 + * The Software IS NOT an item of Licensed Software or Licensed Product
5746 + * under any End User Software License Agreement or Agreement for
5747 + * Licensed Product with Synopsys or any supplement thereto. You are
5748 + * permitted to use and redistribute this Software in source and binary
5749 + * forms, with or without modification, provided that redistributions
5750 + * of source code must retain this notice. You may not view, use,
5751 + * disclose, copy or distribute this file or any information contained
5752 + * herein except pursuant to this license grant from Synopsys. If you
5753 + * do not agree with this notice, including the disclaimer below, then
5754 + * you are not authorized to use the Software.
5755 + *
5756 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
5757 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5758 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
5759 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
5760 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5761 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5762 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5763 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
5764 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5765 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
5766 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
5767 + * DAMAGE.
5768 + * ========================================================================= */
5769 +#ifdef DWC_CCLIB
5770 +
5771 +#include "dwc_cc.h"
5772 +
5773 +typedef struct dwc_cc
5774 +{
5775 + uint32_t uid;
5776 + uint8_t chid[16];
5777 + uint8_t cdid[16];
5778 + uint8_t ck[16];
5779 + uint8_t *name;
5780 + uint8_t length;
5781 + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
5782 +} dwc_cc_t;
5783 +
5784 +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
5785 +
5786 +/** The main structure for CC management. */
5787 +struct dwc_cc_if
5788 +{
5789 + dwc_mutex_t *mutex;
5790 + char *filename;
5791 +
5792 + unsigned is_host:1;
5793 +
5794 + dwc_notifier_t *notifier;
5795 +
5796 + struct context_list list;
5797 +};
5798 +
5799 +#ifdef DEBUG
5800 +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
5801 +{
5802 + int i;
5803 + DWC_PRINTF("%s: ", name);
5804 + for (i=0; i<len; i++) {
5805 + DWC_PRINTF("%02x ", bytes[i]);
5806 + }
5807 + DWC_PRINTF("\n");
5808 +}
5809 +#else
5810 +#define dump_bytes(x...)
5811 +#endif
5812 +
5813 +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
5814 +{
5815 + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
5816 + if (!cc) {
5817 + return NULL;
5818 + }
5819 + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
5820 +
5821 + if (name) {
5822 + cc->length = length;
5823 + cc->name = dwc_alloc(mem_ctx, length);
5824 + if (!cc->name) {
5825 + dwc_free(mem_ctx, cc);
5826 + return NULL;
5827 + }
5828 +
5829 + DWC_MEMCPY(cc->name, name, length);
5830 + }
5831 +
5832 + return cc;
5833 +}
5834 +
5835 +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
5836 +{
5837 + if (cc->name) {
5838 + dwc_free(mem_ctx, cc->name);
5839 + }
5840 + dwc_free(mem_ctx, cc);
5841 +}
5842 +
5843 +static uint32_t next_uid(dwc_cc_if_t *cc_if)
5844 +{
5845 + uint32_t uid = 0;
5846 + dwc_cc_t *cc;
5847 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5848 + if (cc->uid > uid) {
5849 + uid = cc->uid;
5850 + }
5851 + }
5852 +
5853 + if (uid == 0) {
5854 + uid = 255;
5855 + }
5856 +
5857 + return uid + 1;
5858 +}
5859 +
5860 +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
5861 +{
5862 + dwc_cc_t *cc;
5863 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5864 + if (cc->uid == uid) {
5865 + return cc;
5866 + }
5867 + }
5868 + return NULL;
5869 +}
5870 +
5871 +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
5872 +{
5873 + unsigned int size = 0;
5874 + dwc_cc_t *cc;
5875 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5876 + size += (48 + 1);
5877 + if (cc->name) {
5878 + size += cc->length;
5879 + }
5880 + }
5881 + return size;
5882 +}
5883 +
5884 +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
5885 +{
5886 + uint32_t uid = 0;
5887 + dwc_cc_t *cc;
5888 +
5889 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5890 + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
5891 + uid = cc->uid;
5892 + break;
5893 + }
5894 + }
5895 + return uid;
5896 +}
5897 +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
5898 +{
5899 + uint32_t uid = 0;
5900 + dwc_cc_t *cc;
5901 +
5902 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5903 + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
5904 + uid = cc->uid;
5905 + break;
5906 + }
5907 + }
5908 + return uid;
5909 +}
5910 +
5911 +/* Internal cc_add */
5912 +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
5913 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5914 +{
5915 + dwc_cc_t *cc;
5916 + uint32_t uid;
5917 +
5918 + if (cc_if->is_host) {
5919 + uid = cc_match_cdid(cc_if, cdid);
5920 + }
5921 + else {
5922 + uid = cc_match_chid(cc_if, chid);
5923 + }
5924 +
5925 + if (uid) {
5926 + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
5927 + cc = cc_find(cc_if, uid);
5928 + }
5929 + else {
5930 + cc = alloc_cc(mem_ctx, name, length);
5931 + cc->uid = next_uid(cc_if);
5932 + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
5933 + }
5934 +
5935 + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
5936 + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
5937 + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
5938 +
5939 + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
5940 + dump_bytes("CHID", cc->chid, 16);
5941 + dump_bytes("CDID", cc->cdid, 16);
5942 + dump_bytes("CK", cc->ck, 16);
5943 + return cc->uid;
5944 +}
5945 +
5946 +/* Internal cc_clear */
5947 +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
5948 +{
5949 + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
5950 + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
5951 + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
5952 + free_cc(mem_ctx, cc);
5953 + }
5954 +}
5955 +
5956 +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
5957 + dwc_notifier_t *notifier, unsigned is_host)
5958 +{
5959 + dwc_cc_if_t *cc_if = NULL;
5960 +
5961 + /* Allocate a common_cc_if structure */
5962 + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
5963 +
5964 + if (!cc_if)
5965 + return NULL;
5966 +
5967 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
5968 + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
5969 +#else
5970 + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
5971 +#endif
5972 + if (!cc_if->mutex) {
5973 + dwc_free(mem_ctx, cc_if);
5974 + return NULL;
5975 + }
5976 +
5977 + DWC_CIRCLEQ_INIT(&cc_if->list);
5978 + cc_if->is_host = is_host;
5979 + cc_if->notifier = notifier;
5980 + return cc_if;
5981 +}
5982 +
5983 +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
5984 +{
5985 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
5986 + DWC_MUTEX_FREE(cc_if->mutex);
5987 +#else
5988 + dwc_mutex_free(mtx_ctx, cc_if->mutex);
5989 +#endif
5990 + cc_clear(mem_ctx, cc_if);
5991 + dwc_free(mem_ctx, cc_if);
5992 +}
5993 +
5994 +static void cc_changed(dwc_cc_if_t *cc_if)
5995 +{
5996 + if (cc_if->notifier) {
5997 + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
5998 + }
5999 +}
6000 +
6001 +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
6002 +{
6003 + DWC_MUTEX_LOCK(cc_if->mutex);
6004 + cc_clear(mem_ctx, cc_if);
6005 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6006 + cc_changed(cc_if);
6007 +}
6008 +
6009 +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
6010 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
6011 +{
6012 + uint32_t uid;
6013 +
6014 + DWC_MUTEX_LOCK(cc_if->mutex);
6015 + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
6016 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6017 + cc_changed(cc_if);
6018 +
6019 + return uid;
6020 +}
6021 +
6022 +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
6023 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
6024 +{
6025 + dwc_cc_t* cc;
6026 +
6027 + DWC_DEBUGC("Change connection context %d", id);
6028 +
6029 + DWC_MUTEX_LOCK(cc_if->mutex);
6030 + cc = cc_find(cc_if, id);
6031 + if (!cc) {
6032 + DWC_ERROR("Uid %d not found in cc list\n", id);
6033 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6034 + return;
6035 + }
6036 +
6037 + if (chid) {
6038 + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
6039 + }
6040 + if (cdid) {
6041 + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
6042 + }
6043 + if (ck) {
6044 + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
6045 + }
6046 +
6047 + if (name) {
6048 + if (cc->name) {
6049 + dwc_free(mem_ctx, cc->name);
6050 + }
6051 + cc->name = dwc_alloc(mem_ctx, length);
6052 + if (!cc->name) {
6053 + DWC_ERROR("Out of memory in dwc_cc_change()\n");
6054 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6055 + return;
6056 + }
6057 + cc->length = length;
6058 + DWC_MEMCPY(cc->name, name, length);
6059 + }
6060 +
6061 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6062 +
6063 + cc_changed(cc_if);
6064 +
6065 + DWC_DEBUGC("Changed connection context id=%d\n", id);
6066 + dump_bytes("New CHID", cc->chid, 16);
6067 + dump_bytes("New CDID", cc->cdid, 16);
6068 + dump_bytes("New CK", cc->ck, 16);
6069 +}
6070 +
6071 +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
6072 +{
6073 + dwc_cc_t *cc;
6074 +
6075 + DWC_DEBUGC("Removing connection context %d", id);
6076 +
6077 + DWC_MUTEX_LOCK(cc_if->mutex);
6078 + cc = cc_find(cc_if, id);
6079 + if (!cc) {
6080 + DWC_ERROR("Uid %d not found in cc list\n", id);
6081 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6082 + return;
6083 + }
6084 +
6085 + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
6086 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6087 + free_cc(mem_ctx, cc);
6088 +
6089 + cc_changed(cc_if);
6090 +}
6091 +
6092 +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
6093 +{
6094 + uint8_t *buf, *x;
6095 + uint8_t zero = 0;
6096 + dwc_cc_t *cc;
6097 +
6098 + DWC_MUTEX_LOCK(cc_if->mutex);
6099 + *length = cc_data_size(cc_if);
6100 + if (!(*length)) {
6101 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6102 + return NULL;
6103 + }
6104 +
6105 + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
6106 +
6107 + buf = dwc_alloc(mem_ctx, *length);
6108 + if (!buf) {
6109 + *length = 0;
6110 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6111 + return NULL;
6112 + }
6113 +
6114 + x = buf;
6115 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
6116 + DWC_MEMCPY(x, cc->chid, 16);
6117 + x += 16;
6118 + DWC_MEMCPY(x, cc->cdid, 16);
6119 + x += 16;
6120 + DWC_MEMCPY(x, cc->ck, 16);
6121 + x += 16;
6122 + if (cc->name) {
6123 + DWC_MEMCPY(x, &cc->length, 1);
6124 + x += 1;
6125 + DWC_MEMCPY(x, cc->name, cc->length);
6126 + x += cc->length;
6127 + }
6128 + else {
6129 + DWC_MEMCPY(x, &zero, 1);
6130 + x += 1;
6131 + }
6132 + }
6133 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6134 +
6135 + return buf;
6136 +}
6137 +
6138 +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
6139 +{
6140 + uint8_t name_length;
6141 + uint8_t *name;
6142 + uint8_t *chid;
6143 + uint8_t *cdid;
6144 + uint8_t *ck;
6145 + uint32_t i = 0;
6146 +
6147 + DWC_MUTEX_LOCK(cc_if->mutex);
6148 + cc_clear(mem_ctx, cc_if);
6149 +
6150 + while (i < length) {
6151 + chid = &data[i];
6152 + i += 16;
6153 + cdid = &data[i];
6154 + i += 16;
6155 + ck = &data[i];
6156 + i += 16;
6157 +
6158 + name_length = data[i];
6159 + i ++;
6160 +
6161 + if (name_length) {
6162 + name = &data[i];
6163 + i += name_length;
6164 + }
6165 + else {
6166 + name = NULL;
6167 + }
6168 +
6169 + /* check to see if we haven't overflown the buffer */
6170 + if (i > length) {
6171 + DWC_ERROR("Data format error while attempting to load CCs "
6172 + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
6173 + break;
6174 + }
6175 +
6176 + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
6177 + }
6178 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6179 +
6180 + cc_changed(cc_if);
6181 +}
6182 +
6183 +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
6184 +{
6185 + uint32_t uid = 0;
6186 +
6187 + DWC_MUTEX_LOCK(cc_if->mutex);
6188 + uid = cc_match_chid(cc_if, chid);
6189 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6190 + return uid;
6191 +}
6192 +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
6193 +{
6194 + uint32_t uid = 0;
6195 +
6196 + DWC_MUTEX_LOCK(cc_if->mutex);
6197 + uid = cc_match_cdid(cc_if, cdid);
6198 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6199 + return uid;
6200 +}
6201 +
6202 +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
6203 +{
6204 + uint8_t *ck = NULL;
6205 + dwc_cc_t *cc;
6206 +
6207 + DWC_MUTEX_LOCK(cc_if->mutex);
6208 + cc = cc_find(cc_if, id);
6209 + if (cc) {
6210 + ck = cc->ck;
6211 + }
6212 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6213 +
6214 + return ck;
6215 +
6216 +}
6217 +
6218 +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
6219 +{
6220 + uint8_t *retval = NULL;
6221 + dwc_cc_t *cc;
6222 +
6223 + DWC_MUTEX_LOCK(cc_if->mutex);
6224 + cc = cc_find(cc_if, id);
6225 + if (cc) {
6226 + retval = cc->chid;
6227 + }
6228 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6229 +
6230 + return retval;
6231 +}
6232 +
6233 +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
6234 +{
6235 + uint8_t *retval = NULL;
6236 + dwc_cc_t *cc;
6237 +
6238 + DWC_MUTEX_LOCK(cc_if->mutex);
6239 + cc = cc_find(cc_if, id);
6240 + if (cc) {
6241 + retval = cc->cdid;
6242 + }
6243 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6244 +
6245 + return retval;
6246 +}
6247 +
6248 +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
6249 +{
6250 + uint8_t *retval = NULL;
6251 + dwc_cc_t *cc;
6252 +
6253 + DWC_MUTEX_LOCK(cc_if->mutex);
6254 + *length = 0;
6255 + cc = cc_find(cc_if, id);
6256 + if (cc) {
6257 + *length = cc->length;
6258 + retval = cc->name;
6259 + }
6260 + DWC_MUTEX_UNLOCK(cc_if->mutex);
6261 +
6262 + return retval;
6263 +}
6264 +
6265 +#endif /* DWC_CCLIB */
6266 --- /dev/null
6267 +++ b/drivers/usb/host/dwc_common_port/dwc_cc.h
6268 @@ -0,0 +1,224 @@
6269 +/* =========================================================================
6270 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
6271 + * $Revision: #4 $
6272 + * $Date: 2010/09/28 $
6273 + * $Change: 1596182 $
6274 + *
6275 + * Synopsys Portability Library Software and documentation
6276 + * (hereinafter, "Software") is an Unsupported proprietary work of
6277 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
6278 + * between Synopsys and you.
6279 + *
6280 + * The Software IS NOT an item of Licensed Software or Licensed Product
6281 + * under any End User Software License Agreement or Agreement for
6282 + * Licensed Product with Synopsys or any supplement thereto. You are
6283 + * permitted to use and redistribute this Software in source and binary
6284 + * forms, with or without modification, provided that redistributions
6285 + * of source code must retain this notice. You may not view, use,
6286 + * disclose, copy or distribute this file or any information contained
6287 + * herein except pursuant to this license grant from Synopsys. If you
6288 + * do not agree with this notice, including the disclaimer below, then
6289 + * you are not authorized to use the Software.
6290 + *
6291 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
6292 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6293 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
6294 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
6295 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
6296 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
6297 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
6298 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
6299 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6300 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
6301 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
6302 + * DAMAGE.
6303 + * ========================================================================= */
6304 +#ifndef _DWC_CC_H_
6305 +#define _DWC_CC_H_
6306 +
6307 +#ifdef __cplusplus
6308 +extern "C" {
6309 +#endif
6310 +
6311 +/** @file
6312 + *
6313 + * This file defines the Context Context library.
6314 + *
6315 + * The main data structure is dwc_cc_if_t which is returned by either the
6316 + * dwc_cc_if_alloc function or returned by the module to the user via a provided
6317 + * function. The data structure is opaque and should only be manipulated via the
6318 + * functions provied in this API.
6319 + *
6320 + * It manages a list of connection contexts and operations can be performed to
6321 + * add, remove, query, search, and change, those contexts. Additionally,
6322 + * a dwc_notifier_t object can be requested from the manager so that
6323 + * the user can be notified whenever the context list has changed.
6324 + */
6325 +
6326 +#include "dwc_os.h"
6327 +#include "dwc_list.h"
6328 +#include "dwc_notifier.h"
6329 +
6330 +
6331 +/* Notifications */
6332 +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
6333 +
6334 +struct dwc_cc_if;
6335 +typedef struct dwc_cc_if dwc_cc_if_t;
6336 +
6337 +
6338 +/** @name Connection Context Operations */
6339 +/** @{ */
6340 +
6341 +/** This function allocates memory for a dwc_cc_if_t structure, initializes
6342 + * fields to default values, and returns a pointer to the structure or NULL on
6343 + * error. */
6344 +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
6345 + dwc_notifier_t *notifier, unsigned is_host);
6346 +
6347 +/** Frees the memory for the specified CC structure allocated from
6348 + * dwc_cc_if_alloc(). */
6349 +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
6350 +
6351 +/** Removes all contexts from the connection context list */
6352 +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
6353 +
6354 +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
6355 + * If a CHID already exists, the CK and name are overwritten. Statistics are
6356 + * not overwritten.
6357 + *
6358 + * @param cc_if The cc_if structure.
6359 + * @param chid A pointer to the 16-byte CHID. This value will be copied.
6360 + * @param ck A pointer to the 16-byte CK. This value will be copied.
6361 + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
6362 + * @param name An optional host friendly name as defined in the association model
6363 + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
6364 + * @param length The length othe unicode string.
6365 + * @return A unique identifier used to refer to this context that is valid for
6366 + * as long as this context is still in the list. */
6367 +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
6368 + uint8_t *cdid, uint8_t *ck, uint8_t *name,
6369 + uint8_t length);
6370 +
6371 +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
6372 + * list, preserving any accumulated statistics. This would typically be called
6373 + * if the host decideds to change the context with a SET_CONNECTION request.
6374 + *
6375 + * @param cc_if The cc_if structure.
6376 + * @param id The identifier of the connection context.
6377 + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
6378 + * indicates no change.
6379 + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
6380 + * indicates no change.
6381 + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
6382 + * indicates no change.
6383 + * @param name Host friendly name UTF16-LE. NULL indicates no change.
6384 + * @param length Length of name. */
6385 +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
6386 + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
6387 + uint8_t *name, uint8_t length);
6388 +
6389 +/** Remove the specified connection context.
6390 + * @param cc_if The cc_if structure.
6391 + * @param id The identifier of the connection context to remove. */
6392 +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
6393 +
6394 +/** Get a binary block of data for the connection context list and attributes.
6395 + * This data can be used by the OS specific driver to save the connection
6396 + * context list into non-volatile memory.
6397 + *
6398 + * @param cc_if The cc_if structure.
6399 + * @param length Return the length of the data buffer.
6400 + * @return A pointer to the data buffer. The memory for this buffer should be
6401 + * freed with DWC_FREE() after use. */
6402 +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
6403 + unsigned int *length);
6404 +
6405 +/** Restore the connection context list from the binary data that was previously
6406 + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
6407 + * driver to load a connection context list from non-volatile memory.
6408 + *
6409 + * @param cc_if The cc_if structure.
6410 + * @param data The data bytes as returned from dwc_cc_data_for_save.
6411 + * @param length The length of the data. */
6412 +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
6413 + uint8_t *data, unsigned int length);
6414 +
6415 +/** Find the connection context from the specified CHID.
6416 + *
6417 + * @param cc_if The cc_if structure.
6418 + * @param chid A pointer to the CHID data.
6419 + * @return A non-zero identifier of the connection context if the CHID matches.
6420 + * Otherwise returns 0. */
6421 +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
6422 +
6423 +/** Find the connection context from the specified CDID.
6424 + *
6425 + * @param cc_if The cc_if structure.
6426 + * @param cdid A pointer to the CDID data.
6427 + * @return A non-zero identifier of the connection context if the CHID matches.
6428 + * Otherwise returns 0. */
6429 +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
6430 +
6431 +/** Retrieve the CK from the specified connection context.
6432 + *
6433 + * @param cc_if The cc_if structure.
6434 + * @param id The identifier of the connection context.
6435 + * @return A pointer to the CK data. The memory does not need to be freed. */
6436 +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
6437 +
6438 +/** Retrieve the CHID from the specified connection context.
6439 + *
6440 + * @param cc_if The cc_if structure.
6441 + * @param id The identifier of the connection context.
6442 + * @return A pointer to the CHID data. The memory does not need to be freed. */
6443 +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
6444 +
6445 +/** Retrieve the CDID from the specified connection context.
6446 + *
6447 + * @param cc_if The cc_if structure.
6448 + * @param id The identifier of the connection context.
6449 + * @return A pointer to the CDID data. The memory does not need to be freed. */
6450 +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
6451 +
6452 +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
6453 +
6454 +/** Checks a buffer for non-zero.
6455 + * @param id A pointer to a 16 byte buffer.
6456 + * @return true if the 16 byte value is non-zero. */
6457 +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
6458 + int i;
6459 + for (i=0; i<16; i++) {
6460 + if (id[i]) return 1;
6461 + }
6462 + return 0;
6463 +}
6464 +
6465 +/** Checks a buffer for zero.
6466 + * @param id A pointer to a 16 byte buffer.
6467 + * @return true if the 16 byte value is zero. */
6468 +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
6469 + return !dwc_assoc_is_not_zero_id(id);
6470 +}
6471 +
6472 +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
6473 + * buffer. */
6474 +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
6475 + char *ptr = buffer;
6476 + int i;
6477 + for (i=0; i<16; i++) {
6478 + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
6479 + if (i < 15) {
6480 + ptr += DWC_SPRINTF(ptr, " ");
6481 + }
6482 + }
6483 + return ptr - buffer;
6484 +}
6485 +
6486 +/** @} */
6487 +
6488 +#ifdef __cplusplus
6489 +}
6490 +#endif
6491 +
6492 +#endif /* _DWC_CC_H_ */
6493 --- /dev/null
6494 +++ b/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
6495 @@ -0,0 +1,1308 @@
6496 +#include "dwc_os.h"
6497 +#include "dwc_list.h"
6498 +
6499 +#ifdef DWC_CCLIB
6500 +# include "dwc_cc.h"
6501 +#endif
6502 +
6503 +#ifdef DWC_CRYPTOLIB
6504 +# include "dwc_modpow.h"
6505 +# include "dwc_dh.h"
6506 +# include "dwc_crypto.h"
6507 +#endif
6508 +
6509 +#ifdef DWC_NOTIFYLIB
6510 +# include "dwc_notifier.h"
6511 +#endif
6512 +
6513 +/* OS-Level Implementations */
6514 +
6515 +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
6516 +
6517 +
6518 +/* MISC */
6519 +
6520 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
6521 +{
6522 + return memset(dest, byte, size);
6523 +}
6524 +
6525 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
6526 +{
6527 + return memcpy(dest, src, size);
6528 +}
6529 +
6530 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
6531 +{
6532 + bcopy(src, dest, size);
6533 + return dest;
6534 +}
6535 +
6536 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
6537 +{
6538 + return memcmp(m1, m2, size);
6539 +}
6540 +
6541 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
6542 +{
6543 + return strncmp(s1, s2, size);
6544 +}
6545 +
6546 +int DWC_STRCMP(void *s1, void *s2)
6547 +{
6548 + return strcmp(s1, s2);
6549 +}
6550 +
6551 +int DWC_STRLEN(char const *str)
6552 +{
6553 + return strlen(str);
6554 +}
6555 +
6556 +char *DWC_STRCPY(char *to, char const *from)
6557 +{
6558 + return strcpy(to, from);
6559 +}
6560 +
6561 +char *DWC_STRDUP(char const *str)
6562 +{
6563 + int len = DWC_STRLEN(str) + 1;
6564 + char *new = DWC_ALLOC_ATOMIC(len);
6565 +
6566 + if (!new) {
6567 + return NULL;
6568 + }
6569 +
6570 + DWC_MEMCPY(new, str, len);
6571 + return new;
6572 +}
6573 +
6574 +int DWC_ATOI(char *str, int32_t *value)
6575 +{
6576 + char *end = NULL;
6577 +
6578 + *value = strtol(str, &end, 0);
6579 + if (*end == '\0') {
6580 + return 0;
6581 + }
6582 +
6583 + return -1;
6584 +}
6585 +
6586 +int DWC_ATOUI(char *str, uint32_t *value)
6587 +{
6588 + char *end = NULL;
6589 +
6590 + *value = strtoul(str, &end, 0);
6591 + if (*end == '\0') {
6592 + return 0;
6593 + }
6594 +
6595 + return -1;
6596 +}
6597 +
6598 +
6599 +#ifdef DWC_UTFLIB
6600 +/* From usbstring.c */
6601 +
6602 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
6603 +{
6604 + int count = 0;
6605 + u8 c;
6606 + u16 uchar;
6607 +
6608 + /* this insists on correct encodings, though not minimal ones.
6609 + * BUT it currently rejects legit 4-byte UTF-8 code points,
6610 + * which need surrogate pairs. (Unicode 3.1 can use them.)
6611 + */
6612 + while (len != 0 && (c = (u8) *s++) != 0) {
6613 + if (unlikely(c & 0x80)) {
6614 + // 2-byte sequence:
6615 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
6616 + if ((c & 0xe0) == 0xc0) {
6617 + uchar = (c & 0x1f) << 6;
6618 +
6619 + c = (u8) *s++;
6620 + if ((c & 0xc0) != 0xc0)
6621 + goto fail;
6622 + c &= 0x3f;
6623 + uchar |= c;
6624 +
6625 + // 3-byte sequence (most CJKV characters):
6626 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
6627 + } else if ((c & 0xf0) == 0xe0) {
6628 + uchar = (c & 0x0f) << 12;
6629 +
6630 + c = (u8) *s++;
6631 + if ((c & 0xc0) != 0xc0)
6632 + goto fail;
6633 + c &= 0x3f;
6634 + uchar |= c << 6;
6635 +
6636 + c = (u8) *s++;
6637 + if ((c & 0xc0) != 0xc0)
6638 + goto fail;
6639 + c &= 0x3f;
6640 + uchar |= c;
6641 +
6642 + /* no bogus surrogates */
6643 + if (0xd800 <= uchar && uchar <= 0xdfff)
6644 + goto fail;
6645 +
6646 + // 4-byte sequence (surrogate pairs, currently rare):
6647 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
6648 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
6649 + // (uuuuu = wwww + 1)
6650 + // FIXME accept the surrogate code points (only)
6651 + } else
6652 + goto fail;
6653 + } else
6654 + uchar = c;
6655 + put_unaligned (cpu_to_le16 (uchar), cp++);
6656 + count++;
6657 + len--;
6658 + }
6659 + return count;
6660 +fail:
6661 + return -1;
6662 +}
6663 +
6664 +#endif /* DWC_UTFLIB */
6665 +
6666 +
6667 +/* dwc_debug.h */
6668 +
6669 +dwc_bool_t DWC_IN_IRQ(void)
6670 +{
6671 +// return in_irq();
6672 + return 0;
6673 +}
6674 +
6675 +dwc_bool_t DWC_IN_BH(void)
6676 +{
6677 +// return in_softirq();
6678 + return 0;
6679 +}
6680 +
6681 +void DWC_VPRINTF(char *format, va_list args)
6682 +{
6683 + vprintf(format, args);
6684 +}
6685 +
6686 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
6687 +{
6688 + return vsnprintf(str, size, format, args);
6689 +}
6690 +
6691 +void DWC_PRINTF(char *format, ...)
6692 +{
6693 + va_list args;
6694 +
6695 + va_start(args, format);
6696 + DWC_VPRINTF(format, args);
6697 + va_end(args);
6698 +}
6699 +
6700 +int DWC_SPRINTF(char *buffer, char *format, ...)
6701 +{
6702 + int retval;
6703 + va_list args;
6704 +
6705 + va_start(args, format);
6706 + retval = vsprintf(buffer, format, args);
6707 + va_end(args);
6708 + return retval;
6709 +}
6710 +
6711 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
6712 +{
6713 + int retval;
6714 + va_list args;
6715 +
6716 + va_start(args, format);
6717 + retval = vsnprintf(buffer, size, format, args);
6718 + va_end(args);
6719 + return retval;
6720 +}
6721 +
6722 +void __DWC_WARN(char *format, ...)
6723 +{
6724 + va_list args;
6725 +
6726 + va_start(args, format);
6727 + DWC_VPRINTF(format, args);
6728 + va_end(args);
6729 +}
6730 +
6731 +void __DWC_ERROR(char *format, ...)
6732 +{
6733 + va_list args;
6734 +
6735 + va_start(args, format);
6736 + DWC_VPRINTF(format, args);
6737 + va_end(args);
6738 +}
6739 +
6740 +void DWC_EXCEPTION(char *format, ...)
6741 +{
6742 + va_list args;
6743 +
6744 + va_start(args, format);
6745 + DWC_VPRINTF(format, args);
6746 + va_end(args);
6747 +// BUG_ON(1); ???
6748 +}
6749 +
6750 +#ifdef DEBUG
6751 +void __DWC_DEBUG(char *format, ...)
6752 +{
6753 + va_list args;
6754 +
6755 + va_start(args, format);
6756 + DWC_VPRINTF(format, args);
6757 + va_end(args);
6758 +}
6759 +#endif
6760 +
6761 +
6762 +/* dwc_mem.h */
6763 +
6764 +#if 0
6765 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
6766 + uint32_t align,
6767 + uint32_t alloc)
6768 +{
6769 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
6770 + size, align, alloc);
6771 + return (dwc_pool_t *)pool;
6772 +}
6773 +
6774 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
6775 +{
6776 + dma_pool_destroy((struct dma_pool *)pool);
6777 +}
6778 +
6779 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
6780 +{
6781 +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
6782 + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
6783 +}
6784 +
6785 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
6786 +{
6787 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
6788 + memset(..);
6789 +}
6790 +
6791 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
6792 +{
6793 + dma_pool_free(pool, vaddr, daddr);
6794 +}
6795 +#endif
6796 +
6797 +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
6798 +{
6799 + if (error)
6800 + return;
6801 + *(bus_addr_t *)arg = segs[0].ds_addr;
6802 +}
6803 +
6804 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
6805 +{
6806 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
6807 + int error;
6808 +
6809 + error = bus_dma_tag_create(
6810 +#if __FreeBSD_version >= 700000
6811 + bus_get_dma_tag(dma->dev), /* parent */
6812 +#else
6813 + NULL, /* parent */
6814 +#endif
6815 + 4, 0, /* alignment, bounds */
6816 + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
6817 + BUS_SPACE_MAXADDR, /* highaddr */
6818 + NULL, NULL, /* filter, filterarg */
6819 + size, /* maxsize */
6820 + 1, /* nsegments */
6821 + size, /* maxsegsize */
6822 + 0, /* flags */
6823 + NULL, /* lockfunc */
6824 + NULL, /* lockarg */
6825 + &dma->dma_tag);
6826 + if (error) {
6827 + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
6828 + __func__, error);
6829 + goto fail_0;
6830 + }
6831 +
6832 + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
6833 + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
6834 + if (error) {
6835 + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
6836 + __func__, (uintmax_t)size, error);
6837 + goto fail_1;
6838 + }
6839 +
6840 + dma->dma_paddr = 0;
6841 + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
6842 + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
6843 + if (error || dma->dma_paddr == 0) {
6844 + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
6845 + __func__, error);
6846 + goto fail_2;
6847 + }
6848 +
6849 + *dma_addr = dma->dma_paddr;
6850 + return dma->dma_vaddr;
6851 +
6852 +fail_2:
6853 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
6854 +fail_1:
6855 + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
6856 + bus_dma_tag_destroy(dma->dma_tag);
6857 +fail_0:
6858 + dma->dma_map = NULL;
6859 + dma->dma_tag = NULL;
6860 +
6861 + return NULL;
6862 +}
6863 +
6864 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
6865 +{
6866 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
6867 +
6868 + if (dma->dma_tag == NULL)
6869 + return;
6870 + if (dma->dma_map != NULL) {
6871 + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
6872 + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6873 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
6874 + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
6875 + dma->dma_map = NULL;
6876 + }
6877 +
6878 + bus_dma_tag_destroy(dma->dma_tag);
6879 + dma->dma_tag = NULL;
6880 +}
6881 +
6882 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
6883 +{
6884 + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
6885 +}
6886 +
6887 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
6888 +{
6889 + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6890 +}
6891 +
6892 +void __DWC_FREE(void *mem_ctx, void *addr)
6893 +{
6894 + free(addr, M_DEVBUF);
6895 +}
6896 +
6897 +
6898 +#ifdef DWC_CRYPTOLIB
6899 +/* dwc_crypto.h */
6900 +
6901 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
6902 +{
6903 + get_random_bytes(buffer, length);
6904 +}
6905 +
6906 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
6907 +{
6908 + struct crypto_blkcipher *tfm;
6909 + struct blkcipher_desc desc;
6910 + struct scatterlist sgd;
6911 + struct scatterlist sgs;
6912 +
6913 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
6914 + if (tfm == NULL) {
6915 + printk("failed to load transform for aes CBC\n");
6916 + return -1;
6917 + }
6918 +
6919 + crypto_blkcipher_setkey(tfm, key, keylen);
6920 + crypto_blkcipher_set_iv(tfm, iv, 16);
6921 +
6922 + sg_init_one(&sgd, out, messagelen);
6923 + sg_init_one(&sgs, message, messagelen);
6924 +
6925 + desc.tfm = tfm;
6926 + desc.flags = 0;
6927 +
6928 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
6929 + crypto_free_blkcipher(tfm);
6930 + DWC_ERROR("AES CBC encryption failed");
6931 + return -1;
6932 + }
6933 +
6934 + crypto_free_blkcipher(tfm);
6935 + return 0;
6936 +}
6937 +
6938 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
6939 +{
6940 + struct crypto_hash *tfm;
6941 + struct hash_desc desc;
6942 + struct scatterlist sg;
6943 +
6944 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
6945 + if (IS_ERR(tfm)) {
6946 + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
6947 + return 0;
6948 + }
6949 + desc.tfm = tfm;
6950 + desc.flags = 0;
6951 +
6952 + sg_init_one(&sg, message, len);
6953 + crypto_hash_digest(&desc, &sg, len, out);
6954 + crypto_free_hash(tfm);
6955 +
6956 + return 1;
6957 +}
6958 +
6959 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
6960 + uint8_t *key, uint32_t keylen, uint8_t *out)
6961 +{
6962 + struct crypto_hash *tfm;
6963 + struct hash_desc desc;
6964 + struct scatterlist sg;
6965 +
6966 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
6967 + if (IS_ERR(tfm)) {
6968 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
6969 + return 0;
6970 + }
6971 + desc.tfm = tfm;
6972 + desc.flags = 0;
6973 +
6974 + sg_init_one(&sg, message, messagelen);
6975 + crypto_hash_setkey(tfm, key, keylen);
6976 + crypto_hash_digest(&desc, &sg, messagelen, out);
6977 + crypto_free_hash(tfm);
6978 +
6979 + return 1;
6980 +}
6981 +
6982 +#endif /* DWC_CRYPTOLIB */
6983 +
6984 +
6985 +/* Byte Ordering Conversions */
6986 +
6987 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
6988 +{
6989 +#ifdef __LITTLE_ENDIAN
6990 + return *p;
6991 +#else
6992 + uint8_t *u_p = (uint8_t *)p;
6993 +
6994 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6995 +#endif
6996 +}
6997 +
6998 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
6999 +{
7000 +#ifdef __BIG_ENDIAN
7001 + return *p;
7002 +#else
7003 + uint8_t *u_p = (uint8_t *)p;
7004 +
7005 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7006 +#endif
7007 +}
7008 +
7009 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
7010 +{
7011 +#ifdef __LITTLE_ENDIAN
7012 + return *p;
7013 +#else
7014 + uint8_t *u_p = (uint8_t *)p;
7015 +
7016 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7017 +#endif
7018 +}
7019 +
7020 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
7021 +{
7022 +#ifdef __BIG_ENDIAN
7023 + return *p;
7024 +#else
7025 + uint8_t *u_p = (uint8_t *)p;
7026 +
7027 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7028 +#endif
7029 +}
7030 +
7031 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
7032 +{
7033 +#ifdef __LITTLE_ENDIAN
7034 + return *p;
7035 +#else
7036 + uint8_t *u_p = (uint8_t *)p;
7037 + return (u_p[1] | (u_p[0] << 8));
7038 +#endif
7039 +}
7040 +
7041 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
7042 +{
7043 +#ifdef __BIG_ENDIAN
7044 + return *p;
7045 +#else
7046 + uint8_t *u_p = (uint8_t *)p;
7047 + return (u_p[1] | (u_p[0] << 8));
7048 +#endif
7049 +}
7050 +
7051 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
7052 +{
7053 +#ifdef __LITTLE_ENDIAN
7054 + return *p;
7055 +#else
7056 + uint8_t *u_p = (uint8_t *)p;
7057 + return (u_p[1] | (u_p[0] << 8));
7058 +#endif
7059 +}
7060 +
7061 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
7062 +{
7063 +#ifdef __BIG_ENDIAN
7064 + return *p;
7065 +#else
7066 + uint8_t *u_p = (uint8_t *)p;
7067 + return (u_p[1] | (u_p[0] << 8));
7068 +#endif
7069 +}
7070 +
7071 +
7072 +/* Registers */
7073 +
7074 +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
7075 +{
7076 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
7077 + bus_size_t ior = (bus_size_t)reg;
7078 +
7079 + return bus_space_read_4(io->iot, io->ioh, ior);
7080 +}
7081 +
7082 +#if 0
7083 +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
7084 +{
7085 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
7086 + bus_size_t ior = (bus_size_t)reg;
7087 +
7088 + return bus_space_read_8(io->iot, io->ioh, ior);
7089 +}
7090 +#endif
7091 +
7092 +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
7093 +{
7094 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
7095 + bus_size_t ior = (bus_size_t)reg;
7096 +
7097 + bus_space_write_4(io->iot, io->ioh, ior, value);
7098 +}
7099 +
7100 +#if 0
7101 +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
7102 +{
7103 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
7104 + bus_size_t ior = (bus_size_t)reg;
7105 +
7106 + bus_space_write_8(io->iot, io->ioh, ior, value);
7107 +}
7108 +#endif
7109 +
7110 +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
7111 + uint32_t set_mask)
7112 +{
7113 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
7114 + bus_size_t ior = (bus_size_t)reg;
7115 +
7116 + bus_space_write_4(io->iot, io->ioh, ior,
7117 + (bus_space_read_4(io->iot, io->ioh, ior) &
7118 + ~clear_mask) | set_mask);
7119 +}
7120 +
7121 +#if 0
7122 +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
7123 + uint64_t set_mask)
7124 +{
7125 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
7126 + bus_size_t ior = (bus_size_t)reg;
7127 +
7128 + bus_space_write_8(io->iot, io->ioh, ior,
7129 + (bus_space_read_8(io->iot, io->ioh, ior) &
7130 + ~clear_mask) | set_mask);
7131 +}
7132 +#endif
7133 +
7134 +
7135 +/* Locking */
7136 +
7137 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
7138 +{
7139 + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
7140 +
7141 + if (!sl) {
7142 + DWC_ERROR("Cannot allocate memory for spinlock");
7143 + return NULL;
7144 + }
7145 +
7146 + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
7147 + return (dwc_spinlock_t *)sl;
7148 +}
7149 +
7150 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
7151 +{
7152 + struct mtx *sl = (struct mtx *)lock;
7153 +
7154 + mtx_destroy(sl);
7155 + DWC_FREE(sl);
7156 +}
7157 +
7158 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
7159 +{
7160 + mtx_lock_spin((struct mtx *)lock); // ???
7161 +}
7162 +
7163 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
7164 +{
7165 + mtx_unlock_spin((struct mtx *)lock); // ???
7166 +}
7167 +
7168 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
7169 +{
7170 + mtx_lock_spin((struct mtx *)lock);
7171 +}
7172 +
7173 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
7174 +{
7175 + mtx_unlock_spin((struct mtx *)lock);
7176 +}
7177 +
7178 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
7179 +{
7180 + struct mtx *m;
7181 + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
7182 +
7183 + if (!mutex) {
7184 + DWC_ERROR("Cannot allocate memory for mutex");
7185 + return NULL;
7186 + }
7187 +
7188 + m = (struct mtx *)mutex;
7189 + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
7190 + return mutex;
7191 +}
7192 +
7193 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
7194 +#else
7195 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
7196 +{
7197 + mtx_destroy((struct mtx *)mutex);
7198 + DWC_FREE(mutex);
7199 +}
7200 +#endif
7201 +
7202 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
7203 +{
7204 + struct mtx *m = (struct mtx *)mutex;
7205 +
7206 + mtx_lock(m);
7207 +}
7208 +
7209 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
7210 +{
7211 + struct mtx *m = (struct mtx *)mutex;
7212 +
7213 + return mtx_trylock(m);
7214 +}
7215 +
7216 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
7217 +{
7218 + struct mtx *m = (struct mtx *)mutex;
7219 +
7220 + mtx_unlock(m);
7221 +}
7222 +
7223 +
7224 +/* Timing */
7225 +
7226 +void DWC_UDELAY(uint32_t usecs)
7227 +{
7228 + DELAY(usecs);
7229 +}
7230 +
7231 +void DWC_MDELAY(uint32_t msecs)
7232 +{
7233 + do {
7234 + DELAY(1000);
7235 + } while (--msecs);
7236 +}
7237 +
7238 +void DWC_MSLEEP(uint32_t msecs)
7239 +{
7240 + struct timeval tv;
7241 +
7242 + tv.tv_sec = msecs / 1000;
7243 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
7244 + pause("dw3slp", tvtohz(&tv));
7245 +}
7246 +
7247 +uint32_t DWC_TIME(void)
7248 +{
7249 + struct timeval tv;
7250 +
7251 + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
7252 + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
7253 +}
7254 +
7255 +
7256 +/* Timers */
7257 +
7258 +struct dwc_timer {
7259 + struct callout t;
7260 + char *name;
7261 + dwc_spinlock_t *lock;
7262 + dwc_timer_callback_t cb;
7263 + void *data;
7264 +};
7265 +
7266 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
7267 +{
7268 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
7269 +
7270 + if (!t) {
7271 + DWC_ERROR("Cannot allocate memory for timer");
7272 + return NULL;
7273 + }
7274 +
7275 + callout_init(&t->t, 1);
7276 +
7277 + t->name = DWC_STRDUP(name);
7278 + if (!t->name) {
7279 + DWC_ERROR("Cannot allocate memory for timer->name");
7280 + goto no_name;
7281 + }
7282 +
7283 + t->lock = DWC_SPINLOCK_ALLOC();
7284 + if (!t->lock) {
7285 + DWC_ERROR("Cannot allocate memory for lock");
7286 + goto no_lock;
7287 + }
7288 +
7289 + t->cb = cb;
7290 + t->data = data;
7291 +
7292 + return t;
7293 +
7294 + no_lock:
7295 + DWC_FREE(t->name);
7296 + no_name:
7297 + DWC_FREE(t);
7298 +
7299 + return NULL;
7300 +}
7301 +
7302 +void DWC_TIMER_FREE(dwc_timer_t *timer)
7303 +{
7304 + callout_stop(&timer->t);
7305 + DWC_SPINLOCK_FREE(timer->lock);
7306 + DWC_FREE(timer->name);
7307 + DWC_FREE(timer);
7308 +}
7309 +
7310 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
7311 +{
7312 + struct timeval tv;
7313 +
7314 + tv.tv_sec = time / 1000;
7315 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
7316 + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
7317 +}
7318 +
7319 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
7320 +{
7321 + callout_stop(&timer->t);
7322 +}
7323 +
7324 +
7325 +/* Wait Queues */
7326 +
7327 +struct dwc_waitq {
7328 + struct mtx lock;
7329 + int abort;
7330 +};
7331 +
7332 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
7333 +{
7334 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
7335 +
7336 + if (!wq) {
7337 + DWC_ERROR("Cannot allocate memory for waitqueue");
7338 + return NULL;
7339 + }
7340 +
7341 + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
7342 + wq->abort = 0;
7343 +
7344 + return wq;
7345 +}
7346 +
7347 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
7348 +{
7349 + mtx_destroy(&wq->lock);
7350 + DWC_FREE(wq);
7351 +}
7352 +
7353 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
7354 +{
7355 +// intrmask_t ipl;
7356 + int result = 0;
7357 +
7358 + mtx_lock(&wq->lock);
7359 +// ipl = splbio();
7360 +
7361 + /* Skip the sleep if already aborted or triggered */
7362 + if (!wq->abort && !cond(data)) {
7363 +// splx(ipl);
7364 + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
7365 +// ipl = splbio();
7366 + }
7367 +
7368 + if (result == ERESTART) { // signaled - restart
7369 + result = -DWC_E_RESTART;
7370 +
7371 + } else if (result == EINTR) { // signaled - interrupt
7372 + result = -DWC_E_ABORT;
7373 +
7374 + } else if (wq->abort) {
7375 + result = -DWC_E_ABORT;
7376 +
7377 + } else {
7378 + result = 0;
7379 + }
7380 +
7381 + wq->abort = 0;
7382 +// splx(ipl);
7383 + mtx_unlock(&wq->lock);
7384 + return result;
7385 +}
7386 +
7387 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
7388 + void *data, int32_t msecs)
7389 +{
7390 + struct timeval tv, tv1, tv2;
7391 +// intrmask_t ipl;
7392 + int result = 0;
7393 +
7394 + tv.tv_sec = msecs / 1000;
7395 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
7396 +
7397 + mtx_lock(&wq->lock);
7398 +// ipl = splbio();
7399 +
7400 + /* Skip the sleep if already aborted or triggered */
7401 + if (!wq->abort && !cond(data)) {
7402 +// splx(ipl);
7403 + getmicrouptime(&tv1);
7404 + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
7405 + getmicrouptime(&tv2);
7406 +// ipl = splbio();
7407 + }
7408 +
7409 + if (result == 0) { // awoken
7410 + if (wq->abort) {
7411 + result = -DWC_E_ABORT;
7412 + } else {
7413 + tv2.tv_usec -= tv1.tv_usec;
7414 + if (tv2.tv_usec < 0) {
7415 + tv2.tv_usec += 1000000;
7416 + tv2.tv_sec--;
7417 + }
7418 +
7419 + tv2.tv_sec -= tv1.tv_sec;
7420 + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
7421 + result = msecs - result;
7422 + if (result <= 0)
7423 + result = 1;
7424 + }
7425 + } else if (result == ERESTART) { // signaled - restart
7426 + result = -DWC_E_RESTART;
7427 +
7428 + } else if (result == EINTR) { // signaled - interrupt
7429 + result = -DWC_E_ABORT;
7430 +
7431 + } else { // timed out
7432 + result = -DWC_E_TIMEOUT;
7433 + }
7434 +
7435 + wq->abort = 0;
7436 +// splx(ipl);
7437 + mtx_unlock(&wq->lock);
7438 + return result;
7439 +}
7440 +
7441 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
7442 +{
7443 + wakeup(wq);
7444 +}
7445 +
7446 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
7447 +{
7448 +// intrmask_t ipl;
7449 +
7450 + mtx_lock(&wq->lock);
7451 +// ipl = splbio();
7452 + wq->abort = 1;
7453 + wakeup(wq);
7454 +// splx(ipl);
7455 + mtx_unlock(&wq->lock);
7456 +}
7457 +
7458 +
7459 +/* Threading */
7460 +
7461 +struct dwc_thread {
7462 + struct proc *proc;
7463 + int abort;
7464 +};
7465 +
7466 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
7467 +{
7468 + int retval;
7469 + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
7470 +
7471 + if (!thread) {
7472 + return NULL;
7473 + }
7474 +
7475 + thread->abort = 0;
7476 + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
7477 + RFPROC | RFNOWAIT, 0, "%s", name);
7478 + if (retval) {
7479 + DWC_FREE(thread);
7480 + return NULL;
7481 + }
7482 +
7483 + return thread;
7484 +}
7485 +
7486 +int DWC_THREAD_STOP(dwc_thread_t *thread)
7487 +{
7488 + int retval;
7489 +
7490 + thread->abort = 1;
7491 + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
7492 +
7493 + if (retval == 0) {
7494 + /* DWC_THREAD_EXIT() will free the thread struct */
7495 + return 0;
7496 + }
7497 +
7498 + /* NOTE: We leak the thread struct if thread doesn't die */
7499 +
7500 + if (retval == EWOULDBLOCK) {
7501 + return -DWC_E_TIMEOUT;
7502 + }
7503 +
7504 + return -DWC_E_UNKNOWN;
7505 +}
7506 +
7507 +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
7508 +{
7509 + return thread->abort;
7510 +}
7511 +
7512 +void DWC_THREAD_EXIT(dwc_thread_t *thread)
7513 +{
7514 + wakeup(&thread->abort);
7515 + DWC_FREE(thread);
7516 + kthread_exit(0);
7517 +}
7518 +
7519 +
7520 +/* tasklets
7521 + - Runs in interrupt context (cannot sleep)
7522 + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
7523 + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
7524 + */
7525 +struct dwc_tasklet {
7526 + struct task t;
7527 + dwc_tasklet_callback_t cb;
7528 + void *data;
7529 +};
7530 +
7531 +static void tasklet_callback(void *data, int pending) // what to do with pending ???
7532 +{
7533 + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
7534 +
7535 + task->cb(task->data);
7536 +}
7537 +
7538 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
7539 +{
7540 + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
7541 +
7542 + if (task) {
7543 + task->cb = cb;
7544 + task->data = data;
7545 + TASK_INIT(&task->t, 0, tasklet_callback, task);
7546 + } else {
7547 + DWC_ERROR("Cannot allocate memory for tasklet");
7548 + }
7549 +
7550 + return task;
7551 +}
7552 +
7553 +void DWC_TASK_FREE(dwc_tasklet_t *task)
7554 +{
7555 + taskqueue_drain(taskqueue_fast, &task->t); // ???
7556 + DWC_FREE(task);
7557 +}
7558 +
7559 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
7560 +{
7561 + /* Uses predefined system queue */
7562 + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
7563 +}
7564 +
7565 +
7566 +/* workqueues
7567 + - Runs in process context (can sleep)
7568 + */
7569 +typedef struct work_container {
7570 + dwc_work_callback_t cb;
7571 + void *data;
7572 + dwc_workq_t *wq;
7573 + char *name;
7574 + int hz;
7575 +
7576 +#ifdef DEBUG
7577 + DWC_CIRCLEQ_ENTRY(work_container) entry;
7578 +#endif
7579 + struct task task;
7580 +} work_container_t;
7581 +
7582 +#ifdef DEBUG
7583 +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
7584 +#endif
7585 +
7586 +struct dwc_workq {
7587 + struct taskqueue *taskq;
7588 + dwc_spinlock_t *lock;
7589 + dwc_waitq_t *waitq;
7590 + int pending;
7591 +
7592 +#ifdef DEBUG
7593 + struct work_container_queue entries;
7594 +#endif
7595 +};
7596 +
7597 +static void do_work(void *data, int pending) // what to do with pending ???
7598 +{
7599 + work_container_t *container = (work_container_t *)data;
7600 + dwc_workq_t *wq = container->wq;
7601 + dwc_irqflags_t flags;
7602 +
7603 + if (container->hz) {
7604 + pause("dw3wrk", container->hz);
7605 + }
7606 +
7607 + container->cb(container->data);
7608 + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
7609 +
7610 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7611 +
7612 +#ifdef DEBUG
7613 + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
7614 +#endif
7615 + if (container->name)
7616 + DWC_FREE(container->name);
7617 + DWC_FREE(container);
7618 + wq->pending--;
7619 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7620 + DWC_WAITQ_TRIGGER(wq->waitq);
7621 +}
7622 +
7623 +static int work_done(void *data)
7624 +{
7625 + dwc_workq_t *workq = (dwc_workq_t *)data;
7626 +
7627 + return workq->pending == 0;
7628 +}
7629 +
7630 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
7631 +{
7632 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
7633 +}
7634 +
7635 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
7636 +{
7637 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
7638 +
7639 + if (!wq) {
7640 + DWC_ERROR("Cannot allocate memory for workqueue");
7641 + return NULL;
7642 + }
7643 +
7644 + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
7645 + if (!wq->taskq) {
7646 + DWC_ERROR("Cannot allocate memory for taskqueue");
7647 + goto no_taskq;
7648 + }
7649 +
7650 + wq->pending = 0;
7651 +
7652 + wq->lock = DWC_SPINLOCK_ALLOC();
7653 + if (!wq->lock) {
7654 + DWC_ERROR("Cannot allocate memory for spinlock");
7655 + goto no_lock;
7656 + }
7657 +
7658 + wq->waitq = DWC_WAITQ_ALLOC();
7659 + if (!wq->waitq) {
7660 + DWC_ERROR("Cannot allocate memory for waitqueue");
7661 + goto no_waitq;
7662 + }
7663 +
7664 + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
7665 +
7666 +#ifdef DEBUG
7667 + DWC_CIRCLEQ_INIT(&wq->entries);
7668 +#endif
7669 + return wq;
7670 +
7671 + no_waitq:
7672 + DWC_SPINLOCK_FREE(wq->lock);
7673 + no_lock:
7674 + taskqueue_free(wq->taskq);
7675 + no_taskq:
7676 + DWC_FREE(wq);
7677 +
7678 + return NULL;
7679 +}
7680 +
7681 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
7682 +{
7683 +#ifdef DEBUG
7684 + dwc_irqflags_t flags;
7685 +
7686 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7687 +
7688 + if (wq->pending != 0) {
7689 + struct work_container *container;
7690 +
7691 + DWC_ERROR("Destroying work queue with pending work");
7692 +
7693 + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
7694 + DWC_ERROR("Work %s still pending", container->name);
7695 + }
7696 + }
7697 +
7698 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7699 +#endif
7700 + DWC_WAITQ_FREE(wq->waitq);
7701 + DWC_SPINLOCK_FREE(wq->lock);
7702 + taskqueue_free(wq->taskq);
7703 + DWC_FREE(wq);
7704 +}
7705 +
7706 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
7707 + char *format, ...)
7708 +{
7709 + dwc_irqflags_t flags;
7710 + work_container_t *container;
7711 + static char name[128];
7712 + va_list args;
7713 +
7714 + va_start(args, format);
7715 + DWC_VSNPRINTF(name, 128, format, args);
7716 + va_end(args);
7717 +
7718 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7719 + wq->pending++;
7720 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7721 + DWC_WAITQ_TRIGGER(wq->waitq);
7722 +
7723 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
7724 + if (!container) {
7725 + DWC_ERROR("Cannot allocate memory for container");
7726 + return;
7727 + }
7728 +
7729 + container->name = DWC_STRDUP(name);
7730 + if (!container->name) {
7731 + DWC_ERROR("Cannot allocate memory for container->name");
7732 + DWC_FREE(container);
7733 + return;
7734 + }
7735 +
7736 + container->cb = cb;
7737 + container->data = data;
7738 + container->wq = wq;
7739 + container->hz = 0;
7740 +
7741 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
7742 +
7743 + TASK_INIT(&container->task, 0, do_work, container);
7744 +
7745 +#ifdef DEBUG
7746 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
7747 +#endif
7748 + taskqueue_enqueue_fast(wq->taskq, &container->task);
7749 +}
7750 +
7751 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
7752 + void *data, uint32_t time, char *format, ...)
7753 +{
7754 + dwc_irqflags_t flags;
7755 + work_container_t *container;
7756 + static char name[128];
7757 + struct timeval tv;
7758 + va_list args;
7759 +
7760 + va_start(args, format);
7761 + DWC_VSNPRINTF(name, 128, format, args);
7762 + va_end(args);
7763 +
7764 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7765 + wq->pending++;
7766 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7767 + DWC_WAITQ_TRIGGER(wq->waitq);
7768 +
7769 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
7770 + if (!container) {
7771 + DWC_ERROR("Cannot allocate memory for container");
7772 + return;
7773 + }
7774 +
7775 + container->name = DWC_STRDUP(name);
7776 + if (!container->name) {
7777 + DWC_ERROR("Cannot allocate memory for container->name");
7778 + DWC_FREE(container);
7779 + return;
7780 + }
7781 +
7782 + container->cb = cb;
7783 + container->data = data;
7784 + container->wq = wq;
7785 +
7786 + tv.tv_sec = time / 1000;
7787 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
7788 + container->hz = tvtohz(&tv);
7789 +
7790 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
7791 +
7792 + TASK_INIT(&container->task, 0, do_work, container);
7793 +
7794 +#ifdef DEBUG
7795 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
7796 +#endif
7797 + taskqueue_enqueue_fast(wq->taskq, &container->task);
7798 +}
7799 +
7800 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
7801 +{
7802 + return wq->pending;
7803 +}
7804 --- /dev/null
7805 +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
7806 @@ -0,0 +1,1409 @@
7807 +#include <linux/kernel.h>
7808 +#include <linux/init.h>
7809 +#include <linux/module.h>
7810 +#include <linux/kthread.h>
7811 +
7812 +#ifdef DWC_CCLIB
7813 +# include "dwc_cc.h"
7814 +#endif
7815 +
7816 +#ifdef DWC_CRYPTOLIB
7817 +# include "dwc_modpow.h"
7818 +# include "dwc_dh.h"
7819 +# include "dwc_crypto.h"
7820 +#endif
7821 +
7822 +#ifdef DWC_NOTIFYLIB
7823 +# include "dwc_notifier.h"
7824 +#endif
7825 +
7826 +/* OS-Level Implementations */
7827 +
7828 +/* This is the Linux kernel implementation of the DWC platform library. */
7829 +#include <linux/moduleparam.h>
7830 +#include <linux/ctype.h>
7831 +#include <linux/crypto.h>
7832 +#include <linux/delay.h>
7833 +#include <linux/device.h>
7834 +#include <linux/dma-mapping.h>
7835 +#include <linux/cdev.h>
7836 +#include <linux/errno.h>
7837 +#include <linux/interrupt.h>
7838 +#include <linux/jiffies.h>
7839 +#include <linux/list.h>
7840 +#include <linux/pci.h>
7841 +#include <linux/random.h>
7842 +#include <linux/scatterlist.h>
7843 +#include <linux/slab.h>
7844 +#include <linux/stat.h>
7845 +#include <linux/string.h>
7846 +#include <linux/timer.h>
7847 +#include <linux/usb.h>
7848 +
7849 +#include <linux/version.h>
7850 +
7851 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
7852 +# include <linux/usb/gadget.h>
7853 +#else
7854 +# include <linux/usb_gadget.h>
7855 +#endif
7856 +
7857 +#include <asm/io.h>
7858 +#include <asm/page.h>
7859 +#include <asm/uaccess.h>
7860 +#include <asm/unaligned.h>
7861 +
7862 +#include "dwc_os.h"
7863 +#include "dwc_list.h"
7864 +
7865 +
7866 +/* MISC */
7867 +
7868 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
7869 +{
7870 + return memset(dest, byte, size);
7871 +}
7872 +
7873 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
7874 +{
7875 + return memcpy(dest, src, size);
7876 +}
7877 +
7878 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
7879 +{
7880 + return memmove(dest, src, size);
7881 +}
7882 +
7883 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
7884 +{
7885 + return memcmp(m1, m2, size);
7886 +}
7887 +
7888 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
7889 +{
7890 + return strncmp(s1, s2, size);
7891 +}
7892 +
7893 +int DWC_STRCMP(void *s1, void *s2)
7894 +{
7895 + return strcmp(s1, s2);
7896 +}
7897 +
7898 +int DWC_STRLEN(char const *str)
7899 +{
7900 + return strlen(str);
7901 +}
7902 +
7903 +char *DWC_STRCPY(char *to, char const *from)
7904 +{
7905 + return strcpy(to, from);
7906 +}
7907 +
7908 +char *DWC_STRDUP(char const *str)
7909 +{
7910 + int len = DWC_STRLEN(str) + 1;
7911 + char *new = DWC_ALLOC_ATOMIC(len);
7912 +
7913 + if (!new) {
7914 + return NULL;
7915 + }
7916 +
7917 + DWC_MEMCPY(new, str, len);
7918 + return new;
7919 +}
7920 +
7921 +int DWC_ATOI(const char *str, int32_t *value)
7922 +{
7923 + char *end = NULL;
7924 +
7925 + *value = simple_strtol(str, &end, 0);
7926 + if (*end == '\0') {
7927 + return 0;
7928 + }
7929 +
7930 + return -1;
7931 +}
7932 +
7933 +int DWC_ATOUI(const char *str, uint32_t *value)
7934 +{
7935 + char *end = NULL;
7936 +
7937 + *value = simple_strtoul(str, &end, 0);
7938 + if (*end == '\0') {
7939 + return 0;
7940 + }
7941 +
7942 + return -1;
7943 +}
7944 +
7945 +
7946 +#ifdef DWC_UTFLIB
7947 +/* From usbstring.c */
7948 +
7949 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
7950 +{
7951 + int count = 0;
7952 + u8 c;
7953 + u16 uchar;
7954 +
7955 + /* this insists on correct encodings, though not minimal ones.
7956 + * BUT it currently rejects legit 4-byte UTF-8 code points,
7957 + * which need surrogate pairs. (Unicode 3.1 can use them.)
7958 + */
7959 + while (len != 0 && (c = (u8) *s++) != 0) {
7960 + if (unlikely(c & 0x80)) {
7961 + // 2-byte sequence:
7962 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
7963 + if ((c & 0xe0) == 0xc0) {
7964 + uchar = (c & 0x1f) << 6;
7965 +
7966 + c = (u8) *s++;
7967 + if ((c & 0xc0) != 0xc0)
7968 + goto fail;
7969 + c &= 0x3f;
7970 + uchar |= c;
7971 +
7972 + // 3-byte sequence (most CJKV characters):
7973 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
7974 + } else if ((c & 0xf0) == 0xe0) {
7975 + uchar = (c & 0x0f) << 12;
7976 +
7977 + c = (u8) *s++;
7978 + if ((c & 0xc0) != 0xc0)
7979 + goto fail;
7980 + c &= 0x3f;
7981 + uchar |= c << 6;
7982 +
7983 + c = (u8) *s++;
7984 + if ((c & 0xc0) != 0xc0)
7985 + goto fail;
7986 + c &= 0x3f;
7987 + uchar |= c;
7988 +
7989 + /* no bogus surrogates */
7990 + if (0xd800 <= uchar && uchar <= 0xdfff)
7991 + goto fail;
7992 +
7993 + // 4-byte sequence (surrogate pairs, currently rare):
7994 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
7995 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
7996 + // (uuuuu = wwww + 1)
7997 + // FIXME accept the surrogate code points (only)
7998 + } else
7999 + goto fail;
8000 + } else
8001 + uchar = c;
8002 + put_unaligned (cpu_to_le16 (uchar), cp++);
8003 + count++;
8004 + len--;
8005 + }
8006 + return count;
8007 +fail:
8008 + return -1;
8009 +}
8010 +#endif /* DWC_UTFLIB */
8011 +
8012 +
8013 +/* dwc_debug.h */
8014 +
8015 +dwc_bool_t DWC_IN_IRQ(void)
8016 +{
8017 + return in_irq();
8018 +}
8019 +
8020 +dwc_bool_t DWC_IN_BH(void)
8021 +{
8022 + return in_softirq();
8023 +}
8024 +
8025 +void DWC_VPRINTF(char *format, va_list args)
8026 +{
8027 + vprintk(format, args);
8028 +}
8029 +
8030 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
8031 +{
8032 + return vsnprintf(str, size, format, args);
8033 +}
8034 +
8035 +void DWC_PRINTF(char *format, ...)
8036 +{
8037 + va_list args;
8038 +
8039 + va_start(args, format);
8040 + DWC_VPRINTF(format, args);
8041 + va_end(args);
8042 +}
8043 +
8044 +int DWC_SPRINTF(char *buffer, char *format, ...)
8045 +{
8046 + int retval;
8047 + va_list args;
8048 +
8049 + va_start(args, format);
8050 + retval = vsprintf(buffer, format, args);
8051 + va_end(args);
8052 + return retval;
8053 +}
8054 +
8055 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
8056 +{
8057 + int retval;
8058 + va_list args;
8059 +
8060 + va_start(args, format);
8061 + retval = vsnprintf(buffer, size, format, args);
8062 + va_end(args);
8063 + return retval;
8064 +}
8065 +
8066 +void __DWC_WARN(char *format, ...)
8067 +{
8068 + va_list args;
8069 +
8070 + va_start(args, format);
8071 + DWC_PRINTF(KERN_WARNING);
8072 + DWC_VPRINTF(format, args);
8073 + va_end(args);
8074 +}
8075 +
8076 +void __DWC_ERROR(char *format, ...)
8077 +{
8078 + va_list args;
8079 +
8080 + va_start(args, format);
8081 + DWC_PRINTF(KERN_ERR);
8082 + DWC_VPRINTF(format, args);
8083 + va_end(args);
8084 +}
8085 +
8086 +void DWC_EXCEPTION(char *format, ...)
8087 +{
8088 + va_list args;
8089 +
8090 + va_start(args, format);
8091 + DWC_PRINTF(KERN_ERR);
8092 + DWC_VPRINTF(format, args);
8093 + va_end(args);
8094 + BUG_ON(1);
8095 +}
8096 +
8097 +#ifdef DEBUG
8098 +void __DWC_DEBUG(char *format, ...)
8099 +{
8100 + va_list args;
8101 +
8102 + va_start(args, format);
8103 + DWC_PRINTF(KERN_DEBUG);
8104 + DWC_VPRINTF(format, args);
8105 + va_end(args);
8106 +}
8107 +#endif
8108 +
8109 +
8110 +/* dwc_mem.h */
8111 +
8112 +#if 0
8113 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
8114 + uint32_t align,
8115 + uint32_t alloc)
8116 +{
8117 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
8118 + size, align, alloc);
8119 + return (dwc_pool_t *)pool;
8120 +}
8121 +
8122 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
8123 +{
8124 + dma_pool_destroy((struct dma_pool *)pool);
8125 +}
8126 +
8127 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
8128 +{
8129 + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
8130 +}
8131 +
8132 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
8133 +{
8134 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
8135 + memset(..);
8136 +}
8137 +
8138 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
8139 +{
8140 + dma_pool_free(pool, vaddr, daddr);
8141 +}
8142 +#endif
8143 +
8144 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
8145 +{
8146 + return dma_alloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
8147 +}
8148 +
8149 +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
8150 +{
8151 + return dma_alloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
8152 +}
8153 +
8154 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
8155 +{
8156 + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
8157 +}
8158 +
8159 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
8160 +{
8161 + return kzalloc(size, GFP_KERNEL);
8162 +}
8163 +
8164 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
8165 +{
8166 + return kzalloc(size, GFP_ATOMIC);
8167 +}
8168 +
8169 +void __DWC_FREE(void *mem_ctx, void *addr)
8170 +{
8171 + kfree(addr);
8172 +}
8173 +
8174 +
8175 +#ifdef DWC_CRYPTOLIB
8176 +/* dwc_crypto.h */
8177 +
8178 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
8179 +{
8180 + get_random_bytes(buffer, length);
8181 +}
8182 +
8183 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
8184 +{
8185 + struct crypto_blkcipher *tfm;
8186 + struct blkcipher_desc desc;
8187 + struct scatterlist sgd;
8188 + struct scatterlist sgs;
8189 +
8190 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
8191 + if (tfm == NULL) {
8192 + printk("failed to load transform for aes CBC\n");
8193 + return -1;
8194 + }
8195 +
8196 + crypto_blkcipher_setkey(tfm, key, keylen);
8197 + crypto_blkcipher_set_iv(tfm, iv, 16);
8198 +
8199 + sg_init_one(&sgd, out, messagelen);
8200 + sg_init_one(&sgs, message, messagelen);
8201 +
8202 + desc.tfm = tfm;
8203 + desc.flags = 0;
8204 +
8205 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
8206 + crypto_free_blkcipher(tfm);
8207 + DWC_ERROR("AES CBC encryption failed");
8208 + return -1;
8209 + }
8210 +
8211 + crypto_free_blkcipher(tfm);
8212 + return 0;
8213 +}
8214 +
8215 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
8216 +{
8217 + struct crypto_hash *tfm;
8218 + struct hash_desc desc;
8219 + struct scatterlist sg;
8220 +
8221 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
8222 + if (IS_ERR(tfm)) {
8223 + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
8224 + return 0;
8225 + }
8226 + desc.tfm = tfm;
8227 + desc.flags = 0;
8228 +
8229 + sg_init_one(&sg, message, len);
8230 + crypto_hash_digest(&desc, &sg, len, out);
8231 + crypto_free_hash(tfm);
8232 +
8233 + return 1;
8234 +}
8235 +
8236 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
8237 + uint8_t *key, uint32_t keylen, uint8_t *out)
8238 +{
8239 + struct crypto_hash *tfm;
8240 + struct hash_desc desc;
8241 + struct scatterlist sg;
8242 +
8243 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
8244 + if (IS_ERR(tfm)) {
8245 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
8246 + return 0;
8247 + }
8248 + desc.tfm = tfm;
8249 + desc.flags = 0;
8250 +
8251 + sg_init_one(&sg, message, messagelen);
8252 + crypto_hash_setkey(tfm, key, keylen);
8253 + crypto_hash_digest(&desc, &sg, messagelen, out);
8254 + crypto_free_hash(tfm);
8255 +
8256 + return 1;
8257 +}
8258 +#endif /* DWC_CRYPTOLIB */
8259 +
8260 +
8261 +/* Byte Ordering Conversions */
8262 +
8263 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
8264 +{
8265 +#ifdef __LITTLE_ENDIAN
8266 + return *p;
8267 +#else
8268 + uint8_t *u_p = (uint8_t *)p;
8269 +
8270 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
8271 +#endif
8272 +}
8273 +
8274 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
8275 +{
8276 +#ifdef __BIG_ENDIAN
8277 + return *p;
8278 +#else
8279 + uint8_t *u_p = (uint8_t *)p;
8280 +
8281 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
8282 +#endif
8283 +}
8284 +
8285 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
8286 +{
8287 +#ifdef __LITTLE_ENDIAN
8288 + return *p;
8289 +#else
8290 + uint8_t *u_p = (uint8_t *)p;
8291 +
8292 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
8293 +#endif
8294 +}
8295 +
8296 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
8297 +{
8298 +#ifdef __BIG_ENDIAN
8299 + return *p;
8300 +#else
8301 + uint8_t *u_p = (uint8_t *)p;
8302 +
8303 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
8304 +#endif
8305 +}
8306 +
8307 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
8308 +{
8309 +#ifdef __LITTLE_ENDIAN
8310 + return *p;
8311 +#else
8312 + uint8_t *u_p = (uint8_t *)p;
8313 + return (u_p[1] | (u_p[0] << 8));
8314 +#endif
8315 +}
8316 +
8317 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
8318 +{
8319 +#ifdef __BIG_ENDIAN
8320 + return *p;
8321 +#else
8322 + uint8_t *u_p = (uint8_t *)p;
8323 + return (u_p[1] | (u_p[0] << 8));
8324 +#endif
8325 +}
8326 +
8327 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
8328 +{
8329 +#ifdef __LITTLE_ENDIAN
8330 + return *p;
8331 +#else
8332 + uint8_t *u_p = (uint8_t *)p;
8333 + return (u_p[1] | (u_p[0] << 8));
8334 +#endif
8335 +}
8336 +
8337 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
8338 +{
8339 +#ifdef __BIG_ENDIAN
8340 + return *p;
8341 +#else
8342 + uint8_t *u_p = (uint8_t *)p;
8343 + return (u_p[1] | (u_p[0] << 8));
8344 +#endif
8345 +}
8346 +
8347 +
8348 +/* Registers */
8349 +
8350 +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
8351 +{
8352 + return readl(reg);
8353 +}
8354 +
8355 +#if 0
8356 +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
8357 +{
8358 +}
8359 +#endif
8360 +
8361 +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
8362 +{
8363 + writel(value, reg);
8364 +}
8365 +
8366 +#if 0
8367 +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
8368 +{
8369 +}
8370 +#endif
8371 +
8372 +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
8373 +{
8374 + writel((readl(reg) & ~clear_mask) | set_mask, reg);
8375 +}
8376 +
8377 +#if 0
8378 +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
8379 +{
8380 +}
8381 +#endif
8382 +
8383 +
8384 +/* Locking */
8385 +
8386 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
8387 +{
8388 + spinlock_t *sl = (spinlock_t *)1;
8389 +
8390 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8391 + sl = DWC_ALLOC(sizeof(*sl));
8392 + if (!sl) {
8393 + DWC_ERROR("Cannot allocate memory for spinlock\n");
8394 + return NULL;
8395 + }
8396 +
8397 + spin_lock_init(sl);
8398 +#endif
8399 + return (dwc_spinlock_t *)sl;
8400 +}
8401 +
8402 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
8403 +{
8404 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8405 + DWC_FREE(lock);
8406 +#endif
8407 +}
8408 +
8409 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
8410 +{
8411 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8412 + spin_lock((spinlock_t *)lock);
8413 +#endif
8414 +}
8415 +
8416 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
8417 +{
8418 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8419 + spin_unlock((spinlock_t *)lock);
8420 +#endif
8421 +}
8422 +
8423 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
8424 +{
8425 + dwc_irqflags_t f;
8426 +
8427 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8428 + spin_lock_irqsave((spinlock_t *)lock, f);
8429 +#else
8430 + local_irq_save(f);
8431 +#endif
8432 + *flags = f;
8433 +}
8434 +
8435 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
8436 +{
8437 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8438 + spin_unlock_irqrestore((spinlock_t *)lock, flags);
8439 +#else
8440 + local_irq_restore(flags);
8441 +#endif
8442 +}
8443 +
8444 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
8445 +{
8446 + struct mutex *m;
8447 + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
8448 +
8449 + if (!mutex) {
8450 + DWC_ERROR("Cannot allocate memory for mutex\n");
8451 + return NULL;
8452 + }
8453 +
8454 + m = (struct mutex *)mutex;
8455 + mutex_init(m);
8456 + return mutex;
8457 +}
8458 +
8459 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
8460 +#else
8461 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
8462 +{
8463 + mutex_destroy((struct mutex *)mutex);
8464 + DWC_FREE(mutex);
8465 +}
8466 +#endif
8467 +
8468 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
8469 +{
8470 + struct mutex *m = (struct mutex *)mutex;
8471 + mutex_lock(m);
8472 +}
8473 +
8474 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
8475 +{
8476 + struct mutex *m = (struct mutex *)mutex;
8477 + return mutex_trylock(m);
8478 +}
8479 +
8480 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
8481 +{
8482 + struct mutex *m = (struct mutex *)mutex;
8483 + mutex_unlock(m);
8484 +}
8485 +
8486 +
8487 +/* Timing */
8488 +
8489 +void DWC_UDELAY(uint32_t usecs)
8490 +{
8491 + udelay(usecs);
8492 +}
8493 +
8494 +void DWC_MDELAY(uint32_t msecs)
8495 +{
8496 + mdelay(msecs);
8497 +}
8498 +
8499 +void DWC_MSLEEP(uint32_t msecs)
8500 +{
8501 + msleep(msecs);
8502 +}
8503 +
8504 +uint32_t DWC_TIME(void)
8505 +{
8506 + return jiffies_to_msecs(jiffies);
8507 +}
8508 +
8509 +
8510 +/* Timers */
8511 +
8512 +struct dwc_timer {
8513 + struct timer_list t;
8514 + char *name;
8515 + dwc_timer_callback_t cb;
8516 + void *data;
8517 + uint8_t scheduled;
8518 + dwc_spinlock_t *lock;
8519 +};
8520 +
8521 +static void timer_callback(struct timer_list *tt)
8522 +{
8523 + dwc_timer_t *timer = from_timer(timer, tt, t);
8524 + dwc_irqflags_t flags;
8525 +
8526 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8527 + timer->scheduled = 0;
8528 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8529 + DWC_DEBUGC("Timer %s callback", timer->name);
8530 + timer->cb(timer->data);
8531 +}
8532 +
8533 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
8534 +{
8535 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
8536 +
8537 + if (!t) {
8538 + DWC_ERROR("Cannot allocate memory for timer");
8539 + return NULL;
8540 + }
8541 +
8542 + t->name = DWC_STRDUP(name);
8543 + if (!t->name) {
8544 + DWC_ERROR("Cannot allocate memory for timer->name");
8545 + goto no_name;
8546 + }
8547 +
8548 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
8549 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
8550 +#else
8551 + t->lock = DWC_SPINLOCK_ALLOC();
8552 +#endif
8553 + if (!t->lock) {
8554 + DWC_ERROR("Cannot allocate memory for lock");
8555 + goto no_lock;
8556 + }
8557 +
8558 + t->scheduled = 0;
8559 + t->t.expires = jiffies;
8560 + timer_setup(&t->t, timer_callback, 0);
8561 +
8562 + t->cb = cb;
8563 + t->data = data;
8564 +
8565 + return t;
8566 +
8567 + no_lock:
8568 + DWC_FREE(t->name);
8569 + no_name:
8570 + DWC_FREE(t);
8571 + return NULL;
8572 +}
8573 +
8574 +void DWC_TIMER_FREE(dwc_timer_t *timer)
8575 +{
8576 + dwc_irqflags_t flags;
8577 +
8578 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8579 +
8580 + if (timer->scheduled) {
8581 + del_timer(&timer->t);
8582 + timer->scheduled = 0;
8583 + }
8584 +
8585 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8586 + DWC_SPINLOCK_FREE(timer->lock);
8587 + DWC_FREE(timer->name);
8588 + DWC_FREE(timer);
8589 +}
8590 +
8591 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
8592 +{
8593 + dwc_irqflags_t flags;
8594 +
8595 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8596 +
8597 + if (!timer->scheduled) {
8598 + timer->scheduled = 1;
8599 + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
8600 + timer->t.expires = jiffies + msecs_to_jiffies(time);
8601 + add_timer(&timer->t);
8602 + } else {
8603 + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
8604 + mod_timer(&timer->t, jiffies + msecs_to_jiffies(time));
8605 + }
8606 +
8607 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8608 +}
8609 +
8610 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
8611 +{
8612 + del_timer(&timer->t);
8613 +}
8614 +
8615 +
8616 +/* Wait Queues */
8617 +
8618 +struct dwc_waitq {
8619 + wait_queue_head_t queue;
8620 + int abort;
8621 +};
8622 +
8623 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
8624 +{
8625 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
8626 +
8627 + if (!wq) {
8628 + DWC_ERROR("Cannot allocate memory for waitqueue\n");
8629 + return NULL;
8630 + }
8631 +
8632 + init_waitqueue_head(&wq->queue);
8633 + wq->abort = 0;
8634 + return wq;
8635 +}
8636 +
8637 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
8638 +{
8639 + DWC_FREE(wq);
8640 +}
8641 +
8642 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
8643 +{
8644 + int result = wait_event_interruptible(wq->queue,
8645 + cond(data) || wq->abort);
8646 + if (result == -ERESTARTSYS) {
8647 + wq->abort = 0;
8648 + return -DWC_E_RESTART;
8649 + }
8650 +
8651 + if (wq->abort == 1) {
8652 + wq->abort = 0;
8653 + return -DWC_E_ABORT;
8654 + }
8655 +
8656 + wq->abort = 0;
8657 +
8658 + if (result == 0) {
8659 + return 0;
8660 + }
8661 +
8662 + return -DWC_E_UNKNOWN;
8663 +}
8664 +
8665 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
8666 + void *data, int32_t msecs)
8667 +{
8668 + int32_t tmsecs;
8669 + int result = wait_event_interruptible_timeout(wq->queue,
8670 + cond(data) || wq->abort,
8671 + msecs_to_jiffies(msecs));
8672 + if (result == -ERESTARTSYS) {
8673 + wq->abort = 0;
8674 + return -DWC_E_RESTART;
8675 + }
8676 +
8677 + if (wq->abort == 1) {
8678 + wq->abort = 0;
8679 + return -DWC_E_ABORT;
8680 + }
8681 +
8682 + wq->abort = 0;
8683 +
8684 + if (result > 0) {
8685 + tmsecs = jiffies_to_msecs(result);
8686 + if (!tmsecs) {
8687 + return 1;
8688 + }
8689 +
8690 + return tmsecs;
8691 + }
8692 +
8693 + if (result == 0) {
8694 + return -DWC_E_TIMEOUT;
8695 + }
8696 +
8697 + return -DWC_E_UNKNOWN;
8698 +}
8699 +
8700 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
8701 +{
8702 + wq->abort = 0;
8703 + wake_up_interruptible(&wq->queue);
8704 +}
8705 +
8706 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
8707 +{
8708 + wq->abort = 1;
8709 + wake_up_interruptible(&wq->queue);
8710 +}
8711 +
8712 +
8713 +/* Threading */
8714 +
8715 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
8716 +{
8717 + struct task_struct *thread = kthread_run(func, data, name);
8718 +
8719 + if (thread == ERR_PTR(-ENOMEM)) {
8720 + return NULL;
8721 + }
8722 +
8723 + return (dwc_thread_t *)thread;
8724 +}
8725 +
8726 +int DWC_THREAD_STOP(dwc_thread_t *thread)
8727 +{
8728 + return kthread_stop((struct task_struct *)thread);
8729 +}
8730 +
8731 +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
8732 +{
8733 + return kthread_should_stop();
8734 +}
8735 +
8736 +
8737 +/* tasklets
8738 + - run in interrupt context (cannot sleep)
8739 + - each tasklet runs on a single CPU
8740 + - different tasklets can be running simultaneously on different CPUs
8741 + */
8742 +struct dwc_tasklet {
8743 + struct tasklet_struct t;
8744 + dwc_tasklet_callback_t cb;
8745 + void *data;
8746 +};
8747 +
8748 +static void tasklet_callback(unsigned long data)
8749 +{
8750 + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
8751 + t->cb(t->data);
8752 +}
8753 +
8754 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
8755 +{
8756 + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
8757 +
8758 + if (t) {
8759 + t->cb = cb;
8760 + t->data = data;
8761 + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
8762 + } else {
8763 + DWC_ERROR("Cannot allocate memory for tasklet\n");
8764 + }
8765 +
8766 + return t;
8767 +}
8768 +
8769 +void DWC_TASK_FREE(dwc_tasklet_t *task)
8770 +{
8771 + DWC_FREE(task);
8772 +}
8773 +
8774 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
8775 +{
8776 + tasklet_schedule(&task->t);
8777 +}
8778 +
8779 +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
8780 +{
8781 + tasklet_hi_schedule(&task->t);
8782 +}
8783 +
8784 +
8785 +/* workqueues
8786 + - run in process context (can sleep)
8787 + */
8788 +typedef struct work_container {
8789 + dwc_work_callback_t cb;
8790 + void *data;
8791 + dwc_workq_t *wq;
8792 + char *name;
8793 +
8794 +#ifdef DEBUG
8795 + DWC_CIRCLEQ_ENTRY(work_container) entry;
8796 +#endif
8797 + struct delayed_work work;
8798 +} work_container_t;
8799 +
8800 +#ifdef DEBUG
8801 +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
8802 +#endif
8803 +
8804 +struct dwc_workq {
8805 + struct workqueue_struct *wq;
8806 + dwc_spinlock_t *lock;
8807 + dwc_waitq_t *waitq;
8808 + int pending;
8809 +
8810 +#ifdef DEBUG
8811 + struct work_container_queue entries;
8812 +#endif
8813 +};
8814 +
8815 +static void do_work(struct work_struct *work)
8816 +{
8817 + dwc_irqflags_t flags;
8818 + struct delayed_work *dw = container_of(work, struct delayed_work, work);
8819 + work_container_t *container = container_of(dw, struct work_container, work);
8820 + dwc_workq_t *wq = container->wq;
8821 +
8822 + container->cb(container->data);
8823 +
8824 +#ifdef DEBUG
8825 + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
8826 +#endif
8827 + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
8828 + if (container->name) {
8829 + DWC_FREE(container->name);
8830 + }
8831 + DWC_FREE(container);
8832 +
8833 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8834 + wq->pending--;
8835 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8836 + DWC_WAITQ_TRIGGER(wq->waitq);
8837 +}
8838 +
8839 +static int work_done(void *data)
8840 +{
8841 + dwc_workq_t *workq = (dwc_workq_t *)data;
8842 + return workq->pending == 0;
8843 +}
8844 +
8845 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
8846 +{
8847 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
8848 +}
8849 +
8850 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
8851 +{
8852 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
8853 +
8854 + if (!wq) {
8855 + return NULL;
8856 + }
8857 +
8858 + wq->wq = create_singlethread_workqueue(name);
8859 + if (!wq->wq) {
8860 + goto no_wq;
8861 + }
8862 +
8863 + wq->pending = 0;
8864 +
8865 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
8866 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
8867 +#else
8868 + wq->lock = DWC_SPINLOCK_ALLOC();
8869 +#endif
8870 + if (!wq->lock) {
8871 + goto no_lock;
8872 + }
8873 +
8874 + wq->waitq = DWC_WAITQ_ALLOC();
8875 + if (!wq->waitq) {
8876 + goto no_waitq;
8877 + }
8878 +
8879 +#ifdef DEBUG
8880 + DWC_CIRCLEQ_INIT(&wq->entries);
8881 +#endif
8882 + return wq;
8883 +
8884 + no_waitq:
8885 + DWC_SPINLOCK_FREE(wq->lock);
8886 + no_lock:
8887 + destroy_workqueue(wq->wq);
8888 + no_wq:
8889 + DWC_FREE(wq);
8890 +
8891 + return NULL;
8892 +}
8893 +
8894 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
8895 +{
8896 +#ifdef DEBUG
8897 + if (wq->pending != 0) {
8898 + struct work_container *wc;
8899 + DWC_ERROR("Destroying work queue with pending work");
8900 + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
8901 + DWC_ERROR("Work %s still pending", wc->name);
8902 + }
8903 + }
8904 +#endif
8905 + destroy_workqueue(wq->wq);
8906 + DWC_SPINLOCK_FREE(wq->lock);
8907 + DWC_WAITQ_FREE(wq->waitq);
8908 + DWC_FREE(wq);
8909 +}
8910 +
8911 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
8912 + char *format, ...)
8913 +{
8914 + dwc_irqflags_t flags;
8915 + work_container_t *container;
8916 + static char name[128];
8917 + va_list args;
8918 +
8919 + va_start(args, format);
8920 + DWC_VSNPRINTF(name, 128, format, args);
8921 + va_end(args);
8922 +
8923 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8924 + wq->pending++;
8925 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8926 + DWC_WAITQ_TRIGGER(wq->waitq);
8927 +
8928 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
8929 + if (!container) {
8930 + DWC_ERROR("Cannot allocate memory for container\n");
8931 + return;
8932 + }
8933 +
8934 + container->name = DWC_STRDUP(name);
8935 + if (!container->name) {
8936 + DWC_ERROR("Cannot allocate memory for container->name\n");
8937 + DWC_FREE(container);
8938 + return;
8939 + }
8940 +
8941 + container->cb = cb;
8942 + container->data = data;
8943 + container->wq = wq;
8944 + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
8945 + INIT_WORK(&container->work.work, do_work);
8946 +
8947 +#ifdef DEBUG
8948 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
8949 +#endif
8950 + queue_work(wq->wq, &container->work.work);
8951 +}
8952 +
8953 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
8954 + void *data, uint32_t time, char *format, ...)
8955 +{
8956 + dwc_irqflags_t flags;
8957 + work_container_t *container;
8958 + static char name[128];
8959 + va_list args;
8960 +
8961 + va_start(args, format);
8962 + DWC_VSNPRINTF(name, 128, format, args);
8963 + va_end(args);
8964 +
8965 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8966 + wq->pending++;
8967 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8968 + DWC_WAITQ_TRIGGER(wq->waitq);
8969 +
8970 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
8971 + if (!container) {
8972 + DWC_ERROR("Cannot allocate memory for container\n");
8973 + return;
8974 + }
8975 +
8976 + container->name = DWC_STRDUP(name);
8977 + if (!container->name) {
8978 + DWC_ERROR("Cannot allocate memory for container->name\n");
8979 + DWC_FREE(container);
8980 + return;
8981 + }
8982 +
8983 + container->cb = cb;
8984 + container->data = data;
8985 + container->wq = wq;
8986 + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
8987 + INIT_DELAYED_WORK(&container->work, do_work);
8988 +
8989 +#ifdef DEBUG
8990 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
8991 +#endif
8992 + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
8993 +}
8994 +
8995 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
8996 +{
8997 + return wq->pending;
8998 +}
8999 +
9000 +
9001 +#ifdef DWC_LIBMODULE
9002 +
9003 +#ifdef DWC_CCLIB
9004 +/* CC */
9005 +EXPORT_SYMBOL(dwc_cc_if_alloc);
9006 +EXPORT_SYMBOL(dwc_cc_if_free);
9007 +EXPORT_SYMBOL(dwc_cc_clear);
9008 +EXPORT_SYMBOL(dwc_cc_add);
9009 +EXPORT_SYMBOL(dwc_cc_remove);
9010 +EXPORT_SYMBOL(dwc_cc_change);
9011 +EXPORT_SYMBOL(dwc_cc_data_for_save);
9012 +EXPORT_SYMBOL(dwc_cc_restore_from_data);
9013 +EXPORT_SYMBOL(dwc_cc_match_chid);
9014 +EXPORT_SYMBOL(dwc_cc_match_cdid);
9015 +EXPORT_SYMBOL(dwc_cc_ck);
9016 +EXPORT_SYMBOL(dwc_cc_chid);
9017 +EXPORT_SYMBOL(dwc_cc_cdid);
9018 +EXPORT_SYMBOL(dwc_cc_name);
9019 +#endif /* DWC_CCLIB */
9020 +
9021 +#ifdef DWC_CRYPTOLIB
9022 +# ifndef CONFIG_MACH_IPMATE
9023 +/* Modpow */
9024 +EXPORT_SYMBOL(dwc_modpow);
9025 +
9026 +/* DH */
9027 +EXPORT_SYMBOL(dwc_dh_modpow);
9028 +EXPORT_SYMBOL(dwc_dh_derive_keys);
9029 +EXPORT_SYMBOL(dwc_dh_pk);
9030 +# endif /* CONFIG_MACH_IPMATE */
9031 +
9032 +/* Crypto */
9033 +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
9034 +EXPORT_SYMBOL(dwc_wusb_cmf);
9035 +EXPORT_SYMBOL(dwc_wusb_prf);
9036 +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
9037 +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
9038 +EXPORT_SYMBOL(dwc_wusb_gen_key);
9039 +EXPORT_SYMBOL(dwc_wusb_gen_mic);
9040 +#endif /* DWC_CRYPTOLIB */
9041 +
9042 +/* Notification */
9043 +#ifdef DWC_NOTIFYLIB
9044 +EXPORT_SYMBOL(dwc_alloc_notification_manager);
9045 +EXPORT_SYMBOL(dwc_free_notification_manager);
9046 +EXPORT_SYMBOL(dwc_register_notifier);
9047 +EXPORT_SYMBOL(dwc_unregister_notifier);
9048 +EXPORT_SYMBOL(dwc_add_observer);
9049 +EXPORT_SYMBOL(dwc_remove_observer);
9050 +EXPORT_SYMBOL(dwc_notify);
9051 +#endif
9052 +
9053 +/* Memory Debugging Routines */
9054 +#ifdef DWC_DEBUG_MEMORY
9055 +EXPORT_SYMBOL(dwc_alloc_debug);
9056 +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
9057 +EXPORT_SYMBOL(dwc_free_debug);
9058 +EXPORT_SYMBOL(dwc_dma_alloc_debug);
9059 +EXPORT_SYMBOL(dwc_dma_free_debug);
9060 +#endif
9061 +
9062 +EXPORT_SYMBOL(DWC_MEMSET);
9063 +EXPORT_SYMBOL(DWC_MEMCPY);
9064 +EXPORT_SYMBOL(DWC_MEMMOVE);
9065 +EXPORT_SYMBOL(DWC_MEMCMP);
9066 +EXPORT_SYMBOL(DWC_STRNCMP);
9067 +EXPORT_SYMBOL(DWC_STRCMP);
9068 +EXPORT_SYMBOL(DWC_STRLEN);
9069 +EXPORT_SYMBOL(DWC_STRCPY);
9070 +EXPORT_SYMBOL(DWC_STRDUP);
9071 +EXPORT_SYMBOL(DWC_ATOI);
9072 +EXPORT_SYMBOL(DWC_ATOUI);
9073 +
9074 +#ifdef DWC_UTFLIB
9075 +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
9076 +#endif /* DWC_UTFLIB */
9077 +
9078 +EXPORT_SYMBOL(DWC_IN_IRQ);
9079 +EXPORT_SYMBOL(DWC_IN_BH);
9080 +EXPORT_SYMBOL(DWC_VPRINTF);
9081 +EXPORT_SYMBOL(DWC_VSNPRINTF);
9082 +EXPORT_SYMBOL(DWC_PRINTF);
9083 +EXPORT_SYMBOL(DWC_SPRINTF);
9084 +EXPORT_SYMBOL(DWC_SNPRINTF);
9085 +EXPORT_SYMBOL(__DWC_WARN);
9086 +EXPORT_SYMBOL(__DWC_ERROR);
9087 +EXPORT_SYMBOL(DWC_EXCEPTION);
9088 +
9089 +#ifdef DEBUG
9090 +EXPORT_SYMBOL(__DWC_DEBUG);
9091 +#endif
9092 +
9093 +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
9094 +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
9095 +EXPORT_SYMBOL(__DWC_DMA_FREE);
9096 +EXPORT_SYMBOL(__DWC_ALLOC);
9097 +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
9098 +EXPORT_SYMBOL(__DWC_FREE);
9099 +
9100 +#ifdef DWC_CRYPTOLIB
9101 +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
9102 +EXPORT_SYMBOL(DWC_AES_CBC);
9103 +EXPORT_SYMBOL(DWC_SHA256);
9104 +EXPORT_SYMBOL(DWC_HMAC_SHA256);
9105 +#endif
9106 +
9107 +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
9108 +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
9109 +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
9110 +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
9111 +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
9112 +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
9113 +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
9114 +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
9115 +EXPORT_SYMBOL(DWC_READ_REG32);
9116 +EXPORT_SYMBOL(DWC_WRITE_REG32);
9117 +EXPORT_SYMBOL(DWC_MODIFY_REG32);
9118 +
9119 +#if 0
9120 +EXPORT_SYMBOL(DWC_READ_REG64);
9121 +EXPORT_SYMBOL(DWC_WRITE_REG64);
9122 +EXPORT_SYMBOL(DWC_MODIFY_REG64);
9123 +#endif
9124 +
9125 +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
9126 +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
9127 +EXPORT_SYMBOL(DWC_SPINLOCK);
9128 +EXPORT_SYMBOL(DWC_SPINUNLOCK);
9129 +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
9130 +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
9131 +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
9132 +
9133 +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
9134 +EXPORT_SYMBOL(DWC_MUTEX_FREE);
9135 +#endif
9136 +
9137 +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
9138 +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
9139 +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
9140 +EXPORT_SYMBOL(DWC_UDELAY);
9141 +EXPORT_SYMBOL(DWC_MDELAY);
9142 +EXPORT_SYMBOL(DWC_MSLEEP);
9143 +EXPORT_SYMBOL(DWC_TIME);
9144 +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
9145 +EXPORT_SYMBOL(DWC_TIMER_FREE);
9146 +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
9147 +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
9148 +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
9149 +EXPORT_SYMBOL(DWC_WAITQ_FREE);
9150 +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
9151 +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
9152 +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
9153 +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
9154 +EXPORT_SYMBOL(DWC_THREAD_RUN);
9155 +EXPORT_SYMBOL(DWC_THREAD_STOP);
9156 +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
9157 +EXPORT_SYMBOL(DWC_TASK_ALLOC);
9158 +EXPORT_SYMBOL(DWC_TASK_FREE);
9159 +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
9160 +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
9161 +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
9162 +EXPORT_SYMBOL(DWC_WORKQ_FREE);
9163 +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
9164 +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
9165 +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
9166 +
9167 +static int dwc_common_port_init_module(void)
9168 +{
9169 + int result = 0;
9170 +
9171 + printk(KERN_DEBUG "Module dwc_common_port init\n" );
9172 +
9173 +#ifdef DWC_DEBUG_MEMORY
9174 + result = dwc_memory_debug_start(NULL);
9175 + if (result) {
9176 + printk(KERN_ERR
9177 + "dwc_memory_debug_start() failed with error %d\n",
9178 + result);
9179 + return result;
9180 + }
9181 +#endif
9182 +
9183 +#ifdef DWC_NOTIFYLIB
9184 + result = dwc_alloc_notification_manager(NULL, NULL);
9185 + if (result) {
9186 + printk(KERN_ERR
9187 + "dwc_alloc_notification_manager() failed with error %d\n",
9188 + result);
9189 + return result;
9190 + }
9191 +#endif
9192 + return result;
9193 +}
9194 +
9195 +static void dwc_common_port_exit_module(void)
9196 +{
9197 + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
9198 +
9199 +#ifdef DWC_NOTIFYLIB
9200 + dwc_free_notification_manager();
9201 +#endif
9202 +
9203 +#ifdef DWC_DEBUG_MEMORY
9204 + dwc_memory_debug_stop();
9205 +#endif
9206 +}
9207 +
9208 +module_init(dwc_common_port_init_module);
9209 +module_exit(dwc_common_port_exit_module);
9210 +
9211 +MODULE_DESCRIPTION("DWC Common Library - Portable version");
9212 +MODULE_AUTHOR("Synopsys Inc.");
9213 +MODULE_LICENSE ("GPL");
9214 +
9215 +#endif /* DWC_LIBMODULE */
9216 --- /dev/null
9217 +++ b/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
9218 @@ -0,0 +1,1275 @@
9219 +#include "dwc_os.h"
9220 +#include "dwc_list.h"
9221 +
9222 +#ifdef DWC_CCLIB
9223 +# include "dwc_cc.h"
9224 +#endif
9225 +
9226 +#ifdef DWC_CRYPTOLIB
9227 +# include "dwc_modpow.h"
9228 +# include "dwc_dh.h"
9229 +# include "dwc_crypto.h"
9230 +#endif
9231 +
9232 +#ifdef DWC_NOTIFYLIB
9233 +# include "dwc_notifier.h"
9234 +#endif
9235 +
9236 +/* OS-Level Implementations */
9237 +
9238 +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
9239 +
9240 +
9241 +/* MISC */
9242 +
9243 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
9244 +{
9245 + return memset(dest, byte, size);
9246 +}
9247 +
9248 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
9249 +{
9250 + return memcpy(dest, src, size);
9251 +}
9252 +
9253 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
9254 +{
9255 + bcopy(src, dest, size);
9256 + return dest;
9257 +}
9258 +
9259 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
9260 +{
9261 + return memcmp(m1, m2, size);
9262 +}
9263 +
9264 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
9265 +{
9266 + return strncmp(s1, s2, size);
9267 +}
9268 +
9269 +int DWC_STRCMP(void *s1, void *s2)
9270 +{
9271 + return strcmp(s1, s2);
9272 +}
9273 +
9274 +int DWC_STRLEN(char const *str)
9275 +{
9276 + return strlen(str);
9277 +}
9278 +
9279 +char *DWC_STRCPY(char *to, char const *from)
9280 +{
9281 + return strcpy(to, from);
9282 +}
9283 +
9284 +char *DWC_STRDUP(char const *str)
9285 +{
9286 + int len = DWC_STRLEN(str) + 1;
9287 + char *new = DWC_ALLOC_ATOMIC(len);
9288 +
9289 + if (!new) {
9290 + return NULL;
9291 + }
9292 +
9293 + DWC_MEMCPY(new, str, len);
9294 + return new;
9295 +}
9296 +
9297 +int DWC_ATOI(char *str, int32_t *value)
9298 +{
9299 + char *end = NULL;
9300 +
9301 + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
9302 + * should be equivalent on 2's complement machines
9303 + */
9304 + *value = strtoul(str, &end, 0);
9305 + if (*end == '\0') {
9306 + return 0;
9307 + }
9308 +
9309 + return -1;
9310 +}
9311 +
9312 +int DWC_ATOUI(char *str, uint32_t *value)
9313 +{
9314 + char *end = NULL;
9315 +
9316 + *value = strtoul(str, &end, 0);
9317 + if (*end == '\0') {
9318 + return 0;
9319 + }
9320 +
9321 + return -1;
9322 +}
9323 +
9324 +
9325 +#ifdef DWC_UTFLIB
9326 +/* From usbstring.c */
9327 +
9328 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
9329 +{
9330 + int count = 0;
9331 + u8 c;
9332 + u16 uchar;
9333 +
9334 + /* this insists on correct encodings, though not minimal ones.
9335 + * BUT it currently rejects legit 4-byte UTF-8 code points,
9336 + * which need surrogate pairs. (Unicode 3.1 can use them.)
9337 + */
9338 + while (len != 0 && (c = (u8) *s++) != 0) {
9339 + if (unlikely(c & 0x80)) {
9340 + // 2-byte sequence:
9341 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
9342 + if ((c & 0xe0) == 0xc0) {
9343 + uchar = (c & 0x1f) << 6;
9344 +
9345 + c = (u8) *s++;
9346 + if ((c & 0xc0) != 0xc0)
9347 + goto fail;
9348 + c &= 0x3f;
9349 + uchar |= c;
9350 +
9351 + // 3-byte sequence (most CJKV characters):
9352 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
9353 + } else if ((c & 0xf0) == 0xe0) {
9354 + uchar = (c & 0x0f) << 12;
9355 +
9356 + c = (u8) *s++;
9357 + if ((c & 0xc0) != 0xc0)
9358 + goto fail;
9359 + c &= 0x3f;
9360 + uchar |= c << 6;
9361 +
9362 + c = (u8) *s++;
9363 + if ((c & 0xc0) != 0xc0)
9364 + goto fail;
9365 + c &= 0x3f;
9366 + uchar |= c;
9367 +
9368 + /* no bogus surrogates */
9369 + if (0xd800 <= uchar && uchar <= 0xdfff)
9370 + goto fail;
9371 +
9372 + // 4-byte sequence (surrogate pairs, currently rare):
9373 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
9374 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
9375 + // (uuuuu = wwww + 1)
9376 + // FIXME accept the surrogate code points (only)
9377 + } else
9378 + goto fail;
9379 + } else
9380 + uchar = c;
9381 + put_unaligned (cpu_to_le16 (uchar), cp++);
9382 + count++;
9383 + len--;
9384 + }
9385 + return count;
9386 +fail:
9387 + return -1;
9388 +}
9389 +
9390 +#endif /* DWC_UTFLIB */
9391 +
9392 +
9393 +/* dwc_debug.h */
9394 +
9395 +dwc_bool_t DWC_IN_IRQ(void)
9396 +{
9397 +// return in_irq();
9398 + return 0;
9399 +}
9400 +
9401 +dwc_bool_t DWC_IN_BH(void)
9402 +{
9403 +// return in_softirq();
9404 + return 0;
9405 +}
9406 +
9407 +void DWC_VPRINTF(char *format, va_list args)
9408 +{
9409 + vprintf(format, args);
9410 +}
9411 +
9412 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
9413 +{
9414 + return vsnprintf(str, size, format, args);
9415 +}
9416 +
9417 +void DWC_PRINTF(char *format, ...)
9418 +{
9419 + va_list args;
9420 +
9421 + va_start(args, format);
9422 + DWC_VPRINTF(format, args);
9423 + va_end(args);
9424 +}
9425 +
9426 +int DWC_SPRINTF(char *buffer, char *format, ...)
9427 +{
9428 + int retval;
9429 + va_list args;
9430 +
9431 + va_start(args, format);
9432 + retval = vsprintf(buffer, format, args);
9433 + va_end(args);
9434 + return retval;
9435 +}
9436 +
9437 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
9438 +{
9439 + int retval;
9440 + va_list args;
9441 +
9442 + va_start(args, format);
9443 + retval = vsnprintf(buffer, size, format, args);
9444 + va_end(args);
9445 + return retval;
9446 +}
9447 +
9448 +void __DWC_WARN(char *format, ...)
9449 +{
9450 + va_list args;
9451 +
9452 + va_start(args, format);
9453 + DWC_VPRINTF(format, args);
9454 + va_end(args);
9455 +}
9456 +
9457 +void __DWC_ERROR(char *format, ...)
9458 +{
9459 + va_list args;
9460 +
9461 + va_start(args, format);
9462 + DWC_VPRINTF(format, args);
9463 + va_end(args);
9464 +}
9465 +
9466 +void DWC_EXCEPTION(char *format, ...)
9467 +{
9468 + va_list args;
9469 +
9470 + va_start(args, format);
9471 + DWC_VPRINTF(format, args);
9472 + va_end(args);
9473 +// BUG_ON(1); ???
9474 +}
9475 +
9476 +#ifdef DEBUG
9477 +void __DWC_DEBUG(char *format, ...)
9478 +{
9479 + va_list args;
9480 +
9481 + va_start(args, format);
9482 + DWC_VPRINTF(format, args);
9483 + va_end(args);
9484 +}
9485 +#endif
9486 +
9487 +
9488 +/* dwc_mem.h */
9489 +
9490 +#if 0
9491 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
9492 + uint32_t align,
9493 + uint32_t alloc)
9494 +{
9495 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
9496 + size, align, alloc);
9497 + return (dwc_pool_t *)pool;
9498 +}
9499 +
9500 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
9501 +{
9502 + dma_pool_destroy((struct dma_pool *)pool);
9503 +}
9504 +
9505 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
9506 +{
9507 +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
9508 + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
9509 +}
9510 +
9511 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
9512 +{
9513 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
9514 + memset(..);
9515 +}
9516 +
9517 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
9518 +{
9519 + dma_pool_free(pool, vaddr, daddr);
9520 +}
9521 +#endif
9522 +
9523 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
9524 +{
9525 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
9526 + int error;
9527 +
9528 + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
9529 + sizeof(dma->segs) / sizeof(dma->segs[0]),
9530 + &dma->nsegs, BUS_DMA_NOWAIT);
9531 + if (error) {
9532 + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
9533 + (uintmax_t)size, error);
9534 + goto fail_0;
9535 + }
9536 +
9537 + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
9538 + (caddr_t *)&dma->dma_vaddr,
9539 + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
9540 + if (error) {
9541 + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
9542 + goto fail_1;
9543 + }
9544 +
9545 + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
9546 + BUS_DMA_NOWAIT, &dma->dma_map);
9547 + if (error) {
9548 + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
9549 + goto fail_2;
9550 + }
9551 +
9552 + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
9553 + size, NULL, BUS_DMA_NOWAIT);
9554 + if (error) {
9555 + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
9556 + goto fail_3;
9557 + }
9558 +
9559 + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
9560 + *dma_addr = dma->dma_paddr;
9561 + return dma->dma_vaddr;
9562 +
9563 +fail_3:
9564 + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
9565 +fail_2:
9566 + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
9567 +fail_1:
9568 + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
9569 +fail_0:
9570 + dma->dma_map = NULL;
9571 + dma->dma_vaddr = NULL;
9572 + dma->nsegs = 0;
9573 +
9574 + return NULL;
9575 +}
9576 +
9577 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
9578 +{
9579 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
9580 +
9581 + if (dma->dma_map != NULL) {
9582 + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
9583 + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9584 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
9585 + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
9586 + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
9587 + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
9588 + dma->dma_paddr = 0;
9589 + dma->dma_map = NULL;
9590 + dma->dma_vaddr = NULL;
9591 + dma->nsegs = 0;
9592 + }
9593 +}
9594 +
9595 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
9596 +{
9597 + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
9598 +}
9599 +
9600 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
9601 +{
9602 + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
9603 +}
9604 +
9605 +void __DWC_FREE(void *mem_ctx, void *addr)
9606 +{
9607 + free(addr, M_DEVBUF);
9608 +}
9609 +
9610 +
9611 +#ifdef DWC_CRYPTOLIB
9612 +/* dwc_crypto.h */
9613 +
9614 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
9615 +{
9616 + get_random_bytes(buffer, length);
9617 +}
9618 +
9619 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
9620 +{
9621 + struct crypto_blkcipher *tfm;
9622 + struct blkcipher_desc desc;
9623 + struct scatterlist sgd;
9624 + struct scatterlist sgs;
9625 +
9626 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
9627 + if (tfm == NULL) {
9628 + printk("failed to load transform for aes CBC\n");
9629 + return -1;
9630 + }
9631 +
9632 + crypto_blkcipher_setkey(tfm, key, keylen);
9633 + crypto_blkcipher_set_iv(tfm, iv, 16);
9634 +
9635 + sg_init_one(&sgd, out, messagelen);
9636 + sg_init_one(&sgs, message, messagelen);
9637 +
9638 + desc.tfm = tfm;
9639 + desc.flags = 0;
9640 +
9641 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
9642 + crypto_free_blkcipher(tfm);
9643 + DWC_ERROR("AES CBC encryption failed");
9644 + return -1;
9645 + }
9646 +
9647 + crypto_free_blkcipher(tfm);
9648 + return 0;
9649 +}
9650 +
9651 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
9652 +{
9653 + struct crypto_hash *tfm;
9654 + struct hash_desc desc;
9655 + struct scatterlist sg;
9656 +
9657 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
9658 + if (IS_ERR(tfm)) {
9659 + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
9660 + return 0;
9661 + }
9662 + desc.tfm = tfm;
9663 + desc.flags = 0;
9664 +
9665 + sg_init_one(&sg, message, len);
9666 + crypto_hash_digest(&desc, &sg, len, out);
9667 + crypto_free_hash(tfm);
9668 +
9669 + return 1;
9670 +}
9671 +
9672 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
9673 + uint8_t *key, uint32_t keylen, uint8_t *out)
9674 +{
9675 + struct crypto_hash *tfm;
9676 + struct hash_desc desc;
9677 + struct scatterlist sg;
9678 +
9679 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
9680 + if (IS_ERR(tfm)) {
9681 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
9682 + return 0;
9683 + }
9684 + desc.tfm = tfm;
9685 + desc.flags = 0;
9686 +
9687 + sg_init_one(&sg, message, messagelen);
9688 + crypto_hash_setkey(tfm, key, keylen);
9689 + crypto_hash_digest(&desc, &sg, messagelen, out);
9690 + crypto_free_hash(tfm);
9691 +
9692 + return 1;
9693 +}
9694 +
9695 +#endif /* DWC_CRYPTOLIB */
9696 +
9697 +
9698 +/* Byte Ordering Conversions */
9699 +
9700 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
9701 +{
9702 +#ifdef __LITTLE_ENDIAN
9703 + return *p;
9704 +#else
9705 + uint8_t *u_p = (uint8_t *)p;
9706 +
9707 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9708 +#endif
9709 +}
9710 +
9711 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
9712 +{
9713 +#ifdef __BIG_ENDIAN
9714 + return *p;
9715 +#else
9716 + uint8_t *u_p = (uint8_t *)p;
9717 +
9718 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9719 +#endif
9720 +}
9721 +
9722 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
9723 +{
9724 +#ifdef __LITTLE_ENDIAN
9725 + return *p;
9726 +#else
9727 + uint8_t *u_p = (uint8_t *)p;
9728 +
9729 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9730 +#endif
9731 +}
9732 +
9733 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
9734 +{
9735 +#ifdef __BIG_ENDIAN
9736 + return *p;
9737 +#else
9738 + uint8_t *u_p = (uint8_t *)p;
9739 +
9740 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9741 +#endif
9742 +}
9743 +
9744 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
9745 +{
9746 +#ifdef __LITTLE_ENDIAN
9747 + return *p;
9748 +#else
9749 + uint8_t *u_p = (uint8_t *)p;
9750 + return (u_p[1] | (u_p[0] << 8));
9751 +#endif
9752 +}
9753 +
9754 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
9755 +{
9756 +#ifdef __BIG_ENDIAN
9757 + return *p;
9758 +#else
9759 + uint8_t *u_p = (uint8_t *)p;
9760 + return (u_p[1] | (u_p[0] << 8));
9761 +#endif
9762 +}
9763 +
9764 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
9765 +{
9766 +#ifdef __LITTLE_ENDIAN
9767 + return *p;
9768 +#else
9769 + uint8_t *u_p = (uint8_t *)p;
9770 + return (u_p[1] | (u_p[0] << 8));
9771 +#endif
9772 +}
9773 +
9774 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
9775 +{
9776 +#ifdef __BIG_ENDIAN
9777 + return *p;
9778 +#else
9779 + uint8_t *u_p = (uint8_t *)p;
9780 + return (u_p[1] | (u_p[0] << 8));
9781 +#endif
9782 +}
9783 +
9784 +
9785 +/* Registers */
9786 +
9787 +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
9788 +{
9789 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9790 + bus_size_t ior = (bus_size_t)reg;
9791 +
9792 + return bus_space_read_4(io->iot, io->ioh, ior);
9793 +}
9794 +
9795 +#if 0
9796 +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
9797 +{
9798 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9799 + bus_size_t ior = (bus_size_t)reg;
9800 +
9801 + return bus_space_read_8(io->iot, io->ioh, ior);
9802 +}
9803 +#endif
9804 +
9805 +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
9806 +{
9807 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9808 + bus_size_t ior = (bus_size_t)reg;
9809 +
9810 + bus_space_write_4(io->iot, io->ioh, ior, value);
9811 +}
9812 +
9813 +#if 0
9814 +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
9815 +{
9816 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9817 + bus_size_t ior = (bus_size_t)reg;
9818 +
9819 + bus_space_write_8(io->iot, io->ioh, ior, value);
9820 +}
9821 +#endif
9822 +
9823 +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
9824 + uint32_t set_mask)
9825 +{
9826 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9827 + bus_size_t ior = (bus_size_t)reg;
9828 +
9829 + bus_space_write_4(io->iot, io->ioh, ior,
9830 + (bus_space_read_4(io->iot, io->ioh, ior) &
9831 + ~clear_mask) | set_mask);
9832 +}
9833 +
9834 +#if 0
9835 +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
9836 + uint64_t set_mask)
9837 +{
9838 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9839 + bus_size_t ior = (bus_size_t)reg;
9840 +
9841 + bus_space_write_8(io->iot, io->ioh, ior,
9842 + (bus_space_read_8(io->iot, io->ioh, ior) &
9843 + ~clear_mask) | set_mask);
9844 +}
9845 +#endif
9846 +
9847 +
9848 +/* Locking */
9849 +
9850 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
9851 +{
9852 + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
9853 +
9854 + if (!sl) {
9855 + DWC_ERROR("Cannot allocate memory for spinlock");
9856 + return NULL;
9857 + }
9858 +
9859 + simple_lock_init(sl);
9860 + return (dwc_spinlock_t *)sl;
9861 +}
9862 +
9863 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
9864 +{
9865 + struct simplelock *sl = (struct simplelock *)lock;
9866 +
9867 + DWC_FREE(sl);
9868 +}
9869 +
9870 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
9871 +{
9872 + simple_lock((struct simplelock *)lock);
9873 +}
9874 +
9875 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
9876 +{
9877 + simple_unlock((struct simplelock *)lock);
9878 +}
9879 +
9880 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
9881 +{
9882 + simple_lock((struct simplelock *)lock);
9883 + *flags = splbio();
9884 +}
9885 +
9886 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
9887 +{
9888 + splx(flags);
9889 + simple_unlock((struct simplelock *)lock);
9890 +}
9891 +
9892 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
9893 +{
9894 + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
9895 +
9896 + if (!mutex) {
9897 + DWC_ERROR("Cannot allocate memory for mutex");
9898 + return NULL;
9899 + }
9900 +
9901 + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
9902 + return mutex;
9903 +}
9904 +
9905 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
9906 +#else
9907 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
9908 +{
9909 + DWC_FREE(mutex);
9910 +}
9911 +#endif
9912 +
9913 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
9914 +{
9915 + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
9916 +}
9917 +
9918 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
9919 +{
9920 + int status;
9921 +
9922 + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
9923 + return status == 0;
9924 +}
9925 +
9926 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
9927 +{
9928 + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
9929 +}
9930 +
9931 +
9932 +/* Timing */
9933 +
9934 +void DWC_UDELAY(uint32_t usecs)
9935 +{
9936 + DELAY(usecs);
9937 +}
9938 +
9939 +void DWC_MDELAY(uint32_t msecs)
9940 +{
9941 + do {
9942 + DELAY(1000);
9943 + } while (--msecs);
9944 +}
9945 +
9946 +void DWC_MSLEEP(uint32_t msecs)
9947 +{
9948 + struct timeval tv;
9949 +
9950 + tv.tv_sec = msecs / 1000;
9951 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
9952 + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
9953 +}
9954 +
9955 +uint32_t DWC_TIME(void)
9956 +{
9957 + struct timeval tv;
9958 +
9959 + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
9960 + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
9961 +}
9962 +
9963 +
9964 +/* Timers */
9965 +
9966 +struct dwc_timer {
9967 + struct callout t;
9968 + char *name;
9969 + dwc_spinlock_t *lock;
9970 + dwc_timer_callback_t cb;
9971 + void *data;
9972 +};
9973 +
9974 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
9975 +{
9976 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
9977 +
9978 + if (!t) {
9979 + DWC_ERROR("Cannot allocate memory for timer");
9980 + return NULL;
9981 + }
9982 +
9983 + callout_init(&t->t);
9984 +
9985 + t->name = DWC_STRDUP(name);
9986 + if (!t->name) {
9987 + DWC_ERROR("Cannot allocate memory for timer->name");
9988 + goto no_name;
9989 + }
9990 +
9991 + t->lock = DWC_SPINLOCK_ALLOC();
9992 + if (!t->lock) {
9993 + DWC_ERROR("Cannot allocate memory for timer->lock");
9994 + goto no_lock;
9995 + }
9996 +
9997 + t->cb = cb;
9998 + t->data = data;
9999 +
10000 + return t;
10001 +
10002 + no_lock:
10003 + DWC_FREE(t->name);
10004 + no_name:
10005 + DWC_FREE(t);
10006 +
10007 + return NULL;
10008 +}
10009 +
10010 +void DWC_TIMER_FREE(dwc_timer_t *timer)
10011 +{
10012 + callout_stop(&timer->t);
10013 + DWC_SPINLOCK_FREE(timer->lock);
10014 + DWC_FREE(timer->name);
10015 + DWC_FREE(timer);
10016 +}
10017 +
10018 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
10019 +{
10020 + struct timeval tv;
10021 +
10022 + tv.tv_sec = time / 1000;
10023 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
10024 + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
10025 +}
10026 +
10027 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
10028 +{
10029 + callout_stop(&timer->t);
10030 +}
10031 +
10032 +
10033 +/* Wait Queues */
10034 +
10035 +struct dwc_waitq {
10036 + struct simplelock lock;
10037 + int abort;
10038 +};
10039 +
10040 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
10041 +{
10042 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
10043 +
10044 + if (!wq) {
10045 + DWC_ERROR("Cannot allocate memory for waitqueue");
10046 + return NULL;
10047 + }
10048 +
10049 + simple_lock_init(&wq->lock);
10050 + wq->abort = 0;
10051 +
10052 + return wq;
10053 +}
10054 +
10055 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
10056 +{
10057 + DWC_FREE(wq);
10058 +}
10059 +
10060 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
10061 +{
10062 + int ipl;
10063 + int result = 0;
10064 +
10065 + simple_lock(&wq->lock);
10066 + ipl = splbio();
10067 +
10068 + /* Skip the sleep if already aborted or triggered */
10069 + if (!wq->abort && !cond(data)) {
10070 + splx(ipl);
10071 + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
10072 + ipl = splbio();
10073 + }
10074 +
10075 + if (result == 0) { // awoken
10076 + if (wq->abort) {
10077 + wq->abort = 0;
10078 + result = -DWC_E_ABORT;
10079 + } else {
10080 + result = 0;
10081 + }
10082 +
10083 + splx(ipl);
10084 + simple_unlock(&wq->lock);
10085 + } else {
10086 + wq->abort = 0;
10087 + splx(ipl);
10088 + simple_unlock(&wq->lock);
10089 +
10090 + if (result == ERESTART) { // signaled - restart
10091 + result = -DWC_E_RESTART;
10092 + } else { // signaled - must be EINTR
10093 + result = -DWC_E_ABORT;
10094 + }
10095 + }
10096 +
10097 + return result;
10098 +}
10099 +
10100 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
10101 + void *data, int32_t msecs)
10102 +{
10103 + struct timeval tv, tv1, tv2;
10104 + int ipl;
10105 + int result = 0;
10106 +
10107 + tv.tv_sec = msecs / 1000;
10108 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
10109 +
10110 + simple_lock(&wq->lock);
10111 + ipl = splbio();
10112 +
10113 + /* Skip the sleep if already aborted or triggered */
10114 + if (!wq->abort && !cond(data)) {
10115 + splx(ipl);
10116 + getmicrouptime(&tv1);
10117 + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
10118 + getmicrouptime(&tv2);
10119 + ipl = splbio();
10120 + }
10121 +
10122 + if (result == 0) { // awoken
10123 + if (wq->abort) {
10124 + wq->abort = 0;
10125 + splx(ipl);
10126 + simple_unlock(&wq->lock);
10127 + result = -DWC_E_ABORT;
10128 + } else {
10129 + splx(ipl);
10130 + simple_unlock(&wq->lock);
10131 +
10132 + tv2.tv_usec -= tv1.tv_usec;
10133 + if (tv2.tv_usec < 0) {
10134 + tv2.tv_usec += 1000000;
10135 + tv2.tv_sec--;
10136 + }
10137 +
10138 + tv2.tv_sec -= tv1.tv_sec;
10139 + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
10140 + result = msecs - result;
10141 + if (result <= 0)
10142 + result = 1;
10143 + }
10144 + } else {
10145 + wq->abort = 0;
10146 + splx(ipl);
10147 + simple_unlock(&wq->lock);
10148 +
10149 + if (result == ERESTART) { // signaled - restart
10150 + result = -DWC_E_RESTART;
10151 +
10152 + } else if (result == EINTR) { // signaled - interrupt
10153 + result = -DWC_E_ABORT;
10154 +
10155 + } else { // timed out
10156 + result = -DWC_E_TIMEOUT;
10157 + }
10158 + }
10159 +
10160 + return result;
10161 +}
10162 +
10163 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
10164 +{
10165 + wakeup(wq);
10166 +}
10167 +
10168 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
10169 +{
10170 + int ipl;
10171 +
10172 + simple_lock(&wq->lock);
10173 + ipl = splbio();
10174 + wq->abort = 1;
10175 + wakeup(wq);
10176 + splx(ipl);
10177 + simple_unlock(&wq->lock);
10178 +}
10179 +
10180 +
10181 +/* Threading */
10182 +
10183 +struct dwc_thread {
10184 + struct proc *proc;
10185 + int abort;
10186 +};
10187 +
10188 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
10189 +{
10190 + int retval;
10191 + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
10192 +
10193 + if (!thread) {
10194 + return NULL;
10195 + }
10196 +
10197 + thread->abort = 0;
10198 + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
10199 + "%s", name);
10200 + if (retval) {
10201 + DWC_FREE(thread);
10202 + return NULL;
10203 + }
10204 +
10205 + return thread;
10206 +}
10207 +
10208 +int DWC_THREAD_STOP(dwc_thread_t *thread)
10209 +{
10210 + int retval;
10211 +
10212 + thread->abort = 1;
10213 + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
10214 +
10215 + if (retval == 0) {
10216 + /* DWC_THREAD_EXIT() will free the thread struct */
10217 + return 0;
10218 + }
10219 +
10220 + /* NOTE: We leak the thread struct if thread doesn't die */
10221 +
10222 + if (retval == EWOULDBLOCK) {
10223 + return -DWC_E_TIMEOUT;
10224 + }
10225 +
10226 + return -DWC_E_UNKNOWN;
10227 +}
10228 +
10229 +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
10230 +{
10231 + return thread->abort;
10232 +}
10233 +
10234 +void DWC_THREAD_EXIT(dwc_thread_t *thread)
10235 +{
10236 + wakeup(&thread->abort);
10237 + DWC_FREE(thread);
10238 + kthread_exit(0);
10239 +}
10240 +
10241 +/* tasklets
10242 + - Runs in interrupt context (cannot sleep)
10243 + - Each tasklet runs on a single CPU
10244 + - Different tasklets can be running simultaneously on different CPUs
10245 + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
10246 + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
10247 + */
10248 +struct dwc_tasklet {
10249 + dwc_tasklet_callback_t cb;
10250 + void *data;
10251 +};
10252 +
10253 +static void tasklet_callback(void *data)
10254 +{
10255 + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
10256 +
10257 + task->cb(task->data);
10258 +}
10259 +
10260 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
10261 +{
10262 + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
10263 +
10264 + if (task) {
10265 + task->cb = cb;
10266 + task->data = data;
10267 + } else {
10268 + DWC_ERROR("Cannot allocate memory for tasklet");
10269 + }
10270 +
10271 + return task;
10272 +}
10273 +
10274 +void DWC_TASK_FREE(dwc_tasklet_t *task)
10275 +{
10276 + DWC_FREE(task);
10277 +}
10278 +
10279 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
10280 +{
10281 + tasklet_callback(task);
10282 +}
10283 +
10284 +
10285 +/* workqueues
10286 + - Runs in process context (can sleep)
10287 + */
10288 +typedef struct work_container {
10289 + dwc_work_callback_t cb;
10290 + void *data;
10291 + dwc_workq_t *wq;
10292 + char *name;
10293 + int hz;
10294 + struct work task;
10295 +} work_container_t;
10296 +
10297 +struct dwc_workq {
10298 + struct workqueue *taskq;
10299 + dwc_spinlock_t *lock;
10300 + dwc_waitq_t *waitq;
10301 + int pending;
10302 + struct work_container *container;
10303 +};
10304 +
10305 +static void do_work(struct work *task, void *data)
10306 +{
10307 + dwc_workq_t *wq = (dwc_workq_t *)data;
10308 + work_container_t *container = wq->container;
10309 + dwc_irqflags_t flags;
10310 +
10311 + if (container->hz) {
10312 + tsleep(container, 0, "dw3wrk", container->hz);
10313 + }
10314 +
10315 + container->cb(container->data);
10316 + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
10317 +
10318 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10319 + if (container->name)
10320 + DWC_FREE(container->name);
10321 + DWC_FREE(container);
10322 + wq->pending--;
10323 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10324 + DWC_WAITQ_TRIGGER(wq->waitq);
10325 +}
10326 +
10327 +static int work_done(void *data)
10328 +{
10329 + dwc_workq_t *workq = (dwc_workq_t *)data;
10330 +
10331 + return workq->pending == 0;
10332 +}
10333 +
10334 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
10335 +{
10336 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
10337 +}
10338 +
10339 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
10340 +{
10341 + int result;
10342 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
10343 +
10344 + if (!wq) {
10345 + DWC_ERROR("Cannot allocate memory for workqueue");
10346 + return NULL;
10347 + }
10348 +
10349 + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
10350 + IPL_BIO, 0);
10351 + if (result) {
10352 + DWC_ERROR("Cannot create workqueue");
10353 + goto no_taskq;
10354 + }
10355 +
10356 + wq->pending = 0;
10357 +
10358 + wq->lock = DWC_SPINLOCK_ALLOC();
10359 + if (!wq->lock) {
10360 + DWC_ERROR("Cannot allocate memory for spinlock");
10361 + goto no_lock;
10362 + }
10363 +
10364 + wq->waitq = DWC_WAITQ_ALLOC();
10365 + if (!wq->waitq) {
10366 + DWC_ERROR("Cannot allocate memory for waitqueue");
10367 + goto no_waitq;
10368 + }
10369 +
10370 + return wq;
10371 +
10372 + no_waitq:
10373 + DWC_SPINLOCK_FREE(wq->lock);
10374 + no_lock:
10375 + workqueue_destroy(wq->taskq);
10376 + no_taskq:
10377 + DWC_FREE(wq);
10378 +
10379 + return NULL;
10380 +}
10381 +
10382 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
10383 +{
10384 +#ifdef DEBUG
10385 + dwc_irqflags_t flags;
10386 +
10387 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10388 +
10389 + if (wq->pending != 0) {
10390 + struct work_container *container = wq->container;
10391 +
10392 + DWC_ERROR("Destroying work queue with pending work");
10393 +
10394 + if (container && container->name) {
10395 + DWC_ERROR("Work %s still pending", container->name);
10396 + }
10397 + }
10398 +
10399 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10400 +#endif
10401 + DWC_WAITQ_FREE(wq->waitq);
10402 + DWC_SPINLOCK_FREE(wq->lock);
10403 + workqueue_destroy(wq->taskq);
10404 + DWC_FREE(wq);
10405 +}
10406 +
10407 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
10408 + char *format, ...)
10409 +{
10410 + dwc_irqflags_t flags;
10411 + work_container_t *container;
10412 + static char name[128];
10413 + va_list args;
10414 +
10415 + va_start(args, format);
10416 + DWC_VSNPRINTF(name, 128, format, args);
10417 + va_end(args);
10418 +
10419 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10420 + wq->pending++;
10421 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10422 + DWC_WAITQ_TRIGGER(wq->waitq);
10423 +
10424 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
10425 + if (!container) {
10426 + DWC_ERROR("Cannot allocate memory for container");
10427 + return;
10428 + }
10429 +
10430 + container->name = DWC_STRDUP(name);
10431 + if (!container->name) {
10432 + DWC_ERROR("Cannot allocate memory for container->name");
10433 + DWC_FREE(container);
10434 + return;
10435 + }
10436 +
10437 + container->cb = cb;
10438 + container->data = data;
10439 + container->wq = wq;
10440 + container->hz = 0;
10441 + wq->container = container;
10442 +
10443 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
10444 + workqueue_enqueue(wq->taskq, &container->task);
10445 +}
10446 +
10447 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
10448 + void *data, uint32_t time, char *format, ...)
10449 +{
10450 + dwc_irqflags_t flags;
10451 + work_container_t *container;
10452 + static char name[128];
10453 + struct timeval tv;
10454 + va_list args;
10455 +
10456 + va_start(args, format);
10457 + DWC_VSNPRINTF(name, 128, format, args);
10458 + va_end(args);
10459 +
10460 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10461 + wq->pending++;
10462 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10463 + DWC_WAITQ_TRIGGER(wq->waitq);
10464 +
10465 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
10466 + if (!container) {
10467 + DWC_ERROR("Cannot allocate memory for container");
10468 + return;
10469 + }
10470 +
10471 + container->name = DWC_STRDUP(name);
10472 + if (!container->name) {
10473 + DWC_ERROR("Cannot allocate memory for container->name");
10474 + DWC_FREE(container);
10475 + return;
10476 + }
10477 +
10478 + container->cb = cb;
10479 + container->data = data;
10480 + container->wq = wq;
10481 + tv.tv_sec = time / 1000;
10482 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
10483 + container->hz = tvtohz(&tv);
10484 + wq->container = container;
10485 +
10486 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
10487 + workqueue_enqueue(wq->taskq, &container->task);
10488 +}
10489 +
10490 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
10491 +{
10492 + return wq->pending;
10493 +}
10494 --- /dev/null
10495 +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.c
10496 @@ -0,0 +1,308 @@
10497 +/* =========================================================================
10498 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
10499 + * $Revision: #5 $
10500 + * $Date: 2010/09/28 $
10501 + * $Change: 1596182 $
10502 + *
10503 + * Synopsys Portability Library Software and documentation
10504 + * (hereinafter, "Software") is an Unsupported proprietary work of
10505 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10506 + * between Synopsys and you.
10507 + *
10508 + * The Software IS NOT an item of Licensed Software or Licensed Product
10509 + * under any End User Software License Agreement or Agreement for
10510 + * Licensed Product with Synopsys or any supplement thereto. You are
10511 + * permitted to use and redistribute this Software in source and binary
10512 + * forms, with or without modification, provided that redistributions
10513 + * of source code must retain this notice. You may not view, use,
10514 + * disclose, copy or distribute this file or any information contained
10515 + * herein except pursuant to this license grant from Synopsys. If you
10516 + * do not agree with this notice, including the disclaimer below, then
10517 + * you are not authorized to use the Software.
10518 + *
10519 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10520 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10521 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10522 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10523 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10524 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10525 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10526 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10527 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10528 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10529 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10530 + * DAMAGE.
10531 + * ========================================================================= */
10532 +
10533 +/** @file
10534 + * This file contains the WUSB cryptographic routines.
10535 + */
10536 +
10537 +#ifdef DWC_CRYPTOLIB
10538 +
10539 +#include "dwc_crypto.h"
10540 +#include "usb.h"
10541 +
10542 +#ifdef DEBUG
10543 +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
10544 +{
10545 + int i;
10546 + DWC_PRINTF("%s: ", name);
10547 + for (i=0; i<len; i++) {
10548 + DWC_PRINTF("%02x ", bytes[i]);
10549 + }
10550 + DWC_PRINTF("\n");
10551 +}
10552 +#else
10553 +#define dump_bytes(x...)
10554 +#endif
10555 +
10556 +/* Display a block */
10557 +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
10558 +{
10559 +#ifdef DWC_DEBUG_CRYPTO
10560 + int i, blksize = 16;
10561 +
10562 + DWC_DEBUG("%s", prefix);
10563 +
10564 + if (suffix == NULL) {
10565 + suffix = "\n";
10566 + blksize = a;
10567 + }
10568 +
10569 + for (i = 0; i < blksize; i++)
10570 + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
10571 + DWC_PRINT(suffix);
10572 +#endif
10573 +}
10574 +
10575 +/**
10576 + * Encrypts an array of bytes using the AES encryption engine.
10577 + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
10578 + * in-place.
10579 + *
10580 + * @return 0 on success, negative error code on error.
10581 + */
10582 +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
10583 +{
10584 + u8 block_t[16];
10585 + DWC_MEMSET(block_t, 0, 16);
10586 +
10587 + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
10588 +}
10589 +
10590 +/**
10591 + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
10592 + * This function takes a data string and returns the encrypted CBC
10593 + * Counter-mode MIC.
10594 + *
10595 + * @param key The 128-bit symmetric key.
10596 + * @param nonce The CCM nonce.
10597 + * @param label The unique 14-byte ASCII text label.
10598 + * @param bytes The byte array to be encrypted.
10599 + * @param len Length of the byte array.
10600 + * @param result Byte array to receive the 8-byte encrypted MIC.
10601 + */
10602 +void dwc_wusb_cmf(u8 *key, u8 *nonce,
10603 + char *label, u8 *bytes, int len, u8 *result)
10604 +{
10605 + u8 block_m[16];
10606 + u8 block_x[16];
10607 + u8 block_t[8];
10608 + int idx, blkNum;
10609 + u16 la = (u16)(len + 14);
10610 +
10611 + /* Set the AES-128 key */
10612 + //dwc_aes_setkey(tfm, key, 16);
10613 +
10614 + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
10615 + block_m[0] = 0x59;
10616 + for (idx = 0; idx < 13; idx++)
10617 + block_m[idx + 1] = nonce[idx];
10618 + block_m[14] = 0;
10619 + block_m[15] = 0;
10620 +
10621 + /* Produce the CBC IV */
10622 + dwc_wusb_aes_encrypt(block_m, key, block_x);
10623 + show_block(block_m, "CBC IV in: ", "\n", 0);
10624 + show_block(block_x, "CBC IV out:", "\n", 0);
10625 +
10626 + /* Fill block B1 from l(a) = Blen + 14, and A */
10627 + block_x[0] ^= (u8)(la >> 8);
10628 + block_x[1] ^= (u8)la;
10629 + for (idx = 0; idx < 14; idx++)
10630 + block_x[idx + 2] ^= label[idx];
10631 + show_block(block_x, "After xor: ", "b1\n", 16);
10632 +
10633 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10634 + show_block(block_x, "After AES: ", "b1\n", 16);
10635 +
10636 + idx = 0;
10637 + blkNum = 0;
10638 +
10639 + /* Fill remaining blocks with B */
10640 + while (len-- > 0) {
10641 + block_x[idx] ^= *bytes++;
10642 + if (++idx >= 16) {
10643 + idx = 0;
10644 + show_block(block_x, "After xor: ", "\n", blkNum);
10645 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10646 + show_block(block_x, "After AES: ", "\n", blkNum);
10647 + blkNum++;
10648 + }
10649 + }
10650 +
10651 + /* Handle partial last block */
10652 + if (idx > 0) {
10653 + show_block(block_x, "After xor: ", "\n", blkNum);
10654 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10655 + show_block(block_x, "After AES: ", "\n", blkNum);
10656 + }
10657 +
10658 + /* Save the MIC tag */
10659 + DWC_MEMCPY(block_t, block_x, 8);
10660 + show_block(block_t, "MIC tag : ", NULL, 8);
10661 +
10662 + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
10663 + block_m[0] = 0x01;
10664 + block_m[14] = 0;
10665 + block_m[15] = 0;
10666 +
10667 + /* Encrypt the counter */
10668 + dwc_wusb_aes_encrypt(block_m, key, block_x);
10669 + show_block(block_x, "CTR[MIC] : ", NULL, 8);
10670 +
10671 + /* XOR with MIC tag */
10672 + for (idx = 0; idx < 8; idx++) {
10673 + block_t[idx] ^= block_x[idx];
10674 + }
10675 +
10676 + /* Return result to caller */
10677 + DWC_MEMCPY(result, block_t, 8);
10678 + show_block(result, "CCM-MIC : ", NULL, 8);
10679 +
10680 +}
10681 +
10682 +/**
10683 + * The PRF function described in section 6.5 of the WUSB spec. This function
10684 + * concatenates MIC values returned from dwc_cmf() to create a value of
10685 + * the requested length.
10686 + *
10687 + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
10688 + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
10689 + * @param result Byte array to receive the result.
10690 + */
10691 +void dwc_wusb_prf(int prf_len, u8 *key,
10692 + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
10693 +{
10694 + int i;
10695 +
10696 + nonce[0] = 0;
10697 + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
10698 + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
10699 + result += 8;
10700 + }
10701 +}
10702 +
10703 +/**
10704 + * Fills in CCM Nonce per the WUSB spec.
10705 + *
10706 + * @param[in] haddr Host address.
10707 + * @param[in] daddr Device address.
10708 + * @param[in] tkid Session Key(PTK) identifier.
10709 + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
10710 + */
10711 +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
10712 + uint8_t *nonce)
10713 +{
10714 +
10715 + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
10716 +
10717 + DWC_MEMSET(&nonce[0], 0, 16);
10718 +
10719 + DWC_MEMCPY(&nonce[6], tkid, 3);
10720 + nonce[9] = daddr & 0xFF;
10721 + nonce[10] = (daddr >> 8) & 0xFF;
10722 + nonce[11] = haddr & 0xFF;
10723 + nonce[12] = (haddr >> 8) & 0xFF;
10724 +
10725 + dump_bytes("CCM nonce", nonce, 16);
10726 +}
10727 +
10728 +/**
10729 + * Generates a 16-byte cryptographic-grade random number for the Host/Device
10730 + * Nonce.
10731 + */
10732 +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
10733 +{
10734 + uint8_t inonce[16];
10735 + uint32_t temp[4];
10736 +
10737 + /* Fill in the Nonce */
10738 + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
10739 + inonce[9] = addr & 0xFF;
10740 + inonce[10] = (addr >> 8) & 0xFF;
10741 + inonce[11] = inonce[9];
10742 + inonce[12] = inonce[10];
10743 +
10744 + /* Collect "randomness samples" */
10745 + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
10746 +
10747 + dwc_wusb_prf_128((uint8_t *)temp, nonce,
10748 + "Random Numbers", (uint8_t *)temp, sizeof(temp),
10749 + nonce);
10750 +}
10751 +
10752 +/**
10753 + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
10754 + * WUSB spec.
10755 + *
10756 + * @param[in] ccm_nonce Pointer to CCM Nonce.
10757 + * @param[in] mk Master Key to derive the session from
10758 + * @param[in] hnonce Pointer to Host Nonce.
10759 + * @param[in] dnonce Pointer to Device Nonce.
10760 + * @param[out] kck Pointer to where the KCK output is to be written.
10761 + * @param[out] ptk Pointer to where the PTK output is to be written.
10762 + */
10763 +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
10764 + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
10765 +{
10766 + uint8_t idata[32];
10767 + uint8_t odata[32];
10768 +
10769 + dump_bytes("ck", mk, 16);
10770 + dump_bytes("hnonce", hnonce, 16);
10771 + dump_bytes("dnonce", dnonce, 16);
10772 +
10773 + /* The data is the HNonce and DNonce concatenated */
10774 + DWC_MEMCPY(&idata[0], hnonce, 16);
10775 + DWC_MEMCPY(&idata[16], dnonce, 16);
10776 +
10777 + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
10778 +
10779 + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
10780 + DWC_MEMCPY(kck, &odata[0], 16);
10781 + DWC_MEMCPY(ptk, &odata[16], 16);
10782 +
10783 + dump_bytes("kck", kck, 16);
10784 + dump_bytes("ptk", ptk, 16);
10785 +}
10786 +
10787 +/**
10788 + * Generates the Message Integrity Code over the Handshake data per the
10789 + * WUSB spec.
10790 + *
10791 + * @param ccm_nonce Pointer to CCM Nonce.
10792 + * @param kck Pointer to Key Confirmation Key.
10793 + * @param data Pointer to Handshake data to be checked.
10794 + * @param mic Pointer to where the MIC output is to be written.
10795 + */
10796 +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
10797 + uint8_t *data, uint8_t *mic)
10798 +{
10799 +
10800 + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
10801 + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
10802 +}
10803 +
10804 +#endif /* DWC_CRYPTOLIB */
10805 --- /dev/null
10806 +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.h
10807 @@ -0,0 +1,111 @@
10808 +/* =========================================================================
10809 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
10810 + * $Revision: #3 $
10811 + * $Date: 2010/09/28 $
10812 + * $Change: 1596182 $
10813 + *
10814 + * Synopsys Portability Library Software and documentation
10815 + * (hereinafter, "Software") is an Unsupported proprietary work of
10816 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10817 + * between Synopsys and you.
10818 + *
10819 + * The Software IS NOT an item of Licensed Software or Licensed Product
10820 + * under any End User Software License Agreement or Agreement for
10821 + * Licensed Product with Synopsys or any supplement thereto. You are
10822 + * permitted to use and redistribute this Software in source and binary
10823 + * forms, with or without modification, provided that redistributions
10824 + * of source code must retain this notice. You may not view, use,
10825 + * disclose, copy or distribute this file or any information contained
10826 + * herein except pursuant to this license grant from Synopsys. If you
10827 + * do not agree with this notice, including the disclaimer below, then
10828 + * you are not authorized to use the Software.
10829 + *
10830 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10831 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10832 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10833 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10834 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10835 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10836 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10837 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10838 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10839 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10840 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10841 + * DAMAGE.
10842 + * ========================================================================= */
10843 +
10844 +#ifndef _DWC_CRYPTO_H_
10845 +#define _DWC_CRYPTO_H_
10846 +
10847 +#ifdef __cplusplus
10848 +extern "C" {
10849 +#endif
10850 +
10851 +/** @file
10852 + *
10853 + * This file contains declarations for the WUSB Cryptographic routines as
10854 + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
10855 + * modules.
10856 + */
10857 +
10858 +#include "dwc_os.h"
10859 +
10860 +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
10861 +
10862 +void dwc_wusb_cmf(u8 *key, u8 *nonce,
10863 + char *label, u8 *bytes, int len, u8 *result);
10864 +void dwc_wusb_prf(int prf_len, u8 *key,
10865 + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
10866 +
10867 +/**
10868 + * The PRF-64 function described in section 6.5 of the WUSB spec.
10869 + *
10870 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10871 + */
10872 +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
10873 + char *label, u8 *bytes, int len, u8 *result)
10874 +{
10875 + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
10876 +}
10877 +
10878 +/**
10879 + * The PRF-128 function described in section 6.5 of the WUSB spec.
10880 + *
10881 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10882 + */
10883 +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
10884 + char *label, u8 *bytes, int len, u8 *result)
10885 +{
10886 + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
10887 +}
10888 +
10889 +/**
10890 + * The PRF-256 function described in section 6.5 of the WUSB spec.
10891 + *
10892 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10893 + */
10894 +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
10895 + char *label, u8 *bytes, int len, u8 *result)
10896 +{
10897 + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
10898 +}
10899 +
10900 +
10901 +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
10902 + uint8_t *nonce);
10903 +void dwc_wusb_gen_nonce(uint16_t addr,
10904 + uint8_t *nonce);
10905 +
10906 +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
10907 + uint8_t *hnonce, uint8_t *dnonce,
10908 + uint8_t *kck, uint8_t *ptk);
10909 +
10910 +
10911 +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
10912 + *kck, uint8_t *data, uint8_t *mic);
10913 +
10914 +#ifdef __cplusplus
10915 +}
10916 +#endif
10917 +
10918 +#endif /* _DWC_CRYPTO_H_ */
10919 --- /dev/null
10920 +++ b/drivers/usb/host/dwc_common_port/dwc_dh.c
10921 @@ -0,0 +1,291 @@
10922 +/* =========================================================================
10923 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
10924 + * $Revision: #3 $
10925 + * $Date: 2010/09/28 $
10926 + * $Change: 1596182 $
10927 + *
10928 + * Synopsys Portability Library Software and documentation
10929 + * (hereinafter, "Software") is an Unsupported proprietary work of
10930 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10931 + * between Synopsys and you.
10932 + *
10933 + * The Software IS NOT an item of Licensed Software or Licensed Product
10934 + * under any End User Software License Agreement or Agreement for
10935 + * Licensed Product with Synopsys or any supplement thereto. You are
10936 + * permitted to use and redistribute this Software in source and binary
10937 + * forms, with or without modification, provided that redistributions
10938 + * of source code must retain this notice. You may not view, use,
10939 + * disclose, copy or distribute this file or any information contained
10940 + * herein except pursuant to this license grant from Synopsys. If you
10941 + * do not agree with this notice, including the disclaimer below, then
10942 + * you are not authorized to use the Software.
10943 + *
10944 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10945 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10946 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10947 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10948 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10949 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10950 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10951 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10952 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10953 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10954 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10955 + * DAMAGE.
10956 + * ========================================================================= */
10957 +#ifdef DWC_CRYPTOLIB
10958 +
10959 +#ifndef CONFIG_MACH_IPMATE
10960 +
10961 +#include "dwc_dh.h"
10962 +#include "dwc_modpow.h"
10963 +
10964 +#ifdef DEBUG
10965 +/* This function prints out a buffer in the format described in the Association
10966 + * Model specification. */
10967 +static void dh_dump(char *str, void *_num, int len)
10968 +{
10969 + uint8_t *num = _num;
10970 + int i;
10971 + DWC_PRINTF("%s\n", str);
10972 + for (i = 0; i < len; i ++) {
10973 + DWC_PRINTF("%02x", num[i]);
10974 + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
10975 + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
10976 + }
10977 +
10978 + DWC_PRINTF("\n");
10979 +}
10980 +#else
10981 +#define dh_dump(_x...) do {; } while(0)
10982 +#endif
10983 +
10984 +/* Constant g value */
10985 +static __u32 dh_g[] = {
10986 + 0x02000000,
10987 +};
10988 +
10989 +/* Constant p value */
10990 +static __u32 dh_p[] = {
10991 + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
10992 + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
10993 + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
10994 + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
10995 + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
10996 + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
10997 + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
10998 + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
10999 + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
11000 + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
11001 + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
11002 + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
11003 +};
11004 +
11005 +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
11006 +{
11007 + uint8_t *in = _in;
11008 + uint8_t *out = _out;
11009 + int i;
11010 + for (i=0; i<len; i++) {
11011 + out[i] = in[len-1-i];
11012 + }
11013 +}
11014 +
11015 +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
11016 + * big endian numbers of size len, in bytes. Each len value must be a multiple
11017 + * of 4. */
11018 +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
11019 + void *exp, uint32_t exp_len,
11020 + void *mod, uint32_t mod_len,
11021 + void *out)
11022 +{
11023 + /* modpow() takes little endian numbers. AM uses big-endian. This
11024 + * function swaps bytes of numbers before passing onto modpow. */
11025 +
11026 + int retval = 0;
11027 + uint32_t *result;
11028 +
11029 + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
11030 + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
11031 + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
11032 +
11033 + dh_swap_bytes(num, &bignum_num[1], num_len);
11034 + bignum_num[0] = num_len / 4;
11035 +
11036 + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
11037 + bignum_exp[0] = exp_len / 4;
11038 +
11039 + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
11040 + bignum_mod[0] = mod_len / 4;
11041 +
11042 + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
11043 + if (!result) {
11044 + retval = -1;
11045 + goto dh_modpow_nomem;
11046 + }
11047 +
11048 + dh_swap_bytes(&result[1], out, result[0] * 4);
11049 + dwc_free(mem_ctx, result);
11050 +
11051 + dh_modpow_nomem:
11052 + dwc_free(mem_ctx, bignum_num);
11053 + dwc_free(mem_ctx, bignum_exp);
11054 + dwc_free(mem_ctx, bignum_mod);
11055 + return retval;
11056 +}
11057 +
11058 +
11059 +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
11060 +{
11061 + int retval;
11062 + uint8_t m3[385];
11063 +
11064 +#ifndef DH_TEST_VECTORS
11065 + DWC_RANDOM_BYTES(exp, 32);
11066 +#endif
11067 +
11068 + /* Compute the pkd */
11069 + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
11070 + exp, 32,
11071 + dh_p, 384, pk))) {
11072 + return retval;
11073 + }
11074 +
11075 + m3[384] = nd;
11076 + DWC_MEMCPY(&m3[0], pk, 384);
11077 + DWC_SHA256(m3, 385, hash);
11078 +
11079 + dh_dump("PK", pk, 384);
11080 + dh_dump("SHA-256(M3)", hash, 32);
11081 + return 0;
11082 +}
11083 +
11084 +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
11085 + uint8_t *exp, int is_host,
11086 + char *dd, uint8_t *ck, uint8_t *kdk)
11087 +{
11088 + int retval;
11089 + uint8_t mv[784];
11090 + uint8_t sha_result[32];
11091 + uint8_t dhkey[384];
11092 + uint8_t shared_secret[384];
11093 + char *message;
11094 + uint32_t vd;
11095 +
11096 + uint8_t *pk;
11097 +
11098 + if (is_host) {
11099 + pk = pkd;
11100 + }
11101 + else {
11102 + pk = pkh;
11103 + }
11104 +
11105 + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
11106 + exp, 32,
11107 + dh_p, 384, shared_secret))) {
11108 + return retval;
11109 + }
11110 + dh_dump("Shared Secret", shared_secret, 384);
11111 +
11112 + DWC_SHA256(shared_secret, 384, dhkey);
11113 + dh_dump("DHKEY", dhkey, 384);
11114 +
11115 + DWC_MEMCPY(&mv[0], pkd, 384);
11116 + DWC_MEMCPY(&mv[384], pkh, 384);
11117 + DWC_MEMCPY(&mv[768], "displayed digest", 16);
11118 + dh_dump("MV", mv, 784);
11119 +
11120 + DWC_SHA256(mv, 784, sha_result);
11121 + dh_dump("SHA-256(MV)", sha_result, 32);
11122 + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
11123 +
11124 + dh_swap_bytes(sha_result, &vd, 4);
11125 +#ifdef DEBUG
11126 + DWC_PRINTF("Vd (decimal) = %d\n", vd);
11127 +#endif
11128 +
11129 + switch (nd) {
11130 + case 2:
11131 + vd = vd % 100;
11132 + DWC_SPRINTF(dd, "%02d", vd);
11133 + break;
11134 + case 3:
11135 + vd = vd % 1000;
11136 + DWC_SPRINTF(dd, "%03d", vd);
11137 + break;
11138 + case 4:
11139 + vd = vd % 10000;
11140 + DWC_SPRINTF(dd, "%04d", vd);
11141 + break;
11142 + }
11143 +#ifdef DEBUG
11144 + DWC_PRINTF("Display Digits: %s\n", dd);
11145 +#endif
11146 +
11147 + message = "connection key";
11148 + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
11149 + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
11150 + DWC_MEMCPY(ck, sha_result, 16);
11151 +
11152 + message = "key derivation key";
11153 + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
11154 + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
11155 + DWC_MEMCPY(kdk, sha_result, 32);
11156 +
11157 + return 0;
11158 +}
11159 +
11160 +
11161 +#ifdef DH_TEST_VECTORS
11162 +
11163 +static __u8 dh_a[] = {
11164 + 0x44, 0x00, 0x51, 0xd6,
11165 + 0xf0, 0xb5, 0x5e, 0xa9,
11166 + 0x67, 0xab, 0x31, 0xc6,
11167 + 0x8a, 0x8b, 0x5e, 0x37,
11168 + 0xd9, 0x10, 0xda, 0xe0,
11169 + 0xe2, 0xd4, 0x59, 0xa4,
11170 + 0x86, 0x45, 0x9c, 0xaa,
11171 + 0xdf, 0x36, 0x75, 0x16,
11172 +};
11173 +
11174 +static __u8 dh_b[] = {
11175 + 0x5d, 0xae, 0xc7, 0x86,
11176 + 0x79, 0x80, 0xa3, 0x24,
11177 + 0x8c, 0xe3, 0x57, 0x8f,
11178 + 0xc7, 0x5f, 0x1b, 0x0f,
11179 + 0x2d, 0xf8, 0x9d, 0x30,
11180 + 0x6f, 0xa4, 0x52, 0xcd,
11181 + 0xe0, 0x7a, 0x04, 0x8a,
11182 + 0xde, 0xd9, 0x26, 0x56,
11183 +};
11184 +
11185 +void dwc_run_dh_test_vectors(void *mem_ctx)
11186 +{
11187 + uint8_t pkd[384];
11188 + uint8_t pkh[384];
11189 + uint8_t hashd[32];
11190 + uint8_t hashh[32];
11191 + uint8_t ck[16];
11192 + uint8_t kdk[32];
11193 + char dd[5];
11194 +
11195 + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
11196 +
11197 + /* compute the PKd and SHA-256(PKd || Nd) */
11198 + DWC_PRINTF("Computing PKd\n");
11199 + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
11200 +
11201 + /* compute the PKd and SHA-256(PKh || Nd) */
11202 + DWC_PRINTF("Computing PKh\n");
11203 + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
11204 +
11205 + /* compute the dhkey */
11206 + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
11207 +}
11208 +#endif /* DH_TEST_VECTORS */
11209 +
11210 +#endif /* !CONFIG_MACH_IPMATE */
11211 +
11212 +#endif /* DWC_CRYPTOLIB */
11213 --- /dev/null
11214 +++ b/drivers/usb/host/dwc_common_port/dwc_dh.h
11215 @@ -0,0 +1,106 @@
11216 +/* =========================================================================
11217 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
11218 + * $Revision: #4 $
11219 + * $Date: 2010/09/28 $
11220 + * $Change: 1596182 $
11221 + *
11222 + * Synopsys Portability Library Software and documentation
11223 + * (hereinafter, "Software") is an Unsupported proprietary work of
11224 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
11225 + * between Synopsys and you.
11226 + *
11227 + * The Software IS NOT an item of Licensed Software or Licensed Product
11228 + * under any End User Software License Agreement or Agreement for
11229 + * Licensed Product with Synopsys or any supplement thereto. You are
11230 + * permitted to use and redistribute this Software in source and binary
11231 + * forms, with or without modification, provided that redistributions
11232 + * of source code must retain this notice. You may not view, use,
11233 + * disclose, copy or distribute this file or any information contained
11234 + * herein except pursuant to this license grant from Synopsys. If you
11235 + * do not agree with this notice, including the disclaimer below, then
11236 + * you are not authorized to use the Software.
11237 + *
11238 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
11239 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11240 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11241 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
11242 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11243 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11244 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11245 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
11246 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
11247 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
11248 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
11249 + * DAMAGE.
11250 + * ========================================================================= */
11251 +#ifndef _DWC_DH_H_
11252 +#define _DWC_DH_H_
11253 +
11254 +#ifdef __cplusplus
11255 +extern "C" {
11256 +#endif
11257 +
11258 +#include "dwc_os.h"
11259 +
11260 +/** @file
11261 + *
11262 + * This file defines the common functions on device and host for performing
11263 + * numeric association as defined in the WUSB spec. They are only to be
11264 + * used internally by the DWC UWB modules. */
11265 +
11266 +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
11267 +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
11268 + uint8_t *key, uint32_t keylen,
11269 + uint8_t *out);
11270 +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
11271 + void *exp, uint32_t exp_len,
11272 + void *mod, uint32_t mod_len,
11273 + void *out);
11274 +
11275 +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
11276 + *
11277 + * PK = g^exp mod p.
11278 + *
11279 + * Input:
11280 + * Nd = Number of digits on the device.
11281 + *
11282 + * Output:
11283 + * exp = A 32-byte buffer to be filled with a randomly generated number.
11284 + * used as either A or B.
11285 + * pk = A 384-byte buffer to be filled with the PKH or PKD.
11286 + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
11287 + */
11288 +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
11289 +
11290 +/** Computes the DHKEY, and VD.
11291 + *
11292 + * If called from host, then it will comput DHKEY=PKD^exp % p.
11293 + * If called from device, then it will comput DHKEY=PKH^exp % p.
11294 + *
11295 + * Input:
11296 + * pkd = The PKD value.
11297 + * pkh = The PKH value.
11298 + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
11299 + * is_host = Set to non zero if a WUSB host is calling this function.
11300 + *
11301 + * Output:
11302 +
11303 + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
11304 + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
11305 + * null termination character. This buffer can be used directly for display.
11306 + * ck = A 16-byte buffer to be filled with the CK.
11307 + * kdk = A 32-byte buffer to be filled with the KDK.
11308 + */
11309 +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
11310 + uint8_t *exp, int is_host,
11311 + char *dd, uint8_t *ck, uint8_t *kdk);
11312 +
11313 +#ifdef DH_TEST_VECTORS
11314 +extern void dwc_run_dh_test_vectors(void);
11315 +#endif
11316 +
11317 +#ifdef __cplusplus
11318 +}
11319 +#endif
11320 +
11321 +#endif /* _DWC_DH_H_ */
11322 --- /dev/null
11323 +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
11324 @@ -0,0 +1,594 @@
11325 +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
11326 +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
11327 +
11328 +/*
11329 + * Copyright (c) 1991, 1993
11330 + * The Regents of the University of California. All rights reserved.
11331 + *
11332 + * Redistribution and use in source and binary forms, with or without
11333 + * modification, are permitted provided that the following conditions
11334 + * are met:
11335 + * 1. Redistributions of source code must retain the above copyright
11336 + * notice, this list of conditions and the following disclaimer.
11337 + * 2. Redistributions in binary form must reproduce the above copyright
11338 + * notice, this list of conditions and the following disclaimer in the
11339 + * documentation and/or other materials provided with the distribution.
11340 + * 3. Neither the name of the University nor the names of its contributors
11341 + * may be used to endorse or promote products derived from this software
11342 + * without specific prior written permission.
11343 + *
11344 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
11345 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11346 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11347 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
11348 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11349 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
11350 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
11351 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
11352 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
11353 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
11354 + * SUCH DAMAGE.
11355 + *
11356 + * @(#)queue.h 8.5 (Berkeley) 8/20/94
11357 + */
11358 +
11359 +#ifndef _DWC_LIST_H_
11360 +#define _DWC_LIST_H_
11361 +
11362 +#ifdef __cplusplus
11363 +extern "C" {
11364 +#endif
11365 +
11366 +/** @file
11367 + *
11368 + * This file defines linked list operations. It is derived from BSD with
11369 + * only the MACRO names being prefixed with DWC_. This is because a few of
11370 + * these names conflict with those on Linux. For documentation on use, see the
11371 + * inline comments in the source code. The original license for this source
11372 + * code applies and is preserved in the dwc_list.h source file.
11373 + */
11374 +
11375 +/*
11376 + * This file defines five types of data structures: singly-linked lists,
11377 + * lists, simple queues, tail queues, and circular queues.
11378 + *
11379 + *
11380 + * A singly-linked list is headed by a single forward pointer. The elements
11381 + * are singly linked for minimum space and pointer manipulation overhead at
11382 + * the expense of O(n) removal for arbitrary elements. New elements can be
11383 + * added to the list after an existing element or at the head of the list.
11384 + * Elements being removed from the head of the list should use the explicit
11385 + * macro for this purpose for optimum efficiency. A singly-linked list may
11386 + * only be traversed in the forward direction. Singly-linked lists are ideal
11387 + * for applications with large datasets and few or no removals or for
11388 + * implementing a LIFO queue.
11389 + *
11390 + * A list is headed by a single forward pointer (or an array of forward
11391 + * pointers for a hash table header). The elements are doubly linked
11392 + * so that an arbitrary element can be removed without a need to
11393 + * traverse the list. New elements can be added to the list before
11394 + * or after an existing element or at the head of the list. A list
11395 + * may only be traversed in the forward direction.
11396 + *
11397 + * A simple queue is headed by a pair of pointers, one the head of the
11398 + * list and the other to the tail of the list. The elements are singly
11399 + * linked to save space, so elements can only be removed from the
11400 + * head of the list. New elements can be added to the list before or after
11401 + * an existing element, at the head of the list, or at the end of the
11402 + * list. A simple queue may only be traversed in the forward direction.
11403 + *
11404 + * A tail queue is headed by a pair of pointers, one to the head of the
11405 + * list and the other to the tail of the list. The elements are doubly
11406 + * linked so that an arbitrary element can be removed without a need to
11407 + * traverse the list. New elements can be added to the list before or
11408 + * after an existing element, at the head of the list, or at the end of
11409 + * the list. A tail queue may be traversed in either direction.
11410 + *
11411 + * A circle queue is headed by a pair of pointers, one to the head of the
11412 + * list and the other to the tail of the list. The elements are doubly
11413 + * linked so that an arbitrary element can be removed without a need to
11414 + * traverse the list. New elements can be added to the list before or after
11415 + * an existing element, at the head of the list, or at the end of the list.
11416 + * A circle queue may be traversed in either direction, but has a more
11417 + * complex end of list detection.
11418 + *
11419 + * For details on the use of these macros, see the queue(3) manual page.
11420 + */
11421 +
11422 +/*
11423 + * Double-linked List.
11424 + */
11425 +
11426 +typedef struct dwc_list_link {
11427 + struct dwc_list_link *next;
11428 + struct dwc_list_link *prev;
11429 +} dwc_list_link_t;
11430 +
11431 +#define DWC_LIST_INIT(link) do { \
11432 + (link)->next = (link); \
11433 + (link)->prev = (link); \
11434 +} while (0)
11435 +
11436 +#define DWC_LIST_FIRST(link) ((link)->next)
11437 +#define DWC_LIST_LAST(link) ((link)->prev)
11438 +#define DWC_LIST_END(link) (link)
11439 +#define DWC_LIST_NEXT(link) ((link)->next)
11440 +#define DWC_LIST_PREV(link) ((link)->prev)
11441 +#define DWC_LIST_EMPTY(link) \
11442 + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
11443 +#define DWC_LIST_ENTRY(link, type, field) \
11444 + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
11445 +
11446 +#if 0
11447 +#define DWC_LIST_INSERT_HEAD(list, link) do { \
11448 + (link)->next = (list)->next; \
11449 + (link)->prev = (list); \
11450 + (list)->next->prev = (link); \
11451 + (list)->next = (link); \
11452 +} while (0)
11453 +
11454 +#define DWC_LIST_INSERT_TAIL(list, link) do { \
11455 + (link)->next = (list); \
11456 + (link)->prev = (list)->prev; \
11457 + (list)->prev->next = (link); \
11458 + (list)->prev = (link); \
11459 +} while (0)
11460 +#else
11461 +#define DWC_LIST_INSERT_HEAD(list, link) do { \
11462 + dwc_list_link_t *__next__ = (list)->next; \
11463 + __next__->prev = (link); \
11464 + (link)->next = __next__; \
11465 + (link)->prev = (list); \
11466 + (list)->next = (link); \
11467 +} while (0)
11468 +
11469 +#define DWC_LIST_INSERT_TAIL(list, link) do { \
11470 + dwc_list_link_t *__prev__ = (list)->prev; \
11471 + (list)->prev = (link); \
11472 + (link)->next = (list); \
11473 + (link)->prev = __prev__; \
11474 + __prev__->next = (link); \
11475 +} while (0)
11476 +#endif
11477 +
11478 +#if 0
11479 +static inline void __list_add(struct list_head *new,
11480 + struct list_head *prev,
11481 + struct list_head *next)
11482 +{
11483 + next->prev = new;
11484 + new->next = next;
11485 + new->prev = prev;
11486 + prev->next = new;
11487 +}
11488 +
11489 +static inline void list_add(struct list_head *new, struct list_head *head)
11490 +{
11491 + __list_add(new, head, head->next);
11492 +}
11493 +
11494 +static inline void list_add_tail(struct list_head *new, struct list_head *head)
11495 +{
11496 + __list_add(new, head->prev, head);
11497 +}
11498 +
11499 +static inline void __list_del(struct list_head * prev, struct list_head * next)
11500 +{
11501 + next->prev = prev;
11502 + prev->next = next;
11503 +}
11504 +
11505 +static inline void list_del(struct list_head *entry)
11506 +{
11507 + __list_del(entry->prev, entry->next);
11508 + entry->next = LIST_POISON1;
11509 + entry->prev = LIST_POISON2;
11510 +}
11511 +#endif
11512 +
11513 +#define DWC_LIST_REMOVE(link) do { \
11514 + (link)->next->prev = (link)->prev; \
11515 + (link)->prev->next = (link)->next; \
11516 +} while (0)
11517 +
11518 +#define DWC_LIST_REMOVE_INIT(link) do { \
11519 + DWC_LIST_REMOVE(link); \
11520 + DWC_LIST_INIT(link); \
11521 +} while (0)
11522 +
11523 +#define DWC_LIST_MOVE_HEAD(list, link) do { \
11524 + DWC_LIST_REMOVE(link); \
11525 + DWC_LIST_INSERT_HEAD(list, link); \
11526 +} while (0)
11527 +
11528 +#define DWC_LIST_MOVE_TAIL(list, link) do { \
11529 + DWC_LIST_REMOVE(link); \
11530 + DWC_LIST_INSERT_TAIL(list, link); \
11531 +} while (0)
11532 +
11533 +#define DWC_LIST_FOREACH(var, list) \
11534 + for((var) = DWC_LIST_FIRST(list); \
11535 + (var) != DWC_LIST_END(list); \
11536 + (var) = DWC_LIST_NEXT(var))
11537 +
11538 +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
11539 + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
11540 + (var) != DWC_LIST_END(list); \
11541 + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
11542 +
11543 +#define DWC_LIST_FOREACH_REVERSE(var, list) \
11544 + for((var) = DWC_LIST_LAST(list); \
11545 + (var) != DWC_LIST_END(list); \
11546 + (var) = DWC_LIST_PREV(var))
11547 +
11548 +/*
11549 + * Singly-linked List definitions.
11550 + */
11551 +#define DWC_SLIST_HEAD(name, type) \
11552 +struct name { \
11553 + struct type *slh_first; /* first element */ \
11554 +}
11555 +
11556 +#define DWC_SLIST_HEAD_INITIALIZER(head) \
11557 + { NULL }
11558 +
11559 +#define DWC_SLIST_ENTRY(type) \
11560 +struct { \
11561 + struct type *sle_next; /* next element */ \
11562 +}
11563 +
11564 +/*
11565 + * Singly-linked List access methods.
11566 + */
11567 +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
11568 +#define DWC_SLIST_END(head) NULL
11569 +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
11570 +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
11571 +
11572 +#define DWC_SLIST_FOREACH(var, head, field) \
11573 + for((var) = SLIST_FIRST(head); \
11574 + (var) != SLIST_END(head); \
11575 + (var) = SLIST_NEXT(var, field))
11576 +
11577 +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
11578 + for((varp) = &SLIST_FIRST((head)); \
11579 + ((var) = *(varp)) != SLIST_END(head); \
11580 + (varp) = &SLIST_NEXT((var), field))
11581 +
11582 +/*
11583 + * Singly-linked List functions.
11584 + */
11585 +#define DWC_SLIST_INIT(head) { \
11586 + SLIST_FIRST(head) = SLIST_END(head); \
11587 +}
11588 +
11589 +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
11590 + (elm)->field.sle_next = (slistelm)->field.sle_next; \
11591 + (slistelm)->field.sle_next = (elm); \
11592 +} while (0)
11593 +
11594 +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
11595 + (elm)->field.sle_next = (head)->slh_first; \
11596 + (head)->slh_first = (elm); \
11597 +} while (0)
11598 +
11599 +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
11600 + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
11601 +} while (0)
11602 +
11603 +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
11604 + (head)->slh_first = (head)->slh_first->field.sle_next; \
11605 +} while (0)
11606 +
11607 +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
11608 + if ((head)->slh_first == (elm)) { \
11609 + SLIST_REMOVE_HEAD((head), field); \
11610 + } \
11611 + else { \
11612 + struct type *curelm = (head)->slh_first; \
11613 + while( curelm->field.sle_next != (elm) ) \
11614 + curelm = curelm->field.sle_next; \
11615 + curelm->field.sle_next = \
11616 + curelm->field.sle_next->field.sle_next; \
11617 + } \
11618 +} while (0)
11619 +
11620 +/*
11621 + * Simple queue definitions.
11622 + */
11623 +#define DWC_SIMPLEQ_HEAD(name, type) \
11624 +struct name { \
11625 + struct type *sqh_first; /* first element */ \
11626 + struct type **sqh_last; /* addr of last next element */ \
11627 +}
11628 +
11629 +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
11630 + { NULL, &(head).sqh_first }
11631 +
11632 +#define DWC_SIMPLEQ_ENTRY(type) \
11633 +struct { \
11634 + struct type *sqe_next; /* next element */ \
11635 +}
11636 +
11637 +/*
11638 + * Simple queue access methods.
11639 + */
11640 +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
11641 +#define DWC_SIMPLEQ_END(head) NULL
11642 +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
11643 +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
11644 +
11645 +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
11646 + for((var) = SIMPLEQ_FIRST(head); \
11647 + (var) != SIMPLEQ_END(head); \
11648 + (var) = SIMPLEQ_NEXT(var, field))
11649 +
11650 +/*
11651 + * Simple queue functions.
11652 + */
11653 +#define DWC_SIMPLEQ_INIT(head) do { \
11654 + (head)->sqh_first = NULL; \
11655 + (head)->sqh_last = &(head)->sqh_first; \
11656 +} while (0)
11657 +
11658 +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
11659 + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
11660 + (head)->sqh_last = &(elm)->field.sqe_next; \
11661 + (head)->sqh_first = (elm); \
11662 +} while (0)
11663 +
11664 +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
11665 + (elm)->field.sqe_next = NULL; \
11666 + *(head)->sqh_last = (elm); \
11667 + (head)->sqh_last = &(elm)->field.sqe_next; \
11668 +} while (0)
11669 +
11670 +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
11671 + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
11672 + (head)->sqh_last = &(elm)->field.sqe_next; \
11673 + (listelm)->field.sqe_next = (elm); \
11674 +} while (0)
11675 +
11676 +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
11677 + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
11678 + (head)->sqh_last = &(head)->sqh_first; \
11679 +} while (0)
11680 +
11681 +/*
11682 + * Tail queue definitions.
11683 + */
11684 +#define DWC_TAILQ_HEAD(name, type) \
11685 +struct name { \
11686 + struct type *tqh_first; /* first element */ \
11687 + struct type **tqh_last; /* addr of last next element */ \
11688 +}
11689 +
11690 +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
11691 + { NULL, &(head).tqh_first }
11692 +
11693 +#define DWC_TAILQ_ENTRY(type) \
11694 +struct { \
11695 + struct type *tqe_next; /* next element */ \
11696 + struct type **tqe_prev; /* address of previous next element */ \
11697 +}
11698 +
11699 +/*
11700 + * tail queue access methods
11701 + */
11702 +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
11703 +#define DWC_TAILQ_END(head) NULL
11704 +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
11705 +#define DWC_TAILQ_LAST(head, headname) \
11706 + (*(((struct headname *)((head)->tqh_last))->tqh_last))
11707 +/* XXX */
11708 +#define DWC_TAILQ_PREV(elm, headname, field) \
11709 + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
11710 +#define DWC_TAILQ_EMPTY(head) \
11711 + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
11712 +
11713 +#define DWC_TAILQ_FOREACH(var, head, field) \
11714 + for ((var) = DWC_TAILQ_FIRST(head); \
11715 + (var) != DWC_TAILQ_END(head); \
11716 + (var) = DWC_TAILQ_NEXT(var, field))
11717 +
11718 +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
11719 + for ((var) = DWC_TAILQ_LAST(head, headname); \
11720 + (var) != DWC_TAILQ_END(head); \
11721 + (var) = DWC_TAILQ_PREV(var, headname, field))
11722 +
11723 +/*
11724 + * Tail queue functions.
11725 + */
11726 +#define DWC_TAILQ_INIT(head) do { \
11727 + (head)->tqh_first = NULL; \
11728 + (head)->tqh_last = &(head)->tqh_first; \
11729 +} while (0)
11730 +
11731 +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
11732 + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
11733 + (head)->tqh_first->field.tqe_prev = \
11734 + &(elm)->field.tqe_next; \
11735 + else \
11736 + (head)->tqh_last = &(elm)->field.tqe_next; \
11737 + (head)->tqh_first = (elm); \
11738 + (elm)->field.tqe_prev = &(head)->tqh_first; \
11739 +} while (0)
11740 +
11741 +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
11742 + (elm)->field.tqe_next = NULL; \
11743 + (elm)->field.tqe_prev = (head)->tqh_last; \
11744 + *(head)->tqh_last = (elm); \
11745 + (head)->tqh_last = &(elm)->field.tqe_next; \
11746 +} while (0)
11747 +
11748 +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
11749 + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
11750 + (elm)->field.tqe_next->field.tqe_prev = \
11751 + &(elm)->field.tqe_next; \
11752 + else \
11753 + (head)->tqh_last = &(elm)->field.tqe_next; \
11754 + (listelm)->field.tqe_next = (elm); \
11755 + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
11756 +} while (0)
11757 +
11758 +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
11759 + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
11760 + (elm)->field.tqe_next = (listelm); \
11761 + *(listelm)->field.tqe_prev = (elm); \
11762 + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
11763 +} while (0)
11764 +
11765 +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
11766 + if (((elm)->field.tqe_next) != NULL) \
11767 + (elm)->field.tqe_next->field.tqe_prev = \
11768 + (elm)->field.tqe_prev; \
11769 + else \
11770 + (head)->tqh_last = (elm)->field.tqe_prev; \
11771 + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
11772 +} while (0)
11773 +
11774 +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
11775 + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
11776 + (elm2)->field.tqe_next->field.tqe_prev = \
11777 + &(elm2)->field.tqe_next; \
11778 + else \
11779 + (head)->tqh_last = &(elm2)->field.tqe_next; \
11780 + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
11781 + *(elm2)->field.tqe_prev = (elm2); \
11782 +} while (0)
11783 +
11784 +/*
11785 + * Circular queue definitions.
11786 + */
11787 +#define DWC_CIRCLEQ_HEAD(name, type) \
11788 +struct name { \
11789 + struct type *cqh_first; /* first element */ \
11790 + struct type *cqh_last; /* last element */ \
11791 +}
11792 +
11793 +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
11794 + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
11795 +
11796 +#define DWC_CIRCLEQ_ENTRY(type) \
11797 +struct { \
11798 + struct type *cqe_next; /* next element */ \
11799 + struct type *cqe_prev; /* previous element */ \
11800 +}
11801 +
11802 +/*
11803 + * Circular queue access methods
11804 + */
11805 +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
11806 +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
11807 +#define DWC_CIRCLEQ_END(head) ((void *)(head))
11808 +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
11809 +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
11810 +#define DWC_CIRCLEQ_EMPTY(head) \
11811 + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
11812 +
11813 +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
11814 +
11815 +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
11816 + for((var) = DWC_CIRCLEQ_FIRST(head); \
11817 + (var) != DWC_CIRCLEQ_END(head); \
11818 + (var) = DWC_CIRCLEQ_NEXT(var, field))
11819 +
11820 +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
11821 + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
11822 + (var) != DWC_CIRCLEQ_END(head); \
11823 + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
11824 +
11825 +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
11826 + for((var) = DWC_CIRCLEQ_LAST(head); \
11827 + (var) != DWC_CIRCLEQ_END(head); \
11828 + (var) = DWC_CIRCLEQ_PREV(var, field))
11829 +
11830 +/*
11831 + * Circular queue functions.
11832 + */
11833 +#define DWC_CIRCLEQ_INIT(head) do { \
11834 + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
11835 + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
11836 +} while (0)
11837 +
11838 +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
11839 + (elm)->field.cqe_next = NULL; \
11840 + (elm)->field.cqe_prev = NULL; \
11841 +} while (0)
11842 +
11843 +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
11844 + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
11845 + (elm)->field.cqe_prev = (listelm); \
11846 + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
11847 + (head)->cqh_last = (elm); \
11848 + else \
11849 + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
11850 + (listelm)->field.cqe_next = (elm); \
11851 +} while (0)
11852 +
11853 +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
11854 + (elm)->field.cqe_next = (listelm); \
11855 + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
11856 + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
11857 + (head)->cqh_first = (elm); \
11858 + else \
11859 + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
11860 + (listelm)->field.cqe_prev = (elm); \
11861 +} while (0)
11862 +
11863 +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
11864 + (elm)->field.cqe_next = (head)->cqh_first; \
11865 + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
11866 + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
11867 + (head)->cqh_last = (elm); \
11868 + else \
11869 + (head)->cqh_first->field.cqe_prev = (elm); \
11870 + (head)->cqh_first = (elm); \
11871 +} while (0)
11872 +
11873 +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
11874 + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
11875 + (elm)->field.cqe_prev = (head)->cqh_last; \
11876 + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
11877 + (head)->cqh_first = (elm); \
11878 + else \
11879 + (head)->cqh_last->field.cqe_next = (elm); \
11880 + (head)->cqh_last = (elm); \
11881 +} while (0)
11882 +
11883 +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
11884 + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
11885 + (head)->cqh_last = (elm)->field.cqe_prev; \
11886 + else \
11887 + (elm)->field.cqe_next->field.cqe_prev = \
11888 + (elm)->field.cqe_prev; \
11889 + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
11890 + (head)->cqh_first = (elm)->field.cqe_next; \
11891 + else \
11892 + (elm)->field.cqe_prev->field.cqe_next = \
11893 + (elm)->field.cqe_next; \
11894 +} while (0)
11895 +
11896 +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
11897 + DWC_CIRCLEQ_REMOVE(head, elm, field); \
11898 + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
11899 +} while (0)
11900 +
11901 +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
11902 + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
11903 + DWC_CIRCLEQ_END(head)) \
11904 + (head).cqh_last = (elm2); \
11905 + else \
11906 + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
11907 + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
11908 + DWC_CIRCLEQ_END(head)) \
11909 + (head).cqh_first = (elm2); \
11910 + else \
11911 + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
11912 +} while (0)
11913 +
11914 +#ifdef __cplusplus
11915 +}
11916 +#endif
11917 +
11918 +#endif /* _DWC_LIST_H_ */
11919 --- /dev/null
11920 +++ b/drivers/usb/host/dwc_common_port/dwc_mem.c
11921 @@ -0,0 +1,245 @@
11922 +/* Memory Debugging */
11923 +#ifdef DWC_DEBUG_MEMORY
11924 +
11925 +#include "dwc_os.h"
11926 +#include "dwc_list.h"
11927 +
11928 +struct allocation {
11929 + void *addr;
11930 + void *ctx;
11931 + char *func;
11932 + int line;
11933 + uint32_t size;
11934 + int dma;
11935 + DWC_CIRCLEQ_ENTRY(allocation) entry;
11936 +};
11937 +
11938 +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
11939 +
11940 +struct allocation_manager {
11941 + void *mem_ctx;
11942 + struct allocation_queue allocations;
11943 +
11944 + /* statistics */
11945 + int num;
11946 + int num_freed;
11947 + int num_active;
11948 + uint32_t total;
11949 + uint32_t cur;
11950 + uint32_t max;
11951 +};
11952 +
11953 +static struct allocation_manager *manager = NULL;
11954 +
11955 +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
11956 + int dma)
11957 +{
11958 + struct allocation *a;
11959 +
11960 + DWC_ASSERT(manager != NULL, "manager not allocated");
11961 +
11962 + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
11963 + if (!a) {
11964 + return -DWC_E_NO_MEMORY;
11965 + }
11966 +
11967 + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
11968 + if (!a->func) {
11969 + __DWC_FREE(manager->mem_ctx, a);
11970 + return -DWC_E_NO_MEMORY;
11971 + }
11972 +
11973 + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
11974 + a->addr = addr;
11975 + a->ctx = ctx;
11976 + a->line = line;
11977 + a->size = size;
11978 + a->dma = dma;
11979 + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
11980 +
11981 + /* Update stats */
11982 + manager->num++;
11983 + manager->num_active++;
11984 + manager->total += size;
11985 + manager->cur += size;
11986 +
11987 + if (manager->max < manager->cur) {
11988 + manager->max = manager->cur;
11989 + }
11990 +
11991 + return 0;
11992 +}
11993 +
11994 +static struct allocation *find_allocation(void *ctx, void *addr)
11995 +{
11996 + struct allocation *a;
11997 +
11998 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11999 + if (a->ctx == ctx && a->addr == addr) {
12000 + return a;
12001 + }
12002 + }
12003 +
12004 + return NULL;
12005 +}
12006 +
12007 +static void free_allocation(void *ctx, void *addr, char const *func, int line)
12008 +{
12009 + struct allocation *a = find_allocation(ctx, addr);
12010 +
12011 + if (!a) {
12012 + DWC_ASSERT(0,
12013 + "Free of address %p that was never allocated or already freed %s:%d",
12014 + addr, func, line);
12015 + return;
12016 + }
12017 +
12018 + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
12019 +
12020 + manager->num_active--;
12021 + manager->num_freed++;
12022 + manager->cur -= a->size;
12023 + __DWC_FREE(manager->mem_ctx, a->func);
12024 + __DWC_FREE(manager->mem_ctx, a);
12025 +}
12026 +
12027 +int dwc_memory_debug_start(void *mem_ctx)
12028 +{
12029 + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
12030 +
12031 + if (manager) {
12032 + return -DWC_E_BUSY;
12033 + }
12034 +
12035 + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
12036 + if (!manager) {
12037 + return -DWC_E_NO_MEMORY;
12038 + }
12039 +
12040 + DWC_CIRCLEQ_INIT(&manager->allocations);
12041 + manager->mem_ctx = mem_ctx;
12042 + manager->num = 0;
12043 + manager->num_freed = 0;
12044 + manager->num_active = 0;
12045 + manager->total = 0;
12046 + manager->cur = 0;
12047 + manager->max = 0;
12048 +
12049 + return 0;
12050 +}
12051 +
12052 +void dwc_memory_debug_stop(void)
12053 +{
12054 + struct allocation *a;
12055 +
12056 + dwc_memory_debug_report();
12057 +
12058 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
12059 + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
12060 + free_allocation(a->ctx, a->addr, NULL, -1);
12061 + }
12062 +
12063 + __DWC_FREE(manager->mem_ctx, manager);
12064 +}
12065 +
12066 +void dwc_memory_debug_report(void)
12067 +{
12068 + struct allocation *a;
12069 +
12070 + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
12071 + DWC_PRINTF("Num Allocations = %d\n", manager->num);
12072 + DWC_PRINTF("Freed = %d\n", manager->num_freed);
12073 + DWC_PRINTF("Active = %d\n", manager->num_active);
12074 + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
12075 + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
12076 + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
12077 + DWC_PRINTF("Unfreed allocations:\n");
12078 +
12079 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
12080 + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
12081 + a->addr, a->size, a->func, a->line, a->dma);
12082 + }
12083 +}
12084 +
12085 +/* The replacement functions */
12086 +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
12087 +{
12088 + void *addr = __DWC_ALLOC(mem_ctx, size);
12089 +
12090 + if (!addr) {
12091 + return NULL;
12092 + }
12093 +
12094 + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
12095 + __DWC_FREE(mem_ctx, addr);
12096 + return NULL;
12097 + }
12098 +
12099 + return addr;
12100 +}
12101 +
12102 +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
12103 + int line)
12104 +{
12105 + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
12106 +
12107 + if (!addr) {
12108 + return NULL;
12109 + }
12110 +
12111 + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
12112 + __DWC_FREE(mem_ctx, addr);
12113 + return NULL;
12114 + }
12115 +
12116 + return addr;
12117 +}
12118 +
12119 +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
12120 +{
12121 + free_allocation(mem_ctx, addr, func, line);
12122 + __DWC_FREE(mem_ctx, addr);
12123 +}
12124 +
12125 +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
12126 + char const *func, int line)
12127 +{
12128 + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
12129 +
12130 + if (!addr) {
12131 + return NULL;
12132 + }
12133 +
12134 + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
12135 + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
12136 + return NULL;
12137 + }
12138 +
12139 + return addr;
12140 +}
12141 +
12142 +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
12143 + dwc_dma_t *dma_addr, char const *func, int line)
12144 +{
12145 + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
12146 +
12147 + if (!addr) {
12148 + return NULL;
12149 + }
12150 +
12151 + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
12152 + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
12153 + return NULL;
12154 + }
12155 +
12156 + return addr;
12157 +}
12158 +
12159 +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
12160 + dwc_dma_t dma_addr, char const *func, int line)
12161 +{
12162 + free_allocation(dma_ctx, virt_addr, func, line);
12163 + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
12164 +}
12165 +
12166 +#endif /* DWC_DEBUG_MEMORY */
12167 --- /dev/null
12168 +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.c
12169 @@ -0,0 +1,636 @@
12170 +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
12171 + *
12172 + * PuTTY is copyright 1997-2007 Simon Tatham.
12173 + *
12174 + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
12175 + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
12176 + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
12177 + * Kuhn, and CORE SDI S.A.
12178 + *
12179 + * Permission is hereby granted, free of charge, to any person
12180 + * obtaining a copy of this software and associated documentation files
12181 + * (the "Software"), to deal in the Software without restriction,
12182 + * including without limitation the rights to use, copy, modify, merge,
12183 + * publish, distribute, sublicense, and/or sell copies of the Software,
12184 + * and to permit persons to whom the Software is furnished to do so,
12185 + * subject to the following conditions:
12186 + *
12187 + * The above copyright notice and this permission notice shall be
12188 + * included in all copies or substantial portions of the Software.
12189 +
12190 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12191 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12192 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12193 + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
12194 + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
12195 + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
12196 + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12197 + *
12198 + */
12199 +#ifdef DWC_CRYPTOLIB
12200 +
12201 +#ifndef CONFIG_MACH_IPMATE
12202 +
12203 +#include "dwc_modpow.h"
12204 +
12205 +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
12206 +#define BIGNUM_TOP_BIT 0x80000000UL
12207 +#define BIGNUM_INT_BITS 32
12208 +
12209 +
12210 +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
12211 +{
12212 + void *p;
12213 + size *= n;
12214 + if (size == 0) size = 1;
12215 + p = dwc_alloc(mem_ctx, size);
12216 + return p;
12217 +}
12218 +
12219 +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
12220 +#define sfree dwc_free
12221 +
12222 +/*
12223 + * Usage notes:
12224 + * * Do not call the DIVMOD_WORD macro with expressions such as array
12225 + * subscripts, as some implementations object to this (see below).
12226 + * * Note that none of the division methods below will cope if the
12227 + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
12228 + * to avoid this case.
12229 + * If this condition occurs, in the case of the x86 DIV instruction,
12230 + * an overflow exception will occur, which (according to a correspondent)
12231 + * will manifest on Windows as something like
12232 + * 0xC0000095: Integer overflow
12233 + * The C variant won't give the right answer, either.
12234 + */
12235 +
12236 +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
12237 +
12238 +#if defined __GNUC__ && defined __i386__
12239 +#define DIVMOD_WORD(q, r, hi, lo, w) \
12240 + __asm__("div %2" : \
12241 + "=d" (r), "=a" (q) : \
12242 + "r" (w), "d" (hi), "a" (lo))
12243 +#else
12244 +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
12245 + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
12246 + q = n / w; \
12247 + r = n % w; \
12248 +} while (0)
12249 +#endif
12250 +
12251 +// q = n / w;
12252 +// r = n % w;
12253 +
12254 +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
12255 +
12256 +#define BIGNUM_INTERNAL
12257 +
12258 +static Bignum newbn(void *mem_ctx, int length)
12259 +{
12260 + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
12261 + //if (!b)
12262 + //abort(); /* FIXME */
12263 + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
12264 + b[0] = length;
12265 + return b;
12266 +}
12267 +
12268 +void freebn(void *mem_ctx, Bignum b)
12269 +{
12270 + /*
12271 + * Burn the evidence, just in case.
12272 + */
12273 + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
12274 + sfree(mem_ctx, b);
12275 +}
12276 +
12277 +/*
12278 + * Compute c = a * b.
12279 + * Input is in the first len words of a and b.
12280 + * Result is returned in the first 2*len words of c.
12281 + */
12282 +static void internal_mul(BignumInt *a, BignumInt *b,
12283 + BignumInt *c, int len)
12284 +{
12285 + int i, j;
12286 + BignumDblInt t;
12287 +
12288 + for (j = 0; j < 2 * len; j++)
12289 + c[j] = 0;
12290 +
12291 + for (i = len - 1; i >= 0; i--) {
12292 + t = 0;
12293 + for (j = len - 1; j >= 0; j--) {
12294 + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
12295 + t += (BignumDblInt) c[i + j + 1];
12296 + c[i + j + 1] = (BignumInt) t;
12297 + t = t >> BIGNUM_INT_BITS;
12298 + }
12299 + c[i] = (BignumInt) t;
12300 + }
12301 +}
12302 +
12303 +static void internal_add_shifted(BignumInt *number,
12304 + unsigned n, int shift)
12305 +{
12306 + int word = 1 + (shift / BIGNUM_INT_BITS);
12307 + int bshift = shift % BIGNUM_INT_BITS;
12308 + BignumDblInt addend;
12309 +
12310 + addend = (BignumDblInt)n << bshift;
12311 +
12312 + while (addend) {
12313 + addend += number[word];
12314 + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
12315 + addend >>= BIGNUM_INT_BITS;
12316 + word++;
12317 + }
12318 +}
12319 +
12320 +/*
12321 + * Compute a = a % m.
12322 + * Input in first alen words of a and first mlen words of m.
12323 + * Output in first alen words of a
12324 + * (of which first alen-mlen words will be zero).
12325 + * The MSW of m MUST have its high bit set.
12326 + * Quotient is accumulated in the `quotient' array, which is a Bignum
12327 + * rather than the internal bigendian format. Quotient parts are shifted
12328 + * left by `qshift' before adding into quot.
12329 + */
12330 +static void internal_mod(BignumInt *a, int alen,
12331 + BignumInt *m, int mlen,
12332 + BignumInt *quot, int qshift)
12333 +{
12334 + BignumInt m0, m1;
12335 + unsigned int h;
12336 + int i, k;
12337 +
12338 + m0 = m[0];
12339 + if (mlen > 1)
12340 + m1 = m[1];
12341 + else
12342 + m1 = 0;
12343 +
12344 + for (i = 0; i <= alen - mlen; i++) {
12345 + BignumDblInt t;
12346 + unsigned int q, r, c, ai1;
12347 +
12348 + if (i == 0) {
12349 + h = 0;
12350 + } else {
12351 + h = a[i - 1];
12352 + a[i - 1] = 0;
12353 + }
12354 +
12355 + if (i == alen - 1)
12356 + ai1 = 0;
12357 + else
12358 + ai1 = a[i + 1];
12359 +
12360 + /* Find q = h:a[i] / m0 */
12361 + if (h >= m0) {
12362 + /*
12363 + * Special case.
12364 + *
12365 + * To illustrate it, suppose a BignumInt is 8 bits, and
12366 + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
12367 + * our initial division will be 0xA123 / 0xA1, which
12368 + * will give a quotient of 0x100 and a divide overflow.
12369 + * However, the invariants in this division algorithm
12370 + * are not violated, since the full number A1:23:... is
12371 + * _less_ than the quotient prefix A1:B2:... and so the
12372 + * following correction loop would have sorted it out.
12373 + *
12374 + * In this situation we set q to be the largest
12375 + * quotient we _can_ stomach (0xFF, of course).
12376 + */
12377 + q = BIGNUM_INT_MASK;
12378 + } else {
12379 + /* Macro doesn't want an array subscript expression passed
12380 + * into it (see definition), so use a temporary. */
12381 + BignumInt tmplo = a[i];
12382 + DIVMOD_WORD(q, r, h, tmplo, m0);
12383 +
12384 + /* Refine our estimate of q by looking at
12385 + h:a[i]:a[i+1] / m0:m1 */
12386 + t = MUL_WORD(m1, q);
12387 + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
12388 + q--;
12389 + t -= m1;
12390 + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
12391 + if (r >= (BignumDblInt) m0 &&
12392 + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
12393 + }
12394 + }
12395 +
12396 + /* Subtract q * m from a[i...] */
12397 + c = 0;
12398 + for (k = mlen - 1; k >= 0; k--) {
12399 + t = MUL_WORD(q, m[k]);
12400 + t += c;
12401 + c = (unsigned)(t >> BIGNUM_INT_BITS);
12402 + if ((BignumInt) t > a[i + k])
12403 + c++;
12404 + a[i + k] -= (BignumInt) t;
12405 + }
12406 +
12407 + /* Add back m in case of borrow */
12408 + if (c != h) {
12409 + t = 0;
12410 + for (k = mlen - 1; k >= 0; k--) {
12411 + t += m[k];
12412 + t += a[i + k];
12413 + a[i + k] = (BignumInt) t;
12414 + t = t >> BIGNUM_INT_BITS;
12415 + }
12416 + q--;
12417 + }
12418 + if (quot)
12419 + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
12420 + }
12421 +}
12422 +
12423 +/*
12424 + * Compute p % mod.
12425 + * The most significant word of mod MUST be non-zero.
12426 + * We assume that the result array is the same size as the mod array.
12427 + * We optionally write out a quotient if `quotient' is non-NULL.
12428 + * We can avoid writing out the result if `result' is NULL.
12429 + */
12430 +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
12431 +{
12432 + BignumInt *n, *m;
12433 + int mshift;
12434 + int plen, mlen, i, j;
12435 +
12436 + /* Allocate m of size mlen, copy mod to m */
12437 + /* We use big endian internally */
12438 + mlen = mod[0];
12439 + m = snewn(mem_ctx, mlen, BignumInt);
12440 + //if (!m)
12441 + //abort(); /* FIXME */
12442 + for (j = 0; j < mlen; j++)
12443 + m[j] = mod[mod[0] - j];
12444 +
12445 + /* Shift m left to make msb bit set */
12446 + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
12447 + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
12448 + break;
12449 + if (mshift) {
12450 + for (i = 0; i < mlen - 1; i++)
12451 + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
12452 + m[mlen - 1] = m[mlen - 1] << mshift;
12453 + }
12454 +
12455 + plen = p[0];
12456 + /* Ensure plen > mlen */
12457 + if (plen <= mlen)
12458 + plen = mlen + 1;
12459 +
12460 + /* Allocate n of size plen, copy p to n */
12461 + n = snewn(mem_ctx, plen, BignumInt);
12462 + //if (!n)
12463 + //abort(); /* FIXME */
12464 + for (j = 0; j < plen; j++)
12465 + n[j] = 0;
12466 + for (j = 1; j <= (int)p[0]; j++)
12467 + n[plen - j] = p[j];
12468 +
12469 + /* Main computation */
12470 + internal_mod(n, plen, m, mlen, quotient, mshift);
12471 +
12472 + /* Fixup result in case the modulus was shifted */
12473 + if (mshift) {
12474 + for (i = plen - mlen - 1; i < plen - 1; i++)
12475 + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
12476 + n[plen - 1] = n[plen - 1] << mshift;
12477 + internal_mod(n, plen, m, mlen, quotient, 0);
12478 + for (i = plen - 1; i >= plen - mlen; i--)
12479 + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
12480 + }
12481 +
12482 + /* Copy result to buffer */
12483 + if (result) {
12484 + for (i = 1; i <= (int)result[0]; i++) {
12485 + int j = plen - i;
12486 + result[i] = j >= 0 ? n[j] : 0;
12487 + }
12488 + }
12489 +
12490 + /* Free temporary arrays */
12491 + for (i = 0; i < mlen; i++)
12492 + m[i] = 0;
12493 + sfree(mem_ctx, m);
12494 + for (i = 0; i < plen; i++)
12495 + n[i] = 0;
12496 + sfree(mem_ctx, n);
12497 +}
12498 +
12499 +/*
12500 + * Simple remainder.
12501 + */
12502 +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
12503 +{
12504 + Bignum r = newbn(mem_ctx, b[0]);
12505 + bigdivmod(mem_ctx, a, b, r, NULL);
12506 + return r;
12507 +}
12508 +
12509 +/*
12510 + * Compute (base ^ exp) % mod.
12511 + */
12512 +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
12513 +{
12514 + BignumInt *a, *b, *n, *m;
12515 + int mshift;
12516 + int mlen, i, j;
12517 + Bignum base, result;
12518 +
12519 + /*
12520 + * The most significant word of mod needs to be non-zero. It
12521 + * should already be, but let's make sure.
12522 + */
12523 + //assert(mod[mod[0]] != 0);
12524 +
12525 + /*
12526 + * Make sure the base is smaller than the modulus, by reducing
12527 + * it modulo the modulus if not.
12528 + */
12529 + base = bigmod(mem_ctx, base_in, mod);
12530 +
12531 + /* Allocate m of size mlen, copy mod to m */
12532 + /* We use big endian internally */
12533 + mlen = mod[0];
12534 + m = snewn(mem_ctx, mlen, BignumInt);
12535 + //if (!m)
12536 + //abort(); /* FIXME */
12537 + for (j = 0; j < mlen; j++)
12538 + m[j] = mod[mod[0] - j];
12539 +
12540 + /* Shift m left to make msb bit set */
12541 + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
12542 + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
12543 + break;
12544 + if (mshift) {
12545 + for (i = 0; i < mlen - 1; i++)
12546 + m[i] =
12547 + (m[i] << mshift) | (m[i + 1] >>
12548 + (BIGNUM_INT_BITS - mshift));
12549 + m[mlen - 1] = m[mlen - 1] << mshift;
12550 + }
12551 +
12552 + /* Allocate n of size mlen, copy base to n */
12553 + n = snewn(mem_ctx, mlen, BignumInt);
12554 + //if (!n)
12555 + //abort(); /* FIXME */
12556 + i = mlen - base[0];
12557 + for (j = 0; j < i; j++)
12558 + n[j] = 0;
12559 + for (j = 0; j < base[0]; j++)
12560 + n[i + j] = base[base[0] - j];
12561 +
12562 + /* Allocate a and b of size 2*mlen. Set a = 1 */
12563 + a = snewn(mem_ctx, 2 * mlen, BignumInt);
12564 + //if (!a)
12565 + //abort(); /* FIXME */
12566 + b = snewn(mem_ctx, 2 * mlen, BignumInt);
12567 + //if (!b)
12568 + //abort(); /* FIXME */
12569 + for (i = 0; i < 2 * mlen; i++)
12570 + a[i] = 0;
12571 + a[2 * mlen - 1] = 1;
12572 +
12573 + /* Skip leading zero bits of exp. */
12574 + i = 0;
12575 + j = BIGNUM_INT_BITS - 1;
12576 + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
12577 + j--;
12578 + if (j < 0) {
12579 + i++;
12580 + j = BIGNUM_INT_BITS - 1;
12581 + }
12582 + }
12583 +
12584 + /* Main computation */
12585 + while (i < exp[0]) {
12586 + while (j >= 0) {
12587 + internal_mul(a + mlen, a + mlen, b, mlen);
12588 + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
12589 + if ((exp[exp[0] - i] & (1 << j)) != 0) {
12590 + internal_mul(b + mlen, n, a, mlen);
12591 + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
12592 + } else {
12593 + BignumInt *t;
12594 + t = a;
12595 + a = b;
12596 + b = t;
12597 + }
12598 + j--;
12599 + }
12600 + i++;
12601 + j = BIGNUM_INT_BITS - 1;
12602 + }
12603 +
12604 + /* Fixup result in case the modulus was shifted */
12605 + if (mshift) {
12606 + for (i = mlen - 1; i < 2 * mlen - 1; i++)
12607 + a[i] =
12608 + (a[i] << mshift) | (a[i + 1] >>
12609 + (BIGNUM_INT_BITS - mshift));
12610 + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
12611 + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
12612 + for (i = 2 * mlen - 1; i >= mlen; i--)
12613 + a[i] =
12614 + (a[i] >> mshift) | (a[i - 1] <<
12615 + (BIGNUM_INT_BITS - mshift));
12616 + }
12617 +
12618 + /* Copy result to buffer */
12619 + result = newbn(mem_ctx, mod[0]);
12620 + for (i = 0; i < mlen; i++)
12621 + result[result[0] - i] = a[i + mlen];
12622 + while (result[0] > 1 && result[result[0]] == 0)
12623 + result[0]--;
12624 +
12625 + /* Free temporary arrays */
12626 + for (i = 0; i < 2 * mlen; i++)
12627 + a[i] = 0;
12628 + sfree(mem_ctx, a);
12629 + for (i = 0; i < 2 * mlen; i++)
12630 + b[i] = 0;
12631 + sfree(mem_ctx, b);
12632 + for (i = 0; i < mlen; i++)
12633 + m[i] = 0;
12634 + sfree(mem_ctx, m);
12635 + for (i = 0; i < mlen; i++)
12636 + n[i] = 0;
12637 + sfree(mem_ctx, n);
12638 +
12639 + freebn(mem_ctx, base);
12640 +
12641 + return result;
12642 +}
12643 +
12644 +
12645 +#ifdef UNITTEST
12646 +
12647 +static __u32 dh_p[] = {
12648 + 96,
12649 + 0xFFFFFFFF,
12650 + 0xFFFFFFFF,
12651 + 0xA93AD2CA,
12652 + 0x4B82D120,
12653 + 0xE0FD108E,
12654 + 0x43DB5BFC,
12655 + 0x74E5AB31,
12656 + 0x08E24FA0,
12657 + 0xBAD946E2,
12658 + 0x770988C0,
12659 + 0x7A615D6C,
12660 + 0xBBE11757,
12661 + 0x177B200C,
12662 + 0x521F2B18,
12663 + 0x3EC86A64,
12664 + 0xD8760273,
12665 + 0xD98A0864,
12666 + 0xF12FFA06,
12667 + 0x1AD2EE6B,
12668 + 0xCEE3D226,
12669 + 0x4A25619D,
12670 + 0x1E8C94E0,
12671 + 0xDB0933D7,
12672 + 0xABF5AE8C,
12673 + 0xA6E1E4C7,
12674 + 0xB3970F85,
12675 + 0x5D060C7D,
12676 + 0x8AEA7157,
12677 + 0x58DBEF0A,
12678 + 0xECFB8504,
12679 + 0xDF1CBA64,
12680 + 0xA85521AB,
12681 + 0x04507A33,
12682 + 0xAD33170D,
12683 + 0x8AAAC42D,
12684 + 0x15728E5A,
12685 + 0x98FA0510,
12686 + 0x15D22618,
12687 + 0xEA956AE5,
12688 + 0x3995497C,
12689 + 0x95581718,
12690 + 0xDE2BCBF6,
12691 + 0x6F4C52C9,
12692 + 0xB5C55DF0,
12693 + 0xEC07A28F,
12694 + 0x9B2783A2,
12695 + 0x180E8603,
12696 + 0xE39E772C,
12697 + 0x2E36CE3B,
12698 + 0x32905E46,
12699 + 0xCA18217C,
12700 + 0xF1746C08,
12701 + 0x4ABC9804,
12702 + 0x670C354E,
12703 + 0x7096966D,
12704 + 0x9ED52907,
12705 + 0x208552BB,
12706 + 0x1C62F356,
12707 + 0xDCA3AD96,
12708 + 0x83655D23,
12709 + 0xFD24CF5F,
12710 + 0x69163FA8,
12711 + 0x1C55D39A,
12712 + 0x98DA4836,
12713 + 0xA163BF05,
12714 + 0xC2007CB8,
12715 + 0xECE45B3D,
12716 + 0x49286651,
12717 + 0x7C4B1FE6,
12718 + 0xAE9F2411,
12719 + 0x5A899FA5,
12720 + 0xEE386BFB,
12721 + 0xF406B7ED,
12722 + 0x0BFF5CB6,
12723 + 0xA637ED6B,
12724 + 0xF44C42E9,
12725 + 0x625E7EC6,
12726 + 0xE485B576,
12727 + 0x6D51C245,
12728 + 0x4FE1356D,
12729 + 0xF25F1437,
12730 + 0x302B0A6D,
12731 + 0xCD3A431B,
12732 + 0xEF9519B3,
12733 + 0x8E3404DD,
12734 + 0x514A0879,
12735 + 0x3B139B22,
12736 + 0x020BBEA6,
12737 + 0x8A67CC74,
12738 + 0x29024E08,
12739 + 0x80DC1CD1,
12740 + 0xC4C6628B,
12741 + 0x2168C234,
12742 + 0xC90FDAA2,
12743 + 0xFFFFFFFF,
12744 + 0xFFFFFFFF,
12745 +};
12746 +
12747 +static __u32 dh_a[] = {
12748 + 8,
12749 + 0xdf367516,
12750 + 0x86459caa,
12751 + 0xe2d459a4,
12752 + 0xd910dae0,
12753 + 0x8a8b5e37,
12754 + 0x67ab31c6,
12755 + 0xf0b55ea9,
12756 + 0x440051d6,
12757 +};
12758 +
12759 +static __u32 dh_b[] = {
12760 + 8,
12761 + 0xded92656,
12762 + 0xe07a048a,
12763 + 0x6fa452cd,
12764 + 0x2df89d30,
12765 + 0xc75f1b0f,
12766 + 0x8ce3578f,
12767 + 0x7980a324,
12768 + 0x5daec786,
12769 +};
12770 +
12771 +static __u32 dh_g[] = {
12772 + 1,
12773 + 2,
12774 +};
12775 +
12776 +int main(void)
12777 +{
12778 + int i;
12779 + __u32 *k;
12780 + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
12781 +
12782 + printf("\n\n");
12783 + for (i=0; i<k[0]; i++) {
12784 + __u32 word32 = k[k[0] - i];
12785 + __u16 l = word32 & 0xffff;
12786 + __u16 m = (word32 & 0xffff0000) >> 16;
12787 + printf("%04x %04x ", m, l);
12788 + if (!((i + 1)%13)) printf("\n");
12789 + }
12790 + printf("\n\n");
12791 +
12792 + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
12793 + printf("PASS\n\n");
12794 + }
12795 + else {
12796 + printf("FAIL\n\n");
12797 + }
12798 +
12799 +}
12800 +
12801 +#endif /* UNITTEST */
12802 +
12803 +#endif /* CONFIG_MACH_IPMATE */
12804 +
12805 +#endif /*DWC_CRYPTOLIB */
12806 --- /dev/null
12807 +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.h
12808 @@ -0,0 +1,34 @@
12809 +/*
12810 + * dwc_modpow.h
12811 + * See dwc_modpow.c for license and changes
12812 + */
12813 +#ifndef _DWC_MODPOW_H
12814 +#define _DWC_MODPOW_H
12815 +
12816 +#ifdef __cplusplus
12817 +extern "C" {
12818 +#endif
12819 +
12820 +#include "dwc_os.h"
12821 +
12822 +/** @file
12823 + *
12824 + * This file defines the module exponentiation function which is only used
12825 + * internally by the DWC UWB modules for calculation of PKs during numeric
12826 + * association. The routine is taken from the PUTTY, an open source terminal
12827 + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
12828 + *
12829 + */
12830 +
12831 +typedef uint32_t BignumInt;
12832 +typedef uint64_t BignumDblInt;
12833 +typedef BignumInt *Bignum;
12834 +
12835 +/* Compute modular exponentiaion */
12836 +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
12837 +
12838 +#ifdef __cplusplus
12839 +}
12840 +#endif
12841 +
12842 +#endif /* _LINUX_BIGNUM_H */
12843 --- /dev/null
12844 +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.c
12845 @@ -0,0 +1,319 @@
12846 +#ifdef DWC_NOTIFYLIB
12847 +
12848 +#include "dwc_notifier.h"
12849 +#include "dwc_list.h"
12850 +
12851 +typedef struct dwc_observer {
12852 + void *observer;
12853 + dwc_notifier_callback_t callback;
12854 + void *data;
12855 + char *notification;
12856 + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
12857 +} observer_t;
12858 +
12859 +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
12860 +
12861 +typedef struct dwc_notifier {
12862 + void *mem_ctx;
12863 + void *object;
12864 + struct observer_queue observers;
12865 + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
12866 +} notifier_t;
12867 +
12868 +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
12869 +
12870 +typedef struct manager {
12871 + void *mem_ctx;
12872 + void *wkq_ctx;
12873 + dwc_workq_t *wq;
12874 +// dwc_mutex_t *mutex;
12875 + struct notifier_queue notifiers;
12876 +} manager_t;
12877 +
12878 +static manager_t *manager = NULL;
12879 +
12880 +static int create_manager(void *mem_ctx, void *wkq_ctx)
12881 +{
12882 + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
12883 + if (!manager) {
12884 + return -DWC_E_NO_MEMORY;
12885 + }
12886 +
12887 + DWC_CIRCLEQ_INIT(&manager->notifiers);
12888 +
12889 + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
12890 + if (!manager->wq) {
12891 + return -DWC_E_NO_MEMORY;
12892 + }
12893 +
12894 + return 0;
12895 +}
12896 +
12897 +static void free_manager(void)
12898 +{
12899 + dwc_workq_free(manager->wq);
12900 +
12901 + /* All notifiers must have unregistered themselves before this module
12902 + * can be removed. Hitting this assertion indicates a programmer
12903 + * error. */
12904 + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
12905 + "Notification manager being freed before all notifiers have been removed");
12906 + dwc_free(manager->mem_ctx, manager);
12907 +}
12908 +
12909 +#ifdef DEBUG
12910 +static void dump_manager(void)
12911 +{
12912 + notifier_t *n;
12913 + observer_t *o;
12914 +
12915 + DWC_ASSERT(manager, "Notification manager not found");
12916 +
12917 + DWC_DEBUG("List of all notifiers and observers:\n");
12918 + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
12919 + DWC_DEBUG("Notifier %p has observers:\n", n->object);
12920 + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
12921 + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
12922 + }
12923 + }
12924 +}
12925 +#else
12926 +#define dump_manager(...)
12927 +#endif
12928 +
12929 +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
12930 + dwc_notifier_callback_t callback, void *data)
12931 +{
12932 + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
12933 +
12934 + if (!new_observer) {
12935 + return NULL;
12936 + }
12937 +
12938 + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
12939 + new_observer->observer = observer;
12940 + new_observer->notification = notification;
12941 + new_observer->callback = callback;
12942 + new_observer->data = data;
12943 + return new_observer;
12944 +}
12945 +
12946 +static void free_observer(void *mem_ctx, observer_t *observer)
12947 +{
12948 + dwc_free(mem_ctx, observer);
12949 +}
12950 +
12951 +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
12952 +{
12953 + notifier_t *notifier;
12954 +
12955 + if (!object) {
12956 + return NULL;
12957 + }
12958 +
12959 + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
12960 + if (!notifier) {
12961 + return NULL;
12962 + }
12963 +
12964 + DWC_CIRCLEQ_INIT(&notifier->observers);
12965 + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
12966 +
12967 + notifier->mem_ctx = mem_ctx;
12968 + notifier->object = object;
12969 + return notifier;
12970 +}
12971 +
12972 +static void free_notifier(notifier_t *notifier)
12973 +{
12974 + observer_t *observer;
12975 +
12976 + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
12977 + free_observer(notifier->mem_ctx, observer);
12978 + }
12979 +
12980 + dwc_free(notifier->mem_ctx, notifier);
12981 +}
12982 +
12983 +static notifier_t *find_notifier(void *object)
12984 +{
12985 + notifier_t *notifier;
12986 +
12987 + DWC_ASSERT(manager, "Notification manager not found");
12988 +
12989 + if (!object) {
12990 + return NULL;
12991 + }
12992 +
12993 + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
12994 + if (notifier->object == object) {
12995 + return notifier;
12996 + }
12997 + }
12998 +
12999 + return NULL;
13000 +}
13001 +
13002 +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
13003 +{
13004 + return create_manager(mem_ctx, wkq_ctx);
13005 +}
13006 +
13007 +void dwc_free_notification_manager(void)
13008 +{
13009 + free_manager();
13010 +}
13011 +
13012 +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
13013 +{
13014 + notifier_t *notifier;
13015 +
13016 + DWC_ASSERT(manager, "Notification manager not found");
13017 +
13018 + notifier = find_notifier(object);
13019 + if (notifier) {
13020 + DWC_ERROR("Notifier %p is already registered\n", object);
13021 + return NULL;
13022 + }
13023 +
13024 + notifier = alloc_notifier(mem_ctx, object);
13025 + if (!notifier) {
13026 + return NULL;
13027 + }
13028 +
13029 + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
13030 +
13031 + DWC_INFO("Notifier %p registered", object);
13032 + dump_manager();
13033 +
13034 + return notifier;
13035 +}
13036 +
13037 +void dwc_unregister_notifier(dwc_notifier_t *notifier)
13038 +{
13039 + DWC_ASSERT(manager, "Notification manager not found");
13040 +
13041 + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
13042 + observer_t *o;
13043 +
13044 + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
13045 + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
13046 + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
13047 + }
13048 +
13049 + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
13050 + "Notifier %p has active observers when removing", notifier);
13051 + }
13052 +
13053 + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
13054 + free_notifier(notifier);
13055 +
13056 + DWC_INFO("Notifier unregistered");
13057 + dump_manager();
13058 +}
13059 +
13060 +/* Add an observer to observe the notifier for a particular state, event, or notification. */
13061 +int dwc_add_observer(void *observer, void *object, char *notification,
13062 + dwc_notifier_callback_t callback, void *data)
13063 +{
13064 + notifier_t *notifier = find_notifier(object);
13065 + observer_t *new_observer;
13066 +
13067 + if (!notifier) {
13068 + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
13069 + return -DWC_E_INVALID;
13070 + }
13071 +
13072 + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
13073 + if (!new_observer) {
13074 + return -DWC_E_NO_MEMORY;
13075 + }
13076 +
13077 + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
13078 +
13079 + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
13080 + observer, object, notification, callback, data);
13081 +
13082 + dump_manager();
13083 + return 0;
13084 +}
13085 +
13086 +int dwc_remove_observer(void *observer)
13087 +{
13088 + notifier_t *n;
13089 +
13090 + DWC_ASSERT(manager, "Notification manager not found");
13091 +
13092 + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
13093 + observer_t *o;
13094 + observer_t *o2;
13095 +
13096 + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
13097 + if (o->observer == observer) {
13098 + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
13099 + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
13100 + o->observer, n->object, o->notification);
13101 + free_observer(n->mem_ctx, o);
13102 + }
13103 + }
13104 + }
13105 +
13106 + dump_manager();
13107 + return 0;
13108 +}
13109 +
13110 +typedef struct callback_data {
13111 + void *mem_ctx;
13112 + dwc_notifier_callback_t cb;
13113 + void *observer;
13114 + void *data;
13115 + void *object;
13116 + char *notification;
13117 + void *notification_data;
13118 +} cb_data_t;
13119 +
13120 +static void cb_task(void *data)
13121 +{
13122 + cb_data_t *cb = (cb_data_t *)data;
13123 +
13124 + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
13125 + dwc_free(cb->mem_ctx, cb);
13126 +}
13127 +
13128 +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
13129 +{
13130 + observer_t *o;
13131 +
13132 + DWC_ASSERT(manager, "Notification manager not found");
13133 +
13134 + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
13135 + int len = DWC_STRLEN(notification);
13136 +
13137 + if (DWC_STRLEN(o->notification) != len) {
13138 + continue;
13139 + }
13140 +
13141 + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
13142 + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
13143 +
13144 + if (!cb_data) {
13145 + DWC_ERROR("Failed to allocate callback data\n");
13146 + return;
13147 + }
13148 +
13149 + cb_data->mem_ctx = notifier->mem_ctx;
13150 + cb_data->cb = o->callback;
13151 + cb_data->observer = o->observer;
13152 + cb_data->data = o->data;
13153 + cb_data->object = notifier->object;
13154 + cb_data->notification = notification;
13155 + cb_data->notification_data = notification_data;
13156 + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
13157 + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
13158 + "Notify callback from %p for Notification %s, to observer %p",
13159 + cb_data->object, notification, cb_data->observer);
13160 + }
13161 + }
13162 +}
13163 +
13164 +#endif /* DWC_NOTIFYLIB */
13165 --- /dev/null
13166 +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.h
13167 @@ -0,0 +1,122 @@
13168 +
13169 +#ifndef __DWC_NOTIFIER_H__
13170 +#define __DWC_NOTIFIER_H__
13171 +
13172 +#ifdef __cplusplus
13173 +extern "C" {
13174 +#endif
13175 +
13176 +#include "dwc_os.h"
13177 +
13178 +/** @file
13179 + *
13180 + * A simple implementation of the Observer pattern. Any "module" can
13181 + * register as an observer or notifier. The notion of "module" is abstract and
13182 + * can mean anything used to identify either an observer or notifier. Usually
13183 + * it will be a pointer to a data structure which contains some state, ie an
13184 + * object.
13185 + *
13186 + * Before any notifiers can be added, the global notification manager must be
13187 + * brought up with dwc_alloc_notification_manager().
13188 + * dwc_free_notification_manager() will bring it down and free all resources.
13189 + * These would typically be called upon module load and unload. The
13190 + * notification manager is a single global instance that handles all registered
13191 + * observable modules and observers so this should be done only once.
13192 + *
13193 + * A module can be observable by using Notifications to publicize some general
13194 + * information about it's state or operation. It does not care who listens, or
13195 + * even if anyone listens, or what they do with the information. The observable
13196 + * modules do not need to know any information about it's observers or their
13197 + * interface, or their state or data.
13198 + *
13199 + * Any module can register to emit Notifications. It should publish a list of
13200 + * notifications that it can emit and their behavior, such as when they will get
13201 + * triggered, and what information will be provided to the observer. Then it
13202 + * should register itself as an observable module. See dwc_register_notifier().
13203 + *
13204 + * Any module can observe any observable, registered module, provided it has a
13205 + * handle to the other module and knows what notifications to observe. See
13206 + * dwc_add_observer().
13207 + *
13208 + * A function of type dwc_notifier_callback_t is called whenever a notification
13209 + * is triggered with one or more observers observing it. This function is
13210 + * called in it's own process so it may sleep or block if needed. It is
13211 + * guaranteed to be called sometime after the notification has occurred and will
13212 + * be called once per each time the notification is triggered. It will NOT be
13213 + * called in the same process context used to trigger the notification.
13214 + *
13215 + * @section Limitiations
13216 + *
13217 + * Keep in mind that Notifications that can be triggered in rapid sucession may
13218 + * schedule too many processes too handle. Be aware of this limitation when
13219 + * designing to use notifications, and only add notifications for appropriate
13220 + * observable information.
13221 + *
13222 + * Also Notification callbacks are not synchronous. If you need to synchronize
13223 + * the behavior between module/observer you must use other means. And perhaps
13224 + * that will mean Notifications are not the proper solution.
13225 + */
13226 +
13227 +struct dwc_notifier;
13228 +typedef struct dwc_notifier dwc_notifier_t;
13229 +
13230 +/** The callback function must be of this type.
13231 + *
13232 + * @param object This is the object that is being observed.
13233 + * @param notification This is the notification that was triggered.
13234 + * @param observer This is the observer
13235 + * @param notification_data This is notification-specific data that the notifier
13236 + * has included in this notification. The value of this should be published in
13237 + * the documentation of the observable module with the notifications.
13238 + * @param user_data This is any custom data that the observer provided when
13239 + * adding itself as an observer to the notification. */
13240 +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
13241 + void *notification_data, void *user_data);
13242 +
13243 +/** Brings up the notification manager. */
13244 +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
13245 +/** Brings down the notification manager. */
13246 +extern void dwc_free_notification_manager(void);
13247 +
13248 +/** This function registers an observable module. A dwc_notifier_t object is
13249 + * returned to the observable module. This is an opaque object that is used by
13250 + * the observable module to trigger notifications. This object should only be
13251 + * accessible to functions that are authorized to trigger notifications for this
13252 + * module. Observers do not need this object. */
13253 +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
13254 +
13255 +/** This function unregisters an observable module. All observers have to be
13256 + * removed prior to unregistration. */
13257 +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
13258 +
13259 +/** Add a module as an observer to the observable module. The observable module
13260 + * needs to have previously registered with the notification manager.
13261 + *
13262 + * @param observer The observer module
13263 + * @param object The module to observe
13264 + * @param notification The notification to observe
13265 + * @param callback The callback function to call
13266 + * @param user_data Any additional user data to pass into the callback function */
13267 +extern int dwc_add_observer(void *observer, void *object, char *notification,
13268 + dwc_notifier_callback_t callback, void *user_data);
13269 +
13270 +/** Removes the specified observer from all notifications that it is currently
13271 + * observing. */
13272 +extern int dwc_remove_observer(void *observer);
13273 +
13274 +/** This function triggers a Notification. It should be called by the
13275 + * observable module, or any module or library which the observable module
13276 + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
13277 + *
13278 + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
13279 + * their own process context for each trigger. Callbacks can be blocking.
13280 + * dwc_notify can be called from interrupt context if needed.
13281 + *
13282 + */
13283 +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
13284 +
13285 +#ifdef __cplusplus
13286 +}
13287 +#endif
13288 +
13289 +#endif /* __DWC_NOTIFIER_H__ */
13290 --- /dev/null
13291 +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
13292 @@ -0,0 +1,1275 @@
13293 +/* =========================================================================
13294 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
13295 + * $Revision: #14 $
13296 + * $Date: 2010/11/04 $
13297 + * $Change: 1621695 $
13298 + *
13299 + * Synopsys Portability Library Software and documentation
13300 + * (hereinafter, "Software") is an Unsupported proprietary work of
13301 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
13302 + * between Synopsys and you.
13303 + *
13304 + * The Software IS NOT an item of Licensed Software or Licensed Product
13305 + * under any End User Software License Agreement or Agreement for
13306 + * Licensed Product with Synopsys or any supplement thereto. You are
13307 + * permitted to use and redistribute this Software in source and binary
13308 + * forms, with or without modification, provided that redistributions
13309 + * of source code must retain this notice. You may not view, use,
13310 + * disclose, copy or distribute this file or any information contained
13311 + * herein except pursuant to this license grant from Synopsys. If you
13312 + * do not agree with this notice, including the disclaimer below, then
13313 + * you are not authorized to use the Software.
13314 + *
13315 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
13316 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13317 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13318 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
13319 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
13320 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
13321 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
13322 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
13323 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13324 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
13325 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
13326 + * DAMAGE.
13327 + * ========================================================================= */
13328 +#ifndef _DWC_OS_H_
13329 +#define _DWC_OS_H_
13330 +
13331 +#ifdef __cplusplus
13332 +extern "C" {
13333 +#endif
13334 +
13335 +/** @file
13336 + *
13337 + * DWC portability library, low level os-wrapper functions
13338 + *
13339 + */
13340 +
13341 +/* These basic types need to be defined by some OS header file or custom header
13342 + * file for your specific target architecture.
13343 + *
13344 + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
13345 + *
13346 + * Any custom or alternate header file must be added and enabled here.
13347 + */
13348 +
13349 +#ifdef DWC_LINUX
13350 +# include <linux/types.h>
13351 +# ifdef CONFIG_DEBUG_MUTEXES
13352 +# include <linux/mutex.h>
13353 +# endif
13354 +# include <linux/spinlock.h>
13355 +# include <linux/errno.h>
13356 +#endif
13357 +
13358 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13359 +# include <os_dep.h>
13360 +#endif
13361 +
13362 +
13363 +/** @name Primitive Types and Values */
13364 +
13365 +/** We define a boolean type for consistency. Can be either YES or NO */
13366 +typedef uint8_t dwc_bool_t;
13367 +#define YES 1
13368 +#define NO 0
13369 +
13370 +#ifdef DWC_LINUX
13371 +
13372 +/** @name Error Codes */
13373 +#define DWC_E_INVALID EINVAL
13374 +#define DWC_E_NO_MEMORY ENOMEM
13375 +#define DWC_E_NO_DEVICE ENODEV
13376 +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
13377 +#define DWC_E_TIMEOUT ETIMEDOUT
13378 +#define DWC_E_BUSY EBUSY
13379 +#define DWC_E_AGAIN EAGAIN
13380 +#define DWC_E_RESTART ERESTART
13381 +#define DWC_E_ABORT ECONNABORTED
13382 +#define DWC_E_SHUTDOWN ESHUTDOWN
13383 +#define DWC_E_NO_DATA ENODATA
13384 +#define DWC_E_DISCONNECT ECONNRESET
13385 +#define DWC_E_UNKNOWN EINVAL
13386 +#define DWC_E_NO_STREAM_RES ENOSR
13387 +#define DWC_E_COMMUNICATION ECOMM
13388 +#define DWC_E_OVERFLOW EOVERFLOW
13389 +#define DWC_E_PROTOCOL EPROTO
13390 +#define DWC_E_IN_PROGRESS EINPROGRESS
13391 +#define DWC_E_PIPE EPIPE
13392 +#define DWC_E_IO EIO
13393 +#define DWC_E_NO_SPACE ENOSPC
13394 +
13395 +#else
13396 +
13397 +/** @name Error Codes */
13398 +#define DWC_E_INVALID 1001
13399 +#define DWC_E_NO_MEMORY 1002
13400 +#define DWC_E_NO_DEVICE 1003
13401 +#define DWC_E_NOT_SUPPORTED 1004
13402 +#define DWC_E_TIMEOUT 1005
13403 +#define DWC_E_BUSY 1006
13404 +#define DWC_E_AGAIN 1007
13405 +#define DWC_E_RESTART 1008
13406 +#define DWC_E_ABORT 1009
13407 +#define DWC_E_SHUTDOWN 1010
13408 +#define DWC_E_NO_DATA 1011
13409 +#define DWC_E_DISCONNECT 2000
13410 +#define DWC_E_UNKNOWN 3000
13411 +#define DWC_E_NO_STREAM_RES 4001
13412 +#define DWC_E_COMMUNICATION 4002
13413 +#define DWC_E_OVERFLOW 4003
13414 +#define DWC_E_PROTOCOL 4004
13415 +#define DWC_E_IN_PROGRESS 4005
13416 +#define DWC_E_PIPE 4006
13417 +#define DWC_E_IO 4007
13418 +#define DWC_E_NO_SPACE 4008
13419 +
13420 +#endif
13421 +
13422 +
13423 +/** @name Tracing/Logging Functions
13424 + *
13425 + * These function provide the capability to add tracing, debugging, and error
13426 + * messages, as well exceptions as assertions. The WUDEV uses these
13427 + * extensively. These could be logged to the main console, the serial port, an
13428 + * internal buffer, etc. These functions could also be no-op if they are too
13429 + * expensive on your system. By default undefining the DEBUG macro already
13430 + * no-ops some of these functions. */
13431 +
13432 +/** Returns non-zero if in interrupt context. */
13433 +extern dwc_bool_t DWC_IN_IRQ(void);
13434 +#define dwc_in_irq DWC_IN_IRQ
13435 +
13436 +/** Returns "IRQ" if DWC_IN_IRQ is true. */
13437 +static inline char *dwc_irq(void) {
13438 + return DWC_IN_IRQ() ? "IRQ" : "";
13439 +}
13440 +
13441 +/** Returns non-zero if in bottom-half context. */
13442 +extern dwc_bool_t DWC_IN_BH(void);
13443 +#define dwc_in_bh DWC_IN_BH
13444 +
13445 +/** Returns "BH" if DWC_IN_BH is true. */
13446 +static inline char *dwc_bh(void) {
13447 + return DWC_IN_BH() ? "BH" : "";
13448 +}
13449 +
13450 +/**
13451 + * A vprintf() clone. Just call vprintf if you've got it.
13452 + */
13453 +extern void DWC_VPRINTF(char *format, va_list args);
13454 +#define dwc_vprintf DWC_VPRINTF
13455 +
13456 +/**
13457 + * A vsnprintf() clone. Just call vprintf if you've got it.
13458 + */
13459 +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
13460 +#define dwc_vsnprintf DWC_VSNPRINTF
13461 +
13462 +/**
13463 + * printf() clone. Just call printf if you've go it.
13464 + */
13465 +extern void DWC_PRINTF(char *format, ...)
13466 +/* This provides compiler level static checking of the parameters if you're
13467 + * using GCC. */
13468 +#ifdef __GNUC__
13469 + __attribute__ ((format(printf, 1, 2)));
13470 +#else
13471 + ;
13472 +#endif
13473 +#define dwc_printf DWC_PRINTF
13474 +
13475 +/**
13476 + * sprintf() clone. Just call sprintf if you've got it.
13477 + */
13478 +extern int DWC_SPRINTF(char *string, char *format, ...)
13479 +#ifdef __GNUC__
13480 + __attribute__ ((format(printf, 2, 3)));
13481 +#else
13482 + ;
13483 +#endif
13484 +#define dwc_sprintf DWC_SPRINTF
13485 +
13486 +/**
13487 + * snprintf() clone. Just call snprintf if you've got it.
13488 + */
13489 +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
13490 +#ifdef __GNUC__
13491 + __attribute__ ((format(printf, 3, 4)));
13492 +#else
13493 + ;
13494 +#endif
13495 +#define dwc_snprintf DWC_SNPRINTF
13496 +
13497 +/**
13498 + * Prints a WARNING message. On systems that don't differentiate between
13499 + * warnings and regular log messages, just print it. Indicates that something
13500 + * may be wrong with the driver. Works like printf().
13501 + *
13502 + * Use the DWC_WARN macro to call this function.
13503 + */
13504 +extern void __DWC_WARN(char *format, ...)
13505 +#ifdef __GNUC__
13506 + __attribute__ ((format(printf, 1, 2)));
13507 +#else
13508 + ;
13509 +#endif
13510 +
13511 +/**
13512 + * Prints an error message. On systems that don't differentiate between errors
13513 + * and regular log messages, just print it. Indicates that something went wrong
13514 + * with the driver. Works like printf().
13515 + *
13516 + * Use the DWC_ERROR macro to call this function.
13517 + */
13518 +extern void __DWC_ERROR(char *format, ...)
13519 +#ifdef __GNUC__
13520 + __attribute__ ((format(printf, 1, 2)));
13521 +#else
13522 + ;
13523 +#endif
13524 +
13525 +/**
13526 + * Prints an exception error message and takes some user-defined action such as
13527 + * print out a backtrace or trigger a breakpoint. Indicates that something went
13528 + * abnormally wrong with the driver such as programmer error, or other
13529 + * exceptional condition. It should not be ignored so even on systems without
13530 + * printing capability, some action should be taken to notify the developer of
13531 + * it. Works like printf().
13532 + */
13533 +extern void DWC_EXCEPTION(char *format, ...)
13534 +#ifdef __GNUC__
13535 + __attribute__ ((format(printf, 1, 2)));
13536 +#else
13537 + ;
13538 +#endif
13539 +#define dwc_exception DWC_EXCEPTION
13540 +
13541 +#ifndef DWC_OTG_DEBUG_LEV
13542 +#define DWC_OTG_DEBUG_LEV 0
13543 +#endif
13544 +
13545 +#ifdef DEBUG
13546 +/**
13547 + * Prints out a debug message. Used for logging/trace messages.
13548 + *
13549 + * Use the DWC_DEBUG macro to call this function
13550 + */
13551 +extern void __DWC_DEBUG(char *format, ...)
13552 +#ifdef __GNUC__
13553 + __attribute__ ((format(printf, 1, 2)));
13554 +#else
13555 + ;
13556 +#endif
13557 +#else
13558 +#define __DWC_DEBUG printk
13559 +#endif
13560 +
13561 +/**
13562 + * Prints out a Debug message.
13563 + */
13564 +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
13565 + __func__, dwc_irq(), ## _args)
13566 +#define dwc_debug DWC_DEBUG
13567 +/**
13568 + * Prints out a Debug message if enabled at compile time.
13569 + */
13570 +#if DWC_OTG_DEBUG_LEV > 0
13571 +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
13572 +#else
13573 +#define DWC_DEBUGC(_format, _args...)
13574 +#endif
13575 +#define dwc_debugc DWC_DEBUGC
13576 +/**
13577 + * Prints out an informative message.
13578 + */
13579 +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
13580 + dwc_irq(), ## _args)
13581 +#define dwc_info DWC_INFO
13582 +/**
13583 + * Prints out an informative message if enabled at compile time.
13584 + */
13585 +#if DWC_OTG_DEBUG_LEV > 1
13586 +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
13587 +#else
13588 +#define DWC_INFOC(_format, _args...)
13589 +#endif
13590 +#define dwc_infoc DWC_INFOC
13591 +/**
13592 + * Prints out a warning message.
13593 + */
13594 +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
13595 + dwc_irq(), __func__, __LINE__, ## _args)
13596 +#define dwc_warn DWC_WARN
13597 +/**
13598 + * Prints out an error message.
13599 + */
13600 +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
13601 + dwc_irq(), __func__, __LINE__, ## _args)
13602 +#define dwc_error DWC_ERROR
13603 +
13604 +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
13605 + dwc_irq(), __func__, __LINE__, ## _args)
13606 +#define dwc_proto_error DWC_PROTO_ERROR
13607 +
13608 +#ifdef DEBUG
13609 +/** Prints out a exception error message if the _expr expression fails. Disabled
13610 + * if DEBUG is not enabled. */
13611 +#define DWC_ASSERT(_expr, _format, _args...) do { \
13612 + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
13613 + __FILE__, __LINE__, ## _args); } \
13614 + } while (0)
13615 +#else
13616 +#define DWC_ASSERT(_x...)
13617 +#endif
13618 +#define dwc_assert DWC_ASSERT
13619 +
13620 +
13621 +/** @name Byte Ordering
13622 + * The following functions are for conversions between processor's byte ordering
13623 + * and specific ordering you want.
13624 + */
13625 +
13626 +/** Converts 32 bit data in CPU byte ordering to little endian. */
13627 +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
13628 +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
13629 +
13630 +/** Converts 32 bit data in CPU byte orderint to big endian. */
13631 +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
13632 +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
13633 +
13634 +/** Converts 32 bit little endian data to CPU byte ordering. */
13635 +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
13636 +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
13637 +
13638 +/** Converts 32 bit big endian data to CPU byte ordering. */
13639 +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
13640 +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
13641 +
13642 +/** Converts 16 bit data in CPU byte ordering to little endian. */
13643 +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
13644 +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
13645 +
13646 +/** Converts 16 bit data in CPU byte orderint to big endian. */
13647 +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
13648 +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
13649 +
13650 +/** Converts 16 bit little endian data to CPU byte ordering. */
13651 +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
13652 +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
13653 +
13654 +/** Converts 16 bit bi endian data to CPU byte ordering. */
13655 +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
13656 +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
13657 +
13658 +
13659 +/** @name Register Read/Write
13660 + *
13661 + * The following six functions should be implemented to read/write registers of
13662 + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
13663 + * The reg value is a pointer to the register calculated from the void *base
13664 + * variable passed into the driver when it is started. */
13665 +
13666 +#ifdef DWC_LINUX
13667 +/* Linux doesn't need any extra parameters for register read/write, so we
13668 + * just throw away the IO context parameter.
13669 + */
13670 +/** Reads the content of a 32-bit register. */
13671 +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
13672 +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
13673 +
13674 +/** Reads the content of a 64-bit register. */
13675 +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
13676 +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
13677 +
13678 +/** Writes to a 32-bit register. */
13679 +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
13680 +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
13681 +
13682 +/** Writes to a 64-bit register. */
13683 +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
13684 +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
13685 +
13686 +/**
13687 + * Modify bit values in a register. Using the
13688 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13689 + */
13690 +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
13691 +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
13692 +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
13693 +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
13694 +
13695 +#endif /* DWC_LINUX */
13696 +
13697 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13698 +typedef struct dwc_ioctx {
13699 + struct device *dev;
13700 + bus_space_tag_t iot;
13701 + bus_space_handle_t ioh;
13702 +} dwc_ioctx_t;
13703 +
13704 +/** BSD needs two extra parameters for register read/write, so we pass
13705 + * them in using the IO context parameter.
13706 + */
13707 +/** Reads the content of a 32-bit register. */
13708 +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
13709 +#define dwc_read_reg32 DWC_READ_REG32
13710 +
13711 +/** Reads the content of a 64-bit register. */
13712 +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
13713 +#define dwc_read_reg64 DWC_READ_REG64
13714 +
13715 +/** Writes to a 32-bit register. */
13716 +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
13717 +#define dwc_write_reg32 DWC_WRITE_REG32
13718 +
13719 +/** Writes to a 64-bit register. */
13720 +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
13721 +#define dwc_write_reg64 DWC_WRITE_REG64
13722 +
13723 +/**
13724 + * Modify bit values in a register. Using the
13725 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13726 + */
13727 +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
13728 +#define dwc_modify_reg32 DWC_MODIFY_REG32
13729 +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
13730 +#define dwc_modify_reg64 DWC_MODIFY_REG64
13731 +
13732 +#endif /* DWC_FREEBSD || DWC_NETBSD */
13733 +
13734 +/** @cond */
13735 +
13736 +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
13737 + * register writes. */
13738 +
13739 +#ifdef DWC_LINUX
13740 +
13741 +# ifdef DWC_DEBUG_REGS
13742 +
13743 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13744 +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
13745 + return DWC_READ_REG32(&container->regs->_reg[num]); \
13746 +} \
13747 +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
13748 + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
13749 + &(((uint32_t*)container->regs->_reg)[num]), data); \
13750 + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
13751 +}
13752 +
13753 +#define dwc_define_read_write_reg(_reg,_container_type) \
13754 +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
13755 + return DWC_READ_REG32(&container->regs->_reg); \
13756 +} \
13757 +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
13758 + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
13759 + DWC_WRITE_REG32(&container->regs->_reg, data); \
13760 +}
13761 +
13762 +# else /* DWC_DEBUG_REGS */
13763 +
13764 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13765 +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
13766 + return DWC_READ_REG32(&container->regs->_reg[num]); \
13767 +} \
13768 +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
13769 + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
13770 +}
13771 +
13772 +#define dwc_define_read_write_reg(_reg,_container_type) \
13773 +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
13774 + return DWC_READ_REG32(&container->regs->_reg); \
13775 +} \
13776 +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
13777 + DWC_WRITE_REG32(&container->regs->_reg, data); \
13778 +}
13779 +
13780 +# endif /* DWC_DEBUG_REGS */
13781 +
13782 +#endif /* DWC_LINUX */
13783 +
13784 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13785 +
13786 +# ifdef DWC_DEBUG_REGS
13787 +
13788 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13789 +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
13790 + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
13791 +} \
13792 +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
13793 + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
13794 + &(((uint32_t*)container->regs->_reg)[num]), data); \
13795 + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
13796 +}
13797 +
13798 +#define dwc_define_read_write_reg(_reg,_container_type) \
13799 +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
13800 + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
13801 +} \
13802 +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
13803 + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
13804 + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
13805 +}
13806 +
13807 +# else /* DWC_DEBUG_REGS */
13808 +
13809 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13810 +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
13811 + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
13812 +} \
13813 +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
13814 + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
13815 +}
13816 +
13817 +#define dwc_define_read_write_reg(_reg,_container_type) \
13818 +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
13819 + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
13820 +} \
13821 +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
13822 + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
13823 +}
13824 +
13825 +# endif /* DWC_DEBUG_REGS */
13826 +
13827 +#endif /* DWC_FREEBSD || DWC_NETBSD */
13828 +
13829 +/** @endcond */
13830 +
13831 +
13832 +#ifdef DWC_CRYPTOLIB
13833 +/** @name Crypto Functions
13834 + *
13835 + * These are the low-level cryptographic functions used by the driver. */
13836 +
13837 +/** Perform AES CBC */
13838 +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
13839 +#define dwc_aes_cbc DWC_AES_CBC
13840 +
13841 +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
13842 +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
13843 +#define dwc_random_bytes DWC_RANDOM_BYTES
13844 +
13845 +/** Perform the SHA-256 hash function */
13846 +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
13847 +#define dwc_sha256 DWC_SHA256
13848 +
13849 +/** Calculated the HMAC-SHA256 */
13850 +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
13851 +#define dwc_hmac_sha256 DWC_HMAC_SHA256
13852 +
13853 +#endif /* DWC_CRYPTOLIB */
13854 +
13855 +
13856 +/** @name Memory Allocation
13857 + *
13858 + * These function provide access to memory allocation. There are only 2 DMA
13859 + * functions and 3 Regular memory functions that need to be implemented. None
13860 + * of the memory debugging routines need to be implemented. The allocation
13861 + * routines all ZERO the contents of the memory.
13862 + *
13863 + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
13864 + * This checks for memory leaks, keeping track of alloc/free pairs. It also
13865 + * keeps track of how much memory the driver is using at any given time. */
13866 +
13867 +#define DWC_PAGE_SIZE 4096
13868 +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
13869 +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
13870 +
13871 +#define DWC_INVALID_DMA_ADDR 0x0
13872 +
13873 +#ifdef DWC_LINUX
13874 +/** Type for a DMA address */
13875 +typedef dma_addr_t dwc_dma_t;
13876 +#endif
13877 +
13878 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13879 +typedef bus_addr_t dwc_dma_t;
13880 +#endif
13881 +
13882 +#ifdef DWC_FREEBSD
13883 +typedef struct dwc_dmactx {
13884 + struct device *dev;
13885 + bus_dma_tag_t dma_tag;
13886 + bus_dmamap_t dma_map;
13887 + bus_addr_t dma_paddr;
13888 + void *dma_vaddr;
13889 +} dwc_dmactx_t;
13890 +#endif
13891 +
13892 +#ifdef DWC_NETBSD
13893 +typedef struct dwc_dmactx {
13894 + struct device *dev;
13895 + bus_dma_tag_t dma_tag;
13896 + bus_dmamap_t dma_map;
13897 + bus_dma_segment_t segs[1];
13898 + int nsegs;
13899 + bus_addr_t dma_paddr;
13900 + void *dma_vaddr;
13901 +} dwc_dmactx_t;
13902 +#endif
13903 +
13904 +/* @todo these functions will be added in the future */
13905 +#if 0
13906 +/**
13907 + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
13908 + * allocated from this pool will be guaranteed to meet the size, alignment, and
13909 + * boundary requirements specified.
13910 + *
13911 + * @param[in] size Specifies the size of the buffers that will be allocated from
13912 + * this pool.
13913 + * @param[in] align Specifies the byte alignment requirements of the buffers
13914 + * allocated from this pool. Must be a power of 2.
13915 + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
13916 + * this pool must not cross.
13917 + *
13918 + * @returns A pointer to an internal opaque structure which is not to be
13919 + * accessed outside of these library functions. Use this handle to specify
13920 + * which pools to allocate/free DMA buffers from and also to destroy the pool,
13921 + * when you are done with it.
13922 + */
13923 +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
13924 +
13925 +/**
13926 + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
13927 + */
13928 +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
13929 +
13930 +/**
13931 + * Allocate a buffer from the specified DMA pool and zeros its contents.
13932 + */
13933 +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
13934 +
13935 +/**
13936 + * Free a previously allocated buffer from the DMA pool.
13937 + */
13938 +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
13939 +#endif
13940 +
13941 +/** Allocates a DMA capable buffer and zeroes its contents. */
13942 +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
13943 +
13944 +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
13945 +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
13946 +
13947 +/** Frees a previously allocated buffer. */
13948 +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
13949 +
13950 +/** Allocates a block of memory and zeroes its contents. */
13951 +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
13952 +
13953 +/** Allocates a block of memory and zeroes its contents, in an atomic manner
13954 + * which can be used inside interrupt context. The size should be sufficiently
13955 + * small, a few KB at most, such that failures are not likely to occur. Can just call
13956 + * __DWC_ALLOC if it is atomic. */
13957 +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
13958 +
13959 +/** Frees a previously allocated buffer. */
13960 +extern void __DWC_FREE(void *mem_ctx, void *addr);
13961 +
13962 +#ifndef DWC_DEBUG_MEMORY
13963 +
13964 +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
13965 +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
13966 +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
13967 +
13968 +# ifdef DWC_LINUX
13969 +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) __DWC_DMA_ALLOC(_dev, _size_, _dma_)
13970 +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) __DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_)
13971 +#define DWC_DMA_FREE(_dev, _size_,_virt_, _dma_) __DWC_DMA_FREE(_dev, _size_, _virt_, _dma_)
13972 +# endif
13973 +
13974 +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13975 +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
13976 +#define DWC_DMA_FREE __DWC_DMA_FREE
13977 +# endif
13978 +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
13979 +
13980 +#else /* DWC_DEBUG_MEMORY */
13981 +
13982 +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
13983 +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
13984 +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
13985 +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
13986 + char const *func, int line);
13987 +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
13988 + char const *func, int line);
13989 +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
13990 + dwc_dma_t dma_addr, char const *func, int line);
13991 +
13992 +extern int dwc_memory_debug_start(void *mem_ctx);
13993 +extern void dwc_memory_debug_stop(void);
13994 +extern void dwc_memory_debug_report(void);
13995 +
13996 +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
13997 +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
13998 + __func__, __LINE__)
13999 +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
14000 +
14001 +# ifdef DWC_LINUX
14002 +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) \
14003 + dwc_dma_alloc_debug(_dev, _size_, _dma_, __func__, __LINE__)
14004 +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) \
14005 + dwc_dma_alloc_atomic_debug(_dev, _size_, _dma_, __func__, __LINE__)
14006 +#define DWC_DMA_FREE(_dev, _size_, _virt_, _dma_) \
14007 + dwc_dma_free_debug(_dev, _size_, _virt_, _dma_, __func__, __LINE__)
14008 +# endif
14009 +
14010 +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
14011 +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
14012 + _dma_, __func__, __LINE__)
14013 +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
14014 + _virt_, _dma_, __func__, __LINE__)
14015 +# endif
14016 +
14017 +#endif /* DWC_DEBUG_MEMORY */
14018 +
14019 +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
14020 +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
14021 +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
14022 +
14023 +#ifdef DWC_LINUX
14024 +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
14025 + * just throw away the DMA context parameter.
14026 + */
14027 +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
14028 +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
14029 +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
14030 +#endif
14031 +
14032 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
14033 +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
14034 + * them in using the DMA context parameter.
14035 + */
14036 +#define dwc_dma_alloc DWC_DMA_ALLOC
14037 +#define dwc_dma_free DWC_DMA_FREE
14038 +#endif
14039 +
14040 +
14041 +/** @name Memory and String Processing */
14042 +
14043 +/** memset() clone */
14044 +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
14045 +#define dwc_memset DWC_MEMSET
14046 +
14047 +/** memcpy() clone */
14048 +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
14049 +#define dwc_memcpy DWC_MEMCPY
14050 +
14051 +/** memmove() clone */
14052 +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
14053 +#define dwc_memmove DWC_MEMMOVE
14054 +
14055 +/** memcmp() clone */
14056 +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
14057 +#define dwc_memcmp DWC_MEMCMP
14058 +
14059 +/** strcmp() clone */
14060 +extern int DWC_STRCMP(void *s1, void *s2);
14061 +#define dwc_strcmp DWC_STRCMP
14062 +
14063 +/** strncmp() clone */
14064 +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
14065 +#define dwc_strncmp DWC_STRNCMP
14066 +
14067 +/** strlen() clone, for NULL terminated ASCII strings */
14068 +extern int DWC_STRLEN(char const *str);
14069 +#define dwc_strlen DWC_STRLEN
14070 +
14071 +/** strcpy() clone, for NULL terminated ASCII strings */
14072 +extern char *DWC_STRCPY(char *to, const char *from);
14073 +#define dwc_strcpy DWC_STRCPY
14074 +
14075 +/** strdup() clone. If you wish to use memory allocation debugging, this
14076 + * implementation of strdup should use the DWC_* memory routines instead of
14077 + * calling a predefined strdup. Otherwise the memory allocated by this routine
14078 + * will not be seen by the debugging routines. */
14079 +extern char *DWC_STRDUP(char const *str);
14080 +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
14081 +
14082 +/** NOT an atoi() clone. Read the description carefully. Returns an integer
14083 + * converted from the string str in base 10 unless the string begins with a "0x"
14084 + * in which case it is base 16. String must be a NULL terminated sequence of
14085 + * ASCII characters and may optionally begin with whitespace, a + or -, and a
14086 + * "0x" prefix if base 16. The remaining characters must be valid digits for
14087 + * the number and end with a NULL character. If any invalid characters are
14088 + * encountered or it returns with a negative error code and the results of the
14089 + * conversion are undefined. On sucess it returns 0. Overflow conditions are
14090 + * undefined. An example implementation using atoi() can be referenced from the
14091 + * Linux implementation. */
14092 +extern int DWC_ATOI(const char *str, int32_t *value);
14093 +#define dwc_atoi DWC_ATOI
14094 +
14095 +/** Same as above but for unsigned. */
14096 +extern int DWC_ATOUI(const char *str, uint32_t *value);
14097 +#define dwc_atoui DWC_ATOUI
14098 +
14099 +#ifdef DWC_UTFLIB
14100 +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
14101 +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
14102 +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
14103 +#endif
14104 +
14105 +
14106 +/** @name Wait queues
14107 + *
14108 + * Wait queues provide a means of synchronizing between threads or processes. A
14109 + * process can block on a waitq if some condition is not true, waiting for it to
14110 + * become true. When the waitq is triggered all waiting process will get
14111 + * unblocked and the condition will be check again. Waitqs should be triggered
14112 + * every time a condition can potentially change.*/
14113 +struct dwc_waitq;
14114 +
14115 +/** Type for a waitq */
14116 +typedef struct dwc_waitq dwc_waitq_t;
14117 +
14118 +/** The type of waitq condition callback function. This is called every time
14119 + * condition is evaluated. */
14120 +typedef int (*dwc_waitq_condition_t)(void *data);
14121 +
14122 +/** Allocate a waitq */
14123 +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
14124 +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
14125 +
14126 +/** Free a waitq */
14127 +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
14128 +#define dwc_waitq_free DWC_WAITQ_FREE
14129 +
14130 +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
14131 + * condition again. The function returns when the condition becomes true. The return value
14132 + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
14133 +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
14134 +#define dwc_waitq_wait DWC_WAITQ_WAIT
14135 +
14136 +/** Check the condition and if it is false, block on the waitq. When unblocked,
14137 + * check the condition again. The function returns when the condition become
14138 + * true or the timeout has passed. The return value is 0 on condition true or
14139 + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
14140 + * error. */
14141 +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
14142 + void *data, int32_t msecs);
14143 +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
14144 +
14145 +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
14146 + * has potentially changed. */
14147 +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
14148 +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
14149 +
14150 +/** Unblock all processes waiting on the waitq with an ABORTED result. */
14151 +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
14152 +#define dwc_waitq_abort DWC_WAITQ_ABORT
14153 +
14154 +
14155 +/** @name Threads
14156 + *
14157 + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
14158 + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
14159 + * returns the value from the thread.
14160 + */
14161 +
14162 +struct dwc_thread;
14163 +
14164 +/** Type for a thread */
14165 +typedef struct dwc_thread dwc_thread_t;
14166 +
14167 +/** The thread function */
14168 +typedef int (*dwc_thread_function_t)(void *data);
14169 +
14170 +/** Create a thread and start it running the thread_function. Returns a handle
14171 + * to the thread */
14172 +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
14173 +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
14174 +
14175 +/** Stops a thread. Return the value returned by the thread. Or will return
14176 + * DWC_ABORT if the thread never started. */
14177 +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
14178 +#define dwc_thread_stop DWC_THREAD_STOP
14179 +
14180 +/** Signifies to the thread that it must stop. */
14181 +#ifdef DWC_LINUX
14182 +/* Linux doesn't need any parameters for kthread_should_stop() */
14183 +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
14184 +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
14185 +
14186 +/* No thread_exit function in Linux */
14187 +#define dwc_thread_exit(_thrd_)
14188 +#endif
14189 +
14190 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
14191 +/** BSD needs the thread pointer for kthread_suspend_check() */
14192 +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
14193 +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
14194 +
14195 +/** The thread must call this to exit. */
14196 +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
14197 +#define dwc_thread_exit DWC_THREAD_EXIT
14198 +#endif
14199 +
14200 +
14201 +/** @name Work queues
14202 + *
14203 + * Workqs are used to queue a callback function to be called at some later time,
14204 + * in another thread. */
14205 +struct dwc_workq;
14206 +
14207 +/** Type for a workq */
14208 +typedef struct dwc_workq dwc_workq_t;
14209 +
14210 +/** The type of the callback function to be called. */
14211 +typedef void (*dwc_work_callback_t)(void *data);
14212 +
14213 +/** Allocate a workq */
14214 +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
14215 +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
14216 +
14217 +/** Free a workq. All work must be completed before being freed. */
14218 +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
14219 +#define dwc_workq_free DWC_WORKQ_FREE
14220 +
14221 +/** Schedule a callback on the workq, passing in data. The function will be
14222 + * scheduled at some later time. */
14223 +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
14224 + void *data, char *format, ...)
14225 +#ifdef __GNUC__
14226 + __attribute__ ((format(printf, 4, 5)));
14227 +#else
14228 + ;
14229 +#endif
14230 +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
14231 +
14232 +/** Schedule a callback on the workq, that will be called until at least
14233 + * given number miliseconds have passed. */
14234 +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
14235 + void *data, uint32_t time, char *format, ...)
14236 +#ifdef __GNUC__
14237 + __attribute__ ((format(printf, 5, 6)));
14238 +#else
14239 + ;
14240 +#endif
14241 +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
14242 +
14243 +/** The number of processes in the workq */
14244 +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
14245 +#define dwc_workq_pending DWC_WORKQ_PENDING
14246 +
14247 +/** Blocks until all the work in the workq is complete or timed out. Returns <
14248 + * 0 on timeout. */
14249 +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
14250 +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
14251 +
14252 +
14253 +/** @name Tasklets
14254 + *
14255 + */
14256 +struct dwc_tasklet;
14257 +
14258 +/** Type for a tasklet */
14259 +typedef struct dwc_tasklet dwc_tasklet_t;
14260 +
14261 +/** The type of the callback function to be called */
14262 +typedef void (*dwc_tasklet_callback_t)(void *data);
14263 +
14264 +/** Allocates a tasklet */
14265 +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
14266 +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
14267 +
14268 +/** Frees a tasklet */
14269 +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
14270 +#define dwc_task_free DWC_TASK_FREE
14271 +
14272 +/** Schedules a tasklet to run */
14273 +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
14274 +#define dwc_task_schedule DWC_TASK_SCHEDULE
14275 +
14276 +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
14277 +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
14278 +
14279 +/** @name Timer
14280 + *
14281 + * Callbacks must be small and atomic.
14282 + */
14283 +struct dwc_timer;
14284 +
14285 +/** Type for a timer */
14286 +typedef struct dwc_timer dwc_timer_t;
14287 +
14288 +/** The type of the callback function to be called */
14289 +typedef void (*dwc_timer_callback_t)(void *data);
14290 +
14291 +/** Allocates a timer */
14292 +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
14293 +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
14294 +
14295 +/** Frees a timer */
14296 +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
14297 +#define dwc_timer_free DWC_TIMER_FREE
14298 +
14299 +/** Schedules the timer to run at time ms from now. And will repeat at every
14300 + * repeat_interval msec therafter
14301 + *
14302 + * Modifies a timer that is still awaiting execution to a new expiration time.
14303 + * The mod_time is added to the old time. */
14304 +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
14305 +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
14306 +
14307 +/** Disables the timer from execution. */
14308 +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
14309 +#define dwc_timer_cancel DWC_TIMER_CANCEL
14310 +
14311 +
14312 +/** @name Spinlocks
14313 + *
14314 + * These locks are used when the work between the lock/unlock is atomic and
14315 + * short. Interrupts are also disabled during the lock/unlock and thus they are
14316 + * suitable to lock between interrupt/non-interrupt context. They also lock
14317 + * between processes if you have multiple CPUs or Preemption. If you don't have
14318 + * multiple CPUS or Preemption, then the you can simply implement the
14319 + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
14320 + * the work between the lock/unlock is atomic, the process context will never
14321 + * change, and so you never have to lock between processes. */
14322 +
14323 +struct dwc_spinlock;
14324 +
14325 +/** Type for a spinlock */
14326 +typedef struct dwc_spinlock dwc_spinlock_t;
14327 +
14328 +/** Type for the 'flags' argument to spinlock funtions */
14329 +typedef unsigned long dwc_irqflags_t;
14330 +
14331 +/** Returns an initialized lock variable. This function should allocate and
14332 + * initialize the OS-specific data structure used for locking. This data
14333 + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
14334 + * be freed by the DWC_FREE_LOCK when it is no longer used.
14335 + *
14336 + * For Linux Spinlock Debugging make it macro because the debugging routines use
14337 + * the symbol name to determine recursive locking. Using a wrapper function
14338 + * makes it falsely think recursive locking occurs. */
14339 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
14340 +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
14341 + lock = DWC_ALLOC(sizeof(spinlock_t)); \
14342 + if (lock) { \
14343 + spin_lock_init((spinlock_t *)lock); \
14344 + } \
14345 +})
14346 +#else
14347 +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
14348 +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
14349 +#endif
14350 +
14351 +/** Frees an initialized lock variable. */
14352 +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
14353 +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
14354 +
14355 +/** Disables interrupts and blocks until it acquires the lock.
14356 + *
14357 + * @param lock Pointer to the spinlock.
14358 + * @param flags Unsigned long for irq flags storage.
14359 + */
14360 +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
14361 +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
14362 +
14363 +/** Re-enables the interrupt and releases the lock.
14364 + *
14365 + * @param lock Pointer to the spinlock.
14366 + * @param flags Unsigned long for irq flags storage. Must be the same as was
14367 + * passed into DWC_LOCK.
14368 + */
14369 +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
14370 +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
14371 +
14372 +/** Blocks until it acquires the lock.
14373 + *
14374 + * @param lock Pointer to the spinlock.
14375 + */
14376 +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
14377 +#define dwc_spinlock DWC_SPINLOCK
14378 +
14379 +/** Releases the lock.
14380 + *
14381 + * @param lock Pointer to the spinlock.
14382 + */
14383 +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
14384 +#define dwc_spinunlock DWC_SPINUNLOCK
14385 +
14386 +
14387 +/** @name Mutexes
14388 + *
14389 + * Unlike spinlocks Mutexes lock only between processes and the work between the
14390 + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
14391 + */
14392 +
14393 +struct dwc_mutex;
14394 +
14395 +/** Type for a mutex */
14396 +typedef struct dwc_mutex dwc_mutex_t;
14397 +
14398 +/* For Linux Mutex Debugging make it inline because the debugging routines use
14399 + * the symbol to determine recursive locking. This makes it falsely think
14400 + * recursive locking occurs. */
14401 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
14402 +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
14403 + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
14404 + mutex_init((struct mutex *)__mutexp); \
14405 +})
14406 +#endif
14407 +
14408 +/** Allocate a mutex */
14409 +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
14410 +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
14411 +
14412 +/* For memory leak debugging when using Linux Mutex Debugging */
14413 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
14414 +#define DWC_MUTEX_FREE(__mutexp) do { \
14415 + mutex_destroy((struct mutex *)__mutexp); \
14416 + DWC_FREE(__mutexp); \
14417 +} while(0)
14418 +#else
14419 +/** Free a mutex */
14420 +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
14421 +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
14422 +#endif
14423 +
14424 +/** Lock a mutex */
14425 +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
14426 +#define dwc_mutex_lock DWC_MUTEX_LOCK
14427 +
14428 +/** Non-blocking lock returns 1 on successful lock. */
14429 +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
14430 +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
14431 +
14432 +/** Unlock a mutex */
14433 +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
14434 +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
14435 +
14436 +
14437 +/** @name Time */
14438 +
14439 +/** Microsecond delay.
14440 + *
14441 + * @param usecs Microseconds to delay.
14442 + */
14443 +extern void DWC_UDELAY(uint32_t usecs);
14444 +#define dwc_udelay DWC_UDELAY
14445 +
14446 +/** Millisecond delay.
14447 + *
14448 + * @param msecs Milliseconds to delay.
14449 + */
14450 +extern void DWC_MDELAY(uint32_t msecs);
14451 +#define dwc_mdelay DWC_MDELAY
14452 +
14453 +/** Non-busy waiting.
14454 + * Sleeps for specified number of milliseconds.
14455 + *
14456 + * @param msecs Milliseconds to sleep.
14457 + */
14458 +extern void DWC_MSLEEP(uint32_t msecs);
14459 +#define dwc_msleep DWC_MSLEEP
14460 +
14461 +/**
14462 + * Returns number of milliseconds since boot.
14463 + */
14464 +extern uint32_t DWC_TIME(void);
14465 +#define dwc_time DWC_TIME
14466 +
14467 +
14468 +
14469 +
14470 +/* @mainpage DWC Portability and Common Library
14471 + *
14472 + * This is the documentation for the DWC Portability and Common Library.
14473 + *
14474 + * @section intro Introduction
14475 + *
14476 + * The DWC Portability library consists of wrapper calls and data structures to
14477 + * all low-level functions which are typically provided by the OS. The WUDEV
14478 + * driver uses only these functions. In order to port the WUDEV driver, only
14479 + * the functions in this library need to be re-implemented, with the same
14480 + * behavior as documented here.
14481 + *
14482 + * The Common library consists of higher level functions, which rely only on
14483 + * calling the functions from the DWC Portability library. These common
14484 + * routines are shared across modules. Some of the common libraries need to be
14485 + * used directly by the driver programmer when porting WUDEV. Such as the
14486 + * parameter and notification libraries.
14487 + *
14488 + * @section low Portability Library OS Wrapper Functions
14489 + *
14490 + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
14491 + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
14492 + * these functions are included in the dwc_os.h file.
14493 + *
14494 + * There are many functions here covering a wide array of OS services. Please
14495 + * see dwc_os.h for details, and implementation notes for each function.
14496 + *
14497 + * @section common Common Library Functions
14498 + *
14499 + * Any function starting with dwc and in all lowercase is a common library
14500 + * routine. These functions have a portable implementation and do not need to
14501 + * be reimplemented when porting. The common routines can be used by any
14502 + * driver, and some must be used by the end user to control the drivers. For
14503 + * example, you must use the Parameter common library in order to set the
14504 + * parameters in the WUDEV module.
14505 + *
14506 + * The common libraries consist of the following:
14507 + *
14508 + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
14509 + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
14510 + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
14511 + * - Lists - Used internally and can be used by end-user. See dwc_list.h
14512 + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
14513 + * - Modpow - Used internally only. See dwc_modpow.h
14514 + * - DH - Used internally only. See dwc_dh.h
14515 + * - Crypto - Used internally only. See dwc_crypto.h
14516 + *
14517 + *
14518 + * @section prereq Prerequistes For dwc_os.h
14519 + * @subsection types Data Types
14520 + *
14521 + * The dwc_os.h file assumes that several low-level data types are pre defined for the
14522 + * compilation environment. These data types are:
14523 + *
14524 + * - uint8_t - unsigned 8-bit data type
14525 + * - int8_t - signed 8-bit data type
14526 + * - uint16_t - unsigned 16-bit data type
14527 + * - int16_t - signed 16-bit data type
14528 + * - uint32_t - unsigned 32-bit data type
14529 + * - int32_t - signed 32-bit data type
14530 + * - uint64_t - unsigned 64-bit data type
14531 + * - int64_t - signed 64-bit data type
14532 + *
14533 + * Ensure that these are defined before using dwc_os.h. The easiest way to do
14534 + * that is to modify the top of the file to include the appropriate header.
14535 + * This is already done for the Linux environment. If the DWC_LINUX macro is
14536 + * defined, the correct header will be added. A standard header <stdint.h> is
14537 + * also used for environments where standard C headers are available.
14538 + *
14539 + * @subsection stdarg Variable Arguments
14540 + *
14541 + * Variable arguments are provided by a standard C header <stdarg.h>. it is
14542 + * available in Both the Linux and ANSI C enviornment. An equivalent must be
14543 + * provided in your enviornment in order to use dwc_os.h with the debug and
14544 + * tracing message functionality.
14545 + *
14546 + * @subsection thread Threading
14547 + *
14548 + * WUDEV Core must be run on an operating system that provides for multiple
14549 + * threads/processes. Threading can be implemented in many ways, even in
14550 + * embedded systems without an operating system. At the bare minimum, the
14551 + * system should be able to start any number of processes at any time to handle
14552 + * special work. It need not be a pre-emptive system. Process context can
14553 + * change upon a call to a blocking function. The hardware interrupt context
14554 + * that calls the module's ISR() function must be differentiable from process
14555 + * context, even if your processes are impemented via a hardware interrupt.
14556 + * Further locking mechanism between process must exist (or be implemented), and
14557 + * process context must have a way to disable interrupts for a period of time to
14558 + * lock them out. If all of this exists, the functions in dwc_os.h related to
14559 + * threading should be able to be implemented with the defined behavior.
14560 + *
14561 + */
14562 +
14563 +#ifdef __cplusplus
14564 +}
14565 +#endif
14566 +
14567 +#endif /* _DWC_OS_H_ */
14568 --- /dev/null
14569 +++ b/drivers/usb/host/dwc_common_port/usb.h
14570 @@ -0,0 +1,275 @@
14571 +/*
14572 + * Copyright (c) 1998 The NetBSD Foundation, Inc.
14573 + * All rights reserved.
14574 + *
14575 + * This code is derived from software contributed to The NetBSD Foundation
14576 + * by Lennart Augustsson (lennart@augustsson.net) at
14577 + * Carlstedt Research & Technology.
14578 + *
14579 + * Redistribution and use in source and binary forms, with or without
14580 + * modification, are permitted provided that the following conditions
14581 + * are met:
14582 + * 1. Redistributions of source code must retain the above copyright
14583 + * notice, this list of conditions and the following disclaimer.
14584 + * 2. Redistributions in binary form must reproduce the above copyright
14585 + * notice, this list of conditions and the following disclaimer in the
14586 + * documentation and/or other materials provided with the distribution.
14587 + *
14588 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14589 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
14590 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14591 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
14592 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
14593 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
14594 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
14595 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
14596 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
14597 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
14598 + * POSSIBILITY OF SUCH DAMAGE.
14599 + */
14600 +
14601 +/* Modified by Synopsys, Inc, 12/12/2007 */
14602 +
14603 +
14604 +#ifndef _USB_H_
14605 +#define _USB_H_
14606 +
14607 +#ifdef __cplusplus
14608 +extern "C" {
14609 +#endif
14610 +
14611 +/*
14612 + * The USB records contain some unaligned little-endian word
14613 + * components. The U[SG]ETW macros take care of both the alignment
14614 + * and endian problem and should always be used to access non-byte
14615 + * values.
14616 + */
14617 +typedef u_int8_t uByte;
14618 +typedef u_int8_t uWord[2];
14619 +typedef u_int8_t uDWord[4];
14620 +
14621 +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
14622 +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
14623 +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
14624 +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
14625 + (w)[1] = (u_int8_t)((v) >> 8), \
14626 + (w)[2] = (u_int8_t)((v) >> 16), \
14627 + (w)[3] = (u_int8_t)((v) >> 24))
14628 +
14629 +#define UPACKED __attribute__((__packed__))
14630 +
14631 +typedef struct {
14632 + uByte bmRequestType;
14633 + uByte bRequest;
14634 + uWord wValue;
14635 + uWord wIndex;
14636 + uWord wLength;
14637 +} UPACKED usb_device_request_t;
14638 +
14639 +#define UT_GET_DIR(a) ((a) & 0x80)
14640 +#define UT_WRITE 0x00
14641 +#define UT_READ 0x80
14642 +
14643 +#define UT_GET_TYPE(a) ((a) & 0x60)
14644 +#define UT_STANDARD 0x00
14645 +#define UT_CLASS 0x20
14646 +#define UT_VENDOR 0x40
14647 +
14648 +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
14649 +#define UT_DEVICE 0x00
14650 +#define UT_INTERFACE 0x01
14651 +#define UT_ENDPOINT 0x02
14652 +#define UT_OTHER 0x03
14653 +
14654 +/* Requests */
14655 +#define UR_GET_STATUS 0x00
14656 +#define USTAT_STANDARD_STATUS 0x00
14657 +#define WUSTAT_WUSB_FEATURE 0x01
14658 +#define WUSTAT_CHANNEL_INFO 0x02
14659 +#define WUSTAT_RECEIVED_DATA 0x03
14660 +#define WUSTAT_MAS_AVAILABILITY 0x04
14661 +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
14662 +#define UR_CLEAR_FEATURE 0x01
14663 +#define UR_SET_FEATURE 0x03
14664 +#define UR_SET_AND_TEST_FEATURE 0x0c
14665 +#define UR_SET_ADDRESS 0x05
14666 +#define UR_GET_DESCRIPTOR 0x06
14667 +#define UDESC_DEVICE 0x01
14668 +#define UDESC_CONFIG 0x02
14669 +#define UDESC_STRING 0x03
14670 +#define UDESC_INTERFACE 0x04
14671 +#define UDESC_ENDPOINT 0x05
14672 +#define UDESC_SS_USB_COMPANION 0x30
14673 +#define UDESC_DEVICE_QUALIFIER 0x06
14674 +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
14675 +#define UDESC_INTERFACE_POWER 0x08
14676 +#define UDESC_OTG 0x09
14677 +#define WUDESC_SECURITY 0x0c
14678 +#define WUDESC_KEY 0x0d
14679 +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
14680 +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
14681 +#define WUD_KEY_TYPE_ASSOC 0x01
14682 +#define WUD_KEY_TYPE_GTK 0x02
14683 +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
14684 +#define WUD_KEY_ORIGIN_HOST 0x00
14685 +#define WUD_KEY_ORIGIN_DEVICE 0x01
14686 +#define WUDESC_ENCRYPTION_TYPE 0x0e
14687 +#define WUDESC_BOS 0x0f
14688 +#define WUDESC_DEVICE_CAPABILITY 0x10
14689 +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
14690 +#define UDESC_BOS 0x0f
14691 +#define UDESC_DEVICE_CAPABILITY 0x10
14692 +#define UDESC_CS_DEVICE 0x21 /* class specific */
14693 +#define UDESC_CS_CONFIG 0x22
14694 +#define UDESC_CS_STRING 0x23
14695 +#define UDESC_CS_INTERFACE 0x24
14696 +#define UDESC_CS_ENDPOINT 0x25
14697 +#define UDESC_HUB 0x29
14698 +#define UR_SET_DESCRIPTOR 0x07
14699 +#define UR_GET_CONFIG 0x08
14700 +#define UR_SET_CONFIG 0x09
14701 +#define UR_GET_INTERFACE 0x0a
14702 +#define UR_SET_INTERFACE 0x0b
14703 +#define UR_SYNCH_FRAME 0x0c
14704 +#define WUR_SET_ENCRYPTION 0x0d
14705 +#define WUR_GET_ENCRYPTION 0x0e
14706 +#define WUR_SET_HANDSHAKE 0x0f
14707 +#define WUR_GET_HANDSHAKE 0x10
14708 +#define WUR_SET_CONNECTION 0x11
14709 +#define WUR_SET_SECURITY_DATA 0x12
14710 +#define WUR_GET_SECURITY_DATA 0x13
14711 +#define WUR_SET_WUSB_DATA 0x14
14712 +#define WUDATA_DRPIE_INFO 0x01
14713 +#define WUDATA_TRANSMIT_DATA 0x02
14714 +#define WUDATA_TRANSMIT_PARAMS 0x03
14715 +#define WUDATA_RECEIVE_PARAMS 0x04
14716 +#define WUDATA_TRANSMIT_POWER 0x05
14717 +#define WUR_LOOPBACK_DATA_WRITE 0x15
14718 +#define WUR_LOOPBACK_DATA_READ 0x16
14719 +#define WUR_SET_INTERFACE_DS 0x17
14720 +
14721 +/* Feature numbers */
14722 +#define UF_ENDPOINT_HALT 0
14723 +#define UF_DEVICE_REMOTE_WAKEUP 1
14724 +#define UF_TEST_MODE 2
14725 +#define UF_DEVICE_B_HNP_ENABLE 3
14726 +#define UF_DEVICE_A_HNP_SUPPORT 4
14727 +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
14728 +#define WUF_WUSB 3
14729 +#define WUF_TX_DRPIE 0x0
14730 +#define WUF_DEV_XMIT_PACKET 0x1
14731 +#define WUF_COUNT_PACKETS 0x2
14732 +#define WUF_CAPTURE_PACKETS 0x3
14733 +#define UF_FUNCTION_SUSPEND 0
14734 +#define UF_U1_ENABLE 48
14735 +#define UF_U2_ENABLE 49
14736 +#define UF_LTM_ENABLE 50
14737 +
14738 +/* Class requests from the USB 2.0 hub spec, table 11-15 */
14739 +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
14740 +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
14741 +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
14742 +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
14743 +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
14744 +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
14745 +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
14746 +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
14747 +
14748 +#ifdef _MSC_VER
14749 +#include <pshpack1.h>
14750 +#endif
14751 +
14752 +typedef struct {
14753 + uByte bLength;
14754 + uByte bDescriptorType;
14755 + uByte bEndpointAddress;
14756 +#define UE_GET_DIR(a) ((a) & 0x80)
14757 +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
14758 +#define UE_DIR_IN 0x80
14759 +#define UE_DIR_OUT 0x00
14760 +#define UE_ADDR 0x0f
14761 +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
14762 + uByte bmAttributes;
14763 +#define UE_XFERTYPE 0x03
14764 +#define UE_CONTROL 0x00
14765 +#define UE_ISOCHRONOUS 0x01
14766 +#define UE_BULK 0x02
14767 +#define UE_INTERRUPT 0x03
14768 +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
14769 +#define UE_ISO_TYPE 0x0c
14770 +#define UE_ISO_ASYNC 0x04
14771 +#define UE_ISO_ADAPT 0x08
14772 +#define UE_ISO_SYNC 0x0c
14773 +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
14774 + uWord wMaxPacketSize;
14775 + uByte bInterval;
14776 +} UPACKED usb_endpoint_descriptor_t;
14777 +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
14778 +
14779 +/* Hub specific request */
14780 +#define UR_GET_BUS_STATE 0x02
14781 +#define UR_CLEAR_TT_BUFFER 0x08
14782 +#define UR_RESET_TT 0x09
14783 +#define UR_GET_TT_STATE 0x0a
14784 +#define UR_STOP_TT 0x0b
14785 +
14786 +/* Hub features */
14787 +#define UHF_C_HUB_LOCAL_POWER 0
14788 +#define UHF_C_HUB_OVER_CURRENT 1
14789 +#define UHF_PORT_CONNECTION 0
14790 +#define UHF_PORT_ENABLE 1
14791 +#define UHF_PORT_SUSPEND 2
14792 +#define UHF_PORT_OVER_CURRENT 3
14793 +#define UHF_PORT_RESET 4
14794 +#define UHF_PORT_L1 5
14795 +#define UHF_PORT_POWER 8
14796 +#define UHF_PORT_LOW_SPEED 9
14797 +#define UHF_PORT_HIGH_SPEED 10
14798 +#define UHF_C_PORT_CONNECTION 16
14799 +#define UHF_C_PORT_ENABLE 17
14800 +#define UHF_C_PORT_SUSPEND 18
14801 +#define UHF_C_PORT_OVER_CURRENT 19
14802 +#define UHF_C_PORT_RESET 20
14803 +#define UHF_C_PORT_L1 23
14804 +#define UHF_PORT_TEST 21
14805 +#define UHF_PORT_INDICATOR 22
14806 +
14807 +typedef struct {
14808 + uByte bDescLength;
14809 + uByte bDescriptorType;
14810 + uByte bNbrPorts;
14811 + uWord wHubCharacteristics;
14812 +#define UHD_PWR 0x0003
14813 +#define UHD_PWR_GANGED 0x0000
14814 +#define UHD_PWR_INDIVIDUAL 0x0001
14815 +#define UHD_PWR_NO_SWITCH 0x0002
14816 +#define UHD_COMPOUND 0x0004
14817 +#define UHD_OC 0x0018
14818 +#define UHD_OC_GLOBAL 0x0000
14819 +#define UHD_OC_INDIVIDUAL 0x0008
14820 +#define UHD_OC_NONE 0x0010
14821 +#define UHD_TT_THINK 0x0060
14822 +#define UHD_TT_THINK_8 0x0000
14823 +#define UHD_TT_THINK_16 0x0020
14824 +#define UHD_TT_THINK_24 0x0040
14825 +#define UHD_TT_THINK_32 0x0060
14826 +#define UHD_PORT_IND 0x0080
14827 + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
14828 +#define UHD_PWRON_FACTOR 2
14829 + uByte bHubContrCurrent;
14830 + uByte DeviceRemovable[32]; /* max 255 ports */
14831 +#define UHD_NOT_REMOV(desc, i) \
14832 + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
14833 + /* deprecated */ uByte PortPowerCtrlMask[1];
14834 +} UPACKED usb_hub_descriptor_t;
14835 +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
14836 +
14837 +#ifdef _MSC_VER
14838 +#include <poppack.h>
14839 +#endif
14840 +
14841 +#ifdef __cplusplus
14842 +}
14843 +#endif
14844 +
14845 +#endif /* _USB_H_ */
14846 --- /dev/null
14847 +++ b/drivers/usb/host/dwc_otg/Makefile
14848 @@ -0,0 +1,85 @@
14849 +#
14850 +# Makefile for DWC_otg Highspeed USB controller driver
14851 +#
14852 +
14853 +ifneq ($(KERNELRELEASE),)
14854 +
14855 +# Use the BUS_INTERFACE variable to compile the software for either
14856 +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
14857 +ifeq ($(BUS_INTERFACE),)
14858 +# BUS_INTERFACE = -DPCI_INTERFACE
14859 +# BUS_INTERFACE = -DLM_INTERFACE
14860 + BUS_INTERFACE = -DPLATFORM_INTERFACE
14861 +endif
14862 +
14863 +#ccflags-y += -DDEBUG
14864 +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
14865 +
14866 +# Use one of the following flags to compile the software in host-only or
14867 +# device-only mode.
14868 +#ccflags-y += -DDWC_HOST_ONLY
14869 +#ccflags-y += -DDWC_DEVICE_ONLY
14870 +
14871 +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
14872 +#ccflags-y += -DDWC_EN_ISOC
14873 +ccflags-y += -I$(srctree)/drivers/usb/host/dwc_common_port
14874 +#ccflags-y += -I$(PORTLIB)
14875 +ccflags-y += -DDWC_LINUX
14876 +ccflags-y += $(CFI)
14877 +ccflags-y += $(BUS_INTERFACE)
14878 +#ccflags-y += -DDWC_DEV_SRPCAP
14879 +
14880 +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
14881 +
14882 +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
14883 +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
14884 +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
14885 +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
14886 +dwc_otg-objs += dwc_otg_adp.o
14887 +dwc_otg-objs += dwc_otg_fiq_fsm.o
14888 +ifneq ($(CONFIG_ARM64),y)
14889 +dwc_otg-objs += dwc_otg_fiq_stub.o
14890 +endif
14891 +
14892 +ifneq ($(CFI),)
14893 +dwc_otg-objs += dwc_otg_cfi.o
14894 +endif
14895 +
14896 +kernrelwd := $(subst ., ,$(KERNELRELEASE))
14897 +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
14898 +
14899 +ifneq ($(kernrel3),2.6.20)
14900 +ccflags-y += $(CPPFLAGS)
14901 +endif
14902 +
14903 +else
14904 +
14905 +PWD := $(shell pwd)
14906 +PORTLIB := $(PWD)/../dwc_common_port
14907 +
14908 +# Command paths
14909 +CTAGS := $(CTAGS)
14910 +DOXYGEN := $(DOXYGEN)
14911 +
14912 +default: portlib
14913 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
14914 +
14915 +install: default
14916 + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
14917 + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
14918 +
14919 +portlib:
14920 + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
14921 + cp $(PORTLIB)/Module.symvers $(PWD)/
14922 +
14923 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
14924 + $(DOXYGEN) doc/doxygen.cfg
14925 +
14926 +tags: $(wildcard *.[hc])
14927 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
14928 +
14929 +
14930 +clean:
14931 + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
14932 +
14933 +endif
14934 --- /dev/null
14935 +++ b/drivers/usb/host/dwc_otg/doc/doxygen.cfg
14936 @@ -0,0 +1,224 @@
14937 +# Doxyfile 1.3.9.1
14938 +
14939 +#---------------------------------------------------------------------------
14940 +# Project related configuration options
14941 +#---------------------------------------------------------------------------
14942 +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
14943 +PROJECT_NUMBER = v3.00a
14944 +OUTPUT_DIRECTORY = ./doc/
14945 +CREATE_SUBDIRS = NO
14946 +OUTPUT_LANGUAGE = English
14947 +BRIEF_MEMBER_DESC = YES
14948 +REPEAT_BRIEF = YES
14949 +ABBREVIATE_BRIEF = "The $name class" \
14950 + "The $name widget" \
14951 + "The $name file" \
14952 + is \
14953 + provides \
14954 + specifies \
14955 + contains \
14956 + represents \
14957 + a \
14958 + an \
14959 + the
14960 +ALWAYS_DETAILED_SEC = NO
14961 +INLINE_INHERITED_MEMB = NO
14962 +FULL_PATH_NAMES = NO
14963 +STRIP_FROM_PATH =
14964 +STRIP_FROM_INC_PATH =
14965 +SHORT_NAMES = NO
14966 +JAVADOC_AUTOBRIEF = YES
14967 +MULTILINE_CPP_IS_BRIEF = NO
14968 +INHERIT_DOCS = YES
14969 +DISTRIBUTE_GROUP_DOC = NO
14970 +TAB_SIZE = 8
14971 +ALIASES =
14972 +OPTIMIZE_OUTPUT_FOR_C = YES
14973 +OPTIMIZE_OUTPUT_JAVA = NO
14974 +SUBGROUPING = YES
14975 +#---------------------------------------------------------------------------
14976 +# Build related configuration options
14977 +#---------------------------------------------------------------------------
14978 +EXTRACT_ALL = NO
14979 +EXTRACT_PRIVATE = YES
14980 +EXTRACT_STATIC = YES
14981 +EXTRACT_LOCAL_CLASSES = YES
14982 +EXTRACT_LOCAL_METHODS = NO
14983 +HIDE_UNDOC_MEMBERS = NO
14984 +HIDE_UNDOC_CLASSES = NO
14985 +HIDE_FRIEND_COMPOUNDS = NO
14986 +HIDE_IN_BODY_DOCS = NO
14987 +INTERNAL_DOCS = NO
14988 +CASE_SENSE_NAMES = NO
14989 +HIDE_SCOPE_NAMES = NO
14990 +SHOW_INCLUDE_FILES = YES
14991 +INLINE_INFO = YES
14992 +SORT_MEMBER_DOCS = NO
14993 +SORT_BRIEF_DOCS = NO
14994 +SORT_BY_SCOPE_NAME = NO
14995 +GENERATE_TODOLIST = YES
14996 +GENERATE_TESTLIST = YES
14997 +GENERATE_BUGLIST = YES
14998 +GENERATE_DEPRECATEDLIST= YES
14999 +ENABLED_SECTIONS =
15000 +MAX_INITIALIZER_LINES = 30
15001 +SHOW_USED_FILES = YES
15002 +SHOW_DIRECTORIES = YES
15003 +#---------------------------------------------------------------------------
15004 +# configuration options related to warning and progress messages
15005 +#---------------------------------------------------------------------------
15006 +QUIET = YES
15007 +WARNINGS = YES
15008 +WARN_IF_UNDOCUMENTED = NO
15009 +WARN_IF_DOC_ERROR = YES
15010 +WARN_FORMAT = "$file:$line: $text"
15011 +WARN_LOGFILE =
15012 +#---------------------------------------------------------------------------
15013 +# configuration options related to the input files
15014 +#---------------------------------------------------------------------------
15015 +INPUT = .
15016 +FILE_PATTERNS = *.c \
15017 + *.h \
15018 + ./linux/*.c \
15019 + ./linux/*.h
15020 +RECURSIVE = NO
15021 +EXCLUDE = ./test/ \
15022 + ./dwc_otg/.AppleDouble/
15023 +EXCLUDE_SYMLINKS = YES
15024 +EXCLUDE_PATTERNS = *.mod.*
15025 +EXAMPLE_PATH =
15026 +EXAMPLE_PATTERNS = *
15027 +EXAMPLE_RECURSIVE = NO
15028 +IMAGE_PATH =
15029 +INPUT_FILTER =
15030 +FILTER_PATTERNS =
15031 +FILTER_SOURCE_FILES = NO
15032 +#---------------------------------------------------------------------------
15033 +# configuration options related to source browsing
15034 +#---------------------------------------------------------------------------
15035 +SOURCE_BROWSER = YES
15036 +INLINE_SOURCES = NO
15037 +STRIP_CODE_COMMENTS = YES
15038 +REFERENCED_BY_RELATION = NO
15039 +REFERENCES_RELATION = NO
15040 +VERBATIM_HEADERS = NO
15041 +#---------------------------------------------------------------------------
15042 +# configuration options related to the alphabetical class index
15043 +#---------------------------------------------------------------------------
15044 +ALPHABETICAL_INDEX = NO
15045 +COLS_IN_ALPHA_INDEX = 5
15046 +IGNORE_PREFIX =
15047 +#---------------------------------------------------------------------------
15048 +# configuration options related to the HTML output
15049 +#---------------------------------------------------------------------------
15050 +GENERATE_HTML = YES
15051 +HTML_OUTPUT = html
15052 +HTML_FILE_EXTENSION = .html
15053 +HTML_HEADER =
15054 +HTML_FOOTER =
15055 +HTML_STYLESHEET =
15056 +HTML_ALIGN_MEMBERS = YES
15057 +GENERATE_HTMLHELP = NO
15058 +CHM_FILE =
15059 +HHC_LOCATION =
15060 +GENERATE_CHI = NO
15061 +BINARY_TOC = NO
15062 +TOC_EXPAND = NO
15063 +DISABLE_INDEX = NO
15064 +ENUM_VALUES_PER_LINE = 4
15065 +GENERATE_TREEVIEW = YES
15066 +TREEVIEW_WIDTH = 250
15067 +#---------------------------------------------------------------------------
15068 +# configuration options related to the LaTeX output
15069 +#---------------------------------------------------------------------------
15070 +GENERATE_LATEX = NO
15071 +LATEX_OUTPUT = latex
15072 +LATEX_CMD_NAME = latex
15073 +MAKEINDEX_CMD_NAME = makeindex
15074 +COMPACT_LATEX = NO
15075 +PAPER_TYPE = a4wide
15076 +EXTRA_PACKAGES =
15077 +LATEX_HEADER =
15078 +PDF_HYPERLINKS = NO
15079 +USE_PDFLATEX = NO
15080 +LATEX_BATCHMODE = NO
15081 +LATEX_HIDE_INDICES = NO
15082 +#---------------------------------------------------------------------------
15083 +# configuration options related to the RTF output
15084 +#---------------------------------------------------------------------------
15085 +GENERATE_RTF = NO
15086 +RTF_OUTPUT = rtf
15087 +COMPACT_RTF = NO
15088 +RTF_HYPERLINKS = NO
15089 +RTF_STYLESHEET_FILE =
15090 +RTF_EXTENSIONS_FILE =
15091 +#---------------------------------------------------------------------------
15092 +# configuration options related to the man page output
15093 +#---------------------------------------------------------------------------
15094 +GENERATE_MAN = NO
15095 +MAN_OUTPUT = man
15096 +MAN_EXTENSION = .3
15097 +MAN_LINKS = NO
15098 +#---------------------------------------------------------------------------
15099 +# configuration options related to the XML output
15100 +#---------------------------------------------------------------------------
15101 +GENERATE_XML = NO
15102 +XML_OUTPUT = xml
15103 +XML_SCHEMA =
15104 +XML_DTD =
15105 +XML_PROGRAMLISTING = YES
15106 +#---------------------------------------------------------------------------
15107 +# configuration options for the AutoGen Definitions output
15108 +#---------------------------------------------------------------------------
15109 +GENERATE_AUTOGEN_DEF = NO
15110 +#---------------------------------------------------------------------------
15111 +# configuration options related to the Perl module output
15112 +#---------------------------------------------------------------------------
15113 +GENERATE_PERLMOD = NO
15114 +PERLMOD_LATEX = NO
15115 +PERLMOD_PRETTY = YES
15116 +PERLMOD_MAKEVAR_PREFIX =
15117 +#---------------------------------------------------------------------------
15118 +# Configuration options related to the preprocessor
15119 +#---------------------------------------------------------------------------
15120 +ENABLE_PREPROCESSING = YES
15121 +MACRO_EXPANSION = YES
15122 +EXPAND_ONLY_PREDEF = YES
15123 +SEARCH_INCLUDES = YES
15124 +INCLUDE_PATH =
15125 +INCLUDE_FILE_PATTERNS =
15126 +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
15127 +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
15128 +SKIP_FUNCTION_MACROS = NO
15129 +#---------------------------------------------------------------------------
15130 +# Configuration::additions related to external references
15131 +#---------------------------------------------------------------------------
15132 +TAGFILES =
15133 +GENERATE_TAGFILE =
15134 +ALLEXTERNALS = NO
15135 +EXTERNAL_GROUPS = YES
15136 +PERL_PATH = /usr/bin/perl
15137 +#---------------------------------------------------------------------------
15138 +# Configuration options related to the dot tool
15139 +#---------------------------------------------------------------------------
15140 +CLASS_DIAGRAMS = YES
15141 +HIDE_UNDOC_RELATIONS = YES
15142 +HAVE_DOT = NO
15143 +CLASS_GRAPH = YES
15144 +COLLABORATION_GRAPH = YES
15145 +UML_LOOK = NO
15146 +TEMPLATE_RELATIONS = NO
15147 +INCLUDE_GRAPH = YES
15148 +INCLUDED_BY_GRAPH = YES
15149 +CALL_GRAPH = NO
15150 +GRAPHICAL_HIERARCHY = YES
15151 +DOT_IMAGE_FORMAT = png
15152 +DOT_PATH =
15153 +DOTFILE_DIRS =
15154 +MAX_DOT_GRAPH_DEPTH = 1000
15155 +GENERATE_LEGEND = YES
15156 +DOT_CLEANUP = YES
15157 +#---------------------------------------------------------------------------
15158 +# Configuration::additions related to the search engine
15159 +#---------------------------------------------------------------------------
15160 +SEARCHENGINE = NO
15161 --- /dev/null
15162 +++ b/drivers/usb/host/dwc_otg/dummy_audio.c
15163 @@ -0,0 +1,1574 @@
15164 +/*
15165 + * zero.c -- Gadget Zero, for USB development
15166 + *
15167 + * Copyright (C) 2003-2004 David Brownell
15168 + * All rights reserved.
15169 + *
15170 + * Redistribution and use in source and binary forms, with or without
15171 + * modification, are permitted provided that the following conditions
15172 + * are met:
15173 + * 1. Redistributions of source code must retain the above copyright
15174 + * notice, this list of conditions, and the following disclaimer,
15175 + * without modification.
15176 + * 2. Redistributions in binary form must reproduce the above copyright
15177 + * notice, this list of conditions and the following disclaimer in the
15178 + * documentation and/or other materials provided with the distribution.
15179 + * 3. The names of the above-listed copyright holders may not be used
15180 + * to endorse or promote products derived from this software without
15181 + * specific prior written permission.
15182 + *
15183 + * ALTERNATIVELY, this software may be distributed under the terms of the
15184 + * GNU General Public License ("GPL") as published by the Free Software
15185 + * Foundation, either version 2 of that License or (at your option) any
15186 + * later version.
15187 + *
15188 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
15189 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15190 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15191 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
15192 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15193 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
15194 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
15195 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
15196 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
15197 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
15198 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15199 + */
15200 +
15201 +
15202 +/*
15203 + * Gadget Zero only needs two bulk endpoints, and is an example of how you
15204 + * can write a hardware-agnostic gadget driver running inside a USB device.
15205 + *
15206 + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
15207 + * affect most of the driver.
15208 + *
15209 + * Use it with the Linux host/master side "usbtest" driver to get a basic
15210 + * functional test of your device-side usb stack, or with "usb-skeleton".
15211 + *
15212 + * It supports two similar configurations. One sinks whatever the usb host
15213 + * writes, and in return sources zeroes. The other loops whatever the host
15214 + * writes back, so the host can read it. Module options include:
15215 + *
15216 + * buflen=N default N=4096, buffer size used
15217 + * qlen=N default N=32, how many buffers in the loopback queue
15218 + * loopdefault default false, list loopback config first
15219 + *
15220 + * Many drivers will only have one configuration, letting them be much
15221 + * simpler if they also don't support high speed operation (like this
15222 + * driver does).
15223 + */
15224 +
15225 +#include <linux/config.h>
15226 +#include <linux/module.h>
15227 +#include <linux/kernel.h>
15228 +#include <linux/delay.h>
15229 +#include <linux/ioport.h>
15230 +#include <linux/sched.h>
15231 +#include <linux/slab.h>
15232 +#include <linux/smp_lock.h>
15233 +#include <linux/errno.h>
15234 +#include <linux/init.h>
15235 +#include <linux/timer.h>
15236 +#include <linux/list.h>
15237 +#include <linux/interrupt.h>
15238 +#include <linux/uts.h>
15239 +#include <linux/version.h>
15240 +#include <linux/device.h>
15241 +#include <linux/moduleparam.h>
15242 +#include <linux/proc_fs.h>
15243 +
15244 +#include <asm/byteorder.h>
15245 +#include <asm/io.h>
15246 +#include <asm/irq.h>
15247 +#include <asm/system.h>
15248 +#include <asm/unaligned.h>
15249 +
15250 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
15251 +# include <linux/usb/ch9.h>
15252 +#else
15253 +# include <linux/usb_ch9.h>
15254 +#endif
15255 +
15256 +#include <linux/usb_gadget.h>
15257 +
15258 +
15259 +/*-------------------------------------------------------------------------*/
15260 +/*-------------------------------------------------------------------------*/
15261 +
15262 +
15263 +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
15264 +{
15265 + int count = 0;
15266 + u8 c;
15267 + u16 uchar;
15268 +
15269 + /* this insists on correct encodings, though not minimal ones.
15270 + * BUT it currently rejects legit 4-byte UTF-8 code points,
15271 + * which need surrogate pairs. (Unicode 3.1 can use them.)
15272 + */
15273 + while (len != 0 && (c = (u8) *s++) != 0) {
15274 + if (unlikely(c & 0x80)) {
15275 + // 2-byte sequence:
15276 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
15277 + if ((c & 0xe0) == 0xc0) {
15278 + uchar = (c & 0x1f) << 6;
15279 +
15280 + c = (u8) *s++;
15281 + if ((c & 0xc0) != 0xc0)
15282 + goto fail;
15283 + c &= 0x3f;
15284 + uchar |= c;
15285 +
15286 + // 3-byte sequence (most CJKV characters):
15287 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
15288 + } else if ((c & 0xf0) == 0xe0) {
15289 + uchar = (c & 0x0f) << 12;
15290 +
15291 + c = (u8) *s++;
15292 + if ((c & 0xc0) != 0xc0)
15293 + goto fail;
15294 + c &= 0x3f;
15295 + uchar |= c << 6;
15296 +
15297 + c = (u8) *s++;
15298 + if ((c & 0xc0) != 0xc0)
15299 + goto fail;
15300 + c &= 0x3f;
15301 + uchar |= c;
15302 +
15303 + /* no bogus surrogates */
15304 + if (0xd800 <= uchar && uchar <= 0xdfff)
15305 + goto fail;
15306 +
15307 + // 4-byte sequence (surrogate pairs, currently rare):
15308 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
15309 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
15310 + // (uuuuu = wwww + 1)
15311 + // FIXME accept the surrogate code points (only)
15312 +
15313 + } else
15314 + goto fail;
15315 + } else
15316 + uchar = c;
15317 + put_unaligned (cpu_to_le16 (uchar), cp++);
15318 + count++;
15319 + len--;
15320 + }
15321 + return count;
15322 +fail:
15323 + return -1;
15324 +}
15325 +
15326 +
15327 +/**
15328 + * usb_gadget_get_string - fill out a string descriptor
15329 + * @table: of c strings encoded using UTF-8
15330 + * @id: string id, from low byte of wValue in get string descriptor
15331 + * @buf: at least 256 bytes
15332 + *
15333 + * Finds the UTF-8 string matching the ID, and converts it into a
15334 + * string descriptor in utf16-le.
15335 + * Returns length of descriptor (always even) or negative errno
15336 + *
15337 + * If your driver needs stings in multiple languages, you'll probably
15338 + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
15339 + * using this routine after choosing which set of UTF-8 strings to use.
15340 + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
15341 + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
15342 + * characters (which are also widely used in C strings).
15343 + */
15344 +int
15345 +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
15346 +{
15347 + struct usb_string *s;
15348 + int len;
15349 +
15350 + /* descriptor 0 has the language id */
15351 + if (id == 0) {
15352 + buf [0] = 4;
15353 + buf [1] = USB_DT_STRING;
15354 + buf [2] = (u8) table->language;
15355 + buf [3] = (u8) (table->language >> 8);
15356 + return 4;
15357 + }
15358 + for (s = table->strings; s && s->s; s++)
15359 + if (s->id == id)
15360 + break;
15361 +
15362 + /* unrecognized: stall. */
15363 + if (!s || !s->s)
15364 + return -EINVAL;
15365 +
15366 + /* string descriptors have length, tag, then UTF16-LE text */
15367 + len = min ((size_t) 126, strlen (s->s));
15368 + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
15369 + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
15370 + if (len < 0)
15371 + return -EINVAL;
15372 + buf [0] = (len + 1) * 2;
15373 + buf [1] = USB_DT_STRING;
15374 + return buf [0];
15375 +}
15376 +
15377 +
15378 +/*-------------------------------------------------------------------------*/
15379 +/*-------------------------------------------------------------------------*/
15380 +
15381 +
15382 +/**
15383 + * usb_descriptor_fillbuf - fill buffer with descriptors
15384 + * @buf: Buffer to be filled
15385 + * @buflen: Size of buf
15386 + * @src: Array of descriptor pointers, terminated by null pointer.
15387 + *
15388 + * Copies descriptors into the buffer, returning the length or a
15389 + * negative error code if they can't all be copied. Useful when
15390 + * assembling descriptors for an associated set of interfaces used
15391 + * as part of configuring a composite device; or in other cases where
15392 + * sets of descriptors need to be marshaled.
15393 + */
15394 +int
15395 +usb_descriptor_fillbuf(void *buf, unsigned buflen,
15396 + const struct usb_descriptor_header **src)
15397 +{
15398 + u8 *dest = buf;
15399 +
15400 + if (!src)
15401 + return -EINVAL;
15402 +
15403 + /* fill buffer from src[] until null descriptor ptr */
15404 + for (; 0 != *src; src++) {
15405 + unsigned len = (*src)->bLength;
15406 +
15407 + if (len > buflen)
15408 + return -EINVAL;
15409 + memcpy(dest, *src, len);
15410 + buflen -= len;
15411 + dest += len;
15412 + }
15413 + return dest - (u8 *)buf;
15414 +}
15415 +
15416 +
15417 +/**
15418 + * usb_gadget_config_buf - builts a complete configuration descriptor
15419 + * @config: Header for the descriptor, including characteristics such
15420 + * as power requirements and number of interfaces.
15421 + * @desc: Null-terminated vector of pointers to the descriptors (interface,
15422 + * endpoint, etc) defining all functions in this device configuration.
15423 + * @buf: Buffer for the resulting configuration descriptor.
15424 + * @length: Length of buffer. If this is not big enough to hold the
15425 + * entire configuration descriptor, an error code will be returned.
15426 + *
15427 + * This copies descriptors into the response buffer, building a descriptor
15428 + * for that configuration. It returns the buffer length or a negative
15429 + * status code. The config.wTotalLength field is set to match the length
15430 + * of the result, but other descriptor fields (including power usage and
15431 + * interface count) must be set by the caller.
15432 + *
15433 + * Gadget drivers could use this when constructing a config descriptor
15434 + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
15435 + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
15436 + */
15437 +int usb_gadget_config_buf(
15438 + const struct usb_config_descriptor *config,
15439 + void *buf,
15440 + unsigned length,
15441 + const struct usb_descriptor_header **desc
15442 +)
15443 +{
15444 + struct usb_config_descriptor *cp = buf;
15445 + int len;
15446 +
15447 + /* config descriptor first */
15448 + if (length < USB_DT_CONFIG_SIZE || !desc)
15449 + return -EINVAL;
15450 + *cp = *config;
15451 +
15452 + /* then interface/endpoint/class/vendor/... */
15453 + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
15454 + length - USB_DT_CONFIG_SIZE, desc);
15455 + if (len < 0)
15456 + return len;
15457 + len += USB_DT_CONFIG_SIZE;
15458 + if (len > 0xffff)
15459 + return -EINVAL;
15460 +
15461 + /* patch up the config descriptor */
15462 + cp->bLength = USB_DT_CONFIG_SIZE;
15463 + cp->bDescriptorType = USB_DT_CONFIG;
15464 + cp->wTotalLength = cpu_to_le16(len);
15465 + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
15466 + return len;
15467 +}
15468 +
15469 +/*-------------------------------------------------------------------------*/
15470 +/*-------------------------------------------------------------------------*/
15471 +
15472 +
15473 +#define RBUF_LEN (1024*1024)
15474 +static int rbuf_start;
15475 +static int rbuf_len;
15476 +static __u8 rbuf[RBUF_LEN];
15477 +
15478 +/*-------------------------------------------------------------------------*/
15479 +
15480 +#define DRIVER_VERSION "St Patrick's Day 2004"
15481 +
15482 +static const char shortname [] = "zero";
15483 +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
15484 +
15485 +static const char source_sink [] = "source and sink data";
15486 +static const char loopback [] = "loop input to output";
15487 +
15488 +/*-------------------------------------------------------------------------*/
15489 +
15490 +/*
15491 + * driver assumes self-powered hardware, and
15492 + * has no way for users to trigger remote wakeup.
15493 + *
15494 + * this version autoconfigures as much as possible,
15495 + * which is reasonable for most "bulk-only" drivers.
15496 + */
15497 +static const char *EP_IN_NAME; /* source */
15498 +static const char *EP_OUT_NAME; /* sink */
15499 +
15500 +/*-------------------------------------------------------------------------*/
15501 +
15502 +/* big enough to hold our biggest descriptor */
15503 +#define USB_BUFSIZ 512
15504 +
15505 +struct zero_dev {
15506 + spinlock_t lock;
15507 + struct usb_gadget *gadget;
15508 + struct usb_request *req; /* for control responses */
15509 +
15510 + /* when configured, we have one of two configs:
15511 + * - source data (in to host) and sink it (out from host)
15512 + * - or loop it back (out from host back in to host)
15513 + */
15514 + u8 config;
15515 + struct usb_ep *in_ep, *out_ep;
15516 +
15517 + /* autoresume timer */
15518 + struct timer_list resume;
15519 +};
15520 +
15521 +#define xprintk(d,level,fmt,args...) \
15522 + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
15523 +
15524 +#ifdef DEBUG
15525 +#define DBG(dev,fmt,args...) \
15526 + xprintk(dev , KERN_DEBUG , fmt , ## args)
15527 +#else
15528 +#define DBG(dev,fmt,args...) \
15529 + do { } while (0)
15530 +#endif /* DEBUG */
15531 +
15532 +#ifdef VERBOSE
15533 +#define VDBG DBG
15534 +#else
15535 +#define VDBG(dev,fmt,args...) \
15536 + do { } while (0)
15537 +#endif /* VERBOSE */
15538 +
15539 +#define ERROR(dev,fmt,args...) \
15540 + xprintk(dev , KERN_ERR , fmt , ## args)
15541 +#define WARN(dev,fmt,args...) \
15542 + xprintk(dev , KERN_WARNING , fmt , ## args)
15543 +#define INFO(dev,fmt,args...) \
15544 + xprintk(dev , KERN_INFO , fmt , ## args)
15545 +
15546 +/*-------------------------------------------------------------------------*/
15547 +
15548 +static unsigned buflen = 4096;
15549 +static unsigned qlen = 32;
15550 +static unsigned pattern = 0;
15551 +
15552 +module_param (buflen, uint, S_IRUGO|S_IWUSR);
15553 +module_param (qlen, uint, S_IRUGO|S_IWUSR);
15554 +module_param (pattern, uint, S_IRUGO|S_IWUSR);
15555 +
15556 +/*
15557 + * if it's nonzero, autoresume says how many seconds to wait
15558 + * before trying to wake up the host after suspend.
15559 + */
15560 +static unsigned autoresume = 0;
15561 +module_param (autoresume, uint, 0);
15562 +
15563 +/*
15564 + * Normally the "loopback" configuration is second (index 1) so
15565 + * it's not the default. Here's where to change that order, to
15566 + * work better with hosts where config changes are problematic.
15567 + * Or controllers (like superh) that only support one config.
15568 + */
15569 +static int loopdefault = 0;
15570 +
15571 +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
15572 +
15573 +/*-------------------------------------------------------------------------*/
15574 +
15575 +/* Thanks to NetChip Technologies for donating this product ID.
15576 + *
15577 + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
15578 + * Instead: allocate your own, using normal USB-IF procedures.
15579 + */
15580 +#ifndef CONFIG_USB_ZERO_HNPTEST
15581 +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
15582 +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
15583 +#else
15584 +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
15585 +#define DRIVER_PRODUCT_NUM 0xbadd
15586 +#endif
15587 +
15588 +/*-------------------------------------------------------------------------*/
15589 +
15590 +/*
15591 + * DESCRIPTORS ... most are static, but strings and (full)
15592 + * configuration descriptors are built on demand.
15593 + */
15594 +
15595 +/*
15596 +#define STRING_MANUFACTURER 25
15597 +#define STRING_PRODUCT 42
15598 +#define STRING_SERIAL 101
15599 +*/
15600 +#define STRING_MANUFACTURER 1
15601 +#define STRING_PRODUCT 2
15602 +#define STRING_SERIAL 3
15603 +
15604 +#define STRING_SOURCE_SINK 250
15605 +#define STRING_LOOPBACK 251
15606 +
15607 +/*
15608 + * This device advertises two configurations; these numbers work
15609 + * on a pxa250 as well as more flexible hardware.
15610 + */
15611 +#define CONFIG_SOURCE_SINK 3
15612 +#define CONFIG_LOOPBACK 2
15613 +
15614 +/*
15615 +static struct usb_device_descriptor
15616 +device_desc = {
15617 + .bLength = sizeof device_desc,
15618 + .bDescriptorType = USB_DT_DEVICE,
15619 +
15620 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
15621 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
15622 +
15623 + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
15624 + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
15625 + .iManufacturer = STRING_MANUFACTURER,
15626 + .iProduct = STRING_PRODUCT,
15627 + .iSerialNumber = STRING_SERIAL,
15628 + .bNumConfigurations = 2,
15629 +};
15630 +*/
15631 +static struct usb_device_descriptor
15632 +device_desc = {
15633 + .bLength = sizeof device_desc,
15634 + .bDescriptorType = USB_DT_DEVICE,
15635 + .bcdUSB = __constant_cpu_to_le16 (0x0100),
15636 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
15637 + .bDeviceSubClass = 0,
15638 + .bDeviceProtocol = 0,
15639 + .bMaxPacketSize0 = 64,
15640 + .bcdDevice = __constant_cpu_to_le16 (0x0100),
15641 + .idVendor = __constant_cpu_to_le16 (0x0499),
15642 + .idProduct = __constant_cpu_to_le16 (0x3002),
15643 + .iManufacturer = STRING_MANUFACTURER,
15644 + .iProduct = STRING_PRODUCT,
15645 + .iSerialNumber = STRING_SERIAL,
15646 + .bNumConfigurations = 1,
15647 +};
15648 +
15649 +static struct usb_config_descriptor
15650 +z_config = {
15651 + .bLength = sizeof z_config,
15652 + .bDescriptorType = USB_DT_CONFIG,
15653 +
15654 + /* compute wTotalLength on the fly */
15655 + .bNumInterfaces = 2,
15656 + .bConfigurationValue = 1,
15657 + .iConfiguration = 0,
15658 + .bmAttributes = 0x40,
15659 + .bMaxPower = 0, /* self-powered */
15660 +};
15661 +
15662 +
15663 +static struct usb_otg_descriptor
15664 +otg_descriptor = {
15665 + .bLength = sizeof otg_descriptor,
15666 + .bDescriptorType = USB_DT_OTG,
15667 +
15668 + .bmAttributes = USB_OTG_SRP,
15669 +};
15670 +
15671 +/* one interface in each configuration */
15672 +#ifdef CONFIG_USB_GADGET_DUALSPEED
15673 +
15674 +/*
15675 + * usb 2.0 devices need to expose both high speed and full speed
15676 + * descriptors, unless they only run at full speed.
15677 + *
15678 + * that means alternate endpoint descriptors (bigger packets)
15679 + * and a "device qualifier" ... plus more construction options
15680 + * for the config descriptor.
15681 + */
15682 +
15683 +static struct usb_qualifier_descriptor
15684 +dev_qualifier = {
15685 + .bLength = sizeof dev_qualifier,
15686 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
15687 +
15688 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
15689 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
15690 +
15691 + .bNumConfigurations = 2,
15692 +};
15693 +
15694 +
15695 +struct usb_cs_as_general_descriptor {
15696 + __u8 bLength;
15697 + __u8 bDescriptorType;
15698 +
15699 + __u8 bDescriptorSubType;
15700 + __u8 bTerminalLink;
15701 + __u8 bDelay;
15702 + __u16 wFormatTag;
15703 +} __attribute__ ((packed));
15704 +
15705 +struct usb_cs_as_format_descriptor {
15706 + __u8 bLength;
15707 + __u8 bDescriptorType;
15708 +
15709 + __u8 bDescriptorSubType;
15710 + __u8 bFormatType;
15711 + __u8 bNrChannels;
15712 + __u8 bSubframeSize;
15713 + __u8 bBitResolution;
15714 + __u8 bSamfreqType;
15715 + __u8 tLowerSamFreq[3];
15716 + __u8 tUpperSamFreq[3];
15717 +} __attribute__ ((packed));
15718 +
15719 +static const struct usb_interface_descriptor
15720 +z_audio_control_if_desc = {
15721 + .bLength = sizeof z_audio_control_if_desc,
15722 + .bDescriptorType = USB_DT_INTERFACE,
15723 + .bInterfaceNumber = 0,
15724 + .bAlternateSetting = 0,
15725 + .bNumEndpoints = 0,
15726 + .bInterfaceClass = USB_CLASS_AUDIO,
15727 + .bInterfaceSubClass = 0x1,
15728 + .bInterfaceProtocol = 0,
15729 + .iInterface = 0,
15730 +};
15731 +
15732 +static const struct usb_interface_descriptor
15733 +z_audio_if_desc = {
15734 + .bLength = sizeof z_audio_if_desc,
15735 + .bDescriptorType = USB_DT_INTERFACE,
15736 + .bInterfaceNumber = 1,
15737 + .bAlternateSetting = 0,
15738 + .bNumEndpoints = 0,
15739 + .bInterfaceClass = USB_CLASS_AUDIO,
15740 + .bInterfaceSubClass = 0x2,
15741 + .bInterfaceProtocol = 0,
15742 + .iInterface = 0,
15743 +};
15744 +
15745 +static const struct usb_interface_descriptor
15746 +z_audio_if_desc2 = {
15747 + .bLength = sizeof z_audio_if_desc,
15748 + .bDescriptorType = USB_DT_INTERFACE,
15749 + .bInterfaceNumber = 1,
15750 + .bAlternateSetting = 1,
15751 + .bNumEndpoints = 1,
15752 + .bInterfaceClass = USB_CLASS_AUDIO,
15753 + .bInterfaceSubClass = 0x2,
15754 + .bInterfaceProtocol = 0,
15755 + .iInterface = 0,
15756 +};
15757 +
15758 +static const struct usb_cs_as_general_descriptor
15759 +z_audio_cs_as_if_desc = {
15760 + .bLength = 7,
15761 + .bDescriptorType = 0x24,
15762 +
15763 + .bDescriptorSubType = 0x01,
15764 + .bTerminalLink = 0x01,
15765 + .bDelay = 0x0,
15766 + .wFormatTag = __constant_cpu_to_le16 (0x0001)
15767 +};
15768 +
15769 +
15770 +static const struct usb_cs_as_format_descriptor
15771 +z_audio_cs_as_format_desc = {
15772 + .bLength = 0xe,
15773 + .bDescriptorType = 0x24,
15774 +
15775 + .bDescriptorSubType = 2,
15776 + .bFormatType = 1,
15777 + .bNrChannels = 1,
15778 + .bSubframeSize = 1,
15779 + .bBitResolution = 8,
15780 + .bSamfreqType = 0,
15781 + .tLowerSamFreq = {0x7e, 0x13, 0x00},
15782 + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
15783 +};
15784 +
15785 +static const struct usb_endpoint_descriptor
15786 +z_iso_ep = {
15787 + .bLength = 0x09,
15788 + .bDescriptorType = 0x05,
15789 + .bEndpointAddress = 0x04,
15790 + .bmAttributes = 0x09,
15791 + .wMaxPacketSize = 0x0038,
15792 + .bInterval = 0x01,
15793 + .bRefresh = 0x00,
15794 + .bSynchAddress = 0x00,
15795 +};
15796 +
15797 +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
15798 +
15799 +// 9 bytes
15800 +static char z_ac_interface_header_desc[] =
15801 +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
15802 +
15803 +// 12 bytes
15804 +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
15805 + 0x03, 0x00, 0x00, 0x00};
15806 +// 13 bytes
15807 +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
15808 + 0x02, 0x00, 0x02, 0x00, 0x00};
15809 +// 9 bytes
15810 +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
15811 + 0x00};
15812 +
15813 +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
15814 + 0x00};
15815 +
15816 +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
15817 +
15818 +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
15819 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
15820 +
15821 +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
15822 + 0x00};
15823 +
15824 +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
15825 +
15826 +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
15827 + 0x00};
15828 +
15829 +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
15830 +
15831 +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
15832 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
15833 +
15834 +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
15835 + 0x00};
15836 +
15837 +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
15838 +
15839 +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
15840 + 0x00};
15841 +
15842 +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
15843 +
15844 +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
15845 + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
15846 +
15847 +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
15848 + 0x00};
15849 +
15850 +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
15851 +
15852 +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
15853 + 0x00};
15854 +
15855 +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
15856 +
15857 +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
15858 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
15859 +
15860 +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
15861 + 0x00};
15862 +
15863 +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
15864 +
15865 +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
15866 + 0x00};
15867 +
15868 +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
15869 +
15870 +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
15871 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
15872 +
15873 +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
15874 + 0x00};
15875 +
15876 +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
15877 +
15878 +
15879 +
15880 +static const struct usb_descriptor_header *z_function [] = {
15881 + (struct usb_descriptor_header *) &z_audio_control_if_desc,
15882 + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
15883 + (struct usb_descriptor_header *) &z_0,
15884 + (struct usb_descriptor_header *) &z_1,
15885 + (struct usb_descriptor_header *) &z_2,
15886 + (struct usb_descriptor_header *) &z_audio_if_desc,
15887 + (struct usb_descriptor_header *) &z_audio_if_desc2,
15888 + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
15889 + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
15890 + (struct usb_descriptor_header *) &z_iso_ep,
15891 + (struct usb_descriptor_header *) &z_iso_ep2,
15892 + (struct usb_descriptor_header *) &za_0,
15893 + (struct usb_descriptor_header *) &za_1,
15894 + (struct usb_descriptor_header *) &za_2,
15895 + (struct usb_descriptor_header *) &za_3,
15896 + (struct usb_descriptor_header *) &za_4,
15897 + (struct usb_descriptor_header *) &za_5,
15898 + (struct usb_descriptor_header *) &za_6,
15899 + (struct usb_descriptor_header *) &za_7,
15900 + (struct usb_descriptor_header *) &za_8,
15901 + (struct usb_descriptor_header *) &za_9,
15902 + (struct usb_descriptor_header *) &za_10,
15903 + (struct usb_descriptor_header *) &za_11,
15904 + (struct usb_descriptor_header *) &za_12,
15905 + (struct usb_descriptor_header *) &za_13,
15906 + (struct usb_descriptor_header *) &za_14,
15907 + (struct usb_descriptor_header *) &za_15,
15908 + (struct usb_descriptor_header *) &za_16,
15909 + (struct usb_descriptor_header *) &za_17,
15910 + (struct usb_descriptor_header *) &za_18,
15911 + (struct usb_descriptor_header *) &za_19,
15912 + (struct usb_descriptor_header *) &za_20,
15913 + (struct usb_descriptor_header *) &za_21,
15914 + (struct usb_descriptor_header *) &za_22,
15915 + (struct usb_descriptor_header *) &za_23,
15916 + (struct usb_descriptor_header *) &za_24,
15917 + NULL,
15918 +};
15919 +
15920 +/* maxpacket and other transfer characteristics vary by speed. */
15921 +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
15922 +
15923 +#else
15924 +
15925 +/* if there's no high speed support, maxpacket doesn't change. */
15926 +#define ep_desc(g,hs,fs) fs
15927 +
15928 +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
15929 +
15930 +static char manufacturer [40];
15931 +//static char serial [40];
15932 +static char serial [] = "Ser 00 em";
15933 +
15934 +/* static strings, in UTF-8 */
15935 +static struct usb_string strings [] = {
15936 + { STRING_MANUFACTURER, manufacturer, },
15937 + { STRING_PRODUCT, longname, },
15938 + { STRING_SERIAL, serial, },
15939 + { STRING_LOOPBACK, loopback, },
15940 + { STRING_SOURCE_SINK, source_sink, },
15941 + { } /* end of list */
15942 +};
15943 +
15944 +static struct usb_gadget_strings stringtab = {
15945 + .language = 0x0409, /* en-us */
15946 + .strings = strings,
15947 +};
15948 +
15949 +/*
15950 + * config descriptors are also handcrafted. these must agree with code
15951 + * that sets configurations, and with code managing interfaces and their
15952 + * altsettings. other complexity may come from:
15953 + *
15954 + * - high speed support, including "other speed config" rules
15955 + * - multiple configurations
15956 + * - interfaces with alternate settings
15957 + * - embedded class or vendor-specific descriptors
15958 + *
15959 + * this handles high speed, and has a second config that could as easily
15960 + * have been an alternate interface setting (on most hardware).
15961 + *
15962 + * NOTE: to demonstrate (and test) more USB capabilities, this driver
15963 + * should include an altsetting to test interrupt transfers, including
15964 + * high bandwidth modes at high speed. (Maybe work like Intel's test
15965 + * device?)
15966 + */
15967 +static int
15968 +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
15969 +{
15970 + int len;
15971 + const struct usb_descriptor_header **function;
15972 +
15973 + function = z_function;
15974 + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
15975 + if (len < 0)
15976 + return len;
15977 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
15978 + return len;
15979 +}
15980 +
15981 +/*-------------------------------------------------------------------------*/
15982 +
15983 +static struct usb_request *
15984 +alloc_ep_req (struct usb_ep *ep, unsigned length)
15985 +{
15986 + struct usb_request *req;
15987 +
15988 + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
15989 + if (req) {
15990 + req->length = length;
15991 + req->buf = usb_ep_alloc_buffer (ep, length,
15992 + &req->dma, GFP_ATOMIC);
15993 + if (!req->buf) {
15994 + usb_ep_free_request (ep, req);
15995 + req = NULL;
15996 + }
15997 + }
15998 + return req;
15999 +}
16000 +
16001 +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
16002 +{
16003 + if (req->buf)
16004 + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
16005 + usb_ep_free_request (ep, req);
16006 +}
16007 +
16008 +/*-------------------------------------------------------------------------*/
16009 +
16010 +/* optionally require specific source/sink data patterns */
16011 +
16012 +static int
16013 +check_read_data (
16014 + struct zero_dev *dev,
16015 + struct usb_ep *ep,
16016 + struct usb_request *req
16017 +)
16018 +{
16019 + unsigned i;
16020 + u8 *buf = req->buf;
16021 +
16022 + for (i = 0; i < req->actual; i++, buf++) {
16023 + switch (pattern) {
16024 + /* all-zeroes has no synchronization issues */
16025 + case 0:
16026 + if (*buf == 0)
16027 + continue;
16028 + break;
16029 + /* mod63 stays in sync with short-terminated transfers,
16030 + * or otherwise when host and gadget agree on how large
16031 + * each usb transfer request should be. resync is done
16032 + * with set_interface or set_config.
16033 + */
16034 + case 1:
16035 + if (*buf == (u8)(i % 63))
16036 + continue;
16037 + break;
16038 + }
16039 + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
16040 + usb_ep_set_halt (ep);
16041 + return -EINVAL;
16042 + }
16043 + return 0;
16044 +}
16045 +
16046 +/*-------------------------------------------------------------------------*/
16047 +
16048 +static void zero_reset_config (struct zero_dev *dev)
16049 +{
16050 + if (dev->config == 0)
16051 + return;
16052 +
16053 + DBG (dev, "reset config\n");
16054 +
16055 + /* just disable endpoints, forcing completion of pending i/o.
16056 + * all our completion handlers free their requests in this case.
16057 + */
16058 + if (dev->in_ep) {
16059 + usb_ep_disable (dev->in_ep);
16060 + dev->in_ep = NULL;
16061 + }
16062 + if (dev->out_ep) {
16063 + usb_ep_disable (dev->out_ep);
16064 + dev->out_ep = NULL;
16065 + }
16066 + dev->config = 0;
16067 + del_timer (&dev->resume);
16068 +}
16069 +
16070 +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
16071 +
16072 +static void
16073 +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
16074 +{
16075 + struct zero_dev *dev = ep->driver_data;
16076 + int status = req->status;
16077 + int i, j;
16078 +
16079 + switch (status) {
16080 +
16081 + case 0: /* normal completion? */
16082 + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
16083 + for (i=0, j=rbuf_start; i<req->actual; i++) {
16084 + //printk ("%02x ", ((__u8*)req->buf)[i]);
16085 + rbuf[j] = ((__u8*)req->buf)[i];
16086 + j++;
16087 + if (j >= RBUF_LEN) j=0;
16088 + }
16089 + rbuf_start = j;
16090 + //printk ("\n\n");
16091 +
16092 + if (rbuf_len < RBUF_LEN) {
16093 + rbuf_len += req->actual;
16094 + if (rbuf_len > RBUF_LEN) {
16095 + rbuf_len = RBUF_LEN;
16096 + }
16097 + }
16098 +
16099 + break;
16100 +
16101 + /* this endpoint is normally active while we're configured */
16102 + case -ECONNABORTED: /* hardware forced ep reset */
16103 + case -ECONNRESET: /* request dequeued */
16104 + case -ESHUTDOWN: /* disconnect from host */
16105 + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
16106 + req->actual, req->length);
16107 + if (ep == dev->out_ep)
16108 + check_read_data (dev, ep, req);
16109 + free_ep_req (ep, req);
16110 + return;
16111 +
16112 + case -EOVERFLOW: /* buffer overrun on read means that
16113 + * we didn't provide a big enough
16114 + * buffer.
16115 + */
16116 + default:
16117 +#if 1
16118 + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
16119 + status, req->actual, req->length);
16120 +#endif
16121 + case -EREMOTEIO: /* short read */
16122 + break;
16123 + }
16124 +
16125 + status = usb_ep_queue (ep, req, GFP_ATOMIC);
16126 + if (status) {
16127 + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
16128 + ep->name, req->length, status);
16129 + usb_ep_set_halt (ep);
16130 + /* FIXME recover later ... somehow */
16131 + }
16132 +}
16133 +
16134 +static struct usb_request *
16135 +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
16136 +{
16137 + struct usb_request *req;
16138 + int status;
16139 +
16140 + req = alloc_ep_req (ep, 512);
16141 + if (!req)
16142 + return NULL;
16143 +
16144 + req->complete = zero_isoc_complete;
16145 +
16146 + status = usb_ep_queue (ep, req, gfp_flags);
16147 + if (status) {
16148 + struct zero_dev *dev = ep->driver_data;
16149 +
16150 + ERROR (dev, "start %s --> %d\n", ep->name, status);
16151 + free_ep_req (ep, req);
16152 + req = NULL;
16153 + }
16154 +
16155 + return req;
16156 +}
16157 +
16158 +/* change our operational config. this code must agree with the code
16159 + * that returns config descriptors, and altsetting code.
16160 + *
16161 + * it's also responsible for power management interactions. some
16162 + * configurations might not work with our current power sources.
16163 + *
16164 + * note that some device controller hardware will constrain what this
16165 + * code can do, perhaps by disallowing more than one configuration or
16166 + * by limiting configuration choices (like the pxa2xx).
16167 + */
16168 +static int
16169 +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
16170 +{
16171 + int result = 0;
16172 + struct usb_gadget *gadget = dev->gadget;
16173 + const struct usb_endpoint_descriptor *d;
16174 + struct usb_ep *ep;
16175 +
16176 + if (number == dev->config)
16177 + return 0;
16178 +
16179 + zero_reset_config (dev);
16180 +
16181 + gadget_for_each_ep (ep, gadget) {
16182 +
16183 + if (strcmp (ep->name, "ep4") == 0) {
16184 +
16185 + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
16186 + result = usb_ep_enable (ep, d);
16187 +
16188 + if (result == 0) {
16189 + ep->driver_data = dev;
16190 + dev->in_ep = ep;
16191 +
16192 + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
16193 +
16194 + dev->in_ep = ep;
16195 + continue;
16196 + }
16197 +
16198 + usb_ep_disable (ep);
16199 + result = -EIO;
16200 + }
16201 + }
16202 +
16203 + }
16204 +
16205 + dev->config = number;
16206 + return result;
16207 +}
16208 +
16209 +/*-------------------------------------------------------------------------*/
16210 +
16211 +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
16212 +{
16213 + if (req->status || req->actual != req->length)
16214 + DBG ((struct zero_dev *) ep->driver_data,
16215 + "setup complete --> %d, %d/%d\n",
16216 + req->status, req->actual, req->length);
16217 +}
16218 +
16219 +/*
16220 + * The setup() callback implements all the ep0 functionality that's
16221 + * not handled lower down, in hardware or the hardware driver (like
16222 + * device and endpoint feature flags, and their status). It's all
16223 + * housekeeping for the gadget function we're implementing. Most of
16224 + * the work is in config-specific setup.
16225 + */
16226 +static int
16227 +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
16228 +{
16229 + struct zero_dev *dev = get_gadget_data (gadget);
16230 + struct usb_request *req = dev->req;
16231 + int value = -EOPNOTSUPP;
16232 +
16233 + /* usually this stores reply data in the pre-allocated ep0 buffer,
16234 + * but config change events will reconfigure hardware.
16235 + */
16236 + req->zero = 0;
16237 + switch (ctrl->bRequest) {
16238 +
16239 + case USB_REQ_GET_DESCRIPTOR:
16240 +
16241 + switch (ctrl->wValue >> 8) {
16242 +
16243 + case USB_DT_DEVICE:
16244 + value = min (ctrl->wLength, (u16) sizeof device_desc);
16245 + memcpy (req->buf, &device_desc, value);
16246 + break;
16247 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16248 + case USB_DT_DEVICE_QUALIFIER:
16249 + if (!gadget->is_dualspeed)
16250 + break;
16251 + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
16252 + memcpy (req->buf, &dev_qualifier, value);
16253 + break;
16254 +
16255 + case USB_DT_OTHER_SPEED_CONFIG:
16256 + if (!gadget->is_dualspeed)
16257 + break;
16258 + // FALLTHROUGH
16259 +#endif /* CONFIG_USB_GADGET_DUALSPEED */
16260 + case USB_DT_CONFIG:
16261 + value = config_buf (gadget, req->buf,
16262 + ctrl->wValue >> 8,
16263 + ctrl->wValue & 0xff);
16264 + if (value >= 0)
16265 + value = min (ctrl->wLength, (u16) value);
16266 + break;
16267 +
16268 + case USB_DT_STRING:
16269 + /* wIndex == language code.
16270 + * this driver only handles one language, you can
16271 + * add string tables for other languages, using
16272 + * any UTF-8 characters
16273 + */
16274 + value = usb_gadget_get_string (&stringtab,
16275 + ctrl->wValue & 0xff, req->buf);
16276 + if (value >= 0) {
16277 + value = min (ctrl->wLength, (u16) value);
16278 + }
16279 + break;
16280 + }
16281 + break;
16282 +
16283 + /* currently two configs, two speeds */
16284 + case USB_REQ_SET_CONFIGURATION:
16285 + if (ctrl->bRequestType != 0)
16286 + goto unknown;
16287 +
16288 + spin_lock (&dev->lock);
16289 + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
16290 + spin_unlock (&dev->lock);
16291 + break;
16292 + case USB_REQ_GET_CONFIGURATION:
16293 + if (ctrl->bRequestType != USB_DIR_IN)
16294 + goto unknown;
16295 + *(u8 *)req->buf = dev->config;
16296 + value = min (ctrl->wLength, (u16) 1);
16297 + break;
16298 +
16299 + /* until we add altsetting support, or other interfaces,
16300 + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
16301 + * and already killed pending endpoint I/O.
16302 + */
16303 + case USB_REQ_SET_INTERFACE:
16304 +
16305 + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
16306 + goto unknown;
16307 + spin_lock (&dev->lock);
16308 + if (dev->config) {
16309 + u8 config = dev->config;
16310 +
16311 + /* resets interface configuration, forgets about
16312 + * previous transaction state (queued bufs, etc)
16313 + * and re-inits endpoint state (toggle etc)
16314 + * no response queued, just zero status == success.
16315 + * if we had more than one interface we couldn't
16316 + * use this "reset the config" shortcut.
16317 + */
16318 + zero_reset_config (dev);
16319 + zero_set_config (dev, config, GFP_ATOMIC);
16320 + value = 0;
16321 + }
16322 + spin_unlock (&dev->lock);
16323 + break;
16324 + case USB_REQ_GET_INTERFACE:
16325 + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
16326 + value = ctrl->wLength;
16327 + break;
16328 + }
16329 + else {
16330 + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
16331 + goto unknown;
16332 + if (!dev->config)
16333 + break;
16334 + if (ctrl->wIndex != 0) {
16335 + value = -EDOM;
16336 + break;
16337 + }
16338 + *(u8 *)req->buf = 0;
16339 + value = min (ctrl->wLength, (u16) 1);
16340 + }
16341 + break;
16342 +
16343 + /*
16344 + * These are the same vendor-specific requests supported by
16345 + * Intel's USB 2.0 compliance test devices. We exceed that
16346 + * device spec by allowing multiple-packet requests.
16347 + */
16348 + case 0x5b: /* control WRITE test -- fill the buffer */
16349 + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
16350 + goto unknown;
16351 + if (ctrl->wValue || ctrl->wIndex)
16352 + break;
16353 + /* just read that many bytes into the buffer */
16354 + if (ctrl->wLength > USB_BUFSIZ)
16355 + break;
16356 + value = ctrl->wLength;
16357 + break;
16358 + case 0x5c: /* control READ test -- return the buffer */
16359 + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
16360 + goto unknown;
16361 + if (ctrl->wValue || ctrl->wIndex)
16362 + break;
16363 + /* expect those bytes are still in the buffer; send back */
16364 + if (ctrl->wLength > USB_BUFSIZ
16365 + || ctrl->wLength != req->length)
16366 + break;
16367 + value = ctrl->wLength;
16368 + break;
16369 +
16370 + case 0x01: // SET_CUR
16371 + case 0x02:
16372 + case 0x03:
16373 + case 0x04:
16374 + case 0x05:
16375 + value = ctrl->wLength;
16376 + break;
16377 + case 0x81:
16378 + switch (ctrl->wValue) {
16379 + case 0x0201:
16380 + case 0x0202:
16381 + ((u8*)req->buf)[0] = 0x00;
16382 + ((u8*)req->buf)[1] = 0xe3;
16383 + break;
16384 + case 0x0300:
16385 + case 0x0500:
16386 + ((u8*)req->buf)[0] = 0x00;
16387 + break;
16388 + }
16389 + //((u8*)req->buf)[0] = 0x81;
16390 + //((u8*)req->buf)[1] = 0x81;
16391 + value = ctrl->wLength;
16392 + break;
16393 + case 0x82:
16394 + switch (ctrl->wValue) {
16395 + case 0x0201:
16396 + case 0x0202:
16397 + ((u8*)req->buf)[0] = 0x00;
16398 + ((u8*)req->buf)[1] = 0xc3;
16399 + break;
16400 + case 0x0300:
16401 + case 0x0500:
16402 + ((u8*)req->buf)[0] = 0x00;
16403 + break;
16404 + }
16405 + //((u8*)req->buf)[0] = 0x82;
16406 + //((u8*)req->buf)[1] = 0x82;
16407 + value = ctrl->wLength;
16408 + break;
16409 + case 0x83:
16410 + switch (ctrl->wValue) {
16411 + case 0x0201:
16412 + case 0x0202:
16413 + ((u8*)req->buf)[0] = 0x00;
16414 + ((u8*)req->buf)[1] = 0x00;
16415 + break;
16416 + case 0x0300:
16417 + ((u8*)req->buf)[0] = 0x60;
16418 + break;
16419 + case 0x0500:
16420 + ((u8*)req->buf)[0] = 0x18;
16421 + break;
16422 + }
16423 + //((u8*)req->buf)[0] = 0x83;
16424 + //((u8*)req->buf)[1] = 0x83;
16425 + value = ctrl->wLength;
16426 + break;
16427 + case 0x84:
16428 + switch (ctrl->wValue) {
16429 + case 0x0201:
16430 + case 0x0202:
16431 + ((u8*)req->buf)[0] = 0x00;
16432 + ((u8*)req->buf)[1] = 0x01;
16433 + break;
16434 + case 0x0300:
16435 + case 0x0500:
16436 + ((u8*)req->buf)[0] = 0x08;
16437 + break;
16438 + }
16439 + //((u8*)req->buf)[0] = 0x84;
16440 + //((u8*)req->buf)[1] = 0x84;
16441 + value = ctrl->wLength;
16442 + break;
16443 + case 0x85:
16444 + ((u8*)req->buf)[0] = 0x85;
16445 + ((u8*)req->buf)[1] = 0x85;
16446 + value = ctrl->wLength;
16447 + break;
16448 +
16449 +
16450 + default:
16451 +unknown:
16452 + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
16453 + ctrl->bRequestType, ctrl->bRequest,
16454 + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
16455 + }
16456 +
16457 + /* respond with data transfer before status phase? */
16458 + if (value >= 0) {
16459 + req->length = value;
16460 + req->zero = value < ctrl->wLength
16461 + && (value % gadget->ep0->maxpacket) == 0;
16462 + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
16463 + if (value < 0) {
16464 + DBG (dev, "ep_queue < 0 --> %d\n", value);
16465 + req->status = 0;
16466 + zero_setup_complete (gadget->ep0, req);
16467 + }
16468 + }
16469 +
16470 + /* device either stalls (value < 0) or reports success */
16471 + return value;
16472 +}
16473 +
16474 +static void
16475 +zero_disconnect (struct usb_gadget *gadget)
16476 +{
16477 + struct zero_dev *dev = get_gadget_data (gadget);
16478 + unsigned long flags;
16479 +
16480 + spin_lock_irqsave (&dev->lock, flags);
16481 + zero_reset_config (dev);
16482 +
16483 + /* a more significant application might have some non-usb
16484 + * activities to quiesce here, saving resources like power
16485 + * or pushing the notification up a network stack.
16486 + */
16487 + spin_unlock_irqrestore (&dev->lock, flags);
16488 +
16489 + /* next we may get setup() calls to enumerate new connections;
16490 + * or an unbind() during shutdown (including removing module).
16491 + */
16492 +}
16493 +
16494 +static void
16495 +zero_autoresume (unsigned long _dev)
16496 +{
16497 + struct zero_dev *dev = (struct zero_dev *) _dev;
16498 + int status;
16499 +
16500 + /* normally the host would be woken up for something
16501 + * more significant than just a timer firing...
16502 + */
16503 + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
16504 + status = usb_gadget_wakeup (dev->gadget);
16505 + DBG (dev, "wakeup --> %d\n", status);
16506 + }
16507 +}
16508 +
16509 +/*-------------------------------------------------------------------------*/
16510 +
16511 +static void
16512 +zero_unbind (struct usb_gadget *gadget)
16513 +{
16514 + struct zero_dev *dev = get_gadget_data (gadget);
16515 +
16516 + DBG (dev, "unbind\n");
16517 +
16518 + /* we've already been disconnected ... no i/o is active */
16519 + if (dev->req)
16520 + free_ep_req (gadget->ep0, dev->req);
16521 + del_timer_sync (&dev->resume);
16522 + kfree (dev);
16523 + set_gadget_data (gadget, NULL);
16524 +}
16525 +
16526 +static int
16527 +zero_bind (struct usb_gadget *gadget)
16528 +{
16529 + struct zero_dev *dev;
16530 + //struct usb_ep *ep;
16531 +
16532 + printk("binding\n");
16533 + /*
16534 + * DRIVER POLICY CHOICE: you may want to do this differently.
16535 + * One thing to avoid is reusing a bcdDevice revision code
16536 + * with different host-visible configurations or behavior
16537 + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
16538 + */
16539 + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
16540 +
16541 +
16542 + /* ok, we made sense of the hardware ... */
16543 + dev = kzalloc (sizeof *dev, SLAB_KERNEL);
16544 + if (!dev)
16545 + return -ENOMEM;
16546 + spin_lock_init (&dev->lock);
16547 + dev->gadget = gadget;
16548 + set_gadget_data (gadget, dev);
16549 +
16550 + /* preallocate control response and buffer */
16551 + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
16552 + if (!dev->req)
16553 + goto enomem;
16554 + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
16555 + &dev->req->dma, GFP_KERNEL);
16556 + if (!dev->req->buf)
16557 + goto enomem;
16558 +
16559 + dev->req->complete = zero_setup_complete;
16560 +
16561 + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
16562 +
16563 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16564 + /* assume ep0 uses the same value for both speeds ... */
16565 + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
16566 +
16567 + /* and that all endpoints are dual-speed */
16568 + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
16569 + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
16570 +#endif
16571 +
16572 + usb_gadget_set_selfpowered (gadget);
16573 +
16574 + init_timer (&dev->resume);
16575 + dev->resume.function = zero_autoresume;
16576 + dev->resume.data = (unsigned long) dev;
16577 +
16578 + gadget->ep0->driver_data = dev;
16579 +
16580 + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
16581 + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
16582 + EP_OUT_NAME, EP_IN_NAME);
16583 +
16584 + snprintf (manufacturer, sizeof manufacturer,
16585 + UTS_SYSNAME " " UTS_RELEASE " with %s",
16586 + gadget->name);
16587 +
16588 + return 0;
16589 +
16590 +enomem:
16591 + zero_unbind (gadget);
16592 + return -ENOMEM;
16593 +}
16594 +
16595 +/*-------------------------------------------------------------------------*/
16596 +
16597 +static void
16598 +zero_suspend (struct usb_gadget *gadget)
16599 +{
16600 + struct zero_dev *dev = get_gadget_data (gadget);
16601 +
16602 + if (gadget->speed == USB_SPEED_UNKNOWN)
16603 + return;
16604 +
16605 + if (autoresume) {
16606 + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
16607 + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
16608 + } else
16609 + DBG (dev, "suspend\n");
16610 +}
16611 +
16612 +static void
16613 +zero_resume (struct usb_gadget *gadget)
16614 +{
16615 + struct zero_dev *dev = get_gadget_data (gadget);
16616 +
16617 + DBG (dev, "resume\n");
16618 + del_timer (&dev->resume);
16619 +}
16620 +
16621 +
16622 +/*-------------------------------------------------------------------------*/
16623 +
16624 +static struct usb_gadget_driver zero_driver = {
16625 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16626 + .speed = USB_SPEED_HIGH,
16627 +#else
16628 + .speed = USB_SPEED_FULL,
16629 +#endif
16630 + .function = (char *) longname,
16631 + .bind = zero_bind,
16632 + .unbind = zero_unbind,
16633 +
16634 + .setup = zero_setup,
16635 + .disconnect = zero_disconnect,
16636 +
16637 + .suspend = zero_suspend,
16638 + .resume = zero_resume,
16639 +
16640 + .driver = {
16641 + .name = (char *) shortname,
16642 + // .shutdown = ...
16643 + // .suspend = ...
16644 + // .resume = ...
16645 + },
16646 +};
16647 +
16648 +MODULE_AUTHOR ("David Brownell");
16649 +MODULE_LICENSE ("Dual BSD/GPL");
16650 +
16651 +static struct proc_dir_entry *pdir, *pfile;
16652 +
16653 +static int isoc_read_data (char *page, char **start,
16654 + off_t off, int count,
16655 + int *eof, void *data)
16656 +{
16657 + int i;
16658 + static int c = 0;
16659 + static int done = 0;
16660 + static int s = 0;
16661 +
16662 +/*
16663 + printk ("\ncount: %d\n", count);
16664 + printk ("rbuf_start: %d\n", rbuf_start);
16665 + printk ("rbuf_len: %d\n", rbuf_len);
16666 + printk ("off: %d\n", off);
16667 + printk ("start: %p\n\n", *start);
16668 +*/
16669 + if (done) {
16670 + c = 0;
16671 + done = 0;
16672 + *eof = 1;
16673 + return 0;
16674 + }
16675 +
16676 + if (c == 0) {
16677 + if (rbuf_len == RBUF_LEN)
16678 + s = rbuf_start;
16679 + else s = 0;
16680 + }
16681 +
16682 + for (i=0; i<count && c<rbuf_len; i++, c++) {
16683 + page[i] = rbuf[(c+s) % RBUF_LEN];
16684 + }
16685 + *start = page;
16686 +
16687 + if (c >= rbuf_len) {
16688 + *eof = 1;
16689 + done = 1;
16690 + }
16691 +
16692 +
16693 + return i;
16694 +}
16695 +
16696 +static int __init init (void)
16697 +{
16698 +
16699 + int retval = 0;
16700 +
16701 + pdir = proc_mkdir("isoc_test", NULL);
16702 + if(pdir == NULL) {
16703 + retval = -ENOMEM;
16704 + printk("Error creating dir\n");
16705 + goto done;
16706 + }
16707 + pdir->owner = THIS_MODULE;
16708 +
16709 + pfile = create_proc_read_entry("isoc_data",
16710 + 0444, pdir,
16711 + isoc_read_data,
16712 + NULL);
16713 + if (pfile == NULL) {
16714 + retval = -ENOMEM;
16715 + printk("Error creating file\n");
16716 + goto no_file;
16717 + }
16718 + pfile->owner = THIS_MODULE;
16719 +
16720 + return usb_gadget_register_driver (&zero_driver);
16721 +
16722 + no_file:
16723 + remove_proc_entry("isoc_data", NULL);
16724 + done:
16725 + return retval;
16726 +}
16727 +module_init (init);
16728 +
16729 +static void __exit cleanup (void)
16730 +{
16731 +
16732 + usb_gadget_unregister_driver (&zero_driver);
16733 +
16734 + remove_proc_entry("isoc_data", pdir);
16735 + remove_proc_entry("isoc_test", NULL);
16736 +}
16737 +module_exit (cleanup);
16738 --- /dev/null
16739 +++ b/drivers/usb/host/dwc_otg/dwc_cfi_common.h
16740 @@ -0,0 +1,142 @@
16741 +/* ==========================================================================
16742 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
16743 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
16744 + * otherwise expressly agreed to in writing between Synopsys and you.
16745 + *
16746 + * The Software IS NOT an item of Licensed Software or Licensed Product under
16747 + * any End User Software License Agreement or Agreement for Licensed Product
16748 + * with Synopsys or any supplement thereto. You are permitted to use and
16749 + * redistribute this Software in source and binary forms, with or without
16750 + * modification, provided that redistributions of source code must retain this
16751 + * notice. You may not view, use, disclose, copy or distribute this file or
16752 + * any information contained herein except pursuant to this license grant from
16753 + * Synopsys. If you do not agree with this notice, including the disclaimer
16754 + * below, then you are not authorized to use the Software.
16755 + *
16756 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
16757 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16758 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16759 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
16760 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16761 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16762 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16763 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16764 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
16765 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
16766 + * DAMAGE.
16767 + * ========================================================================== */
16768 +
16769 +#if !defined(__DWC_CFI_COMMON_H__)
16770 +#define __DWC_CFI_COMMON_H__
16771 +
16772 +//#include <linux/types.h>
16773 +
16774 +/**
16775 + * @file
16776 + *
16777 + * This file contains the CFI specific common constants, interfaces
16778 + * (functions and macros) and structures for Linux. No PCD specific
16779 + * data structure or definition is to be included in this file.
16780 + *
16781 + */
16782 +
16783 +/** This is a request for all Core Features */
16784 +#define VEN_CORE_GET_FEATURES 0xB1
16785 +
16786 +/** This is a request to get the value of a specific Core Feature */
16787 +#define VEN_CORE_GET_FEATURE 0xB2
16788 +
16789 +/** This command allows the host to set the value of a specific Core Feature */
16790 +#define VEN_CORE_SET_FEATURE 0xB3
16791 +
16792 +/** This command allows the host to set the default values of
16793 + * either all or any specific Core Feature
16794 + */
16795 +#define VEN_CORE_RESET_FEATURES 0xB4
16796 +
16797 +/** This command forces the PCD to write the deferred values of a Core Features */
16798 +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
16799 +
16800 +/** This request reads a DWORD value from a register at the specified offset */
16801 +#define VEN_CORE_READ_REGISTER 0xB6
16802 +
16803 +/** This request writes a DWORD value into a register at the specified offset */
16804 +#define VEN_CORE_WRITE_REGISTER 0xB7
16805 +
16806 +/** This structure is the header of the Core Features dataset returned to
16807 + * the Host
16808 + */
16809 +struct cfi_all_features_header {
16810 +/** The features header structure length is */
16811 +#define CFI_ALL_FEATURES_HDR_LEN 8
16812 + /**
16813 + * The total length of the features dataset returned to the Host
16814 + */
16815 + uint16_t wTotalLen;
16816 +
16817 + /**
16818 + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
16819 + * This field identifies the version of the CFI Specification with which
16820 + * the device is compliant.
16821 + */
16822 + uint16_t wVersion;
16823 +
16824 + /** The ID of the Core */
16825 + uint16_t wCoreID;
16826 +#define CFI_CORE_ID_UDC 1
16827 +#define CFI_CORE_ID_OTG 2
16828 +#define CFI_CORE_ID_WUDEV 3
16829 +
16830 + /** Number of features returned by VEN_CORE_GET_FEATURES request */
16831 + uint16_t wNumFeatures;
16832 +} UPACKED;
16833 +
16834 +typedef struct cfi_all_features_header cfi_all_features_header_t;
16835 +
16836 +/** This structure is a header of the Core Feature descriptor dataset returned to
16837 + * the Host after the VEN_CORE_GET_FEATURES request
16838 + */
16839 +struct cfi_feature_desc_header {
16840 +#define CFI_FEATURE_DESC_HDR_LEN 8
16841 +
16842 + /** The feature ID */
16843 + uint16_t wFeatureID;
16844 +
16845 + /** Length of this feature descriptor in bytes - including the
16846 + * length of the feature name string
16847 + */
16848 + uint16_t wLength;
16849 +
16850 + /** The data length of this feature in bytes */
16851 + uint16_t wDataLength;
16852 +
16853 + /**
16854 + * Attributes of this features
16855 + * D0: Access rights
16856 + * 0 - Read/Write
16857 + * 1 - Read only
16858 + */
16859 + uint8_t bmAttributes;
16860 +#define CFI_FEATURE_ATTR_RO 1
16861 +#define CFI_FEATURE_ATTR_RW 0
16862 +
16863 + /** Length of the feature name in bytes */
16864 + uint8_t bNameLen;
16865 +
16866 + /** The feature name buffer */
16867 + //uint8_t *name;
16868 +} UPACKED;
16869 +
16870 +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
16871 +
16872 +/**
16873 + * This structure describes a NULL terminated string referenced by its id field.
16874 + * It is very similar to usb_string structure but has the id field type set to 16-bit.
16875 + */
16876 +struct cfi_string {
16877 + uint16_t id;
16878 + const uint8_t *s;
16879 +};
16880 +typedef struct cfi_string cfi_string_t;
16881 +
16882 +#endif
16883 --- /dev/null
16884 +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.c
16885 @@ -0,0 +1,854 @@
16886 +/* ==========================================================================
16887 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
16888 + * $Revision: #12 $
16889 + * $Date: 2011/10/26 $
16890 + * $Change: 1873028 $
16891 + *
16892 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
16893 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
16894 + * otherwise expressly agreed to in writing between Synopsys and you.
16895 + *
16896 + * The Software IS NOT an item of Licensed Software or Licensed Product under
16897 + * any End User Software License Agreement or Agreement for Licensed Product
16898 + * with Synopsys or any supplement thereto. You are permitted to use and
16899 + * redistribute this Software in source and binary forms, with or without
16900 + * modification, provided that redistributions of source code must retain this
16901 + * notice. You may not view, use, disclose, copy or distribute this file or
16902 + * any information contained herein except pursuant to this license grant from
16903 + * Synopsys. If you do not agree with this notice, including the disclaimer
16904 + * below, then you are not authorized to use the Software.
16905 + *
16906 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
16907 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16908 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16909 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
16910 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16911 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16912 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16913 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16914 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
16915 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
16916 + * DAMAGE.
16917 + * ========================================================================== */
16918 +
16919 +#include "dwc_os.h"
16920 +#include "dwc_otg_regs.h"
16921 +#include "dwc_otg_cil.h"
16922 +#include "dwc_otg_adp.h"
16923 +
16924 +/** @file
16925 + *
16926 + * This file contains the most of the Attach Detect Protocol implementation for
16927 + * the driver to support OTG Rev2.0.
16928 + *
16929 + */
16930 +
16931 +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
16932 +{
16933 + adpctl_data_t adpctl;
16934 +
16935 + adpctl.d32 = value;
16936 + adpctl.b.ar = 0x2;
16937 +
16938 + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
16939 +
16940 + while (adpctl.b.ar) {
16941 + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
16942 + }
16943 +
16944 +}
16945 +
16946 +/**
16947 + * Function is called to read ADP registers
16948 + */
16949 +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
16950 +{
16951 + adpctl_data_t adpctl;
16952 +
16953 + adpctl.d32 = 0;
16954 + adpctl.b.ar = 0x1;
16955 +
16956 + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
16957 +
16958 + while (adpctl.b.ar) {
16959 + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
16960 + }
16961 +
16962 + return adpctl.d32;
16963 +}
16964 +
16965 +/**
16966 + * Function is called to read ADPCTL register and filter Write-clear bits
16967 + */
16968 +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
16969 +{
16970 + adpctl_data_t adpctl;
16971 +
16972 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
16973 + adpctl.b.adp_tmout_int = 0;
16974 + adpctl.b.adp_prb_int = 0;
16975 + adpctl.b.adp_tmout_int = 0;
16976 +
16977 + return adpctl.d32;
16978 +}
16979 +
16980 +/**
16981 + * Function is called to write ADP registers
16982 + */
16983 +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
16984 + uint32_t set)
16985 +{
16986 + dwc_otg_adp_write_reg(core_if,
16987 + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
16988 +}
16989 +
16990 +static void adp_sense_timeout(void *ptr)
16991 +{
16992 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
16993 + core_if->adp.sense_timer_started = 0;
16994 + DWC_PRINTF("ADP SENSE TIMEOUT\n");
16995 + if (core_if->adp_enable) {
16996 + dwc_otg_adp_sense_stop(core_if);
16997 + dwc_otg_adp_probe_start(core_if);
16998 + }
16999 +}
17000 +
17001 +/**
17002 + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
17003 + */
17004 +static void adp_vbuson_timeout(void *ptr)
17005 +{
17006 + gpwrdn_data_t gpwrdn;
17007 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
17008 + hprt0_data_t hprt0 = {.d32 = 0 };
17009 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
17010 + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
17011 + if (core_if) {
17012 + core_if->adp.vbuson_timer_started = 0;
17013 + /* Turn off vbus */
17014 + hprt0.b.prtpwr = 1;
17015 + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
17016 + gpwrdn.d32 = 0;
17017 +
17018 + /* Power off the core */
17019 + if (core_if->power_down == 2) {
17020 + /* Enable Wakeup Logic */
17021 +// gpwrdn.b.wkupactiv = 1;
17022 + gpwrdn.b.pmuactv = 0;
17023 + gpwrdn.b.pwrdnrstn = 1;
17024 + gpwrdn.b.pwrdnclmp = 1;
17025 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17026 + gpwrdn.d32);
17027 +
17028 + /* Suspend the Phy Clock */
17029 + pcgcctl.b.stoppclk = 1;
17030 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
17031 +
17032 + /* Switch on VDD */
17033 +// gpwrdn.b.wkupactiv = 1;
17034 + gpwrdn.b.pmuactv = 1;
17035 + gpwrdn.b.pwrdnrstn = 1;
17036 + gpwrdn.b.pwrdnclmp = 1;
17037 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17038 + gpwrdn.d32);
17039 + } else {
17040 + /* Enable Power Down Logic */
17041 + gpwrdn.b.pmuintsel = 1;
17042 + gpwrdn.b.pmuactv = 1;
17043 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17044 + }
17045 +
17046 + /* Power off the core */
17047 + if (core_if->power_down == 2) {
17048 + gpwrdn.d32 = 0;
17049 + gpwrdn.b.pwrdnswtch = 1;
17050 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
17051 + gpwrdn.d32, 0);
17052 + }
17053 +
17054 + /* Unmask SRP detected interrupt from Power Down Logic */
17055 + gpwrdn.d32 = 0;
17056 + gpwrdn.b.srp_det_msk = 1;
17057 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17058 +
17059 + dwc_otg_adp_probe_start(core_if);
17060 + dwc_otg_dump_global_registers(core_if);
17061 + dwc_otg_dump_host_registers(core_if);
17062 + }
17063 +
17064 +}
17065 +
17066 +/**
17067 + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
17068 + * not asserted within 1.1 seconds.
17069 + *
17070 + * @param core_if the pointer to core_if strucure.
17071 + */
17072 +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
17073 +{
17074 + core_if->adp.vbuson_timer_started = 1;
17075 + if (core_if->adp.vbuson_timer)
17076 + {
17077 + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
17078 + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
17079 + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
17080 + } else {
17081 + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
17082 + }
17083 +}
17084 +
17085 +#if 0
17086 +/**
17087 + * Masks all DWC OTG core interrupts
17088 + *
17089 + */
17090 +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
17091 +{
17092 + int i;
17093 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
17094 +
17095 + /* Mask Host Interrupts */
17096 +
17097 + /* Clear and disable HCINTs */
17098 + for (i = 0; i < core_if->core_params->host_channels; i++) {
17099 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
17100 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
17101 +
17102 + }
17103 +
17104 + /* Clear and disable HAINT */
17105 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
17106 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
17107 +
17108 + /* Mask Device Interrupts */
17109 + if (!core_if->multiproc_int_enable) {
17110 + /* Clear and disable IN Endpoint interrupts */
17111 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
17112 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
17113 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
17114 + diepint, 0xFFFFFFFF);
17115 + }
17116 +
17117 + /* Clear and disable OUT Endpoint interrupts */
17118 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
17119 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
17120 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
17121 + doepint, 0xFFFFFFFF);
17122 + }
17123 +
17124 + /* Clear and disable DAINT */
17125 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
17126 + 0xFFFFFFFF);
17127 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
17128 + } else {
17129 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
17130 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
17131 + diepeachintmsk[i], 0);
17132 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
17133 + diepint, 0xFFFFFFFF);
17134 + }
17135 +
17136 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
17137 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
17138 + doepeachintmsk[i], 0);
17139 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
17140 + doepint, 0xFFFFFFFF);
17141 + }
17142 +
17143 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
17144 + 0);
17145 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
17146 + 0xFFFFFFFF);
17147 +
17148 + }
17149 +
17150 + /* Disable interrupts */
17151 + ahbcfg.b.glblintrmsk = 1;
17152 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
17153 +
17154 + /* Disable all interrupts. */
17155 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
17156 +
17157 + /* Clear any pending interrupts */
17158 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17159 +
17160 + /* Clear any pending OTG Interrupts */
17161 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
17162 +}
17163 +
17164 +/**
17165 + * Unmask Port Connection Detected interrupt
17166 + *
17167 + */
17168 +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
17169 +{
17170 + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
17171 +
17172 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
17173 +}
17174 +#endif
17175 +
17176 +/**
17177 + * Starts the ADP Probing
17178 + *
17179 + * @param core_if the pointer to core_if structure.
17180 + */
17181 +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
17182 +{
17183 +
17184 + adpctl_data_t adpctl = {.d32 = 0};
17185 + gpwrdn_data_t gpwrdn;
17186 +#if 0
17187 + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
17188 + .b.adp_sns_int = 1, b.adp_tmout_int};
17189 +#endif
17190 + dwc_otg_disable_global_interrupts(core_if);
17191 + DWC_PRINTF("ADP Probe Start\n");
17192 + core_if->adp.probe_enabled = 1;
17193 +
17194 + adpctl.b.adpres = 1;
17195 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17196 +
17197 + while (adpctl.b.adpres) {
17198 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17199 + }
17200 +
17201 + adpctl.d32 = 0;
17202 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17203 +
17204 + /* In Host mode unmask SRP detected interrupt */
17205 + gpwrdn.d32 = 0;
17206 + gpwrdn.b.sts_chngint_msk = 1;
17207 + if (!gpwrdn.b.idsts) {
17208 + gpwrdn.b.srp_det_msk = 1;
17209 + }
17210 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17211 +
17212 + adpctl.b.adp_tmout_int_msk = 1;
17213 + adpctl.b.adp_prb_int_msk = 1;
17214 + adpctl.b.prb_dschg = 1;
17215 + adpctl.b.prb_delta = 1;
17216 + adpctl.b.prb_per = 1;
17217 + adpctl.b.adpen = 1;
17218 + adpctl.b.enaprb = 1;
17219 +
17220 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17221 + DWC_PRINTF("ADP Probe Finish\n");
17222 + return 0;
17223 +}
17224 +
17225 +/**
17226 + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
17227 + * within 3 seconds.
17228 + *
17229 + * @param core_if the pointer to core_if strucure.
17230 + */
17231 +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
17232 +{
17233 + core_if->adp.sense_timer_started = 1;
17234 + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
17235 +}
17236 +
17237 +/**
17238 + * Starts the ADP Sense
17239 + *
17240 + * @param core_if the pointer to core_if strucure.
17241 + */
17242 +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
17243 +{
17244 + adpctl_data_t adpctl;
17245 +
17246 + DWC_PRINTF("ADP Sense Start\n");
17247 +
17248 + /* Unmask ADP sense interrupt and mask all other from the core */
17249 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17250 + adpctl.b.adp_sns_int_msk = 1;
17251 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17252 + dwc_otg_disable_global_interrupts(core_if); // vahrama
17253 +
17254 + /* Set ADP reset bit*/
17255 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17256 + adpctl.b.adpres = 1;
17257 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17258 +
17259 + while (adpctl.b.adpres) {
17260 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17261 + }
17262 +
17263 + adpctl.b.adpres = 0;
17264 + adpctl.b.adpen = 1;
17265 + adpctl.b.enasns = 1;
17266 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17267 +
17268 + dwc_otg_adp_sense_timer_start(core_if);
17269 +
17270 + return 0;
17271 +}
17272 +
17273 +/**
17274 + * Stops the ADP Probing
17275 + *
17276 + * @param core_if the pointer to core_if strucure.
17277 + */
17278 +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
17279 +{
17280 +
17281 + adpctl_data_t adpctl;
17282 + DWC_PRINTF("Stop ADP probe\n");
17283 + core_if->adp.probe_enabled = 0;
17284 + core_if->adp.probe_counter = 0;
17285 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17286 +
17287 + adpctl.b.adpen = 0;
17288 + adpctl.b.adp_prb_int = 1;
17289 + adpctl.b.adp_tmout_int = 1;
17290 + adpctl.b.adp_sns_int = 1;
17291 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17292 +
17293 + return 0;
17294 +}
17295 +
17296 +/**
17297 + * Stops the ADP Sensing
17298 + *
17299 + * @param core_if the pointer to core_if strucure.
17300 + */
17301 +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
17302 +{
17303 + adpctl_data_t adpctl;
17304 +
17305 + core_if->adp.sense_enabled = 0;
17306 +
17307 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17308 + adpctl.b.enasns = 0;
17309 + adpctl.b.adp_sns_int = 1;
17310 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17311 +
17312 + return 0;
17313 +}
17314 +
17315 +/**
17316 + * Called to turn on the VBUS after initial ADP probe in host mode.
17317 + * If port power was already enabled in cil_hcd_start function then
17318 + * only schedule a timer.
17319 + *
17320 + * @param core_if the pointer to core_if structure.
17321 + */
17322 +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
17323 +{
17324 + hprt0_data_t hprt0 = {.d32 = 0 };
17325 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
17326 + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
17327 +
17328 + if (hprt0.b.prtpwr == 0) {
17329 + hprt0.b.prtpwr = 1;
17330 + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
17331 + }
17332 +
17333 + dwc_otg_adp_vbuson_timer_start(core_if);
17334 +}
17335 +
17336 +/**
17337 + * Called right after driver is loaded
17338 + * to perform initial actions for ADP
17339 + *
17340 + * @param core_if the pointer to core_if structure.
17341 + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
17342 + */
17343 +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
17344 +{
17345 + gpwrdn_data_t gpwrdn;
17346 +
17347 + DWC_PRINTF("ADP Initial Start\n");
17348 + core_if->adp.adp_started = 1;
17349 +
17350 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17351 + dwc_otg_disable_global_interrupts(core_if);
17352 + if (is_host) {
17353 + DWC_PRINTF("HOST MODE\n");
17354 + /* Enable Power Down Logic Interrupt*/
17355 + gpwrdn.d32 = 0;
17356 + gpwrdn.b.pmuintsel = 1;
17357 + gpwrdn.b.pmuactv = 1;
17358 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17359 + /* Initialize first ADP probe to obtain Ramp Time value */
17360 + core_if->adp.initial_probe = 1;
17361 + dwc_otg_adp_probe_start(core_if);
17362 + } else {
17363 + gotgctl_data_t gotgctl;
17364 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
17365 + DWC_PRINTF("DEVICE MODE\n");
17366 + if (gotgctl.b.bsesvld == 0) {
17367 + /* Enable Power Down Logic Interrupt*/
17368 + gpwrdn.d32 = 0;
17369 + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
17370 + gpwrdn.b.pmuintsel = 1;
17371 + gpwrdn.b.pmuactv = 1;
17372 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17373 + core_if->adp.initial_probe = 1;
17374 + dwc_otg_adp_probe_start(core_if);
17375 + } else {
17376 + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
17377 + core_if->op_state = B_PERIPHERAL;
17378 + dwc_otg_core_init(core_if);
17379 + dwc_otg_enable_global_interrupts(core_if);
17380 + cil_pcd_start(core_if);
17381 + dwc_otg_dump_global_registers(core_if);
17382 + dwc_otg_dump_dev_registers(core_if);
17383 + }
17384 + }
17385 +}
17386 +
17387 +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
17388 +{
17389 + core_if->adp.adp_started = 0;
17390 + core_if->adp.initial_probe = 0;
17391 + core_if->adp.probe_timer_values[0] = -1;
17392 + core_if->adp.probe_timer_values[1] = -1;
17393 + core_if->adp.probe_enabled = 0;
17394 + core_if->adp.sense_enabled = 0;
17395 + core_if->adp.sense_timer_started = 0;
17396 + core_if->adp.vbuson_timer_started = 0;
17397 + core_if->adp.probe_counter = 0;
17398 + core_if->adp.gpwrdn = 0;
17399 + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
17400 + /* Initialize timers */
17401 + core_if->adp.sense_timer =
17402 + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
17403 + core_if->adp.vbuson_timer =
17404 + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
17405 + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
17406 + {
17407 + DWC_ERROR("Could not allocate memory for ADP timers\n");
17408 + }
17409 +}
17410 +
17411 +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
17412 +{
17413 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
17414 + gpwrdn.b.pmuintsel = 1;
17415 + gpwrdn.b.pmuactv = 1;
17416 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17417 +
17418 + if (core_if->adp.probe_enabled)
17419 + dwc_otg_adp_probe_stop(core_if);
17420 + if (core_if->adp.sense_enabled)
17421 + dwc_otg_adp_sense_stop(core_if);
17422 + if (core_if->adp.sense_timer_started)
17423 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
17424 + if (core_if->adp.vbuson_timer_started)
17425 + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
17426 + DWC_TIMER_FREE(core_if->adp.sense_timer);
17427 + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
17428 +}
17429 +
17430 +/////////////////////////////////////////////////////////////////////
17431 +////////////// ADP Interrupt Handlers ///////////////////////////////
17432 +/////////////////////////////////////////////////////////////////////
17433 +/**
17434 + * This function sets Ramp Timer values
17435 + */
17436 +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
17437 +{
17438 + if (core_if->adp.probe_timer_values[0] == -1) {
17439 + core_if->adp.probe_timer_values[0] = val;
17440 + core_if->adp.probe_timer_values[1] = -1;
17441 + return 1;
17442 + } else {
17443 + core_if->adp.probe_timer_values[1] =
17444 + core_if->adp.probe_timer_values[0];
17445 + core_if->adp.probe_timer_values[0] = val;
17446 + return 0;
17447 + }
17448 +}
17449 +
17450 +/**
17451 + * This function compares Ramp Timer values
17452 + */
17453 +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
17454 +{
17455 + uint32_t diff;
17456 + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
17457 + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
17458 + else
17459 + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
17460 + if(diff < 2) {
17461 + return 0;
17462 + } else {
17463 + return 1;
17464 + }
17465 +}
17466 +
17467 +/**
17468 + * This function handles ADP Probe Interrupts
17469 + */
17470 +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
17471 + uint32_t val)
17472 +{
17473 + adpctl_data_t adpctl = {.d32 = 0 };
17474 + gpwrdn_data_t gpwrdn, temp;
17475 + adpctl.d32 = val;
17476 +
17477 + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17478 + core_if->adp.probe_counter++;
17479 + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17480 + if (adpctl.b.rtim == 0 && !temp.b.idsts){
17481 + DWC_PRINTF("RTIM value is 0\n");
17482 + goto exit;
17483 + }
17484 + if (set_timer_value(core_if, adpctl.b.rtim) &&
17485 + core_if->adp.initial_probe) {
17486 + core_if->adp.initial_probe = 0;
17487 + dwc_otg_adp_probe_stop(core_if);
17488 + gpwrdn.d32 = 0;
17489 + gpwrdn.b.pmuactv = 1;
17490 + gpwrdn.b.pmuintsel = 1;
17491 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17492 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17493 +
17494 + /* check which value is for device mode and which for Host mode */
17495 + if (!temp.b.idsts) { /* considered host mode value is 0 */
17496 + /*
17497 + * Turn on VBUS after initial ADP probe.
17498 + */
17499 + core_if->op_state = A_HOST;
17500 + dwc_otg_enable_global_interrupts(core_if);
17501 + DWC_SPINUNLOCK(core_if->lock);
17502 + cil_hcd_start(core_if);
17503 + dwc_otg_adp_turnon_vbus(core_if);
17504 + DWC_SPINLOCK(core_if->lock);
17505 + } else {
17506 + /*
17507 + * Initiate SRP after initial ADP probe.
17508 + */
17509 + dwc_otg_enable_global_interrupts(core_if);
17510 + dwc_otg_initiate_srp(core_if);
17511 + }
17512 + } else if (core_if->adp.probe_counter > 2){
17513 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17514 + if (compare_timer_values(core_if)) {
17515 + DWC_PRINTF("Difference in timer values !!! \n");
17516 +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
17517 + dwc_otg_adp_probe_stop(core_if);
17518 +
17519 + /* Power on the core */
17520 + if (core_if->power_down == 2) {
17521 + gpwrdn.b.pwrdnswtch = 1;
17522 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17523 + gpwrdn, 0, gpwrdn.d32);
17524 + }
17525 +
17526 + /* check which value is for device mode and which for Host mode */
17527 + if (!temp.b.idsts) { /* considered host mode value is 0 */
17528 + /* Disable Interrupt from Power Down Logic */
17529 + gpwrdn.d32 = 0;
17530 + gpwrdn.b.pmuintsel = 1;
17531 + gpwrdn.b.pmuactv = 1;
17532 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17533 + gpwrdn, gpwrdn.d32, 0);
17534 +
17535 + /*
17536 + * Initialize the Core for Host mode.
17537 + */
17538 + core_if->op_state = A_HOST;
17539 + dwc_otg_core_init(core_if);
17540 + dwc_otg_enable_global_interrupts(core_if);
17541 + cil_hcd_start(core_if);
17542 + } else {
17543 + gotgctl_data_t gotgctl;
17544 + /* Mask SRP detected interrupt from Power Down Logic */
17545 + gpwrdn.d32 = 0;
17546 + gpwrdn.b.srp_det_msk = 1;
17547 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17548 + gpwrdn, gpwrdn.d32, 0);
17549 +
17550 + /* Disable Power Down Logic */
17551 + gpwrdn.d32 = 0;
17552 + gpwrdn.b.pmuintsel = 1;
17553 + gpwrdn.b.pmuactv = 1;
17554 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17555 + gpwrdn, gpwrdn.d32, 0);
17556 +
17557 + /*
17558 + * Initialize the Core for Device mode.
17559 + */
17560 + core_if->op_state = B_PERIPHERAL;
17561 + dwc_otg_core_init(core_if);
17562 + dwc_otg_enable_global_interrupts(core_if);
17563 + cil_pcd_start(core_if);
17564 +
17565 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
17566 + if (!gotgctl.b.bsesvld) {
17567 + dwc_otg_initiate_srp(core_if);
17568 + }
17569 + }
17570 + }
17571 + if (core_if->power_down == 2) {
17572 + if (gpwrdn.b.bsessvld) {
17573 + /* Mask SRP detected interrupt from Power Down Logic */
17574 + gpwrdn.d32 = 0;
17575 + gpwrdn.b.srp_det_msk = 1;
17576 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17577 +
17578 + /* Disable Power Down Logic */
17579 + gpwrdn.d32 = 0;
17580 + gpwrdn.b.pmuactv = 1;
17581 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17582 +
17583 + /*
17584 + * Initialize the Core for Device mode.
17585 + */
17586 + core_if->op_state = B_PERIPHERAL;
17587 + dwc_otg_core_init(core_if);
17588 + dwc_otg_enable_global_interrupts(core_if);
17589 + cil_pcd_start(core_if);
17590 + }
17591 + }
17592 + }
17593 +exit:
17594 + /* Clear interrupt */
17595 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17596 + adpctl.b.adp_prb_int = 1;
17597 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17598 +
17599 + return 0;
17600 +}
17601 +
17602 +/**
17603 + * This function hadles ADP Sense Interrupt
17604 + */
17605 +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
17606 +{
17607 + adpctl_data_t adpctl;
17608 + /* Stop ADP Sense timer */
17609 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
17610 +
17611 + /* Restart ADP Sense timer */
17612 + dwc_otg_adp_sense_timer_start(core_if);
17613 +
17614 + /* Clear interrupt */
17615 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17616 + adpctl.b.adp_sns_int = 1;
17617 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17618 +
17619 + return 0;
17620 +}
17621 +
17622 +/**
17623 + * This function handles ADP Probe Interrupts
17624 + */
17625 +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
17626 + uint32_t val)
17627 +{
17628 + adpctl_data_t adpctl = {.d32 = 0 };
17629 + adpctl.d32 = val;
17630 + set_timer_value(core_if, adpctl.b.rtim);
17631 +
17632 + /* Clear interrupt */
17633 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17634 + adpctl.b.adp_tmout_int = 1;
17635 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17636 +
17637 + return 0;
17638 +}
17639 +
17640 +/**
17641 + * ADP Interrupt handler.
17642 + *
17643 + */
17644 +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
17645 +{
17646 + int retval = 0;
17647 + adpctl_data_t adpctl = {.d32 = 0};
17648 +
17649 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17650 + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
17651 +
17652 + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
17653 + DWC_PRINTF("ADP Sense interrupt\n");
17654 + retval |= dwc_otg_adp_handle_sns_intr(core_if);
17655 + }
17656 + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
17657 + DWC_PRINTF("ADP timeout interrupt\n");
17658 + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
17659 + }
17660 + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
17661 + DWC_PRINTF("ADP Probe interrupt\n");
17662 + adpctl.b.adp_prb_int = 1;
17663 + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
17664 + }
17665 +
17666 +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
17667 + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
17668 + DWC_PRINTF("RETURN FROM ADP ISR\n");
17669 +
17670 + return retval;
17671 +}
17672 +
17673 +/**
17674 + *
17675 + * @param core_if Programming view of DWC_otg controller.
17676 + */
17677 +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
17678 +{
17679 +
17680 +#ifndef DWC_HOST_ONLY
17681 + hprt0_data_t hprt0;
17682 + gpwrdn_data_t gpwrdn;
17683 + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
17684 +
17685 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17686 + /* check which value is for device mode and which for Host mode */
17687 + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
17688 + DWC_PRINTF("SRP: Host mode\n");
17689 +
17690 + if (core_if->adp_enable) {
17691 + dwc_otg_adp_probe_stop(core_if);
17692 +
17693 + /* Power on the core */
17694 + if (core_if->power_down == 2) {
17695 + gpwrdn.b.pwrdnswtch = 1;
17696 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17697 + gpwrdn, 0, gpwrdn.d32);
17698 + }
17699 +
17700 + core_if->op_state = A_HOST;
17701 + dwc_otg_core_init(core_if);
17702 + dwc_otg_enable_global_interrupts(core_if);
17703 + cil_hcd_start(core_if);
17704 + }
17705 +
17706 + /* Turn on the port power bit. */
17707 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
17708 + hprt0.b.prtpwr = 1;
17709 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
17710 +
17711 + /* Start the Connection timer. So a message can be displayed
17712 + * if connect does not occur within 10 seconds. */
17713 + cil_hcd_session_start(core_if);
17714 + } else {
17715 + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
17716 + if (core_if->adp_enable) {
17717 + dwc_otg_adp_probe_stop(core_if);
17718 +
17719 + /* Power on the core */
17720 + if (core_if->power_down == 2) {
17721 + gpwrdn.b.pwrdnswtch = 1;
17722 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17723 + gpwrdn, 0, gpwrdn.d32);
17724 + }
17725 +
17726 + gpwrdn.d32 = 0;
17727 + gpwrdn.b.pmuactv = 0;
17728 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17729 + gpwrdn.d32);
17730 +
17731 + core_if->op_state = B_PERIPHERAL;
17732 + dwc_otg_core_init(core_if);
17733 + dwc_otg_enable_global_interrupts(core_if);
17734 + cil_pcd_start(core_if);
17735 + }
17736 + }
17737 +#endif
17738 + return 1;
17739 +}
17740 --- /dev/null
17741 +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.h
17742 @@ -0,0 +1,80 @@
17743 +/* ==========================================================================
17744 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
17745 + * $Revision: #7 $
17746 + * $Date: 2011/10/24 $
17747 + * $Change: 1871159 $
17748 + *
17749 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
17750 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
17751 + * otherwise expressly agreed to in writing between Synopsys and you.
17752 + *
17753 + * The Software IS NOT an item of Licensed Software or Licensed Product under
17754 + * any End User Software License Agreement or Agreement for Licensed Product
17755 + * with Synopsys or any supplement thereto. You are permitted to use and
17756 + * redistribute this Software in source and binary forms, with or without
17757 + * modification, provided that redistributions of source code must retain this
17758 + * notice. You may not view, use, disclose, copy or distribute this file or
17759 + * any information contained herein except pursuant to this license grant from
17760 + * Synopsys. If you do not agree with this notice, including the disclaimer
17761 + * below, then you are not authorized to use the Software.
17762 + *
17763 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
17764 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17765 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17766 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
17767 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17768 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17769 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17770 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17771 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17772 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
17773 + * DAMAGE.
17774 + * ========================================================================== */
17775 +
17776 +#ifndef __DWC_OTG_ADP_H__
17777 +#define __DWC_OTG_ADP_H__
17778 +
17779 +/**
17780 + * @file
17781 + *
17782 + * This file contains the Attach Detect Protocol interfaces and defines
17783 + * (functions) and structures for Linux.
17784 + *
17785 + */
17786 +
17787 +#define DWC_OTG_ADP_UNATTACHED 0
17788 +#define DWC_OTG_ADP_ATTACHED 1
17789 +#define DWC_OTG_ADP_UNKOWN 2
17790 +
17791 +typedef struct dwc_otg_adp {
17792 + uint32_t adp_started;
17793 + uint32_t initial_probe;
17794 + int32_t probe_timer_values[2];
17795 + uint32_t probe_enabled;
17796 + uint32_t sense_enabled;
17797 + dwc_timer_t *sense_timer;
17798 + uint32_t sense_timer_started;
17799 + dwc_timer_t *vbuson_timer;
17800 + uint32_t vbuson_timer_started;
17801 + uint32_t attached;
17802 + uint32_t probe_counter;
17803 + uint32_t gpwrdn;
17804 +} dwc_otg_adp_t;
17805 +
17806 +/**
17807 + * Attach Detect Protocol functions
17808 + */
17809 +
17810 +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
17811 +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
17812 +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
17813 +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
17814 +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
17815 +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
17816 +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
17817 +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
17818 +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
17819 +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
17820 +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
17821 +
17822 +#endif //__DWC_OTG_ADP_H__
17823 --- /dev/null
17824 +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.c
17825 @@ -0,0 +1,1212 @@
17826 +/* ==========================================================================
17827 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
17828 + * $Revision: #44 $
17829 + * $Date: 2010/11/29 $
17830 + * $Change: 1636033 $
17831 + *
17832 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
17833 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
17834 + * otherwise expressly agreed to in writing between Synopsys and you.
17835 + *
17836 + * The Software IS NOT an item of Licensed Software or Licensed Product under
17837 + * any End User Software License Agreement or Agreement for Licensed Product
17838 + * with Synopsys or any supplement thereto. You are permitted to use and
17839 + * redistribute this Software in source and binary forms, with or without
17840 + * modification, provided that redistributions of source code must retain this
17841 + * notice. You may not view, use, disclose, copy or distribute this file or
17842 + * any information contained herein except pursuant to this license grant from
17843 + * Synopsys. If you do not agree with this notice, including the disclaimer
17844 + * below, then you are not authorized to use the Software.
17845 + *
17846 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
17847 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17848 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17849 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
17850 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17851 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17852 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17853 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17854 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17855 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
17856 + * DAMAGE.
17857 + * ========================================================================== */
17858 +
17859 +/** @file
17860 + *
17861 + * The diagnostic interface will provide access to the controller for
17862 + * bringing up the hardware and testing. The Linux driver attributes
17863 + * feature will be used to provide the Linux Diagnostic
17864 + * Interface. These attributes are accessed through sysfs.
17865 + */
17866 +
17867 +/** @page "Linux Module Attributes"
17868 + *
17869 + * The Linux module attributes feature is used to provide the Linux
17870 + * Diagnostic Interface. These attributes are accessed through sysfs.
17871 + * The diagnostic interface will provide access to the controller for
17872 + * bringing up the hardware and testing.
17873 +
17874 + The following table shows the attributes.
17875 + <table>
17876 + <tr>
17877 + <td><b> Name</b></td>
17878 + <td><b> Description</b></td>
17879 + <td><b> Access</b></td>
17880 + </tr>
17881 +
17882 + <tr>
17883 + <td> mode </td>
17884 + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
17885 + <td> Read</td>
17886 + </tr>
17887 +
17888 + <tr>
17889 + <td> hnpcapable </td>
17890 + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
17891 + Read returns the current value.</td>
17892 + <td> Read/Write</td>
17893 + </tr>
17894 +
17895 + <tr>
17896 + <td> srpcapable </td>
17897 + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
17898 + Read returns the current value.</td>
17899 + <td> Read/Write</td>
17900 + </tr>
17901 +
17902 + <tr>
17903 + <td> hsic_connect </td>
17904 + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
17905 + Read returns the current value.</td>
17906 + <td> Read/Write</td>
17907 + </tr>
17908 +
17909 + <tr>
17910 + <td> inv_sel_hsic </td>
17911 + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
17912 + Read returns the current value.</td>
17913 + <td> Read/Write</td>
17914 + </tr>
17915 +
17916 + <tr>
17917 + <td> hnp </td>
17918 + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
17919 + <td> Read/Write</td>
17920 + </tr>
17921 +
17922 + <tr>
17923 + <td> srp </td>
17924 + <td> Initiates the Session Request Protocol. Read returns the status.</td>
17925 + <td> Read/Write</td>
17926 + </tr>
17927 +
17928 + <tr>
17929 + <td> buspower </td>
17930 + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
17931 + <td> Read/Write</td>
17932 + </tr>
17933 +
17934 + <tr>
17935 + <td> bussuspend </td>
17936 + <td> Suspends the USB bus.</td>
17937 + <td> Read/Write</td>
17938 + </tr>
17939 +
17940 + <tr>
17941 + <td> busconnected </td>
17942 + <td> Gets the connection status of the bus</td>
17943 + <td> Read</td>
17944 + </tr>
17945 +
17946 + <tr>
17947 + <td> gotgctl </td>
17948 + <td> Gets or sets the Core Control Status Register.</td>
17949 + <td> Read/Write</td>
17950 + </tr>
17951 +
17952 + <tr>
17953 + <td> gusbcfg </td>
17954 + <td> Gets or sets the Core USB Configuration Register</td>
17955 + <td> Read/Write</td>
17956 + </tr>
17957 +
17958 + <tr>
17959 + <td> grxfsiz </td>
17960 + <td> Gets or sets the Receive FIFO Size Register</td>
17961 + <td> Read/Write</td>
17962 + </tr>
17963 +
17964 + <tr>
17965 + <td> gnptxfsiz </td>
17966 + <td> Gets or sets the non-periodic Transmit Size Register</td>
17967 + <td> Read/Write</td>
17968 + </tr>
17969 +
17970 + <tr>
17971 + <td> gpvndctl </td>
17972 + <td> Gets or sets the PHY Vendor Control Register</td>
17973 + <td> Read/Write</td>
17974 + </tr>
17975 +
17976 + <tr>
17977 + <td> ggpio </td>
17978 + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
17979 + or sets the upper 16 bits.</td>
17980 + <td> Read/Write</td>
17981 + </tr>
17982 +
17983 + <tr>
17984 + <td> guid </td>
17985 + <td> Gets or sets the value of the User ID Register</td>
17986 + <td> Read/Write</td>
17987 + </tr>
17988 +
17989 + <tr>
17990 + <td> gsnpsid </td>
17991 + <td> Gets the value of the Synopsys ID Regester</td>
17992 + <td> Read</td>
17993 + </tr>
17994 +
17995 + <tr>
17996 + <td> devspeed </td>
17997 + <td> Gets or sets the device speed setting in the DCFG register</td>
17998 + <td> Read/Write</td>
17999 + </tr>
18000 +
18001 + <tr>
18002 + <td> enumspeed </td>
18003 + <td> Gets the device enumeration Speed.</td>
18004 + <td> Read</td>
18005 + </tr>
18006 +
18007 + <tr>
18008 + <td> hptxfsiz </td>
18009 + <td> Gets the value of the Host Periodic Transmit FIFO</td>
18010 + <td> Read</td>
18011 + </tr>
18012 +
18013 + <tr>
18014 + <td> hprt0 </td>
18015 + <td> Gets or sets the value in the Host Port Control and Status Register</td>
18016 + <td> Read/Write</td>
18017 + </tr>
18018 +
18019 + <tr>
18020 + <td> regoffset </td>
18021 + <td> Sets the register offset for the next Register Access</td>
18022 + <td> Read/Write</td>
18023 + </tr>
18024 +
18025 + <tr>
18026 + <td> regvalue </td>
18027 + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
18028 + <td> Read/Write</td>
18029 + </tr>
18030 +
18031 + <tr>
18032 + <td> remote_wakeup </td>
18033 + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
18034 + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
18035 + Wakeup signalling bit in the Device Control Register is set for 1
18036 + milli-second.</td>
18037 + <td> Read/Write</td>
18038 + </tr>
18039 +
18040 + <tr>
18041 + <td> rem_wakeup_pwrdn </td>
18042 + <td> On read, shows the status core - hibernated or not. On write, initiates
18043 + a remote wakeup of the device from Hibernation. </td>
18044 + <td> Read/Write</td>
18045 + </tr>
18046 +
18047 + <tr>
18048 + <td> mode_ch_tim_en </td>
18049 + <td> This bit is used to enable or disable the host core to wait for 200 PHY
18050 + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
18051 + after Suspend or LPM. </td>
18052 + <td> Read/Write</td>
18053 + </tr>
18054 +
18055 + <tr>
18056 + <td> fr_interval </td>
18057 + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
18058 + reload HFIR register during runtime. The application can write a value to this
18059 + register only after the Port Enable bit of the Host Port Control and Status
18060 + register (HPRT.PrtEnaPort) has been set </td>
18061 + <td> Read/Write</td>
18062 + </tr>
18063 +
18064 + <tr>
18065 + <td> disconnect_us </td>
18066 + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
18067 + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
18068 + <td> Read/Write</td>
18069 + </tr>
18070 +
18071 + <tr>
18072 + <td> regdump </td>
18073 + <td> Dumps the contents of core registers.</td>
18074 + <td> Read</td>
18075 + </tr>
18076 +
18077 + <tr>
18078 + <td> spramdump </td>
18079 + <td> Dumps the contents of core registers.</td>
18080 + <td> Read</td>
18081 + </tr>
18082 +
18083 + <tr>
18084 + <td> hcddump </td>
18085 + <td> Dumps the current HCD state.</td>
18086 + <td> Read</td>
18087 + </tr>
18088 +
18089 + <tr>
18090 + <td> hcd_frrem </td>
18091 + <td> Shows the average value of the Frame Remaining
18092 + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
18093 + occurs. This can be used to determine the average interrupt latency. Also
18094 + shows the average Frame Remaining value for start_transfer and the "a" and
18095 + "b" sample points. The "a" and "b" sample points may be used during debugging
18096 + bto determine how long it takes to execute a section of the HCD code.</td>
18097 + <td> Read</td>
18098 + </tr>
18099 +
18100 + <tr>
18101 + <td> rd_reg_test </td>
18102 + <td> Displays the time required to read the GNPTXFSIZ register many times
18103 + (the output shows the number of times the register is read).
18104 + <td> Read</td>
18105 + </tr>
18106 +
18107 + <tr>
18108 + <td> wr_reg_test </td>
18109 + <td> Displays the time required to write the GNPTXFSIZ register many times
18110 + (the output shows the number of times the register is written).
18111 + <td> Read</td>
18112 + </tr>
18113 +
18114 + <tr>
18115 + <td> lpm_response </td>
18116 + <td> Gets or sets lpm_response mode. Applicable only in device mode.
18117 + <td> Write</td>
18118 + </tr>
18119 +
18120 + <tr>
18121 + <td> sleep_status </td>
18122 + <td> Shows sleep status of device.
18123 + <td> Read</td>
18124 + </tr>
18125 +
18126 + </table>
18127 +
18128 + Example usage:
18129 + To get the current mode:
18130 + cat /sys/devices/lm0/mode
18131 +
18132 + To power down the USB:
18133 + echo 0 > /sys/devices/lm0/buspower
18134 + */
18135 +
18136 +#include "dwc_otg_os_dep.h"
18137 +#include "dwc_os.h"
18138 +#include "dwc_otg_driver.h"
18139 +#include "dwc_otg_attr.h"
18140 +#include "dwc_otg_core_if.h"
18141 +#include "dwc_otg_pcd_if.h"
18142 +#include "dwc_otg_hcd_if.h"
18143 +
18144 +/*
18145 + * MACROs for defining sysfs attribute
18146 + */
18147 +#ifdef LM_INTERFACE
18148 +
18149 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18150 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18151 +{ \
18152 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18153 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18154 + uint32_t val; \
18155 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18156 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18157 +}
18158 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18159 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18160 + const char *buf, size_t count) \
18161 +{ \
18162 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18163 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18164 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18165 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18166 + return count; \
18167 +}
18168 +
18169 +#elif defined(PCI_INTERFACE)
18170 +
18171 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18172 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18173 +{ \
18174 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18175 + uint32_t val; \
18176 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18177 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18178 +}
18179 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18180 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18181 + const char *buf, size_t count) \
18182 +{ \
18183 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18184 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18185 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18186 + return count; \
18187 +}
18188 +
18189 +#elif defined(PLATFORM_INTERFACE)
18190 +
18191 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18192 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18193 +{ \
18194 + struct platform_device *platform_dev = \
18195 + container_of(_dev, struct platform_device, dev); \
18196 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18197 + uint32_t val; \
18198 + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
18199 + __func__, _dev, platform_dev, otg_dev); \
18200 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18201 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18202 +}
18203 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18204 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18205 + const char *buf, size_t count) \
18206 +{ \
18207 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18208 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18209 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18210 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18211 + return count; \
18212 +}
18213 +#endif
18214 +
18215 +/*
18216 + * MACROs for defining sysfs attribute for 32-bit registers
18217 + */
18218 +#ifdef LM_INTERFACE
18219 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18220 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18221 +{ \
18222 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18223 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18224 + uint32_t val; \
18225 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18226 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18227 +}
18228 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18229 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18230 + const char *buf, size_t count) \
18231 +{ \
18232 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18233 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18234 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18235 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18236 + return count; \
18237 +}
18238 +#elif defined(PCI_INTERFACE)
18239 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18240 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18241 +{ \
18242 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18243 + uint32_t val; \
18244 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18245 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18246 +}
18247 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18248 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18249 + const char *buf, size_t count) \
18250 +{ \
18251 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18252 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18253 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18254 + return count; \
18255 +}
18256 +
18257 +#elif defined(PLATFORM_INTERFACE)
18258 +#include "dwc_otg_dbg.h"
18259 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18260 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18261 +{ \
18262 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18263 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18264 + uint32_t val; \
18265 + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
18266 + __func__, _dev, platform_dev, otg_dev); \
18267 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18268 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18269 +}
18270 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18271 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18272 + const char *buf, size_t count) \
18273 +{ \
18274 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18275 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18276 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18277 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18278 + return count; \
18279 +}
18280 +
18281 +#endif
18282 +
18283 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
18284 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18285 +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18286 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
18287 +
18288 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
18289 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18290 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
18291 +
18292 +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
18293 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18294 +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18295 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
18296 +
18297 +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
18298 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18299 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
18300 +
18301 +/** @name Functions for Show/Store of Attributes */
18302 +/**@{*/
18303 +
18304 +/**
18305 + * Helper function returning the otg_device structure of the given device
18306 + */
18307 +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
18308 +{
18309 + dwc_otg_device_t *otg_dev;
18310 + DWC_OTG_GETDRVDEV(otg_dev, _dev);
18311 + return otg_dev;
18312 +}
18313 +
18314 +/**
18315 + * Show the register offset of the Register Access.
18316 + */
18317 +static ssize_t regoffset_show(struct device *_dev,
18318 + struct device_attribute *attr, char *buf)
18319 +{
18320 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18321 + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
18322 + otg_dev->os_dep.reg_offset);
18323 +}
18324 +
18325 +/**
18326 + * Set the register offset for the next Register Access Read/Write
18327 + */
18328 +static ssize_t regoffset_store(struct device *_dev,
18329 + struct device_attribute *attr,
18330 + const char *buf, size_t count)
18331 +{
18332 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18333 + uint32_t offset = simple_strtoul(buf, NULL, 16);
18334 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
18335 + if (offset < SZ_256K) {
18336 +#elif defined(PCI_INTERFACE)
18337 + if (offset < 0x00040000) {
18338 +#endif
18339 + otg_dev->os_dep.reg_offset = offset;
18340 + } else {
18341 + dev_err(_dev, "invalid offset\n");
18342 + }
18343 +
18344 + return count;
18345 +}
18346 +
18347 +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
18348 +
18349 +/**
18350 + * Show the value of the register at the offset in the reg_offset
18351 + * attribute.
18352 + */
18353 +static ssize_t regvalue_show(struct device *_dev,
18354 + struct device_attribute *attr, char *buf)
18355 +{
18356 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18357 + uint32_t val;
18358 + volatile uint32_t *addr;
18359 +
18360 + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
18361 + /* Calculate the address */
18362 + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
18363 + (uint8_t *) otg_dev->os_dep.base);
18364 + val = DWC_READ_REG32(addr);
18365 + return snprintf(buf,
18366 + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
18367 + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
18368 + val);
18369 + } else {
18370 + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
18371 + return sprintf(buf, "invalid offset\n");
18372 + }
18373 +}
18374 +
18375 +/**
18376 + * Store the value in the register at the offset in the reg_offset
18377 + * attribute.
18378 + *
18379 + */
18380 +static ssize_t regvalue_store(struct device *_dev,
18381 + struct device_attribute *attr,
18382 + const char *buf, size_t count)
18383 +{
18384 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18385 + volatile uint32_t *addr;
18386 + uint32_t val = simple_strtoul(buf, NULL, 16);
18387 + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
18388 + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
18389 + /* Calculate the address */
18390 + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
18391 + (uint8_t *) otg_dev->os_dep.base);
18392 + DWC_WRITE_REG32(addr, val);
18393 + } else {
18394 + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
18395 + otg_dev->os_dep.reg_offset);
18396 + }
18397 + return count;
18398 +}
18399 +
18400 +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
18401 +
18402 +/*
18403 + * Attributes
18404 + */
18405 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
18406 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
18407 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
18408 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
18409 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
18410 +
18411 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
18412 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
18413 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
18414 +
18415 +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
18416 +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
18417 + &(otg_dev->core_if->core_global_regs->gusbcfg),
18418 + "GUSBCFG");
18419 +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
18420 + &(otg_dev->core_if->core_global_regs->grxfsiz),
18421 + "GRXFSIZ");
18422 +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
18423 + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
18424 + "GNPTXFSIZ");
18425 +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
18426 + &(otg_dev->core_if->core_global_regs->gpvndctl),
18427 + "GPVNDCTL");
18428 +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
18429 + &(otg_dev->core_if->core_global_regs->ggpio),
18430 + "GGPIO");
18431 +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
18432 + "GUID");
18433 +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
18434 + &(otg_dev->core_if->core_global_regs->gsnpsid),
18435 + "GSNPSID");
18436 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
18437 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
18438 +
18439 +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
18440 + &(otg_dev->core_if->core_global_regs->hptxfsiz),
18441 + "HPTXFSIZ");
18442 +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
18443 +
18444 +/**
18445 + * @todo Add code to initiate the HNP.
18446 + */
18447 +/**
18448 + * Show the HNP status bit
18449 + */
18450 +static ssize_t hnp_show(struct device *_dev,
18451 + struct device_attribute *attr, char *buf)
18452 +{
18453 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18454 + return sprintf(buf, "HstNegScs = 0x%x\n",
18455 + dwc_otg_get_hnpstatus(otg_dev->core_if));
18456 +}
18457 +
18458 +/**
18459 + * Set the HNP Request bit
18460 + */
18461 +static ssize_t hnp_store(struct device *_dev,
18462 + struct device_attribute *attr,
18463 + const char *buf, size_t count)
18464 +{
18465 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18466 + uint32_t in = simple_strtoul(buf, NULL, 16);
18467 + dwc_otg_set_hnpreq(otg_dev->core_if, in);
18468 + return count;
18469 +}
18470 +
18471 +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
18472 +
18473 +/**
18474 + * @todo Add code to initiate the SRP.
18475 + */
18476 +/**
18477 + * Show the SRP status bit
18478 + */
18479 +static ssize_t srp_show(struct device *_dev,
18480 + struct device_attribute *attr, char *buf)
18481 +{
18482 +#ifndef DWC_HOST_ONLY
18483 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18484 + return sprintf(buf, "SesReqScs = 0x%x\n",
18485 + dwc_otg_get_srpstatus(otg_dev->core_if));
18486 +#else
18487 + return sprintf(buf, "Host Only Mode!\n");
18488 +#endif
18489 +}
18490 +
18491 +/**
18492 + * Set the SRP Request bit
18493 + */
18494 +static ssize_t srp_store(struct device *_dev,
18495 + struct device_attribute *attr,
18496 + const char *buf, size_t count)
18497 +{
18498 +#ifndef DWC_HOST_ONLY
18499 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18500 + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
18501 +#endif
18502 + return count;
18503 +}
18504 +
18505 +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
18506 +
18507 +/**
18508 + * @todo Need to do more for power on/off?
18509 + */
18510 +/**
18511 + * Show the Bus Power status
18512 + */
18513 +static ssize_t buspower_show(struct device *_dev,
18514 + struct device_attribute *attr, char *buf)
18515 +{
18516 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18517 + return sprintf(buf, "Bus Power = 0x%x\n",
18518 + dwc_otg_get_prtpower(otg_dev->core_if));
18519 +}
18520 +
18521 +/**
18522 + * Set the Bus Power status
18523 + */
18524 +static ssize_t buspower_store(struct device *_dev,
18525 + struct device_attribute *attr,
18526 + const char *buf, size_t count)
18527 +{
18528 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18529 + uint32_t on = simple_strtoul(buf, NULL, 16);
18530 + dwc_otg_set_prtpower(otg_dev->core_if, on);
18531 + return count;
18532 +}
18533 +
18534 +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
18535 +
18536 +/**
18537 + * @todo Need to do more for suspend?
18538 + */
18539 +/**
18540 + * Show the Bus Suspend status
18541 + */
18542 +static ssize_t bussuspend_show(struct device *_dev,
18543 + struct device_attribute *attr, char *buf)
18544 +{
18545 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18546 + return sprintf(buf, "Bus Suspend = 0x%x\n",
18547 + dwc_otg_get_prtsuspend(otg_dev->core_if));
18548 +}
18549 +
18550 +/**
18551 + * Set the Bus Suspend status
18552 + */
18553 +static ssize_t bussuspend_store(struct device *_dev,
18554 + struct device_attribute *attr,
18555 + const char *buf, size_t count)
18556 +{
18557 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18558 + uint32_t in = simple_strtoul(buf, NULL, 16);
18559 + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
18560 + return count;
18561 +}
18562 +
18563 +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
18564 +
18565 +/**
18566 + * Show the Mode Change Ready Timer status
18567 + */
18568 +static ssize_t mode_ch_tim_en_show(struct device *_dev,
18569 + struct device_attribute *attr, char *buf)
18570 +{
18571 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18572 + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
18573 + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
18574 +}
18575 +
18576 +/**
18577 + * Set the Mode Change Ready Timer status
18578 + */
18579 +static ssize_t mode_ch_tim_en_store(struct device *_dev,
18580 + struct device_attribute *attr,
18581 + const char *buf, size_t count)
18582 +{
18583 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18584 + uint32_t in = simple_strtoul(buf, NULL, 16);
18585 + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
18586 + return count;
18587 +}
18588 +
18589 +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
18590 +
18591 +/**
18592 + * Show the value of HFIR Frame Interval bitfield
18593 + */
18594 +static ssize_t fr_interval_show(struct device *_dev,
18595 + struct device_attribute *attr, char *buf)
18596 +{
18597 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18598 + return sprintf(buf, "Frame Interval = 0x%x\n",
18599 + dwc_otg_get_fr_interval(otg_dev->core_if));
18600 +}
18601 +
18602 +/**
18603 + * Set the HFIR Frame Interval value
18604 + */
18605 +static ssize_t fr_interval_store(struct device *_dev,
18606 + struct device_attribute *attr,
18607 + const char *buf, size_t count)
18608 +{
18609 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18610 + uint32_t in = simple_strtoul(buf, NULL, 10);
18611 + dwc_otg_set_fr_interval(otg_dev->core_if, in);
18612 + return count;
18613 +}
18614 +
18615 +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
18616 +
18617 +/**
18618 + * Show the status of Remote Wakeup.
18619 + */
18620 +static ssize_t remote_wakeup_show(struct device *_dev,
18621 + struct device_attribute *attr, char *buf)
18622 +{
18623 +#ifndef DWC_HOST_ONLY
18624 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18625 +
18626 + return sprintf(buf,
18627 + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
18628 + dwc_otg_get_remotewakesig(otg_dev->core_if),
18629 + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
18630 + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
18631 +#else
18632 + return sprintf(buf, "Host Only Mode!\n");
18633 +#endif /* DWC_HOST_ONLY */
18634 +}
18635 +
18636 +/**
18637 + * Initiate a remote wakeup of the host. The Device control register
18638 + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
18639 + * flag is set.
18640 + *
18641 + */
18642 +static ssize_t remote_wakeup_store(struct device *_dev,
18643 + struct device_attribute *attr,
18644 + const char *buf, size_t count)
18645 +{
18646 +#ifndef DWC_HOST_ONLY
18647 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18648 + uint32_t val = simple_strtoul(buf, NULL, 16);
18649 +
18650 + if (val & 1) {
18651 + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
18652 + } else {
18653 + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
18654 + }
18655 +#endif /* DWC_HOST_ONLY */
18656 + return count;
18657 +}
18658 +
18659 +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
18660 + remote_wakeup_store);
18661 +
18662 +/**
18663 + * Show the whether core is hibernated or not.
18664 + */
18665 +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
18666 + struct device_attribute *attr, char *buf)
18667 +{
18668 +#ifndef DWC_HOST_ONLY
18669 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18670 +
18671 + if (dwc_otg_get_core_state(otg_dev->core_if)) {
18672 + DWC_PRINTF("Core is in hibernation\n");
18673 + } else {
18674 + DWC_PRINTF("Core is not in hibernation\n");
18675 + }
18676 +#endif /* DWC_HOST_ONLY */
18677 + return 0;
18678 +}
18679 +
18680 +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
18681 + int rem_wakeup, int reset);
18682 +
18683 +/**
18684 + * Initiate a remote wakeup of the device to exit from hibernation.
18685 + */
18686 +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
18687 + struct device_attribute *attr,
18688 + const char *buf, size_t count)
18689 +{
18690 +#ifndef DWC_HOST_ONLY
18691 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18692 + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
18693 +#endif
18694 + return count;
18695 +}
18696 +
18697 +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
18698 + rem_wakeup_pwrdn_store);
18699 +
18700 +static ssize_t disconnect_us(struct device *_dev,
18701 + struct device_attribute *attr,
18702 + const char *buf, size_t count)
18703 +{
18704 +
18705 +#ifndef DWC_HOST_ONLY
18706 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18707 + uint32_t val = simple_strtoul(buf, NULL, 16);
18708 + DWC_PRINTF("The Passed value is %04x\n", val);
18709 +
18710 + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
18711 +
18712 +#endif /* DWC_HOST_ONLY */
18713 + return count;
18714 +}
18715 +
18716 +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
18717 +
18718 +/**
18719 + * Dump global registers and either host or device registers (depending on the
18720 + * current mode of the core).
18721 + */
18722 +static ssize_t regdump_show(struct device *_dev,
18723 + struct device_attribute *attr, char *buf)
18724 +{
18725 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18726 +
18727 + dwc_otg_dump_global_registers(otg_dev->core_if);
18728 + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
18729 + dwc_otg_dump_host_registers(otg_dev->core_if);
18730 + } else {
18731 + dwc_otg_dump_dev_registers(otg_dev->core_if);
18732 +
18733 + }
18734 + return sprintf(buf, "Register Dump\n");
18735 +}
18736 +
18737 +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
18738 +
18739 +/**
18740 + * Dump global registers and either host or device registers (depending on the
18741 + * current mode of the core).
18742 + */
18743 +static ssize_t spramdump_show(struct device *_dev,
18744 + struct device_attribute *attr, char *buf)
18745 +{
18746 +#if 0
18747 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18748 +
18749 + dwc_otg_dump_spram(otg_dev->core_if);
18750 +#endif
18751 +
18752 + return sprintf(buf, "SPRAM Dump\n");
18753 +}
18754 +
18755 +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
18756 +
18757 +/**
18758 + * Dump the current hcd state.
18759 + */
18760 +static ssize_t hcddump_show(struct device *_dev,
18761 + struct device_attribute *attr, char *buf)
18762 +{
18763 +#ifndef DWC_DEVICE_ONLY
18764 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18765 + dwc_otg_hcd_dump_state(otg_dev->hcd);
18766 +#endif /* DWC_DEVICE_ONLY */
18767 + return sprintf(buf, "HCD Dump\n");
18768 +}
18769 +
18770 +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
18771 +
18772 +/**
18773 + * Dump the average frame remaining at SOF. This can be used to
18774 + * determine average interrupt latency. Frame remaining is also shown for
18775 + * start transfer and two additional sample points.
18776 + */
18777 +static ssize_t hcd_frrem_show(struct device *_dev,
18778 + struct device_attribute *attr, char *buf)
18779 +{
18780 +#ifndef DWC_DEVICE_ONLY
18781 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18782 +
18783 + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
18784 +#endif /* DWC_DEVICE_ONLY */
18785 + return sprintf(buf, "HCD Dump Frame Remaining\n");
18786 +}
18787 +
18788 +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
18789 +
18790 +/**
18791 + * Displays the time required to read the GNPTXFSIZ register many times (the
18792 + * output shows the number of times the register is read).
18793 + */
18794 +#define RW_REG_COUNT 10000000
18795 +#define MSEC_PER_JIFFIE 1000/HZ
18796 +static ssize_t rd_reg_test_show(struct device *_dev,
18797 + struct device_attribute *attr, char *buf)
18798 +{
18799 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18800 + int i;
18801 + int time;
18802 + int start_jiffies;
18803 +
18804 + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
18805 + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
18806 + start_jiffies = jiffies;
18807 + for (i = 0; i < RW_REG_COUNT; i++) {
18808 + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
18809 + }
18810 + time = jiffies - start_jiffies;
18811 + return sprintf(buf,
18812 + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
18813 + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
18814 +}
18815 +
18816 +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
18817 +
18818 +/**
18819 + * Displays the time required to write the GNPTXFSIZ register many times (the
18820 + * output shows the number of times the register is written).
18821 + */
18822 +static ssize_t wr_reg_test_show(struct device *_dev,
18823 + struct device_attribute *attr, char *buf)
18824 +{
18825 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18826 + uint32_t reg_val;
18827 + int i;
18828 + int time;
18829 + int start_jiffies;
18830 +
18831 + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
18832 + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
18833 + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
18834 + start_jiffies = jiffies;
18835 + for (i = 0; i < RW_REG_COUNT; i++) {
18836 + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
18837 + }
18838 + time = jiffies - start_jiffies;
18839 + return sprintf(buf,
18840 + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
18841 + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
18842 +}
18843 +
18844 +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
18845 +
18846 +#ifdef CONFIG_USB_DWC_OTG_LPM
18847 +
18848 +/**
18849 +* Show the lpm_response attribute.
18850 +*/
18851 +static ssize_t lpmresp_show(struct device *_dev,
18852 + struct device_attribute *attr, char *buf)
18853 +{
18854 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18855 +
18856 + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
18857 + return sprintf(buf, "** LPM is DISABLED **\n");
18858 +
18859 + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
18860 + return sprintf(buf, "** Current mode is not device mode\n");
18861 + }
18862 + return sprintf(buf, "lpm_response = %d\n",
18863 + dwc_otg_get_lpmresponse(otg_dev->core_if));
18864 +}
18865 +
18866 +/**
18867 +* Store the lpm_response attribute.
18868 +*/
18869 +static ssize_t lpmresp_store(struct device *_dev,
18870 + struct device_attribute *attr,
18871 + const char *buf, size_t count)
18872 +{
18873 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18874 + uint32_t val = simple_strtoul(buf, NULL, 16);
18875 +
18876 + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
18877 + return 0;
18878 + }
18879 +
18880 + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
18881 + return 0;
18882 + }
18883 +
18884 + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
18885 + return count;
18886 +}
18887 +
18888 +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
18889 +
18890 +/**
18891 +* Show the sleep_status attribute.
18892 +*/
18893 +static ssize_t sleepstatus_show(struct device *_dev,
18894 + struct device_attribute *attr, char *buf)
18895 +{
18896 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18897 + return sprintf(buf, "Sleep Status = %d\n",
18898 + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
18899 +}
18900 +
18901 +/**
18902 + * Store the sleep_status attribure.
18903 + */
18904 +static ssize_t sleepstatus_store(struct device *_dev,
18905 + struct device_attribute *attr,
18906 + const char *buf, size_t count)
18907 +{
18908 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18909 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
18910 +
18911 + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
18912 + if (dwc_otg_is_host_mode(core_if)) {
18913 +
18914 + DWC_PRINTF("Host initiated resume\n");
18915 + dwc_otg_set_prtresume(otg_dev->core_if, 1);
18916 + }
18917 + }
18918 +
18919 + return count;
18920 +}
18921 +
18922 +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
18923 + sleepstatus_store);
18924 +
18925 +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
18926 +
18927 +/**@}*/
18928 +
18929 +/**
18930 + * Create the device files
18931 + */
18932 +void dwc_otg_attr_create(
18933 +#ifdef LM_INTERFACE
18934 + struct lm_device *dev
18935 +#elif defined(PCI_INTERFACE)
18936 + struct pci_dev *dev
18937 +#elif defined(PLATFORM_INTERFACE)
18938 + struct platform_device *dev
18939 +#endif
18940 + )
18941 +{
18942 + int error;
18943 +
18944 + error = device_create_file(&dev->dev, &dev_attr_regoffset);
18945 + error = device_create_file(&dev->dev, &dev_attr_regvalue);
18946 + error = device_create_file(&dev->dev, &dev_attr_mode);
18947 + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
18948 + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
18949 + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
18950 + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
18951 + error = device_create_file(&dev->dev, &dev_attr_hnp);
18952 + error = device_create_file(&dev->dev, &dev_attr_srp);
18953 + error = device_create_file(&dev->dev, &dev_attr_buspower);
18954 + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
18955 + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
18956 + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
18957 + error = device_create_file(&dev->dev, &dev_attr_busconnected);
18958 + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
18959 + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
18960 + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
18961 + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
18962 + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
18963 + error = device_create_file(&dev->dev, &dev_attr_ggpio);
18964 + error = device_create_file(&dev->dev, &dev_attr_guid);
18965 + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
18966 + error = device_create_file(&dev->dev, &dev_attr_devspeed);
18967 + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
18968 + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
18969 + error = device_create_file(&dev->dev, &dev_attr_hprt0);
18970 + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
18971 + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
18972 + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
18973 + error = device_create_file(&dev->dev, &dev_attr_regdump);
18974 + error = device_create_file(&dev->dev, &dev_attr_spramdump);
18975 + error = device_create_file(&dev->dev, &dev_attr_hcddump);
18976 + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
18977 + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
18978 + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
18979 +#ifdef CONFIG_USB_DWC_OTG_LPM
18980 + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
18981 + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
18982 +#endif
18983 +}
18984 +
18985 +/**
18986 + * Remove the device files
18987 + */
18988 +void dwc_otg_attr_remove(
18989 +#ifdef LM_INTERFACE
18990 + struct lm_device *dev
18991 +#elif defined(PCI_INTERFACE)
18992 + struct pci_dev *dev
18993 +#elif defined(PLATFORM_INTERFACE)
18994 + struct platform_device *dev
18995 +#endif
18996 + )
18997 +{
18998 + device_remove_file(&dev->dev, &dev_attr_regoffset);
18999 + device_remove_file(&dev->dev, &dev_attr_regvalue);
19000 + device_remove_file(&dev->dev, &dev_attr_mode);
19001 + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
19002 + device_remove_file(&dev->dev, &dev_attr_srpcapable);
19003 + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
19004 + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
19005 + device_remove_file(&dev->dev, &dev_attr_hnp);
19006 + device_remove_file(&dev->dev, &dev_attr_srp);
19007 + device_remove_file(&dev->dev, &dev_attr_buspower);
19008 + device_remove_file(&dev->dev, &dev_attr_bussuspend);
19009 + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
19010 + device_remove_file(&dev->dev, &dev_attr_fr_interval);
19011 + device_remove_file(&dev->dev, &dev_attr_busconnected);
19012 + device_remove_file(&dev->dev, &dev_attr_gotgctl);
19013 + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
19014 + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
19015 + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
19016 + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
19017 + device_remove_file(&dev->dev, &dev_attr_ggpio);
19018 + device_remove_file(&dev->dev, &dev_attr_guid);
19019 + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
19020 + device_remove_file(&dev->dev, &dev_attr_devspeed);
19021 + device_remove_file(&dev->dev, &dev_attr_enumspeed);
19022 + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
19023 + device_remove_file(&dev->dev, &dev_attr_hprt0);
19024 + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
19025 + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
19026 + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
19027 + device_remove_file(&dev->dev, &dev_attr_regdump);
19028 + device_remove_file(&dev->dev, &dev_attr_spramdump);
19029 + device_remove_file(&dev->dev, &dev_attr_hcddump);
19030 + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
19031 + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
19032 + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
19033 +#ifdef CONFIG_USB_DWC_OTG_LPM
19034 + device_remove_file(&dev->dev, &dev_attr_lpm_response);
19035 + device_remove_file(&dev->dev, &dev_attr_sleep_status);
19036 +#endif
19037 +}
19038 --- /dev/null
19039 +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.h
19040 @@ -0,0 +1,89 @@
19041 +/* ==========================================================================
19042 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
19043 + * $Revision: #13 $
19044 + * $Date: 2010/06/21 $
19045 + * $Change: 1532021 $
19046 + *
19047 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
19048 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
19049 + * otherwise expressly agreed to in writing between Synopsys and you.
19050 + *
19051 + * The Software IS NOT an item of Licensed Software or Licensed Product under
19052 + * any End User Software License Agreement or Agreement for Licensed Product
19053 + * with Synopsys or any supplement thereto. You are permitted to use and
19054 + * redistribute this Software in source and binary forms, with or without
19055 + * modification, provided that redistributions of source code must retain this
19056 + * notice. You may not view, use, disclose, copy or distribute this file or
19057 + * any information contained herein except pursuant to this license grant from
19058 + * Synopsys. If you do not agree with this notice, including the disclaimer
19059 + * below, then you are not authorized to use the Software.
19060 + *
19061 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
19062 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19063 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19064 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
19065 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19066 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19067 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19068 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19069 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19070 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
19071 + * DAMAGE.
19072 + * ========================================================================== */
19073 +
19074 +#if !defined(__DWC_OTG_ATTR_H__)
19075 +#define __DWC_OTG_ATTR_H__
19076 +
19077 +/** @file
19078 + * This file contains the interface to the Linux device attributes.
19079 + */
19080 +extern struct device_attribute dev_attr_regoffset;
19081 +extern struct device_attribute dev_attr_regvalue;
19082 +
19083 +extern struct device_attribute dev_attr_mode;
19084 +extern struct device_attribute dev_attr_hnpcapable;
19085 +extern struct device_attribute dev_attr_srpcapable;
19086 +extern struct device_attribute dev_attr_hnp;
19087 +extern struct device_attribute dev_attr_srp;
19088 +extern struct device_attribute dev_attr_buspower;
19089 +extern struct device_attribute dev_attr_bussuspend;
19090 +extern struct device_attribute dev_attr_mode_ch_tim_en;
19091 +extern struct device_attribute dev_attr_fr_interval;
19092 +extern struct device_attribute dev_attr_busconnected;
19093 +extern struct device_attribute dev_attr_gotgctl;
19094 +extern struct device_attribute dev_attr_gusbcfg;
19095 +extern struct device_attribute dev_attr_grxfsiz;
19096 +extern struct device_attribute dev_attr_gnptxfsiz;
19097 +extern struct device_attribute dev_attr_gpvndctl;
19098 +extern struct device_attribute dev_attr_ggpio;
19099 +extern struct device_attribute dev_attr_guid;
19100 +extern struct device_attribute dev_attr_gsnpsid;
19101 +extern struct device_attribute dev_attr_devspeed;
19102 +extern struct device_attribute dev_attr_enumspeed;
19103 +extern struct device_attribute dev_attr_hptxfsiz;
19104 +extern struct device_attribute dev_attr_hprt0;
19105 +#ifdef CONFIG_USB_DWC_OTG_LPM
19106 +extern struct device_attribute dev_attr_lpm_response;
19107 +extern struct device_attribute devi_attr_sleep_status;
19108 +#endif
19109 +
19110 +void dwc_otg_attr_create(
19111 +#ifdef LM_INTERFACE
19112 + struct lm_device *dev
19113 +#elif defined(PCI_INTERFACE)
19114 + struct pci_dev *dev
19115 +#elif defined(PLATFORM_INTERFACE)
19116 + struct platform_device *dev
19117 +#endif
19118 + );
19119 +
19120 +void dwc_otg_attr_remove(
19121 +#ifdef LM_INTERFACE
19122 + struct lm_device *dev
19123 +#elif defined(PCI_INTERFACE)
19124 + struct pci_dev *dev
19125 +#elif defined(PLATFORM_INTERFACE)
19126 + struct platform_device *dev
19127 +#endif
19128 + );
19129 +#endif
19130 --- /dev/null
19131 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
19132 @@ -0,0 +1,1876 @@
19133 +/* ==========================================================================
19134 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
19135 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
19136 + * otherwise expressly agreed to in writing between Synopsys and you.
19137 + *
19138 + * The Software IS NOT an item of Licensed Software or Licensed Product under
19139 + * any End User Software License Agreement or Agreement for Licensed Product
19140 + * with Synopsys or any supplement thereto. You are permitted to use and
19141 + * redistribute this Software in source and binary forms, with or without
19142 + * modification, provided that redistributions of source code must retain this
19143 + * notice. You may not view, use, disclose, copy or distribute this file or
19144 + * any information contained herein except pursuant to this license grant from
19145 + * Synopsys. If you do not agree with this notice, including the disclaimer
19146 + * below, then you are not authorized to use the Software.
19147 + *
19148 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
19149 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19150 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19151 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
19152 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19153 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19154 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19155 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19156 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19157 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
19158 + * DAMAGE.
19159 + * ========================================================================== */
19160 +
19161 +/** @file
19162 + *
19163 + * This file contains the most of the CFI(Core Feature Interface)
19164 + * implementation for the OTG.
19165 + */
19166 +
19167 +#ifdef DWC_UTE_CFI
19168 +
19169 +#include "dwc_otg_pcd.h"
19170 +#include "dwc_otg_cfi.h"
19171 +
19172 +/** This definition should actually migrate to the Portability Library */
19173 +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
19174 +
19175 +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
19176 +
19177 +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
19178 +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
19179 + struct dwc_otg_pcd *pcd,
19180 + struct cfi_usb_ctrlrequest *ctrl_req);
19181 +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
19182 +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19183 + struct cfi_usb_ctrlrequest *req);
19184 +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19185 + struct cfi_usb_ctrlrequest *req);
19186 +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19187 + struct cfi_usb_ctrlrequest *req);
19188 +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
19189 + struct cfi_usb_ctrlrequest *req);
19190 +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
19191 +
19192 +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
19193 +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
19194 +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
19195 +
19196 +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
19197 +
19198 +/** This is the header of the all features descriptor */
19199 +static cfi_all_features_header_t all_props_desc_header = {
19200 + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
19201 + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
19202 + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
19203 +};
19204 +
19205 +/** This is an array of statically allocated feature descriptors */
19206 +static cfi_feature_desc_header_t prop_descs[] = {
19207 +
19208 + /* FT_ID_DMA_MODE */
19209 + {
19210 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
19211 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19212 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
19213 + },
19214 +
19215 + /* FT_ID_DMA_BUFFER_SETUP */
19216 + {
19217 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
19218 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19219 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19220 + },
19221 +
19222 + /* FT_ID_DMA_BUFF_ALIGN */
19223 + {
19224 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
19225 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19226 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19227 + },
19228 +
19229 + /* FT_ID_DMA_CONCAT_SETUP */
19230 + {
19231 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
19232 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19233 + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19234 + },
19235 +
19236 + /* FT_ID_DMA_CIRCULAR */
19237 + {
19238 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
19239 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19240 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19241 + },
19242 +
19243 + /* FT_ID_THRESHOLD_SETUP */
19244 + {
19245 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
19246 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19247 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19248 + },
19249 +
19250 + /* FT_ID_DFIFO_DEPTH */
19251 + {
19252 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
19253 + .bmAttributes = CFI_FEATURE_ATTR_RO,
19254 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19255 + },
19256 +
19257 + /* FT_ID_TX_FIFO_DEPTH */
19258 + {
19259 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
19260 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19261 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19262 + },
19263 +
19264 + /* FT_ID_RX_FIFO_DEPTH */
19265 + {
19266 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
19267 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19268 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19269 + }
19270 +};
19271 +
19272 +/** The table of feature names */
19273 +cfi_string_t prop_name_table[] = {
19274 + {FT_ID_DMA_MODE, "dma_mode"},
19275 + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
19276 + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
19277 + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
19278 + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
19279 + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
19280 + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
19281 + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
19282 + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
19283 + {}
19284 +};
19285 +
19286 +/************************************************************************/
19287 +
19288 +/**
19289 + * Returns the name of the feature by its ID
19290 + * or NULL if no featute ID matches.
19291 + *
19292 + */
19293 +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
19294 +{
19295 + cfi_string_t *pstr;
19296 + *len = 0;
19297 +
19298 + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
19299 + if (pstr->id == prop_id) {
19300 + *len = DWC_STRLEN(pstr->s);
19301 + return pstr->s;
19302 + }
19303 + }
19304 + return NULL;
19305 +}
19306 +
19307 +/**
19308 + * This function handles all CFI specific control requests.
19309 + *
19310 + * Return a negative value to stall the DCE.
19311 + */
19312 +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
19313 +{
19314 + int retval = 0;
19315 + dwc_otg_pcd_ep_t *ep = NULL;
19316 + cfiobject_t *cfi = pcd->cfi;
19317 + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
19318 + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
19319 + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
19320 + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
19321 + uint32_t regaddr = 0;
19322 + uint32_t regval = 0;
19323 +
19324 + /* Save this Control Request in the CFI object.
19325 + * The data field will be assigned in the data stage completion CB function.
19326 + */
19327 + cfi->ctrl_req = *ctrl;
19328 + cfi->ctrl_req.data = NULL;
19329 +
19330 + cfi->need_gadget_att = 0;
19331 + cfi->need_status_in_complete = 0;
19332 +
19333 + switch (ctrl->bRequest) {
19334 + case VEN_CORE_GET_FEATURES:
19335 + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
19336 + if (retval >= 0) {
19337 + //dump_msg(cfi->buf_in.buf, retval);
19338 + ep = &pcd->ep0;
19339 +
19340 + retval = min((uint16_t) retval, wLen);
19341 + /* Transfer this buffer to the host through the EP0-IN EP */
19342 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19343 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19344 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19345 + ep->dwc_ep.xfer_len = retval;
19346 + ep->dwc_ep.xfer_count = 0;
19347 + ep->dwc_ep.sent_zlp = 0;
19348 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19349 +
19350 + pcd->ep0_pending = 1;
19351 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19352 + }
19353 + retval = 0;
19354 + break;
19355 +
19356 + case VEN_CORE_GET_FEATURE:
19357 + CFI_INFO("VEN_CORE_GET_FEATURE\n");
19358 + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
19359 + pcd, ctrl);
19360 + if (retval >= 0) {
19361 + ep = &pcd->ep0;
19362 +
19363 + retval = min((uint16_t) retval, wLen);
19364 + /* Transfer this buffer to the host through the EP0-IN EP */
19365 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19366 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19367 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19368 + ep->dwc_ep.xfer_len = retval;
19369 + ep->dwc_ep.xfer_count = 0;
19370 + ep->dwc_ep.sent_zlp = 0;
19371 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19372 +
19373 + pcd->ep0_pending = 1;
19374 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19375 + }
19376 + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
19377 + dump_msg(cfi->buf_in.buf, retval);
19378 + break;
19379 +
19380 + case VEN_CORE_SET_FEATURE:
19381 + CFI_INFO("VEN_CORE_SET_FEATURE\n");
19382 + /* Set up an XFER to get the data stage of the control request,
19383 + * which is the new value of the feature to be modified.
19384 + */
19385 + ep = &pcd->ep0;
19386 + ep->dwc_ep.is_in = 0;
19387 + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
19388 + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
19389 + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
19390 + ep->dwc_ep.xfer_len = wLen;
19391 + ep->dwc_ep.xfer_count = 0;
19392 + ep->dwc_ep.sent_zlp = 0;
19393 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19394 +
19395 + pcd->ep0_pending = 1;
19396 + /* Read the control write's data stage */
19397 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19398 + retval = 0;
19399 + break;
19400 +
19401 + case VEN_CORE_RESET_FEATURES:
19402 + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
19403 + cfi->need_gadget_att = 1;
19404 + cfi->need_status_in_complete = 1;
19405 + retval = cfi_preproc_reset(pcd, ctrl);
19406 + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
19407 + break;
19408 +
19409 + case VEN_CORE_ACTIVATE_FEATURES:
19410 + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
19411 + break;
19412 +
19413 + case VEN_CORE_READ_REGISTER:
19414 + CFI_INFO("VEN_CORE_READ_REGISTER\n");
19415 + /* wValue optionally contains the HI WORD of the register offset and
19416 + * wIndex contains the LOW WORD of the register offset
19417 + */
19418 + if (wValue == 0) {
19419 + /* @TODO - MAS - fix the access to the base field */
19420 + regaddr = 0;
19421 + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
19422 + //GET_CORE_IF(pcd)->co
19423 + regaddr |= wIndex;
19424 + } else {
19425 + regaddr = (wValue << 16) | wIndex;
19426 + }
19427 +
19428 + /* Read a 32-bit value of the memory at the regaddr */
19429 + regval = DWC_READ_REG32((uint32_t *) regaddr);
19430 +
19431 + ep = &pcd->ep0;
19432 + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
19433 + ep->dwc_ep.is_in = 1;
19434 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19435 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19436 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19437 + ep->dwc_ep.xfer_len = wLen;
19438 + ep->dwc_ep.xfer_count = 0;
19439 + ep->dwc_ep.sent_zlp = 0;
19440 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19441 +
19442 + pcd->ep0_pending = 1;
19443 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19444 + cfi->need_gadget_att = 0;
19445 + retval = 0;
19446 + break;
19447 +
19448 + case VEN_CORE_WRITE_REGISTER:
19449 + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
19450 + /* Set up an XFER to get the data stage of the control request,
19451 + * which is the new value of the register to be modified.
19452 + */
19453 + ep = &pcd->ep0;
19454 + ep->dwc_ep.is_in = 0;
19455 + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
19456 + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
19457 + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
19458 + ep->dwc_ep.xfer_len = wLen;
19459 + ep->dwc_ep.xfer_count = 0;
19460 + ep->dwc_ep.sent_zlp = 0;
19461 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19462 +
19463 + pcd->ep0_pending = 1;
19464 + /* Read the control write's data stage */
19465 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19466 + retval = 0;
19467 + break;
19468 +
19469 + default:
19470 + retval = -DWC_E_NOT_SUPPORTED;
19471 + break;
19472 + }
19473 +
19474 + return retval;
19475 +}
19476 +
19477 +/**
19478 + * This function prepares the core features descriptors and copies its
19479 + * raw representation into the buffer <buf>.
19480 + *
19481 + * The buffer structure is as follows:
19482 + * all_features_header (8 bytes)
19483 + * features_#1 (8 bytes + feature name string length)
19484 + * features_#2 (8 bytes + feature name string length)
19485 + * .....
19486 + * features_#n - where n=the total count of feature descriptors
19487 + */
19488 +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
19489 +{
19490 + cfi_feature_desc_header_t *prop_hdr = prop_descs;
19491 + cfi_feature_desc_header_t *prop;
19492 + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
19493 + cfi_all_features_header_t *tmp;
19494 + uint8_t *tmpbuf = buf;
19495 + const uint8_t *pname = NULL;
19496 + int i, j, namelen = 0, totlen;
19497 +
19498 + /* Prepare and copy the core features into the buffer */
19499 + CFI_INFO("%s:\n", __func__);
19500 +
19501 + tmp = (cfi_all_features_header_t *) tmpbuf;
19502 + *tmp = *all_props_hdr;
19503 + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
19504 +
19505 + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
19506 + for (i = 0; i < j; i++, prop_hdr++) {
19507 + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
19508 + prop = (cfi_feature_desc_header_t *) tmpbuf;
19509 + *prop = *prop_hdr;
19510 +
19511 + prop->bNameLen = namelen;
19512 + prop->wLength =
19513 + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
19514 + namelen);
19515 +
19516 + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
19517 + dwc_memcpy(tmpbuf, pname, namelen);
19518 + tmpbuf += namelen;
19519 + }
19520 +
19521 + totlen = tmpbuf - buf;
19522 +
19523 + if (totlen > 0) {
19524 + tmp = (cfi_all_features_header_t *) buf;
19525 + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
19526 + }
19527 +
19528 + return totlen;
19529 +}
19530 +
19531 +/**
19532 + * This function releases all the dynamic memory in the CFI object.
19533 + */
19534 +static void cfi_release(cfiobject_t * cfiobj)
19535 +{
19536 + cfi_ep_t *cfiep;
19537 + dwc_list_link_t *tmp;
19538 +
19539 + CFI_INFO("%s\n", __func__);
19540 +
19541 + if (cfiobj->buf_in.buf) {
19542 + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
19543 + cfiobj->buf_in.addr);
19544 + cfiobj->buf_in.buf = NULL;
19545 + }
19546 +
19547 + if (cfiobj->buf_out.buf) {
19548 + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
19549 + cfiobj->buf_out.addr);
19550 + cfiobj->buf_out.buf = NULL;
19551 + }
19552 +
19553 + /* Free the Buffer Setup values for each EP */
19554 + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
19555 + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
19556 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
19557 + cfi_free_ep_bs_dyn_data(cfiep);
19558 + }
19559 +}
19560 +
19561 +/**
19562 + * This function frees the dynamically allocated EP buffer setup data.
19563 + */
19564 +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
19565 +{
19566 + if (cfiep->bm_sg) {
19567 + DWC_FREE(cfiep->bm_sg);
19568 + cfiep->bm_sg = NULL;
19569 + }
19570 +
19571 + if (cfiep->bm_align) {
19572 + DWC_FREE(cfiep->bm_align);
19573 + cfiep->bm_align = NULL;
19574 + }
19575 +
19576 + if (cfiep->bm_concat) {
19577 + if (NULL != cfiep->bm_concat->wTxBytes) {
19578 + DWC_FREE(cfiep->bm_concat->wTxBytes);
19579 + cfiep->bm_concat->wTxBytes = NULL;
19580 + }
19581 + DWC_FREE(cfiep->bm_concat);
19582 + cfiep->bm_concat = NULL;
19583 + }
19584 +}
19585 +
19586 +/**
19587 + * This function initializes the default values of the features
19588 + * for a specific endpoint and should be called only once when
19589 + * the EP is enabled first time.
19590 + */
19591 +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
19592 +{
19593 + int retval = 0;
19594 +
19595 + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
19596 + if (NULL == cfiep->bm_sg) {
19597 + CFI_INFO("Failed to allocate memory for SG feature value\n");
19598 + return -DWC_E_NO_MEMORY;
19599 + }
19600 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
19601 +
19602 + /* For the Concatenation feature's default value we do not allocate
19603 + * memory for the wTxBytes field - it will be done in the set_feature_value
19604 + * request handler.
19605 + */
19606 + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
19607 + if (NULL == cfiep->bm_concat) {
19608 + CFI_INFO
19609 + ("Failed to allocate memory for CONCATENATION feature value\n");
19610 + DWC_FREE(cfiep->bm_sg);
19611 + return -DWC_E_NO_MEMORY;
19612 + }
19613 + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
19614 +
19615 + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
19616 + if (NULL == cfiep->bm_align) {
19617 + CFI_INFO
19618 + ("Failed to allocate memory for Alignment feature value\n");
19619 + DWC_FREE(cfiep->bm_sg);
19620 + DWC_FREE(cfiep->bm_concat);
19621 + return -DWC_E_NO_MEMORY;
19622 + }
19623 + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
19624 +
19625 + return retval;
19626 +}
19627 +
19628 +/**
19629 + * The callback function that notifies the CFI on the activation of
19630 + * an endpoint in the PCD. The following steps are done in this function:
19631 + *
19632 + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
19633 + * active endpoint)
19634 + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
19635 + * Set the Buffer Mode to standard
19636 + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
19637 + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
19638 + */
19639 +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
19640 + struct dwc_otg_pcd_ep *ep)
19641 +{
19642 + cfi_ep_t *cfiep;
19643 + int retval = -DWC_E_NOT_SUPPORTED;
19644 +
19645 + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
19646 + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
19647 + /* MAS - Check whether this endpoint already is in the list */
19648 + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
19649 +
19650 + if (NULL == cfiep) {
19651 + /* Allocate a cfi_ep_t object */
19652 + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
19653 + if (NULL == cfiep) {
19654 + CFI_INFO
19655 + ("Unable to allocate memory for <cfiep> in function %s\n",
19656 + __func__);
19657 + return -DWC_E_NO_MEMORY;
19658 + }
19659 + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
19660 +
19661 + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
19662 + cfiep->ep = ep;
19663 +
19664 + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
19665 + ep->dwc_ep.descs =
19666 + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
19667 + sizeof(dwc_otg_dma_desc_t),
19668 + &ep->dwc_ep.descs_dma_addr);
19669 +
19670 + if (NULL == ep->dwc_ep.descs) {
19671 + DWC_FREE(cfiep);
19672 + return -DWC_E_NO_MEMORY;
19673 + }
19674 +
19675 + DWC_LIST_INIT(&cfiep->lh);
19676 +
19677 + /* Set the buffer mode to BM_STANDARD. It will be modified
19678 + * when building descriptors for a specific buffer mode */
19679 + ep->dwc_ep.buff_mode = BM_STANDARD;
19680 +
19681 + /* Create and initialize the default values for this EP's Buffer modes */
19682 + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
19683 + return retval;
19684 +
19685 + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
19686 + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
19687 + retval = 0;
19688 + } else { /* The sought EP already is in the list */
19689 + CFI_INFO("%s: The sought EP already is in the list\n",
19690 + __func__);
19691 + }
19692 +
19693 + return retval;
19694 +}
19695 +
19696 +/**
19697 + * This function is called when the data stage of a 3-stage Control Write request
19698 + * is complete.
19699 + *
19700 + */
19701 +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
19702 + struct dwc_otg_pcd *pcd)
19703 +{
19704 + uint32_t addr, reg_value;
19705 + uint16_t wIndex, wValue;
19706 + uint8_t bRequest;
19707 + uint8_t *buf = cfi->buf_out.buf;
19708 + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
19709 + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
19710 + int retval = -DWC_E_NOT_SUPPORTED;
19711 +
19712 + CFI_INFO("%s\n", __func__);
19713 +
19714 + bRequest = ctrl_req->bRequest;
19715 + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
19716 + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
19717 +
19718 + /*
19719 + * Save the pointer to the data stage in the ctrl_req's <data> field.
19720 + * The request should be already saved in the command stage by now.
19721 + */
19722 + ctrl_req->data = cfi->buf_out.buf;
19723 + cfi->need_status_in_complete = 0;
19724 + cfi->need_gadget_att = 0;
19725 +
19726 + switch (bRequest) {
19727 + case VEN_CORE_WRITE_REGISTER:
19728 + /* The buffer contains raw data of the new value for the register */
19729 + reg_value = *((uint32_t *) buf);
19730 + if (wValue == 0) {
19731 + addr = 0;
19732 + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
19733 + addr += wIndex;
19734 + } else {
19735 + addr = (wValue << 16) | wIndex;
19736 + }
19737 +
19738 + //writel(reg_value, addr);
19739 +
19740 + retval = 0;
19741 + cfi->need_status_in_complete = 1;
19742 + break;
19743 +
19744 + case VEN_CORE_SET_FEATURE:
19745 + /* The buffer contains raw data of the new value of the feature */
19746 + retval = cfi_set_feature_value(pcd);
19747 + if (retval < 0)
19748 + return retval;
19749 +
19750 + cfi->need_status_in_complete = 1;
19751 + break;
19752 +
19753 + default:
19754 + break;
19755 + }
19756 +
19757 + return retval;
19758 +}
19759 +
19760 +/**
19761 + * This function builds the DMA descriptors for the SG buffer mode.
19762 + */
19763 +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
19764 + dwc_otg_pcd_request_t * req)
19765 +{
19766 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
19767 + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
19768 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
19769 + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
19770 + dma_addr_t buff_addr = req->dma;
19771 + int i;
19772 + uint32_t txsize, off;
19773 +
19774 + txsize = sgval->wSize;
19775 + off = sgval->bOffset;
19776 +
19777 +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
19778 +// __func__, cfiep->ep->ep.name, txsize, off);
19779 +
19780 + for (i = 0; i < sgval->bCount; i++) {
19781 + desc->status.b.bs = BS_HOST_BUSY;
19782 + desc->buf = buff_addr;
19783 + desc->status.b.l = 0;
19784 + desc->status.b.ioc = 0;
19785 + desc->status.b.sp = 0;
19786 + desc->status.b.bytes = txsize;
19787 + desc->status.b.bs = BS_HOST_READY;
19788 +
19789 + /* Set the next address of the buffer */
19790 + buff_addr += txsize + off;
19791 + desc_last = desc;
19792 + desc++;
19793 + }
19794 +
19795 + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
19796 + desc_last->status.b.l = 1;
19797 + desc_last->status.b.ioc = 1;
19798 + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
19799 + /* Save the last DMA descriptor pointer */
19800 + cfiep->dma_desc_last = desc_last;
19801 + cfiep->desc_count = sgval->bCount;
19802 +}
19803 +
19804 +/**
19805 + * This function builds the DMA descriptors for the Concatenation buffer mode.
19806 + */
19807 +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
19808 + dwc_otg_pcd_request_t * req)
19809 +{
19810 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
19811 + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
19812 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
19813 + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
19814 + dma_addr_t buff_addr = req->dma;
19815 + int i;
19816 + uint16_t *txsize;
19817 +
19818 + txsize = concatval->wTxBytes;
19819 +
19820 + for (i = 0; i < concatval->hdr.bDescCount; i++) {
19821 + desc->buf = buff_addr;
19822 + desc->status.b.bs = BS_HOST_BUSY;
19823 + desc->status.b.l = 0;
19824 + desc->status.b.ioc = 0;
19825 + desc->status.b.sp = 0;
19826 + desc->status.b.bytes = *txsize;
19827 + desc->status.b.bs = BS_HOST_READY;
19828 +
19829 + txsize++;
19830 + /* Set the next address of the buffer */
19831 + buff_addr += UGETW(ep->desc->wMaxPacketSize);
19832 + desc_last = desc;
19833 + desc++;
19834 + }
19835 +
19836 + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
19837 + desc_last->status.b.l = 1;
19838 + desc_last->status.b.ioc = 1;
19839 + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
19840 + cfiep->dma_desc_last = desc_last;
19841 + cfiep->desc_count = concatval->hdr.bDescCount;
19842 +}
19843 +
19844 +/**
19845 + * This function builds the DMA descriptors for the Circular buffer mode
19846 + */
19847 +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
19848 + dwc_otg_pcd_request_t * req)
19849 +{
19850 + /* @todo: MAS - add implementation when this feature needs to be tested */
19851 +}
19852 +
19853 +/**
19854 + * This function builds the DMA descriptors for the Alignment buffer mode
19855 + */
19856 +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
19857 + dwc_otg_pcd_request_t * req)
19858 +{
19859 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
19860 + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
19861 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
19862 + dma_addr_t buff_addr = req->dma;
19863 +
19864 + desc->status.b.bs = BS_HOST_BUSY;
19865 + desc->status.b.l = 1;
19866 + desc->status.b.ioc = 1;
19867 + desc->status.b.sp = ep->dwc_ep.sent_zlp;
19868 + desc->status.b.bytes = req->length;
19869 + /* Adjust the buffer alignment */
19870 + desc->buf = (buff_addr + alignval->bAlign);
19871 + desc->status.b.bs = BS_HOST_READY;
19872 + cfiep->dma_desc_last = desc;
19873 + cfiep->desc_count = 1;
19874 +}
19875 +
19876 +/**
19877 + * This function builds the DMA descriptors chain for different modes of the
19878 + * buffer setup of an endpoint.
19879 + */
19880 +static void cfi_build_descriptors(struct cfiobject *cfi,
19881 + struct dwc_otg_pcd *pcd,
19882 + struct dwc_otg_pcd_ep *ep,
19883 + dwc_otg_pcd_request_t * req)
19884 +{
19885 + cfi_ep_t *cfiep;
19886 +
19887 + /* Get the cfiep by the dwc_otg_pcd_ep */
19888 + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
19889 + if (NULL == cfiep) {
19890 + CFI_INFO("%s: Unable to find a matching active endpoint\n",
19891 + __func__);
19892 + return;
19893 + }
19894 +
19895 + cfiep->xfer_len = req->length;
19896 +
19897 + /* Iterate through all the DMA descriptors */
19898 + switch (cfiep->ep->dwc_ep.buff_mode) {
19899 + case BM_SG:
19900 + cfi_build_sg_descs(cfi, cfiep, req);
19901 + break;
19902 +
19903 + case BM_CONCAT:
19904 + cfi_build_concat_descs(cfi, cfiep, req);
19905 + break;
19906 +
19907 + case BM_CIRCULAR:
19908 + cfi_build_circ_descs(cfi, cfiep, req);
19909 + break;
19910 +
19911 + case BM_ALIGN:
19912 + cfi_build_align_descs(cfi, cfiep, req);
19913 + break;
19914 +
19915 + default:
19916 + break;
19917 + }
19918 +}
19919 +
19920 +/**
19921 + * Allocate DMA buffer for different Buffer modes.
19922 + */
19923 +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
19924 + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
19925 + unsigned size, gfp_t flags)
19926 +{
19927 + return DWC_DMA_ALLOC(size, dma);
19928 +}
19929 +
19930 +/**
19931 + * This function initializes the CFI object.
19932 + */
19933 +int init_cfi(cfiobject_t * cfiobj)
19934 +{
19935 + CFI_INFO("%s\n", __func__);
19936 +
19937 + /* Allocate a buffer for IN XFERs */
19938 + cfiobj->buf_in.buf =
19939 + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
19940 + if (NULL == cfiobj->buf_in.buf) {
19941 + CFI_INFO("Unable to allocate buffer for INs\n");
19942 + return -DWC_E_NO_MEMORY;
19943 + }
19944 +
19945 + /* Allocate a buffer for OUT XFERs */
19946 + cfiobj->buf_out.buf =
19947 + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
19948 + if (NULL == cfiobj->buf_out.buf) {
19949 + CFI_INFO("Unable to allocate buffer for OUT\n");
19950 + return -DWC_E_NO_MEMORY;
19951 + }
19952 +
19953 + /* Initialize the callback function pointers */
19954 + cfiobj->ops.release = cfi_release;
19955 + cfiobj->ops.ep_enable = cfi_ep_enable;
19956 + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
19957 + cfiobj->ops.build_descriptors = cfi_build_descriptors;
19958 + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
19959 +
19960 + /* Initialize the list of active endpoints in the CFI object */
19961 + DWC_LIST_INIT(&cfiobj->active_eps);
19962 +
19963 + return 0;
19964 +}
19965 +
19966 +/**
19967 + * This function reads the required feature's current value into the buffer
19968 + *
19969 + * @retval: Returns negative as error, or the data length of the feature
19970 + */
19971 +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
19972 + struct dwc_otg_pcd *pcd,
19973 + struct cfi_usb_ctrlrequest *ctrl_req)
19974 +{
19975 + int retval = -DWC_E_NOT_SUPPORTED;
19976 + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
19977 + uint16_t dfifo, rxfifo, txfifo;
19978 +
19979 + switch (ctrl_req->wIndex) {
19980 + /* Whether the DDMA is enabled or not */
19981 + case FT_ID_DMA_MODE:
19982 + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
19983 + retval = 1;
19984 + break;
19985 +
19986 + case FT_ID_DMA_BUFFER_SETUP:
19987 + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
19988 + break;
19989 +
19990 + case FT_ID_DMA_BUFF_ALIGN:
19991 + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
19992 + break;
19993 +
19994 + case FT_ID_DMA_CONCAT_SETUP:
19995 + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
19996 + break;
19997 +
19998 + case FT_ID_DMA_CIRCULAR:
19999 + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
20000 + break;
20001 +
20002 + case FT_ID_THRESHOLD_SETUP:
20003 + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
20004 + break;
20005 +
20006 + case FT_ID_DFIFO_DEPTH:
20007 + dfifo = get_dfifo_size(coreif);
20008 + *((uint16_t *) buf) = dfifo;
20009 + retval = sizeof(uint16_t);
20010 + break;
20011 +
20012 + case FT_ID_TX_FIFO_DEPTH:
20013 + retval = get_txfifo_size(pcd, ctrl_req->wValue);
20014 + if (retval >= 0) {
20015 + txfifo = retval;
20016 + *((uint16_t *) buf) = txfifo;
20017 + retval = sizeof(uint16_t);
20018 + }
20019 + break;
20020 +
20021 + case FT_ID_RX_FIFO_DEPTH:
20022 + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
20023 + if (retval >= 0) {
20024 + rxfifo = retval;
20025 + *((uint16_t *) buf) = rxfifo;
20026 + retval = sizeof(uint16_t);
20027 + }
20028 + break;
20029 + }
20030 +
20031 + return retval;
20032 +}
20033 +
20034 +/**
20035 + * This function resets the SG for the specified EP to its default value
20036 + */
20037 +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
20038 +{
20039 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20040 + return 0;
20041 +}
20042 +
20043 +/**
20044 + * This function resets the Alignment for the specified EP to its default value
20045 + */
20046 +static int cfi_reset_align_val(cfi_ep_t * cfiep)
20047 +{
20048 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20049 + return 0;
20050 +}
20051 +
20052 +/**
20053 + * This function resets the Concatenation for the specified EP to its default value
20054 + * This function will also set the value of the wTxBytes field to NULL after
20055 + * freeing the memory previously allocated for this field.
20056 + */
20057 +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
20058 +{
20059 + /* First we need to free the wTxBytes field */
20060 + if (cfiep->bm_concat->wTxBytes) {
20061 + DWC_FREE(cfiep->bm_concat->wTxBytes);
20062 + cfiep->bm_concat->wTxBytes = NULL;
20063 + }
20064 +
20065 + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
20066 + return 0;
20067 +}
20068 +
20069 +/**
20070 + * This function resets all the buffer setups of the specified endpoint
20071 + */
20072 +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
20073 +{
20074 + cfi_reset_sg_val(cfiep);
20075 + cfi_reset_align_val(cfiep);
20076 + cfi_reset_concat_val(cfiep);
20077 + return 0;
20078 +}
20079 +
20080 +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
20081 + uint8_t rx_rst, uint8_t tx_rst)
20082 +{
20083 + int retval = -DWC_E_INVALID;
20084 + uint16_t tx_siz[15];
20085 + uint16_t rx_siz = 0;
20086 + dwc_otg_pcd_ep_t *ep = NULL;
20087 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
20088 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
20089 +
20090 + if (rx_rst) {
20091 + rx_siz = params->dev_rx_fifo_size;
20092 + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
20093 + }
20094 +
20095 + if (tx_rst) {
20096 + if (ep_addr == 0) {
20097 + int i;
20098 +
20099 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20100 + tx_siz[i] =
20101 + core_if->core_params->dev_tx_fifo_size[i];
20102 + core_if->core_params->dev_tx_fifo_size[i] =
20103 + core_if->init_txfsiz[i];
20104 + }
20105 + } else {
20106 +
20107 + ep = get_ep_by_addr(pcd, ep_addr);
20108 +
20109 + if (NULL == ep) {
20110 + CFI_INFO
20111 + ("%s: Unable to get the endpoint addr=0x%02x\n",
20112 + __func__, ep_addr);
20113 + return -DWC_E_INVALID;
20114 + }
20115 +
20116 + tx_siz[0] =
20117 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
20118 + 1];
20119 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
20120 + GET_CORE_IF(pcd)->init_txfsiz[ep->
20121 + dwc_ep.tx_fifo_num -
20122 + 1];
20123 + }
20124 + }
20125 +
20126 + if (resize_fifos(GET_CORE_IF(pcd))) {
20127 + retval = 0;
20128 + } else {
20129 + CFI_INFO
20130 + ("%s: Error resetting the feature Reset All(FIFO size)\n",
20131 + __func__);
20132 + if (rx_rst) {
20133 + params->dev_rx_fifo_size = rx_siz;
20134 + }
20135 +
20136 + if (tx_rst) {
20137 + if (ep_addr == 0) {
20138 + int i;
20139 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
20140 + i++) {
20141 + core_if->
20142 + core_params->dev_tx_fifo_size[i] =
20143 + tx_siz[i];
20144 + }
20145 + } else {
20146 + params->dev_tx_fifo_size[ep->
20147 + dwc_ep.tx_fifo_num -
20148 + 1] = tx_siz[0];
20149 + }
20150 + }
20151 + retval = -DWC_E_INVALID;
20152 + }
20153 + return retval;
20154 +}
20155 +
20156 +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
20157 +{
20158 + int retval = 0;
20159 + cfi_ep_t *cfiep;
20160 + cfiobject_t *cfi = pcd->cfi;
20161 + dwc_list_link_t *tmp;
20162 +
20163 + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
20164 + if (retval < 0) {
20165 + return retval;
20166 + }
20167 +
20168 + /* If the EP address is known then reset the features for only that EP */
20169 + if (addr) {
20170 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20171 + if (NULL == cfiep) {
20172 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20173 + __func__, addr);
20174 + return -DWC_E_INVALID;
20175 + }
20176 + retval = cfi_ep_reset_all_setup_vals(cfiep);
20177 + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
20178 + }
20179 + /* Otherwise (wValue == 0), reset all features of all EP's */
20180 + else {
20181 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20182 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20183 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20184 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20185 + retval = cfi_ep_reset_all_setup_vals(cfiep);
20186 + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
20187 + if (retval < 0) {
20188 + CFI_INFO
20189 + ("%s: Error resetting the feature Reset All\n",
20190 + __func__);
20191 + return retval;
20192 + }
20193 + }
20194 + }
20195 + return retval;
20196 +}
20197 +
20198 +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
20199 + uint8_t addr)
20200 +{
20201 + int retval = 0;
20202 + cfi_ep_t *cfiep;
20203 + cfiobject_t *cfi = pcd->cfi;
20204 + dwc_list_link_t *tmp;
20205 +
20206 + /* If the EP address is known then reset the features for only that EP */
20207 + if (addr) {
20208 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20209 + if (NULL == cfiep) {
20210 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20211 + __func__, addr);
20212 + return -DWC_E_INVALID;
20213 + }
20214 + retval = cfi_reset_sg_val(cfiep);
20215 + }
20216 + /* Otherwise (wValue == 0), reset all features of all EP's */
20217 + else {
20218 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20219 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20220 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20221 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20222 + retval = cfi_reset_sg_val(cfiep);
20223 + if (retval < 0) {
20224 + CFI_INFO
20225 + ("%s: Error resetting the feature Buffer Setup\n",
20226 + __func__);
20227 + return retval;
20228 + }
20229 + }
20230 + }
20231 + return retval;
20232 +}
20233 +
20234 +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
20235 +{
20236 + int retval = 0;
20237 + cfi_ep_t *cfiep;
20238 + cfiobject_t *cfi = pcd->cfi;
20239 + dwc_list_link_t *tmp;
20240 +
20241 + /* If the EP address is known then reset the features for only that EP */
20242 + if (addr) {
20243 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20244 + if (NULL == cfiep) {
20245 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20246 + __func__, addr);
20247 + return -DWC_E_INVALID;
20248 + }
20249 + retval = cfi_reset_concat_val(cfiep);
20250 + }
20251 + /* Otherwise (wValue == 0), reset all features of all EP's */
20252 + else {
20253 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20254 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20255 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20256 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20257 + retval = cfi_reset_concat_val(cfiep);
20258 + if (retval < 0) {
20259 + CFI_INFO
20260 + ("%s: Error resetting the feature Concatenation Value\n",
20261 + __func__);
20262 + return retval;
20263 + }
20264 + }
20265 + }
20266 + return retval;
20267 +}
20268 +
20269 +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
20270 +{
20271 + int retval = 0;
20272 + cfi_ep_t *cfiep;
20273 + cfiobject_t *cfi = pcd->cfi;
20274 + dwc_list_link_t *tmp;
20275 +
20276 + /* If the EP address is known then reset the features for only that EP */
20277 + if (addr) {
20278 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20279 + if (NULL == cfiep) {
20280 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20281 + __func__, addr);
20282 + return -DWC_E_INVALID;
20283 + }
20284 + retval = cfi_reset_align_val(cfiep);
20285 + }
20286 + /* Otherwise (wValue == 0), reset all features of all EP's */
20287 + else {
20288 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20289 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20290 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20291 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20292 + retval = cfi_reset_align_val(cfiep);
20293 + if (retval < 0) {
20294 + CFI_INFO
20295 + ("%s: Error resetting the feature Aliignment Value\n",
20296 + __func__);
20297 + return retval;
20298 + }
20299 + }
20300 + }
20301 + return retval;
20302 +
20303 +}
20304 +
20305 +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
20306 + struct cfi_usb_ctrlrequest *req)
20307 +{
20308 + int retval = 0;
20309 +
20310 + switch (req->wIndex) {
20311 + case 0:
20312 + /* Reset all features */
20313 + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
20314 + break;
20315 +
20316 + case FT_ID_DMA_BUFFER_SETUP:
20317 + /* Reset the SG buffer setup */
20318 + retval =
20319 + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
20320 + break;
20321 +
20322 + case FT_ID_DMA_CONCAT_SETUP:
20323 + /* Reset the Concatenation buffer setup */
20324 + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
20325 + break;
20326 +
20327 + case FT_ID_DMA_BUFF_ALIGN:
20328 + /* Reset the Alignment buffer setup */
20329 + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
20330 + break;
20331 +
20332 + case FT_ID_TX_FIFO_DEPTH:
20333 + retval =
20334 + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
20335 + pcd->cfi->need_gadget_att = 0;
20336 + break;
20337 +
20338 + case FT_ID_RX_FIFO_DEPTH:
20339 + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
20340 + pcd->cfi->need_gadget_att = 0;
20341 + break;
20342 + default:
20343 + break;
20344 + }
20345 + return retval;
20346 +}
20347 +
20348 +/**
20349 + * This function sets a new value for the SG buffer setup.
20350 + */
20351 +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20352 +{
20353 + uint8_t inaddr, outaddr;
20354 + cfi_ep_t *epin, *epout;
20355 + ddma_sg_buffer_setup_t *psgval;
20356 + uint32_t desccount, size;
20357 +
20358 + CFI_INFO("%s\n", __func__);
20359 +
20360 + psgval = (ddma_sg_buffer_setup_t *) buf;
20361 + desccount = (uint32_t) psgval->bCount;
20362 + size = (uint32_t) psgval->wSize;
20363 +
20364 + /* Check the DMA descriptor count */
20365 + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
20366 + CFI_INFO
20367 + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
20368 + __func__, MAX_DMA_DESCS_PER_EP);
20369 + return -DWC_E_INVALID;
20370 + }
20371 +
20372 + /* Check the DMA descriptor count */
20373 +
20374 + if (size == 0) {
20375 +
20376 + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
20377 + __func__);
20378 +
20379 + return -DWC_E_INVALID;
20380 +
20381 + }
20382 +
20383 + inaddr = psgval->bInEndpointAddress;
20384 + outaddr = psgval->bOutEndpointAddress;
20385 +
20386 + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
20387 + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
20388 +
20389 + if (NULL == epin || NULL == epout) {
20390 + CFI_INFO
20391 + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
20392 + __func__, inaddr, outaddr);
20393 + return -DWC_E_INVALID;
20394 + }
20395 +
20396 + epin->ep->dwc_ep.buff_mode = BM_SG;
20397 + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
20398 +
20399 + epout->ep->dwc_ep.buff_mode = BM_SG;
20400 + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
20401 +
20402 + return 0;
20403 +}
20404 +
20405 +/**
20406 + * This function sets a new value for the buffer Alignment setup.
20407 + */
20408 +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20409 +{
20410 + cfi_ep_t *ep;
20411 + uint8_t addr;
20412 + ddma_align_buffer_setup_t *palignval;
20413 +
20414 + palignval = (ddma_align_buffer_setup_t *) buf;
20415 + addr = palignval->bEndpointAddress;
20416 +
20417 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20418 +
20419 + if (NULL == ep) {
20420 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20421 + __func__, addr);
20422 + return -DWC_E_INVALID;
20423 + }
20424 +
20425 + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
20426 + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
20427 +
20428 + return 0;
20429 +}
20430 +
20431 +/**
20432 + * This function sets a new value for the Concatenation buffer setup.
20433 + */
20434 +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20435 +{
20436 + uint8_t addr;
20437 + cfi_ep_t *ep;
20438 + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
20439 + uint16_t *pVals;
20440 + uint32_t desccount;
20441 + int i;
20442 + uint16_t mps;
20443 +
20444 + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
20445 + desccount = (uint32_t) pConcatValHdr->bDescCount;
20446 + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
20447 +
20448 + /* Check the DMA descriptor count */
20449 + if (desccount > MAX_DMA_DESCS_PER_EP) {
20450 + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
20451 + __func__, MAX_DMA_DESCS_PER_EP);
20452 + return -DWC_E_INVALID;
20453 + }
20454 +
20455 + addr = pConcatValHdr->bEndpointAddress;
20456 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20457 + if (NULL == ep) {
20458 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20459 + __func__, addr);
20460 + return -DWC_E_INVALID;
20461 + }
20462 +
20463 + mps = UGETW(ep->ep->desc->wMaxPacketSize);
20464 +
20465 +#if 0
20466 + for (i = 0; i < desccount; i++) {
20467 + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
20468 + }
20469 + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
20470 +#endif
20471 +
20472 + /* Check the wTxSizes to be less than or equal to the mps */
20473 + for (i = 0; i < desccount; i++) {
20474 + if (pVals[i] > mps) {
20475 + CFI_INFO
20476 + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
20477 + __func__, i, pVals[i]);
20478 + return -DWC_E_INVALID;
20479 + }
20480 + }
20481 +
20482 + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
20483 + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
20484 +
20485 + /* Free the previously allocated storage for the wTxBytes */
20486 + if (ep->bm_concat->wTxBytes) {
20487 + DWC_FREE(ep->bm_concat->wTxBytes);
20488 + }
20489 +
20490 + /* Allocate a new storage for the wTxBytes field */
20491 + ep->bm_concat->wTxBytes =
20492 + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
20493 + if (NULL == ep->bm_concat->wTxBytes) {
20494 + CFI_INFO("%s: Unable to allocate memory\n", __func__);
20495 + return -DWC_E_NO_MEMORY;
20496 + }
20497 +
20498 + /* Copy the new values into the wTxBytes filed */
20499 + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
20500 + sizeof(uint16_t) * pConcatValHdr->bDescCount);
20501 +
20502 + return 0;
20503 +}
20504 +
20505 +/**
20506 + * This function calculates the total of all FIFO sizes
20507 + *
20508 + * @param core_if Programming view of DWC_otg controller
20509 + *
20510 + * @return The total of data FIFO sizes.
20511 + *
20512 + */
20513 +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
20514 +{
20515 + dwc_otg_core_params_t *params = core_if->core_params;
20516 + uint16_t dfifo_total = 0;
20517 + int i;
20518 +
20519 + /* The shared RxFIFO size */
20520 + dfifo_total =
20521 + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
20522 +
20523 + /* Add up each TxFIFO size to the total */
20524 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20525 + dfifo_total += params->dev_tx_fifo_size[i];
20526 + }
20527 +
20528 + return dfifo_total;
20529 +}
20530 +
20531 +/**
20532 + * This function returns Rx FIFO size
20533 + *
20534 + * @param core_if Programming view of DWC_otg controller
20535 + *
20536 + * @return The total of data FIFO sizes.
20537 + *
20538 + */
20539 +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
20540 +{
20541 + switch (wValue >> 8) {
20542 + case 0:
20543 + return (core_if->pwron_rxfsiz <
20544 + 32768) ? core_if->pwron_rxfsiz : 32768;
20545 + break;
20546 + case 1:
20547 + return core_if->core_params->dev_rx_fifo_size;
20548 + break;
20549 + default:
20550 + return -DWC_E_INVALID;
20551 + break;
20552 + }
20553 +}
20554 +
20555 +/**
20556 + * This function returns Tx FIFO size for IN EP
20557 + *
20558 + * @param core_if Programming view of DWC_otg controller
20559 + *
20560 + * @return The total of data FIFO sizes.
20561 + *
20562 + */
20563 +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
20564 +{
20565 + dwc_otg_pcd_ep_t *ep;
20566 +
20567 + ep = get_ep_by_addr(pcd, wValue & 0xff);
20568 +
20569 + if (NULL == ep) {
20570 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20571 + __func__, wValue & 0xff);
20572 + return -DWC_E_INVALID;
20573 + }
20574 +
20575 + if (!ep->dwc_ep.is_in) {
20576 + CFI_INFO
20577 + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
20578 + __func__, wValue & 0xff);
20579 + return -DWC_E_INVALID;
20580 + }
20581 +
20582 + switch (wValue >> 8) {
20583 + case 0:
20584 + return (GET_CORE_IF(pcd)->pwron_txfsiz
20585 + [ep->dwc_ep.tx_fifo_num - 1] <
20586 + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
20587 + dwc_ep.tx_fifo_num
20588 + - 1] : 32768;
20589 + break;
20590 + case 1:
20591 + return GET_CORE_IF(pcd)->core_params->
20592 + dev_tx_fifo_size[ep->dwc_ep.num - 1];
20593 + break;
20594 + default:
20595 + return -DWC_E_INVALID;
20596 + break;
20597 + }
20598 +}
20599 +
20600 +/**
20601 + * This function checks if the submitted combination of
20602 + * device mode FIFO sizes is possible or not.
20603 + *
20604 + * @param core_if Programming view of DWC_otg controller
20605 + *
20606 + * @return 1 if possible, 0 otherwise.
20607 + *
20608 + */
20609 +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
20610 +{
20611 + uint16_t dfifo_actual = 0;
20612 + dwc_otg_core_params_t *params = core_if->core_params;
20613 + uint16_t start_addr = 0;
20614 + int i;
20615 +
20616 + dfifo_actual =
20617 + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
20618 +
20619 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20620 + dfifo_actual += params->dev_tx_fifo_size[i];
20621 + }
20622 +
20623 + if (dfifo_actual > core_if->total_fifo_size) {
20624 + return 0;
20625 + }
20626 +
20627 + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
20628 + return 0;
20629 +
20630 + if (params->dev_nperio_tx_fifo_size > 32768
20631 + || params->dev_nperio_tx_fifo_size < 16)
20632 + return 0;
20633 +
20634 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20635 +
20636 + if (params->dev_tx_fifo_size[i] > 768
20637 + || params->dev_tx_fifo_size[i] < 4)
20638 + return 0;
20639 + }
20640 +
20641 + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
20642 + return 0;
20643 + start_addr = params->dev_rx_fifo_size;
20644 +
20645 + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
20646 + return 0;
20647 + start_addr += params->dev_nperio_tx_fifo_size;
20648 +
20649 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20650 +
20651 + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
20652 + return 0;
20653 + start_addr += params->dev_tx_fifo_size[i];
20654 + }
20655 +
20656 + return 1;
20657 +}
20658 +
20659 +/**
20660 + * This function resizes Device mode FIFOs
20661 + *
20662 + * @param core_if Programming view of DWC_otg controller
20663 + *
20664 + * @return 1 if successful, 0 otherwise
20665 + *
20666 + */
20667 +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
20668 +{
20669 + int i = 0;
20670 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
20671 + dwc_otg_core_params_t *params = core_if->core_params;
20672 + uint32_t rx_fifo_size;
20673 + fifosize_data_t nptxfifosize;
20674 + fifosize_data_t txfifosize[15];
20675 +
20676 + uint32_t rx_fsz_bak;
20677 + uint32_t nptxfsz_bak;
20678 + uint32_t txfsz_bak[15];
20679 +
20680 + uint16_t start_address;
20681 + uint8_t retval = 1;
20682 +
20683 + if (!check_fifo_sizes(core_if)) {
20684 + return 0;
20685 + }
20686 +
20687 + /* Configure data FIFO sizes */
20688 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
20689 + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
20690 + rx_fifo_size = params->dev_rx_fifo_size;
20691 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
20692 +
20693 + /*
20694 + * Tx FIFOs These FIFOs are numbered from 1 to 15.
20695 + * Indexes of the FIFO size module parameters in the
20696 + * dev_tx_fifo_size array and the FIFO size registers in
20697 + * the dtxfsiz array run from 0 to 14.
20698 + */
20699 +
20700 + /* Non-periodic Tx FIFO */
20701 + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
20702 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
20703 + start_address = params->dev_rx_fifo_size;
20704 + nptxfifosize.b.startaddr = start_address;
20705 +
20706 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
20707 +
20708 + start_address += nptxfifosize.b.depth;
20709 +
20710 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20711 + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
20712 +
20713 + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
20714 + txfifosize[i].b.startaddr = start_address;
20715 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
20716 + txfifosize[i].d32);
20717 +
20718 + start_address += txfifosize[i].b.depth;
20719 + }
20720 +
20721 + /** Check if register values are set correctly */
20722 + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
20723 + retval = 0;
20724 + }
20725 +
20726 + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
20727 + retval = 0;
20728 + }
20729 +
20730 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20731 + if (txfifosize[i].d32 !=
20732 + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
20733 + retval = 0;
20734 + }
20735 + }
20736 +
20737 + /** If register values are not set correctly, reset old values */
20738 + if (retval == 0) {
20739 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
20740 +
20741 + /* Non-periodic Tx FIFO */
20742 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
20743 +
20744 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20745 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
20746 + txfsz_bak[i]);
20747 + }
20748 + }
20749 + } else {
20750 + return 0;
20751 + }
20752 +
20753 + /* Flush the FIFOs */
20754 + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
20755 + dwc_otg_flush_rx_fifo(core_if);
20756 +
20757 + return retval;
20758 +}
20759 +
20760 +/**
20761 + * This function sets a new value for the buffer Alignment setup.
20762 + */
20763 +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
20764 +{
20765 + int retval;
20766 + uint32_t fsiz;
20767 + uint16_t size;
20768 + uint16_t ep_addr;
20769 + dwc_otg_pcd_ep_t *ep;
20770 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
20771 + tx_fifo_size_setup_t *ptxfifoval;
20772 +
20773 + ptxfifoval = (tx_fifo_size_setup_t *) buf;
20774 + ep_addr = ptxfifoval->bEndpointAddress;
20775 + size = ptxfifoval->wDepth;
20776 +
20777 + ep = get_ep_by_addr(pcd, ep_addr);
20778 +
20779 + CFI_INFO
20780 + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
20781 + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
20782 +
20783 + if (NULL == ep) {
20784 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20785 + __func__, ep_addr);
20786 + return -DWC_E_INVALID;
20787 + }
20788 +
20789 + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
20790 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
20791 +
20792 + if (resize_fifos(GET_CORE_IF(pcd))) {
20793 + retval = 0;
20794 + } else {
20795 + CFI_INFO
20796 + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
20797 + __func__, ep_addr);
20798 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
20799 + retval = -DWC_E_INVALID;
20800 + }
20801 +
20802 + return retval;
20803 +}
20804 +
20805 +/**
20806 + * This function sets a new value for the buffer Alignment setup.
20807 + */
20808 +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
20809 +{
20810 + int retval;
20811 + uint32_t fsiz;
20812 + uint16_t size;
20813 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
20814 + rx_fifo_size_setup_t *prxfifoval;
20815 +
20816 + prxfifoval = (rx_fifo_size_setup_t *) buf;
20817 + size = prxfifoval->wDepth;
20818 +
20819 + fsiz = params->dev_rx_fifo_size;
20820 + params->dev_rx_fifo_size = size;
20821 +
20822 + if (resize_fifos(GET_CORE_IF(pcd))) {
20823 + retval = 0;
20824 + } else {
20825 + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
20826 + __func__);
20827 + params->dev_rx_fifo_size = fsiz;
20828 + retval = -DWC_E_INVALID;
20829 + }
20830 +
20831 + return retval;
20832 +}
20833 +
20834 +/**
20835 + * This function reads the SG of an EP's buffer setup into the buffer buf
20836 + */
20837 +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
20838 + struct cfi_usb_ctrlrequest *req)
20839 +{
20840 + int retval = -DWC_E_INVALID;
20841 + uint8_t addr;
20842 + cfi_ep_t *ep;
20843 +
20844 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
20845 + addr = req->wValue & 0xFF;
20846 + if (addr == 0) /* The address should be non-zero */
20847 + return retval;
20848 +
20849 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20850 + if (NULL == ep) {
20851 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
20852 + __func__, addr);
20853 + return retval;
20854 + }
20855 +
20856 + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
20857 + retval = BS_SG_VAL_DESC_LEN;
20858 + return retval;
20859 +}
20860 +
20861 +/**
20862 + * This function reads the Concatenation value of an EP's buffer mode into
20863 + * the buffer buf
20864 + */
20865 +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
20866 + struct cfi_usb_ctrlrequest *req)
20867 +{
20868 + int retval = -DWC_E_INVALID;
20869 + uint8_t addr;
20870 + cfi_ep_t *ep;
20871 + uint8_t desc_count;
20872 +
20873 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
20874 + addr = req->wValue & 0xFF;
20875 + if (addr == 0) /* The address should be non-zero */
20876 + return retval;
20877 +
20878 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20879 + if (NULL == ep) {
20880 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
20881 + __func__, addr);
20882 + return retval;
20883 + }
20884 +
20885 + /* Copy the header to the buffer */
20886 + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
20887 + /* Advance the buffer pointer by the header size */
20888 + buf += BS_CONCAT_VAL_HDR_LEN;
20889 +
20890 + desc_count = ep->bm_concat->hdr.bDescCount;
20891 + /* Copy alll the wTxBytes to the buffer */
20892 + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
20893 +
20894 + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
20895 + return retval;
20896 +}
20897 +
20898 +/**
20899 + * This function reads the buffer Alignment value of an EP's buffer mode into
20900 + * the buffer buf
20901 + *
20902 + * @return The total number of bytes copied to the buffer or negative error code.
20903 + */
20904 +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
20905 + struct cfi_usb_ctrlrequest *req)
20906 +{
20907 + int retval = -DWC_E_INVALID;
20908 + uint8_t addr;
20909 + cfi_ep_t *ep;
20910 +
20911 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
20912 + addr = req->wValue & 0xFF;
20913 + if (addr == 0) /* The address should be non-zero */
20914 + return retval;
20915 +
20916 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20917 + if (NULL == ep) {
20918 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
20919 + __func__, addr);
20920 + return retval;
20921 + }
20922 +
20923 + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
20924 + retval = BS_ALIGN_VAL_HDR_LEN;
20925 +
20926 + return retval;
20927 +}
20928 +
20929 +/**
20930 + * This function sets a new value for the specified feature
20931 + *
20932 + * @param pcd A pointer to the PCD object
20933 + *
20934 + * @return 0 if successful, negative error code otherwise to stall the DCE.
20935 + */
20936 +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
20937 +{
20938 + int retval = -DWC_E_NOT_SUPPORTED;
20939 + uint16_t wIndex, wValue;
20940 + uint8_t bRequest;
20941 + struct dwc_otg_core_if *coreif;
20942 + cfiobject_t *cfi = pcd->cfi;
20943 + struct cfi_usb_ctrlrequest *ctrl_req;
20944 + uint8_t *buf;
20945 + ctrl_req = &cfi->ctrl_req;
20946 +
20947 + buf = pcd->cfi->ctrl_req.data;
20948 +
20949 + coreif = GET_CORE_IF(pcd);
20950 + bRequest = ctrl_req->bRequest;
20951 + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
20952 + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
20953 +
20954 + /* See which feature is to be modified */
20955 + switch (wIndex) {
20956 + case FT_ID_DMA_BUFFER_SETUP:
20957 + /* Modify the feature */
20958 + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
20959 + return retval;
20960 +
20961 + /* And send this request to the gadget */
20962 + cfi->need_gadget_att = 1;
20963 + break;
20964 +
20965 + case FT_ID_DMA_BUFF_ALIGN:
20966 + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
20967 + return retval;
20968 + cfi->need_gadget_att = 1;
20969 + break;
20970 +
20971 + case FT_ID_DMA_CONCAT_SETUP:
20972 + /* Modify the feature */
20973 + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
20974 + return retval;
20975 + cfi->need_gadget_att = 1;
20976 + break;
20977 +
20978 + case FT_ID_DMA_CIRCULAR:
20979 + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
20980 + break;
20981 +
20982 + case FT_ID_THRESHOLD_SETUP:
20983 + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
20984 + break;
20985 +
20986 + case FT_ID_DFIFO_DEPTH:
20987 + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
20988 + break;
20989 +
20990 + case FT_ID_TX_FIFO_DEPTH:
20991 + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
20992 + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
20993 + return retval;
20994 + cfi->need_gadget_att = 0;
20995 + break;
20996 +
20997 + case FT_ID_RX_FIFO_DEPTH:
20998 + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
20999 + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
21000 + return retval;
21001 + cfi->need_gadget_att = 0;
21002 + break;
21003 + }
21004 +
21005 + return retval;
21006 +}
21007 +
21008 +#endif //DWC_UTE_CFI
21009 --- /dev/null
21010 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
21011 @@ -0,0 +1,320 @@
21012 +/* ==========================================================================
21013 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
21014 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
21015 + * otherwise expressly agreed to in writing between Synopsys and you.
21016 + *
21017 + * The Software IS NOT an item of Licensed Software or Licensed Product under
21018 + * any End User Software License Agreement or Agreement for Licensed Product
21019 + * with Synopsys or any supplement thereto. You are permitted to use and
21020 + * redistribute this Software in source and binary forms, with or without
21021 + * modification, provided that redistributions of source code must retain this
21022 + * notice. You may not view, use, disclose, copy or distribute this file or
21023 + * any information contained herein except pursuant to this license grant from
21024 + * Synopsys. If you do not agree with this notice, including the disclaimer
21025 + * below, then you are not authorized to use the Software.
21026 + *
21027 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
21028 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21029 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21030 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
21031 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21032 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21033 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21034 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21035 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21036 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
21037 + * DAMAGE.
21038 + * ========================================================================== */
21039 +
21040 +#if !defined(__DWC_OTG_CFI_H__)
21041 +#define __DWC_OTG_CFI_H__
21042 +
21043 +#include "dwc_otg_pcd.h"
21044 +#include "dwc_cfi_common.h"
21045 +
21046 +/**
21047 + * @file
21048 + * This file contains the CFI related OTG PCD specific common constants,
21049 + * interfaces(functions and macros) and data structures.The CFI Protocol is an
21050 + * optional interface for internal testing purposes that a DUT may implement to
21051 + * support testing of configurable features.
21052 + *
21053 + */
21054 +
21055 +struct dwc_otg_pcd;
21056 +struct dwc_otg_pcd_ep;
21057 +
21058 +/** OTG CFI Features (properties) ID constants */
21059 +/** This is a request for all Core Features */
21060 +#define FT_ID_DMA_MODE 0x0001
21061 +#define FT_ID_DMA_BUFFER_SETUP 0x0002
21062 +#define FT_ID_DMA_BUFF_ALIGN 0x0003
21063 +#define FT_ID_DMA_CONCAT_SETUP 0x0004
21064 +#define FT_ID_DMA_CIRCULAR 0x0005
21065 +#define FT_ID_THRESHOLD_SETUP 0x0006
21066 +#define FT_ID_DFIFO_DEPTH 0x0007
21067 +#define FT_ID_TX_FIFO_DEPTH 0x0008
21068 +#define FT_ID_RX_FIFO_DEPTH 0x0009
21069 +
21070 +/**********************************************************/
21071 +#define CFI_INFO_DEF
21072 +
21073 +#ifdef CFI_INFO_DEF
21074 +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
21075 +#else
21076 +#define CFI_INFO(fmt...)
21077 +#endif
21078 +
21079 +#define min(x,y) ({ \
21080 + x < y ? x : y; })
21081 +
21082 +#define max(x,y) ({ \
21083 + x > y ? x : y; })
21084 +
21085 +/**
21086 + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
21087 + * also used for setting up a buffer for Circular DDMA.
21088 + */
21089 +struct _ddma_sg_buffer_setup {
21090 +#define BS_SG_VAL_DESC_LEN 6
21091 + /* The OUT EP address */
21092 + uint8_t bOutEndpointAddress;
21093 + /* The IN EP address */
21094 + uint8_t bInEndpointAddress;
21095 + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
21096 + uint8_t bOffset;
21097 + /* The number of transfer segments (a DMA descriptors per each segment) */
21098 + uint8_t bCount;
21099 + /* Size (in byte) of each transfer segment */
21100 + uint16_t wSize;
21101 +} __attribute__ ((packed));
21102 +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
21103 +
21104 +/** Descriptor DMA Concatenation Buffer setup structure */
21105 +struct _ddma_concat_buffer_setup_hdr {
21106 +#define BS_CONCAT_VAL_HDR_LEN 4
21107 + /* The endpoint for which the buffer is to be set up */
21108 + uint8_t bEndpointAddress;
21109 + /* The count of descriptors to be used */
21110 + uint8_t bDescCount;
21111 + /* The total size of the transfer */
21112 + uint16_t wSize;
21113 +} __attribute__ ((packed));
21114 +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
21115 +
21116 +/** Descriptor DMA Concatenation Buffer setup structure */
21117 +struct _ddma_concat_buffer_setup {
21118 + /* The SG header */
21119 + ddma_concat_buffer_setup_hdr_t hdr;
21120 +
21121 + /* The XFER sizes pointer (allocated dynamically) */
21122 + uint16_t *wTxBytes;
21123 +} __attribute__ ((packed));
21124 +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
21125 +
21126 +/** Descriptor DMA Alignment Buffer setup structure */
21127 +struct _ddma_align_buffer_setup {
21128 +#define BS_ALIGN_VAL_HDR_LEN 2
21129 + uint8_t bEndpointAddress;
21130 + uint8_t bAlign;
21131 +} __attribute__ ((packed));
21132 +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
21133 +
21134 +/** Transmit FIFO Size setup structure */
21135 +struct _tx_fifo_size_setup {
21136 + uint8_t bEndpointAddress;
21137 + uint16_t wDepth;
21138 +} __attribute__ ((packed));
21139 +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
21140 +
21141 +/** Transmit FIFO Size setup structure */
21142 +struct _rx_fifo_size_setup {
21143 + uint16_t wDepth;
21144 +} __attribute__ ((packed));
21145 +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
21146 +
21147 +/**
21148 + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
21149 + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
21150 + * to the data returned in the data stage of a 3-stage Control Write requests.
21151 + */
21152 +struct cfi_usb_ctrlrequest {
21153 + uint8_t bRequestType;
21154 + uint8_t bRequest;
21155 + uint16_t wValue;
21156 + uint16_t wIndex;
21157 + uint16_t wLength;
21158 + uint8_t *data;
21159 +} UPACKED;
21160 +
21161 +/*---------------------------------------------------------------------------*/
21162 +
21163 +/**
21164 + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
21165 + * This structure is used to store the buffer setup data for any
21166 + * enabled endpoint in the PCD.
21167 + */
21168 +struct cfi_ep {
21169 + /* Entry for the list container */
21170 + dwc_list_link_t lh;
21171 + /* Pointer to the active PCD endpoint structure */
21172 + struct dwc_otg_pcd_ep *ep;
21173 + /* The last descriptor in the chain of DMA descriptors of the endpoint */
21174 + struct dwc_otg_dma_desc *dma_desc_last;
21175 + /* The SG feature value */
21176 + ddma_sg_buffer_setup_t *bm_sg;
21177 + /* The Circular feature value */
21178 + ddma_sg_buffer_setup_t *bm_circ;
21179 + /* The Concatenation feature value */
21180 + ddma_concat_buffer_setup_t *bm_concat;
21181 + /* The Alignment feature value */
21182 + ddma_align_buffer_setup_t *bm_align;
21183 + /* XFER length */
21184 + uint32_t xfer_len;
21185 + /*
21186 + * Count of DMA descriptors currently used.
21187 + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
21188 + * defined in the dwc_otg_cil.h
21189 + */
21190 + uint32_t desc_count;
21191 +};
21192 +typedef struct cfi_ep cfi_ep_t;
21193 +
21194 +typedef struct cfi_dma_buff {
21195 +#define CFI_IN_BUF_LEN 1024
21196 +#define CFI_OUT_BUF_LEN 1024
21197 + dma_addr_t addr;
21198 + uint8_t *buf;
21199 +} cfi_dma_buff_t;
21200 +
21201 +struct cfiobject;
21202 +
21203 +/**
21204 + * This is the interface for the CFI operations.
21205 + *
21206 + * @param ep_enable Called when any endpoint is enabled and activated.
21207 + * @param release Called when the CFI object is released and it needs to correctly
21208 + * deallocate the dynamic memory
21209 + * @param ctrl_write_complete Called when the data stage of the request is complete
21210 + */
21211 +typedef struct cfi_ops {
21212 + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
21213 + struct dwc_otg_pcd_ep * ep);
21214 + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
21215 + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
21216 + unsigned size, gfp_t flags);
21217 + void (*release) (struct cfiobject * cfi);
21218 + int (*ctrl_write_complete) (struct cfiobject * cfi,
21219 + struct dwc_otg_pcd * pcd);
21220 + void (*build_descriptors) (struct cfiobject * cfi,
21221 + struct dwc_otg_pcd * pcd,
21222 + struct dwc_otg_pcd_ep * ep,
21223 + dwc_otg_pcd_request_t * req);
21224 +} cfi_ops_t;
21225 +
21226 +struct cfiobject {
21227 + cfi_ops_t ops;
21228 + struct dwc_otg_pcd *pcd;
21229 + struct usb_gadget *gadget;
21230 +
21231 + /* Buffers used to send/receive CFI-related request data */
21232 + cfi_dma_buff_t buf_in;
21233 + cfi_dma_buff_t buf_out;
21234 +
21235 + /* CFI specific Control request wrapper */
21236 + struct cfi_usb_ctrlrequest ctrl_req;
21237 +
21238 + /* The list of active EP's in the PCD of type cfi_ep_t */
21239 + dwc_list_link_t active_eps;
21240 +
21241 + /* This flag shall control the propagation of a specific request
21242 + * to the gadget's processing routines.
21243 + * 0 - no gadget handling
21244 + * 1 - the gadget needs to know about this request (w/o completing a status
21245 + * phase - just return a 0 to the _setup callback)
21246 + */
21247 + uint8_t need_gadget_att;
21248 +
21249 + /* Flag indicating whether the status IN phase needs to be
21250 + * completed by the PCD
21251 + */
21252 + uint8_t need_status_in_complete;
21253 +};
21254 +typedef struct cfiobject cfiobject_t;
21255 +
21256 +#define DUMP_MSG
21257 +
21258 +#if defined(DUMP_MSG)
21259 +static inline void dump_msg(const u8 * buf, unsigned int length)
21260 +{
21261 + unsigned int start, num, i;
21262 + char line[52], *p;
21263 +
21264 + if (length >= 512)
21265 + return;
21266 +
21267 + start = 0;
21268 + while (length > 0) {
21269 + num = min(length, 16u);
21270 + p = line;
21271 + for (i = 0; i < num; ++i) {
21272 + if (i == 8)
21273 + *p++ = ' ';
21274 + DWC_SPRINTF(p, " %02x", buf[i]);
21275 + p += 3;
21276 + }
21277 + *p = 0;
21278 + DWC_DEBUG("%6x: %s\n", start, line);
21279 + buf += num;
21280 + start += num;
21281 + length -= num;
21282 + }
21283 +}
21284 +#else
21285 +static inline void dump_msg(const u8 * buf, unsigned int length)
21286 +{
21287 +}
21288 +#endif
21289 +
21290 +/**
21291 + * This function returns a pointer to cfi_ep_t object with the addr address.
21292 + */
21293 +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
21294 + uint8_t addr)
21295 +{
21296 + struct cfi_ep *pcfiep;
21297 + dwc_list_link_t *tmp;
21298 +
21299 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
21300 + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
21301 +
21302 + if (pcfiep->ep->desc->bEndpointAddress == addr) {
21303 + return pcfiep;
21304 + }
21305 + }
21306 +
21307 + return NULL;
21308 +}
21309 +
21310 +/**
21311 + * This function returns a pointer to cfi_ep_t object that matches
21312 + * the dwc_otg_pcd_ep object.
21313 + */
21314 +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
21315 + struct dwc_otg_pcd_ep *ep)
21316 +{
21317 + struct cfi_ep *pcfiep = NULL;
21318 + dwc_list_link_t *tmp;
21319 +
21320 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
21321 + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
21322 + if (pcfiep->ep == ep) {
21323 + return pcfiep;
21324 + }
21325 + }
21326 + return NULL;
21327 +}
21328 +
21329 +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
21330 +
21331 +#endif /* (__DWC_OTG_CFI_H__) */
21332 --- /dev/null
21333 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
21334 @@ -0,0 +1,7146 @@
21335 +/* ==========================================================================
21336 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
21337 + * $Revision: #191 $
21338 + * $Date: 2012/08/10 $
21339 + * $Change: 2047372 $
21340 + *
21341 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
21342 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
21343 + * otherwise expressly agreed to in writing between Synopsys and you.
21344 + *
21345 + * The Software IS NOT an item of Licensed Software or Licensed Product under
21346 + * any End User Software License Agreement or Agreement for Licensed Product
21347 + * with Synopsys or any supplement thereto. You are permitted to use and
21348 + * redistribute this Software in source and binary forms, with or without
21349 + * modification, provided that redistributions of source code must retain this
21350 + * notice. You may not view, use, disclose, copy or distribute this file or
21351 + * any information contained herein except pursuant to this license grant from
21352 + * Synopsys. If you do not agree with this notice, including the disclaimer
21353 + * below, then you are not authorized to use the Software.
21354 + *
21355 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
21356 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21357 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21358 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
21359 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21360 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21361 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21362 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21363 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21364 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
21365 + * DAMAGE.
21366 + * ========================================================================== */
21367 +
21368 +/** @file
21369 + *
21370 + * The Core Interface Layer provides basic services for accessing and
21371 + * managing the DWC_otg hardware. These services are used by both the
21372 + * Host Controller Driver and the Peripheral Controller Driver.
21373 + *
21374 + * The CIL manages the memory map for the core so that the HCD and PCD
21375 + * don't have to do this separately. It also handles basic tasks like
21376 + * reading/writing the registers and data FIFOs in the controller.
21377 + * Some of the data access functions provide encapsulation of several
21378 + * operations required to perform a task, such as writing multiple
21379 + * registers to start a transfer. Finally, the CIL performs basic
21380 + * services that are not specific to either the host or device modes
21381 + * of operation. These services include management of the OTG Host
21382 + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
21383 + * Diagnostic API is also provided to allow testing of the controller
21384 + * hardware.
21385 + *
21386 + * The Core Interface Layer has the following requirements:
21387 + * - Provides basic controller operations.
21388 + * - Minimal use of OS services.
21389 + * - The OS services used will be abstracted by using inline functions
21390 + * or macros.
21391 + *
21392 + */
21393 +
21394 +#include "dwc_os.h"
21395 +#include "dwc_otg_regs.h"
21396 +#include "dwc_otg_cil.h"
21397 +
21398 +extern bool cil_force_host;
21399 +
21400 +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
21401 +
21402 +/**
21403 + * This function is called to initialize the DWC_otg CSR data
21404 + * structures. The register addresses in the device and host
21405 + * structures are initialized from the base address supplied by the
21406 + * caller. The calling function must make the OS calls to get the
21407 + * base address of the DWC_otg controller registers. The core_params
21408 + * argument holds the parameters that specify how the core should be
21409 + * configured.
21410 + *
21411 + * @param reg_base_addr Base address of DWC_otg core registers
21412 + *
21413 + */
21414 +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
21415 +{
21416 + dwc_otg_core_if_t *core_if = 0;
21417 + dwc_otg_dev_if_t *dev_if = 0;
21418 + dwc_otg_host_if_t *host_if = 0;
21419 + uint8_t *reg_base = (uint8_t *) reg_base_addr;
21420 + int i = 0;
21421 +
21422 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
21423 +
21424 + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
21425 +
21426 + if (core_if == NULL) {
21427 + DWC_DEBUGPL(DBG_CIL,
21428 + "Allocation of dwc_otg_core_if_t failed\n");
21429 + return 0;
21430 + }
21431 + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
21432 +
21433 + /*
21434 + * Allocate the Device Mode structures.
21435 + */
21436 + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
21437 +
21438 + if (dev_if == NULL) {
21439 + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
21440 + DWC_FREE(core_if);
21441 + return 0;
21442 + }
21443 +
21444 + dev_if->dev_global_regs =
21445 + (dwc_otg_device_global_regs_t *) (reg_base +
21446 + DWC_DEV_GLOBAL_REG_OFFSET);
21447 +
21448 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21449 + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
21450 + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
21451 + (i * DWC_EP_REG_OFFSET));
21452 +
21453 + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
21454 + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
21455 + (i * DWC_EP_REG_OFFSET));
21456 + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
21457 + i, &dev_if->in_ep_regs[i]->diepctl);
21458 + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
21459 + i, &dev_if->out_ep_regs[i]->doepctl);
21460 + }
21461 +
21462 + dev_if->speed = 0; // unknown
21463 +
21464 + core_if->dev_if = dev_if;
21465 +
21466 + /*
21467 + * Allocate the Host Mode structures.
21468 + */
21469 + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
21470 +
21471 + if (host_if == NULL) {
21472 + DWC_DEBUGPL(DBG_CIL,
21473 + "Allocation of dwc_otg_host_if_t failed\n");
21474 + DWC_FREE(dev_if);
21475 + DWC_FREE(core_if);
21476 + return 0;
21477 + }
21478 +
21479 + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
21480 + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
21481 +
21482 + host_if->hprt0 =
21483 + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
21484 +
21485 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21486 + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
21487 + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
21488 + (i * DWC_OTG_CHAN_REGS_OFFSET));
21489 + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
21490 + i, &host_if->hc_regs[i]->hcchar);
21491 + }
21492 +
21493 + host_if->num_host_channels = MAX_EPS_CHANNELS;
21494 + core_if->host_if = host_if;
21495 +
21496 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21497 + core_if->data_fifo[i] =
21498 + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
21499 + (i * DWC_OTG_DATA_FIFO_SIZE));
21500 + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
21501 + i, (unsigned long)core_if->data_fifo[i]);
21502 + }
21503 +
21504 + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
21505 +
21506 + /* Initiate lx_state to L3 disconnected state */
21507 + core_if->lx_state = DWC_OTG_L3;
21508 + /*
21509 + * Store the contents of the hardware configuration registers here for
21510 + * easy access later.
21511 + */
21512 + core_if->hwcfg1.d32 =
21513 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
21514 + core_if->hwcfg2.d32 =
21515 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
21516 + core_if->hwcfg3.d32 =
21517 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
21518 + core_if->hwcfg4.d32 =
21519 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
21520 +
21521 + /* Force host mode to get HPTXFSIZ exact power on value */
21522 + {
21523 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
21524 + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
21525 + gusbcfg.b.force_host_mode = 1;
21526 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
21527 + dwc_mdelay(100);
21528 + core_if->hptxfsiz.d32 =
21529 + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
21530 + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
21531 + if (cil_force_host)
21532 + gusbcfg.b.force_host_mode = 1;
21533 + else
21534 + gusbcfg.b.force_host_mode = 0;
21535 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
21536 + dwc_mdelay(100);
21537 + }
21538 +
21539 + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
21540 + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
21541 + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
21542 + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
21543 +
21544 + core_if->hcfg.d32 =
21545 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
21546 + core_if->dcfg.d32 =
21547 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
21548 +
21549 + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
21550 + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
21551 +
21552 + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
21553 + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
21554 + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
21555 + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
21556 + core_if->hwcfg2.b.num_host_chan);
21557 + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
21558 + core_if->hwcfg2.b.nonperio_tx_q_depth);
21559 + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
21560 + core_if->hwcfg2.b.host_perio_tx_q_depth);
21561 + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
21562 + core_if->hwcfg2.b.dev_token_q_depth);
21563 +
21564 + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
21565 + core_if->hwcfg3.b.dfifo_depth);
21566 + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
21567 + core_if->hwcfg3.b.xfer_size_cntr_width);
21568 +
21569 + /*
21570 + * Set the SRP sucess bit for FS-I2c
21571 + */
21572 + core_if->srp_success = 0;
21573 + core_if->srp_timer_started = 0;
21574 +
21575 + /*
21576 + * Create new workqueue and init works
21577 + */
21578 + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
21579 + if (core_if->wq_otg == 0) {
21580 + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
21581 + DWC_FREE(host_if);
21582 + DWC_FREE(dev_if);
21583 + DWC_FREE(core_if);
21584 + return 0;
21585 + }
21586 +
21587 + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
21588 +
21589 + DWC_PRINTF("Core Release: %x.%x%x%x\n",
21590 + (core_if->snpsid >> 12 & 0xF),
21591 + (core_if->snpsid >> 8 & 0xF),
21592 + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
21593 +
21594 + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
21595 + w_wakeup_detected, core_if);
21596 + if (core_if->wkp_timer == 0) {
21597 + DWC_WARN("DWC_TIMER_ALLOC failed\n");
21598 + DWC_FREE(host_if);
21599 + DWC_FREE(dev_if);
21600 + DWC_WORKQ_FREE(core_if->wq_otg);
21601 + DWC_FREE(core_if);
21602 + return 0;
21603 + }
21604 +
21605 + if (dwc_otg_setup_params(core_if)) {
21606 + DWC_WARN("Error while setting core params\n");
21607 + }
21608 +
21609 + core_if->hibernation_suspend = 0;
21610 +
21611 + /** ADP initialization */
21612 + dwc_otg_adp_init(core_if);
21613 +
21614 + return core_if;
21615 +}
21616 +
21617 +/**
21618 + * This function frees the structures allocated by dwc_otg_cil_init().
21619 + *
21620 + * @param core_if The core interface pointer returned from
21621 + * dwc_otg_cil_init().
21622 + *
21623 + */
21624 +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
21625 +{
21626 + dctl_data_t dctl = {.d32 = 0 };
21627 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
21628 +
21629 + /* Disable all interrupts */
21630 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
21631 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
21632 +
21633 + dctl.b.sftdiscon = 1;
21634 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
21635 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
21636 + dctl.d32);
21637 + }
21638 +
21639 + if (core_if->wq_otg) {
21640 + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
21641 + DWC_WORKQ_FREE(core_if->wq_otg);
21642 + }
21643 + if (core_if->dev_if) {
21644 + DWC_FREE(core_if->dev_if);
21645 + }
21646 + if (core_if->host_if) {
21647 + DWC_FREE(core_if->host_if);
21648 + }
21649 +
21650 + /** Remove ADP Stuff */
21651 + dwc_otg_adp_remove(core_if);
21652 + if (core_if->core_params) {
21653 + DWC_FREE(core_if->core_params);
21654 + }
21655 + if (core_if->wkp_timer) {
21656 + DWC_TIMER_FREE(core_if->wkp_timer);
21657 + }
21658 + if (core_if->srp_timer) {
21659 + DWC_TIMER_FREE(core_if->srp_timer);
21660 + }
21661 + DWC_FREE(core_if);
21662 +}
21663 +
21664 +/**
21665 + * This function enables the controller's Global Interrupt in the AHB Config
21666 + * register.
21667 + *
21668 + * @param core_if Programming view of DWC_otg controller.
21669 + */
21670 +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
21671 +{
21672 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
21673 + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
21674 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
21675 +}
21676 +
21677 +/**
21678 + * This function disables the controller's Global Interrupt in the AHB Config
21679 + * register.
21680 + *
21681 + * @param core_if Programming view of DWC_otg controller.
21682 + */
21683 +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
21684 +{
21685 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
21686 + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
21687 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
21688 +}
21689 +
21690 +/**
21691 + * This function initializes the commmon interrupts, used in both
21692 + * device and host modes.
21693 + *
21694 + * @param core_if Programming view of the DWC_otg controller
21695 + *
21696 + */
21697 +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
21698 +{
21699 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
21700 + gintmsk_data_t intr_mask = {.d32 = 0 };
21701 +
21702 + /* Clear any pending OTG Interrupts */
21703 + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
21704 +
21705 + /* Clear any pending interrupts */
21706 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
21707 +
21708 + /*
21709 + * Enable the interrupts in the GINTMSK.
21710 + */
21711 + intr_mask.b.modemismatch = 1;
21712 + intr_mask.b.otgintr = 1;
21713 +
21714 + if (!core_if->dma_enable) {
21715 + intr_mask.b.rxstsqlvl = 1;
21716 + }
21717 +
21718 + intr_mask.b.conidstschng = 1;
21719 + intr_mask.b.wkupintr = 1;
21720 + intr_mask.b.disconnect = 0;
21721 + intr_mask.b.usbsuspend = 1;
21722 + intr_mask.b.sessreqintr = 1;
21723 +#ifdef CONFIG_USB_DWC_OTG_LPM
21724 + if (core_if->core_params->lpm_enable) {
21725 + intr_mask.b.lpmtranrcvd = 1;
21726 + }
21727 +#endif
21728 + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
21729 +}
21730 +
21731 +/*
21732 + * The restore operation is modified to support Synopsys Emulated Powerdown and
21733 + * Hibernation. This function is for exiting from Device mode hibernation by
21734 + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
21735 + * @param core_if Programming view of DWC_otg controller.
21736 + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
21737 + * @param reset - indicates whether resume is initiated by Reset.
21738 + */
21739 +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
21740 + int rem_wakeup, int reset)
21741 +{
21742 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
21743 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
21744 + dctl_data_t dctl = {.d32 = 0 };
21745 +
21746 + int timeout = 2000;
21747 +
21748 + if (!core_if->hibernation_suspend) {
21749 + DWC_PRINTF("Already exited from Hibernation\n");
21750 + return 1;
21751 + }
21752 +
21753 + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
21754 + /* Switch-on voltage to the core */
21755 + gpwrdn.b.pwrdnswtch = 1;
21756 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21757 + dwc_udelay(10);
21758 +
21759 + /* Reset core */
21760 + gpwrdn.d32 = 0;
21761 + gpwrdn.b.pwrdnrstn = 1;
21762 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21763 + dwc_udelay(10);
21764 +
21765 + /* Assert Restore signal */
21766 + gpwrdn.d32 = 0;
21767 + gpwrdn.b.restore = 1;
21768 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
21769 + dwc_udelay(10);
21770 +
21771 + /* Disable power clamps */
21772 + gpwrdn.d32 = 0;
21773 + gpwrdn.b.pwrdnclmp = 1;
21774 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21775 +
21776 + if (rem_wakeup) {
21777 + dwc_udelay(70);
21778 + }
21779 +
21780 + /* Deassert Reset core */
21781 + gpwrdn.d32 = 0;
21782 + gpwrdn.b.pwrdnrstn = 1;
21783 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
21784 + dwc_udelay(10);
21785 +
21786 + /* Disable PMU interrupt */
21787 + gpwrdn.d32 = 0;
21788 + gpwrdn.b.pmuintsel = 1;
21789 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21790 +
21791 + /* Mask interrupts from gpwrdn */
21792 + gpwrdn.d32 = 0;
21793 + gpwrdn.b.connect_det_msk = 1;
21794 + gpwrdn.b.srp_det_msk = 1;
21795 + gpwrdn.b.disconn_det_msk = 1;
21796 + gpwrdn.b.rst_det_msk = 1;
21797 + gpwrdn.b.lnstchng_msk = 1;
21798 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21799 +
21800 + /* Indicates that we are going out from hibernation */
21801 + core_if->hibernation_suspend = 0;
21802 +
21803 + /*
21804 + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
21805 + * indicates restore from remote_wakeup
21806 + */
21807 + restore_essential_regs(core_if, rem_wakeup, 0);
21808 +
21809 + /*
21810 + * Wait a little for seeing new value of variable hibernation_suspend if
21811 + * Restore done interrupt received before polling
21812 + */
21813 + dwc_udelay(10);
21814 +
21815 + if (core_if->hibernation_suspend == 0) {
21816 + /*
21817 + * Wait For Restore_done Interrupt. This mechanism of polling the
21818 + * interrupt is introduced to avoid any possible race conditions
21819 + */
21820 + do {
21821 + gintsts_data_t gintsts;
21822 + gintsts.d32 =
21823 + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
21824 + if (gintsts.b.restoredone) {
21825 + gintsts.d32 = 0;
21826 + gintsts.b.restoredone = 1;
21827 + DWC_WRITE_REG32(&core_if->core_global_regs->
21828 + gintsts, gintsts.d32);
21829 + DWC_PRINTF("Restore Done Interrupt seen\n");
21830 + break;
21831 + }
21832 + dwc_udelay(10);
21833 + } while (--timeout);
21834 + if (!timeout) {
21835 + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
21836 + }
21837 + }
21838 + /* Clear all pending interupts */
21839 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
21840 +
21841 + /* De-assert Restore */
21842 + gpwrdn.d32 = 0;
21843 + gpwrdn.b.restore = 1;
21844 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21845 + dwc_udelay(10);
21846 +
21847 + if (!rem_wakeup) {
21848 + pcgcctl.d32 = 0;
21849 + pcgcctl.b.rstpdwnmodule = 1;
21850 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
21851 + }
21852 +
21853 + /* Restore GUSBCFG and DCFG */
21854 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
21855 + core_if->gr_backup->gusbcfg_local);
21856 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
21857 + core_if->dr_backup->dcfg);
21858 +
21859 + /* De-assert Wakeup Logic */
21860 + gpwrdn.d32 = 0;
21861 + gpwrdn.b.pmuactv = 1;
21862 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21863 + dwc_udelay(10);
21864 +
21865 + if (!rem_wakeup) {
21866 + /* Set Device programming done bit */
21867 + dctl.b.pwronprgdone = 1;
21868 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
21869 + } else {
21870 + /* Start Remote Wakeup Signaling */
21871 + dctl.d32 = core_if->dr_backup->dctl;
21872 + dctl.b.rmtwkupsig = 1;
21873 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
21874 + }
21875 +
21876 + dwc_mdelay(2);
21877 + /* Clear all pending interupts */
21878 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
21879 +
21880 + /* Restore global registers */
21881 + dwc_otg_restore_global_regs(core_if);
21882 + /* Restore device global registers */
21883 + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
21884 +
21885 + if (rem_wakeup) {
21886 + dwc_mdelay(7);
21887 + dctl.d32 = 0;
21888 + dctl.b.rmtwkupsig = 1;
21889 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
21890 + }
21891 +
21892 + core_if->hibernation_suspend = 0;
21893 + /* The core will be in ON STATE */
21894 + core_if->lx_state = DWC_OTG_L0;
21895 + DWC_PRINTF("Hibernation recovery completes here\n");
21896 +
21897 + return 1;
21898 +}
21899 +
21900 +/*
21901 + * The restore operation is modified to support Synopsys Emulated Powerdown and
21902 + * Hibernation. This function is for exiting from Host mode hibernation by
21903 + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
21904 + * @param core_if Programming view of DWC_otg controller.
21905 + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
21906 + * @param reset - indicates whether resume is initiated by Reset.
21907 + */
21908 +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
21909 + int rem_wakeup, int reset)
21910 +{
21911 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
21912 + hprt0_data_t hprt0 = {.d32 = 0 };
21913 +
21914 + int timeout = 2000;
21915 +
21916 + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
21917 + /* Switch-on voltage to the core */
21918 + gpwrdn.b.pwrdnswtch = 1;
21919 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21920 + dwc_udelay(10);
21921 +
21922 + /* Reset core */
21923 + gpwrdn.d32 = 0;
21924 + gpwrdn.b.pwrdnrstn = 1;
21925 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21926 + dwc_udelay(10);
21927 +
21928 + /* Assert Restore signal */
21929 + gpwrdn.d32 = 0;
21930 + gpwrdn.b.restore = 1;
21931 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
21932 + dwc_udelay(10);
21933 +
21934 + /* Disable power clamps */
21935 + gpwrdn.d32 = 0;
21936 + gpwrdn.b.pwrdnclmp = 1;
21937 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21938 +
21939 + if (!rem_wakeup) {
21940 + dwc_udelay(50);
21941 + }
21942 +
21943 + /* Deassert Reset core */
21944 + gpwrdn.d32 = 0;
21945 + gpwrdn.b.pwrdnrstn = 1;
21946 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
21947 + dwc_udelay(10);
21948 +
21949 + /* Disable PMU interrupt */
21950 + gpwrdn.d32 = 0;
21951 + gpwrdn.b.pmuintsel = 1;
21952 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21953 +
21954 + gpwrdn.d32 = 0;
21955 + gpwrdn.b.connect_det_msk = 1;
21956 + gpwrdn.b.srp_det_msk = 1;
21957 + gpwrdn.b.disconn_det_msk = 1;
21958 + gpwrdn.b.rst_det_msk = 1;
21959 + gpwrdn.b.lnstchng_msk = 1;
21960 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
21961 +
21962 + /* Indicates that we are going out from hibernation */
21963 + core_if->hibernation_suspend = 0;
21964 +
21965 + /* Set Restore Essential Regs bit in PCGCCTL register */
21966 + restore_essential_regs(core_if, rem_wakeup, 1);
21967 +
21968 + /* Wait a little for seeing new value of variable hibernation_suspend if
21969 + * Restore done interrupt received before polling */
21970 + dwc_udelay(10);
21971 +
21972 + if (core_if->hibernation_suspend == 0) {
21973 + /* Wait For Restore_done Interrupt. This mechanism of polling the
21974 + * interrupt is introduced to avoid any possible race conditions
21975 + */
21976 + do {
21977 + gintsts_data_t gintsts;
21978 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
21979 + if (gintsts.b.restoredone) {
21980 + gintsts.d32 = 0;
21981 + gintsts.b.restoredone = 1;
21982 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
21983 + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
21984 + break;
21985 + }
21986 + dwc_udelay(10);
21987 + } while (--timeout);
21988 + if (!timeout) {
21989 + DWC_WARN("Restore Done interrupt wasn't generated\n");
21990 + }
21991 + }
21992 +
21993 + /* Set the flag's value to 0 again after receiving restore done interrupt */
21994 + core_if->hibernation_suspend = 0;
21995 +
21996 + /* This step is not described in functional spec but if not wait for this
21997 + * delay, mismatch interrupts occurred because just after restore core is
21998 + * in Device mode(gintsts.curmode == 0) */
21999 + dwc_mdelay(100);
22000 +
22001 + /* Clear all pending interrupts */
22002 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22003 +
22004 + /* De-assert Restore */
22005 + gpwrdn.d32 = 0;
22006 + gpwrdn.b.restore = 1;
22007 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22008 + dwc_udelay(10);
22009 +
22010 + /* Restore GUSBCFG and HCFG */
22011 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
22012 + core_if->gr_backup->gusbcfg_local);
22013 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
22014 + core_if->hr_backup->hcfg_local);
22015 +
22016 + /* De-assert Wakeup Logic */
22017 + gpwrdn.d32 = 0;
22018 + gpwrdn.b.pmuactv = 1;
22019 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22020 + dwc_udelay(10);
22021 +
22022 + /* Start the Resume operation by programming HPRT0 */
22023 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22024 + hprt0.b.prtpwr = 1;
22025 + hprt0.b.prtena = 0;
22026 + hprt0.b.prtsusp = 0;
22027 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22028 +
22029 + DWC_PRINTF("Resume Starts Now\n");
22030 + if (!reset) { // Indicates it is Resume Operation
22031 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22032 + hprt0.b.prtres = 1;
22033 + hprt0.b.prtpwr = 1;
22034 + hprt0.b.prtena = 0;
22035 + hprt0.b.prtsusp = 0;
22036 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22037 +
22038 + if (!rem_wakeup)
22039 + hprt0.b.prtres = 0;
22040 + /* Wait for Resume time and then program HPRT again */
22041 + dwc_mdelay(100);
22042 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22043 +
22044 + } else { // Indicates it is Reset Operation
22045 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22046 + hprt0.b.prtrst = 1;
22047 + hprt0.b.prtpwr = 1;
22048 + hprt0.b.prtena = 0;
22049 + hprt0.b.prtsusp = 0;
22050 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22051 + /* Wait for Reset time and then program HPRT again */
22052 + dwc_mdelay(60);
22053 + hprt0.b.prtrst = 0;
22054 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22055 + }
22056 + /* Clear all interrupt status */
22057 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
22058 + hprt0.b.prtconndet = 1;
22059 + hprt0.b.prtenchng = 1;
22060 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22061 +
22062 + /* Clear all pending interupts */
22063 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22064 +
22065 + /* Restore global registers */
22066 + dwc_otg_restore_global_regs(core_if);
22067 + /* Restore host global registers */
22068 + dwc_otg_restore_host_regs(core_if, reset);
22069 +
22070 + /* The core will be in ON STATE */
22071 + core_if->lx_state = DWC_OTG_L0;
22072 + DWC_PRINTF("Hibernation recovery is complete here\n");
22073 + return 0;
22074 +}
22075 +
22076 +/** Saves some register values into system memory. */
22077 +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
22078 +{
22079 + struct dwc_otg_global_regs_backup *gr;
22080 + int i;
22081 +
22082 + gr = core_if->gr_backup;
22083 + if (!gr) {
22084 + gr = DWC_ALLOC(sizeof(*gr));
22085 + if (!gr) {
22086 + return -DWC_E_NO_MEMORY;
22087 + }
22088 + core_if->gr_backup = gr;
22089 + }
22090 +
22091 + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
22092 + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
22093 + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
22094 + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
22095 + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
22096 + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
22097 + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
22098 +#ifdef CONFIG_USB_DWC_OTG_LPM
22099 + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
22100 +#endif
22101 + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
22102 + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
22103 + gr->gdfifocfg_local =
22104 + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
22105 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
22106 + gr->dtxfsiz_local[i] =
22107 + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
22108 + }
22109 +
22110 + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
22111 + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
22112 + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
22113 + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
22114 + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
22115 + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
22116 + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
22117 + gr->gnptxfsiz_local);
22118 + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
22119 + gr->hptxfsiz_local);
22120 +#ifdef CONFIG_USB_DWC_OTG_LPM
22121 + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
22122 +#endif
22123 + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
22124 + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
22125 + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
22126 +
22127 + return 0;
22128 +}
22129 +
22130 +/** Saves GINTMSK register before setting the msk bits. */
22131 +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
22132 +{
22133 + struct dwc_otg_global_regs_backup *gr;
22134 +
22135 + gr = core_if->gr_backup;
22136 + if (!gr) {
22137 + gr = DWC_ALLOC(sizeof(*gr));
22138 + if (!gr) {
22139 + return -DWC_E_NO_MEMORY;
22140 + }
22141 + core_if->gr_backup = gr;
22142 + }
22143 +
22144 + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
22145 +
22146 + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
22147 + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
22148 +
22149 + return 0;
22150 +}
22151 +
22152 +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
22153 +{
22154 + struct dwc_otg_dev_regs_backup *dr;
22155 + int i;
22156 +
22157 + dr = core_if->dr_backup;
22158 + if (!dr) {
22159 + dr = DWC_ALLOC(sizeof(*dr));
22160 + if (!dr) {
22161 + return -DWC_E_NO_MEMORY;
22162 + }
22163 + core_if->dr_backup = dr;
22164 + }
22165 +
22166 + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
22167 + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
22168 + dr->daintmsk =
22169 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
22170 + dr->diepmsk =
22171 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
22172 + dr->doepmsk =
22173 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
22174 +
22175 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22176 + dr->diepctl[i] =
22177 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
22178 + dr->dieptsiz[i] =
22179 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
22180 + dr->diepdma[i] =
22181 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
22182 + }
22183 +
22184 + DWC_DEBUGPL(DBG_ANY,
22185 + "=============Backing Host registers==============\n");
22186 + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
22187 + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
22188 + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
22189 + dr->daintmsk);
22190 + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
22191 + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
22192 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22193 + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
22194 + dr->diepctl[i]);
22195 + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
22196 + i, dr->dieptsiz[i]);
22197 + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
22198 + dr->diepdma[i]);
22199 + }
22200 +
22201 + return 0;
22202 +}
22203 +
22204 +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
22205 +{
22206 + struct dwc_otg_host_regs_backup *hr;
22207 + int i;
22208 +
22209 + hr = core_if->hr_backup;
22210 + if (!hr) {
22211 + hr = DWC_ALLOC(sizeof(*hr));
22212 + if (!hr) {
22213 + return -DWC_E_NO_MEMORY;
22214 + }
22215 + core_if->hr_backup = hr;
22216 + }
22217 +
22218 + hr->hcfg_local =
22219 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
22220 + hr->haintmsk_local =
22221 + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
22222 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22223 + hr->hcintmsk_local[i] =
22224 + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
22225 + }
22226 + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
22227 + hr->hfir_local =
22228 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
22229 +
22230 + DWC_DEBUGPL(DBG_ANY,
22231 + "=============Backing Host registers===============\n");
22232 + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
22233 + hr->hcfg_local);
22234 + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
22235 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22236 + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
22237 + hr->hcintmsk_local[i]);
22238 + }
22239 + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
22240 + hr->hprt0_local);
22241 + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
22242 + hr->hfir_local);
22243 +
22244 + return 0;
22245 +}
22246 +
22247 +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
22248 +{
22249 + struct dwc_otg_global_regs_backup *gr;
22250 + int i;
22251 +
22252 + gr = core_if->gr_backup;
22253 + if (!gr) {
22254 + return -DWC_E_INVALID;
22255 + }
22256 +
22257 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
22258 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
22259 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
22260 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
22261 + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
22262 + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
22263 + gr->gnptxfsiz_local);
22264 + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
22265 + gr->hptxfsiz_local);
22266 + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
22267 + gr->gdfifocfg_local);
22268 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
22269 + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
22270 + gr->dtxfsiz_local[i]);
22271 + }
22272 +
22273 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22274 + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
22275 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
22276 + (gr->gahbcfg_local));
22277 + return 0;
22278 +}
22279 +
22280 +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
22281 +{
22282 + struct dwc_otg_dev_regs_backup *dr;
22283 + int i;
22284 +
22285 + dr = core_if->dr_backup;
22286 +
22287 + if (!dr) {
22288 + return -DWC_E_INVALID;
22289 + }
22290 +
22291 + if (!rem_wakeup) {
22292 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
22293 + dr->dctl);
22294 + }
22295 +
22296 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
22297 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
22298 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
22299 +
22300 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22301 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
22302 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
22303 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
22304 + }
22305 +
22306 + return 0;
22307 +}
22308 +
22309 +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
22310 +{
22311 + struct dwc_otg_host_regs_backup *hr;
22312 + int i;
22313 + hr = core_if->hr_backup;
22314 +
22315 + if (!hr) {
22316 + return -DWC_E_INVALID;
22317 + }
22318 +
22319 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
22320 + //if (!reset)
22321 + //{
22322 + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
22323 + //}
22324 +
22325 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
22326 + hr->haintmsk_local);
22327 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22328 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
22329 + hr->hcintmsk_local[i]);
22330 + }
22331 +
22332 + return 0;
22333 +}
22334 +
22335 +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
22336 +{
22337 + struct dwc_otg_global_regs_backup *gr;
22338 +
22339 + gr = core_if->gr_backup;
22340 +
22341 + /* Restore values for LPM and I2C */
22342 +#ifdef CONFIG_USB_DWC_OTG_LPM
22343 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
22344 +#endif
22345 + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
22346 +
22347 + return 0;
22348 +}
22349 +
22350 +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
22351 +{
22352 + struct dwc_otg_global_regs_backup *gr;
22353 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
22354 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
22355 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
22356 + gintmsk_data_t gintmsk = {.d32 = 0 };
22357 +
22358 + /* Restore LPM and I2C registers */
22359 + restore_lpm_i2c_regs(core_if);
22360 +
22361 + /* Set PCGCCTL to 0 */
22362 + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
22363 +
22364 + gr = core_if->gr_backup;
22365 + /* Load restore values for [31:14] bits */
22366 + DWC_WRITE_REG32(core_if->pcgcctl,
22367 + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
22368 +
22369 + /* Umnask global Interrupt in GAHBCFG and restore it */
22370 + gahbcfg.d32 = gr->gahbcfg_local;
22371 + gahbcfg.b.glblintrmsk = 1;
22372 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
22373 +
22374 + /* Clear all pending interupts */
22375 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22376 +
22377 + /* Unmask restore done interrupt */
22378 + gintmsk.b.restoredone = 1;
22379 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
22380 +
22381 + /* Restore GUSBCFG and HCFG/DCFG */
22382 + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
22383 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
22384 +
22385 + if (is_host) {
22386 + hcfg_data_t hcfg = {.d32 = 0 };
22387 + hcfg.d32 = core_if->hr_backup->hcfg_local;
22388 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
22389 + hcfg.d32);
22390 +
22391 + /* Load restore values for [31:14] bits */
22392 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22393 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22394 +
22395 + if (rmode)
22396 + pcgcctl.b.restoremode = 1;
22397 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22398 + dwc_udelay(10);
22399 +
22400 + /* Load restore values for [31:14] bits and set EssRegRestored bit */
22401 + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
22402 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22403 + pcgcctl.b.ess_reg_restored = 1;
22404 + if (rmode)
22405 + pcgcctl.b.restoremode = 1;
22406 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22407 + } else {
22408 + dcfg_data_t dcfg = {.d32 = 0 };
22409 + dcfg.d32 = core_if->dr_backup->dcfg;
22410 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
22411 +
22412 + /* Load restore values for [31:14] bits */
22413 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22414 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22415 + if (!rmode) {
22416 + pcgcctl.d32 |= 0x208;
22417 + }
22418 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22419 + dwc_udelay(10);
22420 +
22421 + /* Load restore values for [31:14] bits */
22422 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22423 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22424 + pcgcctl.b.ess_reg_restored = 1;
22425 + if (!rmode)
22426 + pcgcctl.d32 |= 0x208;
22427 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22428 + }
22429 +
22430 + return 0;
22431 +}
22432 +
22433 +/**
22434 + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
22435 + * type.
22436 + */
22437 +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
22438 +{
22439 + uint32_t val;
22440 + hcfg_data_t hcfg;
22441 +
22442 + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
22443 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22444 + (core_if->core_params->ulpi_fs_ls)) ||
22445 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22446 + /* Full speed PHY */
22447 + val = DWC_HCFG_48_MHZ;
22448 + } else {
22449 + /* High speed PHY running at full speed or high speed */
22450 + val = DWC_HCFG_30_60_MHZ;
22451 + }
22452 +
22453 + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
22454 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
22455 + hcfg.b.fslspclksel = val;
22456 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
22457 +}
22458 +
22459 +/**
22460 + * Initializes the DevSpd field of the DCFG register depending on the PHY type
22461 + * and the enumeration speed of the device.
22462 + */
22463 +static void init_devspd(dwc_otg_core_if_t * core_if)
22464 +{
22465 + uint32_t val;
22466 + dcfg_data_t dcfg;
22467 +
22468 + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
22469 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22470 + (core_if->core_params->ulpi_fs_ls)) ||
22471 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22472 + /* Full speed PHY */
22473 + val = 0x3;
22474 + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
22475 + /* High speed PHY running at full speed */
22476 + val = 0x1;
22477 + } else {
22478 + /* High speed PHY running at high speed */
22479 + val = 0x0;
22480 + }
22481 +
22482 + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
22483 +
22484 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
22485 + dcfg.b.devspd = val;
22486 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
22487 +}
22488 +
22489 +/**
22490 + * This function calculates the number of IN EPS
22491 + * using GHWCFG1 and GHWCFG2 registers values
22492 + *
22493 + * @param core_if Programming view of the DWC_otg controller
22494 + */
22495 +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
22496 +{
22497 + uint32_t num_in_eps = 0;
22498 + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
22499 + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
22500 + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
22501 + int i;
22502 +
22503 + for (i = 0; i < num_eps; ++i) {
22504 + if (!(hwcfg1 & 0x1))
22505 + num_in_eps++;
22506 +
22507 + hwcfg1 >>= 2;
22508 + }
22509 +
22510 + if (core_if->hwcfg4.b.ded_fifo_en) {
22511 + num_in_eps =
22512 + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
22513 + }
22514 +
22515 + return num_in_eps;
22516 +}
22517 +
22518 +/**
22519 + * This function calculates the number of OUT EPS
22520 + * using GHWCFG1 and GHWCFG2 registers values
22521 + *
22522 + * @param core_if Programming view of the DWC_otg controller
22523 + */
22524 +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
22525 +{
22526 + uint32_t num_out_eps = 0;
22527 + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
22528 + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
22529 + int i;
22530 +
22531 + for (i = 0; i < num_eps; ++i) {
22532 + if (!(hwcfg1 & 0x1))
22533 + num_out_eps++;
22534 +
22535 + hwcfg1 >>= 2;
22536 + }
22537 + return num_out_eps;
22538 +}
22539 +
22540 +/**
22541 + * This function initializes the DWC_otg controller registers and
22542 + * prepares the core for device mode or host mode operation.
22543 + *
22544 + * @param core_if Programming view of the DWC_otg controller
22545 + *
22546 + */
22547 +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
22548 +{
22549 + int i = 0;
22550 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
22551 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
22552 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
22553 + gusbcfg_data_t usbcfg = {.d32 = 0 };
22554 + gi2cctl_data_t i2cctl = {.d32 = 0 };
22555 +
22556 + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
22557 + core_if, global_regs);
22558 +
22559 + /* Common Initialization */
22560 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22561 +
22562 + /* Program the ULPI External VBUS bit if needed */
22563 + usbcfg.b.ulpi_ext_vbus_drv =
22564 + (core_if->core_params->phy_ulpi_ext_vbus ==
22565 + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
22566 +
22567 + /* Set external TS Dline pulsing */
22568 + usbcfg.b.term_sel_dl_pulse =
22569 + (core_if->core_params->ts_dline == 1) ? 1 : 0;
22570 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22571 +
22572 + /* Reset the Controller */
22573 + dwc_otg_core_reset(core_if);
22574 +
22575 + core_if->adp_enable = core_if->core_params->adp_supp_enable;
22576 + core_if->power_down = core_if->core_params->power_down;
22577 + core_if->otg_sts = 0;
22578 +
22579 + /* Initialize parameters from Hardware configuration registers. */
22580 + dev_if->num_in_eps = calc_num_in_eps(core_if);
22581 + dev_if->num_out_eps = calc_num_out_eps(core_if);
22582 +
22583 + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
22584 + core_if->hwcfg4.b.num_dev_perio_in_ep);
22585 +
22586 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
22587 + dev_if->perio_tx_fifo_size[i] =
22588 + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
22589 + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
22590 + i, dev_if->perio_tx_fifo_size[i]);
22591 + }
22592 +
22593 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
22594 + dev_if->tx_fifo_size[i] =
22595 + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
22596 + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
22597 + i, dev_if->tx_fifo_size[i]);
22598 + }
22599 +
22600 + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
22601 + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
22602 + core_if->nperio_tx_fifo_size =
22603 + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
22604 +
22605 + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
22606 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
22607 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
22608 + core_if->nperio_tx_fifo_size);
22609 +
22610 + /* This programming sequence needs to happen in FS mode before any other
22611 + * programming occurs */
22612 + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
22613 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22614 + /* If FS mode with FS PHY */
22615 +
22616 + /* core_init() is now called on every switch so only call the
22617 + * following for the first time through. */
22618 + if (!core_if->phy_init_done) {
22619 + core_if->phy_init_done = 1;
22620 + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
22621 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22622 + usbcfg.b.physel = 1;
22623 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22624 +
22625 + /* Reset after a PHY select */
22626 + dwc_otg_core_reset(core_if);
22627 + }
22628 +
22629 + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
22630 + * do this on HNP Dev/Host mode switches (done in dev_init and
22631 + * host_init). */
22632 + if (dwc_otg_is_host_mode(core_if)) {
22633 + init_fslspclksel(core_if);
22634 + } else {
22635 + init_devspd(core_if);
22636 + }
22637 +
22638 + if (core_if->core_params->i2c_enable) {
22639 + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
22640 + /* Program GUSBCFG.OtgUtmifsSel to I2C */
22641 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22642 + usbcfg.b.otgutmifssel = 1;
22643 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22644 +
22645 + /* Program GI2CCTL.I2CEn */
22646 + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
22647 + i2cctl.b.i2cdevaddr = 1;
22648 + i2cctl.b.i2cen = 0;
22649 + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
22650 + i2cctl.b.i2cen = 1;
22651 + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
22652 + }
22653 +
22654 + } /* endif speed == DWC_SPEED_PARAM_FULL */
22655 + else {
22656 + /* High speed PHY. */
22657 + if (!core_if->phy_init_done) {
22658 + core_if->phy_init_done = 1;
22659 + /* HS PHY parameters. These parameters are preserved
22660 + * during soft reset so only program the first time. Do
22661 + * a soft reset immediately after setting phyif. */
22662 +
22663 + if (core_if->core_params->phy_type == 2) {
22664 + /* ULPI interface */
22665 + usbcfg.b.ulpi_utmi_sel = 1;
22666 + usbcfg.b.phyif = 0;
22667 + usbcfg.b.ddrsel =
22668 + core_if->core_params->phy_ulpi_ddr;
22669 + } else if (core_if->core_params->phy_type == 1) {
22670 + /* UTMI+ interface */
22671 + usbcfg.b.ulpi_utmi_sel = 0;
22672 + if (core_if->core_params->phy_utmi_width == 16) {
22673 + usbcfg.b.phyif = 1;
22674 +
22675 + } else {
22676 + usbcfg.b.phyif = 0;
22677 + }
22678 + } else {
22679 + DWC_ERROR("FS PHY TYPE\n");
22680 + }
22681 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22682 + /* Reset after setting the PHY parameters */
22683 + dwc_otg_core_reset(core_if);
22684 + }
22685 + }
22686 +
22687 + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
22688 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22689 + (core_if->core_params->ulpi_fs_ls)) {
22690 + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
22691 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22692 + usbcfg.b.ulpi_fsls = 1;
22693 + usbcfg.b.ulpi_clk_sus_m = 1;
22694 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22695 + } else {
22696 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22697 + usbcfg.b.ulpi_fsls = 0;
22698 + usbcfg.b.ulpi_clk_sus_m = 0;
22699 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22700 + }
22701 +
22702 + /* Program the GAHBCFG Register. */
22703 + switch (core_if->hwcfg2.b.architecture) {
22704 +
22705 + case DWC_SLAVE_ONLY_ARCH:
22706 + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
22707 + ahbcfg.b.nptxfemplvl_txfemplvl =
22708 + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
22709 + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
22710 + core_if->dma_enable = 0;
22711 + core_if->dma_desc_enable = 0;
22712 + break;
22713 +
22714 + case DWC_EXT_DMA_ARCH:
22715 + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
22716 + {
22717 + uint8_t brst_sz = core_if->core_params->dma_burst_size;
22718 + ahbcfg.b.hburstlen = 0;
22719 + while (brst_sz > 1) {
22720 + ahbcfg.b.hburstlen++;
22721 + brst_sz >>= 1;
22722 + }
22723 + }
22724 + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
22725 + core_if->dma_desc_enable =
22726 + (core_if->core_params->dma_desc_enable != 0);
22727 + break;
22728 +
22729 + case DWC_INT_DMA_ARCH:
22730 + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
22731 + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
22732 + Host mode ISOC in issue fix - vahrama */
22733 + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
22734 + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
22735 + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
22736 + core_if->dma_desc_enable =
22737 + (core_if->core_params->dma_desc_enable != 0);
22738 + break;
22739 +
22740 + }
22741 + if (core_if->dma_enable) {
22742 + if (core_if->dma_desc_enable) {
22743 + DWC_PRINTF("Using Descriptor DMA mode\n");
22744 + } else {
22745 + DWC_PRINTF("Using Buffer DMA mode\n");
22746 +
22747 + }
22748 + } else {
22749 + DWC_PRINTF("Using Slave mode\n");
22750 + core_if->dma_desc_enable = 0;
22751 + }
22752 +
22753 + if (core_if->core_params->ahb_single) {
22754 + ahbcfg.b.ahbsingle = 1;
22755 + }
22756 +
22757 + ahbcfg.b.dmaenable = core_if->dma_enable;
22758 + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
22759 +
22760 + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
22761 +
22762 + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
22763 + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
22764 + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
22765 + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
22766 + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
22767 + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
22768 +
22769 + /*
22770 + * Program the GUSBCFG register.
22771 + */
22772 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22773 +
22774 + switch (core_if->hwcfg2.b.op_mode) {
22775 + case DWC_MODE_HNP_SRP_CAPABLE:
22776 + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
22777 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
22778 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
22779 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
22780 + break;
22781 +
22782 + case DWC_MODE_SRP_ONLY_CAPABLE:
22783 + usbcfg.b.hnpcap = 0;
22784 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
22785 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
22786 + break;
22787 +
22788 + case DWC_MODE_NO_HNP_SRP_CAPABLE:
22789 + usbcfg.b.hnpcap = 0;
22790 + usbcfg.b.srpcap = 0;
22791 + break;
22792 +
22793 + case DWC_MODE_SRP_CAPABLE_DEVICE:
22794 + usbcfg.b.hnpcap = 0;
22795 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
22796 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
22797 + break;
22798 +
22799 + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
22800 + usbcfg.b.hnpcap = 0;
22801 + usbcfg.b.srpcap = 0;
22802 + break;
22803 +
22804 + case DWC_MODE_SRP_CAPABLE_HOST:
22805 + usbcfg.b.hnpcap = 0;
22806 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
22807 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
22808 + break;
22809 +
22810 + case DWC_MODE_NO_SRP_CAPABLE_HOST:
22811 + usbcfg.b.hnpcap = 0;
22812 + usbcfg.b.srpcap = 0;
22813 + break;
22814 + }
22815 +
22816 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22817 +
22818 +#ifdef CONFIG_USB_DWC_OTG_LPM
22819 + if (core_if->core_params->lpm_enable) {
22820 + glpmcfg_data_t lpmcfg = {.d32 = 0 };
22821 +
22822 + /* To enable LPM support set lpm_cap_en bit */
22823 + lpmcfg.b.lpm_cap_en = 1;
22824 +
22825 + /* Make AppL1Res ACK */
22826 + lpmcfg.b.appl_resp = 1;
22827 +
22828 + /* Retry 3 times */
22829 + lpmcfg.b.retry_count = 3;
22830 +
22831 + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
22832 + 0, lpmcfg.d32);
22833 +
22834 + }
22835 +#endif
22836 + if (core_if->core_params->ic_usb_cap) {
22837 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
22838 + gusbcfg.b.ic_usb_cap = 1;
22839 + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
22840 + 0, gusbcfg.d32);
22841 + }
22842 + {
22843 + gotgctl_data_t gotgctl = {.d32 = 0 };
22844 + gotgctl.b.otgver = core_if->core_params->otg_ver;
22845 + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
22846 + gotgctl.d32);
22847 + /* Set OTG version supported */
22848 + core_if->otg_ver = core_if->core_params->otg_ver;
22849 + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
22850 + core_if->core_params->otg_ver, core_if->otg_ver);
22851 + }
22852 +
22853 +
22854 + /* Enable common interrupts */
22855 + dwc_otg_enable_common_interrupts(core_if);
22856 +
22857 + /* Do device or host intialization based on mode during PCD
22858 + * and HCD initialization */
22859 + if (dwc_otg_is_host_mode(core_if)) {
22860 + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
22861 + core_if->op_state = A_HOST;
22862 + } else {
22863 + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
22864 + core_if->op_state = B_PERIPHERAL;
22865 +#ifdef DWC_DEVICE_ONLY
22866 + dwc_otg_core_dev_init(core_if);
22867 +#endif
22868 + }
22869 +}
22870 +
22871 +/**
22872 + * This function enables the Device mode interrupts.
22873 + *
22874 + * @param core_if Programming view of DWC_otg controller
22875 + */
22876 +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
22877 +{
22878 + gintmsk_data_t intr_mask = {.d32 = 0 };
22879 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
22880 +
22881 + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
22882 +
22883 + /* Disable all interrupts. */
22884 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
22885 +
22886 + /* Clear any pending interrupts */
22887 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
22888 +
22889 + /* Enable the common interrupts */
22890 + dwc_otg_enable_common_interrupts(core_if);
22891 +
22892 + /* Enable interrupts */
22893 + intr_mask.b.usbreset = 1;
22894 + intr_mask.b.enumdone = 1;
22895 + /* Disable Disconnect interrupt in Device mode */
22896 + intr_mask.b.disconnect = 0;
22897 +
22898 + if (!core_if->multiproc_int_enable) {
22899 + intr_mask.b.inepintr = 1;
22900 + intr_mask.b.outepintr = 1;
22901 + }
22902 +
22903 + intr_mask.b.erlysuspend = 1;
22904 +
22905 + if (core_if->en_multiple_tx_fifo == 0) {
22906 + intr_mask.b.epmismatch = 1;
22907 + }
22908 +
22909 + //intr_mask.b.incomplisoout = 1;
22910 + intr_mask.b.incomplisoin = 1;
22911 +
22912 +/* Enable the ignore frame number for ISOC xfers - MAS */
22913 +/* Disable to support high bandwith ISOC transfers - manukz */
22914 +#if 0
22915 +#ifdef DWC_UTE_PER_IO
22916 + if (core_if->dma_enable) {
22917 + if (core_if->dma_desc_enable) {
22918 + dctl_data_t dctl1 = {.d32 = 0 };
22919 + dctl1.b.ifrmnum = 1;
22920 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
22921 + dctl, 0, dctl1.d32);
22922 + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
22923 + DWC_READ_REG32(&core_if->dev_if->
22924 + dev_global_regs->dctl));
22925 + }
22926 + }
22927 +#endif
22928 +#endif
22929 +#ifdef DWC_EN_ISOC
22930 + if (core_if->dma_enable) {
22931 + if (core_if->dma_desc_enable == 0) {
22932 + if (core_if->pti_enh_enable) {
22933 + dctl_data_t dctl = {.d32 = 0 };
22934 + dctl.b.ifrmnum = 1;
22935 + DWC_MODIFY_REG32(&core_if->
22936 + dev_if->dev_global_regs->dctl,
22937 + 0, dctl.d32);
22938 + } else {
22939 + intr_mask.b.incomplisoin = 1;
22940 + intr_mask.b.incomplisoout = 1;
22941 + }
22942 + }
22943 + } else {
22944 + intr_mask.b.incomplisoin = 1;
22945 + intr_mask.b.incomplisoout = 1;
22946 + }
22947 +#endif /* DWC_EN_ISOC */
22948 +
22949 + /** @todo NGS: Should this be a module parameter? */
22950 +#ifdef USE_PERIODIC_EP
22951 + intr_mask.b.isooutdrop = 1;
22952 + intr_mask.b.eopframe = 1;
22953 + intr_mask.b.incomplisoin = 1;
22954 + intr_mask.b.incomplisoout = 1;
22955 +#endif
22956 +
22957 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
22958 +
22959 + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
22960 + DWC_READ_REG32(&global_regs->gintmsk));
22961 +}
22962 +
22963 +/**
22964 + * This function initializes the DWC_otg controller registers for
22965 + * device mode.
22966 + *
22967 + * @param core_if Programming view of DWC_otg controller
22968 + *
22969 + */
22970 +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
22971 +{
22972 + int i;
22973 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
22974 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
22975 + dwc_otg_core_params_t *params = core_if->core_params;
22976 + dcfg_data_t dcfg = {.d32 = 0 };
22977 + depctl_data_t diepctl = {.d32 = 0 };
22978 + grstctl_t resetctl = {.d32 = 0 };
22979 + uint32_t rx_fifo_size;
22980 + fifosize_data_t nptxfifosize;
22981 + fifosize_data_t txfifosize;
22982 + dthrctl_data_t dthrctl;
22983 + fifosize_data_t ptxfifosize;
22984 + uint16_t rxfsiz, nptxfsiz;
22985 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
22986 + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
22987 +
22988 + /* Restart the Phy Clock */
22989 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
22990 +
22991 + /* Device configuration register */
22992 + init_devspd(core_if);
22993 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
22994 + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
22995 + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
22996 + /* Enable Device OUT NAK in case of DDMA mode*/
22997 + if (core_if->core_params->dev_out_nak) {
22998 + dcfg.b.endevoutnak = 1;
22999 + }
23000 +
23001 + if (core_if->core_params->cont_on_bna) {
23002 + dctl_data_t dctl = {.d32 = 0 };
23003 + dctl.b.encontonbna = 1;
23004 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
23005 + }
23006 +
23007 +
23008 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
23009 +
23010 + /* Configure data FIFO sizes */
23011 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
23012 + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
23013 + core_if->total_fifo_size);
23014 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
23015 + params->dev_rx_fifo_size);
23016 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
23017 + params->dev_nperio_tx_fifo_size);
23018 +
23019 + /* Rx FIFO */
23020 + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
23021 + DWC_READ_REG32(&global_regs->grxfsiz));
23022 +
23023 +#ifdef DWC_UTE_CFI
23024 + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
23025 + core_if->init_rxfsiz = params->dev_rx_fifo_size;
23026 +#endif
23027 + rx_fifo_size = params->dev_rx_fifo_size;
23028 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
23029 +
23030 + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
23031 + DWC_READ_REG32(&global_regs->grxfsiz));
23032 +
23033 + /** Set Periodic Tx FIFO Mask all bits 0 */
23034 + core_if->p_tx_msk = 0;
23035 +
23036 + /** Set Tx FIFO Mask all bits 0 */
23037 + core_if->tx_msk = 0;
23038 +
23039 + if (core_if->en_multiple_tx_fifo == 0) {
23040 + /* Non-periodic Tx FIFO */
23041 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23042 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23043 +
23044 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
23045 + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
23046 +
23047 + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
23048 + nptxfifosize.d32);
23049 +
23050 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23051 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23052 +
23053 + /**@todo NGS: Fix Periodic FIFO Sizing! */
23054 + /*
23055 + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
23056 + * Indexes of the FIFO size module parameters in the
23057 + * dev_perio_tx_fifo_size array and the FIFO size registers in
23058 + * the dptxfsiz array run from 0 to 14.
23059 + */
23060 + /** @todo Finish debug of this */
23061 + ptxfifosize.b.startaddr =
23062 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23063 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
23064 + ptxfifosize.b.depth =
23065 + params->dev_perio_tx_fifo_size[i];
23066 + DWC_DEBUGPL(DBG_CIL,
23067 + "initial dtxfsiz[%d]=%08x\n", i,
23068 + DWC_READ_REG32(&global_regs->dtxfsiz
23069 + [i]));
23070 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
23071 + ptxfifosize.d32);
23072 + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
23073 + i,
23074 + DWC_READ_REG32(&global_regs->dtxfsiz
23075 + [i]));
23076 + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
23077 + }
23078 + } else {
23079 + /*
23080 + * Tx FIFOs These FIFOs are numbered from 1 to 15.
23081 + * Indexes of the FIFO size module parameters in the
23082 + * dev_tx_fifo_size array and the FIFO size registers in
23083 + * the dtxfsiz array run from 0 to 14.
23084 + */
23085 +
23086 + /* Non-periodic Tx FIFO */
23087 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23088 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23089 +
23090 +#ifdef DWC_UTE_CFI
23091 + core_if->pwron_gnptxfsiz =
23092 + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23093 + core_if->init_gnptxfsiz =
23094 + params->dev_nperio_tx_fifo_size;
23095 +#endif
23096 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
23097 + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
23098 +
23099 + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
23100 + nptxfifosize.d32);
23101 +
23102 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23103 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23104 +
23105 + txfifosize.b.startaddr =
23106 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23107 +
23108 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
23109 +
23110 + txfifosize.b.depth =
23111 + params->dev_tx_fifo_size[i];
23112 +
23113 + DWC_DEBUGPL(DBG_CIL,
23114 + "initial dtxfsiz[%d]=%08x\n",
23115 + i,
23116 + DWC_READ_REG32(&global_regs->dtxfsiz
23117 + [i]));
23118 +
23119 +#ifdef DWC_UTE_CFI
23120 + core_if->pwron_txfsiz[i] =
23121 + (DWC_READ_REG32
23122 + (&global_regs->dtxfsiz[i]) >> 16);
23123 + core_if->init_txfsiz[i] =
23124 + params->dev_tx_fifo_size[i];
23125 +#endif
23126 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
23127 + txfifosize.d32);
23128 +
23129 + DWC_DEBUGPL(DBG_CIL,
23130 + "new dtxfsiz[%d]=%08x\n",
23131 + i,
23132 + DWC_READ_REG32(&global_regs->dtxfsiz
23133 + [i]));
23134 +
23135 + txfifosize.b.startaddr += txfifosize.b.depth;
23136 + }
23137 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
23138 + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
23139 + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
23140 + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
23141 + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
23142 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23143 + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
23144 + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23145 + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
23146 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23147 + }
23148 + }
23149 +
23150 + /* Flush the FIFOs */
23151 + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
23152 + dwc_otg_flush_rx_fifo(core_if);
23153 +
23154 + /* Flush the Learning Queue. */
23155 + resetctl.b.intknqflsh = 1;
23156 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
23157 +
23158 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
23159 + core_if->start_predict = 0;
23160 + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
23161 + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
23162 + }
23163 + core_if->nextep_seq[0] = 0;
23164 + core_if->first_in_nextep_seq = 0;
23165 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
23166 + diepctl.b.nextep = 0;
23167 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
23168 +
23169 + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
23170 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
23171 + dcfg.b.epmscnt = 2;
23172 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
23173 +
23174 + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
23175 + __func__, core_if->first_in_nextep_seq);
23176 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
23177 + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
23178 + }
23179 + DWC_DEBUGPL(DBG_CILV,"\n");
23180 + }
23181 +
23182 + /* Clear all pending Device Interrupts */
23183 + /** @todo - if the condition needed to be checked
23184 + * or in any case all pending interrutps should be cleared?
23185 + */
23186 + if (core_if->multiproc_int_enable) {
23187 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
23188 + DWC_WRITE_REG32(&dev_if->
23189 + dev_global_regs->diepeachintmsk[i], 0);
23190 + }
23191 + }
23192 +
23193 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
23194 + DWC_WRITE_REG32(&dev_if->
23195 + dev_global_regs->doepeachintmsk[i], 0);
23196 + }
23197 +
23198 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
23199 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
23200 + } else {
23201 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
23202 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
23203 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
23204 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
23205 + }
23206 +
23207 + for (i = 0; i <= dev_if->num_in_eps; i++) {
23208 + depctl_data_t depctl;
23209 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
23210 + if (depctl.b.epena) {
23211 + depctl.d32 = 0;
23212 + depctl.b.epdis = 1;
23213 + depctl.b.snak = 1;
23214 + } else {
23215 + depctl.d32 = 0;
23216 + }
23217 +
23218 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
23219 +
23220 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
23221 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
23222 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
23223 + }
23224 +
23225 + for (i = 0; i <= dev_if->num_out_eps; i++) {
23226 + depctl_data_t depctl;
23227 + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
23228 + if (depctl.b.epena) {
23229 + dctl_data_t dctl = {.d32 = 0 };
23230 + gintmsk_data_t gintsts = {.d32 = 0 };
23231 + doepint_data_t doepint = {.d32 = 0 };
23232 + dctl.b.sgoutnak = 1;
23233 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
23234 + do {
23235 + dwc_udelay(10);
23236 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
23237 + } while (!gintsts.b.goutnakeff);
23238 + gintsts.d32 = 0;
23239 + gintsts.b.goutnakeff = 1;
23240 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
23241 +
23242 + depctl.d32 = 0;
23243 + depctl.b.epdis = 1;
23244 + depctl.b.snak = 1;
23245 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
23246 + do {
23247 + dwc_udelay(10);
23248 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
23249 + out_ep_regs[i]->doepint);
23250 + } while (!doepint.b.epdisabled);
23251 +
23252 + doepint.b.epdisabled = 1;
23253 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
23254 +
23255 + dctl.d32 = 0;
23256 + dctl.b.cgoutnak = 1;
23257 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
23258 + } else {
23259 + depctl.d32 = 0;
23260 + }
23261 +
23262 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
23263 +
23264 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
23265 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
23266 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
23267 + }
23268 +
23269 + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
23270 + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
23271 + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
23272 + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
23273 +
23274 + dev_if->rx_thr_length = params->rx_thr_length;
23275 + dev_if->tx_thr_length = params->tx_thr_length;
23276 +
23277 + dev_if->setup_desc_index = 0;
23278 +
23279 + dthrctl.d32 = 0;
23280 + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
23281 + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
23282 + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
23283 + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
23284 + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
23285 + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
23286 +
23287 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
23288 + dthrctl.d32);
23289 +
23290 + DWC_DEBUGPL(DBG_CIL,
23291 + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
23292 + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
23293 + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
23294 + dthrctl.b.rx_thr_len);
23295 +
23296 + }
23297 +
23298 + dwc_otg_enable_device_interrupts(core_if);
23299 +
23300 + {
23301 + diepmsk_data_t msk = {.d32 = 0 };
23302 + msk.b.txfifoundrn = 1;
23303 + if (core_if->multiproc_int_enable) {
23304 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
23305 + diepeachintmsk[0], msk.d32, msk.d32);
23306 + } else {
23307 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
23308 + msk.d32, msk.d32);
23309 + }
23310 + }
23311 +
23312 + if (core_if->multiproc_int_enable) {
23313 + /* Set NAK on Babble */
23314 + dctl_data_t dctl = {.d32 = 0 };
23315 + dctl.b.nakonbble = 1;
23316 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
23317 + }
23318 +
23319 + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
23320 + dctl_data_t dctl = {.d32 = 0 };
23321 + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
23322 + dctl.b.sftdiscon = 0;
23323 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
23324 + }
23325 +}
23326 +
23327 +/**
23328 + * This function enables the Host mode interrupts.
23329 + *
23330 + * @param core_if Programming view of DWC_otg controller
23331 + */
23332 +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
23333 +{
23334 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23335 + gintmsk_data_t intr_mask = {.d32 = 0 };
23336 +
23337 + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
23338 +
23339 + /* Disable all interrupts. */
23340 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
23341 +
23342 + /* Clear any pending interrupts. */
23343 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
23344 +
23345 + /* Enable the common interrupts */
23346 + dwc_otg_enable_common_interrupts(core_if);
23347 +
23348 + /*
23349 + * Enable host mode interrupts without disturbing common
23350 + * interrupts.
23351 + */
23352 +
23353 + intr_mask.b.disconnect = 1;
23354 + intr_mask.b.portintr = 1;
23355 + intr_mask.b.hcintr = 1;
23356 +
23357 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
23358 +}
23359 +
23360 +/**
23361 + * This function disables the Host Mode interrupts.
23362 + *
23363 + * @param core_if Programming view of DWC_otg controller
23364 + */
23365 +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
23366 +{
23367 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23368 + gintmsk_data_t intr_mask = {.d32 = 0 };
23369 +
23370 + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
23371 +
23372 + /*
23373 + * Disable host mode interrupts without disturbing common
23374 + * interrupts.
23375 + */
23376 + intr_mask.b.sofintr = 1;
23377 + intr_mask.b.portintr = 1;
23378 + intr_mask.b.hcintr = 1;
23379 + intr_mask.b.ptxfempty = 1;
23380 + intr_mask.b.nptxfempty = 1;
23381 +
23382 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
23383 +}
23384 +
23385 +/**
23386 + * This function initializes the DWC_otg controller registers for
23387 + * host mode.
23388 + *
23389 + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
23390 + * request queues. Host channels are reset to ensure that they are ready for
23391 + * performing transfers.
23392 + *
23393 + * @param core_if Programming view of DWC_otg controller
23394 + *
23395 + */
23396 +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
23397 +{
23398 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23399 + dwc_otg_host_if_t *host_if = core_if->host_if;
23400 + dwc_otg_core_params_t *params = core_if->core_params;
23401 + hprt0_data_t hprt0 = {.d32 = 0 };
23402 + fifosize_data_t nptxfifosize;
23403 + fifosize_data_t ptxfifosize;
23404 + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
23405 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
23406 + int i;
23407 + hcchar_data_t hcchar;
23408 + hcfg_data_t hcfg;
23409 + hfir_data_t hfir;
23410 + dwc_otg_hc_regs_t *hc_regs;
23411 + int num_channels;
23412 + gotgctl_data_t gotgctl = {.d32 = 0 };
23413 +
23414 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
23415 +
23416 + /* Restart the Phy Clock */
23417 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
23418 +
23419 + /* Initialize Host Configuration Register */
23420 + init_fslspclksel(core_if);
23421 + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
23422 + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
23423 + hcfg.b.fslssupp = 1;
23424 + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
23425 +
23426 + }
23427 +
23428 + /* This bit allows dynamic reloading of the HFIR register
23429 + * during runtime. This bit needs to be programmed during
23430 + * initial configuration and its value must not be changed
23431 + * during runtime.*/
23432 + if (core_if->core_params->reload_ctl == 1) {
23433 + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
23434 + hfir.b.hfirrldctrl = 1;
23435 + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
23436 + }
23437 +
23438 + if (core_if->core_params->dma_desc_enable) {
23439 + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
23440 + if (!
23441 + (core_if->hwcfg4.b.desc_dma
23442 + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
23443 + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
23444 + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
23445 + || (op_mode ==
23446 + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
23447 + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
23448 + || (op_mode ==
23449 + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
23450 +
23451 + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
23452 + "Either core version is below 2.90a or "
23453 + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
23454 + "To run the driver in Buffer DMA host mode set dma_desc_enable "
23455 + "module parameter to 0.\n");
23456 + return;
23457 + }
23458 + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
23459 + hcfg.b.descdma = 1;
23460 + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
23461 + }
23462 +
23463 + /* Configure data FIFO sizes */
23464 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
23465 + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
23466 + core_if->total_fifo_size);
23467 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
23468 + params->host_rx_fifo_size);
23469 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
23470 + params->host_nperio_tx_fifo_size);
23471 + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
23472 + params->host_perio_tx_fifo_size);
23473 +
23474 + /* Rx FIFO */
23475 + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
23476 + DWC_READ_REG32(&global_regs->grxfsiz));
23477 + DWC_WRITE_REG32(&global_regs->grxfsiz,
23478 + params->host_rx_fifo_size);
23479 + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
23480 + DWC_READ_REG32(&global_regs->grxfsiz));
23481 +
23482 + /* Non-periodic Tx FIFO */
23483 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23484 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23485 + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
23486 + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
23487 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
23488 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23489 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23490 +
23491 + /* Periodic Tx FIFO */
23492 + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
23493 + DWC_READ_REG32(&global_regs->hptxfsiz));
23494 + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
23495 + ptxfifosize.b.startaddr =
23496 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23497 + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
23498 + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
23499 + DWC_READ_REG32(&global_regs->hptxfsiz));
23500 +
23501 + if (core_if->en_multiple_tx_fifo
23502 + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
23503 + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
23504 + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
23505 + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
23506 + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23507 + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
23508 + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
23509 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23510 + }
23511 + }
23512 +
23513 + /* TODO - check this */
23514 + /* Clear Host Set HNP Enable in the OTG Control Register */
23515 + gotgctl.b.hstsethnpen = 1;
23516 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
23517 + /* Make sure the FIFOs are flushed. */
23518 + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
23519 + dwc_otg_flush_rx_fifo(core_if);
23520 +
23521 + /* Clear Host Set HNP Enable in the OTG Control Register */
23522 + gotgctl.b.hstsethnpen = 1;
23523 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
23524 +
23525 + if (!core_if->core_params->dma_desc_enable) {
23526 + /* Flush out any leftover queued requests. */
23527 + num_channels = core_if->core_params->host_channels;
23528 +
23529 + for (i = 0; i < num_channels; i++) {
23530 + hc_regs = core_if->host_if->hc_regs[i];
23531 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23532 + hcchar.b.chen = 0;
23533 + hcchar.b.chdis = 1;
23534 + hcchar.b.epdir = 0;
23535 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23536 + }
23537 +
23538 + /* Halt all channels to put them into a known state. */
23539 + for (i = 0; i < num_channels; i++) {
23540 + int count = 0;
23541 + hc_regs = core_if->host_if->hc_regs[i];
23542 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23543 + hcchar.b.chen = 1;
23544 + hcchar.b.chdis = 1;
23545 + hcchar.b.epdir = 0;
23546 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23547 + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
23548 + do {
23549 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23550 + if (++count > 1000) {
23551 + DWC_ERROR
23552 + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
23553 + __func__, i, hcchar.d32, &hc_regs->hcchar);
23554 + break;
23555 + }
23556 + dwc_udelay(1);
23557 + } while (hcchar.b.chen);
23558 + }
23559 + }
23560 +
23561 + /* Turn on the vbus power. */
23562 + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
23563 + if (core_if->op_state == A_HOST) {
23564 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
23565 + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
23566 + if (hprt0.b.prtpwr == 0) {
23567 + hprt0.b.prtpwr = 1;
23568 + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
23569 + }
23570 + }
23571 +
23572 + dwc_otg_enable_host_interrupts(core_if);
23573 +}
23574 +
23575 +/**
23576 + * Prepares a host channel for transferring packets to/from a specific
23577 + * endpoint. The HCCHARn register is set up with the characteristics specified
23578 + * in _hc. Host channel interrupts that may need to be serviced while this
23579 + * transfer is in progress are enabled.
23580 + *
23581 + * @param core_if Programming view of DWC_otg controller
23582 + * @param hc Information needed to initialize the host channel
23583 + */
23584 +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
23585 +{
23586 + hcintmsk_data_t hc_intr_mask;
23587 + hcchar_data_t hcchar;
23588 + hcsplt_data_t hcsplt;
23589 +
23590 + uint8_t hc_num = hc->hc_num;
23591 + dwc_otg_host_if_t *host_if = core_if->host_if;
23592 + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
23593 +
23594 + /* Clear old interrupt conditions for this host channel. */
23595 + hc_intr_mask.d32 = 0xFFFFFFFF;
23596 + hc_intr_mask.b.reserved14_31 = 0;
23597 + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
23598 +
23599 + /* Enable channel interrupts required for this transfer. */
23600 + hc_intr_mask.d32 = 0;
23601 + hc_intr_mask.b.chhltd = 1;
23602 + if (core_if->dma_enable) {
23603 + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
23604 + if (!core_if->dma_desc_enable)
23605 + hc_intr_mask.b.ahberr = 1;
23606 + else {
23607 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
23608 + hc_intr_mask.b.xfercompl = 1;
23609 + }
23610 +
23611 + if (hc->error_state && !hc->do_split &&
23612 + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
23613 + hc_intr_mask.b.ack = 1;
23614 + if (hc->ep_is_in) {
23615 + hc_intr_mask.b.datatglerr = 1;
23616 + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
23617 + hc_intr_mask.b.nak = 1;
23618 + }
23619 + }
23620 + }
23621 + } else {
23622 + switch (hc->ep_type) {
23623 + case DWC_OTG_EP_TYPE_CONTROL:
23624 + case DWC_OTG_EP_TYPE_BULK:
23625 + hc_intr_mask.b.xfercompl = 1;
23626 + hc_intr_mask.b.stall = 1;
23627 + hc_intr_mask.b.xacterr = 1;
23628 + hc_intr_mask.b.datatglerr = 1;
23629 + if (hc->ep_is_in) {
23630 + hc_intr_mask.b.bblerr = 1;
23631 + } else {
23632 + hc_intr_mask.b.nak = 1;
23633 + hc_intr_mask.b.nyet = 1;
23634 + if (hc->do_ping) {
23635 + hc_intr_mask.b.ack = 1;
23636 + }
23637 + }
23638 +
23639 + if (hc->do_split) {
23640 + hc_intr_mask.b.nak = 1;
23641 + if (hc->complete_split) {
23642 + hc_intr_mask.b.nyet = 1;
23643 + } else {
23644 + hc_intr_mask.b.ack = 1;
23645 + }
23646 + }
23647 +
23648 + if (hc->error_state) {
23649 + hc_intr_mask.b.ack = 1;
23650 + }
23651 + break;
23652 + case DWC_OTG_EP_TYPE_INTR:
23653 + hc_intr_mask.b.xfercompl = 1;
23654 + hc_intr_mask.b.nak = 1;
23655 + hc_intr_mask.b.stall = 1;
23656 + hc_intr_mask.b.xacterr = 1;
23657 + hc_intr_mask.b.datatglerr = 1;
23658 + hc_intr_mask.b.frmovrun = 1;
23659 +
23660 + if (hc->ep_is_in) {
23661 + hc_intr_mask.b.bblerr = 1;
23662 + }
23663 + if (hc->error_state) {
23664 + hc_intr_mask.b.ack = 1;
23665 + }
23666 + if (hc->do_split) {
23667 + if (hc->complete_split) {
23668 + hc_intr_mask.b.nyet = 1;
23669 + } else {
23670 + hc_intr_mask.b.ack = 1;
23671 + }
23672 + }
23673 + break;
23674 + case DWC_OTG_EP_TYPE_ISOC:
23675 + hc_intr_mask.b.xfercompl = 1;
23676 + hc_intr_mask.b.frmovrun = 1;
23677 + hc_intr_mask.b.ack = 1;
23678 +
23679 + if (hc->ep_is_in) {
23680 + hc_intr_mask.b.xacterr = 1;
23681 + hc_intr_mask.b.bblerr = 1;
23682 + }
23683 + break;
23684 + }
23685 + }
23686 + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
23687 +
23688 + /*
23689 + * Program the HCCHARn register with the endpoint characteristics for
23690 + * the current transfer.
23691 + */
23692 + hcchar.d32 = 0;
23693 + hcchar.b.devaddr = hc->dev_addr;
23694 + hcchar.b.epnum = hc->ep_num;
23695 + hcchar.b.epdir = hc->ep_is_in;
23696 + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
23697 + hcchar.b.eptype = hc->ep_type;
23698 + hcchar.b.mps = hc->max_packet;
23699 +
23700 + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
23701 +
23702 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
23703 + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
23704 + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
23705 + "Max Pkt %d, Multi Cnt %d\n",
23706 + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
23707 + hcchar.b.mps, hcchar.b.multicnt);
23708 +
23709 + /*
23710 + * Program the HCSPLIT register for SPLITs
23711 + */
23712 + hcsplt.d32 = 0;
23713 + if (hc->do_split) {
23714 + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
23715 + hc->hc_num,
23716 + hc->complete_split ? "CSPLIT" : "SSPLIT");
23717 + hcsplt.b.compsplt = hc->complete_split;
23718 + hcsplt.b.xactpos = hc->xact_pos;
23719 + hcsplt.b.hubaddr = hc->hub_addr;
23720 + hcsplt.b.prtaddr = hc->port_addr;
23721 + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
23722 + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
23723 + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
23724 + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
23725 + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
23726 + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
23727 + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
23728 + }
23729 + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
23730 +
23731 +}
23732 +
23733 +/**
23734 + * Attempts to halt a host channel. This function should only be called in
23735 + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
23736 + * normal circumstances in DMA mode, the controller halts the channel when the
23737 + * transfer is complete or a condition occurs that requires application
23738 + * intervention.
23739 + *
23740 + * In slave mode, checks for a free request queue entry, then sets the Channel
23741 + * Enable and Channel Disable bits of the Host Channel Characteristics
23742 + * register of the specified channel to intiate the halt. If there is no free
23743 + * request queue entry, sets only the Channel Disable bit of the HCCHARn
23744 + * register to flush requests for this channel. In the latter case, sets a
23745 + * flag to indicate that the host channel needs to be halted when a request
23746 + * queue slot is open.
23747 + *
23748 + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
23749 + * HCCHARn register. The controller ensures there is space in the request
23750 + * queue before submitting the halt request.
23751 + *
23752 + * Some time may elapse before the core flushes any posted requests for this
23753 + * host channel and halts. The Channel Halted interrupt handler completes the
23754 + * deactivation of the host channel.
23755 + *
23756 + * @param core_if Controller register interface.
23757 + * @param hc Host channel to halt.
23758 + * @param halt_status Reason for halting the channel.
23759 + */
23760 +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
23761 + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
23762 +{
23763 + gnptxsts_data_t nptxsts;
23764 + hptxsts_data_t hptxsts;
23765 + hcchar_data_t hcchar;
23766 + dwc_otg_hc_regs_t *hc_regs;
23767 + dwc_otg_core_global_regs_t *global_regs;
23768 + dwc_otg_host_global_regs_t *host_global_regs;
23769 +
23770 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
23771 + global_regs = core_if->core_global_regs;
23772 + host_global_regs = core_if->host_if->host_global_regs;
23773 +
23774 + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
23775 + "halt_status = %d\n", halt_status);
23776 +
23777 + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
23778 + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
23779 + /*
23780 + * Disable all channel interrupts except Ch Halted. The QTD
23781 + * and QH state associated with this transfer has been cleared
23782 + * (in the case of URB_DEQUEUE), so the channel needs to be
23783 + * shut down carefully to prevent crashes.
23784 + */
23785 + hcintmsk_data_t hcintmsk;
23786 + hcintmsk.d32 = 0;
23787 + hcintmsk.b.chhltd = 1;
23788 + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
23789 +
23790 + /*
23791 + * Make sure no other interrupts besides halt are currently
23792 + * pending. Handling another interrupt could cause a crash due
23793 + * to the QTD and QH state.
23794 + */
23795 + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
23796 +
23797 + /*
23798 + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
23799 + * even if the channel was already halted for some other
23800 + * reason.
23801 + */
23802 + hc->halt_status = halt_status;
23803 +
23804 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23805 + if (hcchar.b.chen == 0) {
23806 + /*
23807 + * The channel is either already halted or it hasn't
23808 + * started yet. In DMA mode, the transfer may halt if
23809 + * it finishes normally or a condition occurs that
23810 + * requires driver intervention. Don't want to halt
23811 + * the channel again. In either Slave or DMA mode,
23812 + * it's possible that the transfer has been assigned
23813 + * to a channel, but not started yet when an URB is
23814 + * dequeued. Don't want to halt a channel that hasn't
23815 + * started yet.
23816 + */
23817 + return;
23818 + }
23819 + }
23820 + if (hc->halt_pending) {
23821 + /*
23822 + * A halt has already been issued for this channel. This might
23823 + * happen when a transfer is aborted by a higher level in
23824 + * the stack.
23825 + */
23826 +#ifdef DEBUG
23827 + DWC_PRINTF
23828 + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
23829 + __func__, hc->hc_num);
23830 +
23831 +#endif
23832 + return;
23833 + }
23834 +
23835 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23836 +
23837 + /* No need to set the bit in DDMA for disabling the channel */
23838 + //TODO check it everywhere channel is disabled
23839 + if (!core_if->core_params->dma_desc_enable)
23840 + hcchar.b.chen = 1;
23841 + hcchar.b.chdis = 1;
23842 +
23843 + if (!core_if->dma_enable) {
23844 + /* Check for space in the request queue to issue the halt. */
23845 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
23846 + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
23847 + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
23848 + if (nptxsts.b.nptxqspcavail == 0) {
23849 + hcchar.b.chen = 0;
23850 + }
23851 + } else {
23852 + hptxsts.d32 =
23853 + DWC_READ_REG32(&host_global_regs->hptxsts);
23854 + if ((hptxsts.b.ptxqspcavail == 0)
23855 + || (core_if->queuing_high_bandwidth)) {
23856 + hcchar.b.chen = 0;
23857 + }
23858 + }
23859 + }
23860 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23861 +
23862 + hc->halt_status = halt_status;
23863 +
23864 + if (hcchar.b.chen) {
23865 + hc->halt_pending = 1;
23866 + hc->halt_on_queue = 0;
23867 + } else {
23868 + hc->halt_on_queue = 1;
23869 + }
23870 +
23871 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
23872 + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
23873 + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
23874 + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
23875 + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
23876 +
23877 + return;
23878 +}
23879 +
23880 +/**
23881 + * Clears the transfer state for a host channel. This function is normally
23882 + * called after a transfer is done and the host channel is being released.
23883 + *
23884 + * @param core_if Programming view of DWC_otg controller.
23885 + * @param hc Identifies the host channel to clean up.
23886 + */
23887 +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
23888 +{
23889 + dwc_otg_hc_regs_t *hc_regs;
23890 +
23891 + hc->xfer_started = 0;
23892 +
23893 + /*
23894 + * Clear channel interrupt enables and any unhandled channel interrupt
23895 + * conditions.
23896 + */
23897 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
23898 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
23899 + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
23900 +#ifdef DEBUG
23901 + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
23902 +#endif
23903 +}
23904 +
23905 +/**
23906 + * Sets the channel property that indicates in which frame a periodic transfer
23907 + * should occur. This is always set to the _next_ frame. This function has no
23908 + * effect on non-periodic transfers.
23909 + *
23910 + * @param core_if Programming view of DWC_otg controller.
23911 + * @param hc Identifies the host channel to set up and its properties.
23912 + * @param hcchar Current value of the HCCHAR register for the specified host
23913 + * channel.
23914 + */
23915 +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
23916 + dwc_hc_t * hc, hcchar_data_t * hcchar)
23917 +{
23918 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
23919 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
23920 + hfnum_data_t hfnum;
23921 + hfnum.d32 =
23922 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
23923 +
23924 + /* 1 if _next_ frame is odd, 0 if it's even */
23925 + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
23926 +#ifdef DEBUG
23927 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
23928 + && !hc->complete_split) {
23929 + switch (hfnum.b.frnum & 0x7) {
23930 + case 7:
23931 + core_if->hfnum_7_samples++;
23932 + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
23933 + break;
23934 + case 0:
23935 + core_if->hfnum_0_samples++;
23936 + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
23937 + break;
23938 + default:
23939 + core_if->hfnum_other_samples++;
23940 + core_if->hfnum_other_frrem_accum +=
23941 + hfnum.b.frrem;
23942 + break;
23943 + }
23944 + }
23945 +#endif
23946 + }
23947 +}
23948 +
23949 +#ifdef DEBUG
23950 +void hc_xfer_timeout(void *ptr)
23951 +{
23952 + hc_xfer_info_t *xfer_info = NULL;
23953 + int hc_num = 0;
23954 +
23955 + if (ptr)
23956 + xfer_info = (hc_xfer_info_t *) ptr;
23957 +
23958 + if (!xfer_info->hc) {
23959 + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
23960 + return;
23961 + }
23962 +
23963 + hc_num = xfer_info->hc->hc_num;
23964 + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
23965 + DWC_WARN(" start_hcchar_val 0x%08x\n",
23966 + xfer_info->core_if->start_hcchar_val[hc_num]);
23967 +}
23968 +#endif
23969 +
23970 +void ep_xfer_timeout(void *ptr)
23971 +{
23972 + ep_xfer_info_t *xfer_info = NULL;
23973 + int ep_num = 0;
23974 + dctl_data_t dctl = {.d32 = 0 };
23975 + gintsts_data_t gintsts = {.d32 = 0 };
23976 + gintmsk_data_t gintmsk = {.d32 = 0 };
23977 +
23978 + if (ptr)
23979 + xfer_info = (ep_xfer_info_t *) ptr;
23980 +
23981 + if (!xfer_info->ep) {
23982 + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
23983 + return;
23984 + }
23985 +
23986 + ep_num = xfer_info->ep->num;
23987 + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
23988 + /* Put the sate to 2 as it was time outed */
23989 + xfer_info->state = 2;
23990 +
23991 + dctl.d32 =
23992 + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
23993 + gintsts.d32 =
23994 + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
23995 + gintmsk.d32 =
23996 + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
23997 +
23998 + if (!gintmsk.b.goutnakeff) {
23999 + /* Unmask it */
24000 + gintmsk.b.goutnakeff = 1;
24001 + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
24002 + gintmsk.d32);
24003 +
24004 + }
24005 +
24006 + if (!gintsts.b.goutnakeff) {
24007 + dctl.b.sgoutnak = 1;
24008 + }
24009 + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
24010 + dctl.d32);
24011 +
24012 +}
24013 +
24014 +void set_pid_isoc(dwc_hc_t * hc)
24015 +{
24016 + /* Set up the initial PID for the transfer. */
24017 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
24018 + if (hc->ep_is_in) {
24019 + if (hc->multi_count == 1) {
24020 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24021 + } else if (hc->multi_count == 2) {
24022 + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
24023 + } else {
24024 + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
24025 + }
24026 + } else {
24027 + if (hc->multi_count == 1) {
24028 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24029 + } else {
24030 + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
24031 + }
24032 + }
24033 + } else {
24034 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24035 + }
24036 +}
24037 +
24038 +/**
24039 + * This function does the setup for a data transfer for a host channel and
24040 + * starts the transfer. May be called in either Slave mode or DMA mode. In
24041 + * Slave mode, the caller must ensure that there is sufficient space in the
24042 + * request queue and Tx Data FIFO.
24043 + *
24044 + * For an OUT transfer in Slave mode, it loads a data packet into the
24045 + * appropriate FIFO. If necessary, additional data packets will be loaded in
24046 + * the Host ISR.
24047 + *
24048 + * For an IN transfer in Slave mode, a data packet is requested. The data
24049 + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
24050 + * additional data packets are requested in the Host ISR.
24051 + *
24052 + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
24053 + * register along with a packet count of 1 and the channel is enabled. This
24054 + * causes a single PING transaction to occur. Other fields in HCTSIZ are
24055 + * simply set to 0 since no data transfer occurs in this case.
24056 + *
24057 + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
24058 + * all the information required to perform the subsequent data transfer. In
24059 + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
24060 + * controller performs the entire PING protocol, then starts the data
24061 + * transfer.
24062 + *
24063 + * @param core_if Programming view of DWC_otg controller.
24064 + * @param hc Information needed to initialize the host channel. The xfer_len
24065 + * value may be reduced to accommodate the max widths of the XferSize and
24066 + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
24067 + * to reflect the final xfer_len value.
24068 + */
24069 +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24070 +{
24071 + hcchar_data_t hcchar;
24072 + hctsiz_data_t hctsiz;
24073 + uint16_t num_packets;
24074 + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
24075 + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
24076 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24077 +
24078 + hctsiz.d32 = 0;
24079 +
24080 + if (hc->do_ping) {
24081 + if (!core_if->dma_enable) {
24082 + dwc_otg_hc_do_ping(core_if, hc);
24083 + hc->xfer_started = 1;
24084 + return;
24085 + } else {
24086 + hctsiz.b.dopng = 1;
24087 + }
24088 + }
24089 +
24090 + if (hc->do_split) {
24091 + num_packets = 1;
24092 +
24093 + if (hc->complete_split && !hc->ep_is_in) {
24094 + /* For CSPLIT OUT Transfer, set the size to 0 so the
24095 + * core doesn't expect any data written to the FIFO */
24096 + hc->xfer_len = 0;
24097 + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
24098 + hc->xfer_len = hc->max_packet;
24099 + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
24100 + hc->xfer_len = 188;
24101 + }
24102 +
24103 + hctsiz.b.xfersize = hc->xfer_len;
24104 + } else {
24105 + /*
24106 + * Ensure that the transfer length and packet count will fit
24107 + * in the widths allocated for them in the HCTSIZn register.
24108 + */
24109 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24110 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24111 + /*
24112 + * Make sure the transfer size is no larger than one
24113 + * (micro)frame's worth of data. (A check was done
24114 + * when the periodic transfer was accepted to ensure
24115 + * that a (micro)frame's worth of data can be
24116 + * programmed into a channel.)
24117 + */
24118 + uint32_t max_periodic_len =
24119 + hc->multi_count * hc->max_packet;
24120 + if (hc->xfer_len > max_periodic_len) {
24121 + hc->xfer_len = max_periodic_len;
24122 + } else {
24123 + }
24124 + } else if (hc->xfer_len > max_hc_xfer_size) {
24125 + /* Make sure that xfer_len is a multiple of max packet size. */
24126 + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
24127 + }
24128 +
24129 + if (hc->xfer_len > 0) {
24130 + num_packets =
24131 + (hc->xfer_len + hc->max_packet -
24132 + 1) / hc->max_packet;
24133 + if (num_packets > max_hc_pkt_count) {
24134 + num_packets = max_hc_pkt_count;
24135 + hc->xfer_len = num_packets * hc->max_packet;
24136 + }
24137 + } else {
24138 + /* Need 1 packet for transfer length of 0. */
24139 + num_packets = 1;
24140 + }
24141 +
24142 + if (hc->ep_is_in) {
24143 + /* Always program an integral # of max packets for IN transfers. */
24144 + hc->xfer_len = num_packets * hc->max_packet;
24145 + }
24146 +
24147 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24148 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24149 + /*
24150 + * Make sure that the multi_count field matches the
24151 + * actual transfer length.
24152 + */
24153 + hc->multi_count = num_packets;
24154 + }
24155 +
24156 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24157 + set_pid_isoc(hc);
24158 +
24159 + hctsiz.b.xfersize = hc->xfer_len;
24160 + }
24161 +
24162 + hc->start_pkt_count = num_packets;
24163 + hctsiz.b.pktcnt = num_packets;
24164 + hctsiz.b.pid = hc->data_pid_start;
24165 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24166 +
24167 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24168 + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
24169 + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
24170 + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
24171 +
24172 + if (core_if->dma_enable) {
24173 + dwc_dma_t dma_addr;
24174 + if (hc->align_buff) {
24175 + dma_addr = hc->align_buff;
24176 + } else {
24177 + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
24178 + }
24179 + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
24180 + }
24181 +
24182 + /* Start the split */
24183 + if (hc->do_split) {
24184 + hcsplt_data_t hcsplt;
24185 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
24186 + hcsplt.b.spltena = 1;
24187 + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
24188 + }
24189 +
24190 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24191 + hcchar.b.multicnt = hc->multi_count;
24192 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24193 +#ifdef DEBUG
24194 + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
24195 + if (hcchar.b.chdis) {
24196 + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
24197 + __func__, hc->hc_num, hcchar.d32);
24198 + }
24199 +#endif
24200 +
24201 + /* Set host channel enable after all other setup is complete. */
24202 + hcchar.b.chen = 1;
24203 + hcchar.b.chdis = 0;
24204 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24205 +
24206 + hc->xfer_started = 1;
24207 + hc->requests++;
24208 +
24209 + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
24210 + /* Load OUT packet into the appropriate Tx FIFO. */
24211 + dwc_otg_hc_write_packet(core_if, hc);
24212 + }
24213 +#ifdef DEBUG
24214 + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
24215 + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
24216 + hc->hc_num, core_if);//GRAYG
24217 + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
24218 + core_if->hc_xfer_info[hc->hc_num].hc = hc;
24219 +
24220 + /* Start a timer for this transfer. */
24221 + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
24222 + }
24223 +#endif
24224 +}
24225 +
24226 +/**
24227 + * This function does the setup for a data transfer for a host channel
24228 + * and starts the transfer in Descriptor DMA mode.
24229 + *
24230 + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
24231 + * Sets PID and NTD values. For periodic transfers
24232 + * initializes SCHED_INFO field with micro-frame bitmap.
24233 + *
24234 + * Initializes HCDMA register with descriptor list address and CTD value
24235 + * then starts the transfer via enabling the channel.
24236 + *
24237 + * @param core_if Programming view of DWC_otg controller.
24238 + * @param hc Information needed to initialize the host channel.
24239 + */
24240 +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24241 +{
24242 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24243 + hcchar_data_t hcchar;
24244 + hctsiz_data_t hctsiz;
24245 + hcdma_data_t hcdma;
24246 +
24247 + hctsiz.d32 = 0;
24248 +
24249 + if (hc->do_ping)
24250 + hctsiz.b_ddma.dopng = 1;
24251 +
24252 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24253 + set_pid_isoc(hc);
24254 +
24255 + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
24256 + hctsiz.b_ddma.pid = hc->data_pid_start;
24257 + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
24258 + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
24259 +
24260 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24261 + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
24262 + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
24263 +
24264 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24265 +
24266 + hcdma.d32 = 0;
24267 + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
24268 +
24269 + /* Always start from first descriptor. */
24270 + hcdma.b.ctd = 0;
24271 + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
24272 +
24273 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24274 + hcchar.b.multicnt = hc->multi_count;
24275 +
24276 +#ifdef DEBUG
24277 + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
24278 + if (hcchar.b.chdis) {
24279 + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
24280 + __func__, hc->hc_num, hcchar.d32);
24281 + }
24282 +#endif
24283 +
24284 + /* Set host channel enable after all other setup is complete. */
24285 + hcchar.b.chen = 1;
24286 + hcchar.b.chdis = 0;
24287 +
24288 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24289 +
24290 + hc->xfer_started = 1;
24291 + hc->requests++;
24292 +
24293 +#ifdef DEBUG
24294 + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
24295 + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
24296 + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
24297 + hc->hc_num, core_if);//GRAYG
24298 + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
24299 + core_if->hc_xfer_info[hc->hc_num].hc = hc;
24300 + /* Start a timer for this transfer. */
24301 + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
24302 + }
24303 +#endif
24304 +
24305 +}
24306 +
24307 +/**
24308 + * This function continues a data transfer that was started by previous call
24309 + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
24310 + * sufficient space in the request queue and Tx Data FIFO. This function
24311 + * should only be called in Slave mode. In DMA mode, the controller acts
24312 + * autonomously to complete transfers programmed to a host channel.
24313 + *
24314 + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
24315 + * if there is any data remaining to be queued. For an IN transfer, another
24316 + * data packet is always requested. For the SETUP phase of a control transfer,
24317 + * this function does nothing.
24318 + *
24319 + * @return 1 if a new request is queued, 0 if no more requests are required
24320 + * for this transfer.
24321 + */
24322 +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24323 +{
24324 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24325 +
24326 + if (hc->do_split) {
24327 + /* SPLITs always queue just once per channel */
24328 + return 0;
24329 + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
24330 + /* SETUPs are queued only once since they can't be NAKed. */
24331 + return 0;
24332 + } else if (hc->ep_is_in) {
24333 + /*
24334 + * Always queue another request for other IN transfers. If
24335 + * back-to-back INs are issued and NAKs are received for both,
24336 + * the driver may still be processing the first NAK when the
24337 + * second NAK is received. When the interrupt handler clears
24338 + * the NAK interrupt for the first NAK, the second NAK will
24339 + * not be seen. So we can't depend on the NAK interrupt
24340 + * handler to requeue a NAKed request. Instead, IN requests
24341 + * are issued each time this function is called. When the
24342 + * transfer completes, the extra requests for the channel will
24343 + * be flushed.
24344 + */
24345 + hcchar_data_t hcchar;
24346 + dwc_otg_hc_regs_t *hc_regs =
24347 + core_if->host_if->hc_regs[hc->hc_num];
24348 +
24349 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24350 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24351 + hcchar.b.chen = 1;
24352 + hcchar.b.chdis = 0;
24353 + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
24354 + hcchar.d32);
24355 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24356 + hc->requests++;
24357 + return 1;
24358 + } else {
24359 + /* OUT transfers. */
24360 + if (hc->xfer_count < hc->xfer_len) {
24361 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24362 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24363 + hcchar_data_t hcchar;
24364 + dwc_otg_hc_regs_t *hc_regs;
24365 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24366 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24367 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24368 + }
24369 +
24370 + /* Load OUT packet into the appropriate Tx FIFO. */
24371 + dwc_otg_hc_write_packet(core_if, hc);
24372 + hc->requests++;
24373 + return 1;
24374 + } else {
24375 + return 0;
24376 + }
24377 + }
24378 +}
24379 +
24380 +/**
24381 + * Starts a PING transfer. This function should only be called in Slave mode.
24382 + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
24383 + */
24384 +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24385 +{
24386 + hcchar_data_t hcchar;
24387 + hctsiz_data_t hctsiz;
24388 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24389 +
24390 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24391 +
24392 + hctsiz.d32 = 0;
24393 + hctsiz.b.dopng = 1;
24394 + hctsiz.b.pktcnt = 1;
24395 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24396 +
24397 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24398 + hcchar.b.chen = 1;
24399 + hcchar.b.chdis = 0;
24400 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24401 +}
24402 +
24403 +/*
24404 + * This function writes a packet into the Tx FIFO associated with the Host
24405 + * Channel. For a channel associated with a non-periodic EP, the non-periodic
24406 + * Tx FIFO is written. For a channel associated with a periodic EP, the
24407 + * periodic Tx FIFO is written. This function should only be called in Slave
24408 + * mode.
24409 + *
24410 + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
24411 + * then number of bytes written to the Tx FIFO.
24412 + */
24413 +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24414 +{
24415 + uint32_t i;
24416 + uint32_t remaining_count;
24417 + uint32_t byte_count;
24418 + uint32_t dword_count;
24419 +
24420 + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
24421 + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
24422 +
24423 + remaining_count = hc->xfer_len - hc->xfer_count;
24424 + if (remaining_count > hc->max_packet) {
24425 + byte_count = hc->max_packet;
24426 + } else {
24427 + byte_count = remaining_count;
24428 + }
24429 +
24430 + dword_count = (byte_count + 3) / 4;
24431 +
24432 + if ((((unsigned long)data_buff) & 0x3) == 0) {
24433 + /* xfer_buff is DWORD aligned. */
24434 + for (i = 0; i < dword_count; i++, data_buff++) {
24435 + DWC_WRITE_REG32(data_fifo, *data_buff);
24436 + }
24437 + } else {
24438 + /* xfer_buff is not DWORD aligned. */
24439 + for (i = 0; i < dword_count; i++, data_buff++) {
24440 + uint32_t data;
24441 + data =
24442 + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
24443 + 16 | data_buff[3] << 24);
24444 + DWC_WRITE_REG32(data_fifo, data);
24445 + }
24446 + }
24447 +
24448 + hc->xfer_count += byte_count;
24449 + hc->xfer_buff += byte_count;
24450 +}
24451 +
24452 +/**
24453 + * Gets the current USB frame number. This is the frame number from the last
24454 + * SOF packet.
24455 + */
24456 +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
24457 +{
24458 + dsts_data_t dsts;
24459 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
24460 +
24461 + /* read current frame/microframe number from DSTS register */
24462 + return dsts.b.soffn;
24463 +}
24464 +
24465 +/**
24466 + * Calculates and gets the frame Interval value of HFIR register according PHY
24467 + * type and speed.The application can modify a value of HFIR register only after
24468 + * the Port Enable bit of the Host Port Control and Status register
24469 + * (HPRT.PrtEnaPort) has been set.
24470 +*/
24471 +
24472 +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
24473 +{
24474 + gusbcfg_data_t usbcfg;
24475 + hwcfg2_data_t hwcfg2;
24476 + hprt0_data_t hprt0;
24477 + int clock = 60; // default value
24478 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
24479 + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
24480 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
24481 + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
24482 + clock = 60;
24483 + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
24484 + clock = 48;
24485 + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24486 + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
24487 + clock = 30;
24488 + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24489 + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
24490 + clock = 60;
24491 + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24492 + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
24493 + clock = 48;
24494 + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
24495 + clock = 48;
24496 + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
24497 + clock = 48;
24498 + if (hprt0.b.prtspd == 0)
24499 + /* High speed case */
24500 + return 125 * clock - 1;
24501 + else
24502 + /* FS/LS case */
24503 + return 1000 * clock - 1;
24504 +}
24505 +
24506 +/**
24507 + * This function reads a setup packet from the Rx FIFO into the destination
24508 + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
24509 + * Interrupt routine when a SETUP packet has been received in Slave mode.
24510 + *
24511 + * @param core_if Programming view of DWC_otg controller.
24512 + * @param dest Destination buffer for packet data.
24513 + */
24514 +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
24515 +{
24516 + device_grxsts_data_t status;
24517 + /* Get the 8 bytes of a setup transaction data */
24518 +
24519 + /* Pop 2 DWORDS off the receive data FIFO into memory */
24520 + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
24521 + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
24522 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
24523 + status.d32 =
24524 + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
24525 + DWC_DEBUGPL(DBG_ANY,
24526 + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
24527 + status.b.epnum, status.b.bcnt, status.b.pktsts,
24528 + status.b.fn, status.b.fn);
24529 + }
24530 +}
24531 +
24532 +/**
24533 + * This function enables EP0 OUT to receive SETUP packets and configures EP0
24534 + * IN for transmitting packets. It is normally called when the
24535 + * "Enumeration Done" interrupt occurs.
24536 + *
24537 + * @param core_if Programming view of DWC_otg controller.
24538 + * @param ep The EP0 data.
24539 + */
24540 +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24541 +{
24542 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
24543 + dsts_data_t dsts;
24544 + depctl_data_t diepctl;
24545 + depctl_data_t doepctl;
24546 + dctl_data_t dctl = {.d32 = 0 };
24547 +
24548 + ep->stp_rollover = 0;
24549 + /* Read the Device Status and Endpoint 0 Control registers */
24550 + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
24551 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
24552 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
24553 +
24554 + /* Set the MPS of the IN EP based on the enumeration speed */
24555 + switch (dsts.b.enumspd) {
24556 + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
24557 + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
24558 + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
24559 + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
24560 + break;
24561 + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
24562 + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
24563 + break;
24564 + }
24565 +
24566 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
24567 +
24568 + /* Enable OUT EP for receive */
24569 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
24570 + doepctl.b.epena = 1;
24571 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
24572 + }
24573 +#ifdef VERBOSE
24574 + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
24575 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
24576 + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
24577 + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
24578 +#endif
24579 + dctl.b.cgnpinnak = 1;
24580 +
24581 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
24582 + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
24583 + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
24584 +
24585 +}
24586 +
24587 +/**
24588 + * This function activates an EP. The Device EP control register for
24589 + * the EP is configured as defined in the ep structure. Note: This
24590 + * function is not used for EP0.
24591 + *
24592 + * @param core_if Programming view of DWC_otg controller.
24593 + * @param ep The EP to activate.
24594 + */
24595 +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24596 +{
24597 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
24598 + depctl_data_t depctl;
24599 + volatile uint32_t *addr;
24600 + daint_data_t daintmsk = {.d32 = 0 };
24601 + dcfg_data_t dcfg;
24602 + uint8_t i;
24603 +
24604 + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
24605 + (ep->is_in ? "IN" : "OUT"));
24606 +
24607 +#ifdef DWC_UTE_PER_IO
24608 + ep->xiso_frame_num = 0xFFFFFFFF;
24609 + ep->xiso_active_xfers = 0;
24610 + ep->xiso_queued_xfers = 0;
24611 +#endif
24612 + /* Read DEPCTLn register */
24613 + if (ep->is_in == 1) {
24614 + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
24615 + daintmsk.ep.in = 1 << ep->num;
24616 + } else {
24617 + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
24618 + daintmsk.ep.out = 1 << ep->num;
24619 + }
24620 +
24621 + /* If the EP is already active don't change the EP Control
24622 + * register. */
24623 + depctl.d32 = DWC_READ_REG32(addr);
24624 + if (!depctl.b.usbactep) {
24625 + depctl.b.mps = ep->maxpacket;
24626 + depctl.b.eptype = ep->type;
24627 + depctl.b.txfnum = ep->tx_fifo_num;
24628 +
24629 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
24630 + depctl.b.setd0pid = 1; // ???
24631 + } else {
24632 + depctl.b.setd0pid = 1;
24633 + }
24634 + depctl.b.usbactep = 1;
24635 +
24636 + /* Update nextep_seq array and EPMSCNT in DCFG*/
24637 + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
24638 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
24639 + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
24640 + break;
24641 + }
24642 + core_if->nextep_seq[i] = ep->num;
24643 + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
24644 + depctl.b.nextep = core_if->nextep_seq[ep->num];
24645 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
24646 + dcfg.b.epmscnt++;
24647 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
24648 +
24649 + DWC_DEBUGPL(DBG_PCDV,
24650 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
24651 + __func__, core_if->first_in_nextep_seq);
24652 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
24653 + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
24654 + core_if->nextep_seq[i]);
24655 + }
24656 +
24657 + }
24658 +
24659 +
24660 + DWC_WRITE_REG32(addr, depctl.d32);
24661 + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
24662 + }
24663 +
24664 + /* Enable the Interrupt for this EP */
24665 + if (core_if->multiproc_int_enable) {
24666 + if (ep->is_in == 1) {
24667 + diepmsk_data_t diepmsk = {.d32 = 0 };
24668 + diepmsk.b.xfercompl = 1;
24669 + diepmsk.b.timeout = 1;
24670 + diepmsk.b.epdisabled = 1;
24671 + diepmsk.b.ahberr = 1;
24672 + diepmsk.b.intknepmis = 1;
24673 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
24674 + diepmsk.b.intknepmis = 0;
24675 + diepmsk.b.txfifoundrn = 1; //?????
24676 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
24677 + diepmsk.b.nak = 1;
24678 + }
24679 +
24680 +
24681 +
24682 +/*
24683 + if (core_if->dma_desc_enable) {
24684 + diepmsk.b.bna = 1;
24685 + }
24686 +*/
24687 +/*
24688 + if (core_if->dma_enable) {
24689 + doepmsk.b.nak = 1;
24690 + }
24691 +*/
24692 + DWC_WRITE_REG32(&dev_if->dev_global_regs->
24693 + diepeachintmsk[ep->num], diepmsk.d32);
24694 +
24695 + } else {
24696 + doepmsk_data_t doepmsk = {.d32 = 0 };
24697 + doepmsk.b.xfercompl = 1;
24698 + doepmsk.b.ahberr = 1;
24699 + doepmsk.b.epdisabled = 1;
24700 + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
24701 + doepmsk.b.outtknepdis = 1;
24702 +
24703 +/*
24704 +
24705 + if (core_if->dma_desc_enable) {
24706 + doepmsk.b.bna = 1;
24707 + }
24708 +*/
24709 +/*
24710 + doepmsk.b.babble = 1;
24711 + doepmsk.b.nyet = 1;
24712 + doepmsk.b.nak = 1;
24713 +*/
24714 + DWC_WRITE_REG32(&dev_if->dev_global_regs->
24715 + doepeachintmsk[ep->num], doepmsk.d32);
24716 + }
24717 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
24718 + 0, daintmsk.d32);
24719 + } else {
24720 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
24721 + if (ep->is_in) {
24722 + diepmsk_data_t diepmsk = {.d32 = 0 };
24723 + diepmsk.b.nak = 1;
24724 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
24725 + } else {
24726 + doepmsk_data_t doepmsk = {.d32 = 0 };
24727 + doepmsk.b.outtknepdis = 1;
24728 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
24729 + }
24730 + }
24731 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
24732 + 0, daintmsk.d32);
24733 + }
24734 +
24735 + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
24736 + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
24737 +
24738 + ep->stall_clear_flag = 0;
24739 +
24740 + return;
24741 +}
24742 +
24743 +/**
24744 + * This function deactivates an EP. This is done by clearing the USB Active
24745 + * EP bit in the Device EP control register. Note: This function is not used
24746 + * for EP0. EP0 cannot be deactivated.
24747 + *
24748 + * @param core_if Programming view of DWC_otg controller.
24749 + * @param ep The EP to deactivate.
24750 + */
24751 +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24752 +{
24753 + depctl_data_t depctl = {.d32 = 0 };
24754 + volatile uint32_t *addr;
24755 + daint_data_t daintmsk = {.d32 = 0 };
24756 + dcfg_data_t dcfg;
24757 + uint8_t i = 0;
24758 +
24759 +#ifdef DWC_UTE_PER_IO
24760 + ep->xiso_frame_num = 0xFFFFFFFF;
24761 + ep->xiso_active_xfers = 0;
24762 + ep->xiso_queued_xfers = 0;
24763 +#endif
24764 +
24765 + /* Read DEPCTLn register */
24766 + if (ep->is_in == 1) {
24767 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
24768 + daintmsk.ep.in = 1 << ep->num;
24769 + } else {
24770 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
24771 + daintmsk.ep.out = 1 << ep->num;
24772 + }
24773 +
24774 + depctl.d32 = DWC_READ_REG32(addr);
24775 +
24776 + depctl.b.usbactep = 0;
24777 +
24778 + /* Update nextep_seq array and EPMSCNT in DCFG*/
24779 + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
24780 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
24781 + if (core_if->nextep_seq[i] == ep->num)
24782 + break;
24783 + }
24784 + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
24785 + if (core_if->first_in_nextep_seq == ep->num)
24786 + core_if->first_in_nextep_seq = i;
24787 + core_if->nextep_seq[ep->num] = 0xff;
24788 + depctl.b.nextep = 0;
24789 + dcfg.d32 =
24790 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
24791 + dcfg.b.epmscnt--;
24792 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
24793 + dcfg.d32);
24794 +
24795 + DWC_DEBUGPL(DBG_PCDV,
24796 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
24797 + __func__, core_if->first_in_nextep_seq);
24798 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
24799 + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
24800 + }
24801 + }
24802 +
24803 + if (ep->is_in == 1)
24804 + depctl.b.txfnum = 0;
24805 +
24806 + if (core_if->dma_desc_enable)
24807 + depctl.b.epdis = 1;
24808 +
24809 + DWC_WRITE_REG32(addr, depctl.d32);
24810 + depctl.d32 = DWC_READ_REG32(addr);
24811 + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
24812 + && depctl.b.epena) {
24813 + depctl_data_t depctl = {.d32 = 0};
24814 + if (ep->is_in) {
24815 + diepint_data_t diepint = {.d32 = 0};
24816 +
24817 + depctl.b.snak = 1;
24818 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
24819 + diepctl, depctl.d32);
24820 + do {
24821 + dwc_udelay(10);
24822 + diepint.d32 =
24823 + DWC_READ_REG32(&core_if->
24824 + dev_if->in_ep_regs[ep->num]->
24825 + diepint);
24826 + } while (!diepint.b.inepnakeff);
24827 + diepint.b.inepnakeff = 1;
24828 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
24829 + diepint, diepint.d32);
24830 + depctl.d32 = 0;
24831 + depctl.b.epdis = 1;
24832 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
24833 + diepctl, depctl.d32);
24834 + do {
24835 + dwc_udelay(10);
24836 + diepint.d32 =
24837 + DWC_READ_REG32(&core_if->
24838 + dev_if->in_ep_regs[ep->num]->
24839 + diepint);
24840 + } while (!diepint.b.epdisabled);
24841 + diepint.b.epdisabled = 1;
24842 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
24843 + diepint, diepint.d32);
24844 + } else {
24845 + dctl_data_t dctl = {.d32 = 0};
24846 + gintmsk_data_t gintsts = {.d32 = 0};
24847 + doepint_data_t doepint = {.d32 = 0};
24848 + dctl.b.sgoutnak = 1;
24849 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
24850 + dctl, 0, dctl.d32);
24851 + do {
24852 + dwc_udelay(10);
24853 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
24854 + } while (!gintsts.b.goutnakeff);
24855 + gintsts.d32 = 0;
24856 + gintsts.b.goutnakeff = 1;
24857 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
24858 +
24859 + depctl.d32 = 0;
24860 + depctl.b.epdis = 1;
24861 + depctl.b.snak = 1;
24862 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
24863 + do
24864 + {
24865 + dwc_udelay(10);
24866 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
24867 + out_ep_regs[ep->num]->doepint);
24868 + } while (!doepint.b.epdisabled);
24869 +
24870 + doepint.b.epdisabled = 1;
24871 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
24872 +
24873 + dctl.d32 = 0;
24874 + dctl.b.cgoutnak = 1;
24875 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
24876 + }
24877 + }
24878 +
24879 + /* Disable the Interrupt for this EP */
24880 + if (core_if->multiproc_int_enable) {
24881 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
24882 + daintmsk.d32, 0);
24883 +
24884 + if (ep->is_in == 1) {
24885 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
24886 + diepeachintmsk[ep->num], 0);
24887 + } else {
24888 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
24889 + doepeachintmsk[ep->num], 0);
24890 + }
24891 + } else {
24892 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
24893 + daintmsk.d32, 0);
24894 + }
24895 +
24896 +}
24897 +
24898 +/**
24899 + * This function initializes dma descriptor chain.
24900 + *
24901 + * @param core_if Programming view of DWC_otg controller.
24902 + * @param ep The EP to start the transfer on.
24903 + */
24904 +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24905 +{
24906 + dwc_otg_dev_dma_desc_t *dma_desc;
24907 + uint32_t offset;
24908 + uint32_t xfer_est;
24909 + int i;
24910 + unsigned maxxfer_local, total_len;
24911 +
24912 + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
24913 + (ep->maxpacket%4)) {
24914 + maxxfer_local = ep->maxpacket;
24915 + total_len = ep->xfer_len;
24916 + } else {
24917 + maxxfer_local = ep->maxxfer;
24918 + total_len = ep->total_len;
24919 + }
24920 +
24921 + ep->desc_cnt = (total_len / maxxfer_local) +
24922 + ((total_len % maxxfer_local) ? 1 : 0);
24923 +
24924 + if (!ep->desc_cnt)
24925 + ep->desc_cnt = 1;
24926 +
24927 + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
24928 + ep->desc_cnt = MAX_DMA_DESC_CNT;
24929 +
24930 + dma_desc = ep->desc_addr;
24931 + if (maxxfer_local == ep->maxpacket) {
24932 + if ((total_len % maxxfer_local) &&
24933 + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
24934 + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
24935 + (total_len % maxxfer_local);
24936 + } else
24937 + xfer_est = ep->desc_cnt * maxxfer_local;
24938 + } else
24939 + xfer_est = total_len;
24940 + offset = 0;
24941 + for (i = 0; i < ep->desc_cnt; ++i) {
24942 + /** DMA Descriptor Setup */
24943 + if (xfer_est > maxxfer_local) {
24944 + dma_desc->status.b.bs = BS_HOST_BUSY;
24945 + dma_desc->status.b.l = 0;
24946 + dma_desc->status.b.ioc = 0;
24947 + dma_desc->status.b.sp = 0;
24948 + dma_desc->status.b.bytes = maxxfer_local;
24949 + dma_desc->buf = ep->dma_addr + offset;
24950 + dma_desc->status.b.sts = 0;
24951 + dma_desc->status.b.bs = BS_HOST_READY;
24952 +
24953 + xfer_est -= maxxfer_local;
24954 + offset += maxxfer_local;
24955 + } else {
24956 + dma_desc->status.b.bs = BS_HOST_BUSY;
24957 + dma_desc->status.b.l = 1;
24958 + dma_desc->status.b.ioc = 1;
24959 + if (ep->is_in) {
24960 + dma_desc->status.b.sp =
24961 + (xfer_est %
24962 + ep->maxpacket) ? 1 : ((ep->
24963 + sent_zlp) ? 1 : 0);
24964 + dma_desc->status.b.bytes = xfer_est;
24965 + } else {
24966 + if (maxxfer_local == ep->maxpacket)
24967 + dma_desc->status.b.bytes = xfer_est;
24968 + else
24969 + dma_desc->status.b.bytes =
24970 + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
24971 + }
24972 +
24973 + dma_desc->buf = ep->dma_addr + offset;
24974 + dma_desc->status.b.sts = 0;
24975 + dma_desc->status.b.bs = BS_HOST_READY;
24976 + }
24977 + dma_desc++;
24978 + }
24979 +}
24980 +/**
24981 + * This function is called when to write ISOC data into appropriate dedicated
24982 + * periodic FIFO.
24983 + */
24984 +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
24985 +{
24986 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
24987 + dwc_otg_dev_in_ep_regs_t *ep_regs;
24988 + dtxfsts_data_t txstatus = {.d32 = 0 };
24989 + uint32_t len = 0;
24990 + int epnum = dwc_ep->num;
24991 + int dwords;
24992 +
24993 + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
24994 +
24995 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
24996 +
24997 + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
24998 +
24999 + if (len > dwc_ep->maxpacket) {
25000 + len = dwc_ep->maxpacket;
25001 + }
25002 +
25003 + dwords = (len + 3) / 4;
25004 +
25005 + /* While there is space in the queue and space in the FIFO and
25006 + * More data to tranfer, Write packets to the Tx FIFO */
25007 + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
25008 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
25009 +
25010 + while (txstatus.b.txfspcavail > dwords &&
25011 + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
25012 + /* Write the FIFO */
25013 + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
25014 +
25015 + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
25016 + if (len > dwc_ep->maxpacket) {
25017 + len = dwc_ep->maxpacket;
25018 + }
25019 +
25020 + dwords = (len + 3) / 4;
25021 + txstatus.d32 =
25022 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
25023 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
25024 + txstatus.d32);
25025 + }
25026 +
25027 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
25028 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
25029 +
25030 + return 1;
25031 +}
25032 +/**
25033 + * This function does the setup for a data transfer for an EP and
25034 + * starts the transfer. For an IN transfer, the packets will be
25035 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
25036 + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
25037 + *
25038 + * @param core_if Programming view of DWC_otg controller.
25039 + * @param ep The EP to start the transfer on.
25040 + */
25041 +
25042 +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25043 +{
25044 + depctl_data_t depctl;
25045 + deptsiz_data_t deptsiz;
25046 + gintmsk_data_t intr_mask = {.d32 = 0 };
25047 +
25048 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
25049 + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
25050 + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
25051 + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
25052 + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
25053 + ep->total_len);
25054 + /* IN endpoint */
25055 + if (ep->is_in == 1) {
25056 + dwc_otg_dev_in_ep_regs_t *in_regs =
25057 + core_if->dev_if->in_ep_regs[ep->num];
25058 +
25059 + gnptxsts_data_t gtxstatus;
25060 +
25061 + gtxstatus.d32 =
25062 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25063 +
25064 + if (core_if->en_multiple_tx_fifo == 0
25065 + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
25066 +#ifdef DEBUG
25067 + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
25068 +#endif
25069 + return;
25070 + }
25071 +
25072 + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
25073 + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
25074 +
25075 + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
25076 + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
25077 + ep->maxxfer : (ep->total_len - ep->xfer_len);
25078 + else
25079 + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
25080 + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
25081 +
25082 +
25083 + /* Zero Length Packet? */
25084 + if ((ep->xfer_len - ep->xfer_count) == 0) {
25085 + deptsiz.b.xfersize = 0;
25086 + deptsiz.b.pktcnt = 1;
25087 + } else {
25088 + /* Program the transfer size and packet count
25089 + * as follows: xfersize = N * maxpacket +
25090 + * short_packet pktcnt = N + (short_packet
25091 + * exist ? 1 : 0)
25092 + */
25093 + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
25094 + deptsiz.b.pktcnt =
25095 + (ep->xfer_len - ep->xfer_count - 1 +
25096 + ep->maxpacket) / ep->maxpacket;
25097 + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
25098 + deptsiz.b.pktcnt = MAX_PKT_CNT;
25099 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
25100 + }
25101 + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
25102 + deptsiz.b.mc = deptsiz.b.pktcnt;
25103 + }
25104 +
25105 + /* Write the DMA register */
25106 + if (core_if->dma_enable) {
25107 + if (core_if->dma_desc_enable == 0) {
25108 + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
25109 + deptsiz.b.mc = 1;
25110 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25111 + deptsiz.d32);
25112 + DWC_WRITE_REG32(&(in_regs->diepdma),
25113 + (uint32_t) ep->dma_addr);
25114 + } else {
25115 +#ifdef DWC_UTE_CFI
25116 + /* The descriptor chain should be already initialized by now */
25117 + if (ep->buff_mode != BM_STANDARD) {
25118 + DWC_WRITE_REG32(&in_regs->diepdma,
25119 + ep->descs_dma_addr);
25120 + } else {
25121 +#endif
25122 + init_dma_desc_chain(core_if, ep);
25123 + /** DIEPDMAn Register write */
25124 + DWC_WRITE_REG32(&in_regs->diepdma,
25125 + ep->dma_desc_addr);
25126 +#ifdef DWC_UTE_CFI
25127 + }
25128 +#endif
25129 + }
25130 + } else {
25131 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25132 + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
25133 + /**
25134 + * Enable the Non-Periodic Tx FIFO empty interrupt,
25135 + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
25136 + * the data will be written into the fifo by the ISR.
25137 + */
25138 + if (core_if->en_multiple_tx_fifo == 0) {
25139 + intr_mask.b.nptxfempty = 1;
25140 + DWC_MODIFY_REG32
25141 + (&core_if->core_global_regs->gintmsk,
25142 + intr_mask.d32, intr_mask.d32);
25143 + } else {
25144 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25145 + if (ep->xfer_len > 0) {
25146 + uint32_t fifoemptymsk = 0;
25147 + fifoemptymsk = 1 << ep->num;
25148 + DWC_MODIFY_REG32
25149 + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25150 + 0, fifoemptymsk);
25151 +
25152 + }
25153 + }
25154 + } else {
25155 + write_isoc_tx_fifo(core_if, ep);
25156 + }
25157 + }
25158 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25159 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25160 +
25161 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25162 + dsts_data_t dsts = {.d32 = 0};
25163 + if (ep->bInterval == 1) {
25164 + dsts.d32 =
25165 + DWC_READ_REG32(&core_if->dev_if->
25166 + dev_global_regs->dsts);
25167 + ep->frame_num = dsts.b.soffn + ep->bInterval;
25168 + if (ep->frame_num > 0x3FFF) {
25169 + ep->frm_overrun = 1;
25170 + ep->frame_num &= 0x3FFF;
25171 + } else
25172 + ep->frm_overrun = 0;
25173 + if (ep->frame_num & 0x1) {
25174 + depctl.b.setd1pid = 1;
25175 + } else {
25176 + depctl.b.setd0pid = 1;
25177 + }
25178 + }
25179 + }
25180 + /* EP enable, IN data in FIFO */
25181 + depctl.b.cnak = 1;
25182 + depctl.b.epena = 1;
25183 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25184 +
25185 + } else {
25186 + /* OUT endpoint */
25187 + dwc_otg_dev_out_ep_regs_t *out_regs =
25188 + core_if->dev_if->out_ep_regs[ep->num];
25189 +
25190 + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
25191 + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
25192 +
25193 + if (!core_if->dma_desc_enable) {
25194 + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
25195 + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
25196 + ep->maxxfer : (ep->total_len - ep->xfer_len);
25197 + else
25198 + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
25199 + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
25200 + }
25201 +
25202 + /* Program the transfer size and packet count as follows:
25203 + *
25204 + * pktcnt = N
25205 + * xfersize = N * maxpacket
25206 + */
25207 + if ((ep->xfer_len - ep->xfer_count) == 0) {
25208 + /* Zero Length Packet */
25209 + deptsiz.b.xfersize = ep->maxpacket;
25210 + deptsiz.b.pktcnt = 1;
25211 + } else {
25212 + deptsiz.b.pktcnt =
25213 + (ep->xfer_len - ep->xfer_count +
25214 + (ep->maxpacket - 1)) / ep->maxpacket;
25215 + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
25216 + deptsiz.b.pktcnt = MAX_PKT_CNT;
25217 + }
25218 + if (!core_if->dma_desc_enable) {
25219 + ep->xfer_len =
25220 + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
25221 + }
25222 + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
25223 + }
25224 +
25225 + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
25226 + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
25227 +
25228 + if (core_if->dma_enable) {
25229 + if (!core_if->dma_desc_enable) {
25230 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25231 + deptsiz.d32);
25232 +
25233 + DWC_WRITE_REG32(&(out_regs->doepdma),
25234 + (uint32_t) ep->dma_addr);
25235 + } else {
25236 +#ifdef DWC_UTE_CFI
25237 + /* The descriptor chain should be already initialized by now */
25238 + if (ep->buff_mode != BM_STANDARD) {
25239 + DWC_WRITE_REG32(&out_regs->doepdma,
25240 + ep->descs_dma_addr);
25241 + } else {
25242 +#endif
25243 + /** This is used for interrupt out transfers*/
25244 + if (!ep->xfer_len)
25245 + ep->xfer_len = ep->total_len;
25246 + init_dma_desc_chain(core_if, ep);
25247 +
25248 + if (core_if->core_params->dev_out_nak) {
25249 + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
25250 + deptsiz.b.pktcnt = (ep->total_len +
25251 + (ep->maxpacket - 1)) / ep->maxpacket;
25252 + deptsiz.b.xfersize = ep->total_len;
25253 + /* Remember initial value of doeptsiz */
25254 + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
25255 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25256 + deptsiz.d32);
25257 + }
25258 + }
25259 + /** DOEPDMAn Register write */
25260 + DWC_WRITE_REG32(&out_regs->doepdma,
25261 + ep->dma_desc_addr);
25262 +#ifdef DWC_UTE_CFI
25263 + }
25264 +#endif
25265 + }
25266 + } else {
25267 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25268 + }
25269 +
25270 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25271 + dsts_data_t dsts = {.d32 = 0};
25272 + if (ep->bInterval == 1) {
25273 + dsts.d32 =
25274 + DWC_READ_REG32(&core_if->dev_if->
25275 + dev_global_regs->dsts);
25276 + ep->frame_num = dsts.b.soffn + ep->bInterval;
25277 + if (ep->frame_num > 0x3FFF) {
25278 + ep->frm_overrun = 1;
25279 + ep->frame_num &= 0x3FFF;
25280 + } else
25281 + ep->frm_overrun = 0;
25282 +
25283 + if (ep->frame_num & 0x1) {
25284 + depctl.b.setd1pid = 1;
25285 + } else {
25286 + depctl.b.setd0pid = 1;
25287 + }
25288 + }
25289 + }
25290 +
25291 + /* EP enable */
25292 + depctl.b.cnak = 1;
25293 + depctl.b.epena = 1;
25294 +
25295 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25296 +
25297 + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
25298 + DWC_READ_REG32(&out_regs->doepctl),
25299 + DWC_READ_REG32(&out_regs->doeptsiz));
25300 + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
25301 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
25302 + daintmsk),
25303 + DWC_READ_REG32(&core_if->core_global_regs->
25304 + gintmsk));
25305 +
25306 + /* Timer is scheduling only for out bulk transfers for
25307 + * "Device DDMA OUT NAK Enhancement" feature to inform user
25308 + * about received data payload in case of timeout
25309 + */
25310 + if (core_if->core_params->dev_out_nak) {
25311 + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
25312 + core_if->ep_xfer_info[ep->num].core_if = core_if;
25313 + core_if->ep_xfer_info[ep->num].ep = ep;
25314 + core_if->ep_xfer_info[ep->num].state = 1;
25315 +
25316 + /* Start a timer for this transfer. */
25317 + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
25318 + }
25319 + }
25320 + }
25321 +}
25322 +
25323 +/**
25324 + * This function setup a zero length transfer in Buffer DMA and
25325 + * Slave modes for usb requests with zero field set
25326 + *
25327 + * @param core_if Programming view of DWC_otg controller.
25328 + * @param ep The EP to start the transfer on.
25329 + *
25330 + */
25331 +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25332 +{
25333 +
25334 + depctl_data_t depctl;
25335 + deptsiz_data_t deptsiz;
25336 + gintmsk_data_t intr_mask = {.d32 = 0 };
25337 +
25338 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
25339 + DWC_PRINTF("zero length transfer is called\n");
25340 +
25341 + /* IN endpoint */
25342 + if (ep->is_in == 1) {
25343 + dwc_otg_dev_in_ep_regs_t *in_regs =
25344 + core_if->dev_if->in_ep_regs[ep->num];
25345 +
25346 + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
25347 + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
25348 +
25349 + deptsiz.b.xfersize = 0;
25350 + deptsiz.b.pktcnt = 1;
25351 +
25352 + /* Write the DMA register */
25353 + if (core_if->dma_enable) {
25354 + if (core_if->dma_desc_enable == 0) {
25355 + deptsiz.b.mc = 1;
25356 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25357 + deptsiz.d32);
25358 + DWC_WRITE_REG32(&(in_regs->diepdma),
25359 + (uint32_t) ep->dma_addr);
25360 + }
25361 + } else {
25362 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25363 + /**
25364 + * Enable the Non-Periodic Tx FIFO empty interrupt,
25365 + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
25366 + * the data will be written into the fifo by the ISR.
25367 + */
25368 + if (core_if->en_multiple_tx_fifo == 0) {
25369 + intr_mask.b.nptxfempty = 1;
25370 + DWC_MODIFY_REG32(&core_if->
25371 + core_global_regs->gintmsk,
25372 + intr_mask.d32, intr_mask.d32);
25373 + } else {
25374 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25375 + if (ep->xfer_len > 0) {
25376 + uint32_t fifoemptymsk = 0;
25377 + fifoemptymsk = 1 << ep->num;
25378 + DWC_MODIFY_REG32(&core_if->
25379 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25380 + 0, fifoemptymsk);
25381 + }
25382 + }
25383 + }
25384 +
25385 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25386 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25387 + /* EP enable, IN data in FIFO */
25388 + depctl.b.cnak = 1;
25389 + depctl.b.epena = 1;
25390 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25391 +
25392 + } else {
25393 + /* OUT endpoint */
25394 + dwc_otg_dev_out_ep_regs_t *out_regs =
25395 + core_if->dev_if->out_ep_regs[ep->num];
25396 +
25397 + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
25398 + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
25399 +
25400 + /* Zero Length Packet */
25401 + deptsiz.b.xfersize = ep->maxpacket;
25402 + deptsiz.b.pktcnt = 1;
25403 +
25404 + if (core_if->dma_enable) {
25405 + if (!core_if->dma_desc_enable) {
25406 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25407 + deptsiz.d32);
25408 +
25409 + DWC_WRITE_REG32(&(out_regs->doepdma),
25410 + (uint32_t) ep->dma_addr);
25411 + }
25412 + } else {
25413 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25414 + }
25415 +
25416 + /* EP enable */
25417 + depctl.b.cnak = 1;
25418 + depctl.b.epena = 1;
25419 +
25420 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25421 +
25422 + }
25423 +}
25424 +
25425 +/**
25426 + * This function does the setup for a data transfer for EP0 and starts
25427 + * the transfer. For an IN transfer, the packets will be loaded into
25428 + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
25429 + * unloaded from the Rx FIFO in the ISR.
25430 + *
25431 + * @param core_if Programming view of DWC_otg controller.
25432 + * @param ep The EP0 data.
25433 + */
25434 +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25435 +{
25436 + depctl_data_t depctl;
25437 + deptsiz0_data_t deptsiz;
25438 + gintmsk_data_t intr_mask = {.d32 = 0 };
25439 + dwc_otg_dev_dma_desc_t *dma_desc;
25440 +
25441 + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
25442 + "xfer_buff=%p start_xfer_buff=%p \n",
25443 + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
25444 + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
25445 +
25446 + ep->total_len = ep->xfer_len;
25447 +
25448 + /* IN endpoint */
25449 + if (ep->is_in == 1) {
25450 + dwc_otg_dev_in_ep_regs_t *in_regs =
25451 + core_if->dev_if->in_ep_regs[0];
25452 +
25453 + gnptxsts_data_t gtxstatus;
25454 +
25455 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
25456 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25457 + if (depctl.b.epena)
25458 + return;
25459 + }
25460 +
25461 + gtxstatus.d32 =
25462 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25463 +
25464 + /* If dedicated FIFO every time flush fifo before enable ep*/
25465 + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
25466 + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
25467 +
25468 + if (core_if->en_multiple_tx_fifo == 0
25469 + && gtxstatus.b.nptxqspcavail == 0
25470 + && !core_if->dma_enable) {
25471 +#ifdef DEBUG
25472 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25473 + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
25474 + DWC_READ_REG32(&in_regs->diepctl));
25475 + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
25476 + deptsiz.d32,
25477 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
25478 + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
25479 + gtxstatus.d32);
25480 +#endif
25481 + return;
25482 + }
25483 +
25484 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25485 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25486 +
25487 + /* Zero Length Packet? */
25488 + if (ep->xfer_len == 0) {
25489 + deptsiz.b.xfersize = 0;
25490 + deptsiz.b.pktcnt = 1;
25491 + } else {
25492 + /* Program the transfer size and packet count
25493 + * as follows: xfersize = N * maxpacket +
25494 + * short_packet pktcnt = N + (short_packet
25495 + * exist ? 1 : 0)
25496 + */
25497 + if (ep->xfer_len > ep->maxpacket) {
25498 + ep->xfer_len = ep->maxpacket;
25499 + deptsiz.b.xfersize = ep->maxpacket;
25500 + } else {
25501 + deptsiz.b.xfersize = ep->xfer_len;
25502 + }
25503 + deptsiz.b.pktcnt = 1;
25504 +
25505 + }
25506 + DWC_DEBUGPL(DBG_PCDV,
25507 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
25508 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
25509 + deptsiz.d32);
25510 +
25511 + /* Write the DMA register */
25512 + if (core_if->dma_enable) {
25513 + if (core_if->dma_desc_enable == 0) {
25514 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25515 + deptsiz.d32);
25516 +
25517 + DWC_WRITE_REG32(&(in_regs->diepdma),
25518 + (uint32_t) ep->dma_addr);
25519 + } else {
25520 + dma_desc = core_if->dev_if->in_desc_addr;
25521 +
25522 + /** DMA Descriptor Setup */
25523 + dma_desc->status.b.bs = BS_HOST_BUSY;
25524 + dma_desc->status.b.l = 1;
25525 + dma_desc->status.b.ioc = 1;
25526 + dma_desc->status.b.sp =
25527 + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
25528 + dma_desc->status.b.bytes = ep->xfer_len;
25529 + dma_desc->buf = ep->dma_addr;
25530 + dma_desc->status.b.sts = 0;
25531 + dma_desc->status.b.bs = BS_HOST_READY;
25532 +
25533 + /** DIEPDMA0 Register write */
25534 + DWC_WRITE_REG32(&in_regs->diepdma,
25535 + core_if->
25536 + dev_if->dma_in_desc_addr);
25537 + }
25538 + } else {
25539 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25540 + }
25541 +
25542 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25543 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25544 + /* EP enable, IN data in FIFO */
25545 + depctl.b.cnak = 1;
25546 + depctl.b.epena = 1;
25547 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25548 +
25549 + /**
25550 + * Enable the Non-Periodic Tx FIFO empty interrupt, the
25551 + * data will be written into the fifo by the ISR.
25552 + */
25553 + if (!core_if->dma_enable) {
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 + } else {
25571 + /* OUT endpoint */
25572 + dwc_otg_dev_out_ep_regs_t *out_regs =
25573 + core_if->dev_if->out_ep_regs[0];
25574 +
25575 + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
25576 + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
25577 +
25578 + /* Program the transfer size and packet count as follows:
25579 + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
25580 + * pktcnt = N */
25581 + /* Zero Length Packet */
25582 + deptsiz.b.xfersize = ep->maxpacket;
25583 + deptsiz.b.pktcnt = 1;
25584 + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
25585 + deptsiz.b.supcnt = 3;
25586 +
25587 + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
25588 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
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 + } else {
25598 + dma_desc = core_if->dev_if->out_desc_addr;
25599 +
25600 + /** DMA Descriptor Setup */
25601 + dma_desc->status.b.bs = BS_HOST_BUSY;
25602 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
25603 + dma_desc->status.b.mtrf = 0;
25604 + dma_desc->status.b.sr = 0;
25605 + }
25606 + dma_desc->status.b.l = 1;
25607 + dma_desc->status.b.ioc = 1;
25608 + dma_desc->status.b.bytes = ep->maxpacket;
25609 + dma_desc->buf = ep->dma_addr;
25610 + dma_desc->status.b.sts = 0;
25611 + dma_desc->status.b.bs = BS_HOST_READY;
25612 +
25613 + /** DOEPDMA0 Register write */
25614 + DWC_WRITE_REG32(&out_regs->doepdma,
25615 + core_if->dev_if->
25616 + dma_out_desc_addr);
25617 + }
25618 + } else {
25619 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25620 + }
25621 +
25622 + /* EP enable */
25623 + depctl.b.cnak = 1;
25624 + depctl.b.epena = 1;
25625 + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
25626 + }
25627 +}
25628 +
25629 +/**
25630 + * This function continues control IN transfers started by
25631 + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
25632 + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
25633 + * bit for the packet count.
25634 + *
25635 + * @param core_if Programming view of DWC_otg controller.
25636 + * @param ep The EP0 data.
25637 + */
25638 +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25639 +{
25640 + depctl_data_t depctl;
25641 + deptsiz0_data_t deptsiz;
25642 + gintmsk_data_t intr_mask = {.d32 = 0 };
25643 + dwc_otg_dev_dma_desc_t *dma_desc;
25644 +
25645 + if (ep->is_in == 1) {
25646 + dwc_otg_dev_in_ep_regs_t *in_regs =
25647 + core_if->dev_if->in_ep_regs[0];
25648 + gnptxsts_data_t tx_status = {.d32 = 0 };
25649 +
25650 + tx_status.d32 =
25651 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25652 + /** @todo Should there be check for room in the Tx
25653 + * Status Queue. If not remove the code above this comment. */
25654 +
25655 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25656 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25657 +
25658 + /* Program the transfer size and packet count
25659 + * as follows: xfersize = N * maxpacket +
25660 + * short_packet pktcnt = N + (short_packet
25661 + * exist ? 1 : 0)
25662 + */
25663 +
25664 + if (core_if->dma_desc_enable == 0) {
25665 + deptsiz.b.xfersize =
25666 + (ep->total_len - ep->xfer_count) >
25667 + ep->maxpacket ? ep->maxpacket : (ep->total_len -
25668 + ep->xfer_count);
25669 + deptsiz.b.pktcnt = 1;
25670 + if (core_if->dma_enable == 0) {
25671 + ep->xfer_len += deptsiz.b.xfersize;
25672 + } else {
25673 + ep->xfer_len = deptsiz.b.xfersize;
25674 + }
25675 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25676 + } else {
25677 + ep->xfer_len =
25678 + (ep->total_len - ep->xfer_count) >
25679 + ep->maxpacket ? ep->maxpacket : (ep->total_len -
25680 + ep->xfer_count);
25681 +
25682 + dma_desc = core_if->dev_if->in_desc_addr;
25683 +
25684 + /** DMA Descriptor Setup */
25685 + dma_desc->status.b.bs = BS_HOST_BUSY;
25686 + dma_desc->status.b.l = 1;
25687 + dma_desc->status.b.ioc = 1;
25688 + dma_desc->status.b.sp =
25689 + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
25690 + dma_desc->status.b.bytes = ep->xfer_len;
25691 + dma_desc->buf = ep->dma_addr;
25692 + dma_desc->status.b.sts = 0;
25693 + dma_desc->status.b.bs = BS_HOST_READY;
25694 +
25695 + /** DIEPDMA0 Register write */
25696 + DWC_WRITE_REG32(&in_regs->diepdma,
25697 + core_if->dev_if->dma_in_desc_addr);
25698 + }
25699 +
25700 + DWC_DEBUGPL(DBG_PCDV,
25701 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
25702 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
25703 + deptsiz.d32);
25704 +
25705 + /* Write the DMA register */
25706 + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
25707 + if (core_if->dma_desc_enable == 0)
25708 + DWC_WRITE_REG32(&(in_regs->diepdma),
25709 + (uint32_t) ep->dma_addr);
25710 + }
25711 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25712 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25713 + /* EP enable, IN data in FIFO */
25714 + depctl.b.cnak = 1;
25715 + depctl.b.epena = 1;
25716 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25717 +
25718 + /**
25719 + * Enable the Non-Periodic Tx FIFO empty interrupt, the
25720 + * data will be written into the fifo by the ISR.
25721 + */
25722 + if (!core_if->dma_enable) {
25723 + if (core_if->en_multiple_tx_fifo == 0) {
25724 + /* First clear it from GINTSTS */
25725 + intr_mask.b.nptxfempty = 1;
25726 + DWC_MODIFY_REG32(&core_if->
25727 + core_global_regs->gintmsk,
25728 + intr_mask.d32, intr_mask.d32);
25729 +
25730 + } else {
25731 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25732 + if (ep->xfer_len > 0) {
25733 + uint32_t fifoemptymsk = 0;
25734 + fifoemptymsk |= 1 << ep->num;
25735 + DWC_MODIFY_REG32(&core_if->
25736 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25737 + 0, fifoemptymsk);
25738 + }
25739 + }
25740 + }
25741 + } else {
25742 + dwc_otg_dev_out_ep_regs_t *out_regs =
25743 + core_if->dev_if->out_ep_regs[0];
25744 +
25745 + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
25746 + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
25747 +
25748 + /* Program the transfer size and packet count
25749 + * as follows: xfersize = N * maxpacket +
25750 + * short_packet pktcnt = N + (short_packet
25751 + * exist ? 1 : 0)
25752 + */
25753 + deptsiz.b.xfersize = ep->maxpacket;
25754 + deptsiz.b.pktcnt = 1;
25755 +
25756 + if (core_if->dma_desc_enable == 0) {
25757 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25758 + } else {
25759 + dma_desc = core_if->dev_if->out_desc_addr;
25760 +
25761 + /** DMA Descriptor Setup */
25762 + dma_desc->status.b.bs = BS_HOST_BUSY;
25763 + dma_desc->status.b.l = 1;
25764 + dma_desc->status.b.ioc = 1;
25765 + dma_desc->status.b.bytes = ep->maxpacket;
25766 + dma_desc->buf = ep->dma_addr;
25767 + dma_desc->status.b.sts = 0;
25768 + dma_desc->status.b.bs = BS_HOST_READY;
25769 +
25770 + /** DOEPDMA0 Register write */
25771 + DWC_WRITE_REG32(&out_regs->doepdma,
25772 + core_if->dev_if->dma_out_desc_addr);
25773 + }
25774 +
25775 + DWC_DEBUGPL(DBG_PCDV,
25776 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
25777 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
25778 + deptsiz.d32);
25779 +
25780 + /* Write the DMA register */
25781 + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
25782 + if (core_if->dma_desc_enable == 0)
25783 + DWC_WRITE_REG32(&(out_regs->doepdma),
25784 + (uint32_t) ep->dma_addr);
25785 +
25786 + }
25787 +
25788 + /* EP enable, IN data in FIFO */
25789 + depctl.b.cnak = 1;
25790 + depctl.b.epena = 1;
25791 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25792 +
25793 + }
25794 +}
25795 +
25796 +#ifdef DEBUG
25797 +void dump_msg(const u8 * buf, unsigned int length)
25798 +{
25799 + unsigned int start, num, i;
25800 + char line[52], *p;
25801 +
25802 + if (length >= 512)
25803 + return;
25804 + start = 0;
25805 + while (length > 0) {
25806 + num = length < 16u ? length : 16u;
25807 + p = line;
25808 + for (i = 0; i < num; ++i) {
25809 + if (i == 8)
25810 + *p++ = ' ';
25811 + DWC_SPRINTF(p, " %02x", buf[i]);
25812 + p += 3;
25813 + }
25814 + *p = 0;
25815 + DWC_PRINTF("%6x: %s\n", start, line);
25816 + buf += num;
25817 + start += num;
25818 + length -= num;
25819 + }
25820 +}
25821 +#else
25822 +static inline void dump_msg(const u8 * buf, unsigned int length)
25823 +{
25824 +}
25825 +#endif
25826 +
25827 +/**
25828 + * This function writes a packet into the Tx FIFO associated with the
25829 + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
25830 + * periodic EPs the periodic Tx FIFO associated with the EP is written
25831 + * with all packets for the next micro-frame.
25832 + *
25833 + * @param core_if Programming view of DWC_otg controller.
25834 + * @param ep The EP to write packet for.
25835 + * @param dma Indicates if DMA is being used.
25836 + */
25837 +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
25838 + int dma)
25839 +{
25840 + /**
25841 + * The buffer is padded to DWORD on a per packet basis in
25842 + * slave/dma mode if the MPS is not DWORD aligned. The last
25843 + * packet, if short, is also padded to a multiple of DWORD.
25844 + *
25845 + * ep->xfer_buff always starts DWORD aligned in memory and is a
25846 + * multiple of DWORD in length
25847 + *
25848 + * ep->xfer_len can be any number of bytes
25849 + *
25850 + * ep->xfer_count is a multiple of ep->maxpacket until the last
25851 + * packet
25852 + *
25853 + * FIFO access is DWORD */
25854 +
25855 + uint32_t i;
25856 + uint32_t byte_count;
25857 + uint32_t dword_count;
25858 + uint32_t *fifo;
25859 + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
25860 +
25861 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
25862 + ep);
25863 + if (ep->xfer_count >= ep->xfer_len) {
25864 + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
25865 + return;
25866 + }
25867 +
25868 + /* Find the byte length of the packet either short packet or MPS */
25869 + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
25870 + byte_count = ep->xfer_len - ep->xfer_count;
25871 + } else {
25872 + byte_count = ep->maxpacket;
25873 + }
25874 +
25875 + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
25876 + * is not a multiple of DWORD */
25877 + dword_count = (byte_count + 3) / 4;
25878 +
25879 +#ifdef VERBOSE
25880 + dump_msg(ep->xfer_buff, byte_count);
25881 +#endif
25882 +
25883 + /**@todo NGS Where are the Periodic Tx FIFO addresses
25884 + * intialized? What should this be? */
25885 +
25886 + fifo = core_if->data_fifo[ep->num];
25887 +
25888 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
25889 + fifo, data_buff, *data_buff, byte_count);
25890 +
25891 + if (!dma) {
25892 + for (i = 0; i < dword_count; i++, data_buff++) {
25893 + DWC_WRITE_REG32(fifo, *data_buff);
25894 + }
25895 + }
25896 +
25897 + ep->xfer_count += byte_count;
25898 + ep->xfer_buff += byte_count;
25899 + ep->dma_addr += byte_count;
25900 +}
25901 +
25902 +/**
25903 + * Set the EP STALL.
25904 + *
25905 + * @param core_if Programming view of DWC_otg controller.
25906 + * @param ep The EP to set the stall on.
25907 + */
25908 +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25909 +{
25910 + depctl_data_t depctl;
25911 + volatile uint32_t *depctl_addr;
25912 +
25913 + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
25914 + (ep->is_in ? "IN" : "OUT"));
25915 +
25916 + if (ep->is_in == 1) {
25917 + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
25918 + depctl.d32 = DWC_READ_REG32(depctl_addr);
25919 +
25920 + /* set the disable and stall bits */
25921 + if (depctl.b.epena) {
25922 + depctl.b.epdis = 1;
25923 + }
25924 + depctl.b.stall = 1;
25925 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
25926 + } else {
25927 + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
25928 + depctl.d32 = DWC_READ_REG32(depctl_addr);
25929 +
25930 + /* set the stall bit */
25931 + depctl.b.stall = 1;
25932 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
25933 + }
25934 +
25935 + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
25936 +
25937 + return;
25938 +}
25939 +
25940 +/**
25941 + * Clear the EP STALL.
25942 + *
25943 + * @param core_if Programming view of DWC_otg controller.
25944 + * @param ep The EP to clear stall from.
25945 + */
25946 +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25947 +{
25948 + depctl_data_t depctl;
25949 + volatile uint32_t *depctl_addr;
25950 +
25951 + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
25952 + (ep->is_in ? "IN" : "OUT"));
25953 +
25954 + if (ep->is_in == 1) {
25955 + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
25956 + } else {
25957 + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
25958 + }
25959 +
25960 + depctl.d32 = DWC_READ_REG32(depctl_addr);
25961 +
25962 + /* clear the stall bits */
25963 + depctl.b.stall = 0;
25964 +
25965 + /*
25966 + * USB Spec 9.4.5: For endpoints using data toggle, regardless
25967 + * of whether an endpoint has the Halt feature set, a
25968 + * ClearFeature(ENDPOINT_HALT) request always results in the
25969 + * data toggle being reinitialized to DATA0.
25970 + */
25971 + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
25972 + ep->type == DWC_OTG_EP_TYPE_BULK) {
25973 + depctl.b.setd0pid = 1; /* DATA0 */
25974 + }
25975 +
25976 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
25977 + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
25978 + return;
25979 +}
25980 +
25981 +/**
25982 + * This function reads a packet from the Rx FIFO into the destination
25983 + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
25984 + *
25985 + * @param core_if Programming view of DWC_otg controller.
25986 + * @param dest Destination buffer for the packet.
25987 + * @param bytes Number of bytes to copy to the destination.
25988 + */
25989 +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
25990 + uint8_t * dest, uint16_t bytes)
25991 +{
25992 + int i;
25993 + int word_count = (bytes + 3) / 4;
25994 +
25995 + volatile uint32_t *fifo = core_if->data_fifo[0];
25996 + uint32_t *data_buff = (uint32_t *) dest;
25997 +
25998 + /**
25999 + * @todo Account for the case where _dest is not dword aligned. This
26000 + * requires reading data from the FIFO into a uint32_t temp buffer,
26001 + * then moving it into the data buffer.
26002 + */
26003 +
26004 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
26005 + core_if, dest, bytes);
26006 +
26007 + for (i = 0; i < word_count; i++, data_buff++) {
26008 + *data_buff = DWC_READ_REG32(fifo);
26009 + }
26010 +
26011 + return;
26012 +}
26013 +
26014 +/**
26015 + * This functions reads the device registers and prints them
26016 + *
26017 + * @param core_if Programming view of DWC_otg controller.
26018 + */
26019 +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
26020 +{
26021 + int i;
26022 + volatile uint32_t *addr;
26023 +
26024 + DWC_PRINTF("Device Global Registers\n");
26025 + addr = &core_if->dev_if->dev_global_regs->dcfg;
26026 + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
26027 + (unsigned long)addr, DWC_READ_REG32(addr));
26028 + addr = &core_if->dev_if->dev_global_regs->dctl;
26029 + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
26030 + (unsigned long)addr, DWC_READ_REG32(addr));
26031 + addr = &core_if->dev_if->dev_global_regs->dsts;
26032 + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
26033 + (unsigned long)addr, DWC_READ_REG32(addr));
26034 + addr = &core_if->dev_if->dev_global_regs->diepmsk;
26035 + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26036 + DWC_READ_REG32(addr));
26037 + addr = &core_if->dev_if->dev_global_regs->doepmsk;
26038 + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26039 + DWC_READ_REG32(addr));
26040 + addr = &core_if->dev_if->dev_global_regs->daint;
26041 + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26042 + DWC_READ_REG32(addr));
26043 + addr = &core_if->dev_if->dev_global_regs->daintmsk;
26044 + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26045 + DWC_READ_REG32(addr));
26046 + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
26047 + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26048 + DWC_READ_REG32(addr));
26049 + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
26050 + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
26051 + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
26052 + (unsigned long)addr, DWC_READ_REG32(addr));
26053 + }
26054 +
26055 + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
26056 + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
26057 + DWC_READ_REG32(addr));
26058 +
26059 + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
26060 + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
26061 + (unsigned long)addr, DWC_READ_REG32(addr));
26062 +
26063 + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
26064 + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
26065 + (unsigned long)addr, DWC_READ_REG32(addr));
26066 +
26067 + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
26068 + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
26069 + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
26070 + (unsigned long)addr, DWC_READ_REG32(addr));
26071 + }
26072 +
26073 + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
26074 + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26075 + DWC_READ_REG32(addr));
26076 +
26077 + if (core_if->hwcfg2.b.multi_proc_int) {
26078 +
26079 + addr = &core_if->dev_if->dev_global_regs->deachint;
26080 + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
26081 + (unsigned long)addr, DWC_READ_REG32(addr));
26082 + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
26083 + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
26084 + (unsigned long)addr, DWC_READ_REG32(addr));
26085 +
26086 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
26087 + addr =
26088 + &core_if->dev_if->
26089 + dev_global_regs->diepeachintmsk[i];
26090 + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
26091 + i, (unsigned long)addr,
26092 + DWC_READ_REG32(addr));
26093 + }
26094 +
26095 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
26096 + addr =
26097 + &core_if->dev_if->
26098 + dev_global_regs->doepeachintmsk[i];
26099 + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
26100 + i, (unsigned long)addr,
26101 + DWC_READ_REG32(addr));
26102 + }
26103 + }
26104 +
26105 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
26106 + DWC_PRINTF("Device IN EP %d Registers\n", i);
26107 + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
26108 + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
26109 + (unsigned long)addr, DWC_READ_REG32(addr));
26110 + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
26111 + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
26112 + (unsigned long)addr, DWC_READ_REG32(addr));
26113 + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
26114 + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
26115 + (unsigned long)addr, DWC_READ_REG32(addr));
26116 + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
26117 + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
26118 + (unsigned long)addr, DWC_READ_REG32(addr));
26119 + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
26120 + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
26121 + (unsigned long)addr, DWC_READ_REG32(addr));
26122 + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
26123 + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
26124 + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
26125 + }
26126 +
26127 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
26128 + DWC_PRINTF("Device OUT EP %d Registers\n", i);
26129 + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
26130 + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
26131 + (unsigned long)addr, DWC_READ_REG32(addr));
26132 + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
26133 + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
26134 + (unsigned long)addr, DWC_READ_REG32(addr));
26135 + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
26136 + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
26137 + (unsigned long)addr, DWC_READ_REG32(addr));
26138 + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
26139 + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
26140 + (unsigned long)addr, DWC_READ_REG32(addr));
26141 + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
26142 + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
26143 + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
26144 + (unsigned long)addr, DWC_READ_REG32(addr));
26145 + }
26146 +
26147 + }
26148 +}
26149 +
26150 +/**
26151 + * This functions reads the SPRAM and prints its content
26152 + *
26153 + * @param core_if Programming view of DWC_otg controller.
26154 + */
26155 +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
26156 +{
26157 + volatile uint8_t *addr, *start_addr, *end_addr;
26158 +
26159 + DWC_PRINTF("SPRAM Data:\n");
26160 + start_addr = (void *)core_if->core_global_regs;
26161 + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
26162 + start_addr += 0x00028000;
26163 + end_addr = (void *)core_if->core_global_regs;
26164 + end_addr += 0x000280e0;
26165 +
26166 + for (addr = start_addr; addr < end_addr; addr += 16) {
26167 + DWC_PRINTF
26168 + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
26169 + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
26170 + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
26171 + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
26172 + );
26173 + }
26174 +
26175 + return;
26176 +}
26177 +
26178 +/**
26179 + * This function reads the host registers and prints them
26180 + *
26181 + * @param core_if Programming view of DWC_otg controller.
26182 + */
26183 +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
26184 +{
26185 + int i;
26186 + volatile uint32_t *addr;
26187 +
26188 + DWC_PRINTF("Host Global Registers\n");
26189 + addr = &core_if->host_if->host_global_regs->hcfg;
26190 + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
26191 + (unsigned long)addr, DWC_READ_REG32(addr));
26192 + addr = &core_if->host_if->host_global_regs->hfir;
26193 + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
26194 + (unsigned long)addr, DWC_READ_REG32(addr));
26195 + addr = &core_if->host_if->host_global_regs->hfnum;
26196 + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
26197 + DWC_READ_REG32(addr));
26198 + addr = &core_if->host_if->host_global_regs->hptxsts;
26199 + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26200 + DWC_READ_REG32(addr));
26201 + addr = &core_if->host_if->host_global_regs->haint;
26202 + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26203 + DWC_READ_REG32(addr));
26204 + addr = &core_if->host_if->host_global_regs->haintmsk;
26205 + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26206 + DWC_READ_REG32(addr));
26207 + if (core_if->dma_desc_enable) {
26208 + addr = &core_if->host_if->host_global_regs->hflbaddr;
26209 + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
26210 + (unsigned long)addr, DWC_READ_REG32(addr));
26211 + }
26212 +
26213 + addr = core_if->host_if->hprt0;
26214 + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26215 + DWC_READ_REG32(addr));
26216 +
26217 + for (i = 0; i < core_if->core_params->host_channels; i++) {
26218 + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
26219 + addr = &core_if->host_if->hc_regs[i]->hcchar;
26220 + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
26221 + (unsigned long)addr, DWC_READ_REG32(addr));
26222 + addr = &core_if->host_if->hc_regs[i]->hcsplt;
26223 + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
26224 + (unsigned long)addr, DWC_READ_REG32(addr));
26225 + addr = &core_if->host_if->hc_regs[i]->hcint;
26226 + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
26227 + (unsigned long)addr, DWC_READ_REG32(addr));
26228 + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
26229 + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
26230 + (unsigned long)addr, DWC_READ_REG32(addr));
26231 + addr = &core_if->host_if->hc_regs[i]->hctsiz;
26232 + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
26233 + (unsigned long)addr, DWC_READ_REG32(addr));
26234 + addr = &core_if->host_if->hc_regs[i]->hcdma;
26235 + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
26236 + (unsigned long)addr, DWC_READ_REG32(addr));
26237 + if (core_if->dma_desc_enable) {
26238 + addr = &core_if->host_if->hc_regs[i]->hcdmab;
26239 + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
26240 + (unsigned long)addr, DWC_READ_REG32(addr));
26241 + }
26242 +
26243 + }
26244 + return;
26245 +}
26246 +
26247 +/**
26248 + * This function reads the core global registers and prints them
26249 + *
26250 + * @param core_if Programming view of DWC_otg controller.
26251 + */
26252 +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
26253 +{
26254 + int i, ep_num;
26255 + volatile uint32_t *addr;
26256 + char *txfsiz;
26257 +
26258 + DWC_PRINTF("Core Global Registers\n");
26259 + addr = &core_if->core_global_regs->gotgctl;
26260 + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26261 + DWC_READ_REG32(addr));
26262 + addr = &core_if->core_global_regs->gotgint;
26263 + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26264 + DWC_READ_REG32(addr));
26265 + addr = &core_if->core_global_regs->gahbcfg;
26266 + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26267 + DWC_READ_REG32(addr));
26268 + addr = &core_if->core_global_regs->gusbcfg;
26269 + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26270 + DWC_READ_REG32(addr));
26271 + addr = &core_if->core_global_regs->grstctl;
26272 + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26273 + DWC_READ_REG32(addr));
26274 + addr = &core_if->core_global_regs->gintsts;
26275 + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26276 + DWC_READ_REG32(addr));
26277 + addr = &core_if->core_global_regs->gintmsk;
26278 + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26279 + DWC_READ_REG32(addr));
26280 + addr = &core_if->core_global_regs->grxstsr;
26281 + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
26282 + DWC_READ_REG32(addr));
26283 + addr = &core_if->core_global_regs->grxfsiz;
26284 + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26285 + DWC_READ_REG32(addr));
26286 + addr = &core_if->core_global_regs->gnptxfsiz;
26287 + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26288 + DWC_READ_REG32(addr));
26289 + addr = &core_if->core_global_regs->gnptxsts;
26290 + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26291 + DWC_READ_REG32(addr));
26292 + addr = &core_if->core_global_regs->gi2cctl;
26293 + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26294 + DWC_READ_REG32(addr));
26295 + addr = &core_if->core_global_regs->gpvndctl;
26296 + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26297 + DWC_READ_REG32(addr));
26298 + addr = &core_if->core_global_regs->ggpio;
26299 + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
26300 + DWC_READ_REG32(addr));
26301 + addr = &core_if->core_global_regs->guid;
26302 + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
26303 + (unsigned long)addr, DWC_READ_REG32(addr));
26304 + addr = &core_if->core_global_regs->gsnpsid;
26305 + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
26306 + DWC_READ_REG32(addr));
26307 + addr = &core_if->core_global_regs->ghwcfg1;
26308 + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26309 + DWC_READ_REG32(addr));
26310 + addr = &core_if->core_global_regs->ghwcfg2;
26311 + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26312 + DWC_READ_REG32(addr));
26313 + addr = &core_if->core_global_regs->ghwcfg3;
26314 + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26315 + DWC_READ_REG32(addr));
26316 + addr = &core_if->core_global_regs->ghwcfg4;
26317 + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26318 + DWC_READ_REG32(addr));
26319 + addr = &core_if->core_global_regs->glpmcfg;
26320 + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26321 + DWC_READ_REG32(addr));
26322 + addr = &core_if->core_global_regs->gpwrdn;
26323 + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
26324 + DWC_READ_REG32(addr));
26325 + addr = &core_if->core_global_regs->gdfifocfg;
26326 + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26327 + DWC_READ_REG32(addr));
26328 + addr = &core_if->core_global_regs->adpctl;
26329 + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26330 + dwc_otg_adp_read_reg(core_if));
26331 + addr = &core_if->core_global_regs->hptxfsiz;
26332 + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26333 + DWC_READ_REG32(addr));
26334 +
26335 + if (core_if->en_multiple_tx_fifo == 0) {
26336 + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
26337 + txfsiz = "DPTXFSIZ";
26338 + } else {
26339 + ep_num = core_if->hwcfg4.b.num_in_eps;
26340 + txfsiz = "DIENPTXF";
26341 + }
26342 + for (i = 0; i < ep_num; i++) {
26343 + addr = &core_if->core_global_regs->dtxfsiz[i];
26344 + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
26345 + (unsigned long)addr, DWC_READ_REG32(addr));
26346 + }
26347 + addr = core_if->pcgcctl;
26348 + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26349 + DWC_READ_REG32(addr));
26350 +}
26351 +
26352 +/**
26353 + * Flush a Tx FIFO.
26354 + *
26355 + * @param core_if Programming view of DWC_otg controller.
26356 + * @param num Tx FIFO to flush.
26357 + */
26358 +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
26359 +{
26360 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26361 + volatile grstctl_t greset = {.d32 = 0 };
26362 + int count = 0;
26363 +
26364 + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
26365 +
26366 + greset.b.txfflsh = 1;
26367 + greset.b.txfnum = num;
26368 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26369 +
26370 + do {
26371 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26372 + if (++count > 10000) {
26373 + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
26374 + __func__, greset.d32,
26375 + DWC_READ_REG32(&global_regs->gnptxsts));
26376 + break;
26377 + }
26378 + dwc_udelay(1);
26379 + } while (greset.b.txfflsh == 1);
26380 +
26381 + /* Wait for 3 PHY Clocks */
26382 + dwc_udelay(1);
26383 +}
26384 +
26385 +/**
26386 + * Flush Rx FIFO.
26387 + *
26388 + * @param core_if Programming view of DWC_otg controller.
26389 + */
26390 +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
26391 +{
26392 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26393 + volatile grstctl_t greset = {.d32 = 0 };
26394 + int count = 0;
26395 +
26396 + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
26397 + /*
26398 + *
26399 + */
26400 + greset.b.rxfflsh = 1;
26401 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26402 +
26403 + do {
26404 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26405 + if (++count > 10000) {
26406 + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
26407 + greset.d32);
26408 + break;
26409 + }
26410 + dwc_udelay(1);
26411 + } while (greset.b.rxfflsh == 1);
26412 +
26413 + /* Wait for 3 PHY Clocks */
26414 + dwc_udelay(1);
26415 +}
26416 +
26417 +/**
26418 + * Do core a soft reset of the core. Be careful with this because it
26419 + * resets all the internal state machines of the core.
26420 + */
26421 +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
26422 +{
26423 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26424 + volatile grstctl_t greset = {.d32 = 0 };
26425 + int count = 0;
26426 +
26427 + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
26428 + /* Wait for AHB master IDLE state. */
26429 + do {
26430 + dwc_udelay(10);
26431 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26432 + if (++count > 100000) {
26433 + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
26434 + greset.d32);
26435 + return;
26436 + }
26437 + }
26438 + while (greset.b.ahbidle == 0);
26439 +
26440 + /* Core Soft Reset */
26441 + count = 0;
26442 + greset.b.csftrst = 1;
26443 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26444 + do {
26445 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26446 + if (++count > 10000) {
26447 + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
26448 + __func__, greset.d32);
26449 + break;
26450 + }
26451 + dwc_udelay(1);
26452 + }
26453 + while (greset.b.csftrst == 1);
26454 +
26455 + /* Wait for 3 PHY Clocks */
26456 + dwc_mdelay(100);
26457 +}
26458 +
26459 +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
26460 +{
26461 + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
26462 +}
26463 +
26464 +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
26465 +{
26466 + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
26467 +}
26468 +
26469 +/**
26470 + * Register HCD callbacks. The callbacks are used to start and stop
26471 + * the HCD for interrupt processing.
26472 + *
26473 + * @param core_if Programming view of DWC_otg controller.
26474 + * @param cb the HCD callback structure.
26475 + * @param p pointer to be passed to callback function (usb_hcd*).
26476 + */
26477 +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
26478 + dwc_otg_cil_callbacks_t * cb, void *p)
26479 +{
26480 + core_if->hcd_cb = cb;
26481 + cb->p = p;
26482 +}
26483 +
26484 +/**
26485 + * Register PCD callbacks. The callbacks are used to start and stop
26486 + * the PCD for interrupt processing.
26487 + *
26488 + * @param core_if Programming view of DWC_otg controller.
26489 + * @param cb the PCD callback structure.
26490 + * @param p pointer to be passed to callback function (pcd*).
26491 + */
26492 +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
26493 + dwc_otg_cil_callbacks_t * cb, void *p)
26494 +{
26495 + core_if->pcd_cb = cb;
26496 + cb->p = p;
26497 +}
26498 +
26499 +#ifdef DWC_EN_ISOC
26500 +
26501 +/**
26502 + * This function writes isoc data per 1 (micro)frame into tx fifo
26503 + *
26504 + * @param core_if Programming view of DWC_otg controller.
26505 + * @param ep The EP to start the transfer on.
26506 + *
26507 + */
26508 +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26509 +{
26510 + dwc_otg_dev_in_ep_regs_t *ep_regs;
26511 + dtxfsts_data_t txstatus = {.d32 = 0 };
26512 + uint32_t len = 0;
26513 + uint32_t dwords;
26514 +
26515 + ep->xfer_len = ep->data_per_frame;
26516 + ep->xfer_count = 0;
26517 +
26518 + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
26519 +
26520 + len = ep->xfer_len - ep->xfer_count;
26521 +
26522 + if (len > ep->maxpacket) {
26523 + len = ep->maxpacket;
26524 + }
26525 +
26526 + dwords = (len + 3) / 4;
26527 +
26528 + /* While there is space in the queue and space in the FIFO and
26529 + * More data to tranfer, Write packets to the Tx FIFO */
26530 + txstatus.d32 =
26531 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
26532 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
26533 +
26534 + while (txstatus.b.txfspcavail > dwords &&
26535 + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
26536 + /* Write the FIFO */
26537 + dwc_otg_ep_write_packet(core_if, ep, 0);
26538 +
26539 + len = ep->xfer_len - ep->xfer_count;
26540 + if (len > ep->maxpacket) {
26541 + len = ep->maxpacket;
26542 + }
26543 +
26544 + dwords = (len + 3) / 4;
26545 + txstatus.d32 =
26546 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
26547 + dtxfsts);
26548 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
26549 + txstatus.d32);
26550 + }
26551 +}
26552 +
26553 +/**
26554 + * This function initializes a descriptor chain for Isochronous transfer
26555 + *
26556 + * @param core_if Programming view of DWC_otg controller.
26557 + * @param ep The EP to start the transfer on.
26558 + *
26559 + */
26560 +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
26561 + dwc_ep_t * ep)
26562 +{
26563 + deptsiz_data_t deptsiz = {.d32 = 0 };
26564 + depctl_data_t depctl = {.d32 = 0 };
26565 + dsts_data_t dsts = {.d32 = 0 };
26566 + volatile uint32_t *addr;
26567 +
26568 + if (ep->is_in) {
26569 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
26570 + } else {
26571 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
26572 + }
26573 +
26574 + ep->xfer_len = ep->data_per_frame;
26575 + ep->xfer_count = 0;
26576 + ep->xfer_buff = ep->cur_pkt_addr;
26577 + ep->dma_addr = ep->cur_pkt_dma_addr;
26578 +
26579 + if (ep->is_in) {
26580 + /* Program the transfer size and packet count
26581 + * as follows: xfersize = N * maxpacket +
26582 + * short_packet pktcnt = N + (short_packet
26583 + * exist ? 1 : 0)
26584 + */
26585 + deptsiz.b.xfersize = ep->xfer_len;
26586 + deptsiz.b.pktcnt =
26587 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
26588 + deptsiz.b.mc = deptsiz.b.pktcnt;
26589 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
26590 + deptsiz.d32);
26591 +
26592 + /* Write the DMA register */
26593 + if (core_if->dma_enable) {
26594 + DWC_WRITE_REG32(&
26595 + (core_if->dev_if->in_ep_regs[ep->num]->
26596 + diepdma), (uint32_t) ep->dma_addr);
26597 + }
26598 + } else {
26599 + deptsiz.b.pktcnt =
26600 + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
26601 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
26602 +
26603 + DWC_WRITE_REG32(&core_if->dev_if->
26604 + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
26605 +
26606 + if (core_if->dma_enable) {
26607 + DWC_WRITE_REG32(&
26608 + (core_if->dev_if->
26609 + out_ep_regs[ep->num]->doepdma),
26610 + (uint32_t) ep->dma_addr);
26611 + }
26612 + }
26613 +
26614 + /** Enable endpoint, clear nak */
26615 +
26616 + depctl.d32 = 0;
26617 + if (ep->bInterval == 1) {
26618 + dsts.d32 =
26619 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
26620 + ep->next_frame = dsts.b.soffn + ep->bInterval;
26621 +
26622 + if (ep->next_frame & 0x1) {
26623 + depctl.b.setd1pid = 1;
26624 + } else {
26625 + depctl.b.setd0pid = 1;
26626 + }
26627 + } else {
26628 + ep->next_frame += ep->bInterval;
26629 +
26630 + if (ep->next_frame & 0x1) {
26631 + depctl.b.setd1pid = 1;
26632 + } else {
26633 + depctl.b.setd0pid = 1;
26634 + }
26635 + }
26636 + depctl.b.epena = 1;
26637 + depctl.b.cnak = 1;
26638 +
26639 + DWC_MODIFY_REG32(addr, 0, depctl.d32);
26640 + depctl.d32 = DWC_READ_REG32(addr);
26641 +
26642 + if (ep->is_in && core_if->dma_enable == 0) {
26643 + write_isoc_frame_data(core_if, ep);
26644 + }
26645 +
26646 +}
26647 +#endif /* DWC_EN_ISOC */
26648 +
26649 +static void dwc_otg_set_uninitialized(int32_t * p, int size)
26650 +{
26651 + int i;
26652 + for (i = 0; i < size; i++) {
26653 + p[i] = -1;
26654 + }
26655 +}
26656 +
26657 +static int dwc_otg_param_initialized(int32_t val)
26658 +{
26659 + return val != -1;
26660 +}
26661 +
26662 +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
26663 +{
26664 + int i;
26665 + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
26666 + if (!core_if->core_params) {
26667 + return -DWC_E_NO_MEMORY;
26668 + }
26669 + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
26670 + sizeof(*core_if->core_params) /
26671 + sizeof(int32_t));
26672 + DWC_PRINTF("Setting default values for core params\n");
26673 + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
26674 + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
26675 + dwc_otg_set_param_dma_desc_enable(core_if,
26676 + dwc_param_dma_desc_enable_default);
26677 + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
26678 + dwc_otg_set_param_dma_burst_size(core_if,
26679 + dwc_param_dma_burst_size_default);
26680 + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
26681 + dwc_param_host_support_fs_ls_low_power_default);
26682 + dwc_otg_set_param_enable_dynamic_fifo(core_if,
26683 + dwc_param_enable_dynamic_fifo_default);
26684 + dwc_otg_set_param_data_fifo_size(core_if,
26685 + dwc_param_data_fifo_size_default);
26686 + dwc_otg_set_param_dev_rx_fifo_size(core_if,
26687 + dwc_param_dev_rx_fifo_size_default);
26688 + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
26689 + dwc_param_dev_nperio_tx_fifo_size_default);
26690 + dwc_otg_set_param_host_rx_fifo_size(core_if,
26691 + dwc_param_host_rx_fifo_size_default);
26692 + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
26693 + dwc_param_host_nperio_tx_fifo_size_default);
26694 + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
26695 + dwc_param_host_perio_tx_fifo_size_default);
26696 + dwc_otg_set_param_max_transfer_size(core_if,
26697 + dwc_param_max_transfer_size_default);
26698 + dwc_otg_set_param_max_packet_count(core_if,
26699 + dwc_param_max_packet_count_default);
26700 + dwc_otg_set_param_host_channels(core_if,
26701 + dwc_param_host_channels_default);
26702 + dwc_otg_set_param_dev_endpoints(core_if,
26703 + dwc_param_dev_endpoints_default);
26704 + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
26705 + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
26706 + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
26707 + dwc_param_host_ls_low_power_phy_clk_default);
26708 + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
26709 + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
26710 + dwc_param_phy_ulpi_ext_vbus_default);
26711 + dwc_otg_set_param_phy_utmi_width(core_if,
26712 + dwc_param_phy_utmi_width_default);
26713 + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
26714 + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
26715 + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
26716 + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
26717 + dwc_param_en_multiple_tx_fifo_default);
26718 + for (i = 0; i < 15; i++) {
26719 + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
26720 + dwc_param_dev_perio_tx_fifo_size_default,
26721 + i);
26722 + }
26723 +
26724 + for (i = 0; i < 15; i++) {
26725 + dwc_otg_set_param_dev_tx_fifo_size(core_if,
26726 + dwc_param_dev_tx_fifo_size_default,
26727 + i);
26728 + }
26729 + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
26730 + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
26731 + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
26732 + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
26733 + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
26734 + dwc_otg_set_param_tx_thr_length(core_if,
26735 + dwc_param_tx_thr_length_default);
26736 + dwc_otg_set_param_rx_thr_length(core_if,
26737 + dwc_param_rx_thr_length_default);
26738 + dwc_otg_set_param_ahb_thr_ratio(core_if,
26739 + dwc_param_ahb_thr_ratio_default);
26740 + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
26741 + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
26742 + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
26743 + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
26744 + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
26745 + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
26746 + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
26747 + DWC_PRINTF("Finished setting default values for core params\n");
26748 +
26749 + return 0;
26750 +}
26751 +
26752 +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
26753 +{
26754 + return core_if->dma_enable;
26755 +}
26756 +
26757 +/* Checks if the parameter is outside of its valid range of values */
26758 +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
26759 + (((_param_) < (_low_)) || \
26760 + ((_param_) > (_high_)))
26761 +
26762 +/* Parameter access functions */
26763 +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
26764 +{
26765 + int valid;
26766 + int retval = 0;
26767 + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
26768 + DWC_WARN("Wrong value for otg_cap parameter\n");
26769 + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
26770 + retval = -DWC_E_INVALID;
26771 + goto out;
26772 + }
26773 +
26774 + valid = 1;
26775 + switch (val) {
26776 + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
26777 + if (core_if->hwcfg2.b.op_mode !=
26778 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
26779 + valid = 0;
26780 + break;
26781 + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
26782 + if ((core_if->hwcfg2.b.op_mode !=
26783 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
26784 + && (core_if->hwcfg2.b.op_mode !=
26785 + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
26786 + && (core_if->hwcfg2.b.op_mode !=
26787 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
26788 + && (core_if->hwcfg2.b.op_mode !=
26789 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
26790 + valid = 0;
26791 + }
26792 + break;
26793 + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
26794 + /* always valid */
26795 + break;
26796 + }
26797 + if (!valid) {
26798 + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
26799 + DWC_ERROR
26800 + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
26801 + val);
26802 + }
26803 + val =
26804 + (((core_if->hwcfg2.b.op_mode ==
26805 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
26806 + || (core_if->hwcfg2.b.op_mode ==
26807 + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
26808 + || (core_if->hwcfg2.b.op_mode ==
26809 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
26810 + || (core_if->hwcfg2.b.op_mode ==
26811 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
26812 + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
26813 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
26814 + retval = -DWC_E_INVALID;
26815 + }
26816 +
26817 + core_if->core_params->otg_cap = val;
26818 +out:
26819 + return retval;
26820 +}
26821 +
26822 +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
26823 +{
26824 + return core_if->core_params->otg_cap;
26825 +}
26826 +
26827 +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
26828 +{
26829 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
26830 + DWC_WARN("Wrong value for opt parameter\n");
26831 + return -DWC_E_INVALID;
26832 + }
26833 + core_if->core_params->opt = val;
26834 + return 0;
26835 +}
26836 +
26837 +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
26838 +{
26839 + return core_if->core_params->opt;
26840 +}
26841 +
26842 +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
26843 +{
26844 + int retval = 0;
26845 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
26846 + DWC_WARN("Wrong value for dma enable\n");
26847 + return -DWC_E_INVALID;
26848 + }
26849 +
26850 + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
26851 + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
26852 + DWC_ERROR
26853 + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
26854 + val);
26855 + }
26856 + val = 0;
26857 + retval = -DWC_E_INVALID;
26858 + }
26859 +
26860 + core_if->core_params->dma_enable = val;
26861 + if (val == 0) {
26862 + dwc_otg_set_param_dma_desc_enable(core_if, 0);
26863 + }
26864 + return retval;
26865 +}
26866 +
26867 +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
26868 +{
26869 + return core_if->core_params->dma_enable;
26870 +}
26871 +
26872 +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
26873 +{
26874 + int retval = 0;
26875 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
26876 + DWC_WARN("Wrong value for dma_enable\n");
26877 + DWC_WARN("dma_desc_enable must be 0 or 1\n");
26878 + return -DWC_E_INVALID;
26879 + }
26880 +
26881 + if ((val == 1)
26882 + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
26883 + || (core_if->hwcfg4.b.desc_dma == 0))) {
26884 + if (dwc_otg_param_initialized
26885 + (core_if->core_params->dma_desc_enable)) {
26886 + DWC_ERROR
26887 + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
26888 + val);
26889 + }
26890 + val = 0;
26891 + retval = -DWC_E_INVALID;
26892 + }
26893 + core_if->core_params->dma_desc_enable = val;
26894 + return retval;
26895 +}
26896 +
26897 +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
26898 +{
26899 + return core_if->core_params->dma_desc_enable;
26900 +}
26901 +
26902 +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
26903 + int32_t val)
26904 +{
26905 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
26906 + DWC_WARN("Wrong value for host_support_fs_low_power\n");
26907 + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
26908 + return -DWC_E_INVALID;
26909 + }
26910 + core_if->core_params->host_support_fs_ls_low_power = val;
26911 + return 0;
26912 +}
26913 +
26914 +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
26915 + core_if)
26916 +{
26917 + return core_if->core_params->host_support_fs_ls_low_power;
26918 +}
26919 +
26920 +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
26921 + int32_t val)
26922 +{
26923 + int retval = 0;
26924 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
26925 + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
26926 + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
26927 + return -DWC_E_INVALID;
26928 + }
26929 +
26930 + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
26931 + if (dwc_otg_param_initialized
26932 + (core_if->core_params->enable_dynamic_fifo)) {
26933 + DWC_ERROR
26934 + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
26935 + val);
26936 + }
26937 + val = 0;
26938 + retval = -DWC_E_INVALID;
26939 + }
26940 + core_if->core_params->enable_dynamic_fifo = val;
26941 + return retval;
26942 +}
26943 +
26944 +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
26945 +{
26946 + return core_if->core_params->enable_dynamic_fifo;
26947 +}
26948 +
26949 +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
26950 +{
26951 + int retval = 0;
26952 + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
26953 + DWC_WARN("Wrong value for data_fifo_size\n");
26954 + DWC_WARN("data_fifo_size must be 32-32768\n");
26955 + return -DWC_E_INVALID;
26956 + }
26957 +
26958 + if (val > core_if->hwcfg3.b.dfifo_depth) {
26959 + if (dwc_otg_param_initialized
26960 + (core_if->core_params->data_fifo_size)) {
26961 + DWC_ERROR
26962 + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
26963 + val);
26964 + }
26965 + val = core_if->hwcfg3.b.dfifo_depth;
26966 + retval = -DWC_E_INVALID;
26967 + }
26968 +
26969 + core_if->core_params->data_fifo_size = val;
26970 + return retval;
26971 +}
26972 +
26973 +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
26974 +{
26975 + return core_if->core_params->data_fifo_size;
26976 +}
26977 +
26978 +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
26979 +{
26980 + int retval = 0;
26981 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
26982 + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
26983 + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
26984 + return -DWC_E_INVALID;
26985 + }
26986 +
26987 + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
26988 + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
26989 + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
26990 + }
26991 + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
26992 + retval = -DWC_E_INVALID;
26993 + }
26994 +
26995 + core_if->core_params->dev_rx_fifo_size = val;
26996 + return retval;
26997 +}
26998 +
26999 +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
27000 +{
27001 + return core_if->core_params->dev_rx_fifo_size;
27002 +}
27003 +
27004 +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27005 + int32_t val)
27006 +{
27007 + int retval = 0;
27008 +
27009 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27010 + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
27011 + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
27012 + return -DWC_E_INVALID;
27013 + }
27014 +
27015 + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
27016 + if (dwc_otg_param_initialized
27017 + (core_if->core_params->dev_nperio_tx_fifo_size)) {
27018 + DWC_ERROR
27019 + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
27020 + val);
27021 + }
27022 + val =
27023 + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
27024 + 16);
27025 + retval = -DWC_E_INVALID;
27026 + }
27027 +
27028 + core_if->core_params->dev_nperio_tx_fifo_size = val;
27029 + return retval;
27030 +}
27031 +
27032 +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27033 +{
27034 + return core_if->core_params->dev_nperio_tx_fifo_size;
27035 +}
27036 +
27037 +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
27038 + int32_t val)
27039 +{
27040 + int retval = 0;
27041 +
27042 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27043 + DWC_WARN("Wrong value for host_rx_fifo_size\n");
27044 + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
27045 + return -DWC_E_INVALID;
27046 + }
27047 +
27048 + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
27049 + if (dwc_otg_param_initialized
27050 + (core_if->core_params->host_rx_fifo_size)) {
27051 + DWC_ERROR
27052 + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
27053 + val);
27054 + }
27055 + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
27056 + retval = -DWC_E_INVALID;
27057 + }
27058 +
27059 + core_if->core_params->host_rx_fifo_size = val;
27060 + return retval;
27061 +
27062 +}
27063 +
27064 +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
27065 +{
27066 + return core_if->core_params->host_rx_fifo_size;
27067 +}
27068 +
27069 +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27070 + int32_t val)
27071 +{
27072 + int retval = 0;
27073 +
27074 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27075 + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
27076 + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
27077 + return -DWC_E_INVALID;
27078 + }
27079 +
27080 + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
27081 + if (dwc_otg_param_initialized
27082 + (core_if->core_params->host_nperio_tx_fifo_size)) {
27083 + DWC_ERROR
27084 + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
27085 + val);
27086 + }
27087 + val =
27088 + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
27089 + 16);
27090 + retval = -DWC_E_INVALID;
27091 + }
27092 +
27093 + core_if->core_params->host_nperio_tx_fifo_size = val;
27094 + return retval;
27095 +}
27096 +
27097 +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27098 +{
27099 + return core_if->core_params->host_nperio_tx_fifo_size;
27100 +}
27101 +
27102 +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27103 + int32_t val)
27104 +{
27105 + int retval = 0;
27106 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27107 + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
27108 + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
27109 + return -DWC_E_INVALID;
27110 + }
27111 +
27112 + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
27113 + if (dwc_otg_param_initialized
27114 + (core_if->core_params->host_perio_tx_fifo_size)) {
27115 + DWC_ERROR
27116 + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
27117 + val);
27118 + }
27119 + val = (core_if->hptxfsiz.d32) >> 16;
27120 + retval = -DWC_E_INVALID;
27121 + }
27122 +
27123 + core_if->core_params->host_perio_tx_fifo_size = val;
27124 + return retval;
27125 +}
27126 +
27127 +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27128 +{
27129 + return core_if->core_params->host_perio_tx_fifo_size;
27130 +}
27131 +
27132 +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
27133 + int32_t val)
27134 +{
27135 + int retval = 0;
27136 +
27137 + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
27138 + DWC_WARN("Wrong value for max_transfer_size\n");
27139 + DWC_WARN("max_transfer_size must be 2047-524288\n");
27140 + return -DWC_E_INVALID;
27141 + }
27142 +
27143 + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
27144 + if (dwc_otg_param_initialized
27145 + (core_if->core_params->max_transfer_size)) {
27146 + DWC_ERROR
27147 + ("%d invalid for max_transfer_size. Check HW configuration.\n",
27148 + val);
27149 + }
27150 + val =
27151 + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
27152 + 1);
27153 + retval = -DWC_E_INVALID;
27154 + }
27155 +
27156 + core_if->core_params->max_transfer_size = val;
27157 + return retval;
27158 +}
27159 +
27160 +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
27161 +{
27162 + return core_if->core_params->max_transfer_size;
27163 +}
27164 +
27165 +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
27166 +{
27167 + int retval = 0;
27168 +
27169 + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
27170 + DWC_WARN("Wrong value for max_packet_count\n");
27171 + DWC_WARN("max_packet_count must be 15-511\n");
27172 + return -DWC_E_INVALID;
27173 + }
27174 +
27175 + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
27176 + if (dwc_otg_param_initialized
27177 + (core_if->core_params->max_packet_count)) {
27178 + DWC_ERROR
27179 + ("%d invalid for max_packet_count. Check HW configuration.\n",
27180 + val);
27181 + }
27182 + val =
27183 + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
27184 + retval = -DWC_E_INVALID;
27185 + }
27186 +
27187 + core_if->core_params->max_packet_count = val;
27188 + return retval;
27189 +}
27190 +
27191 +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
27192 +{
27193 + return core_if->core_params->max_packet_count;
27194 +}
27195 +
27196 +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
27197 +{
27198 + int retval = 0;
27199 +
27200 + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
27201 + DWC_WARN("Wrong value for host_channels\n");
27202 + DWC_WARN("host_channels must be 1-16\n");
27203 + return -DWC_E_INVALID;
27204 + }
27205 +
27206 + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
27207 + if (dwc_otg_param_initialized
27208 + (core_if->core_params->host_channels)) {
27209 + DWC_ERROR
27210 + ("%d invalid for host_channels. Check HW configurations.\n",
27211 + val);
27212 + }
27213 + val = (core_if->hwcfg2.b.num_host_chan + 1);
27214 + retval = -DWC_E_INVALID;
27215 + }
27216 +
27217 + core_if->core_params->host_channels = val;
27218 + return retval;
27219 +}
27220 +
27221 +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
27222 +{
27223 + return core_if->core_params->host_channels;
27224 +}
27225 +
27226 +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
27227 +{
27228 + int retval = 0;
27229 +
27230 + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
27231 + DWC_WARN("Wrong value for dev_endpoints\n");
27232 + DWC_WARN("dev_endpoints must be 1-15\n");
27233 + return -DWC_E_INVALID;
27234 + }
27235 +
27236 + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
27237 + if (dwc_otg_param_initialized
27238 + (core_if->core_params->dev_endpoints)) {
27239 + DWC_ERROR
27240 + ("%d invalid for dev_endpoints. Check HW configurations.\n",
27241 + val);
27242 + }
27243 + val = core_if->hwcfg2.b.num_dev_ep;
27244 + retval = -DWC_E_INVALID;
27245 + }
27246 +
27247 + core_if->core_params->dev_endpoints = val;
27248 + return retval;
27249 +}
27250 +
27251 +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
27252 +{
27253 + return core_if->core_params->dev_endpoints;
27254 +}
27255 +
27256 +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
27257 +{
27258 + int retval = 0;
27259 + int valid = 0;
27260 +
27261 + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
27262 + DWC_WARN("Wrong value for phy_type\n");
27263 + DWC_WARN("phy_type must be 0,1 or 2\n");
27264 + return -DWC_E_INVALID;
27265 + }
27266 +#ifndef NO_FS_PHY_HW_CHECKS
27267 + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
27268 + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
27269 + (core_if->hwcfg2.b.hs_phy_type == 3))) {
27270 + valid = 1;
27271 + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
27272 + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
27273 + (core_if->hwcfg2.b.hs_phy_type == 3))) {
27274 + valid = 1;
27275 + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
27276 + (core_if->hwcfg2.b.fs_phy_type == 1)) {
27277 + valid = 1;
27278 + }
27279 + if (!valid) {
27280 + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
27281 + DWC_ERROR
27282 + ("%d invalid for phy_type. Check HW configurations.\n",
27283 + val);
27284 + }
27285 + if (core_if->hwcfg2.b.hs_phy_type) {
27286 + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
27287 + (core_if->hwcfg2.b.hs_phy_type == 1)) {
27288 + val = DWC_PHY_TYPE_PARAM_UTMI;
27289 + } else {
27290 + val = DWC_PHY_TYPE_PARAM_ULPI;
27291 + }
27292 + }
27293 + retval = -DWC_E_INVALID;
27294 + }
27295 +#endif
27296 + core_if->core_params->phy_type = val;
27297 + return retval;
27298 +}
27299 +
27300 +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
27301 +{
27302 + return core_if->core_params->phy_type;
27303 +}
27304 +
27305 +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
27306 +{
27307 + int retval = 0;
27308 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27309 + DWC_WARN("Wrong value for speed parameter\n");
27310 + DWC_WARN("max_speed parameter must be 0 or 1\n");
27311 + return -DWC_E_INVALID;
27312 + }
27313 + if ((val == 0)
27314 + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
27315 + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
27316 + DWC_ERROR
27317 + ("%d invalid for speed paremter. Check HW configuration.\n",
27318 + val);
27319 + }
27320 + val =
27321 + (dwc_otg_get_param_phy_type(core_if) ==
27322 + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
27323 + retval = -DWC_E_INVALID;
27324 + }
27325 + core_if->core_params->speed = val;
27326 + return retval;
27327 +}
27328 +
27329 +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
27330 +{
27331 + return core_if->core_params->speed;
27332 +}
27333 +
27334 +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
27335 + int32_t val)
27336 +{
27337 + int retval = 0;
27338 +
27339 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27340 + DWC_WARN
27341 + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
27342 + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
27343 + return -DWC_E_INVALID;
27344 + }
27345 +
27346 + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
27347 + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
27348 + if (dwc_otg_param_initialized
27349 + (core_if->core_params->host_ls_low_power_phy_clk)) {
27350 + DWC_ERROR
27351 + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
27352 + val);
27353 + }
27354 + val =
27355 + (dwc_otg_get_param_phy_type(core_if) ==
27356 + DWC_PHY_TYPE_PARAM_FS) ?
27357 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
27358 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
27359 + retval = -DWC_E_INVALID;
27360 + }
27361 +
27362 + core_if->core_params->host_ls_low_power_phy_clk = val;
27363 + return retval;
27364 +}
27365 +
27366 +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
27367 +{
27368 + return core_if->core_params->host_ls_low_power_phy_clk;
27369 +}
27370 +
27371 +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
27372 +{
27373 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27374 + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
27375 + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
27376 + return -DWC_E_INVALID;
27377 + }
27378 +
27379 + core_if->core_params->phy_ulpi_ddr = val;
27380 + return 0;
27381 +}
27382 +
27383 +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
27384 +{
27385 + return core_if->core_params->phy_ulpi_ddr;
27386 +}
27387 +
27388 +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
27389 + int32_t val)
27390 +{
27391 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27392 + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
27393 + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
27394 + return -DWC_E_INVALID;
27395 + }
27396 +
27397 + core_if->core_params->phy_ulpi_ext_vbus = val;
27398 + return 0;
27399 +}
27400 +
27401 +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
27402 +{
27403 + return core_if->core_params->phy_ulpi_ext_vbus;
27404 +}
27405 +
27406 +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
27407 +{
27408 + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
27409 + DWC_WARN("Wrong valaue for phy_utmi_width\n");
27410 + DWC_WARN("phy_utmi_width must be 8 or 16\n");
27411 + return -DWC_E_INVALID;
27412 + }
27413 +
27414 + core_if->core_params->phy_utmi_width = val;
27415 + return 0;
27416 +}
27417 +
27418 +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
27419 +{
27420 + return core_if->core_params->phy_utmi_width;
27421 +}
27422 +
27423 +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
27424 +{
27425 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27426 + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
27427 + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
27428 + return -DWC_E_INVALID;
27429 + }
27430 +
27431 + core_if->core_params->ulpi_fs_ls = val;
27432 + return 0;
27433 +}
27434 +
27435 +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
27436 +{
27437 + return core_if->core_params->ulpi_fs_ls;
27438 +}
27439 +
27440 +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
27441 +{
27442 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27443 + DWC_WARN("Wrong valaue for ts_dline\n");
27444 + DWC_WARN("ts_dline must be 0 or 1\n");
27445 + return -DWC_E_INVALID;
27446 + }
27447 +
27448 + core_if->core_params->ts_dline = val;
27449 + return 0;
27450 +}
27451 +
27452 +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
27453 +{
27454 + return core_if->core_params->ts_dline;
27455 +}
27456 +
27457 +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
27458 +{
27459 + int retval = 0;
27460 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27461 + DWC_WARN("Wrong valaue for i2c_enable\n");
27462 + DWC_WARN("i2c_enable must be 0 or 1\n");
27463 + return -DWC_E_INVALID;
27464 + }
27465 +#ifndef NO_FS_PHY_HW_CHECK
27466 + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
27467 + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
27468 + DWC_ERROR
27469 + ("%d invalid for i2c_enable. Check HW configuration.\n",
27470 + val);
27471 + }
27472 + val = 0;
27473 + retval = -DWC_E_INVALID;
27474 + }
27475 +#endif
27476 +
27477 + core_if->core_params->i2c_enable = val;
27478 + return retval;
27479 +}
27480 +
27481 +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
27482 +{
27483 + return core_if->core_params->i2c_enable;
27484 +}
27485 +
27486 +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27487 + int32_t val, int fifo_num)
27488 +{
27489 + int retval = 0;
27490 +
27491 + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
27492 + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
27493 + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
27494 + return -DWC_E_INVALID;
27495 + }
27496 +
27497 + if (val >
27498 + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
27499 + if (dwc_otg_param_initialized
27500 + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
27501 + DWC_ERROR
27502 + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
27503 + val, fifo_num);
27504 + }
27505 + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
27506 + retval = -DWC_E_INVALID;
27507 + }
27508 +
27509 + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
27510 + return retval;
27511 +}
27512 +
27513 +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27514 + int fifo_num)
27515 +{
27516 + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
27517 +}
27518 +
27519 +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
27520 + int32_t val)
27521 +{
27522 + int retval = 0;
27523 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27524 + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
27525 + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
27526 + return -DWC_E_INVALID;
27527 + }
27528 +
27529 + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
27530 + if (dwc_otg_param_initialized
27531 + (core_if->core_params->en_multiple_tx_fifo)) {
27532 + DWC_ERROR
27533 + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
27534 + val);
27535 + }
27536 + val = 0;
27537 + retval = -DWC_E_INVALID;
27538 + }
27539 +
27540 + core_if->core_params->en_multiple_tx_fifo = val;
27541 + return retval;
27542 +}
27543 +
27544 +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
27545 +{
27546 + return core_if->core_params->en_multiple_tx_fifo;
27547 +}
27548 +
27549 +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
27550 + int fifo_num)
27551 +{
27552 + int retval = 0;
27553 +
27554 + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
27555 + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
27556 + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
27557 + return -DWC_E_INVALID;
27558 + }
27559 +
27560 + if (val >
27561 + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
27562 + if (dwc_otg_param_initialized
27563 + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
27564 + DWC_ERROR
27565 + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
27566 + val, fifo_num);
27567 + }
27568 + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
27569 + retval = -DWC_E_INVALID;
27570 + }
27571 +
27572 + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
27573 + return retval;
27574 +}
27575 +
27576 +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
27577 + int fifo_num)
27578 +{
27579 + return core_if->core_params->dev_tx_fifo_size[fifo_num];
27580 +}
27581 +
27582 +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
27583 +{
27584 + int retval = 0;
27585 +
27586 + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
27587 + DWC_WARN("Wrong value for thr_ctl\n");
27588 + DWC_WARN("thr_ctl must be 0-7\n");
27589 + return -DWC_E_INVALID;
27590 + }
27591 +
27592 + if ((val != 0) &&
27593 + (!dwc_otg_get_param_dma_enable(core_if) ||
27594 + !core_if->hwcfg4.b.ded_fifo_en)) {
27595 + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
27596 + DWC_ERROR
27597 + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
27598 + val);
27599 + }
27600 + val = 0;
27601 + retval = -DWC_E_INVALID;
27602 + }
27603 +
27604 + core_if->core_params->thr_ctl = val;
27605 + return retval;
27606 +}
27607 +
27608 +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
27609 +{
27610 + return core_if->core_params->thr_ctl;
27611 +}
27612 +
27613 +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
27614 +{
27615 + int retval = 0;
27616 +
27617 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27618 + DWC_WARN("Wrong value for lpm_enable\n");
27619 + DWC_WARN("lpm_enable must be 0 or 1\n");
27620 + return -DWC_E_INVALID;
27621 + }
27622 +
27623 + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
27624 + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
27625 + DWC_ERROR
27626 + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
27627 + val);
27628 + }
27629 + val = 0;
27630 + retval = -DWC_E_INVALID;
27631 + }
27632 +
27633 + core_if->core_params->lpm_enable = val;
27634 + return retval;
27635 +}
27636 +
27637 +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
27638 +{
27639 + return core_if->core_params->lpm_enable;
27640 +}
27641 +
27642 +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
27643 +{
27644 + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
27645 + DWC_WARN("Wrong valaue for tx_thr_length\n");
27646 + DWC_WARN("tx_thr_length must be 8 - 128\n");
27647 + return -DWC_E_INVALID;
27648 + }
27649 +
27650 + core_if->core_params->tx_thr_length = val;
27651 + return 0;
27652 +}
27653 +
27654 +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
27655 +{
27656 + return core_if->core_params->tx_thr_length;
27657 +}
27658 +
27659 +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
27660 +{
27661 + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
27662 + DWC_WARN("Wrong valaue for rx_thr_length\n");
27663 + DWC_WARN("rx_thr_length must be 8 - 128\n");
27664 + return -DWC_E_INVALID;
27665 + }
27666 +
27667 + core_if->core_params->rx_thr_length = val;
27668 + return 0;
27669 +}
27670 +
27671 +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
27672 +{
27673 + return core_if->core_params->rx_thr_length;
27674 +}
27675 +
27676 +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
27677 +{
27678 + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
27679 + DWC_OTG_PARAM_TEST(val, 4, 4) &&
27680 + DWC_OTG_PARAM_TEST(val, 8, 8) &&
27681 + DWC_OTG_PARAM_TEST(val, 16, 16) &&
27682 + DWC_OTG_PARAM_TEST(val, 32, 32) &&
27683 + DWC_OTG_PARAM_TEST(val, 64, 64) &&
27684 + DWC_OTG_PARAM_TEST(val, 128, 128) &&
27685 + DWC_OTG_PARAM_TEST(val, 256, 256)) {
27686 + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
27687 + return -DWC_E_INVALID;
27688 + }
27689 + core_if->core_params->dma_burst_size = val;
27690 + return 0;
27691 +}
27692 +
27693 +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
27694 +{
27695 + return core_if->core_params->dma_burst_size;
27696 +}
27697 +
27698 +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
27699 +{
27700 + int retval = 0;
27701 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27702 + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
27703 + return -DWC_E_INVALID;
27704 + }
27705 + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
27706 + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
27707 + DWC_ERROR
27708 + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
27709 + val);
27710 + }
27711 + retval = -DWC_E_INVALID;
27712 + val = 0;
27713 + }
27714 + core_if->core_params->pti_enable = val;
27715 + return retval;
27716 +}
27717 +
27718 +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
27719 +{
27720 + return core_if->core_params->pti_enable;
27721 +}
27722 +
27723 +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
27724 +{
27725 + int retval = 0;
27726 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27727 + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
27728 + return -DWC_E_INVALID;
27729 + }
27730 + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
27731 + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
27732 + DWC_ERROR
27733 + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
27734 + val);
27735 + }
27736 + retval = -DWC_E_INVALID;
27737 + val = 0;
27738 + }
27739 + core_if->core_params->mpi_enable = val;
27740 + return retval;
27741 +}
27742 +
27743 +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
27744 +{
27745 + return core_if->core_params->mpi_enable;
27746 +}
27747 +
27748 +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
27749 +{
27750 + int retval = 0;
27751 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27752 + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
27753 + return -DWC_E_INVALID;
27754 + }
27755 + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
27756 + if (dwc_otg_param_initialized
27757 + (core_if->core_params->adp_supp_enable)) {
27758 + DWC_ERROR
27759 + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
27760 + val);
27761 + }
27762 + retval = -DWC_E_INVALID;
27763 + val = 0;
27764 + }
27765 + core_if->core_params->adp_supp_enable = val;
27766 + /*Set OTG version 2.0 in case of enabling ADP*/
27767 + if (val)
27768 + dwc_otg_set_param_otg_ver(core_if, 1);
27769 +
27770 + return retval;
27771 +}
27772 +
27773 +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
27774 +{
27775 + return core_if->core_params->adp_supp_enable;
27776 +}
27777 +
27778 +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
27779 +{
27780 + int retval = 0;
27781 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27782 + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
27783 + DWC_WARN("ic_usb_cap must be 0 or 1\n");
27784 + return -DWC_E_INVALID;
27785 + }
27786 +
27787 + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
27788 + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
27789 + DWC_ERROR
27790 + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
27791 + val);
27792 + }
27793 + retval = -DWC_E_INVALID;
27794 + val = 0;
27795 + }
27796 + core_if->core_params->ic_usb_cap = val;
27797 + return retval;
27798 +}
27799 +
27800 +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
27801 +{
27802 + return core_if->core_params->ic_usb_cap;
27803 +}
27804 +
27805 +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
27806 +{
27807 + int retval = 0;
27808 + int valid = 1;
27809 +
27810 + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
27811 + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
27812 + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
27813 + return -DWC_E_INVALID;
27814 + }
27815 +
27816 + if (val
27817 + && (core_if->snpsid < OTG_CORE_REV_2_81a
27818 + || !dwc_otg_get_param_thr_ctl(core_if))) {
27819 + valid = 0;
27820 + } else if (val
27821 + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
27822 + 4)) {
27823 + valid = 0;
27824 + }
27825 + if (valid == 0) {
27826 + if (dwc_otg_param_initialized
27827 + (core_if->core_params->ahb_thr_ratio)) {
27828 + DWC_ERROR
27829 + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
27830 + val);
27831 + }
27832 + retval = -DWC_E_INVALID;
27833 + val = 0;
27834 + }
27835 +
27836 + core_if->core_params->ahb_thr_ratio = val;
27837 + return retval;
27838 +}
27839 +
27840 +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
27841 +{
27842 + return core_if->core_params->ahb_thr_ratio;
27843 +}
27844 +
27845 +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
27846 +{
27847 + int retval = 0;
27848 + int valid = 1;
27849 + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
27850 + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
27851 +
27852 + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
27853 + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
27854 + DWC_WARN("power_down must be 0 - 2\n");
27855 + return -DWC_E_INVALID;
27856 + }
27857 +
27858 + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
27859 + valid = 0;
27860 + }
27861 + if ((val == 3)
27862 + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
27863 + || (hwcfg4.b.xhiber == 0))) {
27864 + valid = 0;
27865 + }
27866 + if (valid == 0) {
27867 + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
27868 + DWC_ERROR
27869 + ("%d invalid for parameter power_down. Check HW configuration.\n",
27870 + val);
27871 + }
27872 + retval = -DWC_E_INVALID;
27873 + val = 0;
27874 + }
27875 + core_if->core_params->power_down = val;
27876 + return retval;
27877 +}
27878 +
27879 +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
27880 +{
27881 + return core_if->core_params->power_down;
27882 +}
27883 +
27884 +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
27885 +{
27886 + int retval = 0;
27887 + int valid = 1;
27888 +
27889 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27890 + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
27891 + DWC_WARN("reload_ctl must be 0 or 1\n");
27892 + return -DWC_E_INVALID;
27893 + }
27894 +
27895 + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
27896 + valid = 0;
27897 + }
27898 + if (valid == 0) {
27899 + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
27900 + DWC_ERROR("%d invalid for parameter reload_ctl."
27901 + "Check HW configuration.\n", val);
27902 + }
27903 + retval = -DWC_E_INVALID;
27904 + val = 0;
27905 + }
27906 + core_if->core_params->reload_ctl = val;
27907 + return retval;
27908 +}
27909 +
27910 +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
27911 +{
27912 + return core_if->core_params->reload_ctl;
27913 +}
27914 +
27915 +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
27916 +{
27917 + int retval = 0;
27918 + int valid = 1;
27919 +
27920 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27921 + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
27922 + DWC_WARN("dev_out_nak must be 0 or 1\n");
27923 + return -DWC_E_INVALID;
27924 + }
27925 +
27926 + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
27927 + !(core_if->core_params->dma_desc_enable))) {
27928 + valid = 0;
27929 + }
27930 + if (valid == 0) {
27931 + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
27932 + DWC_ERROR("%d invalid for parameter dev_out_nak."
27933 + "Check HW configuration.\n", val);
27934 + }
27935 + retval = -DWC_E_INVALID;
27936 + val = 0;
27937 + }
27938 + core_if->core_params->dev_out_nak = val;
27939 + return retval;
27940 +}
27941 +
27942 +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
27943 +{
27944 + return core_if->core_params->dev_out_nak;
27945 +}
27946 +
27947 +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
27948 +{
27949 + int retval = 0;
27950 + int valid = 1;
27951 +
27952 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27953 + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
27954 + DWC_WARN("cont_on_bna must be 0 or 1\n");
27955 + return -DWC_E_INVALID;
27956 + }
27957 +
27958 + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
27959 + !(core_if->core_params->dma_desc_enable))) {
27960 + valid = 0;
27961 + }
27962 + if (valid == 0) {
27963 + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
27964 + DWC_ERROR("%d invalid for parameter cont_on_bna."
27965 + "Check HW configuration.\n", val);
27966 + }
27967 + retval = -DWC_E_INVALID;
27968 + val = 0;
27969 + }
27970 + core_if->core_params->cont_on_bna = val;
27971 + return retval;
27972 +}
27973 +
27974 +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
27975 +{
27976 + return core_if->core_params->cont_on_bna;
27977 +}
27978 +
27979 +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
27980 +{
27981 + int retval = 0;
27982 + int valid = 1;
27983 +
27984 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27985 + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
27986 + DWC_WARN("ahb_single must be 0 or 1\n");
27987 + return -DWC_E_INVALID;
27988 + }
27989 +
27990 + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
27991 + valid = 0;
27992 + }
27993 + if (valid == 0) {
27994 + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
27995 + DWC_ERROR("%d invalid for parameter ahb_single."
27996 + "Check HW configuration.\n", val);
27997 + }
27998 + retval = -DWC_E_INVALID;
27999 + val = 0;
28000 + }
28001 + core_if->core_params->ahb_single = val;
28002 + return retval;
28003 +}
28004 +
28005 +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
28006 +{
28007 + return core_if->core_params->ahb_single;
28008 +}
28009 +
28010 +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
28011 +{
28012 + int retval = 0;
28013 +
28014 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28015 + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
28016 + DWC_WARN
28017 + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
28018 + return -DWC_E_INVALID;
28019 + }
28020 +
28021 + core_if->core_params->otg_ver = val;
28022 + return retval;
28023 +}
28024 +
28025 +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
28026 +{
28027 + return core_if->core_params->otg_ver;
28028 +}
28029 +
28030 +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
28031 +{
28032 + gotgctl_data_t otgctl;
28033 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28034 + return otgctl.b.hstnegscs;
28035 +}
28036 +
28037 +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
28038 +{
28039 + gotgctl_data_t otgctl;
28040 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28041 + return otgctl.b.sesreqscs;
28042 +}
28043 +
28044 +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
28045 +{
28046 + if(core_if->otg_ver == 0) {
28047 + gotgctl_data_t otgctl;
28048 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28049 + otgctl.b.hnpreq = val;
28050 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
28051 + } else {
28052 + core_if->otg_sts = val;
28053 + }
28054 +}
28055 +
28056 +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
28057 +{
28058 + return core_if->snpsid;
28059 +}
28060 +
28061 +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
28062 +{
28063 + gintsts_data_t gintsts;
28064 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
28065 + return gintsts.b.curmode;
28066 +}
28067 +
28068 +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
28069 +{
28070 + gusbcfg_data_t usbcfg;
28071 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28072 + return usbcfg.b.hnpcap;
28073 +}
28074 +
28075 +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
28076 +{
28077 + gusbcfg_data_t usbcfg;
28078 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28079 + usbcfg.b.hnpcap = val;
28080 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
28081 +}
28082 +
28083 +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
28084 +{
28085 + gusbcfg_data_t usbcfg;
28086 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28087 + return usbcfg.b.srpcap;
28088 +}
28089 +
28090 +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
28091 +{
28092 + gusbcfg_data_t usbcfg;
28093 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28094 + usbcfg.b.srpcap = val;
28095 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
28096 +}
28097 +
28098 +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
28099 +{
28100 + dcfg_data_t dcfg;
28101 + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
28102 +
28103 + dcfg.d32 = -1; //GRAYG
28104 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
28105 + if (NULL == core_if)
28106 + DWC_ERROR("reg request with NULL core_if\n");
28107 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
28108 + core_if, core_if->dev_if);
28109 + if (NULL == core_if->dev_if)
28110 + DWC_ERROR("reg request with NULL dev_if\n");
28111 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
28112 + "dev_global_regs(%p)\n", __func__,
28113 + core_if, core_if->dev_if,
28114 + core_if->dev_if->dev_global_regs);
28115 + if (NULL == core_if->dev_if->dev_global_regs)
28116 + DWC_ERROR("reg request with NULL dev_global_regs\n");
28117 + else {
28118 + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
28119 + "dev_global_regs(%p)->dcfg = %p\n", __func__,
28120 + core_if, core_if->dev_if,
28121 + core_if->dev_if->dev_global_regs,
28122 + &core_if->dev_if->dev_global_regs->dcfg);
28123 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
28124 + }
28125 + return dcfg.b.devspd;
28126 +}
28127 +
28128 +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
28129 +{
28130 + dcfg_data_t dcfg;
28131 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
28132 + dcfg.b.devspd = val;
28133 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
28134 +}
28135 +
28136 +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
28137 +{
28138 + hprt0_data_t hprt0;
28139 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28140 + return hprt0.b.prtconnsts;
28141 +}
28142 +
28143 +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
28144 +{
28145 + dsts_data_t dsts;
28146 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
28147 + return dsts.b.enumspd;
28148 +}
28149 +
28150 +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
28151 +{
28152 + hprt0_data_t hprt0;
28153 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28154 + return hprt0.b.prtpwr;
28155 +
28156 +}
28157 +
28158 +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
28159 +{
28160 + return core_if->hibernation_suspend;
28161 +}
28162 +
28163 +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
28164 +{
28165 + hprt0_data_t hprt0;
28166 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28167 + hprt0.b.prtpwr = val;
28168 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28169 +}
28170 +
28171 +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
28172 +{
28173 + hprt0_data_t hprt0;
28174 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28175 + return hprt0.b.prtsusp;
28176 +
28177 +}
28178 +
28179 +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
28180 +{
28181 + hprt0_data_t hprt0;
28182 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28183 + hprt0.b.prtsusp = val;
28184 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28185 +}
28186 +
28187 +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
28188 +{
28189 + hfir_data_t hfir;
28190 + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
28191 + return hfir.b.frint;
28192 +
28193 +}
28194 +
28195 +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
28196 +{
28197 + hfir_data_t hfir;
28198 + uint32_t fram_int;
28199 + fram_int = calc_frame_interval(core_if);
28200 + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
28201 + if (!core_if->core_params->reload_ctl) {
28202 + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
28203 + "not set to 1.\nShould load driver with reload_ctl=1"
28204 + " module parameter\n");
28205 + return;
28206 + }
28207 + switch (fram_int) {
28208 + case 3750:
28209 + if ((val < 3350) || (val > 4150)) {
28210 + DWC_WARN("HFIR interval for HS core and 30 MHz"
28211 + "clock freq should be from 3350 to 4150\n");
28212 + return;
28213 + }
28214 + break;
28215 + case 30000:
28216 + if ((val < 26820) || (val > 33180)) {
28217 + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
28218 + "clock freq should be from 26820 to 33180\n");
28219 + return;
28220 + }
28221 + break;
28222 + case 6000:
28223 + if ((val < 5360) || (val > 6640)) {
28224 + DWC_WARN("HFIR interval for HS core and 48 MHz"
28225 + "clock freq should be from 5360 to 6640\n");
28226 + return;
28227 + }
28228 + break;
28229 + case 48000:
28230 + if ((val < 42912) || (val > 53088)) {
28231 + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
28232 + "clock freq should be from 42912 to 53088\n");
28233 + return;
28234 + }
28235 + break;
28236 + case 7500:
28237 + if ((val < 6700) || (val > 8300)) {
28238 + DWC_WARN("HFIR interval for HS core and 60 MHz"
28239 + "clock freq should be from 6700 to 8300\n");
28240 + return;
28241 + }
28242 + break;
28243 + case 60000:
28244 + if ((val < 53640) || (val > 65536)) {
28245 + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
28246 + "clock freq should be from 53640 to 65536\n");
28247 + return;
28248 + }
28249 + break;
28250 + default:
28251 + DWC_WARN("Unknown frame interval\n");
28252 + return;
28253 + break;
28254 +
28255 + }
28256 + hfir.b.frint = val;
28257 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
28258 +}
28259 +
28260 +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
28261 +{
28262 + hcfg_data_t hcfg;
28263 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
28264 + return hcfg.b.modechtimen;
28265 +
28266 +}
28267 +
28268 +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
28269 +{
28270 + hcfg_data_t hcfg;
28271 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
28272 + hcfg.b.modechtimen = val;
28273 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
28274 +}
28275 +
28276 +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
28277 +{
28278 + hprt0_data_t hprt0;
28279 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28280 + hprt0.b.prtres = val;
28281 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28282 +}
28283 +
28284 +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
28285 +{
28286 + dctl_data_t dctl;
28287 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
28288 + return dctl.b.rmtwkupsig;
28289 +}
28290 +
28291 +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
28292 +{
28293 + glpmcfg_data_t lpmcfg;
28294 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28295 +
28296 + DWC_ASSERT(!
28297 + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
28298 + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
28299 + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
28300 +
28301 + return lpmcfg.b.prt_sleep_sts;
28302 +}
28303 +
28304 +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
28305 +{
28306 + glpmcfg_data_t lpmcfg;
28307 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28308 + return lpmcfg.b.rem_wkup_en;
28309 +}
28310 +
28311 +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
28312 +{
28313 + glpmcfg_data_t lpmcfg;
28314 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28315 + return lpmcfg.b.appl_resp;
28316 +}
28317 +
28318 +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
28319 +{
28320 + glpmcfg_data_t lpmcfg;
28321 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28322 + lpmcfg.b.appl_resp = val;
28323 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28324 +}
28325 +
28326 +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
28327 +{
28328 + glpmcfg_data_t lpmcfg;
28329 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28330 + return lpmcfg.b.hsic_connect;
28331 +}
28332 +
28333 +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
28334 +{
28335 + glpmcfg_data_t lpmcfg;
28336 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28337 + lpmcfg.b.hsic_connect = val;
28338 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28339 +}
28340 +
28341 +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
28342 +{
28343 + glpmcfg_data_t lpmcfg;
28344 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28345 + return lpmcfg.b.inv_sel_hsic;
28346 +
28347 +}
28348 +
28349 +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
28350 +{
28351 + glpmcfg_data_t lpmcfg;
28352 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28353 + lpmcfg.b.inv_sel_hsic = val;
28354 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28355 +}
28356 +
28357 +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
28358 +{
28359 + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28360 +}
28361 +
28362 +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
28363 +{
28364 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
28365 +}
28366 +
28367 +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
28368 +{
28369 + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28370 +}
28371 +
28372 +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
28373 +{
28374 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
28375 +}
28376 +
28377 +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
28378 +{
28379 + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
28380 +}
28381 +
28382 +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
28383 +{
28384 + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
28385 +}
28386 +
28387 +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
28388 +{
28389 + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
28390 +}
28391 +
28392 +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
28393 +{
28394 + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
28395 +}
28396 +
28397 +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
28398 +{
28399 + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
28400 +}
28401 +
28402 +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
28403 +{
28404 + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
28405 +}
28406 +
28407 +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
28408 +{
28409 + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
28410 +}
28411 +
28412 +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
28413 +{
28414 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
28415 +}
28416 +
28417 +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
28418 +{
28419 + return DWC_READ_REG32(core_if->host_if->hprt0);
28420 +
28421 +}
28422 +
28423 +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
28424 +{
28425 + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
28426 +}
28427 +
28428 +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
28429 +{
28430 + return DWC_READ_REG32(&core_if->core_global_regs->guid);
28431 +}
28432 +
28433 +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
28434 +{
28435 + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
28436 +}
28437 +
28438 +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
28439 +{
28440 + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
28441 +}
28442 +
28443 +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
28444 +{
28445 + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
28446 +}
28447 +
28448 +/**
28449 + * Start the SRP timer to detect when the SRP does not complete within
28450 + * 6 seconds.
28451 + *
28452 + * @param core_if the pointer to core_if strucure.
28453 + */
28454 +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
28455 +{
28456 + core_if->srp_timer_started = 1;
28457 + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
28458 +}
28459 +
28460 +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
28461 +{
28462 + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
28463 + gotgctl_data_t mem;
28464 + gotgctl_data_t val;
28465 +
28466 + val.d32 = DWC_READ_REG32(addr);
28467 + if (val.b.sesreq) {
28468 + DWC_ERROR("Session Request Already active!\n");
28469 + return;
28470 + }
28471 +
28472 + DWC_INFO("Session Request Initated\n"); //NOTICE
28473 + mem.d32 = DWC_READ_REG32(addr);
28474 + mem.b.sesreq = 1;
28475 + DWC_WRITE_REG32(addr, mem.d32);
28476 +
28477 + /* Start the SRP timer */
28478 + dwc_otg_pcd_start_srp_timer(core_if);
28479 + return;
28480 +}
28481 --- /dev/null
28482 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.h
28483 @@ -0,0 +1,1464 @@
28484 +/* ==========================================================================
28485 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
28486 + * $Revision: #123 $
28487 + * $Date: 2012/08/10 $
28488 + * $Change: 2047372 $
28489 + *
28490 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
28491 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
28492 + * otherwise expressly agreed to in writing between Synopsys and you.
28493 + *
28494 + * The Software IS NOT an item of Licensed Software or Licensed Product under
28495 + * any End User Software License Agreement or Agreement for Licensed Product
28496 + * with Synopsys or any supplement thereto. You are permitted to use and
28497 + * redistribute this Software in source and binary forms, with or without
28498 + * modification, provided that redistributions of source code must retain this
28499 + * notice. You may not view, use, disclose, copy or distribute this file or
28500 + * any information contained herein except pursuant to this license grant from
28501 + * Synopsys. If you do not agree with this notice, including the disclaimer
28502 + * below, then you are not authorized to use the Software.
28503 + *
28504 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
28505 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28506 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28507 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
28508 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28509 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28510 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28511 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28512 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28513 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28514 + * DAMAGE.
28515 + * ========================================================================== */
28516 +
28517 +#if !defined(__DWC_CIL_H__)
28518 +#define __DWC_CIL_H__
28519 +
28520 +#include "dwc_list.h"
28521 +#include "dwc_otg_dbg.h"
28522 +#include "dwc_otg_regs.h"
28523 +
28524 +#include "dwc_otg_core_if.h"
28525 +#include "dwc_otg_adp.h"
28526 +
28527 +/**
28528 + * @file
28529 + * This file contains the interface to the Core Interface Layer.
28530 + */
28531 +
28532 +#ifdef DWC_UTE_CFI
28533 +
28534 +#define MAX_DMA_DESCS_PER_EP 256
28535 +
28536 +/**
28537 + * Enumeration for the data buffer mode
28538 + */
28539 +typedef enum _data_buffer_mode {
28540 + BM_STANDARD = 0, /* data buffer is in normal mode */
28541 + BM_SG = 1, /* data buffer uses the scatter/gather mode */
28542 + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
28543 + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
28544 + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
28545 +} data_buffer_mode_e;
28546 +#endif //DWC_UTE_CFI
28547 +
28548 +/** Macros defined for DWC OTG HW Release version */
28549 +
28550 +#define OTG_CORE_REV_2_60a 0x4F54260A
28551 +#define OTG_CORE_REV_2_71a 0x4F54271A
28552 +#define OTG_CORE_REV_2_72a 0x4F54272A
28553 +#define OTG_CORE_REV_2_80a 0x4F54280A
28554 +#define OTG_CORE_REV_2_81a 0x4F54281A
28555 +#define OTG_CORE_REV_2_90a 0x4F54290A
28556 +#define OTG_CORE_REV_2_91a 0x4F54291A
28557 +#define OTG_CORE_REV_2_92a 0x4F54292A
28558 +#define OTG_CORE_REV_2_93a 0x4F54293A
28559 +#define OTG_CORE_REV_2_94a 0x4F54294A
28560 +#define OTG_CORE_REV_3_00a 0x4F54300A
28561 +
28562 +/**
28563 + * Information for each ISOC packet.
28564 + */
28565 +typedef struct iso_pkt_info {
28566 + uint32_t offset;
28567 + uint32_t length;
28568 + int32_t status;
28569 +} iso_pkt_info_t;
28570 +
28571 +/**
28572 + * The <code>dwc_ep</code> structure represents the state of a single
28573 + * endpoint when acting in device mode. It contains the data items
28574 + * needed for an endpoint to be activated and transfer packets.
28575 + */
28576 +typedef struct dwc_ep {
28577 + /** EP number used for register address lookup */
28578 + uint8_t num;
28579 + /** EP direction 0 = OUT */
28580 + unsigned is_in:1;
28581 + /** EP active. */
28582 + unsigned active:1;
28583 +
28584 + /**
28585 + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
28586 + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
28587 + unsigned tx_fifo_num:4;
28588 + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
28589 + unsigned type:2;
28590 +#define DWC_OTG_EP_TYPE_CONTROL 0
28591 +#define DWC_OTG_EP_TYPE_ISOC 1
28592 +#define DWC_OTG_EP_TYPE_BULK 2
28593 +#define DWC_OTG_EP_TYPE_INTR 3
28594 +
28595 + /** DATA start PID for INTR and BULK EP */
28596 + unsigned data_pid_start:1;
28597 + /** Frame (even/odd) for ISOC EP */
28598 + unsigned even_odd_frame:1;
28599 + /** Max Packet bytes */
28600 + unsigned maxpacket:11;
28601 +
28602 + /** Max Transfer size */
28603 + uint32_t maxxfer;
28604 +
28605 + /** @name Transfer state */
28606 + /** @{ */
28607 +
28608 + /**
28609 + * Pointer to the beginning of the transfer buffer -- do not modify
28610 + * during transfer.
28611 + */
28612 +
28613 + dwc_dma_t dma_addr;
28614 +
28615 + dwc_dma_t dma_desc_addr;
28616 + dwc_otg_dev_dma_desc_t *desc_addr;
28617 +
28618 + uint8_t *start_xfer_buff;
28619 + /** pointer to the transfer buffer */
28620 + uint8_t *xfer_buff;
28621 + /** Number of bytes to transfer */
28622 + unsigned xfer_len:19;
28623 + /** Number of bytes transferred. */
28624 + unsigned xfer_count:19;
28625 + /** Sent ZLP */
28626 + unsigned sent_zlp:1;
28627 + /** Total len for control transfer */
28628 + unsigned total_len:19;
28629 +
28630 + /** stall clear flag */
28631 + unsigned stall_clear_flag:1;
28632 +
28633 + /** SETUP pkt cnt rollover flag for EP0 out*/
28634 + unsigned stp_rollover;
28635 +
28636 +#ifdef DWC_UTE_CFI
28637 + /* The buffer mode */
28638 + data_buffer_mode_e buff_mode;
28639 +
28640 + /* The chain of DMA descriptors.
28641 + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
28642 + */
28643 + dwc_otg_dma_desc_t *descs;
28644 +
28645 + /* The DMA address of the descriptors chain start */
28646 + dma_addr_t descs_dma_addr;
28647 + /** This variable stores the length of the last enqueued request */
28648 + uint32_t cfi_req_len;
28649 +#endif //DWC_UTE_CFI
28650 +
28651 +/** Max DMA Descriptor count for any EP */
28652 +#define MAX_DMA_DESC_CNT 256
28653 + /** Allocated DMA Desc count */
28654 + uint32_t desc_cnt;
28655 +
28656 + /** bInterval */
28657 + uint32_t bInterval;
28658 + /** Next frame num to setup next ISOC transfer */
28659 + uint32_t frame_num;
28660 + /** Indicates SOF number overrun in DSTS */
28661 + uint8_t frm_overrun;
28662 +
28663 +#ifdef DWC_UTE_PER_IO
28664 + /** Next frame num for which will be setup DMA Desc */
28665 + uint32_t xiso_frame_num;
28666 + /** bInterval */
28667 + uint32_t xiso_bInterval;
28668 + /** Count of currently active transfers - shall be either 0 or 1 */
28669 + int xiso_active_xfers;
28670 + int xiso_queued_xfers;
28671 +#endif
28672 +#ifdef DWC_EN_ISOC
28673 + /**
28674 + * Variables specific for ISOC EPs
28675 + *
28676 + */
28677 + /** DMA addresses of ISOC buffers */
28678 + dwc_dma_t dma_addr0;
28679 + dwc_dma_t dma_addr1;
28680 +
28681 + dwc_dma_t iso_dma_desc_addr;
28682 + dwc_otg_dev_dma_desc_t *iso_desc_addr;
28683 +
28684 + /** pointer to the transfer buffers */
28685 + uint8_t *xfer_buff0;
28686 + uint8_t *xfer_buff1;
28687 +
28688 + /** number of ISOC Buffer is processing */
28689 + uint32_t proc_buf_num;
28690 + /** Interval of ISOC Buffer processing */
28691 + uint32_t buf_proc_intrvl;
28692 + /** Data size for regular frame */
28693 + uint32_t data_per_frame;
28694 +
28695 + /* todo - pattern data support is to be implemented in the future */
28696 + /** Data size for pattern frame */
28697 + uint32_t data_pattern_frame;
28698 + /** Frame number of pattern data */
28699 + uint32_t sync_frame;
28700 +
28701 + /** bInterval */
28702 + uint32_t bInterval;
28703 + /** ISO Packet number per frame */
28704 + uint32_t pkt_per_frm;
28705 + /** Next frame num for which will be setup DMA Desc */
28706 + uint32_t next_frame;
28707 + /** Number of packets per buffer processing */
28708 + uint32_t pkt_cnt;
28709 + /** Info for all isoc packets */
28710 + iso_pkt_info_t *pkt_info;
28711 + /** current pkt number */
28712 + uint32_t cur_pkt;
28713 + /** current pkt number */
28714 + uint8_t *cur_pkt_addr;
28715 + /** current pkt number */
28716 + uint32_t cur_pkt_dma_addr;
28717 +#endif /* DWC_EN_ISOC */
28718 +
28719 +/** @} */
28720 +} dwc_ep_t;
28721 +
28722 +/*
28723 + * Reasons for halting a host channel.
28724 + */
28725 +typedef enum dwc_otg_halt_status {
28726 + DWC_OTG_HC_XFER_NO_HALT_STATUS,
28727 + DWC_OTG_HC_XFER_COMPLETE,
28728 + DWC_OTG_HC_XFER_URB_COMPLETE,
28729 + DWC_OTG_HC_XFER_ACK,
28730 + DWC_OTG_HC_XFER_NAK,
28731 + DWC_OTG_HC_XFER_NYET,
28732 + DWC_OTG_HC_XFER_STALL,
28733 + DWC_OTG_HC_XFER_XACT_ERR,
28734 + DWC_OTG_HC_XFER_FRAME_OVERRUN,
28735 + DWC_OTG_HC_XFER_BABBLE_ERR,
28736 + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
28737 + DWC_OTG_HC_XFER_AHB_ERR,
28738 + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
28739 + DWC_OTG_HC_XFER_URB_DEQUEUE
28740 +} dwc_otg_halt_status_e;
28741 +
28742 +/**
28743 + * Host channel descriptor. This structure represents the state of a single
28744 + * host channel when acting in host mode. It contains the data items needed to
28745 + * transfer packets to an endpoint via a host channel.
28746 + */
28747 +typedef struct dwc_hc {
28748 + /** Host channel number used for register address lookup */
28749 + uint8_t hc_num;
28750 +
28751 + /** Device to access */
28752 + unsigned dev_addr:7;
28753 +
28754 + /** EP to access */
28755 + unsigned ep_num:4;
28756 +
28757 + /** EP direction. 0: OUT, 1: IN */
28758 + unsigned ep_is_in:1;
28759 +
28760 + /**
28761 + * EP speed.
28762 + * One of the following values:
28763 + * - DWC_OTG_EP_SPEED_LOW
28764 + * - DWC_OTG_EP_SPEED_FULL
28765 + * - DWC_OTG_EP_SPEED_HIGH
28766 + */
28767 + unsigned speed:2;
28768 +#define DWC_OTG_EP_SPEED_LOW 0
28769 +#define DWC_OTG_EP_SPEED_FULL 1
28770 +#define DWC_OTG_EP_SPEED_HIGH 2
28771 +
28772 + /**
28773 + * Endpoint type.
28774 + * One of the following values:
28775 + * - DWC_OTG_EP_TYPE_CONTROL: 0
28776 + * - DWC_OTG_EP_TYPE_ISOC: 1
28777 + * - DWC_OTG_EP_TYPE_BULK: 2
28778 + * - DWC_OTG_EP_TYPE_INTR: 3
28779 + */
28780 + unsigned ep_type:2;
28781 +
28782 + /** Max packet size in bytes */
28783 + unsigned max_packet:11;
28784 +
28785 + /**
28786 + * PID for initial transaction.
28787 + * 0: DATA0,<br>
28788 + * 1: DATA2,<br>
28789 + * 2: DATA1,<br>
28790 + * 3: MDATA (non-Control EP),
28791 + * SETUP (Control EP)
28792 + */
28793 + unsigned data_pid_start:2;
28794 +#define DWC_OTG_HC_PID_DATA0 0
28795 +#define DWC_OTG_HC_PID_DATA2 1
28796 +#define DWC_OTG_HC_PID_DATA1 2
28797 +#define DWC_OTG_HC_PID_MDATA 3
28798 +#define DWC_OTG_HC_PID_SETUP 3
28799 +
28800 + /** Number of periodic transactions per (micro)frame */
28801 + unsigned multi_count:2;
28802 +
28803 + /** @name Transfer State */
28804 + /** @{ */
28805 +
28806 + /** Pointer to the current transfer buffer position. */
28807 + uint8_t *xfer_buff;
28808 + /**
28809 + * In Buffer DMA mode this buffer will be used
28810 + * if xfer_buff is not DWORD aligned.
28811 + */
28812 + dwc_dma_t align_buff;
28813 + /** Total number of bytes to transfer. */
28814 + uint32_t xfer_len;
28815 + /** Number of bytes transferred so far. */
28816 + uint32_t xfer_count;
28817 + /** Packet count at start of transfer.*/
28818 + uint16_t start_pkt_count;
28819 +
28820 + /**
28821 + * Flag to indicate whether the transfer has been started. Set to 1 if
28822 + * it has been started, 0 otherwise.
28823 + */
28824 + uint8_t xfer_started;
28825 +
28826 + /**
28827 + * Set to 1 to indicate that a PING request should be issued on this
28828 + * channel. If 0, process normally.
28829 + */
28830 + uint8_t do_ping;
28831 +
28832 + /**
28833 + * Set to 1 to indicate that the error count for this transaction is
28834 + * non-zero. Set to 0 if the error count is 0.
28835 + */
28836 + uint8_t error_state;
28837 +
28838 + /**
28839 + * Set to 1 to indicate that this channel should be halted the next
28840 + * time a request is queued for the channel. This is necessary in
28841 + * slave mode if no request queue space is available when an attempt
28842 + * is made to halt the channel.
28843 + */
28844 + uint8_t halt_on_queue;
28845 +
28846 + /**
28847 + * Set to 1 if the host channel has been halted, but the core is not
28848 + * finished flushing queued requests. Otherwise 0.
28849 + */
28850 + uint8_t halt_pending;
28851 +
28852 + /**
28853 + * Reason for halting the host channel.
28854 + */
28855 + dwc_otg_halt_status_e halt_status;
28856 +
28857 + /*
28858 + * Split settings for the host channel
28859 + */
28860 + uint8_t do_split; /**< Enable split for the channel */
28861 + uint8_t complete_split; /**< Enable complete split */
28862 + uint8_t hub_addr; /**< Address of high speed hub */
28863 +
28864 + uint8_t port_addr; /**< Port of the low/full speed device */
28865 + /** Split transaction position
28866 + * One of the following values:
28867 + * - DWC_HCSPLIT_XACTPOS_MID
28868 + * - DWC_HCSPLIT_XACTPOS_BEGIN
28869 + * - DWC_HCSPLIT_XACTPOS_END
28870 + * - DWC_HCSPLIT_XACTPOS_ALL */
28871 + uint8_t xact_pos;
28872 +
28873 + /** Set when the host channel does a short read. */
28874 + uint8_t short_read;
28875 +
28876 + /**
28877 + * Number of requests issued for this channel since it was assigned to
28878 + * the current transfer (not counting PINGs).
28879 + */
28880 + uint8_t requests;
28881 +
28882 + /**
28883 + * Queue Head for the transfer being processed by this channel.
28884 + */
28885 + struct dwc_otg_qh *qh;
28886 +
28887 + /** @} */
28888 +
28889 + /** Entry in list of host channels. */
28890 + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
28891 +
28892 + /** @name Descriptor DMA support */
28893 + /** @{ */
28894 +
28895 + /** Number of Transfer Descriptors */
28896 + uint16_t ntd;
28897 +
28898 + /** Descriptor List DMA address */
28899 + dwc_dma_t desc_list_addr;
28900 +
28901 + /** Scheduling micro-frame bitmap. */
28902 + uint8_t schinfo;
28903 +
28904 + /** @} */
28905 +} dwc_hc_t;
28906 +
28907 +/**
28908 + * The following parameters may be specified when starting the module. These
28909 + * parameters define how the DWC_otg controller should be configured.
28910 + */
28911 +typedef struct dwc_otg_core_params {
28912 + int32_t opt;
28913 +
28914 + /**
28915 + * Specifies the OTG capabilities. The driver will automatically
28916 + * detect the value for this parameter if none is specified.
28917 + * 0 - HNP and SRP capable (default)
28918 + * 1 - SRP Only capable
28919 + * 2 - No HNP/SRP capable
28920 + */
28921 + int32_t otg_cap;
28922 +
28923 + /**
28924 + * Specifies whether to use slave or DMA mode for accessing the data
28925 + * FIFOs. The driver will automatically detect the value for this
28926 + * parameter if none is specified.
28927 + * 0 - Slave
28928 + * 1 - DMA (default, if available)
28929 + */
28930 + int32_t dma_enable;
28931 +
28932 + /**
28933 + * When DMA mode is enabled specifies whether to use address DMA or DMA
28934 + * Descriptor mode for accessing the data FIFOs in device mode. The driver
28935 + * will automatically detect the value for this if none is specified.
28936 + * 0 - address DMA
28937 + * 1 - DMA Descriptor(default, if available)
28938 + */
28939 + int32_t dma_desc_enable;
28940 + /** The DMA Burst size (applicable only for External DMA
28941 + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
28942 + */
28943 + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
28944 +
28945 + /**
28946 + * Specifies the maximum speed of operation in host and device mode.
28947 + * The actual speed depends on the speed of the attached device and
28948 + * the value of phy_type. The actual speed depends on the speed of the
28949 + * attached device.
28950 + * 0 - High Speed (default)
28951 + * 1 - Full Speed
28952 + */
28953 + int32_t speed;
28954 + /** Specifies whether low power mode is supported when attached
28955 + * to a Full Speed or Low Speed device in host mode.
28956 + * 0 - Don't support low power mode (default)
28957 + * 1 - Support low power mode
28958 + */
28959 + int32_t host_support_fs_ls_low_power;
28960 +
28961 + /** Specifies the PHY clock rate in low power mode when connected to a
28962 + * Low Speed device in host mode. This parameter is applicable only if
28963 + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
28964 + * then defaults to 6 MHZ otherwise 48 MHZ.
28965 + *
28966 + * 0 - 48 MHz
28967 + * 1 - 6 MHz
28968 + */
28969 + int32_t host_ls_low_power_phy_clk;
28970 +
28971 + /**
28972 + * 0 - Use cC FIFO size parameters
28973 + * 1 - Allow dynamic FIFO sizing (default)
28974 + */
28975 + int32_t enable_dynamic_fifo;
28976 +
28977 + /** Total number of 4-byte words in the data FIFO memory. This
28978 + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
28979 + * Tx FIFOs.
28980 + * 32 to 32768 (default 8192)
28981 + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
28982 + */
28983 + int32_t data_fifo_size;
28984 +
28985 + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
28986 + * FIFO sizing is enabled.
28987 + * 16 to 32768 (default 1064)
28988 + */
28989 + int32_t dev_rx_fifo_size;
28990 +
28991 + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
28992 + * when dynamic FIFO sizing is enabled.
28993 + * 16 to 32768 (default 1024)
28994 + */
28995 + int32_t dev_nperio_tx_fifo_size;
28996 +
28997 + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
28998 + * mode when dynamic FIFO sizing is enabled.
28999 + * 4 to 768 (default 256)
29000 + */
29001 + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
29002 +
29003 + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
29004 + * FIFO sizing is enabled.
29005 + * 16 to 32768 (default 1024)
29006 + */
29007 + int32_t host_rx_fifo_size;
29008 +
29009 + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
29010 + * when Dynamic FIFO sizing is enabled in the core.
29011 + * 16 to 32768 (default 1024)
29012 + */
29013 + int32_t host_nperio_tx_fifo_size;
29014 +
29015 + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
29016 + * FIFO sizing is enabled.
29017 + * 16 to 32768 (default 1024)
29018 + */
29019 + int32_t host_perio_tx_fifo_size;
29020 +
29021 + /** The maximum transfer size supported in bytes.
29022 + * 2047 to 65,535 (default 65,535)
29023 + */
29024 + int32_t max_transfer_size;
29025 +
29026 + /** The maximum number of packets in a transfer.
29027 + * 15 to 511 (default 511)
29028 + */
29029 + int32_t max_packet_count;
29030 +
29031 + /** The number of host channel registers to use.
29032 + * 1 to 16 (default 12)
29033 + * Note: The FPGA configuration supports a maximum of 12 host channels.
29034 + */
29035 + int32_t host_channels;
29036 +
29037 + /** The number of endpoints in addition to EP0 available for device
29038 + * mode operations.
29039 + * 1 to 15 (default 6 IN and OUT)
29040 + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
29041 + * endpoints in addition to EP0.
29042 + */
29043 + int32_t dev_endpoints;
29044 +
29045 + /**
29046 + * Specifies the type of PHY interface to use. By default, the driver
29047 + * will automatically detect the phy_type.
29048 + *
29049 + * 0 - Full Speed PHY
29050 + * 1 - UTMI+ (default)
29051 + * 2 - ULPI
29052 + */
29053 + int32_t phy_type;
29054 +
29055 + /**
29056 + * Specifies the UTMI+ Data Width. This parameter is
29057 + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
29058 + * PHY_TYPE, this parameter indicates the data width between
29059 + * the MAC and the ULPI Wrapper.) Also, this parameter is
29060 + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
29061 + * to "8 and 16 bits", meaning that the core has been
29062 + * configured to work at either data path width.
29063 + *
29064 + * 8 or 16 bits (default 16)
29065 + */
29066 + int32_t phy_utmi_width;
29067 +
29068 + /**
29069 + * Specifies whether the ULPI operates at double or single
29070 + * data rate. This parameter is only applicable if PHY_TYPE is
29071 + * ULPI.
29072 + *
29073 + * 0 - single data rate ULPI interface with 8 bit wide data
29074 + * bus (default)
29075 + * 1 - double data rate ULPI interface with 4 bit wide data
29076 + * bus
29077 + */
29078 + int32_t phy_ulpi_ddr;
29079 +
29080 + /**
29081 + * Specifies whether to use the internal or external supply to
29082 + * drive the vbus with a ULPI phy.
29083 + */
29084 + int32_t phy_ulpi_ext_vbus;
29085 +
29086 + /**
29087 + * Specifies whether to use the I2Cinterface for full speed PHY. This
29088 + * parameter is only applicable if PHY_TYPE is FS.
29089 + * 0 - No (default)
29090 + * 1 - Yes
29091 + */
29092 + int32_t i2c_enable;
29093 +
29094 + int32_t ulpi_fs_ls;
29095 +
29096 + int32_t ts_dline;
29097 +
29098 + /**
29099 + * Specifies whether dedicated transmit FIFOs are
29100 + * enabled for non periodic IN endpoints in device mode
29101 + * 0 - No
29102 + * 1 - Yes
29103 + */
29104 + int32_t en_multiple_tx_fifo;
29105 +
29106 + /** Number of 4-byte words in each of the Tx FIFOs in device
29107 + * mode when dynamic FIFO sizing is enabled.
29108 + * 4 to 768 (default 256)
29109 + */
29110 + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
29111 +
29112 + /** Thresholding enable flag-
29113 + * bit 0 - enable non-ISO Tx thresholding
29114 + * bit 1 - enable ISO Tx thresholding
29115 + * bit 2 - enable Rx thresholding
29116 + */
29117 + uint32_t thr_ctl;
29118 +
29119 + /** Thresholding length for Tx
29120 + * FIFOs in 32 bit DWORDs
29121 + */
29122 + uint32_t tx_thr_length;
29123 +
29124 + /** Thresholding length for Rx
29125 + * FIFOs in 32 bit DWORDs
29126 + */
29127 + uint32_t rx_thr_length;
29128 +
29129 + /**
29130 + * Specifies whether LPM (Link Power Management) support is enabled
29131 + */
29132 + int32_t lpm_enable;
29133 +
29134 + /** Per Transfer Interrupt
29135 + * mode enable flag
29136 + * 1 - Enabled
29137 + * 0 - Disabled
29138 + */
29139 + int32_t pti_enable;
29140 +
29141 + /** Multi Processor Interrupt
29142 + * mode enable flag
29143 + * 1 - Enabled
29144 + * 0 - Disabled
29145 + */
29146 + int32_t mpi_enable;
29147 +
29148 + /** IS_USB Capability
29149 + * 1 - Enabled
29150 + * 0 - Disabled
29151 + */
29152 + int32_t ic_usb_cap;
29153 +
29154 + /** AHB Threshold Ratio
29155 + * 2'b00 AHB Threshold = MAC Threshold
29156 + * 2'b01 AHB Threshold = 1/2 MAC Threshold
29157 + * 2'b10 AHB Threshold = 1/4 MAC Threshold
29158 + * 2'b11 AHB Threshold = 1/8 MAC Threshold
29159 + */
29160 + int32_t ahb_thr_ratio;
29161 +
29162 + /** ADP Support
29163 + * 1 - Enabled
29164 + * 0 - Disabled
29165 + */
29166 + int32_t adp_supp_enable;
29167 +
29168 + /** HFIR Reload Control
29169 + * 0 - The HFIR cannot be reloaded dynamically.
29170 + * 1 - Allow dynamic reloading of the HFIR register during runtime.
29171 + */
29172 + int32_t reload_ctl;
29173 +
29174 + /** DCFG: Enable device Out NAK
29175 + * 0 - The core does not set NAK after Bulk Out transfer complete.
29176 + * 1 - The core sets NAK after Bulk OUT transfer complete.
29177 + */
29178 + int32_t dev_out_nak;
29179 +
29180 + /** DCFG: Enable Continue on BNA
29181 + * After receiving BNA interrupt the core disables the endpoint,when the
29182 + * endpoint is re-enabled by the application the core starts processing
29183 + * 0 - from the DOEPDMA descriptor
29184 + * 1 - from the descriptor which received the BNA.
29185 + */
29186 + int32_t cont_on_bna;
29187 +
29188 + /** GAHBCFG: AHB Single Support
29189 + * This bit when programmed supports SINGLE transfers for remainder
29190 + * data in a transfer for DMA mode of operation.
29191 + * 0 - in this case the remainder data will be sent using INCR burst size.
29192 + * 1 - in this case the remainder data will be sent using SINGLE burst size.
29193 + */
29194 + int32_t ahb_single;
29195 +
29196 + /** Core Power down mode
29197 + * 0 - No Power Down is enabled
29198 + * 1 - Reserved
29199 + * 2 - Complete Power Down (Hibernation)
29200 + */
29201 + int32_t power_down;
29202 +
29203 + /** OTG revision supported
29204 + * 0 - OTG 1.3 revision
29205 + * 1 - OTG 2.0 revision
29206 + */
29207 + int32_t otg_ver;
29208 +
29209 +} dwc_otg_core_params_t;
29210 +
29211 +#ifdef DEBUG
29212 +struct dwc_otg_core_if;
29213 +typedef struct hc_xfer_info {
29214 + struct dwc_otg_core_if *core_if;
29215 + dwc_hc_t *hc;
29216 +} hc_xfer_info_t;
29217 +#endif
29218 +
29219 +typedef struct ep_xfer_info {
29220 + struct dwc_otg_core_if *core_if;
29221 + dwc_ep_t *ep;
29222 + uint8_t state;
29223 +} ep_xfer_info_t;
29224 +/*
29225 + * Device States
29226 + */
29227 +typedef enum dwc_otg_lx_state {
29228 + /** On state */
29229 + DWC_OTG_L0,
29230 + /** LPM sleep state*/
29231 + DWC_OTG_L1,
29232 + /** USB suspend state*/
29233 + DWC_OTG_L2,
29234 + /** Off state*/
29235 + DWC_OTG_L3
29236 +} dwc_otg_lx_state_e;
29237 +
29238 +struct dwc_otg_global_regs_backup {
29239 + uint32_t gotgctl_local;
29240 + uint32_t gintmsk_local;
29241 + uint32_t gahbcfg_local;
29242 + uint32_t gusbcfg_local;
29243 + uint32_t grxfsiz_local;
29244 + uint32_t gnptxfsiz_local;
29245 +#ifdef CONFIG_USB_DWC_OTG_LPM
29246 + uint32_t glpmcfg_local;
29247 +#endif
29248 + uint32_t gi2cctl_local;
29249 + uint32_t hptxfsiz_local;
29250 + uint32_t pcgcctl_local;
29251 + uint32_t gdfifocfg_local;
29252 + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
29253 + uint32_t gpwrdn_local;
29254 + uint32_t xhib_pcgcctl;
29255 + uint32_t xhib_gpwrdn;
29256 +};
29257 +
29258 +struct dwc_otg_host_regs_backup {
29259 + uint32_t hcfg_local;
29260 + uint32_t haintmsk_local;
29261 + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
29262 + uint32_t hprt0_local;
29263 + uint32_t hfir_local;
29264 +};
29265 +
29266 +struct dwc_otg_dev_regs_backup {
29267 + uint32_t dcfg;
29268 + uint32_t dctl;
29269 + uint32_t daintmsk;
29270 + uint32_t diepmsk;
29271 + uint32_t doepmsk;
29272 + uint32_t diepctl[MAX_EPS_CHANNELS];
29273 + uint32_t dieptsiz[MAX_EPS_CHANNELS];
29274 + uint32_t diepdma[MAX_EPS_CHANNELS];
29275 +};
29276 +/**
29277 + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
29278 + * the DWC_otg controller acting in either host or device mode. It
29279 + * represents the programming view of the controller as a whole.
29280 + */
29281 +struct dwc_otg_core_if {
29282 + /** Parameters that define how the core should be configured.*/
29283 + dwc_otg_core_params_t *core_params;
29284 +
29285 + /** Core Global registers starting at offset 000h. */
29286 + dwc_otg_core_global_regs_t *core_global_regs;
29287 +
29288 + /** Device-specific information */
29289 + dwc_otg_dev_if_t *dev_if;
29290 + /** Host-specific information */
29291 + dwc_otg_host_if_t *host_if;
29292 +
29293 + /** Value from SNPSID register */
29294 + uint32_t snpsid;
29295 +
29296 + /*
29297 + * Set to 1 if the core PHY interface bits in USBCFG have been
29298 + * initialized.
29299 + */
29300 + uint8_t phy_init_done;
29301 +
29302 + /*
29303 + * SRP Success flag, set by srp success interrupt in FS I2C mode
29304 + */
29305 + uint8_t srp_success;
29306 + uint8_t srp_timer_started;
29307 + /** Timer for SRP. If it expires before SRP is successful
29308 + * clear the SRP. */
29309 + dwc_timer_t *srp_timer;
29310 +
29311 +#ifdef DWC_DEV_SRPCAP
29312 + /* This timer is needed to power on the hibernated host core if SRP is not
29313 + * initiated on connected SRP capable device for limited period of time
29314 + */
29315 + uint8_t pwron_timer_started;
29316 + dwc_timer_t *pwron_timer;
29317 +#endif
29318 + /* Common configuration information */
29319 + /** Power and Clock Gating Control Register */
29320 + volatile uint32_t *pcgcctl;
29321 +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
29322 +
29323 + /** Push/pop addresses for endpoints or host channels.*/
29324 + uint32_t *data_fifo[MAX_EPS_CHANNELS];
29325 +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
29326 +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
29327 +
29328 + /** Total RAM for FIFOs (Bytes) */
29329 + uint16_t total_fifo_size;
29330 + /** Size of Rx FIFO (Bytes) */
29331 + uint16_t rx_fifo_size;
29332 + /** Size of Non-periodic Tx FIFO (Bytes) */
29333 + uint16_t nperio_tx_fifo_size;
29334 +
29335 + /** 1 if DMA is enabled, 0 otherwise. */
29336 + uint8_t dma_enable;
29337 +
29338 + /** 1 if DMA descriptor is enabled, 0 otherwise. */
29339 + uint8_t dma_desc_enable;
29340 +
29341 + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
29342 + uint8_t pti_enh_enable;
29343 +
29344 + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
29345 + uint8_t multiproc_int_enable;
29346 +
29347 + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
29348 + uint8_t en_multiple_tx_fifo;
29349 +
29350 + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
29351 + * process of being queued */
29352 + uint8_t queuing_high_bandwidth;
29353 +
29354 + /** Hardware Configuration -- stored here for convenience.*/
29355 + hwcfg1_data_t hwcfg1;
29356 + hwcfg2_data_t hwcfg2;
29357 + hwcfg3_data_t hwcfg3;
29358 + hwcfg4_data_t hwcfg4;
29359 + fifosize_data_t hptxfsiz;
29360 +
29361 + /** Host and Device Configuration -- stored here for convenience.*/
29362 + hcfg_data_t hcfg;
29363 + dcfg_data_t dcfg;
29364 +
29365 + /** The operational State, during transations
29366 + * (a_host>>a_peripherial and b_device=>b_host) this may not
29367 + * match the core but allows the software to determine
29368 + * transitions.
29369 + */
29370 + uint8_t op_state;
29371 +
29372 + /**
29373 + * Set to 1 if the HCD needs to be restarted on a session request
29374 + * interrupt. This is required if no connector ID status change has
29375 + * occurred since the HCD was last disconnected.
29376 + */
29377 + uint8_t restart_hcd_on_session_req;
29378 +
29379 + /** HCD callbacks */
29380 + /** A-Device is a_host */
29381 +#define A_HOST (1)
29382 + /** A-Device is a_suspend */
29383 +#define A_SUSPEND (2)
29384 + /** A-Device is a_peripherial */
29385 +#define A_PERIPHERAL (3)
29386 + /** B-Device is operating as a Peripheral. */
29387 +#define B_PERIPHERAL (4)
29388 + /** B-Device is operating as a Host. */
29389 +#define B_HOST (5)
29390 +
29391 + /** HCD callbacks */
29392 + struct dwc_otg_cil_callbacks *hcd_cb;
29393 + /** PCD callbacks */
29394 + struct dwc_otg_cil_callbacks *pcd_cb;
29395 +
29396 + /** Device mode Periodic Tx FIFO Mask */
29397 + uint32_t p_tx_msk;
29398 + /** Device mode Periodic Tx FIFO Mask */
29399 + uint32_t tx_msk;
29400 +
29401 + /** Workqueue object used for handling several interrupts */
29402 + dwc_workq_t *wq_otg;
29403 +
29404 + /** Timer object used for handling "Wakeup Detected" Interrupt */
29405 + dwc_timer_t *wkp_timer;
29406 + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
29407 + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
29408 + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
29409 + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
29410 +#ifdef DEBUG
29411 + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
29412 +
29413 + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
29414 + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
29415 +
29416 + uint32_t hfnum_7_samples;
29417 + uint64_t hfnum_7_frrem_accum;
29418 + uint32_t hfnum_0_samples;
29419 + uint64_t hfnum_0_frrem_accum;
29420 + uint32_t hfnum_other_samples;
29421 + uint64_t hfnum_other_frrem_accum;
29422 +#endif
29423 +
29424 +#ifdef DWC_UTE_CFI
29425 + uint16_t pwron_rxfsiz;
29426 + uint16_t pwron_gnptxfsiz;
29427 + uint16_t pwron_txfsiz[15];
29428 +
29429 + uint16_t init_rxfsiz;
29430 + uint16_t init_gnptxfsiz;
29431 + uint16_t init_txfsiz[15];
29432 +#endif
29433 +
29434 + /** Lx state of device */
29435 + dwc_otg_lx_state_e lx_state;
29436 +
29437 + /** Saved Core Global registers */
29438 + struct dwc_otg_global_regs_backup *gr_backup;
29439 + /** Saved Host registers */
29440 + struct dwc_otg_host_regs_backup *hr_backup;
29441 + /** Saved Device registers */
29442 + struct dwc_otg_dev_regs_backup *dr_backup;
29443 +
29444 + /** Power Down Enable */
29445 + uint32_t power_down;
29446 +
29447 + /** ADP support Enable */
29448 + uint32_t adp_enable;
29449 +
29450 + /** ADP structure object */
29451 + dwc_otg_adp_t adp;
29452 +
29453 + /** hibernation/suspend flag */
29454 + int hibernation_suspend;
29455 +
29456 + /** Device mode extended hibernation flag */
29457 + int xhib;
29458 +
29459 + /** OTG revision supported */
29460 + uint32_t otg_ver;
29461 +
29462 + /** OTG status flag used for HNP polling */
29463 + uint8_t otg_sts;
29464 +
29465 + /** Pointer to either hcd->lock or pcd->lock */
29466 + dwc_spinlock_t *lock;
29467 +
29468 + /** Start predict NextEP based on Learning Queue if equal 1,
29469 + * also used as counter of disabled NP IN EP's */
29470 + uint8_t start_predict;
29471 +
29472 + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
29473 + * active, 0xff otherwise */
29474 + uint8_t nextep_seq[MAX_EPS_CHANNELS];
29475 +
29476 + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
29477 + uint8_t first_in_nextep_seq;
29478 +
29479 + /** Frame number while entering to ISR - needed for ISOCs **/
29480 + uint32_t frame_num;
29481 +
29482 +};
29483 +
29484 +#ifdef DEBUG
29485 +/*
29486 + * This function is called when transfer is timed out.
29487 + */
29488 +extern void hc_xfer_timeout(void *ptr);
29489 +#endif
29490 +
29491 +/*
29492 + * This function is called when transfer is timed out on endpoint.
29493 + */
29494 +extern void ep_xfer_timeout(void *ptr);
29495 +
29496 +/*
29497 + * The following functions are functions for works
29498 + * using during handling some interrupts
29499 + */
29500 +extern void w_conn_id_status_change(void *p);
29501 +
29502 +extern void w_wakeup_detected(void *p);
29503 +
29504 +/** Saves global register values into system memory. */
29505 +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
29506 +/** Saves device register values into system memory. */
29507 +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
29508 +/** Saves host register values into system memory. */
29509 +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
29510 +/** Restore global register values. */
29511 +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
29512 +/** Restore host register values. */
29513 +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
29514 +/** Restore device register values. */
29515 +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
29516 + int rem_wakeup);
29517 +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
29518 +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
29519 + int is_host);
29520 +
29521 +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
29522 + int restore_mode, int reset);
29523 +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
29524 + int rem_wakeup, int reset);
29525 +
29526 +/*
29527 + * The following functions support initialization of the CIL driver component
29528 + * and the DWC_otg controller.
29529 + */
29530 +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
29531 +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
29532 +
29533 +/** @name Device CIL Functions
29534 + * The following functions support managing the DWC_otg controller in device
29535 + * mode.
29536 + */
29537 +/**@{*/
29538 +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
29539 +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
29540 + uint32_t * _dest);
29541 +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
29542 +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29543 +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29544 +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29545 +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
29546 + dwc_ep_t * _ep);
29547 +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
29548 + dwc_ep_t * _ep);
29549 +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
29550 + dwc_ep_t * _ep);
29551 +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
29552 + dwc_ep_t * _ep);
29553 +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
29554 + dwc_ep_t * _ep, int _dma);
29555 +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29556 +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
29557 + dwc_ep_t * _ep);
29558 +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
29559 +
29560 +#ifdef DWC_EN_ISOC
29561 +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
29562 + dwc_ep_t * ep);
29563 +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
29564 + dwc_ep_t * ep);
29565 +#endif /* DWC_EN_ISOC */
29566 +/**@}*/
29567 +
29568 +/** @name Host CIL Functions
29569 + * The following functions support managing the DWC_otg controller in host
29570 + * mode.
29571 + */
29572 +/**@{*/
29573 +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29574 +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
29575 + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
29576 +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29577 +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
29578 + dwc_hc_t * _hc);
29579 +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
29580 + dwc_hc_t * _hc);
29581 +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29582 +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
29583 + dwc_hc_t * _hc);
29584 +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
29585 +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
29586 +
29587 +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
29588 + dwc_hc_t * hc);
29589 +
29590 +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
29591 +
29592 +/* Macro used to clear one channel interrupt */
29593 +#define clear_hc_int(_hc_regs_, _intr_) \
29594 +do { \
29595 + hcint_data_t hcint_clear = {.d32 = 0}; \
29596 + hcint_clear.b._intr_ = 1; \
29597 + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
29598 +} while (0)
29599 +
29600 +/*
29601 + * Macro used to disable one channel interrupt. Channel interrupts are
29602 + * disabled when the channel is halted or released by the interrupt handler.
29603 + * There is no need to handle further interrupts of that type until the
29604 + * channel is re-assigned. In fact, subsequent handling may cause crashes
29605 + * because the channel structures are cleaned up when the channel is released.
29606 + */
29607 +#define disable_hc_int(_hc_regs_, _intr_) \
29608 +do { \
29609 + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
29610 + hcintmsk.b._intr_ = 1; \
29611 + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
29612 +} while (0)
29613 +
29614 +/**
29615 + * This function Reads HPRT0 in preparation to modify. It keeps the
29616 + * WC bits 0 so that if they are read as 1, they won't clear when you
29617 + * write it back
29618 + */
29619 +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
29620 +{
29621 + hprt0_data_t hprt0;
29622 + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
29623 + hprt0.b.prtena = 0;
29624 + hprt0.b.prtconndet = 0;
29625 + hprt0.b.prtenchng = 0;
29626 + hprt0.b.prtovrcurrchng = 0;
29627 + return hprt0.d32;
29628 +}
29629 +
29630 +/**@}*/
29631 +
29632 +/** @name Common CIL Functions
29633 + * The following functions support managing the DWC_otg controller in either
29634 + * device or host mode.
29635 + */
29636 +/**@{*/
29637 +
29638 +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
29639 + uint8_t * dest, uint16_t bytes);
29640 +
29641 +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
29642 +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
29643 +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
29644 +
29645 +/**
29646 + * This function returns the Core Interrupt register.
29647 + */
29648 +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
29649 +{
29650 + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
29651 + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
29652 +}
29653 +
29654 +/**
29655 + * This function returns the OTG Interrupt register.
29656 + */
29657 +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
29658 +{
29659 + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
29660 +}
29661 +
29662 +/**
29663 + * This function reads the Device All Endpoints Interrupt register and
29664 + * returns the IN endpoint interrupt bits.
29665 + */
29666 +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
29667 + core_if)
29668 +{
29669 +
29670 + uint32_t v;
29671 +
29672 + if (core_if->multiproc_int_enable) {
29673 + v = DWC_READ_REG32(&core_if->dev_if->
29674 + dev_global_regs->deachint) &
29675 + DWC_READ_REG32(&core_if->
29676 + dev_if->dev_global_regs->deachintmsk);
29677 + } else {
29678 + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
29679 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
29680 + }
29681 + return (v & 0xffff);
29682 +}
29683 +
29684 +/**
29685 + * This function reads the Device All Endpoints Interrupt register and
29686 + * returns the OUT endpoint interrupt bits.
29687 + */
29688 +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
29689 + core_if)
29690 +{
29691 + uint32_t v;
29692 +
29693 + if (core_if->multiproc_int_enable) {
29694 + v = DWC_READ_REG32(&core_if->dev_if->
29695 + dev_global_regs->deachint) &
29696 + DWC_READ_REG32(&core_if->
29697 + dev_if->dev_global_regs->deachintmsk);
29698 + } else {
29699 + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
29700 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
29701 + }
29702 +
29703 + return ((v & 0xffff0000) >> 16);
29704 +}
29705 +
29706 +/**
29707 + * This function returns the Device IN EP Interrupt register
29708 + */
29709 +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
29710 + dwc_ep_t * ep)
29711 +{
29712 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
29713 + uint32_t v, msk, emp;
29714 +
29715 + if (core_if->multiproc_int_enable) {
29716 + msk =
29717 + DWC_READ_REG32(&dev_if->
29718 + dev_global_regs->diepeachintmsk[ep->num]);
29719 + emp =
29720 + DWC_READ_REG32(&dev_if->
29721 + dev_global_regs->dtknqr4_fifoemptymsk);
29722 + msk |= ((emp >> ep->num) & 0x1) << 7;
29723 + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
29724 + } else {
29725 + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
29726 + emp =
29727 + DWC_READ_REG32(&dev_if->
29728 + dev_global_regs->dtknqr4_fifoemptymsk);
29729 + msk |= ((emp >> ep->num) & 0x1) << 7;
29730 + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
29731 + }
29732 +
29733 + return v;
29734 +}
29735 +
29736 +/**
29737 + * This function returns the Device OUT EP Interrupt register
29738 + */
29739 +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
29740 + _core_if, dwc_ep_t * _ep)
29741 +{
29742 + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
29743 + uint32_t v;
29744 + doepmsk_data_t msk = {.d32 = 0 };
29745 +
29746 + if (_core_if->multiproc_int_enable) {
29747 + msk.d32 =
29748 + DWC_READ_REG32(&dev_if->
29749 + dev_global_regs->doepeachintmsk[_ep->num]);
29750 + if (_core_if->pti_enh_enable) {
29751 + msk.b.pktdrpsts = 1;
29752 + }
29753 + v = DWC_READ_REG32(&dev_if->
29754 + out_ep_regs[_ep->num]->doepint) & msk.d32;
29755 + } else {
29756 + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
29757 + if (_core_if->pti_enh_enable) {
29758 + msk.b.pktdrpsts = 1;
29759 + }
29760 + v = DWC_READ_REG32(&dev_if->
29761 + out_ep_regs[_ep->num]->doepint) & msk.d32;
29762 + }
29763 + return v;
29764 +}
29765 +
29766 +/**
29767 + * This function returns the Host All Channel Interrupt register
29768 + */
29769 +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
29770 + _core_if)
29771 +{
29772 + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
29773 +}
29774 +
29775 +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
29776 + _core_if, dwc_hc_t * _hc)
29777 +{
29778 + return (DWC_READ_REG32
29779 + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
29780 +}
29781 +
29782 +/**
29783 + * This function returns the mode of the operation, host or device.
29784 + *
29785 + * @return 0 - Device Mode, 1 - Host Mode
29786 + */
29787 +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
29788 +{
29789 + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
29790 +}
29791 +
29792 +/**@}*/
29793 +
29794 +/**
29795 + * DWC_otg CIL callback structure. This structure allows the HCD and
29796 + * PCD to register functions used for starting and stopping the PCD
29797 + * and HCD for role change on for a DRD.
29798 + */
29799 +typedef struct dwc_otg_cil_callbacks {
29800 + /** Start function for role change */
29801 + int (*start) (void *_p);
29802 + /** Stop Function for role change */
29803 + int (*stop) (void *_p);
29804 + /** Disconnect Function for role change */
29805 + int (*disconnect) (void *_p);
29806 + /** Resume/Remote wakeup Function */
29807 + int (*resume_wakeup) (void *_p);
29808 + /** Suspend function */
29809 + int (*suspend) (void *_p);
29810 + /** Session Start (SRP) */
29811 + int (*session_start) (void *_p);
29812 +#ifdef CONFIG_USB_DWC_OTG_LPM
29813 + /** Sleep (switch to L0 state) */
29814 + int (*sleep) (void *_p);
29815 +#endif
29816 + /** Pointer passed to start() and stop() */
29817 + void *p;
29818 +} dwc_otg_cil_callbacks_t;
29819 +
29820 +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
29821 + dwc_otg_cil_callbacks_t * _cb,
29822 + void *_p);
29823 +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
29824 + dwc_otg_cil_callbacks_t * _cb,
29825 + void *_p);
29826 +
29827 +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
29828 +
29829 +//////////////////////////////////////////////////////////////////////
29830 +/** Start the HCD. Helper function for using the HCD callbacks.
29831 + *
29832 + * @param core_if Programming view of DWC_otg controller.
29833 + */
29834 +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
29835 +{
29836 + if (core_if->hcd_cb && core_if->hcd_cb->start) {
29837 + core_if->hcd_cb->start(core_if->hcd_cb->p);
29838 + }
29839 +}
29840 +
29841 +/** Stop the HCD. Helper function for using the HCD callbacks.
29842 + *
29843 + * @param core_if Programming view of DWC_otg controller.
29844 + */
29845 +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
29846 +{
29847 + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
29848 + core_if->hcd_cb->stop(core_if->hcd_cb->p);
29849 + }
29850 +}
29851 +
29852 +/** Disconnect the HCD. Helper function for using the HCD callbacks.
29853 + *
29854 + * @param core_if Programming view of DWC_otg controller.
29855 + */
29856 +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
29857 +{
29858 + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
29859 + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
29860 + }
29861 +}
29862 +
29863 +/** Inform the HCD the a New Session has begun. Helper function for
29864 + * using the HCD callbacks.
29865 + *
29866 + * @param core_if Programming view of DWC_otg controller.
29867 + */
29868 +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
29869 +{
29870 + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
29871 + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
29872 + }
29873 +}
29874 +
29875 +#ifdef CONFIG_USB_DWC_OTG_LPM
29876 +/**
29877 + * Inform the HCD about LPM sleep.
29878 + * Helper function for using the HCD callbacks.
29879 + *
29880 + * @param core_if Programming view of DWC_otg controller.
29881 + */
29882 +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
29883 +{
29884 + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
29885 + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
29886 + }
29887 +}
29888 +#endif
29889 +
29890 +/** Resume the HCD. Helper function for using the HCD callbacks.
29891 + *
29892 + * @param core_if Programming view of DWC_otg controller.
29893 + */
29894 +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
29895 +{
29896 + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
29897 + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
29898 + }
29899 +}
29900 +
29901 +/** Start the PCD. Helper function for using the PCD callbacks.
29902 + *
29903 + * @param core_if Programming view of DWC_otg controller.
29904 + */
29905 +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
29906 +{
29907 + if (core_if->pcd_cb && core_if->pcd_cb->start) {
29908 + core_if->pcd_cb->start(core_if->pcd_cb->p);
29909 + }
29910 +}
29911 +
29912 +/** Stop the PCD. Helper function for using the PCD callbacks.
29913 + *
29914 + * @param core_if Programming view of DWC_otg controller.
29915 + */
29916 +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
29917 +{
29918 + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
29919 + core_if->pcd_cb->stop(core_if->pcd_cb->p);
29920 + }
29921 +}
29922 +
29923 +/** Suspend the PCD. Helper function for using the PCD callbacks.
29924 + *
29925 + * @param core_if Programming view of DWC_otg controller.
29926 + */
29927 +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
29928 +{
29929 + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
29930 + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
29931 + }
29932 +}
29933 +
29934 +/** Resume the PCD. Helper function for using the PCD callbacks.
29935 + *
29936 + * @param core_if Programming view of DWC_otg controller.
29937 + */
29938 +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
29939 +{
29940 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
29941 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
29942 + }
29943 +}
29944 +
29945 +//////////////////////////////////////////////////////////////////////
29946 +
29947 +#endif
29948 --- /dev/null
29949 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
29950 @@ -0,0 +1,1601 @@
29951 +/* ==========================================================================
29952 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
29953 + * $Revision: #32 $
29954 + * $Date: 2012/08/10 $
29955 + * $Change: 2047372 $
29956 + *
29957 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
29958 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
29959 + * otherwise expressly agreed to in writing between Synopsys and you.
29960 + *
29961 + * The Software IS NOT an item of Licensed Software or Licensed Product under
29962 + * any End User Software License Agreement or Agreement for Licensed Product
29963 + * with Synopsys or any supplement thereto. You are permitted to use and
29964 + * redistribute this Software in source and binary forms, with or without
29965 + * modification, provided that redistributions of source code must retain this
29966 + * notice. You may not view, use, disclose, copy or distribute this file or
29967 + * any information contained herein except pursuant to this license grant from
29968 + * Synopsys. If you do not agree with this notice, including the disclaimer
29969 + * below, then you are not authorized to use the Software.
29970 + *
29971 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
29972 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29973 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29974 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
29975 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29976 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29977 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29978 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29979 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29980 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29981 + * DAMAGE.
29982 + * ========================================================================== */
29983 +
29984 +/** @file
29985 + *
29986 + * The Core Interface Layer provides basic services for accessing and
29987 + * managing the DWC_otg hardware. These services are used by both the
29988 + * Host Controller Driver and the Peripheral Controller Driver.
29989 + *
29990 + * This file contains the Common Interrupt handlers.
29991 + */
29992 +#include "dwc_os.h"
29993 +#include "dwc_otg_regs.h"
29994 +#include "dwc_otg_cil.h"
29995 +#include "dwc_otg_driver.h"
29996 +#include "dwc_otg_pcd.h"
29997 +#include "dwc_otg_hcd.h"
29998 +
29999 +#ifdef DEBUG
30000 +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
30001 +{
30002 + return (core_if->op_state == A_HOST ? "a_host" :
30003 + (core_if->op_state == A_SUSPEND ? "a_suspend" :
30004 + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
30005 + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
30006 + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
30007 +}
30008 +#endif
30009 +
30010 +/** This function will log a debug message
30011 + *
30012 + * @param core_if Programming view of DWC_otg controller.
30013 + */
30014 +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
30015 +{
30016 + gintsts_data_t gintsts;
30017 + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
30018 + dwc_otg_mode(core_if) ? "Host" : "Device");
30019 +
30020 + /* Clear interrupt */
30021 + gintsts.d32 = 0;
30022 + gintsts.b.modemismatch = 1;
30023 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30024 + return 1;
30025 +}
30026 +
30027 +/**
30028 + * This function handles the OTG Interrupts. It reads the OTG
30029 + * Interrupt Register (GOTGINT) to determine what interrupt has
30030 + * occurred.
30031 + *
30032 + * @param core_if Programming view of DWC_otg controller.
30033 + */
30034 +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
30035 +{
30036 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
30037 + gotgint_data_t gotgint;
30038 + gotgctl_data_t gotgctl;
30039 + gintmsk_data_t gintmsk;
30040 + gpwrdn_data_t gpwrdn;
30041 +
30042 + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
30043 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30044 + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
30045 + op_state_str(core_if));
30046 +
30047 + if (gotgint.b.sesenddet) {
30048 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30049 + "Session End Detected++ (%s)\n",
30050 + op_state_str(core_if));
30051 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30052 +
30053 + if (core_if->op_state == B_HOST) {
30054 + cil_pcd_start(core_if);
30055 + core_if->op_state = B_PERIPHERAL;
30056 + } else {
30057 + /* If not B_HOST and Device HNP still set. HNP
30058 + * Did not succeed!*/
30059 + if (gotgctl.b.devhnpen) {
30060 + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
30061 + __DWC_ERROR("Device Not Connected/Responding!\n");
30062 + }
30063 +
30064 + /* If Session End Detected the B-Cable has
30065 + * been disconnected. */
30066 + /* Reset PCD and Gadget driver to a
30067 + * clean state. */
30068 + core_if->lx_state = DWC_OTG_L0;
30069 + DWC_SPINUNLOCK(core_if->lock);
30070 + cil_pcd_stop(core_if);
30071 + DWC_SPINLOCK(core_if->lock);
30072 +
30073 + if (core_if->adp_enable) {
30074 + if (core_if->power_down == 2) {
30075 + gpwrdn.d32 = 0;
30076 + gpwrdn.b.pwrdnswtch = 1;
30077 + DWC_MODIFY_REG32(&core_if->
30078 + core_global_regs->
30079 + gpwrdn, gpwrdn.d32, 0);
30080 + }
30081 +
30082 + gpwrdn.d32 = 0;
30083 + gpwrdn.b.pmuintsel = 1;
30084 + gpwrdn.b.pmuactv = 1;
30085 + DWC_MODIFY_REG32(&core_if->core_global_regs->
30086 + gpwrdn, 0, gpwrdn.d32);
30087 +
30088 + dwc_otg_adp_sense_start(core_if);
30089 + }
30090 + }
30091 +
30092 + gotgctl.d32 = 0;
30093 + gotgctl.b.devhnpen = 1;
30094 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
30095 + }
30096 + if (gotgint.b.sesreqsucstschng) {
30097 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30098 + "Session Reqeust Success Status Change++\n");
30099 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30100 + if (gotgctl.b.sesreqscs) {
30101 +
30102 + if ((core_if->core_params->phy_type ==
30103 + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
30104 + core_if->srp_success = 1;
30105 + } else {
30106 + DWC_SPINUNLOCK(core_if->lock);
30107 + cil_pcd_resume(core_if);
30108 + DWC_SPINLOCK(core_if->lock);
30109 + /* Clear Session Request */
30110 + gotgctl.d32 = 0;
30111 + gotgctl.b.sesreq = 1;
30112 + DWC_MODIFY_REG32(&global_regs->gotgctl,
30113 + gotgctl.d32, 0);
30114 + }
30115 + }
30116 + }
30117 + if (gotgint.b.hstnegsucstschng) {
30118 + /* Print statements during the HNP interrupt handling
30119 + * can cause it to fail.*/
30120 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30121 + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
30122 + * this does not help*/
30123 + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
30124 + dwc_udelay(100);
30125 + if (gotgctl.b.hstnegscs) {
30126 + if (dwc_otg_is_host_mode(core_if)) {
30127 + core_if->op_state = B_HOST;
30128 + /*
30129 + * Need to disable SOF interrupt immediately.
30130 + * When switching from device to host, the PCD
30131 + * interrupt handler won't handle the
30132 + * interrupt if host mode is already set. The
30133 + * HCD interrupt handler won't get called if
30134 + * the HCD state is HALT. This means that the
30135 + * interrupt does not get handled and Linux
30136 + * complains loudly.
30137 + */
30138 + gintmsk.d32 = 0;
30139 + gintmsk.b.sofintr = 1;
30140 + DWC_MODIFY_REG32(&global_regs->gintmsk,
30141 + gintmsk.d32, 0);
30142 + /* Call callback function with spin lock released */
30143 + DWC_SPINUNLOCK(core_if->lock);
30144 + cil_pcd_stop(core_if);
30145 + /*
30146 + * Initialize the Core for Host mode.
30147 + */
30148 + cil_hcd_start(core_if);
30149 + DWC_SPINLOCK(core_if->lock);
30150 + core_if->op_state = B_HOST;
30151 + }
30152 + } else {
30153 + gotgctl.d32 = 0;
30154 + gotgctl.b.hnpreq = 1;
30155 + gotgctl.b.devhnpen = 1;
30156 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
30157 + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
30158 + __DWC_ERROR("Device Not Connected/Responding\n");
30159 + }
30160 + }
30161 + if (gotgint.b.hstnegdet) {
30162 + /* The disconnect interrupt is set at the same time as
30163 + * Host Negotiation Detected. During the mode
30164 + * switch all interrupts are cleared so the disconnect
30165 + * interrupt handler will not get executed.
30166 + */
30167 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30168 + "Host Negotiation Detected++ (%s)\n",
30169 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30170 + "Device"));
30171 + if (dwc_otg_is_device_mode(core_if)) {
30172 + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
30173 + core_if->op_state);
30174 + DWC_SPINUNLOCK(core_if->lock);
30175 + cil_hcd_disconnect(core_if);
30176 + cil_pcd_start(core_if);
30177 + DWC_SPINLOCK(core_if->lock);
30178 + core_if->op_state = A_PERIPHERAL;
30179 + } else {
30180 + /*
30181 + * Need to disable SOF interrupt immediately. When
30182 + * switching from device to host, the PCD interrupt
30183 + * handler won't handle the interrupt if host mode is
30184 + * already set. The HCD interrupt handler won't get
30185 + * called if the HCD state is HALT. This means that
30186 + * the interrupt does not get handled and Linux
30187 + * complains loudly.
30188 + */
30189 + gintmsk.d32 = 0;
30190 + gintmsk.b.sofintr = 1;
30191 + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
30192 + DWC_SPINUNLOCK(core_if->lock);
30193 + cil_pcd_stop(core_if);
30194 + cil_hcd_start(core_if);
30195 + DWC_SPINLOCK(core_if->lock);
30196 + core_if->op_state = A_HOST;
30197 + }
30198 + }
30199 + if (gotgint.b.adevtoutchng) {
30200 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30201 + "A-Device Timeout Change++\n");
30202 + }
30203 + if (gotgint.b.debdone) {
30204 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
30205 + }
30206 +
30207 + /* Clear GOTGINT */
30208 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
30209 +
30210 + return 1;
30211 +}
30212 +
30213 +void w_conn_id_status_change(void *p)
30214 +{
30215 + dwc_otg_core_if_t *core_if = p;
30216 + uint32_t count = 0;
30217 + gotgctl_data_t gotgctl = {.d32 = 0 };
30218 +
30219 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
30220 + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
30221 + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
30222 +
30223 + /* B-Device connector (Device Mode) */
30224 + if (gotgctl.b.conidsts) {
30225 + /* Wait for switch to device mode. */
30226 + while (!dwc_otg_is_device_mode(core_if)) {
30227 + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
30228 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30229 + "Peripheral"));
30230 + dwc_mdelay(100);
30231 + if (++count > 10000)
30232 + break;
30233 + }
30234 + DWC_ASSERT(++count < 10000,
30235 + "Connection id status change timed out");
30236 + core_if->op_state = B_PERIPHERAL;
30237 + dwc_otg_core_init(core_if);
30238 + dwc_otg_enable_global_interrupts(core_if);
30239 + cil_pcd_start(core_if);
30240 + } else {
30241 + /* A-Device connector (Host Mode) */
30242 + while (!dwc_otg_is_host_mode(core_if)) {
30243 + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
30244 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30245 + "Peripheral"));
30246 + dwc_mdelay(100);
30247 + if (++count > 10000)
30248 + break;
30249 + }
30250 + DWC_ASSERT(++count < 10000,
30251 + "Connection id status change timed out");
30252 + core_if->op_state = A_HOST;
30253 + /*
30254 + * Initialize the Core for Host mode.
30255 + */
30256 + dwc_otg_core_init(core_if);
30257 + dwc_otg_enable_global_interrupts(core_if);
30258 + cil_hcd_start(core_if);
30259 + }
30260 +}
30261 +
30262 +/**
30263 + * This function handles the Connector ID Status Change Interrupt. It
30264 + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
30265 + * is a Device to Host Mode transition or a Host Mode to Device
30266 + * Transition.
30267 + *
30268 + * This only occurs when the cable is connected/removed from the PHY
30269 + * connector.
30270 + *
30271 + * @param core_if Programming view of DWC_otg controller.
30272 + */
30273 +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
30274 +{
30275 +
30276 + /*
30277 + * Need to disable SOF interrupt immediately. If switching from device
30278 + * to host, the PCD interrupt handler won't handle the interrupt if
30279 + * host mode is already set. The HCD interrupt handler won't get
30280 + * called if the HCD state is HALT. This means that the interrupt does
30281 + * not get handled and Linux complains loudly.
30282 + */
30283 + gintmsk_data_t gintmsk = {.d32 = 0 };
30284 + gintsts_data_t gintsts = {.d32 = 0 };
30285 +
30286 + gintmsk.b.sofintr = 1;
30287 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
30288 +
30289 + DWC_DEBUGPL(DBG_CIL,
30290 + " ++Connector ID Status Change Interrupt++ (%s)\n",
30291 + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
30292 +
30293 + DWC_SPINUNLOCK(core_if->lock);
30294 +
30295 + /*
30296 + * Need to schedule a work, as there are possible DELAY function calls
30297 + * Release lock before scheduling workq as it holds spinlock during scheduling
30298 + */
30299 +
30300 + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
30301 + core_if, "connection id status change");
30302 + DWC_SPINLOCK(core_if->lock);
30303 +
30304 + /* Set flag and clear interrupt */
30305 + gintsts.b.conidstschng = 1;
30306 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30307 +
30308 + return 1;
30309 +}
30310 +
30311 +/**
30312 + * This interrupt indicates that a device is initiating the Session
30313 + * Request Protocol to request the host to turn on bus power so a new
30314 + * session can begin. The handler responds by turning on bus power. If
30315 + * the DWC_otg controller is in low power mode, the handler brings the
30316 + * controller out of low power mode before turning on bus power.
30317 + *
30318 + * @param core_if Programming view of DWC_otg controller.
30319 + */
30320 +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
30321 +{
30322 + gintsts_data_t gintsts;
30323 +
30324 +#ifndef DWC_HOST_ONLY
30325 + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
30326 +
30327 + if (dwc_otg_is_device_mode(core_if)) {
30328 + DWC_PRINTF("SRP: Device mode\n");
30329 + } else {
30330 + hprt0_data_t hprt0;
30331 + DWC_PRINTF("SRP: Host mode\n");
30332 +
30333 + /* Turn on the port power bit. */
30334 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
30335 + hprt0.b.prtpwr = 1;
30336 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
30337 +
30338 + /* Start the Connection timer. So a message can be displayed
30339 + * if connect does not occur within 10 seconds. */
30340 + cil_hcd_session_start(core_if);
30341 + }
30342 +#endif
30343 +
30344 + /* Clear interrupt */
30345 + gintsts.d32 = 0;
30346 + gintsts.b.sessreqintr = 1;
30347 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30348 +
30349 + return 1;
30350 +}
30351 +
30352 +void w_wakeup_detected(void *p)
30353 +{
30354 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
30355 + /*
30356 + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
30357 + * so that OPT tests pass with all PHYs).
30358 + */
30359 + hprt0_data_t hprt0 = {.d32 = 0 };
30360 +#if 0
30361 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
30362 + /* Restart the Phy Clock */
30363 + pcgcctl.b.stoppclk = 1;
30364 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
30365 + dwc_udelay(10);
30366 +#endif //0
30367 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
30368 + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
30369 +// dwc_mdelay(70);
30370 + hprt0.b.prtres = 0; /* Resume */
30371 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
30372 + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
30373 + DWC_READ_REG32(core_if->host_if->hprt0));
30374 +
30375 + cil_hcd_resume(core_if);
30376 +
30377 + /** Change to L0 state*/
30378 + core_if->lx_state = DWC_OTG_L0;
30379 +}
30380 +
30381 +/**
30382 + * This interrupt indicates that the DWC_otg controller has detected a
30383 + * resume or remote wakeup sequence. If the DWC_otg controller is in
30384 + * low power mode, the handler must brings the controller out of low
30385 + * power mode. The controller automatically begins resume
30386 + * signaling. The handler schedules a time to stop resume signaling.
30387 + */
30388 +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
30389 +{
30390 + gintsts_data_t gintsts;
30391 +
30392 + DWC_DEBUGPL(DBG_ANY,
30393 + "++Resume and Remote Wakeup Detected Interrupt++\n");
30394 +
30395 + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
30396 +
30397 + if (dwc_otg_is_device_mode(core_if)) {
30398 + dctl_data_t dctl = {.d32 = 0 };
30399 + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
30400 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
30401 + dsts));
30402 + if (core_if->lx_state == DWC_OTG_L2) {
30403 +#ifdef PARTIAL_POWER_DOWN
30404 + if (core_if->hwcfg4.b.power_optimiz) {
30405 + pcgcctl_data_t power = {.d32 = 0 };
30406 +
30407 + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
30408 + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
30409 + power.d32);
30410 +
30411 + power.b.stoppclk = 0;
30412 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30413 +
30414 + power.b.pwrclmp = 0;
30415 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30416 +
30417 + power.b.rstpdwnmodule = 0;
30418 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30419 + }
30420 +#endif
30421 + /* Clear the Remote Wakeup Signaling */
30422 + dctl.b.rmtwkupsig = 1;
30423 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
30424 + dctl, dctl.d32, 0);
30425 +
30426 + DWC_SPINUNLOCK(core_if->lock);
30427 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
30428 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
30429 + }
30430 + DWC_SPINLOCK(core_if->lock);
30431 + } else {
30432 + glpmcfg_data_t lpmcfg;
30433 + lpmcfg.d32 =
30434 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
30435 + lpmcfg.b.hird_thres &= (~(1 << 4));
30436 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
30437 + lpmcfg.d32);
30438 + }
30439 + /** Change to L0 state*/
30440 + core_if->lx_state = DWC_OTG_L0;
30441 + } else {
30442 + if (core_if->lx_state != DWC_OTG_L1) {
30443 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
30444 +
30445 + /* Restart the Phy Clock */
30446 + pcgcctl.b.stoppclk = 1;
30447 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
30448 + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
30449 + } else {
30450 + /** Change to L0 state*/
30451 + core_if->lx_state = DWC_OTG_L0;
30452 + }
30453 + }
30454 +
30455 + /* Clear interrupt */
30456 + gintsts.d32 = 0;
30457 + gintsts.b.wkupintr = 1;
30458 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30459 +
30460 + return 1;
30461 +}
30462 +
30463 +/**
30464 + * This interrupt indicates that the Wakeup Logic has detected a
30465 + * Device disconnect.
30466 + */
30467 +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
30468 +{
30469 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
30470 + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
30471 + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30472 +
30473 + DWC_PRINTF("%s called\n", __FUNCTION__);
30474 +
30475 + if (!core_if->hibernation_suspend) {
30476 + DWC_PRINTF("Already exited from Hibernation\n");
30477 + return 1;
30478 + }
30479 +
30480 + /* Switch on the voltage to the core */
30481 + gpwrdn.b.pwrdnswtch = 1;
30482 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30483 + dwc_udelay(10);
30484 +
30485 + /* Reset the core */
30486 + gpwrdn.d32 = 0;
30487 + gpwrdn.b.pwrdnrstn = 1;
30488 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30489 + dwc_udelay(10);
30490 +
30491 + /* Disable power clamps*/
30492 + gpwrdn.d32 = 0;
30493 + gpwrdn.b.pwrdnclmp = 1;
30494 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30495 +
30496 + /* Remove reset the core signal */
30497 + gpwrdn.d32 = 0;
30498 + gpwrdn.b.pwrdnrstn = 1;
30499 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30500 + dwc_udelay(10);
30501 +
30502 + /* Disable PMU interrupt */
30503 + gpwrdn.d32 = 0;
30504 + gpwrdn.b.pmuintsel = 1;
30505 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30506 +
30507 + core_if->hibernation_suspend = 0;
30508 +
30509 + /* Disable PMU */
30510 + gpwrdn.d32 = 0;
30511 + gpwrdn.b.pmuactv = 1;
30512 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30513 + dwc_udelay(10);
30514 +
30515 + if (gpwrdn_temp.b.idsts) {
30516 + core_if->op_state = B_PERIPHERAL;
30517 + dwc_otg_core_init(core_if);
30518 + dwc_otg_enable_global_interrupts(core_if);
30519 + cil_pcd_start(core_if);
30520 + } else {
30521 + core_if->op_state = A_HOST;
30522 + dwc_otg_core_init(core_if);
30523 + dwc_otg_enable_global_interrupts(core_if);
30524 + cil_hcd_start(core_if);
30525 + }
30526 +
30527 + return 1;
30528 +}
30529 +
30530 +/**
30531 + * This interrupt indicates that the Wakeup Logic has detected a
30532 + * remote wakeup sequence.
30533 + */
30534 +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
30535 +{
30536 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30537 + DWC_DEBUGPL(DBG_ANY,
30538 + "++Powerdown Remote Wakeup Detected Interrupt++\n");
30539 +
30540 + if (!core_if->hibernation_suspend) {
30541 + DWC_PRINTF("Already exited from Hibernation\n");
30542 + return 1;
30543 + }
30544 +
30545 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30546 + if (gpwrdn.b.idsts) { // Device Mode
30547 + if ((core_if->power_down == 2)
30548 + && (core_if->hibernation_suspend == 1)) {
30549 + dwc_otg_device_hibernation_restore(core_if, 0, 0);
30550 + }
30551 + } else {
30552 + if ((core_if->power_down == 2)
30553 + && (core_if->hibernation_suspend == 1)) {
30554 + dwc_otg_host_hibernation_restore(core_if, 1, 0);
30555 + }
30556 + }
30557 + return 1;
30558 +}
30559 +
30560 +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
30561 +{
30562 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30563 + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
30564 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
30565 +
30566 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
30567 + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30568 + if (core_if->power_down == 2) {
30569 + if (!core_if->hibernation_suspend) {
30570 + DWC_PRINTF("Already exited from Hibernation\n");
30571 + return 1;
30572 + }
30573 + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
30574 + /* Switch on the voltage to the core */
30575 + gpwrdn.b.pwrdnswtch = 1;
30576 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30577 + dwc_udelay(10);
30578 +
30579 + /* Reset the core */
30580 + gpwrdn.d32 = 0;
30581 + gpwrdn.b.pwrdnrstn = 1;
30582 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30583 + dwc_udelay(10);
30584 +
30585 + /* Disable power clamps */
30586 + gpwrdn.d32 = 0;
30587 + gpwrdn.b.pwrdnclmp = 1;
30588 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30589 +
30590 + /* Remove reset the core signal */
30591 + gpwrdn.d32 = 0;
30592 + gpwrdn.b.pwrdnrstn = 1;
30593 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30594 + dwc_udelay(10);
30595 +
30596 + /* Disable PMU interrupt */
30597 + gpwrdn.d32 = 0;
30598 + gpwrdn.b.pmuintsel = 1;
30599 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30600 +
30601 + /*Indicates that we are exiting from hibernation */
30602 + core_if->hibernation_suspend = 0;
30603 +
30604 + /* Disable PMU */
30605 + gpwrdn.d32 = 0;
30606 + gpwrdn.b.pmuactv = 1;
30607 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30608 + dwc_udelay(10);
30609 +
30610 + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
30611 + if (gpwrdn.b.dis_vbus == 1) {
30612 + gpwrdn.d32 = 0;
30613 + gpwrdn.b.dis_vbus = 1;
30614 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30615 + }
30616 +
30617 + if (gpwrdn_temp.b.idsts) {
30618 + core_if->op_state = B_PERIPHERAL;
30619 + dwc_otg_core_init(core_if);
30620 + dwc_otg_enable_global_interrupts(core_if);
30621 + cil_pcd_start(core_if);
30622 + } else {
30623 + core_if->op_state = A_HOST;
30624 + dwc_otg_core_init(core_if);
30625 + dwc_otg_enable_global_interrupts(core_if);
30626 + cil_hcd_start(core_if);
30627 + }
30628 + }
30629 +
30630 + if (core_if->adp_enable) {
30631 + uint8_t is_host = 0;
30632 + DWC_SPINUNLOCK(core_if->lock);
30633 + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
30634 +#ifndef DWC_HOST_ONLY
30635 + if (gpwrdn_temp.b.idsts)
30636 + core_if->lock = otg_dev->pcd->lock;
30637 +#endif
30638 +#ifndef DWC_DEVICE_ONLY
30639 + if (!gpwrdn_temp.b.idsts) {
30640 + core_if->lock = otg_dev->hcd->lock;
30641 + is_host = 1;
30642 + }
30643 +#endif
30644 + DWC_PRINTF("RESTART ADP\n");
30645 + if (core_if->adp.probe_enabled)
30646 + dwc_otg_adp_probe_stop(core_if);
30647 + if (core_if->adp.sense_enabled)
30648 + dwc_otg_adp_sense_stop(core_if);
30649 + if (core_if->adp.sense_timer_started)
30650 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
30651 + if (core_if->adp.vbuson_timer_started)
30652 + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
30653 + core_if->adp.probe_timer_values[0] = -1;
30654 + core_if->adp.probe_timer_values[1] = -1;
30655 + core_if->adp.sense_timer_started = 0;
30656 + core_if->adp.vbuson_timer_started = 0;
30657 + core_if->adp.probe_counter = 0;
30658 + core_if->adp.gpwrdn = 0;
30659 +
30660 + /* Disable PMU and restart ADP */
30661 + gpwrdn_temp.d32 = 0;
30662 + gpwrdn_temp.b.pmuactv = 1;
30663 + gpwrdn_temp.b.pmuintsel = 1;
30664 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30665 + DWC_PRINTF("Check point 1\n");
30666 + dwc_mdelay(110);
30667 + dwc_otg_adp_start(core_if, is_host);
30668 + DWC_SPINLOCK(core_if->lock);
30669 + }
30670 +
30671 +
30672 + return 1;
30673 +}
30674 +
30675 +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
30676 +{
30677 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30678 + int32_t otg_cap_param = core_if->core_params->otg_cap;
30679 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
30680 +
30681 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30682 + if (core_if->power_down == 2) {
30683 + if (!core_if->hibernation_suspend) {
30684 + DWC_PRINTF("Already exited from Hibernation\n");
30685 + return 1;
30686 + }
30687 +
30688 + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
30689 + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
30690 + gpwrdn.b.bsessvld == 0) {
30691 + /* Save gpwrdn register for further usage if stschng interrupt */
30692 + core_if->gr_backup->gpwrdn_local =
30693 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30694 + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
30695 + return 1;
30696 + }
30697 +
30698 + /* Switch on the voltage to the core */
30699 + gpwrdn.d32 = 0;
30700 + gpwrdn.b.pwrdnswtch = 1;
30701 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30702 + dwc_udelay(10);
30703 +
30704 + /* Reset the core */
30705 + gpwrdn.d32 = 0;
30706 + gpwrdn.b.pwrdnrstn = 1;
30707 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30708 + dwc_udelay(10);
30709 +
30710 + /* Disable power clamps */
30711 + gpwrdn.d32 = 0;
30712 + gpwrdn.b.pwrdnclmp = 1;
30713 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30714 +
30715 + /* Remove reset the core signal */
30716 + gpwrdn.d32 = 0;
30717 + gpwrdn.b.pwrdnrstn = 1;
30718 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30719 + dwc_udelay(10);
30720 +
30721 + /* Disable PMU interrupt */
30722 + gpwrdn.d32 = 0;
30723 + gpwrdn.b.pmuintsel = 1;
30724 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30725 + dwc_udelay(10);
30726 +
30727 + /*Indicates that we are exiting from hibernation */
30728 + core_if->hibernation_suspend = 0;
30729 +
30730 + /* Disable PMU */
30731 + gpwrdn.d32 = 0;
30732 + gpwrdn.b.pmuactv = 1;
30733 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30734 + dwc_udelay(10);
30735 +
30736 + core_if->op_state = B_PERIPHERAL;
30737 + dwc_otg_core_init(core_if);
30738 + dwc_otg_enable_global_interrupts(core_if);
30739 + cil_pcd_start(core_if);
30740 +
30741 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
30742 + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
30743 + /*
30744 + * Initiate SRP after initial ADP probe.
30745 + */
30746 + dwc_otg_initiate_srp(core_if);
30747 + }
30748 + }
30749 +
30750 + return 1;
30751 +}
30752 +/**
30753 + * This interrupt indicates that the Wakeup Logic has detected a
30754 + * status change either on IDDIG or BSessVld.
30755 + */
30756 +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
30757 +{
30758 + uint32_t retval = 0;
30759 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30760 + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
30761 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
30762 +
30763 + DWC_PRINTF("%s called\n", __FUNCTION__);
30764 +
30765 + if (core_if->power_down == 2) {
30766 + if (core_if->hibernation_suspend <= 0) {
30767 + DWC_PRINTF("Already exited from Hibernation\n");
30768 + return 1;
30769 + } else
30770 + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
30771 +
30772 + } else {
30773 + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
30774 + }
30775 +
30776 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30777 +
30778 + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
30779 + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
30780 + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
30781 + retval = dwc_otg_handle_pwrdn_session_change(core_if);
30782 + }
30783 +
30784 + return retval;
30785 +}
30786 +
30787 +/**
30788 + * This interrupt indicates that the Wakeup Logic has detected a
30789 + * SRP.
30790 + */
30791 +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
30792 +{
30793 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30794 +
30795 + DWC_PRINTF("%s called\n", __FUNCTION__);
30796 +
30797 + if (!core_if->hibernation_suspend) {
30798 + DWC_PRINTF("Already exited from Hibernation\n");
30799 + return 1;
30800 + }
30801 +#ifdef DWC_DEV_SRPCAP
30802 + if (core_if->pwron_timer_started) {
30803 + core_if->pwron_timer_started = 0;
30804 + DWC_TIMER_CANCEL(core_if->pwron_timer);
30805 + }
30806 +#endif
30807 +
30808 + /* Switch on the voltage to the core */
30809 + gpwrdn.b.pwrdnswtch = 1;
30810 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30811 + dwc_udelay(10);
30812 +
30813 + /* Reset the core */
30814 + gpwrdn.d32 = 0;
30815 + gpwrdn.b.pwrdnrstn = 1;
30816 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30817 + dwc_udelay(10);
30818 +
30819 + /* Disable power clamps */
30820 + gpwrdn.d32 = 0;
30821 + gpwrdn.b.pwrdnclmp = 1;
30822 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30823 +
30824 + /* Remove reset the core signal */
30825 + gpwrdn.d32 = 0;
30826 + gpwrdn.b.pwrdnrstn = 1;
30827 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30828 + dwc_udelay(10);
30829 +
30830 + /* Disable PMU interrupt */
30831 + gpwrdn.d32 = 0;
30832 + gpwrdn.b.pmuintsel = 1;
30833 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30834 +
30835 + /* Indicates that we are exiting from hibernation */
30836 + core_if->hibernation_suspend = 0;
30837 +
30838 + /* Disable PMU */
30839 + gpwrdn.d32 = 0;
30840 + gpwrdn.b.pmuactv = 1;
30841 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30842 + dwc_udelay(10);
30843 +
30844 + /* Programm Disable VBUS to 0 */
30845 + gpwrdn.d32 = 0;
30846 + gpwrdn.b.dis_vbus = 1;
30847 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30848 +
30849 + /*Initialize the core as Host */
30850 + core_if->op_state = A_HOST;
30851 + dwc_otg_core_init(core_if);
30852 + dwc_otg_enable_global_interrupts(core_if);
30853 + cil_hcd_start(core_if);
30854 +
30855 + return 1;
30856 +}
30857 +
30858 +/** This interrupt indicates that restore command after Hibernation
30859 + * was completed by the core. */
30860 +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
30861 +{
30862 + pcgcctl_data_t pcgcctl;
30863 + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
30864 +
30865 + //TODO De-assert restore signal. 8.a
30866 + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
30867 + if (pcgcctl.b.restoremode == 1) {
30868 + gintmsk_data_t gintmsk = {.d32 = 0 };
30869 + /*
30870 + * If restore mode is Remote Wakeup,
30871 + * unmask Remote Wakeup interrupt.
30872 + */
30873 + gintmsk.b.wkupintr = 1;
30874 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
30875 + 0, gintmsk.d32);
30876 + }
30877 +
30878 + return 1;
30879 +}
30880 +
30881 +/**
30882 + * This interrupt indicates that a device has been disconnected from
30883 + * the root port.
30884 + */
30885 +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
30886 +{
30887 + gintsts_data_t gintsts;
30888 +
30889 + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
30890 + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
30891 + op_state_str(core_if));
30892 +
30893 +/** @todo Consolidate this if statement. */
30894 +#ifndef DWC_HOST_ONLY
30895 + if (core_if->op_state == B_HOST) {
30896 + /* If in device mode Disconnect and stop the HCD, then
30897 + * start the PCD. */
30898 + DWC_SPINUNLOCK(core_if->lock);
30899 + cil_hcd_disconnect(core_if);
30900 + cil_pcd_start(core_if);
30901 + DWC_SPINLOCK(core_if->lock);
30902 + core_if->op_state = B_PERIPHERAL;
30903 + } else if (dwc_otg_is_device_mode(core_if)) {
30904 + gotgctl_data_t gotgctl = {.d32 = 0 };
30905 + gotgctl.d32 =
30906 + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
30907 + if (gotgctl.b.hstsethnpen == 1) {
30908 + /* Do nothing, if HNP in process the OTG
30909 + * interrupt "Host Negotiation Detected"
30910 + * interrupt will do the mode switch.
30911 + */
30912 + } else if (gotgctl.b.devhnpen == 0) {
30913 + /* If in device mode Disconnect and stop the HCD, then
30914 + * start the PCD. */
30915 + DWC_SPINUNLOCK(core_if->lock);
30916 + cil_hcd_disconnect(core_if);
30917 + cil_pcd_start(core_if);
30918 + DWC_SPINLOCK(core_if->lock);
30919 + core_if->op_state = B_PERIPHERAL;
30920 + } else {
30921 + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
30922 + }
30923 + } else {
30924 + if (core_if->op_state == A_HOST) {
30925 + /* A-Cable still connected but device disconnected. */
30926 + DWC_SPINUNLOCK(core_if->lock);
30927 + cil_hcd_disconnect(core_if);
30928 + DWC_SPINLOCK(core_if->lock);
30929 + if (core_if->adp_enable) {
30930 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
30931 + cil_hcd_stop(core_if);
30932 + /* Enable Power Down Logic */
30933 + gpwrdn.b.pmuintsel = 1;
30934 + gpwrdn.b.pmuactv = 1;
30935 + DWC_MODIFY_REG32(&core_if->core_global_regs->
30936 + gpwrdn, 0, gpwrdn.d32);
30937 + dwc_otg_adp_probe_start(core_if);
30938 +
30939 + /* Power off the core */
30940 + if (core_if->power_down == 2) {
30941 + gpwrdn.d32 = 0;
30942 + gpwrdn.b.pwrdnswtch = 1;
30943 + DWC_MODIFY_REG32
30944 + (&core_if->core_global_regs->gpwrdn,
30945 + gpwrdn.d32, 0);
30946 + }
30947 + }
30948 + }
30949 + }
30950 +#endif
30951 + /* Change to L3(OFF) state */
30952 + core_if->lx_state = DWC_OTG_L3;
30953 +
30954 + gintsts.d32 = 0;
30955 + gintsts.b.disconnect = 1;
30956 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30957 + return 1;
30958 +}
30959 +
30960 +/**
30961 + * This interrupt indicates that SUSPEND state has been detected on
30962 + * the USB.
30963 + *
30964 + * For HNP the USB Suspend interrupt signals the change from
30965 + * "a_peripheral" to "a_host".
30966 + *
30967 + * When power management is enabled the core will be put in low power
30968 + * mode.
30969 + */
30970 +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
30971 +{
30972 + dsts_data_t dsts;
30973 + gintsts_data_t gintsts;
30974 + dcfg_data_t dcfg;
30975 +
30976 + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
30977 +
30978 + if (dwc_otg_is_device_mode(core_if)) {
30979 + /* Check the Device status register to determine if the Suspend
30980 + * state is active. */
30981 + dsts.d32 =
30982 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
30983 + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
30984 + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
30985 + "HWCFG4.power Optimize=%d\n",
30986 + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
30987 +
30988 +#ifdef PARTIAL_POWER_DOWN
30989 +/** @todo Add a module parameter for power management. */
30990 +
30991 + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
30992 + pcgcctl_data_t power = {.d32 = 0 };
30993 + DWC_DEBUGPL(DBG_CIL, "suspend\n");
30994 +
30995 + power.b.pwrclmp = 1;
30996 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30997 +
30998 + power.b.rstpdwnmodule = 1;
30999 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
31000 +
31001 + power.b.stoppclk = 1;
31002 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
31003 +
31004 + } else {
31005 + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
31006 + }
31007 +#endif
31008 + /* PCD callback for suspend. Release the lock inside of callback function */
31009 + cil_pcd_suspend(core_if);
31010 + if (core_if->power_down == 2)
31011 + {
31012 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
31013 + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
31014 + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
31015 +
31016 + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
31017 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31018 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31019 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
31020 +
31021 + /* Change to L2(suspend) state */
31022 + core_if->lx_state = DWC_OTG_L2;
31023 +
31024 + /* Clear interrupt in gintsts */
31025 + gintsts.d32 = 0;
31026 + gintsts.b.usbsuspend = 1;
31027 + DWC_WRITE_REG32(&core_if->core_global_regs->
31028 + gintsts, gintsts.d32);
31029 + DWC_PRINTF("Start of hibernation completed\n");
31030 + dwc_otg_save_global_regs(core_if);
31031 + dwc_otg_save_dev_regs(core_if);
31032 +
31033 + gusbcfg.d32 =
31034 + DWC_READ_REG32(&core_if->core_global_regs->
31035 + gusbcfg);
31036 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
31037 + /* ULPI interface */
31038 + /* Suspend the Phy Clock */
31039 + pcgcctl.d32 = 0;
31040 + pcgcctl.b.stoppclk = 1;
31041 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
31042 + pcgcctl.d32);
31043 + dwc_udelay(10);
31044 + gpwrdn.b.pmuactv = 1;
31045 + DWC_MODIFY_REG32(&core_if->
31046 + core_global_regs->
31047 + gpwrdn, 0, gpwrdn.d32);
31048 + } else {
31049 + /* UTMI+ Interface */
31050 + gpwrdn.b.pmuactv = 1;
31051 + DWC_MODIFY_REG32(&core_if->
31052 + core_global_regs->
31053 + gpwrdn, 0, gpwrdn.d32);
31054 + dwc_udelay(10);
31055 + pcgcctl.b.stoppclk = 1;
31056 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
31057 + pcgcctl.d32);
31058 + dwc_udelay(10);
31059 + }
31060 +
31061 + /* Set flag to indicate that we are in hibernation */
31062 + core_if->hibernation_suspend = 1;
31063 + /* Enable interrupts from wake up logic */
31064 + gpwrdn.d32 = 0;
31065 + gpwrdn.b.pmuintsel = 1;
31066 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31067 + gpwrdn, 0, gpwrdn.d32);
31068 + dwc_udelay(10);
31069 +
31070 + /* Unmask device mode interrupts in GPWRDN */
31071 + gpwrdn.d32 = 0;
31072 + gpwrdn.b.rst_det_msk = 1;
31073 + gpwrdn.b.lnstchng_msk = 1;
31074 + gpwrdn.b.sts_chngint_msk = 1;
31075 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31076 + gpwrdn, 0, gpwrdn.d32);
31077 + dwc_udelay(10);
31078 +
31079 + /* Enable Power Down Clamp */
31080 + gpwrdn.d32 = 0;
31081 + gpwrdn.b.pwrdnclmp = 1;
31082 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31083 + gpwrdn, 0, gpwrdn.d32);
31084 + dwc_udelay(10);
31085 +
31086 + /* Switch off VDD */
31087 + gpwrdn.d32 = 0;
31088 + gpwrdn.b.pwrdnswtch = 1;
31089 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31090 + gpwrdn, 0, gpwrdn.d32);
31091 +
31092 + /* Save gpwrdn register for further usage if stschng interrupt */
31093 + core_if->gr_backup->gpwrdn_local =
31094 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31095 + DWC_PRINTF("Hibernation completed\n");
31096 +
31097 + return 1;
31098 + }
31099 + } else if (core_if->power_down == 3) {
31100 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31101 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
31102 + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
31103 + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
31104 +
31105 + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
31106 + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
31107 + core_if->xhib = 1;
31108 +
31109 + /* Clear interrupt in gintsts */
31110 + gintsts.d32 = 0;
31111 + gintsts.b.usbsuspend = 1;
31112 + DWC_WRITE_REG32(&core_if->core_global_regs->
31113 + gintsts, gintsts.d32);
31114 +
31115 + dwc_otg_save_global_regs(core_if);
31116 + dwc_otg_save_dev_regs(core_if);
31117 +
31118 + /* Wait for 10 PHY clocks */
31119 + dwc_udelay(10);
31120 +
31121 + /* Program GPIO register while entering to xHib */
31122 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
31123 +
31124 + pcgcctl.b.enbl_extnd_hiber = 1;
31125 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31126 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31127 +
31128 + pcgcctl.d32 = 0;
31129 + pcgcctl.b.extnd_hiber_pwrclmp = 1;
31130 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31131 +
31132 + pcgcctl.d32 = 0;
31133 + pcgcctl.b.extnd_hiber_switch = 1;
31134 + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31135 + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
31136 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31137 +
31138 + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
31139 +
31140 + return 1;
31141 + }
31142 + }
31143 + } else {
31144 + if (core_if->op_state == A_PERIPHERAL) {
31145 + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
31146 + /* Clear the a_peripheral flag, back to a_host. */
31147 + DWC_SPINUNLOCK(core_if->lock);
31148 + cil_pcd_stop(core_if);
31149 + cil_hcd_start(core_if);
31150 + DWC_SPINLOCK(core_if->lock);
31151 + core_if->op_state = A_HOST;
31152 + }
31153 + }
31154 +
31155 + /* Change to L2(suspend) state */
31156 + core_if->lx_state = DWC_OTG_L2;
31157 +
31158 + /* Clear interrupt */
31159 + gintsts.d32 = 0;
31160 + gintsts.b.usbsuspend = 1;
31161 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31162 +
31163 + return 1;
31164 +}
31165 +
31166 +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
31167 +{
31168 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31169 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31170 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
31171 +
31172 + dwc_udelay(10);
31173 +
31174 + /* Program GPIO register while entering to xHib */
31175 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
31176 +
31177 + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
31178 + pcgcctl.b.extnd_hiber_pwrclmp = 0;
31179 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31180 + dwc_udelay(10);
31181 +
31182 + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
31183 + gpwrdn.b.restore = 1;
31184 + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
31185 + dwc_udelay(10);
31186 +
31187 + restore_lpm_i2c_regs(core_if);
31188 +
31189 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31190 + pcgcctl.b.max_xcvrselect = 1;
31191 + pcgcctl.b.ess_reg_restored = 0;
31192 + pcgcctl.b.extnd_hiber_switch = 0;
31193 + pcgcctl.b.extnd_hiber_pwrclmp = 0;
31194 + pcgcctl.b.enbl_extnd_hiber = 1;
31195 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31196 +
31197 + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
31198 + gahbcfg.b.glblintrmsk = 1;
31199 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
31200 +
31201 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
31202 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
31203 +
31204 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
31205 + core_if->gr_backup->gusbcfg_local);
31206 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
31207 + core_if->dr_backup->dcfg);
31208 +
31209 + pcgcctl.d32 = 0;
31210 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31211 + pcgcctl.b.max_xcvrselect = 1;
31212 + pcgcctl.d32 |= 0x608;
31213 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31214 + dwc_udelay(10);
31215 +
31216 + pcgcctl.d32 = 0;
31217 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31218 + pcgcctl.b.max_xcvrselect = 1;
31219 + pcgcctl.b.ess_reg_restored = 1;
31220 + pcgcctl.b.enbl_extnd_hiber = 1;
31221 + pcgcctl.b.rstpdwnmodule = 1;
31222 + pcgcctl.b.restoremode = 1;
31223 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31224 +
31225 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
31226 +
31227 + return 1;
31228 +}
31229 +
31230 +#ifdef CONFIG_USB_DWC_OTG_LPM
31231 +/**
31232 + * This function hadles LPM transaction received interrupt.
31233 + */
31234 +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
31235 +{
31236 + glpmcfg_data_t lpmcfg;
31237 + gintsts_data_t gintsts;
31238 +
31239 + if (!core_if->core_params->lpm_enable) {
31240 + DWC_PRINTF("Unexpected LPM interrupt\n");
31241 + }
31242 +
31243 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
31244 + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
31245 +
31246 + if (dwc_otg_is_host_mode(core_if)) {
31247 + cil_hcd_sleep(core_if);
31248 + } else {
31249 + lpmcfg.b.hird_thres |= (1 << 4);
31250 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
31251 + lpmcfg.d32);
31252 + }
31253 +
31254 + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
31255 + dwc_udelay(10);
31256 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
31257 + if (lpmcfg.b.prt_sleep_sts) {
31258 + /* Save the current state */
31259 + core_if->lx_state = DWC_OTG_L1;
31260 + }
31261 +
31262 + /* Clear interrupt */
31263 + gintsts.d32 = 0;
31264 + gintsts.b.lpmtranrcvd = 1;
31265 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31266 + return 1;
31267 +}
31268 +#endif /* CONFIG_USB_DWC_OTG_LPM */
31269 +
31270 +/**
31271 + * This function returns the Core Interrupt register.
31272 + */
31273 +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)
31274 +{
31275 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
31276 + gintsts_data_t gintsts;
31277 + gintmsk_data_t gintmsk;
31278 + gintmsk_data_t gintmsk_common = {.d32 = 0 };
31279 + gintmsk_common.b.wkupintr = 1;
31280 + gintmsk_common.b.sessreqintr = 1;
31281 + gintmsk_common.b.conidstschng = 1;
31282 + gintmsk_common.b.otgintr = 1;
31283 + gintmsk_common.b.modemismatch = 1;
31284 + gintmsk_common.b.disconnect = 1;
31285 + gintmsk_common.b.usbsuspend = 1;
31286 +#ifdef CONFIG_USB_DWC_OTG_LPM
31287 + gintmsk_common.b.lpmtranrcvd = 1;
31288 +#endif
31289 + gintmsk_common.b.restoredone = 1;
31290 + if(dwc_otg_is_device_mode(core_if))
31291 + {
31292 + /** @todo: The port interrupt occurs while in device
31293 + * mode. Added code to CIL to clear the interrupt for now!
31294 + */
31295 + gintmsk_common.b.portintr = 1;
31296 + }
31297 + if(fiq_enable) {
31298 + local_fiq_disable();
31299 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
31300 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
31301 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
31302 + /* Pull in the interrupts that the FIQ has masked */
31303 + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
31304 + gintmsk.d32 |= gintmsk_common.d32;
31305 + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
31306 + reenable_gintmsk->d32 = gintmsk.d32;
31307 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
31308 + local_fiq_enable();
31309 + } else {
31310 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
31311 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
31312 + }
31313 +
31314 + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
31315 +
31316 +#ifdef DEBUG
31317 + /* if any common interrupts set */
31318 + if (gintsts.d32 & gintmsk_common.d32) {
31319 + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
31320 + gintsts.d32, gintmsk.d32);
31321 + }
31322 +#endif
31323 + if (!fiq_enable){
31324 + if (gahbcfg.b.glblintrmsk)
31325 + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
31326 + else
31327 + return 0;
31328 + } else {
31329 + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
31330 + * Can't trust the global interrupt mask bit in this case.
31331 + */
31332 + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
31333 + }
31334 +
31335 +}
31336 +
31337 +/* MACRO for clearing interupt bits in GPWRDN register */
31338 +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
31339 +do { \
31340 + gpwrdn_data_t gpwrdn = {.d32=0}; \
31341 + gpwrdn.b.__intr = 1; \
31342 + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
31343 + 0, gpwrdn.d32); \
31344 +} while (0)
31345 +
31346 +/**
31347 + * Common interrupt handler.
31348 + *
31349 + * The common interrupts are those that occur in both Host and Device mode.
31350 + * This handler handles the following interrupts:
31351 + * - Mode Mismatch Interrupt
31352 + * - Disconnect Interrupt
31353 + * - OTG Interrupt
31354 + * - Connector ID Status Change Interrupt
31355 + * - Session Request Interrupt.
31356 + * - Resume / Remote Wakeup Detected Interrupt.
31357 + * - LPM Transaction Received Interrupt
31358 + * - ADP Transaction Received Interrupt
31359 + *
31360 + */
31361 +int32_t dwc_otg_handle_common_intr(void *dev)
31362 +{
31363 + int retval = 0;
31364 + gintsts_data_t gintsts;
31365 + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
31366 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31367 + dwc_otg_device_t *otg_dev = dev;
31368 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
31369 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31370 + if (dwc_otg_is_device_mode(core_if))
31371 + core_if->frame_num = dwc_otg_get_frame_number(core_if);
31372 +
31373 + if (core_if->lock)
31374 + DWC_SPINLOCK(core_if->lock);
31375 +
31376 + if (core_if->power_down == 3 && core_if->xhib == 1) {
31377 + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
31378 + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
31379 + core_if->xhib = 2;
31380 + if (core_if->lock)
31381 + DWC_SPINUNLOCK(core_if->lock);
31382 +
31383 + return retval;
31384 + }
31385 +
31386 + if (core_if->hibernation_suspend <= 0) {
31387 + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
31388 + * of this handler - god only knows why it's done like this
31389 + */
31390 + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
31391 +
31392 + if (gintsts.b.modemismatch) {
31393 + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
31394 + }
31395 + if (gintsts.b.otgintr) {
31396 + retval |= dwc_otg_handle_otg_intr(core_if);
31397 + }
31398 + if (gintsts.b.conidstschng) {
31399 + retval |=
31400 + dwc_otg_handle_conn_id_status_change_intr(core_if);
31401 + }
31402 + if (gintsts.b.disconnect) {
31403 + retval |= dwc_otg_handle_disconnect_intr(core_if);
31404 + }
31405 + if (gintsts.b.sessreqintr) {
31406 + retval |= dwc_otg_handle_session_req_intr(core_if);
31407 + }
31408 + if (gintsts.b.wkupintr) {
31409 + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
31410 + }
31411 + if (gintsts.b.usbsuspend) {
31412 + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
31413 + }
31414 +#ifdef CONFIG_USB_DWC_OTG_LPM
31415 + if (gintsts.b.lpmtranrcvd) {
31416 + retval |= dwc_otg_handle_lpm_intr(core_if);
31417 + }
31418 +#endif
31419 + if (gintsts.b.restoredone) {
31420 + gintsts.d32 = 0;
31421 + if (core_if->power_down == 2)
31422 + core_if->hibernation_suspend = -1;
31423 + else if (core_if->power_down == 3 && core_if->xhib == 2) {
31424 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31425 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31426 + dctl_data_t dctl = {.d32 = 0 };
31427 +
31428 + DWC_WRITE_REG32(&core_if->core_global_regs->
31429 + gintsts, 0xFFFFFFFF);
31430 +
31431 + DWC_DEBUGPL(DBG_ANY,
31432 + "RESTORE DONE generated\n");
31433 +
31434 + gpwrdn.b.restore = 1;
31435 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31436 + dwc_udelay(10);
31437 +
31438 + pcgcctl.b.rstpdwnmodule = 1;
31439 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
31440 +
31441 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
31442 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
31443 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
31444 + dwc_udelay(50);
31445 +
31446 + dctl.b.pwronprgdone = 1;
31447 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
31448 + dwc_udelay(10);
31449 +
31450 + dwc_otg_restore_global_regs(core_if);
31451 + dwc_otg_restore_dev_regs(core_if, 0);
31452 +
31453 + dctl.d32 = 0;
31454 + dctl.b.pwronprgdone = 1;
31455 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
31456 + dwc_udelay(10);
31457 +
31458 + pcgcctl.d32 = 0;
31459 + pcgcctl.b.enbl_extnd_hiber = 1;
31460 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
31461 +
31462 + /* The core will be in ON STATE */
31463 + core_if->lx_state = DWC_OTG_L0;
31464 + core_if->xhib = 0;
31465 +
31466 + DWC_SPINUNLOCK(core_if->lock);
31467 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
31468 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
31469 + }
31470 + DWC_SPINLOCK(core_if->lock);
31471 +
31472 + }
31473 +
31474 + gintsts.b.restoredone = 1;
31475 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
31476 + DWC_PRINTF(" --Restore done interrupt received-- \n");
31477 + retval |= 1;
31478 + }
31479 + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
31480 + /* The port interrupt occurs while in device mode with HPRT0
31481 + * Port Enable/Disable.
31482 + */
31483 + gintsts.d32 = 0;
31484 + gintsts.b.portintr = 1;
31485 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
31486 + retval |= 1;
31487 + gintmsk_reenable.b.portintr = 1;
31488 +
31489 + }
31490 + /* Did we actually handle anything? if so, unmask the interrupt */
31491 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
31492 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
31493 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
31494 + if (retval && fiq_enable) {
31495 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
31496 + }
31497 +
31498 + } else {
31499 + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
31500 +
31501 + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
31502 + CLEAR_GPWRDN_INTR(core_if, disconn_det);
31503 + if (gpwrdn.b.linestate == 0) {
31504 + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
31505 + } else {
31506 + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
31507 + }
31508 +
31509 + retval |= 1;
31510 + }
31511 + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
31512 + CLEAR_GPWRDN_INTR(core_if, lnstschng);
31513 + /* remote wakeup from hibernation */
31514 + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
31515 + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
31516 + } else {
31517 + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
31518 + }
31519 + retval |= 1;
31520 + }
31521 + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
31522 + CLEAR_GPWRDN_INTR(core_if, rst_det);
31523 + if (gpwrdn.b.linestate == 0) {
31524 + DWC_PRINTF("Reset detected\n");
31525 + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
31526 + }
31527 + }
31528 + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
31529 + CLEAR_GPWRDN_INTR(core_if, srp_det);
31530 + dwc_otg_handle_pwrdn_srp_intr(core_if);
31531 + retval |= 1;
31532 + }
31533 + }
31534 + /* Handle ADP interrupt here */
31535 + if (gpwrdn.b.adp_int) {
31536 + DWC_PRINTF("ADP interrupt\n");
31537 + CLEAR_GPWRDN_INTR(core_if, adp_int);
31538 + dwc_otg_adp_handle_intr(core_if);
31539 + retval |= 1;
31540 + }
31541 + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
31542 + DWC_PRINTF("STS CHNG interrupt asserted\n");
31543 + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
31544 + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
31545 +
31546 + retval |= 1;
31547 + }
31548 + if (core_if->lock)
31549 + DWC_SPINUNLOCK(core_if->lock);
31550 + return retval;
31551 +}
31552 --- /dev/null
31553 +++ b/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
31554 @@ -0,0 +1,705 @@
31555 +/* ==========================================================================
31556 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
31557 + * $Revision: #13 $
31558 + * $Date: 2012/08/10 $
31559 + * $Change: 2047372 $
31560 + *
31561 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
31562 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
31563 + * otherwise expressly agreed to in writing between Synopsys and you.
31564 + *
31565 + * The Software IS NOT an item of Licensed Software or Licensed Product under
31566 + * any End User Software License Agreement or Agreement for Licensed Product
31567 + * with Synopsys or any supplement thereto. You are permitted to use and
31568 + * redistribute this Software in source and binary forms, with or without
31569 + * modification, provided that redistributions of source code must retain this
31570 + * notice. You may not view, use, disclose, copy or distribute this file or
31571 + * any information contained herein except pursuant to this license grant from
31572 + * Synopsys. If you do not agree with this notice, including the disclaimer
31573 + * below, then you are not authorized to use the Software.
31574 + *
31575 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
31576 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31577 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31578 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
31579 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31580 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31581 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31582 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31583 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31584 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31585 + * DAMAGE.
31586 + * ========================================================================== */
31587 +#if !defined(__DWC_CORE_IF_H__)
31588 +#define __DWC_CORE_IF_H__
31589 +
31590 +#include "dwc_os.h"
31591 +
31592 +/** @file
31593 + * This file defines DWC_OTG Core API
31594 + */
31595 +
31596 +struct dwc_otg_core_if;
31597 +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
31598 +
31599 +/** Maximum number of Periodic FIFOs */
31600 +#define MAX_PERIO_FIFOS 15
31601 +/** Maximum number of Periodic FIFOs */
31602 +#define MAX_TX_FIFOS 15
31603 +
31604 +/** Maximum number of Endpoints/HostChannels */
31605 +#define MAX_EPS_CHANNELS 16
31606 +
31607 +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
31608 +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
31609 +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
31610 +
31611 +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
31612 +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
31613 +
31614 +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
31615 +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
31616 +
31617 +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
31618 +
31619 +/** This function should be called on every hardware interrupt. */
31620 +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
31621 +
31622 +/** @name OTG Core Parameters */
31623 +/** @{ */
31624 +
31625 +/**
31626 + * Specifies the OTG capabilities. The driver will automatically
31627 + * detect the value for this parameter if none is specified.
31628 + * 0 - HNP and SRP capable (default)
31629 + * 1 - SRP Only capable
31630 + * 2 - No HNP/SRP capable
31631 + */
31632 +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
31633 +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
31634 +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
31635 +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
31636 +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
31637 +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
31638 +
31639 +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
31640 +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
31641 +#define dwc_param_opt_default 1
31642 +
31643 +/**
31644 + * Specifies whether to use slave or DMA mode for accessing the data
31645 + * FIFOs. The driver will automatically detect the value for this
31646 + * parameter if none is specified.
31647 + * 0 - Slave
31648 + * 1 - DMA (default, if available)
31649 + */
31650 +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
31651 + int32_t val);
31652 +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
31653 +#define dwc_param_dma_enable_default 1
31654 +
31655 +/**
31656 + * When DMA mode is enabled specifies whether to use
31657 + * address DMA or DMA Descritor mode for accessing the data
31658 + * FIFOs in device mode. The driver will automatically detect
31659 + * the value for this parameter if none is specified.
31660 + * 0 - address DMA
31661 + * 1 - DMA Descriptor(default, if available)
31662 + */
31663 +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
31664 + int32_t val);
31665 +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
31666 +//#define dwc_param_dma_desc_enable_default 1
31667 +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
31668 +
31669 +/** The DMA Burst size (applicable only for External DMA
31670 + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
31671 + */
31672 +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
31673 + int32_t val);
31674 +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
31675 +#define dwc_param_dma_burst_size_default 32
31676 +
31677 +/**
31678 + * Specifies the maximum speed of operation in host and device mode.
31679 + * The actual speed depends on the speed of the attached device and
31680 + * the value of phy_type. The actual speed depends on the speed of the
31681 + * attached device.
31682 + * 0 - High Speed (default)
31683 + * 1 - Full Speed
31684 + */
31685 +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
31686 +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
31687 +#define dwc_param_speed_default 0
31688 +#define DWC_SPEED_PARAM_HIGH 0
31689 +#define DWC_SPEED_PARAM_FULL 1
31690 +
31691 +/** Specifies whether low power mode is supported when attached
31692 + * to a Full Speed or Low Speed device in host mode.
31693 + * 0 - Don't support low power mode (default)
31694 + * 1 - Support low power mode
31695 + */
31696 +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
31697 + core_if, int32_t val);
31698 +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
31699 + * core_if);
31700 +#define dwc_param_host_support_fs_ls_low_power_default 0
31701 +
31702 +/** Specifies the PHY clock rate in low power mode when connected to a
31703 + * Low Speed device in host mode. This parameter is applicable only if
31704 + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
31705 + * then defaults to 6 MHZ otherwise 48 MHZ.
31706 + *
31707 + * 0 - 48 MHz
31708 + * 1 - 6 MHz
31709 + */
31710 +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
31711 + core_if, int32_t val);
31712 +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
31713 + core_if);
31714 +#define dwc_param_host_ls_low_power_phy_clk_default 0
31715 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
31716 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
31717 +
31718 +/**
31719 + * 0 - Use cC FIFO size parameters
31720 + * 1 - Allow dynamic FIFO sizing (default)
31721 + */
31722 +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
31723 + int32_t val);
31724 +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
31725 + core_if);
31726 +#define dwc_param_enable_dynamic_fifo_default 1
31727 +
31728 +/** Total number of 4-byte words in the data FIFO memory. This
31729 + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
31730 + * Tx FIFOs.
31731 + * 32 to 32768 (default 8192)
31732 + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
31733 + */
31734 +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
31735 + int32_t val);
31736 +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
31737 +//#define dwc_param_data_fifo_size_default 8192
31738 +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
31739 +
31740 +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
31741 + * FIFO sizing is enabled.
31742 + * 16 to 32768 (default 1064)
31743 + */
31744 +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
31745 + int32_t val);
31746 +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
31747 +#define dwc_param_dev_rx_fifo_size_default 1064
31748 +
31749 +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
31750 + * when dynamic FIFO sizing is enabled.
31751 + * 16 to 32768 (default 1024)
31752 + */
31753 +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
31754 + core_if, int32_t val);
31755 +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
31756 + core_if);
31757 +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
31758 +
31759 +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
31760 + * mode when dynamic FIFO sizing is enabled.
31761 + * 4 to 768 (default 256)
31762 + */
31763 +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
31764 + int32_t val, int fifo_num);
31765 +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
31766 + core_if, int fifo_num);
31767 +#define dwc_param_dev_perio_tx_fifo_size_default 256
31768 +
31769 +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
31770 + * FIFO sizing is enabled.
31771 + * 16 to 32768 (default 1024)
31772 + */
31773 +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
31774 + int32_t val);
31775 +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
31776 +//#define dwc_param_host_rx_fifo_size_default 1024
31777 +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
31778 +
31779 +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
31780 + * when Dynamic FIFO sizing is enabled in the core.
31781 + * 16 to 32768 (default 1024)
31782 + */
31783 +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
31784 + core_if, int32_t val);
31785 +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
31786 + core_if);
31787 +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
31788 +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
31789 +
31790 +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
31791 + * FIFO sizing is enabled.
31792 + * 16 to 32768 (default 1024)
31793 + */
31794 +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
31795 + core_if, int32_t val);
31796 +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
31797 + core_if);
31798 +//#define dwc_param_host_perio_tx_fifo_size_default 1024
31799 +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
31800 +
31801 +/** The maximum transfer size supported in bytes.
31802 + * 2047 to 65,535 (default 65,535)
31803 + */
31804 +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
31805 + int32_t val);
31806 +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
31807 +#define dwc_param_max_transfer_size_default 65535
31808 +
31809 +/** The maximum number of packets in a transfer.
31810 + * 15 to 511 (default 511)
31811 + */
31812 +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
31813 + int32_t val);
31814 +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
31815 +#define dwc_param_max_packet_count_default 511
31816 +
31817 +/** The number of host channel registers to use.
31818 + * 1 to 16 (default 12)
31819 + * Note: The FPGA configuration supports a maximum of 12 host channels.
31820 + */
31821 +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
31822 + int32_t val);
31823 +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
31824 +//#define dwc_param_host_channels_default 12
31825 +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
31826 +
31827 +/** The number of endpoints in addition to EP0 available for device
31828 + * mode operations.
31829 + * 1 to 15 (default 6 IN and OUT)
31830 + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
31831 + * endpoints in addition to EP0.
31832 + */
31833 +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
31834 + int32_t val);
31835 +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
31836 +#define dwc_param_dev_endpoints_default 6
31837 +
31838 +/**
31839 + * Specifies the type of PHY interface to use. By default, the driver
31840 + * will automatically detect the phy_type.
31841 + *
31842 + * 0 - Full Speed PHY
31843 + * 1 - UTMI+ (default)
31844 + * 2 - ULPI
31845 + */
31846 +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
31847 +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
31848 +#define DWC_PHY_TYPE_PARAM_FS 0
31849 +#define DWC_PHY_TYPE_PARAM_UTMI 1
31850 +#define DWC_PHY_TYPE_PARAM_ULPI 2
31851 +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
31852 +
31853 +/**
31854 + * Specifies the UTMI+ Data Width. This parameter is
31855 + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
31856 + * PHY_TYPE, this parameter indicates the data width between
31857 + * the MAC and the ULPI Wrapper.) Also, this parameter is
31858 + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
31859 + * to "8 and 16 bits", meaning that the core has been
31860 + * configured to work at either data path width.
31861 + *
31862 + * 8 or 16 bits (default 16)
31863 + */
31864 +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
31865 + int32_t val);
31866 +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
31867 +//#define dwc_param_phy_utmi_width_default 16
31868 +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
31869 +
31870 +/**
31871 + * Specifies whether the ULPI operates at double or single
31872 + * data rate. This parameter is only applicable if PHY_TYPE is
31873 + * ULPI.
31874 + *
31875 + * 0 - single data rate ULPI interface with 8 bit wide data
31876 + * bus (default)
31877 + * 1 - double data rate ULPI interface with 4 bit wide data
31878 + * bus
31879 + */
31880 +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
31881 + int32_t val);
31882 +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
31883 +#define dwc_param_phy_ulpi_ddr_default 0
31884 +
31885 +/**
31886 + * Specifies whether to use the internal or external supply to
31887 + * drive the vbus with a ULPI phy.
31888 + */
31889 +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
31890 + int32_t val);
31891 +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
31892 +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
31893 +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
31894 +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
31895 +
31896 +/**
31897 + * Specifies whether to use the I2Cinterface for full speed PHY. This
31898 + * parameter is only applicable if PHY_TYPE is FS.
31899 + * 0 - No (default)
31900 + * 1 - Yes
31901 + */
31902 +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
31903 + int32_t val);
31904 +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
31905 +#define dwc_param_i2c_enable_default 0
31906 +
31907 +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
31908 + int32_t val);
31909 +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
31910 +#define dwc_param_ulpi_fs_ls_default 0
31911 +
31912 +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
31913 +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
31914 +#define dwc_param_ts_dline_default 0
31915 +
31916 +/**
31917 + * Specifies whether dedicated transmit FIFOs are
31918 + * enabled for non periodic IN endpoints in device mode
31919 + * 0 - No
31920 + * 1 - Yes
31921 + */
31922 +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
31923 + int32_t val);
31924 +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
31925 + core_if);
31926 +#define dwc_param_en_multiple_tx_fifo_default 1
31927 +
31928 +/** Number of 4-byte words in each of the Tx FIFOs in device
31929 + * mode when dynamic FIFO sizing is enabled.
31930 + * 4 to 768 (default 256)
31931 + */
31932 +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
31933 + int fifo_num, int32_t val);
31934 +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
31935 + int fifo_num);
31936 +#define dwc_param_dev_tx_fifo_size_default 768
31937 +
31938 +/** Thresholding enable flag-
31939 + * bit 0 - enable non-ISO Tx thresholding
31940 + * bit 1 - enable ISO Tx thresholding
31941 + * bit 2 - enable Rx thresholding
31942 + */
31943 +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
31944 +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
31945 +#define dwc_param_thr_ctl_default 0
31946 +
31947 +/** Thresholding length for Tx
31948 + * FIFOs in 32 bit DWORDs
31949 + */
31950 +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
31951 + int32_t val);
31952 +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
31953 +#define dwc_param_tx_thr_length_default 64
31954 +
31955 +/** Thresholding length for Rx
31956 + * FIFOs in 32 bit DWORDs
31957 + */
31958 +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
31959 + int32_t val);
31960 +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
31961 +#define dwc_param_rx_thr_length_default 64
31962 +
31963 +/**
31964 + * Specifies whether LPM (Link Power Management) support is enabled
31965 + */
31966 +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
31967 + int32_t val);
31968 +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
31969 +#define dwc_param_lpm_enable_default 1
31970 +
31971 +/**
31972 + * Specifies whether PTI enhancement is enabled
31973 + */
31974 +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
31975 + int32_t val);
31976 +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
31977 +#define dwc_param_pti_enable_default 0
31978 +
31979 +/**
31980 + * Specifies whether MPI enhancement is enabled
31981 + */
31982 +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
31983 + int32_t val);
31984 +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
31985 +#define dwc_param_mpi_enable_default 0
31986 +
31987 +/**
31988 + * Specifies whether ADP capability is enabled
31989 + */
31990 +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
31991 + int32_t val);
31992 +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
31993 +#define dwc_param_adp_enable_default 0
31994 +
31995 +/**
31996 + * Specifies whether IC_USB capability is enabled
31997 + */
31998 +
31999 +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
32000 + int32_t val);
32001 +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
32002 +#define dwc_param_ic_usb_cap_default 0
32003 +
32004 +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
32005 + int32_t val);
32006 +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
32007 +#define dwc_param_ahb_thr_ratio_default 0
32008 +
32009 +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
32010 + int32_t val);
32011 +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
32012 +#define dwc_param_power_down_default 0
32013 +
32014 +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
32015 + int32_t val);
32016 +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
32017 +#define dwc_param_reload_ctl_default 0
32018 +
32019 +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
32020 + int32_t val);
32021 +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
32022 +#define dwc_param_dev_out_nak_default 0
32023 +
32024 +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
32025 + int32_t val);
32026 +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
32027 +#define dwc_param_cont_on_bna_default 0
32028 +
32029 +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
32030 + int32_t val);
32031 +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
32032 +#define dwc_param_ahb_single_default 0
32033 +
32034 +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
32035 +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
32036 +#define dwc_param_otg_ver_default 0
32037 +
32038 +/** @} */
32039 +
32040 +/** @name Access to registers and bit-fields */
32041 +
32042 +/**
32043 + * Dump core registers and SPRAM
32044 + */
32045 +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
32046 +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
32047 +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
32048 +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
32049 +
32050 +/**
32051 + * Get host negotiation status.
32052 + */
32053 +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
32054 +
32055 +/**
32056 + * Get srp status
32057 + */
32058 +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
32059 +
32060 +/**
32061 + * Set hnpreq bit in the GOTGCTL register.
32062 + */
32063 +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
32064 +
32065 +/**
32066 + * Get Content of SNPSID register.
32067 + */
32068 +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
32069 +
32070 +/**
32071 + * Get current mode.
32072 + * Returns 0 if in device mode, and 1 if in host mode.
32073 + */
32074 +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
32075 +
32076 +/**
32077 + * Get value of hnpcapable field in the GUSBCFG register
32078 + */
32079 +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
32080 +/**
32081 + * Set value of hnpcapable field in the GUSBCFG register
32082 + */
32083 +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
32084 +
32085 +/**
32086 + * Get value of srpcapable field in the GUSBCFG register
32087 + */
32088 +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
32089 +/**
32090 + * Set value of srpcapable field in the GUSBCFG register
32091 + */
32092 +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
32093 +
32094 +/**
32095 + * Get value of devspeed field in the DCFG register
32096 + */
32097 +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
32098 +/**
32099 + * Set value of devspeed field in the DCFG register
32100 + */
32101 +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
32102 +
32103 +/**
32104 + * Get the value of busconnected field from the HPRT0 register
32105 + */
32106 +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
32107 +
32108 +/**
32109 + * Gets the device enumeration Speed.
32110 + */
32111 +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
32112 +
32113 +/**
32114 + * Get value of prtpwr field from the HPRT0 register
32115 + */
32116 +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
32117 +
32118 +/**
32119 + * Get value of flag indicating core state - hibernated or not
32120 + */
32121 +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
32122 +
32123 +/**
32124 + * Set value of prtpwr field from the HPRT0 register
32125 + */
32126 +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
32127 +
32128 +/**
32129 + * Get value of prtsusp field from the HPRT0 regsiter
32130 + */
32131 +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
32132 +/**
32133 + * Set value of prtpwr field from the HPRT0 register
32134 + */
32135 +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
32136 +
32137 +/**
32138 + * Get value of ModeChTimEn field from the HCFG regsiter
32139 + */
32140 +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
32141 +/**
32142 + * Set value of ModeChTimEn field from the HCFG regsiter
32143 + */
32144 +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
32145 +
32146 +/**
32147 + * Get value of Fram Interval field from the HFIR regsiter
32148 + */
32149 +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
32150 +/**
32151 + * Set value of Frame Interval field from the HFIR regsiter
32152 + */
32153 +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
32154 +
32155 +/**
32156 + * Set value of prtres field from the HPRT0 register
32157 + *FIXME Remove?
32158 + */
32159 +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
32160 +
32161 +/**
32162 + * Get value of rmtwkupsig bit in DCTL register
32163 + */
32164 +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
32165 +
32166 +/**
32167 + * Get value of prt_sleep_sts field from the GLPMCFG register
32168 + */
32169 +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
32170 +
32171 +/**
32172 + * Get value of rem_wkup_en field from the GLPMCFG register
32173 + */
32174 +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
32175 +
32176 +/**
32177 + * Get value of appl_resp field from the GLPMCFG register
32178 + */
32179 +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
32180 +/**
32181 + * Set value of appl_resp field from the GLPMCFG register
32182 + */
32183 +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
32184 +
32185 +/**
32186 + * Get value of hsic_connect field from the GLPMCFG register
32187 + */
32188 +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
32189 +/**
32190 + * Set value of hsic_connect field from the GLPMCFG register
32191 + */
32192 +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
32193 +
32194 +/**
32195 + * Get value of inv_sel_hsic field from the GLPMCFG register.
32196 + */
32197 +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
32198 +/**
32199 + * Set value of inv_sel_hsic field from the GLPMFG register.
32200 + */
32201 +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
32202 +
32203 +/*
32204 + * Some functions for accessing registers
32205 + */
32206 +
32207 +/**
32208 + * GOTGCTL register
32209 + */
32210 +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
32211 +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
32212 +
32213 +/**
32214 + * GUSBCFG register
32215 + */
32216 +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
32217 +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
32218 +
32219 +/**
32220 + * GRXFSIZ register
32221 + */
32222 +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
32223 +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
32224 +
32225 +/**
32226 + * GNPTXFSIZ register
32227 + */
32228 +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
32229 +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
32230 +
32231 +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
32232 +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
32233 +
32234 +/**
32235 + * GGPIO register
32236 + */
32237 +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
32238 +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
32239 +
32240 +/**
32241 + * GUID register
32242 + */
32243 +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
32244 +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
32245 +
32246 +/**
32247 + * HPRT0 register
32248 + */
32249 +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
32250 +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
32251 +
32252 +/**
32253 + * GHPTXFSIZE
32254 + */
32255 +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
32256 +
32257 +/** @} */
32258 +
32259 +#endif /* __DWC_CORE_IF_H__ */
32260 --- /dev/null
32261 +++ b/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
32262 @@ -0,0 +1,117 @@
32263 +/* ==========================================================================
32264 + *
32265 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
32266 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
32267 + * otherwise expressly agreed to in writing between Synopsys and you.
32268 + *
32269 + * The Software IS NOT an item of Licensed Software or Licensed Product under
32270 + * any End User Software License Agreement or Agreement for Licensed Product
32271 + * with Synopsys or any supplement thereto. You are permitted to use and
32272 + * redistribute this Software in source and binary forms, with or without
32273 + * modification, provided that redistributions of source code must retain this
32274 + * notice. You may not view, use, disclose, copy or distribute this file or
32275 + * any information contained herein except pursuant to this license grant from
32276 + * Synopsys. If you do not agree with this notice, including the disclaimer
32277 + * below, then you are not authorized to use the Software.
32278 + *
32279 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
32280 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32281 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32282 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
32283 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32284 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32285 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32286 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32287 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32288 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32289 + * DAMAGE.
32290 + * ========================================================================== */
32291 +
32292 +#ifndef __DWC_OTG_DBG_H__
32293 +#define __DWC_OTG_DBG_H__
32294 +
32295 +/** @file
32296 + * This file defines debug levels.
32297 + * Debugging support vanishes in non-debug builds.
32298 + */
32299 +
32300 +/**
32301 + * The Debug Level bit-mask variable.
32302 + */
32303 +extern uint32_t g_dbg_lvl;
32304 +/**
32305 + * Set the Debug Level variable.
32306 + */
32307 +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
32308 +{
32309 + uint32_t old = g_dbg_lvl;
32310 + g_dbg_lvl = new;
32311 + return old;
32312 +}
32313 +
32314 +#define DBG_USER (0x1)
32315 +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
32316 +#define DBG_CIL (0x2)
32317 +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
32318 + * messages */
32319 +#define DBG_CILV (0x20)
32320 +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
32321 + * messages */
32322 +#define DBG_PCD (0x4)
32323 +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
32324 + * messages */
32325 +#define DBG_PCDV (0x40)
32326 +/** When debug level has the DBG_HCD bit set, display Host debug messages */
32327 +#define DBG_HCD (0x8)
32328 +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
32329 + * messages */
32330 +#define DBG_HCDV (0x80)
32331 +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
32332 + * mode. */
32333 +#define DBG_HCD_URB (0x800)
32334 +/** When debug level has the DBG_HCDI bit set, display host interrupt
32335 + * messages. */
32336 +#define DBG_HCDI (0x1000)
32337 +
32338 +/** When debug level has any bit set, display debug messages */
32339 +#define DBG_ANY (0xFF)
32340 +
32341 +/** All debug messages off */
32342 +#define DBG_OFF 0
32343 +
32344 +/** Prefix string for DWC_DEBUG print macros. */
32345 +#define USB_DWC "DWC_otg: "
32346 +
32347 +/**
32348 + * Print a debug message when the Global debug level variable contains
32349 + * the bit defined in <code>lvl</code>.
32350 + *
32351 + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
32352 + * @param[in] x - like printf
32353 + *
32354 + * Example:<p>
32355 + * <code>
32356 + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
32357 + * </code>
32358 + * <br>
32359 + * results in:<br>
32360 + * <code>
32361 + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
32362 + * </code>
32363 + */
32364 +#ifdef DEBUG
32365 +
32366 +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
32367 +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
32368 +
32369 +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
32370 +
32371 +#else
32372 +
32373 +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
32374 +# define DWC_DEBUGP(x...)
32375 +
32376 +# define CHK_DEBUG_LEVEL(level) (0)
32377 +
32378 +#endif /*DEBUG*/
32379 +#endif
32380 --- /dev/null
32381 +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.c
32382 @@ -0,0 +1,1772 @@
32383 +/* ==========================================================================
32384 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
32385 + * $Revision: #92 $
32386 + * $Date: 2012/08/10 $
32387 + * $Change: 2047372 $
32388 + *
32389 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
32390 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
32391 + * otherwise expressly agreed to in writing between Synopsys and you.
32392 + *
32393 + * The Software IS NOT an item of Licensed Software or Licensed Product under
32394 + * any End User Software License Agreement or Agreement for Licensed Product
32395 + * with Synopsys or any supplement thereto. You are permitted to use and
32396 + * redistribute this Software in source and binary forms, with or without
32397 + * modification, provided that redistributions of source code must retain this
32398 + * notice. You may not view, use, disclose, copy or distribute this file or
32399 + * any information contained herein except pursuant to this license grant from
32400 + * Synopsys. If you do not agree with this notice, including the disclaimer
32401 + * below, then you are not authorized to use the Software.
32402 + *
32403 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
32404 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32405 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32406 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
32407 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32408 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32409 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32410 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32411 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32412 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32413 + * DAMAGE.
32414 + * ========================================================================== */
32415 +
32416 +/** @file
32417 + * The dwc_otg_driver module provides the initialization and cleanup entry
32418 + * points for the DWC_otg driver. This module will be dynamically installed
32419 + * after Linux is booted using the insmod command. When the module is
32420 + * installed, the dwc_otg_driver_init function is called. When the module is
32421 + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
32422 + *
32423 + * This module also defines a data structure for the dwc_otg_driver, which is
32424 + * used in conjunction with the standard ARM lm_device structure. These
32425 + * structures allow the OTG driver to comply with the standard Linux driver
32426 + * model in which devices and drivers are registered with a bus driver. This
32427 + * has the benefit that Linux can expose attributes of the driver and device
32428 + * in its special sysfs file system. Users can then read or write files in
32429 + * this file system to perform diagnostics on the driver components or the
32430 + * device.
32431 + */
32432 +
32433 +#include "dwc_otg_os_dep.h"
32434 +#include "dwc_os.h"
32435 +#include "dwc_otg_dbg.h"
32436 +#include "dwc_otg_driver.h"
32437 +#include "dwc_otg_attr.h"
32438 +#include "dwc_otg_core_if.h"
32439 +#include "dwc_otg_pcd_if.h"
32440 +#include "dwc_otg_hcd_if.h"
32441 +#include "dwc_otg_fiq_fsm.h"
32442 +
32443 +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
32444 +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
32445 +
32446 +bool microframe_schedule=true;
32447 +
32448 +static const char dwc_driver_name[] = "dwc_otg";
32449 +
32450 +
32451 +extern int pcd_init(
32452 +#ifdef LM_INTERFACE
32453 + struct lm_device *_dev
32454 +#elif defined(PCI_INTERFACE)
32455 + struct pci_dev *_dev
32456 +#elif defined(PLATFORM_INTERFACE)
32457 + struct platform_device *dev
32458 +#endif
32459 + );
32460 +extern int hcd_init(
32461 +#ifdef LM_INTERFACE
32462 + struct lm_device *_dev
32463 +#elif defined(PCI_INTERFACE)
32464 + struct pci_dev *_dev
32465 +#elif defined(PLATFORM_INTERFACE)
32466 + struct platform_device *dev
32467 +#endif
32468 + );
32469 +
32470 +extern int pcd_remove(
32471 +#ifdef LM_INTERFACE
32472 + struct lm_device *_dev
32473 +#elif defined(PCI_INTERFACE)
32474 + struct pci_dev *_dev
32475 +#elif defined(PLATFORM_INTERFACE)
32476 + struct platform_device *_dev
32477 +#endif
32478 + );
32479 +
32480 +extern void hcd_remove(
32481 +#ifdef LM_INTERFACE
32482 + struct lm_device *_dev
32483 +#elif defined(PCI_INTERFACE)
32484 + struct pci_dev *_dev
32485 +#elif defined(PLATFORM_INTERFACE)
32486 + struct platform_device *_dev
32487 +#endif
32488 + );
32489 +
32490 +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
32491 +
32492 +/*-------------------------------------------------------------------------*/
32493 +/* Encapsulate the module parameter settings */
32494 +
32495 +struct dwc_otg_driver_module_params {
32496 + int32_t opt;
32497 + int32_t otg_cap;
32498 + int32_t dma_enable;
32499 + int32_t dma_desc_enable;
32500 + int32_t dma_burst_size;
32501 + int32_t speed;
32502 + int32_t host_support_fs_ls_low_power;
32503 + int32_t host_ls_low_power_phy_clk;
32504 + int32_t enable_dynamic_fifo;
32505 + int32_t data_fifo_size;
32506 + int32_t dev_rx_fifo_size;
32507 + int32_t dev_nperio_tx_fifo_size;
32508 + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
32509 + int32_t host_rx_fifo_size;
32510 + int32_t host_nperio_tx_fifo_size;
32511 + int32_t host_perio_tx_fifo_size;
32512 + int32_t max_transfer_size;
32513 + int32_t max_packet_count;
32514 + int32_t host_channels;
32515 + int32_t dev_endpoints;
32516 + int32_t phy_type;
32517 + int32_t phy_utmi_width;
32518 + int32_t phy_ulpi_ddr;
32519 + int32_t phy_ulpi_ext_vbus;
32520 + int32_t i2c_enable;
32521 + int32_t ulpi_fs_ls;
32522 + int32_t ts_dline;
32523 + int32_t en_multiple_tx_fifo;
32524 + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
32525 + uint32_t thr_ctl;
32526 + uint32_t tx_thr_length;
32527 + uint32_t rx_thr_length;
32528 + int32_t pti_enable;
32529 + int32_t mpi_enable;
32530 + int32_t lpm_enable;
32531 + int32_t ic_usb_cap;
32532 + int32_t ahb_thr_ratio;
32533 + int32_t power_down;
32534 + int32_t reload_ctl;
32535 + int32_t dev_out_nak;
32536 + int32_t cont_on_bna;
32537 + int32_t ahb_single;
32538 + int32_t otg_ver;
32539 + int32_t adp_enable;
32540 +};
32541 +
32542 +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
32543 + .opt = -1,
32544 + .otg_cap = -1,
32545 + .dma_enable = -1,
32546 + .dma_desc_enable = -1,
32547 + .dma_burst_size = -1,
32548 + .speed = -1,
32549 + .host_support_fs_ls_low_power = -1,
32550 + .host_ls_low_power_phy_clk = -1,
32551 + .enable_dynamic_fifo = -1,
32552 + .data_fifo_size = -1,
32553 + .dev_rx_fifo_size = -1,
32554 + .dev_nperio_tx_fifo_size = -1,
32555 + .dev_perio_tx_fifo_size = {
32556 + /* dev_perio_tx_fifo_size_1 */
32557 + -1,
32558 + -1,
32559 + -1,
32560 + -1,
32561 + -1,
32562 + -1,
32563 + -1,
32564 + -1,
32565 + -1,
32566 + -1,
32567 + -1,
32568 + -1,
32569 + -1,
32570 + -1,
32571 + -1
32572 + /* 15 */
32573 + },
32574 + .host_rx_fifo_size = -1,
32575 + .host_nperio_tx_fifo_size = -1,
32576 + .host_perio_tx_fifo_size = -1,
32577 + .max_transfer_size = -1,
32578 + .max_packet_count = -1,
32579 + .host_channels = -1,
32580 + .dev_endpoints = -1,
32581 + .phy_type = -1,
32582 + .phy_utmi_width = -1,
32583 + .phy_ulpi_ddr = -1,
32584 + .phy_ulpi_ext_vbus = -1,
32585 + .i2c_enable = -1,
32586 + .ulpi_fs_ls = -1,
32587 + .ts_dline = -1,
32588 + .en_multiple_tx_fifo = -1,
32589 + .dev_tx_fifo_size = {
32590 + /* dev_tx_fifo_size */
32591 + -1,
32592 + -1,
32593 + -1,
32594 + -1,
32595 + -1,
32596 + -1,
32597 + -1,
32598 + -1,
32599 + -1,
32600 + -1,
32601 + -1,
32602 + -1,
32603 + -1,
32604 + -1,
32605 + -1
32606 + /* 15 */
32607 + },
32608 + .thr_ctl = -1,
32609 + .tx_thr_length = -1,
32610 + .rx_thr_length = -1,
32611 + .pti_enable = -1,
32612 + .mpi_enable = -1,
32613 + .lpm_enable = 0,
32614 + .ic_usb_cap = -1,
32615 + .ahb_thr_ratio = -1,
32616 + .power_down = -1,
32617 + .reload_ctl = -1,
32618 + .dev_out_nak = -1,
32619 + .cont_on_bna = -1,
32620 + .ahb_single = -1,
32621 + .otg_ver = -1,
32622 + .adp_enable = -1,
32623 +};
32624 +
32625 +//Global variable to switch the fiq fix on or off
32626 +bool fiq_enable = 1;
32627 +// Global variable to enable the split transaction fix
32628 +bool fiq_fsm_enable = true;
32629 +//Bulk split-transaction NAK holdoff in microframes
32630 +uint16_t nak_holdoff = 8;
32631 +
32632 +//Force host mode during CIL re-init
32633 +bool cil_force_host = true;
32634 +
32635 +unsigned short fiq_fsm_mask = 0x0F;
32636 +
32637 +unsigned short int_ep_interval_min = 0;
32638 +/**
32639 + * This function shows the Driver Version.
32640 + */
32641 +static ssize_t version_show(struct device_driver *dev, char *buf)
32642 +{
32643 + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
32644 + DWC_DRIVER_VERSION);
32645 +}
32646 +
32647 +static DRIVER_ATTR_RO(version);
32648 +
32649 +/**
32650 + * Global Debug Level Mask.
32651 + */
32652 +uint32_t g_dbg_lvl = 0; /* OFF */
32653 +
32654 +/**
32655 + * This function shows the driver Debug Level.
32656 + */
32657 +static ssize_t debuglevel_show(struct device_driver *drv, char *buf)
32658 +{
32659 + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
32660 +}
32661 +
32662 +/**
32663 + * This function stores the driver Debug Level.
32664 + */
32665 +static ssize_t debuglevel_store(struct device_driver *drv, const char *buf,
32666 + size_t count)
32667 +{
32668 + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
32669 + return count;
32670 +}
32671 +
32672 +static DRIVER_ATTR_RW(debuglevel);
32673 +
32674 +/**
32675 + * This function is called during module intialization
32676 + * to pass module parameters to the DWC_OTG CORE.
32677 + */
32678 +static int set_parameters(dwc_otg_core_if_t * core_if)
32679 +{
32680 + int retval = 0;
32681 + int i;
32682 +
32683 + if (dwc_otg_module_params.otg_cap != -1) {
32684 + retval +=
32685 + dwc_otg_set_param_otg_cap(core_if,
32686 + dwc_otg_module_params.otg_cap);
32687 + }
32688 + if (dwc_otg_module_params.dma_enable != -1) {
32689 + retval +=
32690 + dwc_otg_set_param_dma_enable(core_if,
32691 + dwc_otg_module_params.
32692 + dma_enable);
32693 + }
32694 + if (dwc_otg_module_params.dma_desc_enable != -1) {
32695 + retval +=
32696 + dwc_otg_set_param_dma_desc_enable(core_if,
32697 + dwc_otg_module_params.
32698 + dma_desc_enable);
32699 + }
32700 + if (dwc_otg_module_params.opt != -1) {
32701 + retval +=
32702 + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
32703 + }
32704 + if (dwc_otg_module_params.dma_burst_size != -1) {
32705 + retval +=
32706 + dwc_otg_set_param_dma_burst_size(core_if,
32707 + dwc_otg_module_params.
32708 + dma_burst_size);
32709 + }
32710 + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
32711 + retval +=
32712 + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
32713 + dwc_otg_module_params.
32714 + host_support_fs_ls_low_power);
32715 + }
32716 + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
32717 + retval +=
32718 + dwc_otg_set_param_enable_dynamic_fifo(core_if,
32719 + dwc_otg_module_params.
32720 + enable_dynamic_fifo);
32721 + }
32722 + if (dwc_otg_module_params.data_fifo_size != -1) {
32723 + retval +=
32724 + dwc_otg_set_param_data_fifo_size(core_if,
32725 + dwc_otg_module_params.
32726 + data_fifo_size);
32727 + }
32728 + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
32729 + retval +=
32730 + dwc_otg_set_param_dev_rx_fifo_size(core_if,
32731 + dwc_otg_module_params.
32732 + dev_rx_fifo_size);
32733 + }
32734 + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
32735 + retval +=
32736 + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
32737 + dwc_otg_module_params.
32738 + dev_nperio_tx_fifo_size);
32739 + }
32740 + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
32741 + retval +=
32742 + dwc_otg_set_param_host_rx_fifo_size(core_if,
32743 + dwc_otg_module_params.host_rx_fifo_size);
32744 + }
32745 + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
32746 + retval +=
32747 + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
32748 + dwc_otg_module_params.
32749 + host_nperio_tx_fifo_size);
32750 + }
32751 + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
32752 + retval +=
32753 + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
32754 + dwc_otg_module_params.
32755 + host_perio_tx_fifo_size);
32756 + }
32757 + if (dwc_otg_module_params.max_transfer_size != -1) {
32758 + retval +=
32759 + dwc_otg_set_param_max_transfer_size(core_if,
32760 + dwc_otg_module_params.
32761 + max_transfer_size);
32762 + }
32763 + if (dwc_otg_module_params.max_packet_count != -1) {
32764 + retval +=
32765 + dwc_otg_set_param_max_packet_count(core_if,
32766 + dwc_otg_module_params.
32767 + max_packet_count);
32768 + }
32769 + if (dwc_otg_module_params.host_channels != -1) {
32770 + retval +=
32771 + dwc_otg_set_param_host_channels(core_if,
32772 + dwc_otg_module_params.
32773 + host_channels);
32774 + }
32775 + if (dwc_otg_module_params.dev_endpoints != -1) {
32776 + retval +=
32777 + dwc_otg_set_param_dev_endpoints(core_if,
32778 + dwc_otg_module_params.
32779 + dev_endpoints);
32780 + }
32781 + if (dwc_otg_module_params.phy_type != -1) {
32782 + retval +=
32783 + dwc_otg_set_param_phy_type(core_if,
32784 + dwc_otg_module_params.phy_type);
32785 + }
32786 + if (dwc_otg_module_params.speed != -1) {
32787 + retval +=
32788 + dwc_otg_set_param_speed(core_if,
32789 + dwc_otg_module_params.speed);
32790 + }
32791 + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
32792 + retval +=
32793 + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
32794 + dwc_otg_module_params.
32795 + host_ls_low_power_phy_clk);
32796 + }
32797 + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
32798 + retval +=
32799 + dwc_otg_set_param_phy_ulpi_ddr(core_if,
32800 + dwc_otg_module_params.
32801 + phy_ulpi_ddr);
32802 + }
32803 + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
32804 + retval +=
32805 + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
32806 + dwc_otg_module_params.
32807 + phy_ulpi_ext_vbus);
32808 + }
32809 + if (dwc_otg_module_params.phy_utmi_width != -1) {
32810 + retval +=
32811 + dwc_otg_set_param_phy_utmi_width(core_if,
32812 + dwc_otg_module_params.
32813 + phy_utmi_width);
32814 + }
32815 + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
32816 + retval +=
32817 + dwc_otg_set_param_ulpi_fs_ls(core_if,
32818 + dwc_otg_module_params.ulpi_fs_ls);
32819 + }
32820 + if (dwc_otg_module_params.ts_dline != -1) {
32821 + retval +=
32822 + dwc_otg_set_param_ts_dline(core_if,
32823 + dwc_otg_module_params.ts_dline);
32824 + }
32825 + if (dwc_otg_module_params.i2c_enable != -1) {
32826 + retval +=
32827 + dwc_otg_set_param_i2c_enable(core_if,
32828 + dwc_otg_module_params.
32829 + i2c_enable);
32830 + }
32831 + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
32832 + retval +=
32833 + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
32834 + dwc_otg_module_params.
32835 + en_multiple_tx_fifo);
32836 + }
32837 + for (i = 0; i < 15; i++) {
32838 + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
32839 + retval +=
32840 + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
32841 + dwc_otg_module_params.
32842 + dev_perio_tx_fifo_size
32843 + [i], i);
32844 + }
32845 + }
32846 +
32847 + for (i = 0; i < 15; i++) {
32848 + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
32849 + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
32850 + dwc_otg_module_params.
32851 + dev_tx_fifo_size
32852 + [i], i);
32853 + }
32854 + }
32855 + if (dwc_otg_module_params.thr_ctl != -1) {
32856 + retval +=
32857 + dwc_otg_set_param_thr_ctl(core_if,
32858 + dwc_otg_module_params.thr_ctl);
32859 + }
32860 + if (dwc_otg_module_params.mpi_enable != -1) {
32861 + retval +=
32862 + dwc_otg_set_param_mpi_enable(core_if,
32863 + dwc_otg_module_params.
32864 + mpi_enable);
32865 + }
32866 + if (dwc_otg_module_params.pti_enable != -1) {
32867 + retval +=
32868 + dwc_otg_set_param_pti_enable(core_if,
32869 + dwc_otg_module_params.
32870 + pti_enable);
32871 + }
32872 + if (dwc_otg_module_params.lpm_enable != -1) {
32873 + retval +=
32874 + dwc_otg_set_param_lpm_enable(core_if,
32875 + dwc_otg_module_params.
32876 + lpm_enable);
32877 + }
32878 + if (dwc_otg_module_params.ic_usb_cap != -1) {
32879 + retval +=
32880 + dwc_otg_set_param_ic_usb_cap(core_if,
32881 + dwc_otg_module_params.
32882 + ic_usb_cap);
32883 + }
32884 + if (dwc_otg_module_params.tx_thr_length != -1) {
32885 + retval +=
32886 + dwc_otg_set_param_tx_thr_length(core_if,
32887 + dwc_otg_module_params.tx_thr_length);
32888 + }
32889 + if (dwc_otg_module_params.rx_thr_length != -1) {
32890 + retval +=
32891 + dwc_otg_set_param_rx_thr_length(core_if,
32892 + dwc_otg_module_params.
32893 + rx_thr_length);
32894 + }
32895 + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
32896 + retval +=
32897 + dwc_otg_set_param_ahb_thr_ratio(core_if,
32898 + dwc_otg_module_params.ahb_thr_ratio);
32899 + }
32900 + if (dwc_otg_module_params.power_down != -1) {
32901 + retval +=
32902 + dwc_otg_set_param_power_down(core_if,
32903 + dwc_otg_module_params.power_down);
32904 + }
32905 + if (dwc_otg_module_params.reload_ctl != -1) {
32906 + retval +=
32907 + dwc_otg_set_param_reload_ctl(core_if,
32908 + dwc_otg_module_params.reload_ctl);
32909 + }
32910 +
32911 + if (dwc_otg_module_params.dev_out_nak != -1) {
32912 + retval +=
32913 + dwc_otg_set_param_dev_out_nak(core_if,
32914 + dwc_otg_module_params.dev_out_nak);
32915 + }
32916 +
32917 + if (dwc_otg_module_params.cont_on_bna != -1) {
32918 + retval +=
32919 + dwc_otg_set_param_cont_on_bna(core_if,
32920 + dwc_otg_module_params.cont_on_bna);
32921 + }
32922 +
32923 + if (dwc_otg_module_params.ahb_single != -1) {
32924 + retval +=
32925 + dwc_otg_set_param_ahb_single(core_if,
32926 + dwc_otg_module_params.ahb_single);
32927 + }
32928 +
32929 + if (dwc_otg_module_params.otg_ver != -1) {
32930 + retval +=
32931 + dwc_otg_set_param_otg_ver(core_if,
32932 + dwc_otg_module_params.otg_ver);
32933 + }
32934 + if (dwc_otg_module_params.adp_enable != -1) {
32935 + retval +=
32936 + dwc_otg_set_param_adp_enable(core_if,
32937 + dwc_otg_module_params.
32938 + adp_enable);
32939 + }
32940 + return retval;
32941 +}
32942 +
32943 +/**
32944 + * This function is the top level interrupt handler for the Common
32945 + * (Device and host modes) interrupts.
32946 + */
32947 +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
32948 +{
32949 + int32_t retval = IRQ_NONE;
32950 +
32951 + retval = dwc_otg_handle_common_intr(dev);
32952 + if (retval != 0) {
32953 + S3C2410X_CLEAR_EINTPEND();
32954 + }
32955 + return IRQ_RETVAL(retval);
32956 +}
32957 +
32958 +/**
32959 + * This function is called when a lm_device is unregistered with the
32960 + * dwc_otg_driver. This happens, for example, when the rmmod command is
32961 + * executed. The device may or may not be electrically present. If it is
32962 + * present, the driver stops device processing. Any resources used on behalf
32963 + * of this device are freed.
32964 + *
32965 + * @param _dev
32966 + */
32967 +#ifdef LM_INTERFACE
32968 +#define REM_RETVAL(n)
32969 +static void dwc_otg_driver_remove( struct lm_device *_dev )
32970 +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
32971 +#elif defined(PCI_INTERFACE)
32972 +#define REM_RETVAL(n)
32973 +static void dwc_otg_driver_remove( struct pci_dev *_dev )
32974 +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
32975 +#elif defined(PLATFORM_INTERFACE)
32976 +#define REM_RETVAL(n) n
32977 +static int dwc_otg_driver_remove( struct platform_device *_dev )
32978 +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
32979 +#endif
32980 +
32981 + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
32982 +
32983 + if (!otg_dev) {
32984 + /* Memory allocation for the dwc_otg_device failed. */
32985 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
32986 + return REM_RETVAL(-ENOMEM);
32987 + }
32988 +#ifndef DWC_DEVICE_ONLY
32989 + if (otg_dev->hcd) {
32990 + hcd_remove(_dev);
32991 + } else {
32992 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
32993 + return REM_RETVAL(-EINVAL);
32994 + }
32995 +#endif
32996 +
32997 +#ifndef DWC_HOST_ONLY
32998 + if (otg_dev->pcd) {
32999 + pcd_remove(_dev);
33000 + } else {
33001 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
33002 + return REM_RETVAL(-EINVAL);
33003 + }
33004 +#endif
33005 + /*
33006 + * Free the IRQ
33007 + */
33008 + if (otg_dev->common_irq_installed) {
33009 + free_irq(otg_dev->os_dep.irq_num, otg_dev);
33010 + } else {
33011 + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
33012 + return REM_RETVAL(-ENXIO);
33013 + }
33014 +
33015 + if (otg_dev->core_if) {
33016 + dwc_otg_cil_remove(otg_dev->core_if);
33017 + } else {
33018 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
33019 + return REM_RETVAL(-ENXIO);
33020 + }
33021 +
33022 + /*
33023 + * Remove the device attributes
33024 + */
33025 + dwc_otg_attr_remove(_dev);
33026 +
33027 + /*
33028 + * Return the memory.
33029 + */
33030 + if (otg_dev->os_dep.base) {
33031 + iounmap(otg_dev->os_dep.base);
33032 + }
33033 + DWC_FREE(otg_dev);
33034 +
33035 + /*
33036 + * Clear the drvdata pointer.
33037 + */
33038 +#ifdef LM_INTERFACE
33039 + lm_set_drvdata(_dev, 0);
33040 +#elif defined(PCI_INTERFACE)
33041 + release_mem_region(otg_dev->os_dep.rsrc_start,
33042 + otg_dev->os_dep.rsrc_len);
33043 + pci_set_drvdata(_dev, 0);
33044 +#elif defined(PLATFORM_INTERFACE)
33045 + platform_set_drvdata(_dev, 0);
33046 +#endif
33047 + return REM_RETVAL(0);
33048 +}
33049 +
33050 +/**
33051 + * This function is called when an lm_device is bound to a
33052 + * dwc_otg_driver. It creates the driver components required to
33053 + * control the device (CIL, HCD, and PCD) and it initializes the
33054 + * device. The driver components are stored in a dwc_otg_device
33055 + * structure. A reference to the dwc_otg_device is saved in the
33056 + * lm_device. This allows the driver to access the dwc_otg_device
33057 + * structure on subsequent calls to driver methods for this device.
33058 + *
33059 + * @param _dev Bus device
33060 + */
33061 +static int dwc_otg_driver_probe(
33062 +#ifdef LM_INTERFACE
33063 + struct lm_device *_dev
33064 +#elif defined(PCI_INTERFACE)
33065 + struct pci_dev *_dev,
33066 + const struct pci_device_id *id
33067 +#elif defined(PLATFORM_INTERFACE)
33068 + struct platform_device *_dev
33069 +#endif
33070 + )
33071 +{
33072 + int retval = 0;
33073 + dwc_otg_device_t *dwc_otg_device;
33074 + int devirq;
33075 +
33076 + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
33077 +#ifdef LM_INTERFACE
33078 + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
33079 +#elif defined(PCI_INTERFACE)
33080 + if (!id) {
33081 + DWC_ERROR("Invalid pci_device_id %p", id);
33082 + return -EINVAL;
33083 + }
33084 +
33085 + if (!_dev || (pci_enable_device(_dev) < 0)) {
33086 + DWC_ERROR("Invalid pci_device %p", _dev);
33087 + return -ENODEV;
33088 + }
33089 + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
33090 + /* other stuff needed as well? */
33091 +
33092 +#elif defined(PLATFORM_INTERFACE)
33093 + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
33094 + (unsigned)_dev->resource->start,
33095 + (unsigned)(_dev->resource->end - _dev->resource->start));
33096 +#endif
33097 +
33098 + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
33099 +
33100 + if (!dwc_otg_device) {
33101 + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
33102 + return -ENOMEM;
33103 + }
33104 +
33105 + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
33106 + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
33107 + dwc_otg_device->os_dep.platformdev = _dev;
33108 +
33109 + /*
33110 + * Map the DWC_otg Core memory into virtual address space.
33111 + */
33112 +#ifdef LM_INTERFACE
33113 + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
33114 +
33115 + if (!dwc_otg_device->os_dep.base) {
33116 + dev_err(&_dev->dev, "ioremap() failed\n");
33117 + DWC_FREE(dwc_otg_device);
33118 + return -ENOMEM;
33119 + }
33120 + dev_dbg(&_dev->dev, "base=0x%08x\n",
33121 + (unsigned)dwc_otg_device->os_dep.base);
33122 +#elif defined(PCI_INTERFACE)
33123 + _dev->current_state = PCI_D0;
33124 + _dev->dev.power.power_state = PMSG_ON;
33125 +
33126 + if (!_dev->irq) {
33127 + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
33128 + pci_name(_dev));
33129 + iounmap(dwc_otg_device->os_dep.base);
33130 + DWC_FREE(dwc_otg_device);
33131 + return -ENODEV;
33132 + }
33133 +
33134 + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
33135 + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
33136 + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
33137 + (unsigned)dwc_otg_device->os_dep.rsrc_start,
33138 + (unsigned)dwc_otg_device->os_dep.rsrc_len);
33139 + if (!request_mem_region
33140 + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
33141 + "dwc_otg")) {
33142 + dev_dbg(&_dev->dev, "error requesting memory\n");
33143 + iounmap(dwc_otg_device->os_dep.base);
33144 + DWC_FREE(dwc_otg_device);
33145 + return -EFAULT;
33146 + }
33147 +
33148 + dwc_otg_device->os_dep.base =
33149 + ioremap(dwc_otg_device->os_dep.rsrc_start,
33150 + dwc_otg_device->os_dep.rsrc_len);
33151 + if (dwc_otg_device->os_dep.base == NULL) {
33152 + dev_dbg(&_dev->dev, "error mapping memory\n");
33153 + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
33154 + dwc_otg_device->os_dep.rsrc_len);
33155 + iounmap(dwc_otg_device->os_dep.base);
33156 + DWC_FREE(dwc_otg_device);
33157 + return -EFAULT;
33158 + }
33159 + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
33160 + dwc_otg_device->os_dep.base);
33161 + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
33162 + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
33163 + dwc_otg_device->os_dep.base);
33164 + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
33165 + (unsigned)dwc_otg_device->os_dep.rsrc_start,
33166 + dwc_otg_device->os_dep.base);
33167 +
33168 + pci_set_master(_dev);
33169 + pci_set_drvdata(_dev, dwc_otg_device);
33170 +#elif defined(PLATFORM_INTERFACE)
33171 + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
33172 + _dev->resource->start,
33173 + _dev->resource->end - _dev->resource->start + 1);
33174 +#if 1
33175 + if (!request_mem_region(_dev->resource[0].start,
33176 + _dev->resource[0].end - _dev->resource[0].start + 1,
33177 + "dwc_otg")) {
33178 + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
33179 + retval = -EFAULT;
33180 + goto fail;
33181 + }
33182 +
33183 + dwc_otg_device->os_dep.base = ioremap(_dev->resource[0].start,
33184 + _dev->resource[0].end -
33185 + _dev->resource[0].start+1);
33186 + if (fiq_enable)
33187 + {
33188 + if (!request_mem_region(_dev->resource[1].start,
33189 + _dev->resource[1].end - _dev->resource[1].start + 1,
33190 + "dwc_otg")) {
33191 + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
33192 + retval = -EFAULT;
33193 + goto fail;
33194 + }
33195 +
33196 + dwc_otg_device->os_dep.mphi_base = ioremap(_dev->resource[1].start,
33197 + _dev->resource[1].end -
33198 + _dev->resource[1].start + 1);
33199 + dwc_otg_device->os_dep.use_swirq = (_dev->resource[1].end - _dev->resource[1].start) == 0x200;
33200 + }
33201 +
33202 +#else
33203 + {
33204 + struct map_desc desc = {
33205 + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
33206 + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
33207 + .length = SZ_128K,
33208 + .type = MT_DEVICE
33209 + };
33210 + iotable_init(&desc, 1);
33211 + dwc_otg_device->os_dep.base = (void *)desc.virtual;
33212 + }
33213 +#endif
33214 + if (!dwc_otg_device->os_dep.base) {
33215 + dev_err(&_dev->dev, "ioremap() failed\n");
33216 + retval = -ENOMEM;
33217 + goto fail;
33218 + }
33219 +#endif
33220 +
33221 + /*
33222 + * Initialize driver data to point to the global DWC_otg
33223 + * Device structure.
33224 + */
33225 +#ifdef LM_INTERFACE
33226 + lm_set_drvdata(_dev, dwc_otg_device);
33227 +#elif defined(PLATFORM_INTERFACE)
33228 + platform_set_drvdata(_dev, dwc_otg_device);
33229 +#endif
33230 + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
33231 +
33232 + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
33233 + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
33234 + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
33235 +
33236 + if (!dwc_otg_device->core_if) {
33237 + dev_err(&_dev->dev, "CIL initialization failed!\n");
33238 + retval = -ENOMEM;
33239 + goto fail;
33240 + }
33241 +
33242 + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
33243 + /*
33244 + * Attempt to ensure this device is really a DWC_otg Controller.
33245 + * Read and verify the SNPSID register contents. The value should be
33246 + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
33247 + * as in "OTG version 2.XX" or "OTG version 3.XX".
33248 + */
33249 +
33250 + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
33251 + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
33252 + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
33253 + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
33254 + retval = -EINVAL;
33255 + goto fail;
33256 + }
33257 +
33258 + /*
33259 + * Validate parameter values.
33260 + */
33261 + dev_dbg(&_dev->dev, "Calling set_parameters\n");
33262 + if (set_parameters(dwc_otg_device->core_if)) {
33263 + retval = -EINVAL;
33264 + goto fail;
33265 + }
33266 +
33267 + /*
33268 + * Create Device Attributes in sysfs
33269 + */
33270 + dev_dbg(&_dev->dev, "Calling attr_create\n");
33271 + dwc_otg_attr_create(_dev);
33272 +
33273 + /*
33274 + * Disable the global interrupt until all the interrupt
33275 + * handlers are installed.
33276 + */
33277 + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
33278 + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
33279 +
33280 + /*
33281 + * Install the interrupt handler for the common interrupts before
33282 + * enabling common interrupts in core_init below.
33283 + */
33284 +
33285 +#if defined(PLATFORM_INTERFACE)
33286 + devirq = platform_get_irq_byname(_dev, fiq_enable ? "soft" : "usb");
33287 + if (devirq < 0)
33288 + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
33289 +#else
33290 + devirq = _dev->irq;
33291 +#endif
33292 + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
33293 + devirq);
33294 + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
33295 + retval = request_irq(devirq, dwc_otg_common_irq,
33296 + IRQF_SHARED,
33297 + "dwc_otg", dwc_otg_device);
33298 + if (retval) {
33299 + DWC_ERROR("request of irq%d failed\n", devirq);
33300 + retval = -EBUSY;
33301 + goto fail;
33302 + } else {
33303 + dwc_otg_device->common_irq_installed = 1;
33304 + }
33305 + dwc_otg_device->os_dep.irq_num = devirq;
33306 + dwc_otg_device->os_dep.fiq_num = -EINVAL;
33307 + if (fiq_enable) {
33308 + int devfiq = platform_get_irq_byname(_dev, "usb");
33309 + if (devfiq < 0)
33310 + devfiq = platform_get_irq(_dev, 1);
33311 + dwc_otg_device->os_dep.fiq_num = devfiq;
33312 + }
33313 +
33314 +#ifndef IRQF_TRIGGER_LOW
33315 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
33316 + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
33317 + set_irq_type(devirq,
33318 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
33319 + IRQT_LOW
33320 +#else
33321 + IRQ_TYPE_LEVEL_LOW
33322 +#endif
33323 + );
33324 +#endif
33325 +#endif /*IRQF_TRIGGER_LOW*/
33326 +
33327 + /*
33328 + * Initialize the DWC_otg core.
33329 + */
33330 + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
33331 + dwc_otg_core_init(dwc_otg_device->core_if);
33332 +
33333 +#ifndef DWC_HOST_ONLY
33334 + /*
33335 + * Initialize the PCD
33336 + */
33337 + dev_dbg(&_dev->dev, "Calling pcd_init\n");
33338 + retval = pcd_init(_dev);
33339 + if (retval != 0) {
33340 + DWC_ERROR("pcd_init failed\n");
33341 + dwc_otg_device->pcd = NULL;
33342 + goto fail;
33343 + }
33344 +#endif
33345 +#ifndef DWC_DEVICE_ONLY
33346 + /*
33347 + * Initialize the HCD
33348 + */
33349 + dev_dbg(&_dev->dev, "Calling hcd_init\n");
33350 + retval = hcd_init(_dev);
33351 + if (retval != 0) {
33352 + DWC_ERROR("hcd_init failed\n");
33353 + dwc_otg_device->hcd = NULL;
33354 + goto fail;
33355 + }
33356 +#endif
33357 + /* Recover from drvdata having been overwritten by hcd_init() */
33358 +#ifdef LM_INTERFACE
33359 + lm_set_drvdata(_dev, dwc_otg_device);
33360 +#elif defined(PLATFORM_INTERFACE)
33361 + platform_set_drvdata(_dev, dwc_otg_device);
33362 +#elif defined(PCI_INTERFACE)
33363 + pci_set_drvdata(_dev, dwc_otg_device);
33364 + dwc_otg_device->os_dep.pcidev = _dev;
33365 +#endif
33366 +
33367 + /*
33368 + * Enable the global interrupt after all the interrupt
33369 + * handlers are installed if there is no ADP support else
33370 + * perform initial actions required for Internal ADP logic.
33371 + */
33372 + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
33373 + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
33374 + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
33375 + dev_dbg(&_dev->dev, "Done\n");
33376 + } else
33377 + dwc_otg_adp_start(dwc_otg_device->core_if,
33378 + dwc_otg_is_host_mode(dwc_otg_device->core_if));
33379 +
33380 + return 0;
33381 +
33382 +fail:
33383 + dwc_otg_driver_remove(_dev);
33384 + return retval;
33385 +}
33386 +
33387 +/**
33388 + * This structure defines the methods to be called by a bus driver
33389 + * during the lifecycle of a device on that bus. Both drivers and
33390 + * devices are registered with a bus driver. The bus driver matches
33391 + * devices to drivers based on information in the device and driver
33392 + * structures.
33393 + *
33394 + * The probe function is called when the bus driver matches a device
33395 + * to this driver. The remove function is called when a device is
33396 + * unregistered with the bus driver.
33397 + */
33398 +#ifdef LM_INTERFACE
33399 +static struct lm_driver dwc_otg_driver = {
33400 + .drv = {.name = (char *)dwc_driver_name,},
33401 + .probe = dwc_otg_driver_probe,
33402 + .remove = dwc_otg_driver_remove,
33403 + // 'suspend' and 'resume' absent
33404 +};
33405 +#elif defined(PCI_INTERFACE)
33406 +static const struct pci_device_id pci_ids[] = { {
33407 + PCI_DEVICE(0x16c3, 0xabcd),
33408 + .driver_data =
33409 + (unsigned long)0xdeadbeef,
33410 + }, { /* end: all zeroes */ }
33411 +};
33412 +
33413 +MODULE_DEVICE_TABLE(pci, pci_ids);
33414 +
33415 +/* pci driver glue; this is a "new style" PCI driver module */
33416 +static struct pci_driver dwc_otg_driver = {
33417 + .name = "dwc_otg",
33418 + .id_table = pci_ids,
33419 +
33420 + .probe = dwc_otg_driver_probe,
33421 + .remove = dwc_otg_driver_remove,
33422 +
33423 + .driver = {
33424 + .name = (char *)dwc_driver_name,
33425 + },
33426 +};
33427 +#elif defined(PLATFORM_INTERFACE)
33428 +static struct platform_device_id platform_ids[] = {
33429 + {
33430 + .name = "bcm2708_usb",
33431 + .driver_data = (kernel_ulong_t) 0xdeadbeef,
33432 + },
33433 + { /* end: all zeroes */ }
33434 +};
33435 +MODULE_DEVICE_TABLE(platform, platform_ids);
33436 +
33437 +static const struct of_device_id dwc_otg_of_match_table[] = {
33438 + { .compatible = "brcm,bcm2708-usb", },
33439 + {},
33440 +};
33441 +MODULE_DEVICE_TABLE(of, dwc_otg_of_match_table);
33442 +
33443 +static struct platform_driver dwc_otg_driver = {
33444 + .driver = {
33445 + .name = (char *)dwc_driver_name,
33446 + .of_match_table = dwc_otg_of_match_table,
33447 + },
33448 + .id_table = platform_ids,
33449 +
33450 + .probe = dwc_otg_driver_probe,
33451 + .remove = dwc_otg_driver_remove,
33452 + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
33453 +};
33454 +#endif
33455 +
33456 +/**
33457 + * This function is called when the dwc_otg_driver is installed with the
33458 + * insmod command. It registers the dwc_otg_driver structure with the
33459 + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
33460 + * to be called. In addition, the bus driver will automatically expose
33461 + * attributes defined for the device and driver in the special sysfs file
33462 + * system.
33463 + *
33464 + * @return
33465 + */
33466 +static int __init dwc_otg_driver_init(void)
33467 +{
33468 + int retval = 0;
33469 + int error;
33470 + struct device_driver *drv;
33471 +
33472 + if(fiq_fsm_enable && !fiq_enable) {
33473 + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
33474 + fiq_enable = 1;
33475 + }
33476 +
33477 + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
33478 + DWC_DRIVER_VERSION,
33479 +#ifdef LM_INTERFACE
33480 + "logicmodule");
33481 + retval = lm_driver_register(&dwc_otg_driver);
33482 + drv = &dwc_otg_driver.drv;
33483 +#elif defined(PCI_INTERFACE)
33484 + "pci");
33485 + retval = pci_register_driver(&dwc_otg_driver);
33486 + drv = &dwc_otg_driver.driver;
33487 +#elif defined(PLATFORM_INTERFACE)
33488 + "platform");
33489 + retval = platform_driver_register(&dwc_otg_driver);
33490 + drv = &dwc_otg_driver.driver;
33491 +#endif
33492 + if (retval < 0) {
33493 + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
33494 + return retval;
33495 + }
33496 + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
33497 + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
33498 + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
33499 +
33500 + error = driver_create_file(drv, &driver_attr_version);
33501 +#ifdef DEBUG
33502 + error = driver_create_file(drv, &driver_attr_debuglevel);
33503 +#endif
33504 + return retval;
33505 +}
33506 +
33507 +module_init(dwc_otg_driver_init);
33508 +
33509 +/**
33510 + * This function is called when the driver is removed from the kernel
33511 + * with the rmmod command. The driver unregisters itself with its bus
33512 + * driver.
33513 + *
33514 + */
33515 +static void __exit dwc_otg_driver_cleanup(void)
33516 +{
33517 + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
33518 +
33519 +#ifdef LM_INTERFACE
33520 + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
33521 + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
33522 + lm_driver_unregister(&dwc_otg_driver);
33523 +#elif defined(PCI_INTERFACE)
33524 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
33525 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
33526 + pci_unregister_driver(&dwc_otg_driver);
33527 +#elif defined(PLATFORM_INTERFACE)
33528 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
33529 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
33530 + platform_driver_unregister(&dwc_otg_driver);
33531 +#endif
33532 +
33533 + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
33534 +}
33535 +
33536 +module_exit(dwc_otg_driver_cleanup);
33537 +
33538 +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
33539 +MODULE_AUTHOR("Synopsys Inc.");
33540 +MODULE_LICENSE("GPL");
33541 +
33542 +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
33543 +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
33544 +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
33545 +MODULE_PARM_DESC(opt, "OPT Mode");
33546 +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
33547 +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
33548 +
33549 +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
33550 + 0444);
33551 +MODULE_PARM_DESC(dma_desc_enable,
33552 + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
33553 +
33554 +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
33555 + 0444);
33556 +MODULE_PARM_DESC(dma_burst_size,
33557 + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
33558 +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
33559 +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
33560 +module_param_named(host_support_fs_ls_low_power,
33561 + dwc_otg_module_params.host_support_fs_ls_low_power, int,
33562 + 0444);
33563 +MODULE_PARM_DESC(host_support_fs_ls_low_power,
33564 + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
33565 +module_param_named(host_ls_low_power_phy_clk,
33566 + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
33567 +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
33568 + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
33569 +module_param_named(enable_dynamic_fifo,
33570 + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
33571 +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
33572 +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
33573 + 0444);
33574 +MODULE_PARM_DESC(data_fifo_size,
33575 + "Total number of words in the data FIFO memory 32-32768");
33576 +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
33577 + int, 0444);
33578 +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
33579 +module_param_named(dev_nperio_tx_fifo_size,
33580 + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
33581 +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
33582 + "Number of words in the non-periodic Tx FIFO 16-32768");
33583 +module_param_named(dev_perio_tx_fifo_size_1,
33584 + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
33585 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
33586 + "Number of words in the periodic Tx FIFO 4-768");
33587 +module_param_named(dev_perio_tx_fifo_size_2,
33588 + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
33589 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
33590 + "Number of words in the periodic Tx FIFO 4-768");
33591 +module_param_named(dev_perio_tx_fifo_size_3,
33592 + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
33593 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
33594 + "Number of words in the periodic Tx FIFO 4-768");
33595 +module_param_named(dev_perio_tx_fifo_size_4,
33596 + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
33597 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
33598 + "Number of words in the periodic Tx FIFO 4-768");
33599 +module_param_named(dev_perio_tx_fifo_size_5,
33600 + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
33601 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
33602 + "Number of words in the periodic Tx FIFO 4-768");
33603 +module_param_named(dev_perio_tx_fifo_size_6,
33604 + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
33605 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
33606 + "Number of words in the periodic Tx FIFO 4-768");
33607 +module_param_named(dev_perio_tx_fifo_size_7,
33608 + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
33609 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
33610 + "Number of words in the periodic Tx FIFO 4-768");
33611 +module_param_named(dev_perio_tx_fifo_size_8,
33612 + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
33613 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
33614 + "Number of words in the periodic Tx FIFO 4-768");
33615 +module_param_named(dev_perio_tx_fifo_size_9,
33616 + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
33617 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
33618 + "Number of words in the periodic Tx FIFO 4-768");
33619 +module_param_named(dev_perio_tx_fifo_size_10,
33620 + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
33621 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
33622 + "Number of words in the periodic Tx FIFO 4-768");
33623 +module_param_named(dev_perio_tx_fifo_size_11,
33624 + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
33625 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
33626 + "Number of words in the periodic Tx FIFO 4-768");
33627 +module_param_named(dev_perio_tx_fifo_size_12,
33628 + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
33629 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
33630 + "Number of words in the periodic Tx FIFO 4-768");
33631 +module_param_named(dev_perio_tx_fifo_size_13,
33632 + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
33633 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
33634 + "Number of words in the periodic Tx FIFO 4-768");
33635 +module_param_named(dev_perio_tx_fifo_size_14,
33636 + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
33637 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
33638 + "Number of words in the periodic Tx FIFO 4-768");
33639 +module_param_named(dev_perio_tx_fifo_size_15,
33640 + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
33641 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
33642 + "Number of words in the periodic Tx FIFO 4-768");
33643 +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
33644 + int, 0444);
33645 +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
33646 +module_param_named(host_nperio_tx_fifo_size,
33647 + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
33648 +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
33649 + "Number of words in the non-periodic Tx FIFO 16-32768");
33650 +module_param_named(host_perio_tx_fifo_size,
33651 + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
33652 +MODULE_PARM_DESC(host_perio_tx_fifo_size,
33653 + "Number of words in the host periodic Tx FIFO 16-32768");
33654 +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
33655 + int, 0444);
33656 +/** @todo Set the max to 512K, modify checks */
33657 +MODULE_PARM_DESC(max_transfer_size,
33658 + "The maximum transfer size supported in bytes 2047-65535");
33659 +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
33660 + int, 0444);
33661 +MODULE_PARM_DESC(max_packet_count,
33662 + "The maximum number of packets in a transfer 15-511");
33663 +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
33664 + 0444);
33665 +MODULE_PARM_DESC(host_channels,
33666 + "The number of host channel registers to use 1-16");
33667 +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
33668 + 0444);
33669 +MODULE_PARM_DESC(dev_endpoints,
33670 + "The number of endpoints in addition to EP0 available for device mode 1-15");
33671 +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
33672 +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
33673 +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
33674 + 0444);
33675 +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
33676 +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
33677 +MODULE_PARM_DESC(phy_ulpi_ddr,
33678 + "ULPI at double or single data rate 0=Single 1=Double");
33679 +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
33680 + int, 0444);
33681 +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
33682 + "ULPI PHY using internal or external vbus 0=Internal");
33683 +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
33684 +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
33685 +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
33686 +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
33687 +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
33688 +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
33689 +module_param_named(debug, g_dbg_lvl, int, 0444);
33690 +MODULE_PARM_DESC(debug, "");
33691 +
33692 +module_param_named(en_multiple_tx_fifo,
33693 + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
33694 +MODULE_PARM_DESC(en_multiple_tx_fifo,
33695 + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
33696 +module_param_named(dev_tx_fifo_size_1,
33697 + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
33698 +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
33699 +module_param_named(dev_tx_fifo_size_2,
33700 + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
33701 +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
33702 +module_param_named(dev_tx_fifo_size_3,
33703 + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
33704 +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
33705 +module_param_named(dev_tx_fifo_size_4,
33706 + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
33707 +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
33708 +module_param_named(dev_tx_fifo_size_5,
33709 + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
33710 +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
33711 +module_param_named(dev_tx_fifo_size_6,
33712 + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
33713 +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
33714 +module_param_named(dev_tx_fifo_size_7,
33715 + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
33716 +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
33717 +module_param_named(dev_tx_fifo_size_8,
33718 + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
33719 +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
33720 +module_param_named(dev_tx_fifo_size_9,
33721 + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
33722 +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
33723 +module_param_named(dev_tx_fifo_size_10,
33724 + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
33725 +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
33726 +module_param_named(dev_tx_fifo_size_11,
33727 + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
33728 +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
33729 +module_param_named(dev_tx_fifo_size_12,
33730 + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
33731 +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
33732 +module_param_named(dev_tx_fifo_size_13,
33733 + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
33734 +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
33735 +module_param_named(dev_tx_fifo_size_14,
33736 + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
33737 +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
33738 +module_param_named(dev_tx_fifo_size_15,
33739 + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
33740 +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
33741 +
33742 +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
33743 +MODULE_PARM_DESC(thr_ctl,
33744 + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
33745 +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
33746 + 0444);
33747 +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
33748 +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
33749 + 0444);
33750 +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
33751 +
33752 +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
33753 +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
33754 +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
33755 +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
33756 +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
33757 +MODULE_PARM_DESC(ic_usb_cap,
33758 + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
33759 +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
33760 + 0444);
33761 +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
33762 +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
33763 +MODULE_PARM_DESC(power_down, "Power Down Mode");
33764 +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
33765 +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
33766 +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
33767 +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
33768 +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
33769 +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
33770 +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
33771 +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
33772 +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
33773 +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
33774 +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
33775 +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
33776 +module_param(microframe_schedule, bool, 0444);
33777 +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
33778 +
33779 +module_param(fiq_enable, bool, 0444);
33780 +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
33781 +module_param(nak_holdoff, ushort, 0644);
33782 +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
33783 +module_param(fiq_fsm_enable, bool, 0444);
33784 +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
33785 +module_param(fiq_fsm_mask, ushort, 0444);
33786 +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
33787 + "Bit 0 : Non-periodic split transactions\n"
33788 + "Bit 1 : Periodic split transactions\n"
33789 + "Bit 2 : High-speed multi-transfer isochronous\n"
33790 + "All other bits should be set 0.");
33791 +module_param(int_ep_interval_min, ushort, 0644);
33792 +MODULE_PARM_DESC(int_ep_interval_min, "Clamp high-speed Interrupt endpoints to a minimum polling interval.\n"
33793 + "0..1 = Use endpoint default\n"
33794 + "2..n = Minimum interval n microframes. Use powers of 2.\n");
33795 +
33796 +module_param(cil_force_host, bool, 0644);
33797 +MODULE_PARM_DESC(cil_force_host, "On a connector-ID status change, "
33798 + "force Host Mode regardless of OTG state.");
33799 +
33800 +/** @page "Module Parameters"
33801 + *
33802 + * The following parameters may be specified when starting the module.
33803 + * These parameters define how the DWC_otg controller should be
33804 + * configured. Parameter values are passed to the CIL initialization
33805 + * function dwc_otg_cil_init
33806 + *
33807 + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
33808 + *
33809 +
33810 + <table>
33811 + <tr><td>Parameter Name</td><td>Meaning</td></tr>
33812 +
33813 + <tr>
33814 + <td>otg_cap</td>
33815 + <td>Specifies the OTG capabilities. The driver will automatically detect the
33816 + value for this parameter if none is specified.
33817 + - 0: HNP and SRP capable (default, if available)
33818 + - 1: SRP Only capable
33819 + - 2: No HNP/SRP capable
33820 + </td></tr>
33821 +
33822 + <tr>
33823 + <td>dma_enable</td>
33824 + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
33825 + The driver will automatically detect the value for this parameter if none is
33826 + specified.
33827 + - 0: Slave
33828 + - 1: DMA (default, if available)
33829 + </td></tr>
33830 +
33831 + <tr>
33832 + <td>dma_burst_size</td>
33833 + <td>The DMA Burst size (applicable only for External DMA Mode).
33834 + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
33835 + </td></tr>
33836 +
33837 + <tr>
33838 + <td>speed</td>
33839 + <td>Specifies the maximum speed of operation in host and device mode. The
33840 + actual speed depends on the speed of the attached device and the value of
33841 + phy_type.
33842 + - 0: High Speed (default)
33843 + - 1: Full Speed
33844 + </td></tr>
33845 +
33846 + <tr>
33847 + <td>host_support_fs_ls_low_power</td>
33848 + <td>Specifies whether low power mode is supported when attached to a Full
33849 + Speed or Low Speed device in host mode.
33850 + - 0: Don't support low power mode (default)
33851 + - 1: Support low power mode
33852 + </td></tr>
33853 +
33854 + <tr>
33855 + <td>host_ls_low_power_phy_clk</td>
33856 + <td>Specifies the PHY clock rate in low power mode when connected to a Low
33857 + Speed device in host mode. This parameter is applicable only if
33858 + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
33859 + - 0: 48 MHz (default)
33860 + - 1: 6 MHz
33861 + </td></tr>
33862 +
33863 + <tr>
33864 + <td>enable_dynamic_fifo</td>
33865 + <td> Specifies whether FIFOs may be resized by the driver software.
33866 + - 0: Use cC FIFO size parameters
33867 + - 1: Allow dynamic FIFO sizing (default)
33868 + </td></tr>
33869 +
33870 + <tr>
33871 + <td>data_fifo_size</td>
33872 + <td>Total number of 4-byte words in the data FIFO memory. This memory
33873 + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
33874 + - Values: 32 to 32768 (default 8192)
33875 +
33876 + Note: The total FIFO memory depth in the FPGA configuration is 8192.
33877 + </td></tr>
33878 +
33879 + <tr>
33880 + <td>dev_rx_fifo_size</td>
33881 + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
33882 + FIFO sizing is enabled.
33883 + - Values: 16 to 32768 (default 1064)
33884 + </td></tr>
33885 +
33886 + <tr>
33887 + <td>dev_nperio_tx_fifo_size</td>
33888 + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
33889 + dynamic FIFO sizing is enabled.
33890 + - Values: 16 to 32768 (default 1024)
33891 + </td></tr>
33892 +
33893 + <tr>
33894 + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
33895 + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
33896 + when dynamic FIFO sizing is enabled.
33897 + - Values: 4 to 768 (default 256)
33898 + </td></tr>
33899 +
33900 + <tr>
33901 + <td>host_rx_fifo_size</td>
33902 + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
33903 + sizing is enabled.
33904 + - Values: 16 to 32768 (default 1024)
33905 + </td></tr>
33906 +
33907 + <tr>
33908 + <td>host_nperio_tx_fifo_size</td>
33909 + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
33910 + dynamic FIFO sizing is enabled in the core.
33911 + - Values: 16 to 32768 (default 1024)
33912 + </td></tr>
33913 +
33914 + <tr>
33915 + <td>host_perio_tx_fifo_size</td>
33916 + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
33917 + sizing is enabled.
33918 + - Values: 16 to 32768 (default 1024)
33919 + </td></tr>
33920 +
33921 + <tr>
33922 + <td>max_transfer_size</td>
33923 + <td>The maximum transfer size supported in bytes.
33924 + - Values: 2047 to 65,535 (default 65,535)
33925 + </td></tr>
33926 +
33927 + <tr>
33928 + <td>max_packet_count</td>
33929 + <td>The maximum number of packets in a transfer.
33930 + - Values: 15 to 511 (default 511)
33931 + </td></tr>
33932 +
33933 + <tr>
33934 + <td>host_channels</td>
33935 + <td>The number of host channel registers to use.
33936 + - Values: 1 to 16 (default 12)
33937 +
33938 + Note: The FPGA configuration supports a maximum of 12 host channels.
33939 + </td></tr>
33940 +
33941 + <tr>
33942 + <td>dev_endpoints</td>
33943 + <td>The number of endpoints in addition to EP0 available for device mode
33944 + operations.
33945 + - Values: 1 to 15 (default 6 IN and OUT)
33946 +
33947 + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
33948 + addition to EP0.
33949 + </td></tr>
33950 +
33951 + <tr>
33952 + <td>phy_type</td>
33953 + <td>Specifies the type of PHY interface to use. By default, the driver will
33954 + automatically detect the phy_type.
33955 + - 0: Full Speed
33956 + - 1: UTMI+ (default, if available)
33957 + - 2: ULPI
33958 + </td></tr>
33959 +
33960 + <tr>
33961 + <td>phy_utmi_width</td>
33962 + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
33963 + phy_type of UTMI+. Also, this parameter is applicable only if the
33964 + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
33965 + core has been configured to work at either data path width.
33966 + - Values: 8 or 16 bits (default 16)
33967 + </td></tr>
33968 +
33969 + <tr>
33970 + <td>phy_ulpi_ddr</td>
33971 + <td>Specifies whether the ULPI operates at double or single data rate. This
33972 + parameter is only applicable if phy_type is ULPI.
33973 + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
33974 + - 1: double data rate ULPI interface with 4 bit wide data bus
33975 + </td></tr>
33976 +
33977 + <tr>
33978 + <td>i2c_enable</td>
33979 + <td>Specifies whether to use the I2C interface for full speed PHY. This
33980 + parameter is only applicable if PHY_TYPE is FS.
33981 + - 0: Disabled (default)
33982 + - 1: Enabled
33983 + </td></tr>
33984 +
33985 + <tr>
33986 + <td>ulpi_fs_ls</td>
33987 + <td>Specifies whether to use ULPI FS/LS mode only.
33988 + - 0: Disabled (default)
33989 + - 1: Enabled
33990 + </td></tr>
33991 +
33992 + <tr>
33993 + <td>ts_dline</td>
33994 + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
33995 + - 0: Disabled (default)
33996 + - 1: Enabled
33997 + </td></tr>
33998 +
33999 + <tr>
34000 + <td>en_multiple_tx_fifo</td>
34001 + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
34002 + The driver will automatically detect the value for this parameter if none is
34003 + specified.
34004 + - 0: Disabled
34005 + - 1: Enabled (default, if available)
34006 + </td></tr>
34007 +
34008 + <tr>
34009 + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
34010 + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
34011 + when dynamic FIFO sizing is enabled.
34012 + - Values: 4 to 768 (default 256)
34013 + </td></tr>
34014 +
34015 + <tr>
34016 + <td>tx_thr_length</td>
34017 + <td>Transmit Threshold length in 32 bit double words
34018 + - Values: 8 to 128 (default 64)
34019 + </td></tr>
34020 +
34021 + <tr>
34022 + <td>rx_thr_length</td>
34023 + <td>Receive Threshold length in 32 bit double words
34024 + - Values: 8 to 128 (default 64)
34025 + </td></tr>
34026 +
34027 +<tr>
34028 + <td>thr_ctl</td>
34029 + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
34030 + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
34031 + Rx transfers accordingly.
34032 + The driver will automatically detect the value for this parameter if none is
34033 + specified.
34034 + - Values: 0 to 7 (default 0)
34035 + Bit values indicate:
34036 + - 0: Thresholding disabled
34037 + - 1: Thresholding enabled
34038 + </td></tr>
34039 +
34040 +<tr>
34041 + <td>dma_desc_enable</td>
34042 + <td>Specifies whether to enable Descriptor DMA mode.
34043 + The driver will automatically detect the value for this parameter if none is
34044 + specified.
34045 + - 0: Descriptor DMA disabled
34046 + - 1: Descriptor DMA (default, if available)
34047 + </td></tr>
34048 +
34049 +<tr>
34050 + <td>mpi_enable</td>
34051 + <td>Specifies whether to enable MPI enhancement mode.
34052 + The driver will automatically detect the value for this parameter if none is
34053 + specified.
34054 + - 0: MPI disabled (default)
34055 + - 1: MPI enable
34056 + </td></tr>
34057 +
34058 +<tr>
34059 + <td>pti_enable</td>
34060 + <td>Specifies whether to enable PTI enhancement support.
34061 + The driver will automatically detect the value for this parameter if none is
34062 + specified.
34063 + - 0: PTI disabled (default)
34064 + - 1: PTI enable
34065 + </td></tr>
34066 +
34067 +<tr>
34068 + <td>lpm_enable</td>
34069 + <td>Specifies whether to enable LPM support.
34070 + The driver will automatically detect the value for this parameter if none is
34071 + specified.
34072 + - 0: LPM disabled
34073 + - 1: LPM enable (default, if available)
34074 + </td></tr>
34075 +
34076 +<tr>
34077 + <td>ic_usb_cap</td>
34078 + <td>Specifies whether to enable IC_USB capability.
34079 + The driver will automatically detect the value for this parameter if none is
34080 + specified.
34081 + - 0: IC_USB disabled (default, if available)
34082 + - 1: IC_USB enable
34083 + </td></tr>
34084 +
34085 +<tr>
34086 + <td>ahb_thr_ratio</td>
34087 + <td>Specifies AHB Threshold ratio.
34088 + - Values: 0 to 3 (default 0)
34089 + </td></tr>
34090 +
34091 +<tr>
34092 + <td>power_down</td>
34093 + <td>Specifies Power Down(Hibernation) Mode.
34094 + The driver will automatically detect the value for this parameter if none is
34095 + specified.
34096 + - 0: Power Down disabled (default)
34097 + - 2: Power Down enabled
34098 + </td></tr>
34099 +
34100 + <tr>
34101 + <td>reload_ctl</td>
34102 + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
34103 + run time. The driver will automatically detect the value for this parameter if
34104 + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
34105 + the core might misbehave.
34106 + - 0: Reload Control disabled (default)
34107 + - 1: Reload Control enabled
34108 + </td></tr>
34109 +
34110 + <tr>
34111 + <td>dev_out_nak</td>
34112 + <td>Specifies whether Device OUT NAK enhancement enabled or no.
34113 + The driver will automatically detect the value for this parameter if
34114 + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
34115 + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
34116 + - 1: The core sets NAK after Bulk OUT transfer complete
34117 + </td></tr>
34118 +
34119 + <tr>
34120 + <td>cont_on_bna</td>
34121 + <td>Specifies whether Enable Continue on BNA enabled or no.
34122 + After receiving BNA interrupt the core disables the endpoint,when the
34123 + endpoint is re-enabled by the application the
34124 + - 0: Core starts processing from the DOEPDMA descriptor (default)
34125 + - 1: Core starts processing from the descriptor which received the BNA.
34126 + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
34127 + </td></tr>
34128 +
34129 + <tr>
34130 + <td>ahb_single</td>
34131 + <td>This bit when programmed supports SINGLE transfers for remainder data
34132 + in a transfer for DMA mode of operation.
34133 + - 0: The remainder data will be sent using INCR burst size (default)
34134 + - 1: The remainder data will be sent using SINGLE burst size.
34135 + </td></tr>
34136 +
34137 +<tr>
34138 + <td>adp_enable</td>
34139 + <td>Specifies whether ADP feature is enabled.
34140 + The driver will automatically detect the value for this parameter if none is
34141 + specified.
34142 + - 0: ADP feature disabled (default)
34143 + - 1: ADP feature enabled
34144 + </td></tr>
34145 +
34146 + <tr>
34147 + <td>otg_ver</td>
34148 + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
34149 + USB OTG device.
34150 + - 0: OTG 2.0 support disabled (default)
34151 + - 1: OTG 2.0 support enabled
34152 + </td></tr>
34153 +
34154 +*/
34155 --- /dev/null
34156 +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.h
34157 @@ -0,0 +1,86 @@
34158 +/* ==========================================================================
34159 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
34160 + * $Revision: #19 $
34161 + * $Date: 2010/11/15 $
34162 + * $Change: 1627671 $
34163 + *
34164 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
34165 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
34166 + * otherwise expressly agreed to in writing between Synopsys and you.
34167 + *
34168 + * The Software IS NOT an item of Licensed Software or Licensed Product under
34169 + * any End User Software License Agreement or Agreement for Licensed Product
34170 + * with Synopsys or any supplement thereto. You are permitted to use and
34171 + * redistribute this Software in source and binary forms, with or without
34172 + * modification, provided that redistributions of source code must retain this
34173 + * notice. You may not view, use, disclose, copy or distribute this file or
34174 + * any information contained herein except pursuant to this license grant from
34175 + * Synopsys. If you do not agree with this notice, including the disclaimer
34176 + * below, then you are not authorized to use the Software.
34177 + *
34178 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
34179 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34180 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34181 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
34182 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34183 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34184 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34185 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34186 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34187 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
34188 + * DAMAGE.
34189 + * ========================================================================== */
34190 +
34191 +#ifndef __DWC_OTG_DRIVER_H__
34192 +#define __DWC_OTG_DRIVER_H__
34193 +
34194 +/** @file
34195 + * This file contains the interface to the Linux driver.
34196 + */
34197 +#include "dwc_otg_os_dep.h"
34198 +#include "dwc_otg_core_if.h"
34199 +
34200 +/* Type declarations */
34201 +struct dwc_otg_pcd;
34202 +struct dwc_otg_hcd;
34203 +
34204 +/**
34205 + * This structure is a wrapper that encapsulates the driver components used to
34206 + * manage a single DWC_otg controller.
34207 + */
34208 +typedef struct dwc_otg_device {
34209 + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
34210 + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
34211 + * require this. */
34212 + struct os_dependent os_dep;
34213 +
34214 + /** Pointer to the core interface structure. */
34215 + dwc_otg_core_if_t *core_if;
34216 +
34217 + /** Pointer to the PCD structure. */
34218 + struct dwc_otg_pcd *pcd;
34219 +
34220 + /** Pointer to the HCD structure. */
34221 + struct dwc_otg_hcd *hcd;
34222 +
34223 + /** Flag to indicate whether the common IRQ handler is installed. */
34224 + uint8_t common_irq_installed;
34225 +
34226 +} dwc_otg_device_t;
34227 +
34228 +/*We must clear S3C24XX_EINTPEND external interrupt register
34229 + * because after clearing in this register trigerred IRQ from
34230 + * H/W core in kernel interrupt can be occured again before OTG
34231 + * handlers clear all IRQ sources of Core registers because of
34232 + * timing latencies and Low Level IRQ Type.
34233 + */
34234 +#ifdef CONFIG_MACH_IPMATE
34235 +#define S3C2410X_CLEAR_EINTPEND() \
34236 +do { \
34237 + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
34238 +} while (0)
34239 +#else
34240 +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
34241 +#endif
34242 +
34243 +#endif
34244 --- /dev/null
34245 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
34246 @@ -0,0 +1,1433 @@
34247 +/*
34248 + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
34249 + *
34250 + * Copyright (c) 2013 Raspberry Pi Foundation
34251 + *
34252 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
34253 + * All rights reserved.
34254 + *
34255 + * Redistribution and use in source and binary forms, with or without
34256 + * modification, are permitted provided that the following conditions are met:
34257 + * * Redistributions of source code must retain the above copyright
34258 + * notice, this list of conditions and the following disclaimer.
34259 + * * Redistributions in binary form must reproduce the above copyright
34260 + * notice, this list of conditions and the following disclaimer in the
34261 + * documentation and/or other materials provided with the distribution.
34262 + * * Neither the name of Raspberry Pi nor the
34263 + * names of its contributors may be used to endorse or promote products
34264 + * derived from this software without specific prior written permission.
34265 + *
34266 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34267 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34268 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34269 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
34270 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34271 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34272 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34273 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34274 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34275 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34276 + *
34277 + * This FIQ implements functionality that performs split transactions on
34278 + * the dwc_otg hardware without any outside intervention. A split transaction
34279 + * is "queued" by nominating a specific host channel to perform the entirety
34280 + * of a split transaction. This FIQ will then perform the microframe-precise
34281 + * scheduling required in each phase of the transaction until completion.
34282 + *
34283 + * The FIQ functionality is glued into the Synopsys driver via the entry point
34284 + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
34285 + * for a FSM-enabled channel.
34286 + *
34287 + * NB: Large parts of this implementation have architecture-specific code.
34288 + * For porting this functionality to other ARM machines, the minimum is required:
34289 + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
34290 + * to the FIQ
34291 + * - A method of forcing a software generated interrupt from FIQ mode that then
34292 + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
34293 + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
34294 + * processor core - there is no locking between the FIQ and IRQ (aside from
34295 + * local_fiq_disable)
34296 + *
34297 + */
34298 +
34299 +#include "dwc_otg_fiq_fsm.h"
34300 +
34301 +
34302 +char buffer[1000*16];
34303 +int wptr;
34304 +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
34305 +{
34306 + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
34307 + va_list args;
34308 + char text[17];
34309 + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
34310 +
34311 + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
34312 + {
34313 + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
34314 + va_start(args, fmt);
34315 + vsnprintf(text+8, 9, fmt, args);
34316 + va_end(args);
34317 +
34318 + memcpy(buffer + wptr, text, 16);
34319 + wptr = (wptr + 16) % sizeof(buffer);
34320 + }
34321 +}
34322 +
34323 +
34324 +#ifdef CONFIG_ARM64
34325 +
34326 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
34327 +{
34328 + spin_lock((spinlock_t *)lock);
34329 +}
34330 +
34331 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
34332 +{
34333 + spin_unlock((spinlock_t *)lock);
34334 +}
34335 +
34336 +#else
34337 +
34338 +/**
34339 + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
34340 + * Must be called with local interrupts and FIQ disabled.
34341 + */
34342 +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
34343 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
34344 +{
34345 + unsigned long tmp;
34346 + uint32_t newval;
34347 + fiq_lock_t lockval;
34348 + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
34349 + * will be sufficient. If we are on a different CPU, then the lock protects us. */
34350 + prefetchw(&lock->slock);
34351 + asm volatile (
34352 + "1: ldrex %0, [%3]\n"
34353 + " add %1, %0, %4\n"
34354 + " strex %2, %1, [%3]\n"
34355 + " teq %2, #0\n"
34356 + " bne 1b"
34357 + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
34358 + : "r" (&lock->slock), "I" (1 << 16)
34359 + : "cc");
34360 +
34361 + while (lockval.tickets.next != lockval.tickets.owner) {
34362 + wfe();
34363 + lockval.tickets.owner = READ_ONCE(lock->tickets.owner);
34364 + }
34365 + smp_mb();
34366 +}
34367 +#else
34368 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock) { }
34369 +#endif
34370 +
34371 +/**
34372 + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
34373 + */
34374 +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
34375 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
34376 +{
34377 + smp_mb();
34378 + lock->tickets.owner++;
34379 + dsb_sev();
34380 +}
34381 +#else
34382 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock) { }
34383 +#endif
34384 +
34385 +#endif
34386 +
34387 +/**
34388 + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
34389 + * @channel: channel to re-enable
34390 + */
34391 +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
34392 +{
34393 + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
34394 +
34395 + hcchar.b.chen = 0;
34396 + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
34397 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
34398 + /* Hardware bug workaround: update the ssplit index */
34399 + if (st->channel[n].hcsplt_copy.b.spltena)
34400 + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
34401 +
34402 + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
34403 + }
34404 +
34405 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
34406 + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
34407 + hcchar.b.chen = 1;
34408 +
34409 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
34410 + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
34411 +}
34412 +
34413 +/**
34414 + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
34415 + * @st: Pointer to the channel's state
34416 + * @n : channel number
34417 + *
34418 + * Change host channel registers to perform a complete-split transaction. Being mindful of the
34419 + * endpoint direction, set control regs up correctly.
34420 + */
34421 +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
34422 +{
34423 + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
34424 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34425 +
34426 + hcsplt.b.compsplt = 1;
34427 + if (st->channel[n].hcchar_copy.b.epdir == 1) {
34428 + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
34429 + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
34430 + } else {
34431 + // If OUT, the CSPLIT result contains handshake only.
34432 + hctsiz.b.xfersize = 0;
34433 + }
34434 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
34435 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34436 + mb();
34437 +}
34438 +
34439 +/**
34440 + * fiq_fsm_restart_np_pending() - Restart a single non-periodic contended transfer
34441 + * @st: Pointer to the channel's state
34442 + * @num_channels: Total number of host channels
34443 + * @orig_channel: Channel index of completed transfer
34444 + *
34445 + * In the case where an IN and OUT transfer are simultaneously scheduled to the
34446 + * same device/EP, inadequate hub implementations will misbehave. Once the first
34447 + * transfer is complete, a pending non-periodic split can then be issued.
34448 + */
34449 +static void notrace fiq_fsm_restart_np_pending(struct fiq_state *st, int num_channels, int orig_channel)
34450 +{
34451 + int i;
34452 + int dev_addr = st->channel[orig_channel].hcchar_copy.b.devaddr;
34453 + int ep_num = st->channel[orig_channel].hcchar_copy.b.epnum;
34454 + for (i = 0; i < num_channels; i++) {
34455 + if (st->channel[i].fsm == FIQ_NP_SSPLIT_PENDING &&
34456 + st->channel[i].hcchar_copy.b.devaddr == dev_addr &&
34457 + st->channel[i].hcchar_copy.b.epnum == ep_num) {
34458 + st->channel[i].fsm = FIQ_NP_SSPLIT_STARTED;
34459 + fiq_fsm_restart_channel(st, i, 0);
34460 + break;
34461 + }
34462 + }
34463 +}
34464 +
34465 +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
34466 +{
34467 + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
34468 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34469 +
34470 + if (st->channel[n].hcchar_copy.b.epdir == 0) {
34471 + return st->channel[n].hctsiz_copy.b.xfersize;
34472 + } else {
34473 + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
34474 + }
34475 +
34476 +}
34477 +
34478 +
34479 +/**
34480 + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
34481 + *
34482 + * Of use only for IN periodic transfers.
34483 + */
34484 +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
34485 +{
34486 + hcdma_data_t hcdma;
34487 + int i = st->channel[n].dma_info.index;
34488 + int len;
34489 + struct fiq_dma_blob *blob =
34490 + (struct fiq_dma_blob *)(uintptr_t)st->dma_base;
34491 +
34492 + len = fiq_get_xfer_len(st, n);
34493 + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
34494 + st->channel[n].dma_info.slot_len[i] = len;
34495 + i++;
34496 + if (i > 6)
34497 + BUG();
34498 +
34499 + hcdma.d32 = (u32)(uintptr_t)&blob->channel[n].index[i].buf[0];
34500 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
34501 + st->channel[n].dma_info.index = i;
34502 + return 0;
34503 +}
34504 +
34505 +/**
34506 + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
34507 + */
34508 +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
34509 +{
34510 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34511 + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
34512 + hctsiz.b.pktcnt = 1;
34513 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34514 +}
34515 +
34516 +/**
34517 + * fiq_fsm_reload_hcdma() - for OUT transactions, rewind DMA pointer
34518 + */
34519 +static void notrace fiq_fsm_reload_hcdma(struct fiq_state *st, int n)
34520 +{
34521 + hcdma_data_t hcdma = st->channel[n].hcdma_copy;
34522 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
34523 +}
34524 +
34525 +/**
34526 + * fiq_iso_out_advance() - update DMA address and split position bits
34527 + * for isochronous OUT transactions.
34528 + *
34529 + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
34530 + * Split-BEGIN states are not handled - this is done when the transaction was queued.
34531 + *
34532 + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
34533 + */
34534 +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
34535 +{
34536 + hcsplt_data_t hcsplt;
34537 + hctsiz_data_t hctsiz;
34538 + hcdma_data_t hcdma;
34539 + struct fiq_dma_blob *blob =
34540 + (struct fiq_dma_blob *)(uintptr_t)st->dma_base;
34541 + int last = 0;
34542 + int i = st->channel[n].dma_info.index;
34543 +
34544 + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
34545 + i++;
34546 + if (i == 4)
34547 + last = 1;
34548 + if (st->channel[n].dma_info.slot_len[i+1] == 255)
34549 + last = 1;
34550 +
34551 + /* New DMA address - address of bounce buffer referred to in index */
34552 + hcdma.d32 = (u32)(uintptr_t)blob->channel[n].index[i].buf;
34553 + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA);
34554 + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
34555 + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
34556 + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
34557 + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
34558 + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
34559 + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
34560 + /* Set up new packet length */
34561 + hctsiz.b.pktcnt = 1;
34562 + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
34563 + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
34564 +
34565 + st->channel[n].dma_info.index++;
34566 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
34567 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34568 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
34569 + return last;
34570 +}
34571 +
34572 +/**
34573 + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
34574 + *
34575 + * Despite the limitations of the DWC core, we can force a microframe pipeline of
34576 + * isochronous OUT start-split transactions while waiting for a corresponding other-type
34577 + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
34578 + * is very unlikely that filling the start-split FIFO will cause data loss.
34579 + * This allows much better interleaving of transactions in an order-independent way-
34580 + * there is no requirement to prioritise isochronous, just a state-space search has
34581 + * to be performed on each periodic start-split complete interrupt.
34582 + */
34583 +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
34584 +{
34585 + int hub_addr = st->channel[n].hub_addr;
34586 + int port_addr = st->channel[n].port_addr;
34587 + int i, poked = 0;
34588 + for (i = 0; i < num_channels; i++) {
34589 + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
34590 + continue;
34591 + if (st->channel[i].hub_addr == hub_addr &&
34592 + st->channel[i].port_addr == port_addr) {
34593 + switch (st->channel[i].fsm) {
34594 + case FIQ_PER_ISO_OUT_PENDING:
34595 + if (st->channel[i].nrpackets == 1) {
34596 + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
34597 + } else {
34598 + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
34599 + }
34600 + fiq_fsm_restart_channel(st, i, 0);
34601 + poked = 1;
34602 + break;
34603 +
34604 + default:
34605 + break;
34606 + }
34607 + }
34608 + if (poked)
34609 + break;
34610 + }
34611 + return poked;
34612 +}
34613 +
34614 +/**
34615 + * fiq_fsm_tt_in_use() - search for host channels using this TT
34616 + * @n: Channel to use as reference
34617 + *
34618 + */
34619 +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
34620 +{
34621 + int hub_addr = st->channel[n].hub_addr;
34622 + int port_addr = st->channel[n].port_addr;
34623 + int i, in_use = 0;
34624 + for (i = 0; i < num_channels; i++) {
34625 + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
34626 + continue;
34627 + switch (st->channel[i].fsm) {
34628 + /* TT is reserved for channels that are in the middle of a periodic
34629 + * split transaction.
34630 + */
34631 + case FIQ_PER_SSPLIT_STARTED:
34632 + case FIQ_PER_CSPLIT_WAIT:
34633 + case FIQ_PER_CSPLIT_NYET1:
34634 + //case FIQ_PER_CSPLIT_POLL:
34635 + case FIQ_PER_ISO_OUT_ACTIVE:
34636 + case FIQ_PER_ISO_OUT_LAST:
34637 + if (st->channel[i].hub_addr == hub_addr &&
34638 + st->channel[i].port_addr == port_addr) {
34639 + in_use = 1;
34640 + }
34641 + break;
34642 + default:
34643 + break;
34644 + }
34645 + if (in_use)
34646 + break;
34647 + }
34648 + return in_use;
34649 +}
34650 +
34651 +/**
34652 + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
34653 + * to be issued for this IN transaction.
34654 + *
34655 + * We cannot tell the inbound PID of a data packet due to hardware limitations.
34656 + * we need to make an educated guess as to whether we need to queue another CSPLIT
34657 + * or not. A no-brainer is when we have received enough data to fill the endpoint
34658 + * size, but for endpoints that give variable-length data then we have to resort
34659 + * to heuristics.
34660 + *
34661 + * We also return whether this is the last CSPLIT to be queued, again based on
34662 + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
34663 + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
34664 + */
34665 +
34666 +/*
34667 + * We need some way of guaranteeing if a returned periodic packet of size X
34668 + * has a DATA0 PID.
34669 + * The heuristic value of 144 bytes assumes that the received data has maximal
34670 + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
34671 + * permissible limit. If the transfer length results in a final packet size
34672 + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
34673 + * Also used to ensure that an endpoint will nominally only return a single
34674 + * complete-split worth of data.
34675 + */
34676 +#define DATA0_PID_HEURISTIC 144
34677 +
34678 +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
34679 +{
34680 +
34681 + int i;
34682 + int total_len = 0;
34683 + int more_needed = 1;
34684 + struct fiq_channel_state *st = &state->channel[n];
34685 +
34686 + for (i = 0; i < st->dma_info.index; i++) {
34687 + total_len += st->dma_info.slot_len[i];
34688 + }
34689 +
34690 + *probably_last = 0;
34691 +
34692 + if (st->hcchar_copy.b.eptype == 0x3) {
34693 + /*
34694 + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
34695 + * then this is definitely the last CSPLIT.
34696 + */
34697 + *probably_last = 1;
34698 + } else {
34699 + /* Isoc IN. This is a bit risky if we are the first transaction:
34700 + * we may have been held off slightly. */
34701 + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
34702 + more_needed = 0;
34703 + }
34704 + /* If in the next uframe we will receive enough data to fill the endpoint,
34705 + * then only issue 1 more csplit.
34706 + */
34707 + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
34708 + *probably_last = 1;
34709 + }
34710 +
34711 + if (total_len >= st->hctsiz_copy.b.xfersize ||
34712 + i == 6 || total_len == 0)
34713 + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
34714 + * a single endpoint. Accepting more would completely break our scheduling mechanism though
34715 + * - in these extreme cases we will pass through a truncated packet.
34716 + */
34717 + more_needed = 0;
34718 +
34719 + return more_needed;
34720 +}
34721 +
34722 +/**
34723 + * fiq_fsm_too_late() - Test transaction for lateness
34724 + *
34725 + * If a SSPLIT for a large IN transaction is issued too late in a frame,
34726 + * the hub will disable the port to the device and respond with ERR handshakes.
34727 + * The hub status endpoint will not reflect this change.
34728 + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
34729 + */
34730 +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
34731 +{
34732 + int uframe;
34733 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
34734 + uframe = hfnum.b.frnum & 0x7;
34735 + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
34736 + return 1;
34737 + } else {
34738 + return 0;
34739 + }
34740 +}
34741 +
34742 +
34743 +/**
34744 + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
34745 + *
34746 + * Search pending transactions in the start-split pending state and queue them.
34747 + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
34748 + * Note: we specifically don't do isochronous OUT transactions first because better
34749 + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
34750 + */
34751 +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
34752 +{
34753 + int n;
34754 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
34755 + if ((hfnum.b.frnum & 0x7) == 5)
34756 + return;
34757 + for (n = 0; n < num_channels; n++) {
34758 + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
34759 + /* Check to see if any other transactions are using this TT */
34760 + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
34761 + if (!fiq_fsm_too_late(st, n)) {
34762 + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
34763 + fiq_print(FIQDBG_INT, st, "NEXTPER ");
34764 + fiq_fsm_restart_channel(st, n, 0);
34765 + } else {
34766 + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
34767 + }
34768 + break;
34769 + }
34770 + }
34771 + }
34772 + for (n = 0; n < num_channels; n++) {
34773 + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
34774 + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
34775 + fiq_print(FIQDBG_INT, st, "NEXTISO ");
34776 + if (st->channel[n].nrpackets == 1)
34777 + st->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
34778 + else
34779 + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
34780 + fiq_fsm_restart_channel(st, n, 0);
34781 + break;
34782 + }
34783 + }
34784 + }
34785 +}
34786 +
34787 +/**
34788 + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
34789 + * @state: Pointer to fiq_state
34790 + * @n: Channel transaction is active on
34791 + * @hcint: Copy of host channel interrupt register
34792 + *
34793 + * Returns 0 if there are no more transactions for this HC to do, 1
34794 + * otherwise.
34795 + */
34796 +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
34797 +{
34798 + struct fiq_channel_state *st = &state->channel[n];
34799 + int xfer_len = 0, nrpackets = 0;
34800 + hcdma_data_t hcdma;
34801 + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
34802 +
34803 + xfer_len = fiq_get_xfer_len(state, n);
34804 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
34805 +
34806 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
34807 +
34808 + st->hs_isoc_info.index++;
34809 + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
34810 + return 0;
34811 + }
34812 +
34813 + /* grab the next DMA address offset from the array */
34814 + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
34815 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
34816 +
34817 + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
34818 + * the core needs to be told to send the correct number. Caution: for IN transfers,
34819 + * this is always set to the maximum size of the endpoint. */
34820 + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
34821 + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
34822 + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
34823 + if (nrpackets == 0)
34824 + nrpackets = 1;
34825 + st->hcchar_copy.b.multicnt = nrpackets;
34826 + st->hctsiz_copy.b.pktcnt = nrpackets;
34827 +
34828 + /* Initial PID also needs to be set */
34829 + if (st->hcchar_copy.b.epdir == 0) {
34830 + st->hctsiz_copy.b.xfersize = xfer_len;
34831 + switch (st->hcchar_copy.b.multicnt) {
34832 + case 1:
34833 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
34834 + break;
34835 + case 2:
34836 + case 3:
34837 + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
34838 + break;
34839 + }
34840 +
34841 + } else {
34842 + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
34843 + switch (st->hcchar_copy.b.multicnt) {
34844 + case 1:
34845 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
34846 + break;
34847 + case 2:
34848 + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
34849 + break;
34850 + case 3:
34851 + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
34852 + break;
34853 + }
34854 + }
34855 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
34856 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
34857 + /* Channel is enabled on hcint handler exit */
34858 + fiq_print(FIQDBG_INT, state, "HSISOOUT");
34859 + return 1;
34860 +}
34861 +
34862 +
34863 +/**
34864 + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
34865 + * @state: Pointer to the state struct passed from banked FIQ mode registers.
34866 + * @num_channels: set according to the DWC hardware configuration
34867 + *
34868 + * The SOF handler in FSM mode has two functions
34869 + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
34870 + * nothing to do
34871 + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
34872 + * of holdoff.
34873 + *
34874 + * The second part is architecture-specific to mach-bcm2835 -
34875 + * a sane interrupt controller would have a mask register for ARM interrupt sources
34876 + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
34877 + * number (USB) can be enabled. This means that certain parts of the USB specification
34878 + * that require "wait a little while, then issue another packet" cannot be fulfilled with
34879 + * the timing granularity required to achieve optimal throughout. The workaround is to use
34880 + * the SOF "timer" (125uS) to perform this task.
34881 + */
34882 +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
34883 +{
34884 + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
34885 + int n;
34886 + int kick_irq = 0;
34887 +
34888 + if ((hfnum.b.frnum & 0x7) == 1) {
34889 + /* We cannot issue csplits for transactions in the last frame past (n+1).1
34890 + * Check to see if there are any transactions that are stale.
34891 + * Boot them out.
34892 + */
34893 + for (n = 0; n < num_channels; n++) {
34894 + switch (state->channel[n].fsm) {
34895 + case FIQ_PER_CSPLIT_WAIT:
34896 + case FIQ_PER_CSPLIT_NYET1:
34897 + case FIQ_PER_CSPLIT_POLL:
34898 + case FIQ_PER_CSPLIT_LAST:
34899 + /* Check if we are no longer in the same full-speed frame. */
34900 + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
34901 + (hfnum.b.frnum & ~0x7))
34902 + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
34903 + break;
34904 + default:
34905 + break;
34906 + }
34907 + }
34908 + }
34909 +
34910 + for (n = 0; n < num_channels; n++) {
34911 + switch (state->channel[n].fsm) {
34912 +
34913 + case FIQ_NP_SSPLIT_RETRY:
34914 + case FIQ_NP_IN_CSPLIT_RETRY:
34915 + case FIQ_NP_OUT_CSPLIT_RETRY:
34916 + fiq_fsm_restart_channel(state, n, 0);
34917 + break;
34918 +
34919 + case FIQ_HS_ISOC_SLEEPING:
34920 + /* Is it time to wake this channel yet? */
34921 + if (--state->channel[n].uframe_sleeps == 0) {
34922 + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
34923 + fiq_fsm_restart_channel(state, n, 0);
34924 + }
34925 + break;
34926 +
34927 + case FIQ_PER_SSPLIT_QUEUED:
34928 + if ((hfnum.b.frnum & 0x7) == 5)
34929 + break;
34930 + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
34931 + if (!fiq_fsm_too_late(state, n)) {
34932 + fiq_print(FIQDBG_INT, state, "SOF GO %01d", n);
34933 + fiq_fsm_restart_channel(state, n, 0);
34934 + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
34935 + } else {
34936 + /* Transaction cannot be started without risking a device babble error */
34937 + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
34938 + state->haintmsk_saved.b2.chint &= ~(1 << n);
34939 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
34940 + kick_irq |= 1;
34941 + }
34942 + }
34943 + break;
34944 +
34945 + case FIQ_PER_ISO_OUT_PENDING:
34946 + /* Ordinarily, this should be poked after the SSPLIT
34947 + * complete interrupt for a competing transfer on the same
34948 + * TT. Doesn't happen for aborted transactions though.
34949 + */
34950 + if ((hfnum.b.frnum & 0x7) >= 5)
34951 + break;
34952 + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
34953 + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
34954 + * that caused this.
34955 + */
34956 + fiq_fsm_restart_channel(state, n, 0);
34957 + fiq_print(FIQDBG_INT, state, "SOF ISOC");
34958 + if (state->channel[n].nrpackets == 1) {
34959 + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
34960 + } else {
34961 + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
34962 + }
34963 + }
34964 + break;
34965 +
34966 + case FIQ_PER_CSPLIT_WAIT:
34967 + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
34968 + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
34969 + * will utterly bugger this up though.
34970 + */
34971 + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
34972 + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
34973 + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
34974 + fiq_fsm_restart_channel(state, n, 0);
34975 + fiq_fsm_start_next_periodic(state, num_channels);
34976 +
34977 + }
34978 + break;
34979 +
34980 + case FIQ_PER_SPLIT_TIMEOUT:
34981 + case FIQ_DEQUEUE_ISSUED:
34982 + /* Ugly: we have to force a HCD interrupt.
34983 + * Poke the mask for the channel in question.
34984 + * We will take a fake SOF because of this, but
34985 + * that's OK.
34986 + */
34987 + state->haintmsk_saved.b2.chint &= ~(1 << n);
34988 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
34989 + kick_irq |= 1;
34990 + break;
34991 +
34992 + default:
34993 + break;
34994 + }
34995 + }
34996 +
34997 + if (state->kick_np_queues ||
34998 + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
34999 + kick_irq |= 1;
35000 +
35001 + return !kick_irq;
35002 +}
35003 +
35004 +
35005 +/**
35006 + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
35007 + * @state: Pointer to the FIQ state struct
35008 + * @num_channels: Number of channels as per hardware config
35009 + * @n: channel for which HAINT(i) was raised
35010 + *
35011 + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
35012 + */
35013 +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
35014 +{
35015 + hcint_data_t hcint;
35016 + hcintmsk_data_t hcintmsk;
35017 + hcint_data_t hcint_probe;
35018 + hcchar_data_t hcchar;
35019 + int handled = 0;
35020 + int restart = 0;
35021 + int last_csplit = 0;
35022 + int start_next_periodic = 0;
35023 + struct fiq_channel_state *st = &state->channel[n];
35024 + hfnum_data_t hfnum;
35025 +
35026 + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
35027 + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
35028 + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
35029 +
35030 + if (st->fsm != FIQ_PASSTHROUGH) {
35031 + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
35032 + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
35033 + }
35034 +
35035 + switch (st->fsm) {
35036 +
35037 + case FIQ_PASSTHROUGH:
35038 + case FIQ_DEQUEUE_ISSUED:
35039 + /* doesn't belong to us, kick it upstairs */
35040 + break;
35041 +
35042 + case FIQ_PASSTHROUGH_ERRORSTATE:
35043 + /* We are here to emulate the error recovery mechanism of the dwc HCD.
35044 + * Several interrupts are unmasked if a previous transaction failed - it's
35045 + * death for the FIQ to attempt to handle them as the channel isn't halted.
35046 + * Emulate what the HCD does in this situation: mask and continue.
35047 + * The FSM has no other state setup so this has to be handled out-of-band.
35048 + */
35049 + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
35050 + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
35051 + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
35052 + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
35053 + * interrupt, after the device has disappeared.
35054 + */
35055 + if (!hcint.b.xacterr)
35056 + st->nr_errors = 0;
35057 + hcintmsk.b.nak = 0;
35058 + hcintmsk.b.ack = 0;
35059 + hcintmsk.b.datatglerr = 0;
35060 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
35061 + return 1;
35062 + }
35063 + if (hcint_probe.b.chhltd) {
35064 + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
35065 + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
35066 + return 0;
35067 + }
35068 + break;
35069 +
35070 + /* Non-periodic state groups */
35071 + case FIQ_NP_SSPLIT_STARTED:
35072 + case FIQ_NP_SSPLIT_RETRY:
35073 + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
35074 + if (hcint.b.ack) {
35075 + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
35076 + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
35077 + */
35078 + if(st->hcchar_copy.b.epdir == 1)
35079 + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
35080 + else
35081 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35082 + st->nr_errors = 0;
35083 + handled = 1;
35084 + fiq_fsm_setup_csplit(state, n);
35085 + } else if (hcint.b.nak) {
35086 + // No buffer space in TT. Retry on a uframe boundary.
35087 + fiq_fsm_reload_hcdma(state, n);
35088 + st->fsm = FIQ_NP_SSPLIT_RETRY;
35089 + handled = 1;
35090 + } else if (hcint.b.xacterr) {
35091 + // The only other one we care about is xacterr. This implies HS bus error - retry.
35092 + st->nr_errors++;
35093 + if(st->hcchar_copy.b.epdir == 0)
35094 + fiq_fsm_reload_hcdma(state, n);
35095 + st->fsm = FIQ_NP_SSPLIT_RETRY;
35096 + if (st->nr_errors >= 3) {
35097 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35098 + } else {
35099 + handled = 1;
35100 + restart = 1;
35101 + }
35102 + } else {
35103 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35104 + handled = 0;
35105 + restart = 0;
35106 + }
35107 + break;
35108 +
35109 + case FIQ_NP_IN_CSPLIT_RETRY:
35110 + /* Received a CSPLIT done interrupt.
35111 + * Expected Data/NAK/STALL/NYET for IN.
35112 + */
35113 + if (hcint.b.xfercomp) {
35114 + /* For IN, data is present. */
35115 + st->fsm = FIQ_NP_SPLIT_DONE;
35116 + } else if (hcint.b.nak) {
35117 + /* no endpoint data. Punt it upstairs */
35118 + st->fsm = FIQ_NP_SPLIT_DONE;
35119 + } else if (hcint.b.nyet) {
35120 + /* CSPLIT NYET - retry on a uframe boundary. */
35121 + handled = 1;
35122 + st->nr_errors = 0;
35123 + } else if (hcint.b.datatglerr) {
35124 + /* data toggle errors do not set the xfercomp bit. */
35125 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35126 + } else if (hcint.b.xacterr) {
35127 + /* HS error. Retry immediate */
35128 + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
35129 + st->nr_errors++;
35130 + if (st->nr_errors >= 3) {
35131 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35132 + } else {
35133 + handled = 1;
35134 + restart = 1;
35135 + }
35136 + } else if (hcint.b.stall || hcint.b.bblerr) {
35137 + /* A STALL implies either a LS bus error or a genuine STALL. */
35138 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35139 + } else {
35140 + /* Hardware bug. It's possible in some cases to
35141 + * get a channel halt with nothing else set when
35142 + * the response was a NYET. Treat as local 3-strikes retry.
35143 + */
35144 + hcint_data_t hcint_test = hcint;
35145 + hcint_test.b.chhltd = 0;
35146 + if (!hcint_test.d32) {
35147 + st->nr_errors++;
35148 + if (st->nr_errors >= 3) {
35149 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35150 + } else {
35151 + handled = 1;
35152 + }
35153 + } else {
35154 + /* Bail out if something unexpected happened */
35155 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35156 + }
35157 + }
35158 + if (st->fsm != FIQ_NP_IN_CSPLIT_RETRY) {
35159 + fiq_fsm_restart_np_pending(state, num_channels, n);
35160 + }
35161 + break;
35162 +
35163 + case FIQ_NP_OUT_CSPLIT_RETRY:
35164 + /* Received a CSPLIT done interrupt.
35165 + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
35166 + if (hcint.b.xfercomp) {
35167 + st->fsm = FIQ_NP_SPLIT_DONE;
35168 + } else if (hcint.b.nak) {
35169 + // The HCD will implement the holdoff on frame boundaries.
35170 + st->fsm = FIQ_NP_SPLIT_DONE;
35171 + } else if (hcint.b.nyet) {
35172 + // Hub still processing.
35173 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35174 + handled = 1;
35175 + st->nr_errors = 0;
35176 + //restart = 1;
35177 + } else if (hcint.b.xacterr) {
35178 + /* HS error. retry immediate */
35179 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35180 + st->nr_errors++;
35181 + if (st->nr_errors >= 3) {
35182 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35183 + } else {
35184 + handled = 1;
35185 + restart = 1;
35186 + }
35187 + } else if (hcint.b.stall) {
35188 + /* LS bus error or genuine stall */
35189 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35190 + } else {
35191 + /*
35192 + * Hardware bug. It's possible in some cases to get a
35193 + * channel halt with nothing else set when the response was a NYET.
35194 + * Treat as local 3-strikes retry.
35195 + */
35196 + hcint_data_t hcint_test = hcint;
35197 + hcint_test.b.chhltd = 0;
35198 + if (!hcint_test.d32) {
35199 + st->nr_errors++;
35200 + if (st->nr_errors >= 3) {
35201 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35202 + } else {
35203 + handled = 1;
35204 + }
35205 + } else {
35206 + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
35207 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35208 + }
35209 + }
35210 + if (st->fsm != FIQ_NP_OUT_CSPLIT_RETRY) {
35211 + fiq_fsm_restart_np_pending(state, num_channels, n);
35212 + }
35213 + break;
35214 +
35215 + /* Periodic split states (except isoc out) */
35216 + case FIQ_PER_SSPLIT_STARTED:
35217 + /* Expect an ACK or failure for SSPLIT */
35218 + if (hcint.b.ack) {
35219 + /*
35220 + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
35221 + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
35222 + * point for microframe n-3, the packet will not appear on the bus until microframe n.
35223 + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
35224 + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
35225 + * coincident with SOF for n+1.
35226 + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
35227 + * These appear to be caused by timing/clock crossing bugs within the core itself.
35228 + * State machine workaround.
35229 + */
35230 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35231 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35232 + fiq_fsm_setup_csplit(state, n);
35233 + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
35234 + * time. If not, then we're in the next SOF.
35235 + */
35236 + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
35237 + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
35238 + st->expected_uframe = hfnum.b.frnum;
35239 + st->fsm = FIQ_PER_CSPLIT_WAIT;
35240 + } else {
35241 + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
35242 + /* For isochronous IN endpoints,
35243 + * we need to hold off if we are expecting a lot of data */
35244 + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
35245 + start_next_periodic = 1;
35246 + }
35247 + /* Danger will robinson: we are in a broken state. If our first interrupt after
35248 + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
35249 + * lag. Unmask the NYET interrupt.
35250 + */
35251 + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
35252 + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
35253 + restart = 1;
35254 + }
35255 + handled = 1;
35256 + } else if (hcint.b.xacterr) {
35257 + /* 3-strikes retry is enabled, we have hit our max nr_errors */
35258 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35259 + start_next_periodic = 1;
35260 + } else {
35261 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35262 + start_next_periodic = 1;
35263 + }
35264 + /* We can now queue the next isochronous OUT transaction, if one is pending. */
35265 + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
35266 + fiq_print(FIQDBG_INT, state, "NEXTISO ");
35267 + }
35268 + break;
35269 +
35270 + case FIQ_PER_CSPLIT_NYET1:
35271 + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
35272 + * we are too late and the TT has dropped its CSPLIT fifo.
35273 + */
35274 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35275 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35276 + start_next_periodic = 1;
35277 + if (hcint.b.nak) {
35278 + st->fsm = FIQ_PER_SPLIT_DONE;
35279 + } else if (hcint.b.xfercomp) {
35280 + fiq_increment_dma_buf(state, num_channels, n);
35281 + st->fsm = FIQ_PER_CSPLIT_POLL;
35282 + st->nr_errors = 0;
35283 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35284 + handled = 1;
35285 + restart = 1;
35286 + if (!last_csplit)
35287 + start_next_periodic = 0;
35288 + } else {
35289 + st->fsm = FIQ_PER_SPLIT_DONE;
35290 + }
35291 + } else if (hcint.b.nyet) {
35292 + /* Doh. Data lost. */
35293 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35294 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35295 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35296 + } else {
35297 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35298 + }
35299 + break;
35300 +
35301 + case FIQ_PER_CSPLIT_BROKEN_NYET1:
35302 + /*
35303 + * we got here because our host channel is in the delayed-interrupt
35304 + * state and we cannot take a NYET interrupt any later than when it
35305 + * occurred. Disable then re-enable the channel if this happens to force
35306 + * CSPLITs to occur at the right time.
35307 + */
35308 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35309 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35310 + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
35311 + if (hcint.b.nak) {
35312 + st->fsm = FIQ_PER_SPLIT_DONE;
35313 + start_next_periodic = 1;
35314 + } else if (hcint.b.xfercomp) {
35315 + fiq_increment_dma_buf(state, num_channels, n);
35316 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35317 + st->fsm = FIQ_PER_CSPLIT_POLL;
35318 + handled = 1;
35319 + restart = 1;
35320 + start_next_periodic = 1;
35321 + /* Reload HCTSIZ for the next transfer */
35322 + fiq_fsm_reload_hctsiz(state, n);
35323 + if (!last_csplit)
35324 + start_next_periodic = 0;
35325 + } else {
35326 + st->fsm = FIQ_PER_SPLIT_DONE;
35327 + }
35328 + } else if (hcint.b.nyet) {
35329 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35330 + start_next_periodic = 1;
35331 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35332 + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
35333 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35334 + } else {
35335 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35336 + }
35337 + break;
35338 +
35339 + case FIQ_PER_CSPLIT_POLL:
35340 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35341 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35342 + start_next_periodic = 1;
35343 + if (hcint.b.nak) {
35344 + st->fsm = FIQ_PER_SPLIT_DONE;
35345 + } else if (hcint.b.xfercomp) {
35346 + fiq_increment_dma_buf(state, num_channels, n);
35347 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35348 + handled = 1;
35349 + restart = 1;
35350 + /* Reload HCTSIZ for the next transfer */
35351 + fiq_fsm_reload_hctsiz(state, n);
35352 + if (!last_csplit)
35353 + start_next_periodic = 0;
35354 + } else {
35355 + st->fsm = FIQ_PER_SPLIT_DONE;
35356 + }
35357 + } else if (hcint.b.nyet) {
35358 + /* Are we a NYET after the first data packet? */
35359 + if (st->nrpackets == 0) {
35360 + st->fsm = FIQ_PER_CSPLIT_NYET1;
35361 + handled = 1;
35362 + restart = 1;
35363 + } else {
35364 + /* We got a NYET when polling CSPLITs. Can happen
35365 + * if our heuristic fails, or if someone disables us
35366 + * for any significant length of time.
35367 + */
35368 + if (st->nr_errors >= 3) {
35369 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35370 + } else {
35371 + st->fsm = FIQ_PER_SPLIT_DONE;
35372 + }
35373 + }
35374 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35375 + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
35376 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35377 + } else {
35378 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35379 + }
35380 + break;
35381 +
35382 + case FIQ_HS_ISOC_TURBO:
35383 + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
35384 + /* more transactions to come */
35385 + handled = 1;
35386 + fiq_print(FIQDBG_INT, state, "HSISO M ");
35387 + /* For strided transfers, put ourselves to sleep */
35388 + if (st->hs_isoc_info.stride > 1) {
35389 + st->uframe_sleeps = st->hs_isoc_info.stride - 1;
35390 + st->fsm = FIQ_HS_ISOC_SLEEPING;
35391 + } else {
35392 + restart = 1;
35393 + }
35394 + } else {
35395 + st->fsm = FIQ_HS_ISOC_DONE;
35396 + fiq_print(FIQDBG_INT, state, "HSISO F ");
35397 + }
35398 + break;
35399 +
35400 + case FIQ_HS_ISOC_ABORTED:
35401 + /* This abort is called by the driver rewriting the state mid-transaction
35402 + * which allows the dequeue mechanism to work more effectively.
35403 + */
35404 + break;
35405 +
35406 + case FIQ_PER_ISO_OUT_ACTIVE:
35407 + if (hcint.b.ack) {
35408 + if(fiq_iso_out_advance(state, num_channels, n)) {
35409 + /* last OUT transfer */
35410 + st->fsm = FIQ_PER_ISO_OUT_LAST;
35411 + /*
35412 + * Assuming the periodic FIFO in the dwc core
35413 + * actually does its job properly, we can queue
35414 + * the next ssplit now and in theory, the wire
35415 + * transactions will be in-order.
35416 + */
35417 + // No it doesn't. It appears to process requests in host channel order.
35418 + //start_next_periodic = 1;
35419 + }
35420 + handled = 1;
35421 + restart = 1;
35422 + } else {
35423 + /*
35424 + * Isochronous transactions carry on regardless. Log the error
35425 + * and continue.
35426 + */
35427 + //explode += 1;
35428 + st->nr_errors++;
35429 + if(fiq_iso_out_advance(state, num_channels, n)) {
35430 + st->fsm = FIQ_PER_ISO_OUT_LAST;
35431 + //start_next_periodic = 1;
35432 + }
35433 + handled = 1;
35434 + restart = 1;
35435 + }
35436 + break;
35437 +
35438 + case FIQ_PER_ISO_OUT_LAST:
35439 + if (hcint.b.ack) {
35440 + /* All done here */
35441 + st->fsm = FIQ_PER_ISO_OUT_DONE;
35442 + } else {
35443 + st->fsm = FIQ_PER_ISO_OUT_DONE;
35444 + st->nr_errors++;
35445 + }
35446 + start_next_periodic = 1;
35447 + break;
35448 +
35449 + case FIQ_PER_SPLIT_TIMEOUT:
35450 + /* SOF kicked us because we overran. */
35451 + start_next_periodic = 1;
35452 + break;
35453 +
35454 + default:
35455 + break;
35456 + }
35457 +
35458 + if (handled) {
35459 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
35460 + } else {
35461 + /* Copy the regs into the state so the IRQ knows what to do */
35462 + st->hcint_copy.d32 = hcint.d32;
35463 + }
35464 +
35465 + if (restart) {
35466 + /* Restart always implies handled. */
35467 + if (restart == 2) {
35468 + /* For complete-split INs, the show must go on.
35469 + * Force a channel restart */
35470 + fiq_fsm_restart_channel(state, n, 1);
35471 + } else {
35472 + fiq_fsm_restart_channel(state, n, 0);
35473 + }
35474 + }
35475 + if (start_next_periodic) {
35476 + fiq_fsm_start_next_periodic(state, num_channels);
35477 + }
35478 + if (st->fsm != FIQ_PASSTHROUGH)
35479 + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
35480 +
35481 + return handled;
35482 +}
35483 +
35484 +
35485 +/**
35486 + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
35487 + * @state: pointer to state struct passed from the banked FIQ mode registers.
35488 + * @num_channels: set according to the DWC hardware configuration
35489 + * @dma: pointer to DMA bounce buffers for split transaction slots
35490 + *
35491 + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
35492 + * inside an EHCI or similar host controller regarding split transactions. The DWC core
35493 + * interrupts each and every time a split transaction packet is received or sent successfully.
35494 + * This results in either an interrupt storm when everything is working "properly", or
35495 + * the interrupt latency of the system in general breaks time-sensitive periodic split
35496 + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
35497 + * solves these problems.
35498 + *
35499 + * Return: void
35500 + */
35501 +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
35502 +{
35503 + gintsts_data_t gintsts, gintsts_handled;
35504 + gintmsk_data_t gintmsk;
35505 + //hfnum_data_t hfnum;
35506 + haint_data_t haint, haint_handled;
35507 + haintmsk_data_t haintmsk;
35508 + int kick_irq = 0;
35509 +
35510 + /* Ensure peripheral reads issued prior to FIQ entry are complete */
35511 + dsb(sy);
35512 +
35513 + gintsts_handled.d32 = 0;
35514 + haint_handled.d32 = 0;
35515 +
35516 + fiq_fsm_spin_lock(&state->lock);
35517 + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
35518 + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
35519 + gintsts.d32 &= gintmsk.d32;
35520 +
35521 + if (gintsts.b.sofintr) {
35522 + /* For FSM mode, SOF is required to keep the state machine advance for
35523 + * certain stages of the periodic pipeline. It's death to mask this
35524 + * interrupt in that case.
35525 + */
35526 +
35527 + if (!fiq_fsm_do_sof(state, num_channels)) {
35528 + /* Kick IRQ once. Queue advancement means that all pending transactions
35529 + * will get serviced when the IRQ finally executes.
35530 + */
35531 + if (state->gintmsk_saved.b.sofintr == 1)
35532 + kick_irq |= 1;
35533 + state->gintmsk_saved.b.sofintr = 0;
35534 + }
35535 + gintsts_handled.b.sofintr = 1;
35536 + }
35537 +
35538 + if (gintsts.b.hcintr) {
35539 + int i;
35540 + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
35541 + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
35542 + haint.d32 &= haintmsk.d32;
35543 + haint_handled.d32 = 0;
35544 + for (i=0; i<num_channels; i++) {
35545 + if (haint.b2.chint & (1 << i)) {
35546 + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
35547 + /* HCINT was not handled in FIQ
35548 + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
35549 + * Mask HAINT(i) but keep top-level hcint unmasked.
35550 + */
35551 + state->haintmsk_saved.b2.chint &= ~(1 << i);
35552 + } else {
35553 + /* do_hcintr cleaned up after itself, but clear haint */
35554 + haint_handled.b2.chint |= (1 << i);
35555 + }
35556 + }
35557 + }
35558 +
35559 + if (haint_handled.b2.chint) {
35560 + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
35561 + }
35562 +
35563 + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
35564 + /*
35565 + * This is necessary to avoid multiple retriggers of the MPHI in the case
35566 + * where interrupts are held off and HCINTs start to pile up.
35567 + * Only wake up the IRQ if a new interrupt came in, was not handled and was
35568 + * masked.
35569 + */
35570 + haintmsk.d32 &= state->haintmsk_saved.d32;
35571 + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
35572 + kick_irq |= 1;
35573 + }
35574 + /* Top-Level interrupt - always handled because it's level-sensitive */
35575 + gintsts_handled.b.hcintr = 1;
35576 + }
35577 +
35578 +
35579 + /* Clear the bits in the saved register that were not handled but were triggered. */
35580 + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
35581 +
35582 + /* FIQ didn't handle something - mask has changed - write new mask */
35583 + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
35584 + gintmsk.d32 &= state->gintmsk_saved.d32;
35585 + gintmsk.b.sofintr = 1;
35586 + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
35587 +// fiq_print(FIQDBG_INT, state, "KICKGINT");
35588 +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
35589 +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
35590 + kick_irq |= 1;
35591 + }
35592 +
35593 + if (gintsts_handled.d32) {
35594 + /* Only applies to edge-sensitive bits in GINTSTS */
35595 + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
35596 + }
35597 +
35598 + /* We got an interrupt, didn't handle it. */
35599 + if (kick_irq) {
35600 + state->mphi_int_count++;
35601 + if (state->mphi_regs.swirq_set) {
35602 + FIQ_WRITE(state->mphi_regs.swirq_set, 1);
35603 + } else {
35604 + FIQ_WRITE(state->mphi_regs.outdda, state->dummy_send_dma);
35605 + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
35606 + }
35607 +
35608 + }
35609 + state->fiq_done++;
35610 + mb();
35611 + fiq_fsm_spin_unlock(&state->lock);
35612 +}
35613 +
35614 +
35615 +/**
35616 + * dwc_otg_fiq_nop() - FIQ "lite"
35617 + * @state: pointer to state struct passed from the banked FIQ mode registers.
35618 + *
35619 + * The "nop" handler does not intervene on any interrupts other than SOF.
35620 + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
35621 + * with non-periodic/periodic queues) needs to be kicked.
35622 + *
35623 + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
35624 + *
35625 + * Return: void
35626 + */
35627 +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
35628 +{
35629 + gintsts_data_t gintsts, gintsts_handled;
35630 + gintmsk_data_t gintmsk;
35631 + hfnum_data_t hfnum;
35632 +
35633 + /* Ensure peripheral reads issued prior to FIQ entry are complete */
35634 + dsb(sy);
35635 +
35636 + fiq_fsm_spin_lock(&state->lock);
35637 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35638 + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
35639 + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
35640 + gintsts.d32 &= gintmsk.d32;
35641 + gintsts_handled.d32 = 0;
35642 +
35643 + if (gintsts.b.sofintr) {
35644 + if (!state->kick_np_queues &&
35645 + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
35646 + /* SOF handled, no work to do, just ACK interrupt */
35647 + gintsts_handled.b.sofintr = 1;
35648 + } else {
35649 + /* Kick IRQ */
35650 + state->gintmsk_saved.b.sofintr = 0;
35651 + }
35652 + }
35653 +
35654 + /* Reset handled interrupts */
35655 + if(gintsts_handled.d32) {
35656 + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
35657 + }
35658 +
35659 + /* Clear the bits in the saved register that were not handled but were triggered. */
35660 + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
35661 +
35662 + /* We got an interrupt, didn't handle it and want to mask it */
35663 + if (~(state->gintmsk_saved.d32)) {
35664 + state->mphi_int_count++;
35665 + gintmsk.d32 &= state->gintmsk_saved.d32;
35666 + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
35667 + if (state->mphi_regs.swirq_set) {
35668 + FIQ_WRITE(state->mphi_regs.swirq_set, 1);
35669 + } else {
35670 + /* Force a clear before another dummy send */
35671 + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
35672 + FIQ_WRITE(state->mphi_regs.outdda, state->dummy_send_dma);
35673 + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
35674 + }
35675 + }
35676 + state->fiq_done++;
35677 + mb();
35678 + fiq_fsm_spin_unlock(&state->lock);
35679 +}
35680 --- /dev/null
35681 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
35682 @@ -0,0 +1,399 @@
35683 +/*
35684 + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
35685 + *
35686 + * Copyright (c) 2013 Raspberry Pi Foundation
35687 + *
35688 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
35689 + * All rights reserved.
35690 + *
35691 + * Redistribution and use in source and binary forms, with or without
35692 + * modification, are permitted provided that the following conditions are met:
35693 + * * Redistributions of source code must retain the above copyright
35694 + * notice, this list of conditions and the following disclaimer.
35695 + * * Redistributions in binary form must reproduce the above copyright
35696 + * notice, this list of conditions and the following disclaimer in the
35697 + * documentation and/or other materials provided with the distribution.
35698 + * * Neither the name of Raspberry Pi nor the
35699 + * names of its contributors may be used to endorse or promote products
35700 + * derived from this software without specific prior written permission.
35701 + *
35702 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
35703 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35704 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35705 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
35706 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35707 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35708 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35709 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35710 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35711 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35712 + *
35713 + * This FIQ implements functionality that performs split transactions on
35714 + * the dwc_otg hardware without any outside intervention. A split transaction
35715 + * is "queued" by nominating a specific host channel to perform the entirety
35716 + * of a split transaction. This FIQ will then perform the microframe-precise
35717 + * scheduling required in each phase of the transaction until completion.
35718 + *
35719 + * The FIQ functionality has been surgically implanted into the Synopsys
35720 + * vendor-provided driver.
35721 + *
35722 + */
35723 +
35724 +#ifndef DWC_OTG_FIQ_FSM_H_
35725 +#define DWC_OTG_FIQ_FSM_H_
35726 +
35727 +#include "dwc_otg_regs.h"
35728 +#include "dwc_otg_cil.h"
35729 +#include "dwc_otg_hcd.h"
35730 +#include <linux/kernel.h>
35731 +#include <linux/irqflags.h>
35732 +#include <linux/string.h>
35733 +#include <asm/barrier.h>
35734 +
35735 +#if 0
35736 +#define FLAME_ON(x) \
35737 +do { \
35738 + int gpioreg; \
35739 + \
35740 + gpioreg = readl(__io_address(0x20200000+0x8)); \
35741 + gpioreg &= ~(7 << (x-20)*3); \
35742 + gpioreg |= 0x1 << (x-20)*3; \
35743 + writel(gpioreg, __io_address(0x20200000+0x8)); \
35744 + \
35745 + writel(1<<x, __io_address(0x20200000+(0x1C))); \
35746 +} while (0)
35747 +
35748 +#define FLAME_OFF(x) \
35749 +do { \
35750 + writel(1<<x, __io_address(0x20200000+(0x28))); \
35751 +} while (0)
35752 +#else
35753 +#define FLAME_ON(x) do { } while (0)
35754 +#define FLAME_OFF(X) do { } while (0)
35755 +#endif
35756 +
35757 +/* This is a quick-and-dirty arch-specific register read/write. We know that
35758 + * writes to a peripheral on BCM2835 will always arrive in-order, also that
35759 + * reads and writes are executed in-order therefore the need for memory barriers
35760 + * is obviated if we're only talking to USB.
35761 + */
35762 +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
35763 +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
35764 +
35765 +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
35766 +#define GINTSTS 0x014
35767 +#define GINTMSK 0x018
35768 +/* Debug register. Poll the top of the received packets FIFO. */
35769 +#define GRXSTSR 0x01C
35770 +#define HFNUM 0x408
35771 +#define HAINT 0x414
35772 +#define HAINTMSK 0x418
35773 +#define HPRT0 0x440
35774 +
35775 +/* HC_regs start from an offset of 0x500 */
35776 +#define HC_START 0x500
35777 +#define HC_OFFSET 0x020
35778 +
35779 +#define HC_DMA 0x14
35780 +
35781 +#define HCCHAR 0x00
35782 +#define HCSPLT 0x04
35783 +#define HCINT 0x08
35784 +#define HCINTMSK 0x0C
35785 +#define HCTSIZ 0x10
35786 +
35787 +#define ISOC_XACTPOS_ALL 0b11
35788 +#define ISOC_XACTPOS_BEGIN 0b10
35789 +#define ISOC_XACTPOS_MID 0b00
35790 +#define ISOC_XACTPOS_END 0b01
35791 +
35792 +#define DWC_PID_DATA2 0b01
35793 +#define DWC_PID_MDATA 0b11
35794 +#define DWC_PID_DATA1 0b10
35795 +#define DWC_PID_DATA0 0b00
35796 +
35797 +typedef struct {
35798 + volatile void* base;
35799 + volatile void* ctrl;
35800 + volatile void* outdda;
35801 + volatile void* outddb;
35802 + volatile void* intstat;
35803 + volatile void* swirq_set;
35804 + volatile void* swirq_clr;
35805 +} mphi_regs_t;
35806 +
35807 +enum fiq_debug_level {
35808 + FIQDBG_SCHED = (1 << 0),
35809 + FIQDBG_INT = (1 << 1),
35810 + FIQDBG_ERR = (1 << 2),
35811 + FIQDBG_PORTHUB = (1 << 3),
35812 +};
35813 +
35814 +#ifdef CONFIG_ARM64
35815 +
35816 +typedef spinlock_t fiq_lock_t;
35817 +
35818 +#else
35819 +
35820 +typedef struct {
35821 + union {
35822 + uint32_t slock;
35823 + struct _tickets {
35824 + uint16_t owner;
35825 + uint16_t next;
35826 + } tickets;
35827 + };
35828 +} fiq_lock_t;
35829 +
35830 +#endif
35831 +
35832 +struct fiq_state;
35833 +
35834 +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
35835 +#if 0
35836 +#define fiq_print _fiq_print
35837 +#else
35838 +#define fiq_print(x, y, ...)
35839 +#endif
35840 +
35841 +extern bool fiq_enable, fiq_fsm_enable;
35842 +extern ushort nak_holdoff;
35843 +
35844 +/**
35845 + * enum fiq_fsm_state - The FIQ FSM states.
35846 + *
35847 + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
35848 + * USB2.0 specification for host responses to various transaction states.
35849 + * There are modifications to this host state machine because of a variety of
35850 + * quirks and limitations in the dwc_otg hardware.
35851 + *
35852 + * The fsm state is also used to communicate back to the driver on completion of
35853 + * a split transaction. The end states are used in conjunction with the interrupts
35854 + * raised by the final transaction.
35855 + */
35856 +enum fiq_fsm_state {
35857 + /* FIQ isn't enabled for this host channel */
35858 + FIQ_PASSTHROUGH = 0,
35859 + /* For the first interrupt received for this channel,
35860 + * the FIQ has to ack any interrupts indicating success. */
35861 + FIQ_PASSTHROUGH_ERRORSTATE = 31,
35862 + /* Nonperiodic state groups */
35863 + FIQ_NP_SSPLIT_STARTED = 1,
35864 + FIQ_NP_SSPLIT_RETRY = 2,
35865 + /* TT contention - working around hub bugs */
35866 + FIQ_NP_SSPLIT_PENDING = 33,
35867 + FIQ_NP_OUT_CSPLIT_RETRY = 3,
35868 + FIQ_NP_IN_CSPLIT_RETRY = 4,
35869 + FIQ_NP_SPLIT_DONE = 5,
35870 + FIQ_NP_SPLIT_LS_ABORTED = 6,
35871 + /* This differentiates a HS transaction error from a LS one
35872 + * (handling the hub state is different) */
35873 + FIQ_NP_SPLIT_HS_ABORTED = 7,
35874 +
35875 + /* Periodic state groups */
35876 + /* Periodic transactions are either started directly by the IRQ handler
35877 + * or deferred if the TT is already in use.
35878 + */
35879 + FIQ_PER_SSPLIT_QUEUED = 8,
35880 + FIQ_PER_SSPLIT_STARTED = 9,
35881 + FIQ_PER_SSPLIT_LAST = 10,
35882 +
35883 +
35884 + FIQ_PER_ISO_OUT_PENDING = 11,
35885 + FIQ_PER_ISO_OUT_ACTIVE = 12,
35886 + FIQ_PER_ISO_OUT_LAST = 13,
35887 + FIQ_PER_ISO_OUT_DONE = 27,
35888 +
35889 + FIQ_PER_CSPLIT_WAIT = 14,
35890 + FIQ_PER_CSPLIT_NYET1 = 15,
35891 + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
35892 + FIQ_PER_CSPLIT_NYET_FAFF = 29,
35893 + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
35894 + FIQ_PER_CSPLIT_POLL = 16,
35895 + /* The last CSPLIT for a transaction has been issued, differentiates
35896 + * for the state machine to queue the next packet.
35897 + */
35898 + FIQ_PER_CSPLIT_LAST = 17,
35899 +
35900 + FIQ_PER_SPLIT_DONE = 18,
35901 + FIQ_PER_SPLIT_LS_ABORTED = 19,
35902 + FIQ_PER_SPLIT_HS_ABORTED = 20,
35903 + FIQ_PER_SPLIT_NYET_ABORTED = 21,
35904 + /* Frame rollover has occurred without the transaction finishing. */
35905 + FIQ_PER_SPLIT_TIMEOUT = 22,
35906 +
35907 + /* FIQ-accelerated HS Isochronous state groups */
35908 + FIQ_HS_ISOC_TURBO = 23,
35909 + /* For interval > 1, SOF wakes up the isochronous FSM */
35910 + FIQ_HS_ISOC_SLEEPING = 24,
35911 + FIQ_HS_ISOC_DONE = 25,
35912 + FIQ_HS_ISOC_ABORTED = 26,
35913 + FIQ_DEQUEUE_ISSUED = 30,
35914 + FIQ_TEST = 32,
35915 +};
35916 +
35917 +struct fiq_stack {
35918 + int magic1;
35919 + uint8_t stack[2048];
35920 + int magic2;
35921 +};
35922 +
35923 +
35924 +/**
35925 + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
35926 + * @index: Number of slots reported used for IN transactions / number of slots
35927 + * transmitted for an OUT transaction
35928 + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
35929 + *
35930 + * Split transaction transfers can have variable length depending on other bus
35931 + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
35932 + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
35933 + * can happen per-frame.
35934 + */
35935 +struct fiq_dma_info {
35936 + u8 index;
35937 + u8 slot_len[6];
35938 +};
35939 +
35940 +struct fiq_split_dma_slot {
35941 + u8 buf[188];
35942 +} __attribute__((packed));
35943 +
35944 +struct fiq_dma_channel {
35945 + struct fiq_split_dma_slot index[6];
35946 +} __attribute__((packed));
35947 +
35948 +struct fiq_dma_blob {
35949 + struct fiq_dma_channel channel[0];
35950 +} __attribute__((packed));
35951 +
35952 +/**
35953 + * struct fiq_hs_isoc_info - USB2.0 isochronous data
35954 + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
35955 + * @nrframes: Total length of iso_frame_desc array
35956 + * @index: Current index (FIQ-maintained)
35957 + * @stride: Interval in uframes between HS isoc transactions
35958 + */
35959 +struct fiq_hs_isoc_info {
35960 + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
35961 + unsigned int nrframes;
35962 + unsigned int index;
35963 + unsigned int stride;
35964 +};
35965 +
35966 +/**
35967 + * struct fiq_channel_state - FIQ state machine storage
35968 + * @fsm: Current state of the channel as understood by the FIQ
35969 + * @nr_errors: Number of transaction errors on this split-transaction
35970 + * @hub_addr: SSPLIT/CSPLIT destination hub
35971 + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
35972 + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
35973 + * split-IN, number of CSPLIT data packets that were received.
35974 + * @hcchar_copy:
35975 + * @hcsplt_copy:
35976 + * @hcintmsk_copy:
35977 + * @hctsiz_copy: Copies of the host channel registers.
35978 + * For use as scratch, or for returning state.
35979 + *
35980 + * The fiq_channel_state is state storage between interrupts for a host channel. The
35981 + * FSM state is stored here. Members of this structure must only be set up by the
35982 + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
35983 + * has updated the state to either a COMPLETE state group or ABORT state group.
35984 + */
35985 +
35986 +struct fiq_channel_state {
35987 + enum fiq_fsm_state fsm;
35988 + unsigned int nr_errors;
35989 + unsigned int hub_addr;
35990 + unsigned int port_addr;
35991 + /* Hardware bug workaround: sometimes channel halt interrupts are
35992 + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
35993 + unsigned int expected_uframe;
35994 + /* number of uframes remaining (for interval > 1 HS isoc transfers) before next transfer */
35995 + unsigned int uframe_sleeps;
35996 + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
35997 + unsigned int nrpackets;
35998 + struct fiq_dma_info dma_info;
35999 + struct fiq_hs_isoc_info hs_isoc_info;
36000 + /* Copies of HC registers - in/out communication from/to IRQ handler
36001 + * and for ease of channel setup. A bit of mungeing is performed - for
36002 + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
36003 + */
36004 + hcchar_data_t hcchar_copy;
36005 + hcsplt_data_t hcsplt_copy;
36006 + hcint_data_t hcint_copy;
36007 + hcintmsk_data_t hcintmsk_copy;
36008 + hctsiz_data_t hctsiz_copy;
36009 + hcdma_data_t hcdma_copy;
36010 +};
36011 +
36012 +/**
36013 + * struct fiq_state - top-level FIQ state machine storage
36014 + * @mphi_regs: virtual address of the MPHI peripheral register file
36015 + * @dwc_regs_base: virtual address of the base of the DWC core register file
36016 + * @dma_base: physical address for the base of the DMA bounce buffers
36017 + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
36018 + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
36019 + * Used for determining which interrupts fired to set off the IRQ handler.
36020 + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
36021 + * @np_count: Non-periodic transactions in the active queue
36022 + * @np_sent: Count of non-periodic transactions that have completed
36023 + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
36024 + * this is the next frame on which a SOF interrupt is required. Used to hold off
36025 + * passing SOF through to the driver until necessary.
36026 + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
36027 + * channels configured into the core logic.
36028 + *
36029 + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
36030 + * It contains top-level state information.
36031 + */
36032 +struct fiq_state {
36033 + fiq_lock_t lock;
36034 + mphi_regs_t mphi_regs;
36035 + void *dwc_regs_base;
36036 + dma_addr_t dma_base;
36037 + struct fiq_dma_blob *fiq_dmab;
36038 + void *dummy_send;
36039 + dma_addr_t dummy_send_dma;
36040 + gintmsk_data_t gintmsk_saved;
36041 + haintmsk_data_t haintmsk_saved;
36042 + int mphi_int_count;
36043 + unsigned int fiq_done;
36044 + unsigned int kick_np_queues;
36045 + unsigned int next_sched_frame;
36046 +#ifdef FIQ_DEBUG
36047 + char * buffer;
36048 + unsigned int bufsiz;
36049 +#endif
36050 + struct fiq_channel_state channel[0];
36051 +};
36052 +
36053 +#ifdef CONFIG_ARM64
36054 +
36055 +#ifdef local_fiq_enable
36056 +#undef local_fiq_enable
36057 +#endif
36058 +
36059 +#ifdef local_fiq_disable
36060 +#undef local_fiq_disable
36061 +#endif
36062 +
36063 +extern void local_fiq_enable(void);
36064 +
36065 +extern void local_fiq_disable(void);
36066 +
36067 +#endif
36068 +
36069 +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
36070 +
36071 +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
36072 +
36073 +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
36074 +
36075 +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
36076 +
36077 +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
36078 +
36079 +extern void dwc_otg_fiq_nop(struct fiq_state *state);
36080 +
36081 +#endif /* DWC_OTG_FIQ_FSM_H_ */
36082 --- /dev/null
36083 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
36084 @@ -0,0 +1,80 @@
36085 +/*
36086 + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
36087 + *
36088 + * Copyright (c) 2013 Raspberry Pi Foundation
36089 + *
36090 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
36091 + * All rights reserved.
36092 + *
36093 + * Redistribution and use in source and binary forms, with or without
36094 + * modification, are permitted provided that the following conditions are met:
36095 + * * Redistributions of source code must retain the above copyright
36096 + * notice, this list of conditions and the following disclaimer.
36097 + * * Redistributions in binary form must reproduce the above copyright
36098 + * notice, this list of conditions and the following disclaimer in the
36099 + * documentation and/or other materials provided with the distribution.
36100 + * * Neither the name of Raspberry Pi nor the
36101 + * names of its contributors may be used to endorse or promote products
36102 + * derived from this software without specific prior written permission.
36103 + *
36104 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
36105 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36106 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36107 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
36108 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36109 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36110 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36111 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36112 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36113 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36114 + */
36115 +
36116 +
36117 +#include <asm/assembler.h>
36118 +#include <linux/linkage.h>
36119 +
36120 +
36121 +.text
36122 +
36123 +.global _dwc_otg_fiq_stub_end;
36124 +
36125 +/**
36126 + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
36127 + * a C-style function call with arguments from the FIQ banked registers.
36128 + * r0 = &hcd->fiq_state
36129 + * r1 = &hcd->num_channels
36130 + * r2 = &hcd->dma_buffers
36131 + * Tramples: r0, r1, r2, r4, fp, ip
36132 + */
36133 +
36134 +ENTRY(_dwc_otg_fiq_stub)
36135 + /* Stash unbanked regs - SP will have been set up for us */
36136 + mov ip, sp;
36137 + stmdb sp!, {r0-r12, lr};
36138 +#ifdef FIQ_DEBUG
36139 + // Cycle profiling - read cycle counter at start
36140 + mrc p15, 0, r5, c15, c12, 1;
36141 +#endif
36142 + /* r11 = fp, don't trample it */
36143 + mov r4, fp;
36144 + /* set EABI frame size */
36145 + sub fp, ip, #512;
36146 +
36147 + /* for fiq NOP mode - just need state */
36148 + mov r0, r8;
36149 + /* r9 = num_channels */
36150 + mov r1, r9;
36151 + /* r10 = struct *dma_bufs */
36152 +// mov r2, r10;
36153 +
36154 + /* r4 = &fiq_c_function */
36155 + blx r4;
36156 +#ifdef FIQ_DEBUG
36157 + mrc p15, 0, r4, c15, c12, 1;
36158 + subs r5, r5, r4;
36159 + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
36160 +#endif
36161 + ldmia sp!, {r0-r12, lr};
36162 + subs pc, lr, #4;
36163 +_dwc_otg_fiq_stub_end:
36164 +END(_dwc_otg_fiq_stub)
36165 --- /dev/null
36166 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
36167 @@ -0,0 +1,4363 @@
36168 +
36169 +/* ==========================================================================
36170 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
36171 + * $Revision: #104 $
36172 + * $Date: 2011/10/24 $
36173 + * $Change: 1871159 $
36174 + *
36175 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
36176 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
36177 + * otherwise expressly agreed to in writing between Synopsys and you.
36178 + *
36179 + * The Software IS NOT an item of Licensed Software or Licensed Product under
36180 + * any End User Software License Agreement or Agreement for Licensed Product
36181 + * with Synopsys or any supplement thereto. You are permitted to use and
36182 + * redistribute this Software in source and binary forms, with or without
36183 + * modification, provided that redistributions of source code must retain this
36184 + * notice. You may not view, use, disclose, copy or distribute this file or
36185 + * any information contained herein except pursuant to this license grant from
36186 + * Synopsys. If you do not agree with this notice, including the disclaimer
36187 + * below, then you are not authorized to use the Software.
36188 + *
36189 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
36190 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36191 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36192 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
36193 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36194 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36195 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36196 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36197 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36198 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
36199 + * DAMAGE.
36200 + * ========================================================================== */
36201 +#ifndef DWC_DEVICE_ONLY
36202 +
36203 +/** @file
36204 + * This file implements HCD Core. All code in this file is portable and doesn't
36205 + * use any OS specific functions.
36206 + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
36207 + * header file.
36208 + */
36209 +
36210 +#include <linux/usb.h>
36211 +#include <linux/usb/hcd.h>
36212 +
36213 +#include "dwc_otg_hcd.h"
36214 +#include "dwc_otg_regs.h"
36215 +#include "dwc_otg_fiq_fsm.h"
36216 +
36217 +extern bool microframe_schedule;
36218 +extern uint16_t fiq_fsm_mask, nak_holdoff;
36219 +
36220 +//#define DEBUG_HOST_CHANNELS
36221 +#ifdef DEBUG_HOST_CHANNELS
36222 +static int last_sel_trans_num_per_scheduled = 0;
36223 +static int last_sel_trans_num_nonper_scheduled = 0;
36224 +static int last_sel_trans_num_avail_hc_at_start = 0;
36225 +static int last_sel_trans_num_avail_hc_at_end = 0;
36226 +#endif /* DEBUG_HOST_CHANNELS */
36227 +
36228 +
36229 +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
36230 +{
36231 + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
36232 +}
36233 +
36234 +/**
36235 + * Connection timeout function. An OTG host is required to display a
36236 + * message if the device does not connect within 10 seconds.
36237 + */
36238 +void dwc_otg_hcd_connect_timeout(void *ptr)
36239 +{
36240 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
36241 + DWC_PRINTF("Connect Timeout\n");
36242 + __DWC_ERROR("Device Not Connected/Responding\n");
36243 +}
36244 +
36245 +#if defined(DEBUG)
36246 +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
36247 +{
36248 + if (qh->channel != NULL) {
36249 + dwc_hc_t *hc = qh->channel;
36250 + dwc_list_link_t *item;
36251 + dwc_otg_qh_t *qh_item;
36252 + int num_channels = hcd->core_if->core_params->host_channels;
36253 + int i;
36254 +
36255 + dwc_otg_hc_regs_t *hc_regs;
36256 + hcchar_data_t hcchar;
36257 + hcsplt_data_t hcsplt;
36258 + hctsiz_data_t hctsiz;
36259 + uint32_t hcdma;
36260 +
36261 + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
36262 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
36263 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
36264 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
36265 + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
36266 +
36267 + DWC_PRINTF(" Assigned to channel %p:\n", hc);
36268 + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
36269 + hcsplt.d32);
36270 + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
36271 + hcdma);
36272 + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
36273 + hc->dev_addr, hc->ep_num, hc->ep_is_in);
36274 + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
36275 + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
36276 + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
36277 + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
36278 + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
36279 + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
36280 + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
36281 + DWC_PRINTF(" qh: %p\n", hc->qh);
36282 + DWC_PRINTF(" NP inactive sched:\n");
36283 + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
36284 + qh_item =
36285 + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36286 + DWC_PRINTF(" %p\n", qh_item);
36287 + }
36288 + DWC_PRINTF(" NP active sched:\n");
36289 + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
36290 + qh_item =
36291 + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36292 + DWC_PRINTF(" %p\n", qh_item);
36293 + }
36294 + DWC_PRINTF(" Channels: \n");
36295 + for (i = 0; i < num_channels; i++) {
36296 + dwc_hc_t *hc = hcd->hc_ptr_array[i];
36297 + DWC_PRINTF(" %2d: %p\n", i, hc);
36298 + }
36299 + }
36300 +}
36301 +#else
36302 +#define dump_channel_info(hcd, qh)
36303 +#endif /* DEBUG */
36304 +
36305 +/**
36306 + * Work queue function for starting the HCD when A-Cable is connected.
36307 + * The hcd_start() must be called in a process context.
36308 + */
36309 +static void hcd_start_func(void *_vp)
36310 +{
36311 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
36312 +
36313 + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
36314 + if (hcd) {
36315 + hcd->fops->start(hcd);
36316 + }
36317 +}
36318 +
36319 +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
36320 +{
36321 +#ifdef DEBUG
36322 + int i;
36323 + int num_channels = hcd->core_if->core_params->host_channels;
36324 + for (i = 0; i < num_channels; i++) {
36325 + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
36326 + }
36327 +#endif
36328 +}
36329 +
36330 +static void del_timers(dwc_otg_hcd_t * hcd)
36331 +{
36332 + del_xfer_timers(hcd);
36333 + DWC_TIMER_CANCEL(hcd->conn_timer);
36334 +}
36335 +
36336 +/**
36337 + * Processes all the URBs in a single list of QHs. Completes them with
36338 + * -ESHUTDOWN and frees the QTD.
36339 + */
36340 +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
36341 +{
36342 + dwc_list_link_t *qh_item, *qh_tmp;
36343 + dwc_otg_qh_t *qh;
36344 + dwc_otg_qtd_t *qtd, *qtd_tmp;
36345 + int quiesced = 0;
36346 +
36347 + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
36348 + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
36349 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
36350 + &qh->qtd_list, qtd_list_entry) {
36351 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
36352 + if (qtd->urb != NULL) {
36353 + hcd->fops->complete(hcd, qtd->urb->priv,
36354 + qtd->urb, -DWC_E_SHUTDOWN);
36355 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
36356 + }
36357 +
36358 + }
36359 + if(qh->channel) {
36360 + int n = qh->channel->hc_num;
36361 + /* Using hcchar.chen == 1 is not a reliable test.
36362 + * It is possible that the channel has already halted
36363 + * but not yet been through the IRQ handler.
36364 + */
36365 + if (fiq_fsm_enable && (hcd->fiq_state->channel[qh->channel->hc_num].fsm != FIQ_PASSTHROUGH)) {
36366 + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
36367 + qh->channel->halt_pending = 1;
36368 + if (hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_TURBO ||
36369 + hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_SLEEPING)
36370 + hcd->fiq_state->channel[n].fsm = FIQ_HS_ISOC_ABORTED;
36371 + /* We're called from disconnect callback or in the middle of freeing the HCD here,
36372 + * so FIQ is disabled, top-level interrupts masked and we're holding the spinlock.
36373 + * No further URBs will be submitted, but wait 1 microframe for any previously
36374 + * submitted periodic DMA to finish.
36375 + */
36376 + if (!quiesced) {
36377 + udelay(125);
36378 + quiesced = 1;
36379 + }
36380 + } else {
36381 + dwc_otg_hc_halt(hcd->core_if, qh->channel,
36382 + DWC_OTG_HC_XFER_URB_DEQUEUE);
36383 + }
36384 + qh->channel = NULL;
36385 + }
36386 + dwc_otg_hcd_qh_remove(hcd, qh);
36387 + }
36388 +}
36389 +
36390 +/**
36391 + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
36392 + * and periodic schedules. The QTD associated with each URB is removed from
36393 + * the schedule and freed. This function may be called when a disconnect is
36394 + * detected or when the HCD is being stopped.
36395 + */
36396 +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
36397 +{
36398 + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
36399 + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
36400 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
36401 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
36402 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
36403 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
36404 +}
36405 +
36406 +/**
36407 + * Start the connection timer. An OTG host is required to display a
36408 + * message if the device does not connect within 10 seconds. The
36409 + * timer is deleted if a port connect interrupt occurs before the
36410 + * timer expires.
36411 + */
36412 +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
36413 +{
36414 + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
36415 +}
36416 +
36417 +/**
36418 + * HCD Callback function for disconnect of the HCD.
36419 + *
36420 + * @param p void pointer to the <code>struct usb_hcd</code>
36421 + */
36422 +static int32_t dwc_otg_hcd_session_start_cb(void *p)
36423 +{
36424 + dwc_otg_hcd_t *dwc_otg_hcd;
36425 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
36426 + dwc_otg_hcd = p;
36427 + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
36428 + return 1;
36429 +}
36430 +
36431 +/**
36432 + * HCD Callback function for starting the HCD when A-Cable is
36433 + * connected.
36434 + *
36435 + * @param p void pointer to the <code>struct usb_hcd</code>
36436 + */
36437 +static int32_t dwc_otg_hcd_start_cb(void *p)
36438 +{
36439 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36440 + dwc_otg_core_if_t *core_if;
36441 + hprt0_data_t hprt0;
36442 +
36443 + core_if = dwc_otg_hcd->core_if;
36444 +
36445 + if (core_if->op_state == B_HOST) {
36446 + /*
36447 + * Reset the port. During a HNP mode switch the reset
36448 + * needs to occur within 1ms and have a duration of at
36449 + * least 50ms.
36450 + */
36451 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
36452 + hprt0.b.prtrst = 1;
36453 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
36454 + }
36455 + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
36456 + hcd_start_func, dwc_otg_hcd, 50,
36457 + "start hcd");
36458 +
36459 + return 1;
36460 +}
36461 +
36462 +/**
36463 + * HCD Callback function for disconnect of the HCD.
36464 + *
36465 + * @param p void pointer to the <code>struct usb_hcd</code>
36466 + */
36467 +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
36468 +{
36469 + gintsts_data_t intr;
36470 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36471 +
36472 + DWC_SPINLOCK(dwc_otg_hcd->lock);
36473 + /*
36474 + * Set status flags for the hub driver.
36475 + */
36476 + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
36477 + dwc_otg_hcd->flags.b.port_connect_status = 0;
36478 + if(fiq_enable) {
36479 + local_fiq_disable();
36480 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
36481 + }
36482 + /*
36483 + * Shutdown any transfers in process by clearing the Tx FIFO Empty
36484 + * interrupt mask and status bits and disabling subsequent host
36485 + * channel interrupts.
36486 + */
36487 + intr.d32 = 0;
36488 + intr.b.nptxfempty = 1;
36489 + intr.b.ptxfempty = 1;
36490 + intr.b.hcintr = 1;
36491 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
36492 + intr.d32, 0);
36493 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
36494 + intr.d32, 0);
36495 +
36496 + del_timers(dwc_otg_hcd);
36497 +
36498 + /*
36499 + * Turn off the vbus power only if the core has transitioned to device
36500 + * mode. If still in host mode, need to keep power on to detect a
36501 + * reconnection.
36502 + */
36503 + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
36504 + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
36505 + hprt0_data_t hprt0 = {.d32 = 0 };
36506 + DWC_PRINTF("Disconnect: PortPower off\n");
36507 + hprt0.b.prtpwr = 0;
36508 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
36509 + hprt0.d32);
36510 + }
36511 +
36512 + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
36513 + }
36514 +
36515 + /* Respond with an error status to all URBs in the schedule. */
36516 + kill_all_urbs(dwc_otg_hcd);
36517 +
36518 + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
36519 + /* Clean up any host channels that were in use. */
36520 + int num_channels;
36521 + int i;
36522 + dwc_hc_t *channel;
36523 + dwc_otg_hc_regs_t *hc_regs;
36524 + hcchar_data_t hcchar;
36525 +
36526 + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
36527 +
36528 + if (!dwc_otg_hcd->core_if->dma_enable) {
36529 + /* Flush out any channel requests in slave mode. */
36530 + for (i = 0; i < num_channels; i++) {
36531 + channel = dwc_otg_hcd->hc_ptr_array[i];
36532 + if (DWC_CIRCLEQ_EMPTY_ENTRY
36533 + (channel, hc_list_entry)) {
36534 + hc_regs =
36535 + dwc_otg_hcd->core_if->
36536 + host_if->hc_regs[i];
36537 + hcchar.d32 =
36538 + DWC_READ_REG32(&hc_regs->hcchar);
36539 + if (hcchar.b.chen) {
36540 + hcchar.b.chen = 0;
36541 + hcchar.b.chdis = 1;
36542 + hcchar.b.epdir = 0;
36543 + DWC_WRITE_REG32
36544 + (&hc_regs->hcchar,
36545 + hcchar.d32);
36546 + }
36547 + }
36548 + }
36549 + }
36550 +
36551 + if(fiq_fsm_enable) {
36552 + for(i=0; i < 128; i++) {
36553 + dwc_otg_hcd->hub_port[i] = 0;
36554 + }
36555 + }
36556 + }
36557 +
36558 + if(fiq_enable) {
36559 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
36560 + local_fiq_enable();
36561 + }
36562 +
36563 + if (dwc_otg_hcd->fops->disconnect) {
36564 + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
36565 + }
36566 +
36567 + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
36568 + return 1;
36569 +}
36570 +
36571 +/**
36572 + * HCD Callback function for stopping the HCD.
36573 + *
36574 + * @param p void pointer to the <code>struct usb_hcd</code>
36575 + */
36576 +static int32_t dwc_otg_hcd_stop_cb(void *p)
36577 +{
36578 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36579 +
36580 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
36581 + dwc_otg_hcd_stop(dwc_otg_hcd);
36582 + return 1;
36583 +}
36584 +
36585 +#ifdef CONFIG_USB_DWC_OTG_LPM
36586 +/**
36587 + * HCD Callback function for sleep of HCD.
36588 + *
36589 + * @param p void pointer to the <code>struct usb_hcd</code>
36590 + */
36591 +static int dwc_otg_hcd_sleep_cb(void *p)
36592 +{
36593 + dwc_otg_hcd_t *hcd = p;
36594 +
36595 + dwc_otg_hcd_free_hc_from_lpm(hcd);
36596 +
36597 + return 0;
36598 +}
36599 +#endif
36600 +
36601 +
36602 +/**
36603 + * HCD Callback function for Remote Wakeup.
36604 + *
36605 + * @param p void pointer to the <code>struct usb_hcd</code>
36606 + */
36607 +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
36608 +{
36609 + dwc_otg_hcd_t *hcd = p;
36610 +
36611 + if (hcd->core_if->lx_state == DWC_OTG_L2) {
36612 + hcd->flags.b.port_suspend_change = 1;
36613 + }
36614 +#ifdef CONFIG_USB_DWC_OTG_LPM
36615 + else {
36616 + hcd->flags.b.port_l1_change = 1;
36617 + }
36618 +#endif
36619 + return 0;
36620 +}
36621 +
36622 +/**
36623 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
36624 + * stopped.
36625 + */
36626 +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
36627 +{
36628 + hprt0_data_t hprt0 = {.d32 = 0 };
36629 +
36630 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
36631 +
36632 + /*
36633 + * The root hub should be disconnected before this function is called.
36634 + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
36635 + * and the QH lists (via ..._hcd_endpoint_disable).
36636 + */
36637 +
36638 + /* Turn off all host-specific interrupts. */
36639 + dwc_otg_disable_host_interrupts(hcd->core_if);
36640 +
36641 + /* Turn off the vbus power */
36642 + DWC_PRINTF("PortPower off\n");
36643 + hprt0.b.prtpwr = 0;
36644 + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
36645 + dwc_mdelay(1);
36646 +}
36647 +
36648 +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
36649 + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
36650 + int atomic_alloc)
36651 +{
36652 + int retval = 0;
36653 + uint8_t needs_scheduling = 0;
36654 + dwc_otg_transaction_type_e tr_type;
36655 + dwc_otg_qtd_t *qtd;
36656 + gintmsk_data_t intr_mask = {.d32 = 0 };
36657 + hprt0_data_t hprt0 = { .d32 = 0 };
36658 +
36659 +#ifdef DEBUG /* integrity checks (Broadcom) */
36660 + if (NULL == hcd->core_if) {
36661 + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
36662 + /* No longer connected. */
36663 + return -DWC_E_INVALID;
36664 + }
36665 +#endif
36666 + if (!hcd->flags.b.port_connect_status) {
36667 + /* No longer connected. */
36668 + DWC_ERROR("Not connected\n");
36669 + return -DWC_E_NO_DEVICE;
36670 + }
36671 +
36672 + /* Some core configurations cannot support LS traffic on a FS root port */
36673 + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
36674 + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
36675 + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
36676 + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
36677 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
36678 + return -DWC_E_NO_DEVICE;
36679 + }
36680 + }
36681 +
36682 + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
36683 + if (qtd == NULL) {
36684 + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
36685 + return -DWC_E_NO_MEMORY;
36686 + }
36687 +#ifdef DEBUG /* integrity checks (Broadcom) */
36688 + if (qtd->urb == NULL) {
36689 + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
36690 + return -DWC_E_NO_MEMORY;
36691 + }
36692 + if (qtd->urb->priv == NULL) {
36693 + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
36694 + return -DWC_E_NO_MEMORY;
36695 + }
36696 +#endif
36697 + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
36698 + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
36699 + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
36700 + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
36701 + needs_scheduling = 0;
36702 +
36703 + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
36704 + // creates a new queue in ep_handle if it doesn't exist already
36705 + if (retval < 0) {
36706 + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
36707 + "Error status %d\n", retval);
36708 + dwc_otg_hcd_qtd_free(qtd);
36709 + return retval;
36710 + }
36711 +
36712 + if(needs_scheduling) {
36713 + tr_type = dwc_otg_hcd_select_transactions(hcd);
36714 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
36715 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
36716 + }
36717 + }
36718 + return retval;
36719 +}
36720 +
36721 +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
36722 + dwc_otg_hcd_urb_t * dwc_otg_urb)
36723 +{
36724 + dwc_otg_qh_t *qh;
36725 + dwc_otg_qtd_t *urb_qtd;
36726 + BUG_ON(!hcd);
36727 + BUG_ON(!dwc_otg_urb);
36728 +
36729 +#ifdef DEBUG /* integrity checks (Broadcom) */
36730 +
36731 + if (hcd == NULL) {
36732 + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
36733 + return -DWC_E_INVALID;
36734 + }
36735 + if (dwc_otg_urb == NULL) {
36736 + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
36737 + return -DWC_E_INVALID;
36738 + }
36739 + if (dwc_otg_urb->qtd == NULL) {
36740 + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
36741 + return -DWC_E_INVALID;
36742 + }
36743 + urb_qtd = dwc_otg_urb->qtd;
36744 + BUG_ON(!urb_qtd);
36745 + if (urb_qtd->qh == NULL) {
36746 + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
36747 + return -DWC_E_INVALID;
36748 + }
36749 +#else
36750 + urb_qtd = dwc_otg_urb->qtd;
36751 + BUG_ON(!urb_qtd);
36752 +#endif
36753 + qh = urb_qtd->qh;
36754 + BUG_ON(!qh);
36755 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
36756 + if (urb_qtd->in_process) {
36757 + dump_channel_info(hcd, qh);
36758 + }
36759 + }
36760 +#ifdef DEBUG /* integrity checks (Broadcom) */
36761 + if (hcd->core_if == NULL) {
36762 + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
36763 + return -DWC_E_INVALID;
36764 + }
36765 +#endif
36766 + if (urb_qtd->in_process && qh->channel) {
36767 + /* The QTD is in process (it has been assigned to a channel). */
36768 + if (hcd->flags.b.port_connect_status) {
36769 + int n = qh->channel->hc_num;
36770 + /*
36771 + * If still connected (i.e. in host mode), halt the
36772 + * channel so it can be used for other transfers. If
36773 + * no longer connected, the host registers can't be
36774 + * written to halt the channel since the core is in
36775 + * device mode.
36776 + */
36777 + /* In FIQ FSM mode, we need to shut down carefully.
36778 + * The FIQ may attempt to restart a disabled channel */
36779 + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
36780 + int retries = 3;
36781 + int running = 0;
36782 + enum fiq_fsm_state state;
36783 +
36784 + local_fiq_disable();
36785 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
36786 + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
36787 + qh->channel->halt_pending = 1;
36788 + if (hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_TURBO ||
36789 + hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_SLEEPING)
36790 + hcd->fiq_state->channel[n].fsm = FIQ_HS_ISOC_ABORTED;
36791 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
36792 + local_fiq_enable();
36793 +
36794 + if (dwc_qh_is_non_per(qh)) {
36795 + do {
36796 + state = READ_ONCE(hcd->fiq_state->channel[n].fsm);
36797 + running = (state != FIQ_NP_SPLIT_DONE) &&
36798 + (state != FIQ_NP_SPLIT_LS_ABORTED) &&
36799 + (state != FIQ_NP_SPLIT_HS_ABORTED);
36800 + if (!running)
36801 + break;
36802 + udelay(125);
36803 + } while(--retries);
36804 + if (!retries)
36805 + DWC_WARN("Timed out waiting for FSM NP transfer to complete on %d",
36806 + qh->channel->hc_num);
36807 + }
36808 + } else {
36809 + dwc_otg_hc_halt(hcd->core_if, qh->channel,
36810 + DWC_OTG_HC_XFER_URB_DEQUEUE);
36811 + }
36812 + }
36813 + }
36814 +
36815 + /*
36816 + * Free the QTD and clean up the associated QH. Leave the QH in the
36817 + * schedule if it has any remaining QTDs.
36818 + */
36819 +
36820 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
36821 + "delete %sQueue handler\n",
36822 + hcd->core_if->dma_desc_enable?"DMA ":"");
36823 + if (!hcd->core_if->dma_desc_enable) {
36824 + uint8_t b = urb_qtd->in_process;
36825 + if (nak_holdoff && qh->do_split && dwc_qh_is_non_per(qh))
36826 + qh->nak_frame = 0xFFFF;
36827 + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
36828 + if (b) {
36829 + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
36830 + qh->channel = NULL;
36831 + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
36832 + dwc_otg_hcd_qh_remove(hcd, qh);
36833 + }
36834 + } else {
36835 + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
36836 + }
36837 + return 0;
36838 +}
36839 +
36840 +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
36841 + int retry)
36842 +{
36843 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
36844 + int retval = 0;
36845 + dwc_irqflags_t flags;
36846 +
36847 + if (retry < 0) {
36848 + retval = -DWC_E_INVALID;
36849 + goto done;
36850 + }
36851 +
36852 + if (!qh) {
36853 + retval = -DWC_E_INVALID;
36854 + goto done;
36855 + }
36856 +
36857 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36858 +
36859 + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
36860 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36861 + retry--;
36862 + dwc_msleep(5);
36863 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36864 + }
36865 +
36866 + dwc_otg_hcd_qh_remove(hcd, qh);
36867 +
36868 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36869 + /*
36870 + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
36871 + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
36872 + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
36873 + * and dwc_otg_hcd_frame_list_alloc().
36874 + */
36875 + dwc_otg_hcd_qh_free(hcd, qh);
36876 +
36877 +done:
36878 + return retval;
36879 +}
36880 +
36881 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
36882 +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
36883 +{
36884 + int retval = 0;
36885 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
36886 + if (!qh)
36887 + return -DWC_E_INVALID;
36888 +
36889 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
36890 + return retval;
36891 +}
36892 +#endif
36893 +
36894 +/**
36895 + * HCD Callback structure for handling mode switching.
36896 + */
36897 +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
36898 + .start = dwc_otg_hcd_start_cb,
36899 + .stop = dwc_otg_hcd_stop_cb,
36900 + .disconnect = dwc_otg_hcd_disconnect_cb,
36901 + .session_start = dwc_otg_hcd_session_start_cb,
36902 + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
36903 +#ifdef CONFIG_USB_DWC_OTG_LPM
36904 + .sleep = dwc_otg_hcd_sleep_cb,
36905 +#endif
36906 + .p = 0,
36907 +};
36908 +
36909 +/**
36910 + * Reset tasklet function
36911 + */
36912 +static void reset_tasklet_func(void *data)
36913 +{
36914 + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
36915 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
36916 + hprt0_data_t hprt0;
36917 +
36918 + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
36919 +
36920 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
36921 + hprt0.b.prtrst = 1;
36922 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
36923 + dwc_mdelay(60);
36924 +
36925 + hprt0.b.prtrst = 0;
36926 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
36927 + dwc_otg_hcd->flags.b.port_reset_change = 1;
36928 +}
36929 +
36930 +static void completion_tasklet_func(void *ptr)
36931 +{
36932 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
36933 + struct urb *urb;
36934 + urb_tq_entry_t *item;
36935 + dwc_irqflags_t flags;
36936 +
36937 + /* This could just be spin_lock_irq */
36938 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36939 + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
36940 + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
36941 + urb = item->urb;
36942 + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
36943 + urb_tq_entries);
36944 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36945 + DWC_FREE(item);
36946 +
36947 + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
36948 +
36949 +
36950 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36951 + }
36952 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36953 + return;
36954 +}
36955 +
36956 +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
36957 +{
36958 + dwc_list_link_t *item;
36959 + dwc_otg_qh_t *qh;
36960 + dwc_irqflags_t flags;
36961 +
36962 + if (!qh_list->next) {
36963 + /* The list hasn't been initialized yet. */
36964 + return;
36965 + }
36966 + /*
36967 + * Hold spinlock here. Not needed in that case if bellow
36968 + * function is being called from ISR
36969 + */
36970 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
36971 + /* Ensure there are no QTDs or URBs left. */
36972 + kill_urbs_in_qh_list(hcd, qh_list);
36973 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
36974 +
36975 + DWC_LIST_FOREACH(item, qh_list) {
36976 + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36977 + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
36978 + }
36979 +}
36980 +
36981 +/**
36982 + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
36983 + * Device during SRP time by host power up.
36984 + */
36985 +void dwc_otg_hcd_power_up(void *ptr)
36986 +{
36987 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
36988 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
36989 +
36990 + DWC_PRINTF("%s called\n", __FUNCTION__);
36991 +
36992 + if (!core_if->hibernation_suspend) {
36993 + DWC_PRINTF("Already exited from Hibernation\n");
36994 + return;
36995 + }
36996 +
36997 + /* Switch on the voltage to the core */
36998 + gpwrdn.b.pwrdnswtch = 1;
36999 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37000 + dwc_udelay(10);
37001 +
37002 + /* Reset the core */
37003 + gpwrdn.d32 = 0;
37004 + gpwrdn.b.pwrdnrstn = 1;
37005 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37006 + dwc_udelay(10);
37007 +
37008 + /* Disable power clamps */
37009 + gpwrdn.d32 = 0;
37010 + gpwrdn.b.pwrdnclmp = 1;
37011 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37012 +
37013 + /* Remove reset the core signal */
37014 + gpwrdn.d32 = 0;
37015 + gpwrdn.b.pwrdnrstn = 1;
37016 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
37017 + dwc_udelay(10);
37018 +
37019 + /* Disable PMU interrupt */
37020 + gpwrdn.d32 = 0;
37021 + gpwrdn.b.pmuintsel = 1;
37022 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37023 +
37024 + core_if->hibernation_suspend = 0;
37025 +
37026 + /* Disable PMU */
37027 + gpwrdn.d32 = 0;
37028 + gpwrdn.b.pmuactv = 1;
37029 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37030 + dwc_udelay(10);
37031 +
37032 + /* Enable VBUS */
37033 + gpwrdn.d32 = 0;
37034 + gpwrdn.b.dis_vbus = 1;
37035 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37036 +
37037 + core_if->op_state = A_HOST;
37038 + dwc_otg_core_init(core_if);
37039 + dwc_otg_enable_global_interrupts(core_if);
37040 + cil_hcd_start(core_if);
37041 +}
37042 +
37043 +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
37044 +{
37045 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
37046 + struct fiq_dma_blob *blob = hcd->fiq_dmab;
37047 + int i;
37048 +
37049 + st->fsm = FIQ_PASSTHROUGH;
37050 + st->hcchar_copy.d32 = 0;
37051 + st->hcsplt_copy.d32 = 0;
37052 + st->hcint_copy.d32 = 0;
37053 + st->hcintmsk_copy.d32 = 0;
37054 + st->hctsiz_copy.d32 = 0;
37055 + st->hcdma_copy.d32 = 0;
37056 + st->nr_errors = 0;
37057 + st->hub_addr = 0;
37058 + st->port_addr = 0;
37059 + st->expected_uframe = 0;
37060 + st->nrpackets = 0;
37061 + st->dma_info.index = 0;
37062 + for (i = 0; i < 6; i++)
37063 + st->dma_info.slot_len[i] = 255;
37064 + st->hs_isoc_info.index = 0;
37065 + st->hs_isoc_info.iso_desc = NULL;
37066 + st->hs_isoc_info.nrframes = 0;
37067 +
37068 + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
37069 +}
37070 +
37071 +/**
37072 + * Frees secondary storage associated with the dwc_otg_hcd structure contained
37073 + * in the struct usb_hcd field.
37074 + */
37075 +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
37076 +{
37077 + struct device *dev = dwc_otg_hcd_to_dev(dwc_otg_hcd);
37078 + int i;
37079 +
37080 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
37081 +
37082 + del_timers(dwc_otg_hcd);
37083 +
37084 + /* Free memory for QH/QTD lists */
37085 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
37086 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
37087 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
37088 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
37089 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
37090 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
37091 +
37092 + /* Free memory for the host channels. */
37093 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
37094 + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
37095 +
37096 +#ifdef DEBUG
37097 + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
37098 + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
37099 + }
37100 +#endif
37101 + if (hc != NULL) {
37102 + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
37103 + i, hc);
37104 + DWC_FREE(hc);
37105 + }
37106 + }
37107 +
37108 + if (dwc_otg_hcd->core_if->dma_enable) {
37109 + if (dwc_otg_hcd->status_buf_dma) {
37110 + DWC_DMA_FREE(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
37111 + dwc_otg_hcd->status_buf,
37112 + dwc_otg_hcd->status_buf_dma);
37113 + }
37114 + } else if (dwc_otg_hcd->status_buf != NULL) {
37115 + DWC_FREE(dwc_otg_hcd->status_buf);
37116 + }
37117 + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
37118 + /* Set core_if's lock pointer to NULL */
37119 + dwc_otg_hcd->core_if->lock = NULL;
37120 +
37121 + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
37122 + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
37123 + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
37124 + DWC_DMA_FREE(dev, 16, dwc_otg_hcd->fiq_state->dummy_send,
37125 + dwc_otg_hcd->fiq_state->dummy_send_dma);
37126 + DWC_FREE(dwc_otg_hcd->fiq_state);
37127 +
37128 +#ifdef DWC_DEV_SRPCAP
37129 + if (dwc_otg_hcd->core_if->power_down == 2 &&
37130 + dwc_otg_hcd->core_if->pwron_timer) {
37131 + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
37132 + }
37133 +#endif
37134 + DWC_FREE(dwc_otg_hcd);
37135 +}
37136 +
37137 +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
37138 +{
37139 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
37140 + int retval = 0;
37141 + int num_channels;
37142 + int i;
37143 + dwc_hc_t *channel;
37144 +
37145 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
37146 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
37147 +#else
37148 + hcd->lock = DWC_SPINLOCK_ALLOC();
37149 +#endif
37150 + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
37151 + hcd, core_if);
37152 + if (!hcd->lock) {
37153 + DWC_ERROR("Could not allocate lock for pcd");
37154 + DWC_FREE(hcd);
37155 + retval = -DWC_E_NO_MEMORY;
37156 + goto out;
37157 + }
37158 + hcd->core_if = core_if;
37159 +
37160 + /* Register the HCD CIL Callbacks */
37161 + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
37162 + &hcd_cil_callbacks, hcd);
37163 +
37164 + /* Initialize the non-periodic schedule. */
37165 + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
37166 + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
37167 +
37168 + /* Initialize the periodic schedule. */
37169 + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
37170 + DWC_LIST_INIT(&hcd->periodic_sched_ready);
37171 + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
37172 + DWC_LIST_INIT(&hcd->periodic_sched_queued);
37173 + DWC_TAILQ_INIT(&hcd->completed_urb_list);
37174 + /*
37175 + * Create a host channel descriptor for each host channel implemented
37176 + * in the controller. Initialize the channel descriptor array.
37177 + */
37178 + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
37179 + num_channels = hcd->core_if->core_params->host_channels;
37180 + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
37181 + for (i = 0; i < num_channels; i++) {
37182 + channel = DWC_ALLOC(sizeof(dwc_hc_t));
37183 + if (channel == NULL) {
37184 + retval = -DWC_E_NO_MEMORY;
37185 + DWC_ERROR("%s: host channel allocation failed\n",
37186 + __func__);
37187 + dwc_otg_hcd_free(hcd);
37188 + goto out;
37189 + }
37190 + channel->hc_num = i;
37191 + hcd->hc_ptr_array[i] = channel;
37192 +#ifdef DEBUG
37193 + hcd->core_if->hc_xfer_timer[i] =
37194 + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
37195 + &hcd->core_if->hc_xfer_info[i]);
37196 +#endif
37197 + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
37198 + channel);
37199 + }
37200 +
37201 + if (fiq_enable) {
37202 + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
37203 + if (!hcd->fiq_state) {
37204 + retval = -DWC_E_NO_MEMORY;
37205 + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
37206 + dwc_otg_hcd_free(hcd);
37207 + goto out;
37208 + }
37209 + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
37210 +
37211 +#ifdef CONFIG_ARM64
37212 + spin_lock_init(&hcd->fiq_state->lock);
37213 +#endif
37214 +
37215 + for (i = 0; i < num_channels; i++) {
37216 + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
37217 + }
37218 + hcd->fiq_state->dummy_send = DWC_DMA_ALLOC_ATOMIC(dev, 16,
37219 + &hcd->fiq_state->dummy_send_dma);
37220 +
37221 + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
37222 + if (!hcd->fiq_stack) {
37223 + retval = -DWC_E_NO_MEMORY;
37224 + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
37225 + dwc_otg_hcd_free(hcd);
37226 + goto out;
37227 + }
37228 + hcd->fiq_stack->magic1 = 0xDEADBEEF;
37229 + hcd->fiq_stack->magic2 = 0xD00DFEED;
37230 + hcd->fiq_state->gintmsk_saved.d32 = ~0;
37231 + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
37232 +
37233 + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
37234 + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
37235 + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
37236 + * moderately readable array casts.
37237 + */
37238 + hcd->fiq_dmab = DWC_DMA_ALLOC(dev, (sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
37239 + DWC_WARN("FIQ DMA bounce buffers: virt = %px dma = %pad len=%zu",
37240 + hcd->fiq_dmab, &hcd->fiq_state->dma_base,
37241 + sizeof(struct fiq_dma_channel) * num_channels);
37242 +
37243 + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
37244 +
37245 + /* pointer for debug in fiq_print */
37246 + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
37247 + if (fiq_fsm_enable) {
37248 + int i;
37249 + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
37250 + dwc_otg_cleanup_fiq_channel(hcd, i);
37251 + }
37252 + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
37253 + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
37254 + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
37255 + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
37256 + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
37257 + }
37258 + }
37259 +
37260 + /* Initialize the Connection timeout timer. */
37261 + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
37262 + dwc_otg_hcd_connect_timeout, 0);
37263 +
37264 + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
37265 + if (microframe_schedule)
37266 + init_hcd_usecs(hcd);
37267 +
37268 + /* Initialize reset tasklet. */
37269 + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
37270 +
37271 + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
37272 + completion_tasklet_func, hcd);
37273 +#ifdef DWC_DEV_SRPCAP
37274 + if (hcd->core_if->power_down == 2) {
37275 + /* Initialize Power on timer for Host power up in case hibernation */
37276 + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
37277 + dwc_otg_hcd_power_up, core_if);
37278 + }
37279 +#endif
37280 +
37281 + /*
37282 + * Allocate space for storing data on status transactions. Normally no
37283 + * data is sent, but this space acts as a bit bucket. This must be
37284 + * done after usb_add_hcd since that function allocates the DMA buffer
37285 + * pool.
37286 + */
37287 + if (hcd->core_if->dma_enable) {
37288 + hcd->status_buf =
37289 + DWC_DMA_ALLOC(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
37290 + &hcd->status_buf_dma);
37291 + } else {
37292 + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
37293 + }
37294 + if (!hcd->status_buf) {
37295 + retval = -DWC_E_NO_MEMORY;
37296 + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
37297 + dwc_otg_hcd_free(hcd);
37298 + goto out;
37299 + }
37300 +
37301 + hcd->otg_port = 1;
37302 + hcd->frame_list = NULL;
37303 + hcd->frame_list_dma = 0;
37304 + hcd->periodic_qh_count = 0;
37305 +
37306 + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
37307 +#ifdef FIQ_DEBUG
37308 + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
37309 +#endif
37310 +
37311 +out:
37312 + return retval;
37313 +}
37314 +
37315 +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
37316 +{
37317 + /* Turn off all host-specific interrupts. */
37318 + dwc_otg_disable_host_interrupts(hcd->core_if);
37319 +
37320 + dwc_otg_hcd_free(hcd);
37321 +}
37322 +
37323 +/**
37324 + * Initializes dynamic portions of the DWC_otg HCD state.
37325 + */
37326 +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
37327 +{
37328 + int num_channels;
37329 + int i;
37330 + dwc_hc_t *channel;
37331 + dwc_hc_t *channel_tmp;
37332 +
37333 + hcd->flags.d32 = 0;
37334 +
37335 + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
37336 + if (!microframe_schedule) {
37337 + hcd->non_periodic_channels = 0;
37338 + hcd->periodic_channels = 0;
37339 + } else {
37340 + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
37341 + }
37342 + /*
37343 + * Put all channels in the free channel list and clean up channel
37344 + * states.
37345 + */
37346 + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
37347 + &hcd->free_hc_list, hc_list_entry) {
37348 + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
37349 + }
37350 +
37351 + num_channels = hcd->core_if->core_params->host_channels;
37352 + for (i = 0; i < num_channels; i++) {
37353 + channel = hcd->hc_ptr_array[i];
37354 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
37355 + hc_list_entry);
37356 + dwc_otg_hc_cleanup(hcd->core_if, channel);
37357 + }
37358 +
37359 + /* Initialize the DWC core for host mode operation. */
37360 + dwc_otg_core_host_init(hcd->core_if);
37361 +
37362 + /* Set core_if's lock pointer to the hcd->lock */
37363 + hcd->core_if->lock = hcd->lock;
37364 +}
37365 +
37366 +/**
37367 + * Assigns transactions from a QTD to a free host channel and initializes the
37368 + * host channel to perform the transactions. The host channel is removed from
37369 + * the free list.
37370 + *
37371 + * @param hcd The HCD state structure.
37372 + * @param qh Transactions from the first QTD for this QH are selected and
37373 + * assigned to a free host channel.
37374 + */
37375 +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
37376 +{
37377 + dwc_hc_t *hc;
37378 + dwc_otg_qtd_t *qtd;
37379 + dwc_otg_hcd_urb_t *urb;
37380 + void* ptr = NULL;
37381 + uint16_t wLength;
37382 + uint32_t intr_enable;
37383 + unsigned long flags;
37384 + gintmsk_data_t gintmsk = { .d32 = 0, };
37385 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
37386 +
37387 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37388 +
37389 + urb = qtd->urb;
37390 +
37391 + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
37392 +
37393 + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
37394 + urb->actual_length = urb->length;
37395 +
37396 +
37397 + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
37398 +
37399 + /* Remove the host channel from the free list. */
37400 + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
37401 +
37402 + qh->channel = hc;
37403 +
37404 + qtd->in_process = 1;
37405 +
37406 + /*
37407 + * Use usb_pipedevice to determine device address. This address is
37408 + * 0 before the SET_ADDRESS command and the correct address afterward.
37409 + */
37410 + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
37411 + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
37412 + hc->speed = qh->dev_speed;
37413 + hc->max_packet = dwc_max_packet(qh->maxp);
37414 +
37415 + hc->xfer_started = 0;
37416 + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
37417 + hc->error_state = (qtd->error_count > 0);
37418 + hc->halt_on_queue = 0;
37419 + hc->halt_pending = 0;
37420 + hc->requests = 0;
37421 +
37422 + /*
37423 + * The following values may be modified in the transfer type section
37424 + * below. The xfer_len value may be reduced when the transfer is
37425 + * started to accommodate the max widths of the XferSize and PktCnt
37426 + * fields in the HCTSIZn register.
37427 + */
37428 +
37429 + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
37430 + if (hc->ep_is_in) {
37431 + hc->do_ping = 0;
37432 + } else {
37433 + hc->do_ping = qh->ping_state;
37434 + }
37435 +
37436 + hc->data_pid_start = qh->data_toggle;
37437 + hc->multi_count = 1;
37438 +
37439 + if (hcd->core_if->dma_enable) {
37440 + hc->xfer_buff =
37441 + (uint8_t *)(uintptr_t)urb->dma + urb->actual_length;
37442 +
37443 + /* For non-dword aligned case */
37444 + if (((unsigned long)hc->xfer_buff & 0x3)
37445 + && !hcd->core_if->dma_desc_enable) {
37446 + ptr = (uint8_t *) urb->buf + urb->actual_length;
37447 + }
37448 + } else {
37449 + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
37450 + }
37451 + hc->xfer_len = urb->length - urb->actual_length;
37452 + hc->xfer_count = 0;
37453 +
37454 + /*
37455 + * Set the split attributes
37456 + */
37457 + hc->do_split = 0;
37458 + if (qh->do_split) {
37459 + uint32_t hub_addr, port_addr;
37460 + hc->do_split = 1;
37461 + hc->start_pkt_count = 1;
37462 + hc->xact_pos = qtd->isoc_split_pos;
37463 + /* We don't need to do complete splits anymore */
37464 +// if(fiq_fsm_enable)
37465 + if (0)
37466 + hc->complete_split = qtd->complete_split = 0;
37467 + else
37468 + hc->complete_split = qtd->complete_split;
37469 +
37470 + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
37471 + hc->hub_addr = (uint8_t) hub_addr;
37472 + hc->port_addr = (uint8_t) port_addr;
37473 + }
37474 +
37475 + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
37476 + case UE_CONTROL:
37477 + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
37478 + switch (qtd->control_phase) {
37479 + case DWC_OTG_CONTROL_SETUP:
37480 + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
37481 + hc->do_ping = 0;
37482 + hc->ep_is_in = 0;
37483 + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
37484 + if (hcd->core_if->dma_enable) {
37485 + hc->xfer_buff =
37486 + (uint8_t *)(uintptr_t)urb->setup_dma;
37487 + } else {
37488 + hc->xfer_buff = (uint8_t *) urb->setup_packet;
37489 + }
37490 + hc->xfer_len = 8;
37491 + ptr = NULL;
37492 + break;
37493 + case DWC_OTG_CONTROL_DATA:
37494 + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
37495 + /*
37496 + * Hardware bug: small IN packets with length < 4
37497 + * cause a 4-byte write to memory. We can only catch
37498 + * the case where we know a short packet is going to be
37499 + * returned in a control transfer, as the length is
37500 + * specified in the setup packet. This is only an issue
37501 + * for drivers that insist on packing a device's various
37502 + * properties into a struct and querying them one at a
37503 + * time (uvcvideo).
37504 + * Force the use of align_buf so that the subsequent
37505 + * memcpy puts the right number of bytes in the URB's
37506 + * buffer.
37507 + */
37508 + wLength = ((uint16_t *)urb->setup_packet)[3];
37509 + if (hc->ep_is_in && wLength < 4)
37510 + ptr = hc->xfer_buff;
37511 +
37512 + hc->data_pid_start = qtd->data_toggle;
37513 + break;
37514 + case DWC_OTG_CONTROL_STATUS:
37515 + /*
37516 + * Direction is opposite of data direction or IN if no
37517 + * data.
37518 + */
37519 + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
37520 + if (urb->length == 0) {
37521 + hc->ep_is_in = 1;
37522 + } else {
37523 + hc->ep_is_in =
37524 + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
37525 + }
37526 + if (hc->ep_is_in) {
37527 + hc->do_ping = 0;
37528 + }
37529 +
37530 + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
37531 +
37532 + hc->xfer_len = 0;
37533 + if (hcd->core_if->dma_enable) {
37534 + hc->xfer_buff = (uint8_t *)
37535 + (uintptr_t)hcd->status_buf_dma;
37536 + } else {
37537 + hc->xfer_buff = (uint8_t *) hcd->status_buf;
37538 + }
37539 + ptr = NULL;
37540 + break;
37541 + }
37542 + break;
37543 + case UE_BULK:
37544 + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
37545 + break;
37546 + case UE_INTERRUPT:
37547 + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
37548 + break;
37549 + case UE_ISOCHRONOUS:
37550 + {
37551 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
37552 +
37553 + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
37554 +
37555 + if (hcd->core_if->dma_desc_enable)
37556 + break;
37557 +
37558 + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
37559 +
37560 + frame_desc->status = 0;
37561 +
37562 + if (hcd->core_if->dma_enable) {
37563 + hc->xfer_buff = (uint8_t *)(uintptr_t)urb->dma;
37564 + } else {
37565 + hc->xfer_buff = (uint8_t *) urb->buf;
37566 + }
37567 + hc->xfer_buff +=
37568 + frame_desc->offset + qtd->isoc_split_offset;
37569 + hc->xfer_len =
37570 + frame_desc->length - qtd->isoc_split_offset;
37571 +
37572 + /* For non-dword aligned buffers */
37573 + if (((unsigned long)hc->xfer_buff & 0x3)
37574 + && hcd->core_if->dma_enable) {
37575 + ptr =
37576 + (uint8_t *) urb->buf + frame_desc->offset +
37577 + qtd->isoc_split_offset;
37578 + } else
37579 + ptr = NULL;
37580 +
37581 + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
37582 + if (hc->xfer_len <= 188) {
37583 + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
37584 + } else {
37585 + hc->xact_pos =
37586 + DWC_HCSPLIT_XACTPOS_BEGIN;
37587 + }
37588 + }
37589 + }
37590 + break;
37591 + }
37592 + /* non DWORD-aligned buffer case */
37593 + if (ptr) {
37594 + uint32_t buf_size;
37595 + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
37596 + buf_size = hcd->core_if->core_params->max_transfer_size;
37597 + } else {
37598 + buf_size = 4096;
37599 + }
37600 + if (!qh->dw_align_buf) {
37601 + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(dev, buf_size,
37602 + &qh->dw_align_buf_dma);
37603 + if (!qh->dw_align_buf) {
37604 + DWC_ERROR
37605 + ("%s: Failed to allocate memory to handle "
37606 + "non-dword aligned buffer case\n",
37607 + __func__);
37608 + return;
37609 + }
37610 + }
37611 + if (!hc->ep_is_in) {
37612 + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
37613 + }
37614 + hc->align_buff = qh->dw_align_buf_dma;
37615 + } else {
37616 + hc->align_buff = 0;
37617 + }
37618 +
37619 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
37620 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
37621 + /*
37622 + * This value may be modified when the transfer is started to
37623 + * reflect the actual transfer length.
37624 + */
37625 + hc->multi_count = dwc_hb_mult(qh->maxp);
37626 + }
37627 +
37628 + if (hcd->core_if->dma_desc_enable)
37629 + hc->desc_list_addr = qh->desc_list_dma;
37630 +
37631 + dwc_otg_hc_init(hcd->core_if, hc);
37632 +
37633 + local_irq_save(flags);
37634 +
37635 + if (fiq_enable) {
37636 + local_fiq_disable();
37637 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
37638 + }
37639 +
37640 + /* Enable the top level host channel interrupt. */
37641 + intr_enable = (1 << hc->hc_num);
37642 + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
37643 +
37644 + /* Make sure host channel interrupts are enabled. */
37645 + gintmsk.b.hcintr = 1;
37646 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
37647 +
37648 + if (fiq_enable) {
37649 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
37650 + local_fiq_enable();
37651 + }
37652 +
37653 + local_irq_restore(flags);
37654 + hc->qh = qh;
37655 +}
37656 +
37657 +
37658 +/**
37659 + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
37660 + * @hcd: Pointer to the dwc_otg_hcd struct
37661 + * @qh: pointer to the endpoint's queue head
37662 + *
37663 + * Transaction start/end control flow is grafted onto the existing dwc_otg
37664 + * mechanisms, to avoid spaghettifying the functions more than they already are.
37665 + * This function's eligibility check is altered by debug parameter.
37666 + *
37667 + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
37668 + */
37669 +
37670 +int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37671 +{
37672 + if (qh->do_split) {
37673 + switch (qh->ep_type) {
37674 + case UE_CONTROL:
37675 + case UE_BULK:
37676 + if (fiq_fsm_mask & (1 << 0))
37677 + return 1;
37678 + break;
37679 + case UE_INTERRUPT:
37680 + case UE_ISOCHRONOUS:
37681 + if (fiq_fsm_mask & (1 << 1))
37682 + return 1;
37683 + break;
37684 + default:
37685 + break;
37686 + }
37687 + } else if (qh->ep_type == UE_ISOCHRONOUS) {
37688 + if (fiq_fsm_mask & (1 << 2)) {
37689 + /* ISOCH support. We test for compatibility:
37690 + * - DWORD aligned buffers
37691 + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
37692 + * If yes, then the fsm enqueue function will handle the state machine setup.
37693 + */
37694 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37695 + dwc_otg_hcd_urb_t *urb = qtd->urb;
37696 + dwc_dma_t ptr;
37697 + int i;
37698 +
37699 + if (urb->packet_count < 2)
37700 + return 0;
37701 + for (i = 0; i < urb->packet_count; i++) {
37702 + ptr = urb->dma + urb->iso_descs[i].offset;
37703 + if (ptr & 0x3)
37704 + return 0;
37705 + }
37706 + return 1;
37707 + }
37708 + }
37709 + return 0;
37710 +}
37711 +
37712 +/**
37713 + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
37714 + * @hcd: Pointer to the dwc_otg_hcd struct
37715 + * @qh: Pointer to the endpoint's queue head
37716 + *
37717 + * Periodic split transactions are transmitted modulo 188 bytes.
37718 + * This necessitates slicing data up into buckets for isochronous out
37719 + * and fixing up the DMA address for all IN transfers.
37720 + *
37721 + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
37722 + * HC buffer has been used.
37723 + */
37724 +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
37725 + {
37726 + int frame_length, i = 0;
37727 + uint8_t *ptr = NULL;
37728 + dwc_hc_t *hc = qh->channel;
37729 + struct fiq_dma_blob *blob;
37730 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
37731 +
37732 + for (i = 0; i < 6; i++) {
37733 + st->dma_info.slot_len[i] = 255;
37734 + }
37735 + st->dma_info.index = 0;
37736 + i = 0;
37737 + if (hc->ep_is_in) {
37738 + /*
37739 + * Set dma_regs to bounce buffer. FIQ will update the
37740 + * state depending on transaction progress.
37741 + * Pointer arithmetic on hcd->fiq_state->dma_base (a dma_addr_t)
37742 + * to point it to the correct offset in the allocated buffers.
37743 + */
37744 + blob = (struct fiq_dma_blob *)
37745 + (uintptr_t)hcd->fiq_state->dma_base;
37746 + st->hcdma_copy.d32 =(u32)(uintptr_t)
37747 + blob->channel[hc->hc_num].index[0].buf;
37748 +
37749 + /* Calculate the max number of CSPLITS such that the FIQ can time out
37750 + * a transaction if it fails.
37751 + */
37752 + frame_length = st->hcchar_copy.b.mps;
37753 + do {
37754 + i++;
37755 + frame_length -= 188;
37756 + } while (frame_length >= 0);
37757 + st->nrpackets = i;
37758 + return 1;
37759 + } else {
37760 + if (qh->ep_type == UE_ISOCHRONOUS) {
37761 +
37762 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37763 +
37764 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
37765 + frame_length = frame_desc->length;
37766 +
37767 + /* Virtual address for bounce buffers */
37768 + blob = hcd->fiq_dmab;
37769 +
37770 + ptr = qtd->urb->buf + frame_desc->offset;
37771 + if (frame_length == 0) {
37772 + /*
37773 + * for isochronous transactions, we must still transmit a packet
37774 + * even if the length is zero.
37775 + */
37776 + st->dma_info.slot_len[0] = 0;
37777 + st->nrpackets = 1;
37778 + } else {
37779 + do {
37780 + if (frame_length <= 188) {
37781 + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
37782 + st->dma_info.slot_len[i] = frame_length;
37783 + ptr += frame_length;
37784 + } else {
37785 + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
37786 + st->dma_info.slot_len[i] = 188;
37787 + ptr += 188;
37788 + }
37789 + i++;
37790 + frame_length -= 188;
37791 + } while (frame_length > 0);
37792 + st->nrpackets = i;
37793 + }
37794 + ptr = qtd->urb->buf + frame_desc->offset;
37795 + /*
37796 + * Point the HC at the DMA address of the bounce buffers
37797 + *
37798 + * Pointer arithmetic on hcd->fiq_state->dma_base (a
37799 + * dma_addr_t) to point it to the correct offset in the
37800 + * allocated buffers.
37801 + */
37802 + blob = (struct fiq_dma_blob *)
37803 + (uintptr_t)hcd->fiq_state->dma_base;
37804 + st->hcdma_copy.d32 = (u32)(uintptr_t)
37805 + blob->channel[hc->hc_num].index[0].buf;
37806 +
37807 + /* fixup xfersize to the actual packet size */
37808 + st->hctsiz_copy.b.pid = 0;
37809 + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
37810 + return 1;
37811 + } else {
37812 + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
37813 + return 0;
37814 + }
37815 + }
37816 +}
37817 +
37818 +/**
37819 + * fiq_fsm_np_tt_contended() - Avoid performing contended non-periodic transfers
37820 + * @hcd: Pointer to the dwc_otg_hcd struct
37821 + * @qh: Pointer to the endpoint's queue head
37822 + *
37823 + * Certain hub chips don't differentiate between IN and OUT non-periodic pipes
37824 + * with the same endpoint number. If transfers get completed out of order
37825 + * (disregarding the direction token) then the hub can lock up
37826 + * or return erroneous responses.
37827 + *
37828 + * Returns 1 if initiating the transfer would cause contention, 0 otherwise.
37829 + */
37830 +int fiq_fsm_np_tt_contended(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37831 +{
37832 + int i;
37833 + struct fiq_channel_state *st;
37834 + int dev_addr = qh->channel->dev_addr;
37835 + int ep_num = qh->channel->ep_num;
37836 + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
37837 + if (i == qh->channel->hc_num)
37838 + continue;
37839 + st = &hcd->fiq_state->channel[i];
37840 + switch (st->fsm) {
37841 + case FIQ_NP_SSPLIT_STARTED:
37842 + case FIQ_NP_SSPLIT_RETRY:
37843 + case FIQ_NP_SSPLIT_PENDING:
37844 + case FIQ_NP_OUT_CSPLIT_RETRY:
37845 + case FIQ_NP_IN_CSPLIT_RETRY:
37846 + if (st->hcchar_copy.b.devaddr == dev_addr &&
37847 + st->hcchar_copy.b.epnum == ep_num)
37848 + return 1;
37849 + break;
37850 + default:
37851 + break;
37852 + }
37853 + }
37854 + return 0;
37855 +}
37856 +
37857 +/*
37858 + * Pushing a periodic request into the queue near the EOF1 point
37859 + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
37860 + * Usually, the request goes out on the bus causing a transfer but
37861 + * the core does not transfer the data to memory.
37862 + * This guard interval (in number of 60MHz clocks) is required which
37863 + * must cater for CPU latency between reading the value and enabling
37864 + * the channel.
37865 + */
37866 +#define PERIODIC_FRREM_BACKOFF 1000
37867 +
37868 +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37869 +{
37870 + dwc_hc_t *hc = qh->channel;
37871 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
37872 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37873 + int frame;
37874 + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
37875 + int xfer_len, nrpackets;
37876 + hcdma_data_t hcdma;
37877 + hfnum_data_t hfnum;
37878 +
37879 + if (st->fsm != FIQ_PASSTHROUGH)
37880 + return 0;
37881 +
37882 + st->nr_errors = 0;
37883 +
37884 + st->hcchar_copy.d32 = 0;
37885 + st->hcchar_copy.b.mps = hc->max_packet;
37886 + st->hcchar_copy.b.epdir = hc->ep_is_in;
37887 + st->hcchar_copy.b.devaddr = hc->dev_addr;
37888 + st->hcchar_copy.b.epnum = hc->ep_num;
37889 + st->hcchar_copy.b.eptype = hc->ep_type;
37890 +
37891 + st->hcintmsk_copy.b.chhltd = 1;
37892 +
37893 + frame = dwc_otg_hcd_get_frame_number(hcd);
37894 + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
37895 +
37896 + st->hcchar_copy.b.lspddev = 0;
37897 + /* Enable the channel later as a final register write. */
37898 +
37899 + st->hcsplt_copy.d32 = 0;
37900 +
37901 + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
37902 + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
37903 + /* grab the next DMA address offset from the array */
37904 + st->hcdma_copy.d32 = qtd->urb->dma;
37905 + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
37906 +
37907 + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
37908 + * the core needs to be told to send the correct number. Caution: for IN transfers,
37909 + * this is always set to the maximum size of the endpoint. */
37910 + xfer_len = st->hs_isoc_info.iso_desc[0].length;
37911 + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
37912 + if (nrpackets == 0)
37913 + nrpackets = 1;
37914 + st->hcchar_copy.b.multicnt = nrpackets;
37915 + st->hctsiz_copy.b.pktcnt = nrpackets;
37916 +
37917 + /* Initial PID also needs to be set */
37918 + if (st->hcchar_copy.b.epdir == 0) {
37919 + st->hctsiz_copy.b.xfersize = xfer_len;
37920 + switch (st->hcchar_copy.b.multicnt) {
37921 + case 1:
37922 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
37923 + break;
37924 + case 2:
37925 + case 3:
37926 + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
37927 + break;
37928 + }
37929 +
37930 + } else {
37931 + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
37932 + switch (st->hcchar_copy.b.multicnt) {
37933 + case 1:
37934 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
37935 + break;
37936 + case 2:
37937 + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
37938 + break;
37939 + case 3:
37940 + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
37941 + break;
37942 + }
37943 + }
37944 +
37945 + st->hs_isoc_info.stride = qh->interval;
37946 + st->uframe_sleeps = 0;
37947 +
37948 + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
37949 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
37950 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
37951 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
37952 + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
37953 + local_fiq_disable();
37954 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
37955 + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
37956 + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
37957 + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
37958 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
37959 + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
37960 + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
37961 + /* Prevent queueing near EOF1. Bad things happen if a periodic
37962 + * split transaction is queued very close to EOF. SOF interrupt handler
37963 + * will wake this channel at the next interrupt.
37964 + */
37965 + st->fsm = FIQ_HS_ISOC_SLEEPING;
37966 + st->uframe_sleeps = 1;
37967 + } else {
37968 + st->fsm = FIQ_HS_ISOC_TURBO;
37969 + st->hcchar_copy.b.chen = 1;
37970 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
37971 + }
37972 + mb();
37973 + st->hcchar_copy.b.chen = 0;
37974 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
37975 + local_fiq_enable();
37976 + return 0;
37977 +}
37978 +
37979 +
37980 +/**
37981 + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
37982 + * @hcd: Pointer to the dwc_otg_hcd struct
37983 + * @qh: Pointer to the endpoint's queue head
37984 + *
37985 + * This overrides the dwc_otg driver's normal method of queueing a transaction.
37986 + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
37987 + * for the nominated host channel.
37988 + *
37989 + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
37990 + * start is possible. If not, then the FIQ is left to start the transfer.
37991 + */
37992 +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37993 +{
37994 + int start_immediate = 1, i;
37995 + hfnum_data_t hfnum;
37996 + dwc_hc_t *hc = qh->channel;
37997 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
37998 + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
37999 + int hub_addr, port_addr, frame, uframe;
38000 + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
38001 +
38002 + /*
38003 + * Non-periodic channel assignments stay in the non_periodic_active queue.
38004 + * Therefore we get repeatedly called until the FIQ's done processing this channel.
38005 + */
38006 + if (qh->channel->xfer_started == 1)
38007 + return 0;
38008 +
38009 + if (st->fsm != FIQ_PASSTHROUGH) {
38010 + pr_warn_ratelimited("%s:%d: Queue called for an active channel\n", __func__, __LINE__);
38011 + return 0;
38012 + }
38013 +
38014 + qh->channel->xfer_started = 1;
38015 +
38016 + st->nr_errors = 0;
38017 +
38018 + st->hcchar_copy.d32 = 0;
38019 + st->hcchar_copy.b.mps = min_t(uint32_t, hc->xfer_len, hc->max_packet);
38020 + st->hcchar_copy.b.epdir = hc->ep_is_in;
38021 + st->hcchar_copy.b.devaddr = hc->dev_addr;
38022 + st->hcchar_copy.b.epnum = hc->ep_num;
38023 + st->hcchar_copy.b.eptype = hc->ep_type;
38024 + if (hc->ep_type & 0x1) {
38025 + if (hc->ep_is_in)
38026 + st->hcchar_copy.b.multicnt = 3;
38027 + else
38028 + /* Docs say set this to 1, but driver sets to 0! */
38029 + st->hcchar_copy.b.multicnt = 0;
38030 + } else {
38031 + st->hcchar_copy.b.multicnt = 1;
38032 + st->hcchar_copy.b.oddfrm = 0;
38033 + }
38034 + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
38035 + /* Enable the channel later as a final register write. */
38036 +
38037 + st->hcsplt_copy.d32 = 0;
38038 + if(qh->do_split) {
38039 + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
38040 + st->hcsplt_copy.b.compsplt = 0;
38041 + st->hcsplt_copy.b.spltena = 1;
38042 + // XACTPOS is for isoc-out only but needs initialising anyway.
38043 + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
38044 + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
38045 + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
38046 + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
38047 + * will update as necessary.
38048 + */
38049 + if (hc->xfer_len > 188) {
38050 + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
38051 + }
38052 + }
38053 + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
38054 + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
38055 + st->hub_addr = hub_addr;
38056 + st->port_addr = port_addr;
38057 + }
38058 +
38059 + st->hctsiz_copy.d32 = 0;
38060 + st->hctsiz_copy.b.dopng = 0;
38061 + st->hctsiz_copy.b.pid = hc->data_pid_start;
38062 +
38063 + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
38064 + hc->xfer_len = min_t(uint32_t, hc->xfer_len, hc->max_packet);
38065 + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
38066 + hc->xfer_len = 188;
38067 + }
38068 + st->hctsiz_copy.b.xfersize = hc->xfer_len;
38069 +
38070 + st->hctsiz_copy.b.pktcnt = 1;
38071 +
38072 + if (hc->ep_type & 0x1) {
38073 + /*
38074 + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
38075 + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
38076 + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
38077 + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
38078 + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
38079 + * must not touch internal driver state.
38080 + */
38081 + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
38082 + if (hc->align_buff) {
38083 + st->hcdma_copy.d32 = hc->align_buff;
38084 + } else {
38085 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38086 + }
38087 + }
38088 + } else {
38089 + if (hc->align_buff) {
38090 + st->hcdma_copy.d32 = hc->align_buff;
38091 + } else {
38092 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38093 + }
38094 + }
38095 + /* The FIQ depends upon no other interrupts being enabled except channel halt.
38096 + * Fixup channel interrupt mask. */
38097 + st->hcintmsk_copy.d32 = 0;
38098 + st->hcintmsk_copy.b.chhltd = 1;
38099 + st->hcintmsk_copy.b.ahberr = 1;
38100 +
38101 + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
38102 + * as Control puts the transfer into the non-periodic request queue and the
38103 + * non-periodic handler in the hub. Makes things lots easier.
38104 + */
38105 + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
38106 + st->hcchar_copy.b.multicnt = 0;
38107 + st->hcchar_copy.b.oddfrm = 0;
38108 + st->hcchar_copy.b.eptype = UE_CONTROL;
38109 + if (hc->align_buff) {
38110 + st->hcdma_copy.d32 = hc->align_buff;
38111 + } else {
38112 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38113 + }
38114 + }
38115 + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
38116 + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
38117 + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
38118 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38119 + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
38120 +
38121 + local_fiq_disable();
38122 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38123 +
38124 + if (hc->ep_type & 0x1) {
38125 + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
38126 + frame = (hfnum.b.frnum & ~0x7) >> 3;
38127 + uframe = hfnum.b.frnum & 0x7;
38128 + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
38129 + /* Prevent queueing near EOF1. Bad things happen if a periodic
38130 + * split transaction is queued very close to EOF.
38131 + */
38132 + start_immediate = 0;
38133 + } else if (uframe == 5) {
38134 + start_immediate = 0;
38135 + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
38136 + start_immediate = 0;
38137 + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
38138 + start_immediate = 0;
38139 + } else {
38140 + /* Search through all host channels to determine if a transaction
38141 + * is currently in progress */
38142 + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
38143 + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
38144 + continue;
38145 + switch (hcd->fiq_state->channel[i].fsm) {
38146 + /* TT is reserved for channels that are in the middle of a periodic
38147 + * split transaction.
38148 + */
38149 + case FIQ_PER_SSPLIT_STARTED:
38150 + case FIQ_PER_CSPLIT_WAIT:
38151 + case FIQ_PER_CSPLIT_NYET1:
38152 + case FIQ_PER_CSPLIT_POLL:
38153 + case FIQ_PER_ISO_OUT_ACTIVE:
38154 + case FIQ_PER_ISO_OUT_LAST:
38155 + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
38156 + hcd->fiq_state->channel[i].port_addr == port_addr) {
38157 + start_immediate = 0;
38158 + }
38159 + break;
38160 + default:
38161 + break;
38162 + }
38163 + if (!start_immediate)
38164 + break;
38165 + }
38166 + }
38167 + }
38168 + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
38169 + start_immediate = 1;
38170 +
38171 + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
38172 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
38173 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
38174 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
38175 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
38176 + switch (hc->ep_type) {
38177 + case UE_CONTROL:
38178 + case UE_BULK:
38179 + if (fiq_fsm_np_tt_contended(hcd, qh)) {
38180 + st->fsm = FIQ_NP_SSPLIT_PENDING;
38181 + start_immediate = 0;
38182 + } else {
38183 + st->fsm = FIQ_NP_SSPLIT_STARTED;
38184 + }
38185 + break;
38186 + case UE_ISOCHRONOUS:
38187 + if (hc->ep_is_in) {
38188 + if (start_immediate) {
38189 + st->fsm = FIQ_PER_SSPLIT_STARTED;
38190 + } else {
38191 + st->fsm = FIQ_PER_SSPLIT_QUEUED;
38192 + }
38193 + } else {
38194 + if (start_immediate) {
38195 + /* Single-isoc OUT packets don't require FIQ involvement */
38196 + if (st->nrpackets == 1) {
38197 + st->fsm = FIQ_PER_ISO_OUT_LAST;
38198 + } else {
38199 + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
38200 + }
38201 + } else {
38202 + st->fsm = FIQ_PER_ISO_OUT_PENDING;
38203 + }
38204 + }
38205 + break;
38206 + case UE_INTERRUPT:
38207 + if (fiq_fsm_mask & 0x8) {
38208 + if (fiq_fsm_np_tt_contended(hcd, qh)) {
38209 + st->fsm = FIQ_NP_SSPLIT_PENDING;
38210 + start_immediate = 0;
38211 + } else {
38212 + st->fsm = FIQ_NP_SSPLIT_STARTED;
38213 + }
38214 + } else if (start_immediate) {
38215 + st->fsm = FIQ_PER_SSPLIT_STARTED;
38216 + } else {
38217 + st->fsm = FIQ_PER_SSPLIT_QUEUED;
38218 + }
38219 + default:
38220 + break;
38221 + }
38222 + if (start_immediate) {
38223 + /* Set the oddfrm bit as close as possible to actual queueing */
38224 + frame = dwc_otg_hcd_get_frame_number(hcd);
38225 + st->expected_uframe = (frame + 1) & 0x3FFF;
38226 + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
38227 + st->hcchar_copy.b.chen = 1;
38228 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38229 + }
38230 + mb();
38231 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38232 + local_fiq_enable();
38233 + return 0;
38234 +}
38235 +
38236 +
38237 +/**
38238 + * This function selects transactions from the HCD transfer schedule and
38239 + * assigns them to available host channels. It is called from HCD interrupt
38240 + * handler functions.
38241 + *
38242 + * @param hcd The HCD state structure.
38243 + *
38244 + * @return The types of new transactions that were assigned to host channels.
38245 + */
38246 +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
38247 +{
38248 + dwc_list_link_t *qh_ptr;
38249 + dwc_otg_qh_t *qh;
38250 + int num_channels;
38251 + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
38252 +
38253 +#ifdef DEBUG_HOST_CHANNELS
38254 + last_sel_trans_num_per_scheduled = 0;
38255 + last_sel_trans_num_nonper_scheduled = 0;
38256 + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
38257 +#endif /* DEBUG_HOST_CHANNELS */
38258 +
38259 + /* Process entries in the periodic ready list. */
38260 + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
38261 +
38262 + while (qh_ptr != &hcd->periodic_sched_ready &&
38263 + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
38264 +
38265 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38266 +
38267 + if (microframe_schedule) {
38268 + // Make sure we leave one channel for non periodic transactions.
38269 + if (hcd->available_host_channels <= 1) {
38270 + break;
38271 + }
38272 + hcd->available_host_channels--;
38273 +#ifdef DEBUG_HOST_CHANNELS
38274 + last_sel_trans_num_per_scheduled++;
38275 +#endif /* DEBUG_HOST_CHANNELS */
38276 + }
38277 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38278 + assign_and_init_hc(hcd, qh);
38279 +
38280 + /*
38281 + * Move the QH from the periodic ready schedule to the
38282 + * periodic assigned schedule.
38283 + */
38284 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38285 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
38286 + &qh->qh_list_entry);
38287 + }
38288 +
38289 + /*
38290 + * Process entries in the inactive portion of the non-periodic
38291 + * schedule. Some free host channels may not be used if they are
38292 + * reserved for periodic transfers.
38293 + */
38294 + qh_ptr = hcd->non_periodic_sched_inactive.next;
38295 + num_channels = hcd->core_if->core_params->host_channels;
38296 + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
38297 + (microframe_schedule || hcd->non_periodic_channels <
38298 + num_channels - hcd->periodic_channels) &&
38299 + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
38300 +
38301 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38302 + /*
38303 + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
38304 + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
38305 + * cheeky devices that just hold off using NAKs
38306 + */
38307 + if (fiq_enable && nak_holdoff && qh->do_split) {
38308 + if (qh->nak_frame != 0xffff) {
38309 + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
38310 + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
38311 + if (dwc_frame_num_le(frame, next_frame)) {
38312 + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
38313 + hcd->fiq_state->next_sched_frame = next_frame;
38314 + }
38315 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38316 + continue;
38317 + } else {
38318 + qh->nak_frame = 0xFFFF;
38319 + }
38320 + }
38321 + }
38322 +
38323 + if (microframe_schedule) {
38324 + if (hcd->available_host_channels < 1) {
38325 + break;
38326 + }
38327 + hcd->available_host_channels--;
38328 +#ifdef DEBUG_HOST_CHANNELS
38329 + last_sel_trans_num_nonper_scheduled++;
38330 +#endif /* DEBUG_HOST_CHANNELS */
38331 + }
38332 +
38333 + assign_and_init_hc(hcd, qh);
38334 +
38335 + /*
38336 + * Move the QH from the non-periodic inactive schedule to the
38337 + * non-periodic active schedule.
38338 + */
38339 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38340 + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
38341 + &qh->qh_list_entry);
38342 +
38343 + if (!microframe_schedule)
38344 + hcd->non_periodic_channels++;
38345 + }
38346 + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
38347 + * stop the FIQ from kicking us. We could potentially still have elements here if we
38348 + * ran out of host channels.
38349 + */
38350 + if (fiq_enable) {
38351 + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
38352 + hcd->fiq_state->kick_np_queues = 0;
38353 + } else {
38354 + /* For each entry remaining in the NP inactive queue,
38355 + * if this a NAK'd retransmit then don't set the kick flag.
38356 + */
38357 + if(nak_holdoff) {
38358 + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
38359 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38360 + if (qh->nak_frame == 0xFFFF) {
38361 + hcd->fiq_state->kick_np_queues = 1;
38362 + }
38363 + }
38364 + }
38365 + }
38366 + }
38367 + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
38368 + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
38369 +
38370 + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
38371 + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
38372 +
38373 +
38374 +#ifdef DEBUG_HOST_CHANNELS
38375 + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
38376 +#endif /* DEBUG_HOST_CHANNELS */
38377 + return ret_val;
38378 +}
38379 +
38380 +/**
38381 + * Attempts to queue a single transaction request for a host channel
38382 + * associated with either a periodic or non-periodic transfer. This function
38383 + * assumes that there is space available in the appropriate request queue. For
38384 + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
38385 + * is available in the appropriate Tx FIFO.
38386 + *
38387 + * @param hcd The HCD state structure.
38388 + * @param hc Host channel descriptor associated with either a periodic or
38389 + * non-periodic transfer.
38390 + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
38391 + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
38392 + * transfers.
38393 + *
38394 + * @return 1 if a request is queued and more requests may be needed to
38395 + * complete the transfer, 0 if no more requests are required for this
38396 + * transfer, -1 if there is insufficient space in the Tx FIFO.
38397 + */
38398 +static int queue_transaction(dwc_otg_hcd_t * hcd,
38399 + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
38400 +{
38401 + int retval;
38402 +
38403 + if (hcd->core_if->dma_enable) {
38404 + if (hcd->core_if->dma_desc_enable) {
38405 + if (!hc->xfer_started
38406 + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
38407 + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
38408 + hc->qh->ping_state = 0;
38409 + }
38410 + } else if (!hc->xfer_started) {
38411 + if (fiq_fsm_enable && hc->error_state) {
38412 + hcd->fiq_state->channel[hc->hc_num].nr_errors =
38413 + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
38414 + hcd->fiq_state->channel[hc->hc_num].fsm =
38415 + FIQ_PASSTHROUGH_ERRORSTATE;
38416 + }
38417 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38418 + hc->qh->ping_state = 0;
38419 + }
38420 + retval = 0;
38421 + } else if (hc->halt_pending) {
38422 + /* Don't queue a request if the channel has been halted. */
38423 + retval = 0;
38424 + } else if (hc->halt_on_queue) {
38425 + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
38426 + retval = 0;
38427 + } else if (hc->do_ping) {
38428 + if (!hc->xfer_started) {
38429 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38430 + }
38431 + retval = 0;
38432 + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
38433 + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
38434 + if (!hc->xfer_started) {
38435 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38436 + retval = 1;
38437 + } else {
38438 + retval =
38439 + dwc_otg_hc_continue_transfer(hcd->core_if,
38440 + hc);
38441 + }
38442 + } else {
38443 + retval = -1;
38444 + }
38445 + } else {
38446 + if (!hc->xfer_started) {
38447 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38448 + retval = 1;
38449 + } else {
38450 + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
38451 + }
38452 + }
38453 +
38454 + return retval;
38455 +}
38456 +
38457 +/**
38458 + * Processes periodic channels for the next frame and queues transactions for
38459 + * these channels to the DWC_otg controller. After queueing transactions, the
38460 + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
38461 + * to queue as Periodic Tx FIFO or request queue space becomes available.
38462 + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
38463 + */
38464 +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
38465 +{
38466 + hptxsts_data_t tx_status;
38467 + dwc_list_link_t *qh_ptr;
38468 + dwc_otg_qh_t *qh;
38469 + int status = 0;
38470 + int no_queue_space = 0;
38471 + int no_fifo_space = 0;
38472 +
38473 + dwc_otg_host_global_regs_t *host_regs;
38474 + host_regs = hcd->core_if->host_if->host_global_regs;
38475 +
38476 + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
38477 +#ifdef DEBUG
38478 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38479 + DWC_DEBUGPL(DBG_HCDV,
38480 + " P Tx Req Queue Space Avail (before queue): %d\n",
38481 + tx_status.b.ptxqspcavail);
38482 + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
38483 + tx_status.b.ptxfspcavail);
38484 +#endif
38485 +
38486 + qh_ptr = hcd->periodic_sched_assigned.next;
38487 + while (qh_ptr != &hcd->periodic_sched_assigned) {
38488 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38489 + if (tx_status.b.ptxqspcavail == 0) {
38490 + no_queue_space = 1;
38491 + break;
38492 + }
38493 +
38494 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38495 +
38496 + // Do not send a split start transaction any later than frame .6
38497 + // Note, we have to schedule a periodic in .5 to make it go in .6
38498 + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
38499 + {
38500 + qh_ptr = qh_ptr->next;
38501 + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
38502 + continue;
38503 + }
38504 +
38505 + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
38506 + if (qh->do_split)
38507 + fiq_fsm_queue_split_transaction(hcd, qh);
38508 + else
38509 + fiq_fsm_queue_isoc_transaction(hcd, qh);
38510 + } else {
38511 +
38512 + /*
38513 + * Set a flag if we're queueing high-bandwidth in slave mode.
38514 + * The flag prevents any halts to get into the request queue in
38515 + * the middle of multiple high-bandwidth packets getting queued.
38516 + */
38517 + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
38518 + hcd->core_if->queuing_high_bandwidth = 1;
38519 + }
38520 + status = queue_transaction(hcd, qh->channel,
38521 + tx_status.b.ptxfspcavail);
38522 + if (status < 0) {
38523 + no_fifo_space = 1;
38524 + break;
38525 + }
38526 + }
38527 +
38528 + /*
38529 + * In Slave mode, stay on the current transfer until there is
38530 + * nothing more to do or the high-bandwidth request count is
38531 + * reached. In DMA mode, only need to queue one request. The
38532 + * controller automatically handles multiple packets for
38533 + * high-bandwidth transfers.
38534 + */
38535 + if (hcd->core_if->dma_enable || status == 0 ||
38536 + qh->channel->requests == qh->channel->multi_count) {
38537 + qh_ptr = qh_ptr->next;
38538 + /*
38539 + * Move the QH from the periodic assigned schedule to
38540 + * the periodic queued schedule.
38541 + */
38542 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
38543 + &qh->qh_list_entry);
38544 +
38545 + /* done queuing high bandwidth */
38546 + hcd->core_if->queuing_high_bandwidth = 0;
38547 + }
38548 + }
38549 +
38550 + if (!hcd->core_if->dma_enable) {
38551 + dwc_otg_core_global_regs_t *global_regs;
38552 + gintmsk_data_t intr_mask = {.d32 = 0 };
38553 +
38554 + global_regs = hcd->core_if->core_global_regs;
38555 + intr_mask.b.ptxfempty = 1;
38556 +#ifdef DEBUG
38557 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38558 + DWC_DEBUGPL(DBG_HCDV,
38559 + " P Tx Req Queue Space Avail (after queue): %d\n",
38560 + tx_status.b.ptxqspcavail);
38561 + DWC_DEBUGPL(DBG_HCDV,
38562 + " P Tx FIFO Space Avail (after queue): %d\n",
38563 + tx_status.b.ptxfspcavail);
38564 +#endif
38565 + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
38566 + no_queue_space || no_fifo_space) {
38567 + /*
38568 + * May need to queue more transactions as the request
38569 + * queue or Tx FIFO empties. Enable the periodic Tx
38570 + * FIFO empty interrupt. (Always use the half-empty
38571 + * level to ensure that new requests are loaded as
38572 + * soon as possible.)
38573 + */
38574 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
38575 + intr_mask.d32);
38576 + } else {
38577 + /*
38578 + * Disable the Tx FIFO empty interrupt since there are
38579 + * no more transactions that need to be queued right
38580 + * now. This function is called from interrupt
38581 + * handlers to queue more transactions as transfer
38582 + * states change.
38583 + */
38584 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
38585 + 0);
38586 + }
38587 + }
38588 +}
38589 +
38590 +/**
38591 + * Processes active non-periodic channels and queues transactions for these
38592 + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
38593 + * FIFO Empty interrupt is enabled if there are more transactions to queue as
38594 + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
38595 + * FIFO Empty interrupt is disabled.
38596 + */
38597 +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
38598 +{
38599 + gnptxsts_data_t tx_status;
38600 + dwc_list_link_t *orig_qh_ptr;
38601 + dwc_otg_qh_t *qh;
38602 + int status;
38603 + int no_queue_space = 0;
38604 + int no_fifo_space = 0;
38605 + int more_to_do = 0;
38606 +
38607 + dwc_otg_core_global_regs_t *global_regs =
38608 + hcd->core_if->core_global_regs;
38609 +
38610 + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
38611 +#ifdef DEBUG
38612 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38613 + DWC_DEBUGPL(DBG_HCDV,
38614 + " NP Tx Req Queue Space Avail (before queue): %d\n",
38615 + tx_status.b.nptxqspcavail);
38616 + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
38617 + tx_status.b.nptxfspcavail);
38618 +#endif
38619 + /*
38620 + * Keep track of the starting point. Skip over the start-of-list
38621 + * entry.
38622 + */
38623 + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
38624 + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
38625 + }
38626 + orig_qh_ptr = hcd->non_periodic_qh_ptr;
38627 +
38628 + /*
38629 + * Process once through the active list or until no more space is
38630 + * available in the request queue or the Tx FIFO.
38631 + */
38632 + do {
38633 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38634 + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
38635 + no_queue_space = 1;
38636 + break;
38637 + }
38638 +
38639 + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
38640 + qh_list_entry);
38641 +
38642 + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
38643 + fiq_fsm_queue_split_transaction(hcd, qh);
38644 + } else {
38645 + status = queue_transaction(hcd, qh->channel,
38646 + tx_status.b.nptxfspcavail);
38647 +
38648 + if (status > 0) {
38649 + more_to_do = 1;
38650 + } else if (status < 0) {
38651 + no_fifo_space = 1;
38652 + break;
38653 + }
38654 + }
38655 + /* Advance to next QH, skipping start-of-list entry. */
38656 + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
38657 + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
38658 + hcd->non_periodic_qh_ptr =
38659 + hcd->non_periodic_qh_ptr->next;
38660 + }
38661 +
38662 + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
38663 +
38664 + if (!hcd->core_if->dma_enable) {
38665 + gintmsk_data_t intr_mask = {.d32 = 0 };
38666 + intr_mask.b.nptxfempty = 1;
38667 +
38668 +#ifdef DEBUG
38669 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38670 + DWC_DEBUGPL(DBG_HCDV,
38671 + " NP Tx Req Queue Space Avail (after queue): %d\n",
38672 + tx_status.b.nptxqspcavail);
38673 + DWC_DEBUGPL(DBG_HCDV,
38674 + " NP Tx FIFO Space Avail (after queue): %d\n",
38675 + tx_status.b.nptxfspcavail);
38676 +#endif
38677 + if (more_to_do || no_queue_space || no_fifo_space) {
38678 + /*
38679 + * May need to queue more transactions as the request
38680 + * queue or Tx FIFO empties. Enable the non-periodic
38681 + * Tx FIFO empty interrupt. (Always use the half-empty
38682 + * level to ensure that new requests are loaded as
38683 + * soon as possible.)
38684 + */
38685 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
38686 + intr_mask.d32);
38687 + } else {
38688 + /*
38689 + * Disable the Tx FIFO empty interrupt since there are
38690 + * no more transactions that need to be queued right
38691 + * now. This function is called from interrupt
38692 + * handlers to queue more transactions as transfer
38693 + * states change.
38694 + */
38695 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
38696 + 0);
38697 + }
38698 + }
38699 +}
38700 +
38701 +/**
38702 + * This function processes the currently active host channels and queues
38703 + * transactions for these channels to the DWC_otg controller. It is called
38704 + * from HCD interrupt handler functions.
38705 + *
38706 + * @param hcd The HCD state structure.
38707 + * @param tr_type The type(s) of transactions to queue (non-periodic,
38708 + * periodic, or both).
38709 + */
38710 +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
38711 + dwc_otg_transaction_type_e tr_type)
38712 +{
38713 +#ifdef DEBUG_SOF
38714 + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
38715 +#endif
38716 + /* Process host channels associated with periodic transfers. */
38717 + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
38718 + tr_type == DWC_OTG_TRANSACTION_ALL) &&
38719 + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
38720 +
38721 + process_periodic_channels(hcd);
38722 + }
38723 +
38724 + /* Process host channels associated with non-periodic transfers. */
38725 + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
38726 + tr_type == DWC_OTG_TRANSACTION_ALL) {
38727 + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
38728 + process_non_periodic_channels(hcd);
38729 + } else {
38730 + /*
38731 + * Ensure NP Tx FIFO empty interrupt is disabled when
38732 + * there are no non-periodic transfers to process.
38733 + */
38734 + gintmsk_data_t gintmsk = {.d32 = 0 };
38735 + gintmsk.b.nptxfempty = 1;
38736 +
38737 + if (fiq_enable) {
38738 + local_fiq_disable();
38739 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38740 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
38741 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38742 + local_fiq_enable();
38743 + } else {
38744 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
38745 + }
38746 + }
38747 + }
38748 +}
38749 +
38750 +#ifdef DWC_HS_ELECT_TST
38751 +/*
38752 + * Quick and dirty hack to implement the HS Electrical Test
38753 + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
38754 + *
38755 + * This code was copied from our userspace app "hset". It sends a
38756 + * Get Device Descriptor control sequence in two parts, first the
38757 + * Setup packet by itself, followed some time later by the In and
38758 + * Ack packets. Rather than trying to figure out how to add this
38759 + * functionality to the normal driver code, we just hijack the
38760 + * hardware, using these two function to drive the hardware
38761 + * directly.
38762 + */
38763 +
38764 +static dwc_otg_core_global_regs_t *global_regs;
38765 +static dwc_otg_host_global_regs_t *hc_global_regs;
38766 +static dwc_otg_hc_regs_t *hc_regs;
38767 +static uint32_t *data_fifo;
38768 +
38769 +static void do_setup(void)
38770 +{
38771 + gintsts_data_t gintsts;
38772 + hctsiz_data_t hctsiz;
38773 + hcchar_data_t hcchar;
38774 + haint_data_t haint;
38775 + hcint_data_t hcint;
38776 +
38777 + /* Enable HAINTs */
38778 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
38779 +
38780 + /* Enable HCINTs */
38781 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
38782 +
38783 + /* Read GINTSTS */
38784 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38785 +
38786 + /* Read HAINT */
38787 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38788 +
38789 + /* Read HCINT */
38790 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38791 +
38792 + /* Read HCCHAR */
38793 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38794 +
38795 + /* Clear HCINT */
38796 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38797 +
38798 + /* Clear HAINT */
38799 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38800 +
38801 + /* Clear GINTSTS */
38802 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38803 +
38804 + /* Read GINTSTS */
38805 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38806 +
38807 + /*
38808 + * Send Setup packet (Get Device Descriptor)
38809 + */
38810 +
38811 + /* Make sure channel is disabled */
38812 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38813 + if (hcchar.b.chen) {
38814 + hcchar.b.chdis = 1;
38815 +// hcchar.b.chen = 1;
38816 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38817 + //sleep(1);
38818 + dwc_mdelay(1000);
38819 +
38820 + /* Read GINTSTS */
38821 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38822 +
38823 + /* Read HAINT */
38824 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38825 +
38826 + /* Read HCINT */
38827 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38828 +
38829 + /* Read HCCHAR */
38830 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38831 +
38832 + /* Clear HCINT */
38833 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38834 +
38835 + /* Clear HAINT */
38836 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38837 +
38838 + /* Clear GINTSTS */
38839 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38840 +
38841 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38842 + }
38843 +
38844 + /* Set HCTSIZ */
38845 + hctsiz.d32 = 0;
38846 + hctsiz.b.xfersize = 8;
38847 + hctsiz.b.pktcnt = 1;
38848 + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
38849 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
38850 +
38851 + /* Set HCCHAR */
38852 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38853 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
38854 + hcchar.b.epdir = 0;
38855 + hcchar.b.epnum = 0;
38856 + hcchar.b.mps = 8;
38857 + hcchar.b.chen = 1;
38858 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38859 +
38860 + /* Fill FIFO with Setup data for Get Device Descriptor */
38861 + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
38862 + DWC_WRITE_REG32(data_fifo++, 0x01000680);
38863 + DWC_WRITE_REG32(data_fifo++, 0x00080000);
38864 +
38865 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38866 +
38867 + /* Wait for host channel interrupt */
38868 + do {
38869 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38870 + } while (gintsts.b.hcintr == 0);
38871 +
38872 + /* Disable HCINTs */
38873 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
38874 +
38875 + /* Disable HAINTs */
38876 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
38877 +
38878 + /* Read HAINT */
38879 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38880 +
38881 + /* Read HCINT */
38882 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38883 +
38884 + /* Read HCCHAR */
38885 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38886 +
38887 + /* Clear HCINT */
38888 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38889 +
38890 + /* Clear HAINT */
38891 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38892 +
38893 + /* Clear GINTSTS */
38894 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38895 +
38896 + /* Read GINTSTS */
38897 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38898 +}
38899 +
38900 +static void do_in_ack(void)
38901 +{
38902 + gintsts_data_t gintsts;
38903 + hctsiz_data_t hctsiz;
38904 + hcchar_data_t hcchar;
38905 + haint_data_t haint;
38906 + hcint_data_t hcint;
38907 + host_grxsts_data_t grxsts;
38908 +
38909 + /* Enable HAINTs */
38910 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
38911 +
38912 + /* Enable HCINTs */
38913 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
38914 +
38915 + /* Read GINTSTS */
38916 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38917 +
38918 + /* Read HAINT */
38919 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38920 +
38921 + /* Read HCINT */
38922 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38923 +
38924 + /* Read HCCHAR */
38925 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38926 +
38927 + /* Clear HCINT */
38928 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38929 +
38930 + /* Clear HAINT */
38931 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38932 +
38933 + /* Clear GINTSTS */
38934 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38935 +
38936 + /* Read GINTSTS */
38937 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38938 +
38939 + /*
38940 + * Receive Control In packet
38941 + */
38942 +
38943 + /* Make sure channel is disabled */
38944 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38945 + if (hcchar.b.chen) {
38946 + hcchar.b.chdis = 1;
38947 + hcchar.b.chen = 1;
38948 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38949 + //sleep(1);
38950 + dwc_mdelay(1000);
38951 +
38952 + /* Read GINTSTS */
38953 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38954 +
38955 + /* Read HAINT */
38956 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38957 +
38958 + /* Read HCINT */
38959 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38960 +
38961 + /* Read HCCHAR */
38962 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38963 +
38964 + /* Clear HCINT */
38965 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38966 +
38967 + /* Clear HAINT */
38968 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38969 +
38970 + /* Clear GINTSTS */
38971 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38972 +
38973 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38974 + }
38975 +
38976 + /* Set HCTSIZ */
38977 + hctsiz.d32 = 0;
38978 + hctsiz.b.xfersize = 8;
38979 + hctsiz.b.pktcnt = 1;
38980 + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
38981 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
38982 +
38983 + /* Set HCCHAR */
38984 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38985 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
38986 + hcchar.b.epdir = 1;
38987 + hcchar.b.epnum = 0;
38988 + hcchar.b.mps = 8;
38989 + hcchar.b.chen = 1;
38990 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38991 +
38992 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38993 +
38994 + /* Wait for receive status queue interrupt */
38995 + do {
38996 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38997 + } while (gintsts.b.rxstsqlvl == 0);
38998 +
38999 + /* Read RXSTS */
39000 + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
39001 +
39002 + /* Clear RXSTSQLVL in GINTSTS */
39003 + gintsts.d32 = 0;
39004 + gintsts.b.rxstsqlvl = 1;
39005 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39006 +
39007 + switch (grxsts.b.pktsts) {
39008 + case DWC_GRXSTS_PKTSTS_IN:
39009 + /* Read the data into the host buffer */
39010 + if (grxsts.b.bcnt > 0) {
39011 + int i;
39012 + int word_count = (grxsts.b.bcnt + 3) / 4;
39013 +
39014 + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
39015 +
39016 + for (i = 0; i < word_count; i++) {
39017 + (void)DWC_READ_REG32(data_fifo++);
39018 + }
39019 + }
39020 + break;
39021 +
39022 + default:
39023 + break;
39024 + }
39025 +
39026 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39027 +
39028 + /* Wait for receive status queue interrupt */
39029 + do {
39030 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39031 + } while (gintsts.b.rxstsqlvl == 0);
39032 +
39033 + /* Read RXSTS */
39034 + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
39035 +
39036 + /* Clear RXSTSQLVL in GINTSTS */
39037 + gintsts.d32 = 0;
39038 + gintsts.b.rxstsqlvl = 1;
39039 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39040 +
39041 + switch (grxsts.b.pktsts) {
39042 + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
39043 + break;
39044 +
39045 + default:
39046 + break;
39047 + }
39048 +
39049 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39050 +
39051 + /* Wait for host channel interrupt */
39052 + do {
39053 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39054 + } while (gintsts.b.hcintr == 0);
39055 +
39056 + /* Read HAINT */
39057 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39058 +
39059 + /* Read HCINT */
39060 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39061 +
39062 + /* Read HCCHAR */
39063 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39064 +
39065 + /* Clear HCINT */
39066 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39067 +
39068 + /* Clear HAINT */
39069 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39070 +
39071 + /* Clear GINTSTS */
39072 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39073 +
39074 + /* Read GINTSTS */
39075 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39076 +
39077 +// usleep(100000);
39078 +// mdelay(100);
39079 + dwc_mdelay(1);
39080 +
39081 + /*
39082 + * Send handshake packet
39083 + */
39084 +
39085 + /* Read HAINT */
39086 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39087 +
39088 + /* Read HCINT */
39089 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39090 +
39091 + /* Read HCCHAR */
39092 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39093 +
39094 + /* Clear HCINT */
39095 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39096 +
39097 + /* Clear HAINT */
39098 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39099 +
39100 + /* Clear GINTSTS */
39101 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39102 +
39103 + /* Read GINTSTS */
39104 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39105 +
39106 + /* Make sure channel is disabled */
39107 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39108 + if (hcchar.b.chen) {
39109 + hcchar.b.chdis = 1;
39110 + hcchar.b.chen = 1;
39111 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39112 + //sleep(1);
39113 + dwc_mdelay(1000);
39114 +
39115 + /* Read GINTSTS */
39116 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39117 +
39118 + /* Read HAINT */
39119 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39120 +
39121 + /* Read HCINT */
39122 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39123 +
39124 + /* Read HCCHAR */
39125 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39126 +
39127 + /* Clear HCINT */
39128 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39129 +
39130 + /* Clear HAINT */
39131 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39132 +
39133 + /* Clear GINTSTS */
39134 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39135 +
39136 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39137 + }
39138 +
39139 + /* Set HCTSIZ */
39140 + hctsiz.d32 = 0;
39141 + hctsiz.b.xfersize = 0;
39142 + hctsiz.b.pktcnt = 1;
39143 + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
39144 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
39145 +
39146 + /* Set HCCHAR */
39147 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39148 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39149 + hcchar.b.epdir = 0;
39150 + hcchar.b.epnum = 0;
39151 + hcchar.b.mps = 8;
39152 + hcchar.b.chen = 1;
39153 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39154 +
39155 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39156 +
39157 + /* Wait for host channel interrupt */
39158 + do {
39159 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39160 + } while (gintsts.b.hcintr == 0);
39161 +
39162 + /* Disable HCINTs */
39163 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
39164 +
39165 + /* Disable HAINTs */
39166 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
39167 +
39168 + /* Read HAINT */
39169 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39170 +
39171 + /* Read HCINT */
39172 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39173 +
39174 + /* Read HCCHAR */
39175 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39176 +
39177 + /* Clear HCINT */
39178 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39179 +
39180 + /* Clear HAINT */
39181 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39182 +
39183 + /* Clear GINTSTS */
39184 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39185 +
39186 + /* Read GINTSTS */
39187 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39188 +}
39189 +#endif
39190 +
39191 +/** Handles hub class-specific requests. */
39192 +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
39193 + uint16_t typeReq,
39194 + uint16_t wValue,
39195 + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
39196 +{
39197 + int retval = 0;
39198 +
39199 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
39200 + usb_hub_descriptor_t *hub_desc;
39201 + hprt0_data_t hprt0 = {.d32 = 0 };
39202 +
39203 + uint32_t port_status;
39204 +
39205 + switch (typeReq) {
39206 + case UCR_CLEAR_HUB_FEATURE:
39207 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39208 + "ClearHubFeature 0x%x\n", wValue);
39209 + switch (wValue) {
39210 + case UHF_C_HUB_LOCAL_POWER:
39211 + case UHF_C_HUB_OVER_CURRENT:
39212 + /* Nothing required here */
39213 + break;
39214 + default:
39215 + retval = -DWC_E_INVALID;
39216 + DWC_ERROR("DWC OTG HCD - "
39217 + "ClearHubFeature request %xh unknown\n",
39218 + wValue);
39219 + }
39220 + break;
39221 + case UCR_CLEAR_PORT_FEATURE:
39222 +#ifdef CONFIG_USB_DWC_OTG_LPM
39223 + if (wValue != UHF_PORT_L1)
39224 +#endif
39225 + if (!wIndex || wIndex > 1)
39226 + goto error;
39227 +
39228 + switch (wValue) {
39229 + case UHF_PORT_ENABLE:
39230 + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
39231 + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
39232 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39233 + hprt0.b.prtena = 1;
39234 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39235 + break;
39236 + case UHF_PORT_SUSPEND:
39237 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39238 + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
39239 +
39240 + if (core_if->power_down == 2) {
39241 + dwc_otg_host_hibernation_restore(core_if, 0, 0);
39242 + } else {
39243 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
39244 + dwc_mdelay(5);
39245 +
39246 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39247 + hprt0.b.prtres = 1;
39248 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39249 + hprt0.b.prtsusp = 0;
39250 + /* Clear Resume bit */
39251 + dwc_mdelay(100);
39252 + hprt0.b.prtres = 0;
39253 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39254 + }
39255 + break;
39256 +#ifdef CONFIG_USB_DWC_OTG_LPM
39257 + case UHF_PORT_L1:
39258 + {
39259 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39260 + glpmcfg_data_t lpmcfg = {.d32 = 0 };
39261 +
39262 + lpmcfg.d32 =
39263 + DWC_READ_REG32(&core_if->
39264 + core_global_regs->glpmcfg);
39265 + lpmcfg.b.en_utmi_sleep = 0;
39266 + lpmcfg.b.hird_thres &= (~(1 << 4));
39267 + lpmcfg.b.prt_sleep_sts = 1;
39268 + DWC_WRITE_REG32(&core_if->
39269 + core_global_regs->glpmcfg,
39270 + lpmcfg.d32);
39271 +
39272 + /* Clear Enbl_L1Gating bit. */
39273 + pcgcctl.b.enbl_sleep_gating = 1;
39274 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
39275 + 0);
39276 +
39277 + dwc_mdelay(5);
39278 +
39279 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39280 + hprt0.b.prtres = 1;
39281 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39282 + hprt0.d32);
39283 + /* This bit will be cleared in wakeup interrupt handle */
39284 + break;
39285 + }
39286 +#endif
39287 + case UHF_PORT_POWER:
39288 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39289 + "ClearPortFeature USB_PORT_FEAT_POWER\n");
39290 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39291 + hprt0.b.prtpwr = 0;
39292 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39293 + break;
39294 + case UHF_PORT_INDICATOR:
39295 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39296 + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
39297 + /* Port inidicator not supported */
39298 + break;
39299 + case UHF_C_PORT_CONNECTION:
39300 + /* Clears drivers internal connect status change
39301 + * flag */
39302 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39303 + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
39304 + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
39305 + break;
39306 + case UHF_C_PORT_RESET:
39307 + /* Clears the driver's internal Port Reset Change
39308 + * flag */
39309 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39310 + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
39311 + dwc_otg_hcd->flags.b.port_reset_change = 0;
39312 + break;
39313 + case UHF_C_PORT_ENABLE:
39314 + /* Clears the driver's internal Port
39315 + * Enable/Disable Change flag */
39316 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39317 + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
39318 + dwc_otg_hcd->flags.b.port_enable_change = 0;
39319 + break;
39320 + case UHF_C_PORT_SUSPEND:
39321 + /* Clears the driver's internal Port Suspend
39322 + * Change flag, which is set when resume signaling on
39323 + * the host port is complete */
39324 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39325 + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
39326 + dwc_otg_hcd->flags.b.port_suspend_change = 0;
39327 + break;
39328 +#ifdef CONFIG_USB_DWC_OTG_LPM
39329 + case UHF_C_PORT_L1:
39330 + dwc_otg_hcd->flags.b.port_l1_change = 0;
39331 + break;
39332 +#endif
39333 + case UHF_C_PORT_OVER_CURRENT:
39334 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39335 + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
39336 + dwc_otg_hcd->flags.b.port_over_current_change = 0;
39337 + break;
39338 + default:
39339 + retval = -DWC_E_INVALID;
39340 + DWC_ERROR("DWC OTG HCD - "
39341 + "ClearPortFeature request %xh "
39342 + "unknown or unsupported\n", wValue);
39343 + }
39344 + break;
39345 + case UCR_GET_HUB_DESCRIPTOR:
39346 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39347 + "GetHubDescriptor\n");
39348 + hub_desc = (usb_hub_descriptor_t *) buf;
39349 + hub_desc->bDescLength = 9;
39350 + hub_desc->bDescriptorType = 0x29;
39351 + hub_desc->bNbrPorts = 1;
39352 + USETW(hub_desc->wHubCharacteristics, 0x08);
39353 + hub_desc->bPwrOn2PwrGood = 1;
39354 + hub_desc->bHubContrCurrent = 0;
39355 + hub_desc->DeviceRemovable[0] = 0;
39356 + hub_desc->DeviceRemovable[1] = 0xff;
39357 + break;
39358 + case UCR_GET_HUB_STATUS:
39359 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39360 + "GetHubStatus\n");
39361 + DWC_MEMSET(buf, 0, 4);
39362 + break;
39363 + case UCR_GET_PORT_STATUS:
39364 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39365 + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
39366 + wIndex, dwc_otg_hcd->flags.d32);
39367 + if (!wIndex || wIndex > 1)
39368 + goto error;
39369 +
39370 + port_status = 0;
39371 +
39372 + if (dwc_otg_hcd->flags.b.port_connect_status_change)
39373 + port_status |= (1 << UHF_C_PORT_CONNECTION);
39374 +
39375 + if (dwc_otg_hcd->flags.b.port_enable_change)
39376 + port_status |= (1 << UHF_C_PORT_ENABLE);
39377 +
39378 + if (dwc_otg_hcd->flags.b.port_suspend_change)
39379 + port_status |= (1 << UHF_C_PORT_SUSPEND);
39380 +
39381 + if (dwc_otg_hcd->flags.b.port_l1_change)
39382 + port_status |= (1 << UHF_C_PORT_L1);
39383 +
39384 + if (dwc_otg_hcd->flags.b.port_reset_change) {
39385 + port_status |= (1 << UHF_C_PORT_RESET);
39386 + }
39387 +
39388 + if (dwc_otg_hcd->flags.b.port_over_current_change) {
39389 + DWC_WARN("Overcurrent change detected\n");
39390 + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
39391 + }
39392 +
39393 + if (!dwc_otg_hcd->flags.b.port_connect_status) {
39394 + /*
39395 + * The port is disconnected, which means the core is
39396 + * either in device mode or it soon will be. Just
39397 + * return 0's for the remainder of the port status
39398 + * since the port register can't be read if the core
39399 + * is in device mode.
39400 + */
39401 + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
39402 + break;
39403 + }
39404 +
39405 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
39406 + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
39407 +
39408 + if (hprt0.b.prtconnsts)
39409 + port_status |= (1 << UHF_PORT_CONNECTION);
39410 +
39411 + if (hprt0.b.prtena)
39412 + port_status |= (1 << UHF_PORT_ENABLE);
39413 +
39414 + if (hprt0.b.prtsusp)
39415 + port_status |= (1 << UHF_PORT_SUSPEND);
39416 +
39417 + if (hprt0.b.prtovrcurract)
39418 + port_status |= (1 << UHF_PORT_OVER_CURRENT);
39419 +
39420 + if (hprt0.b.prtrst)
39421 + port_status |= (1 << UHF_PORT_RESET);
39422 +
39423 + if (hprt0.b.prtpwr)
39424 + port_status |= (1 << UHF_PORT_POWER);
39425 +
39426 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
39427 + port_status |= (1 << UHF_PORT_HIGH_SPEED);
39428 + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
39429 + port_status |= (1 << UHF_PORT_LOW_SPEED);
39430 +
39431 + if (hprt0.b.prttstctl)
39432 + port_status |= (1 << UHF_PORT_TEST);
39433 + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
39434 + port_status |= (1 << UHF_PORT_L1);
39435 + }
39436 + /*
39437 + For Synopsys HW emulation of Power down wkup_control asserts the
39438 + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
39439 + We intentionally tell the software that port is in L2Suspend state.
39440 + Only for STE.
39441 + */
39442 + if ((core_if->power_down == 2)
39443 + && (core_if->hibernation_suspend == 1)) {
39444 + port_status |= (1 << UHF_PORT_SUSPEND);
39445 + }
39446 + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
39447 +
39448 + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
39449 +
39450 + break;
39451 + case UCR_SET_HUB_FEATURE:
39452 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39453 + "SetHubFeature\n");
39454 + /* No HUB features supported */
39455 + break;
39456 + case UCR_SET_PORT_FEATURE:
39457 + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
39458 + goto error;
39459 +
39460 + if (!dwc_otg_hcd->flags.b.port_connect_status) {
39461 + /*
39462 + * The port is disconnected, which means the core is
39463 + * either in device mode or it soon will be. Just
39464 + * return without doing anything since the port
39465 + * register can't be written if the core is in device
39466 + * mode.
39467 + */
39468 + break;
39469 + }
39470 +
39471 + switch (wValue) {
39472 + case UHF_PORT_SUSPEND:
39473 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39474 + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
39475 + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
39476 + goto error;
39477 + }
39478 + if (core_if->power_down == 2) {
39479 + int timeout = 300;
39480 + dwc_irqflags_t flags;
39481 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39482 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
39483 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
39484 +#ifdef DWC_DEV_SRPCAP
39485 + int32_t otg_cap_param = core_if->core_params->otg_cap;
39486 +#endif
39487 + DWC_PRINTF("Preparing for complete power-off\n");
39488 +
39489 + /* Save registers before hibernation */
39490 + dwc_otg_save_global_regs(core_if);
39491 + dwc_otg_save_host_regs(core_if);
39492 +
39493 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39494 + hprt0.b.prtsusp = 1;
39495 + hprt0.b.prtena = 0;
39496 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39497 + /* Spin hprt0.b.prtsusp to became 1 */
39498 + do {
39499 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39500 + if (hprt0.b.prtsusp) {
39501 + break;
39502 + }
39503 + dwc_mdelay(1);
39504 + } while (--timeout);
39505 + if (!timeout) {
39506 + DWC_WARN("Suspend wasn't genereted\n");
39507 + }
39508 + dwc_udelay(10);
39509 +
39510 + /*
39511 + * We need to disable interrupts to prevent servicing of any IRQ
39512 + * during going to hibernation
39513 + */
39514 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
39515 + core_if->lx_state = DWC_OTG_L2;
39516 +#ifdef DWC_DEV_SRPCAP
39517 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39518 + hprt0.b.prtpwr = 0;
39519 + hprt0.b.prtena = 0;
39520 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39521 + hprt0.d32);
39522 +#endif
39523 + gusbcfg.d32 =
39524 + DWC_READ_REG32(&core_if->core_global_regs->
39525 + gusbcfg);
39526 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
39527 + /* ULPI interface */
39528 + /* Suspend the Phy Clock */
39529 + pcgcctl.d32 = 0;
39530 + pcgcctl.b.stoppclk = 1;
39531 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
39532 + pcgcctl.d32);
39533 + dwc_udelay(10);
39534 + gpwrdn.b.pmuactv = 1;
39535 + DWC_MODIFY_REG32(&core_if->
39536 + core_global_regs->
39537 + gpwrdn, 0, gpwrdn.d32);
39538 + } else {
39539 + /* UTMI+ Interface */
39540 + gpwrdn.b.pmuactv = 1;
39541 + DWC_MODIFY_REG32(&core_if->
39542 + core_global_regs->
39543 + gpwrdn, 0, gpwrdn.d32);
39544 + dwc_udelay(10);
39545 + pcgcctl.b.stoppclk = 1;
39546 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
39547 + dwc_udelay(10);
39548 + }
39549 +#ifdef DWC_DEV_SRPCAP
39550 + gpwrdn.d32 = 0;
39551 + gpwrdn.b.dis_vbus = 1;
39552 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39553 + gpwrdn, 0, gpwrdn.d32);
39554 +#endif
39555 + gpwrdn.d32 = 0;
39556 + gpwrdn.b.pmuintsel = 1;
39557 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39558 + gpwrdn, 0, gpwrdn.d32);
39559 + dwc_udelay(10);
39560 +
39561 + gpwrdn.d32 = 0;
39562 +#ifdef DWC_DEV_SRPCAP
39563 + gpwrdn.b.srp_det_msk = 1;
39564 +#endif
39565 + gpwrdn.b.disconn_det_msk = 1;
39566 + gpwrdn.b.lnstchng_msk = 1;
39567 + gpwrdn.b.sts_chngint_msk = 1;
39568 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39569 + gpwrdn, 0, gpwrdn.d32);
39570 + dwc_udelay(10);
39571 +
39572 + /* Enable Power Down Clamp and all interrupts in GPWRDN */
39573 + gpwrdn.d32 = 0;
39574 + gpwrdn.b.pwrdnclmp = 1;
39575 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39576 + gpwrdn, 0, gpwrdn.d32);
39577 + dwc_udelay(10);
39578 +
39579 + /* Switch off VDD */
39580 + gpwrdn.d32 = 0;
39581 + gpwrdn.b.pwrdnswtch = 1;
39582 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39583 + gpwrdn, 0, gpwrdn.d32);
39584 +
39585 +#ifdef DWC_DEV_SRPCAP
39586 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
39587 + {
39588 + core_if->pwron_timer_started = 1;
39589 + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
39590 + }
39591 +#endif
39592 + /* Save gpwrdn register for further usage if stschng interrupt */
39593 + core_if->gr_backup->gpwrdn_local =
39594 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
39595 +
39596 + /* Set flag to indicate that we are in hibernation */
39597 + core_if->hibernation_suspend = 1;
39598 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
39599 +
39600 + DWC_PRINTF("Host hibernation completed\n");
39601 + // Exit from case statement
39602 + break;
39603 +
39604 + }
39605 + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
39606 + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
39607 + gotgctl_data_t gotgctl = {.d32 = 0 };
39608 + gotgctl.b.hstsethnpen = 1;
39609 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39610 + gotgctl, 0, gotgctl.d32);
39611 + core_if->op_state = A_SUSPEND;
39612 + }
39613 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39614 + hprt0.b.prtsusp = 1;
39615 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39616 + {
39617 + dwc_irqflags_t flags;
39618 + /* Update lx_state */
39619 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
39620 + core_if->lx_state = DWC_OTG_L2;
39621 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
39622 + }
39623 + /* Suspend the Phy Clock */
39624 + {
39625 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39626 + pcgcctl.b.stoppclk = 1;
39627 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
39628 + pcgcctl.d32);
39629 + dwc_udelay(10);
39630 + }
39631 +
39632 + /* For HNP the bus must be suspended for at least 200ms. */
39633 + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
39634 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39635 + pcgcctl.b.stoppclk = 1;
39636 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
39637 + dwc_mdelay(200);
39638 + }
39639 +
39640 + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
39641 +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
39642 + if (core_if->adp_enable) {
39643 + gotgctl_data_t gotgctl = {.d32 = 0 };
39644 + gpwrdn_data_t gpwrdn;
39645 +
39646 + while (gotgctl.b.asesvld == 1) {
39647 + gotgctl.d32 =
39648 + DWC_READ_REG32(&core_if->
39649 + core_global_regs->
39650 + gotgctl);
39651 + dwc_mdelay(100);
39652 + }
39653 +
39654 + /* Enable Power Down Logic */
39655 + gpwrdn.d32 = 0;
39656 + gpwrdn.b.pmuactv = 1;
39657 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39658 + gpwrdn, 0, gpwrdn.d32);
39659 +
39660 + /* Unmask SRP detected interrupt from Power Down Logic */
39661 + gpwrdn.d32 = 0;
39662 + gpwrdn.b.srp_det_msk = 1;
39663 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39664 + gpwrdn, 0, gpwrdn.d32);
39665 +
39666 + dwc_otg_adp_probe_start(core_if);
39667 + }
39668 +#endif
39669 + break;
39670 + case UHF_PORT_POWER:
39671 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39672 + "SetPortFeature - USB_PORT_FEAT_POWER\n");
39673 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39674 + hprt0.b.prtpwr = 1;
39675 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39676 + break;
39677 + case UHF_PORT_RESET:
39678 + if ((core_if->power_down == 2)
39679 + && (core_if->hibernation_suspend == 1)) {
39680 + /* If we are going to exit from Hibernated
39681 + * state via USB RESET.
39682 + */
39683 + dwc_otg_host_hibernation_restore(core_if, 0, 1);
39684 + } else {
39685 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39686 +
39687 + DWC_DEBUGPL(DBG_HCD,
39688 + "DWC OTG HCD HUB CONTROL - "
39689 + "SetPortFeature - USB_PORT_FEAT_RESET\n");
39690 + {
39691 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39692 + pcgcctl.b.enbl_sleep_gating = 1;
39693 + pcgcctl.b.stoppclk = 1;
39694 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
39695 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
39696 + }
39697 +#ifdef CONFIG_USB_DWC_OTG_LPM
39698 + {
39699 + glpmcfg_data_t lpmcfg;
39700 + lpmcfg.d32 =
39701 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
39702 + if (lpmcfg.b.prt_sleep_sts) {
39703 + lpmcfg.b.en_utmi_sleep = 0;
39704 + lpmcfg.b.hird_thres &= (~(1 << 4));
39705 + DWC_WRITE_REG32
39706 + (&core_if->core_global_regs->glpmcfg,
39707 + lpmcfg.d32);
39708 + dwc_mdelay(1);
39709 + }
39710 + }
39711 +#endif
39712 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39713 + /* Clear suspend bit if resetting from suspended state. */
39714 + hprt0.b.prtsusp = 0;
39715 + /* When B-Host the Port reset bit is set in
39716 + * the Start HCD Callback function, so that
39717 + * the reset is started within 1ms of the HNP
39718 + * success interrupt. */
39719 + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
39720 + hprt0.b.prtpwr = 1;
39721 + hprt0.b.prtrst = 1;
39722 + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
39723 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39724 + hprt0.d32);
39725 + }
39726 + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
39727 + dwc_mdelay(60);
39728 + hprt0.b.prtrst = 0;
39729 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39730 + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
39731 + }
39732 + break;
39733 +#ifdef DWC_HS_ELECT_TST
39734 + case UHF_PORT_TEST:
39735 + {
39736 + uint32_t t;
39737 + gintmsk_data_t gintmsk;
39738 +
39739 + t = (wIndex >> 8); /* MSB wIndex USB */
39740 + DWC_DEBUGPL(DBG_HCD,
39741 + "DWC OTG HCD HUB CONTROL - "
39742 + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
39743 + t);
39744 + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
39745 + if (t < 6) {
39746 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39747 + hprt0.b.prttstctl = t;
39748 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39749 + hprt0.d32);
39750 + } else {
39751 + /* Setup global vars with reg addresses (quick and
39752 + * dirty hack, should be cleaned up)
39753 + */
39754 + global_regs = core_if->core_global_regs;
39755 + hc_global_regs =
39756 + core_if->host_if->host_global_regs;
39757 + hc_regs =
39758 + (dwc_otg_hc_regs_t *) ((char *)
39759 + global_regs +
39760 + 0x500);
39761 + data_fifo =
39762 + (uint32_t *) ((char *)global_regs +
39763 + 0x1000);
39764 +
39765 + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
39766 + /* Save current interrupt mask */
39767 + gintmsk.d32 =
39768 + DWC_READ_REG32
39769 + (&global_regs->gintmsk);
39770 +
39771 + /* Disable all interrupts while we muck with
39772 + * the hardware directly
39773 + */
39774 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39775 +
39776 + /* 15 second delay per the test spec */
39777 + dwc_mdelay(15000);
39778 +
39779 + /* Drive suspend on the root port */
39780 + hprt0.d32 =
39781 + dwc_otg_read_hprt0(core_if);
39782 + hprt0.b.prtsusp = 1;
39783 + hprt0.b.prtres = 0;
39784 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39785 +
39786 + /* 15 second delay per the test spec */
39787 + dwc_mdelay(15000);
39788 +
39789 + /* Drive resume on the root port */
39790 + hprt0.d32 =
39791 + dwc_otg_read_hprt0(core_if);
39792 + hprt0.b.prtsusp = 0;
39793 + hprt0.b.prtres = 1;
39794 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39795 + dwc_mdelay(100);
39796 +
39797 + /* Clear the resume bit */
39798 + hprt0.b.prtres = 0;
39799 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39800 +
39801 + /* Restore interrupts */
39802 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
39803 + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
39804 + /* Save current interrupt mask */
39805 + gintmsk.d32 =
39806 + DWC_READ_REG32
39807 + (&global_regs->gintmsk);
39808 +
39809 + /* Disable all interrupts while we muck with
39810 + * the hardware directly
39811 + */
39812 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39813 +
39814 + /* 15 second delay per the test spec */
39815 + dwc_mdelay(15000);
39816 +
39817 + /* Send the Setup packet */
39818 + do_setup();
39819 +
39820 + /* 15 second delay so nothing else happens for awhile */
39821 + dwc_mdelay(15000);
39822 +
39823 + /* Restore interrupts */
39824 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
39825 + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
39826 + /* Save current interrupt mask */
39827 + gintmsk.d32 =
39828 + DWC_READ_REG32
39829 + (&global_regs->gintmsk);
39830 +
39831 + /* Disable all interrupts while we muck with
39832 + * the hardware directly
39833 + */
39834 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39835 +
39836 + /* Send the Setup packet */
39837 + do_setup();
39838 +
39839 + /* 15 second delay so nothing else happens for awhile */
39840 + dwc_mdelay(15000);
39841 +
39842 + /* Send the In and Ack packets */
39843 + do_in_ack();
39844 +
39845 + /* 15 second delay so nothing else happens for awhile */
39846 + dwc_mdelay(15000);
39847 +
39848 + /* Restore interrupts */
39849 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
39850 + }
39851 + }
39852 + break;
39853 + }
39854 +#endif /* DWC_HS_ELECT_TST */
39855 +
39856 + case UHF_PORT_INDICATOR:
39857 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39858 + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
39859 + /* Not supported */
39860 + break;
39861 + default:
39862 + retval = -DWC_E_INVALID;
39863 + DWC_ERROR("DWC OTG HCD - "
39864 + "SetPortFeature request %xh "
39865 + "unknown or unsupported\n", wValue);
39866 + break;
39867 + }
39868 + break;
39869 +#ifdef CONFIG_USB_DWC_OTG_LPM
39870 + case UCR_SET_AND_TEST_PORT_FEATURE:
39871 + if (wValue != UHF_PORT_L1) {
39872 + goto error;
39873 + }
39874 + {
39875 + int portnum, hird, devaddr, remwake;
39876 + glpmcfg_data_t lpmcfg;
39877 + uint32_t time_usecs;
39878 + gintsts_data_t gintsts;
39879 + gintmsk_data_t gintmsk;
39880 +
39881 + if (!dwc_otg_get_param_lpm_enable(core_if)) {
39882 + goto error;
39883 + }
39884 + if (wValue != UHF_PORT_L1 || wLength != 1) {
39885 + goto error;
39886 + }
39887 + /* Check if the port currently is in SLEEP state */
39888 + lpmcfg.d32 =
39889 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
39890 + if (lpmcfg.b.prt_sleep_sts) {
39891 + DWC_INFO("Port is already in sleep mode\n");
39892 + buf[0] = 0; /* Return success */
39893 + break;
39894 + }
39895 +
39896 + portnum = wIndex & 0xf;
39897 + hird = (wIndex >> 4) & 0xf;
39898 + devaddr = (wIndex >> 8) & 0x7f;
39899 + remwake = (wIndex >> 15);
39900 +
39901 + if (portnum != 1) {
39902 + retval = -DWC_E_INVALID;
39903 + DWC_WARN
39904 + ("Wrong port number(%d) in SetandTestPortFeature request\n",
39905 + portnum);
39906 + break;
39907 + }
39908 +
39909 + DWC_PRINTF
39910 + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
39911 + portnum, hird, devaddr, remwake);
39912 + /* Disable LPM interrupt */
39913 + gintmsk.d32 = 0;
39914 + gintmsk.b.lpmtranrcvd = 1;
39915 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
39916 + gintmsk.d32, 0);
39917 +
39918 + if (dwc_otg_hcd_send_lpm
39919 + (dwc_otg_hcd, devaddr, hird, remwake)) {
39920 + retval = -DWC_E_INVALID;
39921 + break;
39922 + }
39923 +
39924 + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
39925 + /* We will consider timeout if time_usecs microseconds pass,
39926 + * and we don't receive LPM transaction status.
39927 + * After receiving non-error responce(ACK/NYET/STALL) from device,
39928 + * core will set lpmtranrcvd bit.
39929 + */
39930 + do {
39931 + gintsts.d32 =
39932 + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
39933 + if (gintsts.b.lpmtranrcvd) {
39934 + break;
39935 + }
39936 + dwc_udelay(1);
39937 + } while (--time_usecs);
39938 + /* lpm_int bit will be cleared in LPM interrupt handler */
39939 +
39940 + /* Now fill status
39941 + * 0x00 - Success
39942 + * 0x10 - NYET
39943 + * 0x11 - Timeout
39944 + */
39945 + if (!gintsts.b.lpmtranrcvd) {
39946 + buf[0] = 0x3; /* Completion code is Timeout */
39947 + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
39948 + } else {
39949 + lpmcfg.d32 =
39950 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
39951 + if (lpmcfg.b.lpm_resp == 0x3) {
39952 + /* ACK responce from the device */
39953 + buf[0] = 0x00; /* Success */
39954 + } else if (lpmcfg.b.lpm_resp == 0x2) {
39955 + /* NYET responce from the device */
39956 + buf[0] = 0x2;
39957 + } else {
39958 + /* Otherwise responce with Timeout */
39959 + buf[0] = 0x3;
39960 + }
39961 + }
39962 + DWC_PRINTF("Device responce to LPM trans is %x\n",
39963 + lpmcfg.b.lpm_resp);
39964 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
39965 + gintmsk.d32);
39966 +
39967 + break;
39968 + }
39969 +#endif /* CONFIG_USB_DWC_OTG_LPM */
39970 + default:
39971 +error:
39972 + retval = -DWC_E_INVALID;
39973 + DWC_WARN("DWC OTG HCD - "
39974 + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
39975 + typeReq, wIndex, wValue);
39976 + break;
39977 + }
39978 +
39979 + return retval;
39980 +}
39981 +
39982 +#ifdef CONFIG_USB_DWC_OTG_LPM
39983 +/** Returns index of host channel to perform LPM transaction. */
39984 +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
39985 +{
39986 + dwc_otg_core_if_t *core_if = hcd->core_if;
39987 + dwc_hc_t *hc;
39988 + hcchar_data_t hcchar;
39989 + gintmsk_data_t gintmsk = {.d32 = 0 };
39990 +
39991 + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
39992 + DWC_PRINTF("No free channel to select for LPM transaction\n");
39993 + return -1;
39994 + }
39995 +
39996 + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
39997 +
39998 + /* Mask host channel interrupts. */
39999 + gintmsk.b.hcintr = 1;
40000 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
40001 +
40002 + /* Fill fields that core needs for LPM transaction */
40003 + hcchar.b.devaddr = devaddr;
40004 + hcchar.b.epnum = 0;
40005 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
40006 + hcchar.b.mps = 64;
40007 + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
40008 + hcchar.b.epdir = 0; /* OUT */
40009 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
40010 + hcchar.d32);
40011 +
40012 + /* Remove the host channel from the free list. */
40013 + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
40014 +
40015 + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
40016 +
40017 + return hc->hc_num;
40018 +}
40019 +
40020 +/** Release hc after performing LPM transaction */
40021 +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
40022 +{
40023 + dwc_hc_t *hc;
40024 + glpmcfg_data_t lpmcfg;
40025 + uint8_t hc_num;
40026 +
40027 + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
40028 + hc_num = lpmcfg.b.lpm_chan_index;
40029 +
40030 + hc = hcd->hc_ptr_array[hc_num];
40031 +
40032 + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
40033 + /* Return host channel to free list */
40034 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
40035 +}
40036 +
40037 +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
40038 + uint8_t bRemoteWake)
40039 +{
40040 + glpmcfg_data_t lpmcfg;
40041 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
40042 + int channel;
40043 +
40044 + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
40045 + if (channel < 0) {
40046 + return channel;
40047 + }
40048 +
40049 + pcgcctl.b.enbl_sleep_gating = 1;
40050 + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
40051 +
40052 + /* Read LPM config register */
40053 + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
40054 +
40055 + /* Program LPM transaction fields */
40056 + lpmcfg.b.rem_wkup_en = bRemoteWake;
40057 + lpmcfg.b.hird = hird;
40058 + lpmcfg.b.hird_thres = 0x1c;
40059 + lpmcfg.b.lpm_chan_index = channel;
40060 + lpmcfg.b.en_utmi_sleep = 1;
40061 + /* Program LPM config register */
40062 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
40063 +
40064 + /* Send LPM transaction */
40065 + lpmcfg.b.send_lpm = 1;
40066 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
40067 +
40068 + return 0;
40069 +}
40070 +
40071 +#endif /* CONFIG_USB_DWC_OTG_LPM */
40072 +
40073 +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
40074 +{
40075 + int retval;
40076 +
40077 + if (port != 1) {
40078 + return -DWC_E_INVALID;
40079 + }
40080 +
40081 + retval = (hcd->flags.b.port_connect_status_change ||
40082 + hcd->flags.b.port_reset_change ||
40083 + hcd->flags.b.port_enable_change ||
40084 + hcd->flags.b.port_suspend_change ||
40085 + hcd->flags.b.port_over_current_change);
40086 +#ifdef DEBUG
40087 + if (retval) {
40088 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
40089 + " Root port status changed\n");
40090 + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
40091 + hcd->flags.b.port_connect_status_change);
40092 + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
40093 + hcd->flags.b.port_reset_change);
40094 + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
40095 + hcd->flags.b.port_enable_change);
40096 + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
40097 + hcd->flags.b.port_suspend_change);
40098 + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
40099 + hcd->flags.b.port_over_current_change);
40100 + }
40101 +#endif
40102 + return retval;
40103 +}
40104 +
40105 +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
40106 +{
40107 + hfnum_data_t hfnum;
40108 + hfnum.d32 =
40109 + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
40110 + hfnum);
40111 +
40112 +#ifdef DEBUG_SOF
40113 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
40114 + hfnum.b.frnum);
40115 +#endif
40116 + return hfnum.b.frnum;
40117 +}
40118 +
40119 +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
40120 + struct dwc_otg_hcd_function_ops *fops)
40121 +{
40122 + int retval = 0;
40123 +
40124 + hcd->fops = fops;
40125 + if (!dwc_otg_is_device_mode(hcd->core_if) &&
40126 + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
40127 + dwc_otg_hcd_reinit(hcd);
40128 + } else {
40129 + retval = -DWC_E_NO_DEVICE;
40130 + }
40131 +
40132 + return retval;
40133 +}
40134 +
40135 +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
40136 +{
40137 + return hcd->priv;
40138 +}
40139 +
40140 +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
40141 +{
40142 + hcd->priv = priv_data;
40143 +}
40144 +
40145 +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
40146 +{
40147 + return hcd->otg_port;
40148 +}
40149 +
40150 +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
40151 +{
40152 + uint32_t is_b_host;
40153 + if (hcd->core_if->op_state == B_HOST) {
40154 + is_b_host = 1;
40155 + } else {
40156 + is_b_host = 0;
40157 + }
40158 +
40159 + return is_b_host;
40160 +}
40161 +
40162 +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
40163 + int iso_desc_count, int atomic_alloc)
40164 +{
40165 + dwc_otg_hcd_urb_t *dwc_otg_urb;
40166 + uint32_t size;
40167 +
40168 + size =
40169 + sizeof(*dwc_otg_urb) +
40170 + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
40171 + if (atomic_alloc)
40172 + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
40173 + else
40174 + dwc_otg_urb = DWC_ALLOC(size);
40175 +
40176 + if (dwc_otg_urb)
40177 + dwc_otg_urb->packet_count = iso_desc_count;
40178 + else {
40179 + DWC_ERROR("**** DWC OTG HCD URB alloc - "
40180 + "%salloc of %db failed\n",
40181 + atomic_alloc?"atomic ":"", size);
40182 + }
40183 + return dwc_otg_urb;
40184 +}
40185 +
40186 +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
40187 + uint8_t dev_addr, uint8_t ep_num,
40188 + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
40189 +{
40190 + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
40191 + ep_type, ep_dir, mps);
40192 +#if 0
40193 + DWC_PRINTF
40194 + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
40195 + dev_addr, ep_num, ep_dir, ep_type, mps);
40196 +#endif
40197 +}
40198 +
40199 +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
40200 + void *urb_handle, void *buf, dwc_dma_t dma,
40201 + uint32_t buflen, void *setup_packet,
40202 + dwc_dma_t setup_dma, uint32_t flags,
40203 + uint16_t interval)
40204 +{
40205 + dwc_otg_urb->priv = urb_handle;
40206 + dwc_otg_urb->buf = buf;
40207 + dwc_otg_urb->dma = dma;
40208 + dwc_otg_urb->length = buflen;
40209 + dwc_otg_urb->setup_packet = setup_packet;
40210 + dwc_otg_urb->setup_dma = setup_dma;
40211 + dwc_otg_urb->flags = flags;
40212 + dwc_otg_urb->interval = interval;
40213 + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
40214 +}
40215 +
40216 +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
40217 +{
40218 + return dwc_otg_urb->status;
40219 +}
40220 +
40221 +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
40222 +{
40223 + return dwc_otg_urb->actual_length;
40224 +}
40225 +
40226 +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
40227 +{
40228 + return dwc_otg_urb->error_count;
40229 +}
40230 +
40231 +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
40232 + int desc_num, uint32_t offset,
40233 + uint32_t length)
40234 +{
40235 + dwc_otg_urb->iso_descs[desc_num].offset = offset;
40236 + dwc_otg_urb->iso_descs[desc_num].length = length;
40237 +}
40238 +
40239 +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
40240 + int desc_num)
40241 +{
40242 + return dwc_otg_urb->iso_descs[desc_num].status;
40243 +}
40244 +
40245 +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
40246 + dwc_otg_urb, int desc_num)
40247 +{
40248 + return dwc_otg_urb->iso_descs[desc_num].actual_length;
40249 +}
40250 +
40251 +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
40252 +{
40253 + int allocated = 0;
40254 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40255 +
40256 + if (qh) {
40257 + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
40258 + allocated = 1;
40259 + }
40260 + }
40261 + return allocated;
40262 +}
40263 +
40264 +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
40265 +{
40266 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40267 + int freed = 0;
40268 + DWC_ASSERT(qh, "qh is not allocated\n");
40269 +
40270 + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
40271 + freed = 1;
40272 + }
40273 +
40274 + return freed;
40275 +}
40276 +
40277 +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
40278 +{
40279 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40280 + DWC_ASSERT(qh, "qh is not allocated\n");
40281 + return qh->usecs;
40282 +}
40283 +
40284 +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
40285 +{
40286 +#ifdef DEBUG
40287 + int num_channels;
40288 + int i;
40289 + gnptxsts_data_t np_tx_status;
40290 + hptxsts_data_t p_tx_status;
40291 +
40292 + num_channels = hcd->core_if->core_params->host_channels;
40293 + DWC_PRINTF("\n");
40294 + DWC_PRINTF
40295 + ("************************************************************\n");
40296 + DWC_PRINTF("HCD State:\n");
40297 + DWC_PRINTF(" Num channels: %d\n", num_channels);
40298 + for (i = 0; i < num_channels; i++) {
40299 + dwc_hc_t *hc = hcd->hc_ptr_array[i];
40300 + DWC_PRINTF(" Channel %d:\n", i);
40301 + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
40302 + hc->dev_addr, hc->ep_num, hc->ep_is_in);
40303 + DWC_PRINTF(" speed: %d\n", hc->speed);
40304 + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
40305 + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
40306 + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
40307 + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
40308 + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
40309 + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
40310 + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
40311 + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
40312 + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
40313 + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
40314 + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
40315 + DWC_PRINTF(" do_split: %d\n", hc->do_split);
40316 + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
40317 + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
40318 + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
40319 + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
40320 + DWC_PRINTF(" requests: %d\n", hc->requests);
40321 + DWC_PRINTF(" qh: %p\n", hc->qh);
40322 + if (hc->xfer_started) {
40323 + hfnum_data_t hfnum;
40324 + hcchar_data_t hcchar;
40325 + hctsiz_data_t hctsiz;
40326 + hcint_data_t hcint;
40327 + hcintmsk_data_t hcintmsk;
40328 + hfnum.d32 =
40329 + DWC_READ_REG32(&hcd->core_if->
40330 + host_if->host_global_regs->hfnum);
40331 + hcchar.d32 =
40332 + DWC_READ_REG32(&hcd->core_if->host_if->
40333 + hc_regs[i]->hcchar);
40334 + hctsiz.d32 =
40335 + DWC_READ_REG32(&hcd->core_if->host_if->
40336 + hc_regs[i]->hctsiz);
40337 + hcint.d32 =
40338 + DWC_READ_REG32(&hcd->core_if->host_if->
40339 + hc_regs[i]->hcint);
40340 + hcintmsk.d32 =
40341 + DWC_READ_REG32(&hcd->core_if->host_if->
40342 + hc_regs[i]->hcintmsk);
40343 + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
40344 + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
40345 + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
40346 + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
40347 + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
40348 + }
40349 + if (hc->xfer_started && hc->qh) {
40350 + dwc_otg_qtd_t *qtd;
40351 + dwc_otg_hcd_urb_t *urb;
40352 +
40353 + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
40354 + if (!qtd->in_process)
40355 + break;
40356 +
40357 + urb = qtd->urb;
40358 + DWC_PRINTF(" URB Info:\n");
40359 + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
40360 + if (urb) {
40361 + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
40362 + dwc_otg_hcd_get_dev_addr(&urb->
40363 + pipe_info),
40364 + dwc_otg_hcd_get_ep_num(&urb->
40365 + pipe_info),
40366 + dwc_otg_hcd_is_pipe_in(&urb->
40367 + pipe_info) ?
40368 + "IN" : "OUT");
40369 + DWC_PRINTF(" Max packet size: %d\n",
40370 + dwc_otg_hcd_get_mps(&urb->
40371 + pipe_info));
40372 + DWC_PRINTF(" transfer_buffer: %p\n",
40373 + urb->buf);
40374 + DWC_PRINTF(" transfer_dma: %p\n",
40375 + (void *)urb->dma);
40376 + DWC_PRINTF(" transfer_buffer_length: %d\n",
40377 + urb->length);
40378 + DWC_PRINTF(" actual_length: %d\n",
40379 + urb->actual_length);
40380 + }
40381 + }
40382 + }
40383 + }
40384 + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
40385 + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
40386 + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
40387 + np_tx_status.d32 =
40388 + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
40389 + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
40390 + np_tx_status.b.nptxqspcavail);
40391 + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
40392 + np_tx_status.b.nptxfspcavail);
40393 + p_tx_status.d32 =
40394 + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
40395 + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
40396 + p_tx_status.b.ptxqspcavail);
40397 + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
40398 + dwc_otg_hcd_dump_frrem(hcd);
40399 + dwc_otg_dump_global_registers(hcd->core_if);
40400 + dwc_otg_dump_host_registers(hcd->core_if);
40401 + DWC_PRINTF
40402 + ("************************************************************\n");
40403 + DWC_PRINTF("\n");
40404 +#endif
40405 +}
40406 +
40407 +#ifdef DEBUG
40408 +void dwc_print_setup_data(uint8_t * setup)
40409 +{
40410 + int i;
40411 + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
40412 + DWC_PRINTF("Setup Data = MSB ");
40413 + for (i = 7; i >= 0; i--)
40414 + DWC_PRINTF("%02x ", setup[i]);
40415 + DWC_PRINTF("\n");
40416 + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
40417 + (setup[0] & 0x80) ? "Device-to-Host" :
40418 + "Host-to-Device");
40419 + DWC_PRINTF(" bmRequestType Type = ");
40420 + switch ((setup[0] & 0x60) >> 5) {
40421 + case 0:
40422 + DWC_PRINTF("Standard\n");
40423 + break;
40424 + case 1:
40425 + DWC_PRINTF("Class\n");
40426 + break;
40427 + case 2:
40428 + DWC_PRINTF("Vendor\n");
40429 + break;
40430 + case 3:
40431 + DWC_PRINTF("Reserved\n");
40432 + break;
40433 + }
40434 + DWC_PRINTF(" bmRequestType Recipient = ");
40435 + switch (setup[0] & 0x1f) {
40436 + case 0:
40437 + DWC_PRINTF("Device\n");
40438 + break;
40439 + case 1:
40440 + DWC_PRINTF("Interface\n");
40441 + break;
40442 + case 2:
40443 + DWC_PRINTF("Endpoint\n");
40444 + break;
40445 + case 3:
40446 + DWC_PRINTF("Other\n");
40447 + break;
40448 + default:
40449 + DWC_PRINTF("Reserved\n");
40450 + break;
40451 + }
40452 + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
40453 + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
40454 + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
40455 + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
40456 + }
40457 +}
40458 +#endif
40459 +
40460 +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
40461 +{
40462 +#if 0
40463 + DWC_PRINTF("Frame remaining at SOF:\n");
40464 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40465 + hcd->frrem_samples, hcd->frrem_accum,
40466 + (hcd->frrem_samples > 0) ?
40467 + hcd->frrem_accum / hcd->frrem_samples : 0);
40468 +
40469 + DWC_PRINTF("\n");
40470 + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
40471 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40472 + hcd->core_if->hfnum_7_samples,
40473 + hcd->core_if->hfnum_7_frrem_accum,
40474 + (hcd->core_if->hfnum_7_samples >
40475 + 0) ? hcd->core_if->hfnum_7_frrem_accum /
40476 + hcd->core_if->hfnum_7_samples : 0);
40477 + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
40478 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40479 + hcd->core_if->hfnum_0_samples,
40480 + hcd->core_if->hfnum_0_frrem_accum,
40481 + (hcd->core_if->hfnum_0_samples >
40482 + 0) ? hcd->core_if->hfnum_0_frrem_accum /
40483 + hcd->core_if->hfnum_0_samples : 0);
40484 + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
40485 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40486 + hcd->core_if->hfnum_other_samples,
40487 + hcd->core_if->hfnum_other_frrem_accum,
40488 + (hcd->core_if->hfnum_other_samples >
40489 + 0) ? hcd->core_if->hfnum_other_frrem_accum /
40490 + hcd->core_if->hfnum_other_samples : 0);
40491 +
40492 + DWC_PRINTF("\n");
40493 + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
40494 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40495 + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
40496 + (hcd->hfnum_7_samples_a > 0) ?
40497 + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
40498 + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
40499 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40500 + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
40501 + (hcd->hfnum_0_samples_a > 0) ?
40502 + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
40503 + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
40504 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40505 + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
40506 + (hcd->hfnum_other_samples_a > 0) ?
40507 + hcd->hfnum_other_frrem_accum_a /
40508 + hcd->hfnum_other_samples_a : 0);
40509 +
40510 + DWC_PRINTF("\n");
40511 + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
40512 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40513 + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
40514 + (hcd->hfnum_7_samples_b > 0) ?
40515 + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
40516 + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
40517 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40518 + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
40519 + (hcd->hfnum_0_samples_b > 0) ?
40520 + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
40521 + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
40522 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40523 + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
40524 + (hcd->hfnum_other_samples_b > 0) ?
40525 + hcd->hfnum_other_frrem_accum_b /
40526 + hcd->hfnum_other_samples_b : 0);
40527 +#endif
40528 +}
40529 +
40530 +#endif /* DWC_DEVICE_ONLY */
40531 --- /dev/null
40532 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
40533 @@ -0,0 +1,870 @@
40534 +/* ==========================================================================
40535 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
40536 + * $Revision: #58 $
40537 + * $Date: 2011/09/15 $
40538 + * $Change: 1846647 $
40539 + *
40540 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
40541 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
40542 + * otherwise expressly agreed to in writing between Synopsys and you.
40543 + *
40544 + * The Software IS NOT an item of Licensed Software or Licensed Product under
40545 + * any End User Software License Agreement or Agreement for Licensed Product
40546 + * with Synopsys or any supplement thereto. You are permitted to use and
40547 + * redistribute this Software in source and binary forms, with or without
40548 + * modification, provided that redistributions of source code must retain this
40549 + * notice. You may not view, use, disclose, copy or distribute this file or
40550 + * any information contained herein except pursuant to this license grant from
40551 + * Synopsys. If you do not agree with this notice, including the disclaimer
40552 + * below, then you are not authorized to use the Software.
40553 + *
40554 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
40555 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40556 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40557 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
40558 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40559 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40560 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40561 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40562 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40563 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40564 + * DAMAGE.
40565 + * ========================================================================== */
40566 +#ifndef DWC_DEVICE_ONLY
40567 +#ifndef __DWC_HCD_H__
40568 +#define __DWC_HCD_H__
40569 +
40570 +#include "dwc_otg_os_dep.h"
40571 +#include "usb.h"
40572 +#include "dwc_otg_hcd_if.h"
40573 +#include "dwc_otg_core_if.h"
40574 +#include "dwc_list.h"
40575 +#include "dwc_otg_cil.h"
40576 +#include "dwc_otg_fiq_fsm.h"
40577 +#include "dwc_otg_driver.h"
40578 +
40579 +
40580 +/**
40581 + * @file
40582 + *
40583 + * This file contains the structures, constants, and interfaces for
40584 + * the Host Contoller Driver (HCD).
40585 + *
40586 + * The Host Controller Driver (HCD) is responsible for translating requests
40587 + * from the USB Driver into the appropriate actions on the DWC_otg controller.
40588 + * It isolates the USBD from the specifics of the controller by providing an
40589 + * API to the USBD.
40590 + */
40591 +
40592 +struct dwc_otg_hcd_pipe_info {
40593 + uint8_t dev_addr;
40594 + uint8_t ep_num;
40595 + uint8_t pipe_type;
40596 + uint8_t pipe_dir;
40597 + uint16_t mps;
40598 +};
40599 +
40600 +struct dwc_otg_hcd_iso_packet_desc {
40601 + uint32_t offset;
40602 + uint32_t length;
40603 + uint32_t actual_length;
40604 + uint32_t status;
40605 +};
40606 +
40607 +struct dwc_otg_qtd;
40608 +
40609 +struct dwc_otg_hcd_urb {
40610 + void *priv;
40611 + struct dwc_otg_qtd *qtd;
40612 + void *buf;
40613 + dwc_dma_t dma;
40614 + void *setup_packet;
40615 + dwc_dma_t setup_dma;
40616 + uint32_t length;
40617 + uint32_t actual_length;
40618 + uint32_t status;
40619 + uint32_t error_count;
40620 + uint32_t packet_count;
40621 + uint32_t flags;
40622 + uint16_t interval;
40623 + struct dwc_otg_hcd_pipe_info pipe_info;
40624 + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
40625 +};
40626 +
40627 +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
40628 +{
40629 + return pipe->ep_num;
40630 +}
40631 +
40632 +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
40633 + *pipe)
40634 +{
40635 + return pipe->pipe_type;
40636 +}
40637 +
40638 +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
40639 +{
40640 + return pipe->mps;
40641 +}
40642 +
40643 +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
40644 + *pipe)
40645 +{
40646 + return pipe->dev_addr;
40647 +}
40648 +
40649 +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
40650 + *pipe)
40651 +{
40652 + return (pipe->pipe_type == UE_ISOCHRONOUS);
40653 +}
40654 +
40655 +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
40656 + *pipe)
40657 +{
40658 + return (pipe->pipe_type == UE_INTERRUPT);
40659 +}
40660 +
40661 +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
40662 + *pipe)
40663 +{
40664 + return (pipe->pipe_type == UE_BULK);
40665 +}
40666 +
40667 +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
40668 + *pipe)
40669 +{
40670 + return (pipe->pipe_type == UE_CONTROL);
40671 +}
40672 +
40673 +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
40674 +{
40675 + return (pipe->pipe_dir == UE_DIR_IN);
40676 +}
40677 +
40678 +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
40679 + *pipe)
40680 +{
40681 + return (!dwc_otg_hcd_is_pipe_in(pipe));
40682 +}
40683 +
40684 +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
40685 + uint8_t devaddr, uint8_t ep_num,
40686 + uint8_t pipe_type, uint8_t pipe_dir,
40687 + uint16_t mps)
40688 +{
40689 + pipe->dev_addr = devaddr;
40690 + pipe->ep_num = ep_num;
40691 + pipe->pipe_type = pipe_type;
40692 + pipe->pipe_dir = pipe_dir;
40693 + pipe->mps = mps;
40694 +}
40695 +
40696 +/**
40697 + * Phases for control transfers.
40698 + */
40699 +typedef enum dwc_otg_control_phase {
40700 + DWC_OTG_CONTROL_SETUP,
40701 + DWC_OTG_CONTROL_DATA,
40702 + DWC_OTG_CONTROL_STATUS
40703 +} dwc_otg_control_phase_e;
40704 +
40705 +/** Transaction types. */
40706 +typedef enum dwc_otg_transaction_type {
40707 + DWC_OTG_TRANSACTION_NONE = 0,
40708 + DWC_OTG_TRANSACTION_PERIODIC = 1,
40709 + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
40710 + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
40711 +} dwc_otg_transaction_type_e;
40712 +
40713 +struct dwc_otg_qh;
40714 +
40715 +/**
40716 + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
40717 + * interrupt, or isochronous transfer. A single QTD is created for each URB
40718 + * (of one of these types) submitted to the HCD. The transfer associated with
40719 + * a QTD may require one or multiple transactions.
40720 + *
40721 + * A QTD is linked to a Queue Head, which is entered in either the
40722 + * non-periodic or periodic schedule for execution. When a QTD is chosen for
40723 + * execution, some or all of its transactions may be executed. After
40724 + * execution, the state of the QTD is updated. The QTD may be retired if all
40725 + * its transactions are complete or if an error occurred. Otherwise, it
40726 + * remains in the schedule so more transactions can be executed later.
40727 + */
40728 +typedef struct dwc_otg_qtd {
40729 + /**
40730 + * Determines the PID of the next data packet for the data phase of
40731 + * control transfers. Ignored for other transfer types.<br>
40732 + * One of the following values:
40733 + * - DWC_OTG_HC_PID_DATA0
40734 + * - DWC_OTG_HC_PID_DATA1
40735 + */
40736 + uint8_t data_toggle;
40737 +
40738 + /** Current phase for control transfers (Setup, Data, or Status). */
40739 + dwc_otg_control_phase_e control_phase;
40740 +
40741 + /** Keep track of the current split type
40742 + * for FS/LS endpoints on a HS Hub */
40743 + uint8_t complete_split;
40744 +
40745 + /** How many bytes transferred during SSPLIT OUT */
40746 + uint32_t ssplit_out_xfer_count;
40747 +
40748 + /**
40749 + * Holds the number of bus errors that have occurred for a transaction
40750 + * within this transfer.
40751 + */
40752 + uint8_t error_count;
40753 +
40754 + /**
40755 + * Index of the next frame descriptor for an isochronous transfer. A
40756 + * frame descriptor describes the buffer position and length of the
40757 + * data to be transferred in the next scheduled (micro)frame of an
40758 + * isochronous transfer. It also holds status for that transaction.
40759 + * The frame index starts at 0.
40760 + */
40761 + uint16_t isoc_frame_index;
40762 +
40763 + /** Position of the ISOC split on full/low speed */
40764 + uint8_t isoc_split_pos;
40765 +
40766 + /** Position of the ISOC split in the buffer for the current frame */
40767 + uint16_t isoc_split_offset;
40768 +
40769 + /** URB for this transfer */
40770 + struct dwc_otg_hcd_urb *urb;
40771 +
40772 + struct dwc_otg_qh *qh;
40773 +
40774 + /** This list of QTDs */
40775 + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
40776 +
40777 + /** Indicates if this QTD is currently processed by HW. */
40778 + uint8_t in_process;
40779 +
40780 + /** Number of DMA descriptors for this QTD */
40781 + uint8_t n_desc;
40782 +
40783 + /**
40784 + * Last activated frame(packet) index.
40785 + * Used in Descriptor DMA mode only.
40786 + */
40787 + uint16_t isoc_frame_index_last;
40788 +
40789 +} dwc_otg_qtd_t;
40790 +
40791 +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
40792 +
40793 +/**
40794 + * A Queue Head (QH) holds the static characteristics of an endpoint and
40795 + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
40796 + * be entered in either the non-periodic or periodic schedule.
40797 + */
40798 +typedef struct dwc_otg_qh {
40799 + /**
40800 + * Endpoint type.
40801 + * One of the following values:
40802 + * - UE_CONTROL
40803 + * - UE_BULK
40804 + * - UE_INTERRUPT
40805 + * - UE_ISOCHRONOUS
40806 + */
40807 + uint8_t ep_type;
40808 + uint8_t ep_is_in;
40809 +
40810 + /** wMaxPacketSize Field of Endpoint Descriptor. */
40811 + uint16_t maxp;
40812 +
40813 + /**
40814 + * Device speed.
40815 + * One of the following values:
40816 + * - DWC_OTG_EP_SPEED_LOW
40817 + * - DWC_OTG_EP_SPEED_FULL
40818 + * - DWC_OTG_EP_SPEED_HIGH
40819 + */
40820 + uint8_t dev_speed;
40821 +
40822 + /**
40823 + * Determines the PID of the next data packet for non-control
40824 + * transfers. Ignored for control transfers.<br>
40825 + * One of the following values:
40826 + * - DWC_OTG_HC_PID_DATA0
40827 + * - DWC_OTG_HC_PID_DATA1
40828 + */
40829 + uint8_t data_toggle;
40830 +
40831 + /** Ping state if 1. */
40832 + uint8_t ping_state;
40833 +
40834 + /**
40835 + * List of QTDs for this QH.
40836 + */
40837 + struct dwc_otg_qtd_list qtd_list;
40838 +
40839 + /** Host channel currently processing transfers for this QH. */
40840 + struct dwc_hc *channel;
40841 +
40842 + /** Full/low speed endpoint on high-speed hub requires split. */
40843 + uint8_t do_split;
40844 +
40845 + /** @name Periodic schedule information */
40846 + /** @{ */
40847 +
40848 + /** Bandwidth in microseconds per (micro)frame. */
40849 + uint16_t usecs;
40850 +
40851 + /** Interval between transfers in (micro)frames. */
40852 + uint16_t interval;
40853 +
40854 + /**
40855 + * (micro)frame to initialize a periodic transfer. The transfer
40856 + * executes in the following (micro)frame.
40857 + */
40858 + uint16_t sched_frame;
40859 +
40860 + /*
40861 + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
40862 + */
40863 + uint16_t nak_frame;
40864 +
40865 + /** (micro)frame at which last start split was initialized. */
40866 + uint16_t start_split_frame;
40867 +
40868 + /** @} */
40869 +
40870 + /**
40871 + * Used instead of original buffer if
40872 + * it(physical address) is not dword-aligned.
40873 + */
40874 + uint8_t *dw_align_buf;
40875 + dwc_dma_t dw_align_buf_dma;
40876 +
40877 + /** Entry for QH in either the periodic or non-periodic schedule. */
40878 + dwc_list_link_t qh_list_entry;
40879 +
40880 + /** @name Descriptor DMA support */
40881 + /** @{ */
40882 +
40883 + /** Descriptor List. */
40884 + dwc_otg_host_dma_desc_t *desc_list;
40885 +
40886 + /** Descriptor List physical address. */
40887 + dwc_dma_t desc_list_dma;
40888 +
40889 + /**
40890 + * Xfer Bytes array.
40891 + * Each element corresponds to a descriptor and indicates
40892 + * original XferSize size value for the descriptor.
40893 + */
40894 + uint32_t *n_bytes;
40895 +
40896 + /** Actual number of transfer descriptors in a list. */
40897 + uint16_t ntd;
40898 +
40899 + /** First activated isochronous transfer descriptor index. */
40900 + uint8_t td_first;
40901 + /** Last activated isochronous transfer descriptor index. */
40902 + uint8_t td_last;
40903 +
40904 + /** @} */
40905 +
40906 +
40907 + uint16_t speed;
40908 + uint16_t frame_usecs[8];
40909 +
40910 + uint32_t skip_count;
40911 +} dwc_otg_qh_t;
40912 +
40913 +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
40914 +
40915 +typedef struct urb_tq_entry {
40916 + struct urb *urb;
40917 + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
40918 +} urb_tq_entry_t;
40919 +
40920 +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
40921 +
40922 +/**
40923 + * This structure holds the state of the HCD, including the non-periodic and
40924 + * periodic schedules.
40925 + */
40926 +struct dwc_otg_hcd {
40927 + /** The DWC otg device pointer */
40928 + struct dwc_otg_device *otg_dev;
40929 + /** DWC OTG Core Interface Layer */
40930 + dwc_otg_core_if_t *core_if;
40931 +
40932 + /** Function HCD driver callbacks */
40933 + struct dwc_otg_hcd_function_ops *fops;
40934 +
40935 + /** Internal DWC HCD Flags */
40936 + volatile union dwc_otg_hcd_internal_flags {
40937 + uint32_t d32;
40938 + struct {
40939 + unsigned port_connect_status_change:1;
40940 + unsigned port_connect_status:1;
40941 + unsigned port_reset_change:1;
40942 + unsigned port_enable_change:1;
40943 + unsigned port_suspend_change:1;
40944 + unsigned port_over_current_change:1;
40945 + unsigned port_l1_change:1;
40946 + unsigned port_speed:2;
40947 + unsigned reserved:24;
40948 + } b;
40949 + } flags;
40950 +
40951 + /**
40952 + * Inactive items in the non-periodic schedule. This is a list of
40953 + * Queue Heads. Transfers associated with these Queue Heads are not
40954 + * currently assigned to a host channel.
40955 + */
40956 + dwc_list_link_t non_periodic_sched_inactive;
40957 +
40958 + /**
40959 + * Active items in the non-periodic schedule. This is a list of
40960 + * Queue Heads. Transfers associated with these Queue Heads are
40961 + * currently assigned to a host channel.
40962 + */
40963 + dwc_list_link_t non_periodic_sched_active;
40964 +
40965 + /**
40966 + * Pointer to the next Queue Head to process in the active
40967 + * non-periodic schedule.
40968 + */
40969 + dwc_list_link_t *non_periodic_qh_ptr;
40970 +
40971 + /**
40972 + * Inactive items in the periodic schedule. This is a list of QHs for
40973 + * periodic transfers that are _not_ scheduled for the next frame.
40974 + * Each QH in the list has an interval counter that determines when it
40975 + * needs to be scheduled for execution. This scheduling mechanism
40976 + * allows only a simple calculation for periodic bandwidth used (i.e.
40977 + * must assume that all periodic transfers may need to execute in the
40978 + * same frame). However, it greatly simplifies scheduling and should
40979 + * be sufficient for the vast majority of OTG hosts, which need to
40980 + * connect to a small number of peripherals at one time.
40981 + *
40982 + * Items move from this list to periodic_sched_ready when the QH
40983 + * interval counter is 0 at SOF.
40984 + */
40985 + dwc_list_link_t periodic_sched_inactive;
40986 +
40987 + /**
40988 + * List of periodic QHs that are ready for execution in the next
40989 + * frame, but have not yet been assigned to host channels.
40990 + *
40991 + * Items move from this list to periodic_sched_assigned as host
40992 + * channels become available during the current frame.
40993 + */
40994 + dwc_list_link_t periodic_sched_ready;
40995 +
40996 + /**
40997 + * List of periodic QHs to be executed in the next frame that are
40998 + * assigned to host channels.
40999 + *
41000 + * Items move from this list to periodic_sched_queued as the
41001 + * transactions for the QH are queued to the DWC_otg controller.
41002 + */
41003 + dwc_list_link_t periodic_sched_assigned;
41004 +
41005 + /**
41006 + * List of periodic QHs that have been queued for execution.
41007 + *
41008 + * Items move from this list to either periodic_sched_inactive or
41009 + * periodic_sched_ready when the channel associated with the transfer
41010 + * is released. If the interval for the QH is 1, the item moves to
41011 + * periodic_sched_ready because it must be rescheduled for the next
41012 + * frame. Otherwise, the item moves to periodic_sched_inactive.
41013 + */
41014 + dwc_list_link_t periodic_sched_queued;
41015 +
41016 + /**
41017 + * Total bandwidth claimed so far for periodic transfers. This value
41018 + * is in microseconds per (micro)frame. The assumption is that all
41019 + * periodic transfers may occur in the same (micro)frame.
41020 + */
41021 + uint16_t periodic_usecs;
41022 +
41023 + /**
41024 + * Total bandwidth claimed so far for all periodic transfers
41025 + * in a frame.
41026 + * This will include a mixture of HS and FS transfers.
41027 + * Units are microseconds per (micro)frame.
41028 + * We have a budget per frame and have to schedule
41029 + * transactions accordingly.
41030 + * Watch out for the fact that things are actually scheduled for the
41031 + * "next frame".
41032 + */
41033 + uint16_t frame_usecs[8];
41034 +
41035 +
41036 + /**
41037 + * Frame number read from the core at SOF. The value ranges from 0 to
41038 + * DWC_HFNUM_MAX_FRNUM.
41039 + */
41040 + uint16_t frame_number;
41041 +
41042 + /**
41043 + * Count of periodic QHs, if using several eps. For SOF enable/disable.
41044 + */
41045 + uint16_t periodic_qh_count;
41046 +
41047 + /**
41048 + * Free host channels in the controller. This is a list of
41049 + * dwc_hc_t items.
41050 + */
41051 + struct hc_list free_hc_list;
41052 + /**
41053 + * Number of host channels assigned to periodic transfers. Currently
41054 + * assuming that there is a dedicated host channel for each periodic
41055 + * transaction and at least one host channel available for
41056 + * non-periodic transactions.
41057 + */
41058 + int periodic_channels; /* microframe_schedule==0 */
41059 +
41060 + /**
41061 + * Number of host channels assigned to non-periodic transfers.
41062 + */
41063 + int non_periodic_channels; /* microframe_schedule==0 */
41064 +
41065 + /**
41066 + * Number of host channels assigned to non-periodic transfers.
41067 + */
41068 + int available_host_channels;
41069 +
41070 + /**
41071 + * Array of pointers to the host channel descriptors. Allows accessing
41072 + * a host channel descriptor given the host channel number. This is
41073 + * useful in interrupt handlers.
41074 + */
41075 + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
41076 +
41077 + /**
41078 + * Buffer to use for any data received during the status phase of a
41079 + * control transfer. Normally no data is transferred during the status
41080 + * phase. This buffer is used as a bit bucket.
41081 + */
41082 + uint8_t *status_buf;
41083 +
41084 + /**
41085 + * DMA address for status_buf.
41086 + */
41087 + dma_addr_t status_buf_dma;
41088 +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
41089 +
41090 + /**
41091 + * Connection timer. An OTG host must display a message if the device
41092 + * does not connect. Started when the VBus power is turned on via
41093 + * sysfs attribute "buspower".
41094 + */
41095 + dwc_timer_t *conn_timer;
41096 +
41097 + /* Tasket to do a reset */
41098 + dwc_tasklet_t *reset_tasklet;
41099 +
41100 + dwc_tasklet_t *completion_tasklet;
41101 + struct urb_list completed_urb_list;
41102 +
41103 + /* */
41104 + dwc_spinlock_t *lock;
41105 + /**
41106 + * Private data that could be used by OS wrapper.
41107 + */
41108 + void *priv;
41109 +
41110 + uint8_t otg_port;
41111 +
41112 + /** Frame List */
41113 + uint32_t *frame_list;
41114 +
41115 + /** Hub - Port assignment */
41116 + int hub_port[128];
41117 +#ifdef FIQ_DEBUG
41118 + int hub_port_alloc[2048];
41119 +#endif
41120 +
41121 + /** Frame List DMA address */
41122 + dma_addr_t frame_list_dma;
41123 +
41124 + struct fiq_stack *fiq_stack;
41125 + struct fiq_state *fiq_state;
41126 +
41127 + /** Virtual address for split transaction DMA bounce buffers */
41128 + struct fiq_dma_blob *fiq_dmab;
41129 +
41130 +#ifdef DEBUG
41131 + uint32_t frrem_samples;
41132 + uint64_t frrem_accum;
41133 +
41134 + uint32_t hfnum_7_samples_a;
41135 + uint64_t hfnum_7_frrem_accum_a;
41136 + uint32_t hfnum_0_samples_a;
41137 + uint64_t hfnum_0_frrem_accum_a;
41138 + uint32_t hfnum_other_samples_a;
41139 + uint64_t hfnum_other_frrem_accum_a;
41140 +
41141 + uint32_t hfnum_7_samples_b;
41142 + uint64_t hfnum_7_frrem_accum_b;
41143 + uint32_t hfnum_0_samples_b;
41144 + uint64_t hfnum_0_frrem_accum_b;
41145 + uint32_t hfnum_other_samples_b;
41146 + uint64_t hfnum_other_frrem_accum_b;
41147 +#endif
41148 +};
41149 +
41150 +static inline struct device *dwc_otg_hcd_to_dev(struct dwc_otg_hcd *hcd)
41151 +{
41152 + return &hcd->otg_dev->os_dep.platformdev->dev;
41153 +}
41154 +
41155 +/** @name Transaction Execution Functions */
41156 +/** @{ */
41157 +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
41158 + * hcd);
41159 +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
41160 + dwc_otg_transaction_type_e tr_type);
41161 +
41162 +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
41163 +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
41164 +
41165 +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
41166 +extern int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
41167 +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
41168 +
41169 +/** @} */
41170 +
41171 +/** @name Interrupt Handler Functions */
41172 +/** @{ */
41173 +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41174 +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41175 +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
41176 + dwc_otg_hcd);
41177 +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
41178 + dwc_otg_hcd);
41179 +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
41180 + dwc_otg_hcd);
41181 +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
41182 + dwc_otg_hcd);
41183 +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41184 +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
41185 + dwc_otg_hcd);
41186 +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41187 +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41188 +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
41189 + uint32_t num);
41190 +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41191 +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
41192 + dwc_otg_hcd);
41193 +/** @} */
41194 +
41195 +/** @name Schedule Queue Functions */
41196 +/** @{ */
41197 +
41198 +/* Implemented in dwc_otg_hcd_queue.c */
41199 +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
41200 + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
41201 +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41202 +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41203 +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41204 +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41205 + int sched_csplit);
41206 +
41207 +/** Remove and free a QH */
41208 +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
41209 + dwc_otg_qh_t * qh)
41210 +{
41211 + dwc_irqflags_t flags;
41212 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
41213 + dwc_otg_hcd_qh_remove(hcd, qh);
41214 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
41215 + dwc_otg_hcd_qh_free(hcd, qh);
41216 +}
41217 +
41218 +/** Allocates memory for a QH structure.
41219 + * @return Returns the memory allocate or NULL on error. */
41220 +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
41221 +{
41222 + if (atomic_alloc)
41223 + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
41224 + else
41225 + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
41226 +}
41227 +
41228 +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
41229 + int atomic_alloc);
41230 +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
41231 +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
41232 + dwc_otg_qh_t ** qh, int atomic_alloc);
41233 +
41234 +/** Allocates memory for a QTD structure.
41235 + * @return Returns the memory allocate or NULL on error. */
41236 +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
41237 +{
41238 + if (atomic_alloc)
41239 + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
41240 + else
41241 + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
41242 +}
41243 +
41244 +/** Frees the memory for a QTD structure. QTD should already be removed from
41245 + * list.
41246 + * @param qtd QTD to free.*/
41247 +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
41248 +{
41249 + DWC_FREE(qtd);
41250 +}
41251 +
41252 +/** Removes a QTD from list.
41253 + * @param hcd HCD instance.
41254 + * @param qtd QTD to remove from list.
41255 + * @param qh QTD belongs to.
41256 + */
41257 +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
41258 + dwc_otg_qtd_t * qtd,
41259 + dwc_otg_qh_t * qh)
41260 +{
41261 + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
41262 +}
41263 +
41264 +/** Remove and free a QTD
41265 + * Need to disable IRQ and hold hcd lock while calling this function out of
41266 + * interrupt servicing chain */
41267 +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
41268 + dwc_otg_qtd_t * qtd,
41269 + dwc_otg_qh_t * qh)
41270 +{
41271 + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
41272 + dwc_otg_hcd_qtd_free(qtd);
41273 +}
41274 +
41275 +/** @} */
41276 +
41277 +/** @name Descriptor DMA Supporting Functions */
41278 +/** @{ */
41279 +
41280 +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41281 +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
41282 + dwc_hc_t * hc,
41283 + dwc_otg_hc_regs_t * hc_regs,
41284 + dwc_otg_halt_status_e halt_status);
41285 +
41286 +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41287 +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41288 +
41289 +/** @} */
41290 +
41291 +/** @name Internal Functions */
41292 +/** @{ */
41293 +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
41294 +/** @} */
41295 +
41296 +#ifdef CONFIG_USB_DWC_OTG_LPM
41297 +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
41298 + uint8_t devaddr);
41299 +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
41300 +#endif
41301 +
41302 +/** Gets the QH that contains the list_head */
41303 +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
41304 +
41305 +/** Gets the QTD that contains the list_head */
41306 +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
41307 +
41308 +/** Check if QH is non-periodic */
41309 +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
41310 + (_qh_ptr_->ep_type == UE_CONTROL))
41311 +
41312 +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
41313 +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
41314 +
41315 +/** Packet size for any kind of endpoint descriptor */
41316 +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
41317 +
41318 +/**
41319 + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
41320 + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
41321 + * frame number when the max frame number is reached.
41322 + */
41323 +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
41324 +{
41325 + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
41326 + (DWC_HFNUM_MAX_FRNUM >> 1);
41327 +}
41328 +
41329 +/**
41330 + * Returns true if _frame1 is greater than _frame2. The comparison is done
41331 + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
41332 + * number when the max frame number is reached.
41333 + */
41334 +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
41335 +{
41336 + return (frame1 != frame2) &&
41337 + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
41338 + (DWC_HFNUM_MAX_FRNUM >> 1));
41339 +}
41340 +
41341 +/**
41342 + * Increments _frame by the amount specified by _inc. The addition is done
41343 + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
41344 + */
41345 +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
41346 +{
41347 + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
41348 +}
41349 +
41350 +static inline uint16_t dwc_full_frame_num(uint16_t frame)
41351 +{
41352 + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
41353 +}
41354 +
41355 +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
41356 +{
41357 + return frame & 0x7;
41358 +}
41359 +
41360 +extern void init_hcd_usecs(dwc_otg_hcd_t *_hcd);
41361 +
41362 +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
41363 + dwc_otg_hc_regs_t * hc_regs,
41364 + dwc_otg_qtd_t * qtd);
41365 +
41366 +#ifdef DEBUG
41367 +/**
41368 + * Macro to sample the remaining PHY clocks left in the current frame. This
41369 + * may be used during debugging to determine the average time it takes to
41370 + * execute sections of code. There are two possible sample points, "a" and
41371 + * "b", so the _letter argument must be one of these values.
41372 + *
41373 + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
41374 + * example, "cat /sys/devices/lm0/hcd_frrem".
41375 + */
41376 +#define dwc_sample_frrem(_hcd, _qh, _letter) \
41377 +{ \
41378 + hfnum_data_t hfnum; \
41379 + dwc_otg_qtd_t *qtd; \
41380 + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
41381 + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
41382 + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
41383 + switch (hfnum.b.frnum & 0x7) { \
41384 + case 7: \
41385 + _hcd->hfnum_7_samples_##_letter++; \
41386 + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
41387 + break; \
41388 + case 0: \
41389 + _hcd->hfnum_0_samples_##_letter++; \
41390 + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
41391 + break; \
41392 + default: \
41393 + _hcd->hfnum_other_samples_##_letter++; \
41394 + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
41395 + break; \
41396 + } \
41397 + } \
41398 +}
41399 +#else
41400 +#define dwc_sample_frrem(_hcd, _qh, _letter)
41401 +#endif
41402 +#endif
41403 +#endif /* DWC_DEVICE_ONLY */
41404 --- /dev/null
41405 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
41406 @@ -0,0 +1,1135 @@
41407 +/*==========================================================================
41408 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
41409 + * $Revision: #10 $
41410 + * $Date: 2011/10/20 $
41411 + * $Change: 1869464 $
41412 + *
41413 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
41414 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
41415 + * otherwise expressly agreed to in writing between Synopsys and you.
41416 + *
41417 + * The Software IS NOT an item of Licensed Software or Licensed Product under
41418 + * any End User Software License Agreement or Agreement for Licensed Product
41419 + * with Synopsys or any supplement thereto. You are permitted to use and
41420 + * redistribute this Software in source and binary forms, with or without
41421 + * modification, provided that redistributions of source code must retain this
41422 + * notice. You may not view, use, disclose, copy or distribute this file or
41423 + * any information contained herein except pursuant to this license grant from
41424 + * Synopsys. If you do not agree with this notice, including the disclaimer
41425 + * below, then you are not authorized to use the Software.
41426 + *
41427 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
41428 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41429 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41430 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
41431 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41432 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41433 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41434 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41435 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41436 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41437 + * DAMAGE.
41438 + * ========================================================================== */
41439 +#ifndef DWC_DEVICE_ONLY
41440 +
41441 +/** @file
41442 + * This file contains Descriptor DMA support implementation for host mode.
41443 + */
41444 +
41445 +#include "dwc_otg_hcd.h"
41446 +#include "dwc_otg_regs.h"
41447 +
41448 +extern bool microframe_schedule;
41449 +
41450 +static inline uint8_t frame_list_idx(uint16_t frame)
41451 +{
41452 + return (frame & (MAX_FRLIST_EN_NUM - 1));
41453 +}
41454 +
41455 +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
41456 +{
41457 + return (idx + inc) &
41458 + (((speed ==
41459 + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
41460 + MAX_DMA_DESC_NUM_GENERIC) - 1);
41461 +}
41462 +
41463 +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
41464 +{
41465 + return (idx - inc) &
41466 + (((speed ==
41467 + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
41468 + MAX_DMA_DESC_NUM_GENERIC) - 1);
41469 +}
41470 +
41471 +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
41472 +{
41473 + return (((qh->ep_type == UE_ISOCHRONOUS)
41474 + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
41475 + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
41476 +}
41477 +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
41478 +{
41479 + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
41480 + ? ((qh->interval + 8 - 1) / 8)
41481 + : qh->interval);
41482 +}
41483 +
41484 +static int desc_list_alloc(struct device *dev, dwc_otg_qh_t * qh)
41485 +{
41486 + int retval = 0;
41487 +
41488 + qh->desc_list = (dwc_otg_host_dma_desc_t *)
41489 + DWC_DMA_ALLOC(dev, sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
41490 + &qh->desc_list_dma);
41491 +
41492 + if (!qh->desc_list) {
41493 + retval = -DWC_E_NO_MEMORY;
41494 + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
41495 +
41496 + }
41497 +
41498 + dwc_memset(qh->desc_list, 0x00,
41499 + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
41500 +
41501 + qh->n_bytes =
41502 + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
41503 +
41504 + if (!qh->n_bytes) {
41505 + retval = -DWC_E_NO_MEMORY;
41506 + DWC_ERROR
41507 + ("%s: Failed to allocate array for descriptors' size actual values\n",
41508 + __func__);
41509 +
41510 + }
41511 + return retval;
41512 +
41513 +}
41514 +
41515 +static void desc_list_free(struct device *dev, dwc_otg_qh_t * qh)
41516 +{
41517 + if (qh->desc_list) {
41518 + DWC_DMA_FREE(dev, max_desc_num(qh), qh->desc_list,
41519 + qh->desc_list_dma);
41520 + qh->desc_list = NULL;
41521 + }
41522 +
41523 + if (qh->n_bytes) {
41524 + DWC_FREE(qh->n_bytes);
41525 + qh->n_bytes = NULL;
41526 + }
41527 +}
41528 +
41529 +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
41530 +{
41531 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41532 + int retval = 0;
41533 +
41534 + if (hcd->frame_list)
41535 + return 0;
41536 +
41537 + hcd->frame_list = DWC_DMA_ALLOC(dev, 4 * MAX_FRLIST_EN_NUM,
41538 + &hcd->frame_list_dma);
41539 + if (!hcd->frame_list) {
41540 + retval = -DWC_E_NO_MEMORY;
41541 + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
41542 + }
41543 +
41544 + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
41545 +
41546 + return retval;
41547 +}
41548 +
41549 +static void frame_list_free(dwc_otg_hcd_t * hcd)
41550 +{
41551 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41552 +
41553 + if (!hcd->frame_list)
41554 + return;
41555 +
41556 + DWC_DMA_FREE(dev, 4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
41557 + hcd->frame_list = NULL;
41558 +}
41559 +
41560 +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
41561 +{
41562 +
41563 + hcfg_data_t hcfg;
41564 +
41565 + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
41566 +
41567 + if (hcfg.b.perschedena) {
41568 + /* already enabled */
41569 + return;
41570 + }
41571 +
41572 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
41573 + hcd->frame_list_dma);
41574 +
41575 + switch (fr_list_en) {
41576 + case 64:
41577 + hcfg.b.frlisten = 3;
41578 + break;
41579 + case 32:
41580 + hcfg.b.frlisten = 2;
41581 + break;
41582 + case 16:
41583 + hcfg.b.frlisten = 1;
41584 + break;
41585 + case 8:
41586 + hcfg.b.frlisten = 0;
41587 + break;
41588 + default:
41589 + break;
41590 + }
41591 +
41592 + hcfg.b.perschedena = 1;
41593 +
41594 + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
41595 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
41596 +
41597 +}
41598 +
41599 +static void per_sched_disable(dwc_otg_hcd_t * hcd)
41600 +{
41601 + hcfg_data_t hcfg;
41602 +
41603 + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
41604 +
41605 + if (!hcfg.b.perschedena) {
41606 + /* already disabled */
41607 + return;
41608 + }
41609 + hcfg.b.perschedena = 0;
41610 +
41611 + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
41612 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
41613 +}
41614 +
41615 +/*
41616 + * Activates/Deactivates FrameList entries for the channel
41617 + * based on endpoint servicing period.
41618 + */
41619 +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
41620 +{
41621 + uint16_t i, j, inc;
41622 + dwc_hc_t *hc = NULL;
41623 +
41624 + if (!qh->channel) {
41625 + DWC_ERROR("qh->channel = %p", qh->channel);
41626 + return;
41627 + }
41628 +
41629 + if (!hcd) {
41630 + DWC_ERROR("------hcd = %p", hcd);
41631 + return;
41632 + }
41633 +
41634 + if (!hcd->frame_list) {
41635 + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
41636 + return;
41637 + }
41638 +
41639 + hc = qh->channel;
41640 + inc = frame_incr_val(qh);
41641 + if (qh->ep_type == UE_ISOCHRONOUS)
41642 + i = frame_list_idx(qh->sched_frame);
41643 + else
41644 + i = 0;
41645 +
41646 + j = i;
41647 + do {
41648 + if (enable)
41649 + hcd->frame_list[j] |= (1 << hc->hc_num);
41650 + else
41651 + hcd->frame_list[j] &= ~(1 << hc->hc_num);
41652 + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
41653 + }
41654 + while (j != i);
41655 + if (!enable)
41656 + return;
41657 + hc->schinfo = 0;
41658 + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
41659 + j = 1;
41660 + /* TODO - check this */
41661 + inc = (8 + qh->interval - 1) / qh->interval;
41662 + for (i = 0; i < inc; i++) {
41663 + hc->schinfo |= j;
41664 + j = j << qh->interval;
41665 + }
41666 + } else {
41667 + hc->schinfo = 0xff;
41668 + }
41669 +}
41670 +
41671 +#if 1
41672 +void dump_frame_list(dwc_otg_hcd_t * hcd)
41673 +{
41674 + int i = 0;
41675 + DWC_PRINTF("--FRAME LIST (hex) --\n");
41676 + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
41677 + DWC_PRINTF("%x\t", hcd->frame_list[i]);
41678 + if (!(i % 8) && i)
41679 + DWC_PRINTF("\n");
41680 + }
41681 + DWC_PRINTF("\n----\n");
41682 +
41683 +}
41684 +#endif
41685 +
41686 +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41687 +{
41688 + dwc_hc_t *hc = qh->channel;
41689 + if (dwc_qh_is_non_per(qh)) {
41690 + if (!microframe_schedule)
41691 + hcd->non_periodic_channels--;
41692 + else
41693 + hcd->available_host_channels++;
41694 + } else
41695 + update_frame_list(hcd, qh, 0);
41696 +
41697 + /*
41698 + * The condition is added to prevent double cleanup try in case of device
41699 + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
41700 + */
41701 + if (hc->qh) {
41702 + dwc_otg_hc_cleanup(hcd->core_if, hc);
41703 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
41704 + hc->qh = NULL;
41705 + }
41706 +
41707 + qh->channel = NULL;
41708 + qh->ntd = 0;
41709 +
41710 + if (qh->desc_list) {
41711 + dwc_memset(qh->desc_list, 0x00,
41712 + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
41713 + }
41714 +}
41715 +
41716 +/**
41717 + * Initializes a QH structure's Descriptor DMA related members.
41718 + * Allocates memory for descriptor list.
41719 + * On first periodic QH, allocates memory for FrameList
41720 + * and enables periodic scheduling.
41721 + *
41722 + * @param hcd The HCD state structure for the DWC OTG controller.
41723 + * @param qh The QH to init.
41724 + *
41725 + * @return 0 if successful, negative error code otherwise.
41726 + */
41727 +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41728 +{
41729 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41730 + int retval = 0;
41731 +
41732 + if (qh->do_split) {
41733 + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
41734 + return -1;
41735 + }
41736 +
41737 + retval = desc_list_alloc(dev, qh);
41738 +
41739 + if ((retval == 0)
41740 + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
41741 + if (!hcd->frame_list) {
41742 + retval = frame_list_alloc(hcd);
41743 + /* Enable periodic schedule on first periodic QH */
41744 + if (retval == 0)
41745 + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
41746 + }
41747 + }
41748 +
41749 + qh->ntd = 0;
41750 +
41751 + return retval;
41752 +}
41753 +
41754 +/**
41755 + * Frees descriptor list memory associated with the QH.
41756 + * If QH is periodic and the last, frees FrameList memory
41757 + * and disables periodic scheduling.
41758 + *
41759 + * @param hcd The HCD state structure for the DWC OTG controller.
41760 + * @param qh The QH to init.
41761 + */
41762 +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41763 +{
41764 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41765 +
41766 + desc_list_free(dev, qh);
41767 +
41768 + /*
41769 + * Channel still assigned due to some reasons.
41770 + * Seen on Isoc URB dequeue. Channel halted but no subsequent
41771 + * ChHalted interrupt to release the channel. Afterwards
41772 + * when it comes here from endpoint disable routine
41773 + * channel remains assigned.
41774 + */
41775 + if (qh->channel)
41776 + release_channel_ddma(hcd, qh);
41777 +
41778 + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
41779 + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
41780 +
41781 + per_sched_disable(hcd);
41782 + frame_list_free(hcd);
41783 + }
41784 +}
41785 +
41786 +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
41787 +{
41788 + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
41789 + /*
41790 + * Descriptor set(8 descriptors) index
41791 + * which is 8-aligned.
41792 + */
41793 + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
41794 + } else {
41795 + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
41796 + }
41797 +}
41798 +
41799 +/*
41800 + * Determine starting frame for Isochronous transfer.
41801 + * Few frames skipped to prevent race condition with HC.
41802 + */
41803 +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41804 + uint8_t * skip_frames)
41805 +{
41806 + uint16_t frame = 0;
41807 + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
41808 +
41809 + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
41810 +
41811 + /*
41812 + * skip_frames is used to limit activated descriptors number
41813 + * to avoid the situation when HC services the last activated
41814 + * descriptor firstly.
41815 + * Example for FS:
41816 + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
41817 + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
41818 + * will be fetched. If the number of descriptors is max=64 (or greather) the
41819 + * list will be fully programmed with Active descriptors and it is possible
41820 + * case(rare) that the latest descriptor(considering rollback) corresponding
41821 + * to frame 2 will be serviced first. HS case is more probable because, in fact,
41822 + * up to 11 uframes(16 in the code) may be skipped.
41823 + */
41824 + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
41825 + /*
41826 + * Consider uframe counter also, to start xfer asap.
41827 + * If half of the frame elapsed skip 2 frames otherwise
41828 + * just 1 frame.
41829 + * Starting descriptor index must be 8-aligned, so
41830 + * if the current frame is near to complete the next one
41831 + * is skipped as well.
41832 + */
41833 +
41834 + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
41835 + *skip_frames = 2 * 8;
41836 + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
41837 + } else {
41838 + *skip_frames = 1 * 8;
41839 + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
41840 + }
41841 +
41842 + frame = dwc_full_frame_num(frame);
41843 + } else {
41844 + /*
41845 + * Two frames are skipped for FS - the current and the next.
41846 + * But for descriptor programming, 1 frame(descriptor) is enough,
41847 + * see example above.
41848 + */
41849 + *skip_frames = 1;
41850 + frame = dwc_frame_num_inc(hcd->frame_number, 2);
41851 + }
41852 +
41853 + return frame;
41854 +}
41855 +
41856 +/*
41857 + * Calculate initial descriptor index for isochronous transfer
41858 + * based on scheduled frame.
41859 + */
41860 +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41861 +{
41862 + uint16_t frame = 0, fr_idx, fr_idx_tmp;
41863 + uint8_t skip_frames = 0;
41864 + /*
41865 + * With current ISOC processing algorithm the channel is being
41866 + * released when no more QTDs in the list(qh->ntd == 0).
41867 + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
41868 + *
41869 + * So qh->channel != NULL branch is not used and just not removed from the
41870 + * source file. It is required for another possible approach which is,
41871 + * do not disable and release the channel when ISOC session completed,
41872 + * just move QH to inactive schedule until new QTD arrives.
41873 + * On new QTD, the QH moved back to 'ready' schedule,
41874 + * starting frame and therefore starting desc_index are recalculated.
41875 + * In this case channel is released only on ep_disable.
41876 + */
41877 +
41878 + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
41879 + if (qh->channel) {
41880 + frame = calc_starting_frame(hcd, qh, &skip_frames);
41881 + /*
41882 + * Calculate initial descriptor index based on FrameList current bitmap
41883 + * and servicing period.
41884 + */
41885 + fr_idx_tmp = frame_list_idx(frame);
41886 + fr_idx =
41887 + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
41888 + fr_idx_tmp)
41889 + % frame_incr_val(qh);
41890 + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
41891 + } else {
41892 + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
41893 + fr_idx = frame_list_idx(qh->sched_frame);
41894 + }
41895 +
41896 + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
41897 +
41898 + return skip_frames;
41899 +}
41900 +
41901 +#define ISOC_URB_GIVEBACK_ASAP
41902 +
41903 +#define MAX_ISOC_XFER_SIZE_FS 1023
41904 +#define MAX_ISOC_XFER_SIZE_HS 3072
41905 +#define DESCNUM_THRESHOLD 4
41906 +
41907 +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41908 + uint8_t skip_frames)
41909 +{
41910 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
41911 + dwc_otg_qtd_t *qtd;
41912 + dwc_otg_host_dma_desc_t *dma_desc;
41913 + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
41914 +
41915 + idx = qh->td_last;
41916 + inc = qh->interval;
41917 + n_desc = 0;
41918 +
41919 + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
41920 + if (skip_frames && !qh->channel)
41921 + ntd_max = ntd_max - skip_frames / qh->interval;
41922 +
41923 + max_xfer_size =
41924 + (qh->dev_speed ==
41925 + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
41926 + MAX_ISOC_XFER_SIZE_FS;
41927 +
41928 + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
41929 + while ((qh->ntd < ntd_max)
41930 + && (qtd->isoc_frame_index_last <
41931 + qtd->urb->packet_count)) {
41932 +
41933 + dma_desc = &qh->desc_list[idx];
41934 + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
41935 +
41936 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
41937 +
41938 + if (frame_desc->length > max_xfer_size)
41939 + qh->n_bytes[idx] = max_xfer_size;
41940 + else
41941 + qh->n_bytes[idx] = frame_desc->length;
41942 + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
41943 + dma_desc->status.b_isoc.a = 1;
41944 + dma_desc->status.b_isoc.sts = 0;
41945 +
41946 + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
41947 +
41948 + qh->ntd++;
41949 +
41950 + qtd->isoc_frame_index_last++;
41951 +
41952 +#ifdef ISOC_URB_GIVEBACK_ASAP
41953 + /*
41954 + * Set IOC for each descriptor corresponding to the
41955 + * last frame of the URB.
41956 + */
41957 + if (qtd->isoc_frame_index_last ==
41958 + qtd->urb->packet_count)
41959 + dma_desc->status.b_isoc.ioc = 1;
41960 +
41961 +#endif
41962 + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
41963 + n_desc++;
41964 +
41965 + }
41966 + qtd->in_process = 1;
41967 + }
41968 +
41969 + qh->td_last = idx;
41970 +
41971 +#ifdef ISOC_URB_GIVEBACK_ASAP
41972 + /* Set IOC for the last descriptor if descriptor list is full */
41973 + if (qh->ntd == ntd_max) {
41974 + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
41975 + qh->desc_list[idx].status.b_isoc.ioc = 1;
41976 + }
41977 +#else
41978 + /*
41979 + * Set IOC bit only for one descriptor.
41980 + * Always try to be ahead of HW processing,
41981 + * i.e. on IOC generation driver activates next descriptors but
41982 + * core continues to process descriptors followed the one with IOC set.
41983 + */
41984 +
41985 + if (n_desc > DESCNUM_THRESHOLD) {
41986 + /*
41987 + * Move IOC "up". Required even if there is only one QTD
41988 + * in the list, cause QTDs migth continue to be queued,
41989 + * but during the activation it was only one queued.
41990 + * Actually more than one QTD might be in the list if this function called
41991 + * from XferCompletion - QTDs was queued during HW processing of the previous
41992 + * descriptor chunk.
41993 + */
41994 + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
41995 + } else {
41996 + /*
41997 + * Set the IOC for the latest descriptor
41998 + * if either number of descriptor is not greather than threshold
41999 + * or no more new descriptors activated.
42000 + */
42001 + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
42002 + }
42003 +
42004 + qh->desc_list[idx].status.b_isoc.ioc = 1;
42005 +#endif
42006 +}
42007 +
42008 +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42009 +{
42010 +
42011 + dwc_hc_t *hc;
42012 + dwc_otg_host_dma_desc_t *dma_desc;
42013 + dwc_otg_qtd_t *qtd;
42014 + int num_packets, len, n_desc = 0;
42015 +
42016 + hc = qh->channel;
42017 +
42018 + /*
42019 + * Start with hc->xfer_buff initialized in
42020 + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
42021 + * this pointer re-assigned to the buffer of the currently processed QTD.
42022 + * For non-SG request there is always one QTD active.
42023 + */
42024 +
42025 + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
42026 +
42027 + if (n_desc) {
42028 + /* SG request - more than 1 QTDs */
42029 + hc->xfer_buff = (uint8_t *)(uintptr_t)qtd->urb->dma +
42030 + qtd->urb->actual_length;
42031 + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
42032 + }
42033 +
42034 + qtd->n_desc = 0;
42035 +
42036 + do {
42037 + dma_desc = &qh->desc_list[n_desc];
42038 + len = hc->xfer_len;
42039 +
42040 + if (len > MAX_DMA_DESC_SIZE)
42041 + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
42042 +
42043 + if (hc->ep_is_in) {
42044 + if (len > 0) {
42045 + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
42046 + } else {
42047 + /* Need 1 packet for transfer length of 0. */
42048 + num_packets = 1;
42049 + }
42050 + /* Always program an integral # of max packets for IN transfers. */
42051 + len = num_packets * hc->max_packet;
42052 + }
42053 +
42054 + dma_desc->status.b.n_bytes = len;
42055 +
42056 + qh->n_bytes[n_desc] = len;
42057 +
42058 + if ((qh->ep_type == UE_CONTROL)
42059 + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
42060 + dma_desc->status.b.sup = 1; /* Setup Packet */
42061 +
42062 + dma_desc->status.b.a = 1; /* Active descriptor */
42063 + dma_desc->status.b.sts = 0;
42064 +
42065 + dma_desc->buf =
42066 + ((unsigned long)hc->xfer_buff & 0xffffffff);
42067 +
42068 + /*
42069 + * Last descriptor(or single) of IN transfer
42070 + * with actual size less than MaxPacket.
42071 + */
42072 + if (len > hc->xfer_len) {
42073 + hc->xfer_len = 0;
42074 + } else {
42075 + hc->xfer_buff += len;
42076 + hc->xfer_len -= len;
42077 + }
42078 +
42079 + qtd->n_desc++;
42080 + n_desc++;
42081 + }
42082 + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
42083 +
42084 +
42085 + qtd->in_process = 1;
42086 +
42087 + if (qh->ep_type == UE_CONTROL)
42088 + break;
42089 +
42090 + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
42091 + break;
42092 + }
42093 +
42094 + if (n_desc) {
42095 + /* Request Transfer Complete interrupt for the last descriptor */
42096 + qh->desc_list[n_desc - 1].status.b.ioc = 1;
42097 + /* End of List indicator */
42098 + qh->desc_list[n_desc - 1].status.b.eol = 1;
42099 +
42100 + hc->ntd = n_desc;
42101 + }
42102 +}
42103 +
42104 +/**
42105 + * For Control and Bulk endpoints initializes descriptor list
42106 + * and starts the transfer.
42107 + *
42108 + * For Interrupt and Isochronous endpoints initializes descriptor list
42109 + * then updates FrameList, marking appropriate entries as active.
42110 + * In case of Isochronous, the starting descriptor index is calculated based
42111 + * on the scheduled frame, but only on the first transfer descriptor within a session.
42112 + * Then starts the transfer via enabling the channel.
42113 + * For Isochronous endpoint the channel is not halted on XferComplete
42114 + * interrupt so remains assigned to the endpoint(QH) until session is done.
42115 + *
42116 + * @param hcd The HCD state structure for the DWC OTG controller.
42117 + * @param qh The QH to init.
42118 + *
42119 + * @return 0 if successful, negative error code otherwise.
42120 + */
42121 +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42122 +{
42123 + /* Channel is already assigned */
42124 + dwc_hc_t *hc = qh->channel;
42125 + uint8_t skip_frames = 0;
42126 +
42127 + switch (hc->ep_type) {
42128 + case DWC_OTG_EP_TYPE_CONTROL:
42129 + case DWC_OTG_EP_TYPE_BULK:
42130 + init_non_isoc_dma_desc(hcd, qh);
42131 +
42132 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42133 + break;
42134 + case DWC_OTG_EP_TYPE_INTR:
42135 + init_non_isoc_dma_desc(hcd, qh);
42136 +
42137 + update_frame_list(hcd, qh, 1);
42138 +
42139 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42140 + break;
42141 + case DWC_OTG_EP_TYPE_ISOC:
42142 +
42143 + if (!qh->ntd)
42144 + skip_frames = recalc_initial_desc_idx(hcd, qh);
42145 +
42146 + init_isoc_dma_desc(hcd, qh, skip_frames);
42147 +
42148 + if (!hc->xfer_started) {
42149 +
42150 + update_frame_list(hcd, qh, 1);
42151 +
42152 + /*
42153 + * Always set to max, instead of actual size.
42154 + * Otherwise ntd will be changed with
42155 + * channel being enabled. Not recommended.
42156 + *
42157 + */
42158 + hc->ntd = max_desc_num(qh);
42159 + /* Enable channel only once for ISOC */
42160 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42161 + }
42162 +
42163 + break;
42164 + default:
42165 +
42166 + break;
42167 + }
42168 +}
42169 +
42170 +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
42171 + dwc_hc_t * hc,
42172 + dwc_otg_hc_regs_t * hc_regs,
42173 + dwc_otg_halt_status_e halt_status)
42174 +{
42175 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
42176 + dwc_otg_qtd_t *qtd, *qtd_tmp;
42177 + dwc_otg_qh_t *qh;
42178 + dwc_otg_host_dma_desc_t *dma_desc;
42179 + uint16_t idx, remain;
42180 + uint8_t urb_compl;
42181 +
42182 + qh = hc->qh;
42183 + idx = qh->td_first;
42184 +
42185 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42186 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
42187 + qtd->in_process = 0;
42188 + return;
42189 + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
42190 + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
42191 + /*
42192 + * Channel is halted in these error cases.
42193 + * Considered as serious issues.
42194 + * Complete all URBs marking all frames as failed,
42195 + * irrespective whether some of the descriptors(frames) succeeded or no.
42196 + * Pass error code to completion routine as well, to
42197 + * update urb->status, some of class drivers might use it to stop
42198 + * queing transfer requests.
42199 + */
42200 + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
42201 + ? (-DWC_E_IO)
42202 + : (-DWC_E_OVERFLOW);
42203 +
42204 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42205 + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
42206 + frame_desc = &qtd->urb->iso_descs[idx];
42207 + frame_desc->status = err;
42208 + }
42209 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
42210 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42211 + }
42212 + return;
42213 + }
42214 +
42215 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42216 +
42217 + if (!qtd->in_process)
42218 + break;
42219 +
42220 + urb_compl = 0;
42221 +
42222 + do {
42223 +
42224 + dma_desc = &qh->desc_list[idx];
42225 +
42226 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
42227 + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
42228 +
42229 + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
42230 + /*
42231 + * XactError or, unable to complete all the transactions
42232 + * in the scheduled micro-frame/frame,
42233 + * both indicated by DMA_DESC_STS_PKTERR.
42234 + */
42235 + qtd->urb->error_count++;
42236 + frame_desc->actual_length = qh->n_bytes[idx] - remain;
42237 + frame_desc->status = -DWC_E_PROTOCOL;
42238 + } else {
42239 + /* Success */
42240 +
42241 + frame_desc->actual_length = qh->n_bytes[idx] - remain;
42242 + frame_desc->status = 0;
42243 + }
42244 +
42245 + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
42246 + /*
42247 + * urb->status is not used for isoc transfers here.
42248 + * The individual frame_desc status are used instead.
42249 + */
42250 +
42251 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
42252 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42253 +
42254 + /*
42255 + * This check is necessary because urb_dequeue can be called
42256 + * from urb complete callback(sound driver example).
42257 + * All pending URBs are dequeued there, so no need for
42258 + * further processing.
42259 + */
42260 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42261 + return;
42262 + }
42263 +
42264 + urb_compl = 1;
42265 +
42266 + }
42267 +
42268 + qh->ntd--;
42269 +
42270 + /* Stop if IOC requested descriptor reached */
42271 + if (dma_desc->status.b_isoc.ioc) {
42272 + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
42273 + goto stop_scan;
42274 + }
42275 +
42276 + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
42277 +
42278 + if (urb_compl)
42279 + break;
42280 + }
42281 + while (idx != qh->td_first);
42282 + }
42283 +stop_scan:
42284 + qh->td_first = idx;
42285 +}
42286 +
42287 +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
42288 + dwc_hc_t * hc,
42289 + dwc_otg_qtd_t * qtd,
42290 + dwc_otg_host_dma_desc_t * dma_desc,
42291 + dwc_otg_halt_status_e halt_status,
42292 + uint32_t n_bytes, uint8_t * xfer_done)
42293 +{
42294 +
42295 + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
42296 + dwc_otg_hcd_urb_t *urb = qtd->urb;
42297 +
42298 + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
42299 + urb->status = -DWC_E_IO;
42300 + return 1;
42301 + }
42302 + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
42303 + switch (halt_status) {
42304 + case DWC_OTG_HC_XFER_STALL:
42305 + urb->status = -DWC_E_PIPE;
42306 + break;
42307 + case DWC_OTG_HC_XFER_BABBLE_ERR:
42308 + urb->status = -DWC_E_OVERFLOW;
42309 + break;
42310 + case DWC_OTG_HC_XFER_XACT_ERR:
42311 + urb->status = -DWC_E_PROTOCOL;
42312 + break;
42313 + default:
42314 + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
42315 + halt_status);
42316 + break;
42317 + }
42318 + return 1;
42319 + }
42320 +
42321 + if (dma_desc->status.b.a == 1) {
42322 + DWC_DEBUGPL(DBG_HCDV,
42323 + "Active descriptor encountered on channel %d\n",
42324 + hc->hc_num);
42325 + return 0;
42326 + }
42327 +
42328 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
42329 + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
42330 + urb->actual_length += n_bytes - remain;
42331 + if (remain || urb->actual_length == urb->length) {
42332 + /*
42333 + * For Control Data stage do not set urb->status=0 to prevent
42334 + * URB callback. Set it when Status phase done. See below.
42335 + */
42336 + *xfer_done = 1;
42337 + }
42338 +
42339 + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
42340 + urb->status = 0;
42341 + *xfer_done = 1;
42342 + }
42343 + /* No handling for SETUP stage */
42344 + } else {
42345 + /* BULK and INTR */
42346 + urb->actual_length += n_bytes - remain;
42347 + if (remain || urb->actual_length == urb->length) {
42348 + urb->status = 0;
42349 + *xfer_done = 1;
42350 + }
42351 + }
42352 +
42353 + return 0;
42354 +}
42355 +
42356 +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
42357 + dwc_hc_t * hc,
42358 + dwc_otg_hc_regs_t * hc_regs,
42359 + dwc_otg_halt_status_e halt_status)
42360 +{
42361 + dwc_otg_hcd_urb_t *urb = NULL;
42362 + dwc_otg_qtd_t *qtd, *qtd_tmp;
42363 + dwc_otg_qh_t *qh;
42364 + dwc_otg_host_dma_desc_t *dma_desc;
42365 + uint32_t n_bytes, n_desc, i;
42366 + uint8_t failed = 0, xfer_done;
42367 +
42368 + n_desc = 0;
42369 +
42370 + qh = hc->qh;
42371 +
42372 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42373 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42374 + qtd->in_process = 0;
42375 + }
42376 + return;
42377 + }
42378 +
42379 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
42380 +
42381 + urb = qtd->urb;
42382 +
42383 + n_bytes = 0;
42384 + xfer_done = 0;
42385 +
42386 + for (i = 0; i < qtd->n_desc; i++) {
42387 + dma_desc = &qh->desc_list[n_desc];
42388 +
42389 + n_bytes = qh->n_bytes[n_desc];
42390 +
42391 + failed =
42392 + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
42393 + dma_desc,
42394 + halt_status, n_bytes,
42395 + &xfer_done);
42396 +
42397 + if (failed
42398 + || (xfer_done
42399 + && (urb->status != -DWC_E_IN_PROGRESS))) {
42400 +
42401 + hcd->fops->complete(hcd, urb->priv, urb,
42402 + urb->status);
42403 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42404 +
42405 + if (failed)
42406 + goto stop_scan;
42407 + } else if (qh->ep_type == UE_CONTROL) {
42408 + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
42409 + if (urb->length > 0) {
42410 + qtd->control_phase = DWC_OTG_CONTROL_DATA;
42411 + } else {
42412 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
42413 + }
42414 + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
42415 + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
42416 + if (xfer_done) {
42417 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
42418 + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
42419 + } else if (i + 1 == qtd->n_desc) {
42420 + /*
42421 + * Last descriptor for Control data stage which is
42422 + * not completed yet.
42423 + */
42424 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
42425 + }
42426 + }
42427 + }
42428 +
42429 + n_desc++;
42430 + }
42431 +
42432 + }
42433 +
42434 +stop_scan:
42435 +
42436 + if (qh->ep_type != UE_CONTROL) {
42437 + /*
42438 + * Resetting the data toggle for bulk
42439 + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
42440 + */
42441 + if (halt_status == DWC_OTG_HC_XFER_STALL)
42442 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
42443 + else
42444 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
42445 + }
42446 +
42447 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
42448 + hcint_data_t hcint;
42449 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
42450 + if (hcint.b.nyet) {
42451 + /*
42452 + * Got a NYET on the last transaction of the transfer. It
42453 + * means that the endpoint should be in the PING state at the
42454 + * beginning of the next transfer.
42455 + */
42456 + qh->ping_state = 1;
42457 + clear_hc_int(hc_regs, nyet);
42458 + }
42459 +
42460 + }
42461 +
42462 +}
42463 +
42464 +/**
42465 + * This function is called from interrupt handlers.
42466 + * Scans the descriptor list, updates URB's status and
42467 + * calls completion routine for the URB if it's done.
42468 + * Releases the channel to be used by other transfers.
42469 + * In case of Isochronous endpoint the channel is not halted until
42470 + * the end of the session, i.e. QTD list is empty.
42471 + * If periodic channel released the FrameList is updated accordingly.
42472 + *
42473 + * Calls transaction selection routines to activate pending transfers.
42474 + *
42475 + * @param hcd The HCD state structure for the DWC OTG controller.
42476 + * @param hc Host channel, the transfer is completed on.
42477 + * @param hc_regs Host channel registers.
42478 + * @param halt_status Reason the channel is being halted,
42479 + * or just XferComplete for isochronous transfer
42480 + */
42481 +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
42482 + dwc_hc_t * hc,
42483 + dwc_otg_hc_regs_t * hc_regs,
42484 + dwc_otg_halt_status_e halt_status)
42485 +{
42486 + uint8_t continue_isoc_xfer = 0;
42487 + dwc_otg_transaction_type_e tr_type;
42488 + dwc_otg_qh_t *qh = hc->qh;
42489 +
42490 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
42491 +
42492 + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
42493 +
42494 + /* Release the channel if halted or session completed */
42495 + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
42496 + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
42497 +
42498 + /* Halt the channel if session completed */
42499 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
42500 + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
42501 + }
42502 +
42503 + release_channel_ddma(hcd, qh);
42504 + dwc_otg_hcd_qh_remove(hcd, qh);
42505 + } else {
42506 + /* Keep in assigned schedule to continue transfer */
42507 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
42508 + &qh->qh_list_entry);
42509 + continue_isoc_xfer = 1;
42510 +
42511 + }
42512 + /** @todo Consider the case when period exceeds FrameList size.
42513 + * Frame Rollover interrupt should be used.
42514 + */
42515 + } else {
42516 + /* Scan descriptor list to complete the URB(s), then release the channel */
42517 + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
42518 +
42519 + release_channel_ddma(hcd, qh);
42520 + dwc_otg_hcd_qh_remove(hcd, qh);
42521 +
42522 + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
42523 + /* Add back to inactive non-periodic schedule on normal completion */
42524 + dwc_otg_hcd_qh_add(hcd, qh);
42525 + }
42526 +
42527 + }
42528 + tr_type = dwc_otg_hcd_select_transactions(hcd);
42529 + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
42530 + if (continue_isoc_xfer) {
42531 + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
42532 + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
42533 + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
42534 + tr_type = DWC_OTG_TRANSACTION_ALL;
42535 + }
42536 + }
42537 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
42538 + }
42539 +}
42540 +
42541 +#endif /* DWC_DEVICE_ONLY */
42542 --- /dev/null
42543 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
42544 @@ -0,0 +1,421 @@
42545 +/* ==========================================================================
42546 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
42547 + * $Revision: #12 $
42548 + * $Date: 2011/10/26 $
42549 + * $Change: 1873028 $
42550 + *
42551 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
42552 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
42553 + * otherwise expressly agreed to in writing between Synopsys and you.
42554 + *
42555 + * The Software IS NOT an item of Licensed Software or Licensed Product under
42556 + * any End User Software License Agreement or Agreement for Licensed Product
42557 + * with Synopsys or any supplement thereto. You are permitted to use and
42558 + * redistribute this Software in source and binary forms, with or without
42559 + * modification, provided that redistributions of source code must retain this
42560 + * notice. You may not view, use, disclose, copy or distribute this file or
42561 + * any information contained herein except pursuant to this license grant from
42562 + * Synopsys. If you do not agree with this notice, including the disclaimer
42563 + * below, then you are not authorized to use the Software.
42564 + *
42565 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
42566 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42567 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42568 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
42569 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42570 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42571 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42572 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42573 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42574 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42575 + * DAMAGE.
42576 + * ========================================================================== */
42577 +#ifndef DWC_DEVICE_ONLY
42578 +#ifndef __DWC_HCD_IF_H__
42579 +#define __DWC_HCD_IF_H__
42580 +
42581 +#include "dwc_otg_core_if.h"
42582 +
42583 +/** @file
42584 + * This file defines DWC_OTG HCD Core API.
42585 + */
42586 +
42587 +struct dwc_otg_hcd;
42588 +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
42589 +
42590 +struct dwc_otg_hcd_urb;
42591 +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
42592 +
42593 +/** @name HCD Function Driver Callbacks */
42594 +/** @{ */
42595 +
42596 +/** This function is called whenever core switches to host mode. */
42597 +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
42598 +
42599 +/** This function is called when device has been disconnected */
42600 +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
42601 +
42602 +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
42603 +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
42604 + void *urb_handle,
42605 + uint32_t * hub_addr,
42606 + uint32_t * port_addr);
42607 +/** Via this function HCD core gets device speed */
42608 +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
42609 + void *urb_handle);
42610 +
42611 +/** This function is called when urb is completed */
42612 +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
42613 + void *urb_handle,
42614 + dwc_otg_hcd_urb_t * dwc_otg_urb,
42615 + int32_t status);
42616 +
42617 +/** Via this function HCD core gets b_hnp_enable parameter */
42618 +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
42619 +
42620 +struct dwc_otg_hcd_function_ops {
42621 + dwc_otg_hcd_start_cb_t start;
42622 + dwc_otg_hcd_disconnect_cb_t disconnect;
42623 + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
42624 + dwc_otg_hcd_speed_from_urb_cb_t speed;
42625 + dwc_otg_hcd_complete_urb_cb_t complete;
42626 + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
42627 +};
42628 +/** @} */
42629 +
42630 +/** @name HCD Core API */
42631 +/** @{ */
42632 +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
42633 +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
42634 +
42635 +/** This function should be called to initiate HCD Core.
42636 + *
42637 + * @param hcd The HCD
42638 + * @param core_if The DWC_OTG Core
42639 + *
42640 + * Returns -DWC_E_NO_MEMORY if no enough memory.
42641 + * Returns 0 on success
42642 + */
42643 +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
42644 +
42645 +/** Frees HCD
42646 + *
42647 + * @param hcd The HCD
42648 + */
42649 +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
42650 +
42651 +/** This function should be called on every hardware interrupt.
42652 + *
42653 + * @param dwc_otg_hcd The HCD
42654 + *
42655 + * Returns non zero if interrupt is handled
42656 + * Return 0 if interrupt is not handled
42657 + */
42658 +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
42659 +
42660 +/** This function is used to handle the fast interrupt
42661 + *
42662 + */
42663 +#ifdef CONFIG_ARM64
42664 +extern void dwc_otg_hcd_handle_fiq(void);
42665 +#else
42666 +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
42667 +#endif
42668 +
42669 +/**
42670 + * Returns private data set by
42671 + * dwc_otg_hcd_set_priv_data function.
42672 + *
42673 + * @param hcd The HCD
42674 + */
42675 +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
42676 +
42677 +/**
42678 + * Set private data.
42679 + *
42680 + * @param hcd The HCD
42681 + * @param priv_data pointer to be stored in private data
42682 + */
42683 +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
42684 +
42685 +/**
42686 + * This function initializes the HCD Core.
42687 + *
42688 + * @param hcd The HCD
42689 + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
42690 + *
42691 + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
42692 + * Returns 0 on success
42693 + */
42694 +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
42695 + struct dwc_otg_hcd_function_ops *fops);
42696 +
42697 +/**
42698 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
42699 + * stopped.
42700 + *
42701 + * @param hcd The HCD
42702 + */
42703 +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
42704 +
42705 +/**
42706 + * Handles hub class-specific requests.
42707 + *
42708 + * @param dwc_otg_hcd The HCD
42709 + * @param typeReq Request Type
42710 + * @param wValue wValue from control request
42711 + * @param wIndex wIndex from control request
42712 + * @param buf data buffer
42713 + * @param wLength data buffer length
42714 + *
42715 + * Returns -DWC_E_INVALID if invalid argument is passed
42716 + * Returns 0 on success
42717 + */
42718 +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
42719 + uint16_t typeReq, uint16_t wValue,
42720 + uint16_t wIndex, uint8_t * buf,
42721 + uint16_t wLength);
42722 +
42723 +/**
42724 + * Returns otg port number.
42725 + *
42726 + * @param hcd The HCD
42727 + */
42728 +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
42729 +
42730 +/**
42731 + * Returns OTG version - either 1.3 or 2.0.
42732 + *
42733 + * @param core_if The core_if structure pointer
42734 + */
42735 +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
42736 +
42737 +/**
42738 + * Returns 1 if currently core is acting as B host, and 0 otherwise.
42739 + *
42740 + * @param hcd The HCD
42741 + */
42742 +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
42743 +
42744 +/**
42745 + * Returns current frame number.
42746 + *
42747 + * @param hcd The HCD
42748 + */
42749 +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
42750 +
42751 +/**
42752 + * Dumps hcd state.
42753 + *
42754 + * @param hcd The HCD
42755 + */
42756 +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
42757 +
42758 +/**
42759 + * Dump the average frame remaining at SOF. This can be used to
42760 + * determine average interrupt latency. Frame remaining is also shown for
42761 + * start transfer and two additional sample points.
42762 + * Currently this function is not implemented.
42763 + *
42764 + * @param hcd The HCD
42765 + */
42766 +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
42767 +
42768 +/**
42769 + * Sends LPM transaction to the local device.
42770 + *
42771 + * @param hcd The HCD
42772 + * @param devaddr Device Address
42773 + * @param hird Host initiated resume duration
42774 + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
42775 + *
42776 + * Returns negative value if sending LPM transaction was not succeeded.
42777 + * Returns 0 on success.
42778 + */
42779 +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
42780 + uint8_t hird, uint8_t bRemoteWake);
42781 +
42782 +/* URB interface */
42783 +
42784 +/**
42785 + * Allocates memory for dwc_otg_hcd_urb structure.
42786 + * Allocated memory should be freed by call of DWC_FREE.
42787 + *
42788 + * @param hcd The HCD
42789 + * @param iso_desc_count Count of ISOC descriptors
42790 + * @param atomic_alloc Specefies whether to perform atomic allocation.
42791 + */
42792 +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
42793 + int iso_desc_count,
42794 + int atomic_alloc);
42795 +
42796 +/**
42797 + * Set pipe information in URB.
42798 + *
42799 + * @param hcd_urb DWC_OTG URB
42800 + * @param devaddr Device Address
42801 + * @param ep_num Endpoint Number
42802 + * @param ep_type Endpoint Type
42803 + * @param ep_dir Endpoint Direction
42804 + * @param mps Max Packet Size
42805 + */
42806 +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
42807 + uint8_t devaddr, uint8_t ep_num,
42808 + uint8_t ep_type, uint8_t ep_dir,
42809 + uint16_t mps);
42810 +
42811 +/* Transfer flags */
42812 +#define URB_GIVEBACK_ASAP 0x1
42813 +#define URB_SEND_ZERO_PACKET 0x2
42814 +
42815 +/**
42816 + * Sets dwc_otg_hcd_urb parameters.
42817 + *
42818 + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
42819 + * @param urb_handle Unique handle for request, this will be passed back
42820 + * to function driver in completion callback.
42821 + * @param buf The buffer for the data
42822 + * @param dma The DMA buffer for the data
42823 + * @param buflen Transfer length
42824 + * @param sp Buffer for setup data
42825 + * @param sp_dma DMA address of setup data buffer
42826 + * @param flags Transfer flags
42827 + * @param interval Polling interval for interrupt or isochronous transfers.
42828 + */
42829 +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
42830 + void *urb_handle, void *buf,
42831 + dwc_dma_t dma, uint32_t buflen, void *sp,
42832 + dwc_dma_t sp_dma, uint32_t flags,
42833 + uint16_t interval);
42834 +
42835 +/** Gets status from dwc_otg_hcd_urb
42836 + *
42837 + * @param dwc_otg_urb DWC_OTG URB
42838 + */
42839 +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
42840 +
42841 +/** Gets actual length from dwc_otg_hcd_urb
42842 + *
42843 + * @param dwc_otg_urb DWC_OTG URB
42844 + */
42845 +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
42846 + dwc_otg_urb);
42847 +
42848 +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
42849 + *
42850 + * @param dwc_otg_urb DWC_OTG URB
42851 + */
42852 +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
42853 + dwc_otg_urb);
42854 +
42855 +/** Set ISOC descriptor offset and length
42856 + *
42857 + * @param dwc_otg_urb DWC_OTG URB
42858 + * @param desc_num ISOC descriptor number
42859 + * @param offset Offset from beginig of buffer.
42860 + * @param length Transaction length
42861 + */
42862 +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
42863 + int desc_num, uint32_t offset,
42864 + uint32_t length);
42865 +
42866 +/** Get status of ISOC descriptor, specified by desc_num
42867 + *
42868 + * @param dwc_otg_urb DWC_OTG URB
42869 + * @param desc_num ISOC descriptor number
42870 + */
42871 +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
42872 + dwc_otg_urb, int desc_num);
42873 +
42874 +/** Get actual length of ISOC descriptor, specified by desc_num
42875 + *
42876 + * @param dwc_otg_urb DWC_OTG URB
42877 + * @param desc_num ISOC descriptor number
42878 + */
42879 +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
42880 + dwc_otg_urb,
42881 + int desc_num);
42882 +
42883 +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
42884 + *
42885 + * @param dwc_otg_hcd The HCD
42886 + * @param dwc_otg_urb DWC_OTG URB
42887 + * @param ep_handle Out parameter for returning endpoint handle
42888 + * @param atomic_alloc Flag to do atomic allocation if needed
42889 + *
42890 + * Returns -DWC_E_NO_DEVICE if no device is connected.
42891 + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
42892 + * Returns 0 on success.
42893 + */
42894 +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
42895 + dwc_otg_hcd_urb_t * dwc_otg_urb,
42896 + void **ep_handle, int atomic_alloc);
42897 +
42898 +/** De-queue the specified URB
42899 + *
42900 + * @param dwc_otg_hcd The HCD
42901 + * @param dwc_otg_urb DWC_OTG URB
42902 + */
42903 +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
42904 + dwc_otg_hcd_urb_t * dwc_otg_urb);
42905 +
42906 +/** Frees resources in the DWC_otg controller related to a given endpoint.
42907 + * Any URBs for the endpoint must already be dequeued.
42908 + *
42909 + * @param hcd The HCD
42910 + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
42911 + * @param retry Number of retries if there are queued transfers.
42912 + *
42913 + * Returns -DWC_E_INVALID if invalid arguments are passed.
42914 + * Returns 0 on success
42915 + */
42916 +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
42917 + int retry);
42918 +
42919 +/* Resets the data toggle in qh structure. This function can be called from
42920 + * usb_clear_halt routine.
42921 + *
42922 + * @param hcd The HCD
42923 + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
42924 + *
42925 + * Returns -DWC_E_INVALID if invalid arguments are passed.
42926 + * Returns 0 on success
42927 + */
42928 +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
42929 +
42930 +/** Returns 1 if status of specified port is changed and 0 otherwise.
42931 + *
42932 + * @param hcd The HCD
42933 + * @param port Port number
42934 + */
42935 +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
42936 +
42937 +/** Call this function to check if bandwidth was allocated for specified endpoint.
42938 + * Only for ISOC and INTERRUPT endpoints.
42939 + *
42940 + * @param hcd The HCD
42941 + * @param ep_handle Endpoint handle
42942 + */
42943 +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
42944 + void *ep_handle);
42945 +
42946 +/** Call this function to check if bandwidth was freed for specified endpoint.
42947 + *
42948 + * @param hcd The HCD
42949 + * @param ep_handle Endpoint handle
42950 + */
42951 +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
42952 +
42953 +/** Returns bandwidth allocated for specified endpoint in microseconds.
42954 + * Only for ISOC and INTERRUPT endpoints.
42955 + *
42956 + * @param hcd The HCD
42957 + * @param ep_handle Endpoint handle
42958 + */
42959 +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
42960 + void *ep_handle);
42961 +
42962 +/** @} */
42963 +
42964 +#endif /* __DWC_HCD_IF_H__ */
42965 +#endif /* DWC_DEVICE_ONLY */
42966 --- /dev/null
42967 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
42968 @@ -0,0 +1,2757 @@
42969 +/* ==========================================================================
42970 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
42971 + * $Revision: #89 $
42972 + * $Date: 2011/10/20 $
42973 + * $Change: 1869487 $
42974 + *
42975 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
42976 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
42977 + * otherwise expressly agreed to in writing between Synopsys and you.
42978 + *
42979 + * The Software IS NOT an item of Licensed Software or Licensed Product under
42980 + * any End User Software License Agreement or Agreement for Licensed Product
42981 + * with Synopsys or any supplement thereto. You are permitted to use and
42982 + * redistribute this Software in source and binary forms, with or without
42983 + * modification, provided that redistributions of source code must retain this
42984 + * notice. You may not view, use, disclose, copy or distribute this file or
42985 + * any information contained herein except pursuant to this license grant from
42986 + * Synopsys. If you do not agree with this notice, including the disclaimer
42987 + * below, then you are not authorized to use the Software.
42988 + *
42989 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
42990 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42991 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42992 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
42993 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42994 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42995 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42996 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42997 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42998 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42999 + * DAMAGE.
43000 + * ========================================================================== */
43001 +#ifndef DWC_DEVICE_ONLY
43002 +
43003 +#include "dwc_otg_hcd.h"
43004 +#include "dwc_otg_regs.h"
43005 +
43006 +#include <linux/jiffies.h>
43007 +#ifdef CONFIG_ARM
43008 +#include <asm/fiq.h>
43009 +#endif
43010 +
43011 +extern bool microframe_schedule;
43012 +
43013 +/** @file
43014 + * This file contains the implementation of the HCD Interrupt handlers.
43015 + */
43016 +
43017 +int fiq_done, int_done;
43018 +
43019 +#ifdef FIQ_DEBUG
43020 +char buffer[1000*16];
43021 +int wptr;
43022 +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
43023 +{
43024 + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
43025 + va_list args;
43026 + char text[17];
43027 + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
43028 +
43029 + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
43030 + {
43031 + local_fiq_disable();
43032 + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
43033 + va_start(args, fmt);
43034 + vsnprintf(text+8, 9, fmt, args);
43035 + va_end(args);
43036 +
43037 + memcpy(buffer + wptr, text, 16);
43038 + wptr = (wptr + 16) % sizeof(buffer);
43039 + local_fiq_enable();
43040 + }
43041 +}
43042 +#endif
43043 +
43044 +/** This function handles interrupts for the HCD. */
43045 +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43046 +{
43047 + int retval = 0;
43048 + static int last_time;
43049 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
43050 + gintsts_data_t gintsts;
43051 + gintmsk_data_t gintmsk;
43052 + hfnum_data_t hfnum;
43053 + haintmsk_data_t haintmsk;
43054 +
43055 +#ifdef DEBUG
43056 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
43057 +
43058 +#endif
43059 +
43060 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
43061 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
43062 +
43063 + /* Exit from ISR if core is hibernated */
43064 + if (core_if->hibernation_suspend == 1) {
43065 + goto exit_handler_routine;
43066 + }
43067 + DWC_SPINLOCK(dwc_otg_hcd->lock);
43068 + /* Check if HOST Mode */
43069 + if (dwc_otg_is_host_mode(core_if)) {
43070 + if (fiq_enable) {
43071 + local_fiq_disable();
43072 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43073 + /* Pull in from the FIQ's disabled mask */
43074 + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
43075 + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
43076 + }
43077 +
43078 + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
43079 + gintsts.b.hcintr = 1;
43080 + }
43081 +
43082 + /* Danger will robinson: fake a SOF if necessary */
43083 + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
43084 + gintsts.b.sofintr = 1;
43085 + }
43086 + gintsts.d32 &= gintmsk.d32;
43087 +
43088 + if (fiq_enable) {
43089 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43090 + local_fiq_enable();
43091 + }
43092 +
43093 + if (!gintsts.d32) {
43094 + goto exit_handler_routine;
43095 + }
43096 +
43097 +#ifdef DEBUG
43098 + // We should be OK doing this because the common interrupts should already have been serviced
43099 + /* Don't print debug message in the interrupt handler on SOF */
43100 +#ifndef DEBUG_SOF
43101 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43102 +#endif
43103 + DWC_DEBUGPL(DBG_HCDI, "\n");
43104 +#endif
43105 +
43106 +#ifdef DEBUG
43107 +#ifndef DEBUG_SOF
43108 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43109 +#endif
43110 + DWC_DEBUGPL(DBG_HCDI,
43111 + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
43112 + gintsts.d32, core_if);
43113 +#endif
43114 + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
43115 + if (gintsts.b.sofintr) {
43116 + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
43117 + }
43118 +
43119 + if (gintsts.b.rxstsqlvl) {
43120 + retval |=
43121 + dwc_otg_hcd_handle_rx_status_q_level_intr
43122 + (dwc_otg_hcd);
43123 + }
43124 + if (gintsts.b.nptxfempty) {
43125 + retval |=
43126 + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
43127 + (dwc_otg_hcd);
43128 + }
43129 + if (gintsts.b.i2cintr) {
43130 + /** @todo Implement i2cintr handler. */
43131 + }
43132 + if (gintsts.b.portintr) {
43133 +
43134 + gintmsk_data_t gintmsk = { .b.portintr = 1};
43135 + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
43136 + if (fiq_enable) {
43137 + local_fiq_disable();
43138 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43139 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
43140 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43141 + local_fiq_enable();
43142 + } else {
43143 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
43144 + }
43145 + }
43146 + if (gintsts.b.hcintr) {
43147 + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
43148 + }
43149 + if (gintsts.b.ptxfempty) {
43150 + retval |=
43151 + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
43152 + (dwc_otg_hcd);
43153 + }
43154 +#ifdef DEBUG
43155 +#ifndef DEBUG_SOF
43156 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43157 +#endif
43158 + {
43159 + DWC_DEBUGPL(DBG_HCDI,
43160 + "DWC OTG HCD Finished Servicing Interrupts\n");
43161 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
43162 + DWC_READ_REG32(&global_regs->gintsts));
43163 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
43164 + DWC_READ_REG32(&global_regs->gintmsk));
43165 + }
43166 +#endif
43167 +
43168 +#ifdef DEBUG
43169 +#ifndef DEBUG_SOF
43170 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43171 +#endif
43172 + DWC_DEBUGPL(DBG_HCDI, "\n");
43173 +#endif
43174 +
43175 + }
43176 +
43177 +exit_handler_routine:
43178 + if (fiq_enable) {
43179 + gintmsk_data_t gintmsk_new;
43180 + haintmsk_data_t haintmsk_new;
43181 + local_fiq_disable();
43182 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43183 + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
43184 + if(fiq_fsm_enable)
43185 + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
43186 + else
43187 + haintmsk_new.d32 = 0x0000FFFF;
43188 +
43189 + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
43190 + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
43191 + if (dwc_otg_hcd->fiq_state->mphi_regs.swirq_clr) {
43192 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.swirq_clr, 1);
43193 + } else {
43194 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
43195 + }
43196 + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
43197 + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
43198 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
43199 + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
43200 + ;
43201 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
43202 + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
43203 + }
43204 + int_done++;
43205 + }
43206 + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
43207 + /* Re-enable interrupts that the FIQ masked (first time round) */
43208 + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
43209 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43210 + local_fiq_enable();
43211 +
43212 + if ((jiffies / HZ) > last_time) {
43213 + //dwc_otg_qh_t *qh;
43214 + //dwc_list_link_t *cur;
43215 + /* Once a second output the fiq and irq numbers, useful for debug */
43216 + last_time = jiffies / HZ;
43217 + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
43218 + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
43219 + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
43220 + //printk(KERN_WARNING "Periodic queues:\n");
43221 + }
43222 + }
43223 +
43224 + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
43225 + return retval;
43226 +}
43227 +
43228 +#ifdef DWC_TRACK_MISSED_SOFS
43229 +
43230 +#warning Compiling code to track missed SOFs
43231 +#define FRAME_NUM_ARRAY_SIZE 1000
43232 +/**
43233 + * This function is for debug only.
43234 + */
43235 +static inline void track_missed_sofs(uint16_t curr_frame_number)
43236 +{
43237 + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
43238 + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
43239 + static int frame_num_idx = 0;
43240 + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
43241 + static int dumped_frame_num_array = 0;
43242 +
43243 + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
43244 + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
43245 + curr_frame_number) {
43246 + frame_num_array[frame_num_idx] = curr_frame_number;
43247 + last_frame_num_array[frame_num_idx++] = last_frame_num;
43248 + }
43249 + } else if (!dumped_frame_num_array) {
43250 + int i;
43251 + DWC_PRINTF("Frame Last Frame\n");
43252 + DWC_PRINTF("----- ----------\n");
43253 + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
43254 + DWC_PRINTF("0x%04x 0x%04x\n",
43255 + frame_num_array[i], last_frame_num_array[i]);
43256 + }
43257 + dumped_frame_num_array = 1;
43258 + }
43259 + last_frame_num = curr_frame_number;
43260 +}
43261 +#endif
43262 +
43263 +/**
43264 + * Handles the start-of-frame interrupt in host mode. Non-periodic
43265 + * transactions may be queued to the DWC_otg controller for the current
43266 + * (micro)frame. Periodic transactions may be queued to the controller for the
43267 + * next (micro)frame.
43268 + */
43269 +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
43270 +{
43271 + hfnum_data_t hfnum;
43272 + gintsts_data_t gintsts = { .d32 = 0 };
43273 + dwc_list_link_t *qh_entry;
43274 + dwc_otg_qh_t *qh;
43275 + dwc_otg_transaction_type_e tr_type;
43276 + int did_something = 0;
43277 + int32_t next_sched_frame = -1;
43278 +
43279 + hfnum.d32 =
43280 + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
43281 +
43282 +#ifdef DEBUG_SOF
43283 + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
43284 +#endif
43285 + hcd->frame_number = hfnum.b.frnum;
43286 +
43287 +#ifdef DEBUG
43288 + hcd->frrem_accum += hfnum.b.frrem;
43289 + hcd->frrem_samples++;
43290 +#endif
43291 +
43292 +#ifdef DWC_TRACK_MISSED_SOFS
43293 + track_missed_sofs(hcd->frame_number);
43294 +#endif
43295 + /* Determine whether any periodic QHs should be executed. */
43296 + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
43297 + while (qh_entry != &hcd->periodic_sched_inactive) {
43298 + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
43299 + qh_entry = qh_entry->next;
43300 + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
43301 +
43302 + /*
43303 + * Move QH to the ready list to be executed next
43304 + * (micro)frame.
43305 + */
43306 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
43307 + &qh->qh_list_entry);
43308 +
43309 + did_something = 1;
43310 + }
43311 + else
43312 + {
43313 + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
43314 + {
43315 + next_sched_frame = qh->sched_frame;
43316 + }
43317 + }
43318 + }
43319 + if (fiq_enable)
43320 + hcd->fiq_state->next_sched_frame = next_sched_frame;
43321 +
43322 + tr_type = dwc_otg_hcd_select_transactions(hcd);
43323 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
43324 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
43325 + did_something = 1;
43326 + }
43327 +
43328 + /* Clear interrupt - but do not trample on the FIQ sof */
43329 + if (!fiq_fsm_enable) {
43330 + gintsts.b.sofintr = 1;
43331 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
43332 + }
43333 + return 1;
43334 +}
43335 +
43336 +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
43337 + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
43338 + * memory if the DWC_otg controller is operating in Slave mode. */
43339 +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43340 +{
43341 + host_grxsts_data_t grxsts;
43342 + dwc_hc_t *hc = NULL;
43343 +
43344 + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
43345 +
43346 + grxsts.d32 =
43347 + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
43348 +
43349 + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
43350 + if (!hc) {
43351 + DWC_ERROR("Unable to get corresponding channel\n");
43352 + return 0;
43353 + }
43354 +
43355 + /* Packet Status */
43356 + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
43357 + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
43358 + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
43359 + hc->data_pid_start);
43360 + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
43361 +
43362 + switch (grxsts.b.pktsts) {
43363 + case DWC_GRXSTS_PKTSTS_IN:
43364 + /* Read the data into the host buffer. */
43365 + if (grxsts.b.bcnt > 0) {
43366 + dwc_otg_read_packet(dwc_otg_hcd->core_if,
43367 + hc->xfer_buff, grxsts.b.bcnt);
43368 +
43369 + /* Update the HC fields for the next packet received. */
43370 + hc->xfer_count += grxsts.b.bcnt;
43371 + hc->xfer_buff += grxsts.b.bcnt;
43372 + }
43373 +
43374 + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
43375 + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
43376 + case DWC_GRXSTS_PKTSTS_CH_HALTED:
43377 + /* Handled in interrupt, just ignore data */
43378 + break;
43379 + default:
43380 + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
43381 + grxsts.b.pktsts);
43382 + break;
43383 + }
43384 +
43385 + return 1;
43386 +}
43387 +
43388 +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
43389 + * data packets may be written to the FIFO for OUT transfers. More requests
43390 + * may be written to the non-periodic request queue for IN transfers. This
43391 + * interrupt is enabled only in Slave mode. */
43392 +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43393 +{
43394 + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
43395 + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
43396 + DWC_OTG_TRANSACTION_NON_PERIODIC);
43397 + return 1;
43398 +}
43399 +
43400 +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
43401 + * packets may be written to the FIFO for OUT transfers. More requests may be
43402 + * written to the periodic request queue for IN transfers. This interrupt is
43403 + * enabled only in Slave mode. */
43404 +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43405 +{
43406 + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
43407 + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
43408 + DWC_OTG_TRANSACTION_PERIODIC);
43409 + return 1;
43410 +}
43411 +
43412 +/** There are multiple conditions that can cause a port interrupt. This function
43413 + * determines which interrupt conditions have occurred and handles them
43414 + * appropriately. */
43415 +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43416 +{
43417 + int retval = 0;
43418 + hprt0_data_t hprt0;
43419 + hprt0_data_t hprt0_modify;
43420 +
43421 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43422 + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43423 +
43424 + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
43425 + * GINTSTS */
43426 +
43427 + hprt0_modify.b.prtena = 0;
43428 + hprt0_modify.b.prtconndet = 0;
43429 + hprt0_modify.b.prtenchng = 0;
43430 + hprt0_modify.b.prtovrcurrchng = 0;
43431 +
43432 + /* Port Connect Detected
43433 + * Set flag and clear if detected */
43434 + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
43435 + // Dont modify port status if we are in hibernation state
43436 + hprt0_modify.b.prtconndet = 1;
43437 + hprt0_modify.b.prtenchng = 1;
43438 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
43439 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43440 + return retval;
43441 + }
43442 +
43443 + if (hprt0.b.prtconndet) {
43444 + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
43445 + if (dwc_otg_hcd->core_if->adp_enable &&
43446 + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
43447 + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
43448 + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
43449 + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
43450 + /* TODO - check if this is required, as
43451 + * host initialization was already performed
43452 + * after initial ADP probing
43453 + */
43454 + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
43455 + dwc_otg_core_init(dwc_otg_hcd->core_if);
43456 + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
43457 + cil_hcd_start(dwc_otg_hcd->core_if);*/
43458 + } else {
43459 +
43460 + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
43461 + "Port Connect Detected--\n", hprt0.d32);
43462 + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
43463 + dwc_otg_hcd->flags.b.port_connect_status = 1;
43464 + hprt0_modify.b.prtconndet = 1;
43465 +
43466 + /* B-Device has connected, Delete the connection timer. */
43467 + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
43468 + }
43469 + /* The Hub driver asserts a reset when it sees port connect
43470 + * status change flag */
43471 + retval |= 1;
43472 + }
43473 +
43474 + /* Port Enable Changed
43475 + * Clear if detected - Set internal flag if disabled */
43476 + if (hprt0.b.prtenchng) {
43477 + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
43478 + "Port Enable Changed--\n", hprt0.d32);
43479 + hprt0_modify.b.prtenchng = 1;
43480 + if (hprt0.b.prtena == 1) {
43481 + hfir_data_t hfir;
43482 + int do_reset = 0;
43483 + dwc_otg_core_params_t *params =
43484 + dwc_otg_hcd->core_if->core_params;
43485 + dwc_otg_core_global_regs_t *global_regs =
43486 + dwc_otg_hcd->core_if->core_global_regs;
43487 + dwc_otg_host_if_t *host_if =
43488 + dwc_otg_hcd->core_if->host_if;
43489 +
43490 + dwc_otg_hcd->flags.b.port_speed = hprt0.b.prtspd;
43491 + if (microframe_schedule)
43492 + init_hcd_usecs(dwc_otg_hcd);
43493 +
43494 + /* Every time when port enables calculate
43495 + * HFIR.FrInterval
43496 + */
43497 + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
43498 + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
43499 + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
43500 +
43501 + /* Check if we need to adjust the PHY clock speed for
43502 + * low power and adjust it */
43503 + if (params->host_support_fs_ls_low_power) {
43504 + gusbcfg_data_t usbcfg;
43505 +
43506 + usbcfg.d32 =
43507 + DWC_READ_REG32(&global_regs->gusbcfg);
43508 +
43509 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
43510 + || hprt0.b.prtspd ==
43511 + DWC_HPRT0_PRTSPD_FULL_SPEED) {
43512 + /*
43513 + * Low power
43514 + */
43515 + hcfg_data_t hcfg;
43516 + if (usbcfg.b.phylpwrclksel == 0) {
43517 + /* Set PHY low power clock select for FS/LS devices */
43518 + usbcfg.b.phylpwrclksel = 1;
43519 + DWC_WRITE_REG32
43520 + (&global_regs->gusbcfg,
43521 + usbcfg.d32);
43522 + do_reset = 1;
43523 + }
43524 +
43525 + hcfg.d32 =
43526 + DWC_READ_REG32
43527 + (&host_if->host_global_regs->hcfg);
43528 +
43529 + if (hprt0.b.prtspd ==
43530 + DWC_HPRT0_PRTSPD_LOW_SPEED
43531 + && params->host_ls_low_power_phy_clk
43532 + ==
43533 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
43534 + {
43535 + /* 6 MHZ */
43536 + DWC_DEBUGPL(DBG_CIL,
43537 + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
43538 + if (hcfg.b.fslspclksel !=
43539 + DWC_HCFG_6_MHZ) {
43540 + hcfg.b.fslspclksel =
43541 + DWC_HCFG_6_MHZ;
43542 + DWC_WRITE_REG32
43543 + (&host_if->host_global_regs->hcfg,
43544 + hcfg.d32);
43545 + do_reset = 1;
43546 + }
43547 + } else {
43548 + /* 48 MHZ */
43549 + DWC_DEBUGPL(DBG_CIL,
43550 + "FS_PHY programming HCFG to 48 MHz ()\n");
43551 + if (hcfg.b.fslspclksel !=
43552 + DWC_HCFG_48_MHZ) {
43553 + hcfg.b.fslspclksel =
43554 + DWC_HCFG_48_MHZ;
43555 + DWC_WRITE_REG32
43556 + (&host_if->host_global_regs->hcfg,
43557 + hcfg.d32);
43558 + do_reset = 1;
43559 + }
43560 + }
43561 + } else {
43562 + /*
43563 + * Not low power
43564 + */
43565 + if (usbcfg.b.phylpwrclksel == 1) {
43566 + usbcfg.b.phylpwrclksel = 0;
43567 + DWC_WRITE_REG32
43568 + (&global_regs->gusbcfg,
43569 + usbcfg.d32);
43570 + do_reset = 1;
43571 + }
43572 + }
43573 +
43574 + if (do_reset) {
43575 + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
43576 + }
43577 + }
43578 +
43579 + if (!do_reset) {
43580 + /* Port has been enabled set the reset change flag */
43581 + dwc_otg_hcd->flags.b.port_reset_change = 1;
43582 + }
43583 + } else {
43584 + dwc_otg_hcd->flags.b.port_enable_change = 1;
43585 + }
43586 + retval |= 1;
43587 + }
43588 +
43589 + /** Overcurrent Change Interrupt */
43590 + if (hprt0.b.prtovrcurrchng) {
43591 + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
43592 + "Port Overcurrent Changed--\n", hprt0.d32);
43593 + dwc_otg_hcd->flags.b.port_over_current_change = 1;
43594 + hprt0_modify.b.prtovrcurrchng = 1;
43595 + retval |= 1;
43596 + }
43597 +
43598 + /* Clear Port Interrupts */
43599 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
43600 +
43601 + return retval;
43602 +}
43603 +
43604 +/** This interrupt indicates that one or more host channels has a pending
43605 + * interrupt. There are multiple conditions that can cause each host channel
43606 + * interrupt. This function determines which conditions have occurred for each
43607 + * host channel interrupt and handles them appropriately. */
43608 +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43609 +{
43610 + int i;
43611 + int retval = 0;
43612 + haint_data_t haint = { .d32 = 0 } ;
43613 +
43614 + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
43615 + * GINTSTS */
43616 +
43617 + if (!fiq_fsm_enable)
43618 + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
43619 +
43620 + // Overwrite with saved interrupts from fiq handler
43621 + if(fiq_fsm_enable)
43622 + {
43623 + /* check the mask? */
43624 + local_fiq_disable();
43625 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43626 + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
43627 + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
43628 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43629 + local_fiq_enable();
43630 + }
43631 +
43632 + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
43633 + if (haint.b2.chint & (1 << i)) {
43634 + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
43635 + }
43636 + }
43637 +
43638 + return retval;
43639 +}
43640 +
43641 +/**
43642 + * Gets the actual length of a transfer after the transfer halts. _halt_status
43643 + * holds the reason for the halt.
43644 + *
43645 + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
43646 + * *short_read is set to 1 upon return if less than the requested
43647 + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
43648 + * return. short_read may also be NULL on entry, in which case it remains
43649 + * unchanged.
43650 + */
43651 +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
43652 + dwc_otg_hc_regs_t * hc_regs,
43653 + dwc_otg_qtd_t * qtd,
43654 + dwc_otg_halt_status_e halt_status,
43655 + int *short_read)
43656 +{
43657 + hctsiz_data_t hctsiz;
43658 + uint32_t length;
43659 +
43660 + if (short_read != NULL) {
43661 + *short_read = 0;
43662 + }
43663 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43664 +
43665 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
43666 + if (hc->ep_is_in) {
43667 + length = hc->xfer_len - hctsiz.b.xfersize;
43668 + if (short_read != NULL) {
43669 + *short_read = (hctsiz.b.xfersize != 0);
43670 + }
43671 + } else if (hc->qh->do_split) {
43672 + //length = split_out_xfersize[hc->hc_num];
43673 + length = qtd->ssplit_out_xfer_count;
43674 + } else {
43675 + length = hc->xfer_len;
43676 + }
43677 + } else {
43678 + /*
43679 + * Must use the hctsiz.pktcnt field to determine how much data
43680 + * has been transferred. This field reflects the number of
43681 + * packets that have been transferred via the USB. This is
43682 + * always an integral number of packets if the transfer was
43683 + * halted before its normal completion. (Can't use the
43684 + * hctsiz.xfersize field because that reflects the number of
43685 + * bytes transferred via the AHB, not the USB).
43686 + */
43687 + length =
43688 + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
43689 + }
43690 +
43691 + return length;
43692 +}
43693 +
43694 +/**
43695 + * Updates the state of the URB after a Transfer Complete interrupt on the
43696 + * host channel. Updates the actual_length field of the URB based on the
43697 + * number of bytes transferred via the host channel. Sets the URB status
43698 + * if the data transfer is finished.
43699 + *
43700 + * @return 1 if the data transfer specified by the URB is completely finished,
43701 + * 0 otherwise.
43702 + */
43703 +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
43704 + dwc_otg_hc_regs_t * hc_regs,
43705 + dwc_otg_hcd_urb_t * urb,
43706 + dwc_otg_qtd_t * qtd)
43707 +{
43708 + int xfer_done = 0;
43709 + int short_read = 0;
43710 +
43711 + int xfer_length;
43712 +
43713 + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
43714 + DWC_OTG_HC_XFER_COMPLETE,
43715 + &short_read);
43716 +
43717 + if (urb->actual_length + xfer_length > urb->length) {
43718 + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
43719 + hc->dev_addr, __func__, __LINE__);
43720 + xfer_length = urb->length - urb->actual_length;
43721 + }
43722 +
43723 + /* non DWORD-aligned buffer case handling. */
43724 + if (hc->align_buff && xfer_length && hc->ep_is_in) {
43725 + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
43726 + xfer_length);
43727 + }
43728 +
43729 + urb->actual_length += xfer_length;
43730 +
43731 + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
43732 + (urb->flags & URB_SEND_ZERO_PACKET)
43733 + && (urb->actual_length == urb->length)
43734 + && !(urb->length % hc->max_packet)) {
43735 + xfer_done = 0;
43736 + } else if (short_read || urb->actual_length >= urb->length) {
43737 + xfer_done = 1;
43738 + urb->status = 0;
43739 + }
43740 +
43741 +#ifdef DEBUG
43742 + {
43743 + hctsiz_data_t hctsiz;
43744 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43745 + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
43746 + __func__, (hc->ep_is_in ? "IN" : "OUT"),
43747 + hc->hc_num);
43748 + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
43749 + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
43750 + hctsiz.b.xfersize);
43751 + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
43752 + urb->length);
43753 + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
43754 + urb->actual_length);
43755 + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
43756 + short_read, xfer_done);
43757 + }
43758 +#endif
43759 +
43760 + return xfer_done;
43761 +}
43762 +
43763 +/*
43764 + * Save the starting data toggle for the next transfer. The data toggle is
43765 + * saved in the QH for non-control transfers and it's saved in the QTD for
43766 + * control transfers.
43767 + */
43768 +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
43769 + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
43770 +{
43771 + hctsiz_data_t hctsiz;
43772 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43773 +
43774 + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
43775 + dwc_otg_qh_t *qh = hc->qh;
43776 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
43777 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
43778 + } else {
43779 + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
43780 + }
43781 + } else {
43782 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
43783 + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
43784 + } else {
43785 + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
43786 + }
43787 + }
43788 +}
43789 +
43790 +/**
43791 + * Updates the state of an Isochronous URB when the transfer is stopped for
43792 + * any reason. The fields of the current entry in the frame descriptor array
43793 + * are set based on the transfer state and the input _halt_status. Completes
43794 + * the Isochronous URB if all the URB frames have been completed.
43795 + *
43796 + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
43797 + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
43798 + */
43799 +static dwc_otg_halt_status_e
43800 +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
43801 + dwc_hc_t * hc,
43802 + dwc_otg_hc_regs_t * hc_regs,
43803 + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
43804 +{
43805 + dwc_otg_hcd_urb_t *urb = qtd->urb;
43806 + dwc_otg_halt_status_e ret_val = halt_status;
43807 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
43808 +
43809 + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
43810 + switch (halt_status) {
43811 + case DWC_OTG_HC_XFER_COMPLETE:
43812 + frame_desc->status = 0;
43813 + frame_desc->actual_length =
43814 + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
43815 +
43816 + /* non DWORD-aligned buffer case handling. */
43817 + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
43818 + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
43819 + hc->qh->dw_align_buf, frame_desc->actual_length);
43820 + }
43821 +
43822 + break;
43823 + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
43824 + urb->error_count++;
43825 + if (hc->ep_is_in) {
43826 + frame_desc->status = -DWC_E_NO_STREAM_RES;
43827 + } else {
43828 + frame_desc->status = -DWC_E_COMMUNICATION;
43829 + }
43830 + frame_desc->actual_length = 0;
43831 + break;
43832 + case DWC_OTG_HC_XFER_BABBLE_ERR:
43833 + urb->error_count++;
43834 + frame_desc->status = -DWC_E_OVERFLOW;
43835 + /* Don't need to update actual_length in this case. */
43836 + break;
43837 + case DWC_OTG_HC_XFER_XACT_ERR:
43838 + urb->error_count++;
43839 + frame_desc->status = -DWC_E_PROTOCOL;
43840 + frame_desc->actual_length =
43841 + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
43842 +
43843 + /* non DWORD-aligned buffer case handling. */
43844 + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
43845 + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
43846 + hc->qh->dw_align_buf, frame_desc->actual_length);
43847 + }
43848 + /* Skip whole frame */
43849 + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
43850 + hc->ep_is_in && hcd->core_if->dma_enable) {
43851 + qtd->complete_split = 0;
43852 + qtd->isoc_split_offset = 0;
43853 + }
43854 +
43855 + break;
43856 + default:
43857 + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
43858 + break;
43859 + }
43860 + if (++qtd->isoc_frame_index == urb->packet_count) {
43861 + /*
43862 + * urb->status is not used for isoc transfers.
43863 + * The individual frame_desc statuses are used instead.
43864 + */
43865 + hcd->fops->complete(hcd, urb->priv, urb, 0);
43866 + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
43867 + } else {
43868 + ret_val = DWC_OTG_HC_XFER_COMPLETE;
43869 + }
43870 + return ret_val;
43871 +}
43872 +
43873 +/**
43874 + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
43875 + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
43876 + * still linked to the QH, the QH is added to the end of the inactive
43877 + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
43878 + * schedule if no more QTDs are linked to the QH.
43879 + */
43880 +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
43881 +{
43882 + int continue_split = 0;
43883 + dwc_otg_qtd_t *qtd;
43884 +
43885 + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
43886 +
43887 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
43888 +
43889 + if (qtd->complete_split) {
43890 + continue_split = 1;
43891 + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
43892 + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
43893 + continue_split = 1;
43894 + }
43895 +
43896 + if (free_qtd) {
43897 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
43898 + continue_split = 0;
43899 + }
43900 +
43901 + qh->channel = NULL;
43902 + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
43903 +}
43904 +
43905 +/**
43906 + * Releases a host channel for use by other transfers. Attempts to select and
43907 + * queue more transactions since at least one host channel is available.
43908 + *
43909 + * @param hcd The HCD state structure.
43910 + * @param hc The host channel to release.
43911 + * @param qtd The QTD associated with the host channel. This QTD may be freed
43912 + * if the transfer is complete or an error has occurred.
43913 + * @param halt_status Reason the channel is being released. This status
43914 + * determines the actions taken by this function.
43915 + */
43916 +static void release_channel(dwc_otg_hcd_t * hcd,
43917 + dwc_hc_t * hc,
43918 + dwc_otg_qtd_t * qtd,
43919 + dwc_otg_halt_status_e halt_status)
43920 +{
43921 + dwc_otg_transaction_type_e tr_type;
43922 + int free_qtd;
43923 +
43924 + int hog_port = 0;
43925 +
43926 + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
43927 + __func__, hc->hc_num, halt_status, hc->xfer_len);
43928 +
43929 + if(fiq_fsm_enable && hc->do_split) {
43930 + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
43931 + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
43932 + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
43933 + hog_port = 0;
43934 + }
43935 + }
43936 + }
43937 +
43938 + switch (halt_status) {
43939 + case DWC_OTG_HC_XFER_URB_COMPLETE:
43940 + free_qtd = 1;
43941 + break;
43942 + case DWC_OTG_HC_XFER_AHB_ERR:
43943 + case DWC_OTG_HC_XFER_STALL:
43944 + case DWC_OTG_HC_XFER_BABBLE_ERR:
43945 + free_qtd = 1;
43946 + break;
43947 + case DWC_OTG_HC_XFER_XACT_ERR:
43948 + if (qtd->error_count >= 3) {
43949 + DWC_DEBUGPL(DBG_HCDV,
43950 + " Complete URB with transaction error\n");
43951 + free_qtd = 1;
43952 + qtd->urb->status = -DWC_E_PROTOCOL;
43953 + hcd->fops->complete(hcd, qtd->urb->priv,
43954 + qtd->urb, -DWC_E_PROTOCOL);
43955 + } else {
43956 + free_qtd = 0;
43957 + }
43958 + break;
43959 + case DWC_OTG_HC_XFER_URB_DEQUEUE:
43960 + /*
43961 + * The QTD has already been removed and the QH has been
43962 + * deactivated. Don't want to do anything except release the
43963 + * host channel and try to queue more transfers.
43964 + */
43965 + goto cleanup;
43966 + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
43967 + free_qtd = 0;
43968 + break;
43969 + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
43970 + DWC_DEBUGPL(DBG_HCDV,
43971 + " Complete URB with I/O error\n");
43972 + free_qtd = 1;
43973 + qtd->urb->status = -DWC_E_IO;
43974 + hcd->fops->complete(hcd, qtd->urb->priv,
43975 + qtd->urb, -DWC_E_IO);
43976 + break;
43977 + default:
43978 + free_qtd = 0;
43979 + break;
43980 + }
43981 +
43982 + deactivate_qh(hcd, hc->qh, free_qtd);
43983 +
43984 +cleanup:
43985 + /*
43986 + * Release the host channel for use by other transfers. The cleanup
43987 + * function clears the channel interrupt enables and conditions, so
43988 + * there's no need to clear the Channel Halted interrupt separately.
43989 + */
43990 + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
43991 + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
43992 + dwc_otg_hc_cleanup(hcd->core_if, hc);
43993 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
43994 +
43995 + if (!microframe_schedule) {
43996 + switch (hc->ep_type) {
43997 + case DWC_OTG_EP_TYPE_CONTROL:
43998 + case DWC_OTG_EP_TYPE_BULK:
43999 + hcd->non_periodic_channels--;
44000 + break;
44001 +
44002 + default:
44003 + /*
44004 + * Don't release reservations for periodic channels here.
44005 + * That's done when a periodic transfer is descheduled (i.e.
44006 + * when the QH is removed from the periodic schedule).
44007 + */
44008 + break;
44009 + }
44010 + } else {
44011 + hcd->available_host_channels++;
44012 + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
44013 + }
44014 +
44015 + /* Try to queue more transfers now that there's a free channel. */
44016 + tr_type = dwc_otg_hcd_select_transactions(hcd);
44017 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
44018 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
44019 + }
44020 +}
44021 +
44022 +/**
44023 + * Halts a host channel. If the channel cannot be halted immediately because
44024 + * the request queue is full, this function ensures that the FIFO empty
44025 + * interrupt for the appropriate queue is enabled so that the halt request can
44026 + * be queued when there is space in the request queue.
44027 + *
44028 + * This function may also be called in DMA mode. In that case, the channel is
44029 + * simply released since the core always halts the channel automatically in
44030 + * DMA mode.
44031 + */
44032 +static void halt_channel(dwc_otg_hcd_t * hcd,
44033 + dwc_hc_t * hc,
44034 + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
44035 +{
44036 + if (hcd->core_if->dma_enable) {
44037 + release_channel(hcd, hc, qtd, halt_status);
44038 + return;
44039 + }
44040 +
44041 + /* Slave mode processing... */
44042 + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
44043 +
44044 + if (hc->halt_on_queue) {
44045 + gintmsk_data_t gintmsk = {.d32 = 0 };
44046 + dwc_otg_core_global_regs_t *global_regs;
44047 + global_regs = hcd->core_if->core_global_regs;
44048 +
44049 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
44050 + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
44051 + /*
44052 + * Make sure the Non-periodic Tx FIFO empty interrupt
44053 + * is enabled so that the non-periodic schedule will
44054 + * be processed.
44055 + */
44056 + gintmsk.b.nptxfempty = 1;
44057 + if (fiq_enable) {
44058 + local_fiq_disable();
44059 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
44060 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44061 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
44062 + local_fiq_enable();
44063 + } else {
44064 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44065 + }
44066 + } else {
44067 + /*
44068 + * Move the QH from the periodic queued schedule to
44069 + * the periodic assigned schedule. This allows the
44070 + * halt to be queued when the periodic schedule is
44071 + * processed.
44072 + */
44073 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
44074 + &hc->qh->qh_list_entry);
44075 +
44076 + /*
44077 + * Make sure the Periodic Tx FIFO Empty interrupt is
44078 + * enabled so that the periodic schedule will be
44079 + * processed.
44080 + */
44081 + gintmsk.b.ptxfempty = 1;
44082 + if (fiq_enable) {
44083 + local_fiq_disable();
44084 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
44085 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44086 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
44087 + local_fiq_enable();
44088 + } else {
44089 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44090 + }
44091 + }
44092 + }
44093 +}
44094 +
44095 +/**
44096 + * Performs common cleanup for non-periodic transfers after a Transfer
44097 + * Complete interrupt. This function should be called after any endpoint type
44098 + * specific handling is finished to release the host channel.
44099 + */
44100 +static void complete_non_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 + hcint_data_t hcint;
44107 +
44108 + qtd->error_count = 0;
44109 +
44110 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
44111 + if (hcint.b.nyet) {
44112 + /*
44113 + * Got a NYET on the last transaction of the transfer. This
44114 + * means that the endpoint should be in the PING state at the
44115 + * beginning of the next transfer.
44116 + */
44117 + hc->qh->ping_state = 1;
44118 + clear_hc_int(hc_regs, nyet);
44119 + }
44120 +
44121 + /*
44122 + * Always halt and release the host channel to make it available for
44123 + * more transfers. There may still be more phases for a control
44124 + * transfer or more data packets for a bulk transfer at this point,
44125 + * but the host channel is still halted. A channel will be reassigned
44126 + * to the transfer when the non-periodic schedule is processed after
44127 + * the channel is released. This allows transactions to be queued
44128 + * properly via dwc_otg_hcd_queue_transactions, which also enables the
44129 + * Tx FIFO Empty interrupt if necessary.
44130 + */
44131 + if (hc->ep_is_in) {
44132 + /*
44133 + * IN transfers in Slave mode require an explicit disable to
44134 + * halt the channel. (In DMA mode, this call simply releases
44135 + * the channel.)
44136 + */
44137 + halt_channel(hcd, hc, qtd, halt_status);
44138 + } else {
44139 + /*
44140 + * The channel is automatically disabled by the core for OUT
44141 + * transfers in Slave mode.
44142 + */
44143 + release_channel(hcd, hc, qtd, halt_status);
44144 + }
44145 +}
44146 +
44147 +/**
44148 + * Performs common cleanup for periodic transfers after a Transfer Complete
44149 + * interrupt. This function should be called after any endpoint type specific
44150 + * handling is finished to release the host channel.
44151 + */
44152 +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
44153 + dwc_hc_t * hc,
44154 + dwc_otg_hc_regs_t * hc_regs,
44155 + dwc_otg_qtd_t * qtd,
44156 + dwc_otg_halt_status_e halt_status)
44157 +{
44158 + hctsiz_data_t hctsiz;
44159 + qtd->error_count = 0;
44160 +
44161 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44162 + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
44163 + /* Core halts channel in these cases. */
44164 + release_channel(hcd, hc, qtd, halt_status);
44165 + } else {
44166 + /* Flush any outstanding requests from the Tx queue. */
44167 + halt_channel(hcd, hc, qtd, halt_status);
44168 + }
44169 +}
44170 +
44171 +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
44172 + dwc_hc_t * hc,
44173 + dwc_otg_hc_regs_t * hc_regs,
44174 + dwc_otg_qtd_t * qtd)
44175 +{
44176 + uint32_t len;
44177 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
44178 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
44179 +
44180 + len = get_actual_xfer_length(hc, hc_regs, qtd,
44181 + DWC_OTG_HC_XFER_COMPLETE, NULL);
44182 +
44183 + if (!len) {
44184 + qtd->complete_split = 0;
44185 + qtd->isoc_split_offset = 0;
44186 + return 0;
44187 + }
44188 + frame_desc->actual_length += len;
44189 +
44190 + if (hc->align_buff && len)
44191 + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
44192 + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
44193 + qtd->isoc_split_offset += len;
44194 +
44195 + if (frame_desc->length == frame_desc->actual_length) {
44196 + frame_desc->status = 0;
44197 + qtd->isoc_frame_index++;
44198 + qtd->complete_split = 0;
44199 + qtd->isoc_split_offset = 0;
44200 + }
44201 +
44202 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
44203 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
44204 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
44205 + } else {
44206 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
44207 + }
44208 +
44209 + return 1; /* Indicates that channel released */
44210 +}
44211 +
44212 +/**
44213 + * Handles a host channel Transfer Complete interrupt. This handler may be
44214 + * called in either DMA mode or Slave mode.
44215 + */
44216 +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
44217 + dwc_hc_t * hc,
44218 + dwc_otg_hc_regs_t * hc_regs,
44219 + dwc_otg_qtd_t * qtd)
44220 +{
44221 + int urb_xfer_done;
44222 + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
44223 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44224 + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
44225 +
44226 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44227 + "Transfer Complete--\n", hc->hc_num);
44228 +
44229 + if (hcd->core_if->dma_desc_enable) {
44230 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
44231 + if (pipe_type == UE_ISOCHRONOUS) {
44232 + /* Do not disable the interrupt, just clear it */
44233 + clear_hc_int(hc_regs, xfercomp);
44234 + return 1;
44235 + }
44236 + goto handle_xfercomp_done;
44237 + }
44238 +
44239 + /*
44240 + * Handle xfer complete on CSPLIT.
44241 + */
44242 +
44243 + if (hc->qh->do_split) {
44244 + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
44245 + && hcd->core_if->dma_enable) {
44246 + if (qtd->complete_split
44247 + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
44248 + qtd))
44249 + goto handle_xfercomp_done;
44250 + } else {
44251 + qtd->complete_split = 0;
44252 + }
44253 + }
44254 +
44255 + /* Update the QTD and URB states. */
44256 + switch (pipe_type) {
44257 + case UE_CONTROL:
44258 + switch (qtd->control_phase) {
44259 + case DWC_OTG_CONTROL_SETUP:
44260 + if (urb->length > 0) {
44261 + qtd->control_phase = DWC_OTG_CONTROL_DATA;
44262 + } else {
44263 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
44264 + }
44265 + DWC_DEBUGPL(DBG_HCDV,
44266 + " Control setup transaction done\n");
44267 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44268 + break;
44269 + case DWC_OTG_CONTROL_DATA:{
44270 + urb_xfer_done =
44271 + update_urb_state_xfer_comp(hc, hc_regs, urb,
44272 + qtd);
44273 + if (urb_xfer_done) {
44274 + qtd->control_phase =
44275 + DWC_OTG_CONTROL_STATUS;
44276 + DWC_DEBUGPL(DBG_HCDV,
44277 + " Control data transfer done\n");
44278 + } else {
44279 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44280 + }
44281 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44282 + break;
44283 + }
44284 + case DWC_OTG_CONTROL_STATUS:
44285 + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
44286 + if (urb->status == -DWC_E_IN_PROGRESS) {
44287 + urb->status = 0;
44288 + }
44289 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44290 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44291 + break;
44292 + }
44293 +
44294 + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44295 + break;
44296 + case UE_BULK:
44297 + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
44298 + urb_xfer_done =
44299 + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
44300 + if (urb_xfer_done) {
44301 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44302 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44303 + } else {
44304 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44305 + }
44306 +
44307 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44308 + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44309 + break;
44310 + case UE_INTERRUPT:
44311 + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
44312 + urb_xfer_done =
44313 + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
44314 +
44315 + /*
44316 + * Interrupt URB is done on the first transfer complete
44317 + * interrupt.
44318 + */
44319 + if (urb_xfer_done) {
44320 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44321 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44322 + } else {
44323 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44324 + }
44325 +
44326 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44327 + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44328 + break;
44329 + case UE_ISOCHRONOUS:
44330 + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
44331 + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
44332 + halt_status =
44333 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44334 + DWC_OTG_HC_XFER_COMPLETE);
44335 + }
44336 + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44337 + break;
44338 + }
44339 +
44340 +handle_xfercomp_done:
44341 + disable_hc_int(hc_regs, xfercompl);
44342 +
44343 + return 1;
44344 +}
44345 +
44346 +/**
44347 + * Handles a host channel STALL interrupt. This handler may be called in
44348 + * either DMA mode or Slave mode.
44349 + */
44350 +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
44351 + dwc_hc_t * hc,
44352 + dwc_otg_hc_regs_t * hc_regs,
44353 + dwc_otg_qtd_t * qtd)
44354 +{
44355 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44356 + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
44357 +
44358 + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
44359 + "STALL Received--\n", hc->hc_num);
44360 +
44361 + if (hcd->core_if->dma_desc_enable) {
44362 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
44363 + goto handle_stall_done;
44364 + }
44365 +
44366 + if (pipe_type == UE_CONTROL) {
44367 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
44368 + }
44369 +
44370 + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
44371 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
44372 + /*
44373 + * USB protocol requires resetting the data toggle for bulk
44374 + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
44375 + * setup command is issued to the endpoint. Anticipate the
44376 + * CLEAR_FEATURE command since a STALL has occurred and reset
44377 + * the data toggle now.
44378 + */
44379 + hc->qh->data_toggle = 0;
44380 + }
44381 +
44382 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
44383 +
44384 +handle_stall_done:
44385 + disable_hc_int(hc_regs, stall);
44386 +
44387 + return 1;
44388 +}
44389 +
44390 +/*
44391 + * Updates the state of the URB when a transfer has been stopped due to an
44392 + * abnormal condition before the transfer completes. Modifies the
44393 + * actual_length field of the URB to reflect the number of bytes that have
44394 + * actually been transferred via the host channel.
44395 + */
44396 +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
44397 + dwc_otg_hc_regs_t * hc_regs,
44398 + dwc_otg_hcd_urb_t * urb,
44399 + dwc_otg_qtd_t * qtd,
44400 + dwc_otg_halt_status_e halt_status)
44401 +{
44402 + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
44403 + halt_status, NULL);
44404 +
44405 + if (urb->actual_length + bytes_transferred > urb->length) {
44406 + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
44407 + hc->dev_addr, __func__, __LINE__);
44408 + bytes_transferred = urb->length - urb->actual_length;
44409 + }
44410 +
44411 + /* non DWORD-aligned buffer case handling. */
44412 + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
44413 + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
44414 + bytes_transferred);
44415 + }
44416 +
44417 + urb->actual_length += bytes_transferred;
44418 +
44419 +#ifdef DEBUG
44420 + {
44421 + hctsiz_data_t hctsiz;
44422 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44423 + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
44424 + __func__, (hc->ep_is_in ? "IN" : "OUT"),
44425 + hc->hc_num);
44426 + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
44427 + hc->start_pkt_count);
44428 + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
44429 + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
44430 + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
44431 + bytes_transferred);
44432 + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
44433 + urb->actual_length);
44434 + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
44435 + urb->length);
44436 + }
44437 +#endif
44438 +}
44439 +
44440 +/**
44441 + * Handles a host channel NAK interrupt. This handler may be called in either
44442 + * DMA mode or Slave mode.
44443 + */
44444 +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
44445 + dwc_hc_t * hc,
44446 + dwc_otg_hc_regs_t * hc_regs,
44447 + dwc_otg_qtd_t * qtd)
44448 +{
44449 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44450 + "NAK Received--\n", hc->hc_num);
44451 +
44452 + /*
44453 + * When we get bulk NAKs then remember this so we holdoff on this qh until
44454 + * the beginning of the next frame
44455 + */
44456 + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44457 + case UE_BULK:
44458 + case UE_CONTROL:
44459 + if (nak_holdoff && qtd->qh->do_split)
44460 + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
44461 + }
44462 +
44463 + /*
44464 + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
44465 + * interrupt. Re-start the SSPLIT transfer.
44466 + */
44467 + if (hc->do_split) {
44468 + if (hc->complete_split) {
44469 + qtd->error_count = 0;
44470 + }
44471 + qtd->complete_split = 0;
44472 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44473 + goto handle_nak_done;
44474 + }
44475 +
44476 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44477 + case UE_CONTROL:
44478 + case UE_BULK:
44479 + if (hcd->core_if->dma_enable && hc->ep_is_in) {
44480 + /*
44481 + * NAK interrupts are enabled on bulk/control IN
44482 + * transfers in DMA mode for the sole purpose of
44483 + * resetting the error count after a transaction error
44484 + * occurs. The core will continue transferring data.
44485 + * Disable other interrupts unmasked for the same
44486 + * reason.
44487 + */
44488 + disable_hc_int(hc_regs, datatglerr);
44489 + disable_hc_int(hc_regs, ack);
44490 + qtd->error_count = 0;
44491 + goto handle_nak_done;
44492 + }
44493 +
44494 + /*
44495 + * NAK interrupts normally occur during OUT transfers in DMA
44496 + * or Slave mode. For IN transfers, more requests will be
44497 + * queued as request queue space is available.
44498 + */
44499 + qtd->error_count = 0;
44500 +
44501 + if (!hc->qh->ping_state) {
44502 + update_urb_state_xfer_intr(hc, hc_regs,
44503 + qtd->urb, qtd,
44504 + DWC_OTG_HC_XFER_NAK);
44505 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44506 +
44507 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
44508 + hc->qh->ping_state = 1;
44509 + }
44510 +
44511 + /*
44512 + * Halt the channel so the transfer can be re-started from
44513 + * the appropriate point or the PING protocol will
44514 + * start/continue.
44515 + */
44516 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44517 + break;
44518 + case UE_INTERRUPT:
44519 + qtd->error_count = 0;
44520 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44521 + break;
44522 + case UE_ISOCHRONOUS:
44523 + /* Should never get called for isochronous transfers. */
44524 + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
44525 + break;
44526 + }
44527 +
44528 +handle_nak_done:
44529 + disable_hc_int(hc_regs, nak);
44530 +
44531 + return 1;
44532 +}
44533 +
44534 +/**
44535 + * Handles a host channel ACK interrupt. This interrupt is enabled when
44536 + * performing the PING protocol in Slave mode, when errors occur during
44537 + * either Slave mode or DMA mode, and during Start Split transactions.
44538 + */
44539 +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
44540 + dwc_hc_t * hc,
44541 + dwc_otg_hc_regs_t * hc_regs,
44542 + dwc_otg_qtd_t * qtd)
44543 +{
44544 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44545 + "ACK Received--\n", hc->hc_num);
44546 +
44547 + if (hc->do_split) {
44548 + /*
44549 + * Handle ACK on SSPLIT.
44550 + * ACK should not occur in CSPLIT.
44551 + */
44552 + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
44553 + qtd->ssplit_out_xfer_count = hc->xfer_len;
44554 + }
44555 + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
44556 + /* Don't need complete for isochronous out transfers. */
44557 + qtd->complete_split = 1;
44558 + }
44559 +
44560 + /* ISOC OUT */
44561 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
44562 + switch (hc->xact_pos) {
44563 + case DWC_HCSPLIT_XACTPOS_ALL:
44564 + break;
44565 + case DWC_HCSPLIT_XACTPOS_END:
44566 + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
44567 + qtd->isoc_split_offset = 0;
44568 + break;
44569 + case DWC_HCSPLIT_XACTPOS_BEGIN:
44570 + case DWC_HCSPLIT_XACTPOS_MID:
44571 + /*
44572 + * For BEGIN or MID, calculate the length for
44573 + * the next microframe to determine the correct
44574 + * SSPLIT token, either MID or END.
44575 + */
44576 + {
44577 + struct dwc_otg_hcd_iso_packet_desc
44578 + *frame_desc;
44579 +
44580 + frame_desc =
44581 + &qtd->urb->
44582 + iso_descs[qtd->isoc_frame_index];
44583 + qtd->isoc_split_offset += 188;
44584 +
44585 + if ((frame_desc->length -
44586 + qtd->isoc_split_offset) <= 188) {
44587 + qtd->isoc_split_pos =
44588 + DWC_HCSPLIT_XACTPOS_END;
44589 + } else {
44590 + qtd->isoc_split_pos =
44591 + DWC_HCSPLIT_XACTPOS_MID;
44592 + }
44593 +
44594 + }
44595 + break;
44596 + }
44597 + } else {
44598 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
44599 + }
44600 + } else {
44601 + /*
44602 + * An unmasked ACK on a non-split DMA transaction is
44603 + * for the sole purpose of resetting error counts. Disable other
44604 + * interrupts unmasked for the same reason.
44605 + */
44606 + if(hcd->core_if->dma_enable) {
44607 + disable_hc_int(hc_regs, datatglerr);
44608 + disable_hc_int(hc_regs, nak);
44609 + }
44610 + qtd->error_count = 0;
44611 +
44612 + if (hc->qh->ping_state) {
44613 + hc->qh->ping_state = 0;
44614 + /*
44615 + * Halt the channel so the transfer can be re-started
44616 + * from the appropriate point. This only happens in
44617 + * Slave mode. In DMA mode, the ping_state is cleared
44618 + * when the transfer is started because the core
44619 + * automatically executes the PING, then the transfer.
44620 + */
44621 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
44622 + }
44623 + }
44624 +
44625 + /*
44626 + * If the ACK occurred when _not_ in the PING state, let the channel
44627 + * continue transferring data after clearing the error count.
44628 + */
44629 +
44630 + disable_hc_int(hc_regs, ack);
44631 +
44632 + return 1;
44633 +}
44634 +
44635 +/**
44636 + * Handles a host channel NYET interrupt. This interrupt should only occur on
44637 + * Bulk and Control OUT endpoints and for complete split transactions. If a
44638 + * NYET occurs at the same time as a Transfer Complete interrupt, it is
44639 + * handled in the xfercomp interrupt handler, not here. This handler may be
44640 + * called in either DMA mode or Slave mode.
44641 + */
44642 +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
44643 + dwc_hc_t * hc,
44644 + dwc_otg_hc_regs_t * hc_regs,
44645 + dwc_otg_qtd_t * qtd)
44646 +{
44647 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44648 + "NYET Received--\n", hc->hc_num);
44649 +
44650 + /*
44651 + * NYET on CSPLIT
44652 + * re-do the CSPLIT immediately on non-periodic
44653 + */
44654 + if (hc->do_split && hc->complete_split) {
44655 + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
44656 + && hcd->core_if->dma_enable) {
44657 + qtd->complete_split = 0;
44658 + qtd->isoc_split_offset = 0;
44659 + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
44660 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
44661 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
44662 + }
44663 + else
44664 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
44665 + goto handle_nyet_done;
44666 + }
44667 +
44668 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
44669 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
44670 + int frnum = dwc_otg_hcd_get_frame_number(hcd);
44671 +
44672 + // With the FIQ running we only ever see the failed NYET
44673 + if (dwc_full_frame_num(frnum) !=
44674 + dwc_full_frame_num(hc->qh->sched_frame) ||
44675 + fiq_fsm_enable) {
44676 + /*
44677 + * No longer in the same full speed frame.
44678 + * Treat this as a transaction error.
44679 + */
44680 +#if 0
44681 + /** @todo Fix system performance so this can
44682 + * be treated as an error. Right now complete
44683 + * splits cannot be scheduled precisely enough
44684 + * due to other system activity, so this error
44685 + * occurs regularly in Slave mode.
44686 + */
44687 + qtd->error_count++;
44688 +#endif
44689 + qtd->complete_split = 0;
44690 + halt_channel(hcd, hc, qtd,
44691 + DWC_OTG_HC_XFER_XACT_ERR);
44692 + /** @todo add support for isoc release */
44693 + goto handle_nyet_done;
44694 + }
44695 + }
44696 +
44697 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
44698 + goto handle_nyet_done;
44699 + }
44700 +
44701 + hc->qh->ping_state = 1;
44702 + qtd->error_count = 0;
44703 +
44704 + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
44705 + DWC_OTG_HC_XFER_NYET);
44706 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44707 +
44708 + /*
44709 + * Halt the channel and re-start the transfer so the PING
44710 + * protocol will start.
44711 + */
44712 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
44713 +
44714 +handle_nyet_done:
44715 + disable_hc_int(hc_regs, nyet);
44716 + return 1;
44717 +}
44718 +
44719 +/**
44720 + * Handles a host channel babble interrupt. This handler may be called in
44721 + * either DMA mode or Slave mode.
44722 + */
44723 +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
44724 + dwc_hc_t * hc,
44725 + dwc_otg_hc_regs_t * hc_regs,
44726 + dwc_otg_qtd_t * qtd)
44727 +{
44728 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44729 + "Babble Error--\n", hc->hc_num);
44730 +
44731 + if (hcd->core_if->dma_desc_enable) {
44732 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44733 + DWC_OTG_HC_XFER_BABBLE_ERR);
44734 + goto handle_babble_done;
44735 + }
44736 +
44737 + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
44738 + hcd->fops->complete(hcd, qtd->urb->priv,
44739 + qtd->urb, -DWC_E_OVERFLOW);
44740 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
44741 + } else {
44742 + dwc_otg_halt_status_e halt_status;
44743 + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44744 + DWC_OTG_HC_XFER_BABBLE_ERR);
44745 + halt_channel(hcd, hc, qtd, halt_status);
44746 + }
44747 +
44748 +handle_babble_done:
44749 + disable_hc_int(hc_regs, bblerr);
44750 + return 1;
44751 +}
44752 +
44753 +/**
44754 + * Handles a host channel AHB error interrupt. This handler is only called in
44755 + * DMA mode.
44756 + */
44757 +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
44758 + dwc_hc_t * hc,
44759 + dwc_otg_hc_regs_t * hc_regs,
44760 + dwc_otg_qtd_t * qtd)
44761 +{
44762 + hcchar_data_t hcchar;
44763 + hcsplt_data_t hcsplt;
44764 + hctsiz_data_t hctsiz;
44765 + uint32_t hcdma;
44766 + char *pipetype, *speed;
44767 +
44768 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44769 +
44770 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44771 + "AHB Error--\n", hc->hc_num);
44772 +
44773 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
44774 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
44775 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44776 + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
44777 +
44778 + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
44779 + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
44780 + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
44781 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
44782 + DWC_ERROR(" Device address: %d\n",
44783 + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
44784 + DWC_ERROR(" Endpoint: %d, %s\n",
44785 + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
44786 + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
44787 +
44788 + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
44789 + case UE_CONTROL:
44790 + pipetype = "CONTROL";
44791 + break;
44792 + case UE_BULK:
44793 + pipetype = "BULK";
44794 + break;
44795 + case UE_INTERRUPT:
44796 + pipetype = "INTERRUPT";
44797 + break;
44798 + case UE_ISOCHRONOUS:
44799 + pipetype = "ISOCHRONOUS";
44800 + break;
44801 + default:
44802 + pipetype = "UNKNOWN";
44803 + break;
44804 + }
44805 +
44806 + DWC_ERROR(" Endpoint type: %s\n", pipetype);
44807 +
44808 + switch (hc->speed) {
44809 + case DWC_OTG_EP_SPEED_HIGH:
44810 + speed = "HIGH";
44811 + break;
44812 + case DWC_OTG_EP_SPEED_FULL:
44813 + speed = "FULL";
44814 + break;
44815 + case DWC_OTG_EP_SPEED_LOW:
44816 + speed = "LOW";
44817 + break;
44818 + default:
44819 + speed = "UNKNOWN";
44820 + break;
44821 + };
44822 +
44823 + DWC_ERROR(" Speed: %s\n", speed);
44824 +
44825 + DWC_ERROR(" Max packet size: %d\n",
44826 + dwc_otg_hcd_get_mps(&urb->pipe_info));
44827 + DWC_ERROR(" Data buffer length: %d\n", urb->length);
44828 + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %pad\n",
44829 + urb->buf, &urb->dma);
44830 + DWC_ERROR(" Setup buffer: %p, Setup DMA: %pad\n",
44831 + urb->setup_packet, &urb->setup_dma);
44832 + DWC_ERROR(" Interval: %d\n", urb->interval);
44833 +
44834 + /* Core haltes the channel for Descriptor DMA mode */
44835 + if (hcd->core_if->dma_desc_enable) {
44836 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44837 + DWC_OTG_HC_XFER_AHB_ERR);
44838 + goto handle_ahberr_done;
44839 + }
44840 +
44841 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
44842 +
44843 + /*
44844 + * Force a channel halt. Don't call halt_channel because that won't
44845 + * write to the HCCHARn register in DMA mode to force the halt.
44846 + */
44847 + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
44848 +handle_ahberr_done:
44849 + disable_hc_int(hc_regs, ahberr);
44850 + return 1;
44851 +}
44852 +
44853 +/**
44854 + * Handles a host channel transaction error interrupt. This handler may be
44855 + * called in either DMA mode or Slave mode.
44856 + */
44857 +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
44858 + dwc_hc_t * hc,
44859 + dwc_otg_hc_regs_t * hc_regs,
44860 + dwc_otg_qtd_t * qtd)
44861 +{
44862 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44863 + "Transaction Error--\n", hc->hc_num);
44864 +
44865 + if (hcd->core_if->dma_desc_enable) {
44866 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44867 + DWC_OTG_HC_XFER_XACT_ERR);
44868 + goto handle_xacterr_done;
44869 + }
44870 +
44871 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44872 + case UE_CONTROL:
44873 + case UE_BULK:
44874 + qtd->error_count++;
44875 + if (!hc->qh->ping_state) {
44876 +
44877 + update_urb_state_xfer_intr(hc, hc_regs,
44878 + qtd->urb, qtd,
44879 + DWC_OTG_HC_XFER_XACT_ERR);
44880 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44881 + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
44882 + hc->qh->ping_state = 1;
44883 + }
44884 + }
44885 +
44886 + /*
44887 + * Halt the channel so the transfer can be re-started from
44888 + * the appropriate point or the PING protocol will start.
44889 + */
44890 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
44891 + break;
44892 + case UE_INTERRUPT:
44893 + qtd->error_count++;
44894 + if (hc->do_split && hc->complete_split) {
44895 + qtd->complete_split = 0;
44896 + }
44897 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
44898 + break;
44899 + case UE_ISOCHRONOUS:
44900 + {
44901 + dwc_otg_halt_status_e halt_status;
44902 + halt_status =
44903 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44904 + DWC_OTG_HC_XFER_XACT_ERR);
44905 +
44906 + halt_channel(hcd, hc, qtd, halt_status);
44907 + }
44908 + break;
44909 + }
44910 +handle_xacterr_done:
44911 + disable_hc_int(hc_regs, xacterr);
44912 +
44913 + return 1;
44914 +}
44915 +
44916 +/**
44917 + * Handles a host channel frame overrun interrupt. This handler may be called
44918 + * in either DMA mode or Slave mode.
44919 + */
44920 +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
44921 + dwc_hc_t * hc,
44922 + dwc_otg_hc_regs_t * hc_regs,
44923 + dwc_otg_qtd_t * qtd)
44924 +{
44925 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44926 + "Frame Overrun--\n", hc->hc_num);
44927 +
44928 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44929 + case UE_CONTROL:
44930 + case UE_BULK:
44931 + break;
44932 + case UE_INTERRUPT:
44933 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
44934 + break;
44935 + case UE_ISOCHRONOUS:
44936 + {
44937 + dwc_otg_halt_status_e halt_status;
44938 + halt_status =
44939 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44940 + DWC_OTG_HC_XFER_FRAME_OVERRUN);
44941 +
44942 + halt_channel(hcd, hc, qtd, halt_status);
44943 + }
44944 + break;
44945 + }
44946 +
44947 + disable_hc_int(hc_regs, frmovrun);
44948 +
44949 + return 1;
44950 +}
44951 +
44952 +/**
44953 + * Handles a host channel data toggle error interrupt. This handler may be
44954 + * called in either DMA mode or Slave mode.
44955 + */
44956 +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
44957 + dwc_hc_t * hc,
44958 + dwc_otg_hc_regs_t * hc_regs,
44959 + dwc_otg_qtd_t * qtd)
44960 +{
44961 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44962 + "Data Toggle Error on %s transfer--\n",
44963 + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
44964 +
44965 + /* Data toggles on split transactions cause the hc to halt.
44966 + * restart transfer */
44967 + if(hc->qh->do_split)
44968 + {
44969 + qtd->error_count++;
44970 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44971 + update_urb_state_xfer_intr(hc, hc_regs,
44972 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
44973 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
44974 + } else if (hc->ep_is_in) {
44975 + /* An unmasked data toggle error on a non-split DMA transaction is
44976 + * for the sole purpose of resetting error counts. Disable other
44977 + * interrupts unmasked for the same reason.
44978 + */
44979 + if(hcd->core_if->dma_enable) {
44980 + disable_hc_int(hc_regs, ack);
44981 + disable_hc_int(hc_regs, nak);
44982 + }
44983 + qtd->error_count = 0;
44984 + }
44985 +
44986 + disable_hc_int(hc_regs, datatglerr);
44987 +
44988 + return 1;
44989 +}
44990 +
44991 +#ifdef DEBUG
44992 +/**
44993 + * This function is for debug only. It checks that a valid halt status is set
44994 + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
44995 + * taken and a warning is issued.
44996 + * @return 1 if halt status is ok, 0 otherwise.
44997 + */
44998 +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
44999 + dwc_hc_t * hc,
45000 + dwc_otg_hc_regs_t * hc_regs,
45001 + dwc_otg_qtd_t * qtd)
45002 +{
45003 + hcchar_data_t hcchar;
45004 + hctsiz_data_t hctsiz;
45005 + hcint_data_t hcint;
45006 + hcintmsk_data_t hcintmsk;
45007 + hcsplt_data_t hcsplt;
45008 +
45009 + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
45010 + /*
45011 + * This code is here only as a check. This condition should
45012 + * never happen. Ignore the halt if it does occur.
45013 + */
45014 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
45015 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
45016 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45017 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45018 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
45019 + DWC_WARN
45020 + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
45021 + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
45022 + "hcint 0x%08x, hcintmsk 0x%08x, "
45023 + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
45024 + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
45025 + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
45026 +
45027 + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
45028 + __func__, hc->hc_num);
45029 + DWC_WARN("\n");
45030 + clear_hc_int(hc_regs, chhltd);
45031 + return 0;
45032 + }
45033 +
45034 + /*
45035 + * This code is here only as a check. hcchar.chdis should
45036 + * never be set when the halt interrupt occurs. Halt the
45037 + * channel again if it does occur.
45038 + */
45039 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
45040 + if (hcchar.b.chdis) {
45041 + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
45042 + "hcchar 0x%08x, trying to halt again\n",
45043 + __func__, hcchar.d32);
45044 + clear_hc_int(hc_regs, chhltd);
45045 + hc->halt_pending = 0;
45046 + halt_channel(hcd, hc, qtd, hc->halt_status);
45047 + return 0;
45048 + }
45049 +
45050 + return 1;
45051 +}
45052 +#endif
45053 +
45054 +/**
45055 + * Handles a host Channel Halted interrupt in DMA mode. This handler
45056 + * determines the reason the channel halted and proceeds accordingly.
45057 + */
45058 +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
45059 + dwc_hc_t * hc,
45060 + dwc_otg_hc_regs_t * hc_regs,
45061 + dwc_otg_qtd_t * qtd)
45062 +{
45063 + int out_nak_enh = 0;
45064 + hcint_data_t hcint;
45065 + hcintmsk_data_t hcintmsk;
45066 + /* For core with OUT NAK enhancement, the flow for high-
45067 + * speed CONTROL/BULK OUT is handled a little differently.
45068 + */
45069 + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
45070 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
45071 + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
45072 + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
45073 + out_nak_enh = 1;
45074 + }
45075 + }
45076 +
45077 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
45078 + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
45079 + && !hcd->core_if->dma_desc_enable)) {
45080 + /*
45081 + * Just release the channel. A dequeue can happen on a
45082 + * transfer timeout. In the case of an AHB Error, the channel
45083 + * was forced to halt because there's no way to gracefully
45084 + * recover.
45085 + */
45086 + if (hcd->core_if->dma_desc_enable)
45087 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
45088 + hc->halt_status);
45089 + else
45090 + release_channel(hcd, hc, qtd, hc->halt_status);
45091 + return;
45092 + }
45093 +
45094 + /* Read the HCINTn register to determine the cause for the halt. */
45095 +
45096 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45097 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45098 +
45099 + if (hcint.b.xfercomp) {
45100 + /** @todo This is here because of a possible hardware bug. Spec
45101 + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
45102 + * interrupt w/ACK bit set should occur, but I only see the
45103 + * XFERCOMP bit, even with it masked out. This is a workaround
45104 + * for that behavior. Should fix this when hardware is fixed.
45105 + */
45106 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
45107 + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
45108 + }
45109 + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
45110 + } else if (hcint.b.stall) {
45111 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45112 + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
45113 + if (out_nak_enh) {
45114 + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
45115 + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
45116 + qtd->error_count = 0;
45117 + } else {
45118 + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
45119 + }
45120 + }
45121 +
45122 + /*
45123 + * Must handle xacterr before nak or ack. Could get a xacterr
45124 + * at the same time as either of these on a BULK/CONTROL OUT
45125 + * that started with a PING. The xacterr takes precedence.
45126 + */
45127 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45128 + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
45129 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45130 + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
45131 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45132 + } else if (hcint.b.bblerr) {
45133 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45134 + } else if (hcint.b.frmovrun) {
45135 + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
45136 + } else if (hcint.b.datatglerr) {
45137 + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
45138 + } else if (!out_nak_enh) {
45139 + if (hcint.b.nyet) {
45140 + /*
45141 + * Must handle nyet before nak or ack. Could get a nyet at the
45142 + * same time as either of those on a BULK/CONTROL OUT that
45143 + * started with a PING. The nyet takes precedence.
45144 + */
45145 + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
45146 + } else if (hcint.b.nak && !hcintmsk.b.nak) {
45147 + /*
45148 + * If nak is not masked, it's because a non-split IN transfer
45149 + * is in an error state. In that case, the nak is handled by
45150 + * the nak interrupt handler, not here. Handle nak here for
45151 + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
45152 + * rewinding the buffer pointer.
45153 + */
45154 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45155 + } else if (hcint.b.ack && !hcintmsk.b.ack) {
45156 + /*
45157 + * If ack is not masked, it's because a non-split IN transfer
45158 + * is in an error state. In that case, the ack is handled by
45159 + * the ack interrupt handler, not here. Handle ack here for
45160 + * split transfers. Start splits halt on ACK.
45161 + */
45162 + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
45163 + } else {
45164 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
45165 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
45166 + /*
45167 + * A periodic transfer halted with no other channel
45168 + * interrupts set. Assume it was halted by the core
45169 + * because it could not be completed in its scheduled
45170 + * (micro)frame.
45171 + */
45172 +#ifdef DEBUG
45173 + DWC_PRINTF
45174 + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
45175 + __func__, hc->hc_num);
45176 +#endif
45177 + halt_channel(hcd, hc, qtd,
45178 + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
45179 + } else {
45180 + DWC_ERROR
45181 + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
45182 + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
45183 + __func__, hc->hc_num, hcint.d32,
45184 + DWC_READ_REG32(&hcd->
45185 + core_if->core_global_regs->
45186 + gintsts));
45187 + /* Failthrough: use 3-strikes rule */
45188 + qtd->error_count++;
45189 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45190 + update_urb_state_xfer_intr(hc, hc_regs,
45191 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45192 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45193 + }
45194 +
45195 + }
45196 + } else {
45197 + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
45198 + hcint.d32);
45199 + /* Failthrough: use 3-strikes rule */
45200 + qtd->error_count++;
45201 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45202 + update_urb_state_xfer_intr(hc, hc_regs,
45203 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45204 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45205 + }
45206 +}
45207 +
45208 +/**
45209 + * Handles a host channel Channel Halted interrupt.
45210 + *
45211 + * In slave mode, this handler is called only when the driver specifically
45212 + * requests a halt. This occurs during handling other host channel interrupts
45213 + * (e.g. nak, xacterr, stall, nyet, etc.).
45214 + *
45215 + * In DMA mode, this is the interrupt that occurs when the core has finished
45216 + * processing a transfer on a channel. Other host channel interrupts (except
45217 + * ahberr) are disabled in DMA mode.
45218 + */
45219 +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
45220 + dwc_hc_t * hc,
45221 + dwc_otg_hc_regs_t * hc_regs,
45222 + dwc_otg_qtd_t * qtd)
45223 +{
45224 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45225 + "Channel Halted--\n", hc->hc_num);
45226 +
45227 + if (hcd->core_if->dma_enable) {
45228 + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
45229 + } else {
45230 +#ifdef DEBUG
45231 + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
45232 + return 1;
45233 + }
45234 +#endif
45235 + release_channel(hcd, hc, qtd, hc->halt_status);
45236 + }
45237 +
45238 + return 1;
45239 +}
45240 +
45241 +
45242 +/**
45243 + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
45244 + * FIQ transfer completion
45245 + * @hcd: Pointer to dwc_otg_hcd struct
45246 + * @num: Host channel number
45247 + *
45248 + * 1. Un-mangle the status as recorded in each iso_frame_desc status
45249 + * 2. Copy it from the dwc_otg_urb into the real URB
45250 + */
45251 +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
45252 +{
45253 + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
45254 + int nr_frames = dwc_urb->packet_count;
45255 + int i;
45256 + hcint_data_t frame_hcint;
45257 +
45258 + for (i = 0; i < nr_frames; i++) {
45259 + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
45260 + if (frame_hcint.b.xfercomp) {
45261 + dwc_urb->iso_descs[i].status = 0;
45262 + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
45263 + } else if (frame_hcint.b.frmovrun) {
45264 + if (qh->ep_is_in)
45265 + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
45266 + else
45267 + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
45268 + dwc_urb->error_count++;
45269 + dwc_urb->iso_descs[i].actual_length = 0;
45270 + } else if (frame_hcint.b.xacterr) {
45271 + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
45272 + dwc_urb->error_count++;
45273 + dwc_urb->iso_descs[i].actual_length = 0;
45274 + } else if (frame_hcint.b.bblerr) {
45275 + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
45276 + dwc_urb->error_count++;
45277 + dwc_urb->iso_descs[i].actual_length = 0;
45278 + } else {
45279 + /* Something went wrong */
45280 + dwc_urb->iso_descs[i].status = -1;
45281 + dwc_urb->iso_descs[i].actual_length = 0;
45282 + dwc_urb->error_count++;
45283 + }
45284 + }
45285 + qh->sched_frame = dwc_frame_num_inc(qh->sched_frame, qh->interval * (nr_frames - 1));
45286 +
45287 + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
45288 + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
45289 +}
45290 +
45291 +/**
45292 + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
45293 + * @hcd: Pointer to dwc_otg_hcd struct
45294 + * @num: Host channel number
45295 + *
45296 + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
45297 + * Returns total length of data or -1 if the buffers were not used.
45298 + *
45299 + */
45300 +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)
45301 +{
45302 + dwc_hc_t *hc = qh->channel;
45303 + struct fiq_dma_blob *blob = hcd->fiq_dmab;
45304 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
45305 + uint8_t *ptr = NULL;
45306 + int index = 0, len = 0;
45307 + int i = 0;
45308 + if (hc->ep_is_in) {
45309 + /* Copy data out of the DMA bounce buffers to the URB's buffer.
45310 + * The align_buf is ignored as this is ignored on FSM enqueue. */
45311 + ptr = qtd->urb->buf;
45312 + if (qh->ep_type == UE_ISOCHRONOUS) {
45313 + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
45314 + index = qtd->isoc_frame_index;
45315 + ptr += qtd->urb->iso_descs[index].offset;
45316 + } else {
45317 + /* Need to increment by actual_length for interrupt IN */
45318 + ptr += qtd->urb->actual_length;
45319 + }
45320 +
45321 + for (i = 0; i < st->dma_info.index; i++) {
45322 + len += st->dma_info.slot_len[i];
45323 + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
45324 + ptr += st->dma_info.slot_len[i];
45325 + }
45326 + return len;
45327 + } else {
45328 + /* OUT endpoints - nothing to do. */
45329 + return -1;
45330 + }
45331 +
45332 +}
45333 +/**
45334 + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
45335 + * from a channel handled in the FIQ
45336 + * @hcd: Pointer to dwc_otg_hcd struct
45337 + * @num: Host channel number
45338 + *
45339 + * If a host channel interrupt was received by the IRQ and this was a channel
45340 + * used by the FIQ, the execution flow for transfer completion is substantially
45341 + * different from the normal (messy) path. This function and its friends handles
45342 + * channel cleanup and transaction completion from a FIQ transaction.
45343 + */
45344 +void dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
45345 +{
45346 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
45347 + dwc_hc_t *hc = hcd->hc_ptr_array[num];
45348 + dwc_otg_qtd_t *qtd;
45349 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
45350 + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
45351 + hctsiz_data_t hctsiz = hcd->fiq_state->channel[num].hctsiz_copy;
45352 + int hostchannels = 0;
45353 + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
45354 +
45355 + hostchannels = hcd->available_host_channels;
45356 + if (hc->halt_pending) {
45357 + /* Dequeue: The FIQ was allowed to complete the transfer but state has been cleared. */
45358 + if (hc->qh && st->fsm == FIQ_NP_SPLIT_DONE &&
45359 + hcint.b.xfercomp && hc->qh->ep_type == UE_BULK) {
45360 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
45361 + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA1;
45362 + } else {
45363 + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA0;
45364 + }
45365 + }
45366 + release_channel(hcd, hc, NULL, hc->halt_status);
45367 + return;
45368 + }
45369 +
45370 + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
45371 + switch (st->fsm) {
45372 + case FIQ_TEST:
45373 + break;
45374 +
45375 + case FIQ_DEQUEUE_ISSUED:
45376 + /* Handled above, but keep for posterity */
45377 + release_channel(hcd, hc, NULL, hc->halt_status);
45378 + break;
45379 +
45380 + case FIQ_NP_SPLIT_DONE:
45381 + /* Nonperiodic transaction complete. */
45382 + if (!hc->ep_is_in) {
45383 + qtd->ssplit_out_xfer_count = hc->xfer_len;
45384 + }
45385 + if (hcint.b.xfercomp) {
45386 + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
45387 + } else if (hcint.b.nak) {
45388 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45389 + } else {
45390 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45391 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45392 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45393 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45394 + }
45395 + break;
45396 +
45397 + case FIQ_NP_SPLIT_HS_ABORTED:
45398 + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
45399 + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
45400 + * because there's no guarantee which order a non-periodic split happened in.
45401 + * We could end up clearing a perfectly good transaction out of the buffer.
45402 + */
45403 + if (hcint.b.xacterr) {
45404 + qtd->error_count += st->nr_errors;
45405 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45406 + } else if (hcint.b.ahberr) {
45407 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45408 + } else {
45409 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45410 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45411 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45412 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45413 + }
45414 + break;
45415 +
45416 + case FIQ_NP_SPLIT_LS_ABORTED:
45417 + /* A few cases can cause this - either an unknown state on a SSPLIT or
45418 + * STALL/data toggle error response on a CSPLIT */
45419 + if (hcint.b.stall) {
45420 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45421 + } else if (hcint.b.datatglerr) {
45422 + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
45423 + } else if (hcint.b.bblerr) {
45424 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45425 + } else if (hcint.b.ahberr) {
45426 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45427 + } else {
45428 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45429 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45430 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45431 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45432 + }
45433 + break;
45434 +
45435 + case FIQ_PER_SPLIT_DONE:
45436 + /* Isoc IN or Interrupt IN/OUT */
45437 +
45438 + /* Flow control here is different from the normal execution by the driver.
45439 + * We need to completely ignore most of the driver's method of handling
45440 + * split transactions and do it ourselves.
45441 + */
45442 + if (hc->ep_type == UE_INTERRUPT) {
45443 + if (hcint.b.nak) {
45444 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45445 + } else if (hc->ep_is_in) {
45446 + int len;
45447 + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
45448 + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
45449 + qtd->urb->actual_length += len;
45450 + if (qtd->urb->actual_length >= qtd->urb->length) {
45451 + qtd->urb->status = 0;
45452 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45453 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45454 + } else {
45455 + /* Interrupt transfer not complete yet - is it a short read? */
45456 + if (len < hc->max_packet) {
45457 + /* Interrupt transaction complete */
45458 + qtd->urb->status = 0;
45459 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45460 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45461 + } else {
45462 + /* Further transactions required */
45463 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45464 + }
45465 + }
45466 + } else {
45467 + /* Interrupt OUT complete. */
45468 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45469 + qtd->urb->actual_length += hc->xfer_len;
45470 + if (qtd->urb->actual_length >= qtd->urb->length) {
45471 + qtd->urb->status = 0;
45472 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45473 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45474 + } else {
45475 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45476 + }
45477 + }
45478 + } else {
45479 + /* ISOC IN complete. */
45480 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45481 + int len = 0;
45482 + /* Record errors, update qtd. */
45483 + if (st->nr_errors) {
45484 + frame_desc->actual_length = 0;
45485 + frame_desc->status = -DWC_E_PROTOCOL;
45486 + } else {
45487 + frame_desc->status = 0;
45488 + /* Unswizzle dma */
45489 + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
45490 + frame_desc->actual_length = len;
45491 + }
45492 + qtd->isoc_frame_index++;
45493 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45494 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45495 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45496 + } else {
45497 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45498 + }
45499 + }
45500 + break;
45501 +
45502 + case FIQ_PER_ISO_OUT_DONE: {
45503 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45504 + /* Record errors, update qtd. */
45505 + if (st->nr_errors) {
45506 + frame_desc->actual_length = 0;
45507 + frame_desc->status = -DWC_E_PROTOCOL;
45508 + } else {
45509 + frame_desc->status = 0;
45510 + frame_desc->actual_length = frame_desc->length;
45511 + }
45512 + qtd->isoc_frame_index++;
45513 + qtd->isoc_split_offset = 0;
45514 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45515 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45516 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45517 + } else {
45518 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45519 + }
45520 + }
45521 + break;
45522 +
45523 + case FIQ_PER_SPLIT_NYET_ABORTED:
45524 + /* Doh. lost the data. */
45525 + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
45526 + "- FIQ reported NYET. Data may have been lost.\n",
45527 + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
45528 + if (hc->ep_type == UE_ISOCHRONOUS) {
45529 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45530 + /* Record errors, update qtd. */
45531 + frame_desc->actual_length = 0;
45532 + frame_desc->status = -DWC_E_PROTOCOL;
45533 + qtd->isoc_frame_index++;
45534 + qtd->isoc_split_offset = 0;
45535 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45536 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45537 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45538 + } else {
45539 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45540 + }
45541 + } else {
45542 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45543 + }
45544 + break;
45545 +
45546 + case FIQ_HS_ISOC_DONE:
45547 + /* The FIQ has performed a whole pile of isochronous transactions.
45548 + * The status is recorded as the interrupt state should the transaction
45549 + * fail.
45550 + */
45551 + dwc_otg_fiq_unmangle_isoc(hcd, hc->qh, qtd, num);
45552 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45553 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45554 + break;
45555 +
45556 + case FIQ_PER_SPLIT_LS_ABORTED:
45557 + if (hcint.b.xacterr) {
45558 + /* Hub has responded with an ERR packet. Device
45559 + * has been unplugged or the port has been disabled.
45560 + * TODO: need to issue a reset to the hub port. */
45561 + qtd->error_count += 3;
45562 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45563 + } else if (hcint.b.stall) {
45564 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45565 + } else if (hcint.b.bblerr) {
45566 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45567 + } else {
45568 + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
45569 + "- FIQ reported FSM=%d. Data may have been lost.\n",
45570 + st->fsm, hc->dev_addr, hc->ep_num);
45571 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45572 + }
45573 + break;
45574 +
45575 + case FIQ_PER_SPLIT_HS_ABORTED:
45576 + /* Either the SSPLIT phase suffered transaction errors or something
45577 + * unexpected happened.
45578 + */
45579 + qtd->error_count += 3;
45580 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45581 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45582 + break;
45583 +
45584 + case FIQ_PER_SPLIT_TIMEOUT:
45585 + /* Couldn't complete in the nominated frame */
45586 + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
45587 + "- FIQ timed out. Data may have been lost.\n",
45588 + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
45589 + if (hc->ep_type == UE_ISOCHRONOUS) {
45590 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45591 + /* Record errors, update qtd. */
45592 + frame_desc->actual_length = 0;
45593 + if (hc->ep_is_in) {
45594 + frame_desc->status = -DWC_E_NO_STREAM_RES;
45595 + } else {
45596 + frame_desc->status = -DWC_E_COMMUNICATION;
45597 + }
45598 + qtd->isoc_frame_index++;
45599 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45600 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45601 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45602 + } else {
45603 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45604 + }
45605 + } else {
45606 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45607 + }
45608 + break;
45609 +
45610 + default:
45611 + DWC_WARN("Unexpected state received on hc=%d fsm=%d on transfer to device %d ep 0x%x",
45612 + hc->hc_num, st->fsm, hc->dev_addr, hc->ep_num);
45613 + qtd->error_count++;
45614 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45615 + }
45616 + return;
45617 +}
45618 +
45619 +/** Handles interrupt for a specific Host Channel */
45620 +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
45621 +{
45622 + int retval = 0;
45623 + hcint_data_t hcint;
45624 + hcintmsk_data_t hcintmsk;
45625 + dwc_hc_t *hc;
45626 + dwc_otg_hc_regs_t *hc_regs;
45627 + dwc_otg_qtd_t *qtd;
45628 +
45629 + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
45630 +
45631 + hc = dwc_otg_hcd->hc_ptr_array[num];
45632 + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
45633 + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
45634 + /* A dequeue was issued for this transfer. Our QTD has gone away
45635 + * but in the case of a FIQ transfer, the transfer would have run
45636 + * to completion.
45637 + */
45638 + if (fiq_fsm_enable && dwc_otg_hcd->fiq_state->channel[num].fsm != FIQ_PASSTHROUGH) {
45639 + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
45640 + } else {
45641 + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
45642 + }
45643 + return 1;
45644 + }
45645 + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
45646 +
45647 + /*
45648 + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
45649 + * Execution path is fundamentally different for the channels after a FIQ has completed
45650 + * a split transaction.
45651 + */
45652 + if (fiq_fsm_enable) {
45653 + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
45654 + case FIQ_PASSTHROUGH:
45655 + break;
45656 + case FIQ_PASSTHROUGH_ERRORSTATE:
45657 + /* Hook into the error count */
45658 + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
45659 + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
45660 + qtd->error_count = 0;
45661 + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
45662 + }
45663 + break;
45664 + default:
45665 + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
45666 + return 1;
45667 + }
45668 + }
45669 +
45670 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45671 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45672 + hcint.d32 = hcint.d32 & hcintmsk.d32;
45673 + if (!dwc_otg_hcd->core_if->dma_enable) {
45674 + if (hcint.b.chhltd && hcint.d32 != 0x2) {
45675 + hcint.b.chhltd = 0;
45676 + }
45677 + }
45678 +
45679 + if (hcint.b.xfercomp) {
45680 + retval |=
45681 + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45682 + /*
45683 + * If NYET occurred at same time as Xfer Complete, the NYET is
45684 + * handled by the Xfer Complete interrupt handler. Don't want
45685 + * to call the NYET interrupt handler in this case.
45686 + */
45687 + hcint.b.nyet = 0;
45688 + }
45689 + if (hcint.b.chhltd) {
45690 + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45691 + }
45692 + if (hcint.b.ahberr) {
45693 + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45694 + }
45695 + if (hcint.b.stall) {
45696 + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45697 + }
45698 + if (hcint.b.nak) {
45699 + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45700 + }
45701 + if (hcint.b.ack) {
45702 + if(!hcint.b.chhltd)
45703 + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45704 + }
45705 + if (hcint.b.nyet) {
45706 + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45707 + }
45708 + if (hcint.b.xacterr) {
45709 + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45710 + }
45711 + if (hcint.b.bblerr) {
45712 + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45713 + }
45714 + if (hcint.b.frmovrun) {
45715 + retval |=
45716 + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45717 + }
45718 + if (hcint.b.datatglerr) {
45719 + retval |=
45720 + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45721 + }
45722 +
45723 + return retval;
45724 +}
45725 +#endif /* DWC_DEVICE_ONLY */
45726 --- /dev/null
45727 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
45728 @@ -0,0 +1,1086 @@
45729 +
45730 +/* ==========================================================================
45731 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
45732 + * $Revision: #20 $
45733 + * $Date: 2011/10/26 $
45734 + * $Change: 1872981 $
45735 + *
45736 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
45737 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
45738 + * otherwise expressly agreed to in writing between Synopsys and you.
45739 + *
45740 + * The Software IS NOT an item of Licensed Software or Licensed Product under
45741 + * any End User Software License Agreement or Agreement for Licensed Product
45742 + * with Synopsys or any supplement thereto. You are permitted to use and
45743 + * redistribute this Software in source and binary forms, with or without
45744 + * modification, provided that redistributions of source code must retain this
45745 + * notice. You may not view, use, disclose, copy or distribute this file or
45746 + * any information contained herein except pursuant to this license grant from
45747 + * Synopsys. If you do not agree with this notice, including the disclaimer
45748 + * below, then you are not authorized to use the Software.
45749 + *
45750 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
45751 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45752 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45753 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
45754 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45755 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45756 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
45757 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45758 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45759 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45760 + * DAMAGE.
45761 + * ========================================================================== */
45762 +#ifndef DWC_DEVICE_ONLY
45763 +
45764 +/**
45765 + * @file
45766 + *
45767 + * This file contains the implementation of the HCD. In Linux, the HCD
45768 + * implements the hc_driver API.
45769 + */
45770 +#include <linux/kernel.h>
45771 +#include <linux/module.h>
45772 +#include <linux/moduleparam.h>
45773 +#include <linux/init.h>
45774 +#include <linux/device.h>
45775 +#include <linux/errno.h>
45776 +#include <linux/list.h>
45777 +#include <linux/interrupt.h>
45778 +#include <linux/string.h>
45779 +#include <linux/dma-mapping.h>
45780 +#include <linux/version.h>
45781 +#include <asm/io.h>
45782 +#ifdef CONFIG_ARM
45783 +#include <asm/fiq.h>
45784 +#endif
45785 +#include <linux/usb.h>
45786 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
45787 +#include <../drivers/usb/core/hcd.h>
45788 +#else
45789 +#include <linux/usb/hcd.h>
45790 +#endif
45791 +#include <asm/bug.h>
45792 +
45793 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
45794 +#define USB_URB_EP_LINKING 1
45795 +#else
45796 +#define USB_URB_EP_LINKING 0
45797 +#endif
45798 +
45799 +#include "dwc_otg_hcd_if.h"
45800 +#include "dwc_otg_dbg.h"
45801 +#include "dwc_otg_driver.h"
45802 +#include "dwc_otg_hcd.h"
45803 +
45804 +#ifndef __virt_to_bus
45805 +#define __virt_to_bus __virt_to_phys
45806 +#define __bus_to_virt __phys_to_virt
45807 +#define __pfn_to_bus(x) __pfn_to_phys(x)
45808 +#define __bus_to_pfn(x) __phys_to_pfn(x)
45809 +#endif
45810 +
45811 +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
45812 +
45813 +/**
45814 + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
45815 + * qualified with its direction (possible 32 endpoints per device).
45816 + */
45817 +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
45818 + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
45819 +
45820 +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
45821 +
45822 +extern bool fiq_enable;
45823 +
45824 +/** @name Linux HC Driver API Functions */
45825 +/** @{ */
45826 +/* manage i/o requests, device state */
45827 +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
45828 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
45829 + struct usb_host_endpoint *ep,
45830 +#endif
45831 + struct urb *urb, gfp_t mem_flags);
45832 +
45833 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
45834 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
45835 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
45836 +#endif
45837 +#else /* kernels at or post 2.6.30 */
45838 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
45839 + struct urb *urb, int status);
45840 +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
45841 +
45842 +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
45843 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
45844 +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
45845 +#endif
45846 +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
45847 +extern int hcd_start(struct usb_hcd *hcd);
45848 +extern void hcd_stop(struct usb_hcd *hcd);
45849 +static int get_frame_number(struct usb_hcd *hcd);
45850 +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
45851 +extern int hub_control(struct usb_hcd *hcd,
45852 + u16 typeReq,
45853 + u16 wValue, u16 wIndex, char *buf, u16 wLength);
45854 +
45855 +struct wrapper_priv_data {
45856 + dwc_otg_hcd_t *dwc_otg_hcd;
45857 +};
45858 +
45859 +/** @} */
45860 +
45861 +static struct hc_driver dwc_otg_hc_driver = {
45862 +
45863 + .description = dwc_otg_hcd_name,
45864 + .product_desc = "DWC OTG Controller",
45865 + .hcd_priv_size = sizeof(struct wrapper_priv_data),
45866 +
45867 + .irq = dwc_otg_hcd_irq,
45868 +
45869 + .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
45870 +
45871 + //.reset =
45872 + .start = hcd_start,
45873 + //.suspend =
45874 + //.resume =
45875 + .stop = hcd_stop,
45876 +
45877 + .urb_enqueue = dwc_otg_urb_enqueue,
45878 + .urb_dequeue = dwc_otg_urb_dequeue,
45879 + .endpoint_disable = endpoint_disable,
45880 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
45881 + .endpoint_reset = endpoint_reset,
45882 +#endif
45883 + .get_frame_number = get_frame_number,
45884 +
45885 + .hub_status_data = hub_status_data,
45886 + .hub_control = hub_control,
45887 + //.bus_suspend =
45888 + //.bus_resume =
45889 +};
45890 +
45891 +/** Gets the dwc_otg_hcd from a struct usb_hcd */
45892 +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
45893 +{
45894 + struct wrapper_priv_data *p;
45895 + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
45896 + return p->dwc_otg_hcd;
45897 +}
45898 +
45899 +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
45900 +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
45901 +{
45902 + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
45903 +}
45904 +
45905 +/** Gets the usb_host_endpoint associated with an URB. */
45906 +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
45907 +{
45908 + struct usb_device *dev = urb->dev;
45909 + int ep_num = usb_pipeendpoint(urb->pipe);
45910 +
45911 + if (usb_pipein(urb->pipe))
45912 + return dev->ep_in[ep_num];
45913 + else
45914 + return dev->ep_out[ep_num];
45915 +}
45916 +
45917 +static int _disconnect(dwc_otg_hcd_t * hcd)
45918 +{
45919 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
45920 +
45921 + usb_hcd->self.is_b_host = 0;
45922 + return 0;
45923 +}
45924 +
45925 +static int _start(dwc_otg_hcd_t * hcd)
45926 +{
45927 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
45928 +
45929 + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
45930 + hcd_start(usb_hcd);
45931 +
45932 + return 0;
45933 +}
45934 +
45935 +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
45936 + uint32_t * port_addr)
45937 +{
45938 + struct urb *urb = (struct urb *)urb_handle;
45939 + struct usb_bus *bus;
45940 +#if 1 //GRAYG - temporary
45941 + if (NULL == urb_handle)
45942 + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
45943 + if (NULL == urb->dev)
45944 + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
45945 + if (NULL == port_addr)
45946 + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
45947 +#endif
45948 + if (urb->dev->tt) {
45949 + if (NULL == urb->dev->tt->hub) {
45950 + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
45951 + __func__); //GRAYG
45952 + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
45953 + *hub_addr = 0; //GRAYG
45954 + // we probably shouldn't have a transaction translator if
45955 + // there's no associated hub?
45956 + } else {
45957 + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
45958 + if (urb->dev->tt->hub == bus->root_hub)
45959 + *hub_addr = 0;
45960 + else
45961 + *hub_addr = urb->dev->tt->hub->devnum;
45962 + }
45963 + *port_addr = urb->dev->ttport;
45964 + } else {
45965 + *hub_addr = 0;
45966 + *port_addr = urb->dev->ttport;
45967 + }
45968 + return 0;
45969 +}
45970 +
45971 +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
45972 +{
45973 + struct urb *urb = (struct urb *)urb_handle;
45974 + return urb->dev->speed;
45975 +}
45976 +
45977 +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
45978 +{
45979 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
45980 + return usb_hcd->self.b_hnp_enable;
45981 +}
45982 +
45983 +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
45984 + struct urb *urb)
45985 +{
45986 + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
45987 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
45988 + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
45989 + } else {
45990 + hcd_to_bus(hcd)->bandwidth_int_reqs++;
45991 + }
45992 +}
45993 +
45994 +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
45995 + struct urb *urb)
45996 +{
45997 + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
45998 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
45999 + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
46000 + } else {
46001 + hcd_to_bus(hcd)->bandwidth_int_reqs--;
46002 + }
46003 +}
46004 +
46005 +/**
46006 + * Sets the final status of an URB and returns it to the device driver. Any
46007 + * required cleanup of the URB is performed. The HCD lock should be held on
46008 + * entry.
46009 + */
46010 +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
46011 + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
46012 +{
46013 + struct urb *urb = (struct urb *)urb_handle;
46014 + urb_tq_entry_t *new_entry;
46015 + int rc = 0;
46016 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46017 + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
46018 + __func__, urb, usb_pipedevice(urb->pipe),
46019 + usb_pipeendpoint(urb->pipe),
46020 + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
46021 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46022 + int i;
46023 + for (i = 0; i < urb->number_of_packets; i++) {
46024 + DWC_PRINTF(" ISO Desc %d status: %d\n",
46025 + i, urb->iso_frame_desc[i].status);
46026 + }
46027 + }
46028 + }
46029 + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
46030 + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
46031 + /* Convert status value. */
46032 + switch (status) {
46033 + case -DWC_E_PROTOCOL:
46034 + status = -EPROTO;
46035 + break;
46036 + case -DWC_E_IN_PROGRESS:
46037 + status = -EINPROGRESS;
46038 + break;
46039 + case -DWC_E_PIPE:
46040 + status = -EPIPE;
46041 + break;
46042 + case -DWC_E_IO:
46043 + status = -EIO;
46044 + break;
46045 + case -DWC_E_TIMEOUT:
46046 + status = -ETIMEDOUT;
46047 + break;
46048 + case -DWC_E_OVERFLOW:
46049 + status = -EOVERFLOW;
46050 + break;
46051 + case -DWC_E_SHUTDOWN:
46052 + status = -ESHUTDOWN;
46053 + break;
46054 + default:
46055 + if (status) {
46056 + DWC_PRINTF("Uknown urb status %d\n", status);
46057 +
46058 + }
46059 + }
46060 +
46061 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46062 + int i;
46063 +
46064 + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
46065 + urb->actual_length = 0;
46066 + for (i = 0; i < urb->number_of_packets; ++i) {
46067 + urb->iso_frame_desc[i].actual_length =
46068 + dwc_otg_hcd_urb_get_iso_desc_actual_length
46069 + (dwc_otg_urb, i);
46070 + urb->actual_length += urb->iso_frame_desc[i].actual_length;
46071 + urb->iso_frame_desc[i].status =
46072 + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
46073 + }
46074 + }
46075 +
46076 + urb->status = status;
46077 + urb->hcpriv = NULL;
46078 + if (!status) {
46079 + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
46080 + (urb->actual_length < urb->transfer_buffer_length)) {
46081 + urb->status = -EREMOTEIO;
46082 + }
46083 + }
46084 +
46085 + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
46086 + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
46087 + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
46088 + if (ep) {
46089 + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
46090 + dwc_otg_hcd_get_ep_bandwidth(hcd,
46091 + ep->hcpriv),
46092 + urb);
46093 + }
46094 + }
46095 + DWC_FREE(dwc_otg_urb);
46096 + if (!new_entry) {
46097 + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
46098 + urb->status = -EPROTO;
46099 + /* don't schedule the tasklet -
46100 + * directly return the packet here with error. */
46101 +#if USB_URB_EP_LINKING
46102 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
46103 +#endif
46104 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46105 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
46106 +#else
46107 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
46108 +#endif
46109 + } else {
46110 + new_entry->urb = urb;
46111 +#if USB_URB_EP_LINKING
46112 + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
46113 + if(0 == rc) {
46114 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
46115 + }
46116 +#endif
46117 + if(0 == rc) {
46118 + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
46119 + urb_tq_entries);
46120 + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
46121 + }
46122 + }
46123 + return 0;
46124 +}
46125 +
46126 +static struct dwc_otg_hcd_function_ops hcd_fops = {
46127 + .start = _start,
46128 + .disconnect = _disconnect,
46129 + .hub_info = _hub_info,
46130 + .speed = _speed,
46131 + .complete = _complete,
46132 + .get_b_hnp_enable = _get_b_hnp_enable,
46133 +};
46134 +
46135 +#ifdef CONFIG_ARM64
46136 +
46137 +static int simfiq_irq = -1;
46138 +
46139 +void local_fiq_enable(void)
46140 +{
46141 + if (simfiq_irq >= 0)
46142 + enable_irq(simfiq_irq);
46143 +}
46144 +
46145 +void local_fiq_disable(void)
46146 +{
46147 + if (simfiq_irq >= 0)
46148 + disable_irq(simfiq_irq);
46149 +}
46150 +
46151 +irqreturn_t fiq_irq_handler(int irq, void *dev_id)
46152 +{
46153 + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *)dev_id;
46154 +
46155 + if (fiq_fsm_enable)
46156 + dwc_otg_fiq_fsm(dwc_otg_hcd->fiq_state, dwc_otg_hcd->core_if->core_params->host_channels);
46157 + else
46158 + dwc_otg_fiq_nop(dwc_otg_hcd->fiq_state);
46159 +
46160 + return IRQ_HANDLED;
46161 +}
46162 +
46163 +#else
46164 +static struct fiq_handler fh = {
46165 + .name = "usb_fiq",
46166 +};
46167 +
46168 +#endif
46169 +
46170 +static void hcd_init_fiq(void *cookie)
46171 +{
46172 + dwc_otg_device_t *otg_dev = cookie;
46173 + dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
46174 +#ifdef CONFIG_ARM64
46175 + int retval = 0;
46176 + int irq;
46177 +#else
46178 + struct pt_regs regs;
46179 + int irq;
46180 +
46181 + if (claim_fiq(&fh)) {
46182 + DWC_ERROR("Can't claim FIQ");
46183 + BUG();
46184 + }
46185 + DWC_WARN("FIQ on core %d", smp_processor_id());
46186 + DWC_WARN("FIQ ASM at %px length %d", &_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
46187 + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
46188 + memset(&regs,0,sizeof(regs));
46189 +
46190 + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
46191 + if (fiq_fsm_enable) {
46192 + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
46193 + //regs.ARM_r10 = dwc_otg_hcd->dma;
46194 + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
46195 + } else {
46196 + regs.ARM_fp = (long) dwc_otg_fiq_nop;
46197 + }
46198 +
46199 + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
46200 +
46201 +// __show_regs(&regs);
46202 + set_fiq_regs(&regs);
46203 +#endif
46204 +
46205 + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
46206 + //Set the mphi periph to the required registers
46207 + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
46208 + if (otg_dev->os_dep.use_swirq) {
46209 + dwc_otg_hcd->fiq_state->mphi_regs.swirq_set =
46210 + otg_dev->os_dep.mphi_base + 0x1f0;
46211 + dwc_otg_hcd->fiq_state->mphi_regs.swirq_clr =
46212 + otg_dev->os_dep.mphi_base + 0x1f4;
46213 + DWC_WARN("Fake MPHI regs_base at %px",
46214 + dwc_otg_hcd->fiq_state->mphi_regs.base);
46215 + } else {
46216 + dwc_otg_hcd->fiq_state->mphi_regs.ctrl =
46217 + otg_dev->os_dep.mphi_base + 0x4c;
46218 + dwc_otg_hcd->fiq_state->mphi_regs.outdda
46219 + = otg_dev->os_dep.mphi_base + 0x28;
46220 + dwc_otg_hcd->fiq_state->mphi_regs.outddb
46221 + = otg_dev->os_dep.mphi_base + 0x2c;
46222 + dwc_otg_hcd->fiq_state->mphi_regs.intstat
46223 + = otg_dev->os_dep.mphi_base + 0x50;
46224 + DWC_WARN("MPHI regs_base at %px",
46225 + dwc_otg_hcd->fiq_state->mphi_regs.base);
46226 +
46227 + //Enable mphi peripheral
46228 + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
46229 +#ifdef DEBUG
46230 + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
46231 + DWC_WARN("MPHI periph has been enabled");
46232 + else
46233 + DWC_WARN("MPHI periph has NOT been enabled");
46234 +#endif
46235 + }
46236 + // Enable FIQ interrupt from USB peripheral
46237 +#ifdef CONFIG_ARM64
46238 + irq = otg_dev->os_dep.fiq_num;
46239 +
46240 + if (irq < 0) {
46241 + DWC_ERROR("Can't get SIM-FIQ irq");
46242 + return;
46243 + }
46244 +
46245 + retval = request_irq(irq, fiq_irq_handler, 0, "dwc_otg_sim-fiq", dwc_otg_hcd);
46246 +
46247 + if (retval < 0) {
46248 + DWC_ERROR("Unable to request SIM-FIQ irq\n");
46249 + return;
46250 + }
46251 +
46252 + simfiq_irq = irq;
46253 +#else
46254 +#ifdef CONFIG_GENERIC_IRQ_MULTI_HANDLER
46255 + irq = otg_dev->os_dep.fiq_num;
46256 +#else
46257 + irq = INTERRUPT_VC_USB;
46258 +#endif
46259 + if (irq < 0) {
46260 + DWC_ERROR("Can't get FIQ irq");
46261 + return;
46262 + }
46263 + /*
46264 + * We could take an interrupt immediately after enabling the FIQ.
46265 + * Ensure coherency of hcd->fiq_state.
46266 + */
46267 + smp_mb();
46268 + enable_fiq(irq);
46269 + local_fiq_enable();
46270 +#endif
46271 +
46272 +}
46273 +
46274 +/**
46275 + * Initializes the HCD. This function allocates memory for and initializes the
46276 + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
46277 + * USB bus with the core and calls the hc_driver->start() function. It returns
46278 + * a negative error on failure.
46279 + */
46280 +int hcd_init(dwc_bus_dev_t *_dev)
46281 +{
46282 + struct usb_hcd *hcd = NULL;
46283 + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
46284 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
46285 + int retval = 0;
46286 + u64 dmamask;
46287 +
46288 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
46289 +
46290 + /* Set device flags indicating whether the HCD supports DMA. */
46291 + if (dwc_otg_is_dma_enable(otg_dev->core_if))
46292 + dmamask = DMA_BIT_MASK(32);
46293 + else
46294 + dmamask = 0;
46295 +
46296 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
46297 + dma_set_mask(&_dev->dev, dmamask);
46298 + dma_set_coherent_mask(&_dev->dev, dmamask);
46299 +#elif defined(PCI_INTERFACE)
46300 + pci_set_dma_mask(_dev, dmamask);
46301 + pci_set_consistent_dma_mask(_dev, dmamask);
46302 +#endif
46303 +
46304 + /*
46305 + * Allocate memory for the base HCD plus the DWC OTG HCD.
46306 + * Initialize the base HCD.
46307 + */
46308 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
46309 + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
46310 +#else
46311 + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
46312 + hcd->has_tt = 1;
46313 +// hcd->uses_new_polling = 1;
46314 +// hcd->poll_rh = 0;
46315 +#endif
46316 + if (!hcd) {
46317 + retval = -ENOMEM;
46318 + goto error1;
46319 + }
46320 +
46321 + hcd->regs = otg_dev->os_dep.base;
46322 +
46323 +
46324 + /* Initialize the DWC OTG HCD. */
46325 + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
46326 + if (!dwc_otg_hcd) {
46327 + goto error2;
46328 + }
46329 + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
46330 + dwc_otg_hcd;
46331 + otg_dev->hcd = dwc_otg_hcd;
46332 + otg_dev->hcd->otg_dev = otg_dev;
46333 +
46334 +#ifdef CONFIG_ARM64
46335 + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if))
46336 + goto error2;
46337 +
46338 + if (fiq_enable)
46339 + hcd_init_fiq(otg_dev);
46340 +#else
46341 + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
46342 + goto error2;
46343 + }
46344 +
46345 + if (fiq_enable) {
46346 + if (num_online_cpus() > 1) {
46347 + /*
46348 + * bcm2709: can run the FIQ on a separate core to IRQs.
46349 + * Ensure driver state is visible to other cores before setting up the FIQ.
46350 + */
46351 + smp_mb();
46352 + smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
46353 + } else {
46354 + smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
46355 + }
46356 + }
46357 +#endif
46358 +
46359 + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
46360 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
46361 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
46362 + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
46363 +#endif
46364 + /* Don't support SG list at this point */
46365 + hcd->self.sg_tablesize = 0;
46366 +#endif
46367 + /*
46368 + * Finish generic HCD initialization and start the HCD. This function
46369 + * allocates the DMA buffer pool, registers the USB bus, requests the
46370 + * IRQ line, and calls hcd_start method.
46371 + */
46372 + retval = usb_add_hcd(hcd, otg_dev->os_dep.irq_num, IRQF_SHARED);
46373 + if (retval < 0) {
46374 + goto error2;
46375 + }
46376 +
46377 + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
46378 + return 0;
46379 +
46380 +error2:
46381 + usb_put_hcd(hcd);
46382 +error1:
46383 + return retval;
46384 +}
46385 +
46386 +/**
46387 + * Removes the HCD.
46388 + * Frees memory and resources associated with the HCD and deregisters the bus.
46389 + */
46390 +void hcd_remove(dwc_bus_dev_t *_dev)
46391 +{
46392 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
46393 + dwc_otg_hcd_t *dwc_otg_hcd;
46394 + struct usb_hcd *hcd;
46395 +
46396 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
46397 +
46398 + if (!otg_dev) {
46399 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
46400 + return;
46401 + }
46402 +
46403 + dwc_otg_hcd = otg_dev->hcd;
46404 +
46405 + if (!dwc_otg_hcd) {
46406 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
46407 + return;
46408 + }
46409 +
46410 + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
46411 +
46412 + if (!hcd) {
46413 + DWC_DEBUGPL(DBG_ANY,
46414 + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
46415 + __func__);
46416 + return;
46417 + }
46418 + usb_remove_hcd(hcd);
46419 + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
46420 + dwc_otg_hcd_remove(dwc_otg_hcd);
46421 + usb_put_hcd(hcd);
46422 +}
46423 +
46424 +/* =========================================================================
46425 + * Linux HC Driver Functions
46426 + * ========================================================================= */
46427 +
46428 +/** Initializes the DWC_otg controller and its root hub and prepares it for host
46429 + * mode operation. Activates the root port. Returns 0 on success and a negative
46430 + * error code on failure. */
46431 +int hcd_start(struct usb_hcd *hcd)
46432 +{
46433 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46434 + struct usb_bus *bus;
46435 +
46436 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
46437 + bus = hcd_to_bus(hcd);
46438 +
46439 + hcd->state = HC_STATE_RUNNING;
46440 + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
46441 + return 0;
46442 + }
46443 +
46444 + /* Initialize and connect root hub if one is not already attached */
46445 + if (bus->root_hub) {
46446 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
46447 + /* Inform the HUB driver to resume. */
46448 + usb_hcd_resume_root_hub(hcd);
46449 + }
46450 +
46451 + return 0;
46452 +}
46453 +
46454 +/**
46455 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
46456 + * stopped.
46457 + */
46458 +void hcd_stop(struct usb_hcd *hcd)
46459 +{
46460 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46461 +
46462 + dwc_otg_hcd_stop(dwc_otg_hcd);
46463 +}
46464 +
46465 +/** Returns the current frame number. */
46466 +static int get_frame_number(struct usb_hcd *hcd)
46467 +{
46468 + hprt0_data_t hprt0;
46469 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46470 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
46471 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
46472 + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
46473 + else
46474 + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
46475 +}
46476 +
46477 +#ifdef DEBUG
46478 +static void dump_urb_info(struct urb *urb, char *fn_name)
46479 +{
46480 + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
46481 + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
46482 + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
46483 + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
46484 + DWC_PRINTF(" Endpoint type: %s\n", ( {
46485 + char *pipetype;
46486 + switch (usb_pipetype(urb->pipe)) {
46487 +case PIPE_CONTROL:
46488 +pipetype = "CONTROL"; break; case PIPE_BULK:
46489 +pipetype = "BULK"; break; case PIPE_INTERRUPT:
46490 +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
46491 +pipetype = "ISOCHRONOUS"; break; default:
46492 + pipetype = "UNKNOWN"; break;};
46493 + pipetype;}
46494 + )) ;
46495 + DWC_PRINTF(" Speed: %s\n", ( {
46496 + char *speed; switch (urb->dev->speed) {
46497 +case USB_SPEED_HIGH:
46498 +speed = "HIGH"; break; case USB_SPEED_FULL:
46499 +speed = "FULL"; break; case USB_SPEED_LOW:
46500 +speed = "LOW"; break; default:
46501 + speed = "UNKNOWN"; break;};
46502 + speed;}
46503 + )) ;
46504 + DWC_PRINTF(" Max packet size: %d\n",
46505 + usb_maxpacket(urb->dev, urb->pipe);
46506 + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
46507 + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
46508 + urb->transfer_buffer, (void *)urb->transfer_dma);
46509 + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
46510 + urb->setup_packet, (void *)urb->setup_dma);
46511 + DWC_PRINTF(" Interval: %d\n", urb->interval);
46512 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46513 + int i;
46514 + for (i = 0; i < urb->number_of_packets; i++) {
46515 + DWC_PRINTF(" ISO Desc %d:\n", i);
46516 + DWC_PRINTF(" offset: %d, length %d\n",
46517 + urb->iso_frame_desc[i].offset,
46518 + urb->iso_frame_desc[i].length);
46519 + }
46520 + }
46521 +}
46522 +#endif
46523 +
46524 +/** Starts processing a USB transfer request specified by a USB Request Block
46525 + * (URB). mem_flags indicates the type of memory allocation to use while
46526 + * processing this URB. */
46527 +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
46528 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46529 + struct usb_host_endpoint *ep,
46530 +#endif
46531 + struct urb *urb, gfp_t mem_flags)
46532 +{
46533 + int retval = 0;
46534 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
46535 + struct usb_host_endpoint *ep = urb->ep;
46536 +#endif
46537 + dwc_irqflags_t irqflags;
46538 + void **ref_ep_hcpriv = &ep->hcpriv;
46539 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46540 + dwc_otg_hcd_urb_t *dwc_otg_urb;
46541 + int i;
46542 + int alloc_bandwidth = 0;
46543 + uint8_t ep_type = 0;
46544 + uint32_t flags = 0;
46545 + void *buf;
46546 +
46547 +#ifdef DEBUG
46548 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46549 + dump_urb_info(urb, "dwc_otg_urb_enqueue");
46550 + }
46551 +#endif
46552 + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
46553 + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
46554 + if (!dwc_otg_hcd_is_bandwidth_allocated
46555 + (dwc_otg_hcd, ref_ep_hcpriv)) {
46556 + alloc_bandwidth = 1;
46557 + }
46558 + }
46559 +
46560 + switch (usb_pipetype(urb->pipe)) {
46561 + case PIPE_CONTROL:
46562 + ep_type = USB_ENDPOINT_XFER_CONTROL;
46563 + break;
46564 + case PIPE_ISOCHRONOUS:
46565 + ep_type = USB_ENDPOINT_XFER_ISOC;
46566 + break;
46567 + case PIPE_BULK:
46568 + ep_type = USB_ENDPOINT_XFER_BULK;
46569 + break;
46570 + case PIPE_INTERRUPT:
46571 + ep_type = USB_ENDPOINT_XFER_INT;
46572 + break;
46573 + default:
46574 + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
46575 + }
46576 +
46577 + /* # of packets is often 0 - do we really need to call this then? */
46578 + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
46579 + urb->number_of_packets,
46580 + mem_flags == GFP_ATOMIC ? 1 : 0);
46581 +
46582 + if(dwc_otg_urb == NULL)
46583 + return -ENOMEM;
46584 +
46585 + if (!dwc_otg_urb && urb->number_of_packets)
46586 + return -ENOMEM;
46587 +
46588 + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
46589 + usb_pipeendpoint(urb->pipe), ep_type,
46590 + usb_pipein(urb->pipe),
46591 + usb_maxpacket(urb->dev, urb->pipe));
46592 +
46593 + buf = urb->transfer_buffer;
46594 + if (hcd_uses_dma(hcd) && !buf && urb->transfer_buffer_length) {
46595 + /*
46596 + * Calculate virtual address from physical address,
46597 + * because some class driver may not fill transfer_buffer.
46598 + * In Buffer DMA mode virual address is used,
46599 + * when handling non DWORD aligned buffers.
46600 + */
46601 + buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
46602 + dev_warn_once(&urb->dev->dev,
46603 + "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
46604 + &urb->transfer_dma, buf);
46605 + }
46606 +
46607 + if (!buf && urb->transfer_buffer_length) {
46608 + DWC_FREE(dwc_otg_urb);
46609 + DWC_ERROR("transfer_buffer is NULL in PIO mode or both "
46610 + "transfer_buffer and transfer_dma are NULL in DMA mode\n");
46611 + return -EINVAL;
46612 + }
46613 +
46614 + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
46615 + flags |= URB_GIVEBACK_ASAP;
46616 + if (urb->transfer_flags & URB_ZERO_PACKET)
46617 + flags |= URB_SEND_ZERO_PACKET;
46618 +
46619 + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
46620 + urb->transfer_dma,
46621 + urb->transfer_buffer_length,
46622 + urb->setup_packet,
46623 + urb->setup_dma, flags, urb->interval);
46624 +
46625 + for (i = 0; i < urb->number_of_packets; ++i) {
46626 + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
46627 + urb->
46628 + iso_frame_desc[i].offset,
46629 + urb->
46630 + iso_frame_desc[i].length);
46631 + }
46632 +
46633 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
46634 + urb->hcpriv = dwc_otg_urb;
46635 +#if USB_URB_EP_LINKING
46636 + retval = usb_hcd_link_urb_to_ep(hcd, urb);
46637 + if (0 == retval)
46638 +#endif
46639 + {
46640 + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
46641 + /*(dwc_otg_qh_t **)*/
46642 + ref_ep_hcpriv, 1);
46643 + if (0 == retval) {
46644 + if (alloc_bandwidth) {
46645 + allocate_bus_bandwidth(hcd,
46646 + dwc_otg_hcd_get_ep_bandwidth(
46647 + dwc_otg_hcd, *ref_ep_hcpriv),
46648 + urb);
46649 + }
46650 + } else {
46651 + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
46652 +#if USB_URB_EP_LINKING
46653 + usb_hcd_unlink_urb_from_ep(hcd, urb);
46654 +#endif
46655 + DWC_FREE(dwc_otg_urb);
46656 + urb->hcpriv = NULL;
46657 + if (retval == -DWC_E_NO_DEVICE)
46658 + retval = -ENODEV;
46659 + }
46660 + }
46661 +#if USB_URB_EP_LINKING
46662 + else
46663 + {
46664 + DWC_FREE(dwc_otg_urb);
46665 + urb->hcpriv = NULL;
46666 + }
46667 +#endif
46668 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
46669 + return retval;
46670 +}
46671 +
46672 +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
46673 + * success. */
46674 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46675 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
46676 +#else
46677 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
46678 +#endif
46679 +{
46680 + dwc_irqflags_t flags;
46681 + dwc_otg_hcd_t *dwc_otg_hcd;
46682 + int rc;
46683 +
46684 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
46685 +
46686 + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46687 +
46688 +#ifdef DEBUG
46689 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46690 + dump_urb_info(urb, "dwc_otg_urb_dequeue");
46691 + }
46692 +#endif
46693 +
46694 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
46695 + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
46696 + if (0 == rc) {
46697 + if(urb->hcpriv != NULL) {
46698 + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
46699 + (dwc_otg_hcd_urb_t *)urb->hcpriv);
46700 +
46701 + DWC_FREE(urb->hcpriv);
46702 + urb->hcpriv = NULL;
46703 + }
46704 + }
46705 +
46706 + if (0 == rc) {
46707 + /* Higher layer software sets URB status. */
46708 +#if USB_URB_EP_LINKING
46709 + usb_hcd_unlink_urb_from_ep(hcd, urb);
46710 +#endif
46711 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46712 +
46713 +
46714 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46715 + usb_hcd_giveback_urb(hcd, urb);
46716 +#else
46717 + usb_hcd_giveback_urb(hcd, urb, status);
46718 +#endif
46719 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46720 + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
46721 + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
46722 + }
46723 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
46724 + } else {
46725 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46726 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
46727 + rc);
46728 + }
46729 +
46730 + return rc;
46731 +}
46732 +
46733 +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
46734 + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
46735 + * must already be dequeued. */
46736 +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
46737 +{
46738 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46739 +
46740 + DWC_DEBUGPL(DBG_HCD,
46741 + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
46742 + "endpoint=%d\n", ep->desc.bEndpointAddress,
46743 + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
46744 + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
46745 + ep->hcpriv = NULL;
46746 +}
46747 +
46748 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
46749 +/* Resets endpoint specific parameter values, in current version used to reset
46750 + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
46751 +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
46752 +{
46753 + dwc_irqflags_t flags;
46754 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46755 +
46756 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n",
46757 + ep->desc.bEndpointAddress);
46758 +
46759 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
46760 + if (ep->hcpriv) {
46761 + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
46762 + }
46763 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46764 +}
46765 +#endif
46766 +
46767 +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
46768 + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
46769 + * interrupt.
46770 + *
46771 + * This function is called by the USB core when an interrupt occurs */
46772 +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
46773 +{
46774 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46775 + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
46776 + if (retval != 0) {
46777 + S3C2410X_CLEAR_EINTPEND();
46778 + }
46779 + return IRQ_RETVAL(retval);
46780 +}
46781 +
46782 +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
46783 + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
46784 + * is the status change indicator for the single root port. Returns 1 if either
46785 + * change indicator is 1, otherwise returns 0. */
46786 +int hub_status_data(struct usb_hcd *hcd, char *buf)
46787 +{
46788 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46789 +
46790 + buf[0] = 0;
46791 + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
46792 +
46793 + return (buf[0] != 0);
46794 +}
46795 +
46796 +/** Handles hub class-specific requests. */
46797 +int hub_control(struct usb_hcd *hcd,
46798 + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
46799 +{
46800 + int retval;
46801 +
46802 + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
46803 + typeReq, wValue, wIndex, buf, wLength);
46804 +
46805 + switch (retval) {
46806 + case -DWC_E_INVALID:
46807 + retval = -EINVAL;
46808 + break;
46809 + }
46810 +
46811 + return retval;
46812 +}
46813 +
46814 +#endif /* DWC_DEVICE_ONLY */
46815 --- /dev/null
46816 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
46817 @@ -0,0 +1,974 @@
46818 +/* ==========================================================================
46819 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
46820 + * $Revision: #44 $
46821 + * $Date: 2011/10/26 $
46822 + * $Change: 1873028 $
46823 + *
46824 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
46825 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
46826 + * otherwise expressly agreed to in writing between Synopsys and you.
46827 + *
46828 + * The Software IS NOT an item of Licensed Software or Licensed Product under
46829 + * any End User Software License Agreement or Agreement for Licensed Product
46830 + * with Synopsys or any supplement thereto. You are permitted to use and
46831 + * redistribute this Software in source and binary forms, with or without
46832 + * modification, provided that redistributions of source code must retain this
46833 + * notice. You may not view, use, disclose, copy or distribute this file or
46834 + * any information contained herein except pursuant to this license grant from
46835 + * Synopsys. If you do not agree with this notice, including the disclaimer
46836 + * below, then you are not authorized to use the Software.
46837 + *
46838 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
46839 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46840 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46841 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
46842 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46843 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46844 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46845 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46846 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46847 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46848 + * DAMAGE.
46849 + * ========================================================================== */
46850 +#ifndef DWC_DEVICE_ONLY
46851 +
46852 +/**
46853 + * @file
46854 + *
46855 + * This file contains the functions to manage Queue Heads and Queue
46856 + * Transfer Descriptors.
46857 + */
46858 +
46859 +#include "dwc_otg_hcd.h"
46860 +#include "dwc_otg_regs.h"
46861 +
46862 +extern bool microframe_schedule;
46863 +extern unsigned short int_ep_interval_min;
46864 +
46865 +/**
46866 + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
46867 + * removed from a list. QTD list should already be empty if called from URB
46868 + * Dequeue.
46869 + *
46870 + * @param hcd HCD instance.
46871 + * @param qh The QH to free.
46872 + */
46873 +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
46874 +{
46875 + dwc_otg_qtd_t *qtd, *qtd_tmp;
46876 + dwc_irqflags_t flags;
46877 + uint32_t buf_size = 0;
46878 + uint8_t *align_buf_virt = NULL;
46879 + dwc_dma_t align_buf_dma;
46880 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
46881 +
46882 + /* Free each QTD in the QTD list */
46883 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
46884 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
46885 + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
46886 + dwc_otg_hcd_qtd_free(qtd);
46887 + }
46888 +
46889 + if (hcd->core_if->dma_desc_enable) {
46890 + dwc_otg_hcd_qh_free_ddma(hcd, qh);
46891 + } else if (qh->dw_align_buf) {
46892 + if (qh->ep_type == UE_ISOCHRONOUS) {
46893 + buf_size = 4096;
46894 + } else {
46895 + buf_size = hcd->core_if->core_params->max_transfer_size;
46896 + }
46897 + align_buf_virt = qh->dw_align_buf;
46898 + align_buf_dma = qh->dw_align_buf_dma;
46899 + }
46900 +
46901 + DWC_FREE(qh);
46902 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
46903 + if (align_buf_virt)
46904 + DWC_DMA_FREE(dev, buf_size, align_buf_virt, align_buf_dma);
46905 + return;
46906 +}
46907 +
46908 +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
46909 +#define HS_HOST_DELAY 5 /* nanoseconds */
46910 +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
46911 +#define HUB_LS_SETUP 333 /* nanoseconds */
46912 +#define NS_TO_US(ns) ((ns + 500) / 1000)
46913 + /* convert & round nanoseconds to microseconds */
46914 +
46915 +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
46916 +{
46917 + unsigned long retval;
46918 +
46919 + switch (speed) {
46920 + case USB_SPEED_HIGH:
46921 + if (is_isoc) {
46922 + retval =
46923 + ((38 * 8 * 2083) +
46924 + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
46925 + HS_HOST_DELAY;
46926 + } else {
46927 + retval =
46928 + ((55 * 8 * 2083) +
46929 + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
46930 + HS_HOST_DELAY;
46931 + }
46932 + break;
46933 + case USB_SPEED_FULL:
46934 + if (is_isoc) {
46935 + retval =
46936 + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
46937 + if (is_in) {
46938 + retval = 7268 + FS_LS_HOST_DELAY + retval;
46939 + } else {
46940 + retval = 6265 + FS_LS_HOST_DELAY + retval;
46941 + }
46942 + } else {
46943 + retval =
46944 + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
46945 + retval = 9107 + FS_LS_HOST_DELAY + retval;
46946 + }
46947 + break;
46948 + case USB_SPEED_LOW:
46949 + if (is_in) {
46950 + retval =
46951 + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
46952 + 1000;
46953 + retval =
46954 + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
46955 + retval;
46956 + } else {
46957 + retval =
46958 + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
46959 + 1000;
46960 + retval =
46961 + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
46962 + retval;
46963 + }
46964 + break;
46965 + default:
46966 + DWC_WARN("Unknown device speed\n");
46967 + retval = -1;
46968 + }
46969 +
46970 + return NS_TO_US(retval);
46971 +}
46972 +
46973 +/**
46974 + * Initializes a QH structure.
46975 + *
46976 + * @param hcd The HCD state structure for the DWC OTG controller.
46977 + * @param qh The QH to init.
46978 + * @param urb Holds the information about the device/endpoint that we need
46979 + * to initialize the QH.
46980 + */
46981 +#define SCHEDULE_SLOP 10
46982 +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
46983 +{
46984 + char *speed, *type;
46985 + int dev_speed;
46986 + uint32_t hub_addr, hub_port;
46987 + hprt0_data_t hprt;
46988 +
46989 + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
46990 + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
46991 +
46992 + /* Initialize QH */
46993 + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
46994 + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
46995 +
46996 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
46997 + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
46998 + DWC_CIRCLEQ_INIT(&qh->qtd_list);
46999 + DWC_LIST_INIT(&qh->qh_list_entry);
47000 + qh->channel = NULL;
47001 +
47002 + /* FS/LS Enpoint on HS Hub
47003 + * NOT virtual root hub */
47004 + dev_speed = hcd->fops->speed(hcd, urb->priv);
47005 +
47006 + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
47007 + qh->do_split = 0;
47008 + if (microframe_schedule)
47009 + qh->speed = dev_speed;
47010 +
47011 + qh->nak_frame = 0xffff;
47012 +
47013 + if (hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED &&
47014 + dev_speed != USB_SPEED_HIGH) {
47015 + DWC_DEBUGPL(DBG_HCD,
47016 + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
47017 + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
47018 + hub_port);
47019 + qh->do_split = 1;
47020 + qh->skip_count = 0;
47021 + }
47022 +
47023 + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
47024 + /* Compute scheduling parameters once and save them. */
47025 +
47026 + /** @todo Account for split transfers in the bus time. */
47027 + int bytecount =
47028 + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
47029 +
47030 + qh->usecs =
47031 + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
47032 + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
47033 + bytecount);
47034 + /* Start in a slightly future (micro)frame. */
47035 + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
47036 + SCHEDULE_SLOP);
47037 + qh->interval = urb->interval;
47038 +
47039 + if (hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) {
47040 + if (dev_speed == USB_SPEED_LOW ||
47041 + dev_speed == USB_SPEED_FULL) {
47042 + qh->interval *= 8;
47043 + qh->sched_frame |= 0x7;
47044 + qh->start_split_frame = qh->sched_frame;
47045 + } else if (int_ep_interval_min >= 2 &&
47046 + qh->interval < int_ep_interval_min &&
47047 + qh->ep_type == UE_INTERRUPT) {
47048 + qh->interval = int_ep_interval_min;
47049 + }
47050 + }
47051 + }
47052 +
47053 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
47054 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
47055 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
47056 + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
47057 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
47058 + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
47059 + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
47060 + switch (dev_speed) {
47061 + case USB_SPEED_LOW:
47062 + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
47063 + speed = "low";
47064 + break;
47065 + case USB_SPEED_FULL:
47066 + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
47067 + speed = "full";
47068 + break;
47069 + case USB_SPEED_HIGH:
47070 + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
47071 + speed = "high";
47072 + break;
47073 + default:
47074 + speed = "?";
47075 + break;
47076 + }
47077 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
47078 +
47079 + switch (qh->ep_type) {
47080 + case UE_ISOCHRONOUS:
47081 + type = "isochronous";
47082 + break;
47083 + case UE_INTERRUPT:
47084 + type = "interrupt";
47085 + break;
47086 + case UE_CONTROL:
47087 + type = "control";
47088 + break;
47089 + case UE_BULK:
47090 + type = "bulk";
47091 + break;
47092 + default:
47093 + type = "?";
47094 + break;
47095 + }
47096 +
47097 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
47098 +
47099 +#ifdef DEBUG
47100 + if (qh->ep_type == UE_INTERRUPT) {
47101 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
47102 + qh->usecs);
47103 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
47104 + qh->interval);
47105 + }
47106 +#endif
47107 +
47108 +}
47109 +
47110 +/**
47111 + * This function allocates and initializes a QH.
47112 + *
47113 + * @param hcd The HCD state structure for the DWC OTG controller.
47114 + * @param urb Holds the information about the device/endpoint that we need
47115 + * to initialize the QH.
47116 + * @param atomic_alloc Flag to do atomic allocation if needed
47117 + *
47118 + * @return Returns pointer to the newly allocated QH, or NULL on error. */
47119 +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
47120 + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
47121 +{
47122 + dwc_otg_qh_t *qh;
47123 +
47124 + /* Allocate memory */
47125 + /** @todo add memflags argument */
47126 + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
47127 + if (qh == NULL) {
47128 + DWC_ERROR("qh allocation failed");
47129 + return NULL;
47130 + }
47131 +
47132 + qh_init(hcd, qh, urb);
47133 +
47134 + if (hcd->core_if->dma_desc_enable
47135 + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
47136 + dwc_otg_hcd_qh_free(hcd, qh);
47137 + return NULL;
47138 + }
47139 +
47140 + return qh;
47141 +}
47142 +
47143 +/* microframe_schedule=0 start */
47144 +
47145 +/**
47146 + * Checks that a channel is available for a periodic transfer.
47147 + *
47148 + * @return 0 if successful, negative error code otherise.
47149 + */
47150 +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
47151 +{
47152 + /*
47153 + * Currently assuming that there is a dedicated host channnel for each
47154 + * periodic transaction plus at least one host channel for
47155 + * non-periodic transactions.
47156 + */
47157 + int status;
47158 + int num_channels;
47159 +
47160 + num_channels = hcd->core_if->core_params->host_channels;
47161 + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
47162 + && (hcd->periodic_channels < num_channels - 1)) {
47163 + status = 0;
47164 + } else {
47165 + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
47166 + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
47167 + status = -DWC_E_NO_SPACE;
47168 + }
47169 +
47170 + return status;
47171 +}
47172 +
47173 +/**
47174 + * Checks that there is sufficient bandwidth for the specified QH in the
47175 + * periodic schedule. For simplicity, this calculation assumes that all the
47176 + * transfers in the periodic schedule may occur in the same (micro)frame.
47177 + *
47178 + * @param hcd The HCD state structure for the DWC OTG controller.
47179 + * @param qh QH containing periodic bandwidth required.
47180 + *
47181 + * @return 0 if successful, negative error code otherwise.
47182 + */
47183 +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47184 +{
47185 + int status;
47186 + int16_t max_claimed_usecs;
47187 +
47188 + status = 0;
47189 +
47190 + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
47191 + /*
47192 + * High speed mode.
47193 + * Max periodic usecs is 80% x 125 usec = 100 usec.
47194 + */
47195 +
47196 + max_claimed_usecs = 100 - qh->usecs;
47197 + } else {
47198 + /*
47199 + * Full speed mode.
47200 + * Max periodic usecs is 90% x 1000 usec = 900 usec.
47201 + */
47202 + max_claimed_usecs = 900 - qh->usecs;
47203 + }
47204 +
47205 + if (hcd->periodic_usecs > max_claimed_usecs) {
47206 + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
47207 + status = -DWC_E_NO_SPACE;
47208 + }
47209 +
47210 + return status;
47211 +}
47212 +
47213 +/* microframe_schedule=0 end */
47214 +
47215 +/**
47216 + * Microframe scheduler
47217 + * track the total use in hcd->frame_usecs
47218 + * keep each qh use in qh->frame_usecs
47219 + * when surrendering the qh then donate the time back
47220 + */
47221 +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
47222 +
47223 +/*
47224 + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
47225 + */
47226 +void init_hcd_usecs(dwc_otg_hcd_t *_hcd)
47227 +{
47228 + int i;
47229 + if (_hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
47230 + _hcd->frame_usecs[0] = 900;
47231 + for (i = 1; i < 8; i++)
47232 + _hcd->frame_usecs[i] = 0;
47233 + } else {
47234 + for (i = 0; i < 8; i++)
47235 + _hcd->frame_usecs[i] = max_uframe_usecs[i];
47236 + }
47237 +}
47238 +
47239 +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47240 +{
47241 + int i;
47242 + unsigned short utime;
47243 + int t_left;
47244 + int ret;
47245 + int done;
47246 +
47247 + ret = -1;
47248 + utime = _qh->usecs;
47249 + t_left = utime;
47250 + i = 0;
47251 + done = 0;
47252 + while (done == 0) {
47253 + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
47254 + if (utime <= _hcd->frame_usecs[i]) {
47255 + _hcd->frame_usecs[i] -= utime;
47256 + _qh->frame_usecs[i] += utime;
47257 + t_left -= utime;
47258 + ret = i;
47259 + done = 1;
47260 + return ret;
47261 + } else {
47262 + i++;
47263 + if (i == 8) {
47264 + done = 1;
47265 + ret = -1;
47266 + }
47267 + }
47268 + }
47269 + return ret;
47270 + }
47271 +
47272 +/*
47273 + * use this for FS apps that can span multiple uframes
47274 + */
47275 +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47276 +{
47277 + int i;
47278 + int j;
47279 + unsigned short utime;
47280 + int t_left;
47281 + int ret;
47282 + int done;
47283 + unsigned short xtime;
47284 +
47285 + ret = -1;
47286 + utime = _qh->usecs;
47287 + t_left = utime;
47288 + i = 0;
47289 + done = 0;
47290 +loop:
47291 + while (done == 0) {
47292 + if(_hcd->frame_usecs[i] <= 0) {
47293 + i++;
47294 + if (i == 8) {
47295 + done = 1;
47296 + ret = -1;
47297 + }
47298 + goto loop;
47299 + }
47300 +
47301 + /*
47302 + * we need n consecutive slots
47303 + * so use j as a start slot j plus j+1 must be enough time (for now)
47304 + */
47305 + xtime= _hcd->frame_usecs[i];
47306 + for (j = i+1 ; j < 8 ; j++ ) {
47307 + /*
47308 + * if we add this frame remaining time to xtime we may
47309 + * be OK, if not we need to test j for a complete frame
47310 + */
47311 + if ((xtime+_hcd->frame_usecs[j]) < utime) {
47312 + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
47313 + j = 8;
47314 + ret = -1;
47315 + continue;
47316 + }
47317 + }
47318 + if (xtime >= utime) {
47319 + ret = i;
47320 + j = 8; /* stop loop with a good value ret */
47321 + continue;
47322 + }
47323 + /* add the frame time to x time */
47324 + xtime += _hcd->frame_usecs[j];
47325 + /* we must have a fully available next frame or break */
47326 + if ((xtime < utime)
47327 + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
47328 + ret = -1;
47329 + j = 8; /* stop loop with a bad value ret */
47330 + continue;
47331 + }
47332 + }
47333 + if (ret >= 0) {
47334 + t_left = utime;
47335 + for (j = i; (t_left>0) && (j < 8); j++ ) {
47336 + t_left -= _hcd->frame_usecs[j];
47337 + if ( t_left <= 0 ) {
47338 + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
47339 + _hcd->frame_usecs[j]= -t_left;
47340 + ret = i;
47341 + done = 1;
47342 + } else {
47343 + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
47344 + _hcd->frame_usecs[j] = 0;
47345 + }
47346 + }
47347 + } else {
47348 + i++;
47349 + if (i == 8) {
47350 + done = 1;
47351 + ret = -1;
47352 + }
47353 + }
47354 + }
47355 + return ret;
47356 +}
47357 +
47358 +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47359 +{
47360 + int ret;
47361 + ret = -1;
47362 +
47363 + if (_qh->speed == USB_SPEED_HIGH ||
47364 + _hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
47365 + /* if this is a hs transaction we need a full frame - or account for FS usecs */
47366 + ret = find_single_uframe(_hcd, _qh);
47367 + } else {
47368 + /* if this is a fs transaction we may need a sequence of frames */
47369 + ret = find_multi_uframe(_hcd, _qh);
47370 + }
47371 + return ret;
47372 +}
47373 +
47374 +/**
47375 + * Checks that the max transfer size allowed in a host channel is large enough
47376 + * to handle the maximum data transfer in a single (micro)frame for a periodic
47377 + * transfer.
47378 + *
47379 + * @param hcd The HCD state structure for the DWC OTG controller.
47380 + * @param qh QH for a periodic endpoint.
47381 + *
47382 + * @return 0 if successful, negative error code otherwise.
47383 + */
47384 +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47385 +{
47386 + int status;
47387 + uint32_t max_xfer_size;
47388 + uint32_t max_channel_xfer_size;
47389 +
47390 + status = 0;
47391 +
47392 + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
47393 + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
47394 +
47395 + if (max_xfer_size > max_channel_xfer_size) {
47396 + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
47397 + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
47398 + status = -DWC_E_NO_SPACE;
47399 + }
47400 +
47401 + return status;
47402 +}
47403 +
47404 +
47405 +
47406 +/**
47407 + * Schedules an interrupt or isochronous transfer in the periodic schedule.
47408 + *
47409 + * @param hcd The HCD state structure for the DWC OTG controller.
47410 + * @param qh QH for the periodic transfer. The QH should already contain the
47411 + * scheduling information.
47412 + *
47413 + * @return 0 if successful, negative error code otherwise.
47414 + */
47415 +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47416 +{
47417 + int status = 0;
47418 +
47419 + if (microframe_schedule) {
47420 + int frame;
47421 + status = find_uframe(hcd, qh);
47422 + frame = -1;
47423 + if (status == 0) {
47424 + frame = 7;
47425 + } else {
47426 + if (status > 0 )
47427 + frame = status-1;
47428 + }
47429 +
47430 + /* Set the new frame up */
47431 + if (frame > -1) {
47432 + qh->sched_frame &= ~0x7;
47433 + qh->sched_frame |= (frame & 7);
47434 + }
47435 +
47436 + if (status != -1)
47437 + status = 0;
47438 + } else {
47439 + status = periodic_channel_available(hcd);
47440 + if (status) {
47441 + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
47442 + return status;
47443 + }
47444 +
47445 + status = check_periodic_bandwidth(hcd, qh);
47446 + }
47447 + if (status) {
47448 + DWC_INFO("%s: Insufficient periodic bandwidth for "
47449 + "periodic transfer.\n", __func__);
47450 + return -DWC_E_NO_SPACE;
47451 + }
47452 + status = check_max_xfer_size(hcd, qh);
47453 + if (status) {
47454 + DWC_INFO("%s: Channel max transfer size too small "
47455 + "for periodic transfer.\n", __func__);
47456 + return status;
47457 + }
47458 +
47459 + if (hcd->core_if->dma_desc_enable) {
47460 + /* Don't rely on SOF and start in ready schedule */
47461 + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
47462 + }
47463 + else {
47464 + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
47465 + {
47466 + hcd->fiq_state->next_sched_frame = qh->sched_frame;
47467 +
47468 + }
47469 + /* Always start in the inactive schedule. */
47470 + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
47471 + }
47472 +
47473 + if (!microframe_schedule) {
47474 + /* Reserve the periodic channel. */
47475 + hcd->periodic_channels++;
47476 + }
47477 +
47478 + /* Update claimed usecs per (micro)frame. */
47479 + hcd->periodic_usecs += qh->usecs;
47480 +
47481 + return status;
47482 +}
47483 +
47484 +
47485 +/**
47486 + * This function adds a QH to either the non periodic or periodic schedule if
47487 + * it is not already in the schedule. If the QH is already in the schedule, no
47488 + * action is taken.
47489 + *
47490 + * @return 0 if successful, negative error code otherwise.
47491 + */
47492 +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47493 +{
47494 + int status = 0;
47495 + gintmsk_data_t intr_mask = {.d32 = 0 };
47496 +
47497 + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
47498 + /* QH already in a schedule. */
47499 + return status;
47500 + }
47501 +
47502 + /* Add the new QH to the appropriate schedule */
47503 + if (dwc_qh_is_non_per(qh)) {
47504 + /* Always start in the inactive schedule. */
47505 + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
47506 + &qh->qh_list_entry);
47507 + //hcd->fiq_state->kick_np_queues = 1;
47508 + } else {
47509 + /* If the QH wasn't in a schedule, then sched_frame is stale. */
47510 + qh->sched_frame = dwc_frame_num_inc(dwc_otg_hcd_get_frame_number(hcd),
47511 + max_t(uint32_t, qh->interval, SCHEDULE_SLOP));
47512 + status = schedule_periodic(hcd, qh);
47513 + qh->start_split_frame = qh->sched_frame;
47514 + if ( !hcd->periodic_qh_count ) {
47515 + intr_mask.b.sofintr = 1;
47516 + if (fiq_enable) {
47517 + local_fiq_disable();
47518 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
47519 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
47520 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
47521 + local_fiq_enable();
47522 + } else {
47523 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
47524 + }
47525 + }
47526 + hcd->periodic_qh_count++;
47527 + }
47528 +
47529 + return status;
47530 +}
47531 +
47532 +/**
47533 + * Removes an interrupt or isochronous transfer from the periodic schedule.
47534 + *
47535 + * @param hcd The HCD state structure for the DWC OTG controller.
47536 + * @param qh QH for the periodic transfer.
47537 + */
47538 +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47539 +{
47540 + int i;
47541 + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
47542 +
47543 + /* Update claimed usecs per (micro)frame. */
47544 + hcd->periodic_usecs -= qh->usecs;
47545 +
47546 + if (!microframe_schedule) {
47547 + /* Release the periodic channel reservation. */
47548 + hcd->periodic_channels--;
47549 + } else {
47550 + for (i = 0; i < 8; i++) {
47551 + hcd->frame_usecs[i] += qh->frame_usecs[i];
47552 + qh->frame_usecs[i] = 0;
47553 + }
47554 + }
47555 +}
47556 +
47557 +/**
47558 + * Removes a QH from either the non-periodic or periodic schedule. Memory is
47559 + * not freed.
47560 + *
47561 + * @param hcd The HCD state structure.
47562 + * @param qh QH to remove from schedule. */
47563 +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47564 +{
47565 + gintmsk_data_t intr_mask = {.d32 = 0 };
47566 +
47567 + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
47568 + /* QH is not in a schedule. */
47569 + return;
47570 + }
47571 +
47572 + if (dwc_qh_is_non_per(qh)) {
47573 + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
47574 + hcd->non_periodic_qh_ptr =
47575 + hcd->non_periodic_qh_ptr->next;
47576 + }
47577 + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
47578 + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
47579 + // hcd->fiq_state->kick_np_queues = 1;
47580 + } else {
47581 + deschedule_periodic(hcd, qh);
47582 + hcd->periodic_qh_count--;
47583 + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
47584 + intr_mask.b.sofintr = 1;
47585 + if (fiq_enable) {
47586 + local_fiq_disable();
47587 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
47588 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
47589 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
47590 + local_fiq_enable();
47591 + } else {
47592 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
47593 + }
47594 + }
47595 + }
47596 +}
47597 +
47598 +/**
47599 + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
47600 + * non-periodic schedule. The QH is added to the inactive non-periodic
47601 + * schedule if any QTDs are still attached to the QH.
47602 + *
47603 + * For periodic QHs, the QH is removed from the periodic queued schedule. If
47604 + * there are any QTDs still attached to the QH, the QH is added to either the
47605 + * periodic inactive schedule or the periodic ready schedule and its next
47606 + * scheduled frame is calculated. The QH is placed in the ready schedule if
47607 + * the scheduled frame has been reached already. Otherwise it's placed in the
47608 + * inactive schedule. If there are no QTDs attached to the QH, the QH is
47609 + * completely removed from the periodic schedule.
47610 + */
47611 +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
47612 + int sched_next_periodic_split)
47613 +{
47614 + if (dwc_qh_is_non_per(qh)) {
47615 + dwc_otg_hcd_qh_remove(hcd, qh);
47616 + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
47617 + /* Add back to inactive non-periodic schedule. */
47618 + dwc_otg_hcd_qh_add(hcd, qh);
47619 + //hcd->fiq_state->kick_np_queues = 1;
47620 + } else {
47621 + if(nak_holdoff && qh->do_split) {
47622 + qh->nak_frame = 0xFFFF;
47623 + }
47624 + }
47625 + } else {
47626 + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
47627 +
47628 + if (qh->do_split) {
47629 + /* Schedule the next continuing periodic split transfer */
47630 + if (sched_next_periodic_split) {
47631 +
47632 + qh->sched_frame = frame_number;
47633 +
47634 + if (dwc_frame_num_le(frame_number,
47635 + dwc_frame_num_inc
47636 + (qh->start_split_frame,
47637 + 1))) {
47638 + /*
47639 + * Allow one frame to elapse after start
47640 + * split microframe before scheduling
47641 + * complete split, but DONT if we are
47642 + * doing the next start split in the
47643 + * same frame for an ISOC out.
47644 + */
47645 + if ((qh->ep_type != UE_ISOCHRONOUS) ||
47646 + (qh->ep_is_in != 0)) {
47647 + qh->sched_frame =
47648 + dwc_frame_num_inc(qh->sched_frame, 1);
47649 + }
47650 + }
47651 + } else {
47652 + qh->sched_frame =
47653 + dwc_frame_num_inc(qh->start_split_frame,
47654 + qh->interval);
47655 + if (dwc_frame_num_le
47656 + (qh->sched_frame, frame_number)) {
47657 + qh->sched_frame = frame_number;
47658 + }
47659 + qh->sched_frame |= 0x7;
47660 + qh->start_split_frame = qh->sched_frame;
47661 + }
47662 + } else {
47663 + qh->sched_frame =
47664 + dwc_frame_num_inc(qh->sched_frame, qh->interval);
47665 + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
47666 + qh->sched_frame = frame_number;
47667 + }
47668 + }
47669 +
47670 + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
47671 + dwc_otg_hcd_qh_remove(hcd, qh);
47672 + } else {
47673 + /*
47674 + * Remove from periodic_sched_queued and move to
47675 + * appropriate queue.
47676 + */
47677 + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
47678 + (!microframe_schedule && qh->sched_frame == frame_number)) {
47679 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
47680 + &qh->qh_list_entry);
47681 + } else {
47682 + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
47683 + {
47684 + hcd->fiq_state->next_sched_frame = qh->sched_frame;
47685 + }
47686 +
47687 + DWC_LIST_MOVE_HEAD
47688 + (&hcd->periodic_sched_inactive,
47689 + &qh->qh_list_entry);
47690 + }
47691 + }
47692 + }
47693 +}
47694 +
47695 +/**
47696 + * This function allocates and initializes a QTD.
47697 + *
47698 + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
47699 + * pointing to each other so each pair should have a unique correlation.
47700 + * @param atomic_alloc Flag to do atomic alloc if needed
47701 + *
47702 + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
47703 +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
47704 +{
47705 + dwc_otg_qtd_t *qtd;
47706 +
47707 + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
47708 + if (qtd == NULL) {
47709 + return NULL;
47710 + }
47711 +
47712 + dwc_otg_hcd_qtd_init(qtd, urb);
47713 + return qtd;
47714 +}
47715 +
47716 +/**
47717 + * Initializes a QTD structure.
47718 + *
47719 + * @param qtd The QTD to initialize.
47720 + * @param urb The URB to use for initialization. */
47721 +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
47722 +{
47723 + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
47724 + qtd->urb = urb;
47725 + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
47726 + /*
47727 + * The only time the QTD data toggle is used is on the data
47728 + * phase of control transfers. This phase always starts with
47729 + * DATA1.
47730 + */
47731 + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
47732 + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
47733 + }
47734 +
47735 + /* start split */
47736 + qtd->complete_split = 0;
47737 + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
47738 + qtd->isoc_split_offset = 0;
47739 + qtd->in_process = 0;
47740 +
47741 + /* Store the qtd ptr in the urb to reference what QTD. */
47742 + urb->qtd = qtd;
47743 + return;
47744 +}
47745 +
47746 +/**
47747 + * This function adds a QTD to the QTD-list of a QH. It will find the correct
47748 + * QH to place the QTD into. If it does not find a QH, then it will create a
47749 + * new QH. If the QH to which the QTD is added is not currently scheduled, it
47750 + * is placed into the proper schedule based on its EP type.
47751 + * HCD lock must be held and interrupts must be disabled on entry
47752 + *
47753 + * @param[in] qtd The QTD to add
47754 + * @param[in] hcd The DWC HCD structure
47755 + * @param[out] qh out parameter to return queue head
47756 + * @param atomic_alloc Flag to do atomic alloc if needed
47757 + *
47758 + * @return 0 if successful, negative error code otherwise.
47759 + */
47760 +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
47761 + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
47762 +{
47763 + int retval = 0;
47764 + dwc_otg_hcd_urb_t *urb = qtd->urb;
47765 +
47766 + /*
47767 + * Get the QH which holds the QTD-list to insert to. Create QH if it
47768 + * doesn't exist.
47769 + */
47770 + if (*qh == NULL) {
47771 + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
47772 + if (*qh == NULL) {
47773 + retval = -DWC_E_NO_MEMORY;
47774 + goto done;
47775 + } else {
47776 + if (fiq_enable)
47777 + hcd->fiq_state->kick_np_queues = 1;
47778 + }
47779 + }
47780 + retval = dwc_otg_hcd_qh_add(hcd, *qh);
47781 + if (retval == 0) {
47782 + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
47783 + qtd_list_entry);
47784 + qtd->qh = *qh;
47785 + }
47786 +done:
47787 +
47788 + return retval;
47789 +}
47790 +
47791 +#endif /* DWC_DEVICE_ONLY */
47792 --- /dev/null
47793 +++ b/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
47794 @@ -0,0 +1,200 @@
47795 +#ifndef _DWC_OS_DEP_H_
47796 +#define _DWC_OS_DEP_H_
47797 +
47798 +/**
47799 + * @file
47800 + *
47801 + * This file contains OS dependent structures.
47802 + *
47803 + */
47804 +
47805 +#include <linux/kernel.h>
47806 +#include <linux/module.h>
47807 +#include <linux/moduleparam.h>
47808 +#include <linux/init.h>
47809 +#include <linux/device.h>
47810 +#include <linux/errno.h>
47811 +#include <linux/types.h>
47812 +#include <linux/slab.h>
47813 +#include <linux/list.h>
47814 +#include <linux/interrupt.h>
47815 +#include <linux/ctype.h>
47816 +#include <linux/string.h>
47817 +#include <linux/dma-mapping.h>
47818 +#include <linux/jiffies.h>
47819 +#include <linux/delay.h>
47820 +#include <linux/timer.h>
47821 +#include <linux/workqueue.h>
47822 +#include <linux/stat.h>
47823 +#include <linux/pci.h>
47824 +#include <linux/compiler.h>
47825 +
47826 +#include <linux/version.h>
47827 +
47828 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
47829 +# include <linux/irq.h>
47830 +#endif
47831 +
47832 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
47833 +# include <linux/usb/ch9.h>
47834 +#else
47835 +# include <linux/usb_ch9.h>
47836 +#endif
47837 +
47838 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
47839 +# include <linux/usb/gadget.h>
47840 +#else
47841 +# include <linux/usb_gadget.h>
47842 +#endif
47843 +
47844 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
47845 +# include <asm/irq.h>
47846 +#endif
47847 +
47848 +#ifdef PCI_INTERFACE
47849 +# include <asm/io.h>
47850 +#endif
47851 +
47852 +#ifdef LM_INTERFACE
47853 +# include <asm/unaligned.h>
47854 +# include <asm/sizes.h>
47855 +# include <asm/param.h>
47856 +# include <asm/io.h>
47857 +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
47858 +# include <asm/arch/hardware.h>
47859 +# include <asm/arch/lm.h>
47860 +# include <asm/arch/irqs.h>
47861 +# include <asm/arch/regs-irq.h>
47862 +# else
47863 +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
47864 + here we assume that the machine architecture provides definitions
47865 + in its own header
47866 +*/
47867 +# include <mach/lm.h>
47868 +# include <mach/hardware.h>
47869 +# endif
47870 +#endif
47871 +
47872 +#ifdef PLATFORM_INTERFACE
47873 +#include <linux/platform_device.h>
47874 +#ifdef CONFIG_ARM
47875 +#include <asm/mach/map.h>
47876 +#endif
47877 +#endif
47878 +
47879 +/** The OS page size */
47880 +#define DWC_OS_PAGE_SIZE PAGE_SIZE
47881 +
47882 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
47883 +typedef int gfp_t;
47884 +#endif
47885 +
47886 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
47887 +# define IRQF_SHARED SA_SHIRQ
47888 +#endif
47889 +
47890 +typedef struct os_dependent {
47891 + /** Base address returned from ioremap() */
47892 + void *base;
47893 +
47894 + /** Register offset for Diagnostic API */
47895 + uint32_t reg_offset;
47896 +
47897 + /** Base address for MPHI peripheral */
47898 + void *mphi_base;
47899 +
47900 + /** mphi_base actually points to the SWIRQ block */
47901 + bool use_swirq;
47902 +
47903 + /** IRQ number (<0 if not valid) */
47904 + int irq_num;
47905 +
47906 + /** FIQ number (<0 if not valid) */
47907 + int fiq_num;
47908 +
47909 +#ifdef LM_INTERFACE
47910 + struct lm_device *lmdev;
47911 +#elif defined(PCI_INTERFACE)
47912 + struct pci_dev *pcidev;
47913 +
47914 + /** Start address of a PCI region */
47915 + resource_size_t rsrc_start;
47916 +
47917 + /** Length address of a PCI region */
47918 + resource_size_t rsrc_len;
47919 +#elif defined(PLATFORM_INTERFACE)
47920 + struct platform_device *platformdev;
47921 +#endif
47922 +
47923 +} os_dependent_t;
47924 +
47925 +#ifdef __cplusplus
47926 +}
47927 +#endif
47928 +
47929 +
47930 +
47931 +/* Type for the our device on the chosen bus */
47932 +#if defined(LM_INTERFACE)
47933 +typedef struct lm_device dwc_bus_dev_t;
47934 +#elif defined(PCI_INTERFACE)
47935 +typedef struct pci_dev dwc_bus_dev_t;
47936 +#elif defined(PLATFORM_INTERFACE)
47937 +typedef struct platform_device dwc_bus_dev_t;
47938 +#endif
47939 +
47940 +/* Helper macro to retrieve drvdata from the device on the chosen bus */
47941 +#if defined(LM_INTERFACE)
47942 +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
47943 +#elif defined(PCI_INTERFACE)
47944 +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
47945 +#elif defined(PLATFORM_INTERFACE)
47946 +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
47947 +#endif
47948 +
47949 +/**
47950 + * Helper macro returning the otg_device structure of a given struct device
47951 + *
47952 + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
47953 + */
47954 +#ifdef LM_INTERFACE
47955 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
47956 + struct lm_device *lm_dev = \
47957 + container_of(_dev, struct lm_device, dev); \
47958 + _var = lm_get_drvdata(lm_dev); \
47959 + } while (0)
47960 +
47961 +#elif defined(PCI_INTERFACE)
47962 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
47963 + _var = dev_get_drvdata(_dev); \
47964 + } while (0)
47965 +
47966 +#elif defined(PLATFORM_INTERFACE)
47967 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
47968 + struct platform_device *platform_dev = \
47969 + container_of(_dev, struct platform_device, dev); \
47970 + _var = platform_get_drvdata(platform_dev); \
47971 + } while (0)
47972 +#endif
47973 +
47974 +
47975 +/**
47976 + * Helper macro returning the struct dev of the given struct os_dependent
47977 + *
47978 + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
47979 + */
47980 +#ifdef LM_INTERFACE
47981 +#define DWC_OTG_OS_GETDEV(_osdep) \
47982 + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
47983 +#elif defined(PCI_INTERFACE)
47984 +#define DWC_OTG_OS_GETDEV(_osdep) \
47985 + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
47986 +#elif defined(PLATFORM_INTERFACE)
47987 +#define DWC_OTG_OS_GETDEV(_osdep) \
47988 + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
47989 +#endif
47990 +
47991 +
47992 +
47993 +
47994 +#endif /* _DWC_OS_DEP_H_ */
47995 --- /dev/null
47996 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
47997 @@ -0,0 +1,2725 @@
47998 +/* ==========================================================================
47999 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
48000 + * $Revision: #101 $
48001 + * $Date: 2012/08/10 $
48002 + * $Change: 2047372 $
48003 + *
48004 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
48005 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
48006 + * otherwise expressly agreed to in writing between Synopsys and you.
48007 + *
48008 + * The Software IS NOT an item of Licensed Software or Licensed Product under
48009 + * any End User Software License Agreement or Agreement for Licensed Product
48010 + * with Synopsys or any supplement thereto. You are permitted to use and
48011 + * redistribute this Software in source and binary forms, with or without
48012 + * modification, provided that redistributions of source code must retain this
48013 + * notice. You may not view, use, disclose, copy or distribute this file or
48014 + * any information contained herein except pursuant to this license grant from
48015 + * Synopsys. If you do not agree with this notice, including the disclaimer
48016 + * below, then you are not authorized to use the Software.
48017 + *
48018 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
48019 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48020 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48021 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
48022 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48023 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48024 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48025 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48026 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48027 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
48028 + * DAMAGE.
48029 + * ========================================================================== */
48030 +#ifndef DWC_HOST_ONLY
48031 +
48032 +/** @file
48033 + * This file implements PCD Core. All code in this file is portable and doesn't
48034 + * use any OS specific functions.
48035 + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
48036 + * header file, which can be used to implement OS specific PCD interface.
48037 + *
48038 + * An important function of the PCD is managing interrupts generated
48039 + * by the DWC_otg controller. The implementation of the DWC_otg device
48040 + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
48041 + *
48042 + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
48043 + * @todo Does it work when the request size is greater than DEPTSIZ
48044 + * transfer size
48045 + *
48046 + */
48047 +
48048 +#include "dwc_otg_pcd.h"
48049 +
48050 +#ifdef DWC_UTE_CFI
48051 +#include "dwc_otg_cfi.h"
48052 +
48053 +extern int init_cfi(cfiobject_t * cfiobj);
48054 +#endif
48055 +
48056 +/**
48057 + * Choose endpoint from ep arrays using usb_ep structure.
48058 + */
48059 +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
48060 +{
48061 + int i;
48062 + if (pcd->ep0.priv == handle) {
48063 + return &pcd->ep0;
48064 + }
48065 + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
48066 + if (pcd->in_ep[i].priv == handle)
48067 + return &pcd->in_ep[i];
48068 + if (pcd->out_ep[i].priv == handle)
48069 + return &pcd->out_ep[i];
48070 + }
48071 +
48072 + return NULL;
48073 +}
48074 +
48075 +/**
48076 + * This function completes a request. It call's the request call back.
48077 + */
48078 +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
48079 + int32_t status)
48080 +{
48081 + unsigned stopped = ep->stopped;
48082 +
48083 + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
48084 + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
48085 +
48086 + /* don't modify queue heads during completion callback */
48087 + ep->stopped = 1;
48088 + /* spin_unlock/spin_lock now done in fops->complete() */
48089 + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
48090 + req->actual);
48091 +
48092 + if (ep->pcd->request_pending > 0) {
48093 + --ep->pcd->request_pending;
48094 + }
48095 +
48096 + ep->stopped = stopped;
48097 + DWC_FREE(req);
48098 +}
48099 +
48100 +/**
48101 + * This function terminates all the requsts in the EP request queue.
48102 + */
48103 +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
48104 +{
48105 + dwc_otg_pcd_request_t *req;
48106 +
48107 + ep->stopped = 1;
48108 +
48109 + /* called with irqs blocked?? */
48110 + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
48111 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
48112 + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
48113 + }
48114 +}
48115 +
48116 +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
48117 + const struct dwc_otg_pcd_function_ops *fops)
48118 +{
48119 + pcd->fops = fops;
48120 +}
48121 +
48122 +/**
48123 + * PCD Callback function for initializing the PCD when switching to
48124 + * device mode.
48125 + *
48126 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48127 + */
48128 +static int32_t dwc_otg_pcd_start_cb(void *p)
48129 +{
48130 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48131 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
48132 +
48133 + /*
48134 + * Initialized the Core for Device mode.
48135 + */
48136 + if (dwc_otg_is_device_mode(core_if)) {
48137 + dwc_otg_core_dev_init(core_if);
48138 + /* Set core_if's lock pointer to the pcd->lock */
48139 + core_if->lock = pcd->lock;
48140 + }
48141 + return 1;
48142 +}
48143 +
48144 +/** CFI-specific buffer allocation function for EP */
48145 +#ifdef DWC_UTE_CFI
48146 +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
48147 + size_t buflen, int flags)
48148 +{
48149 + dwc_otg_pcd_ep_t *ep;
48150 + ep = get_ep_from_handle(pcd, pep);
48151 + if (!ep) {
48152 + DWC_WARN("bad ep\n");
48153 + return -DWC_E_INVALID;
48154 + }
48155 +
48156 + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
48157 + flags);
48158 +}
48159 +#else
48160 +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
48161 + size_t buflen, int flags);
48162 +#endif
48163 +
48164 +/**
48165 + * PCD Callback function for notifying the PCD when resuming from
48166 + * suspend.
48167 + *
48168 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48169 + */
48170 +static int32_t dwc_otg_pcd_resume_cb(void *p)
48171 +{
48172 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48173 +
48174 + if (pcd->fops->resume) {
48175 + pcd->fops->resume(pcd);
48176 + }
48177 +
48178 + /* Stop the SRP timeout timer. */
48179 + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
48180 + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
48181 + if (GET_CORE_IF(pcd)->srp_timer_started) {
48182 + GET_CORE_IF(pcd)->srp_timer_started = 0;
48183 + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
48184 + }
48185 + }
48186 + return 1;
48187 +}
48188 +
48189 +/**
48190 + * PCD Callback function for notifying the PCD device is suspended.
48191 + *
48192 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48193 + */
48194 +static int32_t dwc_otg_pcd_suspend_cb(void *p)
48195 +{
48196 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48197 +
48198 + if (pcd->fops->suspend) {
48199 + DWC_SPINUNLOCK(pcd->lock);
48200 + pcd->fops->suspend(pcd);
48201 + DWC_SPINLOCK(pcd->lock);
48202 + }
48203 +
48204 + return 1;
48205 +}
48206 +
48207 +/**
48208 + * PCD Callback function for stopping the PCD when switching to Host
48209 + * mode.
48210 + *
48211 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48212 + */
48213 +static int32_t dwc_otg_pcd_stop_cb(void *p)
48214 +{
48215 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48216 + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
48217 +
48218 + dwc_otg_pcd_stop(pcd);
48219 + return 1;
48220 +}
48221 +
48222 +/**
48223 + * PCD Callback structure for handling mode switching.
48224 + */
48225 +static dwc_otg_cil_callbacks_t pcd_callbacks = {
48226 + .start = dwc_otg_pcd_start_cb,
48227 + .stop = dwc_otg_pcd_stop_cb,
48228 + .suspend = dwc_otg_pcd_suspend_cb,
48229 + .resume_wakeup = dwc_otg_pcd_resume_cb,
48230 + .p = 0, /* Set at registration */
48231 +};
48232 +
48233 +/**
48234 + * This function allocates a DMA Descriptor chain for the Endpoint
48235 + * buffer to be used for a transfer to/from the specified endpoint.
48236 + */
48237 +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(struct device *dev,
48238 + dwc_dma_t * dma_desc_addr,
48239 + uint32_t count)
48240 +{
48241 + return DWC_DMA_ALLOC_ATOMIC(dev, count * sizeof(dwc_otg_dev_dma_desc_t),
48242 + dma_desc_addr);
48243 +}
48244 +
48245 +/**
48246 + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
48247 + */
48248 +void dwc_otg_ep_free_desc_chain(struct device *dev,
48249 + dwc_otg_dev_dma_desc_t * desc_addr,
48250 + uint32_t dma_desc_addr, uint32_t count)
48251 +{
48252 + DWC_DMA_FREE(dev, count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
48253 + dma_desc_addr);
48254 +}
48255 +
48256 +#ifdef DWC_EN_ISOC
48257 +
48258 +/**
48259 + * This function initializes a descriptor chain for Isochronous transfer
48260 + *
48261 + * @param core_if Programming view of DWC_otg controller.
48262 + * @param dwc_ep The EP to start the transfer on.
48263 + *
48264 + */
48265 +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
48266 + dwc_ep_t * dwc_ep)
48267 +{
48268 +
48269 + dsts_data_t dsts = {.d32 = 0 };
48270 + depctl_data_t depctl = {.d32 = 0 };
48271 + volatile uint32_t *addr;
48272 + int i, j;
48273 + uint32_t len;
48274 +
48275 + if (dwc_ep->is_in)
48276 + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
48277 + else
48278 + dwc_ep->desc_cnt =
48279 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48280 + dwc_ep->bInterval;
48281 +
48282 + /** Allocate descriptors for double buffering */
48283 + dwc_ep->iso_desc_addr =
48284 + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
48285 + dwc_ep->desc_cnt * 2);
48286 + if (dwc_ep->desc_addr) {
48287 + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
48288 + return;
48289 + }
48290 +
48291 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48292 +
48293 + /** ISO OUT EP */
48294 + if (dwc_ep->is_in == 0) {
48295 + dev_dma_desc_sts_t sts = {.d32 = 0 };
48296 + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
48297 + dma_addr_t dma_ad;
48298 + uint32_t data_per_desc;
48299 + dwc_otg_dev_out_ep_regs_t *out_regs =
48300 + core_if->dev_if->out_ep_regs[dwc_ep->num];
48301 + int offset;
48302 +
48303 + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
48304 + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
48305 +
48306 + /** Buffer 0 descriptors setup */
48307 + dma_ad = dwc_ep->dma_addr0;
48308 +
48309 + sts.b_iso_out.bs = BS_HOST_READY;
48310 + sts.b_iso_out.rxsts = 0;
48311 + sts.b_iso_out.l = 0;
48312 + sts.b_iso_out.sp = 0;
48313 + sts.b_iso_out.ioc = 0;
48314 + sts.b_iso_out.pid = 0;
48315 + sts.b_iso_out.framenum = 0;
48316 +
48317 + offset = 0;
48318 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48319 + i += dwc_ep->pkt_per_frm) {
48320 +
48321 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
48322 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48323 + if (len > dwc_ep->data_per_frame)
48324 + data_per_desc =
48325 + dwc_ep->data_per_frame -
48326 + j * dwc_ep->maxpacket;
48327 + else
48328 + data_per_desc = dwc_ep->maxpacket;
48329 + len = data_per_desc % 4;
48330 + if (len)
48331 + data_per_desc += 4 - len;
48332 +
48333 + sts.b_iso_out.rxbytes = data_per_desc;
48334 + dma_desc->buf = dma_ad;
48335 + dma_desc->status.d32 = sts.d32;
48336 +
48337 + offset += data_per_desc;
48338 + dma_desc++;
48339 + dma_ad += data_per_desc;
48340 + }
48341 + }
48342 +
48343 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
48344 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48345 + if (len > dwc_ep->data_per_frame)
48346 + data_per_desc =
48347 + dwc_ep->data_per_frame -
48348 + j * dwc_ep->maxpacket;
48349 + else
48350 + data_per_desc = dwc_ep->maxpacket;
48351 + len = data_per_desc % 4;
48352 + if (len)
48353 + data_per_desc += 4 - len;
48354 + sts.b_iso_out.rxbytes = data_per_desc;
48355 + dma_desc->buf = dma_ad;
48356 + dma_desc->status.d32 = sts.d32;
48357 +
48358 + offset += data_per_desc;
48359 + dma_desc++;
48360 + dma_ad += data_per_desc;
48361 + }
48362 +
48363 + sts.b_iso_out.ioc = 1;
48364 + len = (j + 1) * dwc_ep->maxpacket;
48365 + if (len > dwc_ep->data_per_frame)
48366 + data_per_desc =
48367 + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
48368 + else
48369 + data_per_desc = dwc_ep->maxpacket;
48370 + len = data_per_desc % 4;
48371 + if (len)
48372 + data_per_desc += 4 - len;
48373 + sts.b_iso_out.rxbytes = data_per_desc;
48374 +
48375 + dma_desc->buf = dma_ad;
48376 + dma_desc->status.d32 = sts.d32;
48377 + dma_desc++;
48378 +
48379 + /** Buffer 1 descriptors setup */
48380 + sts.b_iso_out.ioc = 0;
48381 + dma_ad = dwc_ep->dma_addr1;
48382 +
48383 + offset = 0;
48384 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48385 + i += dwc_ep->pkt_per_frm) {
48386 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
48387 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48388 + if (len > dwc_ep->data_per_frame)
48389 + data_per_desc =
48390 + dwc_ep->data_per_frame -
48391 + j * dwc_ep->maxpacket;
48392 + else
48393 + data_per_desc = dwc_ep->maxpacket;
48394 + len = data_per_desc % 4;
48395 + if (len)
48396 + data_per_desc += 4 - len;
48397 +
48398 + data_per_desc =
48399 + sts.b_iso_out.rxbytes = data_per_desc;
48400 + dma_desc->buf = dma_ad;
48401 + dma_desc->status.d32 = sts.d32;
48402 +
48403 + offset += data_per_desc;
48404 + dma_desc++;
48405 + dma_ad += data_per_desc;
48406 + }
48407 + }
48408 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
48409 + data_per_desc =
48410 + ((j + 1) * dwc_ep->maxpacket >
48411 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
48412 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
48413 + data_per_desc +=
48414 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
48415 + sts.b_iso_out.rxbytes = data_per_desc;
48416 + dma_desc->buf = dma_ad;
48417 + dma_desc->status.d32 = sts.d32;
48418 +
48419 + offset += data_per_desc;
48420 + dma_desc++;
48421 + dma_ad += data_per_desc;
48422 + }
48423 +
48424 + sts.b_iso_out.ioc = 1;
48425 + sts.b_iso_out.l = 1;
48426 + data_per_desc =
48427 + ((j + 1) * dwc_ep->maxpacket >
48428 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
48429 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
48430 + data_per_desc +=
48431 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
48432 + sts.b_iso_out.rxbytes = data_per_desc;
48433 +
48434 + dma_desc->buf = dma_ad;
48435 + dma_desc->status.d32 = sts.d32;
48436 +
48437 + dwc_ep->next_frame = 0;
48438 +
48439 + /** Write dma_ad into DOEPDMA register */
48440 + DWC_WRITE_REG32(&(out_regs->doepdma),
48441 + (uint32_t) dwc_ep->iso_dma_desc_addr);
48442 +
48443 + }
48444 + /** ISO IN EP */
48445 + else {
48446 + dev_dma_desc_sts_t sts = {.d32 = 0 };
48447 + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
48448 + dma_addr_t dma_ad;
48449 + dwc_otg_dev_in_ep_regs_t *in_regs =
48450 + core_if->dev_if->in_ep_regs[dwc_ep->num];
48451 + unsigned int frmnumber;
48452 + fifosize_data_t txfifosize, rxfifosize;
48453 +
48454 + txfifosize.d32 =
48455 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
48456 + dtxfsts);
48457 + rxfifosize.d32 =
48458 + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
48459 +
48460 + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
48461 +
48462 + dma_ad = dwc_ep->dma_addr0;
48463 +
48464 + dsts.d32 =
48465 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48466 +
48467 + sts.b_iso_in.bs = BS_HOST_READY;
48468 + sts.b_iso_in.txsts = 0;
48469 + sts.b_iso_in.sp =
48470 + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
48471 + sts.b_iso_in.ioc = 0;
48472 + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
48473 +
48474 + frmnumber = dwc_ep->next_frame;
48475 +
48476 + sts.b_iso_in.framenum = frmnumber;
48477 + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
48478 + sts.b_iso_in.l = 0;
48479 +
48480 + /** Buffer 0 descriptors setup */
48481 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
48482 + dma_desc->buf = dma_ad;
48483 + dma_desc->status.d32 = sts.d32;
48484 + dma_desc++;
48485 +
48486 + dma_ad += dwc_ep->data_per_frame;
48487 + sts.b_iso_in.framenum += dwc_ep->bInterval;
48488 + }
48489 +
48490 + sts.b_iso_in.ioc = 1;
48491 + dma_desc->buf = dma_ad;
48492 + dma_desc->status.d32 = sts.d32;
48493 + ++dma_desc;
48494 +
48495 + /** Buffer 1 descriptors setup */
48496 + sts.b_iso_in.ioc = 0;
48497 + dma_ad = dwc_ep->dma_addr1;
48498 +
48499 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48500 + i += dwc_ep->pkt_per_frm) {
48501 + dma_desc->buf = dma_ad;
48502 + dma_desc->status.d32 = sts.d32;
48503 + dma_desc++;
48504 +
48505 + dma_ad += dwc_ep->data_per_frame;
48506 + sts.b_iso_in.framenum += dwc_ep->bInterval;
48507 +
48508 + sts.b_iso_in.ioc = 0;
48509 + }
48510 + sts.b_iso_in.ioc = 1;
48511 + sts.b_iso_in.l = 1;
48512 +
48513 + dma_desc->buf = dma_ad;
48514 + dma_desc->status.d32 = sts.d32;
48515 +
48516 + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
48517 +
48518 + /** Write dma_ad into diepdma register */
48519 + DWC_WRITE_REG32(&(in_regs->diepdma),
48520 + (uint32_t) dwc_ep->iso_dma_desc_addr);
48521 + }
48522 + /** Enable endpoint, clear nak */
48523 + depctl.d32 = 0;
48524 + depctl.b.epena = 1;
48525 + depctl.b.usbactep = 1;
48526 + depctl.b.cnak = 1;
48527 +
48528 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
48529 + depctl.d32 = DWC_READ_REG32(addr);
48530 +}
48531 +
48532 +/**
48533 + * This function initializes a descriptor chain for Isochronous transfer
48534 + *
48535 + * @param core_if Programming view of DWC_otg controller.
48536 + * @param ep The EP to start the transfer on.
48537 + *
48538 + */
48539 +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
48540 + dwc_ep_t * ep)
48541 +{
48542 + depctl_data_t depctl = {.d32 = 0 };
48543 + volatile uint32_t *addr;
48544 +
48545 + if (ep->is_in) {
48546 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
48547 + } else {
48548 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
48549 + }
48550 +
48551 + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
48552 + return;
48553 + } else {
48554 + deptsiz_data_t deptsiz = {.d32 = 0 };
48555 +
48556 + ep->xfer_len =
48557 + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
48558 + ep->pkt_cnt =
48559 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
48560 + ep->xfer_count = 0;
48561 + ep->xfer_buff =
48562 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
48563 + ep->dma_addr =
48564 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
48565 +
48566 + if (ep->is_in) {
48567 + /* Program the transfer size and packet count
48568 + * as follows: xfersize = N * maxpacket +
48569 + * short_packet pktcnt = N + (short_packet
48570 + * exist ? 1 : 0)
48571 + */
48572 + deptsiz.b.mc = ep->pkt_per_frm;
48573 + deptsiz.b.xfersize = ep->xfer_len;
48574 + deptsiz.b.pktcnt =
48575 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
48576 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
48577 + dieptsiz, deptsiz.d32);
48578 +
48579 + /* Write the DMA register */
48580 + DWC_WRITE_REG32(&
48581 + (core_if->dev_if->in_ep_regs[ep->num]->
48582 + diepdma), (uint32_t) ep->dma_addr);
48583 +
48584 + } else {
48585 + deptsiz.b.pktcnt =
48586 + (ep->xfer_len + (ep->maxpacket - 1)) /
48587 + ep->maxpacket;
48588 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
48589 +
48590 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
48591 + doeptsiz, deptsiz.d32);
48592 +
48593 + /* Write the DMA register */
48594 + DWC_WRITE_REG32(&
48595 + (core_if->dev_if->out_ep_regs[ep->num]->
48596 + doepdma), (uint32_t) ep->dma_addr);
48597 +
48598 + }
48599 + /** Enable endpoint, clear nak */
48600 + depctl.d32 = 0;
48601 + depctl.b.epena = 1;
48602 + depctl.b.cnak = 1;
48603 +
48604 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
48605 + }
48606 +}
48607 +
48608 +/**
48609 + * This function does the setup for a data transfer for an EP and
48610 + * starts the transfer. For an IN transfer, the packets will be
48611 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
48612 + * the packets are unloaded from the Rx FIFO in the ISR.
48613 + *
48614 + * @param core_if Programming view of DWC_otg controller.
48615 + * @param ep The EP to start the transfer on.
48616 + */
48617 +
48618 +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
48619 + dwc_ep_t * ep)
48620 +{
48621 + if (core_if->dma_enable) {
48622 + if (core_if->dma_desc_enable) {
48623 + if (ep->is_in) {
48624 + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
48625 + } else {
48626 + ep->desc_cnt = ep->pkt_cnt;
48627 + }
48628 + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
48629 + } else {
48630 + if (core_if->pti_enh_enable) {
48631 + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
48632 + } else {
48633 + ep->cur_pkt_addr =
48634 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
48635 + xfer_buff0;
48636 + ep->cur_pkt_dma_addr =
48637 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
48638 + dma_addr0;
48639 + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
48640 + }
48641 + }
48642 + } else {
48643 + ep->cur_pkt_addr =
48644 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
48645 + ep->cur_pkt_dma_addr =
48646 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
48647 + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
48648 + }
48649 +}
48650 +
48651 +/**
48652 + * This function stops transfer for an EP and
48653 + * resets the ep's variables.
48654 + *
48655 + * @param core_if Programming view of DWC_otg controller.
48656 + * @param ep The EP to start the transfer on.
48657 + */
48658 +
48659 +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
48660 +{
48661 + depctl_data_t depctl = {.d32 = 0 };
48662 + volatile uint32_t *addr;
48663 +
48664 + if (ep->is_in == 1) {
48665 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
48666 + } else {
48667 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
48668 + }
48669 +
48670 + /* disable the ep */
48671 + depctl.d32 = DWC_READ_REG32(addr);
48672 +
48673 + depctl.b.epdis = 1;
48674 + depctl.b.snak = 1;
48675 +
48676 + DWC_WRITE_REG32(addr, depctl.d32);
48677 +
48678 + if (core_if->dma_desc_enable &&
48679 + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
48680 + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
48681 + ep->iso_dma_desc_addr,
48682 + ep->desc_cnt * 2);
48683 + }
48684 +
48685 + /* reset varibales */
48686 + ep->dma_addr0 = 0;
48687 + ep->dma_addr1 = 0;
48688 + ep->xfer_buff0 = 0;
48689 + ep->xfer_buff1 = 0;
48690 + ep->data_per_frame = 0;
48691 + ep->data_pattern_frame = 0;
48692 + ep->sync_frame = 0;
48693 + ep->buf_proc_intrvl = 0;
48694 + ep->bInterval = 0;
48695 + ep->proc_buf_num = 0;
48696 + ep->pkt_per_frm = 0;
48697 + ep->pkt_per_frm = 0;
48698 + ep->desc_cnt = 0;
48699 + ep->iso_desc_addr = 0;
48700 + ep->iso_dma_desc_addr = 0;
48701 +}
48702 +
48703 +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
48704 + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
48705 + dwc_dma_t dma1, int sync_frame, int dp_frame,
48706 + int data_per_frame, int start_frame,
48707 + int buf_proc_intrvl, void *req_handle,
48708 + int atomic_alloc)
48709 +{
48710 + dwc_otg_pcd_ep_t *ep;
48711 + dwc_irqflags_t flags = 0;
48712 + dwc_ep_t *dwc_ep;
48713 + int32_t frm_data;
48714 + dsts_data_t dsts;
48715 + dwc_otg_core_if_t *core_if;
48716 +
48717 + ep = get_ep_from_handle(pcd, ep_handle);
48718 +
48719 + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
48720 + DWC_WARN("bad ep\n");
48721 + return -DWC_E_INVALID;
48722 + }
48723 +
48724 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
48725 + core_if = GET_CORE_IF(pcd);
48726 + dwc_ep = &ep->dwc_ep;
48727 +
48728 + if (ep->iso_req_handle) {
48729 + DWC_WARN("ISO request in progress\n");
48730 + }
48731 +
48732 + dwc_ep->dma_addr0 = dma0;
48733 + dwc_ep->dma_addr1 = dma1;
48734 +
48735 + dwc_ep->xfer_buff0 = buf0;
48736 + dwc_ep->xfer_buff1 = buf1;
48737 +
48738 + dwc_ep->data_per_frame = data_per_frame;
48739 +
48740 + /** @todo - pattern data support is to be implemented in the future */
48741 + dwc_ep->data_pattern_frame = dp_frame;
48742 + dwc_ep->sync_frame = sync_frame;
48743 +
48744 + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
48745 +
48746 + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
48747 +
48748 + dwc_ep->proc_buf_num = 0;
48749 +
48750 + dwc_ep->pkt_per_frm = 0;
48751 + frm_data = ep->dwc_ep.data_per_frame;
48752 + while (frm_data > 0) {
48753 + dwc_ep->pkt_per_frm++;
48754 + frm_data -= ep->dwc_ep.maxpacket;
48755 + }
48756 +
48757 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48758 +
48759 + if (start_frame == -1) {
48760 + dwc_ep->next_frame = dsts.b.soffn + 1;
48761 + if (dwc_ep->bInterval != 1) {
48762 + dwc_ep->next_frame =
48763 + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
48764 + dwc_ep->next_frame %
48765 + dwc_ep->bInterval);
48766 + }
48767 + } else {
48768 + dwc_ep->next_frame = start_frame;
48769 + }
48770 +
48771 + if (!core_if->pti_enh_enable) {
48772 + dwc_ep->pkt_cnt =
48773 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48774 + dwc_ep->bInterval;
48775 + } else {
48776 + dwc_ep->pkt_cnt =
48777 + (dwc_ep->data_per_frame *
48778 + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
48779 + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
48780 + }
48781 +
48782 + if (core_if->dma_desc_enable) {
48783 + dwc_ep->desc_cnt =
48784 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48785 + dwc_ep->bInterval;
48786 + }
48787 +
48788 + if (atomic_alloc) {
48789 + dwc_ep->pkt_info =
48790 + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48791 + } else {
48792 + dwc_ep->pkt_info =
48793 + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48794 + }
48795 + if (!dwc_ep->pkt_info) {
48796 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48797 + return -DWC_E_NO_MEMORY;
48798 + }
48799 + if (core_if->pti_enh_enable) {
48800 + dwc_memset(dwc_ep->pkt_info, 0,
48801 + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48802 + }
48803 +
48804 + dwc_ep->cur_pkt = 0;
48805 + ep->iso_req_handle = req_handle;
48806 +
48807 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48808 + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
48809 + return 0;
48810 +}
48811 +
48812 +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
48813 + void *req_handle)
48814 +{
48815 + dwc_irqflags_t flags = 0;
48816 + dwc_otg_pcd_ep_t *ep;
48817 + dwc_ep_t *dwc_ep;
48818 +
48819 + ep = get_ep_from_handle(pcd, ep_handle);
48820 + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
48821 + DWC_WARN("bad ep\n");
48822 + return -DWC_E_INVALID;
48823 + }
48824 + dwc_ep = &ep->dwc_ep;
48825 +
48826 + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
48827 +
48828 + DWC_FREE(dwc_ep->pkt_info);
48829 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
48830 + if (ep->iso_req_handle != req_handle) {
48831 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48832 + return -DWC_E_INVALID;
48833 + }
48834 +
48835 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48836 +
48837 + ep->iso_req_handle = 0;
48838 + return 0;
48839 +}
48840 +
48841 +/**
48842 + * This function is used for perodical data exchnage between PCD and gadget drivers.
48843 + * for Isochronous EPs
48844 + *
48845 + * - Every time a sync period completes this function is called to
48846 + * perform data exchange between PCD and gadget
48847 + */
48848 +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
48849 + void *req_handle)
48850 +{
48851 + int i;
48852 + dwc_ep_t *dwc_ep;
48853 +
48854 + dwc_ep = &ep->dwc_ep;
48855 +
48856 + DWC_SPINUNLOCK(ep->pcd->lock);
48857 + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
48858 + dwc_ep->proc_buf_num ^ 0x1);
48859 + DWC_SPINLOCK(ep->pcd->lock);
48860 +
48861 + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
48862 + dwc_ep->pkt_info[i].status = 0;
48863 + dwc_ep->pkt_info[i].offset = 0;
48864 + dwc_ep->pkt_info[i].length = 0;
48865 + }
48866 +}
48867 +
48868 +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
48869 + void *iso_req_handle)
48870 +{
48871 + dwc_otg_pcd_ep_t *ep;
48872 + dwc_ep_t *dwc_ep;
48873 +
48874 + ep = get_ep_from_handle(pcd, ep_handle);
48875 + if (!ep->desc || ep->dwc_ep.num == 0) {
48876 + DWC_WARN("bad ep\n");
48877 + return -DWC_E_INVALID;
48878 + }
48879 + dwc_ep = &ep->dwc_ep;
48880 +
48881 + return dwc_ep->pkt_cnt;
48882 +}
48883 +
48884 +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
48885 + void *iso_req_handle, int packet,
48886 + int *status, int *actual, int *offset)
48887 +{
48888 + dwc_otg_pcd_ep_t *ep;
48889 + dwc_ep_t *dwc_ep;
48890 +
48891 + ep = get_ep_from_handle(pcd, ep_handle);
48892 + if (!ep)
48893 + DWC_WARN("bad ep\n");
48894 +
48895 + dwc_ep = &ep->dwc_ep;
48896 +
48897 + *status = dwc_ep->pkt_info[packet].status;
48898 + *actual = dwc_ep->pkt_info[packet].length;
48899 + *offset = dwc_ep->pkt_info[packet].offset;
48900 +}
48901 +
48902 +#endif /* DWC_EN_ISOC */
48903 +
48904 +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
48905 + uint32_t is_in, uint32_t ep_num)
48906 +{
48907 + /* Init EP structure */
48908 + pcd_ep->desc = 0;
48909 + pcd_ep->pcd = pcd;
48910 + pcd_ep->stopped = 1;
48911 + pcd_ep->queue_sof = 0;
48912 +
48913 + /* Init DWC ep structure */
48914 + pcd_ep->dwc_ep.is_in = is_in;
48915 + pcd_ep->dwc_ep.num = ep_num;
48916 + pcd_ep->dwc_ep.active = 0;
48917 + pcd_ep->dwc_ep.tx_fifo_num = 0;
48918 + /* Control until ep is actvated */
48919 + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
48920 + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
48921 + pcd_ep->dwc_ep.dma_addr = 0;
48922 + pcd_ep->dwc_ep.start_xfer_buff = 0;
48923 + pcd_ep->dwc_ep.xfer_buff = 0;
48924 + pcd_ep->dwc_ep.xfer_len = 0;
48925 + pcd_ep->dwc_ep.xfer_count = 0;
48926 + pcd_ep->dwc_ep.sent_zlp = 0;
48927 + pcd_ep->dwc_ep.total_len = 0;
48928 + pcd_ep->dwc_ep.desc_addr = 0;
48929 + pcd_ep->dwc_ep.dma_desc_addr = 0;
48930 + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
48931 +}
48932 +
48933 +/**
48934 + * Initialize ep's
48935 + */
48936 +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
48937 +{
48938 + int i;
48939 + uint32_t hwcfg1;
48940 + dwc_otg_pcd_ep_t *ep;
48941 + int in_ep_cntr, out_ep_cntr;
48942 + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
48943 + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
48944 +
48945 + /**
48946 + * Initialize the EP0 structure.
48947 + */
48948 + ep = &pcd->ep0;
48949 + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
48950 +
48951 + in_ep_cntr = 0;
48952 + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
48953 + for (i = 1; in_ep_cntr < num_in_eps; i++) {
48954 + if ((hwcfg1 & 0x1) == 0) {
48955 + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
48956 + in_ep_cntr++;
48957 + /**
48958 + * @todo NGS: Add direction to EP, based on contents
48959 + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
48960 + * sprintf(";r
48961 + */
48962 + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
48963 +
48964 + DWC_CIRCLEQ_INIT(&ep->queue);
48965 + }
48966 + hwcfg1 >>= 2;
48967 + }
48968 +
48969 + out_ep_cntr = 0;
48970 + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
48971 + for (i = 1; out_ep_cntr < num_out_eps; i++) {
48972 + if ((hwcfg1 & 0x1) == 0) {
48973 + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
48974 + out_ep_cntr++;
48975 + /**
48976 + * @todo NGS: Add direction to EP, based on contents
48977 + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
48978 + * sprintf(";r
48979 + */
48980 + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
48981 + DWC_CIRCLEQ_INIT(&ep->queue);
48982 + }
48983 + hwcfg1 >>= 2;
48984 + }
48985 +
48986 + pcd->ep0state = EP0_DISCONNECT;
48987 + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
48988 + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
48989 +}
48990 +
48991 +/**
48992 + * This function is called when the SRP timer expires. The SRP should
48993 + * complete within 6 seconds.
48994 + */
48995 +static void srp_timeout(void *ptr)
48996 +{
48997 + gotgctl_data_t gotgctl;
48998 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
48999 + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
49000 +
49001 + gotgctl.d32 = DWC_READ_REG32(addr);
49002 +
49003 + core_if->srp_timer_started = 0;
49004 +
49005 + if (core_if->adp_enable) {
49006 + if (gotgctl.b.bsesvld == 0) {
49007 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
49008 + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
49009 + /* Power off the core */
49010 + if (core_if->power_down == 2) {
49011 + gpwrdn.b.pwrdnswtch = 1;
49012 + DWC_MODIFY_REG32(&core_if->
49013 + core_global_regs->gpwrdn,
49014 + gpwrdn.d32, 0);
49015 + }
49016 +
49017 + gpwrdn.d32 = 0;
49018 + gpwrdn.b.pmuintsel = 1;
49019 + gpwrdn.b.pmuactv = 1;
49020 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
49021 + gpwrdn.d32);
49022 + dwc_otg_adp_probe_start(core_if);
49023 + } else {
49024 + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
49025 + core_if->op_state = B_PERIPHERAL;
49026 + dwc_otg_core_init(core_if);
49027 + dwc_otg_enable_global_interrupts(core_if);
49028 + cil_pcd_start(core_if);
49029 + }
49030 + }
49031 +
49032 + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
49033 + (core_if->core_params->i2c_enable)) {
49034 + DWC_PRINTF("SRP Timeout\n");
49035 +
49036 + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
49037 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
49038 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
49039 + }
49040 +
49041 + /* Clear Session Request */
49042 + gotgctl.d32 = 0;
49043 + gotgctl.b.sesreq = 1;
49044 + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
49045 + gotgctl.d32, 0);
49046 +
49047 + core_if->srp_success = 0;
49048 + } else {
49049 + __DWC_ERROR("Device not connected/responding\n");
49050 + gotgctl.b.sesreq = 0;
49051 + DWC_WRITE_REG32(addr, gotgctl.d32);
49052 + }
49053 + } else if (gotgctl.b.sesreq) {
49054 + DWC_PRINTF("SRP Timeout\n");
49055 +
49056 + __DWC_ERROR("Device not connected/responding\n");
49057 + gotgctl.b.sesreq = 0;
49058 + DWC_WRITE_REG32(addr, gotgctl.d32);
49059 + } else {
49060 + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
49061 + }
49062 +}
49063 +
49064 +/**
49065 + * Tasklet
49066 + *
49067 + */
49068 +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
49069 +
49070 +static void start_xfer_tasklet_func(void *data)
49071 +{
49072 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
49073 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49074 +
49075 + int i;
49076 + depctl_data_t diepctl;
49077 +
49078 + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
49079 +
49080 + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
49081 +
49082 + if (pcd->ep0.queue_sof) {
49083 + pcd->ep0.queue_sof = 0;
49084 + start_next_request(&pcd->ep0);
49085 + // break;
49086 + }
49087 +
49088 + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
49089 + depctl_data_t diepctl;
49090 + diepctl.d32 =
49091 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
49092 +
49093 + if (pcd->in_ep[i].queue_sof) {
49094 + pcd->in_ep[i].queue_sof = 0;
49095 + start_next_request(&pcd->in_ep[i]);
49096 + // break;
49097 + }
49098 + }
49099 +
49100 + return;
49101 +}
49102 +
49103 +/**
49104 + * This function initialized the PCD portion of the driver.
49105 + *
49106 + */
49107 +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev)
49108 +{
49109 + struct device *dev = &otg_dev->os_dep.platformdev->dev;
49110 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
49111 + dwc_otg_pcd_t *pcd = NULL;
49112 + dwc_otg_dev_if_t *dev_if;
49113 + int i;
49114 +
49115 + /*
49116 + * Allocate PCD structure
49117 + */
49118 + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
49119 +
49120 + if (pcd == NULL) {
49121 + return NULL;
49122 + }
49123 +
49124 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
49125 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
49126 +#else
49127 + pcd->lock = DWC_SPINLOCK_ALLOC();
49128 +#endif
49129 + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
49130 + pcd, core_if);//GRAYG
49131 + if (!pcd->lock) {
49132 + DWC_ERROR("Could not allocate lock for pcd");
49133 + DWC_FREE(pcd);
49134 + return NULL;
49135 + }
49136 + /* Set core_if's lock pointer to hcd->lock */
49137 + core_if->lock = pcd->lock;
49138 + pcd->core_if = core_if;
49139 +
49140 + dev_if = core_if->dev_if;
49141 + dev_if->isoc_ep = NULL;
49142 +
49143 + if (core_if->hwcfg4.b.ded_fifo_en) {
49144 + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
49145 + } else {
49146 + DWC_PRINTF("Shared Tx FIFO mode\n");
49147 + }
49148 +
49149 + /*
49150 + * Initialized the Core for Device mode here if there is nod ADP support.
49151 + * Otherwise it will be done later in dwc_otg_adp_start routine.
49152 + */
49153 + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
49154 + dwc_otg_core_dev_init(core_if);
49155 + }
49156 +
49157 + /*
49158 + * Register the PCD Callbacks.
49159 + */
49160 + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
49161 +
49162 + /*
49163 + * Initialize the DMA buffer for SETUP packets
49164 + */
49165 + if (GET_CORE_IF(pcd)->dma_enable) {
49166 + pcd->setup_pkt =
49167 + DWC_DMA_ALLOC(dev, sizeof(*pcd->setup_pkt) * 5,
49168 + &pcd->setup_pkt_dma_handle);
49169 + if (pcd->setup_pkt == NULL) {
49170 + DWC_FREE(pcd);
49171 + return NULL;
49172 + }
49173 +
49174 + pcd->status_buf =
49175 + DWC_DMA_ALLOC(dev, sizeof(uint16_t),
49176 + &pcd->status_buf_dma_handle);
49177 + if (pcd->status_buf == NULL) {
49178 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
49179 + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
49180 + DWC_FREE(pcd);
49181 + return NULL;
49182 + }
49183 +
49184 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49185 + dev_if->setup_desc_addr[0] =
49186 + dwc_otg_ep_alloc_desc_chain(dev,
49187 + &dev_if->dma_setup_desc_addr[0], 1);
49188 + dev_if->setup_desc_addr[1] =
49189 + dwc_otg_ep_alloc_desc_chain(dev,
49190 + &dev_if->dma_setup_desc_addr[1], 1);
49191 + dev_if->in_desc_addr =
49192 + dwc_otg_ep_alloc_desc_chain(dev,
49193 + &dev_if->dma_in_desc_addr, 1);
49194 + dev_if->out_desc_addr =
49195 + dwc_otg_ep_alloc_desc_chain(dev,
49196 + &dev_if->dma_out_desc_addr, 1);
49197 + pcd->data_terminated = 0;
49198 +
49199 + if (dev_if->setup_desc_addr[0] == 0
49200 + || dev_if->setup_desc_addr[1] == 0
49201 + || dev_if->in_desc_addr == 0
49202 + || dev_if->out_desc_addr == 0) {
49203 +
49204 + if (dev_if->out_desc_addr)
49205 + dwc_otg_ep_free_desc_chain(dev,
49206 + dev_if->out_desc_addr,
49207 + dev_if->dma_out_desc_addr, 1);
49208 + if (dev_if->in_desc_addr)
49209 + dwc_otg_ep_free_desc_chain(dev,
49210 + dev_if->in_desc_addr,
49211 + dev_if->dma_in_desc_addr, 1);
49212 + if (dev_if->setup_desc_addr[1])
49213 + dwc_otg_ep_free_desc_chain(dev,
49214 + dev_if->setup_desc_addr[1],
49215 + dev_if->dma_setup_desc_addr[1], 1);
49216 + if (dev_if->setup_desc_addr[0])
49217 + dwc_otg_ep_free_desc_chain(dev,
49218 + dev_if->setup_desc_addr[0],
49219 + dev_if->dma_setup_desc_addr[0], 1);
49220 +
49221 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
49222 + pcd->setup_pkt,
49223 + pcd->setup_pkt_dma_handle);
49224 + DWC_DMA_FREE(dev, sizeof(*pcd->status_buf),
49225 + pcd->status_buf,
49226 + pcd->status_buf_dma_handle);
49227 +
49228 + DWC_FREE(pcd);
49229 +
49230 + return NULL;
49231 + }
49232 + }
49233 + } else {
49234 + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
49235 + if (pcd->setup_pkt == NULL) {
49236 + DWC_FREE(pcd);
49237 + return NULL;
49238 + }
49239 +
49240 + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
49241 + if (pcd->status_buf == NULL) {
49242 + DWC_FREE(pcd->setup_pkt);
49243 + DWC_FREE(pcd);
49244 + return NULL;
49245 + }
49246 + }
49247 +
49248 + dwc_otg_pcd_reinit(pcd);
49249 +
49250 + /* Allocate the cfi object for the PCD */
49251 +#ifdef DWC_UTE_CFI
49252 + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
49253 + if (NULL == pcd->cfi)
49254 + goto fail;
49255 + if (init_cfi(pcd->cfi)) {
49256 + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
49257 + goto fail;
49258 + }
49259 +#endif
49260 +
49261 + /* Initialize tasklets */
49262 + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
49263 + start_xfer_tasklet_func, pcd);
49264 + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
49265 + do_test_mode, pcd);
49266 +
49267 + /* Initialize SRP timer */
49268 + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
49269 +
49270 + if (core_if->core_params->dev_out_nak) {
49271 + /**
49272 + * Initialize xfer timeout timer. Implemented for
49273 + * 2.93a feature "Device DDMA OUT NAK Enhancement"
49274 + */
49275 + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
49276 + pcd->core_if->ep_xfer_timer[i] =
49277 + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
49278 + &pcd->core_if->ep_xfer_info[i]);
49279 + }
49280 + }
49281 +
49282 + return pcd;
49283 +#ifdef DWC_UTE_CFI
49284 +fail:
49285 +#endif
49286 + if (pcd->setup_pkt)
49287 + DWC_FREE(pcd->setup_pkt);
49288 + if (pcd->status_buf)
49289 + DWC_FREE(pcd->status_buf);
49290 +#ifdef DWC_UTE_CFI
49291 + if (pcd->cfi)
49292 + DWC_FREE(pcd->cfi);
49293 +#endif
49294 + if (pcd)
49295 + DWC_FREE(pcd);
49296 + return NULL;
49297 +
49298 +}
49299 +
49300 +/**
49301 + * Remove PCD specific data
49302 + */
49303 +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
49304 +{
49305 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
49306 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49307 + int i;
49308 +
49309 + if (pcd->core_if->core_params->dev_out_nak) {
49310 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49311 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
49312 + pcd->core_if->ep_xfer_info[i].state = 0;
49313 + }
49314 + }
49315 +
49316 + if (GET_CORE_IF(pcd)->dma_enable) {
49317 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
49318 + pcd->setup_pkt_dma_handle);
49319 + DWC_DMA_FREE(dev, sizeof(uint16_t), pcd->status_buf,
49320 + pcd->status_buf_dma_handle);
49321 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49322 + dwc_otg_ep_free_desc_chain(dev,
49323 + dev_if->setup_desc_addr[0],
49324 + dev_if->dma_setup_desc_addr
49325 + [0], 1);
49326 + dwc_otg_ep_free_desc_chain(dev,
49327 + dev_if->setup_desc_addr[1],
49328 + dev_if->dma_setup_desc_addr
49329 + [1], 1);
49330 + dwc_otg_ep_free_desc_chain(dev,
49331 + dev_if->in_desc_addr,
49332 + dev_if->dma_in_desc_addr, 1);
49333 + dwc_otg_ep_free_desc_chain(dev,
49334 + dev_if->out_desc_addr,
49335 + dev_if->dma_out_desc_addr,
49336 + 1);
49337 + }
49338 + } else {
49339 + DWC_FREE(pcd->setup_pkt);
49340 + DWC_FREE(pcd->status_buf);
49341 + }
49342 + DWC_SPINLOCK_FREE(pcd->lock);
49343 + /* Set core_if's lock pointer to NULL */
49344 + pcd->core_if->lock = NULL;
49345 +
49346 + DWC_TASK_FREE(pcd->start_xfer_tasklet);
49347 + DWC_TASK_FREE(pcd->test_mode_tasklet);
49348 + if (pcd->core_if->core_params->dev_out_nak) {
49349 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49350 + if (pcd->core_if->ep_xfer_timer[i]) {
49351 + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
49352 + }
49353 + }
49354 + }
49355 +
49356 +/* Release the CFI object's dynamic memory */
49357 +#ifdef DWC_UTE_CFI
49358 + if (pcd->cfi->ops.release) {
49359 + pcd->cfi->ops.release(pcd->cfi);
49360 + }
49361 +#endif
49362 +
49363 + DWC_FREE(pcd);
49364 +}
49365 +
49366 +/**
49367 + * Returns whether registered pcd is dual speed or not
49368 + */
49369 +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
49370 +{
49371 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49372 +
49373 + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
49374 + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
49375 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
49376 + (core_if->core_params->ulpi_fs_ls))) {
49377 + return 0;
49378 + }
49379 +
49380 + return 1;
49381 +}
49382 +
49383 +/**
49384 + * Returns whether registered pcd is OTG capable or not
49385 + */
49386 +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
49387 +{
49388 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49389 + gusbcfg_data_t usbcfg = {.d32 = 0 };
49390 +
49391 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
49392 + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
49393 + return 0;
49394 + }
49395 +
49396 + return 1;
49397 +}
49398 +
49399 +/**
49400 + * This function assigns periodic Tx FIFO to an periodic EP
49401 + * in shared Tx FIFO mode
49402 + */
49403 +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
49404 +{
49405 + uint32_t TxMsk = 1;
49406 + int i;
49407 +
49408 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
49409 + if ((TxMsk & core_if->tx_msk) == 0) {
49410 + core_if->tx_msk |= TxMsk;
49411 + return i + 1;
49412 + }
49413 + TxMsk <<= 1;
49414 + }
49415 + return 0;
49416 +}
49417 +
49418 +/**
49419 + * This function assigns periodic Tx FIFO to an periodic EP
49420 + * in shared Tx FIFO mode
49421 + */
49422 +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
49423 +{
49424 + uint32_t PerTxMsk = 1;
49425 + int i;
49426 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
49427 + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
49428 + core_if->p_tx_msk |= PerTxMsk;
49429 + return i + 1;
49430 + }
49431 + PerTxMsk <<= 1;
49432 + }
49433 + return 0;
49434 +}
49435 +
49436 +/**
49437 + * This function releases periodic Tx FIFO
49438 + * in shared Tx FIFO mode
49439 + */
49440 +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
49441 + uint32_t fifo_num)
49442 +{
49443 + core_if->p_tx_msk =
49444 + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
49445 +}
49446 +
49447 +/**
49448 + * This function releases periodic Tx FIFO
49449 + * in shared Tx FIFO mode
49450 + */
49451 +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
49452 +{
49453 + core_if->tx_msk =
49454 + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
49455 +}
49456 +
49457 +/**
49458 + * This function is being called from gadget
49459 + * to enable PCD endpoint.
49460 + */
49461 +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
49462 + const uint8_t * ep_desc, void *usb_ep)
49463 +{
49464 + int num, dir;
49465 + dwc_otg_pcd_ep_t *ep = NULL;
49466 + const usb_endpoint_descriptor_t *desc;
49467 + dwc_irqflags_t flags;
49468 + fifosize_data_t dptxfsiz = {.d32 = 0 };
49469 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
49470 + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
49471 + int retval = 0;
49472 + int i, epcount;
49473 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49474 +
49475 + desc = (const usb_endpoint_descriptor_t *)ep_desc;
49476 +
49477 + if (!desc) {
49478 + pcd->ep0.priv = usb_ep;
49479 + ep = &pcd->ep0;
49480 + retval = -DWC_E_INVALID;
49481 + goto out;
49482 + }
49483 +
49484 + num = UE_GET_ADDR(desc->bEndpointAddress);
49485 + dir = UE_GET_DIR(desc->bEndpointAddress);
49486 +
49487 + if (!UGETW(desc->wMaxPacketSize)) {
49488 + DWC_WARN("bad maxpacketsize\n");
49489 + retval = -DWC_E_INVALID;
49490 + goto out;
49491 + }
49492 +
49493 + if (dir == UE_DIR_IN) {
49494 + epcount = pcd->core_if->dev_if->num_in_eps;
49495 + for (i = 0; i < epcount; i++) {
49496 + if (num == pcd->in_ep[i].dwc_ep.num) {
49497 + ep = &pcd->in_ep[i];
49498 + break;
49499 + }
49500 + }
49501 + } else {
49502 + epcount = pcd->core_if->dev_if->num_out_eps;
49503 + for (i = 0; i < epcount; i++) {
49504 + if (num == pcd->out_ep[i].dwc_ep.num) {
49505 + ep = &pcd->out_ep[i];
49506 + break;
49507 + }
49508 + }
49509 + }
49510 +
49511 + if (!ep) {
49512 + DWC_WARN("bad address\n");
49513 + retval = -DWC_E_INVALID;
49514 + goto out;
49515 + }
49516 +
49517 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49518 +
49519 + ep->desc = desc;
49520 + ep->priv = usb_ep;
49521 +
49522 + /*
49523 + * Activate the EP
49524 + */
49525 + ep->stopped = 0;
49526 +
49527 + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
49528 + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
49529 +
49530 + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
49531 +
49532 + if (ep->dwc_ep.is_in) {
49533 + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49534 + ep->dwc_ep.tx_fifo_num = 0;
49535 +
49536 + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
49537 + /*
49538 + * if ISOC EP then assign a Periodic Tx FIFO.
49539 + */
49540 + ep->dwc_ep.tx_fifo_num =
49541 + assign_perio_tx_fifo(GET_CORE_IF(pcd));
49542 + }
49543 + } else {
49544 + /*
49545 + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
49546 + */
49547 + ep->dwc_ep.tx_fifo_num =
49548 + assign_tx_fifo(GET_CORE_IF(pcd));
49549 + }
49550 +
49551 + /* Calculating EP info controller base address */
49552 + if (ep->dwc_ep.tx_fifo_num
49553 + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49554 + gdfifocfg.d32 =
49555 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
49556 + core_global_regs->gdfifocfg);
49557 + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
49558 + dptxfsiz.d32 =
49559 + (DWC_READ_REG32
49560 + (&GET_CORE_IF(pcd)->core_global_regs->
49561 + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
49562 + gdfifocfg.b.epinfobase =
49563 + gdfifocfgbase.d32 + dptxfsiz.d32;
49564 + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
49565 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
49566 + core_global_regs->gdfifocfg,
49567 + gdfifocfg.d32);
49568 + }
49569 + }
49570 + }
49571 + /* Set initial data PID. */
49572 + if (ep->dwc_ep.type == UE_BULK) {
49573 + ep->dwc_ep.data_pid_start = 0;
49574 + }
49575 +
49576 + /* Alloc DMA Descriptors */
49577 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49578 +#ifndef DWC_UTE_PER_IO
49579 + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
49580 +#endif
49581 + ep->dwc_ep.desc_addr =
49582 + dwc_otg_ep_alloc_desc_chain(dev,
49583 + &ep->dwc_ep.dma_desc_addr,
49584 + MAX_DMA_DESC_CNT);
49585 + if (!ep->dwc_ep.desc_addr) {
49586 + DWC_WARN("%s, can't allocate DMA descriptor\n",
49587 + __func__);
49588 + retval = -DWC_E_SHUTDOWN;
49589 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49590 + goto out;
49591 + }
49592 +#ifndef DWC_UTE_PER_IO
49593 + }
49594 +#endif
49595 + }
49596 +
49597 + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
49598 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
49599 + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
49600 +#ifdef DWC_UTE_PER_IO
49601 + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
49602 +#endif
49603 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
49604 + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
49605 + ep->dwc_ep.frame_num = 0xFFFFFFFF;
49606 + }
49607 +
49608 + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
49609 +
49610 +#ifdef DWC_UTE_CFI
49611 + if (pcd->cfi->ops.ep_enable) {
49612 + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
49613 + }
49614 +#endif
49615 +
49616 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49617 +
49618 +out:
49619 + return retval;
49620 +}
49621 +
49622 +/**
49623 + * This function is being called from gadget
49624 + * to disable PCD endpoint.
49625 + */
49626 +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
49627 +{
49628 + dwc_otg_pcd_ep_t *ep;
49629 + dwc_irqflags_t flags;
49630 + dwc_otg_dev_dma_desc_t *desc_addr;
49631 + dwc_dma_t dma_desc_addr;
49632 + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
49633 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
49634 + fifosize_data_t dptxfsiz = {.d32 = 0 };
49635 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49636 +
49637 + ep = get_ep_from_handle(pcd, ep_handle);
49638 +
49639 + if (!ep || !ep->desc) {
49640 + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
49641 + return -DWC_E_INVALID;
49642 + }
49643 +
49644 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49645 +
49646 + dwc_otg_request_nuke(ep);
49647 +
49648 + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
49649 + if (pcd->core_if->core_params->dev_out_nak) {
49650 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
49651 + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
49652 + }
49653 + ep->desc = NULL;
49654 + ep->stopped = 1;
49655 +
49656 + gdfifocfg.d32 =
49657 + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
49658 + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
49659 +
49660 + if (ep->dwc_ep.is_in) {
49661 + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49662 + /* Flush the Tx FIFO */
49663 + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
49664 + ep->dwc_ep.tx_fifo_num);
49665 + }
49666 + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
49667 + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
49668 + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49669 + /* Decreasing EPinfo Base Addr */
49670 + dptxfsiz.d32 =
49671 + (DWC_READ_REG32
49672 + (&GET_CORE_IF(pcd)->
49673 + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
49674 + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
49675 + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
49676 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
49677 + gdfifocfg.d32);
49678 + }
49679 + }
49680 + }
49681 +
49682 + /* Free DMA Descriptors */
49683 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49684 + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
49685 + desc_addr = ep->dwc_ep.desc_addr;
49686 + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
49687 +
49688 + /* Cannot call dma_free_coherent() with IRQs disabled */
49689 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49690 + dwc_otg_ep_free_desc_chain(dev, desc_addr, dma_desc_addr,
49691 + MAX_DMA_DESC_CNT);
49692 +
49693 + goto out_unlocked;
49694 + }
49695 + }
49696 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49697 +
49698 +out_unlocked:
49699 + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
49700 + ep->dwc_ep.is_in ? "IN" : "OUT");
49701 + return 0;
49702 +
49703 +}
49704 +
49705 +/******************************************************************************/
49706 +#ifdef DWC_UTE_PER_IO
49707 +
49708 +/**
49709 + * Free the request and its extended parts
49710 + *
49711 + */
49712 +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
49713 +{
49714 + DWC_FREE(req->ext_req.per_io_frame_descs);
49715 + DWC_FREE(req);
49716 +}
49717 +
49718 +/**
49719 + * Start the next request in the endpoint's queue.
49720 + *
49721 + */
49722 +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
49723 + dwc_otg_pcd_ep_t * ep)
49724 +{
49725 + int i;
49726 + dwc_otg_pcd_request_t *req = NULL;
49727 + dwc_ep_t *dwcep = NULL;
49728 + struct dwc_iso_xreq_port *ereq = NULL;
49729 + struct dwc_iso_pkt_desc_port *ddesc_iso;
49730 + uint16_t nat;
49731 + depctl_data_t diepctl;
49732 +
49733 + dwcep = &ep->dwc_ep;
49734 +
49735 + if (dwcep->xiso_active_xfers > 0) {
49736 +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
49737 + DWC_WARN("There are currently active transfers for EP%d \
49738 + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
49739 + dwcep->xiso_queued_xfers);
49740 +#endif
49741 + return 0;
49742 + }
49743 +
49744 + nat = UGETW(ep->desc->wMaxPacketSize);
49745 + nat = (nat >> 11) & 0x03;
49746 +
49747 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
49748 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
49749 + ereq = &req->ext_req;
49750 + ep->stopped = 0;
49751 +
49752 + /* Get the frame number */
49753 + dwcep->xiso_frame_num =
49754 + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
49755 + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
49756 +
49757 + ddesc_iso = ereq->per_io_frame_descs;
49758 +
49759 + if (dwcep->is_in) {
49760 + /* Setup DMA Descriptor chain for IN Isoc request */
49761 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49762 + //if ((i % (nat + 1)) == 0)
49763 + if ( i > 0 )
49764 + dwcep->xiso_frame_num =
49765 + (dwcep->xiso_bInterval +
49766 + dwcep->xiso_frame_num) & 0x3FFF;
49767 + dwcep->desc_addr[i].buf =
49768 + req->dma + ddesc_iso[i].offset;
49769 + dwcep->desc_addr[i].status.b_iso_in.txbytes =
49770 + ddesc_iso[i].length;
49771 + dwcep->desc_addr[i].status.b_iso_in.framenum =
49772 + dwcep->xiso_frame_num;
49773 + dwcep->desc_addr[i].status.b_iso_in.bs =
49774 + BS_HOST_READY;
49775 + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
49776 + dwcep->desc_addr[i].status.b_iso_in.sp =
49777 + (ddesc_iso[i].length %
49778 + dwcep->maxpacket) ? 1 : 0;
49779 + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
49780 + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
49781 + dwcep->desc_addr[i].status.b_iso_in.l = 0;
49782 +
49783 + /* Process the last descriptor */
49784 + if (i == ereq->pio_pkt_count - 1) {
49785 + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
49786 + dwcep->desc_addr[i].status.b_iso_in.l = 1;
49787 + }
49788 + }
49789 +
49790 + /* Setup and start the transfer for this endpoint */
49791 + dwcep->xiso_active_xfers++;
49792 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
49793 + in_ep_regs[dwcep->num]->diepdma,
49794 + dwcep->dma_desc_addr);
49795 + diepctl.d32 = 0;
49796 + diepctl.b.epena = 1;
49797 + diepctl.b.cnak = 1;
49798 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
49799 + in_ep_regs[dwcep->num]->diepctl, 0,
49800 + diepctl.d32);
49801 + } else {
49802 + /* Setup DMA Descriptor chain for OUT Isoc request */
49803 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49804 + //if ((i % (nat + 1)) == 0)
49805 + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
49806 + dwcep->xiso_frame_num) & 0x3FFF;
49807 + dwcep->desc_addr[i].buf =
49808 + req->dma + ddesc_iso[i].offset;
49809 + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
49810 + ddesc_iso[i].length;
49811 + dwcep->desc_addr[i].status.b_iso_out.framenum =
49812 + dwcep->xiso_frame_num;
49813 + dwcep->desc_addr[i].status.b_iso_out.bs =
49814 + BS_HOST_READY;
49815 + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
49816 + dwcep->desc_addr[i].status.b_iso_out.sp =
49817 + (ddesc_iso[i].length %
49818 + dwcep->maxpacket) ? 1 : 0;
49819 + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
49820 + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
49821 + dwcep->desc_addr[i].status.b_iso_out.l = 0;
49822 +
49823 + /* Process the last descriptor */
49824 + if (i == ereq->pio_pkt_count - 1) {
49825 + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
49826 + dwcep->desc_addr[i].status.b_iso_out.l = 1;
49827 + }
49828 + }
49829 +
49830 + /* Setup and start the transfer for this endpoint */
49831 + dwcep->xiso_active_xfers++;
49832 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
49833 + dev_if->out_ep_regs[dwcep->num]->
49834 + doepdma, dwcep->dma_desc_addr);
49835 + diepctl.d32 = 0;
49836 + diepctl.b.epena = 1;
49837 + diepctl.b.cnak = 1;
49838 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
49839 + dev_if->out_ep_regs[dwcep->num]->
49840 + doepctl, 0, diepctl.d32);
49841 + }
49842 +
49843 + } else {
49844 + ep->stopped = 1;
49845 + }
49846 +
49847 + return 0;
49848 +}
49849 +
49850 +/**
49851 + * - Remove the request from the queue
49852 + */
49853 +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
49854 +{
49855 + dwc_otg_pcd_request_t *req = NULL;
49856 + struct dwc_iso_xreq_port *ereq = NULL;
49857 + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
49858 + dwc_ep_t *dwcep = NULL;
49859 + int i;
49860 +
49861 + //DWC_DEBUG();
49862 + dwcep = &ep->dwc_ep;
49863 +
49864 + /* Get the first pending request from the queue */
49865 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
49866 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
49867 + if (!req) {
49868 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
49869 + return;
49870 + }
49871 + dwcep->xiso_active_xfers--;
49872 + dwcep->xiso_queued_xfers--;
49873 + /* Remove this request from the queue */
49874 + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
49875 + } else {
49876 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
49877 + return;
49878 + }
49879 +
49880 + ep->stopped = 1;
49881 + ereq = &req->ext_req;
49882 + ddesc_iso = ereq->per_io_frame_descs;
49883 +
49884 + if (dwcep->xiso_active_xfers < 0) {
49885 + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
49886 + dwcep->xiso_active_xfers);
49887 + }
49888 +
49889 + /* Fill the Isoc descs of portable extended req from dma descriptors */
49890 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49891 + if (dwcep->is_in) { /* IN endpoints */
49892 + ddesc_iso[i].actual_length = ddesc_iso[i].length -
49893 + dwcep->desc_addr[i].status.b_iso_in.txbytes;
49894 + ddesc_iso[i].status =
49895 + dwcep->desc_addr[i].status.b_iso_in.txsts;
49896 + } else { /* OUT endpoints */
49897 + ddesc_iso[i].actual_length = ddesc_iso[i].length -
49898 + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
49899 + ddesc_iso[i].status =
49900 + dwcep->desc_addr[i].status.b_iso_out.rxsts;
49901 + }
49902 + }
49903 +
49904 + DWC_SPINUNLOCK(ep->pcd->lock);
49905 +
49906 + /* Call the completion function in the non-portable logic */
49907 + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
49908 + &req->ext_req);
49909 +
49910 + DWC_SPINLOCK(ep->pcd->lock);
49911 +
49912 + /* Free the request - specific freeing needed for extended request object */
49913 + dwc_pcd_xiso_ereq_free(ep, req);
49914 +
49915 + /* Start the next request */
49916 + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
49917 +
49918 + return;
49919 +}
49920 +
49921 +/**
49922 + * Create and initialize the Isoc pkt descriptors of the extended request.
49923 + *
49924 + */
49925 +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
49926 + void *ereq_nonport,
49927 + int atomic_alloc)
49928 +{
49929 + struct dwc_iso_xreq_port *ereq = NULL;
49930 + struct dwc_iso_xreq_port *req_mapped = NULL;
49931 + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
49932 + uint32_t pkt_count;
49933 + int i;
49934 +
49935 + ereq = &req->ext_req;
49936 + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
49937 + pkt_count = req_mapped->pio_pkt_count;
49938 +
49939 + /* Create the isoc descs */
49940 + if (atomic_alloc) {
49941 + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
49942 + } else {
49943 + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
49944 + }
49945 +
49946 + if (!ipds) {
49947 + DWC_ERROR("Failed to allocate isoc descriptors");
49948 + return -DWC_E_NO_MEMORY;
49949 + }
49950 +
49951 + /* Initialize the extended request fields */
49952 + ereq->per_io_frame_descs = ipds;
49953 + ereq->error_count = 0;
49954 + ereq->pio_alloc_pkt_count = pkt_count;
49955 + ereq->pio_pkt_count = pkt_count;
49956 + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
49957 +
49958 + /* Init the Isoc descriptors */
49959 + for (i = 0; i < pkt_count; i++) {
49960 + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
49961 + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
49962 + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
49963 + ipds[i].actual_length =
49964 + req_mapped->per_io_frame_descs[i].actual_length;
49965 + }
49966 +
49967 + return 0;
49968 +}
49969 +
49970 +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
49971 +{
49972 + struct dwc_iso_pkt_desc_port *xfd = NULL;
49973 + int i;
49974 +
49975 + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
49976 + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
49977 + DWC_DEBUG("error_count=%d", ereq->error_count);
49978 + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
49979 + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
49980 + DWC_DEBUG("res=%d", ereq->res);
49981 +
49982 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49983 + xfd = &ereq->per_io_frame_descs[0];
49984 + DWC_DEBUG("FD #%d", i);
49985 +
49986 + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
49987 + DWC_DEBUG("xfd->length=%d", xfd->length);
49988 + DWC_DEBUG("xfd->offset=%d", xfd->offset);
49989 + DWC_DEBUG("xfd->status=%d", xfd->status);
49990 + }
49991 +}
49992 +
49993 +/**
49994 + *
49995 + */
49996 +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
49997 + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
49998 + int zero, void *req_handle, int atomic_alloc,
49999 + void *ereq_nonport)
50000 +{
50001 + dwc_otg_pcd_request_t *req = NULL;
50002 + dwc_otg_pcd_ep_t *ep;
50003 + dwc_irqflags_t flags;
50004 + int res;
50005 +
50006 + ep = get_ep_from_handle(pcd, ep_handle);
50007 + if (!ep) {
50008 + DWC_WARN("bad ep\n");
50009 + return -DWC_E_INVALID;
50010 + }
50011 +
50012 + /* We support this extension only for DDMA mode */
50013 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
50014 + if (!GET_CORE_IF(pcd)->dma_desc_enable)
50015 + return -DWC_E_INVALID;
50016 +
50017 + /* Create a dwc_otg_pcd_request_t object */
50018 + if (atomic_alloc) {
50019 + req = DWC_ALLOC_ATOMIC(sizeof(*req));
50020 + } else {
50021 + req = DWC_ALLOC(sizeof(*req));
50022 + }
50023 +
50024 + if (!req) {
50025 + return -DWC_E_NO_MEMORY;
50026 + }
50027 +
50028 + /* Create the Isoc descs for this request which shall be the exact match
50029 + * of the structure sent to us from the non-portable logic */
50030 + res =
50031 + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
50032 + if (res) {
50033 + DWC_WARN("Failed to init the Isoc descriptors");
50034 + DWC_FREE(req);
50035 + return res;
50036 + }
50037 +
50038 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50039 +
50040 + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
50041 + req->buf = buf;
50042 + req->dma = dma_buf;
50043 + req->length = buflen;
50044 + req->sent_zlp = zero;
50045 + req->priv = req_handle;
50046 +
50047 + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50048 + ep->dwc_ep.dma_addr = dma_buf;
50049 + ep->dwc_ep.start_xfer_buff = buf;
50050 + ep->dwc_ep.xfer_buff = buf;
50051 + ep->dwc_ep.xfer_len = 0;
50052 + ep->dwc_ep.xfer_count = 0;
50053 + ep->dwc_ep.sent_zlp = 0;
50054 + ep->dwc_ep.total_len = buflen;
50055 +
50056 + /* Add this request to the tail */
50057 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50058 + ep->dwc_ep.xiso_queued_xfers++;
50059 +
50060 +//DWC_DEBUG("CP_0");
50061 +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
50062 +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
50063 +//prn_ext_request(&req->ext_req);
50064 +
50065 + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50066 +
50067 + /* If the req->status == ASAP then check if there is any active transfer
50068 + * for this endpoint. If no active transfers, then get the first entry
50069 + * from the queue and start that transfer
50070 + */
50071 + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
50072 + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
50073 + if (res) {
50074 + DWC_WARN("Failed to start the next Isoc transfer");
50075 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50076 + DWC_FREE(req);
50077 + return res;
50078 + }
50079 + }
50080 +
50081 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50082 + return 0;
50083 +}
50084 +
50085 +#endif
50086 +/* END ifdef DWC_UTE_PER_IO ***************************************************/
50087 +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
50088 + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
50089 + int zero, void *req_handle, int atomic_alloc)
50090 +{
50091 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
50092 + dwc_irqflags_t flags;
50093 + dwc_otg_pcd_request_t *req;
50094 + dwc_otg_pcd_ep_t *ep;
50095 + uint32_t max_transfer;
50096 +
50097 + ep = get_ep_from_handle(pcd, ep_handle);
50098 + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
50099 + DWC_WARN("bad ep\n");
50100 + return -DWC_E_INVALID;
50101 + }
50102 +
50103 + if (atomic_alloc) {
50104 + req = DWC_ALLOC_ATOMIC(sizeof(*req));
50105 + } else {
50106 + req = DWC_ALLOC(sizeof(*req));
50107 + }
50108 +
50109 + if (!req) {
50110 + return -DWC_E_NO_MEMORY;
50111 + }
50112 + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
50113 + if (!GET_CORE_IF(pcd)->core_params->opt) {
50114 + if (ep->dwc_ep.num != 0) {
50115 + DWC_ERROR("queue req %p, len %d buf %p\n",
50116 + req_handle, buflen, buf);
50117 + }
50118 + }
50119 +
50120 + req->buf = buf;
50121 + req->dma = dma_buf;
50122 + req->length = buflen;
50123 + req->sent_zlp = zero;
50124 + req->priv = req_handle;
50125 + req->dw_align_buf = NULL;
50126 + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
50127 + && !GET_CORE_IF(pcd)->dma_desc_enable)
50128 + req->dw_align_buf = DWC_DMA_ALLOC(dev, buflen,
50129 + &req->dw_align_buf_dma);
50130 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50131 +
50132 + /*
50133 + * After adding request to the queue for IN ISOC wait for In Token Received
50134 + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
50135 + * Received when EP is disabled interrupt to obtain starting microframe
50136 + * (odd/even) start transfer
50137 + */
50138 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
50139 + if (req != 0) {
50140 + depctl_data_t depctl = {.d32 =
50141 + DWC_READ_REG32(&pcd->core_if->dev_if->
50142 + in_ep_regs[ep->dwc_ep.num]->
50143 + diepctl) };
50144 + ++pcd->request_pending;
50145 +
50146 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50147 + if (ep->dwc_ep.is_in) {
50148 + depctl.b.cnak = 1;
50149 + DWC_WRITE_REG32(&pcd->core_if->dev_if->
50150 + in_ep_regs[ep->dwc_ep.num]->
50151 + diepctl, depctl.d32);
50152 + }
50153 +
50154 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50155 + }
50156 + return 0;
50157 + }
50158 +
50159 + /*
50160 + * For EP0 IN without premature status, zlp is required?
50161 + */
50162 + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
50163 + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
50164 + //_req->zero = 1;
50165 + }
50166 +
50167 + /* Start the transfer */
50168 + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
50169 + /* EP0 Transfer? */
50170 + if (ep->dwc_ep.num == 0) {
50171 + switch (pcd->ep0state) {
50172 + case EP0_IN_DATA_PHASE:
50173 + DWC_DEBUGPL(DBG_PCD,
50174 + "%s ep0: EP0_IN_DATA_PHASE\n",
50175 + __func__);
50176 + break;
50177 +
50178 + case EP0_OUT_DATA_PHASE:
50179 + DWC_DEBUGPL(DBG_PCD,
50180 + "%s ep0: EP0_OUT_DATA_PHASE\n",
50181 + __func__);
50182 + if (pcd->request_config) {
50183 + /* Complete STATUS PHASE */
50184 + ep->dwc_ep.is_in = 1;
50185 + pcd->ep0state = EP0_IN_STATUS_PHASE;
50186 + }
50187 + break;
50188 +
50189 + case EP0_IN_STATUS_PHASE:
50190 + DWC_DEBUGPL(DBG_PCD,
50191 + "%s ep0: EP0_IN_STATUS_PHASE\n",
50192 + __func__);
50193 + break;
50194 +
50195 + default:
50196 + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
50197 + pcd->ep0state);
50198 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50199 + return -DWC_E_SHUTDOWN;
50200 + }
50201 +
50202 + ep->dwc_ep.dma_addr = dma_buf;
50203 + ep->dwc_ep.start_xfer_buff = buf;
50204 + ep->dwc_ep.xfer_buff = buf;
50205 + ep->dwc_ep.xfer_len = buflen;
50206 + ep->dwc_ep.xfer_count = 0;
50207 + ep->dwc_ep.sent_zlp = 0;
50208 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
50209 +
50210 + if (zero) {
50211 + if ((ep->dwc_ep.xfer_len %
50212 + ep->dwc_ep.maxpacket == 0)
50213 + && (ep->dwc_ep.xfer_len != 0)) {
50214 + ep->dwc_ep.sent_zlp = 1;
50215 + }
50216 +
50217 + }
50218 +
50219 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
50220 + &ep->dwc_ep);
50221 + } // non-ep0 endpoints
50222 + else {
50223 +#ifdef DWC_UTE_CFI
50224 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
50225 + /* store the request length */
50226 + ep->dwc_ep.cfi_req_len = buflen;
50227 + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
50228 + ep, req);
50229 + } else {
50230 +#endif
50231 + max_transfer =
50232 + GET_CORE_IF(ep->pcd)->core_params->
50233 + max_transfer_size;
50234 +
50235 + /* Setup and start the Transfer */
50236 + if (req->dw_align_buf){
50237 + if (ep->dwc_ep.is_in)
50238 + dwc_memcpy(req->dw_align_buf,
50239 + buf, buflen);
50240 + ep->dwc_ep.dma_addr =
50241 + req->dw_align_buf_dma;
50242 + ep->dwc_ep.start_xfer_buff =
50243 + req->dw_align_buf;
50244 + ep->dwc_ep.xfer_buff =
50245 + req->dw_align_buf;
50246 + } else {
50247 + ep->dwc_ep.dma_addr = dma_buf;
50248 + ep->dwc_ep.start_xfer_buff = buf;
50249 + ep->dwc_ep.xfer_buff = buf;
50250 + }
50251 + ep->dwc_ep.xfer_len = 0;
50252 + ep->dwc_ep.xfer_count = 0;
50253 + ep->dwc_ep.sent_zlp = 0;
50254 + ep->dwc_ep.total_len = buflen;
50255 +
50256 + ep->dwc_ep.maxxfer = max_transfer;
50257 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
50258 + uint32_t out_max_xfer =
50259 + DDMA_MAX_TRANSFER_SIZE -
50260 + (DDMA_MAX_TRANSFER_SIZE % 4);
50261 + if (ep->dwc_ep.is_in) {
50262 + if (ep->dwc_ep.maxxfer >
50263 + DDMA_MAX_TRANSFER_SIZE) {
50264 + ep->dwc_ep.maxxfer =
50265 + DDMA_MAX_TRANSFER_SIZE;
50266 + }
50267 + } else {
50268 + if (ep->dwc_ep.maxxfer >
50269 + out_max_xfer) {
50270 + ep->dwc_ep.maxxfer =
50271 + out_max_xfer;
50272 + }
50273 + }
50274 + }
50275 + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
50276 + ep->dwc_ep.maxxfer -=
50277 + (ep->dwc_ep.maxxfer %
50278 + ep->dwc_ep.maxpacket);
50279 + }
50280 +
50281 + if (zero) {
50282 + if ((ep->dwc_ep.total_len %
50283 + ep->dwc_ep.maxpacket == 0)
50284 + && (ep->dwc_ep.total_len != 0)) {
50285 + ep->dwc_ep.sent_zlp = 1;
50286 + }
50287 + }
50288 +#ifdef DWC_UTE_CFI
50289 + }
50290 +#endif
50291 + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
50292 + &ep->dwc_ep);
50293 + }
50294 + }
50295 +
50296 + if (req != 0) {
50297 + ++pcd->request_pending;
50298 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50299 + if (ep->dwc_ep.is_in && ep->stopped
50300 + && !(GET_CORE_IF(pcd)->dma_enable)) {
50301 + /** @todo NGS Create a function for this. */
50302 + diepmsk_data_t diepmsk = {.d32 = 0 };
50303 + diepmsk.b.intktxfemp = 1;
50304 + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
50305 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
50306 + dev_if->dev_global_regs->diepeachintmsk
50307 + [ep->dwc_ep.num], 0,
50308 + diepmsk.d32);
50309 + } else {
50310 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
50311 + dev_if->dev_global_regs->
50312 + diepmsk, 0, diepmsk.d32);
50313 + }
50314 +
50315 + }
50316 + }
50317 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50318 +
50319 + return 0;
50320 +}
50321 +
50322 +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
50323 + void *req_handle)
50324 +{
50325 + dwc_irqflags_t flags;
50326 + dwc_otg_pcd_request_t *req;
50327 + dwc_otg_pcd_ep_t *ep;
50328 +
50329 + ep = get_ep_from_handle(pcd, ep_handle);
50330 + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
50331 + DWC_WARN("bad argument\n");
50332 + return -DWC_E_INVALID;
50333 + }
50334 +
50335 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50336 +
50337 + /* make sure it's actually queued on this endpoint */
50338 + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
50339 + if (req->priv == (void *)req_handle) {
50340 + break;
50341 + }
50342 + }
50343 +
50344 + if (req->priv != (void *)req_handle) {
50345 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50346 + return -DWC_E_INVALID;
50347 + }
50348 +
50349 + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
50350 + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
50351 + } else {
50352 + req = NULL;
50353 + }
50354 +
50355 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50356 +
50357 + return req ? 0 : -DWC_E_SHUTDOWN;
50358 +
50359 +}
50360 +
50361 +/**
50362 + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
50363 + *
50364 + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
50365 + * requests. If the gadget driver clears the halt status, it will
50366 + * automatically unwedge the endpoint.
50367 + *
50368 + * Returns zero on success, else negative DWC error code.
50369 + */
50370 +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
50371 +{
50372 + dwc_otg_pcd_ep_t *ep;
50373 + dwc_irqflags_t flags;
50374 + int retval = 0;
50375 +
50376 + ep = get_ep_from_handle(pcd, ep_handle);
50377 +
50378 + if ((!ep->desc && ep != &pcd->ep0) ||
50379 + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
50380 + DWC_WARN("%s, bad ep\n", __func__);
50381 + return -DWC_E_INVALID;
50382 + }
50383 +
50384 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50385 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50386 + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
50387 + ep->dwc_ep.is_in ? "IN" : "OUT");
50388 + retval = -DWC_E_AGAIN;
50389 + } else {
50390 + /* This code needs to be reviewed */
50391 + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
50392 + dtxfsts_data_t txstatus;
50393 + fifosize_data_t txfifosize;
50394 +
50395 + txfifosize.d32 =
50396 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
50397 + core_global_regs->dtxfsiz[ep->dwc_ep.
50398 + tx_fifo_num]);
50399 + txstatus.d32 =
50400 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
50401 + dev_if->in_ep_regs[ep->dwc_ep.num]->
50402 + dtxfsts);
50403 +
50404 + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
50405 + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
50406 + retval = -DWC_E_AGAIN;
50407 + } else {
50408 + if (ep->dwc_ep.num == 0) {
50409 + pcd->ep0state = EP0_STALL;
50410 + }
50411 +
50412 + ep->stopped = 1;
50413 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
50414 + &ep->dwc_ep);
50415 + }
50416 + } else {
50417 + if (ep->dwc_ep.num == 0) {
50418 + pcd->ep0state = EP0_STALL;
50419 + }
50420 +
50421 + ep->stopped = 1;
50422 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50423 + }
50424 + }
50425 +
50426 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50427 +
50428 + return retval;
50429 +}
50430 +
50431 +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
50432 +{
50433 + dwc_otg_pcd_ep_t *ep;
50434 + dwc_irqflags_t flags;
50435 + int retval = 0;
50436 +
50437 + ep = get_ep_from_handle(pcd, ep_handle);
50438 +
50439 + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
50440 + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
50441 + DWC_WARN("%s, bad ep\n", __func__);
50442 + return -DWC_E_INVALID;
50443 + }
50444 +
50445 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50446 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50447 + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
50448 + ep->dwc_ep.is_in ? "IN" : "OUT");
50449 + retval = -DWC_E_AGAIN;
50450 + } else if (value == 0) {
50451 + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50452 + } else if (value == 1) {
50453 + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
50454 + dtxfsts_data_t txstatus;
50455 + fifosize_data_t txfifosize;
50456 +
50457 + txfifosize.d32 =
50458 + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
50459 + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
50460 + txstatus.d32 =
50461 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
50462 + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
50463 +
50464 + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
50465 + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
50466 + retval = -DWC_E_AGAIN;
50467 + } else {
50468 + if (ep->dwc_ep.num == 0) {
50469 + pcd->ep0state = EP0_STALL;
50470 + }
50471 +
50472 + ep->stopped = 1;
50473 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
50474 + &ep->dwc_ep);
50475 + }
50476 + } else {
50477 + if (ep->dwc_ep.num == 0) {
50478 + pcd->ep0state = EP0_STALL;
50479 + }
50480 +
50481 + ep->stopped = 1;
50482 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50483 + }
50484 + } else if (value == 2) {
50485 + ep->dwc_ep.stall_clear_flag = 0;
50486 + } else if (value == 3) {
50487 + ep->dwc_ep.stall_clear_flag = 1;
50488 + }
50489 +
50490 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50491 +
50492 + return retval;
50493 +}
50494 +
50495 +/**
50496 + * This function initiates remote wakeup of the host from suspend state.
50497 + */
50498 +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
50499 +{
50500 + dctl_data_t dctl = { 0 };
50501 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50502 + dsts_data_t dsts;
50503 +
50504 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
50505 + if (!dsts.b.suspsts) {
50506 + DWC_WARN("Remote wakeup while is not in suspend state\n");
50507 + }
50508 + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
50509 + if (pcd->remote_wakeup_enable) {
50510 + if (set) {
50511 +
50512 + if (core_if->adp_enable) {
50513 + gpwrdn_data_t gpwrdn;
50514 +
50515 + dwc_otg_adp_probe_stop(core_if);
50516 +
50517 + /* Mask SRP detected interrupt from Power Down Logic */
50518 + gpwrdn.d32 = 0;
50519 + gpwrdn.b.srp_det_msk = 1;
50520 + DWC_MODIFY_REG32(&core_if->
50521 + core_global_regs->gpwrdn,
50522 + gpwrdn.d32, 0);
50523 +
50524 + /* Disable Power Down Logic */
50525 + gpwrdn.d32 = 0;
50526 + gpwrdn.b.pmuactv = 1;
50527 + DWC_MODIFY_REG32(&core_if->
50528 + core_global_regs->gpwrdn,
50529 + gpwrdn.d32, 0);
50530 +
50531 + /*
50532 + * Initialize the Core for Device mode.
50533 + */
50534 + core_if->op_state = B_PERIPHERAL;
50535 + dwc_otg_core_init(core_if);
50536 + dwc_otg_enable_global_interrupts(core_if);
50537 + cil_pcd_start(core_if);
50538 +
50539 + dwc_otg_initiate_srp(core_if);
50540 + }
50541 +
50542 + dctl.b.rmtwkupsig = 1;
50543 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
50544 + dctl, 0, dctl.d32);
50545 + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
50546 +
50547 + dwc_mdelay(2);
50548 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
50549 + dctl, dctl.d32, 0);
50550 + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
50551 + }
50552 + } else {
50553 + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
50554 + }
50555 +}
50556 +
50557 +#ifdef CONFIG_USB_DWC_OTG_LPM
50558 +/**
50559 + * This function initiates remote wakeup of the host from L1 sleep state.
50560 + */
50561 +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
50562 +{
50563 + glpmcfg_data_t lpmcfg;
50564 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50565 +
50566 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
50567 +
50568 + /* Check if we are in L1 state */
50569 + if (!lpmcfg.b.prt_sleep_sts) {
50570 + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
50571 + return;
50572 + }
50573 +
50574 + /* Check if host allows remote wakeup */
50575 + if (!lpmcfg.b.rem_wkup_en) {
50576 + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
50577 + return;
50578 + }
50579 +
50580 + /* Check if Resume OK */
50581 + if (!lpmcfg.b.sleep_state_resumeok) {
50582 + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
50583 + return;
50584 + }
50585 +
50586 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
50587 + lpmcfg.b.en_utmi_sleep = 0;
50588 + lpmcfg.b.hird_thres &= (~(1 << 4));
50589 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
50590 +
50591 + if (set) {
50592 + dctl_data_t dctl = {.d32 = 0 };
50593 + dctl.b.rmtwkupsig = 1;
50594 + /* Set RmtWkUpSig bit to start remote wakup signaling.
50595 + * Hardware will automatically clear this bit.
50596 + */
50597 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
50598 + 0, dctl.d32);
50599 + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
50600 + }
50601 +
50602 +}
50603 +#endif
50604 +
50605 +/**
50606 + * Performs remote wakeup.
50607 + */
50608 +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
50609 +{
50610 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50611 + dwc_irqflags_t flags;
50612 + if (dwc_otg_is_device_mode(core_if)) {
50613 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50614 +#ifdef CONFIG_USB_DWC_OTG_LPM
50615 + if (core_if->lx_state == DWC_OTG_L1) {
50616 + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
50617 + } else {
50618 +#endif
50619 + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
50620 +#ifdef CONFIG_USB_DWC_OTG_LPM
50621 + }
50622 +#endif
50623 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50624 + }
50625 + return;
50626 +}
50627 +
50628 +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
50629 +{
50630 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50631 + dctl_data_t dctl = { 0 };
50632 +
50633 + if (dwc_otg_is_device_mode(core_if)) {
50634 + dctl.b.sftdiscon = 1;
50635 + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
50636 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
50637 + dwc_udelay(no_of_usecs);
50638 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
50639 +
50640 + } else{
50641 + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
50642 + }
50643 + return;
50644 +
50645 +}
50646 +
50647 +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
50648 +{
50649 + dsts_data_t dsts;
50650 + gotgctl_data_t gotgctl;
50651 +
50652 + /*
50653 + * This function starts the Protocol if no session is in progress. If
50654 + * a session is already in progress, but the device is suspended,
50655 + * remote wakeup signaling is started.
50656 + */
50657 +
50658 + /* Check if valid session */
50659 + gotgctl.d32 =
50660 + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
50661 + if (gotgctl.b.bsesvld) {
50662 + /* Check if suspend state */
50663 + dsts.d32 =
50664 + DWC_READ_REG32(&
50665 + (GET_CORE_IF(pcd)->dev_if->
50666 + dev_global_regs->dsts));
50667 + if (dsts.b.suspsts) {
50668 + dwc_otg_pcd_remote_wakeup(pcd, 1);
50669 + }
50670 + } else {
50671 + dwc_otg_pcd_initiate_srp(pcd);
50672 + }
50673 +
50674 + return 0;
50675 +
50676 +}
50677 +
50678 +/**
50679 + * Start the SRP timer to detect when the SRP does not complete within
50680 + * 6 seconds.
50681 + *
50682 + * @param pcd the pcd structure.
50683 + */
50684 +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
50685 +{
50686 + dwc_irqflags_t flags;
50687 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50688 + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
50689 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50690 +}
50691 +
50692 +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
50693 +{
50694 + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
50695 +}
50696 +
50697 +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
50698 +{
50699 + return GET_CORE_IF(pcd)->core_params->lpm_enable;
50700 +}
50701 +
50702 +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
50703 +{
50704 + return pcd->b_hnp_enable;
50705 +}
50706 +
50707 +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
50708 +{
50709 + return pcd->a_hnp_support;
50710 +}
50711 +
50712 +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
50713 +{
50714 + return pcd->a_alt_hnp_support;
50715 +}
50716 +
50717 +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
50718 +{
50719 + return pcd->remote_wakeup_enable;
50720 +}
50721 +
50722 +#endif /* DWC_HOST_ONLY */
50723 --- /dev/null
50724 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
50725 @@ -0,0 +1,273 @@
50726 +/* ==========================================================================
50727 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
50728 + * $Revision: #48 $
50729 + * $Date: 2012/08/10 $
50730 + * $Change: 2047372 $
50731 + *
50732 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
50733 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
50734 + * otherwise expressly agreed to in writing between Synopsys and you.
50735 + *
50736 + * The Software IS NOT an item of Licensed Software or Licensed Product under
50737 + * any End User Software License Agreement or Agreement for Licensed Product
50738 + * with Synopsys or any supplement thereto. You are permitted to use and
50739 + * redistribute this Software in source and binary forms, with or without
50740 + * modification, provided that redistributions of source code must retain this
50741 + * notice. You may not view, use, disclose, copy or distribute this file or
50742 + * any information contained herein except pursuant to this license grant from
50743 + * Synopsys. If you do not agree with this notice, including the disclaimer
50744 + * below, then you are not authorized to use the Software.
50745 + *
50746 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
50747 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50748 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50749 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
50750 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50751 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50752 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50753 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50754 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50755 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
50756 + * DAMAGE.
50757 + * ========================================================================== */
50758 +#ifndef DWC_HOST_ONLY
50759 +#if !defined(__DWC_PCD_H__)
50760 +#define __DWC_PCD_H__
50761 +
50762 +#include "dwc_otg_os_dep.h"
50763 +#include "usb.h"
50764 +#include "dwc_otg_cil.h"
50765 +#include "dwc_otg_pcd_if.h"
50766 +#include "dwc_otg_driver.h"
50767 +
50768 +struct cfiobject;
50769 +
50770 +/**
50771 + * @file
50772 + *
50773 + * This file contains the structures, constants, and interfaces for
50774 + * the Perpherial Contoller Driver (PCD).
50775 + *
50776 + * The Peripheral Controller Driver (PCD) for Linux will implement the
50777 + * Gadget API, so that the existing Gadget drivers can be used. For
50778 + * the Mass Storage Function driver the File-backed USB Storage Gadget
50779 + * (FBS) driver will be used. The FBS driver supports the
50780 + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
50781 + * transports.
50782 + *
50783 + */
50784 +
50785 +/** Invalid DMA Address */
50786 +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
50787 +
50788 +/** Max Transfer size for any EP */
50789 +#define DDMA_MAX_TRANSFER_SIZE 65535
50790 +
50791 +/**
50792 + * Get the pointer to the core_if from the pcd pointer.
50793 + */
50794 +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
50795 +
50796 +/**
50797 + * States of EP0.
50798 + */
50799 +typedef enum ep0_state {
50800 + EP0_DISCONNECT, /* no host */
50801 + EP0_IDLE,
50802 + EP0_IN_DATA_PHASE,
50803 + EP0_OUT_DATA_PHASE,
50804 + EP0_IN_STATUS_PHASE,
50805 + EP0_OUT_STATUS_PHASE,
50806 + EP0_STALL,
50807 +} ep0state_e;
50808 +
50809 +/** Fordward declaration.*/
50810 +struct dwc_otg_pcd;
50811 +
50812 +/** DWC_otg iso request structure.
50813 + *
50814 + */
50815 +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
50816 +
50817 +#ifdef DWC_UTE_PER_IO
50818 +
50819 +/**
50820 + * This shall be the exact analogy of the same type structure defined in the
50821 + * usb_gadget.h. Each descriptor contains
50822 + */
50823 +struct dwc_iso_pkt_desc_port {
50824 + uint32_t offset;
50825 + uint32_t length; /* expected length */
50826 + uint32_t actual_length;
50827 + uint32_t status;
50828 +};
50829 +
50830 +struct dwc_iso_xreq_port {
50831 + /** transfer/submission flag */
50832 + uint32_t tr_sub_flags;
50833 + /** Start the request ASAP */
50834 +#define DWC_EREQ_TF_ASAP 0x00000002
50835 + /** Just enqueue the request w/o initiating a transfer */
50836 +#define DWC_EREQ_TF_ENQUEUE 0x00000004
50837 +
50838 + /**
50839 + * count of ISO packets attached to this request - shall
50840 + * not exceed the pio_alloc_pkt_count
50841 + */
50842 + uint32_t pio_pkt_count;
50843 + /** count of ISO packets allocated for this request */
50844 + uint32_t pio_alloc_pkt_count;
50845 + /** number of ISO packet errors */
50846 + uint32_t error_count;
50847 + /** reserved for future extension */
50848 + uint32_t res;
50849 + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
50850 + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
50851 +};
50852 +#endif
50853 +/** DWC_otg request structure.
50854 + * This structure is a list of requests.
50855 + */
50856 +typedef struct dwc_otg_pcd_request {
50857 + void *priv;
50858 + void *buf;
50859 + dwc_dma_t dma;
50860 + uint32_t length;
50861 + uint32_t actual;
50862 + unsigned sent_zlp:1;
50863 + /**
50864 + * Used instead of original buffer if
50865 + * it(physical address) is not dword-aligned.
50866 + **/
50867 + uint8_t *dw_align_buf;
50868 + dwc_dma_t dw_align_buf_dma;
50869 +
50870 + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
50871 +#ifdef DWC_UTE_PER_IO
50872 + struct dwc_iso_xreq_port ext_req;
50873 + //void *priv_ereq_nport; /* */
50874 +#endif
50875 +} dwc_otg_pcd_request_t;
50876 +
50877 +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
50878 +
50879 +/** PCD EP structure.
50880 + * This structure describes an EP, there is an array of EPs in the PCD
50881 + * structure.
50882 + */
50883 +typedef struct dwc_otg_pcd_ep {
50884 + /** USB EP Descriptor */
50885 + const usb_endpoint_descriptor_t *desc;
50886 +
50887 + /** queue of dwc_otg_pcd_requests. */
50888 + struct req_list queue;
50889 + unsigned stopped:1;
50890 + unsigned disabling:1;
50891 + unsigned dma:1;
50892 + unsigned queue_sof:1;
50893 +
50894 +#ifdef DWC_EN_ISOC
50895 + /** ISOC req handle passed */
50896 + void *iso_req_handle;
50897 +#endif //_EN_ISOC_
50898 +
50899 + /** DWC_otg ep data. */
50900 + dwc_ep_t dwc_ep;
50901 +
50902 + /** Pointer to PCD */
50903 + struct dwc_otg_pcd *pcd;
50904 +
50905 + void *priv;
50906 +} dwc_otg_pcd_ep_t;
50907 +
50908 +/** DWC_otg PCD Structure.
50909 + * This structure encapsulates the data for the dwc_otg PCD.
50910 + */
50911 +struct dwc_otg_pcd {
50912 + const struct dwc_otg_pcd_function_ops *fops;
50913 + /** The DWC otg device pointer */
50914 + struct dwc_otg_device *otg_dev;
50915 + /** Core Interface */
50916 + dwc_otg_core_if_t *core_if;
50917 + /** State of EP0 */
50918 + ep0state_e ep0state;
50919 + /** EP0 Request is pending */
50920 + unsigned ep0_pending:1;
50921 + /** Indicates when SET CONFIGURATION Request is in process */
50922 + unsigned request_config:1;
50923 + /** The state of the Remote Wakeup Enable. */
50924 + unsigned remote_wakeup_enable:1;
50925 + /** The state of the B-Device HNP Enable. */
50926 + unsigned b_hnp_enable:1;
50927 + /** The state of A-Device HNP Support. */
50928 + unsigned a_hnp_support:1;
50929 + /** The state of the A-Device Alt HNP support. */
50930 + unsigned a_alt_hnp_support:1;
50931 + /** Count of pending Requests */
50932 + unsigned request_pending;
50933 +
50934 + /** SETUP packet for EP0
50935 + * This structure is allocated as a DMA buffer on PCD initialization
50936 + * with enough space for up to 3 setup packets.
50937 + */
50938 + union {
50939 + usb_device_request_t req;
50940 + uint32_t d32[2];
50941 + } *setup_pkt;
50942 +
50943 + dwc_dma_t setup_pkt_dma_handle;
50944 +
50945 + /* Additional buffer and flag for CTRL_WR premature case */
50946 + uint8_t *backup_buf;
50947 + unsigned data_terminated;
50948 +
50949 + /** 2-byte dma buffer used to return status from GET_STATUS */
50950 + uint16_t *status_buf;
50951 + dwc_dma_t status_buf_dma_handle;
50952 +
50953 + /** EP0 */
50954 + dwc_otg_pcd_ep_t ep0;
50955 +
50956 + /** Array of IN EPs. */
50957 + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
50958 + /** Array of OUT EPs. */
50959 + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
50960 + /** number of valid EPs in the above array. */
50961 +// unsigned num_eps : 4;
50962 + dwc_spinlock_t *lock;
50963 +
50964 + /** Tasklet to defer starting of TEST mode transmissions until
50965 + * Status Phase has been completed.
50966 + */
50967 + dwc_tasklet_t *test_mode_tasklet;
50968 +
50969 + /** Tasklet to delay starting of xfer in DMA mode */
50970 + dwc_tasklet_t *start_xfer_tasklet;
50971 +
50972 + /** The test mode to enter when the tasklet is executed. */
50973 + unsigned test_mode;
50974 + /** The cfi_api structure that implements most of the CFI API
50975 + * and OTG specific core configuration functionality
50976 + */
50977 +#ifdef DWC_UTE_CFI
50978 + struct cfiobject *cfi;
50979 +#endif
50980 +
50981 +};
50982 +
50983 +static inline struct device *dwc_otg_pcd_to_dev(struct dwc_otg_pcd *pcd)
50984 +{
50985 + return &pcd->otg_dev->os_dep.platformdev->dev;
50986 +}
50987 +
50988 +//FIXME this functions should be static, and this prototypes should be removed
50989 +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
50990 +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
50991 + dwc_otg_pcd_request_t * req, int32_t status);
50992 +
50993 +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
50994 + void *req_handle);
50995 +
50996 +extern void do_test_mode(void *data);
50997 +#endif
50998 +#endif /* DWC_HOST_ONLY */
50999 --- /dev/null
51000 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
51001 @@ -0,0 +1,361 @@
51002 +/* ==========================================================================
51003 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
51004 + * $Revision: #11 $
51005 + * $Date: 2011/10/26 $
51006 + * $Change: 1873028 $
51007 + *
51008 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
51009 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
51010 + * otherwise expressly agreed to in writing between Synopsys and you.
51011 + *
51012 + * The Software IS NOT an item of Licensed Software or Licensed Product under
51013 + * any End User Software License Agreement or Agreement for Licensed Product
51014 + * with Synopsys or any supplement thereto. You are permitted to use and
51015 + * redistribute this Software in source and binary forms, with or without
51016 + * modification, provided that redistributions of source code must retain this
51017 + * notice. You may not view, use, disclose, copy or distribute this file or
51018 + * any information contained herein except pursuant to this license grant from
51019 + * Synopsys. If you do not agree with this notice, including the disclaimer
51020 + * below, then you are not authorized to use the Software.
51021 + *
51022 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
51023 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51024 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51025 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
51026 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51027 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51028 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51029 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51030 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51031 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51032 + * DAMAGE.
51033 + * ========================================================================== */
51034 +#ifndef DWC_HOST_ONLY
51035 +
51036 +#if !defined(__DWC_PCD_IF_H__)
51037 +#define __DWC_PCD_IF_H__
51038 +
51039 +//#include "dwc_os.h"
51040 +#include "dwc_otg_core_if.h"
51041 +#include "dwc_otg_driver.h"
51042 +
51043 +/** @file
51044 + * This file defines DWC_OTG PCD Core API.
51045 + */
51046 +
51047 +struct dwc_otg_pcd;
51048 +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
51049 +
51050 +/** Maxpacket size for EP0 */
51051 +#define MAX_EP0_SIZE 64
51052 +/** Maxpacket size for any EP */
51053 +#define MAX_PACKET_SIZE 1024
51054 +
51055 +/** @name Function Driver Callbacks */
51056 +/** @{ */
51057 +
51058 +/** This function will be called whenever a previously queued request has
51059 + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
51060 + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
51061 + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
51062 + * parameters. */
51063 +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51064 + void *req_handle, int32_t status,
51065 + uint32_t actual);
51066 +/**
51067 + * This function will be called whenever a previousle queued ISOC request has
51068 + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
51069 + * function.
51070 + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
51071 + * functions.
51072 + */
51073 +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51074 + void *req_handle, int proc_buf_num);
51075 +/** This function should handle any SETUP request that cannot be handled by the
51076 + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
51077 + * class-specific requests, etc. The function must non-blocking.
51078 + *
51079 + * Returns 0 on success.
51080 + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
51081 + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
51082 + * Returns -DWC_E_SHUTDOWN on any other error. */
51083 +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
51084 +/** This is called whenever the device has been disconnected. The function
51085 + * driver should take appropriate action to clean up all pending requests in the
51086 + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
51087 + * state. */
51088 +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
51089 +/** This function is called when device has been connected. */
51090 +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
51091 +/** This function is called when device has been suspended */
51092 +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
51093 +/** This function is called when device has received LPM tokens, i.e.
51094 + * device has been sent to sleep state. */
51095 +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
51096 +/** This function is called when device has been resumed
51097 + * from suspend(L2) or L1 sleep state. */
51098 +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
51099 +/** This function is called whenever hnp params has been changed.
51100 + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
51101 + * to get hnp parameters. */
51102 +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
51103 +/** This function is called whenever USB RESET is detected. */
51104 +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
51105 +
51106 +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
51107 +
51108 +/**
51109 + *
51110 + * @param ep_handle Void pointer to the usb_ep structure
51111 + * @param ereq_port Pointer to the extended request structure created in the
51112 + * portable part.
51113 + */
51114 +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51115 + void *req_handle, int32_t status,
51116 + void *ereq_port);
51117 +/** Function Driver Ops Data Structure */
51118 +struct dwc_otg_pcd_function_ops {
51119 + dwc_connect_cb_t connect;
51120 + dwc_disconnect_cb_t disconnect;
51121 + dwc_setup_cb_t setup;
51122 + dwc_completion_cb_t complete;
51123 + dwc_isoc_completion_cb_t isoc_complete;
51124 + dwc_suspend_cb_t suspend;
51125 + dwc_sleep_cb_t sleep;
51126 + dwc_resume_cb_t resume;
51127 + dwc_reset_cb_t reset;
51128 + dwc_hnp_params_changed_cb_t hnp_changed;
51129 + cfi_setup_cb_t cfi_setup;
51130 +#ifdef DWC_UTE_PER_IO
51131 + xiso_completion_cb_t xisoc_complete;
51132 +#endif
51133 +};
51134 +/** @} */
51135 +
51136 +/** @name Function Driver Functions */
51137 +/** @{ */
51138 +
51139 +/** Call this function to get pointer on dwc_otg_pcd_t,
51140 + * this pointer will be used for all PCD API functions.
51141 + *
51142 + * @param core_if The DWC_OTG Core
51143 + */
51144 +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev);
51145 +
51146 +/** Frees PCD allocated by dwc_otg_pcd_init
51147 + *
51148 + * @param pcd The PCD
51149 + */
51150 +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
51151 +
51152 +/** Call this to bind the function driver to the PCD Core.
51153 + *
51154 + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
51155 + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
51156 + */
51157 +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
51158 + const struct dwc_otg_pcd_function_ops *fops);
51159 +
51160 +/** Enables an endpoint for use. This function enables an endpoint in
51161 + * the PCD. The endpoint is described by the ep_desc which has the
51162 + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
51163 + * to the endpoint from other API functions and in callbacks. Normally this
51164 + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
51165 + * core for that interface.
51166 + *
51167 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51168 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51169 + * Returns 0 on success.
51170 + *
51171 + * @param pcd The PCD
51172 + * @param ep_desc Endpoint descriptor
51173 + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
51174 + */
51175 +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
51176 + const uint8_t * ep_desc, void *usb_ep);
51177 +
51178 +/** Disable the endpoint referenced by ep_handle.
51179 + *
51180 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51181 + * Returns -DWC_E_SHUTDOWN if any other error occurred.
51182 + * Returns 0 on success. */
51183 +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
51184 +
51185 +/** Queue a data transfer request on the endpoint referenced by ep_handle.
51186 + * After the transfer is completes, the complete callback will be called with
51187 + * the request status.
51188 + *
51189 + * @param pcd The PCD
51190 + * @param ep_handle The handle of the endpoint
51191 + * @param buf The buffer for the data
51192 + * @param dma_buf The DMA buffer for the data
51193 + * @param buflen The length of the data transfer
51194 + * @param zero Specifies whether to send zero length last packet.
51195 + * @param req_handle Set this handle to any value to use to reference this
51196 + * request in the ep_dequeue function or from the complete callback
51197 + * @param atomic_alloc If driver need to perform atomic allocations
51198 + * for internal data structures.
51199 + *
51200 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51201 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51202 + * Returns 0 on success. */
51203 +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
51204 + uint8_t * buf, dwc_dma_t dma_buf,
51205 + uint32_t buflen, int zero, void *req_handle,
51206 + int atomic_alloc);
51207 +#ifdef DWC_UTE_PER_IO
51208 +/**
51209 + *
51210 + * @param ereq_nonport Pointer to the extended request part of the
51211 + * usb_request structure defined in usb_gadget.h file.
51212 + */
51213 +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
51214 + uint8_t * buf, dwc_dma_t dma_buf,
51215 + uint32_t buflen, int zero,
51216 + void *req_handle, int atomic_alloc,
51217 + void *ereq_nonport);
51218 +
51219 +#endif
51220 +
51221 +/** De-queue the specified data transfer that has not yet completed.
51222 + *
51223 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51224 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51225 + * Returns 0 on success. */
51226 +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
51227 + void *req_handle);
51228 +
51229 +/** Halt (STALL) an endpoint or clear it.
51230 + *
51231 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51232 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51233 + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
51234 + * Returns 0 on success. */
51235 +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
51236 +
51237 +/** This function */
51238 +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
51239 +
51240 +/** This function should be called on every hardware interrupt */
51241 +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
51242 +
51243 +/** This function returns current frame number */
51244 +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
51245 +
51246 +/**
51247 + * Start isochronous transfers on the endpoint referenced by ep_handle.
51248 + * For isochronous transfers duble buffering is used.
51249 + * After processing each of buffers comlete callback will be called with
51250 + * status for each transaction.
51251 + *
51252 + * @param pcd The PCD
51253 + * @param ep_handle The handle of the endpoint
51254 + * @param buf0 The virtual address of first data buffer
51255 + * @param buf1 The virtual address of second data buffer
51256 + * @param dma0 The DMA address of first data buffer
51257 + * @param dma1 The DMA address of second data buffer
51258 + * @param sync_frame Data pattern frame number
51259 + * @param dp_frame Data size for pattern frame
51260 + * @param data_per_frame Data size for regular frame
51261 + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
51262 + * @param buf_proc_intrvl Interval of ISOC Buffer processing
51263 + * @param req_handle Handle of ISOC request
51264 + * @param atomic_alloc Specefies whether to perform atomic allocation for
51265 + * internal data structures.
51266 + *
51267 + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
51268 + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
51269 + * Returns -DW_E_SHUTDOWN for any other error.
51270 + * Returns 0 on success
51271 + */
51272 +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
51273 + uint8_t * buf0, uint8_t * buf1,
51274 + dwc_dma_t dma0, dwc_dma_t dma1,
51275 + int sync_frame, int dp_frame,
51276 + int data_per_frame, int start_frame,
51277 + int buf_proc_intrvl, void *req_handle,
51278 + int atomic_alloc);
51279 +
51280 +/** Stop ISOC transfers on endpoint referenced by ep_handle.
51281 + *
51282 + * @param pcd The PCD
51283 + * @param ep_handle The handle of the endpoint
51284 + * @param req_handle Handle of ISOC request
51285 + *
51286 + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
51287 + * Returns 0 on success
51288 + */
51289 +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
51290 + void *req_handle);
51291 +
51292 +/** Get ISOC packet status.
51293 + *
51294 + * @param pcd The PCD
51295 + * @param ep_handle The handle of the endpoint
51296 + * @param iso_req_handle Isochronoush request handle
51297 + * @param packet Number of packet
51298 + * @param status Out parameter for returning status
51299 + * @param actual Out parameter for returning actual length
51300 + * @param offset Out parameter for returning offset
51301 + *
51302 + */
51303 +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
51304 + void *ep_handle,
51305 + void *iso_req_handle, int packet,
51306 + int *status, int *actual,
51307 + int *offset);
51308 +
51309 +/** Get ISOC packet count.
51310 + *
51311 + * @param pcd The PCD
51312 + * @param ep_handle The handle of the endpoint
51313 + * @param iso_req_handle
51314 + */
51315 +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
51316 + void *ep_handle,
51317 + void *iso_req_handle);
51318 +
51319 +/** This function starts the SRP Protocol if no session is in progress. If
51320 + * a session is already in progress, but the device is suspended,
51321 + * remote wakeup signaling is started.
51322 + */
51323 +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
51324 +
51325 +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
51326 +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
51327 +
51328 +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
51329 +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
51330 +
51331 +/** Initiate SRP */
51332 +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
51333 +
51334 +/** Starts remote wakeup signaling. */
51335 +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
51336 +
51337 +/** Starts micorsecond soft disconnect. */
51338 +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
51339 +/** This function returns whether device is dualspeed.*/
51340 +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
51341 +
51342 +/** This function returns whether device is otg. */
51343 +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
51344 +
51345 +/** These functions allow to get hnp parameters */
51346 +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
51347 +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
51348 +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
51349 +
51350 +/** CFI specific Interface functions */
51351 +/** Allocate a cfi buffer */
51352 +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
51353 + dwc_dma_t * addr, size_t buflen,
51354 + int flags);
51355 +
51356 +/******************************************************************************/
51357 +
51358 +/** @} */
51359 +
51360 +#endif /* __DWC_PCD_IF_H__ */
51361 +
51362 +#endif /* DWC_HOST_ONLY */
51363 --- /dev/null
51364 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
51365 @@ -0,0 +1,5148 @@
51366 +/* ==========================================================================
51367 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
51368 + * $Revision: #116 $
51369 + * $Date: 2012/08/10 $
51370 + * $Change: 2047372 $
51371 + *
51372 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
51373 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
51374 + * otherwise expressly agreed to in writing between Synopsys and you.
51375 + *
51376 + * The Software IS NOT an item of Licensed Software or Licensed Product under
51377 + * any End User Software License Agreement or Agreement for Licensed Product
51378 + * with Synopsys or any supplement thereto. You are permitted to use and
51379 + * redistribute this Software in source and binary forms, with or without
51380 + * modification, provided that redistributions of source code must retain this
51381 + * notice. You may not view, use, disclose, copy or distribute this file or
51382 + * any information contained herein except pursuant to this license grant from
51383 + * Synopsys. If you do not agree with this notice, including the disclaimer
51384 + * below, then you are not authorized to use the Software.
51385 + *
51386 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
51387 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51388 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51389 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
51390 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51391 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51392 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51393 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51394 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51395 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51396 + * DAMAGE.
51397 + * ========================================================================== */
51398 +#ifndef DWC_HOST_ONLY
51399 +
51400 +#include "dwc_otg_pcd.h"
51401 +
51402 +#ifdef DWC_UTE_CFI
51403 +#include "dwc_otg_cfi.h"
51404 +#endif
51405 +
51406 +#ifdef DWC_UTE_PER_IO
51407 +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
51408 +#endif
51409 +//#define PRINT_CFI_DMA_DESCS
51410 +
51411 +#define DEBUG_EP0
51412 +
51413 +/**
51414 + * This function updates OTG.
51415 + */
51416 +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
51417 +{
51418 +
51419 + if (reset) {
51420 + pcd->b_hnp_enable = 0;
51421 + pcd->a_hnp_support = 0;
51422 + pcd->a_alt_hnp_support = 0;
51423 + }
51424 +
51425 + if (pcd->fops->hnp_changed) {
51426 + pcd->fops->hnp_changed(pcd);
51427 + }
51428 +}
51429 +
51430 +/** @file
51431 + * This file contains the implementation of the PCD Interrupt handlers.
51432 + *
51433 + * The PCD handles the device interrupts. Many conditions can cause a
51434 + * device interrupt. When an interrupt occurs, the device interrupt
51435 + * service routine determines the cause of the interrupt and
51436 + * dispatches handling to the appropriate function. These interrupt
51437 + * handling functions are described below.
51438 + * All interrupt registers are processed from LSB to MSB.
51439 + */
51440 +
51441 +/**
51442 + * This function prints the ep0 state for debug purposes.
51443 + */
51444 +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
51445 +{
51446 +#ifdef DEBUG
51447 + char str[40];
51448 +
51449 + switch (pcd->ep0state) {
51450 + case EP0_DISCONNECT:
51451 + dwc_strcpy(str, "EP0_DISCONNECT");
51452 + break;
51453 + case EP0_IDLE:
51454 + dwc_strcpy(str, "EP0_IDLE");
51455 + break;
51456 + case EP0_IN_DATA_PHASE:
51457 + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
51458 + break;
51459 + case EP0_OUT_DATA_PHASE:
51460 + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
51461 + break;
51462 + case EP0_IN_STATUS_PHASE:
51463 + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
51464 + break;
51465 + case EP0_OUT_STATUS_PHASE:
51466 + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
51467 + break;
51468 + case EP0_STALL:
51469 + dwc_strcpy(str, "EP0_STALL");
51470 + break;
51471 + default:
51472 + dwc_strcpy(str, "EP0_INVALID");
51473 + }
51474 +
51475 + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
51476 +#endif
51477 +}
51478 +
51479 +/**
51480 + * This function calculate the size of the payload in the memory
51481 + * for out endpoints and prints size for debug purposes(used in
51482 + * 2.93a DevOutNak feature).
51483 + */
51484 +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
51485 +{
51486 +#ifdef DEBUG
51487 + deptsiz_data_t deptsiz_init = {.d32 = 0 };
51488 + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
51489 + int pack_num;
51490 + unsigned payload;
51491 +
51492 + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
51493 + deptsiz_updt.d32 =
51494 + DWC_READ_REG32(&pcd->core_if->dev_if->
51495 + out_ep_regs[ep->num]->doeptsiz);
51496 + /* Payload will be */
51497 + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
51498 + /* Packet count is decremented every time a packet
51499 + * is written to the RxFIFO not in to the external memory
51500 + * So, if payload == 0, then it means no packet was sent to ext memory*/
51501 + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
51502 + DWC_DEBUGPL(DBG_PCDV,
51503 + "Payload for EP%d-%s\n",
51504 + ep->num, (ep->is_in ? "IN" : "OUT"));
51505 + DWC_DEBUGPL(DBG_PCDV,
51506 + "Number of transfered bytes = 0x%08x\n", payload);
51507 + DWC_DEBUGPL(DBG_PCDV,
51508 + "Number of transfered packets = %d\n", pack_num);
51509 +#endif
51510 +}
51511 +
51512 +
51513 +#ifdef DWC_UTE_CFI
51514 +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
51515 + const uint8_t * epname, int descnum)
51516 +{
51517 + CFI_INFO
51518 + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
51519 + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
51520 + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
51521 + ddesc->status.b.bs);
51522 +}
51523 +#endif
51524 +
51525 +/**
51526 + * This function returns pointer to in ep struct with number ep_num
51527 + */
51528 +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
51529 +{
51530 + int i;
51531 + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
51532 + if (ep_num == 0) {
51533 + return &pcd->ep0;
51534 + } else {
51535 + for (i = 0; i < num_in_eps; ++i) {
51536 + if (pcd->in_ep[i].dwc_ep.num == ep_num)
51537 + return &pcd->in_ep[i];
51538 + }
51539 + return 0;
51540 + }
51541 +}
51542 +
51543 +/**
51544 + * This function returns pointer to out ep struct with number ep_num
51545 + */
51546 +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
51547 +{
51548 + int i;
51549 + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
51550 + if (ep_num == 0) {
51551 + return &pcd->ep0;
51552 + } else {
51553 + for (i = 0; i < num_out_eps; ++i) {
51554 + if (pcd->out_ep[i].dwc_ep.num == ep_num)
51555 + return &pcd->out_ep[i];
51556 + }
51557 + return 0;
51558 + }
51559 +}
51560 +
51561 +/**
51562 + * This functions gets a pointer to an EP from the wIndex address
51563 + * value of the control request.
51564 + */
51565 +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
51566 +{
51567 + dwc_otg_pcd_ep_t *ep;
51568 + uint32_t ep_num = UE_GET_ADDR(wIndex);
51569 +
51570 + if (ep_num == 0) {
51571 + ep = &pcd->ep0;
51572 + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
51573 + ep = &pcd->in_ep[ep_num - 1];
51574 + } else {
51575 + ep = &pcd->out_ep[ep_num - 1];
51576 + }
51577 +
51578 + return ep;
51579 +}
51580 +
51581 +/**
51582 + * This function checks the EP request queue, if the queue is not
51583 + * empty the next request is started.
51584 + */
51585 +void start_next_request(dwc_otg_pcd_ep_t * ep)
51586 +{
51587 + dwc_otg_pcd_request_t *req = 0;
51588 + uint32_t max_transfer =
51589 + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
51590 +
51591 +#ifdef DWC_UTE_CFI
51592 + struct dwc_otg_pcd *pcd;
51593 + pcd = ep->pcd;
51594 +#endif
51595 +
51596 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
51597 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
51598 +
51599 +#ifdef DWC_UTE_CFI
51600 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
51601 + ep->dwc_ep.cfi_req_len = req->length;
51602 + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
51603 + } else {
51604 +#endif
51605 + /* Setup and start the Transfer */
51606 + if (req->dw_align_buf) {
51607 + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
51608 + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
51609 + ep->dwc_ep.xfer_buff = req->dw_align_buf;
51610 + } else {
51611 + ep->dwc_ep.dma_addr = req->dma;
51612 + ep->dwc_ep.start_xfer_buff = req->buf;
51613 + ep->dwc_ep.xfer_buff = req->buf;
51614 + }
51615 + ep->dwc_ep.sent_zlp = 0;
51616 + ep->dwc_ep.total_len = req->length;
51617 + ep->dwc_ep.xfer_len = 0;
51618 + ep->dwc_ep.xfer_count = 0;
51619 +
51620 + ep->dwc_ep.maxxfer = max_transfer;
51621 + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
51622 + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
51623 + - (DDMA_MAX_TRANSFER_SIZE % 4);
51624 + if (ep->dwc_ep.is_in) {
51625 + if (ep->dwc_ep.maxxfer >
51626 + DDMA_MAX_TRANSFER_SIZE) {
51627 + ep->dwc_ep.maxxfer =
51628 + DDMA_MAX_TRANSFER_SIZE;
51629 + }
51630 + } else {
51631 + if (ep->dwc_ep.maxxfer > out_max_xfer) {
51632 + ep->dwc_ep.maxxfer =
51633 + out_max_xfer;
51634 + }
51635 + }
51636 + }
51637 + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
51638 + ep->dwc_ep.maxxfer -=
51639 + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
51640 + }
51641 + if (req->sent_zlp) {
51642 + if ((ep->dwc_ep.total_len %
51643 + ep->dwc_ep.maxpacket == 0)
51644 + && (ep->dwc_ep.total_len != 0)) {
51645 + ep->dwc_ep.sent_zlp = 1;
51646 + }
51647 +
51648 + }
51649 +#ifdef DWC_UTE_CFI
51650 + }
51651 +#endif
51652 + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
51653 + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
51654 + DWC_PRINTF("There are no more ISOC requests \n");
51655 + ep->dwc_ep.frame_num = 0xFFFFFFFF;
51656 + }
51657 +}
51658 +
51659 +/**
51660 + * This function handles the SOF Interrupts. At this time the SOF
51661 + * Interrupt is disabled.
51662 + */
51663 +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
51664 +{
51665 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51666 +
51667 + gintsts_data_t gintsts;
51668 +
51669 + DWC_DEBUGPL(DBG_PCD, "SOF\n");
51670 +
51671 + /* Clear interrupt */
51672 + gintsts.d32 = 0;
51673 + gintsts.b.sofintr = 1;
51674 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
51675 +
51676 + return 1;
51677 +}
51678 +
51679 +/**
51680 + * This function handles the Rx Status Queue Level Interrupt, which
51681 + * indicates that there is a least one packet in the Rx FIFO. The
51682 + * packets are moved from the FIFO to memory, where they will be
51683 + * processed when the Endpoint Interrupt Register indicates Transfer
51684 + * Complete or SETUP Phase Done.
51685 + *
51686 + * Repeat the following until the Rx Status Queue is empty:
51687 + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
51688 + * info
51689 + * -# If Receive FIFO is empty then skip to step Clear the interrupt
51690 + * and exit
51691 + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
51692 + * SETUP data to the buffer
51693 + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
51694 + * to the destination buffer
51695 + */
51696 +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
51697 +{
51698 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51699 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
51700 + gintmsk_data_t gintmask = {.d32 = 0 };
51701 + device_grxsts_data_t status;
51702 + dwc_otg_pcd_ep_t *ep;
51703 + gintsts_data_t gintsts;
51704 +#ifdef DEBUG
51705 + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
51706 +#endif
51707 +
51708 + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
51709 + /* Disable the Rx Status Queue Level interrupt */
51710 + gintmask.b.rxstsqlvl = 1;
51711 + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
51712 +
51713 + /* Get the Status from the top of the FIFO */
51714 + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
51715 +
51716 + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
51717 + "pktsts:%x Frame:%d(0x%0x)\n",
51718 + status.b.epnum, status.b.bcnt,
51719 + dpid_str[status.b.dpid],
51720 + status.b.pktsts, status.b.fn, status.b.fn);
51721 + /* Get pointer to EP structure */
51722 + ep = get_out_ep(pcd, status.b.epnum);
51723 +
51724 + switch (status.b.pktsts) {
51725 + case DWC_DSTS_GOUT_NAK:
51726 + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
51727 + break;
51728 + case DWC_STS_DATA_UPDT:
51729 + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
51730 + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
51731 + /** @todo NGS Check for buffer overflow? */
51732 + dwc_otg_read_packet(core_if,
51733 + ep->dwc_ep.xfer_buff,
51734 + status.b.bcnt);
51735 + ep->dwc_ep.xfer_count += status.b.bcnt;
51736 + ep->dwc_ep.xfer_buff += status.b.bcnt;
51737 + }
51738 + break;
51739 + case DWC_STS_XFER_COMP:
51740 + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
51741 + break;
51742 + case DWC_DSTS_SETUP_COMP:
51743 +#ifdef DEBUG_EP0
51744 + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
51745 +#endif
51746 + break;
51747 + case DWC_DSTS_SETUP_UPDT:
51748 + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
51749 +#ifdef DEBUG_EP0
51750 + DWC_DEBUGPL(DBG_PCD,
51751 + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
51752 + pcd->setup_pkt->req.bmRequestType,
51753 + pcd->setup_pkt->req.bRequest,
51754 + UGETW(pcd->setup_pkt->req.wValue),
51755 + UGETW(pcd->setup_pkt->req.wIndex),
51756 + UGETW(pcd->setup_pkt->req.wLength));
51757 +#endif
51758 + ep->dwc_ep.xfer_count += status.b.bcnt;
51759 + break;
51760 + default:
51761 + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
51762 + status.b.pktsts);
51763 + break;
51764 + }
51765 +
51766 + /* Enable the Rx Status Queue Level interrupt */
51767 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
51768 + /* Clear interrupt */
51769 + gintsts.d32 = 0;
51770 + gintsts.b.rxstsqlvl = 1;
51771 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
51772 +
51773 + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
51774 + return 1;
51775 +}
51776 +
51777 +/**
51778 + * This function examines the Device IN Token Learning Queue to
51779 + * determine the EP number of the last IN token received. This
51780 + * implementation is for the Mass Storage device where there are only
51781 + * 2 IN EPs (Control-IN and BULK-IN).
51782 + *
51783 + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
51784 + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
51785 + *
51786 + * @param core_if Programming view of DWC_otg controller.
51787 + *
51788 + */
51789 +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
51790 +{
51791 + dwc_otg_device_global_regs_t *dev_global_regs =
51792 + core_if->dev_if->dev_global_regs;
51793 + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
51794 + /* Number of Token Queue Registers */
51795 + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
51796 + dtknq1_data_t dtknqr1;
51797 + uint32_t in_tkn_epnums[4];
51798 + int ndx = 0;
51799 + int i = 0;
51800 + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
51801 + int epnum = 0;
51802 +
51803 + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
51804 +
51805 + /* Read the DTKNQ Registers */
51806 + for (i = 0; i < DTKNQ_REG_CNT; i++) {
51807 + in_tkn_epnums[i] = DWC_READ_REG32(addr);
51808 + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
51809 + in_tkn_epnums[i]);
51810 + if (addr == &dev_global_regs->dvbusdis) {
51811 + addr = &dev_global_regs->dtknqr3_dthrctl;
51812 + } else {
51813 + ++addr;
51814 + }
51815 +
51816 + }
51817 +
51818 + /* Copy the DTKNQR1 data to the bit field. */
51819 + dtknqr1.d32 = in_tkn_epnums[0];
51820 + /* Get the EP numbers */
51821 + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
51822 + ndx = dtknqr1.b.intknwptr - 1;
51823 +
51824 + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
51825 + if (ndx == -1) {
51826 + /** @todo Find a simpler way to calculate the max
51827 + * queue position.*/
51828 + int cnt = TOKEN_Q_DEPTH;
51829 + if (TOKEN_Q_DEPTH <= 6) {
51830 + cnt = TOKEN_Q_DEPTH - 1;
51831 + } else if (TOKEN_Q_DEPTH <= 14) {
51832 + cnt = TOKEN_Q_DEPTH - 7;
51833 + } else if (TOKEN_Q_DEPTH <= 22) {
51834 + cnt = TOKEN_Q_DEPTH - 15;
51835 + } else {
51836 + cnt = TOKEN_Q_DEPTH - 23;
51837 + }
51838 + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
51839 + } else {
51840 + if (ndx <= 5) {
51841 + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
51842 + } else if (ndx <= 13) {
51843 + ndx -= 6;
51844 + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
51845 + } else if (ndx <= 21) {
51846 + ndx -= 14;
51847 + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
51848 + } else if (ndx <= 29) {
51849 + ndx -= 22;
51850 + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
51851 + }
51852 + }
51853 + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
51854 + return epnum;
51855 +}
51856 +
51857 +/**
51858 + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
51859 + * The active request is checked for the next packet to be loaded into
51860 + * the non-periodic Tx FIFO.
51861 + */
51862 +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
51863 +{
51864 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51865 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
51866 + dwc_otg_dev_in_ep_regs_t *ep_regs;
51867 + gnptxsts_data_t txstatus = {.d32 = 0 };
51868 + gintsts_data_t gintsts;
51869 +
51870 + int epnum = 0;
51871 + dwc_otg_pcd_ep_t *ep = 0;
51872 + uint32_t len = 0;
51873 + int dwords;
51874 +
51875 + /* Get the epnum from the IN Token Learning Queue. */
51876 + epnum = get_ep_of_last_in_token(core_if);
51877 + ep = get_in_ep(pcd, epnum);
51878 +
51879 + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
51880 +
51881 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
51882 +
51883 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51884 + if (len > ep->dwc_ep.maxpacket) {
51885 + len = ep->dwc_ep.maxpacket;
51886 + }
51887 + dwords = (len + 3) / 4;
51888 +
51889 + /* While there is space in the queue and space in the FIFO and
51890 + * More data to tranfer, Write packets to the Tx FIFO */
51891 + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
51892 + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
51893 +
51894 + while (txstatus.b.nptxqspcavail > 0 &&
51895 + txstatus.b.nptxfspcavail > dwords &&
51896 + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
51897 + /* Write the FIFO */
51898 + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
51899 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51900 +
51901 + if (len > ep->dwc_ep.maxpacket) {
51902 + len = ep->dwc_ep.maxpacket;
51903 + }
51904 +
51905 + dwords = (len + 3) / 4;
51906 + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
51907 + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
51908 + }
51909 +
51910 + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
51911 + DWC_READ_REG32(&global_regs->gnptxsts));
51912 +
51913 + /* Clear interrupt */
51914 + gintsts.d32 = 0;
51915 + gintsts.b.nptxfempty = 1;
51916 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
51917 +
51918 + return 1;
51919 +}
51920 +
51921 +/**
51922 + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
51923 + * The active request is checked for the next packet to be loaded into
51924 + * apropriate Tx FIFO.
51925 + */
51926 +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
51927 +{
51928 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51929 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
51930 + dwc_otg_dev_in_ep_regs_t *ep_regs;
51931 + dtxfsts_data_t txstatus = {.d32 = 0 };
51932 + dwc_otg_pcd_ep_t *ep = 0;
51933 + uint32_t len = 0;
51934 + int dwords;
51935 +
51936 + ep = get_in_ep(pcd, epnum);
51937 +
51938 + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
51939 +
51940 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
51941 +
51942 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51943 +
51944 + if (len > ep->dwc_ep.maxpacket) {
51945 + len = ep->dwc_ep.maxpacket;
51946 + }
51947 +
51948 + dwords = (len + 3) / 4;
51949 +
51950 + /* While there is space in the queue and space in the FIFO and
51951 + * More data to tranfer, Write packets to the Tx FIFO */
51952 + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
51953 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
51954 +
51955 + while (txstatus.b.txfspcavail > dwords &&
51956 + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
51957 + ep->dwc_ep.xfer_len != 0) {
51958 + /* Write the FIFO */
51959 + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
51960 +
51961 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51962 + if (len > ep->dwc_ep.maxpacket) {
51963 + len = ep->dwc_ep.maxpacket;
51964 + }
51965 +
51966 + dwords = (len + 3) / 4;
51967 + txstatus.d32 =
51968 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
51969 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
51970 + txstatus.d32);
51971 + }
51972 +
51973 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
51974 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
51975 +
51976 + return 1;
51977 +}
51978 +
51979 +/**
51980 + * This function is called when the Device is disconnected. It stops
51981 + * any active requests and informs the Gadget driver of the
51982 + * disconnect.
51983 + */
51984 +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
51985 +{
51986 + int i, num_in_eps, num_out_eps;
51987 + dwc_otg_pcd_ep_t *ep;
51988 +
51989 + gintmsk_data_t intr_mask = {.d32 = 0 };
51990 +
51991 + DWC_SPINLOCK(pcd->lock);
51992 +
51993 + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
51994 + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
51995 +
51996 + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
51997 + /* don't disconnect drivers more than once */
51998 + if (pcd->ep0state == EP0_DISCONNECT) {
51999 + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
52000 + DWC_SPINUNLOCK(pcd->lock);
52001 + return;
52002 + }
52003 + pcd->ep0state = EP0_DISCONNECT;
52004 +
52005 + /* Reset the OTG state. */
52006 + dwc_otg_pcd_update_otg(pcd, 1);
52007 +
52008 + /* Disable the NP Tx Fifo Empty Interrupt. */
52009 + intr_mask.b.nptxfempty = 1;
52010 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52011 + intr_mask.d32, 0);
52012 +
52013 + /* Flush the FIFOs */
52014 + /**@todo NGS Flush Periodic FIFOs */
52015 + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
52016 + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
52017 +
52018 + /* prevent new request submissions, kill any outstanding requests */
52019 + ep = &pcd->ep0;
52020 + dwc_otg_request_nuke(ep);
52021 + /* prevent new request submissions, kill any outstanding requests */
52022 + for (i = 0; i < num_in_eps; i++) {
52023 + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
52024 + dwc_otg_request_nuke(ep);
52025 + }
52026 + /* prevent new request submissions, kill any outstanding requests */
52027 + for (i = 0; i < num_out_eps; i++) {
52028 + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
52029 + dwc_otg_request_nuke(ep);
52030 + }
52031 +
52032 + /* report disconnect; the driver is already quiesced */
52033 + if (pcd->fops->disconnect) {
52034 + DWC_SPINUNLOCK(pcd->lock);
52035 + pcd->fops->disconnect(pcd);
52036 + DWC_SPINLOCK(pcd->lock);
52037 + }
52038 + DWC_SPINUNLOCK(pcd->lock);
52039 +}
52040 +
52041 +/**
52042 + * This interrupt indicates that ...
52043 + */
52044 +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
52045 +{
52046 + gintmsk_data_t intr_mask = {.d32 = 0 };
52047 + gintsts_data_t gintsts;
52048 +
52049 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
52050 + intr_mask.b.i2cintr = 1;
52051 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52052 + intr_mask.d32, 0);
52053 +
52054 + /* Clear interrupt */
52055 + gintsts.d32 = 0;
52056 + gintsts.b.i2cintr = 1;
52057 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52058 + gintsts.d32);
52059 + return 1;
52060 +}
52061 +
52062 +/**
52063 + * This interrupt indicates that ...
52064 + */
52065 +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
52066 +{
52067 + gintsts_data_t gintsts;
52068 +#if defined(VERBOSE)
52069 + DWC_PRINTF("Early Suspend Detected\n");
52070 +#endif
52071 +
52072 + /* Clear interrupt */
52073 + gintsts.d32 = 0;
52074 + gintsts.b.erlysuspend = 1;
52075 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52076 + gintsts.d32);
52077 + return 1;
52078 +}
52079 +
52080 +/**
52081 + * This function configures EPO to receive SETUP packets.
52082 + *
52083 + * @todo NGS: Update the comments from the HW FS.
52084 + *
52085 + * -# Program the following fields in the endpoint specific registers
52086 + * for Control OUT EP 0, in order to receive a setup packet
52087 + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
52088 + * setup packets)
52089 + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
52090 + * to back setup packets)
52091 + * - In DMA mode, DOEPDMA0 Register with a memory address to
52092 + * store any setup packets received
52093 + *
52094 + * @param core_if Programming view of DWC_otg controller.
52095 + * @param pcd Programming view of the PCD.
52096 + */
52097 +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
52098 + dwc_otg_pcd_t * pcd)
52099 +{
52100 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52101 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
52102 + dwc_otg_dev_dma_desc_t *dma_desc;
52103 + depctl_data_t doepctl = {.d32 = 0 };
52104 +
52105 +#ifdef VERBOSE
52106 + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
52107 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
52108 +#endif
52109 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
52110 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
52111 + if (doepctl.b.epena) {
52112 + return;
52113 + }
52114 + }
52115 +
52116 + doeptsize0.b.supcnt = 3;
52117 + doeptsize0.b.pktcnt = 1;
52118 + doeptsize0.b.xfersize = 8 * 3;
52119 +
52120 + if (core_if->dma_enable) {
52121 + if (!core_if->dma_desc_enable) {
52122 + /** put here as for Hermes mode deptisz register should not be written */
52123 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
52124 + doeptsize0.d32);
52125 +
52126 + /** @todo dma needs to handle multiple setup packets (up to 3) */
52127 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
52128 + pcd->setup_pkt_dma_handle);
52129 + } else {
52130 + dev_if->setup_desc_index =
52131 + (dev_if->setup_desc_index + 1) & 1;
52132 + dma_desc =
52133 + dev_if->setup_desc_addr[dev_if->setup_desc_index];
52134 +
52135 + /** DMA Descriptor Setup */
52136 + dma_desc->status.b.bs = BS_HOST_BUSY;
52137 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
52138 + dma_desc->status.b.sr = 0;
52139 + dma_desc->status.b.mtrf = 0;
52140 + }
52141 + dma_desc->status.b.l = 1;
52142 + dma_desc->status.b.ioc = 1;
52143 + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
52144 + dma_desc->buf = pcd->setup_pkt_dma_handle;
52145 + dma_desc->status.b.sts = 0;
52146 + dma_desc->status.b.bs = BS_HOST_READY;
52147 +
52148 + /** DOEPDMA0 Register write */
52149 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
52150 + dev_if->dma_setup_desc_addr
52151 + [dev_if->setup_desc_index]);
52152 + }
52153 +
52154 + } else {
52155 + /** put here as for Hermes mode deptisz register should not be written */
52156 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
52157 + doeptsize0.d32);
52158 + }
52159 +
52160 + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
52161 + doepctl.d32 = 0;
52162 + doepctl.b.epena = 1;
52163 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
52164 + doepctl.b.cnak = 1;
52165 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
52166 + } else {
52167 + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
52168 + }
52169 +
52170 +#ifdef VERBOSE
52171 + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
52172 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
52173 + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
52174 + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
52175 +#endif
52176 +}
52177 +
52178 +/**
52179 + * This interrupt occurs when a USB Reset is detected. When the USB
52180 + * Reset Interrupt occurs the device state is set to DEFAULT and the
52181 + * EP0 state is set to IDLE.
52182 + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
52183 + * -# Unmask the following interrupt bits
52184 + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
52185 + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
52186 + * - DOEPMSK.SETUP = 1
52187 + * - DOEPMSK.XferCompl = 1
52188 + * - DIEPMSK.XferCompl = 1
52189 + * - DIEPMSK.TimeOut = 1
52190 + * -# Program the following fields in the endpoint specific registers
52191 + * for Control OUT EP 0, in order to receive a setup packet
52192 + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
52193 + * setup packets)
52194 + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
52195 + * to back setup packets)
52196 + * - In DMA mode, DOEPDMA0 Register with a memory address to
52197 + * store any setup packets received
52198 + * At this point, all the required initialization, except for enabling
52199 + * the control 0 OUT endpoint is done, for receiving SETUP packets.
52200 + */
52201 +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
52202 +{
52203 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52204 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52205 + depctl_data_t doepctl = {.d32 = 0 };
52206 + depctl_data_t diepctl = {.d32 = 0 };
52207 + daint_data_t daintmsk = {.d32 = 0 };
52208 + doepmsk_data_t doepmsk = {.d32 = 0 };
52209 + diepmsk_data_t diepmsk = {.d32 = 0 };
52210 + dcfg_data_t dcfg = {.d32 = 0 };
52211 + grstctl_t resetctl = {.d32 = 0 };
52212 + dctl_data_t dctl = {.d32 = 0 };
52213 + int i = 0;
52214 + gintsts_data_t gintsts;
52215 + pcgcctl_data_t power = {.d32 = 0 };
52216 +
52217 + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
52218 + if (power.b.stoppclk) {
52219 + power.d32 = 0;
52220 + power.b.stoppclk = 1;
52221 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52222 +
52223 + power.b.pwrclmp = 1;
52224 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52225 +
52226 + power.b.rstpdwnmodule = 1;
52227 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52228 + }
52229 +
52230 + core_if->lx_state = DWC_OTG_L0;
52231 +
52232 + DWC_PRINTF("USB RESET\n");
52233 +#ifdef DWC_EN_ISOC
52234 + for (i = 1; i < 16; ++i) {
52235 + dwc_otg_pcd_ep_t *ep;
52236 + dwc_ep_t *dwc_ep;
52237 + ep = get_in_ep(pcd, i);
52238 + if (ep != 0) {
52239 + dwc_ep = &ep->dwc_ep;
52240 + dwc_ep->next_frame = 0xffffffff;
52241 + }
52242 + }
52243 +#endif /* DWC_EN_ISOC */
52244 +
52245 + /* reset the HNP settings */
52246 + dwc_otg_pcd_update_otg(pcd, 1);
52247 +
52248 + /* Clear the Remote Wakeup Signalling */
52249 + dctl.b.rmtwkupsig = 1;
52250 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
52251 +
52252 + /* Set NAK for all OUT EPs */
52253 + doepctl.b.snak = 1;
52254 + for (i = 0; i <= dev_if->num_out_eps; i++) {
52255 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
52256 + }
52257 +
52258 + /* Flush the NP Tx FIFO */
52259 + dwc_otg_flush_tx_fifo(core_if, 0x10);
52260 + /* Flush the Learning Queue */
52261 + resetctl.b.intknqflsh = 1;
52262 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
52263 +
52264 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
52265 + core_if->start_predict = 0;
52266 + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
52267 + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
52268 + }
52269 + core_if->nextep_seq[0] = 0;
52270 + core_if->first_in_nextep_seq = 0;
52271 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
52272 + diepctl.b.nextep = 0;
52273 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
52274 +
52275 + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
52276 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
52277 + dcfg.b.epmscnt = 2;
52278 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
52279 +
52280 + DWC_DEBUGPL(DBG_PCDV,
52281 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
52282 + __func__, core_if->first_in_nextep_seq);
52283 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
52284 + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
52285 + }
52286 + }
52287 +
52288 + if (core_if->multiproc_int_enable) {
52289 + daintmsk.b.inep0 = 1;
52290 + daintmsk.b.outep0 = 1;
52291 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
52292 + daintmsk.d32);
52293 +
52294 + doepmsk.b.setup = 1;
52295 + doepmsk.b.xfercompl = 1;
52296 + doepmsk.b.ahberr = 1;
52297 + doepmsk.b.epdisabled = 1;
52298 +
52299 + if ((core_if->dma_desc_enable) ||
52300 + (core_if->dma_enable
52301 + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
52302 + doepmsk.b.stsphsercvd = 1;
52303 + }
52304 + if (core_if->dma_desc_enable)
52305 + doepmsk.b.bna = 1;
52306 +/*
52307 + doepmsk.b.babble = 1;
52308 + doepmsk.b.nyet = 1;
52309 +
52310 + if (core_if->dma_enable) {
52311 + doepmsk.b.nak = 1;
52312 + }
52313 +*/
52314 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
52315 + doepmsk.d32);
52316 +
52317 + diepmsk.b.xfercompl = 1;
52318 + diepmsk.b.timeout = 1;
52319 + diepmsk.b.epdisabled = 1;
52320 + diepmsk.b.ahberr = 1;
52321 + diepmsk.b.intknepmis = 1;
52322 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
52323 + diepmsk.b.intknepmis = 0;
52324 +
52325 +/* if (core_if->dma_desc_enable) {
52326 + diepmsk.b.bna = 1;
52327 + }
52328 +*/
52329 +/*
52330 + if (core_if->dma_enable) {
52331 + diepmsk.b.nak = 1;
52332 + }
52333 +*/
52334 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
52335 + diepmsk.d32);
52336 + } else {
52337 + daintmsk.b.inep0 = 1;
52338 + daintmsk.b.outep0 = 1;
52339 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
52340 + daintmsk.d32);
52341 +
52342 + doepmsk.b.setup = 1;
52343 + doepmsk.b.xfercompl = 1;
52344 + doepmsk.b.ahberr = 1;
52345 + doepmsk.b.epdisabled = 1;
52346 +
52347 + if ((core_if->dma_desc_enable) ||
52348 + (core_if->dma_enable
52349 + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
52350 + doepmsk.b.stsphsercvd = 1;
52351 + }
52352 + if (core_if->dma_desc_enable)
52353 + doepmsk.b.bna = 1;
52354 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
52355 +
52356 + diepmsk.b.xfercompl = 1;
52357 + diepmsk.b.timeout = 1;
52358 + diepmsk.b.epdisabled = 1;
52359 + diepmsk.b.ahberr = 1;
52360 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
52361 + diepmsk.b.intknepmis = 0;
52362 +/*
52363 + if (core_if->dma_desc_enable) {
52364 + diepmsk.b.bna = 1;
52365 + }
52366 +*/
52367 +
52368 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
52369 + }
52370 +
52371 + /* Reset Device Address */
52372 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
52373 + dcfg.b.devaddr = 0;
52374 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
52375 +
52376 + /* setup EP0 to receive SETUP packets */
52377 + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
52378 + ep0_out_start(core_if, pcd);
52379 +
52380 + /* Clear interrupt */
52381 + gintsts.d32 = 0;
52382 + gintsts.b.usbreset = 1;
52383 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52384 +
52385 + return 1;
52386 +}
52387 +
52388 +/**
52389 + * Get the device speed from the device status register and convert it
52390 + * to USB speed constant.
52391 + *
52392 + * @param core_if Programming view of DWC_otg controller.
52393 + */
52394 +static int get_device_speed(dwc_otg_core_if_t * core_if)
52395 +{
52396 + dsts_data_t dsts;
52397 + int speed = 0;
52398 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
52399 +
52400 + switch (dsts.b.enumspd) {
52401 + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
52402 + speed = USB_SPEED_HIGH;
52403 + break;
52404 + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
52405 + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
52406 + speed = USB_SPEED_FULL;
52407 + break;
52408 +
52409 + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
52410 + speed = USB_SPEED_LOW;
52411 + break;
52412 + }
52413 +
52414 + return speed;
52415 +}
52416 +
52417 +/**
52418 + * Read the device status register and set the device speed in the
52419 + * data structure.
52420 + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
52421 + */
52422 +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
52423 +{
52424 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52425 + gintsts_data_t gintsts;
52426 + gusbcfg_data_t gusbcfg;
52427 + dwc_otg_core_global_regs_t *global_regs =
52428 + GET_CORE_IF(pcd)->core_global_regs;
52429 + uint8_t utmi16b, utmi8b;
52430 + int speed;
52431 + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
52432 +
52433 + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
52434 + utmi16b = 6; //vahrama old value was 6;
52435 + utmi8b = 9;
52436 + } else {
52437 + utmi16b = 4;
52438 + utmi8b = 8;
52439 + }
52440 + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
52441 + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
52442 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52443 + }
52444 +
52445 +#ifdef DEBUG_EP0
52446 + print_ep0_state(pcd);
52447 +#endif
52448 +
52449 + if (pcd->ep0state == EP0_DISCONNECT) {
52450 + pcd->ep0state = EP0_IDLE;
52451 + } else if (pcd->ep0state == EP0_STALL) {
52452 + pcd->ep0state = EP0_IDLE;
52453 + }
52454 +
52455 + pcd->ep0state = EP0_IDLE;
52456 +
52457 + ep0->stopped = 0;
52458 +
52459 + speed = get_device_speed(GET_CORE_IF(pcd));
52460 + pcd->fops->connect(pcd, speed);
52461 +
52462 + /* Set USB turnaround time based on device speed and PHY interface. */
52463 + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
52464 + if (speed == USB_SPEED_HIGH) {
52465 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52466 + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
52467 + /* ULPI interface */
52468 + gusbcfg.b.usbtrdtim = 9;
52469 + }
52470 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52471 + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
52472 + /* UTMI+ interface */
52473 + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
52474 + gusbcfg.b.usbtrdtim = utmi8b;
52475 + } else if (GET_CORE_IF(pcd)->hwcfg4.
52476 + b.utmi_phy_data_width == 1) {
52477 + gusbcfg.b.usbtrdtim = utmi16b;
52478 + } else if (GET_CORE_IF(pcd)->
52479 + core_params->phy_utmi_width == 8) {
52480 + gusbcfg.b.usbtrdtim = utmi8b;
52481 + } else {
52482 + gusbcfg.b.usbtrdtim = utmi16b;
52483 + }
52484 + }
52485 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52486 + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
52487 + /* UTMI+ OR ULPI interface */
52488 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
52489 + /* ULPI interface */
52490 + gusbcfg.b.usbtrdtim = 9;
52491 + } else {
52492 + /* UTMI+ interface */
52493 + if (GET_CORE_IF(pcd)->
52494 + core_params->phy_utmi_width == 16) {
52495 + gusbcfg.b.usbtrdtim = utmi16b;
52496 + } else {
52497 + gusbcfg.b.usbtrdtim = utmi8b;
52498 + }
52499 + }
52500 + }
52501 + } else {
52502 + /* Full or low speed */
52503 + gusbcfg.b.usbtrdtim = 9;
52504 + }
52505 + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
52506 +
52507 + /* Clear interrupt */
52508 + gintsts.d32 = 0;
52509 + gintsts.b.enumdone = 1;
52510 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52511 + gintsts.d32);
52512 + return 1;
52513 +}
52514 +
52515 +/**
52516 + * This interrupt indicates that the ISO OUT Packet was dropped due to
52517 + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
52518 + * read all the data from the Rx FIFO.
52519 + */
52520 +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
52521 +{
52522 + gintmsk_data_t intr_mask = {.d32 = 0 };
52523 + gintsts_data_t gintsts;
52524 +
52525 + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
52526 + "ISOC Out Dropped");
52527 +
52528 + intr_mask.b.isooutdrop = 1;
52529 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52530 + intr_mask.d32, 0);
52531 +
52532 + /* Clear interrupt */
52533 + gintsts.d32 = 0;
52534 + gintsts.b.isooutdrop = 1;
52535 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52536 + gintsts.d32);
52537 +
52538 + return 1;
52539 +}
52540 +
52541 +/**
52542 + * This interrupt indicates the end of the portion of the micro-frame
52543 + * for periodic transactions. If there is a periodic transaction for
52544 + * the next frame, load the packets into the EP periodic Tx FIFO.
52545 + */
52546 +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
52547 +{
52548 + gintmsk_data_t intr_mask = {.d32 = 0 };
52549 + gintsts_data_t gintsts;
52550 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
52551 +
52552 + intr_mask.b.eopframe = 1;
52553 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52554 + intr_mask.d32, 0);
52555 +
52556 + /* Clear interrupt */
52557 + gintsts.d32 = 0;
52558 + gintsts.b.eopframe = 1;
52559 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52560 + gintsts.d32);
52561 +
52562 + return 1;
52563 +}
52564 +
52565 +/**
52566 + * This interrupt indicates that EP of the packet on the top of the
52567 + * non-periodic Tx FIFO does not match EP of the IN Token received.
52568 + *
52569 + * The "Device IN Token Queue" Registers are read to determine the
52570 + * order the IN Tokens have been received. The non-periodic Tx FIFO
52571 + * is flushed, so it can be reloaded in the order seen in the IN Token
52572 + * Queue.
52573 + */
52574 +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
52575 +{
52576 + gintsts_data_t gintsts;
52577 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52578 + dctl_data_t dctl;
52579 + gintmsk_data_t intr_mask = {.d32 = 0 };
52580 +
52581 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
52582 + core_if->start_predict = 1;
52583 +
52584 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
52585 +
52586 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
52587 + if (!gintsts.b.ginnakeff) {
52588 + /* Disable EP Mismatch interrupt */
52589 + intr_mask.d32 = 0;
52590 + intr_mask.b.epmismatch = 1;
52591 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
52592 + /* Enable the Global IN NAK Effective Interrupt */
52593 + intr_mask.d32 = 0;
52594 + intr_mask.b.ginnakeff = 1;
52595 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
52596 + /* Set the global non-periodic IN NAK handshake */
52597 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
52598 + dctl.b.sgnpinnak = 1;
52599 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
52600 + } else {
52601 + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
52602 + }
52603 + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
52604 + * handler after Global IN NAK Effective interrupt will be asserted */
52605 + }
52606 + /* Clear interrupt */
52607 + gintsts.d32 = 0;
52608 + gintsts.b.epmismatch = 1;
52609 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52610 +
52611 + return 1;
52612 +}
52613 +
52614 +/**
52615 + * This interrupt is valid only in DMA mode. This interrupt indicates that the
52616 + * core has stopped fetching data for IN endpoints due to the unavailability of
52617 + * TxFIFO space or Request Queue space. This interrupt is used by the
52618 + * application for an endpoint mismatch algorithm.
52619 + *
52620 + * @param pcd The PCD
52621 + */
52622 +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
52623 +{
52624 + gintsts_data_t gintsts;
52625 + gintmsk_data_t gintmsk_data;
52626 + dctl_data_t dctl;
52627 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52628 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
52629 +
52630 + /* Clear the global non-periodic IN NAK handshake */
52631 + dctl.d32 = 0;
52632 + dctl.b.cgnpinnak = 1;
52633 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
52634 +
52635 + /* Mask GINTSTS.FETSUSP interrupt */
52636 + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
52637 + gintmsk_data.b.fetsusp = 0;
52638 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
52639 +
52640 + /* Clear interrupt */
52641 + gintsts.d32 = 0;
52642 + gintsts.b.fetsusp = 1;
52643 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52644 +
52645 + return 1;
52646 +}
52647 +/**
52648 + * This funcion stalls EP0.
52649 + */
52650 +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
52651 +{
52652 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52653 + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
52654 + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
52655 + ctrl->bmRequestType, ctrl->bRequest, err_val);
52656 +
52657 + ep0->dwc_ep.is_in = 1;
52658 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
52659 + pcd->ep0.stopped = 1;
52660 + pcd->ep0state = EP0_IDLE;
52661 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52662 +}
52663 +
52664 +/**
52665 + * This functions delegates the setup command to the gadget driver.
52666 + */
52667 +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
52668 + usb_device_request_t * ctrl)
52669 +{
52670 + int ret = 0;
52671 + DWC_SPINUNLOCK(pcd->lock);
52672 + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
52673 + DWC_SPINLOCK(pcd->lock);
52674 + if (ret < 0) {
52675 + ep0_do_stall(pcd, ret);
52676 + }
52677 +
52678 + /** @todo This is a g_file_storage gadget driver specific
52679 + * workaround: a DELAYED_STATUS result from the fsg_setup
52680 + * routine will result in the gadget queueing a EP0 IN status
52681 + * phase for a two-stage control transfer. Exactly the same as
52682 + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
52683 + * specific request. Need a generic way to know when the gadget
52684 + * driver will queue the status phase. Can we assume when we
52685 + * call the gadget driver setup() function that it will always
52686 + * queue and require the following flag? Need to look into
52687 + * this.
52688 + */
52689 +
52690 + if (ret == 256 + 999) {
52691 + pcd->request_config = 1;
52692 + }
52693 +}
52694 +
52695 +#ifdef DWC_UTE_CFI
52696 +/**
52697 + * This functions delegates the CFI setup commands to the gadget driver.
52698 + * This function will return a negative value to indicate a failure.
52699 + */
52700 +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
52701 + struct cfi_usb_ctrlrequest *ctrl_req)
52702 +{
52703 + int ret = 0;
52704 +
52705 + if (pcd->fops && pcd->fops->cfi_setup) {
52706 + DWC_SPINUNLOCK(pcd->lock);
52707 + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
52708 + DWC_SPINLOCK(pcd->lock);
52709 + if (ret < 0) {
52710 + ep0_do_stall(pcd, ret);
52711 + return ret;
52712 + }
52713 + }
52714 +
52715 + return ret;
52716 +}
52717 +#endif
52718 +
52719 +/**
52720 + * This function starts the Zero-Length Packet for the IN status phase
52721 + * of a 2 stage control transfer.
52722 + */
52723 +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
52724 +{
52725 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52726 + if (pcd->ep0state == EP0_STALL) {
52727 + return;
52728 + }
52729 +
52730 + pcd->ep0state = EP0_IN_STATUS_PHASE;
52731 +
52732 + /* Prepare for more SETUP Packets */
52733 + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
52734 + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
52735 + && (pcd->core_if->dma_desc_enable)
52736 + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
52737 + DWC_DEBUGPL(DBG_PCDV,
52738 + "Data terminated wait next packet in out_desc_addr\n");
52739 + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
52740 + pcd->data_terminated = 1;
52741 + }
52742 + ep0->dwc_ep.xfer_len = 0;
52743 + ep0->dwc_ep.xfer_count = 0;
52744 + ep0->dwc_ep.is_in = 1;
52745 + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
52746 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52747 +
52748 + /* Prepare for more SETUP Packets */
52749 + //ep0_out_start(GET_CORE_IF(pcd), pcd);
52750 +}
52751 +
52752 +/**
52753 + * This function starts the Zero-Length Packet for the OUT status phase
52754 + * of a 2 stage control transfer.
52755 + */
52756 +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
52757 +{
52758 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52759 + if (pcd->ep0state == EP0_STALL) {
52760 + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
52761 + return;
52762 + }
52763 + pcd->ep0state = EP0_OUT_STATUS_PHASE;
52764 +
52765 + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
52766 + ep0->dwc_ep.xfer_len = 0;
52767 + ep0->dwc_ep.xfer_count = 0;
52768 + ep0->dwc_ep.is_in = 0;
52769 + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
52770 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52771 +
52772 + /* Prepare for more SETUP Packets */
52773 + if (GET_CORE_IF(pcd)->dma_enable == 0) {
52774 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52775 + }
52776 +}
52777 +
52778 +/**
52779 + * Clear the EP halt (STALL) and if pending requests start the
52780 + * transfer.
52781 + */
52782 +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
52783 +{
52784 + if (ep->dwc_ep.stall_clear_flag == 0)
52785 + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
52786 +
52787 + /* Reactive the EP */
52788 + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
52789 + if (ep->stopped) {
52790 + ep->stopped = 0;
52791 + /* If there is a request in the EP queue start it */
52792 +
52793 + /** @todo FIXME: this causes an EP mismatch in DMA mode.
52794 + * epmismatch not yet implemented. */
52795 +
52796 + /*
52797 + * Above fixme is solved by implmenting a tasklet to call the
52798 + * start_next_request(), outside of interrupt context at some
52799 + * time after the current time, after a clear-halt setup packet.
52800 + * Still need to implement ep mismatch in the future if a gadget
52801 + * ever uses more than one endpoint at once
52802 + */
52803 + ep->queue_sof = 1;
52804 + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
52805 + }
52806 + /* Start Control Status Phase */
52807 + do_setup_in_status_phase(pcd);
52808 +}
52809 +
52810 +/**
52811 + * This function is called when the SET_FEATURE TEST_MODE Setup packet
52812 + * is sent from the host. The Device Control register is written with
52813 + * the Test Mode bits set to the specified Test Mode. This is done as
52814 + * a tasklet so that the "Status" phase of the control transfer
52815 + * completes before transmitting the TEST packets.
52816 + *
52817 + * @todo This has not been tested since the tasklet struct was put
52818 + * into the PCD struct!
52819 + *
52820 + */
52821 +void do_test_mode(void *data)
52822 +{
52823 + dctl_data_t dctl;
52824 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
52825 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52826 + int test_mode = pcd->test_mode;
52827 +
52828 +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
52829 +
52830 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
52831 + switch (test_mode) {
52832 + case 1: // TEST_J
52833 + dctl.b.tstctl = 1;
52834 + break;
52835 +
52836 + case 2: // TEST_K
52837 + dctl.b.tstctl = 2;
52838 + break;
52839 +
52840 + case 3: // TEST_SE0_NAK
52841 + dctl.b.tstctl = 3;
52842 + break;
52843 +
52844 + case 4: // TEST_PACKET
52845 + dctl.b.tstctl = 4;
52846 + break;
52847 +
52848 + case 5: // TEST_FORCE_ENABLE
52849 + dctl.b.tstctl = 5;
52850 + break;
52851 + }
52852 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
52853 +}
52854 +
52855 +/**
52856 + * This function process the GET_STATUS Setup Commands.
52857 + */
52858 +static inline void do_get_status(dwc_otg_pcd_t * pcd)
52859 +{
52860 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52861 + dwc_otg_pcd_ep_t *ep;
52862 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52863 + uint16_t *status = pcd->status_buf;
52864 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52865 +
52866 +#ifdef DEBUG_EP0
52867 + DWC_DEBUGPL(DBG_PCD,
52868 + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
52869 + ctrl.bmRequestType, ctrl.bRequest,
52870 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
52871 + UGETW(ctrl.wLength));
52872 +#endif
52873 +
52874 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
52875 + case UT_DEVICE:
52876 + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
52877 + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
52878 + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
52879 + DWC_PRINTF("OTG CAP - %d, %d\n",
52880 + core_if->core_params->otg_cap,
52881 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
52882 + if (core_if->otg_ver == 1
52883 + && core_if->core_params->otg_cap ==
52884 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
52885 + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
52886 + *otgsts = (core_if->otg_sts & 0x1);
52887 + pcd->ep0_pending = 1;
52888 + ep0->dwc_ep.start_xfer_buff =
52889 + (uint8_t *) otgsts;
52890 + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
52891 + ep0->dwc_ep.dma_addr =
52892 + pcd->status_buf_dma_handle;
52893 + ep0->dwc_ep.xfer_len = 1;
52894 + ep0->dwc_ep.xfer_count = 0;
52895 + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
52896 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
52897 + &ep0->dwc_ep);
52898 + return;
52899 + } else {
52900 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52901 + return;
52902 + }
52903 + break;
52904 + } else {
52905 + *status = 0x1; /* Self powered */
52906 + *status |= pcd->remote_wakeup_enable << 1;
52907 + break;
52908 + }
52909 + case UT_INTERFACE:
52910 + *status = 0;
52911 + break;
52912 +
52913 + case UT_ENDPOINT:
52914 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
52915 + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
52916 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52917 + return;
52918 + }
52919 + /** @todo check for EP stall */
52920 + *status = ep->stopped;
52921 + break;
52922 + }
52923 + pcd->ep0_pending = 1;
52924 + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
52925 + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
52926 + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
52927 + ep0->dwc_ep.xfer_len = 2;
52928 + ep0->dwc_ep.xfer_count = 0;
52929 + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
52930 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52931 +}
52932 +
52933 +/**
52934 + * This function process the SET_FEATURE Setup Commands.
52935 + */
52936 +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
52937 +{
52938 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52939 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
52940 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52941 + dwc_otg_pcd_ep_t *ep = 0;
52942 + int32_t otg_cap_param = core_if->core_params->otg_cap;
52943 + gotgctl_data_t gotgctl = {.d32 = 0 };
52944 +
52945 + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
52946 + ctrl.bmRequestType, ctrl.bRequest,
52947 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
52948 + UGETW(ctrl.wLength));
52949 + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
52950 +
52951 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
52952 + case UT_DEVICE:
52953 + switch (UGETW(ctrl.wValue)) {
52954 + case UF_DEVICE_REMOTE_WAKEUP:
52955 + pcd->remote_wakeup_enable = 1;
52956 + break;
52957 +
52958 + case UF_TEST_MODE:
52959 + /* Setup the Test Mode tasklet to do the Test
52960 + * Packet generation after the SETUP Status
52961 + * phase has completed. */
52962 +
52963 + /** @todo This has not been tested since the
52964 + * tasklet struct was put into the PCD
52965 + * struct! */
52966 + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
52967 + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
52968 + break;
52969 +
52970 + case UF_DEVICE_B_HNP_ENABLE:
52971 + DWC_DEBUGPL(DBG_PCDV,
52972 + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
52973 +
52974 + /* dev may initiate HNP */
52975 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
52976 + pcd->b_hnp_enable = 1;
52977 + dwc_otg_pcd_update_otg(pcd, 0);
52978 + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
52979 + /**@todo Is the gotgctl.devhnpen cleared
52980 + * by a USB Reset? */
52981 + gotgctl.b.devhnpen = 1;
52982 + gotgctl.b.hnpreq = 1;
52983 + DWC_WRITE_REG32(&global_regs->gotgctl,
52984 + gotgctl.d32);
52985 + } else {
52986 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52987 + return;
52988 + }
52989 + break;
52990 +
52991 + case UF_DEVICE_A_HNP_SUPPORT:
52992 + /* RH port supports HNP */
52993 + DWC_DEBUGPL(DBG_PCDV,
52994 + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
52995 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
52996 + pcd->a_hnp_support = 1;
52997 + dwc_otg_pcd_update_otg(pcd, 0);
52998 + } else {
52999 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53000 + return;
53001 + }
53002 + break;
53003 +
53004 + case UF_DEVICE_A_ALT_HNP_SUPPORT:
53005 + /* other RH port does */
53006 + DWC_DEBUGPL(DBG_PCDV,
53007 + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
53008 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
53009 + pcd->a_alt_hnp_support = 1;
53010 + dwc_otg_pcd_update_otg(pcd, 0);
53011 + } else {
53012 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53013 + return;
53014 + }
53015 + break;
53016 +
53017 + default:
53018 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53019 + return;
53020 +
53021 + }
53022 + do_setup_in_status_phase(pcd);
53023 + break;
53024 +
53025 + case UT_INTERFACE:
53026 + do_gadget_setup(pcd, &ctrl);
53027 + break;
53028 +
53029 + case UT_ENDPOINT:
53030 + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
53031 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
53032 + if (ep == 0) {
53033 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53034 + return;
53035 + }
53036 + ep->stopped = 1;
53037 + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
53038 + }
53039 + do_setup_in_status_phase(pcd);
53040 + break;
53041 + }
53042 +}
53043 +
53044 +/**
53045 + * This function process the CLEAR_FEATURE Setup Commands.
53046 + */
53047 +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
53048 +{
53049 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53050 + dwc_otg_pcd_ep_t *ep = 0;
53051 +
53052 + DWC_DEBUGPL(DBG_PCD,
53053 + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
53054 + ctrl.bmRequestType, ctrl.bRequest,
53055 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53056 + UGETW(ctrl.wLength));
53057 +
53058 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
53059 + case UT_DEVICE:
53060 + switch (UGETW(ctrl.wValue)) {
53061 + case UF_DEVICE_REMOTE_WAKEUP:
53062 + pcd->remote_wakeup_enable = 0;
53063 + break;
53064 +
53065 + case UF_TEST_MODE:
53066 + /** @todo Add CLEAR_FEATURE for TEST modes. */
53067 + break;
53068 +
53069 + default:
53070 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53071 + return;
53072 + }
53073 + do_setup_in_status_phase(pcd);
53074 + break;
53075 +
53076 + case UT_ENDPOINT:
53077 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
53078 + if (ep == 0) {
53079 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53080 + return;
53081 + }
53082 +
53083 + pcd_clear_halt(pcd, ep);
53084 +
53085 + break;
53086 + }
53087 +}
53088 +
53089 +/**
53090 + * This function process the SET_ADDRESS Setup Commands.
53091 + */
53092 +static inline void do_set_address(dwc_otg_pcd_t * pcd)
53093 +{
53094 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
53095 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53096 +
53097 + if (ctrl.bmRequestType == UT_DEVICE) {
53098 + dcfg_data_t dcfg = {.d32 = 0 };
53099 +
53100 +#ifdef DEBUG_EP0
53101 +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
53102 +#endif
53103 + dcfg.b.devaddr = UGETW(ctrl.wValue);
53104 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
53105 + do_setup_in_status_phase(pcd);
53106 + }
53107 +}
53108 +
53109 +/**
53110 + * This function processes SETUP commands. In Linux, the USB Command
53111 + * processing is done in two places - the first being the PCD and the
53112 + * second in the Gadget Driver (for example, the File-Backed Storage
53113 + * Gadget Driver).
53114 + *
53115 + * <table>
53116 + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
53117 + *
53118 + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
53119 + * defined in chapter 9 of the USB 2.0 Specification chapter 9
53120 + * </td></tr>
53121 + *
53122 + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
53123 + * requests are the ENDPOINT_HALT feature is procesed, all others the
53124 + * interface requests are ignored.</td></tr>
53125 + *
53126 + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
53127 + * requests are processed by the PCD. Interface requests are passed
53128 + * to the Gadget Driver.</td></tr>
53129 + *
53130 + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
53131 + * with device address received </td></tr>
53132 + *
53133 + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
53134 + * requested descriptor</td></tr>
53135 + *
53136 + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
53137 + * not implemented by any of the existing Gadget Drivers.</td></tr>
53138 + *
53139 + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
53140 + * all EPs and enable EPs for new configuration.</td></tr>
53141 + *
53142 + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
53143 + * the current configuration</td></tr>
53144 + *
53145 + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
53146 + * EPs and enable EPs for new configuration.</td></tr>
53147 + *
53148 + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
53149 + * current interface.</td></tr>
53150 + *
53151 + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
53152 + * message.</td></tr>
53153 + * </table>
53154 + *
53155 + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
53156 + * processed by pcd_setup. Calling the Function Driver's setup function from
53157 + * pcd_setup processes the gadget SETUP commands.
53158 + */
53159 +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
53160 +{
53161 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
53162 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53163 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53164 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
53165 +
53166 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
53167 +
53168 +#ifdef DWC_UTE_CFI
53169 + int retval = 0;
53170 + struct cfi_usb_ctrlrequest cfi_req;
53171 +#endif
53172 +
53173 + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
53174 +
53175 + /** In BDMA more then 1 setup packet is not supported till 3.00a */
53176 + if (core_if->dma_enable && core_if->dma_desc_enable == 0
53177 + && (doeptsize0.b.supcnt < 2)
53178 + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
53179 + DWC_ERROR
53180 + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
53181 + }
53182 + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
53183 + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
53184 + ctrl =
53185 + (pcd->setup_pkt +
53186 + (3 - doeptsize0.b.supcnt - 1 +
53187 + ep0->dwc_ep.stp_rollover))->req;
53188 + }
53189 +#ifdef DEBUG_EP0
53190 + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
53191 + ctrl.bmRequestType, ctrl.bRequest,
53192 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53193 + UGETW(ctrl.wLength));
53194 +#endif
53195 +
53196 + /* Clean up the request queue */
53197 + dwc_otg_request_nuke(ep0);
53198 + ep0->stopped = 0;
53199 +
53200 + if (ctrl.bmRequestType & UE_DIR_IN) {
53201 + ep0->dwc_ep.is_in = 1;
53202 + pcd->ep0state = EP0_IN_DATA_PHASE;
53203 + } else {
53204 + ep0->dwc_ep.is_in = 0;
53205 + pcd->ep0state = EP0_OUT_DATA_PHASE;
53206 + }
53207 +
53208 + if (UGETW(ctrl.wLength) == 0) {
53209 + ep0->dwc_ep.is_in = 1;
53210 + pcd->ep0state = EP0_IN_STATUS_PHASE;
53211 + }
53212 +
53213 + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
53214 +
53215 +#ifdef DWC_UTE_CFI
53216 + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
53217 +
53218 + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
53219 + ctrl.bRequestType, ctrl.bRequest);
53220 + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
53221 + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
53222 + retval = cfi_setup(pcd, &cfi_req);
53223 + if (retval < 0) {
53224 + ep0_do_stall(pcd, retval);
53225 + pcd->ep0_pending = 0;
53226 + return;
53227 + }
53228 +
53229 + /* if need gadget setup then call it and check the retval */
53230 + if (pcd->cfi->need_gadget_att) {
53231 + retval =
53232 + cfi_gadget_setup(pcd,
53233 + &pcd->
53234 + cfi->ctrl_req);
53235 + if (retval < 0) {
53236 + pcd->ep0_pending = 0;
53237 + return;
53238 + }
53239 + }
53240 +
53241 + if (pcd->cfi->need_status_in_complete) {
53242 + do_setup_in_status_phase(pcd);
53243 + }
53244 + return;
53245 + }
53246 + }
53247 +#endif
53248 +
53249 + /* handle non-standard (class/vendor) requests in the gadget driver */
53250 + do_gadget_setup(pcd, &ctrl);
53251 + return;
53252 + }
53253 +
53254 + /** @todo NGS: Handle bad setup packet? */
53255 +
53256 +///////////////////////////////////////////
53257 +//// --- Standard Request handling --- ////
53258 +
53259 + switch (ctrl.bRequest) {
53260 + case UR_GET_STATUS:
53261 + do_get_status(pcd);
53262 + break;
53263 +
53264 + case UR_CLEAR_FEATURE:
53265 + do_clear_feature(pcd);
53266 + break;
53267 +
53268 + case UR_SET_FEATURE:
53269 + do_set_feature(pcd);
53270 + break;
53271 +
53272 + case UR_SET_ADDRESS:
53273 + do_set_address(pcd);
53274 + break;
53275 +
53276 + case UR_SET_INTERFACE:
53277 + case UR_SET_CONFIG:
53278 +// _pcd->request_config = 1; /* Configuration changed */
53279 + do_gadget_setup(pcd, &ctrl);
53280 + break;
53281 +
53282 + case UR_SYNCH_FRAME:
53283 + do_gadget_setup(pcd, &ctrl);
53284 + break;
53285 +
53286 + default:
53287 + /* Call the Gadget Driver's setup functions */
53288 + do_gadget_setup(pcd, &ctrl);
53289 + break;
53290 + }
53291 +}
53292 +
53293 +/**
53294 + * This function completes the ep0 control transfer.
53295 + */
53296 +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
53297 +{
53298 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
53299 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53300 + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
53301 + dev_if->in_ep_regs[ep->dwc_ep.num];
53302 +#ifdef DEBUG_EP0
53303 + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
53304 + dev_if->out_ep_regs[ep->dwc_ep.num];
53305 +#endif
53306 + deptsiz0_data_t deptsiz;
53307 + dev_dma_desc_sts_t desc_sts;
53308 + dwc_otg_pcd_request_t *req;
53309 + int is_last = 0;
53310 + dwc_otg_pcd_t *pcd = ep->pcd;
53311 +
53312 +#ifdef DWC_UTE_CFI
53313 + struct cfi_usb_ctrlrequest *ctrlreq;
53314 + int retval = -DWC_E_NOT_SUPPORTED;
53315 +#endif
53316 +
53317 + desc_sts.b.bytes = 0;
53318 +
53319 + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53320 + if (ep->dwc_ep.is_in) {
53321 +#ifdef DEBUG_EP0
53322 + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
53323 +#endif
53324 + do_setup_out_status_phase(pcd);
53325 + } else {
53326 +#ifdef DEBUG_EP0
53327 + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
53328 +#endif
53329 +
53330 +#ifdef DWC_UTE_CFI
53331 + ctrlreq = &pcd->cfi->ctrl_req;
53332 +
53333 + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
53334 + if (ctrlreq->bRequest > 0xB0
53335 + && ctrlreq->bRequest < 0xBF) {
53336 +
53337 + /* Return if the PCD failed to handle the request */
53338 + if ((retval =
53339 + pcd->cfi->ops.
53340 + ctrl_write_complete(pcd->cfi,
53341 + pcd)) < 0) {
53342 + CFI_INFO
53343 + ("ERROR setting a new value in the PCD(%d)\n",
53344 + retval);
53345 + ep0_do_stall(pcd, retval);
53346 + pcd->ep0_pending = 0;
53347 + return 0;
53348 + }
53349 +
53350 + /* If the gadget needs to be notified on the request */
53351 + if (pcd->cfi->need_gadget_att == 1) {
53352 + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
53353 + retval =
53354 + cfi_gadget_setup(pcd,
53355 + &pcd->cfi->
53356 + ctrl_req);
53357 +
53358 + /* Return from the function if the gadget failed to process
53359 + * the request properly - this should never happen !!!
53360 + */
53361 + if (retval < 0) {
53362 + CFI_INFO
53363 + ("ERROR setting a new value in the gadget(%d)\n",
53364 + retval);
53365 + pcd->ep0_pending = 0;
53366 + return 0;
53367 + }
53368 + }
53369 +
53370 + CFI_INFO("%s: RETVAL=%d\n", __func__,
53371 + retval);
53372 + /* If we hit here then the PCD and the gadget has properly
53373 + * handled the request - so send the ZLP IN to the host.
53374 + */
53375 + /* @todo: MAS - decide whether we need to start the setup
53376 + * stage based on the need_setup value of the cfi object
53377 + */
53378 + do_setup_in_status_phase(pcd);
53379 + pcd->ep0_pending = 0;
53380 + return 1;
53381 + }
53382 + }
53383 +#endif
53384 +
53385 + do_setup_in_status_phase(pcd);
53386 + }
53387 + pcd->ep0_pending = 0;
53388 + return 1;
53389 + }
53390 +
53391 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53392 + return 0;
53393 + }
53394 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
53395 +
53396 + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
53397 + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
53398 + is_last = 1;
53399 + } else if (ep->dwc_ep.is_in) {
53400 + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
53401 + if (core_if->dma_desc_enable != 0)
53402 + desc_sts = dev_if->in_desc_addr->status;
53403 +#ifdef DEBUG_EP0
53404 + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
53405 + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
53406 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53407 +#endif
53408 +
53409 + if (((core_if->dma_desc_enable == 0)
53410 + && (deptsiz.b.xfersize == 0))
53411 + || ((core_if->dma_desc_enable != 0)
53412 + && (desc_sts.b.bytes == 0))) {
53413 + req->actual = ep->dwc_ep.xfer_count;
53414 + /* Is a Zero Len Packet needed? */
53415 + if (req->sent_zlp) {
53416 +#ifdef DEBUG_EP0
53417 + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
53418 +#endif
53419 + req->sent_zlp = 0;
53420 + }
53421 + do_setup_out_status_phase(pcd);
53422 + }
53423 + } else {
53424 + /* ep0-OUT */
53425 +#ifdef DEBUG_EP0
53426 + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
53427 + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
53428 + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
53429 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53430 +#endif
53431 + req->actual = ep->dwc_ep.xfer_count;
53432 +
53433 + /* Is a Zero Len Packet needed? */
53434 + if (req->sent_zlp) {
53435 +#ifdef DEBUG_EP0
53436 + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
53437 +#endif
53438 + req->sent_zlp = 0;
53439 + }
53440 + /* For older cores do setup in status phase in Slave/BDMA modes,
53441 + * starting from 3.00 do that only in slave, and for DMA modes
53442 + * just re-enable ep 0 OUT here*/
53443 + if (core_if->dma_enable == 0
53444 + || (core_if->dma_desc_enable == 0
53445 + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
53446 + do_setup_in_status_phase(pcd);
53447 + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
53448 + DWC_DEBUGPL(DBG_PCDV,
53449 + "Enable out ep before in status phase\n");
53450 + ep0_out_start(core_if, pcd);
53451 + }
53452 + }
53453 +
53454 + /* Complete the request */
53455 + if (is_last) {
53456 + dwc_otg_request_done(ep, req, 0);
53457 + ep->dwc_ep.start_xfer_buff = 0;
53458 + ep->dwc_ep.xfer_buff = 0;
53459 + ep->dwc_ep.xfer_len = 0;
53460 + return 1;
53461 + }
53462 + return 0;
53463 +}
53464 +
53465 +#ifdef DWC_UTE_CFI
53466 +/**
53467 + * This function calculates traverses all the CFI DMA descriptors and
53468 + * and accumulates the bytes that are left to be transfered.
53469 + *
53470 + * @return The total bytes left to transfered, or a negative value as failure
53471 + */
53472 +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
53473 +{
53474 + int32_t ret = 0;
53475 + int i;
53476 + struct dwc_otg_dma_desc *ddesc = NULL;
53477 + struct cfi_ep *cfiep;
53478 +
53479 + /* See if the pcd_ep has its respective cfi_ep mapped */
53480 + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
53481 + if (!cfiep) {
53482 + CFI_INFO("%s: Failed to find ep\n", __func__);
53483 + return -1;
53484 + }
53485 +
53486 + ddesc = ep->dwc_ep.descs;
53487 +
53488 + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
53489 +
53490 +#if defined(PRINT_CFI_DMA_DESCS)
53491 + print_desc(ddesc, ep->ep.name, i);
53492 +#endif
53493 + ret += ddesc->status.b.bytes;
53494 + ddesc++;
53495 + }
53496 +
53497 + if (ret)
53498 + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
53499 + ret);
53500 +
53501 + return ret;
53502 +}
53503 +#endif
53504 +
53505 +/**
53506 + * This function completes the request for the EP. If there are
53507 + * additional requests for the EP in the queue they will be started.
53508 + */
53509 +static void complete_ep(dwc_otg_pcd_ep_t * ep)
53510 +{
53511 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
53512 + struct device *dev = dwc_otg_pcd_to_dev(ep->pcd);
53513 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53514 + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
53515 + dev_if->in_ep_regs[ep->dwc_ep.num];
53516 + deptsiz_data_t deptsiz;
53517 + dev_dma_desc_sts_t desc_sts;
53518 + dwc_otg_pcd_request_t *req = 0;
53519 + dwc_otg_dev_dma_desc_t *dma_desc;
53520 + uint32_t byte_count = 0;
53521 + int is_last = 0;
53522 + int i;
53523 +
53524 + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
53525 + (ep->dwc_ep.is_in ? "IN" : "OUT"));
53526 +
53527 + /* Get any pending requests */
53528 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53529 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
53530 + if (!req) {
53531 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
53532 + return;
53533 + }
53534 + } else {
53535 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
53536 + return;
53537 + }
53538 +
53539 + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
53540 +
53541 + if (ep->dwc_ep.is_in) {
53542 + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
53543 +
53544 + if (core_if->dma_enable) {
53545 + if (core_if->dma_desc_enable == 0) {
53546 + if (deptsiz.b.xfersize == 0
53547 + && deptsiz.b.pktcnt == 0) {
53548 + byte_count =
53549 + ep->dwc_ep.xfer_len -
53550 + ep->dwc_ep.xfer_count;
53551 +
53552 + ep->dwc_ep.xfer_buff += byte_count;
53553 + ep->dwc_ep.dma_addr += byte_count;
53554 + ep->dwc_ep.xfer_count += byte_count;
53555 +
53556 + DWC_DEBUGPL(DBG_PCDV,
53557 + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
53558 + ep->dwc_ep.num,
53559 + (ep->dwc_ep.
53560 + is_in ? "IN" : "OUT"),
53561 + ep->dwc_ep.xfer_len,
53562 + deptsiz.b.xfersize,
53563 + deptsiz.b.pktcnt);
53564 +
53565 + if (ep->dwc_ep.xfer_len <
53566 + ep->dwc_ep.total_len) {
53567 + dwc_otg_ep_start_transfer
53568 + (core_if, &ep->dwc_ep);
53569 + } else if (ep->dwc_ep.sent_zlp) {
53570 + /*
53571 + * This fragment of code should initiate 0
53572 + * length transfer in case if it is queued
53573 + * a transfer with size divisible to EPs max
53574 + * packet size and with usb_request zero field
53575 + * is set, which means that after data is transfered,
53576 + * it is also should be transfered
53577 + * a 0 length packet at the end. For Slave and
53578 + * Buffer DMA modes in this case SW has
53579 + * to initiate 2 transfers one with transfer size,
53580 + * and the second with 0 size. For Descriptor
53581 + * DMA mode SW is able to initiate a transfer,
53582 + * which will handle all the packets including
53583 + * the last 0 length.
53584 + */
53585 + ep->dwc_ep.sent_zlp = 0;
53586 + dwc_otg_ep_start_zl_transfer
53587 + (core_if, &ep->dwc_ep);
53588 + } else {
53589 + is_last = 1;
53590 + }
53591 + } else {
53592 + if (ep->dwc_ep.type ==
53593 + DWC_OTG_EP_TYPE_ISOC) {
53594 + req->actual = 0;
53595 + dwc_otg_request_done(ep, req, 0);
53596 +
53597 + ep->dwc_ep.start_xfer_buff = 0;
53598 + ep->dwc_ep.xfer_buff = 0;
53599 + ep->dwc_ep.xfer_len = 0;
53600 +
53601 + /* If there is a request in the queue start it. */
53602 + start_next_request(ep);
53603 + } else
53604 + DWC_WARN
53605 + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
53606 + ep->dwc_ep.num,
53607 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
53608 + deptsiz.b.xfersize,
53609 + deptsiz.b.pktcnt);
53610 + }
53611 + } else {
53612 + dma_desc = ep->dwc_ep.desc_addr;
53613 + byte_count = 0;
53614 + ep->dwc_ep.sent_zlp = 0;
53615 +
53616 +#ifdef DWC_UTE_CFI
53617 + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
53618 + ep->dwc_ep.buff_mode);
53619 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53620 + int residue;
53621 +
53622 + residue = cfi_calc_desc_residue(ep);
53623 + if (residue < 0)
53624 + return;
53625 +
53626 + byte_count = residue;
53627 + } else {
53628 +#endif
53629 + for (i = 0; i < ep->dwc_ep.desc_cnt;
53630 + ++i) {
53631 + desc_sts = dma_desc->status;
53632 + byte_count += desc_sts.b.bytes;
53633 + dma_desc++;
53634 + }
53635 +#ifdef DWC_UTE_CFI
53636 + }
53637 +#endif
53638 + if (byte_count == 0) {
53639 + ep->dwc_ep.xfer_count =
53640 + ep->dwc_ep.total_len;
53641 + is_last = 1;
53642 + } else {
53643 + DWC_WARN("Incomplete transfer\n");
53644 + }
53645 + }
53646 + } else {
53647 + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
53648 + DWC_DEBUGPL(DBG_PCDV,
53649 + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
53650 + ep->dwc_ep.num,
53651 + ep->dwc_ep.is_in ? "IN" : "OUT",
53652 + ep->dwc_ep.xfer_len,
53653 + deptsiz.b.xfersize,
53654 + deptsiz.b.pktcnt);
53655 +
53656 + /* Check if the whole transfer was completed,
53657 + * if no, setup transfer for next portion of data
53658 + */
53659 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53660 + dwc_otg_ep_start_transfer(core_if,
53661 + &ep->dwc_ep);
53662 + } else if (ep->dwc_ep.sent_zlp) {
53663 + /*
53664 + * This fragment of code should initiate 0
53665 + * length trasfer in case if it is queued
53666 + * a trasfer with size divisible to EPs max
53667 + * packet size and with usb_request zero field
53668 + * is set, which means that after data is transfered,
53669 + * it is also should be transfered
53670 + * a 0 length packet at the end. For Slave and
53671 + * Buffer DMA modes in this case SW has
53672 + * to initiate 2 transfers one with transfer size,
53673 + * and the second with 0 size. For Desriptor
53674 + * DMA mode SW is able to initiate a transfer,
53675 + * which will handle all the packets including
53676 + * the last 0 legth.
53677 + */
53678 + ep->dwc_ep.sent_zlp = 0;
53679 + dwc_otg_ep_start_zl_transfer(core_if,
53680 + &ep->dwc_ep);
53681 + } else {
53682 + is_last = 1;
53683 + }
53684 + } else {
53685 + DWC_WARN
53686 + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
53687 + ep->dwc_ep.num,
53688 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
53689 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53690 + }
53691 + }
53692 + } else {
53693 + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
53694 + dev_if->out_ep_regs[ep->dwc_ep.num];
53695 + desc_sts.d32 = 0;
53696 + if (core_if->dma_enable) {
53697 + if (core_if->dma_desc_enable) {
53698 + dma_desc = ep->dwc_ep.desc_addr;
53699 + byte_count = 0;
53700 + ep->dwc_ep.sent_zlp = 0;
53701 +
53702 +#ifdef DWC_UTE_CFI
53703 + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
53704 + ep->dwc_ep.buff_mode);
53705 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53706 + int residue;
53707 + residue = cfi_calc_desc_residue(ep);
53708 + if (residue < 0)
53709 + return;
53710 + byte_count = residue;
53711 + } else {
53712 +#endif
53713 +
53714 + for (i = 0; i < ep->dwc_ep.desc_cnt;
53715 + ++i) {
53716 + desc_sts = dma_desc->status;
53717 + byte_count += desc_sts.b.bytes;
53718 + dma_desc++;
53719 + }
53720 +
53721 +#ifdef DWC_UTE_CFI
53722 + }
53723 +#endif
53724 + /* Checking for interrupt Out transfers with not
53725 + * dword aligned mps sizes
53726 + */
53727 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
53728 + (ep->dwc_ep.maxpacket%4)) {
53729 + ep->dwc_ep.xfer_count =
53730 + ep->dwc_ep.total_len - byte_count;
53731 + if ((ep->dwc_ep.xfer_len %
53732 + ep->dwc_ep.maxpacket)
53733 + && (ep->dwc_ep.xfer_len /
53734 + ep->dwc_ep.maxpacket <
53735 + MAX_DMA_DESC_CNT))
53736 + ep->dwc_ep.xfer_len -=
53737 + (ep->dwc_ep.desc_cnt -
53738 + 1) * ep->dwc_ep.maxpacket +
53739 + ep->dwc_ep.xfer_len %
53740 + ep->dwc_ep.maxpacket;
53741 + else
53742 + ep->dwc_ep.xfer_len -=
53743 + ep->dwc_ep.desc_cnt *
53744 + ep->dwc_ep.maxpacket;
53745 + if (ep->dwc_ep.xfer_len > 0) {
53746 + dwc_otg_ep_start_transfer
53747 + (core_if, &ep->dwc_ep);
53748 + } else {
53749 + is_last = 1;
53750 + }
53751 + } else {
53752 + ep->dwc_ep.xfer_count =
53753 + ep->dwc_ep.total_len - byte_count +
53754 + ((4 -
53755 + (ep->dwc_ep.
53756 + total_len & 0x3)) & 0x3);
53757 + is_last = 1;
53758 + }
53759 + } else {
53760 + deptsiz.d32 = 0;
53761 + deptsiz.d32 =
53762 + DWC_READ_REG32(&out_ep_regs->doeptsiz);
53763 +
53764 + byte_count = (ep->dwc_ep.xfer_len -
53765 + ep->dwc_ep.xfer_count -
53766 + deptsiz.b.xfersize);
53767 + ep->dwc_ep.xfer_buff += byte_count;
53768 + ep->dwc_ep.dma_addr += byte_count;
53769 + ep->dwc_ep.xfer_count += byte_count;
53770 +
53771 + /* Check if the whole transfer was completed,
53772 + * if no, setup transfer for next portion of data
53773 + */
53774 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53775 + dwc_otg_ep_start_transfer(core_if,
53776 + &ep->dwc_ep);
53777 + } else if (ep->dwc_ep.sent_zlp) {
53778 + /*
53779 + * This fragment of code should initiate 0
53780 + * length trasfer in case if it is queued
53781 + * a trasfer with size divisible to EPs max
53782 + * packet size and with usb_request zero field
53783 + * is set, which means that after data is transfered,
53784 + * it is also should be transfered
53785 + * a 0 length packet at the end. For Slave and
53786 + * Buffer DMA modes in this case SW has
53787 + * to initiate 2 transfers one with transfer size,
53788 + * and the second with 0 size. For Desriptor
53789 + * DMA mode SW is able to initiate a transfer,
53790 + * which will handle all the packets including
53791 + * the last 0 legth.
53792 + */
53793 + ep->dwc_ep.sent_zlp = 0;
53794 + dwc_otg_ep_start_zl_transfer(core_if,
53795 + &ep->dwc_ep);
53796 + } else {
53797 + is_last = 1;
53798 + }
53799 + }
53800 + } else {
53801 + /* Check if the whole transfer was completed,
53802 + * if no, setup transfer for next portion of data
53803 + */
53804 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53805 + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
53806 + } else if (ep->dwc_ep.sent_zlp) {
53807 + /*
53808 + * This fragment of code should initiate 0
53809 + * length transfer in case if it is queued
53810 + * a transfer with size divisible to EPs max
53811 + * packet size and with usb_request zero field
53812 + * is set, which means that after data is transfered,
53813 + * it is also should be transfered
53814 + * a 0 length packet at the end. For Slave and
53815 + * Buffer DMA modes in this case SW has
53816 + * to initiate 2 transfers one with transfer size,
53817 + * and the second with 0 size. For Descriptor
53818 + * DMA mode SW is able to initiate a transfer,
53819 + * which will handle all the packets including
53820 + * the last 0 length.
53821 + */
53822 + ep->dwc_ep.sent_zlp = 0;
53823 + dwc_otg_ep_start_zl_transfer(core_if,
53824 + &ep->dwc_ep);
53825 + } else {
53826 + is_last = 1;
53827 + }
53828 + }
53829 +
53830 + DWC_DEBUGPL(DBG_PCDV,
53831 + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
53832 + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
53833 + ep->dwc_ep.is_in ? "IN" : "OUT",
53834 + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
53835 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53836 + }
53837 +
53838 + /* Complete the request */
53839 + if (is_last) {
53840 +#ifdef DWC_UTE_CFI
53841 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53842 + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
53843 + } else {
53844 +#endif
53845 + req->actual = ep->dwc_ep.xfer_count;
53846 +#ifdef DWC_UTE_CFI
53847 + }
53848 +#endif
53849 + if (req->dw_align_buf) {
53850 + if (!ep->dwc_ep.is_in) {
53851 + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
53852 + }
53853 + DWC_DMA_FREE(dev, req->length, req->dw_align_buf,
53854 + req->dw_align_buf_dma);
53855 + }
53856 +
53857 + dwc_otg_request_done(ep, req, 0);
53858 +
53859 + ep->dwc_ep.start_xfer_buff = 0;
53860 + ep->dwc_ep.xfer_buff = 0;
53861 + ep->dwc_ep.xfer_len = 0;
53862 +
53863 + /* If there is a request in the queue start it. */
53864 + start_next_request(ep);
53865 + }
53866 +}
53867 +
53868 +#ifdef DWC_EN_ISOC
53869 +
53870 +/**
53871 + * This function BNA interrupt for Isochronous EPs
53872 + *
53873 + */
53874 +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
53875 +{
53876 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
53877 + volatile uint32_t *addr;
53878 + depctl_data_t depctl = {.d32 = 0 };
53879 + dwc_otg_pcd_t *pcd = ep->pcd;
53880 + dwc_otg_dev_dma_desc_t *dma_desc;
53881 + int i;
53882 +
53883 + dma_desc =
53884 + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
53885 +
53886 + if (dwc_ep->is_in) {
53887 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53888 + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
53889 + sts.d32 = dma_desc->status.d32;
53890 + sts.b_iso_in.bs = BS_HOST_READY;
53891 + dma_desc->status.d32 = sts.d32;
53892 + }
53893 + } else {
53894 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53895 + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
53896 + sts.d32 = dma_desc->status.d32;
53897 + sts.b_iso_out.bs = BS_HOST_READY;
53898 + dma_desc->status.d32 = sts.d32;
53899 + }
53900 + }
53901 +
53902 + if (dwc_ep->is_in == 0) {
53903 + addr =
53904 + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
53905 + num]->doepctl;
53906 + } else {
53907 + addr =
53908 + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
53909 + }
53910 + depctl.b.epena = 1;
53911 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
53912 +}
53913 +
53914 +/**
53915 + * This function sets latest iso packet information(non-PTI mode)
53916 + *
53917 + * @param core_if Programming view of DWC_otg controller.
53918 + * @param ep The EP to start the transfer on.
53919 + *
53920 + */
53921 +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
53922 +{
53923 + deptsiz_data_t deptsiz = {.d32 = 0 };
53924 + dma_addr_t dma_addr;
53925 + uint32_t offset;
53926 +
53927 + if (ep->proc_buf_num)
53928 + dma_addr = ep->dma_addr1;
53929 + else
53930 + dma_addr = ep->dma_addr0;
53931 +
53932 + if (ep->is_in) {
53933 + deptsiz.d32 =
53934 + DWC_READ_REG32(&core_if->dev_if->
53935 + in_ep_regs[ep->num]->dieptsiz);
53936 + offset = ep->data_per_frame;
53937 + } else {
53938 + deptsiz.d32 =
53939 + DWC_READ_REG32(&core_if->dev_if->
53940 + out_ep_regs[ep->num]->doeptsiz);
53941 + offset =
53942 + ep->data_per_frame +
53943 + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
53944 + }
53945 +
53946 + if (!deptsiz.b.xfersize) {
53947 + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
53948 + ep->pkt_info[ep->cur_pkt].offset =
53949 + ep->cur_pkt_dma_addr - dma_addr;
53950 + ep->pkt_info[ep->cur_pkt].status = 0;
53951 + } else {
53952 + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
53953 + ep->pkt_info[ep->cur_pkt].offset =
53954 + ep->cur_pkt_dma_addr - dma_addr;
53955 + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
53956 + }
53957 + ep->cur_pkt_addr += offset;
53958 + ep->cur_pkt_dma_addr += offset;
53959 + ep->cur_pkt++;
53960 +}
53961 +
53962 +/**
53963 + * This function sets latest iso packet information(DDMA mode)
53964 + *
53965 + * @param core_if Programming view of DWC_otg controller.
53966 + * @param dwc_ep The EP to start the transfer on.
53967 + *
53968 + */
53969 +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
53970 + dwc_ep_t * dwc_ep)
53971 +{
53972 + dwc_otg_dev_dma_desc_t *dma_desc;
53973 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53974 + iso_pkt_info_t *iso_packet;
53975 + uint32_t data_per_desc;
53976 + uint32_t offset;
53977 + int i, j;
53978 +
53979 + iso_packet = dwc_ep->pkt_info;
53980 +
53981 + /** Reinit closed DMA Descriptors*/
53982 + /** ISO OUT EP */
53983 + if (dwc_ep->is_in == 0) {
53984 + dma_desc =
53985 + dwc_ep->iso_desc_addr +
53986 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
53987 + offset = 0;
53988 +
53989 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
53990 + i += dwc_ep->pkt_per_frm) {
53991 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
53992 + data_per_desc =
53993 + ((j + 1) * dwc_ep->maxpacket >
53994 + dwc_ep->
53995 + data_per_frame) ? dwc_ep->data_per_frame -
53996 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
53997 + data_per_desc +=
53998 + (data_per_desc % 4) ? (4 -
53999 + data_per_desc %
54000 + 4) : 0;
54001 +
54002 + sts.d32 = dma_desc->status.d32;
54003 +
54004 + /* Write status in iso_packet_decsriptor */
54005 + iso_packet->status =
54006 + sts.b_iso_out.rxsts +
54007 + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54008 + if (iso_packet->status) {
54009 + iso_packet->status = -DWC_E_NO_DATA;
54010 + }
54011 +
54012 + /* Received data length */
54013 + if (!sts.b_iso_out.rxbytes) {
54014 + iso_packet->length =
54015 + data_per_desc -
54016 + sts.b_iso_out.rxbytes;
54017 + } else {
54018 + iso_packet->length =
54019 + data_per_desc -
54020 + sts.b_iso_out.rxbytes + (4 -
54021 + dwc_ep->data_per_frame
54022 + % 4);
54023 + }
54024 +
54025 + iso_packet->offset = offset;
54026 +
54027 + offset += data_per_desc;
54028 + dma_desc++;
54029 + iso_packet++;
54030 + }
54031 + }
54032 +
54033 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
54034 + data_per_desc =
54035 + ((j + 1) * dwc_ep->maxpacket >
54036 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54037 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54038 + data_per_desc +=
54039 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54040 +
54041 + sts.d32 = dma_desc->status.d32;
54042 +
54043 + /* Write status in iso_packet_decsriptor */
54044 + iso_packet->status =
54045 + sts.b_iso_out.rxsts +
54046 + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54047 + if (iso_packet->status) {
54048 + iso_packet->status = -DWC_E_NO_DATA;
54049 + }
54050 +
54051 + /* Received data length */
54052 + iso_packet->length =
54053 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
54054 +
54055 + iso_packet->offset = offset;
54056 +
54057 + offset += data_per_desc;
54058 + iso_packet++;
54059 + dma_desc++;
54060 + }
54061 +
54062 + sts.d32 = dma_desc->status.d32;
54063 +
54064 + /* Write status in iso_packet_decsriptor */
54065 + iso_packet->status =
54066 + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54067 + if (iso_packet->status) {
54068 + iso_packet->status = -DWC_E_NO_DATA;
54069 + }
54070 + /* Received data length */
54071 + if (!sts.b_iso_out.rxbytes) {
54072 + iso_packet->length =
54073 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
54074 + } else {
54075 + iso_packet->length =
54076 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
54077 + (4 - dwc_ep->data_per_frame % 4);
54078 + }
54079 +
54080 + iso_packet->offset = offset;
54081 + } else {
54082 +/** ISO IN EP */
54083 +
54084 + dma_desc =
54085 + dwc_ep->iso_desc_addr +
54086 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54087 +
54088 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
54089 + sts.d32 = dma_desc->status.d32;
54090 +
54091 + /* Write status in iso packet descriptor */
54092 + iso_packet->status =
54093 + sts.b_iso_in.txsts +
54094 + (sts.b_iso_in.bs ^ BS_DMA_DONE);
54095 + if (iso_packet->status != 0) {
54096 + iso_packet->status = -DWC_E_NO_DATA;
54097 +
54098 + }
54099 + /* Bytes has been transfered */
54100 + iso_packet->length =
54101 + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
54102 +
54103 + dma_desc++;
54104 + iso_packet++;
54105 + }
54106 +
54107 + sts.d32 = dma_desc->status.d32;
54108 + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
54109 + sts.d32 = dma_desc->status.d32;
54110 + }
54111 +
54112 + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
54113 + iso_packet->status =
54114 + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
54115 + if (iso_packet->status != 0) {
54116 + iso_packet->status = -DWC_E_NO_DATA;
54117 + }
54118 +
54119 + /* Bytes has been transfered */
54120 + iso_packet->length =
54121 + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
54122 + }
54123 +}
54124 +
54125 +/**
54126 + * This function reinitialize DMA Descriptors for Isochronous transfer
54127 + *
54128 + * @param core_if Programming view of DWC_otg controller.
54129 + * @param dwc_ep The EP to start the transfer on.
54130 + *
54131 + */
54132 +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
54133 +{
54134 + int i, j;
54135 + dwc_otg_dev_dma_desc_t *dma_desc;
54136 + dma_addr_t dma_ad;
54137 + volatile uint32_t *addr;
54138 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54139 + uint32_t data_per_desc;
54140 +
54141 + if (dwc_ep->is_in == 0) {
54142 + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
54143 + } else {
54144 + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
54145 + }
54146 +
54147 + if (dwc_ep->proc_buf_num == 0) {
54148 + /** Buffer 0 descriptors setup */
54149 + dma_ad = dwc_ep->dma_addr0;
54150 + } else {
54151 + /** Buffer 1 descriptors setup */
54152 + dma_ad = dwc_ep->dma_addr1;
54153 + }
54154 +
54155 + /** Reinit closed DMA Descriptors*/
54156 + /** ISO OUT EP */
54157 + if (dwc_ep->is_in == 0) {
54158 + dma_desc =
54159 + dwc_ep->iso_desc_addr +
54160 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54161 +
54162 + sts.b_iso_out.bs = BS_HOST_READY;
54163 + sts.b_iso_out.rxsts = 0;
54164 + sts.b_iso_out.l = 0;
54165 + sts.b_iso_out.sp = 0;
54166 + sts.b_iso_out.ioc = 0;
54167 + sts.b_iso_out.pid = 0;
54168 + sts.b_iso_out.framenum = 0;
54169 +
54170 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
54171 + i += dwc_ep->pkt_per_frm) {
54172 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
54173 + data_per_desc =
54174 + ((j + 1) * dwc_ep->maxpacket >
54175 + dwc_ep->
54176 + data_per_frame) ? dwc_ep->data_per_frame -
54177 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54178 + data_per_desc +=
54179 + (data_per_desc % 4) ? (4 -
54180 + data_per_desc %
54181 + 4) : 0;
54182 + sts.b_iso_out.rxbytes = data_per_desc;
54183 + dma_desc->buf = dma_ad;
54184 + dma_desc->status.d32 = sts.d32;
54185 +
54186 + dma_ad += data_per_desc;
54187 + dma_desc++;
54188 + }
54189 + }
54190 +
54191 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
54192 +
54193 + data_per_desc =
54194 + ((j + 1) * dwc_ep->maxpacket >
54195 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54196 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54197 + data_per_desc +=
54198 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54199 + sts.b_iso_out.rxbytes = data_per_desc;
54200 +
54201 + dma_desc->buf = dma_ad;
54202 + dma_desc->status.d32 = sts.d32;
54203 +
54204 + dma_desc++;
54205 + dma_ad += data_per_desc;
54206 + }
54207 +
54208 + sts.b_iso_out.ioc = 1;
54209 + sts.b_iso_out.l = dwc_ep->proc_buf_num;
54210 +
54211 + data_per_desc =
54212 + ((j + 1) * dwc_ep->maxpacket >
54213 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54214 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54215 + data_per_desc +=
54216 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54217 + sts.b_iso_out.rxbytes = data_per_desc;
54218 +
54219 + dma_desc->buf = dma_ad;
54220 + dma_desc->status.d32 = sts.d32;
54221 + } else {
54222 +/** ISO IN EP */
54223 +
54224 + dma_desc =
54225 + dwc_ep->iso_desc_addr +
54226 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54227 +
54228 + sts.b_iso_in.bs = BS_HOST_READY;
54229 + sts.b_iso_in.txsts = 0;
54230 + sts.b_iso_in.sp = 0;
54231 + sts.b_iso_in.ioc = 0;
54232 + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
54233 + sts.b_iso_in.framenum = dwc_ep->next_frame;
54234 + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
54235 + sts.b_iso_in.l = 0;
54236 +
54237 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
54238 + dma_desc->buf = dma_ad;
54239 + dma_desc->status.d32 = sts.d32;
54240 +
54241 + sts.b_iso_in.framenum += dwc_ep->bInterval;
54242 + dma_ad += dwc_ep->data_per_frame;
54243 + dma_desc++;
54244 + }
54245 +
54246 + sts.b_iso_in.ioc = 1;
54247 + sts.b_iso_in.l = dwc_ep->proc_buf_num;
54248 +
54249 + dma_desc->buf = dma_ad;
54250 + dma_desc->status.d32 = sts.d32;
54251 +
54252 + dwc_ep->next_frame =
54253 + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
54254 + }
54255 + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
54256 +}
54257 +
54258 +/**
54259 + * This function is to handle Iso EP transfer complete interrupt
54260 + * in case Iso out packet was dropped
54261 + *
54262 + * @param core_if Programming view of DWC_otg controller.
54263 + * @param dwc_ep The EP for wihich transfer complete was asserted
54264 + *
54265 + */
54266 +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
54267 + dwc_ep_t * dwc_ep)
54268 +{
54269 + uint32_t dma_addr;
54270 + uint32_t drp_pkt;
54271 + uint32_t drp_pkt_cnt;
54272 + deptsiz_data_t deptsiz = {.d32 = 0 };
54273 + depctl_data_t depctl = {.d32 = 0 };
54274 + int i;
54275 +
54276 + deptsiz.d32 =
54277 + DWC_READ_REG32(&core_if->dev_if->
54278 + out_ep_regs[dwc_ep->num]->doeptsiz);
54279 +
54280 + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
54281 + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
54282 +
54283 + /* Setting dropped packets status */
54284 + for (i = 0; i < drp_pkt_cnt; ++i) {
54285 + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
54286 + drp_pkt++;
54287 + deptsiz.b.pktcnt--;
54288 + }
54289 +
54290 + if (deptsiz.b.pktcnt > 0) {
54291 + deptsiz.b.xfersize =
54292 + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
54293 + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
54294 + } else {
54295 + deptsiz.b.xfersize = 0;
54296 + deptsiz.b.pktcnt = 0;
54297 + }
54298 +
54299 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
54300 + deptsiz.d32);
54301 +
54302 + if (deptsiz.b.pktcnt > 0) {
54303 + if (dwc_ep->proc_buf_num) {
54304 + dma_addr =
54305 + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
54306 + deptsiz.b.xfersize;
54307 + } else {
54308 + dma_addr =
54309 + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
54310 + deptsiz.b.xfersize;;
54311 + }
54312 +
54313 + DWC_WRITE_REG32(&core_if->dev_if->
54314 + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
54315 +
54316 + /** Re-enable endpoint, clear nak */
54317 + depctl.d32 = 0;
54318 + depctl.b.epena = 1;
54319 + depctl.b.cnak = 1;
54320 +
54321 + DWC_MODIFY_REG32(&core_if->dev_if->
54322 + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
54323 + depctl.d32);
54324 + return 0;
54325 + } else {
54326 + return 1;
54327 + }
54328 +}
54329 +
54330 +/**
54331 + * This function sets iso packets information(PTI mode)
54332 + *
54333 + * @param core_if Programming view of DWC_otg controller.
54334 + * @param ep The EP to start the transfer on.
54335 + *
54336 + */
54337 +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
54338 +{
54339 + int i, j;
54340 + dma_addr_t dma_ad;
54341 + iso_pkt_info_t *packet_info = ep->pkt_info;
54342 + uint32_t offset;
54343 + uint32_t frame_data;
54344 + deptsiz_data_t deptsiz;
54345 +
54346 + if (ep->proc_buf_num == 0) {
54347 + /** Buffer 0 descriptors setup */
54348 + dma_ad = ep->dma_addr0;
54349 + } else {
54350 + /** Buffer 1 descriptors setup */
54351 + dma_ad = ep->dma_addr1;
54352 + }
54353 +
54354 + if (ep->is_in) {
54355 + deptsiz.d32 =
54356 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
54357 + dieptsiz);
54358 + } else {
54359 + deptsiz.d32 =
54360 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
54361 + doeptsiz);
54362 + }
54363 +
54364 + if (!deptsiz.b.xfersize) {
54365 + offset = 0;
54366 + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
54367 + frame_data = ep->data_per_frame;
54368 + for (j = 0; j < ep->pkt_per_frm; ++j) {
54369 +
54370 + /* Packet status - is not set as initially
54371 + * it is set to 0 and if packet was sent
54372 + successfully, status field will remain 0*/
54373 +
54374 + /* Bytes has been transfered */
54375 + packet_info->length =
54376 + (ep->maxpacket <
54377 + frame_data) ? ep->maxpacket : frame_data;
54378 +
54379 + /* Received packet offset */
54380 + packet_info->offset = offset;
54381 + offset += packet_info->length;
54382 + frame_data -= packet_info->length;
54383 +
54384 + packet_info++;
54385 + }
54386 + }
54387 + return 1;
54388 + } else {
54389 + /* This is a workaround for in case of Transfer Complete with
54390 + * PktDrpSts interrupts merging - in this case Transfer complete
54391 + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
54392 + * set and with DOEPTSIZ register non zero. Investigations showed,
54393 + * that this happens when Out packet is dropped, but because of
54394 + * interrupts merging during first interrupt handling PktDrpSts
54395 + * bit is cleared and for next merged interrupts it is not reset.
54396 + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
54397 + */
54398 + if (ep->is_in) {
54399 + return 1;
54400 + } else {
54401 + return handle_iso_out_pkt_dropped(core_if, ep);
54402 + }
54403 + }
54404 +}
54405 +
54406 +/**
54407 + * This function is to handle Iso EP transfer complete interrupt
54408 + *
54409 + * @param pcd The PCD
54410 + * @param ep The EP for which transfer complete was asserted
54411 + *
54412 + */
54413 +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
54414 +{
54415 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
54416 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54417 + uint8_t is_last = 0;
54418 +
54419 + if (ep->dwc_ep.next_frame == 0xffffffff) {
54420 + DWC_WARN("Next frame is not set!\n");
54421 + return;
54422 + }
54423 +
54424 + if (core_if->dma_enable) {
54425 + if (core_if->dma_desc_enable) {
54426 + set_ddma_iso_pkts_info(core_if, dwc_ep);
54427 + reinit_ddma_iso_xfer(core_if, dwc_ep);
54428 + is_last = 1;
54429 + } else {
54430 + if (core_if->pti_enh_enable) {
54431 + if (set_iso_pkts_info(core_if, dwc_ep)) {
54432 + dwc_ep->proc_buf_num =
54433 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
54434 + dwc_otg_iso_ep_start_buf_transfer
54435 + (core_if, dwc_ep);
54436 + is_last = 1;
54437 + }
54438 + } else {
54439 + set_current_pkt_info(core_if, dwc_ep);
54440 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
54441 + is_last = 1;
54442 + dwc_ep->cur_pkt = 0;
54443 + dwc_ep->proc_buf_num =
54444 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
54445 + if (dwc_ep->proc_buf_num) {
54446 + dwc_ep->cur_pkt_addr =
54447 + dwc_ep->xfer_buff1;
54448 + dwc_ep->cur_pkt_dma_addr =
54449 + dwc_ep->dma_addr1;
54450 + } else {
54451 + dwc_ep->cur_pkt_addr =
54452 + dwc_ep->xfer_buff0;
54453 + dwc_ep->cur_pkt_dma_addr =
54454 + dwc_ep->dma_addr0;
54455 + }
54456 +
54457 + }
54458 + dwc_otg_iso_ep_start_frm_transfer(core_if,
54459 + dwc_ep);
54460 + }
54461 + }
54462 + } else {
54463 + set_current_pkt_info(core_if, dwc_ep);
54464 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
54465 + is_last = 1;
54466 + dwc_ep->cur_pkt = 0;
54467 + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
54468 + if (dwc_ep->proc_buf_num) {
54469 + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
54470 + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
54471 + } else {
54472 + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
54473 + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
54474 + }
54475 +
54476 + }
54477 + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
54478 + }
54479 + if (is_last)
54480 + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
54481 +}
54482 +#endif /* DWC_EN_ISOC */
54483 +
54484 +/**
54485 + * This function handle BNA interrupt for Non Isochronous EPs
54486 + *
54487 + */
54488 +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
54489 +{
54490 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54491 + volatile uint32_t *addr;
54492 + depctl_data_t depctl = {.d32 = 0 };
54493 + dwc_otg_pcd_t *pcd = ep->pcd;
54494 + dwc_otg_dev_dma_desc_t *dma_desc;
54495 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54496 + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
54497 + int i, start;
54498 +
54499 + if (!dwc_ep->desc_cnt)
54500 + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
54501 + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
54502 +
54503 + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
54504 + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
54505 + uint32_t doepdma;
54506 + dwc_otg_dev_out_ep_regs_t *out_regs =
54507 + core_if->dev_if->out_ep_regs[dwc_ep->num];
54508 + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
54509 + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
54510 + dma_desc = &(dwc_ep->desc_addr[start]);
54511 + } else {
54512 + start = 0;
54513 + dma_desc = dwc_ep->desc_addr;
54514 + }
54515 +
54516 +
54517 + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
54518 + sts.d32 = dma_desc->status.d32;
54519 + sts.b.bs = BS_HOST_READY;
54520 + dma_desc->status.d32 = sts.d32;
54521 + }
54522 +
54523 + if (dwc_ep->is_in == 0) {
54524 + addr =
54525 + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
54526 + doepctl;
54527 + } else {
54528 + addr =
54529 + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
54530 + }
54531 + depctl.b.epena = 1;
54532 + depctl.b.cnak = 1;
54533 + DWC_MODIFY_REG32(addr, 0, depctl.d32);
54534 +}
54535 +
54536 +/**
54537 + * This function handles EP0 Control transfers.
54538 + *
54539 + * The state of the control transfers are tracked in
54540 + * <code>ep0state</code>.
54541 + */
54542 +static void handle_ep0(dwc_otg_pcd_t * pcd)
54543 +{
54544 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
54545 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
54546 + dev_dma_desc_sts_t desc_sts;
54547 + deptsiz0_data_t deptsiz;
54548 + uint32_t byte_count;
54549 +
54550 +#ifdef DEBUG_EP0
54551 + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
54552 + print_ep0_state(pcd);
54553 +#endif
54554 +
54555 +// DWC_PRINTF("HANDLE EP0\n");
54556 +
54557 + switch (pcd->ep0state) {
54558 + case EP0_DISCONNECT:
54559 + break;
54560 +
54561 + case EP0_IDLE:
54562 + pcd->request_config = 0;
54563 +
54564 + pcd_setup(pcd);
54565 + break;
54566 +
54567 + case EP0_IN_DATA_PHASE:
54568 +#ifdef DEBUG_EP0
54569 + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
54570 + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
54571 + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
54572 +#endif
54573 +
54574 + if (core_if->dma_enable != 0) {
54575 + /*
54576 + * For EP0 we can only program 1 packet at a time so we
54577 + * need to do the make calculations after each complete.
54578 + * Call write_packet to make the calculations, as in
54579 + * slave mode, and use those values to determine if we
54580 + * can complete.
54581 + */
54582 + if (core_if->dma_desc_enable == 0) {
54583 + deptsiz.d32 =
54584 + DWC_READ_REG32(&core_if->
54585 + dev_if->in_ep_regs[0]->
54586 + dieptsiz);
54587 + byte_count =
54588 + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
54589 + } else {
54590 + desc_sts =
54591 + core_if->dev_if->in_desc_addr->status;
54592 + byte_count =
54593 + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
54594 + }
54595 + ep0->dwc_ep.xfer_count += byte_count;
54596 + ep0->dwc_ep.xfer_buff += byte_count;
54597 + ep0->dwc_ep.dma_addr += byte_count;
54598 + }
54599 + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
54600 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54601 + &ep0->dwc_ep);
54602 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
54603 + } else if (ep0->dwc_ep.sent_zlp) {
54604 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54605 + &ep0->dwc_ep);
54606 + ep0->dwc_ep.sent_zlp = 0;
54607 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
54608 + } else {
54609 + ep0_complete_request(ep0);
54610 + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
54611 + }
54612 + break;
54613 + case EP0_OUT_DATA_PHASE:
54614 +#ifdef DEBUG_EP0
54615 + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
54616 + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
54617 + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
54618 +#endif
54619 + if (core_if->dma_enable != 0) {
54620 + if (core_if->dma_desc_enable == 0) {
54621 + deptsiz.d32 =
54622 + DWC_READ_REG32(&core_if->
54623 + dev_if->out_ep_regs[0]->
54624 + doeptsiz);
54625 + byte_count =
54626 + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
54627 + } else {
54628 + desc_sts =
54629 + core_if->dev_if->out_desc_addr->status;
54630 + byte_count =
54631 + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
54632 + }
54633 + ep0->dwc_ep.xfer_count += byte_count;
54634 + ep0->dwc_ep.xfer_buff += byte_count;
54635 + ep0->dwc_ep.dma_addr += byte_count;
54636 + }
54637 + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
54638 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54639 + &ep0->dwc_ep);
54640 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
54641 + } else if (ep0->dwc_ep.sent_zlp) {
54642 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54643 + &ep0->dwc_ep);
54644 + ep0->dwc_ep.sent_zlp = 0;
54645 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
54646 + } else {
54647 + ep0_complete_request(ep0);
54648 + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
54649 + }
54650 + break;
54651 +
54652 + case EP0_IN_STATUS_PHASE:
54653 + case EP0_OUT_STATUS_PHASE:
54654 + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
54655 + ep0_complete_request(ep0);
54656 + pcd->ep0state = EP0_IDLE;
54657 + ep0->stopped = 1;
54658 + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
54659 +
54660 + /* Prepare for more SETUP Packets */
54661 + if (core_if->dma_enable) {
54662 + ep0_out_start(core_if, pcd);
54663 + }
54664 + break;
54665 +
54666 + case EP0_STALL:
54667 + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
54668 + break;
54669 + }
54670 +#ifdef DEBUG_EP0
54671 + print_ep0_state(pcd);
54672 +#endif
54673 +}
54674 +
54675 +/**
54676 + * Restart transfer
54677 + */
54678 +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
54679 +{
54680 + dwc_otg_core_if_t *core_if;
54681 + dwc_otg_dev_if_t *dev_if;
54682 + deptsiz_data_t dieptsiz = {.d32 = 0 };
54683 + dwc_otg_pcd_ep_t *ep;
54684 +
54685 + ep = get_in_ep(pcd, epnum);
54686 +
54687 +#ifdef DWC_EN_ISOC
54688 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
54689 + return;
54690 + }
54691 +#endif /* DWC_EN_ISOC */
54692 +
54693 + core_if = GET_CORE_IF(pcd);
54694 + dev_if = core_if->dev_if;
54695 +
54696 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
54697 +
54698 + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
54699 + " stopped=%d\n", ep->dwc_ep.xfer_buff,
54700 + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
54701 + /*
54702 + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
54703 + */
54704 + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
54705 + ep->dwc_ep.start_xfer_buff != 0) {
54706 + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
54707 + ep->dwc_ep.xfer_count = 0;
54708 + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
54709 + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
54710 + } else {
54711 + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
54712 + /* convert packet size to dwords. */
54713 + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
54714 + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
54715 + }
54716 + ep->stopped = 0;
54717 + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
54718 + "xfer_len=%0x stopped=%d\n",
54719 + ep->dwc_ep.xfer_buff,
54720 + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
54721 + ep->stopped);
54722 + if (epnum == 0) {
54723 + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
54724 + } else {
54725 + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
54726 + }
54727 + }
54728 +}
54729 +
54730 +/*
54731 + * This function create new nextep sequnce based on Learn Queue.
54732 + *
54733 + * @param core_if Programming view of DWC_otg controller
54734 + */
54735 +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
54736 +{
54737 + dwc_otg_device_global_regs_t *dev_global_regs =
54738 + core_if->dev_if->dev_global_regs;
54739 + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
54740 + /* Number of Token Queue Registers */
54741 + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
54742 + dtknq1_data_t dtknqr1;
54743 + uint32_t in_tkn_epnums[4];
54744 + uint8_t seqnum[MAX_EPS_CHANNELS];
54745 + uint8_t intkn_seq[1 << 5];
54746 + grstctl_t resetctl = {.d32 = 0 };
54747 + uint8_t temp;
54748 + int ndx = 0;
54749 + int start = 0;
54750 + int end = 0;
54751 + int sort_done = 0;
54752 + int i = 0;
54753 + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
54754 +
54755 +
54756 + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
54757 +
54758 + /* Read the DTKNQ Registers */
54759 + for (i = 0; i < DTKNQ_REG_CNT; i++) {
54760 + in_tkn_epnums[i] = DWC_READ_REG32(addr);
54761 + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
54762 + in_tkn_epnums[i]);
54763 + if (addr == &dev_global_regs->dvbusdis) {
54764 + addr = &dev_global_regs->dtknqr3_dthrctl;
54765 + } else {
54766 + ++addr;
54767 + }
54768 +
54769 + }
54770 +
54771 + /* Copy the DTKNQR1 data to the bit field. */
54772 + dtknqr1.d32 = in_tkn_epnums[0];
54773 + if (dtknqr1.b.wrap_bit) {
54774 + ndx = dtknqr1.b.intknwptr;
54775 + end = ndx -1;
54776 + if (end < 0)
54777 + end = TOKEN_Q_DEPTH -1;
54778 + } else {
54779 + ndx = 0;
54780 + end = dtknqr1.b.intknwptr -1;
54781 + if (end < 0)
54782 + end = 0;
54783 + }
54784 + start = ndx;
54785 +
54786 + /* Fill seqnum[] by initial values: EP number + 31 */
54787 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
54788 + seqnum[i] = i +31;
54789 + }
54790 +
54791 + /* Fill intkn_seq[] from in_tkn_epnums[0] */
54792 + for (i=0; i < 6; i++)
54793 + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
54794 +
54795 + if (TOKEN_Q_DEPTH > 6) {
54796 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54797 + for (i=6; i < 14; i++)
54798 + intkn_seq[i] =
54799 + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
54800 + }
54801 +
54802 + if (TOKEN_Q_DEPTH > 14) {
54803 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54804 + for (i=14; i < 22; i++)
54805 + intkn_seq[i] =
54806 + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
54807 + }
54808 +
54809 + if (TOKEN_Q_DEPTH > 22) {
54810 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54811 + for (i=22; i < 30; i++)
54812 + intkn_seq[i] =
54813 + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
54814 + }
54815 +
54816 + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
54817 + start, end);
54818 + for (i=0; i<TOKEN_Q_DEPTH; i++)
54819 + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
54820 +
54821 + /* Update seqnum based on intkn_seq[] */
54822 + i = 0;
54823 + do {
54824 + seqnum[intkn_seq[ndx]] = i;
54825 + ndx++;
54826 + i++;
54827 + if (ndx == TOKEN_Q_DEPTH)
54828 + ndx = 0;
54829 + } while ( i < TOKEN_Q_DEPTH );
54830 +
54831 + /* Mark non active EP's in seqnum[] by 0xff */
54832 + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
54833 + if (core_if->nextep_seq[i] == 0xff )
54834 + seqnum[i] = 0xff;
54835 + }
54836 +
54837 + /* Sort seqnum[] */
54838 + sort_done = 0;
54839 + while (!sort_done) {
54840 + sort_done = 1;
54841 + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
54842 + if (seqnum[i] > seqnum[i+1]) {
54843 + temp = seqnum[i];
54844 + seqnum[i] = seqnum[i+1];
54845 + seqnum[i+1] = temp;
54846 + sort_done = 0;
54847 + }
54848 + }
54849 + }
54850 +
54851 + ndx = start + seqnum[0];
54852 + if (ndx >= TOKEN_Q_DEPTH)
54853 + ndx = ndx % TOKEN_Q_DEPTH;
54854 + core_if->first_in_nextep_seq = intkn_seq[ndx];
54855 +
54856 + /* Update seqnum[] by EP numbers */
54857 + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
54858 + ndx = start + i;
54859 + if (seqnum[i] < 31) {
54860 + ndx = start + seqnum[i];
54861 + if (ndx >= TOKEN_Q_DEPTH)
54862 + ndx = ndx % TOKEN_Q_DEPTH;
54863 + seqnum[i] = intkn_seq[ndx];
54864 + } else {
54865 + if (seqnum[i] < 0xff) {
54866 + seqnum[i] = seqnum[i] - 31;
54867 + } else {
54868 + break;
54869 + }
54870 + }
54871 + }
54872 +
54873 + /* Update nextep_seq[] based on seqnum[] */
54874 + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
54875 + if (seqnum[i] != 0xff) {
54876 + if (seqnum[i+1] != 0xff) {
54877 + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
54878 + } else {
54879 + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
54880 + break;
54881 + }
54882 + } else {
54883 + break;
54884 + }
54885 + }
54886 +
54887 + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
54888 + __func__, core_if->first_in_nextep_seq);
54889 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
54890 + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
54891 + }
54892 +
54893 + /* Flush the Learning Queue */
54894 + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
54895 + resetctl.b.intknqflsh = 1;
54896 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
54897 +
54898 +
54899 +}
54900 +
54901 +/**
54902 + * handle the IN EP disable interrupt.
54903 + */
54904 +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
54905 + const uint32_t epnum)
54906 +{
54907 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
54908 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
54909 + deptsiz_data_t dieptsiz = {.d32 = 0 };
54910 + dctl_data_t dctl = {.d32 = 0 };
54911 + dwc_otg_pcd_ep_t *ep;
54912 + dwc_ep_t *dwc_ep;
54913 + gintmsk_data_t gintmsk_data;
54914 + depctl_data_t depctl;
54915 + uint32_t diepdma;
54916 + uint32_t remain_to_transfer = 0;
54917 + uint8_t i;
54918 + uint32_t xfer_size;
54919 +
54920 + ep = get_in_ep(pcd, epnum);
54921 + dwc_ep = &ep->dwc_ep;
54922 +
54923 + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
54924 + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
54925 + complete_ep(ep);
54926 + return;
54927 + }
54928 +
54929 + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
54930 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
54931 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
54932 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
54933 +
54934 + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
54935 + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
54936 +
54937 + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
54938 + if (ep->stopped) {
54939 + if (core_if->en_multiple_tx_fifo)
54940 + /* Flush the Tx FIFO */
54941 + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
54942 + /* Clear the Global IN NP NAK */
54943 + dctl.d32 = 0;
54944 + dctl.b.cgnpinnak = 1;
54945 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
54946 + /* Restart the transaction */
54947 + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
54948 + restart_transfer(pcd, epnum);
54949 + }
54950 + } else {
54951 + /* Restart the transaction */
54952 + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
54953 + restart_transfer(pcd, epnum);
54954 + }
54955 + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
54956 + }
54957 + return;
54958 + }
54959 +
54960 + if (core_if->start_predict > 2) { // NP IN EP
54961 + core_if->start_predict--;
54962 + return;
54963 + }
54964 +
54965 + core_if->start_predict--;
54966 +
54967 + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
54968 +
54969 + predict_nextep_seq(core_if);
54970 +
54971 + /* Update all active IN EP's NextEP field based of nextep_seq[] */
54972 + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
54973 + depctl.d32 =
54974 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
54975 + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
54976 + depctl.b.nextep = core_if->nextep_seq[i];
54977 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
54978 + }
54979 + }
54980 + /* Flush Shared NP TxFIFO */
54981 + dwc_otg_flush_tx_fifo(core_if, 0);
54982 + /* Rewind buffers */
54983 + if (!core_if->dma_desc_enable) {
54984 + i = core_if->first_in_nextep_seq;
54985 + do {
54986 + ep = get_in_ep(pcd, i);
54987 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
54988 + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
54989 + if (xfer_size > ep->dwc_ep.maxxfer)
54990 + xfer_size = ep->dwc_ep.maxxfer;
54991 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
54992 + if (dieptsiz.b.pktcnt != 0) {
54993 + if (xfer_size == 0) {
54994 + remain_to_transfer = 0;
54995 + } else {
54996 + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
54997 + remain_to_transfer =
54998 + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
54999 + } else {
55000 + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
55001 + + (xfer_size % ep->dwc_ep.maxpacket);
55002 + }
55003 + }
55004 + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
55005 + dieptsiz.b.xfersize = remain_to_transfer;
55006 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
55007 + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
55008 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
55009 + }
55010 + i = core_if->nextep_seq[i];
55011 + } while (i != core_if->first_in_nextep_seq);
55012 + } else { // dma_desc_enable
55013 + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
55014 + }
55015 +
55016 + /* Restart transfers in predicted sequences */
55017 + i = core_if->first_in_nextep_seq;
55018 + do {
55019 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
55020 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55021 + if (dieptsiz.b.pktcnt != 0) {
55022 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55023 + depctl.b.epena = 1;
55024 + depctl.b.cnak = 1;
55025 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
55026 + }
55027 + i = core_if->nextep_seq[i];
55028 + } while (i != core_if->first_in_nextep_seq);
55029 +
55030 + /* Clear the global non-periodic IN NAK handshake */
55031 + dctl.d32 = 0;
55032 + dctl.b.cgnpinnak = 1;
55033 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
55034 +
55035 + /* Unmask EP Mismatch interrupt */
55036 + gintmsk_data.d32 = 0;
55037 + gintmsk_data.b.epmismatch = 1;
55038 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
55039 +
55040 + core_if->start_predict = 0;
55041 +
55042 + }
55043 +}
55044 +
55045 +/**
55046 + * Handler for the IN EP timeout handshake interrupt.
55047 + */
55048 +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
55049 + const uint32_t epnum)
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 +
55054 +#ifdef DEBUG
55055 + deptsiz_data_t dieptsiz = {.d32 = 0 };
55056 + uint32_t num = 0;
55057 +#endif
55058 + dctl_data_t dctl = {.d32 = 0 };
55059 + dwc_otg_pcd_ep_t *ep;
55060 +
55061 + gintmsk_data_t intr_mask = {.d32 = 0 };
55062 +
55063 + ep = get_in_ep(pcd, epnum);
55064 +
55065 + /* Disable the NP Tx Fifo Empty Interrrupt */
55066 + if (!core_if->dma_enable) {
55067 + intr_mask.b.nptxfempty = 1;
55068 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
55069 + intr_mask.d32, 0);
55070 + }
55071 + /** @todo NGS Check EP type.
55072 + * Implement for Periodic EPs */
55073 + /*
55074 + * Non-periodic EP
55075 + */
55076 + /* Enable the Global IN NAK Effective Interrupt */
55077 + intr_mask.b.ginnakeff = 1;
55078 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
55079 +
55080 + /* Set Global IN NAK */
55081 + dctl.b.sgnpinnak = 1;
55082 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
55083 +
55084 + ep->stopped = 1;
55085 +
55086 +#ifdef DEBUG
55087 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
55088 + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
55089 + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
55090 +#endif
55091 +
55092 +#ifdef DISABLE_PERIODIC_EP
55093 + /*
55094 + * Set the NAK bit for this EP to
55095 + * start the disable process.
55096 + */
55097 + diepctl.d32 = 0;
55098 + diepctl.b.snak = 1;
55099 + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
55100 + diepctl.d32);
55101 + ep->disabling = 1;
55102 + ep->stopped = 1;
55103 +#endif
55104 +}
55105 +
55106 +/**
55107 + * Handler for the IN EP NAK interrupt.
55108 + */
55109 +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
55110 + const uint32_t epnum)
55111 +{
55112 + /** @todo implement ISR */
55113 + dwc_otg_core_if_t *core_if;
55114 + diepmsk_data_t intr_mask = {.d32 = 0 };
55115 +
55116 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
55117 + core_if = GET_CORE_IF(pcd);
55118 + intr_mask.b.nak = 1;
55119 +
55120 + if (core_if->multiproc_int_enable) {
55121 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55122 + diepeachintmsk[epnum], intr_mask.d32, 0);
55123 + } else {
55124 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
55125 + intr_mask.d32, 0);
55126 + }
55127 +
55128 + return 1;
55129 +}
55130 +
55131 +/**
55132 + * Handler for the OUT EP Babble interrupt.
55133 + */
55134 +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
55135 + const uint32_t epnum)
55136 +{
55137 + /** @todo implement ISR */
55138 + dwc_otg_core_if_t *core_if;
55139 + doepmsk_data_t intr_mask = {.d32 = 0 };
55140 +
55141 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
55142 + "OUT EP Babble");
55143 + core_if = GET_CORE_IF(pcd);
55144 + intr_mask.b.babble = 1;
55145 +
55146 + if (core_if->multiproc_int_enable) {
55147 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55148 + doepeachintmsk[epnum], intr_mask.d32, 0);
55149 + } else {
55150 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55151 + intr_mask.d32, 0);
55152 + }
55153 +
55154 + return 1;
55155 +}
55156 +
55157 +/**
55158 + * Handler for the OUT EP NAK interrupt.
55159 + */
55160 +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
55161 + const uint32_t epnum)
55162 +{
55163 + /** @todo implement ISR */
55164 + dwc_otg_core_if_t *core_if;
55165 + doepmsk_data_t intr_mask = {.d32 = 0 };
55166 +
55167 + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
55168 + core_if = GET_CORE_IF(pcd);
55169 + intr_mask.b.nak = 1;
55170 +
55171 + if (core_if->multiproc_int_enable) {
55172 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55173 + doepeachintmsk[epnum], intr_mask.d32, 0);
55174 + } else {
55175 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55176 + intr_mask.d32, 0);
55177 + }
55178 +
55179 + return 1;
55180 +}
55181 +
55182 +/**
55183 + * Handler for the OUT EP NYET interrupt.
55184 + */
55185 +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
55186 + const uint32_t epnum)
55187 +{
55188 + /** @todo implement ISR */
55189 + dwc_otg_core_if_t *core_if;
55190 + doepmsk_data_t intr_mask = {.d32 = 0 };
55191 +
55192 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
55193 + core_if = GET_CORE_IF(pcd);
55194 + intr_mask.b.nyet = 1;
55195 +
55196 + if (core_if->multiproc_int_enable) {
55197 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55198 + doepeachintmsk[epnum], intr_mask.d32, 0);
55199 + } else {
55200 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55201 + intr_mask.d32, 0);
55202 + }
55203 +
55204 + return 1;
55205 +}
55206 +
55207 +/**
55208 + * This interrupt indicates that an IN EP has a pending Interrupt.
55209 + * The sequence for handling the IN EP interrupt is shown below:
55210 + * -# Read the Device All Endpoint Interrupt register
55211 + * -# Repeat the following for each IN EP interrupt bit set (from
55212 + * LSB to MSB).
55213 + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
55214 + * -# If "Transfer Complete" call the request complete function
55215 + * -# If "Endpoint Disabled" complete the EP disable procedure.
55216 + * -# If "AHB Error Interrupt" log error
55217 + * -# If "Time-out Handshake" log error
55218 + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
55219 + * FIFO.
55220 + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
55221 + * Mismatch Interrupt)
55222 + */
55223 +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
55224 +{
55225 +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
55226 +do { \
55227 + diepint_data_t diepint = {.d32=0}; \
55228 + diepint.b.__intr = 1; \
55229 + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
55230 + diepint.d32); \
55231 +} while (0)
55232 +
55233 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55234 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
55235 + diepint_data_t diepint = {.d32 = 0 };
55236 + depctl_data_t depctl = {.d32 = 0 };
55237 + uint32_t ep_intr;
55238 + uint32_t epnum = 0;
55239 + dwc_otg_pcd_ep_t *ep;
55240 + dwc_ep_t *dwc_ep;
55241 + gintmsk_data_t intr_mask = {.d32 = 0 };
55242 +
55243 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
55244 +
55245 + /* Read in the device interrupt bits */
55246 + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
55247 +
55248 + /* Service the Device IN interrupts for each endpoint */
55249 + while (ep_intr) {
55250 + if (ep_intr & 0x1) {
55251 + uint32_t empty_msk;
55252 + /* Get EP pointer */
55253 + ep = get_in_ep(pcd, epnum);
55254 + dwc_ep = &ep->dwc_ep;
55255 +
55256 + depctl.d32 =
55257 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55258 + empty_msk =
55259 + DWC_READ_REG32(&dev_if->
55260 + dev_global_regs->dtknqr4_fifoemptymsk);
55261 +
55262 + DWC_DEBUGPL(DBG_PCDV,
55263 + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
55264 + epnum, empty_msk, depctl.d32);
55265 +
55266 + DWC_DEBUGPL(DBG_PCD,
55267 + "EP%d-%s: type=%d, mps=%d\n",
55268 + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
55269 + dwc_ep->type, dwc_ep->maxpacket);
55270 +
55271 + diepint.d32 =
55272 + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
55273 +
55274 + DWC_DEBUGPL(DBG_PCDV,
55275 + "EP %d Interrupt Register - 0x%x\n", epnum,
55276 + diepint.d32);
55277 + /* Transfer complete */
55278 + if (diepint.b.xfercompl) {
55279 + /* Disable the NP Tx FIFO Empty
55280 + * Interrupt */
55281 + if (core_if->en_multiple_tx_fifo == 0) {
55282 + intr_mask.b.nptxfempty = 1;
55283 + DWC_MODIFY_REG32
55284 + (&core_if->core_global_regs->gintmsk,
55285 + intr_mask.d32, 0);
55286 + } else {
55287 + /* Disable the Tx FIFO Empty Interrupt for this EP */
55288 + uint32_t fifoemptymsk =
55289 + 0x1 << dwc_ep->num;
55290 + DWC_MODIFY_REG32(&core_if->
55291 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
55292 + fifoemptymsk, 0);
55293 + }
55294 + /* Clear the bit in DIEPINTn for this interrupt */
55295 + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
55296 +
55297 + /* Complete the transfer */
55298 + if (epnum == 0) {
55299 + handle_ep0(pcd);
55300 + }
55301 +#ifdef DWC_EN_ISOC
55302 + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55303 + if (!ep->stopped)
55304 + complete_iso_ep(pcd, ep);
55305 + }
55306 +#endif /* DWC_EN_ISOC */
55307 +#ifdef DWC_UTE_PER_IO
55308 + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55309 + if (!ep->stopped)
55310 + complete_xiso_ep(ep);
55311 + }
55312 +#endif /* DWC_UTE_PER_IO */
55313 + else {
55314 + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
55315 + dwc_ep->bInterval > 1) {
55316 + dwc_ep->frame_num += dwc_ep->bInterval;
55317 + if (dwc_ep->frame_num > 0x3FFF)
55318 + {
55319 + dwc_ep->frm_overrun = 1;
55320 + dwc_ep->frame_num &= 0x3FFF;
55321 + } else
55322 + dwc_ep->frm_overrun = 0;
55323 + }
55324 + complete_ep(ep);
55325 + if(diepint.b.nak)
55326 + CLEAR_IN_EP_INTR(core_if, epnum, nak);
55327 + }
55328 + }
55329 + /* Endpoint disable */
55330 + if (diepint.b.epdisabled) {
55331 + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
55332 + epnum);
55333 + handle_in_ep_disable_intr(pcd, epnum);
55334 +
55335 + /* Clear the bit in DIEPINTn for this interrupt */
55336 + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
55337 + }
55338 + /* AHB Error */
55339 + if (diepint.b.ahberr) {
55340 + DWC_ERROR("EP%d IN AHB Error\n", epnum);
55341 + /* Clear the bit in DIEPINTn for this interrupt */
55342 + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
55343 + }
55344 + /* TimeOUT Handshake (non-ISOC IN EPs) */
55345 + if (diepint.b.timeout) {
55346 + DWC_ERROR("EP%d IN Time-out\n", epnum);
55347 + handle_in_ep_timeout_intr(pcd, epnum);
55348 +
55349 + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
55350 + }
55351 + /** IN Token received with TxF Empty */
55352 + if (diepint.b.intktxfemp) {
55353 + DWC_DEBUGPL(DBG_ANY,
55354 + "EP%d IN TKN TxFifo Empty\n",
55355 + epnum);
55356 + if (!ep->stopped && epnum != 0) {
55357 +
55358 + diepmsk_data_t diepmsk = {.d32 = 0 };
55359 + diepmsk.b.intktxfemp = 1;
55360 +
55361 + if (core_if->multiproc_int_enable) {
55362 + DWC_MODIFY_REG32
55363 + (&dev_if->dev_global_regs->diepeachintmsk
55364 + [epnum], diepmsk.d32, 0);
55365 + } else {
55366 + DWC_MODIFY_REG32
55367 + (&dev_if->dev_global_regs->diepmsk,
55368 + diepmsk.d32, 0);
55369 + }
55370 + } else if (core_if->dma_desc_enable
55371 + && epnum == 0
55372 + && pcd->ep0state ==
55373 + EP0_OUT_STATUS_PHASE) {
55374 + // EP0 IN set STALL
55375 + depctl.d32 =
55376 + DWC_READ_REG32(&dev_if->in_ep_regs
55377 + [epnum]->diepctl);
55378 +
55379 + /* set the disable and stall bits */
55380 + if (depctl.b.epena) {
55381 + depctl.b.epdis = 1;
55382 + }
55383 + depctl.b.stall = 1;
55384 + DWC_WRITE_REG32(&dev_if->in_ep_regs
55385 + [epnum]->diepctl,
55386 + depctl.d32);
55387 + }
55388 + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
55389 + }
55390 + /** IN Token Received with EP mismatch */
55391 + if (diepint.b.intknepmis) {
55392 + DWC_DEBUGPL(DBG_ANY,
55393 + "EP%d IN TKN EP Mismatch\n", epnum);
55394 + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
55395 + }
55396 + /** IN Endpoint NAK Effective */
55397 + if (diepint.b.inepnakeff) {
55398 + DWC_DEBUGPL(DBG_ANY,
55399 + "EP%d IN EP NAK Effective\n",
55400 + epnum);
55401 + /* Periodic EP */
55402 + if (ep->disabling) {
55403 + depctl.d32 = 0;
55404 + depctl.b.snak = 1;
55405 + depctl.b.epdis = 1;
55406 + DWC_MODIFY_REG32(&dev_if->in_ep_regs
55407 + [epnum]->diepctl,
55408 + depctl.d32,
55409 + depctl.d32);
55410 + }
55411 + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
55412 +
55413 + }
55414 +
55415 + /** IN EP Tx FIFO Empty Intr */
55416 + if (diepint.b.emptyintr) {
55417 + DWC_DEBUGPL(DBG_ANY,
55418 + "EP%d Tx FIFO Empty Intr \n",
55419 + epnum);
55420 + write_empty_tx_fifo(pcd, epnum);
55421 +
55422 + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
55423 +
55424 + }
55425 +
55426 + /** IN EP BNA Intr */
55427 + if (diepint.b.bna) {
55428 + CLEAR_IN_EP_INTR(core_if, epnum, bna);
55429 + if (core_if->dma_desc_enable) {
55430 +#ifdef DWC_EN_ISOC
55431 + if (dwc_ep->type ==
55432 + DWC_OTG_EP_TYPE_ISOC) {
55433 + /*
55434 + * This checking is performed to prevent first "false" BNA
55435 + * handling occuring right after reconnect
55436 + */
55437 + if (dwc_ep->next_frame !=
55438 + 0xffffffff)
55439 + dwc_otg_pcd_handle_iso_bna(ep);
55440 + } else
55441 +#endif /* DWC_EN_ISOC */
55442 + {
55443 + dwc_otg_pcd_handle_noniso_bna(ep);
55444 + }
55445 + }
55446 + }
55447 + /* NAK Interrutp */
55448 + if (diepint.b.nak) {
55449 + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
55450 + epnum);
55451 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
55452 + depctl_data_t depctl;
55453 + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
55454 + ep->dwc_ep.frame_num = core_if->frame_num;
55455 + if (ep->dwc_ep.bInterval > 1) {
55456 + depctl.d32 = 0;
55457 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55458 + if (ep->dwc_ep.frame_num & 0x1) {
55459 + depctl.b.setd1pid = 1;
55460 + depctl.b.setd0pid = 0;
55461 + } else {
55462 + depctl.b.setd0pid = 1;
55463 + depctl.b.setd1pid = 0;
55464 + }
55465 + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
55466 + }
55467 + start_next_request(ep);
55468 + }
55469 + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
55470 + if (dwc_ep->frame_num > 0x3FFF) {
55471 + dwc_ep->frm_overrun = 1;
55472 + dwc_ep->frame_num &= 0x3FFF;
55473 + } else
55474 + dwc_ep->frm_overrun = 0;
55475 + }
55476 +
55477 + CLEAR_IN_EP_INTR(core_if, epnum, nak);
55478 + }
55479 + }
55480 + epnum++;
55481 + ep_intr >>= 1;
55482 + }
55483 +
55484 + return 1;
55485 +#undef CLEAR_IN_EP_INTR
55486 +}
55487 +
55488 +/**
55489 + * This interrupt indicates that an OUT EP has a pending Interrupt.
55490 + * The sequence for handling the OUT EP interrupt is shown below:
55491 + * -# Read the Device All Endpoint Interrupt register
55492 + * -# Repeat the following for each OUT EP interrupt bit set (from
55493 + * LSB to MSB).
55494 + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
55495 + * -# If "Transfer Complete" call the request complete function
55496 + * -# If "Endpoint Disabled" complete the EP disable procedure.
55497 + * -# If "AHB Error Interrupt" log error
55498 + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
55499 + * Command Processing)
55500 + */
55501 +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
55502 +{
55503 +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
55504 +do { \
55505 + doepint_data_t doepint = {.d32=0}; \
55506 + doepint.b.__intr = 1; \
55507 + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
55508 + doepint.d32); \
55509 +} while (0)
55510 +
55511 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55512 + uint32_t ep_intr;
55513 + doepint_data_t doepint = {.d32 = 0 };
55514 + uint32_t epnum = 0;
55515 + dwc_otg_pcd_ep_t *ep;
55516 + dwc_ep_t *dwc_ep;
55517 + dctl_data_t dctl = {.d32 = 0 };
55518 + gintmsk_data_t gintmsk = {.d32 = 0 };
55519 +
55520 +
55521 + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
55522 +
55523 + /* Read in the device interrupt bits */
55524 + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
55525 +
55526 + while (ep_intr) {
55527 + if (ep_intr & 0x1) {
55528 + /* Get EP pointer */
55529 + ep = get_out_ep(pcd, epnum);
55530 + dwc_ep = &ep->dwc_ep;
55531 +
55532 +#ifdef VERBOSE
55533 + DWC_DEBUGPL(DBG_PCDV,
55534 + "EP%d-%s: type=%d, mps=%d\n",
55535 + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
55536 + dwc_ep->type, dwc_ep->maxpacket);
55537 +#endif
55538 + doepint.d32 =
55539 + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
55540 + /* Moved this interrupt upper due to core deffect of asserting
55541 + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
55542 + if (doepint.b.stsphsercvd) {
55543 + deptsiz0_data_t deptsiz;
55544 + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
55545 + deptsiz.d32 =
55546 + DWC_READ_REG32(&core_if->dev_if->
55547 + out_ep_regs[0]->doeptsiz);
55548 + if (core_if->snpsid >= OTG_CORE_REV_3_00a
55549 + && core_if->dma_enable
55550 + && core_if->dma_desc_enable == 0
55551 + && doepint.b.xfercompl
55552 + && deptsiz.b.xfersize == 24) {
55553 + CLEAR_OUT_EP_INTR(core_if, epnum,
55554 + xfercompl);
55555 + doepint.b.xfercompl = 0;
55556 + ep0_out_start(core_if, pcd);
55557 + }
55558 + if ((core_if->dma_desc_enable) ||
55559 + (core_if->dma_enable
55560 + && core_if->snpsid >=
55561 + OTG_CORE_REV_3_00a)) {
55562 + do_setup_in_status_phase(pcd);
55563 + }
55564 + }
55565 + /* Transfer complete */
55566 + if (doepint.b.xfercompl) {
55567 +
55568 + if (epnum == 0) {
55569 + /* Clear the bit in DOEPINTn for this interrupt */
55570 + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
55571 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
55572 + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
55573 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
55574 + doepint.d32);
55575 + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
55576 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
55577 +
55578 + if (core_if->snpsid >= OTG_CORE_REV_3_00a
55579 + && core_if->dma_enable == 0) {
55580 + doepint_data_t doepint;
55581 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55582 + out_ep_regs[0]->doepint);
55583 + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
55584 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55585 + goto exit_xfercompl;
55586 + }
55587 + }
55588 + /* In case of DDMA look at SR bit to go to the Data Stage */
55589 + if (core_if->dma_desc_enable) {
55590 + dev_dma_desc_sts_t status = {.d32 = 0};
55591 + if (pcd->ep0state == EP0_IDLE) {
55592 + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
55593 + dev_if->setup_desc_index]->status.d32;
55594 + if(pcd->data_terminated) {
55595 + pcd->data_terminated = 0;
55596 + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
55597 + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
55598 + }
55599 + if (status.b.sr) {
55600 + if (doepint.b.setup) {
55601 + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
55602 + /* Already started data stage, clear setup */
55603 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55604 + doepint.b.setup = 0;
55605 + handle_ep0(pcd);
55606 + /* Prepare for more setup packets */
55607 + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
55608 + pcd->ep0state == EP0_IN_DATA_PHASE) {
55609 + ep0_out_start(core_if, pcd);
55610 + }
55611 +
55612 + goto exit_xfercompl;
55613 + } else {
55614 + /* Prepare for more setup packets */
55615 + DWC_DEBUGPL(DBG_PCDV,
55616 + "EP0_IDLE SR=1 setup=0 new setup comes\n");
55617 + ep0_out_start(core_if, pcd);
55618 + }
55619 + }
55620 + } else {
55621 + dwc_otg_pcd_request_t *req;
55622 + dev_dma_desc_sts_t status = {.d32 = 0};
55623 + diepint_data_t diepint0;
55624 + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
55625 + in_ep_regs[0]->diepint);
55626 +
55627 + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
55628 + DWC_ERROR("EP0 is stalled/disconnected\n");
55629 + }
55630 +
55631 + /* Clear IN xfercompl if set */
55632 + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
55633 + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
55634 + DWC_WRITE_REG32(&core_if->dev_if->
55635 + in_ep_regs[0]->diepint, diepint0.d32);
55636 + }
55637 +
55638 + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
55639 + dev_if->setup_desc_index]->status.d32;
55640 +
55641 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
55642 + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
55643 + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
55644 + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
55645 + status.d32 = core_if->dev_if->
55646 + out_desc_addr->status.d32;
55647 +
55648 + if (status.b.sr) {
55649 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55650 + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
55651 + } else {
55652 + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
55653 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55654 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
55655 + pcd->ep0state == EP0_OUT_DATA_PHASE) {
55656 + /* Read arrived setup packet from req->buf */
55657 + dwc_memcpy(&pcd->setup_pkt->req,
55658 + req->buf + ep->dwc_ep.xfer_count, 8);
55659 + }
55660 + req->actual = ep->dwc_ep.xfer_count;
55661 + dwc_otg_request_done(ep, req, -ECONNRESET);
55662 + ep->dwc_ep.start_xfer_buff = 0;
55663 + ep->dwc_ep.xfer_buff = 0;
55664 + ep->dwc_ep.xfer_len = 0;
55665 + }
55666 + pcd->ep0state = EP0_IDLE;
55667 + if (doepint.b.setup) {
55668 + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
55669 + /* Data stage started, clear setup */
55670 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55671 + doepint.b.setup = 0;
55672 + handle_ep0(pcd);
55673 + /* Prepare for setup packets if ep0in was enabled*/
55674 + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
55675 + ep0_out_start(core_if, pcd);
55676 + }
55677 +
55678 + goto exit_xfercompl;
55679 + } else {
55680 + /* Prepare for more setup packets */
55681 + DWC_DEBUGPL(DBG_PCDV,
55682 + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
55683 + ep0_out_start(core_if, pcd);
55684 + }
55685 + }
55686 + }
55687 + }
55688 + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
55689 + && core_if->dma_desc_enable == 0) {
55690 + doepint_data_t doepint_temp = {.d32 = 0};
55691 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
55692 + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
55693 + out_ep_regs[ep->dwc_ep.num]->doepint);
55694 + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
55695 + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
55696 + if (pcd->ep0state == EP0_IDLE) {
55697 + if (doepint_temp.b.sr) {
55698 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55699 + }
55700 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55701 + out_ep_regs[0]->doepint);
55702 + if (doeptsize0.b.supcnt == 3) {
55703 + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
55704 + ep->dwc_ep.stp_rollover = 1;
55705 + }
55706 + if (doepint.b.setup) {
55707 +retry:
55708 + /* Already started data stage, clear setup */
55709 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55710 + doepint.b.setup = 0;
55711 + handle_ep0(pcd);
55712 + ep->dwc_ep.stp_rollover = 0;
55713 + /* Prepare for more setup packets */
55714 + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
55715 + pcd->ep0state == EP0_IN_DATA_PHASE) {
55716 + ep0_out_start(core_if, pcd);
55717 + }
55718 + goto exit_xfercompl;
55719 + } else {
55720 + /* Prepare for more setup packets */
55721 + DWC_DEBUGPL(DBG_ANY,
55722 + "EP0_IDLE SR=1 setup=0 new setup comes\n");
55723 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55724 + out_ep_regs[0]->doepint);
55725 + if(doepint.b.setup)
55726 + goto retry;
55727 + ep0_out_start(core_if, pcd);
55728 + }
55729 + } else {
55730 + dwc_otg_pcd_request_t *req;
55731 + diepint_data_t diepint0 = {.d32 = 0};
55732 + doepint_data_t doepint_temp = {.d32 = 0};
55733 + depctl_data_t diepctl0;
55734 + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
55735 + in_ep_regs[0]->diepint);
55736 + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
55737 + in_ep_regs[0]->diepctl);
55738 +
55739 + if (pcd->ep0state == EP0_IN_DATA_PHASE
55740 + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
55741 + if (diepint0.b.xfercompl) {
55742 + DWC_WRITE_REG32(&core_if->dev_if->
55743 + in_ep_regs[0]->diepint, diepint0.d32);
55744 + }
55745 + if (diepctl0.b.epena) {
55746 + diepint_data_t diepint = {.d32 = 0};
55747 + diepctl0.b.snak = 1;
55748 + DWC_WRITE_REG32(&core_if->dev_if->
55749 + in_ep_regs[0]->diepctl, diepctl0.d32);
55750 + do {
55751 + dwc_udelay(10);
55752 + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55753 + in_ep_regs[0]->diepint);
55754 + } while (!diepint.b.inepnakeff);
55755 + diepint.b.inepnakeff = 1;
55756 + DWC_WRITE_REG32(&core_if->dev_if->
55757 + in_ep_regs[0]->diepint, diepint.d32);
55758 + diepctl0.d32 = 0;
55759 + diepctl0.b.epdis = 1;
55760 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
55761 + diepctl0.d32);
55762 + do {
55763 + dwc_udelay(10);
55764 + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55765 + in_ep_regs[0]->diepint);
55766 + } while (!diepint.b.epdisabled);
55767 + diepint.b.epdisabled = 1;
55768 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
55769 + diepint.d32);
55770 + }
55771 + }
55772 + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
55773 + out_ep_regs[ep->dwc_ep.num]->doepint);
55774 + if (doepint_temp.b.sr) {
55775 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55776 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55777 + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
55778 + } else {
55779 + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
55780 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55781 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
55782 + pcd->ep0state == EP0_OUT_DATA_PHASE) {
55783 + /* Read arrived setup packet from req->buf */
55784 + dwc_memcpy(&pcd->setup_pkt->req,
55785 + req->buf + ep->dwc_ep.xfer_count, 8);
55786 + }
55787 + req->actual = ep->dwc_ep.xfer_count;
55788 + dwc_otg_request_done(ep, req, -ECONNRESET);
55789 + ep->dwc_ep.start_xfer_buff = 0;
55790 + ep->dwc_ep.xfer_buff = 0;
55791 + ep->dwc_ep.xfer_len = 0;
55792 + }
55793 + pcd->ep0state = EP0_IDLE;
55794 + if (doepint.b.setup) {
55795 + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
55796 + /* Data stage started, clear setup */
55797 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55798 + doepint.b.setup = 0;
55799 + handle_ep0(pcd);
55800 + /* Prepare for setup packets if ep0in was enabled*/
55801 + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
55802 + ep0_out_start(core_if, pcd);
55803 + }
55804 + goto exit_xfercompl;
55805 + } else {
55806 + /* Prepare for more setup packets */
55807 + DWC_DEBUGPL(DBG_PCDV,
55808 + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
55809 + ep0_out_start(core_if, pcd);
55810 + }
55811 + }
55812 + }
55813 + }
55814 + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
55815 + handle_ep0(pcd);
55816 +exit_xfercompl:
55817 + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
55818 + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
55819 + } else {
55820 + if (core_if->dma_desc_enable == 0
55821 + || pcd->ep0state != EP0_IDLE)
55822 + handle_ep0(pcd);
55823 + }
55824 +#ifdef DWC_EN_ISOC
55825 + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55826 + if (doepint.b.pktdrpsts == 0) {
55827 + /* Clear the bit in DOEPINTn for this interrupt */
55828 + CLEAR_OUT_EP_INTR(core_if,
55829 + epnum,
55830 + xfercompl);
55831 + complete_iso_ep(pcd, ep);
55832 + } else {
55833 +
55834 + doepint_data_t doepint = {.d32 = 0 };
55835 + doepint.b.xfercompl = 1;
55836 + doepint.b.pktdrpsts = 1;
55837 + DWC_WRITE_REG32
55838 + (&core_if->dev_if->out_ep_regs
55839 + [epnum]->doepint,
55840 + doepint.d32);
55841 + if (handle_iso_out_pkt_dropped
55842 + (core_if, dwc_ep)) {
55843 + complete_iso_ep(pcd,
55844 + ep);
55845 + }
55846 + }
55847 +#endif /* DWC_EN_ISOC */
55848 +#ifdef DWC_UTE_PER_IO
55849 + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55850 + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
55851 + if (!ep->stopped)
55852 + complete_xiso_ep(ep);
55853 +#endif /* DWC_UTE_PER_IO */
55854 + } else {
55855 + /* Clear the bit in DOEPINTn for this interrupt */
55856 + CLEAR_OUT_EP_INTR(core_if, epnum,
55857 + xfercompl);
55858 +
55859 + if (core_if->core_params->dev_out_nak) {
55860 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
55861 + pcd->core_if->ep_xfer_info[epnum].state = 0;
55862 +#ifdef DEBUG
55863 + print_memory_payload(pcd, dwc_ep);
55864 +#endif
55865 + }
55866 + complete_ep(ep);
55867 + }
55868 +
55869 + }
55870 +
55871 + /* Endpoint disable */
55872 + if (doepint.b.epdisabled) {
55873 +
55874 + /* Clear the bit in DOEPINTn for this interrupt */
55875 + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
55876 + if (core_if->core_params->dev_out_nak) {
55877 +#ifdef DEBUG
55878 + print_memory_payload(pcd, dwc_ep);
55879 +#endif
55880 + /* In case of timeout condition */
55881 + if (core_if->ep_xfer_info[epnum].state == 2) {
55882 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
55883 + dev_global_regs->dctl);
55884 + dctl.b.cgoutnak = 1;
55885 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
55886 + dctl.d32);
55887 + /* Unmask goutnakeff interrupt which was masked
55888 + * during handle nak out interrupt */
55889 + gintmsk.b.goutnakeff = 1;
55890 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
55891 + 0, gintmsk.d32);
55892 +
55893 + complete_ep(ep);
55894 + }
55895 + }
55896 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
55897 + {
55898 + dctl_data_t dctl;
55899 + gintmsk_data_t intr_mask = {.d32 = 0};
55900 + dwc_otg_pcd_request_t *req = 0;
55901 +
55902 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
55903 + dev_global_regs->dctl);
55904 + dctl.b.cgoutnak = 1;
55905 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
55906 + dctl.d32);
55907 +
55908 + intr_mask.d32 = 0;
55909 + intr_mask.b.incomplisoout = 1;
55910 +
55911 + /* Get any pending requests */
55912 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55913 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55914 + if (!req) {
55915 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
55916 + } else {
55917 + dwc_otg_request_done(ep, req, 0);
55918 + start_next_request(ep);
55919 + }
55920 + } else {
55921 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
55922 + }
55923 + }
55924 + }
55925 + /* AHB Error */
55926 + if (doepint.b.ahberr) {
55927 + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
55928 + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
55929 + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
55930 + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
55931 + }
55932 + /* Setup Phase Done (contorl EPs) */
55933 + if (doepint.b.setup) {
55934 +#ifdef DEBUG_EP0
55935 + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
55936 +#endif
55937 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55938 +
55939 + handle_ep0(pcd);
55940 + }
55941 +
55942 + /** OUT EP BNA Intr */
55943 + if (doepint.b.bna) {
55944 + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
55945 + if (core_if->dma_desc_enable) {
55946 +#ifdef DWC_EN_ISOC
55947 + if (dwc_ep->type ==
55948 + DWC_OTG_EP_TYPE_ISOC) {
55949 + /*
55950 + * This checking is performed to prevent first "false" BNA
55951 + * handling occuring right after reconnect
55952 + */
55953 + if (dwc_ep->next_frame !=
55954 + 0xffffffff)
55955 + dwc_otg_pcd_handle_iso_bna(ep);
55956 + } else
55957 +#endif /* DWC_EN_ISOC */
55958 + {
55959 + dwc_otg_pcd_handle_noniso_bna(ep);
55960 + }
55961 + }
55962 + }
55963 + /* Babble Interrupt */
55964 + if (doepint.b.babble) {
55965 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
55966 + epnum);
55967 + handle_out_ep_babble_intr(pcd, epnum);
55968 +
55969 + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
55970 + }
55971 + if (doepint.b.outtknepdis) {
55972 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
55973 + disabled\n",epnum);
55974 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
55975 + doepmsk_data_t doepmsk = {.d32 = 0};
55976 + ep->dwc_ep.frame_num = core_if->frame_num;
55977 + if (ep->dwc_ep.bInterval > 1) {
55978 + depctl_data_t depctl;
55979 + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
55980 + out_ep_regs[epnum]->doepctl);
55981 + if (ep->dwc_ep.frame_num & 0x1) {
55982 + depctl.b.setd1pid = 1;
55983 + depctl.b.setd0pid = 0;
55984 + } else {
55985 + depctl.b.setd0pid = 1;
55986 + depctl.b.setd1pid = 0;
55987 + }
55988 + DWC_WRITE_REG32(&core_if->dev_if->
55989 + out_ep_regs[epnum]->doepctl, depctl.d32);
55990 + }
55991 + start_next_request(ep);
55992 + doepmsk.b.outtknepdis = 1;
55993 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55994 + doepmsk.d32, 0);
55995 + }
55996 + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
55997 + }
55998 +
55999 + /* NAK Interrutp */
56000 + if (doepint.b.nak) {
56001 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
56002 + handle_out_ep_nak_intr(pcd, epnum);
56003 +
56004 + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
56005 + }
56006 + /* NYET Interrutp */
56007 + if (doepint.b.nyet) {
56008 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
56009 + handle_out_ep_nyet_intr(pcd, epnum);
56010 +
56011 + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
56012 + }
56013 + }
56014 +
56015 + epnum++;
56016 + ep_intr >>= 1;
56017 + }
56018 +
56019 + return 1;
56020 +
56021 +#undef CLEAR_OUT_EP_INTR
56022 +}
56023 +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
56024 +{
56025 + int retval = 0;
56026 + if(!frm_overrun && curr_fr >= trgt_fr)
56027 + retval = 1;
56028 + else if (frm_overrun
56029 + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
56030 + retval = 1;
56031 + return retval;
56032 +}
56033 +/**
56034 + * Incomplete ISO IN Transfer Interrupt.
56035 + * This interrupt indicates one of the following conditions occurred
56036 + * while transmitting an ISOC transaction.
56037 + * - Corrupted IN Token for ISOC EP.
56038 + * - Packet not complete in FIFO.
56039 + * The follow actions will be taken:
56040 + * -# Determine the EP
56041 + * -# Set incomplete flag in dwc_ep structure
56042 + * -# Disable EP; when "Endpoint Disabled" interrupt is received
56043 + * Flush FIFO
56044 + */
56045 +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
56046 +{
56047 + gintsts_data_t gintsts;
56048 +
56049 +#ifdef DWC_EN_ISOC
56050 + dwc_otg_dev_if_t *dev_if;
56051 + deptsiz_data_t deptsiz = {.d32 = 0 };
56052 + depctl_data_t depctl = {.d32 = 0 };
56053 + dsts_data_t dsts = {.d32 = 0 };
56054 + dwc_ep_t *dwc_ep;
56055 + int i;
56056 +
56057 + dev_if = GET_CORE_IF(pcd)->dev_if;
56058 +
56059 + for (i = 1; i <= dev_if->num_in_eps; ++i) {
56060 + dwc_ep = &pcd->in_ep[i].dwc_ep;
56061 + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
56062 + deptsiz.d32 =
56063 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
56064 + depctl.d32 =
56065 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56066 +
56067 + if (depctl.b.epdis && deptsiz.d32) {
56068 + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
56069 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
56070 + dwc_ep->cur_pkt = 0;
56071 + dwc_ep->proc_buf_num =
56072 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
56073 +
56074 + if (dwc_ep->proc_buf_num) {
56075 + dwc_ep->cur_pkt_addr =
56076 + dwc_ep->xfer_buff1;
56077 + dwc_ep->cur_pkt_dma_addr =
56078 + dwc_ep->dma_addr1;
56079 + } else {
56080 + dwc_ep->cur_pkt_addr =
56081 + dwc_ep->xfer_buff0;
56082 + dwc_ep->cur_pkt_dma_addr =
56083 + dwc_ep->dma_addr0;
56084 + }
56085 +
56086 + }
56087 +
56088 + dsts.d32 =
56089 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
56090 + dev_global_regs->dsts);
56091 + dwc_ep->next_frame = dsts.b.soffn;
56092 +
56093 + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
56094 + (pcd),
56095 + dwc_ep);
56096 + }
56097 + }
56098 + }
56099 +
56100 +#else
56101 + depctl_data_t depctl = {.d32 = 0 };
56102 + dwc_ep_t *dwc_ep;
56103 + dwc_otg_dev_if_t *dev_if;
56104 + int i;
56105 + dev_if = GET_CORE_IF(pcd)->dev_if;
56106 +
56107 + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
56108 +
56109 + for (i = 1; i <= dev_if->num_in_eps; ++i) {
56110 + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
56111 + depctl.d32 =
56112 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56113 + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
56114 + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
56115 + dwc_ep->frm_overrun))
56116 + {
56117 + depctl.d32 =
56118 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56119 + depctl.b.snak = 1;
56120 + depctl.b.epdis = 1;
56121 + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
56122 + }
56123 + }
56124 + }
56125 +
56126 + /*intr_mask.b.incomplisoin = 1;
56127 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56128 + intr_mask.d32, 0); */
56129 +#endif //DWC_EN_ISOC
56130 +
56131 + /* Clear interrupt */
56132 + gintsts.d32 = 0;
56133 + gintsts.b.incomplisoin = 1;
56134 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56135 + gintsts.d32);
56136 +
56137 + return 1;
56138 +}
56139 +
56140 +/**
56141 + * Incomplete ISO OUT Transfer Interrupt.
56142 + *
56143 + * This interrupt indicates that the core has dropped an ISO OUT
56144 + * packet. The following conditions can be the cause:
56145 + * - FIFO Full, the entire packet would not fit in the FIFO.
56146 + * - CRC Error
56147 + * - Corrupted Token
56148 + * The follow actions will be taken:
56149 + * -# Determine the EP
56150 + * -# Set incomplete flag in dwc_ep structure
56151 + * -# Read any data from the FIFO
56152 + * -# Disable EP. When "Endpoint Disabled" interrupt is received
56153 + * re-enable EP.
56154 + */
56155 +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
56156 +{
56157 +
56158 + gintsts_data_t gintsts;
56159 +
56160 +#ifdef DWC_EN_ISOC
56161 + dwc_otg_dev_if_t *dev_if;
56162 + deptsiz_data_t deptsiz = {.d32 = 0 };
56163 + depctl_data_t depctl = {.d32 = 0 };
56164 + dsts_data_t dsts = {.d32 = 0 };
56165 + dwc_ep_t *dwc_ep;
56166 + int i;
56167 +
56168 + dev_if = GET_CORE_IF(pcd)->dev_if;
56169 +
56170 + for (i = 1; i <= dev_if->num_out_eps; ++i) {
56171 + dwc_ep = &pcd->in_ep[i].dwc_ep;
56172 + if (pcd->out_ep[i].dwc_ep.active &&
56173 + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
56174 + deptsiz.d32 =
56175 + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
56176 + depctl.d32 =
56177 + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
56178 +
56179 + if (depctl.b.epdis && deptsiz.d32) {
56180 + set_current_pkt_info(GET_CORE_IF(pcd),
56181 + &pcd->out_ep[i].dwc_ep);
56182 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
56183 + dwc_ep->cur_pkt = 0;
56184 + dwc_ep->proc_buf_num =
56185 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
56186 +
56187 + if (dwc_ep->proc_buf_num) {
56188 + dwc_ep->cur_pkt_addr =
56189 + dwc_ep->xfer_buff1;
56190 + dwc_ep->cur_pkt_dma_addr =
56191 + dwc_ep->dma_addr1;
56192 + } else {
56193 + dwc_ep->cur_pkt_addr =
56194 + dwc_ep->xfer_buff0;
56195 + dwc_ep->cur_pkt_dma_addr =
56196 + dwc_ep->dma_addr0;
56197 + }
56198 +
56199 + }
56200 +
56201 + dsts.d32 =
56202 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
56203 + dev_global_regs->dsts);
56204 + dwc_ep->next_frame = dsts.b.soffn;
56205 +
56206 + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
56207 + (pcd),
56208 + dwc_ep);
56209 + }
56210 + }
56211 + }
56212 +#else
56213 + /** @todo implement ISR */
56214 + gintmsk_data_t intr_mask = {.d32 = 0 };
56215 + dwc_otg_core_if_t *core_if;
56216 + deptsiz_data_t deptsiz = {.d32 = 0 };
56217 + depctl_data_t depctl = {.d32 = 0 };
56218 + dctl_data_t dctl = {.d32 = 0 };
56219 + dwc_ep_t *dwc_ep = NULL;
56220 + int i;
56221 + core_if = GET_CORE_IF(pcd);
56222 +
56223 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
56224 + dwc_ep = &pcd->out_ep[i].dwc_ep;
56225 + depctl.d32 =
56226 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
56227 + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
56228 + core_if->dev_if->isoc_ep = dwc_ep;
56229 + deptsiz.d32 =
56230 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
56231 + break;
56232 + }
56233 + }
56234 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
56235 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
56236 + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
56237 +
56238 + if (!intr_mask.b.goutnakeff) {
56239 + /* Unmask it */
56240 + intr_mask.b.goutnakeff = 1;
56241 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
56242 + }
56243 + if (!gintsts.b.goutnakeff) {
56244 + dctl.b.sgoutnak = 1;
56245 + }
56246 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
56247 +
56248 + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
56249 + if (depctl.b.epena) {
56250 + depctl.b.epdis = 1;
56251 + depctl.b.snak = 1;
56252 + }
56253 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
56254 +
56255 + intr_mask.d32 = 0;
56256 + intr_mask.b.incomplisoout = 1;
56257 +
56258 +#endif /* DWC_EN_ISOC */
56259 +
56260 + /* Clear interrupt */
56261 + gintsts.d32 = 0;
56262 + gintsts.b.incomplisoout = 1;
56263 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56264 + gintsts.d32);
56265 +
56266 + return 1;
56267 +}
56268 +
56269 +/**
56270 + * This function handles the Global IN NAK Effective interrupt.
56271 + *
56272 + */
56273 +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
56274 +{
56275 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
56276 + depctl_data_t diepctl = {.d32 = 0 };
56277 + gintmsk_data_t intr_mask = {.d32 = 0 };
56278 + gintsts_data_t gintsts;
56279 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
56280 + int i;
56281 +
56282 + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
56283 +
56284 + /* Disable all active IN EPs */
56285 + for (i = 0; i <= dev_if->num_in_eps; i++) {
56286 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56287 + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
56288 + if (core_if->start_predict > 0)
56289 + core_if->start_predict++;
56290 + diepctl.b.epdis = 1;
56291 + diepctl.b.snak = 1;
56292 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
56293 + }
56294 + }
56295 +
56296 +
56297 + /* Disable the Global IN NAK Effective Interrupt */
56298 + intr_mask.b.ginnakeff = 1;
56299 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56300 + intr_mask.d32, 0);
56301 +
56302 + /* Clear interrupt */
56303 + gintsts.d32 = 0;
56304 + gintsts.b.ginnakeff = 1;
56305 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56306 + gintsts.d32);
56307 +
56308 + return 1;
56309 +}
56310 +
56311 +/**
56312 + * OUT NAK Effective.
56313 + *
56314 + */
56315 +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
56316 +{
56317 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
56318 + gintmsk_data_t intr_mask = {.d32 = 0 };
56319 + gintsts_data_t gintsts;
56320 + depctl_data_t doepctl;
56321 + int i;
56322 +
56323 + /* Disable the Global OUT NAK Effective Interrupt */
56324 + intr_mask.b.goutnakeff = 1;
56325 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56326 + intr_mask.d32, 0);
56327 +
56328 + /* If DEV OUT NAK enabled*/
56329 + if (pcd->core_if->core_params->dev_out_nak) {
56330 + /* Run over all out endpoints to determine the ep number on
56331 + * which the timeout has happened
56332 + */
56333 + for (i = 0; i <= dev_if->num_out_eps; i++) {
56334 + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
56335 + break;
56336 + }
56337 + if (i > dev_if->num_out_eps) {
56338 + dctl_data_t dctl;
56339 + dctl.d32 =
56340 + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
56341 + dctl.b.cgoutnak = 1;
56342 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
56343 + dctl.d32);
56344 + goto out;
56345 + }
56346 +
56347 + /* Disable the endpoint */
56348 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
56349 + if (doepctl.b.epena) {
56350 + doepctl.b.epdis = 1;
56351 + doepctl.b.snak = 1;
56352 + }
56353 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
56354 + return 1;
56355 + }
56356 + /* We come here from Incomplete ISO OUT handler */
56357 + if (dev_if->isoc_ep) {
56358 + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
56359 + uint32_t epnum = dwc_ep->num;
56360 + doepint_data_t doepint;
56361 + doepint.d32 =
56362 + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
56363 + dev_if->isoc_ep = NULL;
56364 + doepctl.d32 =
56365 + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
56366 + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
56367 + if (doepctl.b.epena) {
56368 + doepctl.b.epdis = 1;
56369 + doepctl.b.snak = 1;
56370 + }
56371 + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
56372 + doepctl.d32);
56373 + return 1;
56374 + } else
56375 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
56376 + "Global OUT NAK Effective\n");
56377 +
56378 +out:
56379 + /* Clear interrupt */
56380 + gintsts.d32 = 0;
56381 + gintsts.b.goutnakeff = 1;
56382 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56383 + gintsts.d32);
56384 +
56385 + return 1;
56386 +}
56387 +
56388 +/**
56389 + * PCD interrupt handler.
56390 + *
56391 + * The PCD handles the device interrupts. Many conditions can cause a
56392 + * device interrupt. When an interrupt occurs, the device interrupt
56393 + * service routine determines the cause of the interrupt and
56394 + * dispatches handling to the appropriate function. These interrupt
56395 + * handling functions are described below.
56396 + *
56397 + * All interrupt registers are processed from LSB to MSB.
56398 + *
56399 + */
56400 +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
56401 +{
56402 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
56403 +#ifdef VERBOSE
56404 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
56405 +#endif
56406 + gintsts_data_t gintr_status;
56407 + int32_t retval = 0;
56408 +
56409 + /* Exit from ISR if core is hibernated */
56410 + if (core_if->hibernation_suspend == 1) {
56411 + return retval;
56412 + }
56413 +#ifdef VERBOSE
56414 + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
56415 + __func__,
56416 + DWC_READ_REG32(&global_regs->gintsts),
56417 + DWC_READ_REG32(&global_regs->gintmsk));
56418 +#endif
56419 +
56420 + if (dwc_otg_is_device_mode(core_if)) {
56421 + DWC_SPINLOCK(pcd->lock);
56422 +#ifdef VERBOSE
56423 + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
56424 + __func__,
56425 + DWC_READ_REG32(&global_regs->gintsts),
56426 + DWC_READ_REG32(&global_regs->gintmsk));
56427 +#endif
56428 +
56429 + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
56430 +
56431 + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
56432 + __func__, gintr_status.d32);
56433 +
56434 + if (gintr_status.b.sofintr) {
56435 + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
56436 + }
56437 + if (gintr_status.b.rxstsqlvl) {
56438 + retval |=
56439 + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
56440 + }
56441 + if (gintr_status.b.nptxfempty) {
56442 + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
56443 + }
56444 + if (gintr_status.b.goutnakeff) {
56445 + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
56446 + }
56447 + if (gintr_status.b.i2cintr) {
56448 + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
56449 + }
56450 + if (gintr_status.b.erlysuspend) {
56451 + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
56452 + }
56453 + if (gintr_status.b.usbreset) {
56454 + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
56455 + }
56456 + if (gintr_status.b.enumdone) {
56457 + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
56458 + }
56459 + if (gintr_status.b.isooutdrop) {
56460 + retval |=
56461 + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
56462 + (pcd);
56463 + }
56464 + if (gintr_status.b.eopframe) {
56465 + retval |=
56466 + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
56467 + }
56468 + if (gintr_status.b.inepint) {
56469 + if (!core_if->multiproc_int_enable) {
56470 + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
56471 + }
56472 + }
56473 + if (gintr_status.b.outepintr) {
56474 + if (!core_if->multiproc_int_enable) {
56475 + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
56476 + }
56477 + }
56478 + if (gintr_status.b.epmismatch) {
56479 + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
56480 + }
56481 + if (gintr_status.b.fetsusp) {
56482 + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
56483 + }
56484 + if (gintr_status.b.ginnakeff) {
56485 + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
56486 + }
56487 + if (gintr_status.b.incomplisoin) {
56488 + retval |=
56489 + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
56490 + }
56491 + if (gintr_status.b.incomplisoout) {
56492 + retval |=
56493 + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
56494 + }
56495 +
56496 + /* In MPI mode Device Endpoints interrupts are asserted
56497 + * without setting outepintr and inepint bits set, so these
56498 + * Interrupt handlers are called without checking these bit-fields
56499 + */
56500 + if (core_if->multiproc_int_enable) {
56501 + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
56502 + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
56503 + }
56504 +#ifdef VERBOSE
56505 + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
56506 + DWC_READ_REG32(&global_regs->gintsts));
56507 +#endif
56508 + DWC_SPINUNLOCK(pcd->lock);
56509 + }
56510 + return retval;
56511 +}
56512 +
56513 +#endif /* DWC_HOST_ONLY */
56514 --- /dev/null
56515 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
56516 @@ -0,0 +1,1262 @@
56517 + /* ==========================================================================
56518 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
56519 + * $Revision: #21 $
56520 + * $Date: 2012/08/10 $
56521 + * $Change: 2047372 $
56522 + *
56523 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
56524 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
56525 + * otherwise expressly agreed to in writing between Synopsys and you.
56526 + *
56527 + * The Software IS NOT an item of Licensed Software or Licensed Product under
56528 + * any End User Software License Agreement or Agreement for Licensed Product
56529 + * with Synopsys or any supplement thereto. You are permitted to use and
56530 + * redistribute this Software in source and binary forms, with or without
56531 + * modification, provided that redistributions of source code must retain this
56532 + * notice. You may not view, use, disclose, copy or distribute this file or
56533 + * any information contained herein except pursuant to this license grant from
56534 + * Synopsys. If you do not agree with this notice, including the disclaimer
56535 + * below, then you are not authorized to use the Software.
56536 + *
56537 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
56538 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56539 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56540 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
56541 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56542 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56543 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56544 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56545 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56546 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
56547 + * DAMAGE.
56548 + * ========================================================================== */
56549 +#ifndef DWC_HOST_ONLY
56550 +
56551 +/** @file
56552 + * This file implements the Peripheral Controller Driver.
56553 + *
56554 + * The Peripheral Controller Driver (PCD) is responsible for
56555 + * translating requests from the Function Driver into the appropriate
56556 + * actions on the DWC_otg controller. It isolates the Function Driver
56557 + * from the specifics of the controller by providing an API to the
56558 + * Function Driver.
56559 + *
56560 + * The Peripheral Controller Driver for Linux will implement the
56561 + * Gadget API, so that the existing Gadget drivers can be used.
56562 + * (Gadget Driver is the Linux terminology for a Function Driver.)
56563 + *
56564 + * The Linux Gadget API is defined in the header file
56565 + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
56566 + * defined in the structure <code>usb_ep_ops</code> and the USB
56567 + * Controller API is defined in the structure
56568 + * <code>usb_gadget_ops</code>.
56569 + *
56570 + */
56571 +
56572 +#include "dwc_otg_os_dep.h"
56573 +#include "dwc_otg_pcd_if.h"
56574 +#include "dwc_otg_pcd.h"
56575 +#include "dwc_otg_driver.h"
56576 +#include "dwc_otg_dbg.h"
56577 +
56578 +extern bool fiq_enable;
56579 +
56580 +static struct gadget_wrapper {
56581 + dwc_otg_pcd_t *pcd;
56582 +
56583 + struct usb_gadget gadget;
56584 + struct usb_gadget_driver *driver;
56585 +
56586 + struct usb_ep ep0;
56587 + struct usb_ep in_ep[16];
56588 + struct usb_ep out_ep[16];
56589 +
56590 +} *gadget_wrapper;
56591 +
56592 +/* Display the contents of the buffer */
56593 +extern void dump_msg(const u8 * buf, unsigned int length);
56594 +/**
56595 + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
56596 + * if the endpoint is not found
56597 + */
56598 +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
56599 +{
56600 + int i;
56601 + if (pcd->ep0.priv == handle) {
56602 + return &pcd->ep0;
56603 + }
56604 +
56605 + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
56606 + if (pcd->in_ep[i].priv == handle)
56607 + return &pcd->in_ep[i];
56608 + if (pcd->out_ep[i].priv == handle)
56609 + return &pcd->out_ep[i];
56610 + }
56611 +
56612 + return NULL;
56613 +}
56614 +
56615 +/* USB Endpoint Operations */
56616 +/*
56617 + * The following sections briefly describe the behavior of the Gadget
56618 + * API endpoint operations implemented in the DWC_otg driver
56619 + * software. Detailed descriptions of the generic behavior of each of
56620 + * these functions can be found in the Linux header file
56621 + * include/linux/usb_gadget.h.
56622 + *
56623 + * The Gadget API provides wrapper functions for each of the function
56624 + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
56625 + * function, which then calls the underlying PCD function. The
56626 + * following sections are named according to the wrapper
56627 + * functions. Within each section, the corresponding DWC_otg PCD
56628 + * function name is specified.
56629 + *
56630 + */
56631 +
56632 +/**
56633 + * This function is called by the Gadget Driver for each EP to be
56634 + * configured for the current configuration (SET_CONFIGURATION).
56635 + *
56636 + * This function initializes the dwc_otg_ep_t data structure, and then
56637 + * calls dwc_otg_ep_activate.
56638 + */
56639 +static int ep_enable(struct usb_ep *usb_ep,
56640 + const struct usb_endpoint_descriptor *ep_desc)
56641 +{
56642 + int retval;
56643 +
56644 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
56645 +
56646 + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
56647 + DWC_WARN("%s, bad ep or descriptor\n", __func__);
56648 + return -EINVAL;
56649 + }
56650 + if (usb_ep == &gadget_wrapper->ep0) {
56651 + DWC_WARN("%s, bad ep(0)\n", __func__);
56652 + return -EINVAL;
56653 + }
56654 +
56655 + /* Check FIFO size? */
56656 + if (!ep_desc->wMaxPacketSize) {
56657 + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
56658 + return -ERANGE;
56659 + }
56660 +
56661 + if (!gadget_wrapper->driver ||
56662 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56663 + DWC_WARN("%s, bogus device state\n", __func__);
56664 + return -ESHUTDOWN;
56665 + }
56666 +
56667 + /* Delete after check - MAS */
56668 +#if 0
56669 + nat = (uint32_t) ep_desc->wMaxPacketSize;
56670 + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
56671 + nat = (nat >> 11) & 0x03;
56672 + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
56673 +#endif
56674 + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
56675 + (const uint8_t *)ep_desc,
56676 + (void *)usb_ep);
56677 + if (retval) {
56678 + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
56679 + return -EINVAL;
56680 + }
56681 +
56682 + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
56683 +
56684 + return 0;
56685 +}
56686 +
56687 +/**
56688 + * This function is called when an EP is disabled due to disconnect or
56689 + * change in configuration. Any pending requests will terminate with a
56690 + * status of -ESHUTDOWN.
56691 + *
56692 + * This function modifies the dwc_otg_ep_t data structure for this EP,
56693 + * and then calls dwc_otg_ep_deactivate.
56694 + */
56695 +static int ep_disable(struct usb_ep *usb_ep)
56696 +{
56697 + int retval;
56698 +
56699 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
56700 + if (!usb_ep) {
56701 + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
56702 + usb_ep ? usb_ep->name : NULL);
56703 + return -EINVAL;
56704 + }
56705 +
56706 + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
56707 + if (retval) {
56708 + retval = -EINVAL;
56709 + }
56710 +
56711 + return retval;
56712 +}
56713 +
56714 +/**
56715 + * This function allocates a request object to use with the specified
56716 + * endpoint.
56717 + *
56718 + * @param ep The endpoint to be used with with the request
56719 + * @param gfp_flags the GFP_* flags to use.
56720 + */
56721 +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
56722 + gfp_t gfp_flags)
56723 +{
56724 + struct usb_request *usb_req;
56725 +
56726 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
56727 + if (0 == ep) {
56728 + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
56729 + return 0;
56730 + }
56731 + usb_req = kzalloc(sizeof(*usb_req), gfp_flags);
56732 + if (0 == usb_req) {
56733 + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
56734 + return 0;
56735 + }
56736 + usb_req->dma = DWC_DMA_ADDR_INVALID;
56737 +
56738 + return usb_req;
56739 +}
56740 +
56741 +/**
56742 + * This function frees a request object.
56743 + *
56744 + * @param ep The endpoint associated with the request
56745 + * @param req The request being freed
56746 + */
56747 +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
56748 +{
56749 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
56750 +
56751 + if (0 == ep || 0 == req) {
56752 + DWC_WARN("%s() %s\n", __func__,
56753 + "Invalid ep or req argument!\n");
56754 + return;
56755 + }
56756 +
56757 + kfree(req);
56758 +}
56759 +
56760 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56761 +/**
56762 + * This function allocates an I/O buffer to be used for a transfer
56763 + * to/from the specified endpoint.
56764 + *
56765 + * @param usb_ep The endpoint to be used with with the request
56766 + * @param bytes The desired number of bytes for the buffer
56767 + * @param dma Pointer to the buffer's DMA address; must be valid
56768 + * @param gfp_flags the GFP_* flags to use.
56769 + * @return address of a new buffer or null is buffer could not be allocated.
56770 + */
56771 +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
56772 + dma_addr_t * dma, gfp_t gfp_flags)
56773 +{
56774 + void *buf;
56775 + dwc_otg_pcd_t *pcd = 0;
56776 +
56777 + pcd = gadget_wrapper->pcd;
56778 +
56779 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
56780 + dma, gfp_flags);
56781 +
56782 + /* Check dword alignment */
56783 + if ((bytes & 0x3UL) != 0) {
56784 + DWC_WARN("%s() Buffer size is not a multiple of"
56785 + "DWORD size (%d)", __func__, bytes);
56786 + }
56787 +
56788 + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
56789 + WARN_ON(!buf);
56790 +
56791 + /* Check dword alignment */
56792 + if (((int)buf & 0x3UL) != 0) {
56793 + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
56794 + __func__, buf);
56795 + }
56796 +
56797 + return buf;
56798 +}
56799 +
56800 +/**
56801 + * This function frees an I/O buffer that was allocated by alloc_buffer.
56802 + *
56803 + * @param usb_ep the endpoint associated with the buffer
56804 + * @param buf address of the buffer
56805 + * @param dma The buffer's DMA address
56806 + * @param bytes The number of bytes of the buffer
56807 + */
56808 +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
56809 + dma_addr_t dma, unsigned bytes)
56810 +{
56811 + dwc_otg_pcd_t *pcd = 0;
56812 +
56813 + pcd = gadget_wrapper->pcd;
56814 +
56815 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
56816 +
56817 + dma_free_coherent(NULL, bytes, buf, dma);
56818 +}
56819 +#endif
56820 +
56821 +/**
56822 + * This function is used to submit an I/O Request to an EP.
56823 + *
56824 + * - When the request completes the request's completion callback
56825 + * is called to return the request to the driver.
56826 + * - An EP, except control EPs, may have multiple requests
56827 + * pending.
56828 + * - Once submitted the request cannot be examined or modified.
56829 + * - Each request is turned into one or more packets.
56830 + * - A BULK EP can queue any amount of data; the transfer is
56831 + * packetized.
56832 + * - Zero length Packets are specified with the request 'zero'
56833 + * flag.
56834 + */
56835 +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
56836 + gfp_t gfp_flags)
56837 +{
56838 + dwc_otg_pcd_t *pcd;
56839 + struct dwc_otg_pcd_ep *ep = NULL;
56840 + int retval = 0, is_isoc_ep = 0;
56841 + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
56842 +
56843 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
56844 + __func__, usb_ep, usb_req, gfp_flags);
56845 +
56846 + if (!usb_req || !usb_req->complete || !usb_req->buf) {
56847 + DWC_WARN("bad params\n");
56848 + return -EINVAL;
56849 + }
56850 +
56851 + if (!usb_ep) {
56852 + DWC_WARN("bad ep\n");
56853 + return -EINVAL;
56854 + }
56855 +
56856 + pcd = gadget_wrapper->pcd;
56857 + if (!gadget_wrapper->driver ||
56858 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56859 + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
56860 + gadget_wrapper->gadget.speed);
56861 + DWC_WARN("bogus device state\n");
56862 + return -ESHUTDOWN;
56863 + }
56864 +
56865 + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
56866 + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
56867 +
56868 + usb_req->status = -EINPROGRESS;
56869 + usb_req->actual = 0;
56870 +
56871 + ep = ep_from_handle(pcd, usb_ep);
56872 + if (ep == NULL)
56873 + is_isoc_ep = 0;
56874 + else
56875 + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
56876 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56877 + dma_addr = usb_req->dma;
56878 +#else
56879 + if (GET_CORE_IF(pcd)->dma_enable) {
56880 + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
56881 + struct device *dev = NULL;
56882 +
56883 + if (otg_dev != NULL)
56884 + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
56885 +
56886 + if (usb_req->length != 0 &&
56887 + usb_req->dma == DWC_DMA_ADDR_INVALID) {
56888 + dma_addr = dma_map_single(dev, usb_req->buf,
56889 + usb_req->length,
56890 + ep->dwc_ep.is_in ?
56891 + DMA_TO_DEVICE:
56892 + DMA_FROM_DEVICE);
56893 + }
56894 + }
56895 +#endif
56896 +
56897 +#ifdef DWC_UTE_PER_IO
56898 + if (is_isoc_ep == 1) {
56899 + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
56900 + usb_req->length, usb_req->zero, usb_req,
56901 + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
56902 + if (retval)
56903 + return -EINVAL;
56904 +
56905 + return 0;
56906 + }
56907 +#endif
56908 + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
56909 + usb_req->length, usb_req->zero, usb_req,
56910 + gfp_flags == GFP_ATOMIC ? 1 : 0);
56911 + if (retval) {
56912 + return -EINVAL;
56913 + }
56914 +
56915 + return 0;
56916 +}
56917 +
56918 +/**
56919 + * This function cancels an I/O request from an EP.
56920 + */
56921 +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
56922 +{
56923 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
56924 +
56925 + if (!usb_ep || !usb_req) {
56926 + DWC_WARN("bad argument\n");
56927 + return -EINVAL;
56928 + }
56929 + if (!gadget_wrapper->driver ||
56930 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56931 + DWC_WARN("bogus device state\n");
56932 + return -ESHUTDOWN;
56933 + }
56934 + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
56935 + return -EINVAL;
56936 + }
56937 +
56938 + return 0;
56939 +}
56940 +
56941 +/**
56942 + * usb_ep_set_halt stalls an endpoint.
56943 + *
56944 + * usb_ep_clear_halt clears an endpoint halt and resets its data
56945 + * toggle.
56946 + *
56947 + * Both of these functions are implemented with the same underlying
56948 + * function. The behavior depends on the value argument.
56949 + *
56950 + * @param[in] usb_ep the Endpoint to halt or clear halt.
56951 + * @param[in] value
56952 + * - 0 means clear_halt.
56953 + * - 1 means set_halt,
56954 + * - 2 means clear stall lock flag.
56955 + * - 3 means set stall lock flag.
56956 + */
56957 +static int ep_halt(struct usb_ep *usb_ep, int value)
56958 +{
56959 + int retval = 0;
56960 +
56961 + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
56962 +
56963 + if (!usb_ep) {
56964 + DWC_WARN("bad ep\n");
56965 + return -EINVAL;
56966 + }
56967 +
56968 + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
56969 + if (retval == -DWC_E_AGAIN) {
56970 + return -EAGAIN;
56971 + } else if (retval) {
56972 + retval = -EINVAL;
56973 + }
56974 +
56975 + return retval;
56976 +}
56977 +
56978 +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
56979 +#if 0
56980 +/**
56981 + * ep_wedge: sets the halt feature and ignores clear requests
56982 + *
56983 + * @usb_ep: the endpoint being wedged
56984 + *
56985 + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
56986 + * requests. If the gadget driver clears the halt status, it will
56987 + * automatically unwedge the endpoint.
56988 + *
56989 + * Returns zero on success, else negative errno. *
56990 + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
56991 + */
56992 +static int ep_wedge(struct usb_ep *usb_ep)
56993 +{
56994 + int retval = 0;
56995 +
56996 + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
56997 +
56998 + if (!usb_ep) {
56999 + DWC_WARN("bad ep\n");
57000 + return -EINVAL;
57001 + }
57002 +
57003 + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
57004 + if (retval == -DWC_E_AGAIN) {
57005 + retval = -EAGAIN;
57006 + } else if (retval) {
57007 + retval = -EINVAL;
57008 + }
57009 +
57010 + return retval;
57011 +}
57012 +#endif
57013 +
57014 +#ifdef DWC_EN_ISOC
57015 +/**
57016 + * This function is used to submit an ISOC Transfer Request to an EP.
57017 + *
57018 + * - Every time a sync period completes the request's completion callback
57019 + * is called to provide data to the gadget driver.
57020 + * - Once submitted the request cannot be modified.
57021 + * - Each request is turned into periodic data packets untill ISO
57022 + * Transfer is stopped..
57023 + */
57024 +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
57025 + gfp_t gfp_flags)
57026 +{
57027 + int retval = 0;
57028 +
57029 + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
57030 + DWC_WARN("bad params\n");
57031 + return -EINVAL;
57032 + }
57033 +
57034 + if (!usb_ep) {
57035 + DWC_PRINTF("bad params\n");
57036 + return -EINVAL;
57037 + }
57038 +
57039 + req->status = -EINPROGRESS;
57040 +
57041 + retval =
57042 + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
57043 + req->buf1, req->dma0, req->dma1,
57044 + req->sync_frame, req->data_pattern_frame,
57045 + req->data_per_frame,
57046 + req->
57047 + flags & USB_REQ_ISO_ASAP ? -1 :
57048 + req->start_frame, req->buf_proc_intrvl,
57049 + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
57050 +
57051 + if (retval) {
57052 + return -EINVAL;
57053 + }
57054 +
57055 + return retval;
57056 +}
57057 +
57058 +/**
57059 + * This function stops ISO EP Periodic Data Transfer.
57060 + */
57061 +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
57062 +{
57063 + int retval = 0;
57064 + if (!usb_ep) {
57065 + DWC_WARN("bad ep\n");
57066 + }
57067 +
57068 + if (!gadget_wrapper->driver ||
57069 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
57070 + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
57071 + gadget_wrapper->gadget.speed);
57072 + DWC_WARN("bogus device state\n");
57073 + }
57074 +
57075 + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
57076 + if (retval) {
57077 + retval = -EINVAL;
57078 + }
57079 +
57080 + return retval;
57081 +}
57082 +
57083 +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
57084 + int packets, gfp_t gfp_flags)
57085 +{
57086 + struct usb_iso_request *pReq = NULL;
57087 + uint32_t req_size;
57088 +
57089 + req_size = sizeof(struct usb_iso_request);
57090 + req_size +=
57091 + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
57092 +
57093 + pReq = kmalloc(req_size, gfp_flags);
57094 + if (!pReq) {
57095 + DWC_WARN("Can't allocate Iso Request\n");
57096 + return 0;
57097 + }
57098 + pReq->iso_packet_desc0 = (void *)(pReq + 1);
57099 +
57100 + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
57101 +
57102 + return pReq;
57103 +}
57104 +
57105 +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
57106 +{
57107 + kfree(req);
57108 +}
57109 +
57110 +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
57111 + .ep_ops = {
57112 + .enable = ep_enable,
57113 + .disable = ep_disable,
57114 +
57115 + .alloc_request = dwc_otg_pcd_alloc_request,
57116 + .free_request = dwc_otg_pcd_free_request,
57117 +
57118 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
57119 + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
57120 + .free_buffer = dwc_otg_pcd_free_buffer,
57121 +#endif
57122 +
57123 + .queue = ep_queue,
57124 + .dequeue = ep_dequeue,
57125 +
57126 + .set_halt = ep_halt,
57127 + .fifo_status = 0,
57128 + .fifo_flush = 0,
57129 + },
57130 + .iso_ep_start = iso_ep_start,
57131 + .iso_ep_stop = iso_ep_stop,
57132 + .alloc_iso_request = alloc_iso_request,
57133 + .free_iso_request = free_iso_request,
57134 +};
57135 +
57136 +#else
57137 +
57138 + int (*enable) (struct usb_ep *ep,
57139 + const struct usb_endpoint_descriptor *desc);
57140 + int (*disable) (struct usb_ep *ep);
57141 +
57142 + struct usb_request *(*alloc_request) (struct usb_ep *ep,
57143 + gfp_t gfp_flags);
57144 + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
57145 +
57146 + int (*queue) (struct usb_ep *ep, struct usb_request *req,
57147 + gfp_t gfp_flags);
57148 + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
57149 +
57150 + int (*set_halt) (struct usb_ep *ep, int value);
57151 + int (*set_wedge) (struct usb_ep *ep);
57152 +
57153 + int (*fifo_status) (struct usb_ep *ep);
57154 + void (*fifo_flush) (struct usb_ep *ep);
57155 +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
57156 + .enable = ep_enable,
57157 + .disable = ep_disable,
57158 +
57159 + .alloc_request = dwc_otg_pcd_alloc_request,
57160 + .free_request = dwc_otg_pcd_free_request,
57161 +
57162 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
57163 + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
57164 + .free_buffer = dwc_otg_pcd_free_buffer,
57165 +#else
57166 + /* .set_wedge = ep_wedge, */
57167 + .set_wedge = NULL, /* uses set_halt instead */
57168 +#endif
57169 +
57170 + .queue = ep_queue,
57171 + .dequeue = ep_dequeue,
57172 +
57173 + .set_halt = ep_halt,
57174 + .fifo_status = 0,
57175 + .fifo_flush = 0,
57176 +
57177 +};
57178 +
57179 +#endif /* _EN_ISOC_ */
57180 +/* Gadget Operations */
57181 +/**
57182 + * The following gadget operations will be implemented in the DWC_otg
57183 + * PCD. Functions in the API that are not described below are not
57184 + * implemented.
57185 + *
57186 + * The Gadget API provides wrapper functions for each of the function
57187 + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
57188 + * wrapper function, which then calls the underlying PCD function. The
57189 + * following sections are named according to the wrapper functions
57190 + * (except for ioctl, which doesn't have a wrapper function). Within
57191 + * each section, the corresponding DWC_otg PCD function name is
57192 + * specified.
57193 + *
57194 + */
57195 +
57196 +/**
57197 + *Gets the USB Frame number of the last SOF.
57198 + */
57199 +static int get_frame_number(struct usb_gadget *gadget)
57200 +{
57201 + struct gadget_wrapper *d;
57202 +
57203 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
57204 +
57205 + if (gadget == 0) {
57206 + return -ENODEV;
57207 + }
57208 +
57209 + d = container_of(gadget, struct gadget_wrapper, gadget);
57210 + return dwc_otg_pcd_get_frame_number(d->pcd);
57211 +}
57212 +
57213 +#ifdef CONFIG_USB_DWC_OTG_LPM
57214 +static int test_lpm_enabled(struct usb_gadget *gadget)
57215 +{
57216 + struct gadget_wrapper *d;
57217 +
57218 + d = container_of(gadget, struct gadget_wrapper, gadget);
57219 +
57220 + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
57221 +}
57222 +#endif
57223 +
57224 +/**
57225 + * Initiates Session Request Protocol (SRP) to wakeup the host if no
57226 + * session is in progress. If a session is already in progress, but
57227 + * the device is suspended, remote wakeup signaling is started.
57228 + *
57229 + */
57230 +static int wakeup(struct usb_gadget *gadget)
57231 +{
57232 + struct gadget_wrapper *d;
57233 +
57234 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
57235 +
57236 + if (gadget == 0) {
57237 + return -ENODEV;
57238 + } else {
57239 + d = container_of(gadget, struct gadget_wrapper, gadget);
57240 + }
57241 + dwc_otg_pcd_wakeup(d->pcd);
57242 + return 0;
57243 +}
57244 +
57245 +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
57246 + .get_frame = get_frame_number,
57247 + .wakeup = wakeup,
57248 +#ifdef CONFIG_USB_DWC_OTG_LPM
57249 + .lpm_support = test_lpm_enabled,
57250 +#endif
57251 + // current versions must always be self-powered
57252 +};
57253 +
57254 +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
57255 +{
57256 + int retval = -DWC_E_NOT_SUPPORTED;
57257 + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
57258 + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
57259 + (struct usb_ctrlrequest
57260 + *)bytes);
57261 + }
57262 +
57263 + if (retval == -ENOTSUPP) {
57264 + retval = -DWC_E_NOT_SUPPORTED;
57265 + } else if (retval < 0) {
57266 + retval = -DWC_E_INVALID;
57267 + }
57268 +
57269 + return retval;
57270 +}
57271 +
57272 +#ifdef DWC_EN_ISOC
57273 +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57274 + void *req_handle, int proc_buf_num)
57275 +{
57276 + int i, packet_count;
57277 + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
57278 + struct usb_iso_request *iso_req = req_handle;
57279 +
57280 + if (proc_buf_num) {
57281 + iso_packet = iso_req->iso_packet_desc1;
57282 + } else {
57283 + iso_packet = iso_req->iso_packet_desc0;
57284 + }
57285 + packet_count =
57286 + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
57287 + for (i = 0; i < packet_count; ++i) {
57288 + int status;
57289 + int actual;
57290 + int offset;
57291 + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
57292 + i, &status, &actual, &offset);
57293 + switch (status) {
57294 + case -DWC_E_NO_DATA:
57295 + status = -ENODATA;
57296 + break;
57297 + default:
57298 + if (status) {
57299 + DWC_PRINTF("unknown status in isoc packet\n");
57300 + }
57301 +
57302 + }
57303 + iso_packet[i].status = status;
57304 + iso_packet[i].offset = offset;
57305 + iso_packet[i].actual_length = actual;
57306 + }
57307 +
57308 + iso_req->status = 0;
57309 + iso_req->process_buffer(ep_handle, iso_req);
57310 +
57311 + return 0;
57312 +}
57313 +#endif /* DWC_EN_ISOC */
57314 +
57315 +#ifdef DWC_UTE_PER_IO
57316 +/**
57317 + * Copy the contents of the extended request to the Linux usb_request's
57318 + * extended part and call the gadget's completion.
57319 + *
57320 + * @param pcd Pointer to the pcd structure
57321 + * @param ep_handle Void pointer to the usb_ep structure
57322 + * @param req_handle Void pointer to the usb_request structure
57323 + * @param status Request status returned from the portable logic
57324 + * @param ereq_port Void pointer to the extended request structure
57325 + * created in the the portable part that contains the
57326 + * results of the processed iso packets.
57327 + */
57328 +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57329 + void *req_handle, int32_t status, void *ereq_port)
57330 +{
57331 + struct dwc_ute_iso_req_ext *ereqorg = NULL;
57332 + struct dwc_iso_xreq_port *ereqport = NULL;
57333 + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
57334 + int i;
57335 + struct usb_request *req;
57336 + //struct dwc_ute_iso_packet_descriptor *
57337 + //int status = 0;
57338 +
57339 + req = (struct usb_request *)req_handle;
57340 + ereqorg = &req->ext_req;
57341 + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
57342 + desc_org = ereqorg->per_io_frame_descs;
57343 +
57344 + if (req && req->complete) {
57345 + /* Copy the request data from the portable logic to our request */
57346 + for (i = 0; i < ereqport->pio_pkt_count; i++) {
57347 + desc_org[i].actual_length =
57348 + ereqport->per_io_frame_descs[i].actual_length;
57349 + desc_org[i].status =
57350 + ereqport->per_io_frame_descs[i].status;
57351 + }
57352 +
57353 + switch (status) {
57354 + case -DWC_E_SHUTDOWN:
57355 + req->status = -ESHUTDOWN;
57356 + break;
57357 + case -DWC_E_RESTART:
57358 + req->status = -ECONNRESET;
57359 + break;
57360 + case -DWC_E_INVALID:
57361 + req->status = -EINVAL;
57362 + break;
57363 + case -DWC_E_TIMEOUT:
57364 + req->status = -ETIMEDOUT;
57365 + break;
57366 + default:
57367 + req->status = status;
57368 + }
57369 +
57370 + /* And call the gadget's completion */
57371 + req->complete(ep_handle, req);
57372 + }
57373 +
57374 + return 0;
57375 +}
57376 +#endif /* DWC_UTE_PER_IO */
57377 +
57378 +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57379 + void *req_handle, int32_t status, uint32_t actual)
57380 +{
57381 + struct usb_request *req = (struct usb_request *)req_handle;
57382 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
57383 + struct dwc_otg_pcd_ep *ep = NULL;
57384 +#endif
57385 +
57386 + if (req && req->complete) {
57387 + switch (status) {
57388 + case -DWC_E_SHUTDOWN:
57389 + req->status = -ESHUTDOWN;
57390 + break;
57391 + case -DWC_E_RESTART:
57392 + req->status = -ECONNRESET;
57393 + break;
57394 + case -DWC_E_INVALID:
57395 + req->status = -EINVAL;
57396 + break;
57397 + case -DWC_E_TIMEOUT:
57398 + req->status = -ETIMEDOUT;
57399 + break;
57400 + default:
57401 + req->status = status;
57402 +
57403 + }
57404 +
57405 + req->actual = actual;
57406 + DWC_SPINUNLOCK(pcd->lock);
57407 + req->complete(ep_handle, req);
57408 + DWC_SPINLOCK(pcd->lock);
57409 + }
57410 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
57411 + ep = ep_from_handle(pcd, ep_handle);
57412 + if (GET_CORE_IF(pcd)->dma_enable) {
57413 + if (req->length != 0) {
57414 + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
57415 + struct device *dev = NULL;
57416 +
57417 + if (otg_dev != NULL)
57418 + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
57419 +
57420 + dma_unmap_single(dev, req->dma, req->length,
57421 + ep->dwc_ep.is_in ?
57422 + DMA_TO_DEVICE: DMA_FROM_DEVICE);
57423 + }
57424 + }
57425 +#endif
57426 +
57427 + return 0;
57428 +}
57429 +
57430 +static int _connect(dwc_otg_pcd_t * pcd, int speed)
57431 +{
57432 + gadget_wrapper->gadget.speed = speed;
57433 + return 0;
57434 +}
57435 +
57436 +static int _disconnect(dwc_otg_pcd_t * pcd)
57437 +{
57438 + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
57439 + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
57440 + }
57441 + return 0;
57442 +}
57443 +
57444 +static int _resume(dwc_otg_pcd_t * pcd)
57445 +{
57446 + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
57447 + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
57448 + }
57449 +
57450 + return 0;
57451 +}
57452 +
57453 +static int _suspend(dwc_otg_pcd_t * pcd)
57454 +{
57455 + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
57456 + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
57457 + }
57458 + return 0;
57459 +}
57460 +
57461 +/**
57462 + * This function updates the otg values in the gadget structure.
57463 + */
57464 +static int _hnp_changed(dwc_otg_pcd_t * pcd)
57465 +{
57466 +
57467 + if (!gadget_wrapper->gadget.is_otg)
57468 + return 0;
57469 +
57470 + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
57471 + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
57472 + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
57473 + return 0;
57474 +}
57475 +
57476 +static int _reset(dwc_otg_pcd_t * pcd)
57477 +{
57478 + return 0;
57479 +}
57480 +
57481 +#ifdef DWC_UTE_CFI
57482 +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
57483 +{
57484 + int retval = -DWC_E_INVALID;
57485 + if (gadget_wrapper->driver->cfi_feature_setup) {
57486 + retval =
57487 + gadget_wrapper->driver->
57488 + cfi_feature_setup(&gadget_wrapper->gadget,
57489 + (struct cfi_usb_ctrlrequest *)cfi_req);
57490 + }
57491 +
57492 + return retval;
57493 +}
57494 +#endif
57495 +
57496 +static const struct dwc_otg_pcd_function_ops fops = {
57497 + .complete = _complete,
57498 +#ifdef DWC_EN_ISOC
57499 + .isoc_complete = _isoc_complete,
57500 +#endif
57501 + .setup = _setup,
57502 + .disconnect = _disconnect,
57503 + .connect = _connect,
57504 + .resume = _resume,
57505 + .suspend = _suspend,
57506 + .hnp_changed = _hnp_changed,
57507 + .reset = _reset,
57508 +#ifdef DWC_UTE_CFI
57509 + .cfi_setup = _cfi_setup,
57510 +#endif
57511 +#ifdef DWC_UTE_PER_IO
57512 + .xisoc_complete = _xisoc_complete,
57513 +#endif
57514 +};
57515 +
57516 +/**
57517 + * This function is the top level PCD interrupt handler.
57518 + */
57519 +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
57520 +{
57521 + dwc_otg_pcd_t *pcd = dev;
57522 + int32_t retval = IRQ_NONE;
57523 +
57524 + retval = dwc_otg_pcd_handle_intr(pcd);
57525 + if (retval != 0) {
57526 + S3C2410X_CLEAR_EINTPEND();
57527 + }
57528 + return IRQ_RETVAL(retval);
57529 +}
57530 +
57531 +/**
57532 + * This function initialized the usb_ep structures to there default
57533 + * state.
57534 + *
57535 + * @param d Pointer on gadget_wrapper.
57536 + */
57537 +void gadget_add_eps(struct gadget_wrapper *d)
57538 +{
57539 + static const char *names[] = {
57540 +
57541 + "ep0",
57542 + "ep1in",
57543 + "ep2in",
57544 + "ep3in",
57545 + "ep4in",
57546 + "ep5in",
57547 + "ep6in",
57548 + "ep7in",
57549 + "ep8in",
57550 + "ep9in",
57551 + "ep10in",
57552 + "ep11in",
57553 + "ep12in",
57554 + "ep13in",
57555 + "ep14in",
57556 + "ep15in",
57557 + "ep1out",
57558 + "ep2out",
57559 + "ep3out",
57560 + "ep4out",
57561 + "ep5out",
57562 + "ep6out",
57563 + "ep7out",
57564 + "ep8out",
57565 + "ep9out",
57566 + "ep10out",
57567 + "ep11out",
57568 + "ep12out",
57569 + "ep13out",
57570 + "ep14out",
57571 + "ep15out"
57572 + };
57573 +
57574 + int i;
57575 + struct usb_ep *ep;
57576 + int8_t dev_endpoints;
57577 +
57578 + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
57579 +
57580 + INIT_LIST_HEAD(&d->gadget.ep_list);
57581 + d->gadget.ep0 = &d->ep0;
57582 + d->gadget.speed = USB_SPEED_UNKNOWN;
57583 +
57584 + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
57585 +
57586 + /**
57587 + * Initialize the EP0 structure.
57588 + */
57589 + ep = &d->ep0;
57590 +
57591 + /* Init the usb_ep structure. */
57592 + ep->name = names[0];
57593 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57594 +
57595 + /**
57596 + * @todo NGS: What should the max packet size be set to
57597 + * here? Before EP type is set?
57598 + */
57599 + ep->maxpacket = MAX_PACKET_SIZE;
57600 + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
57601 +
57602 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57603 +
57604 + /**
57605 + * Initialize the EP structures.
57606 + */
57607 + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
57608 +
57609 + for (i = 0; i < dev_endpoints; i++) {
57610 + ep = &d->in_ep[i];
57611 +
57612 + /* Init the usb_ep structure. */
57613 + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
57614 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57615 +
57616 + /**
57617 + * @todo NGS: What should the max packet size be set to
57618 + * here? Before EP type is set?
57619 + */
57620 + ep->maxpacket = MAX_PACKET_SIZE;
57621 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57622 + }
57623 +
57624 + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
57625 +
57626 + for (i = 0; i < dev_endpoints; i++) {
57627 + ep = &d->out_ep[i];
57628 +
57629 + /* Init the usb_ep structure. */
57630 + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
57631 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57632 +
57633 + /**
57634 + * @todo NGS: What should the max packet size be set to
57635 + * here? Before EP type is set?
57636 + */
57637 + ep->maxpacket = MAX_PACKET_SIZE;
57638 +
57639 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57640 + }
57641 +
57642 + /* remove ep0 from the list. There is a ep0 pointer. */
57643 + list_del_init(&d->ep0.ep_list);
57644 +
57645 + d->ep0.maxpacket = MAX_EP0_SIZE;
57646 +}
57647 +
57648 +/**
57649 + * This function releases the Gadget device.
57650 + * required by device_unregister().
57651 + *
57652 + * @todo Should this do something? Should it free the PCD?
57653 + */
57654 +static void dwc_otg_pcd_gadget_release(struct device *dev)
57655 +{
57656 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
57657 +}
57658 +
57659 +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
57660 +{
57661 + static char pcd_name[] = "dwc_otg_pcd";
57662 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57663 + struct gadget_wrapper *d;
57664 + int retval;
57665 +
57666 + d = DWC_ALLOC(sizeof(*d));
57667 + if (d == NULL) {
57668 + return NULL;
57669 + }
57670 +
57671 + memset(d, 0, sizeof(*d));
57672 +
57673 + d->gadget.name = pcd_name;
57674 + d->pcd = otg_dev->pcd;
57675 +
57676 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
57677 + strcpy(d->gadget.dev.bus_id, "gadget");
57678 +#else
57679 + dev_set_name(&d->gadget.dev, "%s", "gadget");
57680 +#endif
57681 +
57682 + d->gadget.dev.parent = &_dev->dev;
57683 + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
57684 + d->gadget.ops = &dwc_otg_pcd_ops;
57685 + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
57686 + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
57687 +
57688 + d->driver = 0;
57689 + /* Register the gadget device */
57690 + retval = device_register(&d->gadget.dev);
57691 + if (retval != 0) {
57692 + DWC_ERROR("device_register failed\n");
57693 + DWC_FREE(d);
57694 + return NULL;
57695 + }
57696 +
57697 + return d;
57698 +}
57699 +
57700 +static void free_wrapper(struct gadget_wrapper *d)
57701 +{
57702 + if (d->driver) {
57703 + /* should have been done already by driver model core */
57704 + DWC_WARN("driver '%s' is still registered\n",
57705 + d->driver->driver.name);
57706 +#ifdef CONFIG_USB_GADGET
57707 + usb_gadget_unregister_driver(d->driver);
57708 +#endif
57709 + }
57710 +
57711 + device_unregister(&d->gadget.dev);
57712 + DWC_FREE(d);
57713 +}
57714 +
57715 +/**
57716 + * This function initialized the PCD portion of the driver.
57717 + *
57718 + */
57719 +int pcd_init(dwc_bus_dev_t *_dev)
57720 +{
57721 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57722 + int retval = 0;
57723 +
57724 + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
57725 +
57726 + otg_dev->pcd = dwc_otg_pcd_init(otg_dev);
57727 +
57728 + if (!otg_dev->pcd) {
57729 + DWC_ERROR("dwc_otg_pcd_init failed\n");
57730 + return -ENOMEM;
57731 + }
57732 +
57733 + otg_dev->pcd->otg_dev = otg_dev;
57734 + gadget_wrapper = alloc_wrapper(_dev);
57735 +
57736 + /*
57737 + * Initialize EP structures
57738 + */
57739 + gadget_add_eps(gadget_wrapper);
57740 + /*
57741 + * Setup interupt handler
57742 + */
57743 + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
57744 + otg_dev->os_dep.irq_num);
57745 + retval = request_irq(otg_dev->os_dep.irq_num, dwc_otg_pcd_irq,
57746 + IRQF_SHARED, gadget_wrapper->gadget.name,
57747 + otg_dev->pcd);
57748 + if (retval != 0) {
57749 + DWC_ERROR("request of irq%d failed\n", otg_dev->os_dep.irq_num);
57750 + free_wrapper(gadget_wrapper);
57751 + return -EBUSY;
57752 + }
57753 +
57754 + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
57755 +
57756 + return retval;
57757 +}
57758 +
57759 +/**
57760 + * Cleanup the PCD.
57761 + */
57762 +void pcd_remove(dwc_bus_dev_t *_dev)
57763 +{
57764 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57765 + dwc_otg_pcd_t *pcd = otg_dev->pcd;
57766 +
57767 + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
57768 +
57769 + /*
57770 + * Free the IRQ
57771 + */
57772 + free_irq(otg_dev->os_dep.irq_num, pcd);
57773 + dwc_otg_pcd_remove(otg_dev->pcd);
57774 + free_wrapper(gadget_wrapper);
57775 + otg_dev->pcd = 0;
57776 +}
57777 +
57778 +#endif /* DWC_HOST_ONLY */
57779 --- /dev/null
57780 +++ b/drivers/usb/host/dwc_otg/dwc_otg_regs.h
57781 @@ -0,0 +1,2550 @@
57782 +/* ==========================================================================
57783 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
57784 + * $Revision: #98 $
57785 + * $Date: 2012/08/10 $
57786 + * $Change: 2047372 $
57787 + *
57788 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
57789 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
57790 + * otherwise expressly agreed to in writing between Synopsys and you.
57791 + *
57792 + * The Software IS NOT an item of Licensed Software or Licensed Product under
57793 + * any End User Software License Agreement or Agreement for Licensed Product
57794 + * with Synopsys or any supplement thereto. You are permitted to use and
57795 + * redistribute this Software in source and binary forms, with or without
57796 + * modification, provided that redistributions of source code must retain this
57797 + * notice. You may not view, use, disclose, copy or distribute this file or
57798 + * any information contained herein except pursuant to this license grant from
57799 + * Synopsys. If you do not agree with this notice, including the disclaimer
57800 + * below, then you are not authorized to use the Software.
57801 + *
57802 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
57803 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57804 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57805 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
57806 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57807 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57808 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57809 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57810 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57811 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
57812 + * DAMAGE.
57813 + * ========================================================================== */
57814 +
57815 +#ifndef __DWC_OTG_REGS_H__
57816 +#define __DWC_OTG_REGS_H__
57817 +
57818 +#include "dwc_otg_core_if.h"
57819 +
57820 +/**
57821 + * @file
57822 + *
57823 + * This file contains the data structures for accessing the DWC_otg core registers.
57824 + *
57825 + * The application interfaces with the HS OTG core by reading from and
57826 + * writing to the Control and Status Register (CSR) space through the
57827 + * AHB Slave interface. These registers are 32 bits wide, and the
57828 + * addresses are 32-bit-block aligned.
57829 + * CSRs are classified as follows:
57830 + * - Core Global Registers
57831 + * - Device Mode Registers
57832 + * - Device Global Registers
57833 + * - Device Endpoint Specific Registers
57834 + * - Host Mode Registers
57835 + * - Host Global Registers
57836 + * - Host Port CSRs
57837 + * - Host Channel Specific Registers
57838 + *
57839 + * Only the Core Global registers can be accessed in both Device and
57840 + * Host modes. When the HS OTG core is operating in one mode, either
57841 + * Device or Host, the application must not access registers from the
57842 + * other mode. When the core switches from one mode to another, the
57843 + * registers in the new mode of operation must be reprogrammed as they
57844 + * would be after a power-on reset.
57845 + */
57846 +
57847 +/****************************************************************************/
57848 +/** DWC_otg Core registers .
57849 + * The dwc_otg_core_global_regs structure defines the size
57850 + * and relative field offsets for the Core Global registers.
57851 + */
57852 +typedef struct dwc_otg_core_global_regs {
57853 + /** OTG Control and Status Register. <i>Offset: 000h</i> */
57854 + volatile uint32_t gotgctl;
57855 + /** OTG Interrupt Register. <i>Offset: 004h</i> */
57856 + volatile uint32_t gotgint;
57857 + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
57858 + volatile uint32_t gahbcfg;
57859 +
57860 +#define DWC_GLBINTRMASK 0x0001
57861 +#define DWC_DMAENABLE 0x0020
57862 +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
57863 +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
57864 +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
57865 +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
57866 +
57867 + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
57868 + volatile uint32_t gusbcfg;
57869 + /**Core Reset Register. <i>Offset: 010h</i> */
57870 + volatile uint32_t grstctl;
57871 + /**Core Interrupt Register. <i>Offset: 014h</i> */
57872 + volatile uint32_t gintsts;
57873 + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
57874 + volatile uint32_t gintmsk;
57875 + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
57876 + volatile uint32_t grxstsr;
57877 + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
57878 + volatile uint32_t grxstsp;
57879 + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
57880 + volatile uint32_t grxfsiz;
57881 + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
57882 + volatile uint32_t gnptxfsiz;
57883 + /**Non Periodic Transmit FIFO/Queue Status Register (Read
57884 + * Only). <i>Offset: 02Ch</i> */
57885 + volatile uint32_t gnptxsts;
57886 + /**I2C Access Register. <i>Offset: 030h</i> */
57887 + volatile uint32_t gi2cctl;
57888 + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
57889 + volatile uint32_t gpvndctl;
57890 + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
57891 + volatile uint32_t ggpio;
57892 + /**User ID Register. <i>Offset: 03Ch</i> */
57893 + volatile uint32_t guid;
57894 + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
57895 + volatile uint32_t gsnpsid;
57896 + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
57897 + volatile uint32_t ghwcfg1;
57898 + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
57899 + volatile uint32_t ghwcfg2;
57900 +#define DWC_SLAVE_ONLY_ARCH 0
57901 +#define DWC_EXT_DMA_ARCH 1
57902 +#define DWC_INT_DMA_ARCH 2
57903 +
57904 +#define DWC_MODE_HNP_SRP_CAPABLE 0
57905 +#define DWC_MODE_SRP_ONLY_CAPABLE 1
57906 +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
57907 +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
57908 +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
57909 +#define DWC_MODE_SRP_CAPABLE_HOST 5
57910 +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
57911 +
57912 + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
57913 + volatile uint32_t ghwcfg3;
57914 + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
57915 + volatile uint32_t ghwcfg4;
57916 + /** Core LPM Configuration register <i>Offset: 054h</i>*/
57917 + volatile uint32_t glpmcfg;
57918 + /** Global PowerDn Register <i>Offset: 058h</i> */
57919 + volatile uint32_t gpwrdn;
57920 + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
57921 + volatile uint32_t gdfifocfg;
57922 + /** ADP Control Register <i>Offset: 060h</i> */
57923 + volatile uint32_t adpctl;
57924 + /** Reserved <i>Offset: 064h-0FFh</i> */
57925 + volatile uint32_t reserved39[39];
57926 + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
57927 + volatile uint32_t hptxfsiz;
57928 + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
57929 + otherwise Device Transmit FIFO#n Register.
57930 + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
57931 + volatile uint32_t dtxfsiz[15];
57932 +} dwc_otg_core_global_regs_t;
57933 +
57934 +/**
57935 + * This union represents the bit fields of the Core OTG Control
57936 + * and Status Register (GOTGCTL). Set the bits using the bit
57937 + * fields then write the <i>d32</i> value to the register.
57938 + */
57939 +typedef union gotgctl_data {
57940 + /** raw register data */
57941 + uint32_t d32;
57942 + /** register bits */
57943 + struct {
57944 + unsigned sesreqscs:1;
57945 + unsigned sesreq:1;
57946 + unsigned vbvalidoven:1;
57947 + unsigned vbvalidovval:1;
57948 + unsigned avalidoven:1;
57949 + unsigned avalidovval:1;
57950 + unsigned bvalidoven:1;
57951 + unsigned bvalidovval:1;
57952 + unsigned hstnegscs:1;
57953 + unsigned hnpreq:1;
57954 + unsigned hstsethnpen:1;
57955 + unsigned devhnpen:1;
57956 + unsigned reserved12_15:4;
57957 + unsigned conidsts:1;
57958 + unsigned dbnctime:1;
57959 + unsigned asesvld:1;
57960 + unsigned bsesvld:1;
57961 + unsigned otgver:1;
57962 + unsigned reserved1:1;
57963 + unsigned multvalidbc:5;
57964 + unsigned chirpen:1;
57965 + unsigned reserved28_31:4;
57966 + } b;
57967 +} gotgctl_data_t;
57968 +
57969 +/**
57970 + * This union represents the bit fields of the Core OTG Interrupt Register
57971 + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
57972 + * value to the register.
57973 + */
57974 +typedef union gotgint_data {
57975 + /** raw register data */
57976 + uint32_t d32;
57977 + /** register bits */
57978 + struct {
57979 + /** Current Mode */
57980 + unsigned reserved0_1:2;
57981 +
57982 + /** Session End Detected */
57983 + unsigned sesenddet:1;
57984 +
57985 + unsigned reserved3_7:5;
57986 +
57987 + /** Session Request Success Status Change */
57988 + unsigned sesreqsucstschng:1;
57989 + /** Host Negotiation Success Status Change */
57990 + unsigned hstnegsucstschng:1;
57991 +
57992 + unsigned reserved10_16:7;
57993 +
57994 + /** Host Negotiation Detected */
57995 + unsigned hstnegdet:1;
57996 + /** A-Device Timeout Change */
57997 + unsigned adevtoutchng:1;
57998 + /** Debounce Done */
57999 + unsigned debdone:1;
58000 + /** Multi-Valued input changed */
58001 + unsigned mvic:1;
58002 +
58003 + unsigned reserved31_21:11;
58004 +
58005 + } b;
58006 +} gotgint_data_t;
58007 +
58008 +/**
58009 + * This union represents the bit fields of the Core AHB Configuration
58010 + * Register (GAHBCFG). Set/clear the bits using the bit fields then
58011 + * write the <i>d32</i> value to the register.
58012 + */
58013 +typedef union gahbcfg_data {
58014 + /** raw register data */
58015 + uint32_t d32;
58016 + /** register bits */
58017 + struct {
58018 + unsigned glblintrmsk:1;
58019 +#define DWC_GAHBCFG_GLBINT_ENABLE 1
58020 +
58021 + unsigned hburstlen:4;
58022 +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
58023 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
58024 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
58025 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
58026 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
58027 +
58028 + unsigned dmaenable:1;
58029 +#define DWC_GAHBCFG_DMAENABLE 1
58030 + unsigned reserved:1;
58031 + unsigned nptxfemplvl_txfemplvl:1;
58032 + unsigned ptxfemplvl:1;
58033 +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
58034 +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
58035 + unsigned reserved9_20:12;
58036 + unsigned remmemsupp:1;
58037 + unsigned notialldmawrit:1;
58038 + unsigned ahbsingle:1;
58039 + unsigned reserved24_31:8;
58040 + } b;
58041 +} gahbcfg_data_t;
58042 +
58043 +/**
58044 + * This union represents the bit fields of the Core USB Configuration
58045 + * Register (GUSBCFG). Set the bits using the bit fields then write
58046 + * the <i>d32</i> value to the register.
58047 + */
58048 +typedef union gusbcfg_data {
58049 + /** raw register data */
58050 + uint32_t d32;
58051 + /** register bits */
58052 + struct {
58053 + unsigned toutcal:3;
58054 + unsigned phyif:1;
58055 + unsigned ulpi_utmi_sel:1;
58056 + unsigned fsintf:1;
58057 + unsigned physel:1;
58058 + unsigned ddrsel:1;
58059 + unsigned srpcap:1;
58060 + unsigned hnpcap:1;
58061 + unsigned usbtrdtim:4;
58062 + unsigned reserved1:1;
58063 + unsigned phylpwrclksel:1;
58064 + unsigned otgutmifssel:1;
58065 + unsigned ulpi_fsls:1;
58066 + unsigned ulpi_auto_res:1;
58067 + unsigned ulpi_clk_sus_m:1;
58068 + unsigned ulpi_ext_vbus_drv:1;
58069 + unsigned ulpi_int_vbus_indicator:1;
58070 + unsigned term_sel_dl_pulse:1;
58071 + unsigned indicator_complement:1;
58072 + unsigned indicator_pass_through:1;
58073 + unsigned ulpi_int_prot_dis:1;
58074 + unsigned ic_usb_cap:1;
58075 + unsigned ic_traffic_pull_remove:1;
58076 + unsigned tx_end_delay:1;
58077 + unsigned force_host_mode:1;
58078 + unsigned force_dev_mode:1;
58079 + unsigned reserved31:1;
58080 + } b;
58081 +} gusbcfg_data_t;
58082 +
58083 +/**
58084 + * This union represents the bit fields of the Core Reset Register
58085 + * (GRSTCTL). Set/clear the bits using the bit fields then write the
58086 + * <i>d32</i> value to the register.
58087 + */
58088 +typedef union grstctl_data {
58089 + /** raw register data */
58090 + uint32_t d32;
58091 + /** register bits */
58092 + struct {
58093 + /** Core Soft Reset (CSftRst) (Device and Host)
58094 + *
58095 + * The application can flush the control logic in the
58096 + * entire core using this bit. This bit resets the
58097 + * pipelines in the AHB Clock domain as well as the
58098 + * PHY Clock domain.
58099 + *
58100 + * The state machines are reset to an IDLE state, the
58101 + * control bits in the CSRs are cleared, all the
58102 + * transmit FIFOs and the receive FIFO are flushed.
58103 + *
58104 + * The status mask bits that control the generation of
58105 + * the interrupt, are cleared, to clear the
58106 + * interrupt. The interrupt status bits are not
58107 + * cleared, so the application can get the status of
58108 + * any events that occurred in the core after it has
58109 + * set this bit.
58110 + *
58111 + * Any transactions on the AHB are terminated as soon
58112 + * as possible following the protocol. Any
58113 + * transactions on the USB are terminated immediately.
58114 + *
58115 + * The configuration settings in the CSRs are
58116 + * unchanged, so the software doesn't have to
58117 + * reprogram these registers (Device
58118 + * Configuration/Host Configuration/Core System
58119 + * Configuration/Core PHY Configuration).
58120 + *
58121 + * The application can write to this bit, any time it
58122 + * wants to reset the core. This is a self clearing
58123 + * bit and the core clears this bit after all the
58124 + * necessary logic is reset in the core, which may
58125 + * take several clocks, depending on the current state
58126 + * of the core.
58127 + */
58128 + unsigned csftrst:1;
58129 + /** Hclk Soft Reset
58130 + *
58131 + * The application uses this bit to reset the control logic in
58132 + * the AHB clock domain. Only AHB clock domain pipelines are
58133 + * reset.
58134 + */
58135 + unsigned hsftrst:1;
58136 + /** Host Frame Counter Reset (Host Only)<br>
58137 + *
58138 + * The application can reset the (micro)frame number
58139 + * counter inside the core, using this bit. When the
58140 + * (micro)frame counter is reset, the subsequent SOF
58141 + * sent out by the core, will have a (micro)frame
58142 + * number of 0.
58143 + */
58144 + unsigned hstfrm:1;
58145 + /** In Token Sequence Learning Queue Flush
58146 + * (INTknQFlsh) (Device Only)
58147 + */
58148 + unsigned intknqflsh:1;
58149 + /** RxFIFO Flush (RxFFlsh) (Device and Host)
58150 + *
58151 + * The application can flush the entire Receive FIFO
58152 + * using this bit. The application must first
58153 + * ensure that the core is not in the middle of a
58154 + * transaction. The application should write into
58155 + * this bit, only after making sure that neither the
58156 + * DMA engine is reading from the RxFIFO nor the MAC
58157 + * is writing the data in to the FIFO. The
58158 + * application should wait until the bit is cleared
58159 + * before performing any other operations. This bit
58160 + * will takes 8 clocks (slowest of PHY or AHB clock)
58161 + * to clear.
58162 + */
58163 + unsigned rxfflsh:1;
58164 + /** TxFIFO Flush (TxFFlsh) (Device and Host).
58165 + *
58166 + * This bit is used to selectively flush a single or
58167 + * all transmit FIFOs. The application must first
58168 + * ensure that the core is not in the middle of a
58169 + * transaction. The application should write into
58170 + * this bit, only after making sure that neither the
58171 + * DMA engine is writing into the TxFIFO nor the MAC
58172 + * is reading the data out of the FIFO. The
58173 + * application should wait until the core clears this
58174 + * bit, before performing any operations. This bit
58175 + * will takes 8 clocks (slowest of PHY or AHB clock)
58176 + * to clear.
58177 + */
58178 + unsigned txfflsh:1;
58179 +
58180 + /** TxFIFO Number (TxFNum) (Device and Host).
58181 + *
58182 + * This is the FIFO number which needs to be flushed,
58183 + * using the TxFIFO Flush bit. This field should not
58184 + * be changed until the TxFIFO Flush bit is cleared by
58185 + * the core.
58186 + * - 0x0 : Non Periodic TxFIFO Flush
58187 + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
58188 + * or Periodic TxFIFO in host mode
58189 + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
58190 + * - ...
58191 + * - 0xF : Periodic TxFIFO #15 Flush in device mode
58192 + * - 0x10: Flush all the Transmit NonPeriodic and
58193 + * Transmit Periodic FIFOs in the core
58194 + */
58195 + unsigned txfnum:5;
58196 + /** Reserved */
58197 + unsigned reserved11_29:19;
58198 + /** DMA Request Signal. Indicated DMA request is in
58199 + * probress. Used for debug purpose. */
58200 + unsigned dmareq:1;
58201 + /** AHB Master Idle. Indicates the AHB Master State
58202 + * Machine is in IDLE condition. */
58203 + unsigned ahbidle:1;
58204 + } b;
58205 +} grstctl_t;
58206 +
58207 +/**
58208 + * This union represents the bit fields of the Core Interrupt Mask
58209 + * Register (GINTMSK). Set/clear the bits using the bit fields then
58210 + * write the <i>d32</i> value to the register.
58211 + */
58212 +typedef union gintmsk_data {
58213 + /** raw register data */
58214 + uint32_t d32;
58215 + /** register bits */
58216 + struct {
58217 + unsigned reserved0:1;
58218 + unsigned modemismatch:1;
58219 + unsigned otgintr:1;
58220 + unsigned sofintr:1;
58221 + unsigned rxstsqlvl:1;
58222 + unsigned nptxfempty:1;
58223 + unsigned ginnakeff:1;
58224 + unsigned goutnakeff:1;
58225 + unsigned ulpickint:1;
58226 + unsigned i2cintr:1;
58227 + unsigned erlysuspend:1;
58228 + unsigned usbsuspend:1;
58229 + unsigned usbreset:1;
58230 + unsigned enumdone:1;
58231 + unsigned isooutdrop:1;
58232 + unsigned eopframe:1;
58233 + unsigned restoredone:1;
58234 + unsigned epmismatch:1;
58235 + unsigned inepintr:1;
58236 + unsigned outepintr:1;
58237 + unsigned incomplisoin:1;
58238 + unsigned incomplisoout:1;
58239 + unsigned fetsusp:1;
58240 + unsigned resetdet:1;
58241 + unsigned portintr:1;
58242 + unsigned hcintr:1;
58243 + unsigned ptxfempty:1;
58244 + unsigned lpmtranrcvd:1;
58245 + unsigned conidstschng:1;
58246 + unsigned disconnect:1;
58247 + unsigned sessreqintr:1;
58248 + unsigned wkupintr:1;
58249 + } b;
58250 +} gintmsk_data_t;
58251 +/**
58252 + * This union represents the bit fields of the Core Interrupt Register
58253 + * (GINTSTS). Set/clear the bits using the bit fields then write the
58254 + * <i>d32</i> value to the register.
58255 + */
58256 +typedef union gintsts_data {
58257 + /** raw register data */
58258 + uint32_t d32;
58259 +#define DWC_SOF_INTR_MASK 0x0008
58260 + /** register bits */
58261 + struct {
58262 +#define DWC_HOST_MODE 1
58263 + unsigned curmode:1;
58264 + unsigned modemismatch:1;
58265 + unsigned otgintr:1;
58266 + unsigned sofintr:1;
58267 + unsigned rxstsqlvl:1;
58268 + unsigned nptxfempty:1;
58269 + unsigned ginnakeff:1;
58270 + unsigned goutnakeff:1;
58271 + unsigned ulpickint:1;
58272 + unsigned i2cintr:1;
58273 + unsigned erlysuspend:1;
58274 + unsigned usbsuspend:1;
58275 + unsigned usbreset:1;
58276 + unsigned enumdone:1;
58277 + unsigned isooutdrop:1;
58278 + unsigned eopframe:1;
58279 + unsigned restoredone:1;
58280 + unsigned epmismatch:1;
58281 + unsigned inepint:1;
58282 + unsigned outepintr:1;
58283 + unsigned incomplisoin:1;
58284 + unsigned incomplisoout:1;
58285 + unsigned fetsusp:1;
58286 + unsigned resetdet:1;
58287 + unsigned portintr:1;
58288 + unsigned hcintr:1;
58289 + unsigned ptxfempty:1;
58290 + unsigned lpmtranrcvd:1;
58291 + unsigned conidstschng:1;
58292 + unsigned disconnect:1;
58293 + unsigned sessreqintr:1;
58294 + unsigned wkupintr:1;
58295 + } b;
58296 +} gintsts_data_t;
58297 +
58298 +/**
58299 + * This union represents the bit fields in the Device Receive Status Read and
58300 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
58301 + * element then read out the bits using the <i>b</i>it elements.
58302 + */
58303 +typedef union device_grxsts_data {
58304 + /** raw register data */
58305 + uint32_t d32;
58306 + /** register bits */
58307 + struct {
58308 + unsigned epnum:4;
58309 + unsigned bcnt:11;
58310 + unsigned dpid:2;
58311 +
58312 +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
58313 +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
58314 +
58315 +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
58316 +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
58317 +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
58318 + unsigned pktsts:4;
58319 + unsigned fn:4;
58320 + unsigned reserved25_31:7;
58321 + } b;
58322 +} device_grxsts_data_t;
58323 +
58324 +/**
58325 + * This union represents the bit fields in the Host Receive Status Read and
58326 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
58327 + * element then read out the bits using the <i>b</i>it elements.
58328 + */
58329 +typedef union host_grxsts_data {
58330 + /** raw register data */
58331 + uint32_t d32;
58332 + /** register bits */
58333 + struct {
58334 + unsigned chnum:4;
58335 + unsigned bcnt:11;
58336 + unsigned dpid:2;
58337 +
58338 + unsigned pktsts:4;
58339 +#define DWC_GRXSTS_PKTSTS_IN 0x2
58340 +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
58341 +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
58342 +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
58343 +
58344 + unsigned reserved21_31:11;
58345 + } b;
58346 +} host_grxsts_data_t;
58347 +
58348 +/**
58349 + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
58350 + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
58351 + * then read out the bits using the <i>b</i>it elements.
58352 + */
58353 +typedef union fifosize_data {
58354 + /** raw register data */
58355 + uint32_t d32;
58356 + /** register bits */
58357 + struct {
58358 + unsigned startaddr:16;
58359 + unsigned depth:16;
58360 + } b;
58361 +} fifosize_data_t;
58362 +
58363 +/**
58364 + * This union represents the bit fields in the Non-Periodic Transmit
58365 + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
58366 + * <i>d32</i> element then read out the bits using the <i>b</i>it
58367 + * elements.
58368 + */
58369 +typedef union gnptxsts_data {
58370 + /** raw register data */
58371 + uint32_t d32;
58372 + /** register bits */
58373 + struct {
58374 + unsigned nptxfspcavail:16;
58375 + unsigned nptxqspcavail:8;
58376 + /** Top of the Non-Periodic Transmit Request Queue
58377 + * - bit 24 - Terminate (Last entry for the selected
58378 + * channel/EP)
58379 + * - bits 26:25 - Token Type
58380 + * - 2'b00 - IN/OUT
58381 + * - 2'b01 - Zero Length OUT
58382 + * - 2'b10 - PING/Complete Split
58383 + * - 2'b11 - Channel Halt
58384 + * - bits 30:27 - Channel/EP Number
58385 + */
58386 + unsigned nptxqtop_terminate:1;
58387 + unsigned nptxqtop_token:2;
58388 + unsigned nptxqtop_chnep:4;
58389 + unsigned reserved:1;
58390 + } b;
58391 +} gnptxsts_data_t;
58392 +
58393 +/**
58394 + * This union represents the bit fields in the Transmit
58395 + * FIFO Status Register (DTXFSTS). Read the register into the
58396 + * <i>d32</i> element then read out the bits using the <i>b</i>it
58397 + * elements.
58398 + */
58399 +typedef union dtxfsts_data {
58400 + /** raw register data */
58401 + uint32_t d32;
58402 + /** register bits */
58403 + struct {
58404 + unsigned txfspcavail:16;
58405 + unsigned reserved:16;
58406 + } b;
58407 +} dtxfsts_data_t;
58408 +
58409 +/**
58410 + * This union represents the bit fields in the I2C Control Register
58411 + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
58412 + * bits using the <i>b</i>it elements.
58413 + */
58414 +typedef union gi2cctl_data {
58415 + /** raw register data */
58416 + uint32_t d32;
58417 + /** register bits */
58418 + struct {
58419 + unsigned rwdata:8;
58420 + unsigned regaddr:8;
58421 + unsigned addr:7;
58422 + unsigned i2cen:1;
58423 + unsigned ack:1;
58424 + unsigned i2csuspctl:1;
58425 + unsigned i2cdevaddr:2;
58426 + unsigned i2cdatse0:1;
58427 + unsigned reserved:1;
58428 + unsigned rw:1;
58429 + unsigned bsydne:1;
58430 + } b;
58431 +} gi2cctl_data_t;
58432 +
58433 +/**
58434 + * This union represents the bit fields in the PHY Vendor Control Register
58435 + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
58436 + * bits using the <i>b</i>it elements.
58437 + */
58438 +typedef union gpvndctl_data {
58439 + /** raw register data */
58440 + uint32_t d32;
58441 + /** register bits */
58442 + struct {
58443 + unsigned regdata:8;
58444 + unsigned vctrl:8;
58445 + unsigned regaddr16_21:6;
58446 + unsigned regwr:1;
58447 + unsigned reserved23_24:2;
58448 + unsigned newregreq:1;
58449 + unsigned vstsbsy:1;
58450 + unsigned vstsdone:1;
58451 + unsigned reserved28_30:3;
58452 + unsigned disulpidrvr:1;
58453 + } b;
58454 +} gpvndctl_data_t;
58455 +
58456 +/**
58457 + * This union represents the bit fields in the General Purpose
58458 + * Input/Output Register (GGPIO).
58459 + * Read the register into the <i>d32</i> element then read out the
58460 + * bits using the <i>b</i>it elements.
58461 + */
58462 +typedef union ggpio_data {
58463 + /** raw register data */
58464 + uint32_t d32;
58465 + /** register bits */
58466 + struct {
58467 + unsigned gpi:16;
58468 + unsigned gpo:16;
58469 + } b;
58470 +} ggpio_data_t;
58471 +
58472 +/**
58473 + * This union represents the bit fields in the User ID Register
58474 + * (GUID). Read the register into the <i>d32</i> element then read out the
58475 + * bits using the <i>b</i>it elements.
58476 + */
58477 +typedef union guid_data {
58478 + /** raw register data */
58479 + uint32_t d32;
58480 + /** register bits */
58481 + struct {
58482 + unsigned rwdata:32;
58483 + } b;
58484 +} guid_data_t;
58485 +
58486 +/**
58487 + * This union represents the bit fields in the Synopsys ID Register
58488 + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
58489 + * bits using the <i>b</i>it elements.
58490 + */
58491 +typedef union gsnpsid_data {
58492 + /** raw register data */
58493 + uint32_t d32;
58494 + /** register bits */
58495 + struct {
58496 + unsigned rwdata:32;
58497 + } b;
58498 +} gsnpsid_data_t;
58499 +
58500 +/**
58501 + * This union represents the bit fields in the User HW Config1
58502 + * Register. Read the register into the <i>d32</i> element then read
58503 + * out the bits using the <i>b</i>it elements.
58504 + */
58505 +typedef union hwcfg1_data {
58506 + /** raw register data */
58507 + uint32_t d32;
58508 + /** register bits */
58509 + struct {
58510 + unsigned ep_dir0:2;
58511 + unsigned ep_dir1:2;
58512 + unsigned ep_dir2:2;
58513 + unsigned ep_dir3:2;
58514 + unsigned ep_dir4:2;
58515 + unsigned ep_dir5:2;
58516 + unsigned ep_dir6:2;
58517 + unsigned ep_dir7:2;
58518 + unsigned ep_dir8:2;
58519 + unsigned ep_dir9:2;
58520 + unsigned ep_dir10:2;
58521 + unsigned ep_dir11:2;
58522 + unsigned ep_dir12:2;
58523 + unsigned ep_dir13:2;
58524 + unsigned ep_dir14:2;
58525 + unsigned ep_dir15:2;
58526 + } b;
58527 +} hwcfg1_data_t;
58528 +
58529 +/**
58530 + * This union represents the bit fields in the User HW Config2
58531 + * Register. Read the register into the <i>d32</i> element then read
58532 + * out the bits using the <i>b</i>it elements.
58533 + */
58534 +typedef union hwcfg2_data {
58535 + /** raw register data */
58536 + uint32_t d32;
58537 + /** register bits */
58538 + struct {
58539 + /* GHWCFG2 */
58540 + unsigned op_mode:3;
58541 +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
58542 +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
58543 +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
58544 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
58545 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
58546 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
58547 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
58548 +
58549 + unsigned architecture:2;
58550 + unsigned point2point:1;
58551 + unsigned hs_phy_type:2;
58552 +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
58553 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
58554 +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
58555 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
58556 +
58557 + unsigned fs_phy_type:2;
58558 + unsigned num_dev_ep:4;
58559 + unsigned num_host_chan:4;
58560 + unsigned perio_ep_supported:1;
58561 + unsigned dynamic_fifo:1;
58562 + unsigned multi_proc_int:1;
58563 + unsigned reserved21:1;
58564 + unsigned nonperio_tx_q_depth:2;
58565 + unsigned host_perio_tx_q_depth:2;
58566 + unsigned dev_token_q_depth:5;
58567 + unsigned otg_enable_ic_usb:1;
58568 + } b;
58569 +} hwcfg2_data_t;
58570 +
58571 +/**
58572 + * This union represents the bit fields in the User HW Config3
58573 + * Register. Read the register into the <i>d32</i> element then read
58574 + * out the bits using the <i>b</i>it elements.
58575 + */
58576 +typedef union hwcfg3_data {
58577 + /** raw register data */
58578 + uint32_t d32;
58579 + /** register bits */
58580 + struct {
58581 + /* GHWCFG3 */
58582 + unsigned xfer_size_cntr_width:4;
58583 + unsigned packet_size_cntr_width:3;
58584 + unsigned otg_func:1;
58585 + unsigned i2c:1;
58586 + unsigned vendor_ctrl_if:1;
58587 + unsigned optional_features:1;
58588 + unsigned synch_reset_type:1;
58589 + unsigned adp_supp:1;
58590 + unsigned otg_enable_hsic:1;
58591 + unsigned bc_support:1;
58592 + unsigned otg_lpm_en:1;
58593 + unsigned dfifo_depth:16;
58594 + } b;
58595 +} hwcfg3_data_t;
58596 +
58597 +/**
58598 + * This union represents the bit fields in the User HW Config4
58599 + * Register. Read the register into the <i>d32</i> element then read
58600 + * out the bits using the <i>b</i>it elements.
58601 + */
58602 +typedef union hwcfg4_data {
58603 + /** raw register data */
58604 + uint32_t d32;
58605 + /** register bits */
58606 + struct {
58607 + unsigned num_dev_perio_in_ep:4;
58608 + unsigned power_optimiz:1;
58609 + unsigned min_ahb_freq:1;
58610 + unsigned hiber:1;
58611 + unsigned xhiber:1;
58612 + unsigned reserved:6;
58613 + unsigned utmi_phy_data_width:2;
58614 + unsigned num_dev_mode_ctrl_ep:4;
58615 + unsigned iddig_filt_en:1;
58616 + unsigned vbus_valid_filt_en:1;
58617 + unsigned a_valid_filt_en:1;
58618 + unsigned b_valid_filt_en:1;
58619 + unsigned session_end_filt_en:1;
58620 + unsigned ded_fifo_en:1;
58621 + unsigned num_in_eps:4;
58622 + unsigned desc_dma:1;
58623 + unsigned desc_dma_dyn:1;
58624 + } b;
58625 +} hwcfg4_data_t;
58626 +
58627 +/**
58628 + * This union represents the bit fields of the Core LPM Configuration
58629 + * Register (GLPMCFG). Set the bits using bit fields then write
58630 + * the <i>d32</i> value to the register.
58631 + */
58632 +typedef union glpmctl_data {
58633 + /** raw register data */
58634 + uint32_t d32;
58635 + /** register bits */
58636 + struct {
58637 + /** LPM-Capable (LPMCap) (Device and Host)
58638 + * The application uses this bit to control
58639 + * the DWC_otg core LPM capabilities.
58640 + */
58641 + unsigned lpm_cap_en:1;
58642 + /** LPM response programmed by application (AppL1Res) (Device)
58643 + * Handshake response to LPM token pre-programmed
58644 + * by device application software.
58645 + */
58646 + unsigned appl_resp:1;
58647 + /** Host Initiated Resume Duration (HIRD) (Device and Host)
58648 + * In Host mode this field indicates the value of HIRD
58649 + * to be sent in an LPM transaction.
58650 + * In Device mode this field is updated with the
58651 + * Received LPM Token HIRD bmAttribute
58652 + * when an ACK/NYET/STALL response is sent
58653 + * to an LPM transaction.
58654 + */
58655 + unsigned hird:4;
58656 + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
58657 + * In Host mode this bit indicates the value of remote
58658 + * wake up to be sent in wIndex field of LPM transaction.
58659 + * In Device mode this field is updated with the
58660 + * Received LPM Token bRemoteWake bmAttribute
58661 + * when an ACK/NYET/STALL response is sent
58662 + * to an LPM transaction.
58663 + */
58664 + unsigned rem_wkup_en:1;
58665 + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
58666 + * The application uses this bit to control
58667 + * the utmi_sleep_n assertion to the PHY when in L1 state.
58668 + */
58669 + unsigned en_utmi_sleep:1;
58670 + /** HIRD Threshold (HIRD_Thres) (Device and Host)
58671 + */
58672 + unsigned hird_thres:5;
58673 + /** LPM Response (CoreL1Res) (Device and Host)
58674 + * In Host mode this bit contains handsake response to
58675 + * LPM transaction.
58676 + * In Device mode the response of the core to
58677 + * LPM transaction received is reflected in these two bits.
58678 + - 0x0 : ERROR (No handshake response)
58679 + - 0x1 : STALL
58680 + - 0x2 : NYET
58681 + - 0x3 : ACK
58682 + */
58683 + unsigned lpm_resp:2;
58684 + /** Port Sleep Status (SlpSts) (Device and Host)
58685 + * This bit is set as long as a Sleep condition
58686 + * is present on the USB bus.
58687 + */
58688 + unsigned prt_sleep_sts:1;
58689 + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
58690 + * Indicates that the application or host
58691 + * can start resume from Sleep state.
58692 + */
58693 + unsigned sleep_state_resumeok:1;
58694 + /** LPM channel Index (LPM_Chnl_Indx) (Host)
58695 + * The channel number on which the LPM transaction
58696 + * has to be applied while sending
58697 + * an LPM transaction to the local device.
58698 + */
58699 + unsigned lpm_chan_index:4;
58700 + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
58701 + * Number host retries that would be performed
58702 + * if the device response was not valid response.
58703 + */
58704 + unsigned retry_count:3;
58705 + /** Send LPM Transaction (SndLPM) (Host)
58706 + * When set by application software,
58707 + * an LPM transaction containing two tokens
58708 + * is sent.
58709 + */
58710 + unsigned send_lpm:1;
58711 + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
58712 + * Number of LPM Host Retries still remaining
58713 + * to be transmitted for the current LPM sequence
58714 + */
58715 + unsigned retry_count_sts:3;
58716 + unsigned reserved28_29:2;
58717 + /** In host mode once this bit is set, the host
58718 + * configures to drive the HSIC Idle state on the bus.
58719 + * It then waits for the device to initiate the Connect sequence.
58720 + * In device mode once this bit is set, the device waits for
58721 + * the HSIC Idle line state on the bus. Upon receving the Idle
58722 + * line state, it initiates the HSIC Connect sequence.
58723 + */
58724 + unsigned hsic_connect:1;
58725 + /** This bit overrides and functionally inverts
58726 + * the if_select_hsic input port signal.
58727 + */
58728 + unsigned inv_sel_hsic:1;
58729 + } b;
58730 +} glpmcfg_data_t;
58731 +
58732 +/**
58733 + * This union represents the bit fields of the Core ADP Timer, Control and
58734 + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
58735 + * the <i>d32</i> value to the register.
58736 + */
58737 +typedef union adpctl_data {
58738 + /** raw register data */
58739 + uint32_t d32;
58740 + /** register bits */
58741 + struct {
58742 + /** Probe Discharge (PRB_DSCHG)
58743 + * These bits set the times for TADP_DSCHG.
58744 + * These bits are defined as follows:
58745 + * 2'b00 - 4 msec
58746 + * 2'b01 - 8 msec
58747 + * 2'b10 - 16 msec
58748 + * 2'b11 - 32 msec
58749 + */
58750 + unsigned prb_dschg:2;
58751 + /** Probe Delta (PRB_DELTA)
58752 + * These bits set the resolution for RTIM value.
58753 + * The bits are defined in units of 32 kHz clock cycles as follows:
58754 + * 2'b00 - 1 cycles
58755 + * 2'b01 - 2 cycles
58756 + * 2'b10 - 3 cycles
58757 + * 2'b11 - 4 cycles
58758 + * For example if this value is chosen to 2'b01, it means that RTIM
58759 + * increments for every 3(three) 32Khz clock cycles.
58760 + */
58761 + unsigned prb_delta:2;
58762 + /** Probe Period (PRB_PER)
58763 + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
58764 + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
58765 + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
58766 + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
58767 + * 2'b11 - Reserved
58768 + */
58769 + unsigned prb_per:2;
58770 + /** These bits capture the latest time it took for VBUS to ramp from
58771 + * VADP_SINK to VADP_PRB.
58772 + * 0x000 - 1 cycles
58773 + * 0x001 - 2 cycles
58774 + * 0x002 - 3 cycles
58775 + * etc
58776 + * 0x7FF - 2048 cycles
58777 + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
58778 + */
58779 + unsigned rtim:11;
58780 + /** Enable Probe (EnaPrb)
58781 + * When programmed to 1'b1, the core performs a probe operation.
58782 + * This bit is valid only if OTG_Ver = 1'b1.
58783 + */
58784 + unsigned enaprb:1;
58785 + /** Enable Sense (EnaSns)
58786 + * When programmed to 1'b1, the core performs a Sense operation.
58787 + * This bit is valid only if OTG_Ver = 1'b1.
58788 + */
58789 + unsigned enasns:1;
58790 + /** ADP Reset (ADPRes)
58791 + * When set, ADP controller is reset.
58792 + * This bit is valid only if OTG_Ver = 1'b1.
58793 + */
58794 + unsigned adpres:1;
58795 + /** ADP Enable (ADPEn)
58796 + * When set, the core performs either ADP probing or sensing
58797 + * based on EnaPrb or EnaSns.
58798 + * This bit is valid only if OTG_Ver = 1'b1.
58799 + */
58800 + unsigned adpen:1;
58801 + /** ADP Probe Interrupt (ADP_PRB_INT)
58802 + * When this bit is set, it means that the VBUS
58803 + * voltage is greater than VADP_PRB or VADP_PRB is reached.
58804 + * This bit is valid only if OTG_Ver = 1'b1.
58805 + */
58806 + unsigned adp_prb_int:1;
58807 + /**
58808 + * ADP Sense Interrupt (ADP_SNS_INT)
58809 + * When this bit is set, it means that the VBUS voltage is greater than
58810 + * VADP_SNS value or VADP_SNS is reached.
58811 + * This bit is valid only if OTG_Ver = 1'b1.
58812 + */
58813 + unsigned adp_sns_int:1;
58814 + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
58815 + * This bit is relevant only for an ADP probe.
58816 + * When this bit is set, it means that the ramp time has
58817 + * completed ie ADPCTL.RTIM has reached its terminal value
58818 + * of 0x7FF. This is a debug feature that allows software
58819 + * to read the ramp time after each cycle.
58820 + * This bit is valid only if OTG_Ver = 1'b1.
58821 + */
58822 + unsigned adp_tmout_int:1;
58823 + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
58824 + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
58825 + * This bit is valid only if OTG_Ver = 1'b1.
58826 + */
58827 + unsigned adp_prb_int_msk:1;
58828 + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
58829 + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
58830 + * This bit is valid only if OTG_Ver = 1'b1.
58831 + */
58832 + unsigned adp_sns_int_msk:1;
58833 + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
58834 + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
58835 + * This bit is valid only if OTG_Ver = 1'b1.
58836 + */
58837 + unsigned adp_tmout_int_msk:1;
58838 + /** Access Request
58839 + * 2'b00 - Read/Write Valid (updated by the core)
58840 + * 2'b01 - Read
58841 + * 2'b00 - Write
58842 + * 2'b00 - Reserved
58843 + */
58844 + unsigned ar:2;
58845 + /** Reserved */
58846 + unsigned reserved29_31:3;
58847 + } b;
58848 +} adpctl_data_t;
58849 +
58850 +////////////////////////////////////////////
58851 +// Device Registers
58852 +/**
58853 + * Device Global Registers. <i>Offsets 800h-BFFh</i>
58854 + *
58855 + * The following structures define the size and relative field offsets
58856 + * for the Device Mode Registers.
58857 + *
58858 + * <i>These registers are visible only in Device mode and must not be
58859 + * accessed in Host mode, as the results are unknown.</i>
58860 + */
58861 +typedef struct dwc_otg_dev_global_regs {
58862 + /** Device Configuration Register. <i>Offset 800h</i> */
58863 + volatile uint32_t dcfg;
58864 + /** Device Control Register. <i>Offset: 804h</i> */
58865 + volatile uint32_t dctl;
58866 + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
58867 + volatile uint32_t dsts;
58868 + /** Reserved. <i>Offset: 80Ch</i> */
58869 + uint32_t unused;
58870 + /** Device IN Endpoint Common Interrupt Mask
58871 + * Register. <i>Offset: 810h</i> */
58872 + volatile uint32_t diepmsk;
58873 + /** Device OUT Endpoint Common Interrupt Mask
58874 + * Register. <i>Offset: 814h</i> */
58875 + volatile uint32_t doepmsk;
58876 + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
58877 + volatile uint32_t daint;
58878 + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
58879 + * 81Ch</i> */
58880 + volatile uint32_t daintmsk;
58881 + /** Device IN Token Queue Read Register-1 (Read Only).
58882 + * <i>Offset: 820h</i> */
58883 + volatile uint32_t dtknqr1;
58884 + /** Device IN Token Queue Read Register-2 (Read Only).
58885 + * <i>Offset: 824h</i> */
58886 + volatile uint32_t dtknqr2;
58887 + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
58888 + volatile uint32_t dvbusdis;
58889 + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
58890 + volatile uint32_t dvbuspulse;
58891 + /** Device IN Token Queue Read Register-3 (Read Only). /
58892 + * Device Thresholding control register (Read/Write)
58893 + * <i>Offset: 830h</i> */
58894 + volatile uint32_t dtknqr3_dthrctl;
58895 + /** Device IN Token Queue Read Register-4 (Read Only). /
58896 + * Device IN EPs empty Inr. Mask Register (Read/Write)
58897 + * <i>Offset: 834h</i> */
58898 + volatile uint32_t dtknqr4_fifoemptymsk;
58899 + /** Device Each Endpoint Interrupt Register (Read Only). /
58900 + * <i>Offset: 838h</i> */
58901 + volatile uint32_t deachint;
58902 + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
58903 + * <i>Offset: 83Ch</i> */
58904 + volatile uint32_t deachintmsk;
58905 + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
58906 + * <i>Offset: 840h</i> */
58907 + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
58908 + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
58909 + * <i>Offset: 880h</i> */
58910 + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
58911 +} dwc_otg_device_global_regs_t;
58912 +
58913 +/**
58914 + * This union represents the bit fields in the Device Configuration
58915 + * Register. Read the register into the <i>d32</i> member then
58916 + * set/clear the bits using the <i>b</i>it elements. Write the
58917 + * <i>d32</i> member to the dcfg register.
58918 + */
58919 +typedef union dcfg_data {
58920 + /** raw register data */
58921 + uint32_t d32;
58922 + /** register bits */
58923 + struct {
58924 + /** Device Speed */
58925 + unsigned devspd:2;
58926 + /** Non Zero Length Status OUT Handshake */
58927 + unsigned nzstsouthshk:1;
58928 +#define DWC_DCFG_SEND_STALL 1
58929 +
58930 + unsigned ena32khzs:1;
58931 + /** Device Addresses */
58932 + unsigned devaddr:7;
58933 + /** Periodic Frame Interval */
58934 + unsigned perfrint:2;
58935 +#define DWC_DCFG_FRAME_INTERVAL_80 0
58936 +#define DWC_DCFG_FRAME_INTERVAL_85 1
58937 +#define DWC_DCFG_FRAME_INTERVAL_90 2
58938 +#define DWC_DCFG_FRAME_INTERVAL_95 3
58939 +
58940 + /** Enable Device OUT NAK for bulk in DDMA mode */
58941 + unsigned endevoutnak:1;
58942 +
58943 + unsigned reserved14_17:4;
58944 + /** In Endpoint Mis-match count */
58945 + unsigned epmscnt:5;
58946 + /** Enable Descriptor DMA in Device mode */
58947 + unsigned descdma:1;
58948 + unsigned perschintvl:2;
58949 + unsigned resvalid:6;
58950 + } b;
58951 +} dcfg_data_t;
58952 +
58953 +/**
58954 + * This union represents the bit fields in the Device Control
58955 + * Register. Read the register into the <i>d32</i> member then
58956 + * set/clear the bits using the <i>b</i>it elements.
58957 + */
58958 +typedef union dctl_data {
58959 + /** raw register data */
58960 + uint32_t d32;
58961 + /** register bits */
58962 + struct {
58963 + /** Remote Wakeup */
58964 + unsigned rmtwkupsig:1;
58965 + /** Soft Disconnect */
58966 + unsigned sftdiscon:1;
58967 + /** Global Non-Periodic IN NAK Status */
58968 + unsigned gnpinnaksts:1;
58969 + /** Global OUT NAK Status */
58970 + unsigned goutnaksts:1;
58971 + /** Test Control */
58972 + unsigned tstctl:3;
58973 + /** Set Global Non-Periodic IN NAK */
58974 + unsigned sgnpinnak:1;
58975 + /** Clear Global Non-Periodic IN NAK */
58976 + unsigned cgnpinnak:1;
58977 + /** Set Global OUT NAK */
58978 + unsigned sgoutnak:1;
58979 + /** Clear Global OUT NAK */
58980 + unsigned cgoutnak:1;
58981 + /** Power-On Programming Done */
58982 + unsigned pwronprgdone:1;
58983 + /** Reserved */
58984 + unsigned reserved:1;
58985 + /** Global Multi Count */
58986 + unsigned gmc:2;
58987 + /** Ignore Frame Number for ISOC EPs */
58988 + unsigned ifrmnum:1;
58989 + /** NAK on Babble */
58990 + unsigned nakonbble:1;
58991 + /** Enable Continue on BNA */
58992 + unsigned encontonbna:1;
58993 +
58994 + unsigned reserved18_31:14;
58995 + } b;
58996 +} dctl_data_t;
58997 +
58998 +/**
58999 + * This union represents the bit fields in the Device Status
59000 + * Register. Read the register into the <i>d32</i> member then
59001 + * set/clear the bits using the <i>b</i>it elements.
59002 + */
59003 +typedef union dsts_data {
59004 + /** raw register data */
59005 + uint32_t d32;
59006 + /** register bits */
59007 + struct {
59008 + /** Suspend Status */
59009 + unsigned suspsts:1;
59010 + /** Enumerated Speed */
59011 + unsigned enumspd:2;
59012 +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
59013 +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
59014 +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
59015 +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
59016 + /** Erratic Error */
59017 + unsigned errticerr:1;
59018 + unsigned reserved4_7:4;
59019 + /** Frame or Microframe Number of the received SOF */
59020 + unsigned soffn:14;
59021 + unsigned reserved22_31:10;
59022 + } b;
59023 +} dsts_data_t;
59024 +
59025 +/**
59026 + * This union represents the bit fields in the Device IN EP Interrupt
59027 + * Register and the Device IN EP Common Mask Register.
59028 + *
59029 + * - Read the register into the <i>d32</i> member then set/clear the
59030 + * bits using the <i>b</i>it elements.
59031 + */
59032 +typedef union diepint_data {
59033 + /** raw register data */
59034 + uint32_t d32;
59035 + /** register bits */
59036 + struct {
59037 + /** Transfer complete mask */
59038 + unsigned xfercompl:1;
59039 + /** Endpoint disable mask */
59040 + unsigned epdisabled:1;
59041 + /** AHB Error mask */
59042 + unsigned ahberr:1;
59043 + /** TimeOUT Handshake mask (non-ISOC EPs) */
59044 + unsigned timeout:1;
59045 + /** IN Token received with TxF Empty mask */
59046 + unsigned intktxfemp:1;
59047 + /** IN Token Received with EP mismatch mask */
59048 + unsigned intknepmis:1;
59049 + /** IN Endpoint NAK Effective mask */
59050 + unsigned inepnakeff:1;
59051 + /** Reserved */
59052 + unsigned emptyintr:1;
59053 +
59054 + unsigned txfifoundrn:1;
59055 +
59056 + /** BNA Interrupt mask */
59057 + unsigned bna:1;
59058 +
59059 + unsigned reserved10_12:3;
59060 + /** BNA Interrupt mask */
59061 + unsigned nak:1;
59062 +
59063 + unsigned reserved14_31:18;
59064 + } b;
59065 +} diepint_data_t;
59066 +
59067 +/**
59068 + * This union represents the bit fields in the Device IN EP
59069 + * Common/Dedicated Interrupt Mask Register.
59070 + */
59071 +typedef union diepint_data diepmsk_data_t;
59072 +
59073 +/**
59074 + * This union represents the bit fields in the Device OUT EP Interrupt
59075 + * Registerand Device OUT EP Common Interrupt Mask Register.
59076 + *
59077 + * - Read the register into the <i>d32</i> member then set/clear the
59078 + * bits using the <i>b</i>it elements.
59079 + */
59080 +typedef union doepint_data {
59081 + /** raw register data */
59082 + uint32_t d32;
59083 + /** register bits */
59084 + struct {
59085 + /** Transfer complete */
59086 + unsigned xfercompl:1;
59087 + /** Endpoint disable */
59088 + unsigned epdisabled:1;
59089 + /** AHB Error */
59090 + unsigned ahberr:1;
59091 + /** Setup Phase Done (contorl EPs) */
59092 + unsigned setup:1;
59093 + /** OUT Token Received when Endpoint Disabled */
59094 + unsigned outtknepdis:1;
59095 +
59096 + unsigned stsphsercvd:1;
59097 + /** Back-to-Back SETUP Packets Received */
59098 + unsigned back2backsetup:1;
59099 +
59100 + unsigned reserved7:1;
59101 + /** OUT packet Error */
59102 + unsigned outpkterr:1;
59103 + /** BNA Interrupt */
59104 + unsigned bna:1;
59105 +
59106 + unsigned reserved10:1;
59107 + /** Packet Drop Status */
59108 + unsigned pktdrpsts:1;
59109 + /** Babble Interrupt */
59110 + unsigned babble:1;
59111 + /** NAK Interrupt */
59112 + unsigned nak:1;
59113 + /** NYET Interrupt */
59114 + unsigned nyet:1;
59115 + /** Bit indicating setup packet received */
59116 + unsigned sr:1;
59117 +
59118 + unsigned reserved16_31:16;
59119 + } b;
59120 +} doepint_data_t;
59121 +
59122 +/**
59123 + * This union represents the bit fields in the Device OUT EP
59124 + * Common/Dedicated Interrupt Mask Register.
59125 + */
59126 +typedef union doepint_data doepmsk_data_t;
59127 +
59128 +/**
59129 + * This union represents the bit fields in the Device All EP Interrupt
59130 + * and Mask Registers.
59131 + * - Read the register into the <i>d32</i> member then set/clear the
59132 + * bits using the <i>b</i>it elements.
59133 + */
59134 +typedef union daint_data {
59135 + /** raw register data */
59136 + uint32_t d32;
59137 + /** register bits */
59138 + struct {
59139 + /** IN Endpoint bits */
59140 + unsigned in:16;
59141 + /** OUT Endpoint bits */
59142 + unsigned out:16;
59143 + } ep;
59144 + struct {
59145 + /** IN Endpoint bits */
59146 + unsigned inep0:1;
59147 + unsigned inep1:1;
59148 + unsigned inep2:1;
59149 + unsigned inep3:1;
59150 + unsigned inep4:1;
59151 + unsigned inep5:1;
59152 + unsigned inep6:1;
59153 + unsigned inep7:1;
59154 + unsigned inep8:1;
59155 + unsigned inep9:1;
59156 + unsigned inep10:1;
59157 + unsigned inep11:1;
59158 + unsigned inep12:1;
59159 + unsigned inep13:1;
59160 + unsigned inep14:1;
59161 + unsigned inep15:1;
59162 + /** OUT Endpoint bits */
59163 + unsigned outep0:1;
59164 + unsigned outep1:1;
59165 + unsigned outep2:1;
59166 + unsigned outep3:1;
59167 + unsigned outep4:1;
59168 + unsigned outep5:1;
59169 + unsigned outep6:1;
59170 + unsigned outep7:1;
59171 + unsigned outep8:1;
59172 + unsigned outep9:1;
59173 + unsigned outep10:1;
59174 + unsigned outep11:1;
59175 + unsigned outep12:1;
59176 + unsigned outep13:1;
59177 + unsigned outep14:1;
59178 + unsigned outep15:1;
59179 + } b;
59180 +} daint_data_t;
59181 +
59182 +/**
59183 + * This union represents the bit fields in the Device IN Token Queue
59184 + * Read Registers.
59185 + * - Read the register into the <i>d32</i> member.
59186 + * - READ-ONLY Register
59187 + */
59188 +typedef union dtknq1_data {
59189 + /** raw register data */
59190 + uint32_t d32;
59191 + /** register bits */
59192 + struct {
59193 + /** In Token Queue Write Pointer */
59194 + unsigned intknwptr:5;
59195 + /** Reserved */
59196 + unsigned reserved05_06:2;
59197 + /** write pointer has wrapped. */
59198 + unsigned wrap_bit:1;
59199 + /** EP Numbers of IN Tokens 0 ... 4 */
59200 + unsigned epnums0_5:24;
59201 + } b;
59202 +} dtknq1_data_t;
59203 +
59204 +/**
59205 + * This union represents Threshold control Register
59206 + * - Read and write the register into the <i>d32</i> member.
59207 + * - READ-WRITABLE Register
59208 + */
59209 +typedef union dthrctl_data {
59210 + /** raw register data */
59211 + uint32_t d32;
59212 + /** register bits */
59213 + struct {
59214 + /** non ISO Tx Thr. Enable */
59215 + unsigned non_iso_thr_en:1;
59216 + /** ISO Tx Thr. Enable */
59217 + unsigned iso_thr_en:1;
59218 + /** Tx Thr. Length */
59219 + unsigned tx_thr_len:9;
59220 + /** AHB Threshold ratio */
59221 + unsigned ahb_thr_ratio:2;
59222 + /** Reserved */
59223 + unsigned reserved13_15:3;
59224 + /** Rx Thr. Enable */
59225 + unsigned rx_thr_en:1;
59226 + /** Rx Thr. Length */
59227 + unsigned rx_thr_len:9;
59228 + unsigned reserved26:1;
59229 + /** Arbiter Parking Enable*/
59230 + unsigned arbprken:1;
59231 + /** Reserved */
59232 + unsigned reserved28_31:4;
59233 + } b;
59234 +} dthrctl_data_t;
59235 +
59236 +/**
59237 + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
59238 + * 900h-AFCh</i>
59239 + *
59240 + * There will be one set of endpoint registers per logical endpoint
59241 + * implemented.
59242 + *
59243 + * <i>These registers are visible only in Device mode and must not be
59244 + * accessed in Host mode, as the results are unknown.</i>
59245 + */
59246 +typedef struct dwc_otg_dev_in_ep_regs {
59247 + /** Device IN Endpoint Control Register. <i>Offset:900h +
59248 + * (ep_num * 20h) + 00h</i> */
59249 + volatile uint32_t diepctl;
59250 + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
59251 + uint32_t reserved04;
59252 + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
59253 + * (ep_num * 20h) + 08h</i> */
59254 + volatile uint32_t diepint;
59255 + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
59256 + uint32_t reserved0C;
59257 + /** Device IN Endpoint Transfer Size
59258 + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
59259 + volatile uint32_t dieptsiz;
59260 + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
59261 + * (ep_num * 20h) + 14h</i> */
59262 + volatile uint32_t diepdma;
59263 + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
59264 + * (ep_num * 20h) + 18h</i> */
59265 + volatile uint32_t dtxfsts;
59266 + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
59267 + * (ep_num * 20h) + 1Ch</i> */
59268 + volatile uint32_t diepdmab;
59269 +} dwc_otg_dev_in_ep_regs_t;
59270 +
59271 +/**
59272 + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
59273 + * B00h-CFCh</i>
59274 + *
59275 + * There will be one set of endpoint registers per logical endpoint
59276 + * implemented.
59277 + *
59278 + * <i>These registers are visible only in Device mode and must not be
59279 + * accessed in Host mode, as the results are unknown.</i>
59280 + */
59281 +typedef struct dwc_otg_dev_out_ep_regs {
59282 + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
59283 + * (ep_num * 20h) + 00h</i> */
59284 + volatile uint32_t doepctl;
59285 + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
59286 + uint32_t reserved04;
59287 + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
59288 + * (ep_num * 20h) + 08h</i> */
59289 + volatile uint32_t doepint;
59290 + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
59291 + uint32_t reserved0C;
59292 + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
59293 + * B00h + (ep_num * 20h) + 10h</i> */
59294 + volatile uint32_t doeptsiz;
59295 + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
59296 + * + (ep_num * 20h) + 14h</i> */
59297 + volatile uint32_t doepdma;
59298 + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
59299 + uint32_t unused;
59300 + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
59301 + * + (ep_num * 20h) + 1Ch</i> */
59302 + uint32_t doepdmab;
59303 +} dwc_otg_dev_out_ep_regs_t;
59304 +
59305 +/**
59306 + * This union represents the bit fields in the Device EP Control
59307 + * Register. Read the register into the <i>d32</i> member then
59308 + * set/clear the bits using the <i>b</i>it elements.
59309 + */
59310 +typedef union depctl_data {
59311 + /** raw register data */
59312 + uint32_t d32;
59313 + /** register bits */
59314 + struct {
59315 + /** Maximum Packet Size
59316 + * IN/OUT EPn
59317 + * IN/OUT EP0 - 2 bits
59318 + * 2'b00: 64 Bytes
59319 + * 2'b01: 32
59320 + * 2'b10: 16
59321 + * 2'b11: 8 */
59322 + unsigned mps:11;
59323 +#define DWC_DEP0CTL_MPS_64 0
59324 +#define DWC_DEP0CTL_MPS_32 1
59325 +#define DWC_DEP0CTL_MPS_16 2
59326 +#define DWC_DEP0CTL_MPS_8 3
59327 +
59328 + /** Next Endpoint
59329 + * IN EPn/IN EP0
59330 + * OUT EPn/OUT EP0 - reserved */
59331 + unsigned nextep:4;
59332 +
59333 + /** USB Active Endpoint */
59334 + unsigned usbactep:1;
59335 +
59336 + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
59337 + * This field contains the PID of the packet going to
59338 + * be received or transmitted on this endpoint. The
59339 + * application should program the PID of the first
59340 + * packet going to be received or transmitted on this
59341 + * endpoint , after the endpoint is
59342 + * activated. Application use the SetD1PID and
59343 + * SetD0PID fields of this register to program either
59344 + * D0 or D1 PID.
59345 + *
59346 + * The encoding for this field is
59347 + * - 0: D0
59348 + * - 1: D1
59349 + */
59350 + unsigned dpid:1;
59351 +
59352 + /** NAK Status */
59353 + unsigned naksts:1;
59354 +
59355 + /** Endpoint Type
59356 + * 2'b00: Control
59357 + * 2'b01: Isochronous
59358 + * 2'b10: Bulk
59359 + * 2'b11: Interrupt */
59360 + unsigned eptype:2;
59361 +
59362 + /** Snoop Mode
59363 + * OUT EPn/OUT EP0
59364 + * IN EPn/IN EP0 - reserved */
59365 + unsigned snp:1;
59366 +
59367 + /** Stall Handshake */
59368 + unsigned stall:1;
59369 +
59370 + /** Tx Fifo Number
59371 + * IN EPn/IN EP0
59372 + * OUT EPn/OUT EP0 - reserved */
59373 + unsigned txfnum:4;
59374 +
59375 + /** Clear NAK */
59376 + unsigned cnak:1;
59377 + /** Set NAK */
59378 + unsigned snak:1;
59379 + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
59380 + * Writing to this field sets the Endpoint DPID (DPID)
59381 + * field in this register to DATA0. Set Even
59382 + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
59383 + * Writing to this field sets the Even/Odd
59384 + * (micro)frame (EO_FrNum) field to even (micro)
59385 + * frame.
59386 + */
59387 + unsigned setd0pid:1;
59388 + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
59389 + * Writing to this field sets the Endpoint DPID (DPID)
59390 + * field in this register to DATA1 Set Odd
59391 + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
59392 + * Writing to this field sets the Even/Odd
59393 + * (micro)frame (EO_FrNum) field to odd (micro) frame.
59394 + */
59395 + unsigned setd1pid:1;
59396 +
59397 + /** Endpoint Disable */
59398 + unsigned epdis:1;
59399 + /** Endpoint Enable */
59400 + unsigned epena:1;
59401 + } b;
59402 +} depctl_data_t;
59403 +
59404 +/**
59405 + * This union represents the bit fields in the Device EP Transfer
59406 + * Size Register. Read the register into the <i>d32</i> member then
59407 + * set/clear the bits using the <i>b</i>it elements.
59408 + */
59409 +typedef union deptsiz_data {
59410 + /** raw register data */
59411 + uint32_t d32;
59412 + /** register bits */
59413 + struct {
59414 + /** Transfer size */
59415 + unsigned xfersize:19;
59416 +/** Max packet count for EP (pow(2,10)-1) */
59417 +#define MAX_PKT_CNT 1023
59418 + /** Packet Count */
59419 + unsigned pktcnt:10;
59420 + /** Multi Count - Periodic IN endpoints */
59421 + unsigned mc:2;
59422 + unsigned reserved:1;
59423 + } b;
59424 +} deptsiz_data_t;
59425 +
59426 +/**
59427 + * This union represents the bit fields in the Device EP 0 Transfer
59428 + * Size Register. Read the register into the <i>d32</i> member then
59429 + * set/clear the bits using the <i>b</i>it elements.
59430 + */
59431 +typedef union deptsiz0_data {
59432 + /** raw register data */
59433 + uint32_t d32;
59434 + /** register bits */
59435 + struct {
59436 + /** Transfer size */
59437 + unsigned xfersize:7;
59438 + /** Reserved */
59439 + unsigned reserved7_18:12;
59440 + /** Packet Count */
59441 + unsigned pktcnt:2;
59442 + /** Reserved */
59443 + unsigned reserved21_28:8;
59444 + /**Setup Packet Count (DOEPTSIZ0 Only) */
59445 + unsigned supcnt:2;
59446 + unsigned reserved31;
59447 + } b;
59448 +} deptsiz0_data_t;
59449 +
59450 +/////////////////////////////////////////////////
59451 +// DMA Descriptor Specific Structures
59452 +//
59453 +
59454 +/** Buffer status definitions */
59455 +
59456 +#define BS_HOST_READY 0x0
59457 +#define BS_DMA_BUSY 0x1
59458 +#define BS_DMA_DONE 0x2
59459 +#define BS_HOST_BUSY 0x3
59460 +
59461 +/** Receive/Transmit status definitions */
59462 +
59463 +#define RTS_SUCCESS 0x0
59464 +#define RTS_BUFFLUSH 0x1
59465 +#define RTS_RESERVED 0x2
59466 +#define RTS_BUFERR 0x3
59467 +
59468 +/**
59469 + * This union represents the bit fields in the DMA Descriptor
59470 + * status quadlet. Read the quadlet into the <i>d32</i> member then
59471 + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
59472 + * <i>b_iso_in</i> elements.
59473 + */
59474 +typedef union dev_dma_desc_sts {
59475 + /** raw register data */
59476 + uint32_t d32;
59477 + /** quadlet bits */
59478 + struct {
59479 + /** Received number of bytes */
59480 + unsigned bytes:16;
59481 + /** NAK bit - only for OUT EPs */
59482 + unsigned nak:1;
59483 + unsigned reserved17_22:6;
59484 + /** Multiple Transfer - only for OUT EPs */
59485 + unsigned mtrf:1;
59486 + /** Setup Packet received - only for OUT EPs */
59487 + unsigned sr:1;
59488 + /** Interrupt On Complete */
59489 + unsigned ioc:1;
59490 + /** Short Packet */
59491 + unsigned sp:1;
59492 + /** Last */
59493 + unsigned l:1;
59494 + /** Receive Status */
59495 + unsigned sts:2;
59496 + /** Buffer Status */
59497 + unsigned bs:2;
59498 + } b;
59499 +
59500 +//#ifdef DWC_EN_ISOC
59501 + /** iso out quadlet bits */
59502 + struct {
59503 + /** Received number of bytes */
59504 + unsigned rxbytes:11;
59505 +
59506 + unsigned reserved11:1;
59507 + /** Frame Number */
59508 + unsigned framenum:11;
59509 + /** Received ISO Data PID */
59510 + unsigned pid:2;
59511 + /** Interrupt On Complete */
59512 + unsigned ioc:1;
59513 + /** Short Packet */
59514 + unsigned sp:1;
59515 + /** Last */
59516 + unsigned l:1;
59517 + /** Receive Status */
59518 + unsigned rxsts:2;
59519 + /** Buffer Status */
59520 + unsigned bs:2;
59521 + } b_iso_out;
59522 +
59523 + /** iso in quadlet bits */
59524 + struct {
59525 + /** Transmited number of bytes */
59526 + unsigned txbytes:12;
59527 + /** Frame Number */
59528 + unsigned framenum:11;
59529 + /** Transmited ISO Data PID */
59530 + unsigned pid:2;
59531 + /** Interrupt On Complete */
59532 + unsigned ioc:1;
59533 + /** Short Packet */
59534 + unsigned sp:1;
59535 + /** Last */
59536 + unsigned l:1;
59537 + /** Transmit Status */
59538 + unsigned txsts:2;
59539 + /** Buffer Status */
59540 + unsigned bs:2;
59541 + } b_iso_in;
59542 +//#endif /* DWC_EN_ISOC */
59543 +} dev_dma_desc_sts_t;
59544 +
59545 +/**
59546 + * DMA Descriptor structure
59547 + *
59548 + * DMA Descriptor structure contains two quadlets:
59549 + * Status quadlet and Data buffer pointer.
59550 + */
59551 +typedef struct dwc_otg_dev_dma_desc {
59552 + /** DMA Descriptor status quadlet */
59553 + dev_dma_desc_sts_t status;
59554 + /** DMA Descriptor data buffer pointer */
59555 + uint32_t buf;
59556 +} dwc_otg_dev_dma_desc_t;
59557 +
59558 +/**
59559 + * The dwc_otg_dev_if structure contains information needed to manage
59560 + * the DWC_otg controller acting in device mode. It represents the
59561 + * programming view of the device-specific aspects of the controller.
59562 + */
59563 +typedef struct dwc_otg_dev_if {
59564 + /** Pointer to device Global registers.
59565 + * Device Global Registers starting at offset 800h
59566 + */
59567 + dwc_otg_device_global_regs_t *dev_global_regs;
59568 +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
59569 +
59570 + /**
59571 + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
59572 + */
59573 + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
59574 +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
59575 +#define DWC_EP_REG_OFFSET 0x20
59576 +
59577 + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
59578 + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
59579 +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
59580 +
59581 + /* Device configuration information */
59582 + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
59583 + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
59584 + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
59585 +
59586 + /** Size of periodic FIFOs (Bytes) */
59587 + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
59588 +
59589 + /** Size of Tx FIFOs (Bytes) */
59590 + uint16_t tx_fifo_size[MAX_TX_FIFOS];
59591 +
59592 + /** Thresholding enable flags and length varaiables **/
59593 + uint16_t rx_thr_en;
59594 + uint16_t iso_tx_thr_en;
59595 + uint16_t non_iso_tx_thr_en;
59596 +
59597 + uint16_t rx_thr_length;
59598 + uint16_t tx_thr_length;
59599 +
59600 + /**
59601 + * Pointers to the DMA Descriptors for EP0 Control
59602 + * transfers (virtual and physical)
59603 + */
59604 +
59605 + /** 2 descriptors for SETUP packets */
59606 + dwc_dma_t dma_setup_desc_addr[2];
59607 + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
59608 +
59609 + /** Pointer to Descriptor with latest SETUP packet */
59610 + dwc_otg_dev_dma_desc_t *psetup;
59611 +
59612 + /** Index of current SETUP handler descriptor */
59613 + uint32_t setup_desc_index;
59614 +
59615 + /** Descriptor for Data In or Status In phases */
59616 + dwc_dma_t dma_in_desc_addr;
59617 + dwc_otg_dev_dma_desc_t *in_desc_addr;
59618 +
59619 + /** Descriptor for Data Out or Status Out phases */
59620 + dwc_dma_t dma_out_desc_addr;
59621 + dwc_otg_dev_dma_desc_t *out_desc_addr;
59622 +
59623 + /** Setup Packet Detected - if set clear NAK when queueing */
59624 + uint32_t spd;
59625 + /** Isoc ep pointer on which incomplete happens */
59626 + void *isoc_ep;
59627 +
59628 +} dwc_otg_dev_if_t;
59629 +
59630 +/////////////////////////////////////////////////
59631 +// Host Mode Register Structures
59632 +//
59633 +/**
59634 + * The Host Global Registers structure defines the size and relative
59635 + * field offsets for the Host Mode Global Registers. Host Global
59636 + * Registers offsets 400h-7FFh.
59637 +*/
59638 +typedef struct dwc_otg_host_global_regs {
59639 + /** Host Configuration Register. <i>Offset: 400h</i> */
59640 + volatile uint32_t hcfg;
59641 + /** Host Frame Interval Register. <i>Offset: 404h</i> */
59642 + volatile uint32_t hfir;
59643 + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
59644 + volatile uint32_t hfnum;
59645 + /** Reserved. <i>Offset: 40Ch</i> */
59646 + uint32_t reserved40C;
59647 + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
59648 + volatile uint32_t hptxsts;
59649 + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
59650 + volatile uint32_t haint;
59651 + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
59652 + volatile uint32_t haintmsk;
59653 + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
59654 + volatile uint32_t hflbaddr;
59655 +} dwc_otg_host_global_regs_t;
59656 +
59657 +/**
59658 + * This union represents the bit fields in the Host Configuration Register.
59659 + * Read the register into the <i>d32</i> member then set/clear the bits using
59660 + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
59661 + */
59662 +typedef union hcfg_data {
59663 + /** raw register data */
59664 + uint32_t d32;
59665 +
59666 + /** register bits */
59667 + struct {
59668 + /** FS/LS Phy Clock Select */
59669 + unsigned fslspclksel:2;
59670 +#define DWC_HCFG_30_60_MHZ 0
59671 +#define DWC_HCFG_48_MHZ 1
59672 +#define DWC_HCFG_6_MHZ 2
59673 +
59674 + /** FS/LS Only Support */
59675 + unsigned fslssupp:1;
59676 + unsigned reserved3_6:4;
59677 + /** Enable 32-KHz Suspend Mode */
59678 + unsigned ena32khzs:1;
59679 + /** Resume Validation Periiod */
59680 + unsigned resvalid:8;
59681 + unsigned reserved16_22:7;
59682 + /** Enable Scatter/gather DMA in Host mode */
59683 + unsigned descdma:1;
59684 + /** Frame List Entries */
59685 + unsigned frlisten:2;
59686 + /** Enable Periodic Scheduling */
59687 + unsigned perschedena:1;
59688 + unsigned reserved27_30:4;
59689 + unsigned modechtimen:1;
59690 + } b;
59691 +} hcfg_data_t;
59692 +
59693 +/**
59694 + * This union represents the bit fields in the Host Frame Remaing/Number
59695 + * Register.
59696 + */
59697 +typedef union hfir_data {
59698 + /** raw register data */
59699 + uint32_t d32;
59700 +
59701 + /** register bits */
59702 + struct {
59703 + unsigned frint:16;
59704 + unsigned hfirrldctrl:1;
59705 + unsigned reserved:15;
59706 + } b;
59707 +} hfir_data_t;
59708 +
59709 +/**
59710 + * This union represents the bit fields in the Host Frame Remaing/Number
59711 + * Register.
59712 + */
59713 +typedef union hfnum_data {
59714 + /** raw register data */
59715 + uint32_t d32;
59716 +
59717 + /** register bits */
59718 + struct {
59719 + unsigned frnum:16;
59720 +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
59721 + unsigned frrem:16;
59722 + } b;
59723 +} hfnum_data_t;
59724 +
59725 +typedef union hptxsts_data {
59726 + /** raw register data */
59727 + uint32_t d32;
59728 +
59729 + /** register bits */
59730 + struct {
59731 + unsigned ptxfspcavail:16;
59732 + unsigned ptxqspcavail:8;
59733 + /** Top of the Periodic Transmit Request Queue
59734 + * - bit 24 - Terminate (last entry for the selected channel)
59735 + * - bits 26:25 - Token Type
59736 + * - 2'b00 - Zero length
59737 + * - 2'b01 - Ping
59738 + * - 2'b10 - Disable
59739 + * - bits 30:27 - Channel Number
59740 + * - bit 31 - Odd/even microframe
59741 + */
59742 + unsigned ptxqtop_terminate:1;
59743 + unsigned ptxqtop_token:2;
59744 + unsigned ptxqtop_chnum:4;
59745 + unsigned ptxqtop_odd:1;
59746 + } b;
59747 +} hptxsts_data_t;
59748 +
59749 +/**
59750 + * This union represents the bit fields in the Host Port Control and Status
59751 + * Register. Read the register into the <i>d32</i> member then set/clear the
59752 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59753 + * hprt0 register.
59754 + */
59755 +typedef union hprt0_data {
59756 + /** raw register data */
59757 + uint32_t d32;
59758 + /** register bits */
59759 + struct {
59760 + unsigned prtconnsts:1;
59761 + unsigned prtconndet:1;
59762 + unsigned prtena:1;
59763 + unsigned prtenchng:1;
59764 + unsigned prtovrcurract:1;
59765 + unsigned prtovrcurrchng:1;
59766 + unsigned prtres:1;
59767 + unsigned prtsusp:1;
59768 + unsigned prtrst:1;
59769 + unsigned reserved9:1;
59770 + unsigned prtlnsts:2;
59771 + unsigned prtpwr:1;
59772 + unsigned prttstctl:4;
59773 + unsigned prtspd:2;
59774 +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
59775 +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
59776 +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
59777 + unsigned reserved19_31:13;
59778 + } b;
59779 +} hprt0_data_t;
59780 +
59781 +/**
59782 + * This union represents the bit fields in the Host All Interrupt
59783 + * Register.
59784 + */
59785 +typedef union haint_data {
59786 + /** raw register data */
59787 + uint32_t d32;
59788 + /** register bits */
59789 + struct {
59790 + unsigned ch0:1;
59791 + unsigned ch1:1;
59792 + unsigned ch2:1;
59793 + unsigned ch3:1;
59794 + unsigned ch4:1;
59795 + unsigned ch5:1;
59796 + unsigned ch6:1;
59797 + unsigned ch7:1;
59798 + unsigned ch8:1;
59799 + unsigned ch9:1;
59800 + unsigned ch10:1;
59801 + unsigned ch11:1;
59802 + unsigned ch12:1;
59803 + unsigned ch13:1;
59804 + unsigned ch14:1;
59805 + unsigned ch15:1;
59806 + unsigned reserved:16;
59807 + } b;
59808 +
59809 + struct {
59810 + unsigned chint:16;
59811 + unsigned reserved:16;
59812 + } b2;
59813 +} haint_data_t;
59814 +
59815 +/**
59816 + * This union represents the bit fields in the Host All Interrupt
59817 + * Register.
59818 + */
59819 +typedef union haintmsk_data {
59820 + /** raw register data */
59821 + uint32_t d32;
59822 + /** register bits */
59823 + struct {
59824 + unsigned ch0:1;
59825 + unsigned ch1:1;
59826 + unsigned ch2:1;
59827 + unsigned ch3:1;
59828 + unsigned ch4:1;
59829 + unsigned ch5:1;
59830 + unsigned ch6:1;
59831 + unsigned ch7:1;
59832 + unsigned ch8:1;
59833 + unsigned ch9:1;
59834 + unsigned ch10:1;
59835 + unsigned ch11:1;
59836 + unsigned ch12:1;
59837 + unsigned ch13:1;
59838 + unsigned ch14:1;
59839 + unsigned ch15:1;
59840 + unsigned reserved:16;
59841 + } b;
59842 +
59843 + struct {
59844 + unsigned chint:16;
59845 + unsigned reserved:16;
59846 + } b2;
59847 +} haintmsk_data_t;
59848 +
59849 +/**
59850 + * Host Channel Specific Registers. <i>500h-5FCh</i>
59851 + */
59852 +typedef struct dwc_otg_hc_regs {
59853 + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
59854 + volatile uint32_t hcchar;
59855 + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
59856 + volatile uint32_t hcsplt;
59857 + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
59858 + volatile uint32_t hcint;
59859 + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
59860 + volatile uint32_t hcintmsk;
59861 + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
59862 + volatile uint32_t hctsiz;
59863 + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
59864 + volatile uint32_t hcdma;
59865 + volatile uint32_t reserved;
59866 + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
59867 + volatile uint32_t hcdmab;
59868 +} dwc_otg_hc_regs_t;
59869 +
59870 +/**
59871 + * This union represents the bit fields in the Host Channel Characteristics
59872 + * Register. Read the register into the <i>d32</i> member then set/clear the
59873 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59874 + * hcchar register.
59875 + */
59876 +typedef union hcchar_data {
59877 + /** raw register data */
59878 + uint32_t d32;
59879 +
59880 + /** register bits */
59881 + struct {
59882 + /** Maximum packet size in bytes */
59883 + unsigned mps:11;
59884 +
59885 + /** Endpoint number */
59886 + unsigned epnum:4;
59887 +
59888 + /** 0: OUT, 1: IN */
59889 + unsigned epdir:1;
59890 +
59891 + unsigned reserved:1;
59892 +
59893 + /** 0: Full/high speed device, 1: Low speed device */
59894 + unsigned lspddev:1;
59895 +
59896 + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
59897 + unsigned eptype:2;
59898 +
59899 + /** Packets per frame for periodic transfers. 0 is reserved. */
59900 + unsigned multicnt:2;
59901 +
59902 + /** Device address */
59903 + unsigned devaddr:7;
59904 +
59905 + /**
59906 + * Frame to transmit periodic transaction.
59907 + * 0: even, 1: odd
59908 + */
59909 + unsigned oddfrm:1;
59910 +
59911 + /** Channel disable */
59912 + unsigned chdis:1;
59913 +
59914 + /** Channel enable */
59915 + unsigned chen:1;
59916 + } b;
59917 +} hcchar_data_t;
59918 +
59919 +typedef union hcsplt_data {
59920 + /** raw register data */
59921 + uint32_t d32;
59922 +
59923 + /** register bits */
59924 + struct {
59925 + /** Port Address */
59926 + unsigned prtaddr:7;
59927 +
59928 + /** Hub Address */
59929 + unsigned hubaddr:7;
59930 +
59931 + /** Transaction Position */
59932 + unsigned xactpos:2;
59933 +#define DWC_HCSPLIT_XACTPOS_MID 0
59934 +#define DWC_HCSPLIT_XACTPOS_END 1
59935 +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
59936 +#define DWC_HCSPLIT_XACTPOS_ALL 3
59937 +
59938 + /** Do Complete Split */
59939 + unsigned compsplt:1;
59940 +
59941 + /** Reserved */
59942 + unsigned reserved:14;
59943 +
59944 + /** Split Enble */
59945 + unsigned spltena:1;
59946 + } b;
59947 +} hcsplt_data_t;
59948 +
59949 +/**
59950 + * This union represents the bit fields in the Host All Interrupt
59951 + * Register.
59952 + */
59953 +typedef union hcint_data {
59954 + /** raw register data */
59955 + uint32_t d32;
59956 + /** register bits */
59957 + struct {
59958 + /** Transfer Complete */
59959 + unsigned xfercomp:1;
59960 + /** Channel Halted */
59961 + unsigned chhltd:1;
59962 + /** AHB Error */
59963 + unsigned ahberr:1;
59964 + /** STALL Response Received */
59965 + unsigned stall:1;
59966 + /** NAK Response Received */
59967 + unsigned nak:1;
59968 + /** ACK Response Received */
59969 + unsigned ack:1;
59970 + /** NYET Response Received */
59971 + unsigned nyet:1;
59972 + /** Transaction Err */
59973 + unsigned xacterr:1;
59974 + /** Babble Error */
59975 + unsigned bblerr:1;
59976 + /** Frame Overrun */
59977 + unsigned frmovrun:1;
59978 + /** Data Toggle Error */
59979 + unsigned datatglerr:1;
59980 + /** Buffer Not Available (only for DDMA mode) */
59981 + unsigned bna:1;
59982 + /** Exessive transaction error (only for DDMA mode) */
59983 + unsigned xcs_xact:1;
59984 + /** Frame List Rollover interrupt */
59985 + unsigned frm_list_roll:1;
59986 + /** Reserved */
59987 + unsigned reserved14_31:18;
59988 + } b;
59989 +} hcint_data_t;
59990 +
59991 +/**
59992 + * This union represents the bit fields in the Host Channel Interrupt Mask
59993 + * Register. Read the register into the <i>d32</i> member then set/clear the
59994 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59995 + * hcintmsk register.
59996 + */
59997 +typedef union hcintmsk_data {
59998 + /** raw register data */
59999 + uint32_t d32;
60000 +
60001 + /** register bits */
60002 + struct {
60003 + unsigned xfercompl:1;
60004 + unsigned chhltd:1;
60005 + unsigned ahberr:1;
60006 + unsigned stall:1;
60007 + unsigned nak:1;
60008 + unsigned ack:1;
60009 + unsigned nyet:1;
60010 + unsigned xacterr:1;
60011 + unsigned bblerr:1;
60012 + unsigned frmovrun:1;
60013 + unsigned datatglerr:1;
60014 + unsigned bna:1;
60015 + unsigned xcs_xact:1;
60016 + unsigned frm_list_roll:1;
60017 + unsigned reserved14_31:18;
60018 + } b;
60019 +} hcintmsk_data_t;
60020 +
60021 +/**
60022 + * This union represents the bit fields in the Host Channel Transfer Size
60023 + * Register. Read the register into the <i>d32</i> member then set/clear the
60024 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
60025 + * hcchar register.
60026 + */
60027 +
60028 +typedef union hctsiz_data {
60029 + /** raw register data */
60030 + uint32_t d32;
60031 +
60032 + /** register bits */
60033 + struct {
60034 + /** Total transfer size in bytes */
60035 + unsigned xfersize:19;
60036 +
60037 + /** Data packets to transfer */
60038 + unsigned pktcnt:10;
60039 +
60040 + /**
60041 + * Packet ID for next data packet
60042 + * 0: DATA0
60043 + * 1: DATA2
60044 + * 2: DATA1
60045 + * 3: MDATA (non-Control), SETUP (Control)
60046 + */
60047 + unsigned pid:2;
60048 +#define DWC_HCTSIZ_DATA0 0
60049 +#define DWC_HCTSIZ_DATA1 2
60050 +#define DWC_HCTSIZ_DATA2 1
60051 +#define DWC_HCTSIZ_MDATA 3
60052 +#define DWC_HCTSIZ_SETUP 3
60053 +
60054 + /** Do PING protocol when 1 */
60055 + unsigned dopng:1;
60056 + } b;
60057 +
60058 + /** register bits */
60059 + struct {
60060 + /** Scheduling information */
60061 + unsigned schinfo:8;
60062 +
60063 + /** Number of transfer descriptors.
60064 + * Max value:
60065 + * 64 in general,
60066 + * 256 only for HS isochronous endpoint.
60067 + */
60068 + unsigned ntd:8;
60069 +
60070 + /** Data packets to transfer */
60071 + unsigned reserved16_28:13;
60072 +
60073 + /**
60074 + * Packet ID for next data packet
60075 + * 0: DATA0
60076 + * 1: DATA2
60077 + * 2: DATA1
60078 + * 3: MDATA (non-Control)
60079 + */
60080 + unsigned pid:2;
60081 +
60082 + /** Do PING protocol when 1 */
60083 + unsigned dopng:1;
60084 + } b_ddma;
60085 +} hctsiz_data_t;
60086 +
60087 +/**
60088 + * This union represents the bit fields in the Host DMA Address
60089 + * Register used in Descriptor DMA mode.
60090 + */
60091 +typedef union hcdma_data {
60092 + /** raw register data */
60093 + uint32_t d32;
60094 + /** register bits */
60095 + struct {
60096 + unsigned reserved0_2:3;
60097 + /** Current Transfer Descriptor. Not used for ISOC */
60098 + unsigned ctd:8;
60099 + /** Start Address of Descriptor List */
60100 + unsigned dma_addr:21;
60101 + } b;
60102 +} hcdma_data_t;
60103 +
60104 +/**
60105 + * This union represents the bit fields in the DMA Descriptor
60106 + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
60107 + * set/clear the bits using the <i>b</i>it elements.
60108 + */
60109 +typedef union host_dma_desc_sts {
60110 + /** raw register data */
60111 + uint32_t d32;
60112 + /** quadlet bits */
60113 +
60114 + /* for non-isochronous */
60115 + struct {
60116 + /** Number of bytes */
60117 + unsigned n_bytes:17;
60118 + /** QTD offset to jump when Short Packet received - only for IN EPs */
60119 + unsigned qtd_offset:6;
60120 + /**
60121 + * Set to request the core to jump to alternate QTD if
60122 + * Short Packet received - only for IN EPs
60123 + */
60124 + unsigned a_qtd:1;
60125 + /**
60126 + * Setup Packet bit. When set indicates that buffer contains
60127 + * setup packet.
60128 + */
60129 + unsigned sup:1;
60130 + /** Interrupt On Complete */
60131 + unsigned ioc:1;
60132 + /** End of List */
60133 + unsigned eol:1;
60134 + unsigned reserved27:1;
60135 + /** Rx/Tx Status */
60136 + unsigned sts:2;
60137 +#define DMA_DESC_STS_PKTERR 1
60138 + unsigned reserved30:1;
60139 + /** Active Bit */
60140 + unsigned a:1;
60141 + } b;
60142 + /* for isochronous */
60143 + struct {
60144 + /** Number of bytes */
60145 + unsigned n_bytes:12;
60146 + unsigned reserved12_24:13;
60147 + /** Interrupt On Complete */
60148 + unsigned ioc:1;
60149 + unsigned reserved26_27:2;
60150 + /** Rx/Tx Status */
60151 + unsigned sts:2;
60152 + unsigned reserved30:1;
60153 + /** Active Bit */
60154 + unsigned a:1;
60155 + } b_isoc;
60156 +} host_dma_desc_sts_t;
60157 +
60158 +#define MAX_DMA_DESC_SIZE 131071
60159 +#define MAX_DMA_DESC_NUM_GENERIC 64
60160 +#define MAX_DMA_DESC_NUM_HS_ISOC 256
60161 +#define MAX_FRLIST_EN_NUM 64
60162 +/**
60163 + * Host-mode DMA Descriptor structure
60164 + *
60165 + * DMA Descriptor structure contains two quadlets:
60166 + * Status quadlet and Data buffer pointer.
60167 + */
60168 +typedef struct dwc_otg_host_dma_desc {
60169 + /** DMA Descriptor status quadlet */
60170 + host_dma_desc_sts_t status;
60171 + /** DMA Descriptor data buffer pointer */
60172 + uint32_t buf;
60173 +} dwc_otg_host_dma_desc_t;
60174 +
60175 +/** OTG Host Interface Structure.
60176 + *
60177 + * The OTG Host Interface Structure structure contains information
60178 + * needed to manage the DWC_otg controller acting in host mode. It
60179 + * represents the programming view of the host-specific aspects of the
60180 + * controller.
60181 + */
60182 +typedef struct dwc_otg_host_if {
60183 + /** Host Global Registers starting at offset 400h.*/
60184 + dwc_otg_host_global_regs_t *host_global_regs;
60185 +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
60186 +
60187 + /** Host Port 0 Control and Status Register */
60188 + volatile uint32_t *hprt0;
60189 +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
60190 +
60191 + /** Host Channel Specific Registers at offsets 500h-5FCh. */
60192 + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
60193 +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
60194 +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
60195 +
60196 + /* Host configuration information */
60197 + /** Number of Host Channels (range: 1-16) */
60198 + uint8_t num_host_channels;
60199 + /** Periodic EPs supported (0: no, 1: yes) */
60200 + uint8_t perio_eps_supported;
60201 + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
60202 + uint16_t perio_tx_fifo_size;
60203 +
60204 +} dwc_otg_host_if_t;
60205 +
60206 +/**
60207 + * This union represents the bit fields in the Power and Clock Gating Control
60208 + * Register. Read the register into the <i>d32</i> member then set/clear the
60209 + * bits using the <i>b</i>it elements.
60210 + */
60211 +typedef union pcgcctl_data {
60212 + /** raw register data */
60213 + uint32_t d32;
60214 +
60215 + /** register bits */
60216 + struct {
60217 + /** Stop Pclk */
60218 + unsigned stoppclk:1;
60219 + /** Gate Hclk */
60220 + unsigned gatehclk:1;
60221 + /** Power Clamp */
60222 + unsigned pwrclmp:1;
60223 + /** Reset Power Down Modules */
60224 + unsigned rstpdwnmodule:1;
60225 + /** Reserved */
60226 + unsigned reserved:1;
60227 + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
60228 + unsigned enbl_sleep_gating:1;
60229 + /** PHY In Sleep (PhySleep) */
60230 + unsigned phy_in_sleep:1;
60231 + /** Deep Sleep*/
60232 + unsigned deep_sleep:1;
60233 + unsigned resetaftsusp:1;
60234 + unsigned restoremode:1;
60235 + unsigned enbl_extnd_hiber:1;
60236 + unsigned extnd_hiber_pwrclmp:1;
60237 + unsigned extnd_hiber_switch:1;
60238 + unsigned ess_reg_restored:1;
60239 + unsigned prt_clk_sel:2;
60240 + unsigned port_power:1;
60241 + unsigned max_xcvrselect:2;
60242 + unsigned max_termsel:1;
60243 + unsigned mac_dev_addr:7;
60244 + unsigned p2hd_dev_enum_spd:2;
60245 + unsigned p2hd_prt_spd:2;
60246 + unsigned if_dev_mode:1;
60247 + } b;
60248 +} pcgcctl_data_t;
60249 +
60250 +/**
60251 + * This union represents the bit fields in the Global Data FIFO Software
60252 + * Configuration Register. Read the register into the <i>d32</i> member then
60253 + * set/clear the bits using the <i>b</i>it elements.
60254 + */
60255 +typedef union gdfifocfg_data {
60256 + /* raw register data */
60257 + uint32_t d32;
60258 + /** register bits */
60259 + struct {
60260 + /** OTG Data FIFO depth */
60261 + unsigned gdfifocfg:16;
60262 + /** Start address of EP info controller */
60263 + unsigned epinfobase:16;
60264 + } b;
60265 +} gdfifocfg_data_t;
60266 +
60267 +/**
60268 + * This union represents the bit fields in the Global Power Down Register
60269 + * Register. Read the register into the <i>d32</i> member then set/clear the
60270 + * bits using the <i>b</i>it elements.
60271 + */
60272 +typedef union gpwrdn_data {
60273 + /* raw register data */
60274 + uint32_t d32;
60275 +
60276 + /** register bits */
60277 + struct {
60278 + /** PMU Interrupt Select */
60279 + unsigned pmuintsel:1;
60280 + /** PMU Active */
60281 + unsigned pmuactv:1;
60282 + /** Restore */
60283 + unsigned restore:1;
60284 + /** Power Down Clamp */
60285 + unsigned pwrdnclmp:1;
60286 + /** Power Down Reset */
60287 + unsigned pwrdnrstn:1;
60288 + /** Power Down Switch */
60289 + unsigned pwrdnswtch:1;
60290 + /** Disable VBUS */
60291 + unsigned dis_vbus:1;
60292 + /** Line State Change */
60293 + unsigned lnstschng:1;
60294 + /** Line state change mask */
60295 + unsigned lnstchng_msk:1;
60296 + /** Reset Detected */
60297 + unsigned rst_det:1;
60298 + /** Reset Detect mask */
60299 + unsigned rst_det_msk:1;
60300 + /** Disconnect Detected */
60301 + unsigned disconn_det:1;
60302 + /** Disconnect Detect mask */
60303 + unsigned disconn_det_msk:1;
60304 + /** Connect Detected*/
60305 + unsigned connect_det:1;
60306 + /** Connect Detected Mask*/
60307 + unsigned connect_det_msk:1;
60308 + /** SRP Detected */
60309 + unsigned srp_det:1;
60310 + /** SRP Detect mask */
60311 + unsigned srp_det_msk:1;
60312 + /** Status Change Interrupt */
60313 + unsigned sts_chngint:1;
60314 + /** Status Change Interrupt Mask */
60315 + unsigned sts_chngint_msk:1;
60316 + /** Line State */
60317 + unsigned linestate:2;
60318 + /** Indicates current mode(status of IDDIG signal) */
60319 + unsigned idsts:1;
60320 + /** B Session Valid signal status*/
60321 + unsigned bsessvld:1;
60322 + /** ADP Event Detected */
60323 + unsigned adp_int:1;
60324 + /** Multi Valued ID pin */
60325 + unsigned mult_val_id_bc:5;
60326 + /** Reserved 24_31 */
60327 + unsigned reserved29_31:3;
60328 + } b;
60329 +} gpwrdn_data_t;
60330 +
60331 +#endif
60332 --- /dev/null
60333 +++ b/drivers/usb/host/dwc_otg/test/Makefile
60334 @@ -0,0 +1,16 @@
60335 +
60336 +PERL=/usr/bin/perl
60337 +PL_TESTS=test_sysfs.pl test_mod_param.pl
60338 +
60339 +.PHONY : test
60340 +test : perl_tests
60341 +
60342 +perl_tests :
60343 + @echo
60344 + @echo Running perl tests
60345 + @for test in $(PL_TESTS); do \
60346 + if $(PERL) ./$$test ; then \
60347 + echo "=======> $$test, PASSED" ; \
60348 + else echo "=======> $$test, FAILED" ; \
60349 + fi \
60350 + done
60351 --- /dev/null
60352 +++ b/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
60353 @@ -0,0 +1,337 @@
60354 +package dwc_otg_test;
60355 +
60356 +use strict;
60357 +use Exporter ();
60358 +
60359 +use vars qw(@ISA @EXPORT
60360 +$sysfsdir $paramdir $errors $params
60361 +);
60362 +
60363 +@ISA = qw(Exporter);
60364 +
60365 +#
60366 +# Globals
60367 +#
60368 +$sysfsdir = "/sys/devices/lm0";
60369 +$paramdir = "/sys/module/dwc_otg";
60370 +$errors = 0;
60371 +
60372 +$params = [
60373 + {
60374 + NAME => "otg_cap",
60375 + DEFAULT => 0,
60376 + ENUM => [],
60377 + LOW => 0,
60378 + HIGH => 2
60379 + },
60380 + {
60381 + NAME => "dma_enable",
60382 + DEFAULT => 0,
60383 + ENUM => [],
60384 + LOW => 0,
60385 + HIGH => 1
60386 + },
60387 + {
60388 + NAME => "dma_burst_size",
60389 + DEFAULT => 32,
60390 + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
60391 + LOW => 1,
60392 + HIGH => 256
60393 + },
60394 + {
60395 + NAME => "host_speed",
60396 + DEFAULT => 0,
60397 + ENUM => [],
60398 + LOW => 0,
60399 + HIGH => 1
60400 + },
60401 + {
60402 + NAME => "host_support_fs_ls_low_power",
60403 + DEFAULT => 0,
60404 + ENUM => [],
60405 + LOW => 0,
60406 + HIGH => 1
60407 + },
60408 + {
60409 + NAME => "host_ls_low_power_phy_clk",
60410 + DEFAULT => 0,
60411 + ENUM => [],
60412 + LOW => 0,
60413 + HIGH => 1
60414 + },
60415 + {
60416 + NAME => "dev_speed",
60417 + DEFAULT => 0,
60418 + ENUM => [],
60419 + LOW => 0,
60420 + HIGH => 1
60421 + },
60422 + {
60423 + NAME => "enable_dynamic_fifo",
60424 + DEFAULT => 1,
60425 + ENUM => [],
60426 + LOW => 0,
60427 + HIGH => 1
60428 + },
60429 + {
60430 + NAME => "data_fifo_size",
60431 + DEFAULT => 8192,
60432 + ENUM => [],
60433 + LOW => 32,
60434 + HIGH => 32768
60435 + },
60436 + {
60437 + NAME => "dev_rx_fifo_size",
60438 + DEFAULT => 1064,
60439 + ENUM => [],
60440 + LOW => 16,
60441 + HIGH => 32768
60442 + },
60443 + {
60444 + NAME => "dev_nperio_tx_fifo_size",
60445 + DEFAULT => 1024,
60446 + ENUM => [],
60447 + LOW => 16,
60448 + HIGH => 32768
60449 + },
60450 + {
60451 + NAME => "dev_perio_tx_fifo_size_1",
60452 + DEFAULT => 256,
60453 + ENUM => [],
60454 + LOW => 4,
60455 + HIGH => 768
60456 + },
60457 + {
60458 + NAME => "dev_perio_tx_fifo_size_2",
60459 + DEFAULT => 256,
60460 + ENUM => [],
60461 + LOW => 4,
60462 + HIGH => 768
60463 + },
60464 + {
60465 + NAME => "dev_perio_tx_fifo_size_3",
60466 + DEFAULT => 256,
60467 + ENUM => [],
60468 + LOW => 4,
60469 + HIGH => 768
60470 + },
60471 + {
60472 + NAME => "dev_perio_tx_fifo_size_4",
60473 + DEFAULT => 256,
60474 + ENUM => [],
60475 + LOW => 4,
60476 + HIGH => 768
60477 + },
60478 + {
60479 + NAME => "dev_perio_tx_fifo_size_5",
60480 + DEFAULT => 256,
60481 + ENUM => [],
60482 + LOW => 4,
60483 + HIGH => 768
60484 + },
60485 + {
60486 + NAME => "dev_perio_tx_fifo_size_6",
60487 + DEFAULT => 256,
60488 + ENUM => [],
60489 + LOW => 4,
60490 + HIGH => 768
60491 + },
60492 + {
60493 + NAME => "dev_perio_tx_fifo_size_7",
60494 + DEFAULT => 256,
60495 + ENUM => [],
60496 + LOW => 4,
60497 + HIGH => 768
60498 + },
60499 + {
60500 + NAME => "dev_perio_tx_fifo_size_8",
60501 + DEFAULT => 256,
60502 + ENUM => [],
60503 + LOW => 4,
60504 + HIGH => 768
60505 + },
60506 + {
60507 + NAME => "dev_perio_tx_fifo_size_9",
60508 + DEFAULT => 256,
60509 + ENUM => [],
60510 + LOW => 4,
60511 + HIGH => 768
60512 + },
60513 + {
60514 + NAME => "dev_perio_tx_fifo_size_10",
60515 + DEFAULT => 256,
60516 + ENUM => [],
60517 + LOW => 4,
60518 + HIGH => 768
60519 + },
60520 + {
60521 + NAME => "dev_perio_tx_fifo_size_11",
60522 + DEFAULT => 256,
60523 + ENUM => [],
60524 + LOW => 4,
60525 + HIGH => 768
60526 + },
60527 + {
60528 + NAME => "dev_perio_tx_fifo_size_12",
60529 + DEFAULT => 256,
60530 + ENUM => [],
60531 + LOW => 4,
60532 + HIGH => 768
60533 + },
60534 + {
60535 + NAME => "dev_perio_tx_fifo_size_13",
60536 + DEFAULT => 256,
60537 + ENUM => [],
60538 + LOW => 4,
60539 + HIGH => 768
60540 + },
60541 + {
60542 + NAME => "dev_perio_tx_fifo_size_14",
60543 + DEFAULT => 256,
60544 + ENUM => [],
60545 + LOW => 4,
60546 + HIGH => 768
60547 + },
60548 + {
60549 + NAME => "dev_perio_tx_fifo_size_15",
60550 + DEFAULT => 256,
60551 + ENUM => [],
60552 + LOW => 4,
60553 + HIGH => 768
60554 + },
60555 + {
60556 + NAME => "host_rx_fifo_size",
60557 + DEFAULT => 1024,
60558 + ENUM => [],
60559 + LOW => 16,
60560 + HIGH => 32768
60561 + },
60562 + {
60563 + NAME => "host_nperio_tx_fifo_size",
60564 + DEFAULT => 1024,
60565 + ENUM => [],
60566 + LOW => 16,
60567 + HIGH => 32768
60568 + },
60569 + {
60570 + NAME => "host_perio_tx_fifo_size",
60571 + DEFAULT => 1024,
60572 + ENUM => [],
60573 + LOW => 16,
60574 + HIGH => 32768
60575 + },
60576 + {
60577 + NAME => "max_transfer_size",
60578 + DEFAULT => 65535,
60579 + ENUM => [],
60580 + LOW => 2047,
60581 + HIGH => 65535
60582 + },
60583 + {
60584 + NAME => "max_packet_count",
60585 + DEFAULT => 511,
60586 + ENUM => [],
60587 + LOW => 15,
60588 + HIGH => 511
60589 + },
60590 + {
60591 + NAME => "host_channels",
60592 + DEFAULT => 12,
60593 + ENUM => [],
60594 + LOW => 1,
60595 + HIGH => 16
60596 + },
60597 + {
60598 + NAME => "dev_endpoints",
60599 + DEFAULT => 6,
60600 + ENUM => [],
60601 + LOW => 1,
60602 + HIGH => 15
60603 + },
60604 + {
60605 + NAME => "phy_type",
60606 + DEFAULT => 1,
60607 + ENUM => [],
60608 + LOW => 0,
60609 + HIGH => 2
60610 + },
60611 + {
60612 + NAME => "phy_utmi_width",
60613 + DEFAULT => 16,
60614 + ENUM => [8, 16],
60615 + LOW => 8,
60616 + HIGH => 16
60617 + },
60618 + {
60619 + NAME => "phy_ulpi_ddr",
60620 + DEFAULT => 0,
60621 + ENUM => [],
60622 + LOW => 0,
60623 + HIGH => 1
60624 + },
60625 + ];
60626 +
60627 +
60628 +#
60629 +#
60630 +sub check_arch {
60631 + $_ = `uname -m`;
60632 + chomp;
60633 + unless (m/armv4tl/) {
60634 + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
60635 + return 0;
60636 + }
60637 + return 1;
60638 +}
60639 +
60640 +#
60641 +#
60642 +sub load_module {
60643 + my $params = shift;
60644 + print "\nRemoving Module\n";
60645 + system "rmmod dwc_otg";
60646 + print "Loading Module\n";
60647 + if ($params ne "") {
60648 + print "Module Parameters: $params\n";
60649 + }
60650 + if (system("modprobe dwc_otg $params")) {
60651 + warn "Unable to load module\n";
60652 + return 0;
60653 + }
60654 + return 1;
60655 +}
60656 +
60657 +#
60658 +#
60659 +sub test_status {
60660 + my $arg = shift;
60661 +
60662 + print "\n";
60663 +
60664 + if (defined $arg) {
60665 + warn "WARNING: $arg\n";
60666 + }
60667 +
60668 + if ($errors > 0) {
60669 + warn "TEST FAILED with $errors errors\n";
60670 + return 0;
60671 + } else {
60672 + print "TEST PASSED\n";
60673 + return 0 if (defined $arg);
60674 + }
60675 + return 1;
60676 +}
60677 +
60678 +#
60679 +#
60680 +@EXPORT = qw(
60681 +$sysfsdir
60682 +$paramdir
60683 +$params
60684 +$errors
60685 +check_arch
60686 +load_module
60687 +test_status
60688 +);
60689 +
60690 +1;
60691 --- /dev/null
60692 +++ b/drivers/usb/host/dwc_otg/test/test_mod_param.pl
60693 @@ -0,0 +1,133 @@
60694 +#!/usr/bin/perl -w
60695 +#
60696 +# Run this program on the integrator.
60697 +#
60698 +# - Tests module parameter default values.
60699 +# - Tests setting of valid module parameter values via modprobe.
60700 +# - Tests invalid module parameter values.
60701 +# -----------------------------------------------------------------------------
60702 +use strict;
60703 +use dwc_otg_test;
60704 +
60705 +check_arch() or die;
60706 +
60707 +#
60708 +#
60709 +sub test {
60710 + my ($param,$expected) = @_;
60711 + my $value = get($param);
60712 +
60713 + if ($value == $expected) {
60714 + print "$param = $value, okay\n";
60715 + }
60716 +
60717 + else {
60718 + warn "ERROR: value of $param != $expected, $value\n";
60719 + $errors ++;
60720 + }
60721 +}
60722 +
60723 +#
60724 +#
60725 +sub get {
60726 + my $param = shift;
60727 + my $tmp = `cat $paramdir/$param`;
60728 + chomp $tmp;
60729 + return $tmp;
60730 +}
60731 +
60732 +#
60733 +#
60734 +sub test_main {
60735 +
60736 + print "\nTesting Module Parameters\n";
60737 +
60738 + load_module("") or die;
60739 +
60740 + # Test initial values
60741 + print "\nTesting Default Values\n";
60742 + foreach (@{$params}) {
60743 + test ($_->{NAME}, $_->{DEFAULT});
60744 + }
60745 +
60746 + # Test low value
60747 + print "\nTesting Low Value\n";
60748 + my $cmd_params = "";
60749 + foreach (@{$params}) {
60750 + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
60751 + }
60752 + load_module($cmd_params) or die;
60753 +
60754 + foreach (@{$params}) {
60755 + test ($_->{NAME}, $_->{LOW});
60756 + }
60757 +
60758 + # Test high value
60759 + print "\nTesting High Value\n";
60760 + $cmd_params = "";
60761 + foreach (@{$params}) {
60762 + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
60763 + }
60764 + load_module($cmd_params) or die;
60765 +
60766 + foreach (@{$params}) {
60767 + test ($_->{NAME}, $_->{HIGH});
60768 + }
60769 +
60770 + # Test Enum
60771 + print "\nTesting Enumerated\n";
60772 + foreach (@{$params}) {
60773 + if (defined $_->{ENUM}) {
60774 + my $value;
60775 + foreach $value (@{$_->{ENUM}}) {
60776 + $cmd_params = "$_->{NAME}=$value";
60777 + load_module($cmd_params) or die;
60778 + test ($_->{NAME}, $value);
60779 + }
60780 + }
60781 + }
60782 +
60783 + # Test Invalid Values
60784 + print "\nTesting Invalid Values\n";
60785 + $cmd_params = "";
60786 + foreach (@{$params}) {
60787 + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
60788 + }
60789 + load_module($cmd_params) or die;
60790 +
60791 + foreach (@{$params}) {
60792 + test ($_->{NAME}, $_->{DEFAULT});
60793 + }
60794 +
60795 + $cmd_params = "";
60796 + foreach (@{$params}) {
60797 + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
60798 + }
60799 + load_module($cmd_params) or die;
60800 +
60801 + foreach (@{$params}) {
60802 + test ($_->{NAME}, $_->{DEFAULT});
60803 + }
60804 +
60805 + print "\nTesting Enumerated\n";
60806 + foreach (@{$params}) {
60807 + if (defined $_->{ENUM}) {
60808 + my $value;
60809 + foreach $value (@{$_->{ENUM}}) {
60810 + $value = $value + 1;
60811 + $cmd_params = "$_->{NAME}=$value";
60812 + load_module($cmd_params) or die;
60813 + test ($_->{NAME}, $_->{DEFAULT});
60814 + $value = $value - 2;
60815 + $cmd_params = "$_->{NAME}=$value";
60816 + load_module($cmd_params) or die;
60817 + test ($_->{NAME}, $_->{DEFAULT});
60818 + }
60819 + }
60820 + }
60821 +
60822 + test_status() or die;
60823 +}
60824 +
60825 +test_main();
60826 +0;
60827 --- /dev/null
60828 +++ b/drivers/usb/host/dwc_otg/test/test_sysfs.pl
60829 @@ -0,0 +1,193 @@
60830 +#!/usr/bin/perl -w
60831 +#
60832 +# Run this program on the integrator
60833 +# - Tests select sysfs attributes.
60834 +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
60835 +# -----------------------------------------------------------------------------
60836 +use strict;
60837 +use dwc_otg_test;
60838 +
60839 +check_arch() or die;
60840 +
60841 +#
60842 +#
60843 +sub test {
60844 + my ($attr,$expected) = @_;
60845 + my $string = get($attr);
60846 +
60847 + if ($string eq $expected) {
60848 + printf("$attr = $string, okay\n");
60849 + }
60850 + else {
60851 + warn "ERROR: value of $attr != $expected, $string\n";
60852 + $errors ++;
60853 + }
60854 +}
60855 +
60856 +#
60857 +#
60858 +sub set {
60859 + my ($reg, $value) = @_;
60860 + system "echo $value > $sysfsdir/$reg";
60861 +}
60862 +
60863 +#
60864 +#
60865 +sub get {
60866 + my $attr = shift;
60867 + my $string = `cat $sysfsdir/$attr`;
60868 + chomp $string;
60869 + if ($string =~ m/\s\=\s/) {
60870 + my $tmp;
60871 + ($tmp, $string) = split /\s=\s/, $string;
60872 + }
60873 + return $string;
60874 +}
60875 +
60876 +#
60877 +#
60878 +sub test_main {
60879 + print("\nTesting Sysfs Attributes\n");
60880 +
60881 + load_module("") or die;
60882 +
60883 + # Test initial values of regoffset/regvalue/guid/gsnpsid
60884 + print("\nTesting Default Values\n");
60885 +
60886 + test("regoffset", "0xffffffff");
60887 + test("regvalue", "invalid offset");
60888 + test("guid", "0x12345678"); # this will fail if it has been changed
60889 + test("gsnpsid", "0x4f54200a");
60890 +
60891 + # Test operation of regoffset/regvalue
60892 + print("\nTesting regoffset\n");
60893 + set('regoffset', '5a5a5a5a');
60894 + test("regoffset", "0xffffffff");
60895 +
60896 + set('regoffset', '0');
60897 + test("regoffset", "0x00000000");
60898 +
60899 + set('regoffset', '40000');
60900 + test("regoffset", "0x00000000");
60901 +
60902 + set('regoffset', '3ffff');
60903 + test("regoffset", "0x0003ffff");
60904 +
60905 + set('regoffset', '1');
60906 + test("regoffset", "0x00000001");
60907 +
60908 + print("\nTesting regvalue\n");
60909 + set('regoffset', '3c');
60910 + test("regvalue", "0x12345678");
60911 + set('regvalue', '5a5a5a5a');
60912 + test("regvalue", "0x5a5a5a5a");
60913 + set('regvalue','a5a5a5a5');
60914 + test("regvalue", "0xa5a5a5a5");
60915 + set('guid','12345678');
60916 +
60917 + # Test HNP Capable
60918 + print("\nTesting HNP Capable bit\n");
60919 + set('hnpcapable', '1');
60920 + test("hnpcapable", "0x1");
60921 + set('hnpcapable','0');
60922 + test("hnpcapable", "0x0");
60923 +
60924 + set('regoffset','0c');
60925 +
60926 + my $old = get('gusbcfg');
60927 + print("setting hnpcapable\n");
60928 + set('hnpcapable', '1');
60929 + test("hnpcapable", "0x1");
60930 + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
60931 + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
60932 +
60933 + $old = get('gusbcfg');
60934 + print("clearing hnpcapable\n");
60935 + set('hnpcapable', '0');
60936 + test("hnpcapable", "0x0");
60937 + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
60938 + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
60939 +
60940 + # Test SRP Capable
60941 + print("\nTesting SRP Capable bit\n");
60942 + set('srpcapable', '1');
60943 + test("srpcapable", "0x1");
60944 + set('srpcapable','0');
60945 + test("srpcapable", "0x0");
60946 +
60947 + set('regoffset','0c');
60948 +
60949 + $old = get('gusbcfg');
60950 + print("setting srpcapable\n");
60951 + set('srpcapable', '1');
60952 + test("srpcapable", "0x1");
60953 + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
60954 + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
60955 +
60956 + $old = get('gusbcfg');
60957 + print("clearing srpcapable\n");
60958 + set('srpcapable', '0');
60959 + test("srpcapable", "0x0");
60960 + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
60961 + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
60962 +
60963 + # Test GGPIO
60964 + print("\nTesting GGPIO\n");
60965 + set('ggpio','5a5a5a5a');
60966 + test('ggpio','0x5a5a0000');
60967 + set('ggpio','a5a5a5a5');
60968 + test('ggpio','0xa5a50000');
60969 + set('ggpio','11110000');
60970 + test('ggpio','0x11110000');
60971 + set('ggpio','00001111');
60972 + test('ggpio','0x00000000');
60973 +
60974 + # Test DEVSPEED
60975 + print("\nTesting DEVSPEED\n");
60976 + set('regoffset','800');
60977 + $old = get('regvalue');
60978 + set('devspeed','0');
60979 + test('devspeed','0x0');
60980 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
60981 + set('devspeed','1');
60982 + test('devspeed','0x1');
60983 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
60984 + set('devspeed','2');
60985 + test('devspeed','0x2');
60986 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
60987 + set('devspeed','3');
60988 + test('devspeed','0x3');
60989 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
60990 + set('devspeed','4');
60991 + test('devspeed','0x0');
60992 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
60993 + set('devspeed','5');
60994 + test('devspeed','0x1');
60995 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
60996 +
60997 +
60998 + # mode Returns the current mode:0 for device mode1 for host mode Read
60999 + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
61000 + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
61001 + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
61002 + # bussuspend Suspend the USB bus. Read/Write
61003 + # busconnected Get the connection status of the bus Read
61004 +
61005 + # gotgctl Get or set the Core Control Status Register. Read/Write
61006 + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
61007 + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
61008 + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
61009 + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
61010 + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
61011 + ## guid Get or set the value of the User ID Register Read/Write
61012 + ## gsnpsid Get the value of the Synopsys ID Regester Read
61013 + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
61014 + # enumspeed Gets the device enumeration Speed. Read
61015 + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
61016 + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
61017 +
61018 + test_status("TEST NYI") or die;
61019 +}
61020 +
61021 +test_main();
61022 +0;